Chris@175: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@175: Chris@175: /* Chris@175: EasyMercurial Chris@175: Chris@175: Based on HgExplorer by Jari Korhonen Chris@175: Copyright (c) 2010 Jari Korhonen Chris@244: Copyright (c) 2011 Chris Cannam Chris@244: Copyright (c) 2011 Queen Mary, University of London Chris@175: Chris@175: This program is free software; you can redistribute it and/or Chris@175: modify it under the terms of the GNU General Public License as Chris@175: published by the Free Software Foundation; either version 2 of the Chris@175: License, or (at your option) any later version. See the file Chris@175: COPYING included with this distribution for more information. Chris@175: */ Chris@175: Chris@175: #include "settingsdialog.h" Chris@175: #include "common.h" Chris@177: #include "debug.h" Chris@175: Chris@175: #include Chris@175: #include Chris@175: #include Chris@175: #include Chris@181: #include Chris@181: #include Chris@273: #include Chris@175: Chris@239: QString Chris@239: SettingsDialog::m_installPath; Chris@239: Chris@175: SettingsDialog::SettingsDialog(QWidget *parent) : Chris@230: QDialog(parent), Chris@230: m_presentationChanged(false) Chris@175: { Chris@175: setModal(true); Chris@175: setWindowTitle(tr("Settings")); Chris@175: Chris@175: QGridLayout *mainLayout = new QGridLayout; Chris@175: setLayout(mainLayout); Chris@175: Chris@230: Chris@230: Chris@181: QGroupBox *meBox = new QGroupBox(tr("User details")); Chris@175: mainLayout->addWidget(meBox, 0, 0); Chris@175: QGridLayout *meLayout = new QGridLayout; Chris@175: meBox->setLayout(meLayout); Chris@175: Chris@175: int row = 0; Chris@175: Chris@175: meLayout->addWidget(new QLabel(tr("Name:")), row, 0); Chris@175: Chris@175: m_nameEdit = new QLineEdit(); Chris@175: meLayout->addWidget(m_nameEdit, row++, 1); Chris@175: Chris@175: meLayout->addWidget(new QLabel(tr("Email address:")), row, 0); Chris@175: Chris@175: m_emailEdit = new QLineEdit(); Chris@175: meLayout->addWidget(m_emailEdit, row++, 1); Chris@175: Chris@230: Chris@230: Chris@230: QGroupBox *lookBox = new QGroupBox(tr("Presentation")); Chris@230: mainLayout->addWidget(lookBox, 1, 0); Chris@230: QGridLayout *lookLayout = new QGridLayout; Chris@230: lookBox->setLayout(lookLayout); Chris@230: Chris@230: row = 0; Chris@230: Chris@230: m_showIconLabels = new QCheckBox(tr("Show labels on toolbar icons")); Chris@273: lookLayout->addWidget(m_showIconLabels, row++, 0, 1, 2); Chris@230: Chris@230: m_showExtraText = new QCheckBox(tr("Show long descriptions for file status headings")); Chris@273: lookLayout->addWidget(m_showExtraText, row++, 0, 1, 2); Chris@273: Chris@273: #ifdef NOT_IMPLEMENTED_YET Chris@273: lookLayout->addWidget(new QLabel(tr("Place the work and history views")), row, 0); Chris@273: m_workHistoryArrangement = new QComboBox(); Chris@273: m_workHistoryArrangement->addItem(tr("In separate tabs")); Chris@273: m_workHistoryArrangement->addItem(tr("Side-by-side in a single pane")); Chris@273: lookLayout->addWidget(m_workHistoryArrangement, row++, 1, Qt::AlignLeft); Chris@273: lookLayout->setColumnStretch(1, 20); Chris@273: #endif Chris@230: Chris@273: lookLayout->addWidget(new QLabel(tr("Label the history timeline with")), row, 0); Chris@273: m_dateFormat = new QComboBox(); Chris@273: m_dateFormat->addItem(tr("Ages, for example \"5 weeks ago\"")); Chris@273: m_dateFormat->addItem(tr("Dates, for example \"2010-06-23\"")); Chris@273: lookLayout->addWidget(m_dateFormat, row++, 1, Qt::AlignLeft); Chris@273: lookLayout->setColumnStretch(1, 20); Chris@273: Chris@230: Chris@175: QGroupBox *pathsBox = new QGroupBox(tr("System application locations")); Chris@230: mainLayout->addWidget(pathsBox, 2, 0); Chris@175: QGridLayout *pathsLayout = new QGridLayout; Chris@175: pathsBox->setLayout(pathsLayout); Chris@175: Chris@175: row = 0; Chris@175: Chris@175: pathsLayout->addWidget(new QLabel(tr("Mercurial (hg) program:")), row, 0); Chris@175: Chris@195: m_hgPathLabel = new QLineEdit(); Chris@181: pathsLayout->addWidget(m_hgPathLabel, row, 2); Chris@175: Chris@175: QPushButton *browse = new QPushButton(tr("Browse...")); Chris@181: pathsLayout->addWidget(browse, row++, 1); Chris@175: connect(browse, SIGNAL(clicked()), this, SLOT(hgPathBrowse())); Chris@175: Chris@175: pathsLayout->addWidget(new QLabel(tr("External diff program:")), row, 0); Chris@175: Chris@195: m_diffPathLabel = new QLineEdit(); Chris@181: pathsLayout->addWidget(m_diffPathLabel, row, 2); Chris@175: Chris@175: browse = new QPushButton(tr("Browse...")); Chris@181: pathsLayout->addWidget(browse, row++, 1); Chris@175: connect(browse, SIGNAL(clicked()), this, SLOT(diffPathBrowse())); Chris@178: Chris@175: pathsLayout->addWidget(new QLabel(tr("External file-merge program:")), row, 0); Chris@175: Chris@195: m_mergePathLabel = new QLineEdit(); Chris@181: pathsLayout->addWidget(m_mergePathLabel, row, 2); Chris@175: Chris@175: browse = new QPushButton(tr("Browse...")); Chris@181: pathsLayout->addWidget(browse, row++, 1); Chris@175: connect(browse, SIGNAL(clicked()), this, SLOT(mergePathBrowse())); Chris@175: Chris@178: pathsLayout->addWidget(new QLabel(tr("External text editor:")), row, 0); Chris@178: Chris@195: m_editPathLabel = new QLineEdit(); Chris@181: pathsLayout->addWidget(m_editPathLabel, row, 2); Chris@178: Chris@178: browse = new QPushButton(tr("Browse...")); Chris@181: pathsLayout->addWidget(browse, row++, 1); Chris@178: connect(browse, SIGNAL(clicked()), this, SLOT(editPathBrowse())); Chris@178: Chris@176: pathsLayout->addWidget(new QLabel(tr("EasyHg Mercurial extension:")), row, 0); Chris@176: Chris@195: m_extensionPathLabel = new QLineEdit(); Chris@181: pathsLayout->addWidget(m_extensionPathLabel, row, 2); Chris@176: Chris@176: browse = new QPushButton(tr("Browse...")); Chris@181: pathsLayout->addWidget(browse, row++, 1); Chris@176: connect(browse, SIGNAL(clicked()), this, SLOT(extensionPathBrowse())); Chris@176: Chris@176: //!!! more info plz Chris@176: m_useExtension = new QCheckBox(tr("Use EasyHg Mercurial extension")); Chris@181: pathsLayout->addWidget(m_useExtension, row++, 2); Chris@176: Chris@176: Chris@239: reset(); // loads current defaults from settings Chris@175: Chris@230: Chris@175: QDialogButtonBox *bbox = new QDialogButtonBox(QDialogButtonBox::Ok); Chris@239: connect(bbox->addButton(tr("Restore defaults"), QDialogButtonBox::ResetRole), Chris@239: SIGNAL(clicked()), this, SLOT(restoreDefaults())); Chris@175: connect(bbox, SIGNAL(accepted()), this, SLOT(accept())); Chris@230: mainLayout->addWidget(bbox, 3, 0); Chris@175: m_ok = bbox->button(QDialogButtonBox::Ok); Chris@175: } Chris@175: Chris@175: void Chris@175: SettingsDialog::hgPathBrowse() Chris@175: { Chris@181: browseFor(tr("Mercurial program"), m_hgPathLabel); Chris@175: } Chris@175: Chris@175: void Chris@175: SettingsDialog::diffPathBrowse() Chris@175: { Chris@181: browseFor(tr("External diff program"), m_diffPathLabel); Chris@175: } Chris@175: Chris@175: void Chris@175: SettingsDialog::mergePathBrowse() Chris@175: { Chris@181: browseFor(tr("External file-merge program"), m_mergePathLabel); Chris@175: } Chris@175: Chris@175: void Chris@178: SettingsDialog::editPathBrowse() Chris@175: { Chris@181: browseFor(tr("External text editor"), m_editPathLabel); Chris@176: } Chris@176: Chris@176: void Chris@176: SettingsDialog::extensionPathBrowse() Chris@176: { Chris@181: browseFor(tr("EasyHg Mercurial extension"), m_extensionPathLabel); Chris@181: } Chris@181: Chris@181: void Chris@195: SettingsDialog::browseFor(QString title, QLineEdit *edit) Chris@181: { Chris@181: QString origin = edit->text(); Chris@181: Chris@181: if (origin == "") { Chris@181: #ifdef Q_OS_WIN32 Chris@181: origin = "c:"; Chris@181: #else Chris@181: origin = QDir::homePath(); Chris@181: #endif Chris@181: } Chris@181: Chris@181: QString path = QFileDialog::getOpenFileName(this, title, origin); Chris@181: if (path != QString()) { Chris@181: edit->setText(path); Chris@181: } Chris@176: } Chris@176: Chris@176: void Chris@239: SettingsDialog::restoreDefaults() Chris@239: { Chris@273: if (QMessageBox::question Chris@273: (this, tr("Restore default settings?"), Chris@273: tr("Restore default settings?

Are you sure you want to reset all settings to their default values?"), Chris@273: QMessageBox::Ok | QMessageBox::Cancel, Chris@273: QMessageBox::Cancel) == QMessageBox::Ok) { Chris@273: clear(); Chris@273: findDefaultLocations(); Chris@273: reset(); Chris@273: } Chris@239: } Chris@239: Chris@239: void Chris@239: SettingsDialog::findDefaultLocations(QString installPath) Chris@239: { Chris@239: m_installPath = installPath; Chris@239: findHgBinaryName(); Chris@239: findExtension(); Chris@239: findDiffBinaryName(); Chris@239: findMergeBinaryName(); Chris@239: findEditorBinaryName(); Chris@239: } Chris@239: Chris@239: void Chris@239: SettingsDialog::findHgBinaryName() Chris@239: { Chris@239: QSettings settings; Chris@239: settings.beginGroup("Locations"); Chris@239: QString hg = settings.value("hgbinary", "").toString(); Chris@239: if (hg == "") { Chris@239: hg = findInPath("hg", m_installPath, true); Chris@239: } Chris@239: if (hg != "") { Chris@239: settings.setValue("hgbinary", hg); Chris@239: } Chris@239: } Chris@239: Chris@240: QString Chris@240: SettingsDialog::getUnbundledExtensionFileName() Chris@240: { Chris@240: QString home = QDir::homePath(); Chris@240: QString target = QString("%1/.easyhg").arg(home); Chris@240: QString extpath = QString("%1/easyhg.py").arg(target); Chris@240: return extpath; Chris@240: } Chris@240: Chris@239: void Chris@239: SettingsDialog::findExtension() Chris@239: { Chris@239: QSettings settings; Chris@239: settings.beginGroup("Locations"); Chris@239: Chris@239: QString extpath = settings.value("extensionpath", "").toString(); Chris@239: if (extpath != "" || !QFile(extpath).exists()) { Chris@239: Chris@240: extpath = getUnbundledExtensionFileName(); Chris@239: Chris@239: if (!QFile(extpath).exists()) { Chris@239: extpath = findInPath("easyhg.py", m_installPath, false); Chris@239: } Chris@239: } Chris@239: Chris@239: settings.setValue("extensionpath", extpath); Chris@239: } Chris@239: Chris@239: void Chris@239: SettingsDialog::findDiffBinaryName() Chris@239: { Chris@239: QSettings settings; Chris@239: settings.beginGroup("Locations"); Chris@239: QString diff = settings.value("extdiffbinary", "").toString(); Chris@239: if (diff == "") { Chris@239: QStringList bases; Chris@272: #ifdef Q_OS_WIN32 Chris@272: bases << "easyhg-extdiff.bat"; Chris@272: #else Chris@269: bases << "easyhg-extdiff.sh"; Chris@272: #endif Chris@239: bases << "kompare" << "kdiff3" << "meld"; Chris@239: bool found = false; Chris@239: foreach (QString base, bases) { Chris@239: diff = findInPath(base, m_installPath, true); Chris@239: if (diff != "") { Chris@239: found = true; Chris@239: break; Chris@239: } Chris@239: } Chris@239: if (found) { Chris@239: settings.setValue("extdiffbinary", diff); Chris@239: } Chris@239: } Chris@239: } Chris@239: Chris@239: void Chris@239: SettingsDialog::findMergeBinaryName() Chris@239: { Chris@239: QSettings settings; Chris@239: settings.beginGroup("Locations"); Chris@239: if (settings.contains("mergebinary")) { Chris@239: return; Chris@239: } Chris@239: QString merge; Chris@239: QStringList bases; Chris@272: #ifdef Q_OS_WIN32 Chris@272: bases << "easyhg-merge.bat"; Chris@272: #else Chris@269: bases << "easyhg-merge.sh"; Chris@272: #endif Chris@269: // NB it's not a good idea to add other tools here, as command Chris@269: // line argument ordering varies. Configure them through hgrc Chris@269: // instead Chris@239: bool found = false; Chris@239: foreach (QString base, bases) { Chris@239: merge = findInPath(base, m_installPath, true); Chris@239: if (merge != "") { Chris@239: found = true; Chris@239: break; Chris@239: } Chris@239: } Chris@239: if (found) { Chris@239: settings.setValue("mergebinary", merge); Chris@239: } Chris@239: } Chris@239: Chris@239: void Chris@239: SettingsDialog::findEditorBinaryName() Chris@239: { Chris@239: QSettings settings; Chris@239: settings.beginGroup("Locations"); Chris@239: QString editor = settings.value("editorbinary", "").toString(); Chris@239: if (editor == "") { Chris@239: QStringList bases; Chris@239: bases Chris@239: #if defined Q_OS_WIN32 Chris@239: << "wordpad.exe" Chris@239: << "C:\\Program Files\\Windows NT\\Accessories\\wordpad.exe" Chris@239: << "notepad.exe" Chris@239: #elif defined Q_OS_MAC Chris@239: << "/Applications/TextEdit.app/Contents/MacOS/TextEdit" Chris@239: #else Chris@239: << "gedit" << "kate" Chris@239: #endif Chris@239: ; Chris@239: bool found = false; Chris@239: foreach (QString base, bases) { Chris@239: editor = findInPath(base, m_installPath, true); Chris@239: if (editor != "") { Chris@239: found = true; Chris@239: break; Chris@239: } Chris@239: } Chris@239: if (found) { Chris@239: settings.setValue("editorbinary", editor); Chris@239: } Chris@239: } Chris@239: } Chris@239: Chris@239: void Chris@239: SettingsDialog::clear() Chris@239: { Chris@239: // Clear everything that has a default setting Chris@239: DEBUG << "SettingsDialog::clear" << endl; Chris@239: QSettings settings; Chris@239: settings.beginGroup("Presentation"); Chris@239: settings.remove("showiconlabels"); Chris@239: settings.remove("showhelpfultext"); Chris@239: settings.endGroup(); Chris@239: settings.beginGroup("Locations"); Chris@239: settings.remove("hgbinary"); Chris@239: settings.remove("extdiffbinary"); Chris@239: settings.remove("mergebinary"); Chris@239: settings.remove("editorbinary"); Chris@239: settings.remove("extensionpath"); Chris@239: settings.endGroup(); Chris@239: settings.beginGroup("General"); Chris@239: settings.remove("useextension"); Chris@239: settings.endGroup(); Chris@239: } Chris@239: Chris@239: void Chris@239: SettingsDialog::reset() Chris@239: { Chris@239: DEBUG << "SettingsDialog::reset" << endl; Chris@239: QSettings settings; Chris@239: settings.beginGroup("User Information"); Chris@239: m_nameEdit->setText(settings.value("name", getUserRealName()).toString()); Chris@239: m_emailEdit->setText(settings.value("email").toString()); Chris@239: settings.endGroup(); Chris@239: settings.beginGroup("Presentation"); Chris@239: m_showIconLabels->setChecked(settings.value("showiconlabels", true).toBool()); Chris@239: m_showExtraText->setChecked(settings.value("showhelpfultext", true).toBool()); Chris@273: #ifdef NOT_IMPLEMENTED_YET Chris@273: m_workHistoryArrangement->setCurrentIndex(settings.value("workhistoryarrangement", 0).toInt()); Chris@273: #endif Chris@273: m_dateFormat->setCurrentIndex(settings.value("dateformat", 0).toInt()); Chris@239: settings.endGroup(); Chris@239: settings.beginGroup("Locations"); Chris@239: m_hgPathLabel->setText(settings.value("hgbinary").toString()); Chris@239: m_diffPathLabel->setText(settings.value("extdiffbinary").toString()); Chris@239: m_mergePathLabel->setText(settings.value("mergebinary").toString()); Chris@239: m_editPathLabel->setText(settings.value("editorbinary").toString()); Chris@239: m_extensionPathLabel->setText(settings.value("extensionpath").toString()); Chris@239: settings.endGroup(); Chris@239: settings.beginGroup("General"); Chris@239: m_useExtension->setChecked(settings.value("useextension", true).toBool()); Chris@239: settings.endGroup(); Chris@239: } Chris@239: Chris@239: void Chris@175: SettingsDialog::accept() Chris@175: { Chris@177: DEBUG << "SettingsDialog::accept" << endl; Chris@175: QSettings settings; Chris@175: settings.beginGroup("User Information"); Chris@175: settings.setValue("name", m_nameEdit->text()); Chris@175: settings.setValue("email", m_emailEdit->text()); Chris@175: settings.endGroup(); Chris@230: settings.beginGroup("Presentation"); Chris@230: bool b; Chris@230: b = m_showIconLabels->isChecked(); Chris@230: if (b != settings.value("showiconlabels", true)) { Chris@230: settings.setValue("showiconlabels", b); Chris@230: m_presentationChanged = true; Chris@230: } Chris@230: b = m_showExtraText->isChecked(); Chris@230: if (b != settings.value("showhelpfultext", true)) { Chris@230: settings.setValue("showhelpfultext", b); Chris@230: m_presentationChanged = true; Chris@230: } Chris@273: int i; Chris@273: #ifdef NOT_IMPLEMENTED_YET Chris@273: i = m_workHistoryArrangement->currentIndex(); Chris@273: if (i != settings.value("workhistoryarrangement", 0)) { Chris@273: settings.setValue("workhistoryarrangement", i); Chris@273: m_presentationChanged = true; Chris@273: } Chris@273: #endif Chris@273: i = m_dateFormat->currentIndex(); Chris@273: if (i != settings.value("dateformat", 0)) { Chris@273: settings.setValue("dateformat", i); Chris@273: m_presentationChanged = true; Chris@273: } Chris@230: settings.endGroup(); Chris@175: settings.beginGroup("Locations"); Chris@181: settings.setValue("hgbinary", m_hgPathLabel->text()); Chris@181: settings.setValue("extdiffbinary", m_diffPathLabel->text()); Chris@181: settings.setValue("mergebinary", m_mergePathLabel->text()); Chris@221: settings.setValue("editorbinary", m_editPathLabel->text()); Chris@181: settings.setValue("extensionpath", m_extensionPathLabel->text()); Chris@176: settings.endGroup(); Chris@176: settings.beginGroup("General"); Chris@176: settings.setValue("useextension", m_useExtension->isChecked()); Chris@175: settings.endGroup(); Chris@175: QDialog::accept(); Chris@175: } Chris@175: Chris@175: