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

libplasma

dialog.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2007 by Alexis Ménard <darktears31@gmail.com>           *
00003  *   Copyright (C) 2007 Sebastian Kuegler <sebas@kde.org>                  *
00004  *   Copyright (C) 2006 Aaron Seigo <aseigo@kde.org>                       *
00005  *                                                                         *
00006  *   This program is free software; you can redistribute it and/or modify  *
00007  *   it under the terms of the GNU General Public License as published by  *
00008  *   the Free Software Foundation; either version 2 of the License, or     *
00009  *   (at your option) any later version.                                   *
00010  *                                                                         *
00011  *   This program is distributed in the hope that it will be useful,       *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU General Public License for more details.                          *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU General Public License     *
00017  *   along with this program; if not, write to the                         *
00018  *   Free Software Foundation, Inc.,                                       *
00019  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA .        *
00020  ***************************************************************************/
00021 
00022 #include "dialog.h"
00023 
00024 #include <QPainter>
00025 #include <QSvgRenderer>
00026 #include <QResizeEvent>
00027 #include <QMouseEvent>
00028 #ifdef Q_WS_X11
00029 #include <QX11Info>
00030 #endif
00031 #include <QGraphicsView>
00032 #include <QtGui/QGraphicsSceneEvent>
00033 
00034 #include <KDebug>
00035 #include <NETRootInfo>
00036 
00037 #include <plasma/svg.h>
00038 
00039 #ifdef Q_WS_X11
00040 #include <X11/Xlib.h>
00041 #endif
00042 
00043 
00044 namespace Plasma
00045 {
00046 
00047 
00048 Dialog::Dialog( QWidget * parent, Qt::WindowFlags f )
00049     : QWidget(parent, f),
00050       m_cachedBackground(0)
00051 {
00052     m_background = new Plasma::Svg("dialogs/background", this);
00053 
00054     const int topHeight = m_background->elementSize("top").height();
00055     const int leftWidth = m_background->elementSize("left").width();
00056     const int rightWidth = m_background->elementSize("right").width();
00057     const int bottomHeight = m_background->elementSize("bottom").height();
00058     setContentsMargins(leftWidth, topHeight, rightWidth, bottomHeight);
00059 
00060     connect(m_background, SIGNAL(repaintNeeded()), this, SLOT(update()));
00061 }
00062 
00063 Dialog::~Dialog()
00064 {
00065 }
00066 
00067 void Dialog::paintEvent(QPaintEvent *e)
00068 {
00069     QPainter p(this);
00070     p.setRenderHint(QPainter::Antialiasing);
00071     p.setClipRect(e->rect());
00072     p.setCompositionMode(QPainter::CompositionMode_Source );
00073     p.fillRect(rect(), Qt::transparent);
00074     paintBackground(&p, e->rect());
00075 }
00076 
00077 void Dialog::paintBackground(QPainter* painter, const QRect &exposedRect)
00078 {
00079     if (!m_cachedBackground || m_cachedBackground->size() != rect().size()) {
00080         const int contentWidth = rect().width();
00081         const int contentHeight = rect().height();
00082         
00083         m_background->resize();
00084         
00085         const int topHeight = m_background->elementSize("top").height();
00086         const int topWidth = m_background->elementSize("top").width();
00087         const int leftWidth = m_background->elementSize("left").width();
00088         const int leftHeight = m_background->elementSize("left").height();
00089         const int rightHeight = m_background->elementSize("right").height();
00090         const int rightWidth = m_background->elementSize("right").width();
00091         const int bottomHeight = m_background->elementSize("bottom").height();
00092         const int bottomWidth = m_background->elementSize("bottom").width();
00093 
00094         const int topOffset = 0;
00095         const int leftOffset = 0;
00096         const int rightOffset = contentWidth - rightWidth;
00097         const int bottomOffset = contentHeight - bottomHeight;
00098         const int contentTop = topHeight;
00099         const int contentLeft = leftWidth;
00100         
00101         delete m_cachedBackground;
00102         m_cachedBackground = new QPixmap(contentWidth, contentHeight);
00103         m_cachedBackground->fill(Qt::transparent);
00104         QPainter p(m_cachedBackground);
00105         p.setCompositionMode(QPainter::CompositionMode_Source);
00106         p.setRenderHint(QPainter::SmoothPixmapTransform);
00107         
00108         //FIXME: This is a hack to fix a drawing problems with svg files where a thin transparent border is drawn around the svg image.
00109         //       the transparent border around the svg seems to vary in size depending on the size of the svg and as a result increasing the
00110         //       svn image by 2 all around didn't resolve the issue. For now it resizes based on the border size.
00111         
00112         m_background->resize(contentWidth, contentHeight);
00113         m_background->paint(&p, QRect(contentLeft, contentTop, contentWidth, contentHeight), "center");
00114         m_background->resize();
00115 
00116         m_background->paint(&p, QRect(leftOffset, topOffset, leftWidth, topHeight), "topleft");
00117         m_background->paint(&p, QRect(rightOffset, topOffset, rightWidth, topHeight), "topright");
00118         m_background->paint(&p, QRect(leftOffset, bottomOffset, leftWidth, bottomHeight), "bottomleft");
00119         m_background->paint(&p, QRect(rightOffset, bottomOffset, rightWidth, bottomHeight), "bottomright");
00120         
00121         if (m_background->elementExists("hint-stretch-borders")) {
00122             m_background->paint(&p, QRect(leftOffset, contentTop, leftWidth, contentHeight), "left");
00123             m_background->paint(&p, QRect(rightOffset, contentTop, rightWidth, contentHeight), "right");
00124             m_background->paint(&p, QRect(contentLeft, topOffset, contentWidth, topHeight), "top");
00125             m_background->paint(&p, QRect(contentLeft, bottomOffset, contentWidth, bottomHeight), "bottom");
00126         } else {
00127             QPixmap left(leftWidth, leftHeight);
00128             left.fill(Qt::transparent);
00129             {
00130                 QPainter sidePainter(&left);
00131                 sidePainter.setCompositionMode(QPainter::CompositionMode_Source);
00132                 m_background->paint(&sidePainter, QPoint(0, 0), "left");
00133             }
00134             p.drawTiledPixmap(QRect(leftOffset, contentTop, leftWidth, contentHeight - topHeight - bottomHeight), left);
00135             
00136             QPixmap right(rightWidth, rightHeight);
00137             right.fill(Qt::transparent);
00138             {
00139                 QPainter sidePainter(&right);
00140                 sidePainter.setCompositionMode(QPainter::CompositionMode_Source);
00141                 m_background->paint(&sidePainter, QPoint(0, 0), "right");
00142             }
00143             p.drawTiledPixmap(QRect(rightOffset, contentTop, rightWidth, contentHeight - topHeight - bottomHeight), right);
00144             
00145             QPixmap top(topWidth, topHeight);
00146             top.fill(Qt::transparent);
00147             {
00148                 QPainter sidePainter(&top);
00149                 sidePainter.setCompositionMode(QPainter::CompositionMode_Source);
00150                 m_background->paint(&sidePainter, QPoint(0, 0), "top");
00151             }
00152             p.drawTiledPixmap(QRect(contentLeft, topOffset, contentWidth - rightWidth - leftWidth, topHeight), top);
00153             
00154             QPixmap bottom(bottomWidth, bottomHeight);
00155             bottom.fill(Qt::transparent);
00156             {
00157                 QPainter sidePainter(&bottom);
00158                 sidePainter.setCompositionMode(QPainter::CompositionMode_Source);
00159                 m_background->paint(&sidePainter, QPoint(0, 0), "bottom");
00160             }
00161             p.drawTiledPixmap(QRect(contentLeft, bottomOffset, contentWidth - rightWidth - leftWidth, bottomHeight), bottom);
00162         }
00163         
00164         // re-enable this once Qt's svg rendering is un-buggered
00165         //background->resize(contentWidth, contentHeight);
00166         //background->paint(&p, QRect(contentLeft, contentTop, contentWidth, contentHeight), "center");
00167     }
00168     
00169     painter->drawPixmap(exposedRect, *m_cachedBackground, exposedRect);
00170 }
00171 
00172 void Dialog::position(QGraphicsSceneEvent *event, const QRectF boundingRect, QPointF scenePos)
00173 {
00174     QWidget *viewWidget = event->widget() ? event->widget()->parentWidget() : 0;
00175     //QPointF scenePos = mapToScene(boundingRect.topLeft());
00176     QGraphicsView *view = qobject_cast<QGraphicsView*>(viewWidget);
00177     position(view,boundingRect,scenePos);
00178 }
00179 
00180 void Dialog::position(QGraphicsView * view,const QRectF boundingRect,QPointF scenePos)
00181 {
00182     if (view) {
00183     QPoint viewPos = view->mapFromScene(scenePos);
00184     QPoint globalPos = view->mapToGlobal(viewPos);
00185     if ((globalPos.ry()-height())< 0) {
00186     scenePos = QPointF(scenePos.x() + boundingRect.width(), scenePos.y() + boundingRect.height());
00187     viewPos = view->mapFromScene(scenePos);
00188     globalPos = view->mapToGlobal(viewPos)+QPoint(0,10);
00189     }
00190     else {
00191         globalPos.ry() -= (height()+10);
00192     }
00193     if ((globalPos.rx() + width()) > view->width()) {
00194         globalPos.rx()-=((globalPos.rx() + width())-view->width());
00195     }
00196     move(globalPos);
00197     kDebug() << globalPos;
00198     }
00199 }
00200 
00201 }
00202 #include "dialog.moc"

libplasma

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

API Reference

Skip menu "API Reference"
  • KWin
  •   KWin Libraries
  • Libraries
  •   libkworkspace
  •   libplasma
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.4
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