Add (and install) svg for the new krunner interface.
[kdebase/uwolfer.git] / workspace / libs / plasma / dialog.cpp
blob0bfb969da95142cb7e413295664365f8a1199e32
1 /***************************************************************************
2 * Copyright (C) 2007 by Alexis Ménard <darktears31@gmail.com> *
3 * Copyright (C) 2007 Sebastian Kuegler <sebas@kde.org> *
4 * Copyright (C) 2006 Aaron Seigo <aseigo@kde.org> *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
20 ***************************************************************************/
22 #include "dialog.h"
24 #include <QPainter>
25 #include <QSvgRenderer>
26 #include <QResizeEvent>
27 #include <QMouseEvent>
28 #ifdef Q_WS_X11
29 #include <QX11Info>
30 #endif
31 #include <QGraphicsView>
32 #include <QtGui/QGraphicsSceneEvent>
34 #include <KDebug>
35 #include <NETRootInfo>
37 #include <plasma/svgpanel.h>
38 #include <plasma/theme.h>
40 #ifdef Q_WS_X11
41 #include <X11/Xlib.h>
42 #endif
45 namespace Plasma
48 class Dialog::Private
50 public:
51 /**
52 * Holds the background SVG, to be re-rendered when the cache is invalidated,
53 * for example by resizing the dialogue.
55 Plasma::SvgPanel *background;
58 Dialog::Dialog( QWidget * parent, Qt::WindowFlags f )
59 : QWidget(parent, f),
60 d(new Private)
62 d->background = new SvgPanel("dialogs/background", this);
63 d->background->setBorderFlags(SvgPanel::DrawAllBorders);
64 d->background->resize(size());
66 connect(d->background, SIGNAL(repaintNeeded()), this, SLOT(update()));
68 connect(Plasma::Theme::self(), SIGNAL(changed()), this, SLOT(themeUpdated()));
69 themeUpdated();
72 Dialog::~Dialog()
76 void Dialog::themeUpdated()
78 const int topHeight = d->background->marginSize(Plasma::TopMargin);
79 const int leftWidth = d->background->marginSize(Plasma::LeftMargin);
80 const int rightWidth = d->background->marginSize(Plasma::RightMargin);
81 const int bottomHeight = d->background->marginSize(Plasma::BottomMargin);
82 setContentsMargins(leftWidth, topHeight, rightWidth, bottomHeight);
85 void Dialog::paintEvent(QPaintEvent *e)
87 QPainter p(this);
88 p.setRenderHint(QPainter::Antialiasing);
89 p.setClipRect(e->rect());
90 p.setCompositionMode(QPainter::CompositionMode_Source );
91 p.fillRect(rect(), Qt::transparent);
92 d->background->paint(&p, e->rect());
95 void Dialog::resizeEvent(QResizeEvent *e)
97 d->background->resize(e->size());
100 void Dialog::position(QGraphicsSceneEvent *event, const QRectF boundingRect, QPointF scenePos)
102 QWidget *viewWidget = event->widget() ? event->widget()->parentWidget() : 0;
103 //QPointF scenePos = mapToScene(boundingRect.topLeft());
104 QGraphicsView *view = qobject_cast<QGraphicsView*>(viewWidget);
105 position(view,boundingRect,scenePos);
108 void Dialog::position(QGraphicsView * view,const QRectF boundingRect,QPointF scenePos)
110 if (view) {
111 QPoint viewPos = view->mapFromScene(scenePos);
112 QPoint globalPos = view->mapToGlobal(viewPos);
113 if ((globalPos.ry()-height())< 0) {
114 scenePos = QPointF(scenePos.x() + boundingRect.width(), scenePos.y() + boundingRect.height());
115 viewPos = view->mapFromScene(scenePos);
116 globalPos = view->mapToGlobal(viewPos)+QPoint(0,10);
118 else {
119 globalPos.ry() -= (height()+10);
121 if ((globalPos.rx() + width()) > view->width()) {
122 globalPos.rx()-=((globalPos.rx() + width())-view->width());
124 move(globalPos);
125 kDebug() << globalPos;
130 #include "dialog.moc"