• Skip to content
  • Skip to link menu
KDE 4.3 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

Plasma

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.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         //TODO: package support
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 } // namespace Plasma
00174 
00175 #include <radiobutton.moc>
00176 

Plasma

Skip menu "Plasma"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.6.1
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal