libplasma
dialog.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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 <QBitmap>
00032 #include <QGraphicsView>
00033 #include <QtGui/QGraphicsSceneEvent>
00034
00035 #include <KDebug>
00036 #include <NETRootInfo>
00037
00038 #include <plasma/panelsvg.h>
00039 #include <plasma/theme.h>
00040
00041 #ifdef Q_WS_X11
00042 #include <X11/Xlib.h>
00043 #endif
00044
00045
00046 namespace Plasma
00047 {
00048
00049 class DialogPrivate
00050 {
00051 public:
00056 Plasma::PanelSvg *background;
00057 Plasma::Dialog *q;
00058
00059 void themeUpdated()
00060 {
00061 const int topHeight = background->marginSize(Plasma::TopMargin);
00062 const int leftWidth = background->marginSize(Plasma::LeftMargin);
00063 const int rightWidth = background->marginSize(Plasma::RightMargin);
00064 const int bottomHeight = background->marginSize(Plasma::BottomMargin);
00065 q->setContentsMargins(leftWidth, topHeight, rightWidth, bottomHeight);
00066 };
00067 };
00068
00069 Dialog::Dialog( QWidget * parent, Qt::WindowFlags f )
00070 : QWidget(parent, f),
00071 d(new DialogPrivate)
00072 {
00073 d->q = this;
00074 d->background = new PanelSvg(this);
00075 d->background->setImagePath("dialogs/background");
00076 d->background->setEnabledBorders(PanelSvg::AllBorders);
00077 d->background->resizePanel(size());
00078
00079 connect(d->background, SIGNAL(repaintNeeded()), this, SLOT(update()));
00080
00081 connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()), this, SLOT(themeUpdated()));
00082 d->themeUpdated();
00083 }
00084
00085 Dialog::~Dialog()
00086 {
00087 delete d;
00088 }
00089
00090 void Dialog::paintEvent(QPaintEvent *e)
00091 {
00092 QPainter p(this);
00093 p.setRenderHint(QPainter::Antialiasing);
00094 p.setClipRect(e->rect());
00095 p.setCompositionMode(QPainter::CompositionMode_Source );
00096 p.fillRect(rect(), Qt::transparent);
00097 d->background->paintPanel(&p, e->rect());
00098 }
00099
00100 void Dialog::resizeEvent(QResizeEvent *e)
00101 {
00102 d->background->resizePanel(e->size());
00103
00104 setMask(d->background->mask());
00105 }
00106
00107 void Dialog::position(QGraphicsSceneEvent *event, const QRectF boundingRect, QPointF scenePos)
00108 {
00109 QWidget *viewWidget = event->widget() ? event->widget()->parentWidget() : 0;
00110
00111 QGraphicsView *view = qobject_cast<QGraphicsView*>(viewWidget);
00112 position(view,boundingRect,scenePos);
00113 }
00114
00115 void Dialog::position(QGraphicsView * view,const QRectF boundingRect,QPointF scenePos)
00116 {
00117 if (view) {
00118 QPoint viewPos = view->mapFromScene(scenePos);
00119 QPoint globalPos = view->mapToGlobal(viewPos);
00120
00121 if ((globalPos.ry()-height())< 0) {
00122 scenePos = QPointF(scenePos.x() + boundingRect.width(), scenePos.y() + boundingRect.height());
00123 viewPos = view->mapFromScene(scenePos);
00124 globalPos = view->mapToGlobal(viewPos)+QPoint(0,10);
00125 } else {
00126 globalPos.ry() -= (height()+10);
00127 }
00128
00129 if ((globalPos.rx() + width()) > view->width()) {
00130 globalPos.rx()-=((globalPos.rx() + width())-view->width());
00131 }
00132
00133 move(globalPos);
00134 kDebug() << globalPos;
00135 }
00136 }
00137
00138 }
00139 #include "dialog.moc"