QtMultiViewLayout.cc 8.19 KB
Newer Older
1
/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
 *                                                                            *
 *                              OpenFlipper                                   *
 *      Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen       *
 *                           www.openflipper.org                              *
 *                                                                            *
 *--------------------------------------------------------------------------- *
 *  This file is part of OpenFlipper.                                         *
 *                                                                            *
 *  OpenFlipper is free software: you can redistribute it and/or modify       *
 *  it under the terms of the GNU Lesser General Public License as            *
 *  published by the Free Software Foundation, either version 3 of            *
 *  the License, or (at your option) any later version with the               *
 *  following exceptions:                                                     *
 *                                                                            *
 *  If other files instantiate templates or use macros                        *
 *  or inline functions from this file, or you compile this file and          *
 *  link it with other files to produce an executable, this file does         *
 *  not by itself cause the resulting executable to be covered by the         *
 *  GNU Lesser General Public License. This exception does not however        *
 *  invalidate any other reasons why the executable file might be             *
 *  covered by the GNU Lesser General Public License.                         *
 *                                                                            *
 *  OpenFlipper is distributed in the hope that it will be useful,            *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of            *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
 *  GNU Lesser General Public License for more details.                       *
 *                                                                            *
 *  You should have received a copy of the GNU LesserGeneral Public           *
 *  License along with OpenFlipper. If not,                                   *
 *  see <http://www.gnu.org/licenses/>.                                       *
 *                                                                            *
33 34 35
\*===========================================================================*/

/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
36 37 38 39 40
 *                                                                            *
 *   $Revision$                                                       *
 *   $LastChangedBy$                                                *
 *   $Date$                     *
 *                                                                            *
41
\*===========================================================================*/
Jan Möbius's avatar
Jan Möbius committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61




//=============================================================================
//
//  CLASS QtMultiViewLayout - IMPLEMENTATION
//
//=============================================================================

//== INCLUDES =================================================================

#include "QtMultiViewLayout.hh"

//== NAMESPACES ===============================================================


//== CLASS IMPLEMENTATION======================================================

QtMultiViewLayout::QtMultiViewLayout (QGraphicsLayoutItem * _parent) :
Jan Möbius's avatar
Jan Möbius committed
62 63 64 65
QGraphicsLayout (_parent),
mode_ (SingleView),
spacing_ (2),
primary_ (0)
Jan Möbius's avatar
Jan Möbius committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
{
  items_[0] = 0;
  items_[1] = 0;
  items_[2] = 0;
  items_[3] = 0;
}

//-----------------------------------------------------------------------------

void QtMultiViewLayout::addItem (QGraphicsWidget *_item, unsigned int _pos)
{
  if (_pos > 3)
    return;
  items_[_pos] = _item;
}

//-----------------------------------------------------------------------------

void QtMultiViewLayout::setMode (MultiViewMode _mode)
{
  mode_ = _mode;
  invalidate();
}

//-----------------------------------------------------------------------------

92 93 94 95 96 97 98 99 100 101
void QtMultiViewLayout::setPrimary (unsigned int _i)
{
  if ((int) _i > count ())
    return;
  primary_ = _i;
  invalidate();
}

//-----------------------------------------------------------------------------

Jan Möbius's avatar
Jan Möbius committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
void QtMultiViewLayout::setSpacing (unsigned int _s)
{
  spacing_ = _s;
  invalidate();
}

//-----------------------------------------------------------------------------

int QtMultiViewLayout::count() const
{
  int rv = 0;

  for (int i = 0; i < 4; i++)
    if (items_[i])
      ++rv;

  return rv; 
}

//-----------------------------------------------------------------------------

QGraphicsLayoutItem * QtMultiViewLayout::itemAt(int _i) const
{
  if (_i < 0 || _i > 3)
    return 0;
  return items_[_i];
}

//-----------------------------------------------------------------------------

void QtMultiViewLayout::removeAt (int _index)
{
  if (_index < 0 || _index > 3)
    return;
  items_[_index] = 0;
}

//-----------------------------------------------------------------------------

Jan Möbius's avatar
Jan Möbius committed
141
QSizeF QtMultiViewLayout::sizeHint(Qt::SizeHint /*_which*/, const QSizeF & _constraint) const
Jan Möbius's avatar
Jan Möbius committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
{
  return _constraint;
}

//-----------------------------------------------------------------------------

void QtMultiViewLayout::setGeometry(const QRectF & rect)
{
  QGraphicsLayoutItem::setGeometry (rect);
  reLayout ();
}

//-----------------------------------------------------------------------------

void QtMultiViewLayout::reLayout ()
{
  MultiViewMode mode = mode_;
  QRectF r = contentsRect ();

161
  if (!items_[primary_])
Jan Möbius's avatar
Jan Möbius committed
162 163 164 165 166 167
    return;
  if (count() != 4)
    mode = SingleView;

  if (mode == SingleView)
  {
168
    for (int i = 0; i < 4; i++)
Jan Möbius's avatar
Jan Möbius committed
169
      items_[i]->hide();
170
    items_[primary_]->show ();
Jan Möbius's avatar
Jan Möbius committed
171 172 173 174 175 176 177 178
  } else if ( mode == DoubleView) {
    for (int i = 0; i < 2; i++)
      if (items_[i])
        items_[i]->show();
    for (int i = 2; i < 4; i++)
      if (items_[i])
        items_[i]->hide();
  }else {
179 180 181
    for (int i = 0; i < 4; i++)
      if (items_[i])
        items_[i]->show();
Jan Möbius's avatar
Jan Möbius committed
182 183 184 185 186
  }

  switch (mode)
  {
    case SingleView:
187
      items_[primary_]->setGeometry (r);
Jan Möbius's avatar
Jan Möbius committed
188
      break;
Jan Möbius's avatar
Jan Möbius committed
189 190 191 192 193 194 195 196 197 198 199
    case DoubleView:
    {
      int width  = (r.width() - spacing_) / 2;
      int height = r.height();
      items_[0]->resize (width, height);
      items_[1]->resize (width, height);

      items_[0]->setPos(r.topLeft());
      items_[1]->setPos(r.x() + width + spacing_, r.y());
    }
    break;
Jan Möbius's avatar
Jan Möbius committed
200
    case Grid:
Jan Möbius's avatar
Jan Möbius committed
201 202 203 204 205 206 207 208 209 210 211 212 213
    {
      int width = (r.width() - spacing_) / 2;
      int height = (r.height() - spacing_) / 2;
      items_[0]->resize (width, height);
      items_[1]->resize (r.width() - width - spacing_, height);
      items_[2]->resize (width, r.height() - height - spacing_);
      items_[3]->resize (r.width() - width - spacing_,r.height() - height - spacing_);
      items_[0]->setPos (r.topLeft());
      items_[1]->setPos (r.x() + width + spacing_, r.y());
      items_[2]->setPos (r.x(), r.y() + height + spacing_);
      items_[3]->setPos (r.x() + width + spacing_, r.y() + height + spacing_);
    }
    break;
Jan Möbius's avatar
Jan Möbius committed
214
    case HSplit:
Jan Möbius's avatar
Jan Möbius committed
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
    {
      int order[4];
      order[0] = primary_;
      for (unsigned int i = 0, j = 1; i < 4; i++)
        if (i != primary_)
          order[j++] = i;
      int width = (r.width() - spacing_) * 3 / 4;
      int ewidth = r.width() - spacing_ - width;
      int eheight = (r.height() - (spacing_ * 2)) / 3;
      items_[order[0]]->resize (width, r.height());
      items_[order[1]]->resize (ewidth, eheight);
      items_[order[2]]->resize (ewidth, eheight);
      items_[order[3]]->resize (ewidth, r.height() - ((eheight + spacing_) * 2));
      items_[order[0]]->setPos (r.topLeft());
      items_[order[1]]->setPos (r.x() + width + spacing_, r.y());
      items_[order[2]]->setPos (r.x() + width + spacing_, r.y() + eheight + spacing_);
      items_[order[3]]->setPos (r.x() + width + spacing_,
          r.y() + ((eheight + spacing_) * 2));
    }
    break;
Jan Möbius's avatar
Jan Möbius committed
235 236 237 238 239 240
  }
}


//=============================================================================
//=============================================================================