add preference option to select default and text font

This commit is contained in:
Axel Kohlmeyer
2023-08-08 20:06:05 -04:00
parent 4397e13aaf
commit 7b1c00137a
6 changed files with 76 additions and 8 deletions

View File

@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.16) 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_AUTOUIC ON)
set(CMAKE_AUTOMOC ON) set(CMAKE_AUTOMOC ON)

View File

@ -213,8 +213,8 @@ void Highlighter::highlightBlock(const QString &text)
auto undo = QRegularExpression("^(unfix|uncompute|undump)\\s+(\\w+)").match(text); auto undo = QRegularExpression("^(unfix|uncompute|undump)\\s+(\\w+)").match(text);
bool do_style = true; bool do_style = true;
bool do_force = true; bool do_force = true;
bool do_defs = true; bool do_defs = true;
bool do_undo = true; bool do_undo = true;
for (const HighlightingRule &rule : qAsConst(highlightingRules)) { for (const HighlightingRule &rule : qAsConst(highlightingRules)) {
QRegularExpressionMatchIterator matchIterator = rule.pattern.globalMatch(text); QRegularExpressionMatchIterator matchIterator = rule.pattern.globalMatch(text);
while (matchIterator.hasNext()) { while (matchIterator.hasNext()) {
@ -226,17 +226,17 @@ void Highlighter::highlightBlock(const QString &text)
setFormat(style.capturedStart(3), style.capturedLength(3), stringFormat); setFormat(style.capturedStart(3), style.capturedLength(3), stringFormat);
setFormat(style.capturedStart(4), style.capturedLength(4), runFormat); setFormat(style.capturedStart(4), style.capturedLength(4), runFormat);
do_style = false; do_style = false;
// special treatment for force styles styles // special treatment for force styles styles
} else if (force.hasMatch() && do_force) { } else if (force.hasMatch() && do_force) {
setFormat(force.capturedStart(1), force.capturedLength(1), particleFormat); setFormat(force.capturedStart(1), force.capturedLength(1), particleFormat);
setFormat(force.capturedStart(2), force.capturedLength(2), runFormat); setFormat(force.capturedStart(2), force.capturedLength(2), runFormat);
do_force = false; do_force = false;
// special treatment for undo commands // special treatment for undo commands
} else if (undo.hasMatch() && do_undo) { } else if (undo.hasMatch() && do_undo) {
setFormat(undo.capturedStart(1), undo.capturedLength(1), defineFormat); setFormat(undo.capturedStart(1), undo.capturedLength(1), defineFormat);
setFormat(undo.capturedStart(2), undo.capturedLength(2), stringFormat); setFormat(undo.capturedStart(2), undo.capturedLength(2), stringFormat);
do_undo = false; do_undo = false;
// special treatment for some definitions // special treatment for some definitions
} else if (defs.hasMatch() && do_defs) { } else if (defs.hasMatch() && do_defs) {
setFormat(defs.capturedStart(1), defs.capturedLength(1), particleFormat); setFormat(defs.capturedStart(1), defs.capturedLength(1), particleFormat);
setFormat(defs.capturedStart(2), defs.capturedLength(2), stringFormat); setFormat(defs.capturedStart(2), defs.capturedLength(2), stringFormat);

View File

@ -143,9 +143,14 @@ LammpsGui::LammpsGui(QWidget *parent, const char *filename) :
QFont all_font("Arial", -1); QFont all_font("Arial", -1);
all_font.setStyleHint(QFont::SansSerif, QFont::PreferOutline); 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); QApplication::setFont(all_font);
QFont text_font("Monospace");
QFont text_font("Monospace", -1);
text_font.setStyleHint(QFont::Monospace, QFont::PreferOutline); 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->document()->setDefaultFont(text_font);
ui->textEdit->setMinimumSize(600, 400); ui->textEdit->setMinimumSize(600, 400);

View File

@ -24,6 +24,8 @@
// forward declarations // forward declarations
class GeneralTab;
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
namespace Ui { namespace Ui {
class LammpsGui; class LammpsGui;
@ -45,6 +47,7 @@ class LammpsGui : public QMainWindow {
Q_OBJECT Q_OBJECT
friend class CodeEditor; friend class CodeEditor;
friend class GeneralTab;
public: public:
LammpsGui(QWidget *parent = nullptr, const char *filename = nullptr); LammpsGui(QWidget *parent = nullptr, const char *filename = nullptr);
@ -81,8 +84,10 @@ private slots:
void preferences(); void preferences();
void defaults(); void defaults();
private: protected:
Ui::LammpsGui *ui; Ui::LammpsGui *ui;
private:
Highlighter *highlighter; Highlighter *highlighter;
StdCapture *capturer; StdCapture *capturer;
QLabel *status; QLabel *status;

View File

@ -13,14 +13,18 @@
#include "preferences.h" #include "preferences.h"
#include "lammpsgui.h"
#include "lammpswrapper.h" #include "lammpswrapper.h"
#include "ui_lammpsgui.h"
#include <QApplication>
#include <QCheckBox> #include <QCheckBox>
#include <QCoreApplication> #include <QCoreApplication>
#include <QDialogButtonBox> #include <QDialogButtonBox>
#include <QDir> #include <QDir>
#include <QDoubleValidator> #include <QDoubleValidator>
#include <QFileDialog> #include <QFileDialog>
#include <QFontDialog>
#include <QGroupBox> #include <QGroupBox>
#include <QHBoxLayout> #include <QHBoxLayout>
#include <QIntValidator> #include <QIntValidator>
@ -121,6 +125,7 @@ void Preferences::accept()
if (box) settings->setValue("viewlog", box->isChecked()); if (box) settings->setValue("viewlog", box->isChecked());
box = tabWidget->findChild<QCheckBox *>("viewchart"); box = tabWidget->findChild<QCheckBox *>("viewchart");
if (box) settings->setValue("viewchart", box->isChecked()); if (box) settings->setValue("viewchart", box->isChecked());
QDialog::accept(); QDialog::accept();
} }
@ -158,6 +163,7 @@ GeneralTab::GeneralTab(QSettings *_settings, LammpsWrapper *_lammps, QWidget *pa
tmplayout->addWidget(tmplabel); tmplayout->addWidget(tmplabel);
tmplayout->addWidget(tmpedit); tmplayout->addWidget(tmpedit);
tmplayout->addWidget(tmpbrowse); tmplayout->addWidget(tmpbrowse);
connect(tmpbrowse, &QPushButton::released, this, &GeneralTab::newtmpfolder);
#endif #endif
#if defined(LAMMPS_GUI_USE_PLUGIN) #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); connect(pluginbrowse, &QPushButton::released, this, &GeneralTab::pluginpath);
#endif #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(echo);
layout->addWidget(cite); layout->addWidget(cite);
layout->addWidget(logv); layout->addWidget(logv);
@ -185,10 +200,49 @@ GeneralTab::GeneralTab(QSettings *_settings, LammpsWrapper *_lammps, QWidget *pa
layout->addWidget(pluginlabel); layout->addWidget(pluginlabel);
layout->addLayout(pluginlayout); layout->addLayout(pluginlayout);
#endif #endif
layout->addLayout(fontlayout);
layout->addStretch(1); layout->addStretch(1);
setLayout(layout); 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<LammpsGui *>(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() void GeneralTab::newtmpfolder()
{ {
QLineEdit *field = findChild<QLineEdit *>("tmpedit"); QLineEdit *field = findChild<QLineEdit *>("tmpedit");

View File

@ -17,6 +17,7 @@
#include <QDialog> #include <QDialog>
class QDialogButtonBox; class QDialogButtonBox;
class QFont;
class QSettings; class QSettings;
class QTabWidget; class QTabWidget;
class LammpsWrapper; class LammpsWrapper;
@ -49,8 +50,11 @@ public:
private slots: private slots:
void pluginpath(); void pluginpath();
void newtmpfolder(); void newtmpfolder();
void newallfont();
void newtextfont();
private: private:
void updatefonts(const QFont &all, const QFont &text);
QSettings *settings; QSettings *settings;
LammpsWrapper *lammps; LammpsWrapper *lammps;
}; };