From e54b0ee106542aa42a300c528bb93bdfec10700c Mon Sep 17 00:00:00 2001 From: xarkes Date: Sat, 14 Oct 2017 11:35:49 +0200 Subject: [PATCH] Updated Configuration model + Added Font selection --- src/AnalThread.cpp | 1 - src/MainWindow.cpp | 2 +- src/MainWindow.h | 2 + src/Settings.h | 56 --------------------- src/cutter.cpp | 60 +++++++++++----------- src/cutter.h | 2 + src/cutter.pro | 7 +-- src/dialogs/AsmOptionsDialog.cpp | 18 ++++++- src/dialogs/AsmOptionsDialog.h | 1 + src/dialogs/AsmOptionsDialog.ui | 20 +++++++- src/dialogs/OptionsDialog.cpp | 1 - src/utils/Configuration.cpp | 25 ++++++++++ src/utils/Configuration.h | 72 +++++++++++++++++++++++++++ src/widgets/DisassemblerGraphView.cpp | 13 ++--- src/widgets/DisassemblyWidget.cpp | 11 +++- src/widgets/DisassemblyWidget.h | 1 + 16 files changed, 189 insertions(+), 103 deletions(-) delete mode 100644 src/Settings.h create mode 100644 src/utils/Configuration.cpp create mode 100644 src/utils/Configuration.h diff --git a/src/AnalThread.cpp b/src/AnalThread.cpp index 2addc8be..5ec51b81 100644 --- a/src/AnalThread.cpp +++ b/src/AnalThread.cpp @@ -2,7 +2,6 @@ #include "cutter.h" #include "AnalThread.h" #include "MainWindow.h" -#include "Settings.h" #include "dialogs/OptionsDialog.h" AnalThread::AnalThread(OptionsDialog *parent) : diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index e964b634..5416825a 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -58,7 +58,6 @@ #include "widgets/SdbDock.h" #include "widgets/Omnibar.h" #include "widgets/ConsoleWidget.h" -#include "Settings.h" #include "dialogs/OptionsDialog.h" #include "widgets/EntrypointWidget.h" #include "widgets/DisassemblerGraphView.h" @@ -113,6 +112,7 @@ MainWindow::MainWindow(QWidget *parent) : webserver(core) { doLock = false; + configuration = new Configuration(); } MainWindow::~MainWindow() diff --git a/src/MainWindow.h b/src/MainWindow.h index 6061aaa7..7c8a4ef9 100644 --- a/src/MainWindow.h +++ b/src/MainWindow.h @@ -9,6 +9,7 @@ #include "widgets/SidebarWidget.h" #include "widgets/HexdumpWidget.h" #include "cutter.h" // only needed for ut64 +#include "utils/Configuration.h" class CutterCore; class DockWidget; @@ -180,6 +181,7 @@ private: SideBar *sideBar; PreviewWidget *previewDock; Notepad *notepadDock; + Configuration *configuration; bool doLock; void refreshMem(); diff --git a/src/Settings.h b/src/Settings.h deleted file mode 100644 index 433f1647..00000000 --- a/src/Settings.h +++ /dev/null @@ -1,56 +0,0 @@ - -#ifndef SETTINGS_H -#define SETTINGS_H - -#include - -class Settings -{ -private: - QSettings settings; - -public: - bool getAsmESIL() const { return settings.value("asm.esil", false).toBool(); } - void setAsmESIL(bool v) { settings.setValue("asm.esil", v); } - - bool getAsmPseudo() const { return settings.value("asm.pseudo", false).toBool(); } - void setAsmPseudo(bool v) { settings.setValue("asm.pseudo", v); } - - bool getAsmOffset() const { return settings.value("asm.offset", true).toBool(); } - void setAsmOffset(bool v) { settings.setValue("asm.offset", v); } - - bool getAsmDescribe() const { return settings.value("asm.describe", false).toBool(); } - void setAsmDescribe(bool v) { settings.setValue("asm.describe", v); } - - bool getAsmStackPointer() const { return settings.value("asm.stackptr", false).toBool(); } - void setAsmStackPointer(bool v) { settings.setValue("asm.stackptr", v); } - - bool getAsmBytes() const { return settings.value("asm.bytes", false).toBool(); } - void setAsmBytes(bool v) { settings.setValue("asm.bytes", v); } - - bool getAsmBytespace() const { return settings.value("asm.bytespace", false).toBool(); } - void setAsmBytespace(bool v) { settings.setValue("asm.bytespace", v); } - - bool getAsmLBytes() const { return settings.value("asm.lbytes", true).toBool(); } - void setAsmLBytes(bool v) { settings.setValue("asm.lbytes", v); } - - QString getAsmSyntax() const { return settings.value("asm.syntax", "intel").toString(); } - void setAsmSyntax(const QString &v) { settings.setValue("asm.syntax", v); } - - bool getAsmUppercase() const { return settings.value("asm.ucase", false).toBool(); } - void setAsmUppercase(bool v) { settings.setValue("asm.ucase", v); } - - bool getAsmBBLine() const { return settings.value("asm.bbline", false).toBool(); } - void setAsmBBLine(bool v) { settings.setValue("asm.bbline", v); } - - bool getAsmCapitalize() const { return settings.value("asm.capitalize", false).toBool(); } - void setAsmCapitalize(bool v) { settings.setValue("asm.capitalize", v); } - - bool getAsmVarsub() const { return settings.value("asm.varsub", true).toBool(); } - void setAsmVarsub(bool v) { settings.setValue("asm.varsub", v); } - - bool getAsmVarsubOnly() const { return settings.value("asm.varsub_only", true).toBool(); } - void setAsmVarsubOnly(bool v) { settings.setValue("asm.varsub_only", v); } -}; - -#endif // SETTINGS_H diff --git a/src/cutter.cpp b/src/cutter.cpp index 4fc727b6..e443880e 100644 --- a/src/cutter.cpp +++ b/src/cutter.cpp @@ -1,8 +1,8 @@ #include #include +#include "utils/Configuration.h" #include "cutter.h" #include "sdb.h" -#include "Settings.h" Q_GLOBAL_STATIC(ccClass, uniqueInstance) @@ -513,40 +513,38 @@ void CutterCore::triggerAsmOptionsChanged() void CutterCore::resetDefaultAsmOptions() { - Settings settings; - setConfig("asm.esil", settings.getAsmESIL()); - setConfig("asm.pseudo", settings.getAsmPseudo()); - setConfig("asm.offset", settings.getAsmOffset()); - setConfig("asm.describe", settings.getAsmDescribe()); - setConfig("asm.stackptr", settings.getAsmStackPointer()); - setConfig("asm.bytes", settings.getAsmBytes()); - setConfig("asm.bytespace", settings.getAsmBytespace()); - setConfig("asm.lbytes", settings.getAsmLBytes()); - setConfig("asm.syntax", settings.getAsmSyntax()); - setConfig("asm.ucase", settings.getAsmUppercase()); - setConfig("asm.bbline", settings.getAsmBBLine()); - setConfig("asm.capitalize", settings.getAsmCapitalize()); - setConfig("asm.varsub", settings.getAsmVarsub()); - setConfig("asm.varsub_only", settings.getAsmVarsubOnly()); + setConfig("asm.esil", Config()->getAsmESIL()); + setConfig("asm.pseudo", Config()->getAsmPseudo()); + setConfig("asm.offset", Config()->getAsmOffset()); + setConfig("asm.describe", Config()->getAsmDescribe()); + setConfig("asm.stackptr", Config()->getAsmStackPointer()); + setConfig("asm.bytes", Config()->getAsmBytes()); + setConfig("asm.bytespace", Config()->getAsmBytespace()); + setConfig("asm.lbytes", Config()->getAsmLBytes()); + setConfig("asm.syntax", Config()->getAsmSyntax()); + setConfig("asm.ucase", Config()->getAsmUppercase()); + setConfig("asm.bbline", Config()->getAsmBBLine()); + setConfig("asm.capitalize", Config()->getAsmCapitalize()); + setConfig("asm.varsub", Config()->getAsmVarsub()); + setConfig("asm.varsub_only", Config()->getAsmVarsubOnly()); } void CutterCore::saveDefaultAsmOptions() { - Settings settings; - settings.setAsmESIL(getConfigb("asm.esil")); - settings.setAsmPseudo(getConfigb("asm.pseudo")); - settings.setAsmOffset(getConfigb("asm.offset")); - settings.setAsmDescribe(getConfigb("asm.describe")); - settings.setAsmStackPointer(getConfigb("asm.stackptr")); - settings.setAsmBytes(getConfigb("asm.bytes")); - settings.setAsmBytespace(getConfigb("asm.bytespace")); - settings.setAsmLBytes(getConfigb("asm.lbytes")); - settings.setAsmSyntax(getConfig("asm.syntax")); - settings.setAsmUppercase(getConfigb("asm.ucase")); - settings.setAsmBBLine(getConfigb("asm.bbline")); - settings.setAsmCapitalize(getConfigb("asm.capitalize")); - settings.setAsmVarsub(getConfigb("asm.varsub")); - settings.setAsmVarsubOnly(getConfigb("asm.varsub_only")); + Config()->setAsmESIL(getConfigb("asm.esil")); + Config()->setAsmPseudo(getConfigb("asm.pseudo")); + Config()->setAsmOffset(getConfigb("asm.offset")); + Config()->setAsmDescribe(getConfigb("asm.describe")); + Config()->setAsmStackPointer(getConfigb("asm.stackptr")); + Config()->setAsmBytes(getConfigb("asm.bytes")); + Config()->setAsmBytespace(getConfigb("asm.bytespace")); + Config()->setAsmLBytes(getConfigb("asm.lbytes")); + Config()->setAsmSyntax(getConfig("asm.syntax")); + Config()->setAsmUppercase(getConfigb("asm.ucase")); + Config()->setAsmBBLine(getConfigb("asm.bbline")); + Config()->setAsmCapitalize(getConfigb("asm.capitalize")); + Config()->setAsmVarsub(getConfigb("asm.varsub")); + Config()->setAsmVarsubOnly(getConfigb("asm.varsub_only")); } QString CutterCore::getConfig(const QString &k) diff --git a/src/cutter.h b/src/cutter.h index 6ecb40ba..4e398508 100644 --- a/src/cutter.h +++ b/src/cutter.h @@ -32,6 +32,8 @@ #define APPNAME "Cutter" +#define Core() (CutterCore::getInstance()) + typedef ut64 RVA; #define RVA_INVALID UT64_MAX diff --git a/src/cutter.pro b/src/cutter.pro index f2374e51..4be129fb 100644 --- a/src/cutter.pro +++ b/src/cutter.pro @@ -76,7 +76,8 @@ SOURCES += \ menus/DisassemblyContextMenu.cpp \ widgets/DisassemblyWidget.cpp \ widgets/SidebarWidget.cpp \ - widgets/HexdumpWidget.cpp + widgets/HexdumpWidget.cpp \ + utils/Configuration.cpp HEADERS += \ cutter.h \ @@ -96,7 +97,6 @@ HEADERS += \ MainWindow.h \ utils/Highlighter.h \ utils/MdHighlighter.h \ - Settings.h \ dialogs/OptionsDialog.h \ dialogs/CreateNewDialog.h \ dialogs/NewFileDialog.h \ @@ -125,7 +125,8 @@ HEADERS += \ menus/DisassemblyContextMenu.h \ widgets/DisassemblyWidget.h \ widgets/SidebarWidget.h \ - widgets/HexdumpWidget.h + widgets/HexdumpWidget.h \ + utils/Configuration.h FORMS += \ widgets/PreviewWidget.ui \ diff --git a/src/dialogs/AsmOptionsDialog.cpp b/src/dialogs/AsmOptionsDialog.cpp index 13ac2b74..e64c867a 100644 --- a/src/dialogs/AsmOptionsDialog.cpp +++ b/src/dialogs/AsmOptionsDialog.cpp @@ -1,9 +1,11 @@ +#include +#include -#include "Settings.h" #include "AsmOptionsDialog.h" #include "ui_AsmOptionsDialog.h" #include "utils/Helpers.h" +#include "utils/Configuration.h" AsmOptionsDialog::AsmOptionsDialog(QWidget *parent) : QDialog(parent), @@ -19,6 +21,9 @@ AsmOptionsDialog::AsmOptionsDialog(QWidget *parent) ui->syntaxComboBox->addItem(syntax, syntax); ui->syntaxComboBox->blockSignals(false); + QFont currentFont = Config()->getFont(); + ui->fontSelectionLabel->setText(currentFont.toString()); + updateFromVars(); } @@ -209,3 +214,14 @@ void AsmOptionsDialog::on_buttonBox_clicked(QAbstractButton *button) break; } } + +void AsmOptionsDialog::on_fontSelectionButton_clicked() +{ + QFont currentFont = Config()->getFont(); + bool ok; + QFont newFont = QFontDialog::getFont(&ok, currentFont, this); + if (ok) { + Config()->setFont(newFont); + ui->fontSelectionLabel->setText(newFont.toString()); + } +} diff --git a/src/dialogs/AsmOptionsDialog.h b/src/dialogs/AsmOptionsDialog.h index 7355b8c8..ae8eb1a3 100644 --- a/src/dialogs/AsmOptionsDialog.h +++ b/src/dialogs/AsmOptionsDialog.h @@ -44,6 +44,7 @@ private slots: void on_varsubCheckBox_toggled(bool checked); void on_varsubOnlyCheckBox_toggled(bool checked); void on_buttonBox_clicked(QAbstractButton *button); + void on_fontSelectionButton_clicked(); }; diff --git a/src/dialogs/AsmOptionsDialog.ui b/src/dialogs/AsmOptionsDialog.ui index fd131ddf..a2758bd8 100644 --- a/src/dialogs/AsmOptionsDialog.ui +++ b/src/dialogs/AsmOptionsDialog.ui @@ -6,7 +6,7 @@ 0 0 - 445 + 476 631 @@ -17,6 +17,24 @@ QLayout::SetMinAndMaxSize + + + + + + + + + + + + + Select font + + + + + diff --git a/src/dialogs/OptionsDialog.cpp b/src/dialogs/OptionsDialog.cpp index c2e2d89d..4769d04b 100644 --- a/src/dialogs/OptionsDialog.cpp +++ b/src/dialogs/OptionsDialog.cpp @@ -7,7 +7,6 @@ // TODO: remove us #include "widgets/PreviewWidget.h" #include "widgets/Notepad.h" -#include "Settings.h" #include #include diff --git a/src/utils/Configuration.cpp b/src/utils/Configuration.cpp new file mode 100644 index 00000000..b8083f75 --- /dev/null +++ b/src/utils/Configuration.cpp @@ -0,0 +1,25 @@ +#include "Configuration.h" + +Configuration* Configuration::mPtr = nullptr; + +Configuration::Configuration() : QObject() +{ + mPtr = this; +} + +Configuration* Configuration::instance() +{ + return mPtr; +} + +const QFont Configuration::getFont() const +{ + QFont font = s.value("font", QFont("Monospace", 12)).value(); + return font; +} + +void Configuration::setFont(const QFont &font) +{ + s.setValue("font", font); + emit fontsUpdated(); +} diff --git a/src/utils/Configuration.h b/src/utils/Configuration.h new file mode 100644 index 00000000..d656cf43 --- /dev/null +++ b/src/utils/Configuration.h @@ -0,0 +1,72 @@ +#ifndef CONFIGURATION_H +#define CONFIGURATION_H + +#include +#include + +#define Config() (Configuration::instance()) + +class Configuration : public QObject +{ + Q_OBJECT +private: + QSettings s; + static Configuration* mPtr; + +public: + // Functions + Configuration(); + static Configuration* instance(); + + // Fonts + const QFont getFont() const; + void setFont(const QFont &font); + + // TODO Imho it's wrong doing it this way. Should find something else. + bool getAsmESIL() const { return s.value("asm.esil", false).toBool(); } + void setAsmESIL(bool v) { s.setValue("asm.esil", v); } + + bool getAsmPseudo() const { return s.value("asm.pseudo", false).toBool(); } + void setAsmPseudo(bool v) { s.setValue("asm.pseudo", v); } + + bool getAsmOffset() const { return s.value("asm.offset", true).toBool(); } + void setAsmOffset(bool v) { s.setValue("asm.offset", v); } + + bool getAsmDescribe() const { return s.value("asm.describe", false).toBool(); } + void setAsmDescribe(bool v) { s.setValue("asm.describe", v); } + + bool getAsmStackPointer() const { return s.value("asm.stackptr", false).toBool(); } + void setAsmStackPointer(bool v) { s.setValue("asm.stackptr", v); } + + bool getAsmBytes() const { return s.value("asm.bytes", false).toBool(); } + void setAsmBytes(bool v) { s.setValue("asm.bytes", v); } + + bool getAsmBytespace() const { return s.value("asm.bytespace", false).toBool(); } + void setAsmBytespace(bool v) { s.setValue("asm.bytespace", v); } + + bool getAsmLBytes() const { return s.value("asm.lbytes", true).toBool(); } + void setAsmLBytes(bool v) { s.setValue("asm.lbytes", v); } + + QString getAsmSyntax() const { return s.value("asm.syntax", "intel").toString(); } + void setAsmSyntax(const QString &v) { s.setValue("asm.syntax", v); } + + bool getAsmUppercase() const { return s.value("asm.ucase", false).toBool(); } + void setAsmUppercase(bool v) { s.setValue("asm.ucase", v); } + + bool getAsmBBLine() const { return s.value("asm.bbline", false).toBool(); } + void setAsmBBLine(bool v) { s.setValue("asm.bbline", v); } + + bool getAsmCapitalize() const { return s.value("asm.capitalize", false).toBool(); } + void setAsmCapitalize(bool v) { s.setValue("asm.capitalize", v); } + + bool getAsmVarsub() const { return s.value("asm.varsub", true).toBool(); } + void setAsmVarsub(bool v) { s.setValue("asm.varsub", v); } + + bool getAsmVarsubOnly() const { return s.value("asm.varsub_only", true).toBool(); } + void setAsmVarsubOnly(bool v) { s.setValue("asm.varsub_only", v); } + +signals: + void fontsUpdated(); +}; + +#endif // CONFIGURATION_H diff --git a/src/widgets/DisassemblerGraphView.cpp b/src/widgets/DisassemblerGraphView.cpp index c043c8de..ab51cfd3 100644 --- a/src/widgets/DisassemblerGraphView.cpp +++ b/src/widgets/DisassemblerGraphView.cpp @@ -9,6 +9,7 @@ #include #include #include +#include "utils/Configuration.h" #ifdef _WIN32 #undef min @@ -74,7 +75,7 @@ DisassemblerGraphView::DisassemblerGraphView(QWidget *parent) setupContextMenu(); //Connect to bridge - connect(CutterCore::getInstance(), SIGNAL(seekChanged(RVA)), this, SLOT(on_seekChanged(RVA))); + connect(Core(), SIGNAL(seekChanged(RVA)), this, SLOT(on_seekChanged(RVA))); //connect(Bridge::getBridge(), SIGNAL(loadGraph(BridgeCFGraphList*, duint)), this, SLOT(loadGraphSlot(BridgeCFGraphList*, duint))); //connect(Bridge::getBridge(), SIGNAL(graphAt(duint)), this, SLOT(graphAtSlot(duint))); //connect(Bridge::getBridge(), SIGNAL(updateGraph()), this, SLOT(updateGraphSlot())); @@ -84,7 +85,7 @@ DisassemblerGraphView::DisassemblerGraphView(QWidget *parent) //Connect to config //connect(Config(), SIGNAL(colorsUpdated()), this, SLOT(colorsUpdatedSlot())); - //connect(Config(), SIGNAL(fontsUpdated()), this, SLOT(fontsUpdatedSlot())); + connect(Config(), SIGNAL(fontsUpdated()), this, SLOT(fontsUpdatedSlot())); //connect(Config(), SIGNAL(shortcutsUpdated()), this, SLOT(shortcutsUpdatedSlot())); //connect(Config(), SIGNAL(tokenizerConfigUpdated()), this, SLOT(tokenizerConfigUpdatedSlot())); @@ -98,7 +99,7 @@ DisassemblerGraphView::~DisassemblerGraphView() void DisassemblerGraphView::initFont() { - setFont(QFont("Monospace", 10)); + setFont(Config()->getFont()); QFontMetricsF metrics(this->font()); this->baseline = int(metrics.ascent()); this->charWidth = metrics.width('X'); @@ -734,9 +735,9 @@ void DisassemblerGraphView::mouseDoubleClickEvent(QMouseEvent* event) { duint instr = this->getInstrForMouseEvent(event); //DbgCmdExec(QString("graph dis.branchdest(%1), silent").arg(ToPtrString(instr)).toUtf8().constData()); - QList refs = CutterCore::getInstance()->getXRefs(instr, false, false); + QList refs = Core()->getXRefs(instr, false, false); if (refs.length()) { - CutterCore::getInstance()->seek(refs.at(0).to); + Core()->seek(refs.at(0).to); } if (refs.length() > 1) { qWarning() << "Too many references here. Weird behaviour expected."; @@ -1547,7 +1548,7 @@ void DisassemblerGraphView::tokenizerConfigUpdatedSlot() void DisassemblerGraphView::loadCurrentGraph() { // Read functions - QJsonDocument functionsDoc = CutterCore::getInstance()->cmdj("agj"); + QJsonDocument functionsDoc = Core()->cmdj("agj"); QJsonArray functions = functionsDoc.array(); Analysis anal; diff --git a/src/widgets/DisassemblyWidget.cpp b/src/widgets/DisassemblyWidget.cpp index de3be773..4209021a 100644 --- a/src/widgets/DisassemblyWidget.cpp +++ b/src/widgets/DisassemblyWidget.cpp @@ -5,6 +5,7 @@ #include "utils/HexHighlighter.h" #include #include +#include "utils/Configuration.h" DisassemblyWidget::DisassemblyWidget(QWidget *parent) : QDockWidget(parent), @@ -15,8 +16,7 @@ DisassemblyWidget::DisassemblyWidget(QWidget *parent) : setAllowedAreas(Qt::AllDockWidgetAreas); setObjectName("DisassemblyWidget"); - // TODO Use Settings - mDisasTextEdit->setFont(QFont("Monospace", 10)); + mDisasTextEdit->setFont(Config()->getFont()); // Increase asm text edit margin QTextDocument *asm_docu = mDisasTextEdit->document(); @@ -66,6 +66,7 @@ DisassemblyWidget::DisassemblyWidget(QWidget *parent) : // Seek signal connect(CutterCore::getInstance(), SIGNAL(seekChanged(RVA)), this, SLOT(on_seekChanged(RVA))); + connect(Config(), SIGNAL(fontsUpdated()), this, SLOT(fontsUpdatedSlot())); } DisassemblyWidget::DisassemblyWidget(const QString &title, QWidget *parent) : @@ -407,3 +408,9 @@ void DisassemblyWidget::highlightDisasms() //Highlighter *preview_highlighter = new Highlighter(mDisasTextEdit->document()); //Highlighter *deco_highlighter = new Highlighter(mDisasTextEdit->document()); } + +void DisassemblyWidget::fontsUpdatedSlot() +{ + mDisasTextEdit->setFont(Config()->getFont()); + refreshDisasm(); +} diff --git a/src/widgets/DisassemblyWidget.h b/src/widgets/DisassemblyWidget.h index 7bbc08ed..20325c88 100644 --- a/src/widgets/DisassemblyWidget.h +++ b/src/widgets/DisassemblyWidget.h @@ -22,6 +22,7 @@ public slots: void on_mDisasTextEdit_cursorPositionChanged(); void on_seekChanged(RVA offset); void refreshDisasm(); + void fontsUpdatedSlot(); private: QTextEdit *mDisasTextEdit;