SideElement.cc 8.83 KB
Newer Older
1 2 3
/*===========================================================================*\
 *                                                                           *
 *                              OpenFlipper                                  *
Jan Möbius's avatar
Jan Möbius committed
4
 *      Copyright (C) 2001-2010 by Computer Graphics Group, RWTH Aachen      *
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 33 34 35 36
 *                           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/>.                                      *
 *                                                                           *
\*===========================================================================*/

/*===========================================================================*\
 *                                                                           *
37 38 39
 *   $Revision$                                                         *
 *   $Author$                                                      *
 *   $Date$                   *
40 41 42 43
 *                                                                           *
\*===========================================================================*/


Jan Möbius's avatar
Dennis:  
Jan Möbius committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

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

#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QToolButton>
#include <QAction>
#include <QFrame>
#include <QMouseEvent>
#include <QFont>
#include <QDialog>

#include <OpenFlipper/common/GlobalOptions.hh>

#include "SideElement.hh"

//== IMPLEMENTATION ==========================================================

SideElement::SideElement (SideArea *_parent, QWidget *_w, QString _name) :
  parent_ (_parent),
  widget_ (_w),
  name_ (_name),
  active_ (0),
  dialog_ (0)
{
  QFont font;
  font.setBold (false);

  QHBoxLayout *hl = new QHBoxLayout;

75
  SideElement::TopArea *tra = new SideElement::TopArea (this);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
76 77 78 79 80

  label_ = new QLabel (_name);
  label_->setFont (font);

  detachButton_ = new QToolButton ();
81
  detachButton_->setAutoRaise(true);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
82 83 84 85
  hl->addWidget (label_);
  hl->addStretch (1);
  hl->addWidget (detachButton_);

86 87 88 89 90 91 92
  
  QIcon detachIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"button-detach.png");
  detachIcon.addPixmap(QPixmap(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"button-detach_over.png"), QIcon::Active);
  detachIcon.addPixmap(QPixmap(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"button-detach_over.png"), QIcon::Selected);
  
  detachAction_ = new QAction ( detachIcon, "", this);
  detachAction_->setToolTip( tr("Show as separate window") );
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
93 94
  detachAction_->setCheckable (true);
  detachButton_->setDefaultAction (detachAction_);
95
 
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
96 97
  connect (detachAction_, SIGNAL (triggered (bool)), this, SLOT (detachPressed (bool)));

98
  tra->setLayout (hl);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
99 100 101 102 103 104 105

  QFrame *f = new QFrame ();
  f->setFrameShape (QFrame::HLine);

  mainLayout_ = new QVBoxLayout;

  mainLayout_->addWidget (f);
106
  mainLayout_->addWidget (tra);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
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
  mainLayout_->addWidget (_w);
  mainLayout_->setSpacing (0);
  mainLayout_->setContentsMargins (1,1,1,1);
  setLayout (mainLayout_);

  _w->hide ();
}

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

SideElement::~SideElement ()
{
  if (dialog_)
    dialog_->close ();
  widget_->setParent (0);
}

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

void SideElement::labelPress ()
{
  if (dialog_)
  {
    dialog_->raise ();
    dialog_->activateWindow ();
  }
  else
  {
    active_ = !active_;
    if (active_)
      widget_->show ();
    else
      widget_->hide ();

141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
    QFont font;
    font.setBold (active_);
    label_->setFont (font);
  }
}

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

void SideElement::setActive(bool _active)
{
  if ( dialog_ )
  {
    dialog_->raise ();
    dialog_->activateWindow ();
  }
  else
  {
    active_ = _active;
    if (active_)
      widget_->show ();
    else
      widget_->hide ();

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
164 165 166 167 168 169 170 171 172 173 174 175 176
    QFont font;
    font.setBold (active_);
    label_->setFont (font);
  }
}

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

void SideElement::detachPressed (bool checked_)
{
  if (checked_)
  {
    mainLayout_->removeWidget (widget_);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
177
    dialog_ = new QDialog(0, Qt::Window);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
178
    dialog_->setWindowTitle (name_);
179
    dialog_->setWindowIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"button-detach.png"));
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
180 181
    dialog_->setLayout (new QVBoxLayout);
    dialog_->resize (widget_->size ());
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
182 183 184 185 186 187 188 189 190 191
    if (window ())
    {
      int x = (window ()->width () - widget_->width ()) / 2;
      x += window ()->x ();
      x = qMax (0, x);
      int y = (window ()->height () - widget_->height ()) / 2;
      y += window ()->y ();
      y = qMax (0, y);
      dialog_->move (x, y);
    }
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
    dialog_->layout ()->addWidget (widget_);
    dialog_->show ();
    widget_->setAttribute(Qt::WA_DeleteOnClose, false);
    widget_->show ();

    connect (dialog_, SIGNAL(finished (int)), this, SLOT(dialogClosed ()));

    QFont font;
    font.setBold (true);
    font.setItalic (true);
    label_->setFont (font);
  }
  else if (dialog_)
    dialog_->close ();

}

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

void SideElement::dialogClosed ()
{
  dialog_ = 0;
  mainLayout_->addWidget (widget_);
  widget_->setAttribute(Qt::WA_DeleteOnClose, true);

  if (active_)
    widget_->show ();
  else
    widget_->hide ();

  detachAction_->setChecked (false);
  QFont font;
  font.setBold (active_);
  label_->setFont (font);
}

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

void SideElement::saveState (QSettings &_settings)
{
  _settings.beginGroup (name_);
  _settings.setValue ("Active", active_);
  _settings.setValue ("Detached", (dialog_ != 0));
  _settings.setValue ("DialogGeometry", (dialog_) ? dialog_->saveGeometry (): QByteArray());
  _settings.endGroup ();
}

//-----------------------------------------------------------------------------
void SideElement::restoreState (QSettings &_settings)
{
  _settings.beginGroup (name_);

  active_ = _settings.value ("Active", active_).toBool ();

  if (active_)
    widget_->show ();
  else
    widget_->hide ();

  QFont font;
  font.setBold (active_);
  label_->setFont (font);

  if (_settings.value ("Detached", false).toBool () && !dialog_)
    detachPressed (true);

  if (dialog_)
    dialog_->restoreGeometry (_settings.value ("DialogGeometry").toByteArray ());

  _settings.endGroup ();
}

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

SideElement::TopArea::TopArea (SideElement *_e) :
  e_ (_e)
{
}

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

void SideElement::TopArea::mousePressEvent (QMouseEvent *_event)
{
  e_->labelPress ();
  _event->accept ();
}

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