Sorted by duration additionally to start.
[toast/confclerk.git] / src / gui / searchtabcontainer.cpp
1 /*
2  * Copyright (C) 2010 Ixonos Plc.
3  * Copyright (C) 2011 Philipp Spitzer, gregor herrmann
4  *
5  * This file is part of ConfClerk.
6  *
7  * ConfClerk is free software: you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the Free
9  * Software Foundation, either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * ConfClerk is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * ConfClerk.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include "searchtabcontainer.h"
22 #include "searchhead.h"
23 #include <QMessageBox>
24
25 SearchTabContainer::SearchTabContainer(QWidget *aParent) : TabContainer( aParent )
26 {
27     header = new SearchHead(this);
28     header->setObjectName(QString::fromUtf8("header"));
29     QSizePolicy sizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Expanding);
30     sizePolicy.setHorizontalStretch(0);
31     sizePolicy.setVerticalStretch(0);
32     //sizePolicy.setHeightForWidth(TabContainer::sizePolicy().hasHeightForWidth());
33     sizePolicy.setHeightForWidth(header->sizePolicy().hasHeightForWidth());
34     header->setSizePolicy(sizePolicy);
35     header->setMinimumSize(QSize(10, 10));
36     verticalLayout->insertWidget(0,header);
37     connect(header, SIGNAL(searchClicked()), SLOT(searchButtonClicked()));
38     showSearchDialog();
39 }
40
41
42 void SearchTabContainer::showSearchDialog() {
43     header->show();
44     treeView->hide();
45 }
46
47
48 void SearchTabContainer::searchButtonClicked() {
49     QHash<QString,QString> columns;
50
51     SearchHead *searchHeader = static_cast<SearchHead*>(header);
52     if( searchHeader->searchTitle->isChecked() )
53         columns.insertMulti("EVENT", "title");
54     if( searchHeader->searchAbstract->isChecked() )
55         columns.insertMulti("EVENT", "abstract");
56     if( searchHeader->searchTag->isChecked() )
57         columns.insertMulti("EVENT", "tag");
58     if( searchHeader->searchSpeaker->isChecked() )
59         columns["PERSON"] = "name";
60     if( searchHeader->searchRoom->isChecked() )
61         columns["ROOM"] = "name";
62
63     QString keyword = searchHeader->searchEdit->text();
64
65     int confId = Conference::activeConference();
66     if (confId == -1) return;
67
68     SqlEngine::searchEvent( confId, columns, keyword );
69
70     QDate startDate = Conference::getById(confId).start();
71     QDate endDate = Conference::getById(confId).end();
72
73     int nrofFounds = 0;
74     QDate firstDateWithFounds = endDate;
75     QDate lastDateWithFounds = startDate;
76     for(QDate d=startDate; d<=endDate; d=d.addDays(1))
77     {
78         try{
79             int count = Event::getSearchResultByDate(d, confId, "start, duration").count();
80             if(count && (firstDateWithFounds==endDate))
81                 firstDateWithFounds=d;
82             if(count)
83                 lastDateWithFounds=d;
84             nrofFounds+=count;
85         }
86         catch( OrmException &e  ){
87             qDebug() << "Event::getSearchResultByDate failed: " << e.text();
88         }
89         catch(...){
90             qDebug() << "Event::getSearchResultByDate failed";
91         }
92     }
93
94     if(!nrofFounds)
95     {
96         // TODO: display some message
97         treeView->hide();
98         header->show();
99         QMessageBox::information(
100                 this,
101                 QString("Keyword '%1' not found!").arg(keyword),
102                 QString("No events containing '%1' found!").arg(keyword),
103                 QMessageBox::Ok);
104     }
105     else
106     {
107         treeView->show();
108         header->hide();
109
110         updateTreeView( firstDateWithFounds );
111     }
112 }
113
114
115 void SearchTabContainer::loadEvents( const QDate &aDate, const int aConferenceId )
116 {
117     static_cast<EventModel*>(treeView->model())->loadSearchResultEvents( aDate, aConferenceId );
118 }
119