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 <QGraphicsSceneResizeEvent> 00024 #include <QWidget> 00025 #include <QDir> 00026 #include <QPainter> 00027 00028 //KDE 00029 #include <kmimetype.h> 00030 00031 //Plasma 00032 #include "framesvg.h" 00033 #include "private/themedwidgetinterface_p.h" 00034 #include "theme.h" 00035 00036 namespace Plasma 00037 { 00038 00039 class FramePrivate : public ThemedWidgetInterface<Frame> 00040 { 00041 public: 00042 FramePrivate(Frame *parent) 00043 : ThemedWidgetInterface<Frame>(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 FrameSvg *svg; 00058 Frame::Shadow shadow; 00059 QString text; 00060 QString styleSheet; 00061 QString imagePath; 00062 QString absImagePath; 00063 Svg *image; 00064 QPixmap *pixmap; 00065 }; 00066 00067 void FramePrivate::syncBorders() 00068 { 00069 //set margins from the normal element 00070 qreal left, top, right, bottom; 00071 00072 svg->getMargins(left, top, right, bottom); 00073 00074 if (!text.isNull()) { 00075 QFontMetricsF fm(q->font()); 00076 top += fm.height(); 00077 } 00078 00079 q->setContentsMargins(left, top, right, bottom); 00080 } 00081 00082 Frame::Frame(QGraphicsWidget *parent) 00083 : QGraphicsWidget(parent), 00084 d(new FramePrivate(this)) 00085 { 00086 d->svg = new Plasma::FrameSvg(this); 00087 d->svg->setImagePath("widgets/frame"); 00088 d->svg->setElementPrefix("plain"); 00089 d->syncBorders(); 00090 00091 connect(d->svg, SIGNAL(repaintNeeded()), SLOT(syncBorders())); 00092 d->initTheming(); 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::setEnabledBorders(const FrameSvg::EnabledBorders borders) 00126 { 00127 if (borders != d->svg->enabledBorders()) { 00128 d->svg->setEnabledBorders(borders); 00129 d->syncBorders(); 00130 update(); 00131 } 00132 } 00133 00134 FrameSvg::EnabledBorders Frame::enabledBorders() const 00135 { 00136 return d->svg->enabledBorders(); 00137 } 00138 00139 void Frame::setText(QString text) 00140 { 00141 d->text = text; 00142 d->syncBorders(); 00143 updateGeometry(); 00144 update(); 00145 } 00146 00147 QString Frame::text() const 00148 { 00149 return d->text; 00150 } 00151 00152 void Frame::setImage(const QString &path) 00153 { 00154 if (d->imagePath == path) { 00155 return; 00156 } 00157 00158 delete d->image; 00159 d->image = 0; 00160 d->imagePath = path; 00161 delete d->pixmap; 00162 d->pixmap = 0; 00163 00164 bool absolutePath = !path.isEmpty() && 00165 #ifdef Q_WS_WIN 00166 !QDir::isRelativePath(path) 00167 #else 00168 (path[0] == '/' || path.startsWith(QLatin1String(":/"))) 00169 #endif 00170 ; 00171 00172 if (absolutePath) { 00173 d->absImagePath = path; 00174 } else { 00175 //TODO: package support 00176 d->absImagePath = Theme::defaultTheme()->imagePath(path); 00177 } 00178 00179 if (path.isEmpty()) { 00180 return; 00181 } 00182 00183 KMimeType::Ptr mime = KMimeType::findByPath(d->absImagePath); 00184 00185 if (!mime->is("image/svg+xml") && !mime->is("application/x-gzip")) { 00186 d->pixmap = new QPixmap(d->absImagePath); 00187 } else { 00188 d->image = new Plasma::Svg(this); 00189 d->image->setImagePath(path); 00190 } 00191 } 00192 00193 QString Frame::image() const 00194 { 00195 return d->imagePath; 00196 } 00197 00198 void Frame::setStyleSheet(const QString &styleSheet) 00199 { 00200 //TODO: implement stylesheets painting 00201 d->styleSheet = styleSheet; 00202 } 00203 00204 QString Frame::styleSheet() const 00205 { 00206 return d->styleSheet; 00207 } 00208 00209 QWidget *Frame::nativeWidget() const 00210 { 00211 return 0; 00212 } 00213 00214 void Frame::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) 00215 { 00216 Q_UNUSED(option) 00217 Q_UNUSED(widget) 00218 00219 d->svg->paintFrame(painter); 00220 00221 if (!d->text.isNull()) { 00222 QFontMetricsF fm(font()); 00223 QRectF textRect = d->svg->contentsRect(); 00224 textRect.setHeight(fm.height()); 00225 painter->setFont(font()); 00226 painter->setPen(Plasma::Theme::defaultTheme()->color(Theme::TextColor)); 00227 painter->drawText(textRect, Qt::AlignHCenter|Qt::AlignTop, d->text); 00228 } 00229 00230 if (!d->imagePath.isNull()) { 00231 if (d->pixmap && !d->pixmap->isNull()) { 00232 painter->drawPixmap(contentsRect(), *d->pixmap, d->pixmap->rect()); 00233 } else if (d->image) { 00234 d->image->paint(painter, contentsRect()); 00235 } 00236 } 00237 } 00238 00239 void Frame::resizeEvent(QGraphicsSceneResizeEvent *event) 00240 { 00241 d->svg->resizeFrame(event->newSize()); 00242 00243 if (d->image) { 00244 d->image->resize(contentsRect().size()); 00245 } 00246 00247 QGraphicsWidget::resizeEvent(event); 00248 } 00249 00250 QSizeF Frame::sizeHint(Qt::SizeHint which, const QSizeF & constraint) const 00251 { 00252 QSizeF hint = QGraphicsWidget::sizeHint(which, constraint); 00253 00254 if (!d->image && !layout()) { 00255 QFontMetricsF fm(font()); 00256 00257 qreal left, top, right, bottom; 00258 d->svg->getMargins(left, top, right, bottom); 00259 00260 hint.setHeight(fm.height() + top + bottom); 00261 if (which == Qt::MinimumSize || which == Qt::PreferredSize) { 00262 QRectF rect = fm.boundingRect(d->text); 00263 hint.setWidth(rect.width() + left + right); 00264 } 00265 } 00266 00267 return hint; 00268 } 00269 00270 void Frame::changeEvent(QEvent *event) 00271 { 00272 d->changeEvent(event); 00273 QGraphicsWidget::changeEvent(event); 00274 } 00275 00276 } // namespace Plasma 00277 00278 #include <frame.moc> 00279
KDE 4.6 API Reference