diff options
Diffstat (limited to 'cpp/mainwindow.cpp')
| -rw-r--r-- | cpp/mainwindow.cpp | 327 |
1 files changed, 145 insertions, 182 deletions
diff --git a/cpp/mainwindow.cpp b/cpp/mainwindow.cpp index 4ad16f0..ce5b2bc 100644 --- a/cpp/mainwindow.cpp +++ b/cpp/mainwindow.cpp @@ -1,220 +1,183 @@ #include "mainwindow.h" #include "ui_mainwindow.h" -#include <QDir> #include <QDebug> +#include <QDir> #include <QProcess> -#include <QRegExp> #include <QTextBlock> +#include <QtCore/QRegularExpression> -MainWindow::MainWindow(QWidget *parent) : - QMainWindow(parent), - ui(new Ui::MainWindow) -{ - ui->setupUi(this); -} - -MainWindow::~MainWindow() -{ - delete ui; +MainWindow::MainWindow(QWidget *parent) + : QMainWindow(parent), ui(new Ui::MainWindow) { + ui->setupUi(this); } -void MainWindow::changeEvent(QEvent *e) -{ - QMainWindow::changeEvent(e); - switch (e->type()) { - case QEvent::LanguageChange: - ui->retranslateUi(this); - break; - default: - break; - } +MainWindow::~MainWindow() { delete ui; } + +void MainWindow::changeEvent(QEvent *e) { + QMainWindow::changeEvent(e); + switch (e->type()) { + case QEvent::LanguageChange: + ui->retranslateUi(this); + break; + default: + break; + } } -void MainWindow::on_quit_clicked() -{ - qApp->quit(); +void MainWindow::on_quit_clicked() { qApp->quit(); } + +void MainWindow::findFiles(QString path) { + QDir currentDir = QDir(path); + QStringList files = currentDir.entryList(QStringList("*.new"), + QDir::Files | QDir::NoSymLinks); + Q_FOREACH (QString f, files) { + ui->files->addItem(path + "/" + f); + } + + files = + currentDir.entryList(QStringList("*"), QDir::Dirs | QDir::NoSymLinks | + QDir::NoDot | QDir::NoDotDot); + Q_FOREACH (QString dir, files) { + findFiles(path + "/" + dir); + } } -void MainWindow::findFiles(QString path) -{ - QDir currentDir = QDir(path); - QStringList files = - currentDir.entryList(QStringList("*.new"), - QDir::Files | QDir::NoSymLinks); - Q_FOREACH (QString f, files) - { - ui->files->addItem(path + "/" + f); - } - - files = - currentDir.entryList(QStringList("*"), - QDir::Dirs | QDir::NoSymLinks - | QDir::NoDot | QDir::NoDotDot); - Q_FOREACH (QString dir, files) - { - findFiles(path + "/" + dir); - } +void MainWindow::enableButtons() { + bool enable = 0 != ui->files->count(); + ui->accept->setEnabled(enable); + ui->reject->setEnabled(enable); + ui->edit->setEnabled(enable); } -void MainWindow::enableButtons() -{ - bool enable = 0 != ui->files->count(); - ui->accept->setEnabled(enable); - ui->reject->setEnabled(enable); - ui->edit->setEnabled(enable); +void MainWindow::on_files_currentItemChanged(QListWidgetItem *current, + QListWidgetItem * /*previous*/) { + if (!current) { + ui->diff->clear(); + return; + } + update_diff(); } -void MainWindow::on_files_currentItemChanged - (QListWidgetItem* current, QListWidgetItem* /*previous*/) -{ - if (!current) - { - ui->diff->clear(); - return; - } - update_diff(); +void MainWindow::update_diff() { + QString fold, fnew; + fold = fnew = ui->files->currentItem()->text(); + fold.replace(QRegularExpression("\\.new$"), ""); + QStringList difflines = diff(fold, fnew).split("\n"); + difflines.replaceInStrings( + QRegularExpression("(@@[^@]+@@)"), + "<span style=\"color:blue;font-weight:bold;\">\\1</span>"); + difflines.replaceInStrings(QRegularExpression("^((---|\\+\\+\\+)\\s.*)$"), + "<span style=\"font-weight:bold;\">\\1</span>"); + difflines.replaceInStrings( + QRegularExpression("^(-.*)$"), + "<span style=\"color:red;font-weight:bold;\">\\1</span>"); + difflines.replaceInStrings( + QRegularExpression("^(\\+.*)$"), + "<span style=\"color:green;font-weight:bold;\">\\1</span>"); + + if (difflines.count() < 2) + on_reject_clicked(); + else { + ui->diff->setHtml("<pre>" + difflines.join("\n") + "</pre>"); + enableButtons(); + } } -void MainWindow::update_diff() -{ - QString fold, fnew; - fold = fnew = ui->files->currentItem()->text(); - fold.replace(QRegExp("\\.new$"), ""); - QStringList difflines = diff(fold, fnew).split("\n"); - difflines.replaceInStrings(QRegExp("(@@[^@]+@@)"), - "<span style=\"color:blue;font-weight:bold;\">\\1</span>"); - difflines.replaceInStrings(QRegExp("^((---|\\+\\+\\+)\\s.*)$"), - "<span style=\"font-weight:bold;\">\\1</span>"); - difflines.replaceInStrings(QRegExp("^(-.*)$"), - "<span style=\"color:red;font-weight:bold;\">\\1</span>"); - difflines.replaceInStrings(QRegExp("^(\\+.*)$"), - "<span style=\"color:green;font-weight:bold;\">\\1</span>"); - - if (difflines.count() < 2) - on_reject_clicked(); - else - { - ui->diff->setHtml("<pre>" + difflines.join("\n") + "</pre>"); - enableButtons(); - } +QString MainWindow::diff(QString fold, QString fnew) { + QProcess pipe; + QStringList args; + args << "-udb" << fold << fnew; + pipe.start("diff", args, QIODevice::ReadOnly); + pipe.waitForFinished(); + QString res = QString::fromUtf8(pipe.readAllStandardOutput().constData()); + return res; } -QString MainWindow::diff(QString fold, QString fnew) -{ - QProcess pipe; - QStringList args; - args << "-udb" << fold << fnew; - pipe.start("diff", args, QIODevice::ReadOnly); - pipe.waitForFinished(); - QString res = QString::fromUtf8( - pipe.readAllStandardOutput().constData()); - return res; +void MainWindow::on_accept_clicked() { + QString fold, fnew; + fold = fnew = ui->files->currentItem()->text(); + fold.replace(QRegularExpression("\\.new$"), ""); + if (QFile::remove(fold) && QFile::rename(fnew, fold)) { + ui->files->takeItem(ui->files->row(ui->files->currentItem())); + } else { + qWarning() << "Unable to move" << fnew << "to" << fold; + } + enableButtons(); } -void MainWindow::on_accept_clicked() -{ - QString fold, fnew; - fold = fnew = ui->files->currentItem()->text(); - fold.replace(QRegExp("\\.new$"), ""); - if (QFile::remove(fold) && QFile::rename(fnew, fold)) - { - ui->files->takeItem( - ui->files->row( - ui->files->currentItem())); - } - else - { - qWarning() << "Unable to move" - << fnew << "to" << fold; - } - enableButtons(); +void MainWindow::on_reject_clicked() { + QString fnew; + fnew = ui->files->currentItem()->text(); + if (QFile::remove(fnew)) { + ui->files->takeItem(ui->files->row(ui->files->currentItem())); + } else { + qWarning() << "Unable to delete" << fnew; + } + enableButtons(); } -void MainWindow::on_reject_clicked() -{ - QString fnew; - fnew = ui->files->currentItem()->text(); - if (QFile::remove(fnew)) - { - ui->files->takeItem(ui->files->row(ui->files->currentItem())); - } - else - { - qWarning() << "Unable to delete" << fnew; - } - enableButtons(); +void MainWindow::on_diff_selectionChanged() { + ui->apply->setEnabled(ui->diff->textCursor().selectionStart() != + ui->diff->textCursor().selectionEnd()); } -void MainWindow::on_diff_selectionChanged() -{ - ui->apply->setEnabled( - ui->diff->textCursor().selectionStart() != - ui->diff->textCursor().selectionEnd()); +void MainWindow::on_edit_clicked() { + QString fold, fnew; + fold = fnew = ui->files->currentItem()->text(); + fold.replace(QRegularExpression("\\.new$"), ""); + + QProcess editor; + QStringList args; + args << "-f"; + args << fold; + editor.start("gvim", args); + editor.waitForFinished(); + update_diff(); } -void MainWindow::on_edit_clicked() -{ - QString fold, fnew; - fold = fnew = ui->files->currentItem()->text(); - fold.replace(QRegExp("\\.new$"), ""); - - QProcess editor; - QStringList args; - args << "-f"; - args << fold; - editor.start("gvim", args); - editor.waitForFinished(); - update_diff(); -} +void MainWindow::on_apply_clicked() { + int start = ui->diff->textCursor().selectionStart(); + int end = ui->diff->textCursor().selectionEnd(); -void MainWindow::on_apply_clicked() -{ - int start = ui->diff->textCursor().selectionStart(); - int end = ui->diff->textCursor().selectionEnd(); + int first = ui->diff->document()->findBlock(start).firstLineNumber(); + int last = ui->diff->document()->findBlock(end).firstLineNumber(); - int first = ui->diff->document()->findBlock(start).firstLineNumber(); - int last = ui->diff->document()->findBlock(end).firstLineNumber(); + if (last <= first) + last = first + 1; - if (last <= first) - last = first + 1; + QStringList lines = ui->diff->toPlainText().split("\n"); - QStringList lines = ui->diff->toPlainText().split("\n"); + QStringList part_diff; + part_diff << lines[0] << lines[1]; - QStringList part_diff; - part_diff << lines[0] << lines[1]; + // Looking for the beginning + if (first < 2) + first = 2; + else + while (lines[first].left(2) != "@@") + first--; - // Looking for the begining - if (first < 2) first = 2; + // Looking for the end + int nlines = lines.count(); + while (last < nlines) { + if (lines[last].left(2) == "@@") + break; else - while (lines[first].left(2) != "@@") - first--; - - // Looking for the end - int nlines = lines.count(); - while (last < nlines) - { - if (lines[last].left(2) == "@@") - break; - else last++; - } - part_diff << lines.mid(first, last-first) << "\n"; - patch(part_diff.join("\n")); - update_diff(); + last++; + } + part_diff << lines.mid(first, last - first) << "\n"; + patch(part_diff.join("\n")); + update_diff(); } -void MainWindow::patch(QString diff) -{ - QProcess pipe; - QStringList args; - // args << "--dry-run"; // TODO: remove me - args << "-p0" << "-l"; - pipe.start("patch", args); - pipe.waitForStarted(); - pipe.write(diff.toUtf8()); - pipe.closeWriteChannel(); - pipe.waitForFinished(); - qDebug() << pipe.readAllStandardOutput(); +void MainWindow::patch(QString diff) { + QProcess pipe; + QStringList args; + args << "-p0" << "-l"; + pipe.start("patch", args); + pipe.waitForStarted(); + pipe.write(diff.toUtf8()); + pipe.closeWriteChannel(); + pipe.waitForFinished(); + qDebug() << pipe.readAllStandardOutput(); } - - - |
