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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/********************************************************************
 KWin - the KDE window manager
 This file is part of the KDE project.

Copyright (C) 2010 Rohan Prabhu <rohan@rohanprabhu.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*********************************************************************/

#include "toplevel.h"

Q_DECLARE_METATYPE(SWrapper::Toplevel*)

SWrapper::Toplevel::Toplevel(KWin::Toplevel* tlevel)
    : QObject(tlevel)
{
    tl_centralObject = 0;
}

KWin::Toplevel* SWrapper::Toplevel::extractClient(const QScriptValue& value)
{
    SWrapper::Toplevel* tlevel = qscriptvalue_cast<SWrapper::Toplevel*>(value);

    if (tlevel != 0) {
        return tlevel->tl_centralObject;
    }

    return 0;
}

MAP_GET(int, x)
MAP_GET(int, y)
MAP_GET(QSize, size)
MAP_GET(int, width)
MAP_GET(int, height)
MAP_GET(QRect, geometry)
MAP_GET(double, opacity)
MAP_GET(bool, hasAlpha)

QScriptValue SWrapper::Toplevel::pos(QScriptContext* ctx, QScriptEngine* eng)
{
    KWin::Toplevel* central = extractClient(ctx->thisObject());

    if (central == 0) {
        return QScriptValue();
    } else {
        return eng->toScriptValue(central->pos());
    }
}

QScriptValue SWrapper::Toplevel::setOpacity(QScriptContext* ctx, QScriptEngine* eng)
{
    KWin::Toplevel* central = extractClient(ctx->thisObject());

    if (central == 0) {
        return eng->toScriptValue<bool>(0);
    } else {
        QScriptValue opac = ctx->argument(0);
        if (opac.isUndefined() || !opac.isNumber()) {
            return eng->toScriptValue<bool>(0);
        } else {
            qsreal _opac = opac.toNumber();
            // For some access restrictions, setting any window
            // completely transperent is not allowed. As if, a window
            // with 0.001 is perceptible. huh
            if ((_opac > 1) || (_opac <= 0)) {
                return eng->toScriptValue<bool>(0);
            } else {
                central->setOpacity(_opac);
                return eng->toScriptValue<bool>(1);
            }
        }
    }
}

void SWrapper::Toplevel::tl_append(QScriptValue& value, QScriptEngine* eng)
{
    QScriptValue func = eng->newFunction(pos, 0);
    value.setProperty("pos", func, QScriptValue::Undeletable);
    value.setProperty("x", eng->newFunction(x, 0), QScriptValue::Undeletable);
    value.setProperty("y", eng->newFunction(y, 0), QScriptValue::Undeletable);
    value.setProperty("size", eng->newFunction(size, 0), QScriptValue::Undeletable);
    value.setProperty("width", eng->newFunction(width, 0), QScriptValue::Undeletable);
    value.setProperty("height", eng->newFunction(height, 0), QScriptValue::Undeletable);
    value.setProperty("geometry", eng->newFunction(geometry, 0), QScriptValue::Undeletable);
    value.setProperty("opacity", eng->newFunction(opacity, 0), QScriptValue::Undeletable);
    value.setProperty("hasAlpha", eng->newFunction(hasAlpha, 0), QScriptValue::Undeletable);
    value.setProperty("setOpacity", eng->newFunction(setOpacity, 1), QScriptValue::Undeletable);
}