not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / plasma / shells / desktop / dashboardview.cpp
bloba4f4ab41dbe37b99f09bb4e359a408f502beda96
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>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Library General Public License as
8 * published by the Free Software Foundation; either version 2, or
9 * (at your option) any later version.
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
16 * You should have received a copy of the GNU Library General Public
17 * License 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.
22 #include "dashboardview.h"
24 #include <QAction>
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>
34 #include "plasmaapp.h"
36 #include <kephal/screens.h>
38 #include "appletbrowser.h"
40 static const int SUPPRESS_SHOW_TIMEOUT = 500; // Number of millis to prevent reshow of dashboard
42 DashboardView::DashboardView(Plasma::Containment *containment, QWidget *parent)
43 : Plasma::View(containment, parent),
44 m_appletBrowser(0),
45 m_suppressShow(false),
46 m_zoomIn(false),
47 m_zoomOut(false)
49 //setContextMenuPolicy(Qt::NoContextMenu);
50 setWindowFlags(Qt::FramelessWindowHint);
51 if (!PlasmaApp::hasComposite()) {
52 setAutoFillBackground(false);
53 setAttribute(Qt::WA_NoSystemBackground);
56 setGeometry(Kephal::ScreenUtils::screenGeometry(containment->screen()));
58 setWallpaperEnabled(!PlasmaApp::hasComposite());
60 connect(scene(), SIGNAL(releaseVisualFocus()), SLOT(hideView()));
62 m_hideAction = new QAction(i18n("Hide Dashboard"), this);
63 m_hideAction->setIcon(KIcon("preferences-desktop-display"));
64 m_hideAction->setEnabled(false);
65 containment->addToolBoxAction(m_hideAction);
66 connect(m_hideAction, SIGNAL(triggered()), this, SLOT(hideView()));
68 installEventFilter(this);
71 DashboardView::~DashboardView()
73 delete m_appletBrowser;
76 void DashboardView::drawBackground(QPainter * painter, const QRectF & rect)
78 if (PlasmaApp::hasComposite()) {
79 setWallpaperEnabled(false);
80 painter->setCompositionMode(QPainter::CompositionMode_Source);
81 painter->fillRect(rect, QColor(0, 0, 0, 180));
82 } else {
83 setWallpaperEnabled(true);
84 Plasma::View::drawBackground(painter, rect);
88 void DashboardView::paintEvent(QPaintEvent *event)
90 Plasma::View::paintEvent(event);
92 // now draw a little label saying "this is your friendly neighbourhood dashboard"
93 const QRect r = rect();
94 const QString text = i18n("Widget Dashboard");
95 QFont f = font();
96 f.bold();
97 const QFontMetrics fm(f);
98 const int margin = 6;
99 const int textWidth = fm.width(text);
100 const QPoint centered(r.width() / 2 - textWidth / 2 - margin, r.y());
101 const QRect boundingBox(centered, QSize(margin * 2 + textWidth, fm.height() + margin * 2));
103 if (!viewport() || !event->rect().intersects(boundingBox)) {
104 return;
107 QPainterPath box;
108 box.moveTo(boundingBox.topLeft());
109 box.lineTo(boundingBox.bottomLeft() + QPoint(0, -margin * 2));
110 box.quadTo(boundingBox.bottomLeft(), boundingBox.bottomLeft() + QPoint(margin * 2, 0));
111 box.lineTo(boundingBox.bottomRight() + QPoint(-margin * 2, 0));
112 box.quadTo(boundingBox.bottomRight(), boundingBox.bottomRight() + QPoint(0, -margin * 2));
113 box.lineTo(boundingBox.topRight());
114 box.closeSubpath();
116 QPainter painter(viewport());
117 painter.setRenderHint(QPainter::Antialiasing);
118 painter.setFont(f);
119 //kDebug() << "******************** painting from" << centered << boundingBox << rect() << event->rect();
120 QColor highlight = palette().highlight().color();
121 highlight.setAlphaF(0.7);
122 painter.setPen(highlight.darker());
123 painter.setBrush(highlight);
124 painter.drawPath(box);
125 painter.setPen(palette().highlightedText().color());
126 painter.drawText(boundingBox, Qt::AlignCenter | Qt::AlignVCenter, text);
129 void DashboardView::showAppletBrowser()
131 if (!containment()) {
132 return;
135 if (!m_appletBrowser) {
136 m_appletBrowser = new Plasma::AppletBrowser(this, Qt::FramelessWindowHint );
137 m_appletBrowser->setContainment(containment());
138 //TODO: make this proportional to the screen
139 m_appletBrowser->setInitialSize(QSize(400, 400));
140 m_appletBrowser->setApplication();
141 m_appletBrowser->setWindowTitle(i18n("Add Widgets"));
142 QPalette p = m_appletBrowser->palette();
143 p.setBrush(QPalette::Background, QBrush(QColor(0, 0, 0, 180)));
144 m_appletBrowser->setPalette(p);
145 m_appletBrowser->setBackgroundRole(QPalette::Background);
146 m_appletBrowser->setAutoFillBackground(true);
147 KWindowSystem::setState(m_appletBrowser->winId(), NET::KeepAbove|NET::SkipTaskbar);
148 m_appletBrowser->move(0, 0);
149 m_appletBrowser->installEventFilter(this);
152 m_appletBrowser->setHidden(m_appletBrowser->isVisible());
155 void DashboardView::appletBrowserDestroyed()
157 m_appletBrowser = 0;
160 bool DashboardView::eventFilter(QObject *watched, QEvent *event)
162 if (watched != m_appletBrowser) {
163 return false;
166 if (event->type() == QEvent::MouseButtonPress) {
167 QMouseEvent *me = static_cast<QMouseEvent *>(event);
168 m_appletBrowserDragStart = me->globalPos();
169 } else if (event->type() == QEvent::MouseMove && m_appletBrowserDragStart != QPoint()) {
170 const QMouseEvent *me = static_cast<QMouseEvent *>(event);
171 const QPoint newPos = me->globalPos();
172 const QPoint curPos = m_appletBrowser->pos();
173 int x = curPos.x();
174 int y = curPos.y();
176 if (curPos.y() == 0 || curPos.y() + m_appletBrowser->height() >= height()) {
177 x = curPos.x() + (newPos.x() - m_appletBrowserDragStart.x());
178 if (x < 0) {
179 x = 0;
180 } else if (x + m_appletBrowser->width() > width()) {
181 x = width() - m_appletBrowser->width();
185 if (x == 0 || x + m_appletBrowser->width() >= width()) {
186 y = curPos.y() + (newPos.y() - m_appletBrowserDragStart.y());
188 if (y < 0) {
189 y = 0;
190 } else if (y + m_appletBrowser->height() > height()) {
191 y = height() - m_appletBrowser->height();
195 m_appletBrowser->move(x, y);
196 m_appletBrowserDragStart = newPos;
197 } else if (event->type() == QEvent::MouseButtonRelease) {
198 m_appletBrowserDragStart = QPoint();
201 return false;
204 bool DashboardView::event(QEvent *event)
206 if (event->type() == QEvent::Paint) {
207 QPainter p(this);
208 p.setCompositionMode(QPainter::CompositionMode_Source);
209 p.fillRect(rect(), Qt::transparent);
212 return Plasma::View::event(event);
215 void DashboardView::toggleVisibility()
217 if (isHidden() && containment()) {
218 if (m_suppressShow) {
219 kDebug() << "DashboardView::toggleVisibility but show was suppressed";
220 return;
223 setWindowState(Qt::WindowFullScreen);
224 KWindowSystem::setOnAllDesktops(winId(), true);
225 KWindowSystem::setState(winId(), NET::KeepAbove|NET::SkipTaskbar);
227 QAction *action = containment()->action("zoom out");
228 m_zoomOut = action ? action->isEnabled() : false;
229 action = containment()->action("zoom in");
230 m_zoomIn = action ? action->isEnabled() : false;
232 m_hideAction->setEnabled(true);
233 containment()->enableAction("zoom out", false);
234 containment()->enableAction("zoom in", false);
236 show();
237 raise();
239 m_suppressShow = true;
240 QTimer::singleShot(SUPPRESS_SHOW_TIMEOUT, this, SLOT(suppressShowTimeout()));
241 containment()->openToolBox();
242 } else {
243 hideView();
247 void DashboardView::setContainment(Plasma::Containment *newContainment)
249 if (!newContainment || newContainment == containment()) {
250 return;
253 Plasma::Containment *oldContainment = containment();
254 if (oldContainment) {
255 oldContainment->removeToolBoxAction(m_hideAction);
257 newContainment->addToolBoxAction(m_hideAction);
259 if (isVisible()) {
260 if (oldContainment) {
261 disconnect(oldContainment, SIGNAL(showAddWidgetsInterface(QPointF)), this, SLOT(showAppletBrowser()));
262 oldContainment->closeToolBox();
263 oldContainment->enableAction("zoom out", m_zoomOut);
264 oldContainment->enableAction("zoom in", m_zoomIn);
267 connect(newContainment, SIGNAL(showAddWidgetsInterface(QPointF)), this, SLOT(showAppletBrowser()));
268 QAction *action = newContainment->action("zoom out");
269 m_zoomOut = action ? action->isEnabled() : false;
270 action = newContainment->action("zoom in");
271 m_zoomIn = action ? action->isEnabled() : false;
272 newContainment->enableAction("zoom out", false);
273 newContainment->enableAction("zoom in", false);
274 newContainment->openToolBox();
277 if (m_appletBrowser) {
278 m_appletBrowser->setContainment(newContainment);
281 View::setContainment(0); // we don't actually to mess with the screen settings
282 View::setContainment(newContainment);
285 void DashboardView::hideView()
287 if (m_appletBrowser) {
288 m_appletBrowser->hide();
291 #ifndef Q_WS_WIN
292 disconnect(KWindowSystem::self(), SIGNAL(activeWindowChanged(WId)), this, SLOT(activeWindowChanged(WId)));
293 #endif
295 if (containment()) {
296 disconnect(containment(), SIGNAL(showAddWidgetsInterface(QPointF)), this, SLOT(showAppletBrowser()));
298 containment()->closeToolBox();
299 containment()->enableAction("zoom out", m_zoomOut);
300 containment()->enableAction("zoom in", m_zoomIn);
303 m_hideAction->setEnabled(false);
304 hide();
307 void DashboardView::suppressShowTimeout()
309 kDebug() << "DashboardView::suppressShowTimeout";
310 m_suppressShow = false;
313 void DashboardView::keyPressEvent(QKeyEvent *event)
315 if (event->key() == Qt::Key_Escape) {
316 hideView();
317 event->accept();
318 return;
321 Plasma::View::keyPressEvent(event);
324 void DashboardView::activeWindowChanged(WId id)
326 if (id != winId() && (!m_appletBrowser || id != m_appletBrowser->winId()) && find(id) != 0) {
327 hideView();
331 void DashboardView::showEvent(QShowEvent *event)
333 KWindowSystem::setState(winId(), NET::SkipPager);
334 #ifndef Q_WS_WIN
335 connect(KWindowSystem::self(), SIGNAL(activeWindowChanged(WId)), this, SLOT(activeWindowChanged(WId)));
336 #endif
337 if (containment()) {
338 connect(containment(), SIGNAL(showAddWidgetsInterface(QPointF)), this, SLOT(showAppletBrowser()));
340 Plasma::View::showEvent(event);
343 #include "dashboardview.moc"