d9b050c24d618cc06218492f05bc3bdb9699a87c
[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
37     verticalLayout->insertWidget(0,header);
38
39     header->show();
40
41     searchAgainButton = new QToolButton(this);
42     searchAgainButton->setObjectName(QString::fromUtf8("button"));
43     QIcon icon;
44     icon.addPixmap(QPixmap(QString::fromUtf8(":/icons/search.png")), QIcon::Normal, QIcon::Off);
45     searchAgainButton->setIcon(icon);
46     QSizePolicy sizePolicy1(QSizePolicy::Minimum, QSizePolicy::Minimum);
47     sizePolicy1.setHorizontalStretch(0);
48     sizePolicy1.setVerticalStretch(0);
49     sizePolicy1.setHeightForWidth(searchAgainButton->sizePolicy().hasHeightForWidth());
50     searchAgainButton->setSizePolicy(sizePolicy1);
51
52     verticalLayout_2->insertWidget(0,searchAgainButton);
53
54     searchAgainButton->hide();
55     treeView->hide();
56     // do not show 'search' header if there are no conferences in the DB
57     if(Conference::getAll().count()==0)
58     {
59         header->hide();
60     }
61
62     connect( header, SIGNAL(searchClicked()), SLOT(searchButtonClicked()));
63     connect( searchAgainButton, SIGNAL(clicked()), SLOT(searchAgainClicked()));
64 }
65
66 SearchTabContainer::~SearchTabContainer()
67 {
68 }
69
70 void SearchTabContainer::searchButtonClicked()
71 {
72     QHash<QString,QString> columns;
73
74     SearchHead *searchHeader = static_cast<SearchHead*>(header);
75     if( searchHeader->searchTitle->isChecked() )
76         columns.insertMulti("EVENT", "title");
77     if( searchHeader->searchAbstract->isChecked() )
78         columns.insertMulti("EVENT", "abstract");
79     if( searchHeader->searchTag->isChecked() )
80         columns.insertMulti("EVENT", "tag");
81     if( searchHeader->searchSpeaker->isChecked() )
82         columns["PERSON"] = "name";
83     if( searchHeader->searchRoom->isChecked() )
84         columns["ROOM"] = "name";
85
86     QString keyword = searchHeader->searchEdit->text().replace( QString("%"), QString("\\%") );
87     //qDebug() << "\nKeyword to search: " << keyword;
88
89     int confId = Conference::activeConference();
90     SqlEngine::searchEvent( confId, columns, keyword );
91
92     QDate startDate = Conference::getById(confId).start();
93     QDate endDate = Conference::getById(confId).end();
94
95     int nrofFounds = 0;
96     QDate firstDateWithFounds = endDate;
97     QDate lastDateWithFounds = startDate;
98     for(QDate d=startDate; d<=endDate; d=d.addDays(1))
99     {
100         try{
101             int count = Event::getSearchResultByDate(d, confId, "start").count();
102             if(count && (firstDateWithFounds==endDate))
103                 firstDateWithFounds=d;
104             if(count)
105                 lastDateWithFounds=d;
106             nrofFounds+=count;
107         }
108         catch( OrmException &e  ){
109             qDebug() << "Event::getSearchResultByDate failed: " << e.text();
110         }
111         catch(...){
112             qDebug() << "Event::getSearchResultByDate failed";
113         }
114     }
115
116     if(!nrofFounds)
117     {
118         // TODO: display some message
119         treeView->hide();
120         searchAgainButton->hide();
121         dayNavigator->hide();
122         header->show();
123         QMessageBox::information(
124                 this,
125                 QString("Keyword '%1' not found!").arg(keyword),
126                 QString("No events containing '%1' found!").arg(keyword),
127                 QMessageBox::Ok);
128     }
129     else
130     {
131         searchAgainButton->show();
132         dayNavigator->show();
133         treeView->show();
134         header->hide();
135
136         updateTreeView( firstDateWithFounds );
137         dayNavigator->setDates(firstDateWithFounds, lastDateWithFounds);
138     }
139 }
140
141 void SearchTabContainer::searchAgainClicked()
142 {
143     header->show();
144     searchAgainButton->hide();
145     dayNavigator->hide();
146     treeView->hide();
147 }
148
149 void SearchTabContainer::loadEvents( const QDate &aDate, const int aConferenceId )
150 {
151     static_cast<EventModel*>(treeView->model())->loadSearchResultEvents( aDate, aConferenceId );
152 }
153