1 /************************************************************************
3 * Copyright 2010-2012 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 ************************************************************************/
22 #include "primitives.h"
24 #include "../Common.h"
26 #include "../QcApplication.h"
27 #include "../QObjectProxy.h"
28 #include "../style/style.hpp"
29 #include "QtCollider.h"
32 # include "../hacks/hacks_mac.hpp"
35 #include <PyrKernel.h>
37 #include <QFontMetrics>
38 #include <QDesktopWidget>
39 #include <QFontDatabase>
40 #include <QStyleFactory>
41 #include <QWebSettings>
43 namespace QtCollider
{
45 QC_LANG_PRIMITIVE( QtGUI_SetDebugLevel
, 1, PyrSlot
*r
, PyrSlot
*a
, VMGlobals
*g
)
47 QtCollider::setDebugLevel( Slot::toInt(a
) );
51 QC_LANG_PRIMITIVE( QtGUI_DebugLevel
, 0, PyrSlot
*r
, PyrSlot
*a
, VMGlobals
*g
)
53 SetInt( r
, QtCollider::debugLevel() );
57 QC_LANG_PRIMITIVE( QWindow_ScreenBounds
, 0, PyrSlot
*r
, PyrSlot
*a
, VMGlobals
*g
)
59 if( !QcApplication::compareThread() ) return QtCollider::wrongThreadError();
61 QRect screenGeometry
= QApplication::desktop()->screenGeometry();
62 Slot::setRect( r
, screenGeometry
);
66 QC_LANG_PRIMITIVE( QWindow_AvailableGeometry
, 0, PyrSlot
*r
, PyrSlot
*a
, VMGlobals
*g
)
68 if( !QcApplication::compareThread() ) return QtCollider::wrongThreadError();
70 QRect rect
= QApplication::desktop()->availableGeometry();
71 Slot::setRect( r
, rect
);
75 QC_LANG_PRIMITIVE( Qt_StringBounds
, 2, PyrSlot
*r
, PyrSlot
*a
, VMGlobals
*g
)
77 QString str
= Slot::toString( a
);
79 QFont f
= Slot::toFont( a
+1 );
82 QRect bounds
= fm
.boundingRect( str
);
84 // we keep the font height even on empty string;
85 if( str
.isEmpty() ) bounds
.setHeight( fm
.height() );
87 Slot::setRect( r
, bounds
);
91 QC_LANG_PRIMITIVE( Qt_AvailableFonts
, 0, PyrSlot
*r
, PyrSlot
*a
, VMGlobals
*g
)
93 QFontDatabase database
;
95 Q_FOREACH( QString family
, database
.families() ) {
96 l
.data
<< QVariant(family
);
98 Slot::setVariantList( r
, l
);
102 QC_LANG_PRIMITIVE( Qt_GlobalPalette
, 0, PyrSlot
*r
, PyrSlot
*a
, VMGlobals
*g
)
104 if( !QcApplication::compareThread() ) return QtCollider::wrongThreadError();
106 QPalette
p( QApplication::palette() );
107 Slot::setPalette( r
, p
);
111 QC_LANG_PRIMITIVE( Qt_SetGlobalPalette
, 1, PyrSlot
*r
, PyrSlot
*a
, VMGlobals
*g
)
113 if( !QcApplication::compareThread() ) return QtCollider::wrongThreadError();
115 QPalette p
= Slot::toPalette( a
);
116 QApplication::setPalette( p
);
121 QC_LANG_PRIMITIVE( Qt_FocusWidget
, 0, PyrSlot
*r
, PyrSlot
*a
, VMGlobals
*g
)
123 if( !QcApplication::compareThread() ) return QtCollider::wrongThreadError();
125 QWidget
*w
= QApplication::focusWidget();
128 // On Mac we need to make additional checks, as Qt does not monitor
129 // focus changes to native Cocoa windows in the same application.
130 if( w
&& !QtCollider::Mac::isKeyWindow( w
) )
135 QObjectProxy
*proxy
= QObjectProxy::fromObject(w
);
136 if( proxy
&& proxy
->scObject() ) {
137 SetObject( r
, proxy
->scObject() );
146 QC_LANG_PRIMITIVE( Qt_SetStyle
, 1, PyrSlot
*r
, PyrSlot
*a
, VMGlobals
*g
)
148 if( !QcApplication::compareThread() ) return QtCollider::wrongThreadError();
150 QString str
= Slot::toString( a
);
151 if( str
.isEmpty() ) return errFailed
;
153 QStyle
*style
= QStyleFactory::create( str
);
154 if( !style
) return errFailed
;
156 QApplication::setStyle( new QtCollider::Style::StyleImpl( style
) );
160 QC_LANG_PRIMITIVE( Qt_AvailableStyles
, 0, PyrSlot
*r
, PyrSlot
*a
, VMGlobals
*g
)
162 if( !QcApplication::compareThread() ) return QtCollider::wrongThreadError();
165 Q_FOREACH( QString key
, QStyleFactory::keys() ) {
166 list
.data
<< QVariant(key
);
169 Slot::setVariantList( r
, list
);
173 QC_LANG_PRIMITIVE( QWebView_ClearMemoryCaches
, 0, PyrSlot
*r
, PyrSlot
*a
, VMGlobals
*g
)
175 if( !QcApplication::compareThread() ) return QtCollider::wrongThreadError();
177 QWebSettings::clearMemoryCaches();
182 void defineMiscPrimitives()
184 LangPrimitiveDefiner definer
;
185 definer
.define
<QtGUI_SetDebugLevel
>();
186 definer
.define
<QtGUI_DebugLevel
>();
187 definer
.define
<QWindow_ScreenBounds
>();
188 definer
.define
<QWindow_AvailableGeometry
>();
189 definer
.define
<Qt_StringBounds
>();
190 definer
.define
<Qt_AvailableFonts
>();
191 definer
.define
<Qt_GlobalPalette
>();
192 definer
.define
<Qt_SetGlobalPalette
>();
193 definer
.define
<Qt_FocusWidget
>();
194 definer
.define
<Qt_SetStyle
>();
195 definer
.define
<Qt_AvailableStyles
>();
196 definer
.define
<QWebView_ClearMemoryCaches
>();
199 } // namespace QtCollider