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 ************************************************************************/
25 #include "../QcObjectFactory.h"
27 #include <QHBoxLayout>
28 #include <QVBoxLayout>
29 #include <QGridLayout>
30 #include <QStackedLayout>
32 template <class LAYOUT
> struct QcLayout
: public LAYOUT
35 VariantList
margins() const { return VariantList(); }
37 void setMargins( const VariantList
&list
) {
38 if( list
.data
.size() < 4 ) return;
40 for( int i
=0; i
<4; ++i
) {
41 m
[i
] = list
.data
[i
].value
<int>();
43 LAYOUT::setContentsMargins( m
[0], m
[1], m
[2], m
[3] );
47 template <class BOXLAYOUT
> class QcBoxLayout
: public QcLayout
<BOXLAYOUT
>
52 QcBoxLayout( const VariantList
& items
) {
53 Q_FOREACH( QVariant v
, items
.data
) {
54 VariantList item
= v
.value
<VariantList
>();
59 void addItem( const VariantList
& item
) {
60 if( item
.data
.size() < 3 ) return;
62 int stretch
= item
.data
[1].toInt();
63 Qt::Alignment alignment
= (Qt::Alignment
) item
.data
[2].toInt();
65 QVariant varObject
= item
.data
[0];
67 if( !varObject
.isValid() ) {
68 BOXLAYOUT::addStretch( stretch
);
72 if( varObject
.canConvert
<int>() ) {
73 int size
= varObject
.toInt();
74 BOXLAYOUT::addSpacing( size
);
78 QObjectProxy
*p
= varObject
.value
<QObjectProxy
*>();
79 if( !p
|| !p
->object() ) return;
81 QWidget
*w
= qobject_cast
<QWidget
*>( p
->object() );
83 BOXLAYOUT::addWidget( w
, stretch
, alignment
);
87 QLayout
*l2
= qobject_cast
<QLayout
*>( p
->object() );
89 BOXLAYOUT::addLayout( l2
, stretch
);
94 void insertItem( const VariantList
& item
) {
95 if( item
.data
.size() < 4 ) return;
97 int index
= item
.data
[1].toInt();
98 int stretch
= item
.data
[2].toInt();
99 Qt::Alignment alignment
= (Qt::Alignment
) item
.data
[3].toInt();
101 QVariant varObject
= item
.data
[0];
103 if( !varObject
.isValid() ) {
104 BOXLAYOUT::insertStretch( index
, stretch
);
108 if( varObject
.canConvert
<int>() ) {
109 int size
= varObject
.toInt();
110 BOXLAYOUT::insertSpacing( index
, size
);
114 QObjectProxy
*p
= varObject
.value
<QObjectProxy
*>();
115 if( !p
|| !p
->object() ) return;
117 QWidget
*w
= qobject_cast
<QWidget
*>( p
->object() );
119 BOXLAYOUT::insertWidget( index
, w
, stretch
, alignment
);
123 QLayout
*l2
= qobject_cast
<QLayout
*>( p
->object() );
125 BOXLAYOUT::insertLayout( index
, l2
, stretch
);
130 void setStretch( QObjectProxy
*p
, int stretch
) {
131 QWidget
*w
= qobject_cast
<QWidget
*>( p
->object() );
133 BOXLAYOUT::setStretchFactor( w
, stretch
);
137 QLayout
*l
= qobject_cast
<QLayout
*>( p
->object() );
139 BOXLAYOUT::setStretchFactor( l
, stretch
);
144 void setAlignment( QObjectProxy
*p
, Qt::Alignment alignment
) {
145 QWidget
*w
= qobject_cast
<QWidget
*>( p
->object() );
147 BOXLAYOUT::setAlignment( w
, alignment
);
151 QLayout
*l
= qobject_cast
<QLayout
*>( p
->object() );
153 BOXLAYOUT::setAlignment( l
, alignment
);
159 class QcHBoxLayout
: public QcBoxLayout
<QHBoxLayout
>
162 Q_PROPERTY( VariantList margins READ margins WRITE setMargins
)
165 Q_INVOKABLE
QcHBoxLayout( const VariantList
&items
): QcBoxLayout
<QHBoxLayout
>(items
) {}
166 Q_INVOKABLE
void addItem( const VariantList
&data
) { QcBoxLayout
<QHBoxLayout
>::addItem(data
); }
167 Q_INVOKABLE
void insertItem( const VariantList
&data
) { QcBoxLayout
<QHBoxLayout
>::insertItem(data
); }
168 Q_INVOKABLE
void setStretch( int index
, int stretch
) {
169 QBoxLayout::setStretch( index
, stretch
);
171 Q_INVOKABLE
void setStretch( QObjectProxy
*p
, int stretch
) {
172 QcBoxLayout
<QHBoxLayout
>::setStretch( p
, stretch
);
174 Q_INVOKABLE
void setAlignment( int i
, int a
) {
175 itemAt(i
)->setAlignment( (Qt::Alignment
) a
);
178 Q_INVOKABLE
void setAlignment( QObjectProxy
*p
, int a
) {
179 QcBoxLayout
<QHBoxLayout
>::setAlignment( p
, (Qt::Alignment
) a
);
183 class QcVBoxLayout
: public QcBoxLayout
<QVBoxLayout
>
186 Q_PROPERTY( VariantList margins READ margins WRITE setMargins
)
189 Q_INVOKABLE
QcVBoxLayout( const VariantList
&items
): QcBoxLayout
<QVBoxLayout
>(items
) {}
190 Q_INVOKABLE
void addItem( const VariantList
&data
) { QcBoxLayout
<QVBoxLayout
>::addItem(data
); }
191 Q_INVOKABLE
void insertItem( const VariantList
&data
) { QcBoxLayout
<QVBoxLayout
>::insertItem(data
); }
192 Q_INVOKABLE
void setStretch( int index
, int stretch
) {
193 QBoxLayout::setStretch( index
, stretch
);
195 Q_INVOKABLE
void setStretch( QObjectProxy
*p
, int stretch
) {
196 QcBoxLayout
<QVBoxLayout
>::setStretch( p
, stretch
);
198 Q_INVOKABLE
void setAlignment( int i
, int a
) {
199 itemAt(i
)->setAlignment( (Qt::Alignment
) a
);
202 Q_INVOKABLE
void setAlignment( QObjectProxy
*p
, int a
) {
203 QcBoxLayout
<QVBoxLayout
>::setAlignment( p
, (Qt::Alignment
) a
);
207 class QcGridLayout
: public QcLayout
<QGridLayout
>
210 Q_PROPERTY( VariantList margins READ margins WRITE setMargins
)
211 Q_PROPERTY( int verticalSpacing READ verticalSpacing WRITE setVerticalSpacing
)
212 Q_PROPERTY( int horizontalSpacing READ horizontalSpacing WRITE setHorizontalSpacing
)
214 Q_INVOKABLE
void addItem( const VariantList
&dataList
);
215 Q_INVOKABLE
void setRowStretch( int row
, int factor
) {
216 QcLayout
<QGridLayout
>::setRowStretch( row
, factor
);
218 Q_INVOKABLE
void setColumnStretch( int column
, int factor
) {
219 QcLayout
<QGridLayout
>::setColumnStretch( column
, factor
);
221 Q_INVOKABLE
void setAlignment( int r
, int c
, int a
) {
222 QLayoutItem
*item
= itemAtPosition(r
,c
);
224 item
->setAlignment( (Qt::Alignment
) a
);
228 Q_INVOKABLE
void setAlignment( QObjectProxy
*p
, int a
) {
229 QWidget
*w
= qobject_cast
<QWidget
*>( p
->object() );
231 QLayout::setAlignment( w
, (Qt::Alignment
) a
);
235 QLayout
*l
= qobject_cast
<QLayout
*>( p
->object() );
237 QLayout::setAlignment( l
, (Qt::Alignment
) a
);
241 Q_INVOKABLE
int minRowHeight( int row
) {
242 return ( row
>= 0 && row
< rowCount() ) ? rowMinimumHeight( row
) : 0;
244 Q_INVOKABLE
int minColumnWidth( int col
) {
245 return ( col
>= 0 && col
< columnCount() ) ? columnMinimumWidth( col
) : 0;
247 Q_INVOKABLE
void setMinRowHeight( int row
, int h
) {
248 setRowMinimumHeight( row
, h
);
250 Q_INVOKABLE
void setMinColumnWidth( int col
, int w
) {
251 setColumnMinimumWidth( col
, w
);