OpenWalnut  1.5.0dev
WSettingAction.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 "WMainWindow.h"
28 #include "WSettingAction.h"
29 
30 WSettingAction::WSettingAction( QObject* parent, std::string settingName, std::string actionName, std::string tooltip, bool defaultValue,
31  bool showRestartInfo, const QKeySequence& shortcut ):
32  QAction( QString::fromStdString( actionName ), parent ),
33  m_settingName( QString::fromStdString( settingName ) ),
34  m_showRestartInfo( showRestartInfo )
35 {
36  // set the user-specified shortcut
37  QList<QKeySequence> shortcuts;
38  shortcuts.push_back( shortcut );
39  setShortcuts( shortcuts );
40 
41  // binary action
42  setCheckable( true );
43 
44  setToolTip( QString::fromStdString( tooltip ) );
45 
46  // set with current value
47  setChecked( WQtGui::getSettings().value( QString::fromStdString( settingName ), defaultValue ).toBool() );
48 
49  // handle the change
50  connect( this, SIGNAL( toggled( bool ) ), this, SLOT( stateChange( bool ) ) );
51 }
52 
54 {
55 }
56 
57 bool WSettingAction::get() const
58 {
59  return isChecked();
60 }
61 
62 void WSettingAction::stateChange( bool state )
63 {
64  // store the value
65  WQtGui::getSettings().setValue( m_settingName, state );
66 
67  // does this setting require restart?
68  if( m_showRestartInfo )
69  {
70  QMessageBox::information( WQtGui::getMainWindow(), QString( "Restart required" ), QString( "This setting is applied after restart." ) );
71  }
72 
73  emit change( state );
74 }
75 
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
WSettingAction(QObject *parent, std::string settingName, std::string actionName, std::string tooltip, bool defaultValue, bool showRestartInfo=false, const QKeySequence &shortcut=0)
Constructs an action which handles a certain setting.
bool get() const
Gets the current state.
virtual ~WSettingAction()
Destructor.
QString m_settingName
The name of the setting handled here.
void stateChange(bool state)
The state has changed by the action.
bool m_showRestartInfo
If true, a change of the setting causes an restart notification dialog.
void change(bool value)
This signal is emitted if this setting changes.