OpenWalnut  1.5.0dev
WSettingMenu.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #include <string>
26 
27 #include <QMessageBox>
28 
29 #include "WMainWindow.h"
30 #include "WQtGui.h"
31 #include "WSettingMenu.h"
32 
33 WSettingMenu::WSettingMenu( QWidget* parent, std::string settingName, std::string menuName, std::string tooltip, unsigned int defaultValue,
34  const QList< QString >& options, bool showRestartInfo ):
35  QMenu( QString::fromStdString( menuName ), parent ),
36  m_settingName( QString::fromStdString( settingName ) ),
37  m_showRestartInfo( showRestartInfo )
38 {
39  m_currentItem = WQtGui::getSettings().value( m_settingName, defaultValue ).toUInt();
40 
41  // setup this menu
42  setToolTip( QString::fromStdString( tooltip ) );
43 
44  QActionGroup* actionGroup = new QActionGroup( parent );
45 
46  // add an action for each option to the menu and the action group
47  unsigned int i = 0;
48  for( QList< QString >::const_iterator iter = options.begin(); iter != options.end(); ++iter )
49  {
50  QAction* action = new QAction( *iter, parent );
51  action->setCheckable( true );
52  action->setData( i );
53  action->setActionGroup( actionGroup );
54  addAction( action );
55 
56  // is this currently active?
57  if( i == m_currentItem )
58  {
59  action->setChecked( true );
60  }
61 
62  ++i;
63  }
64 
65  // get notified about changes
66  connect( actionGroup, SIGNAL( triggered( QAction* ) ), this, SLOT( handleUpdate( QAction* ) ) );
67 }
68 
70 {
71 }
72 
74 {
75  // update setting and emit signal
76  m_currentItem = action->data().toUInt();
78 
79  // does this setting require restart?
80  if( m_showRestartInfo )
81  {
82  QMessageBox::information( WQtGui::getMainWindow(), QString( "Restart required" ), QString( "This setting is applied after restart." ) );
83  }
84 
85  emit change( m_currentItem );
86 }
87 
88 unsigned int WSettingMenu::get() const
89 {
90  return m_currentItem;
91 }
92 
static QSettings & getSettings()
Returns the settings object.
Definition: WQtGui.cpp:394
static WMainWindow * getMainWindow()
Returns the current main window instance or NULL if not existent.
Definition: WQtGui.cpp:88
WSettingMenu(QWidget *parent, std::string settingName, std::string menuName, std::string tooltip, unsigned int defaultValue, const QList< QString > &options, bool showRestartInfo=false)
Constructs a menu with the specified options and automatically handles updates.
unsigned int get() const
Returns the current setting.
unsigned int m_currentItem
Tracks the currently selected option.
Definition: WSettingMenu.h:89
void change(unsigned int index)
Signal getting emitted if the selected option changes.
void handleUpdate(QAction *action)
Handles updates in the option.
virtual ~WSettingMenu()
Destructor.
bool m_showRestartInfo
If true, a change of the setting causes an restart notification dialog.
Definition: WSettingMenu.h:84
QString m_settingName
The name of the setting handled here.
Definition: WSettingMenu.h:79