annotate settingsdialog.cpp @ 242:3f4ba5e4b4be easyhg_v0.2

* Bump version
author Chris Cannam
date Mon, 10 Jan 2011 15:34:15 +0000
parents 53ad43d5a463
children 8fd71f570884
rev   line source
Chris@175 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@175 2
Chris@175 3 /*
Chris@175 4 EasyMercurial
Chris@175 5
Chris@175 6 Based on HgExplorer by Jari Korhonen
Chris@175 7 Copyright (c) 2010 Jari Korhonen
Chris@175 8 Copyright (c) 2010 Chris Cannam
Chris@175 9 Copyright (c) 2010 Queen Mary, University of London
Chris@175 10
Chris@175 11 This program is free software; you can redistribute it and/or
Chris@175 12 modify it under the terms of the GNU General Public License as
Chris@175 13 published by the Free Software Foundation; either version 2 of the
Chris@175 14 License, or (at your option) any later version. See the file
Chris@175 15 COPYING included with this distribution for more information.
Chris@175 16 */
Chris@175 17
Chris@175 18 #include "settingsdialog.h"
Chris@175 19 #include "common.h"
Chris@177 20 #include "debug.h"
Chris@175 21
Chris@175 22 #include <QGridLayout>
Chris@175 23 #include <QGroupBox>
Chris@175 24 #include <QDialogButtonBox>
Chris@175 25 #include <QSettings>
Chris@181 26 #include <QDir>
Chris@181 27 #include <QFileDialog>
Chris@175 28
Chris@239 29 QString
Chris@239 30 SettingsDialog::m_installPath;
Chris@239 31
Chris@175 32 SettingsDialog::SettingsDialog(QWidget *parent) :
Chris@230 33 QDialog(parent),
Chris@230 34 m_presentationChanged(false)
Chris@175 35 {
Chris@175 36 setModal(true);
Chris@175 37 setWindowTitle(tr("Settings"));
Chris@175 38
Chris@175 39 QGridLayout *mainLayout = new QGridLayout;
Chris@175 40 setLayout(mainLayout);
Chris@175 41
Chris@230 42
Chris@230 43
Chris@181 44 QGroupBox *meBox = new QGroupBox(tr("User details"));
Chris@175 45 mainLayout->addWidget(meBox, 0, 0);
Chris@175 46 QGridLayout *meLayout = new QGridLayout;
Chris@175 47 meBox->setLayout(meLayout);
Chris@175 48
Chris@175 49 int row = 0;
Chris@175 50
Chris@175 51 meLayout->addWidget(new QLabel(tr("Name:")), row, 0);
Chris@175 52
Chris@175 53 m_nameEdit = new QLineEdit();
Chris@175 54 meLayout->addWidget(m_nameEdit, row++, 1);
Chris@175 55
Chris@175 56 meLayout->addWidget(new QLabel(tr("Email address:")), row, 0);
Chris@175 57
Chris@175 58 m_emailEdit = new QLineEdit();
Chris@175 59 meLayout->addWidget(m_emailEdit, row++, 1);
Chris@175 60
Chris@230 61
Chris@230 62
Chris@230 63 QGroupBox *lookBox = new QGroupBox(tr("Presentation"));
Chris@230 64 mainLayout->addWidget(lookBox, 1, 0);
Chris@230 65 QGridLayout *lookLayout = new QGridLayout;
Chris@230 66 lookBox->setLayout(lookLayout);
Chris@230 67
Chris@230 68 row = 0;
Chris@230 69
Chris@230 70 m_showIconLabels = new QCheckBox(tr("Show labels on toolbar icons"));
Chris@230 71 lookLayout->addWidget(m_showIconLabels, row++, 0);
Chris@230 72
Chris@230 73 m_showExtraText = new QCheckBox(tr("Show long descriptions for file status headings"));
Chris@230 74 lookLayout->addWidget(m_showExtraText, row++, 0);
Chris@230 75
Chris@230 76
Chris@230 77
Chris@175 78 QGroupBox *pathsBox = new QGroupBox(tr("System application locations"));
Chris@230 79 mainLayout->addWidget(pathsBox, 2, 0);
Chris@175 80 QGridLayout *pathsLayout = new QGridLayout;
Chris@175 81 pathsBox->setLayout(pathsLayout);
Chris@175 82
Chris@175 83 row = 0;
Chris@175 84
Chris@175 85 pathsLayout->addWidget(new QLabel(tr("Mercurial (hg) program:")), row, 0);
Chris@175 86
Chris@195 87 m_hgPathLabel = new QLineEdit();
Chris@181 88 pathsLayout->addWidget(m_hgPathLabel, row, 2);
Chris@175 89
Chris@175 90 QPushButton *browse = new QPushButton(tr("Browse..."));
Chris@181 91 pathsLayout->addWidget(browse, row++, 1);
Chris@175 92 connect(browse, SIGNAL(clicked()), this, SLOT(hgPathBrowse()));
Chris@175 93
Chris@175 94 pathsLayout->addWidget(new QLabel(tr("External diff program:")), row, 0);
Chris@175 95
Chris@195 96 m_diffPathLabel = new QLineEdit();
Chris@181 97 pathsLayout->addWidget(m_diffPathLabel, row, 2);
Chris@175 98
Chris@175 99 browse = new QPushButton(tr("Browse..."));
Chris@181 100 pathsLayout->addWidget(browse, row++, 1);
Chris@175 101 connect(browse, SIGNAL(clicked()), this, SLOT(diffPathBrowse()));
Chris@178 102
Chris@175 103 pathsLayout->addWidget(new QLabel(tr("External file-merge program:")), row, 0);
Chris@175 104
Chris@195 105 m_mergePathLabel = new QLineEdit();
Chris@181 106 pathsLayout->addWidget(m_mergePathLabel, row, 2);
Chris@175 107
Chris@175 108 browse = new QPushButton(tr("Browse..."));
Chris@181 109 pathsLayout->addWidget(browse, row++, 1);
Chris@175 110 connect(browse, SIGNAL(clicked()), this, SLOT(mergePathBrowse()));
Chris@175 111
Chris@178 112 pathsLayout->addWidget(new QLabel(tr("External text editor:")), row, 0);
Chris@178 113
Chris@195 114 m_editPathLabel = new QLineEdit();
Chris@181 115 pathsLayout->addWidget(m_editPathLabel, row, 2);
Chris@178 116
Chris@178 117 browse = new QPushButton(tr("Browse..."));
Chris@181 118 pathsLayout->addWidget(browse, row++, 1);
Chris@178 119 connect(browse, SIGNAL(clicked()), this, SLOT(editPathBrowse()));
Chris@178 120
Chris@176 121 pathsLayout->addWidget(new QLabel(tr("EasyHg Mercurial extension:")), row, 0);
Chris@176 122
Chris@195 123 m_extensionPathLabel = new QLineEdit();
Chris@181 124 pathsLayout->addWidget(m_extensionPathLabel, row, 2);
Chris@176 125
Chris@176 126 browse = new QPushButton(tr("Browse..."));
Chris@181 127 pathsLayout->addWidget(browse, row++, 1);
Chris@176 128 connect(browse, SIGNAL(clicked()), this, SLOT(extensionPathBrowse()));
Chris@176 129
Chris@176 130 //!!! more info plz
Chris@176 131 m_useExtension = new QCheckBox(tr("Use EasyHg Mercurial extension"));
Chris@181 132 pathsLayout->addWidget(m_useExtension, row++, 2);
Chris@176 133
Chris@176 134
Chris@239 135 reset(); // loads current defaults from settings
Chris@175 136
Chris@230 137
Chris@175 138 QDialogButtonBox *bbox = new QDialogButtonBox(QDialogButtonBox::Ok);
Chris@239 139 connect(bbox->addButton(tr("Restore defaults"), QDialogButtonBox::ResetRole),
Chris@239 140 SIGNAL(clicked()), this, SLOT(restoreDefaults()));
Chris@175 141 connect(bbox, SIGNAL(accepted()), this, SLOT(accept()));
Chris@230 142 mainLayout->addWidget(bbox, 3, 0);
Chris@175 143 m_ok = bbox->button(QDialogButtonBox::Ok);
Chris@175 144 }
Chris@175 145
Chris@175 146 void
Chris@175 147 SettingsDialog::hgPathBrowse()
Chris@175 148 {
Chris@181 149 browseFor(tr("Mercurial program"), m_hgPathLabel);
Chris@175 150 }
Chris@175 151
Chris@175 152 void
Chris@175 153 SettingsDialog::diffPathBrowse()
Chris@175 154 {
Chris@181 155 browseFor(tr("External diff program"), m_diffPathLabel);
Chris@175 156 }
Chris@175 157
Chris@175 158 void
Chris@175 159 SettingsDialog::mergePathBrowse()
Chris@175 160 {
Chris@181 161 browseFor(tr("External file-merge program"), m_mergePathLabel);
Chris@175 162 }
Chris@175 163
Chris@175 164 void
Chris@178 165 SettingsDialog::editPathBrowse()
Chris@175 166 {
Chris@181 167 browseFor(tr("External text editor"), m_editPathLabel);
Chris@176 168 }
Chris@176 169
Chris@176 170 void
Chris@176 171 SettingsDialog::extensionPathBrowse()
Chris@176 172 {
Chris@181 173 browseFor(tr("EasyHg Mercurial extension"), m_extensionPathLabel);
Chris@181 174 }
Chris@181 175
Chris@181 176 void
Chris@195 177 SettingsDialog::browseFor(QString title, QLineEdit *edit)
Chris@181 178 {
Chris@181 179 QString origin = edit->text();
Chris@181 180
Chris@181 181 if (origin == "") {
Chris@181 182 #ifdef Q_OS_WIN32
Chris@181 183 origin = "c:";
Chris@181 184 #else
Chris@181 185 origin = QDir::homePath();
Chris@181 186 #endif
Chris@181 187 }
Chris@181 188
Chris@181 189 QString path = QFileDialog::getOpenFileName(this, title, origin);
Chris@181 190 if (path != QString()) {
Chris@181 191 edit->setText(path);
Chris@181 192 }
Chris@176 193 }
Chris@176 194
Chris@176 195 void
Chris@239 196 SettingsDialog::restoreDefaults()
Chris@239 197 {
Chris@239 198 clear();
Chris@239 199 findDefaultLocations();
Chris@239 200 reset();
Chris@239 201 }
Chris@239 202
Chris@239 203 void
Chris@239 204 SettingsDialog::findDefaultLocations(QString installPath)
Chris@239 205 {
Chris@239 206 m_installPath = installPath;
Chris@239 207 findHgBinaryName();
Chris@239 208 findExtension();
Chris@239 209 findDiffBinaryName();
Chris@239 210 findMergeBinaryName();
Chris@239 211 findEditorBinaryName();
Chris@239 212 }
Chris@239 213
Chris@239 214 void
Chris@239 215 SettingsDialog::findHgBinaryName()
Chris@239 216 {
Chris@239 217 QSettings settings;
Chris@239 218 settings.beginGroup("Locations");
Chris@239 219 QString hg = settings.value("hgbinary", "").toString();
Chris@239 220 if (hg == "") {
Chris@239 221 hg = findInPath("hg", m_installPath, true);
Chris@239 222 }
Chris@239 223 if (hg != "") {
Chris@239 224 settings.setValue("hgbinary", hg);
Chris@239 225 }
Chris@239 226 }
Chris@239 227
Chris@240 228 QString
Chris@240 229 SettingsDialog::getUnbundledExtensionFileName()
Chris@240 230 {
Chris@240 231 QString home = QDir::homePath();
Chris@240 232 QString target = QString("%1/.easyhg").arg(home);
Chris@240 233 QString extpath = QString("%1/easyhg.py").arg(target);
Chris@240 234 return extpath;
Chris@240 235 }
Chris@240 236
Chris@239 237 void
Chris@239 238 SettingsDialog::findExtension()
Chris@239 239 {
Chris@239 240 QSettings settings;
Chris@239 241 settings.beginGroup("Locations");
Chris@239 242
Chris@239 243 QString extpath = settings.value("extensionpath", "").toString();
Chris@239 244 if (extpath != "" || !QFile(extpath).exists()) {
Chris@239 245
Chris@240 246 extpath = getUnbundledExtensionFileName();
Chris@239 247
Chris@239 248 if (!QFile(extpath).exists()) {
Chris@239 249 extpath = findInPath("easyhg.py", m_installPath, false);
Chris@239 250 }
Chris@239 251 }
Chris@239 252
Chris@239 253 settings.setValue("extensionpath", extpath);
Chris@239 254 }
Chris@239 255
Chris@239 256 void
Chris@239 257 SettingsDialog::findDiffBinaryName()
Chris@239 258 {
Chris@239 259 QSettings settings;
Chris@239 260 settings.beginGroup("Locations");
Chris@239 261 QString diff = settings.value("extdiffbinary", "").toString();
Chris@239 262 if (diff == "") {
Chris@239 263 QStringList bases;
Chris@239 264 #ifdef Q_OS_MAC
Chris@239 265 bases << "easyhg-extdiff-osx.sh";
Chris@239 266 #endif
Chris@239 267 bases << "kompare" << "kdiff3" << "meld";
Chris@239 268 bool found = false;
Chris@239 269 foreach (QString base, bases) {
Chris@239 270 diff = findInPath(base, m_installPath, true);
Chris@239 271 if (diff != "") {
Chris@239 272 found = true;
Chris@239 273 break;
Chris@239 274 }
Chris@239 275 }
Chris@239 276 if (found) {
Chris@239 277 settings.setValue("extdiffbinary", diff);
Chris@239 278 }
Chris@239 279 }
Chris@239 280 }
Chris@239 281
Chris@239 282 void
Chris@239 283 SettingsDialog::findMergeBinaryName()
Chris@239 284 {
Chris@239 285 QSettings settings;
Chris@239 286 settings.beginGroup("Locations");
Chris@239 287 if (settings.contains("mergebinary")) {
Chris@239 288 return;
Chris@239 289 }
Chris@239 290 QString merge;
Chris@239 291 QStringList bases;
Chris@239 292 #ifdef Q_OS_MAC
Chris@239 293 bases << "easyhg-merge-osx.sh";
Chris@239 294 #endif
Chris@239 295 // I think this is too dangerous, given command line ordering
Chris@239 296 // differences and suchlike. Need to make sure the hg
Chris@239 297 // installation is configured OK instead
Chris@239 298 // bases << "meld" << "diffuse" << "kdiff3";
Chris@239 299 bool found = false;
Chris@239 300 foreach (QString base, bases) {
Chris@239 301 merge = findInPath(base, m_installPath, true);
Chris@239 302 if (merge != "") {
Chris@239 303 found = true;
Chris@239 304 break;
Chris@239 305 }
Chris@239 306 }
Chris@239 307 if (found) {
Chris@239 308 settings.setValue("mergebinary", merge);
Chris@239 309 }
Chris@239 310 }
Chris@239 311
Chris@239 312 void
Chris@239 313 SettingsDialog::findEditorBinaryName()
Chris@239 314 {
Chris@239 315 QSettings settings;
Chris@239 316 settings.beginGroup("Locations");
Chris@239 317 QString editor = settings.value("editorbinary", "").toString();
Chris@239 318 if (editor == "") {
Chris@239 319 QStringList bases;
Chris@239 320 bases
Chris@239 321 #if defined Q_OS_WIN32
Chris@239 322 << "wordpad.exe"
Chris@239 323 << "C:\\Program Files\\Windows NT\\Accessories\\wordpad.exe"
Chris@239 324 << "notepad.exe"
Chris@239 325 #elif defined Q_OS_MAC
Chris@239 326 << "/Applications/TextEdit.app/Contents/MacOS/TextEdit"
Chris@239 327 #else
Chris@239 328 << "gedit" << "kate"
Chris@239 329 #endif
Chris@239 330 ;
Chris@239 331 bool found = false;
Chris@239 332 foreach (QString base, bases) {
Chris@239 333 editor = findInPath(base, m_installPath, true);
Chris@239 334 if (editor != "") {
Chris@239 335 found = true;
Chris@239 336 break;
Chris@239 337 }
Chris@239 338 }
Chris@239 339 if (found) {
Chris@239 340 settings.setValue("editorbinary", editor);
Chris@239 341 }
Chris@239 342 }
Chris@239 343 }
Chris@239 344
Chris@239 345 void
Chris@239 346 SettingsDialog::clear()
Chris@239 347 {
Chris@239 348 // Clear everything that has a default setting
Chris@239 349 DEBUG << "SettingsDialog::clear" << endl;
Chris@239 350 QSettings settings;
Chris@239 351 settings.beginGroup("Presentation");
Chris@239 352 settings.remove("showiconlabels");
Chris@239 353 settings.remove("showhelpfultext");
Chris@239 354 settings.endGroup();
Chris@239 355 settings.beginGroup("Locations");
Chris@239 356 settings.remove("hgbinary");
Chris@239 357 settings.remove("extdiffbinary");
Chris@239 358 settings.remove("mergebinary");
Chris@239 359 settings.remove("editorbinary");
Chris@239 360 settings.remove("extensionpath");
Chris@239 361 settings.endGroup();
Chris@239 362 settings.beginGroup("General");
Chris@239 363 settings.remove("useextension");
Chris@239 364 settings.endGroup();
Chris@239 365 }
Chris@239 366
Chris@239 367 void
Chris@239 368 SettingsDialog::reset()
Chris@239 369 {
Chris@239 370 DEBUG << "SettingsDialog::reset" << endl;
Chris@239 371 QSettings settings;
Chris@239 372 settings.beginGroup("User Information");
Chris@239 373 m_nameEdit->setText(settings.value("name", getUserRealName()).toString());
Chris@239 374 m_emailEdit->setText(settings.value("email").toString());
Chris@239 375 settings.endGroup();
Chris@239 376 settings.beginGroup("Presentation");
Chris@239 377 m_showIconLabels->setChecked(settings.value("showiconlabels", true).toBool());
Chris@239 378 m_showExtraText->setChecked(settings.value("showhelpfultext", true).toBool());
Chris@239 379 settings.endGroup();
Chris@239 380 settings.beginGroup("Locations");
Chris@239 381 m_hgPathLabel->setText(settings.value("hgbinary").toString());
Chris@239 382 m_diffPathLabel->setText(settings.value("extdiffbinary").toString());
Chris@239 383 m_mergePathLabel->setText(settings.value("mergebinary").toString());
Chris@239 384 m_editPathLabel->setText(settings.value("editorbinary").toString());
Chris@239 385 m_extensionPathLabel->setText(settings.value("extensionpath").toString());
Chris@239 386 settings.endGroup();
Chris@239 387 settings.beginGroup("General");
Chris@239 388 m_useExtension->setChecked(settings.value("useextension", true).toBool());
Chris@239 389 settings.endGroup();
Chris@239 390 }
Chris@239 391
Chris@239 392 void
Chris@175 393 SettingsDialog::accept()
Chris@175 394 {
Chris@177 395 DEBUG << "SettingsDialog::accept" << endl;
Chris@175 396 QSettings settings;
Chris@175 397 settings.beginGroup("User Information");
Chris@175 398 settings.setValue("name", m_nameEdit->text());
Chris@175 399 settings.setValue("email", m_emailEdit->text());
Chris@175 400 settings.endGroup();
Chris@230 401 settings.beginGroup("Presentation");
Chris@230 402 bool b;
Chris@230 403 b = m_showIconLabels->isChecked();
Chris@230 404 if (b != settings.value("showiconlabels", true)) {
Chris@230 405 settings.setValue("showiconlabels", b);
Chris@230 406 m_presentationChanged = true;
Chris@230 407 }
Chris@230 408 b = m_showExtraText->isChecked();
Chris@230 409 if (b != settings.value("showhelpfultext", true)) {
Chris@230 410 settings.setValue("showhelpfultext", b);
Chris@230 411 m_presentationChanged = true;
Chris@230 412 }
Chris@230 413 settings.endGroup();
Chris@175 414 settings.beginGroup("Locations");
Chris@181 415 settings.setValue("hgbinary", m_hgPathLabel->text());
Chris@181 416 settings.setValue("extdiffbinary", m_diffPathLabel->text());
Chris@181 417 settings.setValue("mergebinary", m_mergePathLabel->text());
Chris@221 418 settings.setValue("editorbinary", m_editPathLabel->text());
Chris@181 419 settings.setValue("extensionpath", m_extensionPathLabel->text());
Chris@176 420 settings.endGroup();
Chris@176 421 settings.beginGroup("General");
Chris@176 422 settings.setValue("useextension", m_useExtension->isChecked());
Chris@175 423 settings.endGroup();
Chris@175 424 QDialog::accept();
Chris@175 425 }
Chris@175 426
Chris@175 427