aboutsummaryrefslogtreecommitdiff
path: root/cpp
diff options
context:
space:
mode:
Diffstat (limited to 'cpp')
-rw-r--r--cpp/main.cpp23
-rw-r--r--cpp/mainwindow.cpp327
-rw-r--r--cpp/mainwindow.h44
-rw-r--r--cpp/mainwindow.ui3
-rw-r--r--cpp/newslack.pro29
5 files changed, 194 insertions, 232 deletions
diff --git a/cpp/main.cpp b/cpp/main.cpp
index db7ef9d..1ffb56b 100644
--- a/cpp/main.cpp
+++ b/cpp/main.cpp
@@ -1,18 +1,17 @@
-#include <QtGui/QApplication>
#include "mainwindow.h"
+#include <QtWidgets/QApplication>
-int main(int argc, char *argv[])
-{
- QApplication a(argc, argv);
- MainWindow w;
+int main(int argc, char *argv[]) {
+ QApplication a(argc, argv);
+ MainWindow w;
- if (argc > 1)
- for (int i = 1; i < argc; i++)
- w.findFiles(argv[i]);
- else
- w.findFiles("/home/pashev/tmp");
+ if (argc > 1)
+ for (int i = 1; i < argc; i++)
+ w.findFiles(argv[i]);
+ else
+ w.findFiles("/etc");
- w.show();
+ w.show();
- return a.exec();
+ return a.exec();
}
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();
}
-
-
-
diff --git a/cpp/mainwindow.h b/cpp/mainwindow.h
index f18ee95..a8719a4 100644
--- a/cpp/mainwindow.h
+++ b/cpp/mainwindow.h
@@ -1,40 +1,40 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
-#include <QtGui/QMainWindow>
-#include <QListWidgetItem>
+#include <QtWidgets/QListWidgetItem>
+#include <QtWidgets/QMainWindow>
namespace Ui {
- class MainWindow;
+class MainWindow;
}
-class MainWindow : public QMainWindow
-{
- Q_OBJECT
+class MainWindow : public QMainWindow {
+ Q_OBJECT
public:
- explicit MainWindow(QWidget *parent = 0);
- void findFiles(QString);
- ~MainWindow();
+ explicit MainWindow(QWidget *parent = 0);
+ void findFiles(QString);
+ ~MainWindow();
protected:
- void changeEvent(QEvent *e);
- void patch(QString);
- QString diff(QString, QString);
- void enableButtons();
- void update_diff();
+ void changeEvent(QEvent *e);
+ void patch(QString);
+ QString diff(QString, QString);
+ void enableButtons();
+ void update_diff();
private:
- Ui::MainWindow *ui;
+ Ui::MainWindow *ui;
private slots:
- void on_apply_clicked();
- void on_edit_clicked();
- void on_diff_selectionChanged();
- void on_reject_clicked();
- void on_accept_clicked();
- void on_files_currentItemChanged(QListWidgetItem* current, QListWidgetItem* previous);
- void on_quit_clicked();
+ void on_apply_clicked();
+ void on_edit_clicked();
+ void on_diff_selectionChanged();
+ void on_reject_clicked();
+ void on_accept_clicked();
+ void on_files_currentItemChanged(QListWidgetItem *current,
+ QListWidgetItem *previous);
+ void on_quit_clicked();
};
#endif // MAINWINDOW_H
diff --git a/cpp/mainwindow.ui b/cpp/mainwindow.ui
index 93f7ac2..4450fa1 100644
--- a/cpp/mainwindow.ui
+++ b/cpp/mainwindow.ui
@@ -33,9 +33,6 @@
<property name="readOnly">
<bool>true</bool>
</property>
- <property name="tabStopWidth">
- <number>40</number>
- </property>
</widget>
</item>
<item>
diff --git a/cpp/newslack.pro b/cpp/newslack.pro
index df4fb8b..2081eb0 100644
--- a/cpp/newslack.pro
+++ b/cpp/newslack.pro
@@ -1,18 +1,21 @@
-#-------------------------------------------------
-#
-# Project created by QtCreator 2010-11-06T17:12:39
-#
-#-------------------------------------------------
+######################################################################
+# Automatically generated by qmake (3.1) Thu Jul 2 18:24:27 2026
+######################################################################
-QT += core gui
-
-TARGET = newslack
TEMPLATE = app
+TARGET = newslack
+INCLUDEPATH += .
+QT += widgets
-SOURCES += main.cpp\
- mainwindow.cpp
-
-HEADERS += mainwindow.h
+# You can make your code fail to compile if you use deprecated APIs.
+# In order to do so, uncomment the following line.
+# Please consult the documentation of the deprecated API in order to know
+# how to port your code away from it.
+# You can also select to disable deprecated APIs only up to a certain version of Qt.
+#DEFINES += QT_DISABLE_DEPRECATED_UP_TO=0x060000 # disables all APIs deprecated in Qt 6.0.0 and earlier
-FORMS += mainwindow.ui
+# Input
+HEADERS += mainwindow.h
+FORMS += mainwindow.ui
+SOURCES += main.cpp mainwindow.cpp