comparison src/settingsdialog.cpp @ 370:b9c153e00e84

Move source files to src/
author Chris Cannam
date Thu, 24 Mar 2011 10:27:51 +0000
parents settingsdialog.cpp@cc95394e2392
children c567fed39559
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 "settingsdialog.h"
19 #include "common.h"
20 #include "debug.h"
21
22 #include <QGridLayout>
23 #include <QGroupBox>
24 #include <QDialogButtonBox>
25 #include <QSettings>
26 #include <QDir>
27 #include <QFileDialog>
28 #include <QMessageBox>
29
30 QString
31 SettingsDialog::m_installPath;
32
33 SettingsDialog::SettingsDialog(QWidget *parent) :
34 QDialog(parent),
35 m_presentationChanged(false)
36 {
37 setModal(true);
38 setWindowTitle(tr("Settings"));
39
40 QGridLayout *mainLayout = new QGridLayout;
41 setLayout(mainLayout);
42
43
44
45 QGroupBox *meBox = new QGroupBox(tr("User details"));
46 mainLayout->addWidget(meBox, 0, 0);
47 QGridLayout *meLayout = new QGridLayout;
48 meBox->setLayout(meLayout);
49
50 int row = 0;
51
52 meLayout->addWidget(new QLabel(tr("Name:")), row, 0);
53
54 m_nameEdit = new QLineEdit();
55 meLayout->addWidget(m_nameEdit, row++, 1);
56
57 meLayout->addWidget(new QLabel(tr("Email address:")), row, 0);
58
59 m_emailEdit = new QLineEdit();
60 meLayout->addWidget(m_emailEdit, row++, 1);
61
62
63
64 QGroupBox *lookBox = new QGroupBox(tr("Presentation"));
65 mainLayout->addWidget(lookBox, 1, 0);
66 QGridLayout *lookLayout = new QGridLayout;
67 lookBox->setLayout(lookLayout);
68
69 row = 0;
70
71 m_showIconLabels = new QCheckBox(tr("Show labels on toolbar icons"));
72 lookLayout->addWidget(m_showIconLabels, row++, 0, 1, 2);
73
74 m_showExtraText = new QCheckBox(tr("Show long descriptions for file status headings"));
75 lookLayout->addWidget(m_showExtraText, row++, 0, 1, 2);
76
77 #ifdef NOT_IMPLEMENTED_YET
78 lookLayout->addWidget(new QLabel(tr("Place the work and history views")), row, 0);
79 m_workHistoryArrangement = new QComboBox();
80 m_workHistoryArrangement->addItem(tr("In separate tabs"));
81 m_workHistoryArrangement->addItem(tr("Side-by-side in a single pane"));
82 lookLayout->addWidget(m_workHistoryArrangement, row++, 1, Qt::AlignLeft);
83 lookLayout->setColumnStretch(1, 20);
84 #endif
85
86 lookLayout->addWidget(new QLabel(tr("Label the history timeline with")), row, 0);
87 m_dateFormat = new QComboBox();
88 m_dateFormat->addItem(tr("Ages, for example \"5 weeks ago\""));
89 m_dateFormat->addItem(tr("Dates, for example \"2010-06-23\""));
90 lookLayout->addWidget(m_dateFormat, row++, 1, Qt::AlignLeft);
91 lookLayout->setColumnStretch(1, 20);
92
93
94 QGroupBox *pathsBox = new QGroupBox(tr("System application locations"));
95 mainLayout->addWidget(pathsBox, 2, 0);
96 QGridLayout *pathsLayout = new QGridLayout;
97 pathsBox->setLayout(pathsLayout);
98
99 row = 0;
100
101 pathsLayout->addWidget(new QLabel(tr("Mercurial (hg) program:")), row, 0);
102
103 m_hgPathLabel = new QLineEdit();
104 pathsLayout->addWidget(m_hgPathLabel, row, 2);
105
106 QPushButton *browse = new QPushButton(tr("Browse..."));
107 pathsLayout->addWidget(browse, row++, 1);
108 connect(browse, SIGNAL(clicked()), this, SLOT(hgPathBrowse()));
109
110 pathsLayout->addWidget(new QLabel(tr("External diff program:")), row, 0);
111
112 m_diffPathLabel = new QLineEdit();
113 pathsLayout->addWidget(m_diffPathLabel, row, 2);
114
115 browse = new QPushButton(tr("Browse..."));
116 pathsLayout->addWidget(browse, row++, 1);
117 connect(browse, SIGNAL(clicked()), this, SLOT(diffPathBrowse()));
118
119 pathsLayout->addWidget(new QLabel(tr("External file-merge program:")), row, 0);
120
121 m_mergePathLabel = new QLineEdit();
122 pathsLayout->addWidget(m_mergePathLabel, row, 2);
123
124 browse = new QPushButton(tr("Browse..."));
125 pathsLayout->addWidget(browse, row++, 1);
126 connect(browse, SIGNAL(clicked()), this, SLOT(mergePathBrowse()));
127
128 pathsLayout->addWidget(new QLabel(tr("External text editor:")), row, 0);
129
130 m_editPathLabel = new QLineEdit();
131 pathsLayout->addWidget(m_editPathLabel, row, 2);
132
133 browse = new QPushButton(tr("Browse..."));
134 pathsLayout->addWidget(browse, row++, 1);
135 connect(browse, SIGNAL(clicked()), this, SLOT(editPathBrowse()));
136
137 pathsLayout->addWidget(new QLabel(tr("EasyHg Mercurial extension:")), row, 0);
138
139 m_extensionPathLabel = new QLineEdit();
140 pathsLayout->addWidget(m_extensionPathLabel, row, 2);
141
142 browse = new QPushButton(tr("Browse..."));
143 pathsLayout->addWidget(browse, row++, 1);
144 connect(browse, SIGNAL(clicked()), this, SLOT(extensionPathBrowse()));
145
146 //!!! more info plz
147 m_useExtension = new QCheckBox(tr("Use EasyHg Mercurial extension"));
148 pathsLayout->addWidget(m_useExtension, row++, 2);
149
150
151 reset(); // loads current defaults from settings
152
153
154 QDialogButtonBox *bbox = new QDialogButtonBox(QDialogButtonBox::Ok);
155 connect(bbox->addButton(tr("Restore defaults"), QDialogButtonBox::ResetRole),
156 SIGNAL(clicked()), this, SLOT(restoreDefaults()));
157 connect(bbox, SIGNAL(accepted()), this, SLOT(accept()));
158 mainLayout->addWidget(bbox, 3, 0);
159 m_ok = bbox->button(QDialogButtonBox::Ok);
160 }
161
162 void
163 SettingsDialog::hgPathBrowse()
164 {
165 browseFor(tr("Mercurial program"), m_hgPathLabel);
166 }
167
168 void
169 SettingsDialog::diffPathBrowse()
170 {
171 browseFor(tr("External diff program"), m_diffPathLabel);
172 }
173
174 void
175 SettingsDialog::mergePathBrowse()
176 {
177 browseFor(tr("External file-merge program"), m_mergePathLabel);
178 }
179
180 void
181 SettingsDialog::editPathBrowse()
182 {
183 browseFor(tr("External text editor"), m_editPathLabel);
184 }
185
186 void
187 SettingsDialog::extensionPathBrowse()
188 {
189 browseFor(tr("EasyHg Mercurial extension"), m_extensionPathLabel);
190 }
191
192 void
193 SettingsDialog::browseFor(QString title, QLineEdit *edit)
194 {
195 QString origin = edit->text();
196
197 if (origin == "") {
198 #ifdef Q_OS_WIN32
199 origin = "c:";
200 #else
201 origin = QDir::homePath();
202 #endif
203 }
204
205 QString path = QFileDialog::getOpenFileName(this, title, origin);
206 if (path != QString()) {
207 edit->setText(path);
208 }
209 }
210
211 void
212 SettingsDialog::restoreDefaults()
213 {
214 if (QMessageBox::question
215 (this, tr("Restore default settings?"),
216 tr("<qt><b>Restore default settings?</b><br><br>Are you sure you want to reset all settings to their default values?"),
217 QMessageBox::Ok | QMessageBox::Cancel,
218 QMessageBox::Cancel) == QMessageBox::Ok) {
219 clear();
220 findDefaultLocations();
221 reset();
222 }
223 }
224
225 void
226 SettingsDialog::findDefaultLocations(QString installPath)
227 {
228 m_installPath = installPath;
229 findHgBinaryName();
230 findExtension();
231 findDiffBinaryName();
232 findMergeBinaryName();
233 findEditorBinaryName();
234 }
235
236 void
237 SettingsDialog::findHgBinaryName()
238 {
239 QSettings settings;
240 settings.beginGroup("Locations");
241 QString hg = settings.value("hgbinary", "").toString();
242 if (hg == "") {
243 hg = findInPath("hg", m_installPath, true);
244 }
245 if (hg != "") {
246 settings.setValue("hgbinary", hg);
247 }
248 }
249
250 QString
251 SettingsDialog::getUnbundledExtensionFileName()
252 {
253 QString home = QDir::homePath();
254 QString target = QString("%1/.easyhg").arg(home);
255 QString extpath = QString("%1/easyhg.py").arg(target);
256 return extpath;
257 }
258
259 void
260 SettingsDialog::findExtension()
261 {
262 QSettings settings;
263 settings.beginGroup("Locations");
264
265 QString extpath = settings.value("extensionpath", "").toString();
266 if (extpath != "" || !QFile(extpath).exists()) {
267
268 extpath = getUnbundledExtensionFileName();
269
270 if (!QFile(extpath).exists()) {
271 extpath = findInPath("easyhg.py", m_installPath, false);
272 }
273 }
274
275 settings.setValue("extensionpath", extpath);
276 }
277
278 void
279 SettingsDialog::findDiffBinaryName()
280 {
281 QSettings settings;
282 settings.beginGroup("Locations");
283 QString diff = settings.value("extdiffbinary", "").toString();
284 if (diff == "") {
285 QStringList bases;
286 #ifdef Q_OS_WIN32
287 bases << "easyhg-extdiff.bat";
288 #else
289 bases << "easyhg-extdiff.sh";
290 #endif
291 bases << "kompare" << "kdiff3" << "meld";
292 bool found = false;
293 foreach (QString base, bases) {
294 diff = findInPath(base, m_installPath, true);
295 if (diff != "") {
296 found = true;
297 break;
298 }
299 }
300 if (found) {
301 settings.setValue("extdiffbinary", diff);
302 }
303 }
304 }
305
306 void
307 SettingsDialog::findMergeBinaryName()
308 {
309 QSettings settings;
310 settings.beginGroup("Locations");
311 if (settings.contains("mergebinary")) {
312 return;
313 }
314 QString merge;
315 QStringList bases;
316 #ifdef Q_OS_WIN32
317 bases << "easyhg-merge.bat";
318 #else
319 bases << "easyhg-merge.sh";
320 #endif
321 // NB it's not a good idea to add other tools here, as command
322 // line argument ordering varies. Configure them through hgrc
323 // instead
324 bool found = false;
325 foreach (QString base, bases) {
326 merge = findInPath(base, m_installPath, true);
327 if (merge != "") {
328 found = true;
329 break;
330 }
331 }
332 if (found) {
333 settings.setValue("mergebinary", merge);
334 }
335 }
336
337 void
338 SettingsDialog::findEditorBinaryName()
339 {
340 QSettings settings;
341 settings.beginGroup("Locations");
342 QString editor = settings.value("editorbinary", "").toString();
343 if (editor == "") {
344 QStringList bases;
345 bases
346 #if defined Q_OS_WIN32
347 << "wordpad.exe"
348 << "C:\\Program Files\\Windows NT\\Accessories\\wordpad.exe"
349 << "notepad.exe"
350 #elif defined Q_OS_MAC
351 << "/Applications/TextEdit.app/Contents/MacOS/TextEdit"
352 #else
353 << "gedit" << "kate"
354 #endif
355 ;
356 bool found = false;
357 foreach (QString base, bases) {
358 editor = findInPath(base, m_installPath, true);
359 if (editor != "") {
360 found = true;
361 break;
362 }
363 }
364 if (found) {
365 settings.setValue("editorbinary", editor);
366 }
367 }
368 }
369
370 void
371 SettingsDialog::clear()
372 {
373 // Clear everything that has a default setting
374 DEBUG << "SettingsDialog::clear" << endl;
375 QSettings settings;
376 settings.beginGroup("Presentation");
377 settings.remove("showiconlabels");
378 settings.remove("showhelpfultext");
379 settings.endGroup();
380 settings.beginGroup("Locations");
381 settings.remove("hgbinary");
382 settings.remove("extdiffbinary");
383 settings.remove("mergebinary");
384 settings.remove("editorbinary");
385 settings.remove("extensionpath");
386 settings.endGroup();
387 settings.beginGroup("General");
388 settings.remove("useextension");
389 settings.endGroup();
390 }
391
392 void
393 SettingsDialog::reset()
394 {
395 DEBUG << "SettingsDialog::reset" << endl;
396 QSettings settings;
397 settings.beginGroup("User Information");
398 m_nameEdit->setText(settings.value("name", getUserRealName()).toString());
399 m_emailEdit->setText(settings.value("email").toString());
400 settings.endGroup();
401 settings.beginGroup("Presentation");
402 m_showIconLabels->setChecked(settings.value("showiconlabels", true).toBool());
403 m_showExtraText->setChecked(settings.value("showhelpfultext", true).toBool());
404 #ifdef NOT_IMPLEMENTED_YET
405 m_workHistoryArrangement->setCurrentIndex(settings.value("workhistoryarrangement", 0).toInt());
406 #endif
407 m_dateFormat->setCurrentIndex(settings.value("dateformat", 0).toInt());
408 settings.endGroup();
409 settings.beginGroup("Locations");
410 m_hgPathLabel->setText(settings.value("hgbinary").toString());
411 m_diffPathLabel->setText(settings.value("extdiffbinary").toString());
412 m_mergePathLabel->setText(settings.value("mergebinary").toString());
413 m_editPathLabel->setText(settings.value("editorbinary").toString());
414 m_extensionPathLabel->setText(settings.value("extensionpath").toString());
415 settings.endGroup();
416 settings.beginGroup("General");
417 m_useExtension->setChecked(settings.value("useextension", true).toBool());
418 settings.endGroup();
419 }
420
421 void
422 SettingsDialog::accept()
423 {
424 DEBUG << "SettingsDialog::accept" << endl;
425 QSettings settings;
426 settings.beginGroup("User Information");
427 settings.setValue("name", m_nameEdit->text());
428 settings.setValue("email", m_emailEdit->text());
429 settings.endGroup();
430 settings.beginGroup("Presentation");
431 bool b;
432 b = m_showIconLabels->isChecked();
433 if (b != settings.value("showiconlabels", true)) {
434 settings.setValue("showiconlabels", b);
435 m_presentationChanged = true;
436 }
437 b = m_showExtraText->isChecked();
438 if (b != settings.value("showhelpfultext", true)) {
439 settings.setValue("showhelpfultext", b);
440 m_presentationChanged = true;
441 }
442 int i;
443 #ifdef NOT_IMPLEMENTED_YET
444 i = m_workHistoryArrangement->currentIndex();
445 if (i != settings.value("workhistoryarrangement", 0)) {
446 settings.setValue("workhistoryarrangement", i);
447 m_presentationChanged = true;
448 }
449 #endif
450 i = m_dateFormat->currentIndex();
451 if (i != settings.value("dateformat", 0)) {
452 settings.setValue("dateformat", i);
453 m_presentationChanged = true;
454 }
455 settings.endGroup();
456 settings.beginGroup("Locations");
457 settings.setValue("hgbinary", m_hgPathLabel->text());
458 settings.setValue("extdiffbinary", m_diffPathLabel->text());
459 settings.setValue("mergebinary", m_mergePathLabel->text());
460 settings.setValue("editorbinary", m_editPathLabel->text());
461 settings.setValue("extensionpath", m_extensionPathLabel->text());
462 settings.endGroup();
463 settings.beginGroup("General");
464 settings.setValue("useextension", m_useExtension->isChecked());
465 settings.endGroup();
466 QDialog::accept();
467 }
468
469