Skip to content

Commit

Permalink
Widgets can save/restore settings
Browse files Browse the repository at this point in the history
  • Loading branch information
sguionni committed Jul 16, 2024
1 parent 8146b23 commit 0ba555a
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 53 deletions.
4 changes: 2 additions & 2 deletions lib/ui/qt/include/qt/actions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ namespace VTX::UI::QT

namespace Action
{
struct ShowToolbarText : public DescAction
struct ShowToolBarText : public DescAction
{
ShowToolbarText()
ShowToolBarText()
{
name = "Show toolbar text";
tip = "Show/hide text beside tool buttons";
Expand Down
13 changes: 5 additions & 8 deletions lib/ui/qt/include/qt/application.hpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
#ifndef __VTX_UI_QT_APPLICATION__
#define __VTX_UI_QT_APPLICATION__

#include "settings.hpp"
#include "widget/main_window.hpp"
#include <QApplication>
#include <QPointer>
#include <QSettings>
#include <QSplashScreen>
#include <QTimer>
#include <app/filesystem.hpp>
#include <ui/base_application.hpp>
#include <util/chrono.hpp>

namespace VTX::UI::QT
{
inline QSettings SETTINGS
= QSettings( QString::fromStdString( App::Filesystem::getConfigIniFile().string() ), QSettings::IniFormat );

class Application final : public UI::BaseApplication<Widget::MainWindow>, QApplication
class Application final : public UI::BaseApplication<Widget::MainWindow>, public QApplication, public Savable
{
public:
Application();
Expand All @@ -28,6 +24,9 @@ namespace VTX::UI::QT
// Check exception in Qt events.
bool notify( QObject * const, QEvent * const ) override;

void save() override;
void restore() override;

protected:
// Override BaseApplication.
void _start() override;
Expand All @@ -39,8 +38,6 @@ namespace VTX::UI::QT
QPointer<QSplashScreen> _qSplashScreen;

void _loadTheme();
void _saveSettings();
void _restoreSettings();
};
} // namespace VTX::UI::QT

Expand Down
6 changes: 0 additions & 6 deletions lib/ui/qt/include/qt/base_widget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@
#include <QGuiApplication>
#include <QScreen>
#include <QWidget>
#include <iostream>
#include <map>
#include <string>
#include <syncstream>
#include <thread>
#include <typeinfo>
#include <util/logger.hpp>

namespace VTX::UI::QT
Expand Down
14 changes: 11 additions & 3 deletions lib/ui/qt/include/qt/dock_widget/options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <QCheckBox>
#include <QDockWidget>
#include <QGroupBox>
#include <QToolButton>
#include <QVBoxLayout>

namespace VTX::UI::QT::DockWidget
Expand All @@ -27,17 +28,24 @@ namespace VTX::UI::QT::DockWidget
auto * layoutInterface = new QVBoxLayout( widget );

// Use action?
auto * aToolbarText = ACTION<Action::ShowToolbarText>();
auto * aToolbarText = ACTION<Action::ShowToolBarText>();
aToolbarText->setCheckable( true );
aToolbarText->setChecked( true );
QObject::connect(
aToolbarText,
&QAction::triggered,
[ aToolbarText ]() { VTX_INFO( "Show toolbar text: {}", aToolbarText->isChecked() ); }
);
widget->addAction( aToolbarText );

// layoutInterface->addWidget( aToolbarText );
auto * cbToolbarText = new QCheckBox( aToolbarText->text(), gbInterface );

auto * tbToolbarText = new QToolButton( gbInterface );
tbToolbarText->setDefaultAction( aToolbarText );
tbToolbarText->setToolButtonStyle( Qt::ToolButtonTextBesideIcon );
tbToolbarText->setCheckable( true );

layoutInterface->addWidget( tbToolbarText );

gbInterface->setLayout( layoutInterface );
layout->addWidget( gbInterface );

Expand Down
40 changes: 40 additions & 0 deletions lib/ui/qt/include/qt/settings.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#ifndef __VTX_UI_QT_SETTINGS__
#define __VTX_UI_QT_SETTINGS__

#include <QSettings>
#include <app/filesystem.hpp>
#include <vector>

namespace VTX::UI::QT
{
class Savable;

// TODO: set accessible for tools.
class Settings : public QSettings
{
public:
Settings();
virtual ~Settings() {}

inline void add( Savable * const p_savable ) { _savables.push_back( p_savable ); }

void save();
void restore() const;

private:
std::vector<Savable *> _savables;
};

inline Settings SETTINGS;

class Savable
{
public:
Savable() { SETTINGS.add( this ); }

virtual void save() = 0;
virtual void restore() = 0;
};
} // namespace VTX::UI::QT

#endif
10 changes: 7 additions & 3 deletions lib/ui/qt/include/qt/widget/main_window.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#ifndef __VTX_UI_QT_WIDGET_MAIN_WINDOW__
#define __VTX_UI_QT_WIDGET_MAIN_WINDOW__

#include "opengl_widget.hpp"
#include "qt/base_widget.hpp"
#include "qt/helper.hpp"
#include "opengl_widget.hpp"
#include "qt/settings.hpp"
#include "status_bar.hpp"
#include "ui/concepts.hpp"
#include <QMainWindow>
Expand All @@ -16,7 +17,7 @@
namespace VTX::UI::QT::Widget
{

class MainWindow : public BaseWidget<MainWindow, QMainWindow>
class MainWindow : public BaseWidget<MainWindow, QMainWindow>, public Savable
{
public:
MainWindow();
Expand Down Expand Up @@ -89,6 +90,9 @@ namespace VTX::UI::QT::Widget
return dockWidget;
}

void save() override;
void restore() override;

private:
QPointer<OpenGLWidget> _openGLWidget;
QPointer<StatusBar> _statusBar;
Expand All @@ -100,6 +104,6 @@ namespace VTX::UI::QT::Widget
App::Core::Input::InputManager & _inputManager;
};

} // namespace VTX::UI::QT
} // namespace VTX::UI::QT::Widget

#endif
35 changes: 6 additions & 29 deletions lib/ui/qt/src/qt/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include <QFile>
#include <QIcon>
#include <app/infos.hpp>
#include <util/enum.hpp>

namespace VTX::UI::QT
{
Expand Down Expand Up @@ -78,7 +77,7 @@ namespace VTX::UI::QT
try
{
// Restore settings after main window is built.
_restoreSettings();
SETTINGS.restore();
}
catch ( const std::exception & e )
{
Expand Down Expand Up @@ -106,7 +105,7 @@ namespace VTX::UI::QT

try
{
_saveSettings();
SETTINGS.save();
}
catch ( const std::exception & e )
{
Expand Down Expand Up @@ -135,6 +134,10 @@ namespace VTX::UI::QT
_timer.stop();
}

void Application::save() {}

void Application::restore() {}

void Application::_loadTheme()
{
using namespace Resources;
Expand Down Expand Up @@ -186,30 +189,4 @@ namespace VTX::UI::QT
setPalette( p );
}

void Application::_saveSettings()
{
VTX_INFO( "Saving settings: {}", SETTINGS.fileName().toStdString() );
SETTINGS.setValue( "geometry", _mainWindow->saveGeometry() );
SETTINGS.setValue( "windowState", _mainWindow->saveState() );

if ( SETTINGS.status() != QSettings::NoError )
{
throw std::runtime_error( fmt::format( "{}", Util::Enum::enumName( SETTINGS.status() ) ) );
}

SETTINGS.sync();
}

void Application::_restoreSettings()
{
if ( SETTINGS.status() != QSettings::NoError )
{
throw std::runtime_error( fmt::format( "{}", Util::Enum::enumName( SETTINGS.status() ) ) );
}

VTX_INFO( "Restoring settings: {}", SETTINGS.fileName().toStdString() );
_mainWindow->restoreGeometry( SETTINGS.value( "geometry" ).toByteArray() );
_mainWindow->restoreState( SETTINGS.value( "windowState" ).toByteArray() );
}

} // namespace VTX::UI::QT
2 changes: 1 addition & 1 deletion lib/ui/qt/src/qt/menu/theme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace VTX::UI::QT::Menu
void Theme::_resetLayout()
{
WIDGET<Widget::MainWindow>()->resetLayout();
VTX_DEBUG( "Layout reseted" );
VTX_INFO( "Layout reseted" );
}

} // namespace VTX::UI::QT::Menu
45 changes: 45 additions & 0 deletions lib/ui/qt/src/qt/settings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <qt/settings.hpp>
#include <util/enum.hpp>
#include <util/logger.hpp>

namespace VTX::UI::QT
{

Settings::Settings() :
QSettings( QString::fromStdString( App::Filesystem::getConfigIniFile().string() ), QSettings::IniFormat )
{
}

void Settings::save()
{
VTX_INFO( "Saving settings: {}", fileName().toStdString() );

for ( auto & savable : _savables )
{
savable->save();
}

if ( status() != QSettings::NoError )
{
throw std::runtime_error( fmt::format( "{}", Util::Enum::enumName( status() ) ) );
}

sync();
}

void Settings::restore() const
{
VTX_INFO( "Restoring settings: {}", fileName().toStdString() );

if ( status() != QSettings::NoError )
{
throw std::runtime_error( fmt::format( "{}", Util::Enum::enumName( status() ) ) );
}

for ( auto & savable : _savables )
{
savable->restore();
}
}

} // namespace VTX::UI::QT
18 changes: 17 additions & 1 deletion lib/ui/qt/src/qt/widget/main_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ namespace VTX::UI::QT::Widget
resize( 1920, 1080 );

// Set all settings.
setToolButtonStyle( Qt::ToolButtonTextUnderIcon );
setDockNestingEnabled( false );
setAnimated( true );
setUnifiedTitleAndToolBarOnMac( true );
Expand Down Expand Up @@ -144,4 +143,21 @@ namespace VTX::UI::QT::Widget
p_event->accept();
QApplication::quit();
}

void MainWindow::save()
{
SETTINGS.setValue( "geometry", saveGeometry() );
SETTINGS.setValue( "windowState", saveState() );
SETTINGS.setValue( "showToolBarText", toolButtonStyle() == Qt::ToolButtonTextUnderIcon );
}

void MainWindow::restore()
{
restoreGeometry( SETTINGS.value( "geometry" ).toByteArray() );
restoreState( SETTINGS.value( "windowState" ).toByteArray() );
setToolButtonStyle(
SETTINGS.value( "showToolBarText", true ).toBool() ? Qt::ToolButtonTextUnderIcon : Qt::ToolButtonIconOnly
);
}

} // namespace VTX::UI::QT::Widget

0 comments on commit 0ba555a

Please sign in to comment.