diff --git a/runastyle b/runastyle index 2236617e5..d7374130e 100755 --- a/runastyle +++ b/runastyle @@ -32,7 +32,9 @@ $ASTYLE $style $options test/*.cpp $ASTYLE $style $options test/cfg/*.c* $ASTYLE $style $options test/*.h -$ASTYLE $style $options tools/*.cpp +$ASTYLE $style $options --recursive "tools/*.cpp" +$ASTYLE $style $options --recursive "tools/*.h" + $ASTYLE $style $options --recursive "samples/*.c" $ASTYLE $style $options --recursive "samples/*.cpp" diff --git a/runastyle.bat b/runastyle.bat index 47141671c..c7e198d85 100644 --- a/runastyle.bat +++ b/runastyle.bat @@ -15,6 +15,7 @@ astyle %STYLE% %OPTIONS% lib/*.h astyle %STYLE% %OPTIONS% test/*.cpp astyle %STYLE% %OPTIONS% test/cfg/*.cpp astyle %STYLE% %OPTIONS% test/*.h -astyle %STYLE% %OPTIONS% tools/*.cpp +astyle %STYLE% %OPTIONS% -r tools/*.cpp +astyle %STYLE% %OPTIONS% -r tools/*.h astyle %STYLE% %OPTIONS% -r samples/*.c astyle %STYLE% %OPTIONS% -r samples/*.cpp diff --git a/tools/triage/triage/codeeditor.cpp b/tools/triage/triage/codeeditor.cpp new file mode 100644 index 000000000..4d5461e60 --- /dev/null +++ b/tools/triage/triage/codeeditor.cpp @@ -0,0 +1,111 @@ +#include + +#include "codeeditor.h" + + +CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit(parent) +{ + lineNumberArea = new LineNumberArea(this); + + connect(this, SIGNAL(blockCountChanged(int)), this, SLOT(updateLineNumberAreaWidth(int))); + connect(this, SIGNAL(updateRequest(QRect,int)), this, SLOT(updateLineNumberArea(QRect,int))); + connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(highlightCurrentLine())); + + updateLineNumberAreaWidth(0); + highlightCurrentLine(); +} + + + +int CodeEditor::lineNumberAreaWidth() +{ + int digits = 1; + int max = qMax(1, blockCount()); + while (max >= 10) { + max /= 10; + ++digits; + } + + int space = 3 + fontMetrics().width(QLatin1Char('9')) * digits; + + return space; +} + + + +void CodeEditor::updateLineNumberAreaWidth(int /* newBlockCount */) +{ + setViewportMargins(lineNumberAreaWidth(), 0, 0, 0); +} + + + +void CodeEditor::updateLineNumberArea(const QRect &rect, int dy) +{ + if (dy) + lineNumberArea->scroll(0, dy); + else + lineNumberArea->update(0, rect.y(), lineNumberArea->width(), rect.height()); + + if (rect.contains(viewport()->rect())) + updateLineNumberAreaWidth(0); +} + + + +void CodeEditor::resizeEvent(QResizeEvent *e) +{ + QPlainTextEdit::resizeEvent(e); + + QRect cr = contentsRect(); + lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height())); +} + + + +void CodeEditor::highlightCurrentLine() +{ + QList extraSelections; + + if (!isReadOnly()) { + QTextEdit::ExtraSelection selection; + + QColor lineColor = QColor(Qt::yellow).lighter(160); + + selection.format.setBackground(lineColor); + selection.format.setProperty(QTextFormat::FullWidthSelection, true); + selection.cursor = textCursor(); + selection.cursor.clearSelection(); + extraSelections.append(selection); + } + + setExtraSelections(extraSelections); +} + + + +void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event) +{ + QPainter painter(lineNumberArea); + painter.fillRect(event->rect(), Qt::lightGray); + + + QTextBlock block = firstVisibleBlock(); + int blockNumber = block.blockNumber(); + int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top(); + int bottom = top + (int) blockBoundingRect(block).height(); + + while (block.isValid() && top <= event->rect().bottom()) { + if (block.isVisible() && bottom >= event->rect().top()) { + QString number = QString::number(blockNumber + 1); + painter.setPen(Qt::black); + painter.drawText(0, top, lineNumberArea->width(), fontMetrics().height(), + Qt::AlignRight, number); + } + + block = block.next(); + top = bottom; + bottom = top + (int) blockBoundingRect(block).height(); + ++blockNumber; + } +} diff --git a/tools/triage/triage/codeeditor.h b/tools/triage/triage/codeeditor.h new file mode 100644 index 000000000..0165f035f --- /dev/null +++ b/tools/triage/triage/codeeditor.h @@ -0,0 +1,58 @@ +#ifndef CODEEDITOR_H +#define CODEEDITOR_H + +#include +#include + +class QPaintEvent; +class QResizeEvent; +class QSize; +class QWidget; + +class LineNumberArea; + + +class CodeEditor : public QPlainTextEdit +{ + Q_OBJECT + +public: + CodeEditor(QWidget *parent = 0); + + void lineNumberAreaPaintEvent(QPaintEvent *event); + int lineNumberAreaWidth(); + +protected: + void resizeEvent(QResizeEvent *event) override; + +private slots: + void updateLineNumberAreaWidth(int newBlockCount); + void highlightCurrentLine(); + void updateLineNumberArea(const QRect &, int); + +private: + QWidget *lineNumberArea; +}; + + +class LineNumberArea : public QWidget +{ +public: + LineNumberArea(CodeEditor *editor) : QWidget(editor) { + codeEditor = editor; + } + + QSize sizeHint() const override { + return QSize(codeEditor->lineNumberAreaWidth(), 0); + } + +protected: + void paintEvent(QPaintEvent *event) override { + codeEditor->lineNumberAreaPaintEvent(event); + } + +private: + CodeEditor *codeEditor; +}; + +#endif // CODEEDITOR_H diff --git a/tools/triage/triage/main.cpp b/tools/triage/triage/main.cpp new file mode 100644 index 000000000..b48f94ec8 --- /dev/null +++ b/tools/triage/triage/main.cpp @@ -0,0 +1,11 @@ +#include "mainwindow.h" +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + MainWindow w; + w.show(); + + return a.exec(); +} diff --git a/tools/triage/triage/mainwindow.cpp b/tools/triage/triage/mainwindow.cpp new file mode 100644 index 000000000..e974887e0 --- /dev/null +++ b/tools/triage/triage/mainwindow.cpp @@ -0,0 +1,97 @@ +#include "mainwindow.h" +#include "ui_mainwindow.h" +#include +#include +#include +#include +#include +#include + +const QString WORK_FOLDER(QDir::homePath() + "/triage"); + +MainWindow::MainWindow(QWidget *parent) : + QMainWindow(parent), + ui(new Ui::MainWindow) +{ + ui->setupUi(this); +} + +MainWindow::~MainWindow() +{ + delete ui; +} + +void MainWindow::pasteResults() +{ + ui->results->clear(); + const QStringList lines = QApplication::clipboard()->text().split("\n"); + QString url; + foreach (const QString line, lines) { + if (line.startsWith("ftp://")) + url = line; + else if (!url.isEmpty()) { + ui->results->addItem(url + "\n" + line); + url.clear(); + } + } +} + +void MainWindow::showResult(QListWidgetItem *item) +{ + if (!item->text().startsWith("ftp://")) + return; + const QStringList lines = item->text().split("\n"); + if (lines.size() != 2) + return; + const QString url = lines[0]; + const QString msg = lines[1]; + + const QString archiveName = url.mid(url.lastIndexOf("/") + 1); + const int pos1 = msg.indexOf(":"); + const int pos2 = msg.indexOf(":", pos1+1); + const QString fileName = msg.left(msg.indexOf(":")); + const int lineNumber = msg.mid(pos1+1,pos2-pos1-1).toInt(); + + QProcess process; + process.setWorkingDirectory(WORK_FOLDER); + + // Download archive + if (!QFileInfo(WORK_FOLDER + '/' + archiveName).exists()) { + process.start("wget", QStringList()<code->setFocus(); + QFile f(WORK_FOLDER + '/' + fileName); + f.open(QIODevice::ReadOnly | QIODevice::Text); + QTextStream textStream(&f); + const QString fileData = textStream.readAll(); + ui->code->setPlainText(fileData); + for (int pos = 0, line = 1; pos < fileData.size(); ++pos) { + if (fileData[pos] == '\n') { + ++line; + if (line == lineNumber) { + QTextCursor textCursor = ui->code->textCursor(); + textCursor.setPosition(pos+1); + ui->code->setTextCursor(textCursor); + ui->code->centerCursor(); + break; + } + } + } +} diff --git a/tools/triage/triage/mainwindow.h b/tools/triage/triage/mainwindow.h new file mode 100644 index 000000000..69a18544f --- /dev/null +++ b/tools/triage/triage/mainwindow.h @@ -0,0 +1,27 @@ +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include +#include + +namespace Ui { +class MainWindow; +} + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + explicit MainWindow(QWidget *parent = 0); + ~MainWindow(); + +public slots: + void pasteResults(); + void showResult(QListWidgetItem *item); + +private: + Ui::MainWindow *ui; +}; + +#endif // MAINWINDOW_H diff --git a/tools/triage/triage/mainwindow.ui b/tools/triage/triage/mainwindow.ui new file mode 100644 index 000000000..cb03eb677 --- /dev/null +++ b/tools/triage/triage/mainwindow.ui @@ -0,0 +1,125 @@ + + + MainWindow + + + + 0 + 0 + 683 + 465 + + + + MainWindow + + + + + + + + + + + Paste results + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + + + + + + false + + + + + + + + + 0 + 0 + 683 + 25 + + + + + + TopToolBarArea + + + false + + + + + + + + CodeEditor + QPlainTextEdit +
codeeditor.h
+
+
+ + + + pasteResults + clicked() + MainWindow + pasteResults() + + + 86 + 68 + + + 135 + 68 + + + + + results + itemDoubleClicked(QListWidgetItem*) + MainWindow + showResult(QListWidgetItem*) + + + 28 + 104 + + + 5 + 104 + + + + + + pasteResults() + showResult(QListWidgetItem*) + +
diff --git a/tools/triage/triage/triage.pro b/tools/triage/triage/triage.pro new file mode 100644 index 000000000..4f25c939f --- /dev/null +++ b/tools/triage/triage/triage.pro @@ -0,0 +1,33 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2017-12-31T22:22:56 +# +#------------------------------------------------- + +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = triage +TEMPLATE = app + +# The following define makes your compiler emit warnings if you use +# any feature of Qt which as been marked as deprecated (the exact warnings +# depend on your compiler). Please consult the documentation of the +# deprecated API in order to know how to port your code away from it. +DEFINES += QT_DEPRECATED_WARNINGS + +# You can also make your code fail to compile if you use deprecated APIs. +# In order to do so, uncomment the following line. +# You can also select to disable deprecated APIs only up to a certain version of Qt. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + + +SOURCES += main.cpp\ + mainwindow.cpp \ + codeeditor.cpp + +HEADERS += mainwindow.h \ + codeeditor.h + +FORMS += mainwindow.ui