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