add more spacing
[personal-kdebase.git] / apps / plasma / applets / folderview / dialog.cpp
blobc3b0bc568e0eb18cb232b7aa15623ed8dc554513
1 /*
2 * Copyright © 2008 Fredrik Höglund <fredrik@kde.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
20 #include "dialog.h"
22 #include <QApplication>
23 #include <QBitmap>
24 #include <QDesktopWidget>
25 #include <QGraphicsView>
26 #include <QGraphicsWidget>
27 #include <QGraphicsScene>
29 #include <KWindowSystem>
31 #include <Plasma/Applet>
32 #include <Plasma/FrameSvg>
34 #ifdef Q_WS_X11
35 # include <QX11Info>
36 #endif
39 Dialog::Dialog(QWidget *parent, Qt::WindowFlags f)
40 : QWidget(parent, f), m_widget(0)
42 setWindowFlags(Qt::Popup | Qt::WindowStaysOnTopHint);
44 #ifdef Q_WS_X11
45 if (!QX11Info::isCompositingManagerRunning()) {
46 setAttribute(Qt::WA_NoSystemBackground);
48 #endif
50 KWindowSystem::setState(effectiveWinId(), NET::SkipTaskbar | NET::SkipPager);
52 QPalette pal = palette();
53 pal.setColor(backgroundRole(), Qt::transparent);
54 setPalette(pal);
56 m_background = new Plasma::FrameSvg(this);
57 m_background->setImagePath("dialogs/background");
59 m_scene = new QGraphicsScene(this);
60 m_view = new QGraphicsView(m_scene, this);
61 m_view->setFrameShape(QFrame::NoFrame);
62 m_view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
63 m_view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
64 m_view->viewport()->setAutoFillBackground(false);
67 Dialog::~Dialog()
71 void Dialog::setGraphicsWidget(QGraphicsWidget *widget)
73 m_widget = widget;
74 m_scene->addItem(widget);
77 void Dialog::show(Plasma::Applet *applet)
79 Plasma::FrameSvg::EnabledBorders borders = Plasma::FrameSvg::AllBorders;
80 m_background->setEnabledBorders(borders);
82 int left = m_background->marginSize(Plasma::LeftMargin);
83 int top = m_background->marginSize(Plasma::TopMargin);
84 int right = m_background->marginSize(Plasma::RightMargin);
85 int bottom = m_background->marginSize(Plasma::BottomMargin);
87 switch (applet->location())
89 case Plasma::BottomEdge:
90 borders &= ~Plasma::FrameSvg::BottomBorder;
91 bottom = qMin(bottom, 2);
92 break;
94 case Plasma::TopEdge:
95 borders &= ~Plasma::FrameSvg::TopBorder;
96 top = qMin(top, 2);
97 break;
99 case Plasma::LeftEdge:
100 borders &= ~Plasma::FrameSvg::LeftBorder;
101 left = qMin(left, 2);
102 break;
104 case Plasma::RightEdge:
105 borders &= ~Plasma::FrameSvg::RightBorder;
106 right = qMin(right, 2);
107 break;
109 default:
110 break;
113 const QRect availableGeometry = QApplication::desktop()->availableGeometry();
114 const QSize maxSize = availableGeometry.size();
116 const QSize margin(left + right, top + bottom);
117 QSize size = m_widget->preferredSize().toSize() + margin;
118 QPoint pos = applet->popupPosition(size);
120 if (pos.y() < 0) {
121 size.rheight() += pos.y();
122 pos.ry() = 0;
123 } else if (pos.y() + size.height() > availableGeometry.bottom()) {
124 size.rheight() -= pos.y() + size.height() - availableGeometry.bottom();
127 if (pos.x() < 0) {
128 size.rwidth() += pos.x();
129 pos.rx() = 0;
130 } else if (pos.x() + size.width() > availableGeometry.right()) {
131 size.rwidth() -= pos.x() + size.width() - availableGeometry.right();
134 m_widget->resize(size - margin);
136 m_background->setEnabledBorders(borders);
137 setContentsMargins(left, top, right, bottom);
139 resize(size);
140 move(pos);
142 QWidget::show();
145 void Dialog::resizeEvent(QResizeEvent *event)
147 Q_UNUSED(event)
149 m_background->resizeFrame(rect().size());
150 m_view->setGeometry(contentsRect());
152 #ifdef Q_WS_X11
153 if (!QX11Info::isCompositingManagerRunning()) {
154 setMask(m_background->mask());
156 #endif
159 void Dialog::paintEvent(QPaintEvent *event)
161 Q_UNUSED(event)
163 QPainter p(this);
164 p.setCompositionMode(QPainter::CompositionMode_Source);
165 p.fillRect(rect(), Qt::transparent);
166 p.setCompositionMode(QPainter::CompositionMode_SourceOver);
167 m_background->paintFrame(&p);
170 void Dialog::focusOutEvent(QFocusEvent *event)
172 Q_UNUSED(event)
173 hide();