From 7b1c00137ab479e0dcb4f4cdc68f4802415ab02c Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Tue, 8 Aug 2023 20:06:05 -0400 Subject: [PATCH] add preference option to select default and text font --- tools/lammps-gui/CMakeLists.txt | 2 +- tools/lammps-gui/highlighter.cpp | 10 +++--- tools/lammps-gui/lammpsgui.cpp | 7 ++++- tools/lammps-gui/lammpsgui.h | 7 ++++- tools/lammps-gui/preferences.cpp | 54 ++++++++++++++++++++++++++++++++ tools/lammps-gui/preferences.h | 4 +++ 6 files changed, 76 insertions(+), 8 deletions(-) diff --git a/tools/lammps-gui/CMakeLists.txt b/tools/lammps-gui/CMakeLists.txt index 121e33d765..38cff1df87 100644 --- a/tools/lammps-gui/CMakeLists.txt +++ b/tools/lammps-gui/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.16) -project(lammps-gui VERSION 1.1.2 LANGUAGES CXX) +project(lammps-gui VERSION 1.1.3 LANGUAGES CXX) set(CMAKE_AUTOUIC ON) set(CMAKE_AUTOMOC ON) diff --git a/tools/lammps-gui/highlighter.cpp b/tools/lammps-gui/highlighter.cpp index 84b9b0035f..c2afb28342 100644 --- a/tools/lammps-gui/highlighter.cpp +++ b/tools/lammps-gui/highlighter.cpp @@ -213,8 +213,8 @@ void Highlighter::highlightBlock(const QString &text) auto undo = QRegularExpression("^(unfix|uncompute|undump)\\s+(\\w+)").match(text); bool do_style = true; bool do_force = true; - bool do_defs = true; - bool do_undo = true; + bool do_defs = true; + bool do_undo = true; for (const HighlightingRule &rule : qAsConst(highlightingRules)) { QRegularExpressionMatchIterator matchIterator = rule.pattern.globalMatch(text); while (matchIterator.hasNext()) { @@ -226,17 +226,17 @@ void Highlighter::highlightBlock(const QString &text) setFormat(style.capturedStart(3), style.capturedLength(3), stringFormat); setFormat(style.capturedStart(4), style.capturedLength(4), runFormat); do_style = false; - // special treatment for force styles styles + // special treatment for force styles styles } else if (force.hasMatch() && do_force) { setFormat(force.capturedStart(1), force.capturedLength(1), particleFormat); setFormat(force.capturedStart(2), force.capturedLength(2), runFormat); do_force = false; - // special treatment for undo commands + // special treatment for undo commands } else if (undo.hasMatch() && do_undo) { setFormat(undo.capturedStart(1), undo.capturedLength(1), defineFormat); setFormat(undo.capturedStart(2), undo.capturedLength(2), stringFormat); do_undo = false; - // special treatment for some definitions + // special treatment for some definitions } else if (defs.hasMatch() && do_defs) { setFormat(defs.capturedStart(1), defs.capturedLength(1), particleFormat); setFormat(defs.capturedStart(2), defs.capturedLength(2), stringFormat); diff --git a/tools/lammps-gui/lammpsgui.cpp b/tools/lammps-gui/lammpsgui.cpp index e3ea8056f9..cf67916e0c 100644 --- a/tools/lammps-gui/lammpsgui.cpp +++ b/tools/lammps-gui/lammpsgui.cpp @@ -143,9 +143,14 @@ LammpsGui::LammpsGui(QWidget *parent, const char *filename) : QFont all_font("Arial", -1); all_font.setStyleHint(QFont::SansSerif, QFont::PreferOutline); + all_font.fromString(settings.value("allfont", all_font.toString()).toString()); + settings.setValue("allfont", all_font.toString()); QApplication::setFont(all_font); - QFont text_font("Monospace"); + + QFont text_font("Monospace", -1); text_font.setStyleHint(QFont::Monospace, QFont::PreferOutline); + text_font.fromString(settings.value("textfont", text_font.toString()).toString()); + settings.setValue("textfont", text_font.toString()); ui->textEdit->document()->setDefaultFont(text_font); ui->textEdit->setMinimumSize(600, 400); diff --git a/tools/lammps-gui/lammpsgui.h b/tools/lammps-gui/lammpsgui.h index 8a365adfcb..45f840fab3 100644 --- a/tools/lammps-gui/lammpsgui.h +++ b/tools/lammps-gui/lammpsgui.h @@ -24,6 +24,8 @@ // forward declarations +class GeneralTab; + QT_BEGIN_NAMESPACE namespace Ui { class LammpsGui; @@ -45,6 +47,7 @@ class LammpsGui : public QMainWindow { Q_OBJECT friend class CodeEditor; + friend class GeneralTab; public: LammpsGui(QWidget *parent = nullptr, const char *filename = nullptr); @@ -81,8 +84,10 @@ private slots: void preferences(); void defaults(); -private: +protected: Ui::LammpsGui *ui; + +private: Highlighter *highlighter; StdCapture *capturer; QLabel *status; diff --git a/tools/lammps-gui/preferences.cpp b/tools/lammps-gui/preferences.cpp index eb7745dc45..f50a4be2c0 100644 --- a/tools/lammps-gui/preferences.cpp +++ b/tools/lammps-gui/preferences.cpp @@ -13,14 +13,18 @@ #include "preferences.h" +#include "lammpsgui.h" #include "lammpswrapper.h" +#include "ui_lammpsgui.h" +#include #include #include #include #include #include #include +#include #include #include #include @@ -121,6 +125,7 @@ void Preferences::accept() if (box) settings->setValue("viewlog", box->isChecked()); box = tabWidget->findChild("viewchart"); if (box) settings->setValue("viewchart", box->isChecked()); + QDialog::accept(); } @@ -158,6 +163,7 @@ GeneralTab::GeneralTab(QSettings *_settings, LammpsWrapper *_lammps, QWidget *pa tmplayout->addWidget(tmplabel); tmplayout->addWidget(tmpedit); tmplayout->addWidget(tmpbrowse); + connect(tmpbrowse, &QPushButton::released, this, &GeneralTab::newtmpfolder); #endif #if defined(LAMMPS_GUI_USE_PLUGIN) @@ -172,6 +178,15 @@ GeneralTab::GeneralTab(QSettings *_settings, LammpsWrapper *_lammps, QWidget *pa connect(pluginbrowse, &QPushButton::released, this, &GeneralTab::pluginpath); #endif + + auto *fontlayout = new QHBoxLayout; + auto *getallfont = new QPushButton("Select Default Font..."); + auto *gettextfont = new QPushButton("Select Text Font..."); + fontlayout->addWidget(getallfont); + fontlayout->addWidget(gettextfont); + connect(getallfont, &QPushButton::released, this, &GeneralTab::newallfont); + connect(gettextfont, &QPushButton::released, this, &GeneralTab::newtextfont); + layout->addWidget(echo); layout->addWidget(cite); layout->addWidget(logv); @@ -185,10 +200,49 @@ GeneralTab::GeneralTab(QSettings *_settings, LammpsWrapper *_lammps, QWidget *pa layout->addWidget(pluginlabel); layout->addLayout(pluginlayout); #endif + layout->addLayout(fontlayout); layout->addStretch(1); setLayout(layout); } +void GeneralTab::updatefonts(const QFont &all, const QFont &text) +{ + LammpsGui *main; + for (QWidget *widget : QApplication::topLevelWidgets()) + if (widget->objectName() == "LammpsGui") main = dynamic_cast(widget); + + QApplication::setFont(all); + main->ui->textEdit->document()->setDefaultFont(text); +} + +void GeneralTab::newallfont() +{ + QSettings settings; + QFont all, text; + all.fromString(settings.value("allfont", "").toString()); + text.fromString(settings.value("textfont", "").toString()); + + bool ok = false; + QFont font = QFontDialog::getFont(&ok, all, this, QString("Select Default Font")); + if (ok) updatefonts(font, text); + + settings.setValue("allfont", font.toString()); +} + +void GeneralTab::newtextfont() +{ + QSettings settings; + QFont all, text; + all.fromString(settings.value("allfont", "").toString()); + text.fromString(settings.value("textfont", "").toString()); + + bool ok = false; + QFont font = QFontDialog::getFont(&ok, text, this, QString("Select Text Font")); + if (ok) updatefonts(all, font); + + settings.setValue("textfont", font.toString()); +} + void GeneralTab::newtmpfolder() { QLineEdit *field = findChild("tmpedit"); diff --git a/tools/lammps-gui/preferences.h b/tools/lammps-gui/preferences.h index 46a512772a..fe487c8a7f 100644 --- a/tools/lammps-gui/preferences.h +++ b/tools/lammps-gui/preferences.h @@ -17,6 +17,7 @@ #include class QDialogButtonBox; +class QFont; class QSettings; class QTabWidget; class LammpsWrapper; @@ -49,8 +50,11 @@ public: private slots: void pluginpath(); void newtmpfolder(); + void newallfont(); + void newtextfont(); private: + void updatefonts(const QFont &all, const QFont &text); QSettings *settings; LammpsWrapper *lammps; };