From: Soeren Apel Date: Mon, 6 Mar 2017 18:04:47 +0000 (+0100) Subject: Implement initial version of the settings management X-Git-Tag: pulseview-0.4.0~172 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=bf9f12687c8d43422455cbdc27ec1cc5d4305149;p=pulseview.git Implement initial version of the settings management --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 3ae7e7d2..547c7c12 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -210,6 +210,7 @@ set(pulseview_SOURCES main.cpp pv/application.cpp pv/devicemanager.cpp + pv/globalsettings.cpp pv/mainwindow.cpp pv/session.cpp pv/storesession.cpp @@ -232,6 +233,7 @@ set(pulseview_SOURCES pv/dialogs/about.cpp pv/dialogs/connect.cpp pv/dialogs/inputoutputoptions.cpp + pv/dialogs/settings.cpp pv/dialogs/storeprogress.cpp pv/popups/deviceoptions.cpp pv/popups/channels.cpp @@ -283,6 +285,7 @@ set(pulseview_SOURCES # This list includes only QObject derived class headers. set(pulseview_HEADERS + pv/globalsettings.hpp pv/mainwindow.hpp pv/session.hpp pv/storesession.hpp @@ -295,6 +298,7 @@ set(pulseview_HEADERS pv/dialogs/about.hpp pv/dialogs/connect.hpp pv/dialogs/inputoutputoptions.hpp + pv/dialogs/settings.hpp pv/dialogs/storeprogress.hpp pv/popups/channels.hpp pv/popups/deviceoptions.hpp diff --git a/pv/dialogs/settings.cpp b/pv/dialogs/settings.cpp new file mode 100644 index 00000000..8cb8d67d --- /dev/null +++ b/pv/dialogs/settings.cpp @@ -0,0 +1,100 @@ +/* + * This file is part of the PulseView project. + * + * Copyright (C) 2017 Soeren Apel + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + */ + +#include "settings.hpp" +#include "pv/globalsettings.hpp" + +#include +#include +#include +#include +#include +#include + +namespace pv { +namespace dialogs { + +Settings::Settings(QWidget *parent) : + QDialog(parent, 0) +{ + QTabWidget *tab_stack = new QTabWidget(this); + tab_stack->addTab(get_view_settings_form(tab_stack), tr("&Views")); + + QDialogButtonBox *button_box = new QDialogButtonBox( + QDialogButtonBox::Ok | QDialogButtonBox::Cancel); + + QVBoxLayout* root_layout = new QVBoxLayout(this); + root_layout->addWidget(tab_stack); + root_layout->addWidget(button_box); + + connect(button_box, SIGNAL(accepted()), this, SLOT(accept())); + connect(button_box, SIGNAL(rejected()), this, SLOT(reject())); +} + +QWidget *Settings::get_view_settings_form(QWidget *parent) const +{ + GlobalSettings settings; + + QWidget *form = new QWidget(parent); + QVBoxLayout *form_layout = new QVBoxLayout(form); + + // Trace view settings + QGroupBox *trace_view_group = new QGroupBox(tr("Trace View")); + form_layout->addWidget(trace_view_group); + + QFormLayout *trace_view_layout = new QFormLayout(); + trace_view_group->setLayout(trace_view_layout); + + QCheckBox *coloured_bg_cb = new QCheckBox(); + coloured_bg_cb->setChecked(settings.value(GlobalSettings::Key_View_ColouredBG).toBool()); + connect(coloured_bg_cb, SIGNAL(stateChanged(int)), this, SLOT(on_view_colouredBG_changed(int))); + trace_view_layout->addRow(tr("Use &coloured trace background"), coloured_bg_cb); + + QCheckBox *always_zoom_to_fit_cb = new QCheckBox(); + always_zoom_to_fit_cb->setChecked(settings.value(GlobalSettings::Key_View_AlwaysZoomToFit).toBool()); + connect(always_zoom_to_fit_cb, SIGNAL(stateChanged(int)), this, SLOT(on_view_alwaysZoomToFit_changed(int))); + trace_view_layout->addRow(tr("Always zoom-to-&fit during capture"), always_zoom_to_fit_cb); + + return form; +} + +void Settings::accept() +{ + QDialog::accept(); +} + +void Settings::reject() +{ + QDialog::reject(); +} + +void Settings::on_view_alwaysZoomToFit_changed(int state) +{ + GlobalSettings settings; + settings.setValue(GlobalSettings::Key_View_AlwaysZoomToFit, state ? true : false); +} + +void Settings::on_view_colouredBG_changed(int state) +{ + GlobalSettings settings; + settings.setValue(GlobalSettings::Key_View_ColouredBG, state ? true : false); +} + +} // namespace dialogs +} // namespace pv diff --git a/pv/dialogs/settings.hpp b/pv/dialogs/settings.hpp new file mode 100644 index 00000000..20c4f915 --- /dev/null +++ b/pv/dialogs/settings.hpp @@ -0,0 +1,48 @@ +/* + * This file is part of the PulseView project. + * + * Copyright (C) 2017 Soeren Apel + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + */ + +#ifndef PULSEVIEW_PV_SETTINGS_HPP +#define PULSEVIEW_PV_SETTINGS_HPP + +#include + +namespace pv { +namespace dialogs { + +class Settings : public QDialog +{ + Q_OBJECT + +public: + Settings(QWidget *parent = 0); + + QWidget *get_view_settings_form(QWidget *parent) const; + + void accept(); + void reject(); + +private Q_SLOTS: + void on_view_alwaysZoomToFit_changed(int state); + void on_view_colouredBG_changed(int state); +}; + +} // namespace dialogs +} // namespace pv + +#endif // PULSEVIEW_PV_SETTINGS_HPP diff --git a/pv/globalsettings.cpp b/pv/globalsettings.cpp new file mode 100644 index 00000000..5da89ceb --- /dev/null +++ b/pv/globalsettings.cpp @@ -0,0 +1,53 @@ +/* + * This file is part of the PulseView project. + * + * Copyright (C) 2017 Soeren Apel + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + */ + +#include "globalsettings.hpp" + +namespace pv { + +const QString GlobalSettings::Key_View_AlwaysZoomToFit = "View_AlwaysZoomToFit"; +const QString GlobalSettings::Key_View_ColouredBG = "View_ColouredBG"; + +std::multimap< QString, std::function > GlobalSettings::callbacks_; + +GlobalSettings::GlobalSettings() : + QSettings() +{ + beginGroup("Settings"); +} + +void GlobalSettings::register_change_handler(const QString key, + std::function cb) +{ + callbacks_.emplace(key, cb); +} + +void GlobalSettings::setValue(const QString &key, const QVariant &value) +{ + QSettings::setValue(key, value); + + // Call all registered callbacks for this key + auto range = callbacks_.equal_range(key); + + for (auto it = range.first; it != range.second; it++) + it->second(value); +} + + +} // namespace pv diff --git a/pv/globalsettings.hpp b/pv/globalsettings.hpp new file mode 100644 index 00000000..b7457963 --- /dev/null +++ b/pv/globalsettings.hpp @@ -0,0 +1,54 @@ +/* + * This file is part of the PulseView project. + * + * Copyright (C) 2017 Soeren Apel + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + */ + +#ifndef PULSEVIEW_GLOBALSETTINGS_HPP +#define PULSEVIEW_GLOBALSETTINGS_HPP + +#include +#include + +#include +#include +#include + +namespace pv { + +class GlobalSettings : public QSettings +{ + Q_OBJECT + +public: + static const QString Key_View_AlwaysZoomToFit; + static const QString Key_View_ColouredBG; + +public: + GlobalSettings(); + + static void register_change_handler(const QString key, + std::function cb); + + void setValue(const QString& key, const QVariant& value); + +private: + static std::multimap< QString, std::function > callbacks_; +}; + +} // namespace pv + +#endif // PULSEVIEW_GLOBALSETTINGS_HPP diff --git a/pv/mainwindow.cpp b/pv/mainwindow.cpp index 33ebdd64..8b650e1c 100644 --- a/pv/mainwindow.cpp +++ b/pv/mainwindow.cpp @@ -39,9 +39,11 @@ #include "mainwindow.hpp" #include "devicemanager.hpp" +#include "globalsettings.hpp" #include "util.hpp" #include "devices/hardwaredevice.hpp" #include "dialogs/about.hpp" +#include "dialogs/settings.hpp" #include "toolbars/mainbar.hpp" #include "view/view.hpp" #include "views/trace/standardbar.hpp" @@ -65,6 +67,9 @@ class ViewItem; using toolbars::MainBar; +using std::bind; +using std::placeholders::_1; + const QString MainWindow::WindowTitle = tr("PulseView"); MainWindow::MainWindow(DeviceManager &device_manager, @@ -392,6 +397,8 @@ void MainWindow::setup_ui() this, SLOT(on_run_stop_clicked())); connect(&session_state_mapper_, SIGNAL(mapped(QObject*)), this, SLOT(on_capture_state_changed(QObject*))); + connect(settings_button_, SIGNAL(clicked(bool)), + this, SLOT(on_settings_clicked())); connect(&session_selector_, SIGNAL(tabCloseRequested(int)), this, SLOT(on_tab_close_requested(int))); @@ -591,6 +598,12 @@ void MainWindow::on_run_stop_clicked() } } +void MainWindow::on_settings_clicked() +{ + dialogs::Settings dlg; + dlg.exec(); +} + void MainWindow::on_session_name_changed() { // Update the corresponding dock widget's name(s) diff --git a/pv/mainwindow.hpp b/pv/mainwindow.hpp index ac098b21..f38ee447 100644 --- a/pv/mainwindow.hpp +++ b/pv/mainwindow.hpp @@ -112,6 +112,7 @@ private Q_SLOTS: void on_new_session_clicked(); void on_run_stop_clicked(); + void on_settings_clicked(); void on_session_name_changed(); void on_capture_state_changed(QObject *obj); diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 069f15f6..05b5cb4b 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -20,6 +20,7 @@ set(pulseview_TEST_SOURCES ${PROJECT_SOURCE_DIR}/pv/devicemanager.cpp + ${PROJECT_SOURCE_DIR}/pv/globalsettings.cpp ${PROJECT_SOURCE_DIR}/pv/session.cpp ${PROJECT_SOURCE_DIR}/pv/storesession.cpp ${PROJECT_SOURCE_DIR}/pv/util.cpp @@ -97,6 +98,7 @@ set(pulseview_TEST_SOURCES # This list includes only QObject derived class headers. set(pulseview_TEST_HEADERS + ${PROJECT_SOURCE_DIR}/pv/globalsettings.hpp ${PROJECT_SOURCE_DIR}/pv/session.hpp ${PROJECT_SOURCE_DIR}/pv/storesession.hpp ${PROJECT_SOURCE_DIR}/pv/binding/device.hpp