StackWidget.cc

00001 /*===========================================================================*\
00002  *                                                                           *
00003  *                              OpenFlipper                                  *
00004  *      Copyright (C) 2001-2009 by Computer Graphics Group, RWTH Aachen      *
00005  *                           www.openflipper.org                             *
00006  *                                                                           *
00007  *---------------------------------------------------------------------------*
00008  *  This file is part of OpenFlipper.                                        *
00009  *                                                                           *
00010  *  OpenFlipper is free software: you can redistribute it and/or modify      *
00011  *  it under the terms of the GNU Lesser General Public License as           *
00012  *  published by the Free Software Foundation, either version 3 of           *
00013  *  the License, or (at your option) any later version with the              *
00014  *  following exceptions:                                                    *
00015  *                                                                           *
00016  *  If other files instantiate templates or use macros                       *
00017  *  or inline functions from this file, or you compile this file and         *
00018  *  link it with other files to produce an executable, this file does        *
00019  *  not by itself cause the resulting executable to be covered by the        *
00020  *  GNU Lesser General Public License. This exception does not however       *
00021  *  invalidate any other reasons why the executable file might be            *
00022  *  covered by the GNU Lesser General Public License.                        *
00023  *                                                                           *
00024  *  OpenFlipper is distributed in the hope that it will be useful,           *
00025  *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
00026  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
00027  *  GNU Lesser General Public License for more details.                      *
00028  *                                                                           *
00029  *  You should have received a copy of the GNU LesserGeneral Public          *
00030  *  License along with OpenFlipper. If not,                                  *
00031  *  see <http://www.gnu.org/licenses/>.                                      *
00032  *                                                                           *
00033 \*===========================================================================*/
00034 
00035 /*===========================================================================*\
00036  *                                                                           *
00037  *   $Revision: 8520 $                                                         *
00038  *   $Author: moebius $                                                      *
00039  *   $Date: 2010-02-10 15:56:59 +0100 (Mi, 10. Feb 2010) $                   *
00040  *                                                                           *
00041 \*===========================================================================*/
00042 
00043 
00044 
00045 
00046 //=============================================================================
00047 //
00048 //  CLASS CoreWidget - IMPLEMENTATION
00049 //
00050 //=============================================================================
00051 
00052 
00053 //== INCLUDES =================================================================
00054 
00055 // -------------------- mview
00056 #include "CoreWidget.hh"
00057 
00058 #include <OpenFlipper/common/GlobalOptions.hh>
00059 
00060 //== IMPLEMENTATION ========================================================== 
00061 
00062 StackWidgetInfo::StackWidgetInfo(bool _editable,QString _name,QWidget* _widget) :
00063     editable(_editable),
00064     name(_name),
00065     widget(_widget)
00066 {
00067 }
00068 
00069 void CoreWidget::slotGetStackWidget( QString _name, QWidget*& _widget) {
00070   
00071   for ( uint i = 0 ; i < stackWidgetList_.size(); ++i) {
00072     if ( stackWidgetList_[i].name == _name ) {
00073       _widget = stackWidgetList_[i].widget;
00074       return; 
00075     }
00076   }
00077   
00078   _widget = 0;
00079 }
00080 
00081 void CoreWidget::slotAddStackWidget( QString _name, QWidget* _widget ) {
00082   QWidget* widget;
00083   
00084   slotGetStackWidget( _name, widget );
00085   
00086   if ( widget ) {
00087     emit log(LOGERR,tr("Name already existing"));
00088     return; 
00089   }
00090   
00091   stackedWidget_->addWidget(_widget);
00092   stackWidgetList_.push_back( StackWidgetInfo( true, _name , _widget ) );
00093   
00094   slotUpdateStackMenu();
00095 }
00096 
00097 
00098 void CoreWidget::slotUpdateStackWidget( QString _name, QWidget* _widget ) {
00099    
00100   QWidget* oldWidget = 0;      
00101   uint index = 0;
00102   for ( uint i = 0 ; i < stackWidgetList_.size(); ++i) {
00103     if ( stackWidgetList_[i].name == _name ) {
00104       oldWidget = stackWidgetList_[i].widget;
00105       index = i;
00106       break;
00107     }
00108   }
00109   
00110   if ( oldWidget == 0 ) {
00111     emit log(LOGERR,tr("Did not find widget to update stackwidget"));
00112     return; 
00113   }
00114   
00115   stackedWidget_->removeWidget( oldWidget );
00116   stackWidgetList_.erase( stackWidgetList_.begin() + index );
00117   
00118   stackedWidget_->addWidget(_widget);
00119   stackWidgetList_.push_back( StackWidgetInfo( true, _name , _widget ) );
00120   
00121   slotUpdateStackMenu();
00122 }
00123 
00124 void CoreWidget::slotViewMenuAction( QAction * _action) {
00125   
00126   // Get the object Name from the action
00127   QString objectName = _action->text();
00128   
00129   QWidget* widget = 0;      
00130   for ( uint i = 0 ; i < stackWidgetList_.size(); ++i) {
00131     if ( stackWidgetList_[i].name == objectName ) {
00132       widget = stackWidgetList_[i].widget;
00133       break;
00134     }
00135   }
00136   
00137   if ( widget == 0 ) {
00138     emit log(LOGERR,tr("Cannot set Widget"));
00139     return; 
00140   }
00141   
00142   stackedWidget_->setCurrentWidget ( widget ); 
00143 }
00144 
00145 void CoreWidget::slotUpdateStackMenu() {
00146   // Only create menu if there are more then two widgets
00147   if ( stackWidgetList_.size() > 1 ) {
00148     
00149     if ( stackMenu_ == 0 ) {
00150       stackMenu_ = new QMenu(tr("Views"));
00151       menuBar()->addMenu(stackMenu_ );
00152     }
00153   
00154     
00155     if ( ! stackMenu_->isEmpty() )
00156       stackMenu_->clear();
00157   
00158     // Create an action group for every view
00159     QActionGroup* actionGroup = new QActionGroup( stackMenu_ );
00160     actionGroup->setExclusive( true );
00161     
00162     for ( uint i = 0 ; i < stackWidgetList_.size() ; ++i ) {
00163       QAction* newAction = new QAction(stackWidgetList_[i].name, actionGroup);  
00164       newAction->setText(stackWidgetList_[i].name);
00165     }
00166     
00167     stackMenu_->addActions(actionGroup->actions());   
00168     
00169     // Connect this actiongoup to a slot
00170     connect( actionGroup, SIGNAL( triggered( QAction * ) ), this, SLOT( slotViewMenuAction( QAction * ) ) );
00171   
00172     stackMenu_->show();
00173   } else {
00174     // Only One widget left, so delete the switching menu
00175     if ( stackMenu_ != 0 ) {
00176       
00177       // remove menu from menuBar
00178       menuBar()->removeAction( stackMenu_->menuAction() );
00179       delete stackMenu_;
00180       
00181       stackMenu_ = 0;
00182     }
00183   }
00184 }
00185 
00186 
00187 //=============================================================================

acg pic Project OpenFlipper, ©  Computer Graphics Group, RWTH Aachen. Documentation generated using doxygen .