Restructured the SqlEngine. Not yet finished (see "TODO" in the code).
[toast/confclerk.git] / src / gui / searchtabcontainer.cpp
1 /*
2  * Copyright (C) 2010 Ixonos Plc.
3  * Copyright (C) 2011-2012 Philipp Spitzer, gregor herrmann, Stefan Stahl
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), sqlEngine(0) {
26     header = new SearchHead(this);
27     header->setObjectName(QString::fromUtf8("header"));
28     QSizePolicy sizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Expanding);
29     sizePolicy.setHorizontalStretch(0);
30     sizePolicy.setVerticalStretch(0);
31     //sizePolicy.setHeightForWidth(TabContainer::sizePolicy().hasHeightForWidth());
32     sizePolicy.setHeightForWidth(header->sizePolicy().hasHeightForWidth());
33     header->setSizePolicy(sizePolicy);
34     header->setMinimumSize(QSize(10, 10));
35     verticalLayout->insertWidget(0,header);
36     connect(header, SIGNAL(searchClicked()), SLOT(searchButtonClicked()));
37     showSearchDialog();
38 }
39
40
41 bool SearchTabContainer::searchDialogIsVisible() const {
42     return header->isVisible();
43 }
44
45
46 int SearchTabContainer::searchResultCount(const QDate& date) const {
47     int confId = Conference::activeConference();
48     if (confId == -1) return 0;
49     return Event::getSearchResultByDate(date, confId, "start, duration").count();
50 }
51
52
53 void SearchTabContainer::showSearchDialog(bool show) {
54     header->setVisible(show);
55     treeView->setVisible(!show);
56 }
57
58
59 void SearchTabContainer::searchButtonClicked() {
60     if (!sqlEngine) return;
61
62     QHash<QString,QString> columns;
63
64     SearchHead *searchHeader = static_cast<SearchHead*>(header);
65     if( searchHeader->searchTitle->isChecked() )
66         columns.insertMulti("EVENT", "title");
67     if( searchHeader->searchAbstract->isChecked() )
68         columns.insertMulti("EVENT", "abstract");
69     if( searchHeader->searchTag->isChecked() )
70         columns.insertMulti("EVENT", "tag");
71     if( searchHeader->searchSpeaker->isChecked() )
72         columns["PERSON"] = "name";
73     if( searchHeader->searchRoom->isChecked() )
74         columns["ROOM"] = "name";
75
76     QString keyword = searchHeader->searchEdit->text();
77
78     int confId = Conference::activeConference();
79     if (confId == -1) return;
80     Conference conf = Conference::getById(confId);
81
82     sqlEngine->searchEvent( confId, columns, keyword );
83
84     int nrofFounds = 0;
85     for (QDate d = conf.start(); d <= conf.end(); d = d.addDays(1))
86         nrofFounds += Event::getSearchResultByDate(d, confId, "start, duration").count();
87
88     if (!nrofFounds) {
89         treeView->hide();
90         header->show();
91         QMessageBox::information(
92                 this,
93                 QString("Keyword '%1' not found!").arg(keyword),
94                 QString("No events containing '%1' found!").arg(keyword),
95                 QMessageBox::Ok);
96     } else {
97         treeView->show();
98         header->hide();
99
100         emit searchResultChanged();
101     }
102 }
103
104
105 void SearchTabContainer::loadEvents( const QDate &aDate, const int aConferenceId )
106 {
107     static_cast<EventModel*>(treeView->model())->loadSearchResultEvents( aDate, aConferenceId );
108 }
109