OSDN Git Service

8a87c22bc14a927cb5254d95a53a49fde0946564
[kde/kde-extraapps.git] / kdeplasma-addons / applets / showdesktop / showdesktop.cpp
1 /*
2  * Copyright 2008  Petri Damsten <damu@iki.fi>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of
7  * the License, or (at your option) any later version.
8  *
9  * This program 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
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include "showdesktop.h"
19 #include <plasma/widgets/iconwidget.h>
20 #include <plasma/containment.h>
21 #include <plasma/tooltipmanager.h>
22 #include <KGlobalSettings>
23 #include <KIcon>
24 #include <kwindowsystem.h>
25 #include <netwm.h>
26 #include <KIconLoader>
27 #include <QX11Info>
28 #include <QTimer>
29 #include <QGraphicsLinearLayout>
30
31 ShowDesktop::ShowDesktop(QObject *parent, const QVariantList &args)
32     : Plasma::Applet(parent, args), m_wm2ShowingDesktop(false)
33 #ifndef MINIMIZE_ONLY
34       , m_down(false), m_goingDown(false)
35 #endif
36 {
37     setAspectRatioMode(Plasma::ConstrainedSquare);
38     int iconSize = IconSize(KIconLoader::Desktop);
39     resize(iconSize * 2, iconSize * 2);
40     setAcceptDrops(true);
41     connect(&m_timer, SIGNAL(timeout()), this, SLOT(minimizeAll()));
42 }
43
44 ShowDesktop::~ShowDesktop()
45 {
46 }
47
48 void ShowDesktop::init()
49 {
50     connect(this, SIGNAL(activate()), this, SLOT(minimizeAll()));
51
52     QGraphicsLinearLayout *layout = new QGraphicsLinearLayout(this);
53     layout->setContentsMargins(0, 0, 0, 0);
54     layout->setSpacing(0);
55
56     Plasma::IconWidget *icon = new Plasma::IconWidget(KIcon("user-desktop"), QString(), this);
57     layout->addItem(icon);
58     registerAsDragHandle(icon);
59     connect(icon, SIGNAL(clicked()), this, SLOT(minimizeAll()));
60
61     Plasma::ToolTipContent toolTipData(i18n("Show the Desktop"),
62                                        i18n("Minimize all open windows and show the Desktop"),
63                                        icon->icon().pixmap(IconSize(KIconLoader::Desktop)));
64     Plasma::ToolTipManager::self()->setContent(this, toolTipData);
65
66     NETRootInfo info(QX11Info::display(), NET::Supported);
67     m_wm2ShowingDesktop = info.isSupported(NET::WM2ShowingDesktop);
68
69 #ifndef MINIMIZE_ONLY
70     if (m_wm2ShowingDesktop) {
71         connect(KWindowSystem::self(), SIGNAL(activeWindowChanged(WId)), this, SLOT(reset()));
72     }
73 #endif
74
75     connect(KGlobalSettings::self(), SIGNAL(iconChanged(int)),
76         this, SLOT(iconSizeChanged(int)));
77 }
78
79 void ShowDesktop::minimizeAll()
80 {
81     m_timer.stop();
82     if (m_wm2ShowingDesktop) {
83         NETRootInfo info(QX11Info::display(), 0);
84 #ifndef MINIMIZE_ONLY
85         m_down = !m_down;
86         m_goingDown = m_down;
87         info.setShowingDesktop(m_down);
88         // NETRootInfo::showingDesktop() returns always false
89         QTimer::singleShot(500, this, SLOT(delay()));
90 #else
91         info.setShowingDesktop(true);
92 #endif
93     }
94 }
95
96 void ShowDesktop::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
97 {
98     m_timer.start(750);
99     event->accept();
100 }
101
102 void ShowDesktop::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
103 {
104     m_timer.stop();
105 }
106 #ifndef MINIMIZE_ONLY
107
108 void ShowDesktop::delay()
109 {
110     m_goingDown = false;
111 }
112
113 void ShowDesktop::reset()
114 {
115     if (!m_goingDown) {
116         m_down = false;
117     }
118 }
119
120 QSizeF ShowDesktop::sizeHint(Qt::SizeHint which, const QSizeF & constraint) const
121 {
122     if (which == Qt::PreferredSize) {
123         int iconSize;
124
125         switch (formFactor()) {
126             case Plasma::Planar:
127             case Plasma::MediaCenter:
128                 iconSize = IconSize(KIconLoader::Desktop);
129                 break;
130
131             case Plasma::Horizontal:
132             case Plasma::Vertical:
133                 iconSize = IconSize(KIconLoader::Panel);
134                 break;
135         }
136
137         return QSizeF(iconSize, iconSize);
138     }
139
140     return Plasma::Applet::sizeHint(which, constraint);
141 }
142
143 void ShowDesktop::iconSizeChanged(int group)
144 {
145     if (group == KIconLoader::Desktop || group == KIconLoader::Panel) {
146         updateGeometry();
147     }
148 }
149
150 #endif
151
152 #include "moc_showdesktop.cpp"