Custom fonts registration and usage (#50)

* Register custom fonts once

The QHelpers functions registered the fonts on every call, which is not
necessary.

- added anonymous namespace for internal linkage
- added helper function for font registration

* Move helper functions from class to namespace

Fixes a possible memleak, because up until now the QHelpers object was
allocated with new without a parent QObject or following delete.

* Removed unused functions
This commit is contained in:
C. Balles 2017-04-05 10:56:59 +02:00 committed by Hugo Teso
parent d7435b1e54
commit 44b72a1826
4 changed files with 35 additions and 58 deletions

View File

@ -1,31 +1,27 @@
#include "helpers.h" #include "helpers.h"
QHelpers::QHelpers(QObject *parent) : #include <QPlainTextEdit>
QObject(parent) #include <QTextEdit>
namespace qhelpers
{ {
// Meow
}
void QHelpers::normalizeFont(QPlainTextEdit *edit) { // TODO: wouldn't it be enough to setFont on the QWidget?
void normalizeFont(QPlainTextEdit *edit) {
#ifdef Q_OS_LINUX #ifdef Q_OS_LINUX
// Add custom monospaced font
QFontDatabase fontDB;
fontDB.addApplicationFont(":/new/prefix1/fonts/Inconsolata-Regular.ttf");
QFont anonFont("Inconsolata", 12); QFont anonFont("Inconsolata", 12);
QTextDocument *out_doc = edit->document(); QTextDocument *out_doc = edit->document();
out_doc->setDefaultFont(anonFont); out_doc->setDefaultFont(anonFont);
#endif #endif
} }
void QHelpers::normalizeEditFont(QTextEdit *edit) { void normalizeEditFont(QTextEdit *edit) {
#ifdef Q_OS_LINUX #ifdef Q_OS_LINUX
// Add custom monospaced font
QFontDatabase fontDB;
fontDB.addApplicationFont(":/new/prefix1/fonts/Inconsolata-Regular.ttf");
QFont anonFont("Inconsolata", 12); QFont anonFont("Inconsolata", 12);
QTextDocument *out_doc = edit->document(); QTextDocument *out_doc = edit->document();
out_doc->setDefaultFont(anonFont); out_doc->setDefaultFont(anonFont);
#endif #endif
} }
} // end namespace

View File

@ -1,24 +1,13 @@
#ifndef QHELPERS_H #ifndef QHELPERS_H
#define QHELPERS_H #define QHELPERS_H
#include <QObject> class QPlainTextEdit;
#include <QTextEdit> class QTextEdit;
#include <QPlainTextEdit>
#include "mainwindow.h" namespace qhelpers
class QHelpers : public QObject
{ {
Q_OBJECT
public:
explicit QHelpers(QObject *parent = 0);
void normalizeFont(QPlainTextEdit *edit); void normalizeFont(QPlainTextEdit *edit);
void normalizeEditFont(QTextEdit *edit); void normalizeEditFont(QTextEdit *edit);
}
signals:
public slots:
};
#endif // HELPERS_H #endif // HELPERS_H

View File

@ -34,29 +34,23 @@
#include <QGraphicsScene> #include <QGraphicsScene>
#include <QGraphicsView> #include <QGraphicsView>
static void adjustColumns(QTreeWidget *tw) { #include <cassert>
int count = tw->columnCount();
for (int i = 0; i != count; ++i) { namespace
tw->resizeColumnToContents(i); {
void registerCustomFonts()
{
int ret = QFontDatabase::addApplicationFont(":/new/prefix1/fonts/Anonymous Pro.ttf");
assert(-1 != ret && "unable to register Anonymous Pro.ttf");
ret = QFontDatabase::addApplicationFont(":/new/prefix1/fonts/Inconsolata-Regular.ttf");
assert(-1 != ret && "unable to register Inconsolata-Regular.ttf");
// do not issue a warning in release
Q_UNUSED(ret)
} }
} }
static void appendRow(QTreeWidget *tw, const QString &str, const QString &str2=NULL,
const QString &str3=NULL, const QString &str4=NULL, const QString &str5=NULL) {
QTreeWidgetItem *tempItem = new QTreeWidgetItem();
// Fill dummy hidden column
tempItem->setText(0,"0");
tempItem->setText(1,str);
if (str2!=NULL)
tempItem->setText(2, str2);
if (str3!=NULL)
tempItem->setText(3, str3);
if (str4!=NULL)
tempItem->setText(4, str4);
if (str5!=NULL)
tempItem->setText(5, str5);
tw->insertTopLevelItem(0, tempItem);
}
MainWindow::MainWindow(QWidget *parent, QRCore *kore) : MainWindow::MainWindow(QWidget *parent, QRCore *kore) :
QMainWindow(parent), QMainWindow(parent),
@ -69,8 +63,8 @@ MainWindow::MainWindow(QWidget *parent, QRCore *kore) :
doLock = false; doLock = false;
// Add custom font registerCustomFonts();
QFontDatabase::addApplicationFont(":/new/prefix1/fonts/Anonymous Pro.ttf");
/* /*
* Toolbar * Toolbar
@ -134,8 +128,7 @@ MainWindow::MainWindow(QWidget *parent, QRCore *kore) :
addToolBar(graphicsBar); addToolBar(graphicsBar);
// Fix output panel font // Fix output panel font
QHelpers *help = new QHelpers(); qhelpers::normalizeFont(ui->consoleOutputTextEdit);
help->normalizeFont(ui->consoleOutputTextEdit);
/* /*
* Dock Widgets * Dock Widgets

View File

@ -74,11 +74,10 @@ MemoryWidget::MemoryWidget(MainWindow *main, QWidget *parent) :
ui->fcnNameEdit->setTextMargins(5, 0, 0, 0); ui->fcnNameEdit->setTextMargins(5, 0, 0, 0);
// Normalize fonts for other OS // Normalize fonts for other OS
QHelpers *help = new QHelpers(); qhelpers::normalizeFont(this->disasTextEdit);
help->normalizeFont(this->disasTextEdit); qhelpers::normalizeEditFont(this->hexOffsetText);
help->normalizeEditFont(this->hexOffsetText); qhelpers::normalizeEditFont(this->hexHexText);
help->normalizeEditFont(this->hexHexText); qhelpers::normalizeEditFont(this->hexASCIIText);
help->normalizeEditFont(this->hexASCIIText);
// Popup menu on Settings toolbutton // Popup menu on Settings toolbutton
QMenu *memMenu = new QMenu(); QMenu *memMenu = new QMenu();