Skip to content

Commit

Permalink
Generalise preferences, hide impl
Browse files Browse the repository at this point in the history
Will allow us to add more preferences without breaking ABI
  • Loading branch information
dhood committed Apr 6, 2018
1 parent d37f606 commit ff5d88f
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 14 deletions.
43 changes: 43 additions & 0 deletions src/rviz/preferences.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (c) 2018, Open Source Robotics Foundation, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the Willow Garage, Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef RVIZ_PREFERENCES_H
#define RVIZ_PREFERENCES_H

namespace rviz
{

struct Preferences
{
bool prompt_save_on_exit = true;
};

} //namespace rviz

#endif // RVIZ_PREFERENCES_H
9 changes: 5 additions & 4 deletions src/rviz/preferences_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,17 @@

#include "preferences_dialog.h"
#include "rviz/load_resource.h"
#include "rviz/preferences.h"

namespace rviz
{

PreferencesDialog::PreferencesDialog( Factory* factory,
bool* prompt_save_on_exit_output,
Preferences *preferences,
QWidget* parent)
: QDialog( parent )
, factory_( factory )
, prompt_save_on_exit_output_( prompt_save_on_exit_output )
, preferences_( preferences)
{
//***** Layout

Expand All @@ -65,7 +66,7 @@ PreferencesDialog::PreferencesDialog( Factory* factory,
QVBoxLayout* preferences_layout = new QVBoxLayout;
preferences_layout->setAlignment(Qt::AlignLeft | Qt::AlignTop);
prompt_save_on_exit_checkbox_ = new QCheckBox;
prompt_save_on_exit_checkbox_->setChecked(*prompt_save_on_exit_output_);
prompt_save_on_exit_checkbox_->setChecked(preferences_->prompt_save_on_exit);
prompt_save_on_exit_checkbox_->setText(QString( "Prompt Save on Exit?"));
preferences_layout->addWidget( prompt_save_on_exit_checkbox_ );
preferences_box->setLayout( preferences_layout );
Expand Down Expand Up @@ -104,7 +105,7 @@ void PreferencesDialog::accept()
{
if( isValid() )
{
*prompt_save_on_exit_output_ = prompt_save_on_exit_checkbox_->isChecked();
preferences_->prompt_save_on_exit = prompt_save_on_exit_checkbox_->isChecked();
QDialog::accept();
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/rviz/preferences_dialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class QLabel;
namespace rviz
{

class Preferences;

class PreferencesDialog : public QDialog
{
Q_OBJECT
Expand All @@ -64,7 +66,7 @@ Q_OBJECT
* put the display name entered, or NULL (default) if display
* name entry field should not be shown. */
PreferencesDialog( Factory* factory,
bool* prompt_save_on_exit_output,
Preferences* preferences,
QWidget* parent = 0 );

virtual QSize sizeHint () const;
Expand All @@ -84,7 +86,7 @@ public Q_SLOTS:
Factory* factory_;

QCheckBox* prompt_save_on_exit_checkbox_;
bool* prompt_save_on_exit_output_;
Preferences* preferences_;

/** Widget with OK and CANCEL buttons. */
QDialogButtonBox* button_box_;
Expand Down
15 changes: 8 additions & 7 deletions src/rviz/visualization_frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
#include "rviz/help_panel.h"
#include "rviz/loading_dialog.h"
#include "rviz/new_object_dialog.h"
#include "rviz/preferences.h"
#include "rviz/preferences_dialog.h"
#include "rviz/panel_dock_widget.h"
#include "rviz/panel_factory.h"
Expand Down Expand Up @@ -126,7 +127,7 @@ VisualizationFrame::VisualizationFrame( QWidget* parent )
, loading_( false )
, post_load_timer_( new QTimer( this ))
, frame_count_(0)
, prompt_save_on_exit_(true)
, preferences_( new Preferences() )
{
panel_factory_ = new PanelFactory();

Expand Down Expand Up @@ -588,14 +589,14 @@ void VisualizationFrame::onDockPanelVisibilityChange( bool visible )

void VisualizationFrame::openPreferencesDialog()
{
bool prompt_save_on_exit = prompt_save_on_exit_;
Preferences temp_preferences( *preferences_.get() );
PreferencesDialog* dialog = new PreferencesDialog( panel_factory_,
&prompt_save_on_exit,
&temp_preferences,
this );
manager_->stopUpdate();
if( dialog->exec() == QDialog::Accepted ) {
// Apply preferences.
prompt_save_on_exit_ = prompt_save_on_exit;
preferences_ = boost::make_shared<Preferences>( temp_preferences );
}
manager_->startUpdate();
}
Expand Down Expand Up @@ -927,12 +928,12 @@ void VisualizationFrame::savePanels( Config config )

void VisualizationFrame::loadPreferences( const Config& config )
{
config.mapGetBool( "PromptSaveOnExit", &prompt_save_on_exit_ );
config.mapGetBool( "PromptSaveOnExit", &(preferences_->prompt_save_on_exit) );
}

void VisualizationFrame::savePreferences( Config config )
{
config.mapSetValue( "PromptSaveOnExit", prompt_save_on_exit_ );
config.mapSetValue( "PromptSaveOnExit", preferences_->prompt_save_on_exit );
}

bool VisualizationFrame::prepareToExit()
Expand All @@ -944,7 +945,7 @@ bool VisualizationFrame::prepareToExit()

savePersistentSettings();

if( isWindowModified() && prompt_save_on_exit_)
if( isWindowModified() && preferences_->prompt_save_on_exit)
{
QMessageBox box( this );
box.setText( "There are unsaved changes." );
Expand Down
5 changes: 4 additions & 1 deletion src/rviz/visualization_frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#ifndef RVIZ_VISUALIZATION_FRAME_H
#define RVIZ_VISUALIZATION_FRAME_H

#include <boost/shared_ptr.hpp>

#include <QMainWindow>
#include <QList>

Expand All @@ -54,6 +56,7 @@ namespace rviz
{

class PanelFactory;
class Preferences;
class RenderPanel;
class VisualizationManager;
class Tool;
Expand Down Expand Up @@ -307,7 +310,7 @@ protected Q_SLOTS:
std::string last_image_dir_;
std::string home_dir_;

bool prompt_save_on_exit_;
boost::shared_ptr<Preferences> preferences_;

QMenu* file_menu_;
QMenu* recent_configs_menu_;
Expand Down

0 comments on commit ff5d88f

Please sign in to comment.