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 
45 //=============================================================================
46 //
47 // CLASS QtMultiViewLayout - IMPLEMENTATION
48 //
49 //=============================================================================
50 
51 //== INCLUDES =================================================================
52 
53 #include "QtMultiViewLayout.hh"
54 
55 //== NAMESPACES ===============================================================
56 
57 
58 //== CLASS IMPLEMENTATION======================================================
59 
60 QtMultiViewLayout::QtMultiViewLayout (QGraphicsLayoutItem * _parent) :
61 QGraphicsLayout (_parent),
62 mode_ (SingleView),
63 spacing_ (2),
64 primary_ (0)
65 {
66  items_[0] = 0;
67  items_[1] = 0;
68  items_[2] = 0;
69  items_[3] = 0;
70 }
71 
72 //-----------------------------------------------------------------------------
73 
74 void QtMultiViewLayout::addItem (QGraphicsWidget *_item, unsigned int _pos)
75 {
76  if (_pos > 3)
77  return;
78  items_[_pos] = _item;
79 }
80 
81 //-----------------------------------------------------------------------------
82 
84 {
85  mode_ = _mode;
86  invalidate();
87 }
88 
89 //-----------------------------------------------------------------------------
90 
91 void QtMultiViewLayout::setPrimary (unsigned int _i)
92 {
93  if ((int) _i > count ())
94  return;
95  primary_ = _i;
96  invalidate();
97 }
98 
99 //-----------------------------------------------------------------------------
100 
101 void QtMultiViewLayout::setSpacing (unsigned int _s)
102 {
103  spacing_ = _s;
104  invalidate();
105 }
106 
107 //-----------------------------------------------------------------------------
108 
110 {
111  int rv = 0;
112 
113  for (int i = 0; i < 4; i++)
114  if (items_[i])
115  ++rv;
116 
117  return rv;
118 }
119 
120 //-----------------------------------------------------------------------------
121 
122 QGraphicsLayoutItem * QtMultiViewLayout::itemAt(int _i) const
123 {
124  if (_i < 0 || _i > 3)
125  return 0;
126  return items_[_i];
127 }
128 
129 //-----------------------------------------------------------------------------
130 
131 void QtMultiViewLayout::removeAt (int _index)
132 {
133  if (_index < 0 || _index > 3)
134  return;
135  items_[_index] = 0;
136 }
137 
138 //-----------------------------------------------------------------------------
139 
140 QSizeF QtMultiViewLayout::sizeHint(Qt::SizeHint /*_which*/, const QSizeF & _constraint) const
141 {
142  return _constraint;
143 }
144 
145 //-----------------------------------------------------------------------------
146 
147 void QtMultiViewLayout::setGeometry(const QRectF & rect)
148 {
149  QGraphicsLayoutItem::setGeometry (rect);
150  reLayout ();
151 }
152 
153 //-----------------------------------------------------------------------------
154 
156 {
157  MultiViewMode mode = mode_;
158  QRectF r = contentsRect ();
159 
160  if (!items_[primary_])
161  return;
162  if (count() != 4)
163  mode = SingleView;
164 
165  if (mode == SingleView)
166  {
167  for (int i = 0; i < 4; i++)
168  items_[i]->hide();
169  items_[primary_]->show ();
170  } else if ( mode == DoubleView) {
171  for (int i = 0; i < 2; i++)
172  if (items_[i])
173  items_[i]->show();
174  for (int i = 2; i < 4; i++)
175  if (items_[i])
176  items_[i]->hide();
177  }else {
178  for (int i = 0; i < 4; i++)
179  if (items_[i])
180  items_[i]->show();
181  }
182 
183  switch (mode)
184  {
185  case SingleView:
186  items_[primary_]->setGeometry (r);
187  break;
188  case DoubleView:
189  {
190  int width = (r.width() - spacing_) / 2;
191  int height = r.height();
192  items_[0]->resize (width, height);
193  items_[1]->resize (width, height);
194 
195  items_[0]->setPos(r.topLeft());
196  items_[1]->setPos(r.x() + width + spacing_, r.y());
197  }
198  break;
199  case Grid:
200  {
201  int width = (r.width() - spacing_) / 2;
202  int height = (r.height() - spacing_) / 2;
203  items_[0]->resize (width, height);
204  items_[1]->resize (r.width() - width - spacing_, height);
205  items_[2]->resize (width, r.height() - height - spacing_);
206  items_[3]->resize (r.width() - width - spacing_,r.height() - height - spacing_);
207  items_[0]->setPos (r.topLeft());
208  items_[1]->setPos (r.x() + width + spacing_, r.y());
209  items_[2]->setPos (r.x(), r.y() + height + spacing_);
210  items_[3]->setPos (r.x() + width + spacing_, r.y() + height + spacing_);
211  }
212  break;
213  case HSplit:
214  {
215  int order[4];
216  order[0] = primary_;
217  for (unsigned int i = 0, j = 1; i < 4; i++)
218  if (i != primary_)
219  order[j++] = i;
220  int width = (r.width() - spacing_) * 3 / 4;
221  int ewidth = r.width() - spacing_ - width;
222  int eheight = (r.height() - (spacing_ * 2)) / 3;
223  items_[order[0]]->resize (width, r.height());
224  items_[order[1]]->resize (ewidth, eheight);
225  items_[order[2]]->resize (ewidth, eheight);
226  items_[order[3]]->resize (ewidth, r.height() - ((eheight + spacing_) * 2));
227  items_[order[0]]->setPos (r.topLeft());
228  items_[order[1]]->setPos (r.x() + width + spacing_, r.y());
229  items_[order[2]]->setPos (r.x() + width + spacing_, r.y() + eheight + spacing_);
230  items_[order[3]]->setPos (r.x() + width + spacing_,
231  r.y() + ((eheight + spacing_) * 2));
232  }
233  break;
234  }
235 }
236 
237 
238 //=============================================================================
239 //=============================================================================
void setPrimary(unsigned int _i)
Sets primary element for SingleView and HSplit.
void setMode(MultiViewMode _mode)
Sets layout mode.
void addItem(QGraphicsWidget *_item, unsigned int _pos)
Adds Widget to Layout.
virtual void setGeometry(const QRectF &rect)
Tracks geometry changes.
void reLayout()
Recalculate layout.
MultiViewMode
MultiView display modes.
virtual int count() const
Pure virtual functions that have to be implemented.
void setSpacing(unsigned int _s)
Sets space between items.