forked from supercollider/supercollider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype_codec.cpp
483 lines (377 loc) · 11.4 KB
/
type_codec.cpp
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/************************************************************************
*
* Copyright 2010 - 2012 Jakob Leben ([email protected])
*
* This file is part of SuperCollider Qt GUI.
*
* 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 "type_codec.hpp"
#include "metatype.hpp"
#include "QObjectProxy.h"
#include "widgets/QcTreeWidget.h"
#include "primitives/prim_QPalette.hpp"
#include "image.h"
#include <PyrObject.h>
#include <PyrKernel.h>
#include <GC.h>
#include <VMGlobals.h>
#include <qmath.h>
namespace QtCollider {
QString TypeCodec<QString>::read( PyrSlot *slot )
{
if( IsSym(slot) ) {
return QString::fromUtf8( slotRawSymbol(slot)->name );
}
else if( isKindOfSlot( slot, class_string ) ) {
int len = slotRawObject( slot )->size;
return QString::fromUtf8( slotRawString(slot)->s, len );
}
return QString();
}
void TypeCodec<QString>::write( PyrSlot *slot, const QString & val )
{
PyrString *str = newPyrString( gMainVMGlobals->gc,
val.toUtf8().constData(), 0, true );
SetObject( slot, str );
}
QPointF TypeCodec<QPointF>::read( PyrSlot *slot )
{
PyrSlot *slots = slotRawObject( slot )->slots;
float x, y;
int err;
err = slotFloatVal( slots+0, &x ); if( err ) return QPointF();
err = slotFloatVal( slots+1, &y ); if( err ) return QPointF();
return QPointF( x, y );
}
QPointF TypeCodec<QPointF>::safeRead( PyrSlot *slot )
{
if ( isKindOfSlot( slot, SC_CLASS(Point) ) )
return read( slot );
else
return QPointF();
}
void TypeCodec<QPointF>::write( PyrSlot *slot, const QPointF & pt )
{
PyrObject *obj = instantiateObject( gMainVMGlobals->gc, SC_CLASS(Point), 0, true, true );
SetObject( slot, obj );
PyrSlot *slots = obj->slots;
SetFloat( slots+0, pt.x() );
SetFloat( slots+1, pt.y() );
}
QRectF TypeCodec<QRectF>::read( PyrSlot *slot )
{
PyrSlot *slots = slotRawObject( slot )->slots;
float bounds[4];
for( int i=0; i<4; ++i )
{
int err = slotFloatVal(slots + i, &bounds[i]);
if( err ) return QRectF();
}
return QRectF( bounds[0], bounds[1], bounds[2], bounds[3] );
}
QRectF TypeCodec<QRectF>::safeRead( PyrSlot *slot )
{
if ( isKindOfSlot( slot, SC_CLASS(Rect) ) )
return read( slot );
else
return QRectF();
}
void TypeCodec<QRectF>::write( PyrSlot *slot, const QRectF & r )
{
PyrObject *obj = instantiateObject( gMainVMGlobals->gc, SC_CLASS(Rect), 0, true, true );
SetObject( slot, obj );
PyrSlot *slots = obj->slots;
SetFloat( slots+0, r.x() );
SetFloat( slots+1, r.y() );
SetFloat( slots+2, r.width() );
SetFloat( slots+3, r.height() );
}
QSizeF TypeCodec<QSizeF>::read( PyrSlot *slot )
{
PyrSlot *slots = slotRawObject( slot )->slots;
float w = 0.f, h = 0.f;
slotFloatVal( slots+0, &w );
slotFloatVal( slots+1, &h );
return QSizeF( w, h );
}
QSizeF TypeCodec<QSizeF>::safeRead( PyrSlot *slot )
{
if ( isKindOfSlot( slot, SC_CLASS(Size) ) )
return read( slot );
else
return QSizeF();
}
void TypeCodec<QSizeF>::write( PyrSlot *slot, const QSizeF & sz )
{
PyrObject *obj = instantiateObject( gMainVMGlobals->gc, SC_CLASS(Size), 0, true, true );
SetObject( slot, obj );
PyrSlot *slots = obj->slots;
SetFloat( slots+0, sz.width() );
SetFloat( slots+1, sz.height() );
}
inline
QColor asColor( PyrObject *obj )
{
PyrSlot *slots = obj->slots;
float r,g,b,a;
r = g = b = a = 0.f;
slotFloatVal(slots+0, &r);
slotFloatVal(slots+1, &g);
slotFloatVal(slots+2, &b);
slotFloatVal(slots+3, &a);
return QColor( r*255, g*255, b*255, a*255 );
}
QColor TypeCodec<QColor>::read( PyrSlot *slot )
{
PyrObject *obj = slotRawObject(slot);
PyrClass *klass = obj->classptr;
if( klass == SC_CLASS(Color) )
return asColor(obj);
if( klass == SC_CLASS(Gradient) || klass == SC_CLASS(HiliteGradient) )
{
qcWarningMsg("WARNING: Gradient and HiliteGradient are not supported yet."
" Using the average gradient color instead.");
QColor c1( safeRead(obj->slots+0) );
QColor c2( safeRead(obj->slots+1) );
QColor mix( (c1.red() + c2.red()) / 2,
(c1.green() + c2.green()) / 2,
(c1.blue() + c2.blue()) / 2 );
return mix;
}
return QColor();
}
void TypeCodec<QColor>::write( PyrSlot *slot, const QColor & c )
{
if(!c.isValid()) {
SetNil(slot);
return;
}
PyrObject *obj = instantiateObject( gMainVMGlobals->gc, SC_CLASS(Color), 0, true, true );
SetObject( slot, obj );
PyrSlot *slots = obj->slots;
SetFloat( slots+0, c.red() / 255.0 );
SetFloat( slots+1, c.green() / 255.0 );
SetFloat( slots+2, c.blue() / 255.0 );
SetFloat( slots+3, c.alpha() / 255.0 );
}
QFont TypeCodec<QFont>::read( PyrSlot *slot )
{
PyrSlot *slots = slotRawObject(slot)->slots;
QString family = TypeCodec<QString>::safeRead( slots+0 );
float fSize = TypeCodec<float>::safeRead( slots+1 );
bool bold = TypeCodec<bool>::safeRead( slots+2 );
bool italic = TypeCodec<bool>::safeRead( slots+3 );
bool isPtSize = TypeCodec<bool>::safeRead( slots+4 );
QFont f;
if( !family.isEmpty() ) f.setFamily( family );
if( fSize > 0.f ) {
if( isPtSize ) {
f.setPointSizeF( fSize );
}
else {
int pixSize = ( fSize > 1.f ? qRound(fSize) : 1 );
f.setPixelSize( pixSize );
}
}
f.setBold( bold );
f.setItalic( italic );
return f;
}
QFont TypeCodec<QFont>::safeRead( PyrSlot *slot )
{
if ( isKindOfSlot( slot, SC_CLASS(Font) ) )
return TypeCodec<QFont>::read( slot );
else
return QFont();
}
QPalette TypeCodec<QPalette>::read( PyrSlot *slot )
{
QPalette *p = QPALETTE_FROM_OBJECT(slotRawObject(slot));
return *p;
}
QPalette TypeCodec<QPalette>::safeRead( PyrSlot *slot )
{
if ( isKindOfSlot( slot, SC_CLASS(QPalette) ) )
return TypeCodec<QPalette>::read( slot );
else
return QPalette();
}
void TypeCodec<QPalette>::write( PyrSlot *slot, const QPalette & plt )
{
PyrGC *gc = gMainVMGlobals->gc;
PyrObject *obj = instantiateObject( gc, SC_CLASS(QPalette), 0, true, true );
SetObject( slot, obj );
QPalette_Init( gMainVMGlobals, obj, plt );
}
QObjectProxy * TypeCodec<QObjectProxy*>::safeRead( PyrSlot *slot )
{
if( !isKindOfSlot( slot, SC_CLASS(QObject) ) ) return 0;
return read(slot);
}
void TypeCodec<QObject*>::write( PyrSlot *slot, QObject * obj )
{
QObjectProxy *proxy = QObjectProxy::fromObject(obj);
if( proxy && proxy->scObject() )
SetObject( slot, proxy->scObject() );
else
SetNil( slot );
}
QVariantList TypeCodec<QVariantList>::read( PyrSlot *slot )
{
if( isKindOfSlot( slot, class_array ) ) {
PyrObject *obj = slotRawObject( slot );
PyrSlot *slots = obj->slots;
int size = obj->size;
QVariantList list;
for( int i = 0; i < size; ++i, ++slots ) {
list << QtCollider::get<QVariant>(slots);
}
return list;
}
else if( isKindOfSlot( slot, class_symbolarray ) ) {
PyrSymbolArray *symarray = slotRawSymbolArray( slot );
PyrSymbol **symbols = symarray->symbols;
int size = symarray->size;
QVariantList list;
for( int i = 0; i < size; ++i, ++symbols )
list << QVariant( QString( (*symbols)->name) );
return list;
}
return QVariantList();
}
void TypeCodec<QVariantList>::write( PyrSlot *slot, const QVariantList & varList )
{
VMGlobals *g = gMainVMGlobals;
int count = varList.count();
PyrObject *array = newPyrArray( g->gc, count, 0, true );
SetObject( slot, array );
int i;
PyrSlot *s = array->slots;
for( i = 0; i < count; ++i, ++s ) {
if( !QtCollider::set( s, varList[i] ) ) {
qcDebugMsg(1, "WARNING: Could not set one slot of array" );
}
array->size++;
g->gc->GCWrite( array, s );
}
}
#define WRONG_OBJECT_FORMAT false
template<typename DEST, typename ORIG>
inline static void copy( QVector<DEST> & dest, PyrSlot *orig, int size )
{
ORIG *array = (ORIG*) orig;
for( int i = 0; i < size; ++i )
dest << DEST(array[i]);
}
template<typename numeric_type>
static QVector<numeric_type> toNumericVector( PyrObject *obj )
{
int size = obj->size;
PyrSlot *slots = obj->slots;
QVector<numeric_type> vector;
vector.reserve(size);
switch (obj->obj_format) {
case obj_double:
copy<numeric_type, double>( vector, slots, size ); break;
case obj_float:
copy<numeric_type, float>( vector, slots, size ); break;
case obj_int32:
copy<numeric_type, int32>( vector, slots, size ); break;
case obj_int16:
copy<numeric_type, int16>( vector, slots, size ); break;
case obj_int8:
copy<numeric_type, int8>( vector, slots, size ); break;
default:
Q_ASSERT( WRONG_OBJECT_FORMAT );
}
return vector;
}
template<typename numeric_type>
static void setNumeric( PyrSlot *, numeric_type );
template<> inline
void setNumeric<double>( PyrSlot *s, double val )
{
SetFloat( s, val );
}
template<> inline
void setNumeric<int>( PyrSlot *s, int val )
{
SetInt( s, val );
}
template<typename numeric_type>
static void setNumericVector( PyrSlot *slot, const QVector<numeric_type> & vec )
{
VMGlobals *g = gMainVMGlobals;
int count = vec.count();
PyrObject *array = newPyrArray( g->gc, count, 0, true );
SetObject( slot, array );
PyrSlot *s = array->slots;
Q_FOREACH( numeric_type val, vec ) {
setNumeric<numeric_type>( s, val );
++array->size;
++s;
}
}
QVector<double> TypeCodec< QVector<double> >::read( PyrSlot *slot )
{
return toNumericVector<double>(slotRawObject(slot));
}
void TypeCodec< QVector<double> >::write( PyrSlot *slot, const QVector<double> & vec )
{
setNumericVector<double>( slot, vec );
}
QVector<int> TypeCodec< QVector<int> >::read( PyrSlot *slot )
{
return toNumericVector<int>(slotRawObject(slot));
}
void TypeCodec< QVector<int> >::write( PyrSlot *slot, const QVector<int> & vec )
{
setNumericVector<int>( slot, vec );
}
QcTreeWidget::ItemPtr TypeCodec<QcTreeWidget::ItemPtr>::read( PyrSlot *slot )
{
PyrSlot *ptrSlot = slotRawObject(slot)->slots+0;
if( IsPtr( ptrSlot ) ) {
QcTreeWidget::ItemPtr *safePtr = static_cast<QcTreeWidget::ItemPtr*>( slotRawPtr(ptrSlot) );
return *safePtr;
}
else {
return QcTreeWidget::ItemPtr();
}
}
void TypeCodec<QcTreeWidget::ItemPtr>::write( PyrSlot *slot, const QcTreeWidget::ItemPtr & item )
{
PyrObject *obj = instantiateObject( gMainVMGlobals->gc, SC_CLASS(TreeViewItem), 0, true, true );
QcTreeWidget::Item::initialize( gMainVMGlobals, obj, item );
SetObject( slot, obj );
}
SharedImage TypeCodec<SharedImage>::read( PyrSlot * slot )
{
SharedImage *ptr = reinterpret_cast<SharedImage*>( slotRawPtr( slotRawObject(slot)->slots+0 ) );
return *ptr;
}
SharedImage TypeCodec<SharedImage>::safeRead( PyrSlot * slot )
{
if (!isKindOfSlot(slot, SC_CLASS(Image)))
return SharedImage();
else
return read(slot);
}
void TypeCodec<SharedImage>::write( PyrSlot *slot, SharedImage image )
{
qWarning("WARNING: QtCollider: writing SharedImage to PyrSlot not supported.");
}
} // namespace QtCollider