File: rect.cpp

package info (click to toggle)
actiona 3.10.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 7,312 kB
  • sloc: cpp: 57,403; ansic: 90; xml: 27; makefile: 19
file content (357 lines) | stat: -rw-r--r-- 7,124 bytes parent folder | download | duplicates (2)
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
	Actiona
	Copyright (C) 2005 Jonathan Mercier-Ganady

	Actiona 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 3 of the License, or
	(at your option) any later version.

	Actiona 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/>.

	Contact : jmgr@jmgr.info
*/

#include "rect.h"
#include "point.h"
#include "size.h"
#include "codetools.h"

namespace Code
{
	QScriptValue Rect::constructor(QScriptContext *context, QScriptEngine *engine)
	{
		Rect *rect = nullptr;
		
		switch(context->argumentCount())
		{
		case 0:
			rect = new Rect;
			break;
		case 1:
			{
				QObject *object = context->argument(0).toQObject();
				if(Rect *codeRect = qobject_cast<Rect*>(object))
					rect = new Rect(*codeRect);
				else
					throwError(context, engine, QStringLiteral("ParameterTypeError"), tr("Incorrect parameter type"));
			}
			break;
		case 4:
			rect = new Rect(QRect(context->argument(0).toInt32(),
								  context->argument(1).toInt32(),
								  context->argument(2).toInt32(),
								  context->argument(3).toInt32()));
			break;
		default:
			throwError(context, engine, QStringLiteral("ParameterCountError"), tr("Incorrect parameter count"));
			break;
		}
		
		if(!rect)
			return engine->undefinedValue();

		return CodeClass::constructor(rect, context, engine);
	}
	
	QScriptValue Rect::constructor(const QRect &rect, QScriptEngine *engine)
	{
		return CodeClass::constructor(new Rect(rect), engine);
	}
	
	QRect Rect::parameter(QScriptContext *context, QScriptEngine *engine)
	{
		switch(context->argumentCount())
		{
		case 1:
			{
				QObject *object = context->argument(0).toQObject();
				if(Rect *rect = qobject_cast<Rect*>(object))
					return rect->mRect;
				else
					throwError(context, engine, QStringLiteral("ParameterTypeError"), tr("Incorrect parameter type"));
			}
			return {};
		case 4:
			return QRect(context->argument(0).toInt32(),
						 context->argument(1).toInt32(),
						 context->argument(2).toInt32(),
						 context->argument(3).toInt32());
		default:
			throwError(context, engine, QStringLiteral("ParameterCountError"), tr("Incorrect parameter count"));
			return QRect();
		}
	}

	void Rect::registerClass(QScriptEngine *scriptEngine)
	{
		CodeTools::addClassToScriptEngine<Rect>(scriptEngine);
	}
	
	Rect::Rect()
		: CodeClass()
	{
		
	}

	Rect::Rect(const Rect &other)
		: CodeClass(),
		mRect(other.rect())
	{
		
	}

	Rect::Rect(const QRect &rect)
		: CodeClass(),
		mRect(rect)
	{
		
	}
	
	Rect &Rect::operator=(Rect other)
	{
		swap(other);
		
		return *this;
	}

	Rect &Rect::operator=(QRect rect)
	{
		swap(rect);
		
		return *this;
	}
	
	void Rect::swap(Rect &other)
	{
		std::swap(mRect, other.mRect);
	}

	void Rect::swap(QRect &rect)
	{
		std::swap(mRect, rect);
	}
	
	const QRect &Rect::rect() const
	{
		return mRect;
	}

	int Rect::width() const
	{
		return mRect.width();
	}

	int Rect::height() const
	{
		return mRect.height();
	}

	int Rect::x() const
	{
		return mRect.x();
	}

	int Rect::y() const
	{
		return mRect.y();
	}

	int Rect::left() const
	{
		return mRect.left();
	}

	int Rect::right() const
	{
		return mRect.right();
	}

	int Rect::top() const
	{
		return mRect.top();
	}

	int Rect::bottom() const
	{
		return mRect.bottom();
	}
	
	QScriptValue Rect::clone() const
	{
		return constructor(mRect, engine());
	}

	bool Rect::equals(const QScriptValue &other) const
	{
		if(other.isUndefined() || other.isNull())
			return false;
		
		QObject *object = other.toQObject();
		if(Rect *otherRect = qobject_cast<Rect*>(object))
			return (otherRect == this || otherRect->mRect == mRect);
			
		return false;
	}
	
	QString Rect::toString() const
	{
		return QStringLiteral("Rect {x: %1, y: %2, width: %3, height: %4}").arg(x()).arg(y()).arg(width()).arg(height());
	}
	
	QScriptValue Rect::normalize()
	{
		mRect = mRect.normalized();
		
		return thisObject();
	}
	
	QScriptValue Rect::setTop(int top)
	{
		mRect.setTop(top);
		
		return thisObject();
	}

	QScriptValue Rect::setBottom(int bottom)
	{
		mRect.setBottom(bottom);
		
		return thisObject();
	}

	QScriptValue Rect::setLeft(int left)
	{
		mRect.setLeft(left);
		
		return thisObject();
	}

	QScriptValue Rect::setRight(int right)
	{
		mRect.setRight(right);
		
		return thisObject();
	}

	QScriptValue Rect::setX(int x)
	{
		mRect.setX(x);
		
		return thisObject();
	}

	QScriptValue Rect::setY(int y)
	{
		mRect.setY(y);
		
		return thisObject();
	}

	QScriptValue Rect::setWidth(int width)
	{
		mRect.setWidth(width);
		
		return thisObject();
	}

	QScriptValue Rect::setHeight(int height)
	{
		mRect.setHeight(height);
		
		return thisObject();
	}

	QScriptValue Rect::setSize()
	{
		mRect.setSize(Size::parameter(context(), engine()));
		
		return thisObject();
	}

	QScriptValue Rect::setCoords(int x1, int y1, int x2, int y2)
	{
		mRect.setCoords(x1, y1, x2, y2);
		
		return thisObject();
	}

	QScriptValue Rect::setRect()
	{
		const QRect &rect = parameter(context(), engine());
		mRect.setRect(rect.x(), rect.y(), rect.width(), rect.height());
		
		return thisObject();
	}

	QScriptValue Rect::translate()
	{
		mRect.translate(Point::parameter(context(), engine()));
		
		return thisObject();
	}

	bool Rect::contains(const QScriptValue &point) const
	{
		switch(context()->argumentCount())
		{
		case 1:
			{
				QObject *object = point.toQObject();
				if(auto codePoint = qobject_cast<Point*>(object))
					return mRect.contains(codePoint->point());
				else if(Rect *codeRect = qobject_cast<Rect*>(object))
					return (codeRect == this || mRect.contains(codeRect->mRect));
				else
					throwError(QStringLiteral("ParameterTypeError"), tr("Incorrect parameter type"));
			}
			return false;
		case 2:
			return mRect.contains(QPoint(context()->argument(0).toInt32(), context()->argument(1).toInt32()));
		case 4:
			return mRect.contains(QRect(context()->argument(0).toInt32(),
										context()->argument(1).toInt32(),
										context()->argument(2).toInt32(),
										context()->argument(3).toInt32()));
		default:
			throwError(QStringLiteral("ParameterCountError"), tr("Incorrect parameter count"));
			return false;
		}
	}
	
	QScriptValue Rect::united() const
	{
		return constructor(mRect.united(parameter(context(), engine())), engine());
	}
	
	QScriptValue Rect::intersected() const
	{
		return constructor(mRect.intersected(parameter(context(), engine())), engine());
	}
	
	bool Rect::intersects() const
	{
		return mRect.intersects(parameter(context(), engine()));
	}
	
	bool Rect::isEmpty() const
	{
		return mRect.isEmpty();
	}
	
	QScriptValue Rect::center() const
	{
		return Point::constructor(mRect.center(), engine());
	}
	
	QScriptValue Rect::size() const
	{
		return Size::constructor(mRect.size(), engine());
	}
}