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"
QHelpers::QHelpers(QObject *parent) :
QObject(parent)
#include <QPlainTextEdit>
#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
// Add custom monospaced font
QFontDatabase fontDB;
fontDB.addApplicationFont(":/new/prefix1/fonts/Inconsolata-Regular.ttf");
QFont anonFont("Inconsolata", 12);
QTextDocument *out_doc = edit->document();
out_doc->setDefaultFont(anonFont);
#endif
}
void QHelpers::normalizeEditFont(QTextEdit *edit) {
void normalizeEditFont(QTextEdit *edit) {
#ifdef Q_OS_LINUX
// Add custom monospaced font
QFontDatabase fontDB;
fontDB.addApplicationFont(":/new/prefix1/fonts/Inconsolata-Regular.ttf");
QFont anonFont("Inconsolata", 12);
QTextDocument *out_doc = edit->document();
out_doc->setDefaultFont(anonFont);
#endif
}
} // end namespace

View File

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

View File

@ -34,29 +34,23 @@
#include <QGraphicsScene>
#include <QGraphicsView>
static void adjustColumns(QTreeWidget *tw) {
int count = tw->columnCount();
for (int i = 0; i != count; ++i) {
tw->resizeColumnToContents(i);
#include <cassert>
namespace
{
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) :
QMainWindow(parent),
@ -69,8 +63,8 @@ MainWindow::MainWindow(QWidget *parent, QRCore *kore) :
doLock = false;
// Add custom font
QFontDatabase::addApplicationFont(":/new/prefix1/fonts/Anonymous Pro.ttf");
registerCustomFonts();
/*
* Toolbar
@ -134,8 +128,7 @@ MainWindow::MainWindow(QWidget *parent, QRCore *kore) :
addToolBar(graphicsBar);
// Fix output panel font
QHelpers *help = new QHelpers();
help->normalizeFont(ui->consoleOutputTextEdit);
qhelpers::normalizeFont(ui->consoleOutputTextEdit);
/*
* Dock Widgets

View File

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