comparison src/multichoicedialog.cpp @ 370:b9c153e00e84

Move source files to src/
author Chris Cannam
date Thu, 24 Mar 2011 10:27:51 +0000
parents multichoicedialog.cpp@3824e8bb91eb
children a582c6417004
comparison
equal deleted inserted replaced
369:19cce6d2c470 370:b9c153e00e84
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 EasyMercurial
5
6 Based on HgExplorer by Jari Korhonen
7 Copyright (c) 2010 Jari Korhonen
8 Copyright (c) 2011 Chris Cannam
9 Copyright (c) 2011 Queen Mary, University of London
10
11 This program is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public License as
13 published by the Free Software Foundation; either version 2 of the
14 License, or (at your option) any later version. See the file
15 COPYING included with this distribution for more information.
16 */
17
18 #include "multichoicedialog.h"
19
20 #include "selectablelabel.h"
21
22 #include "debug.h"
23
24 #include <QDialogButtonBox>
25 #include <QToolButton>
26 #include <QPushButton>
27 #include <QFont>
28 #include <QDir>
29 #include <QFileDialog>
30 #include <QUrl>
31
32 MultiChoiceDialog::MultiChoiceDialog(QString title, QString heading, QWidget *parent) :
33 QDialog(parent)
34 {
35 setModal(true);
36 setWindowTitle(title);
37
38 QGridLayout *outer = new QGridLayout;
39 setLayout(outer);
40
41 outer->addWidget(new QLabel(heading), 0, 0, 1, 3);
42
43 QWidget *innerWidget = new QWidget;
44 outer->addWidget(innerWidget, 1, 0, 1, 3);
45 m_choiceLayout = new QHBoxLayout;
46 innerWidget->setLayout(m_choiceLayout);
47
48 m_descriptionLabel = new QLabel;
49 outer->addWidget(m_descriptionLabel, 2, 0, 1, 3);
50
51 QFont f = m_descriptionLabel->font();
52 f.setPointSize(f.pointSize() * 0.95);
53 m_descriptionLabel->setFont(f);
54
55 m_urlLabel = new QLabel(tr("&URL:"));
56 outer->addWidget(m_urlLabel, 3, 0);
57
58 m_urlCombo = new QComboBox();
59 m_urlCombo->setEditable(true);
60 m_urlLabel->setBuddy(m_urlCombo);
61 connect(m_urlCombo, SIGNAL(editTextChanged(const QString &)),
62 this, SLOT(urlChanged(const QString &)));
63 outer->addWidget(m_urlCombo, 3, 1, 1, 2);
64
65 m_fileLabel = new QLabel(tr("&File:"));
66 outer->addWidget(m_fileLabel, 4, 0);
67
68 m_fileCombo = new QComboBox();
69 m_fileCombo->setEditable(true);
70 m_fileLabel->setBuddy(m_fileCombo);
71 connect(m_fileCombo, SIGNAL(editTextChanged(const QString &)),
72 this, SLOT(fileChanged(const QString &)));
73 outer->addWidget(m_fileCombo, 4, 1);
74 outer->setColumnStretch(1, 20);
75
76 m_browseButton = new QPushButton(tr("Browse..."));
77 outer->addWidget(m_browseButton, 4, 2);
78 connect(m_browseButton, SIGNAL(clicked()), this, SLOT(browse()));
79
80 QDialogButtonBox *bbox = new QDialogButtonBox(QDialogButtonBox::Ok |
81 QDialogButtonBox::Cancel);
82 connect(bbox, SIGNAL(accepted()), this, SLOT(accept()));
83 connect(bbox, SIGNAL(rejected()), this, SLOT(reject()));
84 outer->addWidget(bbox, 5, 0, 1, 3);
85
86 m_okButton = bbox->button(QDialogButtonBox::Ok);
87 updateOkButton();
88
89 setMinimumWidth(480);
90 }
91
92 QString
93 MultiChoiceDialog::getCurrentChoice()
94 {
95 return m_currentChoice;
96 }
97
98 void
99 MultiChoiceDialog::setCurrentChoice(QString c)
100 {
101 m_currentChoice = c;
102 choiceChanged();
103 }
104
105 QString
106 MultiChoiceDialog::getArgument()
107 {
108 if (m_argTypes[m_currentChoice] == UrlArg ||
109 m_argTypes[m_currentChoice] == UrlToDirectoryArg) {
110 return m_urlCombo->currentText();
111 } else {
112 return m_fileCombo->currentText();
113 }
114 }
115
116 QString
117 MultiChoiceDialog::getAdditionalArgument()
118 {
119 if (m_argTypes[m_currentChoice] == UrlToDirectoryArg) {
120 return m_fileCombo->currentText();
121 } else {
122 return "";
123 }
124 }
125
126 void
127 MultiChoiceDialog::addRecentArgument(QString id, QString arg,
128 bool additionalArgument)
129 {
130 if (additionalArgument) {
131 RecentFiles(QString("Recent-%1-add").arg(id)).addFile(arg);
132 } else {
133 RecentFiles(QString("Recent-%1").arg(id)).addFile(arg);
134 }
135 }
136
137 void
138 MultiChoiceDialog::addChoice(QString id, QString text,
139 QString description, ArgType arg)
140 {
141 bool first = (m_texts.empty());
142
143 m_texts[id] = text;
144 m_descriptions[id] = description;
145 m_argTypes[id] = arg;
146
147 if (arg != NoArg) {
148 m_recentFiles[id] = QSharedPointer<RecentFiles>
149 (new RecentFiles(QString("Recent-%1").arg(id)));
150 }
151
152 SelectableLabel *cb = new SelectableLabel;
153 cb->setSelectedText(text);
154 cb->setUnselectedText(text);
155 cb->setMaximumWidth(270);
156
157 m_choiceLayout->addWidget(cb);
158 m_choiceButtons[cb] = id;
159
160 connect(cb, SIGNAL(selectionChanged()), this, SLOT(choiceChanged()));
161
162 if (first) {
163 m_currentChoice = id;
164 choiceChanged();
165 }
166 }
167
168 QString
169 MultiChoiceDialog::getDefaultPath() const
170 {
171 QDir home(QDir::home());
172 QDir dflt;
173
174 dflt = QDir(home.filePath(tr("My Documents")));
175 DEBUG << "testing " << dflt << endl;
176 if (dflt.exists()) return dflt.canonicalPath();
177
178 dflt = QDir(home.filePath(tr("Documents")));
179 DEBUG << "testing " << dflt << endl;
180 if (dflt.exists()) return dflt.canonicalPath();
181
182 DEBUG << "all failed, returning " << home << endl;
183 return home.canonicalPath();
184 }
185
186 void
187 MultiChoiceDialog::browse()
188 {
189 QString origin = getArgument();
190
191 if (origin == "") {
192 origin = getDefaultPath();
193 }
194
195 QString path = origin;
196
197 if (m_argTypes[m_currentChoice] == DirectoryArg ||
198 m_argTypes[m_currentChoice] == UrlToDirectoryArg) {
199
200 path = QFileDialog::getExistingDirectory
201 (this, tr("Open Directory"), origin,
202 QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
203 if (path != QString()) {
204 m_fileCombo->lineEdit()->setText(path + QDir::separator());
205 }
206
207 } else {
208
209 path = QFileDialog::getOpenFileName
210 (this, tr("Open File"), origin);
211 if (path != QString()) {
212 m_fileCombo->lineEdit()->setText(path);
213 }
214 }
215 }
216
217 void
218 MultiChoiceDialog::urlChanged(const QString &s)
219 {
220 updateOkButton();
221 }
222
223 void
224 MultiChoiceDialog::fileChanged(const QString &s)
225 {
226 updateOkButton();
227 }
228
229 void
230 MultiChoiceDialog::updateOkButton()
231 {
232 /* This doesn't work well
233 if (m_argTypes[m_currentChoice] != UrlToDirectoryArg) {
234 return;
235 }
236 QDir dirPath(m_fileCombo->currentText());
237 if (!dirPath.exists()) {
238 if (!dirPath.cdUp()) return;
239 }
240 QString url = m_urlCombo->currentText();
241 if (QRegExp("^\\w+://").indexIn(url) < 0) {
242 return;
243 }
244 QString urlDirName = url;
245 urlDirName.replace(QRegExp("^.*\\//.*\\/"), "");
246 if (urlDirName == "" || urlDirName == url) {
247 return;
248 }
249 m_fileCombo->lineEdit()->setText(dirPath.filePath(urlDirName));
250 */
251 if (m_argTypes[m_currentChoice] == UrlToDirectoryArg) {
252 m_okButton->setEnabled(getArgument() != "" &&
253 getAdditionalArgument() != "");
254 } else {
255 m_okButton->setEnabled(getArgument() != "");
256 }
257 }
258
259 void
260 MultiChoiceDialog::choiceChanged()
261 {
262 DEBUG << "choiceChanged" << endl;
263
264 if (m_choiceButtons.empty()) return;
265
266 QString id = "";
267
268 QObject *s = sender();
269 QWidget *w = qobject_cast<QWidget *>(s);
270 if (w) id = m_choiceButtons[w];
271
272 if (id == m_currentChoice) return;
273 if (id == "") {
274 // Happens when this is called for the very first time, when
275 // m_currentChoice has been set to the intended ID but no
276 // button has actually been pressed -- then we need to
277 // initialise
278 id = m_currentChoice;
279 }
280
281 m_currentChoice = id;
282
283 foreach (QWidget *cw, m_choiceButtons.keys()) {
284 SelectableLabel *sl = qobject_cast<SelectableLabel *>(cw);
285 if (sl) {
286 sl->setSelected(m_choiceButtons[cw] == id);
287 }
288 }
289
290 m_descriptionLabel->setText(m_descriptions[id]);
291
292 m_fileLabel->hide();
293 m_fileCombo->hide();
294 m_browseButton->hide();
295 m_urlLabel->hide();
296 m_urlCombo->hide();
297
298 QSharedPointer<RecentFiles> rf = m_recentFiles[id];
299 m_fileCombo->clear();
300 m_urlCombo->clear();
301
302 switch (m_argTypes[id]) {
303
304 case NoArg:
305 break;
306
307 case FileArg:
308 m_fileLabel->setText(tr("&File:"));
309 m_fileLabel->show();
310 m_fileCombo->show();
311 m_fileCombo->addItems(rf->getRecent());
312 m_browseButton->show();
313 break;
314
315 case DirectoryArg:
316 m_fileLabel->setText(tr("&Folder:"));
317 m_fileLabel->show();
318 m_fileCombo->show();
319 m_fileCombo->addItems(rf->getRecent());
320 m_browseButton->show();
321 break;
322
323 case UrlArg:
324 m_urlLabel->show();
325 m_urlCombo->show();
326 m_urlCombo->addItems(rf->getRecent());
327 break;
328
329 case UrlToDirectoryArg:
330 m_urlLabel->show();
331 m_urlCombo->show();
332 m_urlCombo->addItems(rf->getRecent());
333 m_fileLabel->setText(tr("&Folder:"));
334 m_fileLabel->show();
335 m_fileCombo->show();
336 m_fileCombo->lineEdit()->setText(getDefaultPath());
337 m_browseButton->show();
338 break;
339 }
340
341 updateOkButton();
342 adjustSize();
343 }
344
345