All Classes Namespaces Functions Variables Enumerations Properties Pages
backgroundwidget.cpp
1 /*
2 
3 Pencil2D - Traditional Animation Software
4 Copyright (C) 2005-2007 Patrick Corrieri & Pascal Naidon
5 Copyright (C) 2012-2020 Matthew Chiawen Chang
6 
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; version 2 of the License.
10 
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15 
16 */
17 
18 #include "backgroundwidget.h"
19 
20 #include <QStyleOption>
21 #include <QPainter>
22 
23 
24 BackgroundWidget::BackgroundWidget(QWidget* parent) : QWidget(parent)
25 {
26  setObjectName("BackgroundWidget");
27 
28  // Qt::WA_StaticContents ensure that the widget contents are rooted to the top-left corner
29  // and don't change when the widget is resized.
30  setAttribute( Qt::WA_StaticContents );
31 }
32 
33 BackgroundWidget::~BackgroundWidget()
34 {
35 }
36 
37 void BackgroundWidget::init(PreferenceManager *prefs)
38 {
39  mPrefs = prefs;
40  connect(mPrefs, &PreferenceManager::optionChanged, this, &BackgroundWidget::settingUpdated);
41 
42  loadBackgroundStyle();
43  mHasShadow = mPrefs->isOn(SETTING::SHADOW);
44 
45  update();
46 }
47 
48 void BackgroundWidget::settingUpdated(SETTING setting)
49 {
50  switch ( setting )
51  {
52  case SETTING::BACKGROUND_STYLE:
53 
54  loadBackgroundStyle();
55  update();
56  break;
57 
58  case SETTING::SHADOW:
59 
60  mHasShadow = mPrefs->isOn(SETTING::SHADOW);
61  update();
62  break;
63 
64  default:
65  break;
66  }
67 }
68 
69 void BackgroundWidget::paintEvent(QPaintEvent *)
70 {
71  QStyleOption opt;
72  opt.init(this);
73  QPainter painter(this);
74  style()->drawPrimitive(QStyle::PE_Widget, &opt, &painter, this);
75 
76  if (mHasShadow)
77  drawShadow(painter);
78 }
79 
80 void BackgroundWidget::loadBackgroundStyle()
81 {
82  QString bgName = mPrefs->getString(SETTING::BACKGROUND_STYLE);
83  mStyle = "background-color:white; border: 1px solid lightGrey;";
84 
85  if ( bgName == "white" )
86  {
87  mStyle = "background-color:white; border: 1px solid lightGrey;";
88  }
89  else if ( bgName == "grey" )
90  {
91  mStyle = "background-color:lightGrey; border: 1px solid grey;";
92  }
93  else if ( bgName == "checkerboard" )
94  {
95  mStyle = "background-image: url(:background/checkerboard.png); background-repeat: repeat-xy; border: 1px solid lightGrey;";
96  }
97  else if ( bgName == "dots" )
98  {
99  mStyle = "background-image: url(:background/dots.png); background-repeat: repeat-xy; border: 1px solid lightGrey;";
100  }
101  else if ( bgName == "weave" )
102  {
103  mStyle = "background-image: url(:background/weave.jpg); background-repeat: repeat-xy; border: 1px solid lightGrey;";
104  }
105  else if ( bgName == "grid" )
106  {
107  mStyle = "background-image: url(:background/grid.jpg); background-repeat: repeat-xy; border: 1px solid lightGrey;";
108  }
109 
110  mStyle = QString("BackgroundWidget { %1 }").arg(mStyle);
111 
112  setStyleSheet(mStyle);
113 }
114 
115 void BackgroundWidget::drawShadow( QPainter& painter )
116 {
117  int radius1 = 12;
118  int radius2 = 8;
119 
120  QColor color = Qt::black;
121  qreal opacity = 0.15;
122 
123  QLinearGradient shadow = QLinearGradient( 0, 0, 0, radius1 );
124 
125  int r = color.red();
126  int g = color.green();
127  int b = color.blue();
128  qreal a = color.alphaF();
129  shadow.setColorAt( 0.0, QColor( r, g, b, qRound( a * 255 * opacity ) ) );
130  shadow.setColorAt( 1.0, QColor( r, g, b, 0 ) );
131 
132  painter.setPen( Qt::NoPen );
133  painter.setBrush( shadow );
134  painter.drawRect( QRect( 0, 0, width(), radius1 ) );
135 
136  shadow.setFinalStop( radius1, 0 );
137  painter.setBrush( shadow );
138  painter.drawRect( QRect( 0, 0, radius1, height() ) );
139 
140  shadow.setStart( 0, height() );
141  shadow.setFinalStop( 0, height() - radius2 );
142  painter.setBrush( shadow );
143  painter.drawRect( QRect( 0, height() - radius2, width(), height() ) );
144 
145  shadow.setStart( width(), 0 );
146  shadow.setFinalStop( width() - radius2, 0 );
147  painter.setBrush( shadow );
148  painter.drawRect( QRect( width() - radius2, 0, width(), height() ) );
149 }
void setStyleSheet(const QString &styleSheet)
qreal alphaF() const const
void setColorAt(qreal position, const QColor &color)
QStyle * style() const const
void update()
int width() const const
void drawRect(const QRectF &rectangle)
int red() const const
void setPen(const QColor &color)
void setBrush(const QBrush &brush)
int green() const const
void setFinalStop(const QPointF &stop)
WA_StaticContents
int blue() const const
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
void init(const QWidget *widget)
virtual void drawPrimitive(QStyle::PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const const =0
void setStart(const QPointF &start)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
int height() const const