00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "dashboardview.h"
00023
00024 #include <QAction>
00025 #include <QDesktopWidget>
00026 #include <QKeyEvent>
00027 #include <QTimer>
00028
00029 #include <KWindowSystem>
00030
00031 #include "plasma/applet.h"
00032 #include "plasma/corona.h"
00033 #include "plasma/containment.h"
00034 #include "plasma/svg.h"
00035 #include "plasma/appletbrowser.h"
00036 #include "plasmaapp.h"
00037
00038 static const int SUPPRESS_SHOW_TIMEOUT = 500;
00039
00040 DashboardView::DashboardView(Plasma::Containment *containment, QWidget *parent)
00041 : Plasma::View(containment, parent),
00042 m_appletBrowser(0),
00043 m_suppressShow(false),
00044 m_zoomIn(false),
00045 m_zoomOut(false)
00046 {
00047
00048 setWindowFlags(Qt::FramelessWindowHint);
00049 if (!PlasmaApp::hasComposite()) {
00050 setAutoFillBackground(false);
00051 setAttribute(Qt::WA_NoSystemBackground);
00052 }
00053
00054 QDesktopWidget *desktop = QApplication::desktop();
00055 setGeometry(desktop->screenGeometry(containment->screen()));
00056
00057 setWallpaperEnabled(!PlasmaApp::hasComposite());
00058
00059 connect(scene(), SIGNAL(releaseVisualFocus()), SLOT(hideView()));
00060
00061 m_hideAction = new QAction(i18n("Hide Dashboard"), this);
00062 m_hideAction->setIcon(KIcon("preferences-desktop-display"));
00063 m_hideAction->setEnabled(false);
00064 containment->addToolBoxTool(m_hideAction);
00065 connect(m_hideAction, SIGNAL(triggered()), this, SLOT(hideView()));
00066
00067 installEventFilter(this);
00068 }
00069
00070 DashboardView::~DashboardView()
00071 {
00072 delete m_appletBrowser;
00073 }
00074
00075 void DashboardView::drawBackground(QPainter * painter, const QRectF & rect)
00076 {
00077 if (PlasmaApp::hasComposite()) {
00078 setWallpaperEnabled(false);
00079 painter->setCompositionMode(QPainter::CompositionMode_Source);
00080 painter->fillRect(rect, QColor(0, 0, 0, 180));
00081 } else {
00082 setWallpaperEnabled(true);
00083 Plasma::View::drawBackground(painter, rect);
00084 }
00085 }
00086
00087 void DashboardView::paintEvent(QPaintEvent *event)
00088 {
00089 Plasma::View::paintEvent(event);
00090
00091
00092 const QRect r = rect();
00093 const QString text = i18n("Plasma Dashboard");
00094 QFont f = font();
00095 f.bold();
00096 const QFontMetrics fm(f);
00097 const int margin = 6;
00098 const int textWidth = fm.width(text);
00099 const QPoint centered(r.width() / 2 - textWidth / 2 - margin, r.y());
00100 const QRect boundingBox(centered, QSize(margin * 2 + textWidth, fm.height() + margin * 2));
00101
00102 if (!viewport() || !event->rect().intersects(boundingBox)) {
00103 return;
00104 }
00105
00106 QPainterPath box;
00107 box.moveTo(boundingBox.topLeft());
00108 box.lineTo(boundingBox.bottomLeft() + QPoint(0, -margin * 2));
00109 box.quadTo(boundingBox.bottomLeft(), boundingBox.bottomLeft() + QPoint(margin * 2, 0));
00110 box.lineTo(boundingBox.bottomRight() + QPoint(-margin * 2, 0));
00111 box.quadTo(boundingBox.bottomRight(), boundingBox.bottomRight() + QPoint(0, -margin * 2));
00112 box.lineTo(boundingBox.topRight());
00113 box.closeSubpath();
00114
00115 QPainter painter(viewport());
00116 painter.setRenderHint(QPainter::Antialiasing);
00117 painter.setFont(f);
00118
00119 QColor highlight = palette().highlight().color();
00120 highlight.setAlphaF(0.7);
00121 painter.setPen(highlight.darker());
00122 painter.setBrush(highlight);
00123 painter.drawPath(box);
00124 painter.setPen(palette().highlightedText().color());
00125 painter.drawText(boundingBox, Qt::AlignCenter | Qt::AlignVCenter, text);
00126 }
00127
00128 void DashboardView::showAppletBrowser()
00129 {
00130 if (!containment()) {
00131 return;
00132 }
00133
00134 if (!m_appletBrowser) {
00135 m_appletBrowser = new Plasma::AppletBrowser(this, Qt::FramelessWindowHint );
00136 m_appletBrowser->setContainment(containment());
00137
00138 m_appletBrowser->setInitialSize(QSize(400, 400));
00139 m_appletBrowser->setApplication();
00140 m_appletBrowser->setWindowTitle(i18n("Add Widgets"));
00141 QPalette p = m_appletBrowser->palette();
00142 p.setBrush(QPalette::Background, QBrush(QColor(0, 0, 0, 180)));
00143 m_appletBrowser->setPalette(p);
00144 m_appletBrowser->setBackgroundRole(QPalette::Background);
00145 m_appletBrowser->setAutoFillBackground(true);
00146 KWindowSystem::setState(m_appletBrowser->winId(), NET::KeepAbove|NET::SkipTaskbar);
00147 m_appletBrowser->move(0, 0);
00148 m_appletBrowser->installEventFilter(this);
00149 }
00150
00151 m_appletBrowser->setHidden(m_appletBrowser->isVisible());
00152 }
00153
00154 void DashboardView::appletBrowserDestroyed()
00155 {
00156 m_appletBrowser = 0;
00157 }
00158
00159 bool DashboardView::eventFilter(QObject *watched, QEvent *event)
00160 {
00161 if (watched != m_appletBrowser) {
00162 if (event->type() == QEvent::MouseButtonPress) {
00163 QMouseEvent *me = static_cast<QMouseEvent *>(event);
00164 if (me->button() == Qt::LeftButton) {
00165 hideView();
00166 }
00167 }
00168 return false;
00169 }
00170
00171 if (event->type() == QEvent::MouseButtonPress) {
00172 QMouseEvent *me = static_cast<QMouseEvent *>(event);
00173 m_appletBrowserDragStart = me->globalPos();
00174 } else if (event->type() == QEvent::MouseMove && m_appletBrowserDragStart != QPoint()) {
00175 QMouseEvent *me = static_cast<QMouseEvent *>(event);
00176 QPoint newPos = me->globalPos();
00177 QPoint curPos = m_appletBrowser->pos();
00178 int x = curPos.x();
00179 int y = curPos.y();
00180
00181 if (curPos.y() == 0 || curPos.y() + m_appletBrowser->height() >= height()) {
00182 x = curPos.x() + (newPos.x() - m_appletBrowserDragStart.x());
00183 if (x < 0) {
00184 x = 0;
00185 } else if (x + m_appletBrowser->width() > width()) {
00186 x = width() - m_appletBrowser->width();
00187 }
00188 }
00189
00190 if (x == 0 || x + m_appletBrowser->width() >= width()) {
00191 y = m_appletBrowser->y() + (newPos.y() - m_appletBrowserDragStart.y());
00192
00193 if (y < 0) {
00194 y = 0;
00195 } else if (y + m_appletBrowser->height() > height()) {
00196 y = height() - m_appletBrowser->height();
00197 }
00198 }
00199 m_appletBrowser->move(x, y);
00200 m_appletBrowserDragStart = newPos;
00201 } else if (event->type() == QEvent::MouseButtonRelease) {
00202 m_appletBrowserDragStart = QPoint();
00203 }
00204
00205 return false;
00206 }
00207
00208 void DashboardView::toggleVisibility()
00209 {
00210 if (isHidden() && containment()) {
00211 if (m_suppressShow) {
00212 kDebug() << "DashboardView::toggleVisibility but show was suppressed";
00213 return;
00214 }
00215
00216 setWindowState(Qt::WindowFullScreen);
00217 KWindowSystem::setOnAllDesktops(winId(), true);
00218 KWindowSystem::setState(winId(), NET::KeepAbove|NET::SkipTaskbar);
00219
00220 QAction *action = containment()->action("zoom out");
00221 m_zoomOut = action ? action->isEnabled() : false;
00222 action = containment()->action("zoom in");
00223 m_zoomIn = action ? action->isEnabled() : false;
00224
00225 m_hideAction->setEnabled(true);
00226 containment()->enableAction("zoom out", false);
00227 containment()->enableAction("zoom in", false);
00228
00229 show();
00230 raise();
00231
00232 m_suppressShow = true;
00233 QTimer::singleShot(SUPPRESS_SHOW_TIMEOUT, this, SLOT(suppressShowTimeout()));
00234 containment()->openToolBox();
00235 } else {
00236 hideView();
00237 }
00238 }
00239
00240 void DashboardView::setContainment(Plasma::Containment *newContainment)
00241 {
00242 if (!newContainment || newContainment == containment()) {
00243 return;
00244 }
00245
00246 Plasma::Containment *oldContainment = containment();
00247 if (oldContainment) {
00248 oldContainment->removeToolBoxTool(m_hideAction);
00249 }
00250 newContainment->addToolBoxTool(m_hideAction);
00251
00252 if (isVisible()) {
00253 if (oldContainment) {
00254 disconnect(oldContainment, SIGNAL(showAddWidgetsInterface(QPointF)), this, SLOT(showAppletBrowser()));
00255 oldContainment->closeToolBox();
00256 oldContainment->enableAction("zoom out", m_zoomOut);
00257 oldContainment->enableAction("zoom in", m_zoomIn);
00258 }
00259
00260 connect(newContainment, SIGNAL(showAddWidgetsInterface(QPointF)), this, SLOT(showAppletBrowser()));
00261 QAction *action = newContainment->action("zoom out");
00262 m_zoomOut = action ? action->isEnabled() : false;
00263 action = newContainment->action("zoom in");
00264 m_zoomIn = action ? action->isEnabled() : false;
00265 newContainment->enableAction("zoom out", false);
00266 newContainment->enableAction("zoom in", false);
00267 newContainment->openToolBox();
00268 }
00269
00270 if (m_appletBrowser) {
00271 m_appletBrowser->setContainment(newContainment);
00272 }
00273
00274 View::setContainment(0);
00275 View::setContainment(newContainment);
00276 }
00277
00278 void DashboardView::hideView()
00279 {
00280 if (m_appletBrowser) {
00281 m_appletBrowser->hide();
00282 }
00283
00284 disconnect(KWindowSystem::self(), SIGNAL(activeWindowChanged(WId)), this, SLOT(activeWindowChanged(WId)));
00285
00286 if (containment()) {
00287 disconnect(containment(), SIGNAL(showAddWidgetsInterface(QPointF)), this, SLOT(showAppletBrowser()));
00288
00289 containment()->closeToolBox();
00290 containment()->enableAction("zoom out", m_zoomOut);
00291 containment()->enableAction("zoom in", m_zoomIn);
00292 }
00293
00294 m_hideAction->setEnabled(false);
00295 hide();
00296 }
00297
00298 void DashboardView::suppressShowTimeout()
00299 {
00300 kDebug() << "DashboardView::suppressShowTimeout";
00301 m_suppressShow = false;
00302 }
00303
00304 void DashboardView::keyPressEvent(QKeyEvent *event)
00305 {
00306 if (event->key() == Qt::Key_Escape) {
00307 hideView();
00308 event->accept();
00309 return;
00310 }
00311
00312 Plasma::View::keyPressEvent(event);
00313 }
00314
00315 void DashboardView::activeWindowChanged(WId id)
00316 {
00317 if (id != winId() &&
00318 (!m_appletBrowser || id != m_appletBrowser->winId())) {
00319 hideView();
00320 }
00321 }
00322
00323 void DashboardView::showEvent(QShowEvent *event)
00324 {
00325 KWindowSystem::setState(winId(), NET::SkipPager);
00326 connect(KWindowSystem::self(), SIGNAL(activeWindowChanged(WId)), this, SLOT(activeWindowChanged(WId)));
00327 if (containment()) {
00328 connect(containment(), SIGNAL(showAddWidgetsInterface(QPointF)), this, SLOT(showAppletBrowser()));
00329 }
00330 Plasma::View::showEvent(event);
00331 }
00332
00333 #include "dashboardview.moc"
00334