QcPenPrinter: no need to allocate QPrintDialog on heap
[supercollider.git] / QtCollider / QcWidgetFactory.h
blob61aca95c6235eac213597d0aacfa04094a245265
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 ************************************************************************/
22 #ifndef QC_WIDGET_FACTORY_H
23 #define QC_WIDGET_FACTORY_H
25 #include "QcObjectFactory.h"
26 #include "QWidgetProxy.h"
28 #include <QLayout>
30 template <class QWIDGET>
31 class QcWidgetFactory : public QcObjectFactory<QWIDGET>
33 public:
35 virtual QObjectProxy *newInstance( PyrObject *scObject, QList<QVariant> & arguments ) {
37 int argc = arguments.count();
39 // check if parent arg is valid;
41 QObjectProxy *parentProxy = argc > 0 ? arguments[0].value<QObjectProxy*>() : 0;
43 // create the widget
45 QWIDGET *widget = new QWIDGET();
46 QWidget *w = widget; // template parameter type-safety
48 QWidgetProxy *proxy = new QWidgetProxy ( w, scObject );
50 // set requested geometry
52 QRect r;
53 if( argc > 1 ) r = arguments[1].value<QRect>();
54 if( r.size().isEmpty() ) r.setSize( w->sizeHint() );
56 w->setGeometry( r );
58 // automatically support drag and drop
60 w->setAcceptDrops( true );
62 // do custom initialization
64 initialize( proxy, widget, arguments ); // use template parameter type
66 // set parent
68 QWidget *parent = parentProxy ? qobject_cast<QWidget*>( parentProxy->object() ) : 0;
70 if( parent ) {
72 // Ok, we have the parent, so stuff the child in
74 const QMetaObject *mo = parent->metaObject();
75 bool ok = mo->invokeMethod( parent, "addChild", Q_ARG( QWidget*, w ) );
77 if( !ok ) w->setParent( parent );
79 w->show();
82 return proxy;
85 protected:
87 virtual void initialize( QWidgetProxy *, QWIDGET *, QList<QVariant> & ) {};
90 #endif