r21873 by jghali - avoid expensive QFontDatabase() calls in font combo by using a single QFontDatabase object initialized in ScribusQApp
scribus-commit
scribus-commit at lists.scribus.net
Mon Apr 10 19:04:56 UTC 2017
Author: jghali
Date: Mon Apr 10 19:04:56 2017
New Revision: 21873
URL: http://scribus.net/websvn/listing.php?repname=Scribus&sc=1&rev=21873
Log:
avoid expensive QFontDatabase() calls in font combo by using a single QFontDatabase object initialized in ScribusQApp
Modified:
trunk/Scribus/scribus/scribusapp.h
trunk/Scribus/scribus/ui/fontcombo.cpp
trunk/Scribus/scribus/ui/fontcombo.h
Modified: trunk/Scribus/scribus/scribusapp.h
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=21873&path=/trunk/Scribus/scribus/scribusapp.h
==============================================================================
--- trunk/Scribus/scribus/scribusapp.h (original)
+++ trunk/Scribus/scribus/scribusapp.h Mon Apr 10 19:04:56 2017
@@ -21,7 +21,9 @@
#ifndef SCRIBUSAPP_H
#define SCRIBUSAPP_H
+
#include <QApplication>
+#include <QFontDatabase>
#include <QString>
#include <QStringList>
@@ -38,6 +40,7 @@
public:
ScribusQApp( int & argc, char ** argv );
~ScribusQApp();
+
int init();
void initLang();
void initDLMgr();
@@ -72,6 +75,7 @@
const QString& currGUILanguage() { return m_GUILang; }
const QString& userPrefsDir() { return m_prefsUserDir; }
ScDLManager* dlManager() { return m_scDLMgr; }
+ QFontDatabase& qtFontDatabase() { return m_fontDb; }
QString pythonScript; // script to be run in python from CLI
QStringList pythonScriptArgs; // command line arguments and flags for script from CLI
@@ -102,6 +106,7 @@
QString m_prefsUserDir;
QList<QString> m_filesToLoad;
QString m_fileName;
+ QFontDatabase m_fontDb;
ScDLManager *m_scDLMgr;
protected:
Modified: trunk/Scribus/scribus/ui/fontcombo.cpp
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=21873&path=/trunk/Scribus/scribus/ui/fontcombo.cpp
==============================================================================
--- trunk/Scribus/scribus/ui/fontcombo.cpp (original)
+++ trunk/Scribus/scribus/ui/fontcombo.cpp Mon Apr 10 19:04:56 2017
@@ -35,8 +35,11 @@
#include "prefsmanager.h"
#include "sccombobox.h"
#include "scpage.h"
+#include "scribusapp.h"
#include "scribusdoc.h"
#include "util.h"
+
+extern ScribusQApp* ScQApp;
FontCombo::FontCombo(QWidget* pa) : QComboBox(pa)
{
@@ -404,48 +407,16 @@
return writingSystemFromScript(script);
}
-static QFontDatabase::WritingSystem writingSystemForFont(const QFont &font, bool *hasLatin)
-{
- QList<QFontDatabase::WritingSystem> writingSystems = QFontDatabase().writingSystems(font.family());
-
- // this just confuses the algorithm below. Vietnamese is Latin with lots of special chars
- writingSystems.removeOne(QFontDatabase::Vietnamese);
- *hasLatin = writingSystems.removeOne(QFontDatabase::Latin);
-
- if (writingSystems.isEmpty())
- return QFontDatabase::Any;
-
- QFontDatabase::WritingSystem system = writingSystemFromLocale();
-
- if (writingSystems.contains(system))
- return system;
-
- if (system == QFontDatabase::TraditionalChinese && writingSystems.contains(QFontDatabase::SimplifiedChinese))
- return QFontDatabase::SimplifiedChinese;
-
- if (system == QFontDatabase::SimplifiedChinese && writingSystems.contains(QFontDatabase::TraditionalChinese))
- return QFontDatabase::TraditionalChinese;
-
- system = writingSystems.last();
-
- if (!*hasLatin)
- // we need to show something
- return system;
-
- if (writingSystems.count() == 1 && system > QFontDatabase::Cyrillic)
- return system;
-
- if (writingSystems.count() <= 2 && system > QFontDatabase::Armenian && system < QFontDatabase::Vietnamese)
- return system;
-
- if (writingSystems.count() <= 5 && system >= QFontDatabase::SimplifiedChinese && system <= QFontDatabase::Korean)
- return system;
-
- return QFontDatabase::Any;
-}
-
-static const ScFace& getscFace(QString classname, QString text)
-{
+fontFamilyDelegate::fontFamilyDelegate(QObject *parent)
+ : QAbstractItemDelegate(parent)
+ , writingSystem(QFontDatabase::Any)
+{
+ pixmapCache.setCacheLimit(64*1024);
+}
+
+const ScFace& fontFamilyDelegate::getScFace(QString classname, QString text) const
+{
+ QFontDatabase& fontDb = ScQApp->qtFontDatabase();
PrefsManager* prefsManager = PrefsManager::instance();
// Handle FontComboH class witch has only Family names in the combo class.
@@ -462,25 +433,17 @@
else if (styles.contains("Book"))
style = "Book";
const ScFace& fon = prefsManager->appPrefs.fontPrefs.AvailFonts.findFont(text, style);
- if (!QFontDatabase().families().contains(text))
- QFontDatabase().addApplicationFont(fon.fontFilePath());
+ if (!fontDb.families().contains(text))
+ QFontDatabase::addApplicationFont(fon.fontFilePath());
return fon;
}
else
{
const ScFace& scFace = prefsManager->appPrefs.fontPrefs.AvailFonts.findFont(text);
- if (!QFontDatabase().families().contains(scFace.family()))
- QFontDatabase().addApplicationFont(scFace.fontFilePath());
+ if (!fontDb.families().contains(scFace.family()))
+ QFontDatabase::addApplicationFont(scFace.fontFilePath());
return scFace;
}
-
-}
-
-fontFamilyDelegate::fontFamilyDelegate(QObject *parent)
- : QAbstractItemDelegate(parent)
- , writingSystem(QFontDatabase::Any)
-{
- pixmapCache.setCacheLimit(64*1024);
}
void fontFamilyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
@@ -497,7 +460,8 @@
return;
}
- const ScFace& scFace = getscFace(this->parent()->metaObject()->className(), text);
+ QFontDatabase& fontDb = ScQApp->qtFontDatabase();
+ const ScFace& scFace = getScFace(this->parent()->metaObject()->className(), text);
QPixmap pixmap(option.rect.width(), option.rect.height());
QPixmap invPixmap(option.rect.width(), option.rect.height());
@@ -511,7 +475,7 @@
QFont font = option.font;
font.setPointSize(QFontInfo(font).pointSize() * 3 / 2);
- QFont font2 = QFontDatabase().font(scFace.family(), scFace.style(), QFontInfo(option.font).pointSize());
+ QFont font2 = fontDb.font(scFace.family(), scFace.style(), QFontInfo(option.font).pointSize());
font2.setPointSize(QFontInfo(font2).pointSize() * 3 / 2);
bool hasLatin;
@@ -561,7 +525,7 @@
int w = pixPainter.fontMetrics().width(text + QLatin1String(" "));
pixPainter.setFont(font2);
invpixPainter.setFont(font2);
- QString sample = QFontDatabase().writingSystemSample(system);
+ QString sample = fontDb.writingSystemSample(system);
if (system == QFontDatabase::Arabic)
sample = "أبجدÙØ© عربÙØ©";
@@ -611,3 +575,44 @@
return QSize(fontMetrics.width(text), fontMetrics.height() + 5);
}
+QFontDatabase::WritingSystem fontFamilyDelegate::writingSystemForFont(const QFont &font, bool *hasLatin) const
+{
+ QFontDatabase& fontDb = ScQApp->qtFontDatabase();
+ QList<QFontDatabase::WritingSystem> writingSystems = fontDb.writingSystems(font.family());
+
+ // this just confuses the algorithm below. Vietnamese is Latin with lots of special chars
+ writingSystems.removeOne(QFontDatabase::Vietnamese);
+ *hasLatin = writingSystems.removeOne(QFontDatabase::Latin);
+
+ if (writingSystems.isEmpty())
+ return QFontDatabase::Any;
+
+ QFontDatabase::WritingSystem system = writingSystemFromLocale();
+
+ if (writingSystems.contains(system))
+ return system;
+
+ if (system == QFontDatabase::TraditionalChinese && writingSystems.contains(QFontDatabase::SimplifiedChinese))
+ return QFontDatabase::SimplifiedChinese;
+
+ if (system == QFontDatabase::SimplifiedChinese && writingSystems.contains(QFontDatabase::TraditionalChinese))
+ return QFontDatabase::TraditionalChinese;
+
+ system = writingSystems.last();
+
+ if (!*hasLatin)
+ // we need to show something
+ return system;
+
+ if (writingSystems.count() == 1 && system > QFontDatabase::Cyrillic)
+ return system;
+
+ if (writingSystems.count() <= 2 && system > QFontDatabase::Armenian && system < QFontDatabase::Vietnamese)
+ return system;
+
+ if (writingSystems.count() <= 5 && system >= QFontDatabase::SimplifiedChinese && system <= QFontDatabase::Korean)
+ return system;
+
+ return QFontDatabase::Any;
+}
+
Modified: trunk/Scribus/scribus/ui/fontcombo.h
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=21873&path=/trunk/Scribus/scribus/ui/fontcombo.h
==============================================================================
--- trunk/Scribus/scribus/ui/fontcombo.h (original)
+++ trunk/Scribus/scribus/ui/fontcombo.h Mon Apr 10 19:04:56 2017
@@ -118,6 +118,10 @@
QFontDatabase::WritingSystem writingSystem;
protected:
QFont m_font;
+
static QPixmapCache pixmapCache;
+
+ const ScFace& getScFace(QString classname, QString text) const;
+ QFontDatabase::WritingSystem writingSystemForFont(const QFont &font, bool *hasLatin) const;
};
#endif
More information about the scribus-commit
mailing list