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

//== 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 ==========================================================

Dirk Wilden's avatar
Dirk Wilden committed
63
SideElement::SideElement (SideArea *_parent, QWidget *_w, QString _name, QIcon* _icon) :
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
64 65 66
  parent_ (_parent),
  widget_ (_w),
  name_ (_name),
Dirk Wilden's avatar
Dirk Wilden committed
67
  icon_ (_icon),
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
68 69 70 71 72 73 74 75
  active_ (0),
  dialog_ (0)
{
  QFont font;
  font.setBold (false);

  QHBoxLayout *hl = new QHBoxLayout;

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

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

Dirk Wilden's avatar
Dirk Wilden committed
81 82 83 84 85 86 87 88 89 90 91
  iconHolder_ = new QLabel ();

  if (icon_ != 0)
    iconHolder_->setPixmap( icon_->pixmap(22,22) );
  else{
    QPixmap pic(QSize(22,22));
    pic.fill( QColor(0,0,0,0) );

    iconHolder_->setPixmap( pic );
  }

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
92
  detachButton_ = new QToolButton ();
93
  detachButton_->setAutoRaise(true);
Dirk Wilden's avatar
Dirk Wilden committed
94
  hl->addWidget (iconHolder_);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
95 96 97 98
  hl->addWidget (label_);
  hl->addStretch (1);
  hl->addWidget (detachButton_);

99

100 101 102
  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);
103

104 105
  detachAction_ = new QAction ( detachIcon, "", this);
  detachAction_->setToolTip( tr("Show as separate window") );
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
106 107
  detachAction_->setCheckable (true);
  detachButton_->setDefaultAction (detachAction_);
108

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
109 110
  connect (detachAction_, SIGNAL (triggered (bool)), this, SLOT (detachPressed (bool)));

111
  tra->setLayout (hl);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
112 113 114 115 116 117 118

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

  mainLayout_ = new QVBoxLayout;

  mainLayout_->addWidget (f);
119
  mainLayout_->addWidget (tra);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
120 121 122 123 124 125 126 127 128 129 130 131
  mainLayout_->addWidget (_w);
  mainLayout_->setSpacing (0);
  mainLayout_->setContentsMargins (1,1,1,1);
  setLayout (mainLayout_);

  _w->hide ();
}

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

SideElement::~SideElement ()
{
132 133
  if (dialog_) {
    disconnect(dialog_,SIGNAL(finished(int)),this,SLOT(dialogClosed()));
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
134
    dialog_->close ();
135
  }
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
  widget_->setParent (0);
}

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

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

156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
    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
179 180 181 182 183 184 185 186 187 188 189 190 191
    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
192
    dialog_ = new QDialog(0, Qt::Window);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
193
    dialog_->setWindowTitle (name_);
194
    dialog_->setWindowIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"button-detach.png"));
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
195 196
    dialog_->setLayout (new QVBoxLayout);
    dialog_->resize (widget_->size ());
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
197 198 199 200 201 202 203 204 205 206
    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
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
    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_);
248
  _settings.setValue ("Active", active());
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
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
  _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 ();
}

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

Dirk Wilden's avatar
Dirk Wilden committed
281 282 283 284 285
const QString& SideElement::name(){
  return name_;
}

//-----------------------------------------------------------------------------
286

287 288 289 290 291 292
bool SideElement::active(){
  return widget_->isVisible();
}

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

293 294 295 296 297 298
QWidget const * SideElement::widget() {
  return widget_;
}

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

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
SideElement::TopArea::TopArea (SideElement *_e) :
  e_ (_e)
{
}

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

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

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