##// END OF EJS Templates
Settings binding (3)...
Alexandre Leroux -
r432:4682fc2670df
parent child
Show More
@@ -1,34 +1,40
1 #ifndef SCIQLOP_SQPSETTINGSDIALOG_H
1 #ifndef SCIQLOP_SQPSETTINGSDIALOG_H
2 #define SCIQLOP_SQPSETTINGSDIALOG_H
2 #define SCIQLOP_SQPSETTINGSDIALOG_H
3
3
4 #include "Settings/ISqpSettingsBindable.h"
4 #include "Settings/ISqpSettingsBindable.h"
5
5
6 #include <QDialog>
6 #include <QDialog>
7
7
8 namespace Ui {
8 namespace Ui {
9 class SqpSettingsDialog;
9 class SqpSettingsDialog;
10 } // Ui
10 } // Ui
11
11
12 /**
12 /**
13 * @brief The SqpSettingsDialog class represents the dialog in which the parameters of SciQlop are
13 * @brief The SqpSettingsDialog class represents the dialog in which the parameters of SciQlop are
14 * set
14 * set
15 */
15 */
16 class SqpSettingsDialog : public QDialog {
16 class SqpSettingsDialog : public QDialog, public ISqpSettingsBindable {
17 Q_OBJECT
17 Q_OBJECT
18
18
19 public:
19 public:
20 explicit SqpSettingsDialog(QWidget *parent = 0);
20 explicit SqpSettingsDialog(QWidget *parent = 0);
21 virtual ~SqpSettingsDialog() noexcept;
21 virtual ~SqpSettingsDialog() noexcept;
22
22
23 /// @sa ISqpSettingsBindable::loadSettings()
24 void loadSettings() override final;
25
26 /// @sa ISqpSettingsBindable::saveSettings()
27 void saveSettings() const override final;
28
23 /**
29 /**
24 * Registers a widget into the dialog
30 * Registers a widget into the dialog
25 * @param name the name under which the widget will appear in the dialog
31 * @param name the name under which the widget will appear in the dialog
26 * @param widget the widget to register
32 * @param widget the widget to register
27 */
33 */
28 void registerWidget(const QString &name, QWidget *widget) noexcept;
34 void registerWidget(const QString &name, QWidget *widget) noexcept;
29
35
30 private:
36 private:
31 Ui::SqpSettingsDialog *ui;
37 Ui::SqpSettingsDialog *ui;
32 };
38 };
33
39
34 #endif // SCIQLOP_SQPSETTINGSDIALOG_H
40 #endif // SCIQLOP_SQPSETTINGSDIALOG_H
@@ -1,29 +1,66
1 #include "Settings/SqpSettingsDialog.h"
1 #include "Settings/SqpSettingsDialog.h"
2 #include "ui_SqpSettingsDialog.h"
2 #include "ui_SqpSettingsDialog.h"
3
3
4 namespace {
5
6 /**
7 * Performs a bind operation on widgets that can be binded to SciQlop settings
8 * @param widgets
9 * @param bind the bind operation
10 * @sa ISqpSettingsBindable
11 */
12 template <typename BindMethod>
13 void processBind(const QStackedWidget &widgets, BindMethod bind)
14 {
15 auto count = widgets.count();
16 for (auto i = 0; i < count; ++i) {
17 // Performs operation if widget is an ISqpSettingsBindable
18 if (auto sqpSettingsWidget = dynamic_cast<ISqpSettingsBindable *>(widgets.widget(i))) {
19 bind(*sqpSettingsWidget);
20 }
21 }
22 }
23
24 } // namespace
25
4 SqpSettingsDialog::SqpSettingsDialog(QWidget *parent)
26 SqpSettingsDialog::SqpSettingsDialog(QWidget *parent)
5 : QDialog{parent}, ui{new Ui::SqpSettingsDialog}
27 : QDialog{parent}, ui{new Ui::SqpSettingsDialog}
6 {
28 {
7 ui->setupUi(this);
29 ui->setupUi(this);
8
30
9 // Connection to change the current page to the selection of an entry in the list
31 // Connection to change the current page to the selection of an entry in the list
10 connect(ui->listWidget, &QListWidget::currentRowChanged, ui->stackedWidget,
32 connect(ui->listWidget, &QListWidget::currentRowChanged, ui->stackedWidget,
11 &QStackedWidget::setCurrentIndex);
33 &QStackedWidget::setCurrentIndex);
12 }
34 }
13
35
14 SqpSettingsDialog::~SqpSettingsDialog() noexcept
36 SqpSettingsDialog::~SqpSettingsDialog() noexcept
15 {
37 {
16 delete ui;
38 delete ui;
17 }
39 }
40
41 void SqpSettingsDialog::loadSettings()
42 {
43 // Performs load on all widgets that can be binded to SciQlop settings
44 processBind(*ui->stackedWidget,
45 [](ISqpSettingsBindable &bindable) { bindable.loadSettings(); });
46 }
47
48 void SqpSettingsDialog::saveSettings() const
49 {
50 // Performs save on all widgets that can be binded to SciQlop settings
51 processBind(*ui->stackedWidget,
52 [](ISqpSettingsBindable &bindable) { bindable.saveSettings(); });
53 }
54
18 void SqpSettingsDialog::registerWidget(const QString &name, QWidget *widget) noexcept
55 void SqpSettingsDialog::registerWidget(const QString &name, QWidget *widget) noexcept
19 {
56 {
20 auto newItem = new QListWidgetItem{ui->listWidget};
57 auto newItem = new QListWidgetItem{ui->listWidget};
21 newItem->setText(name);
58 newItem->setText(name);
22
59
23 ui->stackedWidget->addWidget(widget);
60 ui->stackedWidget->addWidget(widget);
24
61
25 // Selects widget if it's the first in the dialog
62 // Selects widget if it's the first in the dialog
26 if (ui->listWidget->count() == 1) {
63 if (ui->listWidget->count() == 1) {
27 ui->listWidget->setCurrentItem(newItem);
64 ui->listWidget->setCurrentItem(newItem);
28 }
65 }
29 }
66 }
General Comments 0
You need to be logged in to leave comments. Login now