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