Developer Documentation
StackWidget.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 //
48 // CLASS CoreWidget - IMPLEMENTATION
49 //
50 //=============================================================================
51 
52 
53 //== INCLUDES =================================================================
54 
55 // -------------------- mview
56 #include "CoreWidget.hh"
57 
58 //== IMPLEMENTATION ==========================================================
59 
60 StackWidgetInfo::StackWidgetInfo(bool _editable,QString _name,QWidget* _widget) :
61  editable(_editable),
62  name(_name),
63  widget(_widget)
64 {
65 }
66 
67 void CoreWidget::slotGetStackWidget( QString _name, QWidget*& _widget) {
68 
69  for ( uint i = 0 ; i < stackWidgetList_.size(); ++i) {
70  if ( stackWidgetList_[i].name == _name ) {
71  _widget = stackWidgetList_[i].widget;
72  return;
73  }
74  }
75 
76  _widget = 0;
77 }
78 
79 void CoreWidget::slotAddStackWidget( QString _name, QWidget* _widget ) {
80  QWidget* widget;
81 
82  slotGetStackWidget( _name, widget );
83 
84  if ( widget ) {
85  emit log(LOGERR,tr("Name already existing"));
86  return;
87  }
88 
89  stackedWidget_->addWidget(_widget);
90  stackWidgetList_.push_back( StackWidgetInfo( true, _name , _widget ) );
91 
92  slotUpdateStackMenu();
93 }
94 
95 
96 void CoreWidget::slotUpdateStackWidget( QString _name, QWidget* _widget ) {
97 
98  QWidget* oldWidget = 0;
99  uint index = 0;
100  for ( uint i = 0 ; i < stackWidgetList_.size(); ++i) {
101  if ( stackWidgetList_[i].name == _name ) {
102  oldWidget = stackWidgetList_[i].widget;
103  index = i;
104  break;
105  }
106  }
107 
108  if ( oldWidget == 0 ) {
109  emit log(LOGERR,tr("Did not find widget to update stackwidget"));
110  return;
111  }
112 
113  stackedWidget_->removeWidget( oldWidget );
114  stackWidgetList_.erase( stackWidgetList_.begin() + index );
115 
116  stackedWidget_->addWidget(_widget);
117  stackWidgetList_.push_back( StackWidgetInfo( true, _name , _widget ) );
118 
119  slotUpdateStackMenu();
120 }
121 
122 void CoreWidget::slotViewMenuAction( QAction * _action) {
123 
124  // Get the object Name from the action
125  QString objectName = _action->text();
126 
127  QWidget* widget = 0;
128  for ( uint i = 0 ; i < stackWidgetList_.size(); ++i) {
129  if ( stackWidgetList_[i].name == objectName ) {
130  widget = stackWidgetList_[i].widget;
131  break;
132  }
133  }
134 
135  if ( widget == 0 ) {
136  emit log(LOGERR,tr("Cannot set Widget"));
137  return;
138  }
139 
140  stackedWidget_->setCurrentWidget ( widget );
141 }
142 
144  // Only create menu if there are more then two widgets
145  if ( stackWidgetList_.size() > 1 ) {
146 
147  if ( stackMenu_ == 0 ) {
148  stackMenu_ = new QMenu(tr("Views"));
149  menuBar()->addMenu(stackMenu_ );
150  }
151 
152 
153  if ( ! stackMenu_->isEmpty() )
154  stackMenu_->clear();
155 
156  // Create an action group for every view
157  QActionGroup* actionGroup = new QActionGroup( stackMenu_ );
158  actionGroup->setExclusive( true );
159 
160  for ( uint i = 0 ; i < stackWidgetList_.size() ; ++i ) {
161  QAction* newAction = new QAction(stackWidgetList_[i].name, actionGroup);
162  newAction->setText(stackWidgetList_[i].name);
163  }
164 
165  stackMenu_->addActions(actionGroup->actions());
166 
167  // Connect this actiongoup to a slot
168  connect( actionGroup, SIGNAL( triggered( QAction * ) ), this, SLOT( slotViewMenuAction( QAction * ) ) );
169 
170  stackMenu_->show();
171  } else {
172  // Only One widget left, so delete the switching menu
173  if ( stackMenu_ != 0 ) {
174 
175  // remove menu from menuBar
176  menuBar()->removeAction( stackMenu_->menuAction() );
177  delete stackMenu_;
178 
179  stackMenu_ = 0;
180  }
181  }
182 }
183 
184 
185 //=============================================================================
void slotViewMenuAction(QAction *_action)
QMenu containing the recently opened files.
Definition: StackWidget.cc:122
const QString & name()
return the name
Definition: SideElement.cc:298
void slotUpdateStackMenu()
QMenu containing the recently opened files.
Definition: StackWidget.cc:143
QWidget const * widget()
returns the pointer to the plugin tool widget
Definition: SideElement.cc:310
void slotUpdateStackWidget(QString _name, QWidget *_widget)
QMenu containing the recently opened files.
Definition: StackWidget.cc:96
void slotAddStackWidget(QString _name, QWidget *_widget)
QMenu containing the recently opened files.
Definition: StackWidget.cc:79
void slotGetStackWidget(QString _name, QWidget *&_widget)
QMenu containing the recently opened files.
Definition: StackWidget.cc:67