Plasma
radiobutton.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 "radiobutton.h"
00021
00022 #include <QRadioButton>
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 RadioButtonPrivate
00035 {
00036 public:
00037 RadioButtonPrivate(RadioButton *w)
00038 : q(w),
00039 svg(0)
00040 {
00041 }
00042
00043 ~RadioButtonPrivate()
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<QRadioButton*>(q->widget())->setIcon(QIcon(pm));
00076 }
00077
00078 RadioButton *q;
00079 QString imagePath;
00080 QString absImagePath;
00081 Svg *svg;
00082 };
00083
00084 RadioButton::RadioButton(QGraphicsWidget *parent)
00085 : QGraphicsProxyWidget(parent),
00086 d(new RadioButtonPrivate(this))
00087 {
00088 QRadioButton *native = new QRadioButton;
00089 connect(native, SIGNAL(toggled(bool)), this, SIGNAL(toggled(bool)));
00090 setWidget(native);
00091 native->setAttribute(Qt::WA_NoSystemBackground);
00092 }
00093
00094 RadioButton::~RadioButton()
00095 {
00096 delete d;
00097 }
00098
00099 void RadioButton::setText(const QString &text)
00100 {
00101 static_cast<QRadioButton*>(widget())->setText(text);
00102 }
00103
00104 QString RadioButton::text() const
00105 {
00106 return static_cast<QRadioButton*>(widget())->text();
00107 }
00108
00109 void RadioButton::setImage(const QString &path)
00110 {
00111 if (d->imagePath == path) {
00112 return;
00113 }
00114
00115 delete d->svg;
00116 d->svg = 0;
00117 d->imagePath = path;
00118
00119 bool absolutePath = !path.isEmpty() &&
00120 #ifdef Q_WS_WIN
00121 !QDir::isRelativePath(path)
00122 #else
00123 (path[0] == '/' || path.startsWith(":/"))
00124 #endif
00125 ;
00126
00127 if (absolutePath) {
00128 d->absImagePath = path;
00129 } else {
00130
00131 d->absImagePath = Theme::defaultTheme()->imagePath(path);
00132 }
00133
00134 d->setPixmap();
00135 }
00136
00137 QString RadioButton::image() const
00138 {
00139 return d->imagePath;
00140 }
00141
00142 void RadioButton::setStyleSheet(const QString &stylesheet)
00143 {
00144 widget()->setStyleSheet(stylesheet);
00145 }
00146
00147 QString RadioButton::styleSheet()
00148 {
00149 return widget()->styleSheet();
00150 }
00151
00152 QRadioButton *RadioButton::nativeWidget() const
00153 {
00154 return static_cast<QRadioButton*>(widget());
00155 }
00156
00157 void RadioButton::resizeEvent(QGraphicsSceneResizeEvent *event)
00158 {
00159 d->setPixmap();
00160 QGraphicsProxyWidget::resizeEvent(event);
00161 }
00162
00163 void RadioButton::setChecked(bool checked)
00164 {
00165 static_cast<QRadioButton*>(widget())->setChecked(checked);
00166 }
00167
00168 bool RadioButton::isChecked() const
00169 {
00170 return static_cast<QRadioButton*>(widget())->isChecked();
00171 }
00172
00173 }
00174
00175 #include <radiobutton.moc>
00176