RTXI  2.4
The Real-Time eXperiment Interface Documentation
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, Weill Cornell Medical College
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18 */
19 
20 /*
21  * The settings are only written when the panel is closed and the QSettings
22  * is destructed. So all Reset, Apply, and Cancel buttons all close the panel.
23  */
24 
25 #include <debug.h>
26 #include <main_window.h>
27 #include <userprefs.h>
28 
29 UserPrefs::Panel::Panel(QWidget *parent) : QWidget(parent)
30 {
31 
32  // Make Mdi
33  subWindow = new QMdiSubWindow;
34  subWindow->setWindowIcon(QIcon("/usr/local/share/rtxi/RTXI-widget-icon.png"));
35  subWindow->setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint |
36  Qt::WindowMinimizeButtonHint);
37  subWindow->setAttribute(Qt::WA_DeleteOnClose);
38  MainWindow::getInstance()->createMdi(subWindow);
39 
40  // Preferences structure
41  QSettings userprefs;
42  userprefs.setPath(QSettings::NativeFormat, QSettings::SystemScope, "/usr/local/share/rtxi/");
43 
44  // Main layout
45  QVBoxLayout *layout = new QVBoxLayout;
46 
47  // Create child widget and layout
48  dirGroup = new QGroupBox;
49  QGridLayout *dirLayout = new QGridLayout;
50 
51  // Create elements for directory paths
52  settingsDirEdit = new QLineEdit(dirGroup);
53  settingsDirEdit->setText(userprefs.value("/dirs/setfiles", getenv("HOME")).toString());
54  dirLayout->addWidget(new QLabel(tr("Default settings directory:")), 0, 0, 1, 1);
55  dirLayout->addWidget(settingsDirEdit, 0, 1, 1, 1);
56  QPushButton *chooseSettingsDirButton = new QPushButton("Browse", this);
57  dirLayout->addWidget(chooseSettingsDirButton, 0, 2, 1, 2);
58  QObject::connect(chooseSettingsDirButton,SIGNAL(released(void)),this,SLOT(chooseSettingsDir(void)));
59 
60  dataDirEdit = new QLineEdit(dirGroup);
61  dataDirEdit->setText(userprefs.value("/dirs/data", getenv("HOME")).toString());
62  dirLayout->addWidget(new QLabel(tr("Default HDF5 data directory:")), 1, 0, 1, 1);
63  dirLayout->addWidget(dataDirEdit, 1, 1, 1, 1);
64  QPushButton *chooseDataDirButton = new QPushButton("Browse", this);
65  dirLayout->addWidget(chooseDataDirButton, 1, 2, 1, 2);
66  QObject::connect(chooseDataDirButton,SIGNAL(released(void)),this,SLOT(chooseDataDir(void)));
67 
68  // Attach layout to group
69  dirGroup->setLayout(dirLayout);
70 
71  // Create new child widget and layout
72  HDF = new QGroupBox;
73  QGridLayout *hdfLayout = new QGridLayout;
74 
75  // Create elements for child widget
76  HDFBufferEdit = new QLineEdit(HDF);
77  hdfLayout->addWidget(new QLabel(tr("HDF Data Recorder Buffer Size (MB):")), 0, 0, 1, 1);
78  hdfLayout->addWidget(HDFBufferEdit, 0, 1, 1, 1);
79  HDFBufferEdit->setText(QString::number(userprefs.value("/system/HDFbuffer", 10).toInt()));
80 
81  // Attach child to parent
82  HDF->setLayout(hdfLayout);
83 
84  // Create new child widget
85  buttons = new QGroupBox;
86  QHBoxLayout *buttonLayout = new QHBoxLayout;
87 
88  // Create elements for child widget
89  QPushButton *resetButton = new QPushButton("Reset");
90  QObject::connect(resetButton,SIGNAL(released(void)),this,SLOT(reset(void)));
91  QPushButton *applyButton = new QPushButton("Save");
92  QObject::connect(applyButton,SIGNAL(released(void)),this,SLOT(apply(void)));
93  QPushButton *cancelButton = new QPushButton("Close");
94  QObject::connect(cancelButton,SIGNAL(released(void)),subWindow,SLOT(close(void)));
95  status = new QLabel;
96  status->setText("Defaults \nloaded");
97  status->setFrameStyle(QFrame::Panel | QFrame::Sunken);
98  status->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
99 
100  // Assign elements to layout in child widget
101  buttonLayout->addWidget(resetButton);
102  buttonLayout->addWidget(applyButton);
103  buttonLayout->addWidget(cancelButton);
104  buttonLayout->addWidget(status);
105 
106  // Assign layout to child widget
107  buttons->setLayout(buttonLayout);
108 
109  // Attach child widget to parent widget
110  layout->addWidget(dirGroup);
111  layout->addWidget(HDF);
112  layout->addWidget(buttons);
113 
114  // Attach layout to widget
115  setLayout(layout);
116  setWindowTitle("User Preferences");
117 
118  // Set layout to Mdi
119  subWindow->setWidget(this);
120  subWindow->setFixedSize(500,subWindow->sizeHint().height());
121  show();
122 }
123 
125 {
126  Prefs::getInstance()->panel = 0;
127 }
128 
130 {
131  settingsDirEdit->setText(getenv("HOME"));
132  dataDirEdit->setText(getenv("HOME"));
133  HDFBufferEdit->setText(QString::number(10));
134  userprefs.setValue("/dirs/setfiles", settingsDirEdit->text());
135  userprefs.setValue("/dirs/data", dataDirEdit->text());
136  bool ok;
137  QString buffer = HDFBufferEdit->text();
138  userprefs.setValue("/system/HDFbuffer", buffer.toInt(&ok));
139  status->setText("Preferences \nreset");
140 }
141 
143 {
144  userprefs.setValue("/dirs/setfiles", settingsDirEdit->text());
145  userprefs.setValue("/dirs/data", dataDirEdit->text());
146  bool ok;
147  QString buffer = HDFBufferEdit->text();
148  userprefs.setValue("/system/HDFbuffer", buffer.toInt(&ok));
149  status->setText("Preferences \napplied");
150 }
151 
153 {
154  QString dir_name = QFileDialog::getExistingDirectory(this, tr("Choose default directory for settings files"),
155  userprefs.value("/dirs/setfiles", getenv("HOME")).toString(),
156  QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
157  settingsDirEdit->setText(dir_name);
158 }
159 
161 {
162  QString dir_name = QFileDialog::getExistingDirectory(this, tr("Choose default directory for HDF5 data files"),
163  userprefs.value("/dirs/data", getenv("HOME")).toString(),
164  QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
165  dataDirEdit->setText(dir_name);
166 }
167 
168 extern "C" Plugin::Object * createRTXIPlugin(void *)
169 {
171 }
172 
173 UserPrefs::Prefs::Prefs(void) : panel(0)
174 {
175  MainWindow::getInstance()->createSystemMenuItem("Preferences",this,SLOT(createPrefsPanel(void)));
176 }
177 
178 UserPrefs::Prefs::~Prefs(void)
179 {
180  if (panel)
181  delete panel;
182  instance = 0;
183  panel = 0;
184 }
185 
187 {
188  if (!panel)
189  panel = new Panel(MainWindow::getInstance()->centralWidget());
190  panel->show();
191 }
192 
193 static Mutex mutex;
194 UserPrefs::Prefs *UserPrefs::Prefs::instance = 0;
195 
198 {
199  if (instance)
200  return instance;
201 
202  /*************************************************************************
203  * Seems like alot of hoops to jump through, but allocation isn't *
204  * thread-safe. So effort must be taken to ensure mutual exclusion. *
205  *************************************************************************/
206 
207  Mutex::Locker lock(&::mutex);
208  if (!instance)
209  instance = new Prefs();
210 
211  return instance;
212 }
MainWindow::createSystemMenuItem
QAction * createSystemMenuItem(const QString &label, const QObject *handler, const char *slot)
Definition: main_window.cpp:253
Plugin::Object
Definition: plugin.h:145
MainWindow::createMdi
void createMdi(QMdiSubWindow *)
Definition: main_window.cpp:230
userprefs.h
UserPrefs::Panel
Definition: userprefs.h:58
UserPrefs::Panel::chooseSettingsDir
void chooseSettingsDir(void)
Definition: userprefs.cpp:152
UserPrefs::Prefs::createPrefsPanel
void createPrefsPanel(void)
Definition: userprefs.cpp:186
Mutex
Definition: mutex.h:28
UserPrefs::Panel::chooseDataDir
void chooseDataDir(void)
Definition: userprefs.cpp:160
UserPrefs::Panel::~Panel
virtual ~Panel(void)
Definition: userprefs.cpp:124
UserPrefs::Panel::Panel
Panel(QWidget *)
Definition: userprefs.cpp:29
Mutex::Locker
Definition: mutex.h:36
MainWindow::getInstance
static MainWindow * getInstance(void)
Definition: main_window.cpp:454
createRTXIPlugin
Plugin::Object * createRTXIPlugin(void *)
Definition: userprefs.cpp:168
UserPrefs::Panel::apply
void apply(void)
Definition: userprefs.cpp:142
UserPrefs::Prefs::getInstance
static Prefs * getInstance(void)
Definition: userprefs.cpp:197
debug.h
UserPrefs::Panel::status
QLabel * status
Definition: userprefs.h:66
UserPrefs::Panel::reset
void reset(void)
Definition: userprefs.cpp:129
main_window.h
UserPrefs::Prefs
Definition: userprefs.h:32