Developer Documentation
QtMultiViewLayout.cc
1 /*===========================================================================*\
2  * *
3  * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39  * *
40 \*===========================================================================*/
41 
42 /*===========================================================================*\
43  * *
44  * $Revision$ *
45  * $LastChangedBy$ *
46  * $Date$ *
47  * *
48 \*===========================================================================*/
49 
50 
51 
52 
53 //=============================================================================
54 //
55 // CLASS QtMultiViewLayout - IMPLEMENTATION
56 //
57 //=============================================================================
58 
59 //== INCLUDES =================================================================
60 
61 #include "QtMultiViewLayout.hh"
62 
63 //== NAMESPACES ===============================================================
64 
65 
66 //== CLASS IMPLEMENTATION======================================================
67 
68 QtMultiViewLayout::QtMultiViewLayout (QGraphicsLayoutItem * _parent) :
69 QGraphicsLayout (_parent),
70 mode_ (SingleView),
71 spacing_ (2),
72 primary_ (0)
73 {
74  items_[0] = 0;
75  items_[1] = 0;
76  items_[2] = 0;
77  items_[3] = 0;
78 }
79 
80 //-----------------------------------------------------------------------------
81 
82 void QtMultiViewLayout::addItem (QGraphicsWidget *_item, unsigned int _pos)
83 {
84  if (_pos > 3)
85  return;
86  items_[_pos] = _item;
87 }
88 
89 //-----------------------------------------------------------------------------
90 
92 {
93  mode_ = _mode;
94  invalidate();
95 }
96 
97 //-----------------------------------------------------------------------------
98 
99 void QtMultiViewLayout::setPrimary (unsigned int _i)
100 {
101  if ((int) _i > count ())
102  return;
103  primary_ = _i;
104  invalidate();
105 }
106 
107 //-----------------------------------------------------------------------------
108 
109 void QtMultiViewLayout::setSpacing (unsigned int _s)
110 {
111  spacing_ = _s;
112  invalidate();
113 }
114 
115 //-----------------------------------------------------------------------------
116 
118 {
119  int rv = 0;
120 
121  for (int i = 0; i < 4; i++)
122  if (items_[i])
123  ++rv;
124 
125  return rv;
126 }
127 
128 //-----------------------------------------------------------------------------
129 
130 QGraphicsLayoutItem * QtMultiViewLayout::itemAt(int _i) const
131 {
132  if (_i < 0 || _i > 3)
133  return 0;
134  return items_[_i];
135 }
136 
137 //-----------------------------------------------------------------------------
138 
139 void QtMultiViewLayout::removeAt (int _index)
140 {
141  if (_index < 0 || _index > 3)
142  return;
143  items_[_index] = 0;
144 }
145 
146 //-----------------------------------------------------------------------------
147 
148 QSizeF QtMultiViewLayout::sizeHint(Qt::SizeHint /*_which*/, const QSizeF & _constraint) const
149 {
150  return _constraint;
151 }
152 
153 //-----------------------------------------------------------------------------
154 
155 void QtMultiViewLayout::setGeometry(const QRectF & rect)
156 {
157  QGraphicsLayoutItem::setGeometry (rect);
158  reLayout ();
159 }
160 
161 //-----------------------------------------------------------------------------
162 
164 {
165  MultiViewMode mode = mode_;
166  QRectF r = contentsRect ();
167 
168  if (!items_[primary_])
169  return;
170  if (count() != 4)
171  mode = SingleView;
172 
173  if (mode == SingleView)
174  {
175  for (int i = 0; i < 4; i++)
176  items_[i]->hide();
177  items_[primary_]->show ();
178  } else if ( mode == DoubleView) {
179  for (int i = 0; i < 2; i++)
180  if (items_[i])
181  items_[i]->show();
182  for (int i = 2; i < 4; i++)
183  if (items_[i])
184  items_[i]->hide();
185  }else {
186  for (int i = 0; i < 4; i++)
187  if (items_[i])
188  items_[i]->show();
189  }
190 
191  switch (mode)
192  {
193  case SingleView:
194  items_[primary_]->setGeometry (r);
195  break;
196  case DoubleView:
197  {
198  int width = (r.width() - spacing_) / 2;
199  int height = r.height();
200  items_[0]->resize (width, height);
201  items_[1]->resize (width, height);
202 
203  items_[0]->setPos(r.topLeft());
204  items_[1]->setPos(r.x() + width + spacing_, r.y());
205  }
206  break;
207  case Grid:
208  {
209  int width = (r.width() - spacing_) / 2;
210  int height = (r.height() - spacing_) / 2;
211  items_[0]->resize (width, height);
212  items_[1]->resize (r.width() - width - spacing_, height);
213  items_[2]->resize (width, r.height() - height - spacing_);
214  items_[3]->resize (r.width() - width - spacing_,r.height() - height - spacing_);
215  items_[0]->setPos (r.topLeft());
216  items_[1]->setPos (r.x() + width + spacing_, r.y());
217  items_[2]->setPos (r.x(), r.y() + height + spacing_);
218  items_[3]->setPos (r.x() + width + spacing_, r.y() + height + spacing_);
219  }
220  break;
221  case HSplit:
222  {
223  int order[4];
224  order[0] = primary_;
225  for (unsigned int i = 0, j = 1; i < 4; i++)
226  if (i != primary_)
227  order[j++] = i;
228  int width = (r.width() - spacing_) * 3 / 4;
229  int ewidth = r.width() - spacing_ - width;
230  int eheight = (r.height() - (spacing_ * 2)) / 3;
231  items_[order[0]]->resize (width, r.height());
232  items_[order[1]]->resize (ewidth, eheight);
233  items_[order[2]]->resize (ewidth, eheight);
234  items_[order[3]]->resize (ewidth, r.height() - ((eheight + spacing_) * 2));
235  items_[order[0]]->setPos (r.topLeft());
236  items_[order[1]]->setPos (r.x() + width + spacing_, r.y());
237  items_[order[2]]->setPos (r.x() + width + spacing_, r.y() + eheight + spacing_);
238  items_[order[3]]->setPos (r.x() + width + spacing_,
239  r.y() + ((eheight + spacing_) * 2));
240  }
241  break;
242  }
243 }
244 
245 
246 //=============================================================================
247 //=============================================================================
void addItem(QGraphicsWidget *_item, unsigned int _pos)
Adds Widget to Layout.
virtual int count() const
Pure virtual functions that have to be implemented.
void setPrimary(unsigned int _i)
Sets primary element for SingleView and HSplit.
void setMode(MultiViewMode _mode)
Sets layout mode.
MultiViewMode
MultiView display modes.
void setSpacing(unsigned int _s)
Sets space between items.
void reLayout()
Recalculate layout.
virtual void setGeometry(const QRectF &rect)
Tracks geometry changes.