File: rect.cpp

package info (click to toggle)
plasma-workspace 4%3A5.8.6-2.1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 54,996 kB
  • sloc: cpp: 70,006; sh: 868; xml: 867; python: 46; makefile: 16
file content (330 lines) | stat: -rw-r--r-- 10,655 bytes parent folder | download
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
 *   Copyright 2007 Richard J. Moore <rich@kde.org>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Library General Public License version 2 as
 *   published by the Free Software Foundation
 *
 *   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 Library General Public
 *   License along with this program; if not, write to the
 *   Free Software Foundation, Inc.,
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include <QtScript/QScriptValue>
#include <QtScript/QScriptEngine>
#include <QtScript/QScriptContext>
#include <QtCore/QRectF>
#include "backportglobal.h"

Q_DECLARE_METATYPE(QRectF*)
Q_DECLARE_METATYPE(QRectF)

static QScriptValue ctor(QScriptContext *ctx, QScriptEngine *eng)
{
    if (ctx->argumentCount() == 4)
    {
        qreal x = ctx->argument(0).toNumber();
        qreal y = ctx->argument(1).toNumber();
        qreal width = ctx->argument(2).toNumber();
        qreal height = ctx->argument(3).toNumber();
        return qScriptValueFromValue(eng, QRectF(x, y, width, height));
    }

    return qScriptValueFromValue(eng, QRectF());
}

static QScriptValue adjust(QScriptContext *ctx, QScriptEngine *)
{
    DECLARE_SELF(QRectF, adjust);
    qreal dx1 = ctx->argument(0).toNumber();
    qreal dy1 = ctx->argument(1).toNumber();
    qreal dx2 = ctx->argument(2).toNumber();
    qreal dy2 = ctx->argument(3).toNumber();

    self->adjust(dx1, dy1, dx2, dy2);
    return QScriptValue();
}

static QScriptValue adjusted(QScriptContext *ctx, QScriptEngine *eng)
{
    DECLARE_SELF(QRectF, adjusted);
    qreal dx1 = ctx->argument(0).toNumber();
    qreal dy1 = ctx->argument(1).toNumber();
    qreal dx2 = ctx->argument(2).toNumber();
    qreal dy2 = ctx->argument(3).toNumber();

    QRectF tmp = self->adjusted(dx1, dy1, dx2, dy2);
    return qScriptValueFromValue(eng, tmp);
}

static QScriptValue bottom(QScriptContext *ctx, QScriptEngine *eng)
{
    DECLARE_SELF(QRectF, bottom);

    if (ctx->argumentCount() > 0) {
        int bottom = ctx->argument(0).toInt32();
        self->setBottom(bottom);
    }

    return QScriptValue(eng, self->bottom());
}

static QScriptValue top(QScriptContext *ctx, QScriptEngine *eng)
{
    DECLARE_SELF(QRectF, top);

    if (ctx->argumentCount() > 0) {
        int top = ctx->argument(0).toInt32();
        self->setTop(top);
    }

    return QScriptValue(eng, self->top());
}

static QScriptValue contains(QScriptContext *ctx, QScriptEngine *eng)
{
    DECLARE_SELF(QRectF, contains);
    qreal x = ctx->argument(0).toNumber();
    qreal y = ctx->argument(1).toNumber();
    return QScriptValue(eng, self->contains(x, y));
}

static QScriptValue height(QScriptContext *ctx, QScriptEngine *eng)
{
    DECLARE_SELF(QRectF, height);

    if (ctx->argumentCount() > 0) {
        int height = ctx->argument(0).toInt32();
        self->setHeight(height);
    }

    return QScriptValue(eng, self->height());
}

static QScriptValue empty(QScriptContext *ctx, QScriptEngine *eng)
{
    DECLARE_SELF(QRectF, empty);
    return QScriptValue(eng, self->isEmpty());
}

static QScriptValue null(QScriptContext *ctx, QScriptEngine *eng)
{
    DECLARE_SELF(QRectF, null);
    return QScriptValue(eng, self->isNull());
}

static QScriptValue valid(QScriptContext *ctx, QScriptEngine *eng)
{
    DECLARE_SELF(QRectF, valid);
    return QScriptValue(eng, self->isValid());
}

static QScriptValue left(QScriptContext *ctx, QScriptEngine *eng)
{
    DECLARE_SELF(QRectF, left);

    if (ctx->argumentCount() > 0) {
        int left = ctx->argument(0).toInt32();
        self->setLeft(left);
    }

    return QScriptValue(eng, self->left());
}

static QScriptValue moveBottom(QScriptContext *ctx, QScriptEngine *)
{
    DECLARE_SELF(QRectF, moveBottom);
    qreal bottom = ctx->argument(0).toNumber();
    self->moveBottom(bottom);
    return QScriptValue();
}

static QScriptValue moveLeft(QScriptContext *ctx, QScriptEngine *)
{
    DECLARE_SELF(QRectF, moveLeft);
    qreal left = ctx->argument(0).toNumber();
    self->moveBottom(left);
    return QScriptValue();
}

static QScriptValue moveRight(QScriptContext *ctx, QScriptEngine *)
{
    DECLARE_SELF(QRectF, moveRight);
    qreal right = ctx->argument(0).toNumber();
    self->moveBottom(right);
    return QScriptValue();
}


static QScriptValue moveTo(QScriptContext *ctx, QScriptEngine *)
{
    DECLARE_SELF(QRectF, moveTo);
    qreal x = ctx->argument(0).toNumber();
    qreal y = ctx->argument(1).toNumber();
    self->moveTo(x, y);
    return QScriptValue();
}

static QScriptValue moveTop(QScriptContext *ctx, QScriptEngine *)
{
    DECLARE_SELF(QRectF, moveTop);
    qreal top = ctx->argument(0).toNumber();
    self->moveTop(top);
    return QScriptValue();
}

static QScriptValue right(QScriptContext *ctx, QScriptEngine *eng)
{
    DECLARE_SELF(QRectF, right);

    if (ctx->argumentCount() > 0) {
        int right = ctx->argument(0).toInt32();
        self->setRight(right);
    }

    return QScriptValue(eng, self->right());
}

static QScriptValue setCoords(QScriptContext *ctx, QScriptEngine *)
{
    DECLARE_SELF(QRectF, setCoords);
    qreal x1 = ctx->argument(0).toNumber();
    qreal y1 = ctx->argument(1).toNumber();
    qreal x2 = ctx->argument(2).toNumber();
    qreal y2 = ctx->argument(3).toNumber();
    self->setCoords(x1, y1, x2, y2);
    return QScriptValue();
}

static QScriptValue setRect(QScriptContext *ctx, QScriptEngine *)
{
    DECLARE_SELF(QRectF, setRect);
    qreal x = ctx->argument(0).toNumber();
    qreal y = ctx->argument(1).toNumber();
    qreal width = ctx->argument(2).toNumber();
    qreal height = ctx->argument(3).toNumber();
    self->setRect(x, y, width, height);
    return QScriptValue();
}

static QScriptValue translate(QScriptContext *ctx, QScriptEngine *)
{
    DECLARE_SELF(QRectF, translate);
    qreal dx = ctx->argument(0).toNumber();
    qreal dy = ctx->argument(1).toNumber();
    self->translate(dx, dy);
    return QScriptValue();
}

static QScriptValue width(QScriptContext *ctx, QScriptEngine *eng)
{
    DECLARE_SELF(QRectF, width);

    if (ctx->argumentCount() > 0) {
        int width = ctx->argument(0).toInt32();
        self->setWidth(width);
    }

    return QScriptValue(eng, self->width());
}

static QScriptValue x(QScriptContext *ctx, QScriptEngine *eng)
{
    DECLARE_SELF(QRectF, x);

    if (ctx->argumentCount() > 0) {
        int x = ctx->argument(0).toInt32();
        self->setX(x);
    }

    return QScriptValue(eng, self->x());
}

static QScriptValue y(QScriptContext *ctx, QScriptEngine *eng)
{
    DECLARE_SELF(QRectF, y);

    if (ctx->argumentCount() > 0) {
        int y = ctx->argument(0).toInt32();
        self->setY(y);
    }

    return QScriptValue(eng, self->y());
}

/* Not Implemented Yet */
// QPointF bottomLeft () const
// QPointF bottomRight () const
// QPointF center () const
// bool contains ( const QPointF & point ) const
// bool contains ( const QRectF & rectangle ) const
// void getCoords ( qreal * x1, qreal * y1, qreal * x2, qreal * y2 ) const
// void getRect ( qreal * x, qreal * y, qreal * width, qreal * height ) const
// QRectF intersected ( const QRectF & rectangle ) const
// bool intersects ( const QRectF & rectangle ) const
// void moveBottomLeft ( const QPointF & position )
// void moveBottomRight ( const QPointF & position )
// void moveCenter ( const QPointF & position )
// void moveTo ( const QPointF & position )
// void moveTopLeft ( const QPointF & position )
// void moveTopRight ( const QPointF & position )
// QRectF normalized () const
// void setBottomLeft ( const QPointF & position )
// void setBottomRight ( const QPointF & position )
// void setSize ( const QSizeF & size )
// void setTopLeft ( const QPointF & position )
// void setTopRight ( const QPointF & position )
// QSizeF size () const
// QRect toAlignedRect () const
// QRect toRect () const
// QPointF topLeft () const
// QPointF topRight () const
// void translate ( const QPointF & offset )
// QRectF translated ( qreal dx, qreal dy ) const
// QRectF translated ( const QPointF & offset ) const
// QRectF united ( const QRectF & rectangle ) const

QScriptValue constructQRectFClass(QScriptEngine *eng)
{
    QScriptValue proto = qScriptValueFromValue(eng, QRectF());
    QScriptValue::PropertyFlags getter = QScriptValue::PropertyGetter;
    QScriptValue::PropertyFlags setter = QScriptValue::PropertySetter;

    proto.setProperty(QStringLiteral("adjust"), eng->newFunction(adjust));
    proto.setProperty(QStringLiteral("adjusted"), eng->newFunction(adjusted), getter);
    proto.setProperty(QStringLiteral("translate"), eng->newFunction(translate));
    proto.setProperty(QStringLiteral("setCoords"), eng->newFunction(setCoords));
    proto.setProperty(QStringLiteral("setRect"), eng->newFunction(setRect));

    proto.setProperty(QStringLiteral("contains"), eng->newFunction(contains));

    proto.setProperty(QStringLiteral("moveBottom"), eng->newFunction(moveBottom));
    proto.setProperty(QStringLiteral("moveLeft"), eng->newFunction(moveLeft));
    proto.setProperty(QStringLiteral("moveRight"), eng->newFunction(moveRight));
    proto.setProperty(QStringLiteral("moveTo"), eng->newFunction(moveTo));
    proto.setProperty(QStringLiteral("moveTop"), eng->newFunction(moveTop));

    proto.setProperty(QStringLiteral("empty"), eng->newFunction(empty), getter);
    proto.setProperty(QStringLiteral("null"), eng->newFunction(null), getter);
    proto.setProperty(QStringLiteral("valid"), eng->newFunction(valid), getter);

    proto.setProperty(QStringLiteral("left"), eng->newFunction(left), getter | setter);
    proto.setProperty(QStringLiteral("top"), eng->newFunction(top), getter | setter);
    proto.setProperty(QStringLiteral("bottom"), eng->newFunction(bottom), getter | setter);
    proto.setProperty(QStringLiteral("right"), eng->newFunction(right), getter | setter);
    proto.setProperty(QStringLiteral("height"), eng->newFunction(height), getter | setter);
    proto.setProperty(QStringLiteral("width"), eng->newFunction(width), getter | setter);
    proto.setProperty(QStringLiteral("x"), eng->newFunction(x), getter | setter);
    proto.setProperty(QStringLiteral("y"), eng->newFunction(y), getter | setter);

    eng->setDefaultPrototype(qMetaTypeId<QRectF>(), proto);
    eng->setDefaultPrototype(qMetaTypeId<QRectF*>(), proto);

    return eng->newFunction(ctor, proto);
}