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

« back to all changes in this revision

Viewing changes to plasma/desktop/shell/scripting/panel.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 2009 Aaron Seigo <aseigo@kde.org>
 
3
 *
 
4
 *   This program is free software; you can redistribute it and/or modify
 
5
 *   it under the terms of the GNU Library General Public License as
 
6
 *   published by the Free Software Foundation; either version 2, or
 
7
 *   (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 Library General Public
 
15
 *   License along with this program; if not, write to the
 
16
 *   Free Software Foundation, Inc.,
 
17
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
18
 */
 
19
 
 
20
#include "panel.h"
 
21
 
 
22
#include <QAction>
 
23
 
 
24
#include <Plasma/Corona>
 
25
#include <Plasma/Containment>
 
26
 
 
27
#include "panelview.h"
 
28
#include "plasmaapp.h"
 
29
#include <plasmagenericshell/scripting/scriptengine.h>
 
30
#include <plasmagenericshell/scripting/widget.h>
 
31
 
 
32
namespace WorkspaceScripting
 
33
{
 
34
 
 
35
Panel::Panel(Plasma::Containment *containment, QObject *parent)
 
36
    : Containment(containment, parent)
 
37
{
 
38
}
 
39
 
 
40
Panel::~Panel()
 
41
{
 
42
}
 
43
 
 
44
QString Panel::location() const
 
45
{
 
46
    Plasma::Containment *c = containment();
 
47
    if (!c) {
 
48
        return "floating";
 
49
    }
 
50
 
 
51
    switch (c->location()) {
 
52
        case Plasma::Floating:
 
53
            return "floating";
 
54
            break;
 
55
        case Plasma::Desktop:
 
56
            return "desktop";
 
57
            break;
 
58
        case Plasma::FullScreen:
 
59
            return "fullscreen";
 
60
            break;
 
61
        case Plasma::TopEdge:
 
62
            return "top";
 
63
            break;
 
64
        case Plasma::BottomEdge:
 
65
            return "bottom";
 
66
            break;
 
67
        case Plasma::LeftEdge:
 
68
            return "left";
 
69
            break;
 
70
        case Plasma::RightEdge:
 
71
            return "right";
 
72
            break;
 
73
    }
 
74
 
 
75
    return "floating";
 
76
}
 
77
 
 
78
void Panel::setLocation(const QString &locationString)
 
79
{
 
80
    Plasma::Containment *c = containment();
 
81
    if (!c) {
 
82
        return;
 
83
    }
 
84
 
 
85
    const QString lower = locationString.toLower();
 
86
    Plasma::Location loc = Plasma::Floating;
 
87
    if (lower == "desktop") {
 
88
        loc = Plasma::Desktop;
 
89
    } else if (lower == "fullscreen") {
 
90
        loc = Plasma::FullScreen;
 
91
    } else if (lower == "top") {
 
92
        loc = Plasma::TopEdge;
 
93
    } else if (lower == "bottom") {
 
94
        loc = Plasma::BottomEdge;
 
95
    } else if (lower == "left") {
 
96
        loc = Plasma::LeftEdge;
 
97
    } else if (lower == "right") {
 
98
        loc = Plasma::RightEdge;
 
99
    }
 
100
 
 
101
    c->setLocation(loc);
 
102
}
 
103
 
 
104
PanelView *Panel::panel() const
 
105
{
 
106
    Plasma::Containment *c = containment();
 
107
    if (!c) {
 
108
        return 0;
 
109
    }
 
110
 
 
111
    foreach (PanelView *v, PlasmaApp::self()->panelViews()) {
 
112
        if (v->containment() == c) {
 
113
            return v;
 
114
        }
 
115
    }
 
116
 
 
117
    return 0;
 
118
}
 
119
 
 
120
QString Panel::alignment() const
 
121
{
 
122
    PanelView *v = panel();
 
123
    if (!v) {
 
124
        return "left";
 
125
    }
 
126
 
 
127
    switch (v->alignment()) {
 
128
        case Qt::AlignRight:
 
129
            return "right";
 
130
            break;
 
131
        case Qt::AlignCenter:
 
132
            return "center";
 
133
            break;
 
134
        default:
 
135
            return "left";
 
136
            break;
 
137
    }
 
138
 
 
139
    return "left";
 
140
}
 
141
 
 
142
void Panel::setAlignment(const QString &alignment)
 
143
{
 
144
    PanelView *v = panel();
 
145
    if (v) {
 
146
        bool success = false;
 
147
 
 
148
        if (alignment.compare("left", Qt::CaseInsensitive) == 0) {
 
149
            if (v->alignment() != Qt::AlignLeft) {
 
150
                success = true;
 
151
                v->setAlignment(Qt::AlignLeft);
 
152
            }
 
153
        } else if (alignment.compare("right", Qt::CaseInsensitive) == 0) {
 
154
            if (v->alignment() != Qt::AlignRight) {
 
155
                success = true;
 
156
                v->setAlignment(Qt::AlignRight);
 
157
            }
 
158
        } else if (alignment.compare("center", Qt::CaseInsensitive) == 0) {
 
159
            if (v->alignment() != Qt::AlignCenter) {
 
160
                success = true;
 
161
                v->setAlignment(Qt::AlignCenter);
 
162
            }
 
163
        }
 
164
 
 
165
        if (success) {
 
166
            v->setOffset(0);
 
167
        }
 
168
    }
 
169
}
 
170
 
 
171
int Panel::offset() const
 
172
{
 
173
    PanelView *v = panel();
 
174
    if (v) {
 
175
        return v->offset();
 
176
    }
 
177
 
 
178
    return 0;
 
179
}
 
180
 
 
181
void Panel::setOffset(int pixels)
 
182
{
 
183
    Plasma::Containment *c = containment();
 
184
    if (pixels < 0 || !c) {
 
185
        return;
 
186
    }
 
187
 
 
188
    PanelView *v = panel();
 
189
    if (v) {
 
190
        QRectF screen = c->corona()->screenGeometry(v->screen());
 
191
        QSizeF size = c->size();
 
192
 
 
193
        if (c->formFactor() == Plasma::Vertical) {
 
194
            if (pixels > screen.height()) {
 
195
                return;
 
196
            }
 
197
 
 
198
            if (size.height() + pixels > screen.height()) {
 
199
                c->resize(size.width(), screen.height() - pixels);
 
200
            }
 
201
        } else if (pixels > screen.width()) {
 
202
            return;
 
203
        } else if (size.width() + pixels > screen.width()) {
 
204
            size.setWidth(screen.width() - pixels);
 
205
            c->resize(size);
 
206
            c->setMinimumSize(size);
 
207
            c->setMaximumSize(size);
 
208
        }
 
209
 
 
210
        v->setOffset(pixels);
 
211
    }
 
212
}
 
213
 
 
214
int Panel::length() const
 
215
{
 
216
    Plasma::Containment *c = containment();
 
217
    if (!c) {
 
218
        return 0;
 
219
    }
 
220
 
 
221
    if (c->formFactor() == Plasma::Vertical) {
 
222
        return c->size().height();
 
223
    } else {
 
224
        return c->size().width();
 
225
    }
 
226
}
 
227
 
 
228
void Panel::setLength(int pixels)
 
229
{
 
230
    Plasma::Containment *c = containment();
 
231
    if (pixels < 0 || !c) {
 
232
        return;
 
233
    }
 
234
 
 
235
    PanelView *v = panel();
 
236
    if (v) {
 
237
        QRectF screen = c->corona()->screenGeometry(v->screen());
 
238
        QSizeF s = c->size();
 
239
        if (c->formFactor() == Plasma::Vertical) {
 
240
            if (pixels > screen.height() - v->offset()) {
 
241
                return;
 
242
            }
 
243
 
 
244
            s.setHeight(pixels);
 
245
        } else if (pixels > screen.width() - v->offset()) {
 
246
            return;
 
247
        } else {
 
248
            s.setWidth(pixels);
 
249
        }
 
250
 
 
251
        c->resize(s);
 
252
        c->setMinimumSize(s);
 
253
        c->setMaximumSize(s);
 
254
    }
 
255
}
 
256
 
 
257
int Panel::height() const
 
258
{
 
259
    Plasma::Containment *c = containment();
 
260
    if (!c) {
 
261
        return 0;
 
262
    }
 
263
 
 
264
    return c->formFactor() == Plasma::Vertical ? c->size().width()
 
265
                                               : c->size().height();
 
266
}
 
267
 
 
268
void Panel::setHeight(int height)
 
269
{
 
270
    Plasma::Containment *c = containment();
 
271
    if (height < 16 || !c) {
 
272
        return;
 
273
    }
 
274
 
 
275
    PanelView *v = panel();
 
276
    if (v) {
 
277
        QRect screen = c->corona()->screenGeometry(v->screen());
 
278
        QSizeF size = c->size();
 
279
        const int max = (c->formFactor() == Plasma::Vertical ? screen.width() : screen.height()) / 3;
 
280
        height = qBound(16, height, max);
 
281
 
 
282
        if (c->formFactor() == Plasma::Vertical) {
 
283
            size.setWidth(height);
 
284
        } else {
 
285
            size.setHeight(height);
 
286
        }
 
287
 
 
288
        c->resize(size);
 
289
        c->setMinimumSize(size);
 
290
        c->setMaximumSize(size);
 
291
    }
 
292
}
 
293
 
 
294
QString Panel::hiding() const
 
295
{
 
296
    PanelView *v = panel();
 
297
    if (v) {
 
298
        switch (v->visibilityMode()) {
 
299
            case PanelView::NormalPanel:
 
300
                return "none";
 
301
                break;
 
302
            case PanelView::AutoHide:
 
303
                return "autohide";
 
304
                break;
 
305
            case PanelView::LetWindowsCover:
 
306
                return "windowscover";
 
307
                break;
 
308
            case PanelView::WindowsGoBelow:
 
309
                return "windowsbelow";
 
310
                break;
 
311
        }
 
312
    }
 
313
 
 
314
    return "none";
 
315
}
 
316
 
 
317
void Panel::setHiding(const QString &mode)
 
318
{
 
319
    PanelView *v = panel();
 
320
    if (v) {
 
321
        if (mode.compare("autohide", Qt::CaseInsensitive) == 0) {
 
322
            v->setVisibilityMode(PanelView::AutoHide);
 
323
        } else if (mode.compare("windowscover", Qt::CaseInsensitive) == 0) {
 
324
            v->setVisibilityMode(PanelView::LetWindowsCover);
 
325
        } else if (mode.compare("windowsbelow", Qt::CaseInsensitive) == 0) {
 
326
            v->setVisibilityMode(PanelView::WindowsGoBelow);
 
327
        } else {
 
328
            v->setVisibilityMode(PanelView::NormalPanel);
 
329
        }
 
330
    }
 
331
}
 
332
 
 
333
}
 
334
 
 
335
#include "panel.moc"
 
336