Merge pull request #506 from andrewcsmith/patch-2
[supercollider.git] / QtCollider / layouts / classic_layouts.hpp
blob3ec642d7c899dadef70ba64bdf88fec48a89e43e
1 /************************************************************************
3 * Copyright 2010-2011 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_CLASSIC_LAYOUTS_H
23 #define QC_CLASSIC_LAYOUTS_H
25 #include <QWidget>
26 #include <QResizeEvent>
27 #include "../Common.h"
29 namespace QtCollider
32 enum HSizePolicy {
33 StickLeft = 1,
34 HStretch,
35 StickRight
38 enum VSizePolicy {
39 StickTop = 1,
40 VStretch,
41 StickBottom
44 static VSizePolicy vSizePolicy( QWidget *w )
46 QVariant var = w->property( "_qc_vSizePolicy" );
47 if( !var.isValid() ) return StickTop;
48 return (VSizePolicy) var.toInt();
51 static HSizePolicy hSizePolicy( QWidget *w )
53 QVariant var = w->property( "_qc_hSizePolicy" );
54 if( !var.isValid() ) return StickLeft;
55 return (HSizePolicy) var.toInt();
59 class DefaultLayout
61 public:
62 DefaultLayout( QWidget * w ): parent(w), initialized(false) {}
64 void resize( QResizeEvent *e )
66 QRect r;
67 r.setSize(e->size());
69 if( !initialized ) {
70 initialized = true;
71 geom = r;
72 return;
75 QPoint dPos = r.topLeft() - geom.topLeft();
76 QSize dSize = r.size() - geom.size();
78 const QObjectList &children = parent->children();
80 Q_FOREACH( QObject *o, children )
82 if(!o->isWidgetType())
83 continue;
85 QWidget *child = static_cast<QWidget*>(o);
87 QRect g = child->geometry();
88 int x = g.x();
89 int y = g.y();
90 int w = g.width();
91 int h = g.height();
93 if( !dPos.isNull() ) {
94 x += dPos.x();
95 y += dPos.y();
98 if( !dSize.isNull() ) {
99 if( hSizePolicy(child) == QtCollider::StickRight )
100 x += dSize.width();
101 if( vSizePolicy(child) == QtCollider::StickBottom )
102 y += dSize.height();
103 if( hSizePolicy(child) == QtCollider::HStretch )
104 w += dSize.width();
105 if( vSizePolicy(child) == QtCollider::VStretch )
106 h += dSize.height();
109 child->setGeometry( QRect(x, y, w, h) );
112 geom = r;
115 private:
116 QWidget *parent;
117 bool initialized;
118 QRect geom;
121 class HLayout
123 public:
124 HLayout( QWidget * w ): parent(w) {}
126 void resize( QResizeEvent *e )
128 QRect geom;
129 geom.setSize(e->size());
131 const QObjectList &children = parent->children();
132 int varWidth = geom.width();
133 int i = 0;
135 Q_FOREACH( QObject *o, children )
137 if(!o->isWidgetType()) continue;
138 QWidget *w = static_cast<QWidget*>(o);
140 if( hSizePolicy(w) == QtCollider::HStretch ) {
141 ++i;
143 else {
144 QRect r = w->geometry();
145 varWidth -= r.width();
146 if( varWidth < 0 )
147 break;
151 int partWidth = i > 0 && varWidth > 0 ? varWidth / i : 0;
152 int x = 0;
154 Q_FOREACH( QObject *o, children )
156 if(!o->isWidgetType()) continue;
157 QWidget *w = static_cast<QWidget*>(o);
159 QRect r = w->geometry();
160 r.setHeight( geom.height() );
161 r.moveTo( x, geom.top() );
162 if( hSizePolicy(w) == QtCollider::HStretch )
163 r.setWidth( partWidth );
164 x += r.width();
166 w->setGeometry( r );
170 private:
171 QWidget *parent;
174 class VLayout
176 public:
177 VLayout( QWidget * w ): parent(w) {}
179 void resize( QResizeEvent *e )
181 QRect geom;
182 geom.setSize(e->size());
184 const QObjectList &children = parent->children();
185 int varHeight = geom.height();
186 int i = 0;
188 Q_FOREACH( QObject *o, children )
190 if(!o->isWidgetType()) continue;
191 QWidget *w = static_cast<QWidget*>(o);
193 if( vSizePolicy(w) == QtCollider::VStretch ) {
194 ++i;
196 else {
197 QRect r = w->geometry();
198 varHeight -= r.height();
199 if( varHeight < 0 )
200 break;
204 int partHeight = i > 0 && varHeight > 0 ? varHeight / i : 0;
205 int y = 0;
207 Q_FOREACH( QObject *o, children )
209 if(!o->isWidgetType()) continue;
210 QWidget *w = static_cast<QWidget*>(o);
212 QRect r = w->geometry();
213 r.setWidth( geom.width() );
214 r.moveTo( geom.left(), y );
215 if( vSizePolicy(w) == QtCollider::VStretch )
216 r.setHeight( partHeight );
217 y += r.height();
219 w->setGeometry( r );
223 private:
224 QWidget *parent;
227 } // namespace QtCollider
229 #endif // QC_CLASSIC_LAYOUTS_H