Adjust whitespace in if statements.
[toast/confclerk.git] / src / gui / searchtabcontainer.cpp
index b0938a6a81544282b72fd007c39b937ad6cd72d8..3c3d7bdd326c42cef91b777c04023ec61864a5b0 100644 (file)
@@ -1,10 +1,28 @@
+/*
+ * Copyright (C) 2010 Ixonos Plc.
+ * Copyright (C) 2011-2017 Philipp Spitzer, gregor herrmann, Stefan Stahl
+ *
+ * This file is part of ConfClerk.
+ *
+ * ConfClerk is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation, either version 2 of the License, or (at your option)
+ * any later version.
+ *
+ * ConfClerk is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * ConfClerk.  If not, see <http://www.gnu.org/licenses/>.
+ */
 
 #include "searchtabcontainer.h"
 #include "searchhead.h"
+#include <QMessageBox>
 
-
-SearchTabContainer::SearchTabContainer(QWidget *aParent) : TabContainer( aParent )
-{
+SearchTabContainer::SearchTabContainer(QWidget *aParent): TabContainer(aParent), sqlEngine(0) {
     header = new SearchHead(this);
     header->setObjectName(QString::fromUtf8("header"));
     QSizePolicy sizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Expanding);
@@ -14,90 +32,79 @@ SearchTabContainer::SearchTabContainer(QWidget *aParent) : TabContainer( aParent
     sizePolicy.setHeightForWidth(header->sizePolicy().hasHeightForWidth());
     header->setSizePolicy(sizePolicy);
     header->setMinimumSize(QSize(10, 10));
-
     verticalLayout->insertWidget(0,header);
+    connect(header, SIGNAL(searchClicked()), SLOT(searchButtonClicked()));
+    showSearchDialog();
+}
 
-    header->show();
-
-    searchAgainButton = new QToolButton(this);
-    searchAgainButton->setObjectName(QString::fromUtf8("button"));
-    QIcon icon;
-    icon.addPixmap(QPixmap(QString::fromUtf8(":/icons/search.png")), QIcon::Normal, QIcon::Off);
-    searchAgainButton->setIcon(icon);
-    QSizePolicy sizePolicy1(QSizePolicy::Minimum, QSizePolicy::Minimum);
-    sizePolicy1.setHorizontalStretch(0);
-    sizePolicy1.setVerticalStretch(0);
-    sizePolicy1.setHeightForWidth(searchAgainButton->sizePolicy().hasHeightForWidth());
-    searchAgainButton->setSizePolicy(sizePolicy1);
 
-    verticalLayout_2->insertWidget(0,searchAgainButton);
+bool SearchTabContainer::searchDialogIsVisible() const {
+    return header->isVisible();
+}
 
-    searchAgainButton->hide();
-    treeView->hide();
 
-    connect( header, SIGNAL(searchClicked()), SLOT(searchButtonClicked()));
-    connect( searchAgainButton, SIGNAL(clicked()), SLOT(searchAgainClicked()));
+int SearchTabContainer::searchResultCount(const QDate& date) const {
+    int confId = Conference::activeConference();
+    if (confId == -1) return 0;
+    return Event::getSearchResultByDate(date, confId, "start, duration").count();
 }
 
-SearchTabContainer::~SearchTabContainer()
-{
+
+void SearchTabContainer::showSearchDialog(bool show) {
+    header->setVisible(show);
+    treeView->setVisible(!show);
+    if (show) header->searchEdit->setFocus(Qt::OtherFocusReason);
 }
 
-void SearchTabContainer::searchButtonClicked()
-{
-    qDebug() << "SearchTab::searchButtonClicked()";
 
-    QHash<QString,QString> columns;
+void SearchTabContainer::searchButtonClicked() {
+    if (!sqlEngine) return;
+
+    QMultiHash<QString,QString> columns;
 
     SearchHead *searchHeader = static_cast<SearchHead*>(header);
-    if( searchHeader->searchTitle->isChecked() )
-        columns.insertMulti("EVENT", "title");
-    if( searchHeader->searchAbstract->isChecked() )
-        columns.insertMulti("EVENT", "abstract");
-    if( searchHeader->searchTag->isChecked() )
-        columns.insertMulti("EVENT", "tag");
-    if( searchHeader->searchSpeaker->isChecked() )
-        columns["PERSON"] = "name";
-    if( searchHeader->searchRoom->isChecked() )
-        columns["ROOM"] = "name";
-
-    QString keyword = searchHeader->searchEdit->text().replace( QString("%"), QString("\\%") );
-    qDebug() << "\nKeyword to search: " << keyword;
+    if (searchHeader->searchTitle->isChecked())
+        columns.insert("EVENT", "title");
+    if (searchHeader->searchAbstract->isChecked())
+        columns.insert("EVENT", "abstract");
+    if (searchHeader->searchTag->isChecked())
+        columns.insert("EVENT", "tag");
+    if (searchHeader->searchSpeaker->isChecked())
+        columns.insert("PERSON", "name");
+    if (searchHeader->searchRoom->isChecked())
+        columns.insert("ROOM", "name");
+
+    QString keyword = searchHeader->searchEdit->text();
 
     int confId = Conference::activeConference();
-    SqlEngine::searchEvent( confId, columns, keyword );
+    if (confId == -1) return;
+    Conference conf = Conference::getById(confId);
 
-    QDate startDate = Conference::getById(confId).start();
-    QDate endDate = Conference::getById(confId).end();
-    dayNavigator->setDates(startDate, endDate);
-    updateTreeView( Conference::getById(confId).start() );
-}
+    sqlEngine->searchEvent( confId, columns, keyword );
 
-void SearchTabContainer::searchAgainClicked()
-{
-    qDebug() << "SearchTab::searchAgainClicked()";
-    header->show();
-    searchAgainButton->hide();
-    dayNavigator->hide();
-    treeView->hide();
-}
+    int nrofFounds = 0;
+    for (QDate d = conf.start(); d <= conf.end(); d = d.addDays(1))
+        nrofFounds += Event::getSearchResultByDate(d, confId, "start, duration").count();
 
-void SearchTabContainer::loadEvents( const QDate &aDate, const int aConferenceId )
-{
-    int eventsCount = static_cast<EventModel*>(treeView->model())->loadSearchResultEvents( aDate, aConferenceId );
-    if( eventsCount ||
-            //TODO: this is not good test
-            dayNavigator->getCurrentDate() != Conference::getById( aConferenceId ).start()
-            ){
-        searchAgainButton->show();
-        dayNavigator->show();
-        treeView->show();
-        header->hide();
-    }
-    else{
+    if (!nrofFounds) {
         treeView->hide();
-        searchAgainButton->hide();
-        dayNavigator->hide();
         header->show();
+        QMessageBox::information(
+                this,
+                QString("Keyword '%1' not found!").arg(keyword),
+                QString("No events containing '%1' found!").arg(keyword),
+                QMessageBox::Ok);
+    } else {
+        treeView->show();
+        header->hide();
+
+        emit searchResultChanged();
     }
 }
+
+
+void SearchTabContainer::loadEvents( const QDate &aDate, const int aConferenceId )
+{
+    static_cast<EventModel*>(treeView->model())->loadSearchResultEvents( aDate, aConferenceId );
+}
+