Pencil2D  ff90c0872e88be3bf81c548cd60f01983012ec49
Pencil2D is an animation software for both bitmap and vector graphics. It is free, multi-platform, and open source.
 All Classes Functions
backgroundwidget.cpp
1 /*
2 
3 Pencil - Traditional Animation Software
4 Copyright (C) 2005-2007 Patrick Corrieri & Pascal Naidon
5 Copyright (C) 2012-2017 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 "QStyleOption"
19 #include "QPainter"
20 #include "backgroundwidget.h"
21 
22 BackgroundWidget::BackgroundWidget(QWidget *parent) : QWidget(parent)
23 {
24  setObjectName( "BackgroundWidget" );
25 
26  // Qt::WA_StaticContents ensure that the widget contents are rooted to the top-left corner
27  // and don't change when the widget is resized.
28  setAttribute( Qt::WA_StaticContents );
29 }
30 
31 BackgroundWidget::~BackgroundWidget()
32 {
33 
34 }
35 
36 void BackgroundWidget::init(PreferenceManager *prefs)
37 {
38  mPrefs = prefs;
39  connect(mPrefs, &PreferenceManager::optionChanged, this, &BackgroundWidget::settingUpdated);
40 
41  loadBackgroundStyle();
42  mHasShadow = mPrefs->isOn(SETTING::SHADOW);
43 
44  update();
45 }
46 
47 void BackgroundWidget::settingUpdated(SETTING setting)
48 {
49  switch ( setting )
50  {
51  case SETTING::BACKGROUND_STYLE:
52 
53  loadBackgroundStyle();
54  update();
55  break;
56 
57  case SETTING::SHADOW:
58 
59  mHasShadow = mPrefs->isOn(SETTING::SHADOW);
60  update();
61  break;
62 
63  default:
64  break;
65  }
66 }
67 
68 void BackgroundWidget::paintEvent(QPaintEvent *)
69 {
70  QStyleOption opt;
71  opt.init(this);
72  QPainter painter(this);
73  style()->drawPrimitive(QStyle::PE_Widget, &opt, &painter, this);
74 
75  if (mHasShadow) {
76  drawShadow(painter);
77  }
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  setStyleSheet(mStyle);
111 }
112 
113 void BackgroundWidget::drawShadow( QPainter& painter )
114 {
115  int radius1 = 12;
116  int radius2 = 8;
117 
118  QColor colour = Qt::black;
119  qreal opacity = 0.15;
120 
121  QLinearGradient shadow = QLinearGradient( 0, 0, 0, radius1 );
122 
123  int r = colour.red();
124  int g = colour.green();
125  int b = colour.blue();
126  qreal a = colour.alphaF();
127  shadow.setColorAt( 0.0, QColor( r, g, b, qRound( a * 255 * opacity ) ) );
128  shadow.setColorAt( 1.0, QColor( r, g, b, 0 ) );
129 
130 
131  painter.setPen( Qt::NoPen );
132  painter.setBrush( shadow );
133  painter.drawRect( QRect( 0, 0, width(), radius1 ) );
134 
135  shadow.setFinalStop( radius1, 0 );
136  painter.setBrush( shadow );
137  painter.drawRect( QRect( 0, 0, radius1, height() ) );
138 
139  shadow.setStart( 0, height() );
140  shadow.setFinalStop( 0, height() - radius2 );
141  painter.setBrush( shadow );
142  painter.drawRect( QRect( 0, height() - radius2, width(), height() ) );
143 
144  shadow.setStart( width(), 0 );
145  shadow.setFinalStop( width() - radius2, 0 );
146  painter.setBrush( shadow );
147  painter.drawRect( QRect( width() - radius2, 0, width(), height() ) );
148 }