• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

Plasma

  • plasma
  • widgets
checkbox.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2008 Aaron Seigo <aseigo@kde.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Library General Public License as
6 * published by the Free Software Foundation; either version 2, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19
20#include "checkbox.h"
21
22#include <QCheckBox>
23#include <QPainter>
24#include <QDir>
25
26#include <kmimetype.h>
27
28#include "private/themedwidgetinterface_p.h"
29#include "svg.h"
30#include "theme.h"
31
32namespace Plasma
33{
34
35class CheckBoxPrivate : public ThemedWidgetInterface<CheckBox>
36{
37public:
38 CheckBoxPrivate(CheckBox *c)
39 : ThemedWidgetInterface<CheckBox>(c),
40 svg(0)
41 {
42 }
43
44 ~CheckBoxPrivate()
45 {
46 delete svg;
47 }
48
49 void setPixmap()
50 {
51 if (imagePath.isEmpty()) {
52 delete svg;
53 svg = 0;
54 return;
55 }
56
57 KMimeType::Ptr mime = KMimeType::findByPath(absImagePath);
58 QPixmap pm(q->size().toSize());
59
60 if (mime->is("image/svg+xml") || mime->is("image/svg+xml-compressed")) {
61 if (!svg || svg->imagePath() != imagePath) {
62 delete svg;
63 svg = new Svg();
64 svg->setImagePath(imagePath);
65 QObject::connect(svg, SIGNAL(repaintNeeded()), q, SLOT(setPixmap()));
66 }
67
68 QPainter p(&pm);
69 svg->paint(&p, pm.rect());
70 } else {
71 delete svg;
72 svg = 0;
73 pm = QPixmap(absImagePath);
74 }
75
76 static_cast<QCheckBox*>(q->widget())->setIcon(QIcon(pm));
77 }
78
79 QString imagePath;
80 QString absImagePath;
81 Svg *svg;
82};
83
84CheckBox::CheckBox(QGraphicsWidget *parent)
85 : QGraphicsProxyWidget(parent),
86 d(new CheckBoxPrivate(this))
87{
88 QCheckBox *native = new QCheckBox;
89 connect(native, SIGNAL(toggled(bool)), this, SIGNAL(toggled(bool)));
90 d->setWidget(native);
91 native->setWindowIcon(QIcon());
92 native->setAttribute(Qt::WA_NoSystemBackground);
93
94 d->initTheming();
95}
96
97CheckBox::~CheckBox()
98{
99 delete d;
100}
101
102void CheckBox::setText(const QString &text)
103{
104 static_cast<QCheckBox*>(widget())->setText(text);
105}
106
107QString CheckBox::text() const
108{
109 return static_cast<QCheckBox*>(widget())->text();
110}
111
112void CheckBox::setImage(const QString &path)
113{
114 if (d->imagePath == path) {
115 return;
116 }
117
118 delete d->svg;
119 d->svg = 0;
120 d->imagePath = path;
121
122 bool absolutePath = !path.isEmpty() &&
123 #ifdef Q_WS_WIN
124 !QDir::isRelativePath(path)
125 #else
126 (path[0] == '/' || path.startsWith(QLatin1String(":/")))
127 #endif
128 ;
129
130 if (absolutePath) {
131 d->absImagePath = path;
132 } else {
133 //TODO: package support
134 d->absImagePath = Theme::defaultTheme()->imagePath(path);
135 }
136
137 d->setPixmap();
138}
139
140QString CheckBox::image() const
141{
142 return d->imagePath;
143}
144
145void CheckBox::setStyleSheet(const QString &stylesheet)
146{
147 widget()->setStyleSheet(stylesheet);
148}
149
150QString CheckBox::styleSheet()
151{
152 return widget()->styleSheet();
153}
154
155QCheckBox *CheckBox::nativeWidget() const
156{
157 return static_cast<QCheckBox*>(widget());
158}
159
160void CheckBox::resizeEvent(QGraphicsSceneResizeEvent *event)
161{
162 d->setPixmap();
163 QGraphicsProxyWidget::resizeEvent(event);
164}
165
166void CheckBox::setChecked(bool checked)
167{
168 static_cast<QCheckBox*>(widget())->setChecked(checked);
169}
170
171bool CheckBox::isChecked() const
172{
173 return static_cast<QCheckBox*>(widget())->isChecked();
174}
175
176void CheckBox::changeEvent(QEvent *event)
177{
178 d->changeEvent(event);
179 QGraphicsProxyWidget::changeEvent(event);
180}
181
182} // namespace Plasma
183
184#include <checkbox.moc>
185
checkbox.h
Plasma::CheckBox::checked
bool checked
Definition: checkbox.h:48
Plasma::CheckBox::setText
void setText(const QString &text)
Sets the display text for this CheckBox.
Definition: checkbox.cpp:102
Plasma::CheckBox::text
QString text
Definition: checkbox.h:44
Plasma::CheckBox::setChecked
void setChecked(bool checked)
Sets the checked state.
Definition: checkbox.cpp:166
Plasma::CheckBox::isChecked
bool isChecked() const
Definition: checkbox.cpp:171
Plasma::CheckBox::toggled
void toggled(bool)
Plasma::Theme::imagePath
Q_INVOKABLE QString imagePath(const QString &name) const
Retrieve the path for an SVG image in the current theme.
Definition: theme.cpp:794
Plasma::Theme::defaultTheme
static Theme * defaultTheme()
Singleton pattern accessor.
Definition: theme.cpp:544
QGraphicsProxyWidget
QGraphicsWidget
Plasma
Namespace for everything in libplasma.
Definition: abstractdialogmanager.cpp:25
svg.h
theme.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Plasma

Skip menu "Plasma"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal