'search' tab functionality moved to 'tabcontainer'
[toast/confclerk.git] / src / gui / tabcontainer.cpp
1 #include "tabcontainer.h"
2
3 #include <QFile>
4 #include <QMessageBox>
5 #include <QTimer>
6
7 #include <sqlengine.h>
8 #include <appsettings.h>
9
10 #include <conference.h>
11
12 #include <treeview.h>
13 #include <eventmodel.h>
14 #include <delegate.h>
15
16 #include "eventdialog.h"
17 #include "mapwindow.h"
18
19 TabContainer::TabContainer(QWidget *aParent)
20     : QWidget(aParent)
21     , mType(EContainerTypeNone)
22 {
23     setupUi(this);
24
25     searchAgainButton->hide();
26     searchHead->hide();
27
28     treeView->setHeaderHidden(true);
29     treeView->setRootIsDecorated(false);
30     treeView->setIndentation(0);
31     treeView->setAnimated(true);
32     treeView->setModel(new EventModel());
33     treeView->setItemDelegate(new Delegate(treeView));
34
35     connect(dayNavigator, SIGNAL(dateChanged(const QDate &)), SLOT(updateTreeView(const QDate &)));
36
37     connect(treeView, SIGNAL(eventHasChanged(int)), SIGNAL(eventHasChanged(int)));
38     connect(treeView, SIGNAL(clicked(const QModelIndex &)), SLOT(itemClicked(const QModelIndex &)));
39     connect(treeView, SIGNAL(requestForMap(const QModelIndex &)), SLOT(displayMap(const QModelIndex &)));
40     connect(treeView, SIGNAL(requestForWarning(const QModelIndex &)), SLOT(displayWarning(const QModelIndex &)));
41
42     connect(searchButton, SIGNAL(clicked()), SLOT(searchClicked()));
43     connect(searchAgainButton, SIGNAL(clicked()), SLOT(searchAgainClicked()));
44
45     if(!Conference::getAll().count()) // no conference(s) in the DB
46     {
47         dayNavigator->hide(); // hide DayNavigatorWidget
48     }
49     else
50     {
51         QDate aStartDate = Conference::getById(AppSettings::confId()).start();
52         QDate aEndDate = Conference::getById(AppSettings::confId()).end();
53         dayNavigator->setDates(aStartDate, aEndDate);
54     }
55 }
56
57 void TabContainer::setType(TabContainer::Type aType)
58 {
59     mType = aType;
60
61     if(aType == EContainerTypeNow)
62     {
63         QTimer *timer = new QTimer( this );
64         connect( timer, SIGNAL(timeout()), SLOT(timerUpdateTreeView()) );
65         timer->start( 30000); // 30 seconds timer
66     }
67     if(aType == EContainerTypeSearch)
68     {
69         searchHead->show();
70     }
71 }
72
73 void TabContainer::updateTreeView(const QDate &aDate)
74 {
75     switch(mType)
76     {
77         case EContainerTypeDay:
78             {
79                 static_cast<EventModel*>(treeView->model())->loadEvents(aDate,AppSettings::confId());
80             }
81             break;
82         case EContainerTypeFavs:
83             {
84                 static_cast<EventModel*>(treeView->model())->loadFavEvents(aDate,AppSettings::confId());
85             }
86             break;
87         case EContainerTypeTracks:
88             {
89                 static_cast<EventModel*>(treeView->model())->loadEventsByTrack(aDate, AppSettings::confId());
90             }
91             break;
92         case EContainerTypeRooms:
93             {
94                 static_cast<EventModel*>(treeView->model())->loadEventsByRoom(aDate, AppSettings::confId());
95             }
96             break;
97         case EContainerTypeNow:
98             {
99                 static_cast<EventModel*>(treeView->model())->loadNowEvents(AppSettings::confId());
100                 treeView->setAllExpanded(true);
101             }
102             break;
103         case EContainerTypeSearch:
104             {
105                 treeView->reset();
106                 int eventsCount = static_cast<EventModel*>(treeView->model())->loadSearchResultEvents(aDate,AppSettings::confId());
107                 if( eventsCount ||
108                         dayNavigator->getCurrentDate() != Conference::getById(AppSettings::confId()).start() ){
109                     searchAgainButton->show();
110                     dayNavigator->show();
111                     treeView->show();
112                     searchHead->hide();
113                 }
114                 else{
115                     treeView->hide();
116                     searchAgainButton->hide();
117                     dayNavigator->hide();
118                     searchHead->show();
119                 }
120             }
121             break;
122         case EContainerTypeNone:
123         default:
124             {
125                 qDebug() << "Container type not specified";
126             }
127     }
128     treeView->reset();
129     dayNavigator->show();
130 }
131
132 void TabContainer::itemClicked(const QModelIndex &aIndex)
133 {
134     // have to handle only events, not time-groups
135     if(!aIndex.parent().isValid()) // time-group
136         return;
137
138     EventDialog dialog(static_cast<Event*>(aIndex.internalPointer())->id(),this);
139     dialog.exec();
140 }
141
142 void TabContainer::displayMap(const QModelIndex &aIndex)
143 {
144     Event *event = static_cast<Event*>(aIndex.internalPointer());
145
146     // room names are stored in lower-case format
147     // room names are stored without dots in the name, eg. "aw.1124.png" -> "aw1124.png"
148     QString mapPath = QString(":/maps/rooms/%1.png").arg(event->room().toLower().remove("."));
149     if(!QFile::exists(mapPath))
150         mapPath = QString(":/maps/rooms/not-available.png");
151
152     QString roomName;
153     if(mapPath.contains("not-available", Qt::CaseInsensitive))
154         roomName = QString("Map is not available: %1").arg(event->room());
155     else
156         roomName = event->room();
157
158     QPixmap map(mapPath);
159     MapWindow window(map,roomName,this);
160     window.exec();
161 }
162
163 void TabContainer::displayWarning(const QModelIndex &aIndex)
164 {
165     Q_UNUSED(aIndex);
166
167     QMessageBox::warning(
168         this,
169         tr("Time Conflict Warning"),
170         tr("This event happens at the same time than another one of your favourites.") );
171 }
172
173 void TabContainer::updateTreeViewModel(int aEventId)
174 {
175     switch(mType)
176     {
177         case EContainerTypeFavs:
178             {
179                 // requires special handling
180                 // we need to reload favourites, because some favourite could be deleted
181                 //static_cast<EventModel*>(favTreeView->model())->updateModel(aEventId);
182                 QDate aStartDate = Conference::getById(AppSettings::confId()).start();
183                 QDate aEndDate = Conference::getById(AppSettings::confId()).end();
184                 dayNavigator->setDates(aStartDate, aEndDate);
185                 updateTreeView( Conference::getById(AppSettings::confId()).start() );
186             }
187             break;
188         case EContainerTypeDay:
189         case EContainerTypeNone:
190         default:
191             {
192                 static_cast<EventModel*>(treeView->model())->updateModel(aEventId);
193             }
194     }
195 }
196
197 void TabContainer::setDates(const QDate &aStart, const QDate &aEnd)
198 {
199     dayNavigator->setDates(aStart, aEnd);
200 }
201
202 void TabContainer::timerUpdateTreeView()
203 {
204     if(mType == EContainerTypeNow)
205     {
206         updateTreeView(QDate());
207     }
208 }
209
210 void TabContainer::searchClicked()
211 {
212     if(mType == EContainerTypeSearch)
213     {
214         QHash<QString,QString> columns;
215
216         if( searchTitle->isChecked() )
217             columns.insertMulti("EVENT", "title");
218         if( searchAbstract->isChecked() )
219             columns.insertMulti("EVENT", "abstract");
220         if( searchTag->isChecked() )
221             columns.insertMulti("EVENT", "tag");
222         if( searchSpeaker->isChecked() )
223             columns["PERSON"] = "name";
224         if( searchRoom->isChecked() )
225             columns["ROOM"] = "name";
226
227         QString keyword = searchEdit->text().replace( QString("%"), QString("\\%") );
228         qDebug() << "\nKeyword to search: " << keyword;
229         SqlEngine::searchEvent( AppSettings::confId(), columns, keyword );
230
231         QDate startDate = Conference::getById(AppSettings::confId()).start();
232         QDate endDate = Conference::getById(AppSettings::confId()).end();
233         dayNavigator->setDates(startDate, endDate);
234         updateTreeView( Conference::getById(AppSettings::confId()).start() );
235     }
236 }
237
238 void TabContainer::searchAgainClicked()
239 {
240     if(mType == EContainerTypeSearch)
241     {
242         searchHead->show();
243         searchAgainButton->hide();
244         dayNavigator->hide();
245         treeView->hide();
246     }
247 }
248