libplasma
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>
00027
00028 #include "theme.h"
00029 #include "svg.h"
00030
00031 namespace Plasma
00032 {
00033
00034 class CheckBoxPrivate
00035 {
00036 public:
00037 CheckBoxPrivate()
00038 : svg(0)
00039 {
00040 }
00041
00042 ~CheckBoxPrivate()
00043 {
00044 delete svg;
00045 }
00046
00047 void setPixmap(CheckBox *q)
00048 {
00049 if (imagePath.isEmpty()) {
00050 return;
00051 }
00052
00053 KMimeType::Ptr mime = KMimeType::findByPath(absImagePath);
00054 QPixmap pm(q->size().toSize());
00055
00056 if (mime->is("image/svg+xml")) {
00057 svg = new Svg();
00058 QPainter p(&pm);
00059 svg->paint(&p, pm.rect());
00060 } else {
00061 pm = QPixmap(absImagePath);
00062 }
00063
00064 static_cast<QCheckBox*>(q->widget())->setIcon(QIcon(pm));
00065 }
00066
00067 QString imagePath;
00068 QString absImagePath;
00069 Svg *svg;
00070 };
00071
00072 CheckBox::CheckBox(QGraphicsWidget *parent)
00073 : QGraphicsProxyWidget(parent),
00074 d(new CheckBoxPrivate)
00075 {
00076 QCheckBox* native = new QCheckBox;
00077 connect(native, SIGNAL(toggled(bool)), this, SIGNAL(toggled(bool)));
00078 setWidget(native);
00079 native->setAttribute(Qt::WA_NoSystemBackground);
00080 }
00081
00082 CheckBox::~CheckBox()
00083 {
00084 delete d;
00085 }
00086
00087 void CheckBox::setText(const QString &text)
00088 {
00089 static_cast<QCheckBox*>(widget())->setText(text);
00090 }
00091
00092 QString CheckBox::text() const
00093 {
00094 return static_cast<QCheckBox*>(widget())->text();
00095 }
00096
00097 void CheckBox::setImage(const QString &path)
00098 {
00099 if (d->imagePath == path) {
00100 return;
00101 }
00102
00103 delete d->svg;
00104 d->svg = 0;
00105 d->imagePath = path;
00106
00107 bool absolutePath = !path.isEmpty() &&
00108 #ifdef Q_WS_WIN
00109 !QDir::isRelativePath(path)
00110 #else
00111 (path[0] == '/' || path.startsWith(":/"))
00112 #endif
00113 ;
00114
00115 if (absolutePath) {
00116 d->absImagePath = path;
00117 } else {
00118
00119 d->absImagePath = Theme::defaultTheme()->imagePath(path);
00120 }
00121
00122 d->setPixmap(this);
00123 }
00124
00125 QString CheckBox::image() const
00126 {
00127 return d->imagePath;
00128 }
00129
00130 void CheckBox::setStyleSheet(const QString &stylesheet)
00131 {
00132 widget()->setStyleSheet(stylesheet);
00133 }
00134
00135 QString CheckBox::styleSheet()
00136 {
00137 return widget()->styleSheet();
00138 }
00139
00140 QCheckBox* CheckBox::nativeWidget() const
00141 {
00142 return static_cast<QCheckBox*>(widget());
00143 }
00144
00145 void CheckBox::resizeEvent(QGraphicsSceneResizeEvent *event)
00146 {
00147 d->setPixmap(this);
00148 QGraphicsProxyWidget::resizeEvent(event);
00149 }
00150
00151 void CheckBox::setChecked(bool checked)
00152 {
00153 static_cast<QCheckBox*>(widget())->setChecked(checked);
00154 }
00155
00156 bool CheckBox::isChecked() const
00157 {
00158 return static_cast<QCheckBox*>(widget())->isChecked();
00159 }
00160
00161 }
00162
00163 #include <checkbox.moc>
00164