RTXI  3.0.0
The Real-Time eXperiment Interface Reference Manual
userprefs.cpp
Go to the documentation of this file.
1 /*
2  The Real-Time eXperiment Interface (RTXI)
3  Copyright (C) 2011 Georgia Institute of Technology, University of Utah,
4  Will Cornell Medical College
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 3 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 
21 /*
22  * The settings are only written when the panel is closed and the QSettings
23  * is destructed. So all Reset, Apply, and Cancel buttons all close the panel.
24  */
25 
26 #include <QFileDialog>
27 
28 #include "userprefs.hpp"
29 
30 #include "debug.hpp"
31 #include "main_window.hpp"
32 
34  : Widgets::Plugin(ev_manager, std::string(UserPrefs::MODULE_NAME))
35 {
36 }
37 
38 UserPrefs::Panel::Panel(QMainWindow* mwindow, Event::Manager* ev_manager)
39  : Widgets::Panel(std::string(UserPrefs::MODULE_NAME), mwindow, ev_manager)
40  , status(new QLabel)
41  , dirGroup(new QGroupBox)
42  , HDF(new QGroupBox)
43  , buttons(new QGroupBox)
44  , settingsDirEdit(new QLineEdit(dirGroup))
45  , dataDirEdit(new QLineEdit(dirGroup))
46  , HDFBufferEdit(new QLineEdit(HDF))
47 {
48  // Preferences structure
49  const QSettings user_preferences;
50  QSettings::setPath(QSettings::NativeFormat,
51  QSettings::SystemScope,
52  "/usr/local/share/rtxi/");
53 
54  // Main layout
55  auto* box_layout = new QVBoxLayout;
56 
57  // Create child widget and layout
58  auto* dirLayout = new QGridLayout;
59 
60  // Create elements for directory paths
61  const QString env_var = QString::fromLocal8Bit(qgetenv("HOME"));
62  settingsDirEdit->setText(
63  user_preferences.value("/dirs/setfiles", env_var).toString());
64  dirLayout->addWidget(
65  new QLabel(tr("Default settings directory:")), 0, 0, 1, 1);
66  dirLayout->addWidget(settingsDirEdit, 0, 1, 1, 1);
67  auto* chooseSettingsDirButton = new QPushButton("Browse", this);
68  dirLayout->addWidget(chooseSettingsDirButton, 0, 2, 1, 2);
69  QObject::connect(chooseSettingsDirButton,
70  SIGNAL(released()),
71  this,
72  SLOT(chooseSettingsDir()));
73 
74  dataDirEdit->setText(
75  user_preferences.value("/dirs/data", env_var).toString());
76  dirLayout->addWidget(
77  new QLabel(tr("Default HDF5 data directory:")), 1, 0, 1, 1);
78  dirLayout->addWidget(dataDirEdit, 1, 1, 1, 1);
79  auto* chooseDataDirButton = new QPushButton("Browse", this);
80  dirLayout->addWidget(chooseDataDirButton, 1, 2, 1, 2);
81  QObject::connect(
82  chooseDataDirButton, SIGNAL(released()), this, SLOT(chooseDataDir()));
83 
84  // Attach layout to group
85  dirGroup->setLayout(dirLayout);
86 
87  // Create new child widget and layout
88  auto* hdfLayout = new QGridLayout;
89 
90  // Create elements for child widget
91  hdfLayout->addWidget(
92  new QLabel(tr("HDF Data Recorder Buffer Size (MB):")), 0, 0, 1, 1);
93  hdfLayout->addWidget(HDFBufferEdit, 0, 1, 1, 1);
94  HDFBufferEdit->setText(
95  QString::number(user_preferences.value("/system/HDFbuffer", 10).toInt()));
96 
97  // Attach child to parent
98  HDF->setLayout(hdfLayout);
99 
100  // Create new child widget
101  auto* buttonLayout = new QHBoxLayout;
102 
103  // Create elements for child widget
104  auto* resetButton = new QPushButton("Reset");
105  QObject::connect(resetButton, SIGNAL(released()), this, SLOT(reset()));
106  auto* applyButton = new QPushButton("Save");
107  QObject::connect(applyButton, SIGNAL(released()), this, SLOT(apply()));
108  auto* cancelButton = new QPushButton("Close");
109  QObject::connect(
110  cancelButton, SIGNAL(released()), parentWidget(), SLOT(close()));
111 
112  status->setText("Defaults \nloaded");
113  // NOLINTNEXTLINE
114  status->setFrameStyle(QFrame::Panel | QFrame::Sunken);
115  status->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
116 
117  // Assign elements to layout in child widget
118  buttonLayout->addWidget(resetButton);
119  buttonLayout->addWidget(applyButton);
120  buttonLayout->addWidget(cancelButton);
121  buttonLayout->addWidget(status);
122 
123  // Assign layout to child widget
124  buttons->setLayout(buttonLayout);
125 
126  // Attach child widget to parent widget
127  box_layout->addWidget(dirGroup);
128  box_layout->addWidget(HDF);
129  box_layout->addWidget(buttons);
130 
131  // Attach layout to widget
132  setLayout(box_layout);
133  setWindowTitle(QString(this->getName().c_str()));
134 
135  // Set layout to Mdi
136  this->getMdiWindow()->setFixedSize(500, this->sizeHint().height() + 50);
137 }
138 
140 {
141  const QString env_var = QString::fromLocal8Bit(qgetenv("HOME"));
142  settingsDirEdit->setText(env_var);
143  dataDirEdit->setText(env_var);
144  HDFBufferEdit->setText(QString::number(10));
145  userprefs.setValue("/dirs/setfiles", settingsDirEdit->text());
146  userprefs.setValue("/dirs/data", dataDirEdit->text());
147  bool ok = false;
148  const QString buffer = HDFBufferEdit->text();
149  userprefs.setValue("/system/HDFbuffer", buffer.toInt(&ok));
150  status->setText("Preferences \nreset");
151 }
152 
154 {
155  userprefs.setValue("/dirs/setfiles", settingsDirEdit->text());
156  userprefs.setValue("/dirs/data", dataDirEdit->text());
157  bool ok = false;
158  const QString buffer = HDFBufferEdit->text();
159  userprefs.setValue("/system/HDFbuffer", buffer.toInt(&ok));
160  status->setText("Preferences \napplied");
161 }
162 
164 {
165  const QString env_var = QString::fromLocal8Bit(qgetenv("HOME"));
166  const QString dir_name = QFileDialog::getExistingDirectory(
167  this,
168  tr("Choose default directory for settings files"),
169  userprefs.value("/dirs/setfiles", env_var).toString(),
170  QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
171  settingsDirEdit->setText(dir_name);
172 }
173 
175 {
176  const QString env_var = QString::fromLocal8Bit(qgetenv("HOME"));
177  const QString dir_name = QFileDialog::getExistingDirectory(
178  this,
179  tr("Choose default directory for HDF5 data files"),
180  userprefs.value("/dirs/data", env_var).toString(),
181  QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
182  dataDirEdit->setText(dir_name);
183 }
184 
185 std::unique_ptr<Widgets::Plugin> UserPrefs::createRTXIPlugin(
186  Event::Manager* ev_manager)
187 {
188  return std::make_unique<UserPrefs::Plugin>(ev_manager);
189 }
190 
191 Widgets::Panel* UserPrefs::createRTXIPanel(QMainWindow* main_window,
192  Event::Manager* ev_manager)
193 {
194  return static_cast<Widgets::Panel*>(
195  new UserPrefs::Panel(main_window, ev_manager));
196 }
197 
198 std::unique_ptr<Widgets::Component> UserPrefs::createRTXIComponent(
199  Widgets::Plugin* /*host_plugin*/)
200 {
201  return {nullptr};
202 }
203 
205 {
210  return fact;
211 }
void chooseSettingsDir()
Definition: userprefs.cpp:163
void chooseDataDir()
Definition: userprefs.cpp:174
Panel(QMainWindow *mwindow, Event::Manager *ev_manager)
Definition: userprefs.cpp:38
Plugin(Event::Manager *ev_manager)
Definition: userprefs.cpp:33
QMdiSubWindow * getMdiWindow()
Definition: widgets.hpp:286
std::string getName()
Definition: widgets.hpp:422
constexpr std::string_view MODULE_NAME
Definition: connector.hpp:35
std::unique_ptr< Widgets::Component > createRTXIComponent(Widgets::Plugin *host_plugin)
Definition: userprefs.cpp:198
Widgets::Panel * createRTXIPanel(QMainWindow *main_window, Event::Manager *ev_manager)
Definition: userprefs.cpp:191
Widgets::FactoryMethods getFactories()
Definition: userprefs.cpp:204
std::unique_ptr< Widgets::Plugin > createRTXIPlugin(Event::Manager *ev_manager)
Definition: userprefs.cpp:185
constexpr std::string_view MODULE_NAME
Definition: userprefs.hpp:31
Definition: rt.hpp:35
std::unique_ptr< Widgets::Plugin >(* createPlugin)(Event::Manager *)
Function that returns a smart pointer to plugin object.
Definition: widgets.hpp:145
Widgets::Panel *(* createPanel)(QMainWindow *, Event::Manager *)
Definition: widgets.hpp:170
std::unique_ptr< Widgets::Component >(* createComponent)(Widgets::Plugin *)
Definition: widgets.hpp:156