libplasma
radiobutton.cpp
Go to the documentation of this file.00001 /* 00002 * Copyright 2008 Aaron Seigo <aseigo@kde.org> 00003 * 00004 * This program is free software; you can redistribute it and/or modify 00005 * it under the terms of the GNU Library General Public License as 00006 * published by the Free Software Foundation; either version 2, or 00007 * (at your option) any later version. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details 00013 * 00014 * You should have received a copy of the GNU Library General Public 00015 * License along with this program; if not, write to the 00016 * Free Software Foundation, Inc., 00017 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00018 */ 00019 00020 #include "radiobutton.h" 00021 00022 #include <QRadioButton> 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 RadioButtonPrivate 00035 { 00036 public: 00037 RadioButtonPrivate() 00038 : svg(0) 00039 { 00040 } 00041 00042 ~RadioButtonPrivate() 00043 { 00044 delete svg; 00045 } 00046 00047 void setPixmap(RadioButton *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<QRadioButton*>(q->widget())->setIcon(QIcon(pm)); 00065 } 00066 00067 QString imagePath; 00068 QString absImagePath; 00069 Svg *svg; 00070 }; 00071 00072 RadioButton::RadioButton(QGraphicsWidget *parent) 00073 : QGraphicsProxyWidget(parent), 00074 d(new RadioButtonPrivate) 00075 { 00076 QRadioButton* native = new QRadioButton; 00077 connect(native, SIGNAL(toggled(bool)), this, SIGNAL(toggled(bool))); 00078 setWidget(native); 00079 native->setAttribute(Qt::WA_NoSystemBackground); 00080 } 00081 00082 RadioButton::~RadioButton() 00083 { 00084 delete d; 00085 } 00086 00087 void RadioButton::setText(const QString &text) 00088 { 00089 static_cast<QRadioButton*>(widget())->setText(text); 00090 } 00091 00092 QString RadioButton::text() const 00093 { 00094 return static_cast<QRadioButton*>(widget())->text(); 00095 } 00096 00097 void RadioButton::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 //TODO: package support 00119 d->absImagePath = Theme::defaultTheme()->imagePath(path); 00120 } 00121 00122 d->setPixmap(this); 00123 } 00124 00125 QString RadioButton::image() const 00126 { 00127 return d->imagePath; 00128 } 00129 00130 void RadioButton::setStyleSheet(const QString &stylesheet) 00131 { 00132 widget()->setStyleSheet(stylesheet); 00133 } 00134 00135 QString RadioButton::styleSheet() 00136 { 00137 return widget()->styleSheet(); 00138 } 00139 00140 QRadioButton* RadioButton::nativeWidget() const 00141 { 00142 return static_cast<QRadioButton*>(widget()); 00143 } 00144 00145 void RadioButton::resizeEvent(QGraphicsSceneResizeEvent *event) 00146 { 00147 d->setPixmap(this); 00148 QGraphicsProxyWidget::resizeEvent(event); 00149 } 00150 00151 void RadioButton::setChecked(bool checked) 00152 { 00153 static_cast<QRadioButton*>(widget())->setChecked(checked); 00154 } 00155 00156 bool RadioButton::isChecked() const 00157 { 00158 return static_cast<QRadioButton*>(widget())->isChecked(); 00159 } 00160 00161 } // namespace Plasma 00162 00163 #include <radiobutton.moc> 00164