comparison multichoicedialog.cpp @ 69:6d5a5571caec

* Basic working "Open Repository" dialog
author Chris Cannam
date Thu, 18 Nov 2010 15:14:39 +0000
parents b918e57c7bea
children 121cb1032717
comparison
equal deleted inserted replaced
68:b918e57c7bea 69:6d5a5571caec
15 COPYING included with this distribution for more information. 15 COPYING included with this distribution for more information.
16 */ 16 */
17 17
18 #include "multichoicedialog.h" 18 #include "multichoicedialog.h"
19 19
20 #include "selectablelabel.h"
21
22 #include "debug.h"
23
20 #include <QDialogButtonBox> 24 #include <QDialogButtonBox>
25 #include <QToolButton>
26 #include <QPushButton>
27 #include <QFont>
28 #include <QDir>
29 #include <QFileDialog>
21 30
22 MultiChoiceDialog::MultiChoiceDialog(QString title, QString heading, QWidget *parent) : 31 MultiChoiceDialog::MultiChoiceDialog(QString title, QString heading, QWidget *parent) :
23 QDialog(parent) 32 QDialog(parent)
24 { 33 {
25 setModal(true); 34 setModal(true);
29 setLayout(outer); 38 setLayout(outer);
30 39
31 outer->addWidget(new QLabel(heading), 0, 0, 1, 3); 40 outer->addWidget(new QLabel(heading), 0, 0, 1, 3);
32 41
33 QWidget *innerWidget = new QWidget; 42 QWidget *innerWidget = new QWidget;
34 outer->addWidget(innerWidget, 1, 0, 1, 2); 43 outer->addWidget(innerWidget, 1, 0, 1, 3);
35 m_choiceLayout = new QGridLayout; 44 m_choiceLayout = new QHBoxLayout;
36 innerWidget->setLayout(m_choiceLayout); 45 innerWidget->setLayout(m_choiceLayout);
37 46
38 m_descriptionLabel = new QLabel; 47 m_descriptionLabel = new QLabel;
39 outer->addWidget(m_descriptionLabel, 2, 0, 1, 3); 48 outer->addWidget(m_descriptionLabel, 2, 0, 1, 3);
40 49
50 QFont f = m_descriptionLabel->font();
51 f.setPointSize(f.pointSize() * 0.9);
52 m_descriptionLabel->setFont(f);
53
41 m_argLabel = new QLabel(); 54 m_argLabel = new QLabel();
42 outer->addWidget(m_argLabel, 3, 0); 55 outer->addWidget(m_argLabel, 3, 0);
43 56
44 m_argEdit = new QLineEdit(); 57 m_argEdit = new QComboBox();
58 m_argEdit->setEditable(true);
45 outer->addWidget(m_argEdit, 3, 1); 59 outer->addWidget(m_argEdit, 3, 1);
60 outer->setColumnStretch(1, 20);
46 61
47 m_browseButton = new QPushButton(tr("Browse...")); 62 m_browseButton = new QPushButton(tr("Browse..."));
48 outer->addWidget(m_browseButton, 3, 2); 63 outer->addWidget(m_browseButton, 3, 2);
64 connect(m_browseButton, SIGNAL(clicked()), this, SLOT(browse()));
49 65
50 QDialogButtonBox *bbox = new QDialogButtonBox(QDialogButtonBox::Ok | 66 QDialogButtonBox *bbox = new QDialogButtonBox(QDialogButtonBox::Ok |
51 QDialogButtonBox::Cancel); 67 QDialogButtonBox::Cancel);
52 connect(bbox, SIGNAL(accepted()), this, SLOT(accept())); 68 connect(bbox, SIGNAL(accepted()), this, SLOT(accept()));
53 connect(bbox, SIGNAL(rejected()), this, SLOT(reject())); 69 connect(bbox, SIGNAL(rejected()), this, SLOT(reject()));
54 outer->addWidget(bbox, 4, 0, 1, 3); 70 outer->addWidget(bbox, 4, 0, 1, 3);
55 } 71
56 72 setMinimumWidth(480);
73 }
74
75 QString
76 MultiChoiceDialog::getCurrentChoice()
77 {
78 return m_currentChoice;
79 }
80
81 void
82 MultiChoiceDialog::setCurrentChoice(QString c)
83 {
84 m_currentChoice = c;
85 choiceChanged();
86 }
87
88 QString
89 MultiChoiceDialog::getArgument()
90 {
91 return m_argEdit->currentText();
92 }
93
94 void
95 MultiChoiceDialog::addRecentArgument(QString id, QString arg)
96 {
97 RecentFiles(QString("Recent-%1").arg(id)).addFile(arg);
98 }
99
100 void
101 MultiChoiceDialog::addChoice(QString id, QString text,
102 QString description, ArgType arg)
103 {
104 bool first = (m_texts.empty());
105
106 m_texts[id] = text;
107 m_descriptions[id] = description;
108 m_argTypes[id] = arg;
109
110 if (arg != NoArg) {
111 m_recentFiles[id] = QSharedPointer<RecentFiles>
112 (new RecentFiles(QString("Recent-%1").arg(id)));
113 }
114
115 SelectableLabel *cb = new SelectableLabel;
116 cb->setSelectedText(text);
117 cb->setUnselectedText(text);
118
119 m_choiceLayout->addWidget(cb);
120 m_choiceButtons[cb] = id;
121
122 connect(cb, SIGNAL(selectionChanged()), this, SLOT(choiceChanged()));
123
124 if (first) {
125 m_currentChoice = id;
126 choiceChanged();
127 }
128 }
129
130 void
131 MultiChoiceDialog::browse()
132 {
133 QString origin = getArgument();
134
135 if (origin == "") {
136 #ifdef Q_OS_WIN32
137 origin = "c:";
138 #else
139 origin = QDir::homePath();
140 #endif
141 }
142
143 QString path = origin;
144
145 if (m_argTypes[m_currentChoice] == DirectoryArg) {
146
147 path = QFileDialog::getExistingDirectory
148 (this, tr("Open Directory"), origin,
149 QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
150 if (path != QString()) {
151 m_argEdit->lineEdit()->setText(path + QDir::separator());
152 }
153
154 } else {
155
156 path = QFileDialog::getOpenFileName
157 (this, tr("Open File"), origin);
158 if (path != QString()) {
159 m_argEdit->lineEdit()->setText(path);
160 }
161 }
162 }
163
164 void
165 MultiChoiceDialog::choiceChanged()
166 {
167 DEBUG << "choiceChanged" << endl;
168
169 if (m_choiceButtons.empty()) return;
170
171 QString id = "";
172
173 QObject *s = sender();
174 QWidget *w = qobject_cast<QWidget *>(s);
175 if (w) id = m_choiceButtons[w];
176
177 if (id == m_currentChoice) return;
178 if (id == "") {
179 // Happens when this is called for the very first time, when
180 // m_currentChoice has been set to the intended ID but no
181 // button has actually been pressed -- then we need to
182 // initialise
183 id = m_currentChoice;
184 }
185
186 m_currentChoice = id;
187
188 foreach (QWidget *cw, m_choiceButtons.keys()) {
189 SelectableLabel *sl = qobject_cast<SelectableLabel *>(cw);
190 if (sl) {
191 sl->setSelected(m_choiceButtons[cw] == id);
192 }
193 }
194
195 m_descriptionLabel->setText(m_descriptions[id]);
196
197 switch (m_argTypes[id]) {
198
199 case NoArg:
200 m_argLabel->hide();
201 m_argEdit->hide();
202 m_browseButton->hide();
203 break;
204
205 case FileArg:
206 m_argLabel->setText(tr("File:"));
207 m_argLabel->show();
208 m_argEdit->show();
209 m_browseButton->show();
210 break;
211
212 case DirectoryArg:
213 m_argLabel->setText(tr("Folder:"));
214 m_argLabel->show();
215 m_argEdit->show();
216 m_browseButton->show();
217 break;
218
219 case UrlArg:
220 m_argLabel->setText(tr("URL:"));
221 m_argLabel->show();
222 m_argEdit->show();
223 m_browseButton->hide();
224 break;
225
226 case FileOrUrlArg:
227 m_argLabel->setText(tr("File or URL:"));
228 m_argLabel->show();
229 m_argEdit->show();
230 m_browseButton->show();
231 break;
232 }
233
234 if (m_argTypes[id] != NoArg) {
235 QSharedPointer<RecentFiles> rf = m_recentFiles[id];
236 m_argEdit->clear();
237 m_argEdit->addItems(rf->getRecent());
238 }
239
240 adjustSize();
241 }
242
243