changeset 476:7e8688784980

Use built-in edit window for editing .hgignore file (fixes #215)
author Chris Cannam
date Tue, 26 Jul 2011 15:21:45 +0100
parents dec4695f64e0
children 555496d418f1
files src/mainwindow.cpp src/settingsdialog.cpp src/settingsdialog.h
diffstat 3 files changed, 43 insertions(+), 78 deletions(-) [+]
line wrap: on
line diff
--- a/src/mainwindow.cpp	Tue Jul 26 13:42:09 2011 +0100
+++ b/src/mainwindow.cpp	Tue Jul 26 15:21:45 2011 +0100
@@ -33,6 +33,7 @@
 #include <QRegExp>
 #include <QShortcut>
 #include <QUrl>
+#include <QDialogButtonBox>
 #include <QTimer>
 
 #include "mainwindow.h"
@@ -577,23 +578,50 @@
     initHgIgnore();
 
     QString hgIgnorePath = m_workFolderPath + "/.hgignore";
-    QStringList params;
-
-    params << hgIgnorePath;
+
+    QFile f(hgIgnorePath);
+    if (!f.exists()) return; // shouldn't happen (after initHgIgnore called)
+
+    if (!f.open(QFile::ReadOnly)) return;
+    QTextStream sin(&f);
+    QString all = sin.readAll();
+    f.close();
+
+    QDialog d;
+    QGridLayout layout;
+    d.setLayout(&layout);
+
+    int row = 0;
+    layout.addWidget(new QLabel(tr("<qt><h3>Ignored File Patterns</h3></qt>")), row++, 0);//!!! todo: link to Hg docs?
+
+    QTextEdit ed;
+    ed.setAcceptRichText(false);
+    ed.setLineWrapMode(QTextEdit::NoWrap);
+    layout.setRowStretch(row, 10);
+    layout.addWidget(&ed, row++, 0);
     
-    QString editor = getEditorBinaryName();
-
-    if (editor == "") {
-        QMessageBox::critical
-            (this, tr("Edit .hgignore"),
-             tr("Failed to locate a system text editor program!"));
-        return;
+    QDialogButtonBox bb(QDialogButtonBox::Save | QDialogButtonBox::Cancel);
+    connect(bb.button(QDialogButtonBox::Save), SIGNAL(clicked()),
+            &d, SLOT(accept()));
+    connect(bb.button(QDialogButtonBox::Cancel), SIGNAL(clicked()),
+            &d, SLOT(reject()));
+    layout.addWidget(&bb, row++, 0);
+
+    ed.document()->setPlainText(all);
+
+    d.resize(QSize(300, 400));
+
+    if (d.exec() == QDialog::Accepted) {
+        if (!f.open(QFile::WriteOnly | QFile::Truncate)) {
+            QMessageBox::critical(this, tr("Write failed"),
+                                  tr("Failed to open file %1 for writing")
+                                  .arg(f.fileName()));
+            return;
+        }
+        QTextStream sout(&f);
+        sout << ed.document()->toPlainText();
+        f.close();
     }
-
-    HgAction action(ACT_HG_IGNORE, m_workFolderPath, params);
-    action.executable = editor;
-
-    m_runner->requestAction(action);
 }
 
 static QString regexEscape(QString filename)
@@ -767,13 +795,6 @@
     return settings.value("mergebinary", "").toString();
 }
 
-QString MainWindow::getEditorBinaryName()
-{
-    QSettings settings;
-    settings.beginGroup("Locations");
-    return settings.value("editorbinary", "").toString();
-}
-
 void MainWindow::hgShowSummary()
 {
     QStringList params;
--- a/src/settingsdialog.cpp	Tue Jul 26 13:42:09 2011 +0100
+++ b/src/settingsdialog.cpp	Tue Jul 26 15:21:45 2011 +0100
@@ -151,15 +151,6 @@
     pathsLayout->addWidget(browse, row++, 1);
     connect(browse, SIGNAL(clicked()), this, SLOT(sshPathBrowse()));
 
-    pathsLayout->addWidget(new QLabel(tr("External text editor:")), row, 0);
-
-    m_editPathLabel = new QLineEdit();
-    pathsLayout->addWidget(m_editPathLabel, row, 2);
-
-    browse = new QPushButton(tr("Browse..."));
-    pathsLayout->addWidget(browse, row++, 1);
-    connect(browse, SIGNAL(clicked()), this, SLOT(editPathBrowse()));
-
     pathsLayout->addWidget(new QLabel(tr("EasyHg Mercurial extension:")), row, 0);
 
     m_extensionPathLabel = new QLineEdit();
@@ -222,12 +213,6 @@
 }
 
 void
-SettingsDialog::editPathBrowse()
-{
-    browseFor(tr("External text editor"), m_editPathLabel);
-}
-
-void
 SettingsDialog::extensionPathBrowse()
 {
     browseFor(tr("EasyHg Mercurial extension"), m_extensionPathLabel);
@@ -275,7 +260,6 @@
     findDiffBinaryName();
     findMergeBinaryName();
     findSshBinaryName();
-    findEditorBinaryName();
 }
 
 void
@@ -409,40 +393,6 @@
 }
 
 void
-SettingsDialog::findEditorBinaryName()
-{
-    QSettings settings;
-    settings.beginGroup("Locations");
-    QString editor = settings.value("editorbinary", "").toString();
-    if (editor != "" && QFile(editor).exists()) {
-        return;
-    }
-    QStringList bases;
-    bases
-#if defined Q_OS_WIN32
-        << "wordpad.exe"
-        << "C:\\Program Files\\Windows NT\\Accessories\\wordpad.exe"
-        << "notepad.exe"
-#elif defined Q_OS_MAC
-        << "/Applications/TextEdit.app/Contents/MacOS/TextEdit"
-#else
-        << "gedit" << "kate"
-#endif
-        ;
-    bool found = false;
-    foreach (QString base, bases) {
-        editor = findInPath(base, m_installPath, true);
-        if (editor != "") {
-            found = true;
-            break;
-        }
-    }
-    if (found) {
-        settings.setValue("editorbinary", editor);
-    }
-}
-
-void
 SettingsDialog::clear()
 {
     // Clear everything that has a default setting
@@ -458,7 +408,6 @@
     settings.remove("extdiffbinary");
     settings.remove("mergebinary");
     settings.remove("sshbinary");
-    settings.remove("editorbinary");
     settings.remove("extensionpath");
     settings.endGroup();
     settings.beginGroup("General");
@@ -488,7 +437,6 @@
     m_diffPathLabel->setText(settings.value("extdiffbinary").toString());
     m_mergePathLabel->setText(settings.value("mergebinary").toString());
     m_sshPathLabel->setText(settings.value("sshbinary").toString());
-    m_editPathLabel->setText(settings.value("editorbinary").toString());
     m_extensionPathLabel->setText(settings.value("extensionpath").toString());
     settings.endGroup();
     settings.beginGroup("General");
@@ -536,7 +484,6 @@
     settings.setValue("extdiffbinary", m_diffPathLabel->text());
     settings.setValue("mergebinary", m_mergePathLabel->text());
     settings.setValue("sshbinary", m_sshPathLabel->text());
-    settings.setValue("editorbinary", m_editPathLabel->text());
     settings.setValue("extensionpath", m_extensionPathLabel->text());
     settings.endGroup();
     settings.beginGroup("General");
--- a/src/settingsdialog.h	Tue Jul 26 13:42:09 2011 +0100
+++ b/src/settingsdialog.h	Tue Jul 26 15:21:45 2011 +0100
@@ -53,7 +53,6 @@
     void diffPathBrowse();
     void mergePathBrowse();
     void sshPathBrowse();
-    void editPathBrowse();
     void extensionPathBrowse();
 
     void accept();
@@ -70,7 +69,6 @@
     QLineEdit *m_diffPathLabel;
     QLineEdit *m_mergePathLabel;
     QLineEdit *m_sshPathLabel;
-    QLineEdit *m_editPathLabel;
 
     QCheckBox *m_useExtension;
     QLineEdit *m_extensionPathLabel;
@@ -93,7 +91,6 @@
     static void findDiffBinaryName();
     static void findMergeBinaryName();
     static void findSshBinaryName();
-    static void findEditorBinaryName();
 
     static QString m_installPath;
 };