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 return;
00052 }
00053
00054 KMimeType::Ptr mime = KMimeType::findByPath(absImagePath);
00055 QPixmap pm(q->size().toSize());
00056
00057 if (mime->is("image/svg+xml")) {
00058 svg = new Svg();
00059 QPainter p(&pm);
00060 svg->paint(&p, pm.rect());
00061 } else {
00062 pm = QPixmap(absImagePath);
00063 }
00064
00065 static_cast<QCheckBox*>(q->widget())->setIcon(QIcon(pm));
00066 }
00067
00068 void setPalette()
00069 {
00070 QCheckBox *native = q->nativeWidget();
00071 QColor color = Theme::defaultTheme()->color(Theme::TextColor);
00072 QPalette p = native->palette();
00073 p.setColor(QPalette::Normal, QPalette::WindowText, color);
00074 p.setColor(QPalette::Inactive, QPalette::WindowText, color);
00075 native->setPalette(p);
00076 }
00077
00078 CheckBox *q;
00079 QString imagePath;
00080 QString absImagePath;
00081 Svg *svg;
00082 };
00083
00084 CheckBox::CheckBox(QGraphicsWidget *parent)
00085 : QGraphicsProxyWidget(parent),
00086 d(new CheckBoxPrivate(this))
00087 {
00088 QCheckBox *native = new QCheckBox;
00089 connect(native, SIGNAL(toggled(bool)), this, SIGNAL(toggled(bool)));
00090 setWidget(native);
00091 d->setPalette();
00092 native->setAttribute(Qt::WA_NoSystemBackground);
00093 connect(Theme::defaultTheme(), SIGNAL(themeChanged()), this, SLOT(setPalette()));
00094 }
00095
00096 CheckBox::~CheckBox()
00097 {
00098 delete d;
00099 }
00100
00101 void CheckBox::setText(const QString &text)
00102 {
00103 static_cast<QCheckBox*>(widget())->setText(text);
00104 }
00105
00106 QString CheckBox::text() const
00107 {
00108 return static_cast<QCheckBox*>(widget())->text();
00109 }
00110
00111 void CheckBox::setImage(const QString &path)
00112 {
00113 if (d->imagePath == path) {
00114 return;
00115 }
00116
00117 delete d->svg;
00118 d->svg = 0;
00119 d->imagePath = path;
00120
00121 bool absolutePath = !path.isEmpty() &&
00122 #ifdef Q_WS_WIN
00123 !QDir::isRelativePath(path)
00124 #else
00125 (path[0] == '/' || path.startsWith(":/"))
00126 #endif
00127 ;
00128
00129 if (absolutePath) {
00130 d->absImagePath = path;
00131 } else {
00132
00133 d->absImagePath = Theme::defaultTheme()->imagePath(path);
00134 }
00135
00136 d->setPixmap();
00137 }
00138
00139 QString CheckBox::image() const
00140 {
00141 return d->imagePath;
00142 }
00143
00144 void CheckBox::setStyleSheet(const QString &stylesheet)
00145 {
00146 widget()->setStyleSheet(stylesheet);
00147 }
00148
00149 QString CheckBox::styleSheet()
00150 {
00151 return widget()->styleSheet();
00152 }
00153
00154 QCheckBox *CheckBox::nativeWidget() const
00155 {
00156 return static_cast<QCheckBox*>(widget());
00157 }
00158
00159 void CheckBox::resizeEvent(QGraphicsSceneResizeEvent *event)
00160 {
00161 d->setPixmap();
00162 QGraphicsProxyWidget::resizeEvent(event);
00163 }
00164
00165 void CheckBox::setChecked(bool checked)
00166 {
00167 static_cast<QCheckBox*>(widget())->setChecked(checked);
00168 }
00169
00170 bool CheckBox::isChecked() const
00171 {
00172 return static_cast<QCheckBox*>(widget())->isChecked();
00173 }
00174
00175 }
00176
00177 #include <checkbox.moc>
00178