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

Plasma

  • sources
  • kde-4.12
  • kdelibs
  • 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 
32 namespace Plasma
33 {
34 
35 class CheckBoxPrivate : public ThemedWidgetInterface<CheckBox>
36 {
37 public:
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 
84 CheckBox::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 
97 CheckBox::~CheckBox()
98 {
99  delete d;
100 }
101 
102 void CheckBox::setText(const QString &text)
103 {
104  static_cast<QCheckBox*>(widget())->setText(text);
105 }
106 
107 QString CheckBox::text() const
108 {
109  return static_cast<QCheckBox*>(widget())->text();
110 }
111 
112 void 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 
140 QString CheckBox::image() const
141 {
142  return d->imagePath;
143 }
144 
145 void CheckBox::setStyleSheet(const QString &stylesheet)
146 {
147  widget()->setStyleSheet(stylesheet);
148 }
149 
150 QString CheckBox::styleSheet()
151 {
152  return widget()->styleSheet();
153 }
154 
155 QCheckBox *CheckBox::nativeWidget() const
156 {
157  return static_cast<QCheckBox*>(widget());
158 }
159 
160 void CheckBox::resizeEvent(QGraphicsSceneResizeEvent *event)
161 {
162  d->setPixmap();
163  QGraphicsProxyWidget::resizeEvent(event);
164 }
165 
166 void CheckBox::setChecked(bool checked)
167 {
168  static_cast<QCheckBox*>(widget())->setChecked(checked);
169 }
170 
171 bool CheckBox::isChecked() const
172 {
173  return static_cast<QCheckBox*>(widget())->isChecked();
174 }
175 
176 void CheckBox::changeEvent(QEvent *event)
177 {
178  d->changeEvent(event);
179  QGraphicsProxyWidget::changeEvent(event);
180 }
181 
182 } // namespace Plasma
183 
184 #include <checkbox.moc>
185 
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::CheckBox::nativeWidget
QCheckBox * nativeWidget() const
Plasma::CheckBox::toggled
void toggled(bool)
theme.h
Plasma::CheckBox::setText
void setText(const QString &text)
Sets the display text for this CheckBox.
Definition: checkbox.cpp:102
checkbox.h
Plasma::CheckBox::text
QString text() const
Plasma::CheckBox::setImage
void setImage(const QString &path)
Sets the path to an image to display.
Definition: checkbox.cpp:112
Plasma::CheckBox::setStyleSheet
void setStyleSheet(const QString &stylesheet)
Sets the stylesheet used to control the visual display of this CheckBox.
Definition: checkbox.cpp:145
Plasma::CheckBox::image
QString image() const
QGraphicsProxyWidget
Plasma::CheckBox::resizeEvent
void resizeEvent(QGraphicsSceneResizeEvent *event)
Definition: checkbox.cpp:160
Plasma::CheckBox::isChecked
bool isChecked() const
Definition: checkbox.cpp:171
Plasma::Theme::defaultTheme
static Theme * defaultTheme()
Singleton pattern accessor.
Definition: theme.cpp:544
Plasma::CheckBox::changeEvent
void changeEvent(QEvent *event)
Definition: checkbox.cpp:176
Plasma::CheckBox::CheckBox
CheckBox(QGraphicsWidget *parent=0)
Definition: checkbox.cpp:84
Plasma::CheckBox::~CheckBox
~CheckBox()
Definition: checkbox.cpp:97
Plasma::CheckBox::styleSheet
QString styleSheet()
Plasma::CheckBox::setChecked
void setChecked(bool checked)
Sets the checked state.
Definition: checkbox.cpp:166
svg.h
QGraphicsWidget
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:48:33 by doxygen 1.8.7 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 API Reference

Skip menu "kdelibs 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
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



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