1 /************************************************************************
3 * Copyright 2010 Jakob Leben (jakob.leben@gmail.com)
5 * This file is part of SuperCollider Qt GUI.
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ************************************************************************/
23 #include "QObjectProxy.h"
25 #include "primitives/prim_QPalette.hpp"
27 #include <PyrObject.h>
28 #include <PyrKernel.h>
30 #include <VMGlobals.h>
37 using namespace QtCollider
;
39 static QPalette::ColorRole paletteColorRoles
[] = {
47 QPalette::HighlightedText
,
50 void Slot::setRect( PyrSlot
*slot
, const QRectF
&r
)
52 PyrObject
*obj
= instantiateObject( gMainVMGlobals
->gc
, class_Rect
, 0, true, true );
53 SetObject( slot
, obj
);
55 PyrSlot
*slots
= obj
->slots
;
56 SetFloat( slots
+0, r
.x() );
57 SetFloat( slots
+1, r
.y() );
58 SetFloat( slots
+2, r
.width() );
59 SetFloat( slots
+3, r
.height() );
62 void Slot::setPoint( PyrSlot
*slot
, const QPointF
&pt
)
64 PyrObject
*obj
= instantiateObject( gMainVMGlobals
->gc
, class_Point
, 0, true, true );
65 SetObject( slot
, obj
);
67 PyrSlot
*slots
= obj
->slots
;
68 SetFloat( slots
+0, pt
.x() );
69 SetFloat( slots
+1, pt
.y() );
72 void Slot::setSize( PyrSlot
*slot
, const QSizeF
&sz
)
74 PyrObject
*obj
= instantiateObject( gMainVMGlobals
->gc
, class_Size
, 0, true, true );
75 SetObject( slot
, obj
);
77 PyrSlot
*slots
= obj
->slots
;
78 SetFloat( slots
+0, sz
.width() );
79 SetFloat( slots
+1, sz
.height() );
82 void Slot::setString( PyrSlot
*slot
, const QString
& arg
)
84 PyrString
*str
= newPyrString( gMainVMGlobals
->gc
,
85 arg
.toStdString().c_str(), 0, true );
86 SetObject( slot
, str
);
89 void Slot::setColor( PyrSlot
*slot
, const QColor
&c
)
91 PyrObject
*obj
= instantiateObject( gMainVMGlobals
->gc
, class_Color
, 0, true, true );
92 SetObject( slot
, obj
);
94 PyrSlot
*slots
= obj
->slots
;
95 SetFloat( slots
+0, c
.red() / 255.0 );
96 SetFloat( slots
+1, c
.green() / 255.0 );
97 SetFloat( slots
+2, c
.blue() / 255.0 );
98 SetFloat( slots
+3, c
.alpha() / 255.0 );
101 void Slot::setPalette( PyrSlot
*slot
, const QPalette
&plt
)
103 PyrGC
*gc
= gMainVMGlobals
->gc
;
104 PyrObject
*obj
= instantiateObject( gc
, class_QPalette
, 0, true, true );
105 SetObject( slot
, obj
);
107 QPalette_Init( gMainVMGlobals
, obj
, plt
);
110 void Slot::setQObject( PyrSlot
*s
, QObject
*o
)
117 QObjectProxy
*proxy
= QObjectProxy::fromObject(o
);
118 if( proxy
&& proxy
->scObject() )
119 SetObject( s
, proxy
->scObject() );
124 void Slot::setTreeWidgetItem( PyrSlot
*s
, const SafePtr
<QcTreeWidget::Item
> & itemPtr
)
126 PyrObject
*obj
= instantiateObject( gMainVMGlobals
->gc
, class_QTreeViewItem
, 0, true, true );
127 QcTreeWidget::Item::initialize( gMainVMGlobals
, obj
, itemPtr
);
131 void Slot::setVariantList( PyrSlot
*slot
, const VariantList
& varList
)
133 VMGlobals
*g
= gMainVMGlobals
;
135 int count
= varList
.data
.count();
137 PyrObject
*array
= newPyrArray( g
->gc
, count
, 0, true );
138 SetObject( slot
, array
);
141 PyrSlot
*s
= array
->slots
;
142 for( i
= 0; i
< count
; ++i
, ++s
) {
143 if( !Slot::setVariant( s
, varList
.data
[i
] ) ) {
144 qcDebugMsg(1, "WARNING: Could not set one slot of array" );
147 g
->gc
->GCWrite( array
, s
);
151 bool Slot::setVariant( PyrSlot
*slot
, const QVariant
&val
)
154 int type
= val
.userType();
157 case QMetaType::Bool
:
158 b_val
= val
.toBool();
159 if( b_val
) SetTrue( slot
);
160 else SetFalse( slot
);
163 case QMetaType::QPoint
:
164 case QMetaType::QPointF
:
165 Slot::setPoint( slot
, val
.toPointF() );
168 case QMetaType::QSize
:
169 case QMetaType::QSizeF
:
170 Slot::setSize( slot
, val
.toSizeF() );
173 case QMetaType::QRect
:
174 case QMetaType::QRectF
:
175 Slot::setRect( slot
, val
.toRectF() );
178 case QMetaType::QString
:
179 Slot::setString( slot
, val
.toString() );
182 case QMetaType::QColor
:
183 Slot::setColor( slot
, val
.value
<QColor
>() );
186 case QMetaType::QPalette
:
187 Slot::setPalette( slot
, val
.value
<QPalette
>() );
190 case QMetaType::Float
:
191 case QMetaType::Double
:
192 SetFloat( slot
, val
.value
<double>() );
196 SetInt( slot
, val
.toInt() );
199 case QMetaType::QObjectStar
:
200 Slot::setQObject( slot
, val
.value
<QObject
*>() );
203 case QMetaType::QWidgetStar
:
204 Slot::setQObject( slot
, val
.value
<QWidget
*>() );
207 case QMetaType::Void
:
212 if( type
== qMetaTypeId
<PyrObject
*>() ) {
213 SetObject( slot
, val
.value
<PyrObject
*>() );
215 else if( type
== qMetaTypeId
<VariantList
>() ) {
216 Slot::setVariantList( slot
, val
.value
<VariantList
>() );
218 else if( type
== qMetaTypeId
<QcTreeWidget::ItemPtr
>() ) {
219 Slot::setTreeWidgetItem( slot
, val
.value
< QtCollider::SafePtr
<QcTreeWidget::Item
> >() );
222 qcErrorMsg( "the QVariant could not be interpreted!" );
230 bool Slot::toBool( PyrSlot
*slot
)
232 return IsTrue( slot
);
235 int Slot::toInt( PyrSlot
*slot
)
238 if( slotIntVal( slot
, &i
) ) return 0;
242 float Slot::toFloat( PyrSlot
*slot
)
245 if( slotFloatVal( slot
, &f
) ) return 0.f
;
249 double Slot::toDouble( PyrSlot
*slot
)
252 if( slotDoubleVal( slot
, &d
) ) return 0.0;
256 QString
Slot::toString( PyrSlot
*slot
)
259 return QString( slotRawSymbol(slot
)->name
);
261 else if( isKindOfSlot( slot
, class_String
) ) {
262 int len
= slotRawObject( slot
)->size
;
263 return QString::fromAscii( slotRawString(slot
)->s
, len
);
268 QPointF
Slot::toPoint( PyrSlot
*slot
)
270 if( !isKindOfSlot( slot
, class_Point
) ) {
273 PyrSlot
*slots
= slotRawObject( slot
)->slots
;
276 err
= slotFloatVal( slots
+0, &x
); if( err
) return QPointF();
277 err
= slotFloatVal( slots
+1, &y
); if( err
) return QPointF();
278 return QPointF( x
, y
);
281 QRectF
Slot::toRect( PyrSlot
*slot
)
283 if( !isKindOfSlot( slot
, class_Rect
) ) {
287 PyrSlot
*slots
= slotRawObject( slot
)->slots
;
289 for( int i
=0; i
<4; ++i
)
291 int err
= slotFloatVal(slots
+ i
, &bounds
[i
]);
292 if( err
) return QRectF();
295 return QRectF( bounds
[0], bounds
[1], bounds
[2], bounds
[3] );
298 QSizeF
Slot::toSize( PyrSlot
*slot
)
300 if( !isKindOfSlot( slot
, class_Size
) ) {
304 PyrSlot
*slots
= slotRawObject( slot
)->slots
;
305 float w
= 0.f
, h
= 0.f
;
306 slotFloatVal( slots
+0, &w
);
307 slotFloatVal( slots
+1, &h
);
309 return QSizeF( w
, h
);
312 QColor
Slot::toColor( PyrSlot
*slot
)
314 if( !isKindOfSlot( slot
, class_Color
) )
317 PyrSlot
*slots
= slotRawObject(slot
)->slots
;
322 err
= slotFloatVal(slots
+0, &r
);
324 err
= slotFloatVal(slots
+1, &g
);
326 err
= slotFloatVal(slots
+2, &b
);
328 err
= slotFloatVal(slots
+3, &a
);
330 return QColor( r
*255, g
*255, b
*255, a
*255 );
333 QFont
Slot::toFont( PyrSlot
*slot
)
335 if( !isKindOfSlot( slot
, class_QFont
) )
338 PyrSlot
*slots
= slotRawObject(slot
)->slots
;
340 QString family
= Slot::toString( slots
+0 );
341 float fSize
= Slot::toFloat( slots
+1 );
342 bool bold
= IsTrue( slots
+2 );
343 bool italic
= IsTrue( slots
+3 );
344 bool isPtSize
= IsTrue( slots
+4 );
348 if( !family
.isEmpty() ) f
.setFamily( family
);
352 f
.setPointSizeF( fSize
);
355 int pixSize
= ( fSize
> 1.f
? qRound(fSize
) : 1 );
356 f
.setPixelSize( pixSize
);
362 f
.setItalic( italic
);
367 QPalette
Slot::toPalette( PyrSlot
*slot
)
369 if( !isKindOfSlot( slot
, class_QPalette
) )
372 QPalette
*p
= QPALETTE_FROM_OBJECT(slotRawObject(slot
));
376 VariantList
Slot::toVariantList( PyrSlot
*slot
)
378 if( isKindOfSlot( slot
, class_Array
) ) {
379 PyrObject
*obj
= slotRawObject( slot
);
380 PyrSlot
*slots
= obj
->slots
;
381 int size
= obj
->size
;
383 for( int i
= 0; i
< size
; ++i
, ++slots
)
384 list
.data
<< Slot::toVariant( slots
);
387 else if( isKindOfSlot( slot
, class_SymbolArray
) ) {
388 PyrSymbolArray
*symarray
= slotRawSymbolArray( slot
);
389 PyrSymbol
**symbols
= symarray
->symbols
;
390 int size
= symarray
->size
;
392 for( int i
= 0; i
< size
; ++i
, ++symbols
)
393 list
.data
<< QVariant( QString( (*symbols
)->name
) );
397 return VariantList();
400 QObjectProxy
* Slot::toObjectProxy( PyrSlot
*slot
)
402 if( !isKindOfSlot( slot
, class_QObject
) ) return 0;
403 QObjectProxy
*proxy
= 0;
404 PyrSlot
*proxySlot
= slotRawObject( slot
)->slots
;
405 if( IsPtr( proxySlot
) ) proxy
= (QObjectProxy
*) slotRawPtr( proxySlot
);
409 QcTreeWidget::ItemPtr
Slot::toTreeWidgetItem( PyrSlot
*slot
)
411 if( !isKindOfSlot( slot
, class_QTreeViewItem
) ) return QcTreeWidget::ItemPtr();
412 PyrSlot
*ptrSlot
= slotRawObject(slot
)->slots
+0;
413 if( IsPtr( ptrSlot
) ) {
414 QcTreeWidget::ItemPtr
*safePtr
= static_cast<QcTreeWidget::ItemPtr
*>( slotRawPtr(ptrSlot
) );
418 return QcTreeWidget::ItemPtr();
422 QVariant
Slot::toVariant( PyrSlot
*slot
)
425 switch (GetTag(slot
)) {
430 return QVariant( toInt(slot
) );
432 return QVariant( toString(slot
) );
434 return QVariant( false );
436 return QVariant( true );
439 if( isKindOfSlot( slot
, class_String
) ) {
440 return QVariant( toString(slot
) );
442 else if( isKindOfSlot( slot
, class_Point
) ) {
443 return QVariant( toPoint( slot
) );
445 else if( isKindOfSlot( slot
, class_Rect
) ) {
446 return QVariant( toRect(slot
) );
448 else if( isKindOfSlot( slot
, class_Size
) ) {
449 return QVariant( toSize(slot
) );
451 else if( isKindOfSlot( slot
, class_Color
) ) {
452 return QVariant::fromValue
<QColor
>( toColor(slot
) );
454 else if( isKindOfSlot( slot
, class_QFont
) ) {
455 return QVariant::fromValue
<QFont
>( toFont(slot
) );
457 else if( isKindOfSlot( slot
, class_QPalette
) ) {
458 return QVariant::fromValue
<QPalette
>( toPalette(slot
) );
460 else if( isKindOfSlot( slot
, class_QObject
) ) {
461 proxy
= toObjectProxy(slot
);
462 return QVariant::fromValue
<QObjectProxy
*>( proxy
);
464 else if( isKindOfSlot( slot
, class_Array
) || isKindOfSlot( slot
, class_SymbolArray
) ) {
465 return QVariant::fromValue
<VariantList
>( toVariantList(slot
) );
467 else if( isKindOfSlot( slot
, class_QTreeViewItem
) ) {
468 return QVariant::fromValue
<QcTreeWidget::ItemPtr
>( toTreeWidgetItem(slot
) );
471 QString className
= Slot::toString( &slotRawObject(slot
)->classptr
->name
);
472 qcWarningMsg(QString("WARNING: Do not know how to use an instance of class '%1'").arg(className
));
477 return QVariant( toDouble( slot
) );
481 using namespace Slot
;
483 void QtCollider::Variant::setData( PyrSlot
*slot
)
486 switch (GetTag(slot
)) {
489 _type
= QMetaType::Void
;
493 _type
= QMetaType::Int
;
494 _ptr
= new int( toInt(slot
) );
497 _type
= QMetaType::QString
;
498 _ptr
= new QString( toString(slot
) );
501 _type
= QMetaType::Bool
;
502 _ptr
= new bool( false );
505 _type
= QMetaType::Bool
;
506 _ptr
= new bool( true );
510 if( isKindOfSlot( slot
, class_String
) ) {
511 _type
= QMetaType::QString
;
512 _ptr
= new QString( toString(slot
) );
514 else if( isKindOfSlot( slot
, class_Point
) ) {
515 _type
= QMetaType::QPointF
;
516 _ptr
= new QPointF( toPoint(slot
) );
518 else if( isKindOfSlot( slot
, class_Rect
) ) {
519 _type
= QMetaType::QRectF
;
520 _ptr
= new QRectF( toRect(slot
) );
522 else if( isKindOfSlot( slot
, class_Size
) ) {
523 _type
= QMetaType::QSizeF
;
524 _ptr
= new QSizeF( toSize(slot
) );
526 else if( isKindOfSlot( slot
, class_Color
) ) {
527 _type
= QMetaType::QColor
;
528 _ptr
= new QColor( toColor(slot
) );
530 else if( isKindOfSlot( slot
, class_Array
) || isKindOfSlot( slot
, class_SymbolArray
) ) {
531 _type
= qMetaTypeId
<VariantList
>();
532 _ptr
= new VariantList( toVariantList(slot
) );
534 else if( isKindOfSlot( slot
, class_QObject
) ) {
535 proxy
= toObjectProxy(slot
);
537 _type
= QMetaType::Void
;
541 _type
= qMetaTypeId
<QObjectProxy
*>();
542 _ptr
= new QObjectProxy
*( proxy
);
545 else if( isKindOfSlot( slot
, class_QTreeViewItem
) ) {
546 _type
= qMetaTypeId
<QcTreeWidget::ItemPtr
>();
547 _ptr
= new QcTreeWidget::ItemPtr( toTreeWidgetItem(slot
) );
550 QString className
= Slot::toString( &slotRawObject(slot
)->classptr
->name
);
551 qcWarningMsg(QString("WARNING: Do not know how to use an instance of class '%1'").arg(className
));
552 _type
= QMetaType::Void
;
558 _type
= QMetaType::Double
;
559 _ptr
= new double( toDouble(slot
) );