From caaeb31286683911311553d069f398315f32e47f Mon Sep 17 00:00:00 2001 From: James Hogan Date: Sat, 21 Feb 2009 01:58:17 +0000 Subject: [PATCH] CSS text properties implemented, margins not accurate, no gui to set --- kworship/CMakeLists.txt | 1 + kworship/css/KwCssStyle.cpp | 85 ++++++++++++++++++++++ kworship/display/KwDisplayStyles.h | 23 ++++-- kworship/display/KwTextLayer.cpp | 38 ++++++++-- kworship/display/KwTextLayer.h | 7 ++ kworship/display/KwTextManager.cpp | 7 +- kworship/display/KwTextManager.h | 4 + .../display/{KwTextManager.cpp => KwTextStyle.cpp} | 75 +++++++++---------- kworship/display/KwTextStyle.h | 63 ++++++++++++++-- 9 files changed, 237 insertions(+), 66 deletions(-) copy kworship/display/{KwTextManager.cpp => KwTextStyle.cpp} (56%) diff --git a/kworship/CMakeLists.txt b/kworship/CMakeLists.txt index 59448de..39749d2 100644 --- a/kworship/CMakeLists.txt +++ b/kworship/CMakeLists.txt @@ -57,6 +57,7 @@ set(kworshipdisplay_SRCS display/KwAbstractDisplayManager.cpp display/KwBackgroundManager.cpp display/KwTextManager.cpp + display/KwTextStyle.cpp display/KwOverlayManager.cpp display/KwDisplayStyles.cpp ) diff --git a/kworship/css/KwCssStyle.cpp b/kworship/css/KwCssStyle.cpp index 6e00153..16a8f46 100644 --- a/kworship/css/KwCssStyle.cpp +++ b/kworship/css/KwCssStyle.cpp @@ -40,6 +40,65 @@ KwCssUnprocessed KwCssUnstringify(const KwCssUnprocessed& valu return value; } +// Primitives +template <> +KwCssUnprocessed KwCssStringify(const bool& value) +{ + return KwCssUnprocessed(value ? "true" : "false"); +} +template <> +bool KwCssUnstringify(const KwCssUnprocessed& value, bool* success) +{ + if (value == "true") + { + *success = true; + return true; + } + else if (value == "false") + { + *success = true; + return false; + } + else + { + *success = false; + return false; + } +} + +template <> +KwCssUnprocessed KwCssStringify(const int& value) +{ + return KwCssUnprocessed::number(value); +} +template <> +int KwCssUnstringify(const KwCssUnprocessed& value, bool* success) +{ + return value.toInt(success); +} + +template <> +KwCssUnprocessed KwCssStringify(const float& value) +{ + return KwCssUnprocessed::number(value); +} +template <> +float KwCssUnstringify(const KwCssUnprocessed& value, bool* success) +{ + return value.toFloat(success); +} + +template <> +KwCssUnprocessed KwCssStringify(const double& value) +{ + return KwCssUnprocessed::number(value); +} +template <> +double KwCssUnstringify(const KwCssUnprocessed& value, bool* success) +{ + return value.toDouble(success); +} + #include template <> KwCssUnprocessed KwCssStringify(const KwResourceLink& value) @@ -134,3 +193,29 @@ QBrush KwCssUnstringify(const KwCssUnprocessed& value, bool* success) *success = true; return QBrush(Qt::black); } + +#include +template <> +KwCssUnprocessed KwCssStringify(const QPen& value) +{ + return "Pen()"; +} +template <> +QPen KwCssUnstringify(const KwCssUnprocessed& value, bool* success) +{ + *success = true; + return QPen(); +} + +#include +template <> +KwCssUnprocessed KwCssStringify(const QFont& value) +{ + return "Font()"; +} +template <> +QFont KwCssUnstringify(const KwCssUnprocessed& value, bool* success) +{ + *success = true; + return QFont(); +} diff --git a/kworship/display/KwDisplayStyles.h b/kworship/display/KwDisplayStyles.h index ad93c45..830fad5 100644 --- a/kworship/display/KwDisplayStyles.h +++ b/kworship/display/KwDisplayStyles.h @@ -29,7 +29,9 @@ #include #include +#include #include +#include // The cpp file defines this to declare static variables #ifdef KW_DISPLAY_STYLES_DECLARE @@ -62,24 +64,33 @@ namespace KwDisplayStyles */ KWCSS_END_NAMESPACE() - /* // Text KWCSS_ROOT_NAMESPACE(text) - // Font display settings - KWCSS_START_NAMESPACE(text, font) + // Character styles + KWCSS_START_NAMESPACE(text, character) KWCSS_DEFINE_PROPERTY(QFont, font) KWCSS_DEFINE_PROPERTY(QBrush, brush) - KWCSS_START_NAMESPACE(text.font, outline) + KWCSS_START_NAMESPACE(text.character, outline) KWCSS_DEFINE_PROPERTY(bool, enabled) KWCSS_DEFINE_PROPERTY(QPen, pen) KWCSS_END_NAMESPACE() - KWCSS_START_NAMESPACE(text.font, shadow) + KWCSS_START_NAMESPACE(text.character, shadow) KWCSS_DEFINE_PROPERTY(bool, enabled) + KWCSS_DEFINE_PROPERTY(QBrush, brush) + KWCSS_DEFINE_PROPERTY(int, offset) KWCSS_END_NAMESPACE() KWCSS_END_NAMESPACE() // Text layout settings + KWCSS_START_NAMESPACE(text, layout) + //KWCSS_DEFINE_PROPERTY(int, alignment) + KWCSS_START_NAMESPACE(text.layout, margins) + KWCSS_DEFINE_PROPERTY(float, left) + KWCSS_DEFINE_PROPERTY(float, right) + KWCSS_DEFINE_PROPERTY(float, top) + KWCSS_DEFINE_PROPERTY(float, bottom) + KWCSS_END_NAMESPACE() + KWCSS_END_NAMESPACE() KWCSS_END_NAMESPACE() - */ } #endif // _KwDisplayStyles_h_ diff --git a/kworship/display/KwTextLayer.cpp b/kworship/display/KwTextLayer.cpp index 6e4cc54..4ee2d2e 100644 --- a/kworship/display/KwTextLayer.cpp +++ b/kworship/display/KwTextLayer.cpp @@ -25,7 +25,8 @@ #include "KwTextLayer.h" -#include +#include +#include #include /* @@ -55,6 +56,16 @@ KwTextLayer::~KwTextLayer() } /* + * Mutators + */ + +/// Set text styles. +void KwTextLayer::setStyle(const KwTextStyle& style) +{ + m_style = style; +} + +/* * Private structures */ @@ -67,6 +78,7 @@ struct KwTextLayerData * Methods */ +#include void* KwTextLayer::addWidgets(QWidget* master) const { KwTextLayerData* data = new KwTextLayerData; @@ -79,8 +91,7 @@ void* KwTextLayer::addWidgets(QWidget* master) const data->textBrowser->viewport()->setAutoFillBackground(false); // Set the content - QFont font = data->textBrowser->document()->defaultFont(); - font.setPixelSize(64); + QFont font = m_style.character.font; data->textBrowser->document()->setDefaultFont(font); if (m_formatted) { @@ -97,14 +108,25 @@ void* KwTextLayer::addWidgets(QWidget* master) const // Create a text character format with outline etc and apply QTextCharFormat fmt; - fmt.setForeground(QBrush(Qt::cyan)); - fmt.setTextOutline(QPen(Qt::blue, 2)); + fmt.setForeground(m_style.character.brush); + if (m_style.character.outline.enabled) + { + fmt.setTextOutline(m_style.character.outline.pen); + } cursor.mergeCharFormat(fmt); - QStackedLayout* layout = new QStackedLayout(); - layout->setStackingMode(QStackedLayout::StackAll); + // Slightly hacky way of getting margins + QGridLayout* layout = new QGridLayout(); master->setLayout(layout); - layout->addWidget(data->textBrowser); + layout->addWidget(data->textBrowser, 1, 1, 1, 1); + layout->addItem(new QSpacerItem(1, m_style.layout.margins.top * master->height(), + QSizePolicy::Fixed, QSizePolicy::Fixed), 0, 1); + layout->addItem(new QSpacerItem(m_style.layout.margins.left * master->width(), 1, + QSizePolicy::Fixed, QSizePolicy::Fixed), 1, 0); + layout->addItem(new QSpacerItem(m_style.layout.margins.right * master->width(), 1, + QSizePolicy::Fixed, QSizePolicy::Fixed), 1, 2); + layout->addItem(new QSpacerItem(1, m_style.layout.margins.bottom * master->height(), + QSizePolicy::Fixed, QSizePolicy::Fixed), 2, 1); return (void*)data; } diff --git a/kworship/display/KwTextLayer.h b/kworship/display/KwTextLayer.h index 892a732..d58d73b 100644 --- a/kworship/display/KwTextLayer.h +++ b/kworship/display/KwTextLayer.h @@ -51,6 +51,13 @@ class KwTextLayer : public KwAbstractLayer virtual ~KwTextLayer(); /* + * Mutators + */ + + /// Set text styles. + void setStyle(const KwTextStyle& style); + + /* * Methods */ diff --git a/kworship/display/KwTextManager.cpp b/kworship/display/KwTextManager.cpp index a44289a..80d9459 100644 --- a/kworship/display/KwTextManager.cpp +++ b/kworship/display/KwTextManager.cpp @@ -33,7 +33,8 @@ /// Default constructor. KwTextManager::KwTextManager() -: m_currentLayer(0) +: m_style() +, m_currentLayer(0) { } @@ -50,7 +51,7 @@ KwTextManager::~KwTextManager() /// Apply the styles in a scope. void KwTextManager::applyStyles(KwCssScope* scope) { - Q_UNUSED(scope) + m_style.loadFrom(scope); } /// Clear all text. @@ -63,6 +64,7 @@ void KwTextManager::clear() void KwTextManager::setText(const QString& text) { KwTextLayer* lyrics = new KwTextLayer(text); + lyrics->setStyle(m_style); m_display.clearLayers(); m_display.setLayer(0, lyrics, true); @@ -74,6 +76,7 @@ void KwTextManager::setText(const QString& text) void KwTextManager::setHtml(const QString& html) { KwTextLayer* lyrics = new KwTextLayer(html, true); + lyrics->setStyle(m_style); m_display.clearLayers(); m_display.setLayer(0, lyrics, true); diff --git a/kworship/display/KwTextManager.h b/kworship/display/KwTextManager.h index d2479c1..0e91dd7 100644 --- a/kworship/display/KwTextManager.h +++ b/kworship/display/KwTextManager.h @@ -27,6 +27,7 @@ */ #include "KwAbstractDisplayManager.h" +#include "KwTextStyle.h" #include @@ -75,6 +76,9 @@ class KDE_EXPORT KwTextManager : public KwAbstractDisplayManager * Variables */ + /// Current style. + KwTextStyle m_style; + /// Current text layer. KwTextLayer* m_currentLayer; }; diff --git a/kworship/display/KwTextManager.cpp b/kworship/display/KwTextStyle.cpp similarity index 56% copy from kworship/display/KwTextManager.cpp copy to kworship/display/KwTextStyle.cpp index a44289a..822a25f 100644 --- a/kworship/display/KwTextManager.cpp +++ b/kworship/display/KwTextStyle.cpp @@ -18,66 +18,57 @@ ***************************************************************************/ /** - * @file KwTextManager.cpp - * @brief Text manager. + * @file KwTextStyle.cpp + * @brief Graphical text style information. * @author James Hogan */ -#include "KwTextManager.h" - -#include "KwTextLayer.h" +#include "KwTextStyle.h" +#include "KwDisplayStyles.h" /* - * Constructors + destructors + * Constructors + destructor. */ /// Default constructor. -KwTextManager::KwTextManager() -: m_currentLayer(0) +KwTextStyle::KwTextStyle() { + character.outline.enabled = false; + character.shadow.enabled = false; + character.shadow.offset = 5; + layout.margins.left = 0.05f; + layout.margins.right = 0.05f; + layout.margins.top = 0.05f; + layout.margins.bottom = 0.05f; } /// Destructor. -KwTextManager::~KwTextManager() +KwTextStyle::~KwTextStyle() { - delete m_currentLayer; } /* - * Main interface + * CSS Styles interface. */ -/// Apply the styles in a scope. -void KwTextManager::applyStyles(KwCssScope* scope) -{ - Q_UNUSED(scope) -} - -/// Clear all text. -void KwTextManager::clear() -{ - m_display.clearLayers(); -} - -/// Set the text. -void KwTextManager::setText(const QString& text) -{ - KwTextLayer* lyrics = new KwTextLayer(text); - m_display.clearLayers(); - m_display.setLayer(0, lyrics, true); - - delete m_currentLayer; - m_currentLayer = lyrics; -} - -/// Set formatted text. -void KwTextManager::setHtml(const QString& html) +/// Load styles from scope. +void KwTextStyle::loadFrom(KwCssScope* scope) { - KwTextLayer* lyrics = new KwTextLayer(html, true); - m_display.clearLayers(); - m_display.setLayer(0, lyrics, true); +#define GETSTYLE(A,B) A = KwDisplayStyles::text::B(scope) +#define GETSTYLE1(A1) GETSTYLE(A1, A1) +#define GETSTYLE2(A1,A2) GETSTYLE(A1.A2, A1::A2) +#define GETSTYLE3(A1,A2,A3) GETSTYLE(A1.A2.A3, A1::A2::A3) - delete m_currentLayer; - m_currentLayer = lyrics; + GETSTYLE2(character, font); + GETSTYLE2(character, brush); + GETSTYLE3(character, outline, enabled); + GETSTYLE3(character, outline, pen); + GETSTYLE3(character, shadow, enabled); + GETSTYLE3(character, shadow, brush); + GETSTYLE3(character, shadow, offset); + //GETSTYLE2(layout, alignment); + GETSTYLE3(layout, margins, left); + GETSTYLE3(layout, margins, right); + GETSTYLE3(layout, margins, top); + GETSTYLE3(layout, margins, bottom); } - diff --git a/kworship/display/KwTextStyle.h b/kworship/display/KwTextStyle.h index 48453fa..e6bf4f2 100644 --- a/kworship/display/KwTextStyle.h +++ b/kworship/display/KwTextStyle.h @@ -30,26 +30,73 @@ #include #include +class KwCssScope; + /// Background text layer. class KwTextStyle { public: /* + * Constructors + destructor. + */ + + /// Default constructor. + KwTextStyle(); + + /// Destructor. + virtual ~KwTextStyle(); + + /* + * CSS Styles interface. + */ + + /// Load styles from scope. + void loadFrom(KwCssScope* scope); + + /* * Variables */ - /// Font information. - QFont font; + /// Character style + struct Character + { + /// Font information. + QFont font; + + /// Inner brush fill. + QBrush brush; + + struct Outline + { + /// Whether to draw an outline around characters + bool enabled; + + /// Outline pen. + QPen pen; + } outline; + + struct Shadow + { + /// Whether to shadow behind characters + bool enabled; - /// Inner brush fill. - QBrush innerBrush; + /// Shadow brush. + QBrush brush; - /// Outline pen. - QPen ountlinePen; + /// Shadow offset. + int offset; + } shadow; + } character; - /// Shadow brush. - QBrush shadowBrush; + /// Layout style + struct Layout + { + /// Margins. + struct Margins { + float left, right, top, bottom; + } margins; + } layout; }; #endif // _KwTextStyle_h_ -- 2.11.4.GIT