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.
22 #include <QApplication>
24 #include <QDesktopWidget>
25 #include <QGraphicsView>
26 #include <QGraphicsWidget>
27 #include <QGraphicsScene>
29 #include <KWindowSystem>
31 #include <Plasma/Applet>
32 #include <Plasma/FrameSvg>
39 Dialog::Dialog(QWidget
*parent
, Qt::WindowFlags f
)
40 : QWidget(parent
, f
), m_widget(0)
42 setWindowFlags(Qt::Popup
| Qt::WindowStaysOnTopHint
);
45 if (!QX11Info::isCompositingManagerRunning()) {
46 setAttribute(Qt::WA_NoSystemBackground
);
50 KWindowSystem::setState(effectiveWinId(), NET::SkipTaskbar
| NET::SkipPager
);
52 QPalette pal
= palette();
53 pal
.setColor(backgroundRole(), Qt::transparent
);
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);
71 void Dialog::setGraphicsWidget(QGraphicsWidget
*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);
95 borders
&= ~Plasma::FrameSvg::TopBorder
;
99 case Plasma::LeftEdge
:
100 borders
&= ~Plasma::FrameSvg::LeftBorder
;
101 left
= qMin(left
, 2);
104 case Plasma::RightEdge
:
105 borders
&= ~Plasma::FrameSvg::RightBorder
;
106 right
= qMin(right
, 2);
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
);
121 size
.rheight() += pos
.y();
123 } else if (pos
.y() + size
.height() > availableGeometry
.bottom()) {
124 size
.rheight() -= pos
.y() + size
.height() - availableGeometry
.bottom();
128 size
.rwidth() += pos
.x();
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
);
145 void Dialog::resizeEvent(QResizeEvent
*event
)
149 m_background
->resizeFrame(rect().size());
150 m_view
->setGeometry(contentsRect());
153 if (!QX11Info::isCompositingManagerRunning()) {
154 setMask(m_background
->mask());
159 void Dialog::paintEvent(QPaintEvent
*event
)
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
)