minor UI fixes
[toast/confclerk.git] / src / gui / conferenceeditor.cpp
1 /*
2  * Copyright (C) 2010 Ixonos Plc.
3  *
4  * This file is part of fosdem-schedule.
5  *
6  * fosdem-schedule is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the Free
8  * Software Foundation, either version 2 of the License, or (at your option)
9  * any later version.
10  *
11  * fosdem-schedule is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * fosdem-schedule.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 #include "conferenceeditor.h"
20
21 #include "conferencemodel.h"
22 #include "urlinputdialog.h"
23
24 #include <QInputDialog>
25 #include <QItemSelectionModel>
26 #include <QFileDialog>
27 #include <QMessageBox>
28
29 ConferenceEditor::ConferenceEditor(ConferenceModel* model, QWidget* parent)
30 : QDialog(parent)
31 , model(model)
32 , selected_id(-1)
33 {
34     setupUi(this);
35     progressBar->hide();
36
37     confView->setModel(model);
38
39     QItemSelectionModel* confViewSelection = new QItemSelectionModel(model, this);
40     confView->setSelectionModel(confViewSelection);
41
42     connect(confViewSelection, SIGNAL(currentChanged(const QModelIndex&, const QModelIndex&)),
43                                 SLOT(itemSelected(const QModelIndex&, const QModelIndex&)));
44     connect(this, SIGNAL(wantCurrent(const QModelIndex&, QItemSelectionModel::SelectionFlags)),
45             confViewSelection, SLOT(setCurrentIndex(const QModelIndex&, QItemSelectionModel::SelectionFlags)));
46     connect(addBtn, SIGNAL(clicked()), SLOT(addClicked()));
47     connect(removeBtn, SIGNAL(clicked()), SLOT(removeClicked()));
48     connect(changeUrl, SIGNAL(clicked()), SLOT(changeUrlClicked()));
49     connect(refreshBtn, SIGNAL(clicked()), SLOT(refreshClicked()));
50
51     // it's OK to emit selection signals here
52     // because they are not yet connected to anybody
53     int active_id = Conference::activeConference();
54     if (active_id > 0) {
55         emit wantCurrent(model->indexFromId(active_id), QItemSelectionModel::SelectCurrent);
56     } else {
57         itemSelected(QModelIndex(), QModelIndex());
58     }
59 }
60
61 void ConferenceEditor::conferenceRemoved()
62 {
63     if (model->rowCount() > 0) {
64         emit wantCurrent(model->index(0, 0), QItemSelectionModel::SelectCurrent);
65     } else {
66         itemSelected(QModelIndex(), QModelIndex());
67     }
68 }
69
70 void ConferenceEditor::itemSelected(const QModelIndex& current, const QModelIndex& previous)
71 {
72     // TODO: fill all required fields
73     Q_UNUSED(previous);
74     if (!current.isValid()) {
75         selected_id = -1;
76
77         emit noneConferenceSelected();
78
79         conferenceInfo->setCurrentIndex(1);
80         removeBtn->hide();
81     } else {
82         const Conference& conf = model->conferenceFromIndex(current);
83         selected_id = conf.id();
84
85         emit haveConferenceSelected(selected_id);
86
87         conferenceTitle->setText(conf.title());
88         conferenceSubtitle->setText(conf.subtitle());
89         conferenceWhere->setText(conf.city() + ", " + conf.venue());
90         conferenceWhen->setText(
91                 conf.start().toString("dd-MM-yyyy")
92                 + ", " +
93                 conf.end().toString("dd-MM-yyyy"));
94         conferenceInfo->setCurrentIndex(0);
95         removeBtn->show();
96     }
97 }
98
99 void ConferenceEditor::addClicked()
100 {
101     UrlInputDialog url_input(this);
102     switch (url_input.exec()) {
103     case UrlInputDialog::HaveUrl: emit haveConferenceUrl(url_input.url()); break;
104     case UrlInputDialog::HaveFile: emit haveConferenceFile(url_input.url()); break;
105     case UrlInputDialog::Cancel: return;
106     }
107 }
108
109 void ConferenceEditor::removeClicked()
110 {
111     if (selected_id < 0) {
112         // TODO: disable it when none is selected
113         return;
114     }
115
116     QMessageBox::StandardButton answer = 
117         QMessageBox::question(0
118             , "Deletion confirmation"
119             , QString("Really delete the %1 conference").arg(Conference::getById(selected_id).title())
120             , QMessageBox::Yes | QMessageBox::No
121             , QMessageBox::No);
122
123     if (answer == QMessageBox::Yes) {
124         emit removeConferenceRequested(selected_id);
125     }
126 }
127
128 void ConferenceEditor::changeUrlClicked()
129 {
130     if (selected_id < 0) {
131         return;
132     }
133     const Conference& selected = Conference::getById(selected_id);
134
135     bool ok;
136     QString url = QInputDialog::getText(this, "URL Input", "Enter schedule URL", QLineEdit::Normal, selected.url(), &ok);
137
138     if (ok) {
139         emit changeUrlRequested(selected_id, url);
140     }
141 }
142
143 void ConferenceEditor::refreshClicked()
144 {
145     if (selected_id < 0) {
146         return;
147     }
148     const Conference& selected = Conference::getById(selected_id);
149
150     QString url = selected.url();
151
152     if (!url.isEmpty()) {
153         emit haveConferenceUrl(url);
154     } else {
155         static const QString format("Schedule URL for %1 is not set. Enter the schedule URL:");
156         bool ok;
157         QString url = QInputDialog::getText(this, "URL Input", format.arg(selected.title()), QLineEdit::Normal, QString(), &ok);
158
159         if (ok) {
160             // first save it, to remain if fetch fails
161             emit changeUrlRequested(selected_id, url);
162             // then fetch
163             emit haveConferenceUrl(url);
164         }
165     }
166 }
167
168 void ConferenceEditor::importStarted()
169 {
170     addBtn->hide();
171     removeBtn->hide();
172     buttons->layout()->removeItem(buttonsSpacer);
173     progressBar->setValue(0);
174     progressBar->show();
175
176     QApplication::processEvents();
177 }
178
179 void ConferenceEditor::showParsingProgress(int progress)
180 {
181     progressBar->setValue(progress);
182
183     QApplication::processEvents();
184 }
185
186 void ConferenceEditor::importFinished(const QString& title)
187 {
188     qDebug() << __PRETTY_FUNCTION__ << title;
189     addBtn->show();
190     // removeItem should be shown later, but it takes some time,
191     // and not looks good
192     // anyway it will be shown a bit later
193     removeBtn->show();
194     buttons->layout()->addItem(buttonsSpacer);
195     progressBar->hide();
196
197     QApplication::processEvents();
198
199     int num = model->rowCount();
200     for (int i = 0; i < num; i++) {
201         QModelIndex item = model->index(i, 0);
202         if (model->data(item) == title) {
203             emit wantCurrent(item, QItemSelectionModel::SelectCurrent);
204             return;
205         }
206     }
207     itemSelected(QModelIndex(), QModelIndex());
208 }