diff options
| author | Igor <pashev.igor@gmail.com> | 2010-11-18 20:09:43 +0300 |
|---|---|---|
| committer | Igor <pashev.igor@gmail.com> | 2010-11-18 20:09:43 +0300 |
| commit | 157370c4afbeb49836e10bff0a3400e12fd34f94 (patch) | |
| tree | 69cdb0e0926d4efc1b8742ca650056ce84084d98 /cpp/mainwindow.cpp | |
| parent | 0ea94d3ba924eaf5b739ab4cf4d30b39631476a0 (diff) | |
| download | newslack-157370c4afbeb49836e10bff0a3400e12fd34f94.tar.gz | |
C++
Diffstat (limited to 'cpp/mainwindow.cpp')
| -rw-r--r-- | cpp/mainwindow.cpp | 220 |
1 files changed, 220 insertions, 0 deletions
diff --git a/cpp/mainwindow.cpp b/cpp/mainwindow.cpp new file mode 100644 index 0000000..4ad16f0 --- /dev/null +++ b/cpp/mainwindow.cpp @@ -0,0 +1,220 @@ +#include "mainwindow.h" +#include "ui_mainwindow.h" +#include <QDir> +#include <QDebug> +#include <QProcess> +#include <QRegExp> +#include <QTextBlock> + +MainWindow::MainWindow(QWidget *parent) : + QMainWindow(parent), + ui(new Ui::MainWindow) +{ + ui->setupUi(this); +} + +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::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::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(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; +} + +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_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(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(); + + int first = ui->diff->document()->findBlock(start).firstLineNumber(); + int last = ui->diff->document()->findBlock(end).firstLineNumber(); + + if (last <= first) + last = first + 1; + + QStringList lines = ui->diff->toPlainText().split("\n"); + + QStringList part_diff; + part_diff << lines[0] << lines[1]; + + // Looking for the begining + if (first < 2) first = 2; + 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(); +} + +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(); +} + + + |
