Plasma
checkbox.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "checkbox.h"
00021
00022 #include <QCheckBox>
00023 #include <QPainter>
00024 #include <QDir>
00025
00026 #include <kmimetype.h>
00027
00028 #include "theme.h"
00029 #include "svg.h"
00030
00031 namespace Plasma
00032 {
00033
00034 class CheckBoxPrivate
00035 {
00036 public:
00037 CheckBoxPrivate(CheckBox *c)
00038 : q(c),
00039 svg(0)
00040 {
00041 }
00042
00043 ~CheckBoxPrivate()
00044 {
00045 delete svg;
00046 }
00047
00048 void setPixmap()
00049 {
00050 if (imagePath.isEmpty()) {
00051 delete svg;
00052 svg = 0;
00053 return;
00054 }
00055
00056 KMimeType::Ptr mime = KMimeType::findByPath(absImagePath);
00057 QPixmap pm(q->size().toSize());
00058
00059 if (mime->is("image/svg+xml") || mime->is("image/svg+xml-compressed")) {
00060 if (!svg || svg->imagePath() != imagePath) {
00061 delete svg;
00062 svg = new Svg();
00063 svg->setImagePath(imagePath);
00064 QObject::connect(svg, SIGNAL(repaintNeeded()), q, SLOT(setPixmap()));
00065 }
00066
00067 QPainter p(&pm);
00068 svg->paint(&p, pm.rect());
00069 } else {
00070 delete svg;
00071 svg = 0;
00072 pm = QPixmap(absImagePath);
00073 }
00074
00075 static_cast<QCheckBox*>(q->widget())->setIcon(QIcon(pm));
00076 }
00077
00078 void setPalette()
00079 {
00080 QCheckBox *native = q->nativeWidget();
00081 QColor color = Theme::defaultTheme()->color(Theme::TextColor);
00082 QPalette p = native->palette();
00083 p.setColor(QPalette::Normal, QPalette::WindowText, color);
00084 p.setColor(QPalette::Inactive, QPalette::WindowText, color);
00085 native->setPalette(p);
00086 }
00087
00088 CheckBox *q;
00089 QString imagePath;
00090 QString absImagePath;
00091 Svg *svg;
00092 };
00093
00094 CheckBox::CheckBox(QGraphicsWidget *parent)
00095 : QGraphicsProxyWidget(parent),
00096 d(new CheckBoxPrivate(this))
00097 {
00098 QCheckBox *native = new QCheckBox;
00099 connect(native, SIGNAL(toggled(bool)), this, SIGNAL(toggled(bool)));
00100 setWidget(native);
00101 d->setPalette();
00102 native->setAttribute(Qt::WA_NoSystemBackground);
00103 connect(Theme::defaultTheme(), SIGNAL(themeChanged()), this, SLOT(setPalette()));
00104 }
00105
00106 CheckBox::~CheckBox()
00107 {
00108 delete d;
00109 }
00110
00111 void CheckBox::setText(const QString &text)
00112 {
00113 static_cast<QCheckBox*>(widget())->setText(text);
00114 }
00115
00116 QString CheckBox::text() const
00117 {
00118 return static_cast<QCheckBox*>(widget())->text();
00119 }
00120
00121 void CheckBox::setImage(const QString &path)
00122 {
00123 if (d->imagePath == path) {
00124 return;
00125 }
00126
00127 delete d->svg;
00128 d->svg = 0;
00129 d->imagePath = path;
00130
00131 bool absolutePath = !path.isEmpty() &&
00132 #ifdef Q_WS_WIN
00133 !QDir::isRelativePath(path)
00134 #else
00135 (path[0] == '/' || path.startsWith(":/"))
00136 #endif
00137 ;
00138
00139 if (absolutePath) {
00140 d->absImagePath = path;
00141 } else {
00142
00143 d->absImagePath = Theme::defaultTheme()->imagePath(path);
00144 }
00145
00146 d->setPixmap();
00147 }
00148
00149 QString CheckBox::image() const
00150 {
00151 return d->imagePath;
00152 }
00153
00154 void CheckBox::setStyleSheet(const QString &stylesheet)
00155 {
00156 widget()->setStyleSheet(stylesheet);
00157 }
00158
00159 QString CheckBox::styleSheet()
00160 {
00161 return widget()->styleSheet();
00162 }
00163
00164 QCheckBox *CheckBox::nativeWidget() const
00165 {
00166 return static_cast<QCheckBox*>(widget());
00167 }
00168
00169 void CheckBox::resizeEvent(QGraphicsSceneResizeEvent *event)
00170 {
00171 d->setPixmap();
00172 QGraphicsProxyWidget::resizeEvent(event);
00173 }
00174
00175 void CheckBox::setChecked(bool checked)
00176 {
00177 static_cast<QCheckBox*>(widget())->setChecked(checked);
00178 }
00179
00180 bool CheckBox::isChecked() const
00181 {
00182 return static_cast<QCheckBox*>(widget())->isChecked();
00183 }
00184
00185 }
00186
00187 #include <checkbox.moc>
00188