Plasma
checkbox.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 "checkbox.h" 00021 00022 #include <QCheckBox> 00023 #include <QPainter> 00024 #include <QDir> 00025 00026 #include <kmimetype.h> 00027 00028 #include "private/themedwidgetinterface_p.h" 00029 #include "svg.h" 00030 #include "theme.h" 00031 00032 namespace Plasma 00033 { 00034 00035 class CheckBoxPrivate : public ThemedWidgetInterface<CheckBox> 00036 { 00037 public: 00038 CheckBoxPrivate(CheckBox *c) 00039 : ThemedWidgetInterface<CheckBox>(c), 00040 svg(0) 00041 { 00042 } 00043 00044 ~CheckBoxPrivate() 00045 { 00046 delete svg; 00047 } 00048 00049 void setPixmap() 00050 { 00051 if (imagePath.isEmpty()) { 00052 delete svg; 00053 svg = 0; 00054 return; 00055 } 00056 00057 KMimeType::Ptr mime = KMimeType::findByPath(absImagePath); 00058 QPixmap pm(q->size().toSize()); 00059 00060 if (mime->is("image/svg+xml") || mime->is("image/svg+xml-compressed")) { 00061 if (!svg || svg->imagePath() != imagePath) { 00062 delete svg; 00063 svg = new Svg(); 00064 svg->setImagePath(imagePath); 00065 QObject::connect(svg, SIGNAL(repaintNeeded()), q, SLOT(setPixmap())); 00066 } 00067 00068 QPainter p(&pm); 00069 svg->paint(&p, pm.rect()); 00070 } else { 00071 delete svg; 00072 svg = 0; 00073 pm = QPixmap(absImagePath); 00074 } 00075 00076 static_cast<QCheckBox*>(q->widget())->setIcon(QIcon(pm)); 00077 } 00078 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 native->setWindowIcon(QIcon()); 00092 native->setAttribute(Qt::WA_NoSystemBackground); 00093 00094 d->initTheming(); 00095 } 00096 00097 CheckBox::~CheckBox() 00098 { 00099 delete d; 00100 } 00101 00102 void CheckBox::setText(const QString &text) 00103 { 00104 static_cast<QCheckBox*>(widget())->setText(text); 00105 } 00106 00107 QString CheckBox::text() const 00108 { 00109 return static_cast<QCheckBox*>(widget())->text(); 00110 } 00111 00112 void CheckBox::setImage(const QString &path) 00113 { 00114 if (d->imagePath == path) { 00115 return; 00116 } 00117 00118 delete d->svg; 00119 d->svg = 0; 00120 d->imagePath = path; 00121 00122 bool absolutePath = !path.isEmpty() && 00123 #ifdef Q_WS_WIN 00124 !QDir::isRelativePath(path) 00125 #else 00126 (path[0] == '/' || path.startsWith(QLatin1String(":/"))) 00127 #endif 00128 ; 00129 00130 if (absolutePath) { 00131 d->absImagePath = path; 00132 } else { 00133 //TODO: package support 00134 d->absImagePath = Theme::defaultTheme()->imagePath(path); 00135 } 00136 00137 d->setPixmap(); 00138 } 00139 00140 QString CheckBox::image() const 00141 { 00142 return d->imagePath; 00143 } 00144 00145 void CheckBox::setStyleSheet(const QString &stylesheet) 00146 { 00147 widget()->setStyleSheet(stylesheet); 00148 } 00149 00150 QString CheckBox::styleSheet() 00151 { 00152 return widget()->styleSheet(); 00153 } 00154 00155 QCheckBox *CheckBox::nativeWidget() const 00156 { 00157 return static_cast<QCheckBox*>(widget()); 00158 } 00159 00160 void CheckBox::resizeEvent(QGraphicsSceneResizeEvent *event) 00161 { 00162 d->setPixmap(); 00163 QGraphicsProxyWidget::resizeEvent(event); 00164 } 00165 00166 void CheckBox::setChecked(bool checked) 00167 { 00168 static_cast<QCheckBox*>(widget())->setChecked(checked); 00169 } 00170 00171 bool CheckBox::isChecked() const 00172 { 00173 return static_cast<QCheckBox*>(widget())->isChecked(); 00174 } 00175 00176 void CheckBox::changeEvent(QEvent *event) 00177 { 00178 d->changeEvent(event); 00179 QGraphicsProxyWidget::changeEvent(event); 00180 } 00181 00182 } // namespace Plasma 00183 00184 #include <checkbox.moc> 00185
KDE 4.6 API Reference