014ca5a6a6ff7123c6334e984be432bd3b325b28
[toast/confclerk.git] / src / gui / searchtabcontainer.cpp
1
2 #include "searchtabcontainer.h"
3 #include "searchhead.h"
4 #include <QMessageBox>
5
6 SearchTabContainer::SearchTabContainer(QWidget *aParent) : TabContainer( aParent )
7 {
8     header = new SearchHead(this);
9     header->setObjectName(QString::fromUtf8("header"));
10     QSizePolicy sizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Expanding);
11     sizePolicy.setHorizontalStretch(0);
12     sizePolicy.setVerticalStretch(0);
13     //sizePolicy.setHeightForWidth(TabContainer::sizePolicy().hasHeightForWidth());
14     sizePolicy.setHeightForWidth(header->sizePolicy().hasHeightForWidth());
15     header->setSizePolicy(sizePolicy);
16     header->setMinimumSize(QSize(10, 10));
17
18     verticalLayout->insertWidget(0,header);
19
20     header->show();
21
22     searchAgainButton = new QToolButton(this);
23     searchAgainButton->setObjectName(QString::fromUtf8("button"));
24     QIcon icon;
25     icon.addPixmap(QPixmap(QString::fromUtf8(":/icons/search.png")), QIcon::Normal, QIcon::Off);
26     searchAgainButton->setIcon(icon);
27     QSizePolicy sizePolicy1(QSizePolicy::Minimum, QSizePolicy::Minimum);
28     sizePolicy1.setHorizontalStretch(0);
29     sizePolicy1.setVerticalStretch(0);
30     sizePolicy1.setHeightForWidth(searchAgainButton->sizePolicy().hasHeightForWidth());
31     searchAgainButton->setSizePolicy(sizePolicy1);
32
33     verticalLayout_2->insertWidget(0,searchAgainButton);
34
35     searchAgainButton->hide();
36     treeView->hide();
37     // do not show 'search' header if there are no conferences in the DB
38     if(Conference::getAll().count()==0)
39     {
40         header->hide();
41     }
42
43     connect( header, SIGNAL(searchClicked()), SLOT(searchButtonClicked()));
44     connect( searchAgainButton, SIGNAL(clicked()), SLOT(searchAgainClicked()));
45 }
46
47 SearchTabContainer::~SearchTabContainer()
48 {
49 }
50
51 void SearchTabContainer::searchButtonClicked()
52 {
53     QHash<QString,QString> columns;
54
55     SearchHead *searchHeader = static_cast<SearchHead*>(header);
56     if( searchHeader->searchTitle->isChecked() )
57         columns.insertMulti("EVENT", "title");
58     if( searchHeader->searchAbstract->isChecked() )
59         columns.insertMulti("EVENT", "abstract");
60     if( searchHeader->searchTag->isChecked() )
61         columns.insertMulti("EVENT", "tag");
62     if( searchHeader->searchSpeaker->isChecked() )
63         columns["PERSON"] = "name";
64     if( searchHeader->searchRoom->isChecked() )
65         columns["ROOM"] = "name";
66
67     QString keyword = searchHeader->searchEdit->text().replace( QString("%"), QString("\\%") );
68     //qDebug() << "\nKeyword to search: " << keyword;
69
70     int confId = Conference::activeConference();
71     SqlEngine::searchEvent( confId, columns, keyword );
72
73     QDate startDate = Conference::getById(confId).start();
74     QDate endDate = Conference::getById(confId).end();
75
76     int nrofFounds = 0;
77     QDate firstDateWithFounds = endDate;
78     QDate lastDateWithFounds = startDate;
79     for(QDate d=startDate; d<=endDate; d=d.addDays(1))
80     {
81         try{
82             int count = Event::getSearchResultByDate(d, confId, "start").count();
83             if(count && (firstDateWithFounds==endDate))
84                 firstDateWithFounds=d;
85             if(count)
86                 lastDateWithFounds=d;
87             nrofFounds+=count;
88         }
89         catch( OrmException &e  ){
90             qDebug() << "Event::getSearchResultByDate failed: " << e.text();
91         }
92         catch(...){
93             qDebug() << "Event::getSearchResultByDate failed";
94         }
95     }
96
97     if(!nrofFounds)
98     {
99         // TODO: display some message
100         treeView->hide();
101         searchAgainButton->hide();
102         dayNavigator->hide();
103         header->show();
104         QMessageBox::information(
105                 this,
106                 QString("Keyword '%1' not found!").arg(keyword),
107                 QString("No events containing '%1' found!").arg(keyword),
108                 QMessageBox::Ok);
109     }
110     else
111     {
112         searchAgainButton->show();
113         dayNavigator->show();
114         treeView->show();
115         header->hide();
116
117         updateTreeView( firstDateWithFounds );
118         dayNavigator->setDates(firstDateWithFounds, lastDateWithFounds);
119     }
120 }
121
122 void SearchTabContainer::searchAgainClicked()
123 {
124     header->show();
125     searchAgainButton->hide();
126     dayNavigator->hide();
127     treeView->hide();
128 }
129
130 void SearchTabContainer::loadEvents( const QDate &aDate, const int aConferenceId )
131 {
132     static_cast<EventModel*>(treeView->model())->loadSearchResultEvents( aDate, aConferenceId );
133 }
134