add theme class

This commit is contained in:
laserpants 2016-05-12 01:18:09 +03:00
parent e17b6367b1
commit 7a9acce2d1
10 changed files with 554 additions and 18 deletions

View File

@ -1,13 +1,13 @@
#ifndef SLIDER_P_H
#define SLIDER_P_H
#include "slider.h"
#include <QPainter>
#include <QPropertyAnimation>
#include <QStateMachine>
#include <QEventTransition>
#include <QSignalTransition>
#include <QDebug>
#include "slider.h"
#include "lib/style.h"
#include "sliderthumb.h"
@ -51,14 +51,15 @@ SliderPrivate::SliderPrivate(Slider *parent)
pageStepMode(false),
stepTo(0),
oldValue(parent->value()),
trackWidth(2),
trackColor(QColor(200, 200, 200))
trackWidth(2)
{
parent->setMouseTracking(true);
}
void SliderPrivate::init(Slider *slider)
{
Style &style = Style::instance();
QState *topState = new QState(QState::ParallelStates);
QState *fstState = new QState(topState);
@ -79,16 +80,27 @@ void SliderPrivate::init(Slider *slider)
pulseOutState->assignProperty(thumb, "haloSize", 35);
pulseInState->assignProperty(thumb, "haloSize", 28);
QColor disabledColor = style.themeColor("disabled");
disabledState->assignProperty(thumb, "diameter", 7);
disabledState->assignProperty(thumb, "fillColor", QColor(200, 200, 200));
disabledState->assignProperty(thumb, "fillColor", disabledColor);
disabledState->assignProperty(slider, "trackColor", disabledColor);
inactiveState->assignProperty(thumb, "diameter", 11);
focusState->assignProperty(thumb, "diameter", 11);
slidingState->assignProperty(thumb, "diameter", 17);
inactiveState->assignProperty(thumb, "fillColor", QColor(0, 0, 0));
focusState->assignProperty(thumb, "fillColor", QColor(0, 0, 0));
slidingState->assignProperty(thumb, "fillColor", QColor(0, 0, 0));
QColor trackColor = style.themeColor("accent3");
inactiveState->assignProperty(slider, "trackColor", trackColor);
focusState->assignProperty(slider, "trackColor", trackColor);
slidingState->assignProperty(slider, "trackColor", trackColor);
QColor fillColor = style.themeColor("primary1");
inactiveState->assignProperty(thumb, "fillColor", fillColor);
focusState->assignProperty(thumb, "fillColor", fillColor);
slidingState->assignProperty(thumb, "fillColor", fillColor);
machine.addState(topState);
@ -180,11 +192,20 @@ void SliderPrivate::init(Slider *slider)
QState *minState = new QState(sndState);
QState *normalState = new QState(sndState);
minState->assignProperty(thumb, "minFillColor", QColor(255, 255, 255));
minState->assignProperty(thumb, "fillColor", QColor(255, 255, 255));
QColor minHaloColor(trackColor);
QColor haloColor = style.themeColor("primary1");
minHaloColor.setAlphaF(0.2);
haloColor.setAlphaF(0.2);
QColor canvasColor = style.themeColor("canvas");
minState->assignProperty(thumb, "minFillColor", canvasColor);
minState->assignProperty(thumb, "fillColor", canvasColor);
minState->assignProperty(thumb, "haloColor", minHaloColor);
minState->assignProperty(thumb, "borderWidth", 2);
normalState->assignProperty(thumb, "fillColor", QColor(0, 0, 0));
normalState->assignProperty(thumb, "minFillColor", QColor(0, 0, 0));
normalState->assignProperty(thumb, "fillColor", fillColor);
normalState->assignProperty(thumb, "minFillColor", fillColor);
normalState->assignProperty(thumb, "haloColor", haloColor);
normalState->assignProperty(thumb, "borderWidth", 0);
sndState->setInitialState(minState);
@ -195,6 +216,10 @@ void SliderPrivate::init(Slider *slider)
animation->setDuration(200);
transition->addAnimation(animation);
minState->addTransition(transition);
animation = new QPropertyAnimation(thumb, "haloColor");
animation->setDuration(200);
transition->addAnimation(animation);
minState->addTransition(transition);
transition = new QSignalTransition(slider, SIGNAL(changedToMinimum()));
transition->setTargetState(minState);
@ -202,6 +227,10 @@ void SliderPrivate::init(Slider *slider)
animation->setDuration(200);
transition->addAnimation(animation);
normalState->addTransition(transition);
animation = new QPropertyAnimation(thumb, "haloColor");
animation->setDuration(200);
transition->addAnimation(animation);
normalState->addTransition(transition);
machine.start();

View File

@ -1,6 +1,7 @@
#include "sliderthumb.h"
#include <QEvent>
#include <QPainter>
#include "lib/style.h"
#include "slider.h"
SliderThumb::SliderThumb(Slider *slider)
@ -37,7 +38,7 @@ void SliderThumb::paintEvent(QPaintEvent *event)
QBrush brush;
brush.setStyle(Qt::SolidPattern);
brush.setColor(QColor(0, 0, 0, 20));
brush.setColor(_haloColor);
painter.setBrush(brush);
painter.setPen(Qt::NoPen);

View File

@ -17,6 +17,7 @@ class SliderThumb : public QWidget
Q_PROPERTY(QColor fillColor WRITE setFillColor READ fillColor)
Q_PROPERTY(QColor minFillColor WRITE setMinFillColor READ minFillColor)
Q_PROPERTY(qreal haloSize WRITE setHaloSize READ haloSize)
Q_PROPERTY(QColor haloColor WRITE setHaloColor READ haloColor)
friend class SliderPrivate;
explicit SliderThumb(Slider *slider);
@ -74,7 +75,21 @@ public:
update();
}
inline qreal haloSize() const { return _haloSize; }
inline qreal haloSize() const
{
return _haloSize;
}
inline void setHaloColor(const QColor &color)
{
_haloColor = color;
update();
}
inline QColor haloColor() const
{
return _haloColor;
}
protected:
bool eventFilter(QObject *obj, QEvent *event);
@ -86,10 +101,10 @@ private:
const Slider *const slider;
qreal _diameter;
qreal _borderWidth;
qreal _haloSize;
QColor _fillColor;
QColor _minFillColor;
qreal _haloSize;
QColor _haloColor;
};
#endif // SLIDERTHUMB_H

View File

@ -40,6 +40,7 @@ SliderExamples::SliderExamples(QWidget *parent)
ExampleView *view = new ExampleView;
view->setWidget(widget);
view->setBackgroundRole(QPalette::Base);
Frame *frame = new Frame;
frame->setCodeSnippet(

View File

@ -1,6 +1,20 @@
#include "lib/style.h"
#include <QPainter>
#include <QStyleOption>
#include "lib/style.h"
void Style::setTheme(Theme *theme)
{
_theme = theme;
theme->setParent(this);
}
QColor Style::themeColor(const QString &key) const
{
if (!_theme)
return QColor();
return _theme->getColor(key);
}
void Style::drawPrimitive(PrimitiveElement pe, const QStyleOption *opt, QPainter *p, const QWidget *w) const
{

View File

@ -2,6 +2,7 @@
#define STYLE_H
#include <QCommonStyle>
#include "theme.h"
class Style : public QCommonStyle
{
@ -11,9 +12,13 @@ public:
static Style &instance()
{
static Style instance;
instance.setTheme(new Theme);
return instance;
}
void setTheme(Theme *theme);
QColor themeColor(const QString &key) const;
void drawPrimitive(PrimitiveElement pe, const QStyleOption *opt, QPainter *p,
const QWidget *w = 0) const Q_DECL_OVERRIDE;
@ -22,6 +27,8 @@ private:
Style(Style const &);
void operator=(Style const &);
Theme *_theme;
};
#endif // STYLE_H

130
lib/theme.cpp Normal file
View File

@ -0,0 +1,130 @@
#include "theme.h"
#include <QDebug>
#include "theme_p.h"
Theme::Theme(QObject *parent)
: QObject(parent),
d_ptr(new ThemePrivate(this))
{
setColor("primary1", Material::cyan500);
setColor("primary2", Material::cyan700);
setColor("primary3", Material::lightBlack);
setColor("accent1", Material::pinkA200);
setColor("accent2", Material::grey100);
setColor("accent3", Material::grey500);
setColor("text", Material::darkBlack);
setColor("alternateText", Material::white);
setColor("canvas", Material::white);
setColor("border", Material::grey300);
QColor black(Material::darkBlack);
black.setAlphaF(0.28);
setColor("disabled", black);
}
Theme::~Theme()
{
}
QColor Theme::getColor(const QString &key) const
{
Q_D(const Theme);
if (!d->colors.contains(key)) {
qWarning() << "Theme color with name " << key << " does not exist.";
return QColor();
}
return d->colors.value(key);
}
void Theme::setColor(const QString &key, const QColor &value)
{
Q_D(Theme);
d->colors.insert(key, value);
}
void Theme::setColor(const QString &key, Material::Color color)
{
Q_D(Theme);
static const QColor palette[] = {
QColor("#ffebee"), QColor("#ffcdd2"), QColor("#ef9a9a"), QColor("#e57373"),
QColor("#ef5350"), QColor("#f44336"), QColor("#e53935"), QColor("#d32f2f"),
QColor("#c62828"), QColor("#b71c1c"), QColor("#ff8a80"), QColor("#ff5252"),
QColor("#ff1744"), QColor("#d50000"), QColor("#fce4ec"), QColor("#f8bbd0"),
QColor("#f48fb1"), QColor("#f06292"), QColor("#ec407a"), QColor("#e91e63"),
QColor("#d81b60"), QColor("#c2185b"), QColor("#ad1457"), QColor("#880e4f"),
QColor("#ff80ab"), QColor("#ff4081"), QColor("#f50057"), QColor("#c51162"),
QColor("#f3e5f5"), QColor("#e1bee7"), QColor("#ce93d8"), QColor("#ba68c8"),
QColor("#ab47bc"), QColor("#9c27b0"), QColor("#8e24aa"), QColor("#7b1fa2"),
QColor("#6a1b9a"), QColor("#4a148c"), QColor("#ea80fc"), QColor("#e040fb"),
QColor("#d500f9"), QColor("#aa00ff"), QColor("#ede7f6"), QColor("#d1c4e9"),
QColor("#b39ddb"), QColor("#9575cd"), QColor("#7e57c2"), QColor("#673ab7"),
QColor("#5e35b1"), QColor("#512da8"), QColor("#4527a0"), QColor("#311b92"),
QColor("#b388ff"), QColor("#7c4dff"), QColor("#651fff"), QColor("#6200ea"),
QColor("#e8eaf6"), QColor("#c5cae9"), QColor("#9fa8da"), QColor("#7986cb"),
QColor("#5c6bc0"), QColor("#3f51b5"), QColor("#3949ab"), QColor("#303f9f"),
QColor("#283593"), QColor("#1a237e"), QColor("#8c9eff"), QColor("#536dfe"),
QColor("#3d5afe"), QColor("#304ffe"), QColor("#e3f2fd"), QColor("#bbdefb"),
QColor("#90caf9"), QColor("#64b5f6"), QColor("#42a5f5"), QColor("#2196f3"),
QColor("#1e88e5"), QColor("#1976d2"), QColor("#1565c0"), QColor("#0d47a1"),
QColor("#82b1ff"), QColor("#448aff"), QColor("#2979ff"), QColor("#2962ff"),
QColor("#e1f5fe"), QColor("#b3e5fc"), QColor("#81d4fa"), QColor("#4fc3f7"),
QColor("#29b6f6"), QColor("#03a9f4"), QColor("#039be5"), QColor("#0288d1"),
QColor("#0277bd"), QColor("#01579b"), QColor("#80d8ff"), QColor("#40c4ff"),
QColor("#00b0ff"), QColor("#0091ea"), QColor("#e0f7fa"), QColor("#b2ebf2"),
QColor("#80deea"), QColor("#4dd0e1"), QColor("#26c6da"), QColor("#00bcd4"),
QColor("#00acc1"), QColor("#0097a7"), QColor("#00838f"), QColor("#006064"),
QColor("#84ffff"), QColor("#18ffff"), QColor("#00e5ff"), QColor("#00b8d4"),
QColor("#e0f2f1"), QColor("#b2dfdb"), QColor("#80cbc4"), QColor("#4db6ac"),
QColor("#26a69a"), QColor("#009688"), QColor("#00897b"), QColor("#00796b"),
QColor("#00695c"), QColor("#004d40"), QColor("#a7ffeb"), QColor("#64ffda"),
QColor("#1de9b6"), QColor("#00bfa5"), QColor("#e8f5e9"), QColor("#c8e6c9"),
QColor("#a5d6a7"), QColor("#81c784"), QColor("#66bb6a"), QColor("#4caf50"),
QColor("#43a047"), QColor("#388e3c"), QColor("#2e7d32"), QColor("#1b5e20"),
QColor("#b9f6ca"), QColor("#69f0ae"), QColor("#00e676"), QColor("#00c853"),
QColor("#f1f8e9"), QColor("#dcedc8"), QColor("#c5e1a5"), QColor("#aed581"),
QColor("#9ccc65"), QColor("#8bc34a"), QColor("#7cb342"), QColor("#689f38"),
QColor("#558b2f"), QColor("#33691e"), QColor("#ccff90"), QColor("#b2ff59"),
QColor("#76ff03"), QColor("#64dd17"), QColor("#f9fbe7"), QColor("#f0f4c3"),
QColor("#e6ee9c"), QColor("#dce775"), QColor("#d4e157"), QColor("#cddc39"),
QColor("#c0ca33"), QColor("#afb42b"), QColor("#9e9d24"), QColor("#827717"),
QColor("#f4ff81"), QColor("#eeff41"), QColor("#c6ff00"), QColor("#aeea00"),
QColor("#fffde7"), QColor("#fff9c4"), QColor("#fff59d"), QColor("#fff176"),
QColor("#ffee58"), QColor("#ffeb3b"), QColor("#fdd835"), QColor("#fbc02d"),
QColor("#f9a825"), QColor("#f57f17"), QColor("#ffff8d"), QColor("#ffff00"),
QColor("#ffea00"), QColor("#ffd600"), QColor("#fff8e1"), QColor("#ffecb3"),
QColor("#ffe082"), QColor("#ffd54f"), QColor("#ffca28"), QColor("#ffc107"),
QColor("#ffb300"), QColor("#ffa000"), QColor("#ff8f00"), QColor("#ff6f00"),
QColor("#ffe57f"), QColor("#ffd740"), QColor("#ffc400"), QColor("#ffab00"),
QColor("#fff3e0"), QColor("#ffe0b2"), QColor("#ffcc80"), QColor("#ffb74d"),
QColor("#ffa726"), QColor("#ff9800"), QColor("#fb8c00"), QColor("#f57c00"),
QColor("#ef6c00"), QColor("#e65100"), QColor("#ffd180"), QColor("#ffab40"),
QColor("#ff9100"), QColor("#ff6d00"), QColor("#fbe9e7"), QColor("#ffccbc"),
QColor("#ffab91"), QColor("#ff8a65"), QColor("#ff7043"), QColor("#ff5722"),
QColor("#f4511e"), QColor("#e64a19"), QColor("#d84315"), QColor("#bf360c"),
QColor("#ff9e80"), QColor("#ff6e40"), QColor("#ff3d00"), QColor("#dd2c00"),
QColor("#efebe9"), QColor("#d7ccc8"), QColor("#bcaaa4"), QColor("#a1887f"),
QColor("#8d6e63"), QColor("#795548"), QColor("#6d4c41"), QColor("#5d4037"),
QColor("#4e342e"), QColor("#3e2723"), QColor("#eceff1"), QColor("#cfd8dc"),
QColor("#b0bec5"), QColor("#90a4ae"), QColor("#78909c"), QColor("#607d8b"),
QColor("#546e7a"), QColor("#455a64"), QColor("#37474f"), QColor("#263238"),
QColor("#fafafa"), QColor("#f5f5f5"), QColor("#eeeeee"), QColor("#e0e0e0"),
QColor("#bdbdbd"), QColor("#9e9e9e"), QColor("#757575"), QColor("#616161"),
QColor("#424242"), QColor("#212121"), QColor("#000000"), QColor("#ffffff"),
d->rgba(0, 0, 0, 0),
d->rgba(0, 0, 0, 1),
d->rgba(0, 0, 0, 0.87),
d->rgba(0, 0, 0, 0.54),
d->rgba(0, 0, 0, 0.26),
d->rgba(0, 0, 0, 0.12),
d->rgba(255, 255, 255, 1),
d->rgba(255, 255, 255, 0.87),
d->rgba(255, 255, 255, 0.54)
};
d->colors.insert(key, palette[color]);
}

303
lib/theme.h Normal file
View File

@ -0,0 +1,303 @@
#ifndef THEME_H
#define THEME_H
#include <QObject>
#include <QHash>
#include <QColor>
#include <QScopedPointer>
namespace Material
{
enum Color {
red50,
red100,
red200,
red300,
red400,
red500,
red600,
red700,
red800,
red900,
redA100,
redA200,
redA400,
redA700,
pink50,
pink100,
pink200,
pink300,
pink400,
pink500,
pink600,
pink700,
pink800,
pink900,
pinkA100,
pinkA200,
pinkA400,
pinkA700,
purple50,
purple100,
purple200,
purple300,
purple400,
purple500,
purple600,
purple700,
purple800,
purple900,
purpleA100,
purpleA200,
purpleA400,
purpleA700,
deepPurple50,
deepPurple100,
deepPurple200,
deepPurple300,
deepPurple400,
deepPurple500,
deepPurple600,
deepPurple700,
deepPurple800,
deepPurple900,
deepPurpleA100,
deepPurpleA200,
deepPurpleA400,
deepPurpleA700,
indigo50,
indigo100,
indigo200,
indigo300,
indigo400,
indigo500,
indigo600,
indigo700,
indigo800,
indigo900,
indigoA100,
indigoA200,
indigoA400,
indigoA700,
blue50,
blue100,
blue200,
blue300,
blue400,
blue500,
blue600,
blue700,
blue800,
blue900,
blueA100,
blueA200,
blueA400,
blueA700,
lightBlue50,
lightBlue100,
lightBlue200,
lightBlue300,
lightBlue400,
lightBlue500,
lightBlue600,
lightBlue700,
lightBlue800,
lightBlue900,
lightBlueA100,
lightBlueA200,
lightBlueA400,
lightBlueA700,
cyan50,
cyan100,
cyan200,
cyan300,
cyan400,
cyan500,
cyan600,
cyan700,
cyan800,
cyan900,
cyanA100,
cyanA200,
cyanA400,
cyanA700,
teal50,
teal100,
teal200,
teal300,
teal400,
teal500,
teal600,
teal700,
teal800,
teal900,
tealA100,
tealA200,
tealA400,
tealA700,
green50,
green100,
green200,
green300,
green400,
green500,
green600,
green700,
green800,
green900,
greenA100,
greenA200,
greenA400,
greenA700,
lightGreen50,
lightGreen100,
lightGreen200,
lightGreen300,
lightGreen400,
lightGreen500,
lightGreen600,
lightGreen700,
lightGreen800,
lightGreen900,
lightGreenA100,
lightGreenA200,
lightGreenA400,
lightGreenA700,
lime50,
lime100,
lime200,
lime300,
lime400,
lime500,
lime600,
lime700,
lime800,
lime900,
limeA100,
limeA200,
limeA400,
limeA700,
yellow50,
yellow100,
yellow200,
yellow300,
yellow400,
yellow500,
yellow600,
yellow700,
yellow800,
yellow900,
yellowA100,
yellowA200,
yellowA400,
yellowA700,
amber50,
amber100,
amber200,
amber300,
amber400,
amber500,
amber600,
amber700,
amber800,
amber900,
amberA100,
amberA200,
amberA400,
amberA700,
orange50,
orange100,
orange200,
orange300,
orange400,
orange500,
orange600,
orange700,
orange800,
orange900,
orangeA100,
orangeA200,
orangeA400,
orangeA700,
deepOrange50,
deepOrange100,
deepOrange200,
deepOrange300,
deepOrange400,
deepOrange500,
deepOrange600,
deepOrange700,
deepOrange800,
deepOrange900,
deepOrangeA100,
deepOrangeA200,
deepOrangeA400,
deepOrangeA700,
brown50,
brown100,
brown200,
brown300,
brown400,
brown500,
brown600,
brown700,
brown800,
brown900,
blueGrey50,
blueGrey100,
blueGrey200,
blueGrey300,
blueGrey400,
blueGrey500,
blueGrey600,
blueGrey700,
blueGrey800,
blueGrey900,
grey50,
grey100,
grey200,
grey300,
grey400,
grey500,
grey600,
grey700,
grey800,
grey900,
black,
white,
transparent,
fullBlack,
darkBlack,
lightBlack,
minBlack,
faintBlack,
fullWhite,
darkWhite,
lightWhite
};
}
class ThemePrivate;
class Theme : public QObject
{
Q_OBJECT
public:
explicit Theme(QObject *parent = 0);
~Theme();
QColor getColor(const QString &key) const;
void setColor(const QString &key, const QColor &color);
void setColor(const QString &key, Material::Color color);
protected:
const QScopedPointer<ThemePrivate> d_ptr;
private:
Q_DISABLE_COPY(Theme)
Q_DECLARE_PRIVATE(Theme)
};
#endif // THEME_H

33
lib/theme_p.h Normal file
View File

@ -0,0 +1,33 @@
#ifndef THEME_P_H
#define THEME_P_H
#include "theme.h"
class ThemePrivate
{
Q_DISABLE_COPY(ThemePrivate)
Q_DECLARE_PUBLIC(Theme)
public:
ThemePrivate(Theme *parent);
QColor rgba(int r, int g, int b, qreal a) const;
Theme *const q_ptr;
QHash<QString, QColor> colors;
};
ThemePrivate::ThemePrivate(Theme *parent)
: q_ptr(parent)
{
}
QColor ThemePrivate::rgba(int r, int g, int b, qreal a) const
{
QColor color(r, g, b);
color.setAlphaF(a);
return color;
}
#endif // THEME_P_H

View File

@ -52,7 +52,8 @@ SOURCES += main.cpp\
lib/scaleeffect.cpp \
lib/style.cpp \
components/sliderthumb.cpp \
components/searchfield.cpp
components/searchfield.cpp \
lib/theme.cpp
HEADERS += mainwindow.h \
components/appbar.h \
@ -99,7 +100,9 @@ HEADERS += mainwindow.h \
lib/scaleeffect.h \
lib/style.h \
components/sliderthumb.h \
components/searchfield.h
components/searchfield.h \
lib/theme.h \
lib/theme_p.h
RESOURCES += \
resources.qrc