~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to plasma/desktop/shell/panelapplethandle.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 20010 by Marco Martin <notmart@gmail.com>
 
3
 *
 
4
 * This library is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU Lesser General Public
 
6
 * License as published by the Free Software Foundation; either
 
7
 * version 2.1 of the License, or (at your option) any later version.
 
8
 *
 
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
 * Lesser General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU Lesser General Public
 
15
 * License along with this library; if not, write to the Free Software
 
16
 * Foundation, Inc., 51 Franklin St, Fifth Floor,
 
17
 * Boston, MA  02110-1301  USA
 
18
 */
 
19
 
 
20
#include "panelapplethandle.h"
 
21
#include "toolbutton.h"
 
22
 
 
23
#include <QAction>
 
24
#include <QApplication>
 
25
#include <QBoxLayout>
 
26
#include <QLabel>
 
27
#include <QPropertyAnimation>
 
28
#include <QTimer>
 
29
 
 
30
#include <KWindowSystem>
 
31
 
 
32
#include <Plasma/Applet>
 
33
#include <Plasma/Containment>
 
34
#include <Plasma/Corona>
 
35
#include <Plasma/Svg>
 
36
#include <Plasma/Theme>
 
37
#include <Plasma/WindowEffects>
 
38
 
 
39
PanelAppletHandle::PanelAppletHandle(QWidget *parent, Qt::WindowFlags f)
 
40
    : Plasma::Dialog(parent, f)
 
41
{
 
42
    m_icons = new Plasma::Svg(this);
 
43
    m_icons->setImagePath("widgets/configuration-icons");
 
44
    KWindowSystem::setType(winId(), NET::Dock);
 
45
    setWindowFlags(Qt::X11BypassWindowManagerHint);
 
46
    hide();
 
47
 
 
48
    m_hideTimer = new QTimer(this);
 
49
    m_hideTimer->setSingleShot(true);
 
50
    connect(m_hideTimer, SIGNAL(timeout()), this, SLOT(hide()));
 
51
 
 
52
    m_layout = new QBoxLayout(QBoxLayout::LeftToRight, this);
 
53
    m_layout->setContentsMargins(0, 0, 0, 0);
 
54
    m_configureButton = new ToolButton(this);
 
55
    m_configureButton->setIcon(m_icons->pixmap("configure"));
 
56
    connect(m_configureButton, SIGNAL(clicked()), this, SLOT(configureApplet()));
 
57
 
 
58
    m_layout->addWidget(m_configureButton);
 
59
    m_layout->addStretch(10);
 
60
    m_title = new QLabel(this);
 
61
    m_layout->addWidget(m_title);
 
62
    m_layout->addStretch(0);
 
63
 
 
64
    m_closeButton = new ToolButton(this);
 
65
    m_closeButton->setIcon(m_icons->pixmap("close"));
 
66
    m_layout->addWidget(m_closeButton);
 
67
    connect(m_closeButton, SIGNAL(clicked()), this, SLOT(closeApplet()));
 
68
 
 
69
    m_moveAnimation = new QPropertyAnimation(this, "pos", this);
 
70
 
 
71
    m_layout->activate();
 
72
    resize(minimumSizeHint());
 
73
 
 
74
    connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()), this, SLOT(updatePalette()));
 
75
    updatePalette();
 
76
}
 
77
 
 
78
PanelAppletHandle::~PanelAppletHandle()
 
79
{
 
80
}
 
81
 
 
82
void PanelAppletHandle::updatePalette()
 
83
{
 
84
    QPalette p = m_title->palette();
 
85
    p.setColor(QPalette::WindowText, Plasma::Theme::defaultTheme()->color(Plasma::Theme::TextColor));
 
86
    m_title->setPalette(p);
 
87
}
 
88
 
 
89
void PanelAppletHandle::setApplet(Plasma::Applet *applet)
 
90
{
 
91
    if (applet == m_applet.data()) {
 
92
        moveToApplet();
 
93
        return;
 
94
    }
 
95
    Plasma::Applet *oldApplet = m_applet.data();
 
96
    if (oldApplet) {
 
97
        disconnect(oldApplet, SIGNAL(destroyed()), this, SLOT(appletDestroyed()));
 
98
    }
 
99
 
 
100
    m_applet = applet;
 
101
    m_hideTimer->stop();
 
102
 
 
103
    if (applet) {
 
104
        m_title->setText(applet->name());
 
105
        m_layout->activate();
 
106
        resize(minimumSizeHint());
 
107
 
 
108
        if (QApplication::layoutDirection() == Qt::RightToLeft) {
 
109
            m_layout->setDirection(QBoxLayout::RightToLeft);
 
110
        } else {
 
111
            m_layout->setDirection(QBoxLayout::LeftToRight);
 
112
        }
 
113
 
 
114
        QAction *configAction = applet->action("configure");
 
115
        m_configureButton->setVisible(configAction && configAction->isEnabled());
 
116
 
 
117
        connect(applet, SIGNAL(destroyed()), this, SLOT(appletDestroyed()));
 
118
 
 
119
        moveToApplet();
 
120
    }
 
121
}
 
122
 
 
123
void PanelAppletHandle::moveToApplet()
 
124
{
 
125
    Plasma::Applet *applet = m_applet.data();
 
126
    if (!applet) {
 
127
        return;
 
128
    }
 
129
    Plasma::Containment *containment = applet->containment();
 
130
    if (!containment || !containment->corona()) {
 
131
        return;
 
132
    }
 
133
 
 
134
    if (isVisible()) {
 
135
        m_moveAnimation->setStartValue(pos());
 
136
        m_moveAnimation->setEndValue(containment->corona()->popupPosition(applet, size(), Qt::AlignCenter));
 
137
        m_moveAnimation->setDuration(250);
 
138
        m_moveAnimation->start();
 
139
    } else {
 
140
        move(applet->containment()->corona()->popupPosition(applet, size(), Qt::AlignCenter));
 
141
        Plasma::WindowEffects::slideWindow(this, applet->location());
 
142
        show();
 
143
    }
 
144
}
 
145
 
 
146
void PanelAppletHandle::startHideTimeout()
 
147
{
 
148
    m_hideTimer->start(800);
 
149
    Plasma::Applet *applet = m_applet.data();
 
150
    if (applet) {
 
151
        Plasma::WindowEffects::slideWindow(this, applet->location());
 
152
    }
 
153
}
 
154
 
 
155
void PanelAppletHandle::configureApplet()
 
156
{
 
157
    Plasma::Applet *applet = m_applet.data();
 
158
    if (applet) {
 
159
        applet->showConfigurationInterface();
 
160
    }
 
161
}
 
162
 
 
163
void PanelAppletHandle::closeApplet()
 
164
{
 
165
    Plasma::Applet *applet = m_applet.data();
 
166
    if (applet) {
 
167
        applet->destroy();
 
168
    }
 
169
}
 
170
 
 
171
void PanelAppletHandle::appletDestroyed()
 
172
{
 
173
    Plasma::Applet *applet = m_applet.data();
 
174
    if (applet) {
 
175
        disconnect(applet, SIGNAL(destroyed()), this, SLOT(appletDestroyed()));
 
176
        m_applet.clear();
 
177
    }
 
178
    hide();
 
179
}
 
180
 
 
181
void PanelAppletHandle::enterEvent(QEvent *event)
 
182
{
 
183
    Q_UNUSED(event)
 
184
 
 
185
    m_hideTimer->stop();
 
186
}
 
187
 
 
188
void PanelAppletHandle::leaveEvent(QEvent *event)
 
189
{
 
190
    Q_UNUSED(event)
 
191
 
 
192
    m_hideTimer->start(800);
 
193
}
 
194
 
 
195
void PanelAppletHandle::mousePressEvent(QMouseEvent *event)
 
196
{
 
197
    Plasma::Applet *applet = m_applet.data();
 
198
    if (applet) {
 
199
        emit mousePressed(applet, event);
 
200
    }
 
201
}
 
202
 
 
203
void PanelAppletHandle::mouseMoveEvent(QMouseEvent *event)
 
204
{
 
205
    Plasma::Applet *applet = m_applet.data();
 
206
    if (applet) {
 
207
        emit mouseMoved(applet, event);
 
208
    }
 
209
}
 
210
 
 
211
void PanelAppletHandle::mouseReleaseEvent(QMouseEvent *event)
 
212
{
 
213
    Plasma::Applet *applet = m_applet.data();
 
214
    if (applet) {
 
215
        emit mouseReleased(applet, event);
 
216
    }
 
217
}
 
218
 
 
219
 
 
220
#include "panelapplethandle.moc"