From 64fa985928aaeb8121907e4a3abba0472ebd22a2 Mon Sep 17 00:00:00 2001 From: laserpants Date: Sun, 20 Mar 2016 19:43:05 +0300 Subject: [PATCH] make Style class singleton --- components/flatbutton.cpp | 13 +++++++------ components/iconbutton.cpp | 3 +++ components/table.cpp | 3 +++ style.cpp | 18 ++++++++---------- style.h | 15 +++++++++++---- 5 files changed, 32 insertions(+), 20 deletions(-) diff --git a/components/flatbutton.cpp b/components/flatbutton.cpp index f09a471..069d745 100644 --- a/components/flatbutton.cpp +++ b/components/flatbutton.cpp @@ -3,11 +3,13 @@ #include #include #include "flatbutton.h" +#include "style.h" FlatButton::FlatButton(QWidget *parent) : QAbstractButton(parent), _overlay(new RippleOverlay(this)) { + setStyle(&Style::instance()); } FlatButton::FlatButton(const QString &text, QWidget *parent) @@ -15,6 +17,7 @@ FlatButton::FlatButton(const QString &text, QWidget *parent) _overlay(new RippleOverlay(this)) { setText(text); + setStyle(&Style::instance()); } FlatButton::~FlatButton() @@ -51,8 +54,8 @@ QSize FlatButton::sizeHint() const w += sz.width(); if (!empty || !h) h = qMax(h, sz.height()); - return (style()->sizeFromContents(QStyle::CT_PushButton, &option, QSize(w, h), this). - expandedTo(QApplication::globalStrut())); + return (style()->sizeFromContents(QStyle::CT_PushButton, &option, QSize(w, h), this) + .expandedTo(QApplication::globalStrut())); } void FlatButton::resizeEvent(QResizeEvent *event) @@ -68,11 +71,9 @@ void FlatButton::paintEvent(QPaintEvent *event) QStylePainter painter(this); - QStyleOptionButton option(getStyleOption()); + painter.drawControl(QStyle::CE_PushButton, getStyleOption()); - painter.drawControl(QStyle::CE_PushButton, option); - - if (underMouse()) { + if (testAttribute(Qt::WA_Hover) && underMouse()) { QRect r(rect()); QBrush brush; brush.setStyle(Qt::SolidPattern); diff --git a/components/iconbutton.cpp b/components/iconbutton.cpp index b0985b8..2830388 100644 --- a/components/iconbutton.cpp +++ b/components/iconbutton.cpp @@ -5,6 +5,7 @@ #include #include "iconbutton.h" #include "../lib/rippleoverlay.h" +#include "style.h" IconButton::IconButton(const QIcon &icon, QWidget *parent) : QAbstractButton(parent), @@ -18,6 +19,8 @@ IconButton::IconButton(const QIcon &icon, QWidget *parent) setSizePolicy(policy); setGeometryWidget(this); + + setStyle(&Style::instance()); } IconButton::~IconButton() diff --git a/components/table.cpp b/components/table.cpp index 149b321..5496f92 100644 --- a/components/table.cpp +++ b/components/table.cpp @@ -2,6 +2,7 @@ #include #include #include "table.h" +#include "style.h" Table::Table(QWidget *parent) : QTableWidget(parent) @@ -10,6 +11,8 @@ Table::Table(QWidget *parent) setSelectionMode(QAbstractItemView::SingleSelection); setSelectionBehavior(QAbstractItemView::SelectRows); setVerticalScrollMode(QAbstractItemView::ScrollPerPixel); + + setStyle(&Style::instance()); } Table::~Table() diff --git a/style.cpp b/style.cpp index 0c96856..fbccb02 100644 --- a/style.cpp +++ b/style.cpp @@ -1,17 +1,9 @@ #include #include #include +#include #include "style.h" -Style::Style() - : QProxyStyle() -{ -} - -Style::~Style() -{ -} - void Style::drawPrimitive(PrimitiveElement pe, const QStyleOption *opt, QPainter *p, const QWidget *w) const { @@ -20,7 +12,7 @@ void Style::drawPrimitive(PrimitiveElement pe, const QStyleOption *opt, QPainter switch (pe) { case PE_FrameFocusRect: p->setPen(Qt::blue); - p->drawRect(opt->rect); + p->drawRect(opt->rect.adjusted(0, 0, -1, -1)); break; default: QProxyStyle::drawPrimitive(pe, opt, p, w); @@ -33,6 +25,12 @@ void Style::drawControl(ControlElement element, const QStyleOption *opt, qDebug() << element; switch (element) { + case CE_CheckBox: + if (const QStyleOptionButton *item = qstyleoption_cast(opt)) { + + } + p->fillRect(opt->rect, Qt::black); + break; /* case CE_FocusFrame: p->fillRect(opt->rect, opt->palette.foreground()); diff --git a/style.h b/style.h index 417e6f0..d553e0e 100644 --- a/style.h +++ b/style.h @@ -3,14 +3,16 @@ #include -//class Style : public QCommonStyle class Style : public QProxyStyle { Q_OBJECT public: - Style(); - ~Style(); + static Style &instance() + { + static Style instance; + return instance; + } void drawPrimitive(PrimitiveElement pe, const QStyleOption *opt, QPainter *p, const QWidget *w = 0) const Q_DECL_OVERRIDE; @@ -24,7 +26,12 @@ public: const QWidget *w = 0) const Q_DECL_OVERRIDE; int pixelMetric(PixelMetric m, const QStyleOption *opt = 0, const QWidget *widget = 0) const Q_DECL_OVERRIDE; + +private: + Style() {} + + Style(Style const&); + void operator=(Style const&); }; - #endif // STYLE_H