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

Plasma

frame.cpp

Go to the documentation of this file.
00001 /*
00002  *   Copyright 2008 Marco Martin <notmart@gmail.com>
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 "frame.h"
00021 
00022 //Qt
00023 #include <QPainter>
00024 #include <QGraphicsSceneResizeEvent>
00025 #include <QWidget>
00026 #include <QDir>
00027 #include <QApplication>
00028 
00029 //KDE
00030 #include <kmimetype.h>
00031 
00032 //Plasma
00033 #include "plasma/theme.h"
00034 #include "plasma/framesvg.h"
00035 
00036 namespace Plasma
00037 {
00038 
00039 class FramePrivate
00040 {
00041 public:
00042     FramePrivate(Frame *parent)
00043         : q(parent),
00044           svg(0),
00045           image(0),
00046           pixmap(0)
00047     {
00048     }
00049 
00050     ~FramePrivate()
00051     {
00052         delete pixmap;
00053     }
00054 
00055     void syncBorders();
00056 
00057     Frame *q;
00058     FrameSvg *svg;
00059     Frame::Shadow shadow;
00060     QString text;
00061     QString styleSheet;
00062     QString imagePath;
00063     QString absImagePath;
00064     Svg *image;
00065     QPixmap *pixmap;
00066 };
00067 
00068 void FramePrivate::syncBorders()
00069 {
00070     //set margins from the normal element
00071     qreal left, top, right, bottom;
00072 
00073     svg->getMargins(left, top, right, bottom);
00074 
00075     if (!text.isNull()) {
00076         QFontMetricsF fm(QApplication::font());
00077         top += fm.height();
00078     }
00079 
00080     q->setContentsMargins(left, top, right, bottom);
00081 }
00082 
00083 Frame::Frame(QGraphicsWidget *parent)
00084     : QGraphicsWidget(parent),
00085       d(new FramePrivate(this))
00086 {
00087     d->svg = new Plasma::FrameSvg(this);
00088     d->svg->setImagePath("widgets/frame");
00089     d->svg->setElementPrefix("plain");
00090     d->syncBorders();
00091 
00092     connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()), SLOT(syncBorders()));
00093 }
00094 
00095 Frame::~Frame()
00096 {
00097     delete d;
00098 }
00099 
00100 void Frame::setFrameShadow(Shadow shadow)
00101 {
00102     d->shadow = shadow;
00103 
00104     switch (d->shadow) {
00105     case Raised:
00106         d->svg->setElementPrefix("raised");
00107         break;
00108     case Sunken:
00109         d->svg->setElementPrefix("sunken");
00110         break;
00111     case Plain:
00112     default:
00113         d->svg->setElementPrefix("plain");
00114         break;
00115     }
00116 
00117     d->syncBorders();
00118 }
00119 
00120 Frame::Shadow Frame::frameShadow() const
00121 {
00122     return d->shadow;
00123 }
00124 
00125 void Frame::setText(QString text)
00126 {
00127     d->text = text;
00128     d->syncBorders();
00129 }
00130 
00131 QString Frame::text() const
00132 {
00133     return d->text;
00134 }
00135 
00136 void Frame::setImage(const QString &path)
00137 {
00138     if (d->imagePath == path) {
00139         return;
00140     }
00141 
00142     delete d->image;
00143     d->image = 0;
00144     d->imagePath = path;
00145     delete d->pixmap;
00146     d->pixmap = 0;
00147 
00148     bool absolutePath = !path.isEmpty() &&
00149                         #ifdef Q_WS_WIN
00150                             !QDir::isRelativePath(path)
00151                         #else
00152                             (path[0] == '/' || path.startsWith(":/"))
00153                         #endif
00154         ;
00155 
00156     if (absolutePath) {
00157         d->absImagePath = path;
00158     } else {
00159         //TODO: package support
00160         d->absImagePath = Theme::defaultTheme()->imagePath(path);
00161     }
00162 
00163     if (path.isEmpty()) {
00164         return;
00165     }
00166 
00167     KMimeType::Ptr mime = KMimeType::findByPath(d->absImagePath);
00168 
00169     if (!mime->is("image/svg+xml") && !mime->is("application/x-gzip")) {
00170         d->pixmap = new QPixmap(d->absImagePath);
00171     } else {
00172         d->image = new Plasma::Svg(this);
00173         d->image->setImagePath(path);
00174     }
00175 }
00176 
00177 QString Frame::image() const
00178 {
00179     return d->imagePath;
00180 }
00181 
00182 void Frame::setStyleSheet(const QString &styleSheet)
00183 {
00184     //TODO: implement stylesheets painting
00185     d->styleSheet = styleSheet;
00186 }
00187 
00188 QString Frame::styleSheet() const
00189 {
00190     return d->styleSheet;
00191 }
00192 
00193 QWidget *Frame::nativeWidget() const
00194 {
00195     return 0;
00196 }
00197 
00198 void Frame::paint(QPainter *painter,
00199                    const QStyleOptionGraphicsItem *option,
00200                    QWidget *widget)
00201 {
00202     Q_UNUSED(option)
00203     Q_UNUSED(widget)
00204 
00205     d->svg->paintFrame(painter);
00206 
00207     if (!d->text.isNull()) {
00208         QFontMetricsF fm(QApplication::font());
00209         QRectF textRect = d->svg->contentsRect();
00210         textRect.setHeight(fm.height());
00211         painter->setPen(Plasma::Theme::defaultTheme()->color(Theme::TextColor));
00212         painter->drawText(textRect, Qt::AlignHCenter|Qt::AlignTop, d->text);
00213     }
00214 
00215     if (!d->imagePath.isNull()) {
00216         if (d->pixmap && !d->pixmap->isNull()) {
00217             painter->drawPixmap(contentsRect(), *d->pixmap, d->pixmap->rect());
00218         } else if (d->image) {
00219             d->image->paint(painter, contentsRect());
00220         }
00221     }
00222 }
00223 
00224 void Frame::resizeEvent(QGraphicsSceneResizeEvent *event)
00225 {
00226     d->svg->resizeFrame(event->newSize());
00227 
00228     if (d->image) {
00229         d->image->resize(contentsRect().size());
00230     }
00231 }
00232 
00233 QSizeF Frame::sizeHint(Qt::SizeHint which, const QSizeF & constraint) const
00234 {
00235     QSizeF hint = QGraphicsWidget::sizeHint(which, constraint);
00236 
00237     if (!d->image && !layout()) {
00238         QFontMetricsF fm(QApplication::font());
00239 
00240         qreal left, top, right, bottom;
00241         d->svg->getMargins(left, top, right, bottom);
00242 
00243         hint.setHeight(fm.height() + top + bottom);
00244     }
00245 
00246     return hint;
00247 }
00248 
00249 } // namespace Plasma
00250 
00251 #include <frame.moc>
00252 

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
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
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