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