Work on skins.

This commit is contained in:
Martin Rotter 2013-09-30 18:18:23 +02:00
parent 8a5ee09118
commit ecaf277594
7 changed files with 68 additions and 26 deletions

View file

@ -6,5 +6,8 @@
<email>rotter.martinos@gmail.com</email> <email>rotter.martinos@gmail.com</email>
</author> </author>
<style>fusion,plastique</style> <style>fusion,plastique</style>
<markup>
<html> <head> <style>pre{white-space:pre-wrap}.headertext{font-size:19px;margin-bottom:10px}.header{font-size:15px;-webkit-transition:background 1s ease-out;background:-webkit-gradient(linear,left top,left bottom,color-stop(0%,#45484d),color-stop(100%,#000000));background:-webkit-linear-gradient(top,#45484d 0,#000000 100%);-webkit-border-radius:3px;text-shadow:0 0 1px #fff;filter:dropshadow(color=#ffffff,offx=0,offy=0);padding:8px;margin:5px auto 5px auto;color:white}.header a{color:white}.content{font-size:14px;background:-webkit-gradient(linear,left top,left bottom,color-stop(0%,rgba(238,238,238,0.66)),color-stop(100%,rgba(238,238,238,0.66)));background:-webkit-linear-gradient(top,rgba(238,238,238,0.66) 0,rgba(238,238,238,0.66) 100%);-webkit-border-radius:3px;margin:5px auto 5px auto;padding:8px}.footer{font-size:12px;text-align:center;vertical-align:middle;-webkit-transition:background 1s ease-out;background:-webkit-gradient(linear,left top,left bottom,color-stop(0%,#45484d),color-stop(100%,#000000));background:-webkit-linear-gradient(top,#45484d 0,#000000 100%);-webkit-border-radius:3px;text-shadow:0 0 1px #fff;filter:dropshadow(color=#ffffff,offx=0,offy=0);padding:8px;margin:5px auto 5px auto;color:white}</style> <title>%1</title> </head> <body> <div class=\"header\"> <div class=\"headertext\">%1</div> %2<br> <a href=\"%3\">%3</a> </div> <div class=\"content\"> %4 </div> <div class=\"footer\"> %5 </div> </body> </html>
</markup>
<data/> <data/>
</skin> </skin>

View file

@ -37,6 +37,7 @@
#define APP_CFG_CUTS "keyboard" #define APP_CFG_CUTS "keyboard"
#define APP_CFG_BROWSER "browser" #define APP_CFG_BROWSER "browser"
#define APP_HTML_MARKUP "styled_pattern.html"
#define APP_DB_PATH "data/storage/database.db" #define APP_DB_PATH "data/storage/database.db"
#define APP_PREFIX "@CMAKE_INSTALL_PREFIX@" #define APP_PREFIX "@CMAKE_INSTALL_PREFIX@"
#define APP_REVISION "@APP_REVISION@" #define APP_REVISION "@APP_REVISION@"

View file

@ -10,7 +10,6 @@ class TextFactory {
virtual ~TextFactory(); virtual ~TextFactory();
static QString shorten(const QString &input); static QString shorten(const QString &input);
}; };
#endif // TEXTFACTORY_H #endif // TEXTFACTORY_H

View file

@ -1,6 +1,8 @@
#include <QStyleOptionFrameV3> #include <QStyleOptionFrameV3>
#include <QAction> #include <QAction>
#include <QMenu> #include <QMenu>
#include <QDir>
#include <QFile>
#include <QWebFrame> #include <QWebFrame>
#include <QContextMenuEvent> #include <QContextMenuEvent>
@ -8,6 +10,7 @@
#include "core/settings.h" #include "core/settings.h"
#include "core/basewebpage.h" #include "core/basewebpage.h"
#include "gui/basewebview.h" #include "gui/basewebview.h"
#include "gui/skinfactory.h"
#include "gui/iconthemefactory.h" #include "gui/iconthemefactory.h"
@ -96,10 +99,12 @@ void BaseWebView::initializeActions() {
} }
void BaseWebView::displayErrorPage() { void BaseWebView::displayErrorPage() {
// TODO: Add better custom error page. Custom htmls are now copied during // TODO: Add better custom error page.
// "make install" to APP_HTML_PATH. It is needed to setup css absolute setHtml(SkinFactory::getInstance()->getCurrentMarkup().arg(tr("Page not found"),
// path by replacing "##" with APP_HTML_PATH/css in "compact_text.html". "bbb",
setHtml("error", url()); "ccc",
"ddd",
"eee"));
} }
void BaseWebView::popupContextMenu(const QPoint &pos) { void BaseWebView::popupContextMenu(const QPoint &pos) {

View file

@ -42,6 +42,7 @@ class BaseWebView : public QWebView {
void popupContextMenu(const QPoint &pos); void popupContextMenu(const QPoint &pos);
protected: protected:
// Initializes all actions.
void initializeActions(); void initializeActions();
// Creates necessary connections. // Creates necessary connections.

View file

@ -11,8 +11,8 @@
QPointer<SkinFactory> SkinFactory::s_instance; QPointer<SkinFactory> SkinFactory::s_instance;
SkinFactory::SkinFactory(QObject *parent) SkinFactory::SkinFactory(QObject *parent) : QObject(parent) {
: QObject(parent), m_currentSkin(APP_THEME_SYSTEM) { m_currentSkin = generateDefaultSkin();
} }
SkinFactory::~SkinFactory() { SkinFactory::~SkinFactory() {
@ -29,14 +29,18 @@ SkinFactory *SkinFactory::getInstance() {
void SkinFactory::loadCurrentSkin() { void SkinFactory::loadCurrentSkin() {
QString skin_name_from_settings = getSelectedSkinName(); QString skin_name_from_settings = getSelectedSkinName();
bool loaded = false;
Skin skin_data = getSkinInfo(skin_name_from_settings, &loaded);
if (skin_name_from_settings == APP_THEME_SYSTEM) { if (skin_name_from_settings == APP_THEME_SYSTEM) {
// NOTE: No need to call qApp->setStylesheet(QString()) here. // User selected default skin for loading.
// NOTE: No need to do anything here.
qDebug("'Default system skin' loaded."); qDebug("'Default system skin' loaded.");
return;
} }
else if (loaded) {
bool skin_parsed;
Skin skin_data = getSkinInfo(skin_name_from_settings, &skin_parsed);
if (skin_parsed) {
loadSkinFromData(skin_data.m_rawData, skin_name_from_settings); loadSkinFromData(skin_data.m_rawData, skin_name_from_settings);
foreach (QString style, skin_data.m_stylesNames) { foreach (QString style, skin_data.m_stylesNames) {
@ -46,7 +50,8 @@ void SkinFactory::loadCurrentSkin() {
} }
} }
m_currentSkin = skin_name_from_settings; // Set this 'Skin' object as active one.
m_currentSkin = skin_data;
qDebug("Skin '%s' loaded.", qPrintable(skin_name_from_settings)); qDebug("Skin '%s' loaded.", qPrintable(skin_name_from_settings));
} }
@ -56,6 +61,21 @@ void SkinFactory::loadCurrentSkin() {
} }
} }
Skin SkinFactory::generateDefaultSkin() {
Skin default_skin;
default_skin.m_author = "-";
default_skin.m_baseName = APP_THEME_SYSTEM;
default_skin.m_email = "-";
default_skin.m_version = "-";
default_skin.m_visibleName = tr("default system skin");
// NOTE: Used http://www.htmlcompressor.com/compressor/ for compression.
default_skin.m_layoutMarkup = "<html> <head> <style>pre{white-space:pre-wrap}.headertext{font-size:19px;margin-bottom:10px}.header{font-size:15px;-webkit-transition:background 1s ease-out;background:-webkit-gradient(linear,left top,left bottom,color-stop(0%,#45484d),color-stop(100%,#000000));background:-webkit-linear-gradient(top,#45484d 0,#000000 100%);-webkit-border-radius:3px;text-shadow:0 0 1px #fff;filter:dropshadow(color=#ffffff,offx=0,offy=0);padding:8px;margin:5px auto 5px auto;color:white}.header a{color:white}.content{font-size:14px;background:-webkit-gradient(linear,left top,left bottom,color-stop(0%,rgba(238,238,238,0.66)),color-stop(100%,rgba(238,238,238,0.66)));background:-webkit-linear-gradient(top,rgba(238,238,238,0.66) 0,rgba(238,238,238,0.66) 100%);-webkit-border-radius:3px;margin:5px auto 5px auto;padding:8px}.footer{font-size:12px;text-align:center;vertical-align:middle;-webkit-transition:background 1s ease-out;background:-webkit-gradient(linear,left top,left bottom,color-stop(0%,#45484d),color-stop(100%,#000000));background:-webkit-linear-gradient(top,#45484d 0,#000000 100%);-webkit-border-radius:3px;text-shadow:0 0 1px #fff;filter:dropshadow(color=#ffffff,offx=0,offy=0);padding:8px;margin:5px auto 5px auto;color:white}</style> <title>%1</title> </head> <body> <div class=\"header\"> <div class=\"headertext\">%1</div> %2<br> <a href=\"%3\">%3</a> </div> <div class=\"content\"> %4 </div> <div class=\"footer\"> %5 </div> </body> </html>";
return default_skin;
}
bool SkinFactory::loadSkinFromData(QString skin_data, const QString &skin_path) { bool SkinFactory::loadSkinFromData(QString skin_data, const QString &skin_path) {
QStringList skin_parts = skin_path.split('/', QString::SkipEmptyParts); QStringList skin_parts = skin_path.split('/', QString::SkipEmptyParts);
@ -95,27 +115,30 @@ QString SkinFactory::getSelectedSkinName() {
} }
QString SkinFactory::getCurrentSkinName() { QString SkinFactory::getCurrentSkinName() {
return m_currentSkin; return m_currentSkin.m_baseName;
}
QString SkinFactory::getCurrentMarkup() {
return m_currentSkin.m_layoutMarkup;
} }
Skin SkinFactory::getSkinInfo(const QString &skin_name, bool *ok) { Skin SkinFactory::getSkinInfo(const QString &skin_name, bool *ok) {
Skin skin;
if (skin_name == APP_THEME_SYSTEM) { if (skin_name == APP_THEME_SYSTEM) {
skin.m_author = "-";
skin.m_baseName = APP_THEME_SYSTEM;
skin.m_email = "-";
skin.m_version = "-";
skin.m_visibleName = tr("default system skin");
if (ok != NULL) { if (ok != NULL) {
*ok = true; *ok = true;
} }
return skin; if (m_currentSkin.m_baseName == APP_THEME_SYSTEM) {
return m_currentSkin;
}
else {
return generateDefaultSkin();
}
} }
Skin skin;
QXmlQuery query; QXmlQuery query;
QString styles;
QFile skin_file(APP_SKIN_PATH + QDir::separator() + skin_name); QFile skin_file(APP_SKIN_PATH + QDir::separator() + skin_name);
if (!skin_file.open(QIODevice::ReadOnly) || !query.setFocus(&skin_file)) { if (!skin_file.open(QIODevice::ReadOnly) || !query.setFocus(&skin_file)) {
@ -136,7 +159,6 @@ Skin SkinFactory::getSkinInfo(const QString &skin_name, bool *ok) {
// Obtain style name. // Obtain style name.
query.setQuery("string(/skin/style)"); query.setQuery("string(/skin/style)");
QString styles;
query.evaluateTo(&styles); query.evaluateTo(&styles);
skin.m_stylesNames = styles.remove("\n").split(","); skin.m_stylesNames = styles.remove("\n").split(",");
@ -155,6 +177,10 @@ Skin SkinFactory::getSkinInfo(const QString &skin_name, bool *ok) {
query.evaluateTo(&skin.m_version); query.evaluateTo(&skin.m_version);
skin.m_version = skin.m_version.remove("\n"); skin.m_version = skin.m_version.remove("\n");
// Obtain layout markup.
query.setQuery("string(/skin/markup)");
query.evaluateTo(&skin.m_layoutMarkup);
// Obtain other information. // Obtain other information.
skin.m_baseName = skin_name; skin.m_baseName = skin_name;
@ -174,7 +200,7 @@ Skin SkinFactory::getSkinInfo(const QString &skin_name, bool *ok) {
QList<Skin> SkinFactory::getInstalledSkins() { QList<Skin> SkinFactory::getInstalledSkins() {
QList<Skin> skins; QList<Skin> skins;
skins.append(getSkinInfo(APP_THEME_SYSTEM)); skins.append(generateDefaultSkin());
bool skin_load_ok; bool skin_load_ok;
QStringList skin_directories = QDir(APP_SKIN_PATH).entryList(QDir::Dirs | QStringList skin_directories = QDir(APP_SKIN_PATH).entryList(QDir::Dirs |

View file

@ -15,6 +15,7 @@ struct Skin {
QString m_email; QString m_email;
QString m_version; QString m_version;
QString m_rawData; QString m_rawData;
QString m_layoutMarkup;
}; };
Q_DECLARE_METATYPE(Skin) Q_DECLARE_METATYPE(Skin)
@ -30,6 +31,9 @@ class SkinFactory : public QObject {
// external resources. // external resources.
bool loadSkinFromData(QString skin_data, const QString &skin_path); bool loadSkinFromData(QString skin_data, const QString &skin_path);
// Generates "default" skin.
Skin generateDefaultSkin();
public: public:
// Singleton getter. // Singleton getter.
static SkinFactory *getInstance(); static SkinFactory *getInstance();
@ -40,10 +44,13 @@ class SkinFactory : public QObject {
// Loads skin name from settings and sets it as active. // Loads skin name from settings and sets it as active.
void loadCurrentSkin(); void loadCurrentSkin();
// Return the name of the currently activated skin. // Returns the name of the currently activated skin.
// NOTE: Skin name is formatted as "<folder>/<skin>.xml". // NOTE: Skin name is formatted as "<folder>/<skin>.xml".
QString getCurrentSkinName(); QString getCurrentSkinName();
// Returns contents of current layout markup.
QString getCurrentMarkup();
// Returns the name of the skin, that should be activated // Returns the name of the skin, that should be activated
// after application restart. // after application restart.
QString getSelectedSkinName(); QString getSelectedSkinName();
@ -60,7 +67,7 @@ class SkinFactory : public QObject {
private: private:
// Holds name of the current skin. // Holds name of the current skin.
QString m_currentSkin; Skin m_currentSkin;
// Singleton. // Singleton.
static QPointer<SkinFactory> s_instance; static QPointer<SkinFactory> s_instance;