Settings: Prettify the settings dialog
[pulseview.git] / pv / dialogs / settings.cpp
1 /*
2  * This file is part of the PulseView project.
3  *
4  * Copyright (C) 2017 Soeren Apel <soeren@apelpie.net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "settings.hpp"
21 #include "pv/globalsettings.hpp"
22
23 #include <QCheckBox>
24 #include <QDialogButtonBox>
25 #include <QFormLayout>
26 #include <QGroupBox>
27 #include <QHBoxLayout>
28 #include <QVBoxLayout>
29
30 namespace pv {
31 namespace dialogs {
32
33 Settings::Settings(QWidget *parent) :
34         QDialog(parent, nullptr)
35 {
36         const int icon_size = 64;
37
38         page_list = new QListWidget;
39         page_list->setViewMode(QListView::IconMode);
40         page_list->setIconSize(QSize(icon_size, icon_size));
41         page_list->setMovement(QListView::Static);
42         page_list->setMaximumWidth(icon_size + icon_size/2);
43         page_list->setSpacing(12);
44
45         pages = new QStackedWidget;
46         create_pages();
47
48         QHBoxLayout *tab_layout = new QHBoxLayout;
49         tab_layout->addWidget(page_list);
50         tab_layout->addWidget(pages, Qt::AlignLeft);
51
52         QDialogButtonBox *button_box = new QDialogButtonBox(
53                 QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
54
55         QVBoxLayout* root_layout = new QVBoxLayout(this);
56         root_layout->addLayout(tab_layout);
57         root_layout->addWidget(button_box);
58
59         connect(button_box, SIGNAL(accepted()), this, SLOT(accept()));
60         connect(button_box, SIGNAL(rejected()), this, SLOT(reject()));
61         connect(page_list, SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)),
62                 this, SLOT(on_page_changed(QListWidgetItem*, QListWidgetItem*)));
63
64         // Start to record changes
65         GlobalSettings settings;
66         settings.start_tracking();
67 }
68
69 void Settings::create_pages()
70 {
71         // View page
72         pages->addWidget(get_view_settings_form(pages));
73
74         QListWidgetItem *viewButton = new QListWidgetItem(page_list);
75         viewButton->setIcon(QIcon(":/icons/sigrok-logo-notext.svg"));
76         viewButton->setText(tr("Views"));
77         viewButton->setTextAlignment(Qt::AlignHCenter);
78         viewButton->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
79 }
80
81 QWidget *Settings::get_view_settings_form(QWidget *parent) const
82 {
83         GlobalSettings settings;
84
85         QWidget *form = new QWidget(parent);
86         QVBoxLayout *form_layout = new QVBoxLayout(form);
87
88         // Trace view settings
89         QGroupBox *trace_view_group = new QGroupBox(tr("Trace View"));
90         form_layout->addWidget(trace_view_group);
91
92         QFormLayout *trace_view_layout = new QFormLayout();
93         trace_view_group->setLayout(trace_view_layout);
94
95         QCheckBox *coloured_bg_cb = new QCheckBox();
96         coloured_bg_cb->setChecked(settings.value(GlobalSettings::Key_View_ColouredBG).toBool());
97         connect(coloured_bg_cb, SIGNAL(stateChanged(int)), this, SLOT(on_view_colouredBG_changed(int)));
98         trace_view_layout->addRow(tr("Use &coloured trace background"), coloured_bg_cb);
99
100         QCheckBox *always_zoom_to_fit_cb = new QCheckBox();
101         always_zoom_to_fit_cb->setChecked(settings.value(GlobalSettings::Key_View_AlwaysZoomToFit).toBool());
102         connect(always_zoom_to_fit_cb, SIGNAL(stateChanged(int)), this, SLOT(on_view_alwaysZoomToFit_changed(int)));
103         trace_view_layout->addRow(tr("Always zoom-to-&fit during capture"), always_zoom_to_fit_cb);
104
105         return form;
106 }
107
108 void Settings::accept()
109 {
110         GlobalSettings settings;
111         settings.stop_tracking();
112
113         QDialog::accept();
114 }
115
116 void Settings::reject()
117 {
118         GlobalSettings settings;
119         settings.undo_tracked_changes();
120
121         QDialog::reject();
122 }
123
124 void Settings::on_page_changed(QListWidgetItem *current, QListWidgetItem *previous)
125 {
126         if (!current)
127                 current = previous;
128
129         pages->setCurrentIndex(page_list->row(current));
130 }
131
132 void Settings::on_view_alwaysZoomToFit_changed(int state)
133 {
134         GlobalSettings settings;
135         settings.setValue(GlobalSettings::Key_View_AlwaysZoomToFit, state ? true : false);
136 }
137
138 void Settings::on_view_colouredBG_changed(int state)
139 {
140         GlobalSettings settings;
141         settings.setValue(GlobalSettings::Key_View_ColouredBG, state ? true : false);
142 }
143
144 } // namespace dialogs
145 } // namespace pv