not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / plasma / shells / screensaver / saverview.cpp
blob54a30693f0fecdefdb9aefe62c9a0dd91d72a95a
1 /*
2 * Copyright 2007 Aaron Seigo <aseigo@kde.org>
3 * Copyright 2007 Matt Broadstone <mbroadst@gmail.com>
4 * Copyright 2007 André Duffeck <duffeck@kde.org>
5 * Copyright 2008 Chani Armitage <chanika@gmail.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Library General Public License as
9 * published by the Free Software Foundation; either version 2, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details
17 * You should have received a copy of the GNU Library General Public
18 * License along with this program; if not, write to the
19 * Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #include "saverview.h"
25 #include <QKeyEvent>
26 #include <QTimer>
28 //#include <KWindowSystem>
30 #include <Plasma/Applet>
31 #include <Plasma/Corona>
32 #include <Plasma/Containment>
33 #include <Plasma/Svg>
35 #include "appletbrowser.h"
36 #include "plasmaapp.h"
38 static const int SUPPRESS_SHOW_TIMEOUT = 500; // Number of millis to prevent reshow of dashboard
40 SaverView::SaverView(Plasma::Containment *containment, QWidget *parent)
41 : Plasma::View(containment, parent),
42 m_appletBrowser(0),
43 m_suppressShow(false),
44 m_setupMode(false)
46 setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint |
47 Qt::X11BypassWindowManagerHint);
48 if (!PlasmaApp::hasComposite()) {
49 setAutoFillBackground(false);
50 setAttribute(Qt::WA_NoSystemBackground);
53 //app is doing this for us - if needed
54 //QDesktopWidget *desktop = QApplication::desktop();
55 //setGeometry(desktop->screenGeometry(containment->screen()));
57 setWallpaperEnabled(!PlasmaApp::hasComposite());
59 installEventFilter(this);
62 SaverView::~SaverView()
64 delete m_appletBrowser;
67 void SaverView::enableSetupMode()
69 if (!m_setupMode) {
70 m_setupMode = true;
71 update();
75 void SaverView::disableSetupMode()
77 if (m_setupMode) {
78 m_setupMode = false;
79 update();
83 void SaverView::drawBackground(QPainter * painter, const QRectF & rect)
85 if (PlasmaApp::hasComposite()) {
86 painter->setCompositionMode(QPainter::CompositionMode_Source);
87 painter->fillRect(rect, QColor(0, 0, 0, 0));
88 //FIXME kwin's shadow effect is getting drawn behind me. do not want.
89 } else {
90 Plasma::View::drawBackground(painter, rect);
94 void SaverView::showAppletBrowser()
96 if (!m_appletBrowser) {
97 m_appletBrowser = new Plasma::AppletBrowser(this, Qt::FramelessWindowHint );
98 m_appletBrowser->setContainment(containment());
99 //TODO: make this proportional to the screen
100 m_appletBrowser->setInitialSize(QSize(400, 400));
101 m_appletBrowser->setApplication();
102 m_appletBrowser->setWindowTitle(i18n("Add Widgets"));
103 QPalette p = m_appletBrowser->palette();
104 p.setBrush(QPalette::Background, QBrush(QColor(0, 0, 0, 180)));
105 m_appletBrowser->setPalette(p);
106 m_appletBrowser->setBackgroundRole(QPalette::Background);
107 m_appletBrowser->setAutoFillBackground(true);
108 m_appletBrowser->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint |
109 Qt::X11BypassWindowManagerHint);
110 //KWindowSystem::setState(m_appletBrowser->winId(), NET::KeepAbove|NET::SkipTaskbar);
111 m_appletBrowser->move(0, 0);
112 m_appletBrowser->installEventFilter(this);
115 //TODO give the filter kbd focus
116 m_appletBrowser->setHidden(m_appletBrowser->isVisible());
119 void SaverView::hideAppletBrowser()
121 if (m_appletBrowser) {
122 m_appletBrowser->hide();
126 void SaverView::appletBrowserDestroyed()
128 m_appletBrowser = 0;
131 void SaverView::paintEvent(QPaintEvent *event)
133 Plasma::View::paintEvent(event);
134 if (!m_setupMode) {
135 return;
138 // now draw a little label reminding the user their screen's not quite locked
139 const QRect r = rect();
140 const QString text = i18n("Setup Mode - Screen is NOT locked");
141 QFont f = font();
142 f.bold();
143 const QFontMetrics fm(f);
144 const int margin = 6;
145 const int textWidth = fm.width(text);
146 const QPoint centered(r.width() / 2 - textWidth / 2 - margin, r.y());
147 const QRect boundingBox(centered, QSize(margin * 2 + textWidth, fm.height() + margin * 2));
149 if (!viewport() || !event->rect().intersects(boundingBox)) {
150 return;
153 QPainterPath box;
154 box.moveTo(boundingBox.topLeft());
155 box.lineTo(boundingBox.bottomLeft() + QPoint(0, -margin * 2));
156 box.quadTo(boundingBox.bottomLeft(), boundingBox.bottomLeft() + QPoint(margin * 2, 0));
157 box.lineTo(boundingBox.bottomRight() + QPoint(-margin * 2, 0));
158 box.quadTo(boundingBox.bottomRight(), boundingBox.bottomRight() + QPoint(0, -margin * 2));
159 box.lineTo(boundingBox.topRight());
160 box.closeSubpath();
162 QPainter painter(viewport());
163 painter.setRenderHint(QPainter::Antialiasing);
164 painter.setFont(f);
165 //kDebug() << "******************** painting from" << centered << boundingBox << rect() << event->rect();
166 QColor highlight = palette().highlight().color();
167 highlight.setAlphaF(0.7);
168 painter.setPen(highlight.darker());
169 painter.setBrush(highlight);
170 painter.drawPath(box);
171 painter.setPen(palette().highlightedText().color());
172 painter.drawText(boundingBox, Qt::AlignCenter | Qt::AlignVCenter, text);
175 bool SaverView::eventFilter(QObject *watched, QEvent *event)
177 if (watched != m_appletBrowser) {
178 /*if (event->type() == QEvent::MouseButtonPress) {
179 QMouseEvent *me = static_cast<QMouseEvent *>(event);
180 if (me->button() == Qt::LeftButton) {
181 hideView();
184 return false;
187 if (event->type() == QEvent::MouseButtonPress) {
188 QMouseEvent *me = static_cast<QMouseEvent *>(event);
189 m_appletBrowserDragStart = me->globalPos();
190 } else if (event->type() == QEvent::MouseMove && m_appletBrowserDragStart != QPoint()) {
191 QMouseEvent *me = static_cast<QMouseEvent *>(event);
192 QPoint newPos = me->globalPos();
193 QPoint curPos = m_appletBrowser->pos();
194 int x = curPos.x();
195 int y = curPos.y();
197 if (curPos.y() == 0 || curPos.y() + m_appletBrowser->height() >= height()) {
198 x = curPos.x() + (newPos.x() - m_appletBrowserDragStart.x());
199 if (x < 0) {
200 x = 0;
201 } else if (x + m_appletBrowser->width() > width()) {
202 x = width() - m_appletBrowser->width();
206 if (x == 0 || x + m_appletBrowser->width() >= width()) {
207 y = m_appletBrowser->y() + (newPos.y() - m_appletBrowserDragStart.y());
209 if (y < 0) {
210 y = 0;
211 } else if (y + m_appletBrowser->height() > height()) {
212 y = height() - m_appletBrowser->height();
215 m_appletBrowser->move(x, y);
216 m_appletBrowserDragStart = newPos;
217 } else if (event->type() == QEvent::MouseButtonRelease) {
218 m_appletBrowserDragStart = QPoint();
221 return false;
224 void SaverView::showView()
226 if (isHidden()) {
227 if (m_suppressShow) {
228 kDebug() << "show was suppressed";
229 return;
232 setWindowState(Qt::WindowFullScreen);
233 //KWindowSystem::setOnAllDesktops(winId(), true);
234 //KWindowSystem::setState(winId(), NET::KeepAbove|NET::SkipTaskbar);
236 show();
237 raise();
239 m_suppressShow = true;
240 QTimer::singleShot(SUPPRESS_SHOW_TIMEOUT, this, SLOT(suppressShowTimeout()));
244 void SaverView::setContainment(Plasma::Containment *newContainment)
246 if (newContainment == containment()) {
247 return;
250 if (isVisible()) {
251 disconnect(containment(), SIGNAL(showAddWidgetsInterface(QPointF)), this, SLOT(showAppletBrowser()));
252 connect(newContainment, SIGNAL(showAddWidgetsInterface(QPointF)), this, SLOT(showAppletBrowser()));
255 if (m_appletBrowser) {
256 m_appletBrowser->setContainment(newContainment);
259 View::setContainment(newContainment);
262 void SaverView::hideView()
264 if (isHidden()) {
265 return;
267 if (m_appletBrowser) {
268 m_appletBrowser->hide();
271 disconnect(containment(), SIGNAL(showAddWidgetsInterface(QPointF)), this, SLOT(showAppletBrowser()));
273 containment()->closeToolBox();
274 hide();
275 //let the lockprocess know
276 emit hidden();
279 void SaverView::suppressShowTimeout()
281 kDebug() << "SaverView::suppressShowTimeout";
282 m_suppressShow = false;
285 void SaverView::keyPressEvent(QKeyEvent *event)
287 /*if (event->key() == Qt::Key_Escape) {
288 hideView();
289 event->accept();
290 return;
293 //kDebug() << event->key() << event->spontaneous();
294 Plasma::View::keyPressEvent(event);
297 //eeeeew. why did dashboard ever have this? wtf!
298 void SaverView::showEvent(QShowEvent *event)
300 //KWindowSystem::setState(winId(), NET::SkipPager);
301 connect(containment(), SIGNAL(showAddWidgetsInterface(QPointF)), this, SLOT(showAppletBrowser()));
302 Plasma::View::showEvent(event);
305 #include "saverview.moc"