Adjust whitespace in if statements.
[toast/confclerk.git] / src / gui / searchtabcontainer.cpp
index d9b050c24d618cc06218492f05bc3bdb9699a87c..3c3d7bdd326c42cef91b777c04023ec61864a5b0 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2010 Ixonos Plc.
- * Copyright (C) 2011 Philipp Spitzer, gregor herrmann
+ * Copyright (C) 2011-2017 Philipp Spitzer, gregor herrmann, Stefan Stahl
  *
  * This file is part of ConfClerk.
  *
@@ -22,8 +22,7 @@
 #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);
@@ -33,118 +32,76 @@ 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);
-
-    searchAgainButton->hide();
-    treeView->hide();
-    // do not show 'search' header if there are no conferences in the DB
-    if(Conference::getAll().count()==0)
-    {
-        header->hide();
-    }
 
-    connect( header, SIGNAL(searchClicked()), SLOT(searchButtonClicked()));
-    connect( searchAgainButton, SIGNAL(clicked()), SLOT(searchAgainClicked()));
+bool SearchTabContainer::searchDialogIsVisible() const {
+    return header->isVisible();
 }
 
-SearchTabContainer::~SearchTabContainer()
-{
+
+int SearchTabContainer::searchResultCount(const QDate& date) const {
+    int confId = Conference::activeConference();
+    if (confId == -1) return 0;
+    return Event::getSearchResultByDate(date, confId, "start, duration").count();
 }
 
-void SearchTabContainer::searchButtonClicked()
-{
-    QHash<QString,QString> columns;
+
+void SearchTabContainer::showSearchDialog(bool show) {
+    header->setVisible(show);
+    treeView->setVisible(!show);
+    if (show) header->searchEdit->setFocus(Qt::OtherFocusReason);
+}
+
+
+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();
+    sqlEngine->searchEvent( confId, columns, keyword );
 
     int nrofFounds = 0;
-    QDate firstDateWithFounds = endDate;
-    QDate lastDateWithFounds = startDate;
-    for(QDate d=startDate; d<=endDate; d=d.addDays(1))
-    {
-        try{
-            int count = Event::getSearchResultByDate(d, confId, "start").count();
-            if(count && (firstDateWithFounds==endDate))
-                firstDateWithFounds=d;
-            if(count)
-                lastDateWithFounds=d;
-            nrofFounds+=count;
-        }
-        catch( OrmException &e  ){
-            qDebug() << "Event::getSearchResultByDate failed: " << e.text();
-        }
-        catch(...){
-            qDebug() << "Event::getSearchResultByDate failed";
-        }
-    }
+    for (QDate d = conf.start(); d <= conf.end(); d = d.addDays(1))
+        nrofFounds += Event::getSearchResultByDate(d, confId, "start, duration").count();
 
-    if(!nrofFounds)
-    {
-        // TODO: display some message
+    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
-    {
-        searchAgainButton->show();
-        dayNavigator->show();
+    } else {
         treeView->show();
         header->hide();
 
-        updateTreeView( firstDateWithFounds );
-        dayNavigator->setDates(firstDateWithFounds, lastDateWithFounds);
+        emit searchResultChanged();
     }
 }
 
-void SearchTabContainer::searchAgainClicked()
-{
-    header->show();
-    searchAgainButton->hide();
-    dayNavigator->hide();
-    treeView->hide();
-}
 
 void SearchTabContainer::loadEvents( const QDate &aDate, const int aConferenceId )
 {