-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqtsvgbutton.cpp
More file actions
203 lines (154 loc) · 4.84 KB
/
qtsvgbutton.cpp
File metadata and controls
203 lines (154 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
Embedded Widgets Demo
Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).*
Contact: Qt Software Information (qt-info@nokia.com)**
This file may be used under the terms of the Embedded Widgets Demo License
Agreement.
*/
#include <QtSvg/QSvgRenderer>
#include <QtGui/QPainter>
#include "qtsvgbutton.h"
/*!
\class QtSvgButton qtsvgbutton.h
The description of the QtSvgButton.
\brief The QtSvgButton class provides a Button, that can change the display style.
The QtSvgButton is an example to show the capabilities of the Qt Framework related
to customized controls.
Here is a possible implementation shown
\code
// Create a QtSvgButton
QWidget * widget = new QWidget(this)
QtSvgButton * button = new QtSvgButton(widget);
butten->setSkin("Beryl");
widget->addWidget(button);
widget->show();
\endcode
It is important to call the function \a setSkin() to load a skin. If \a setSkin() is not called
the Button will not have any visible content.
The parameter \p skin has the skin name. The skins are in the resource file defined in the project file.
The name scheme in the resource file is "Skin Name"/"unique picture name.svg".
The \a setSkin() function loads the diffrent skins over the "Skin name".
The needed realised graphics for this control are "normal.svg", "hovered.svg" and "pressed.svg".
If you want to add new skins, they have to follow the name scheme to work.
The actual skin can be read over the \a skin() function.
\sa skin(), setSkin()
No Signals are defined.
No Slots are defined.
*/
/*!
Constructor of the QtSvgButton.
The \a parent object holds the Parent of the Widget.
*/
QtSvgButton::QtSvgButton(QWidget * parent)
: QAbstractButton(parent)
{
// trigger repaint on enter and leave event
setAttribute(Qt::WA_Hover, true);
}
/*!
Destructor
*/
QtSvgButton::~QtSvgButton()
{
}
/*!
With this function you can set the skin that will be displayed in the widget.
\code
QtSvgButton * button = new QtSvgButton(this);
button->setSkin("Beryl");
\endcode
This function has to be called before using the QSvgButton.
\sa skin()
*/
void QtSvgButton::setSkin(const QString& skin)
{
m_skin = skin;
//QString base = ":/svgbutton/" + skin + '/';
QString base = "./" + skin + '/';
// create null size and null pictures
m_defaultSize = QSize();
m_normal = QPicture();
m_hovered = QPicture();
m_pressed = QPicture();
QSvgRenderer renderer;
QPainter painter;
if (renderer.load(base + "normal.svg")) {
m_defaultSize = renderer.defaultSize();
painter.begin(&m_normal);
renderer.render(&painter, QRectF(0.0, 0.0, 1.0, 1.0));
painter.end();
}
if (renderer.load(base + "pressed.svg")) {
painter.begin(&m_pressed);
renderer.render(&painter, QRectF(0.0, 0.0, 1.0, 1.0));
painter.end();
}
if (renderer.load(base + "hovered.svg")) {
painter.begin(&m_hovered);
renderer.render(&painter, QRectF(0.0, 0.0, 1.0, 1.0));
painter.end();
}
// update geometry for new sizeHint and repaint
updateGeometry();
update();
}
/*!
This function returns the actual skin name. If no skin has been set the return value is "".
\code
QtSvgButton * button = new QtSvgButton(this);
button->setSkin("Beryl");
qDebug() << button->skin();
\endcode
\sa setSkin()
*/
QString QtSvgButton::skin() const
{
return m_skin;
}
/*!
This function calculates the possibe Button size. This is needed to keep the aspect ratio even
if the space for the widget is bigger.
*/
QRect QtSvgButton::buttonRect() const
{
// Keep aspect ratio:
QSize buttonSize = m_defaultSize;
buttonSize.scale(rect().size(), Qt::KeepAspectRatio);
// Keep the button aligned horizontally centered (like the text):
int buttonPosX = (rect().width() - buttonSize.width()) / 2;
if (buttonPosX < 0) buttonPosX = 0;
buttonPosX += rect().left();
QPoint buttonPos = QPoint(buttonPosX, 0);
return QRect(buttonPos, buttonSize);
}
/*!
\overload
Overloaded paint event. In this function is the set skin painted.
\sa setSkin(), skin()
*/
void QtSvgButton::paintEvent(QPaintEvent * event)
{
Q_UNUSED(event);
QPainter painter(this);
painter.scale(buttonRect().width(), buttonRect().height());
if (isDown()) {
m_pressed.play(&painter);
} else if (underMouse()) {
m_hovered.play(&painter);
} else {
m_normal.play(&painter);
}
}
/*!
\overload
Return size hint provided by the SVG graphics.
Can be changed during runtime.
*/
QSize QtSvgButton::sizeHint() const
{
if (!m_defaultSize.isEmpty()) {
return m_defaultSize;
} else {
return QSize(100, 24);
}
}