RTXI 1.3
plugins/userprefs/userprefs.cpp
Go to the documentation of this file.
00001 /*
00002  Copyright (C) 2011 Georgia Institute of Technology, University of Utah, Weill Cornell Medical College
00003 
00004  This program is free software: you can redistribute it and/or modify
00005  it under the terms of the GNU General Public License as published by
00006  the Free Software Foundation, either version 3 of the License, or
00007  (at your option) any later version.
00008 
00009  This program is distributed in the hope that it will be useful,
00010  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  GNU General Public License for more details.
00013 
00014  You should have received a copy of the GNU General Public License
00015  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016 
00017  */
00018 
00019 /*
00020  * The settings are only written when the panel is closed and the QSettings
00021  * is destructed. So all Reset, Apply, and Cancel buttons all close the panel.
00022  */
00023 
00024 #include <qhbox.h>
00025 #include <qhbuttongroup.h>
00026 #include <qvbuttongroup.h>
00027 #include <qlabel.h>
00028 #include <qlayout.h>
00029 #include <qlineedit.h>
00030 #include <qpushbutton.h>
00031 #include <qfiledialog.h>
00032 #include <debug.h>
00033 #include <main_window.h>
00034 #include <userprefs.h>
00035 
00036 UserPrefs::Panel::Panel(QWidget *parent) :
00037   QWidget(parent, 0, Qt::WStyle_NormalBorder | Qt::WDestructiveClose)
00038 {
00039 
00040   QBoxLayout *layout = new QVBoxLayout(this);
00041 
00042   setCaption("RTXI User Preferences");
00043 
00044   // load QSettings
00045   QSettings userprefs;
00046   userprefs.setPath("RTXI.org", "RTXI", QSettings::User);
00047 
00048   // create GUI elements and set text
00049   QVButtonGroup *dirGroup;
00050   dirGroup = new QVButtonGroup("Default Directories", this);
00051 
00052   QHBox *hbox;
00053   hbox = new QHBox(dirGroup);
00054   (void) (new QLabel("Default settings directory:", hbox))->setFixedWidth(250);
00055   settingsDirEdit = new QLineEdit(hbox);
00056   settingsDirEdit->setFixedWidth(200);
00057   settingsDirEdit->setText(
00058       userprefs.readEntry("/dirs/setfiles", getenv("HOME")));
00059   QPushButton *chooseSettingsDirButton = new QPushButton("Browse", hbox);
00060   QObject::connect(chooseSettingsDirButton,SIGNAL(clicked(void)),this,SLOT(chooseSettingsDir(void)));
00061 
00062   hbox = new QHBox(dirGroup);
00063   (void) (new QLabel("Default DYNAMO models directory:", hbox))->setFixedWidth(
00064       250);
00065   dynamoDirEdit = new QLineEdit(hbox);
00066   dynamoDirEdit->setText(userprefs.readEntry("/dirs/dynamomodels", getenv(
00067       "HOME")));
00068   QPushButton *chooseDynamoDirButton = new QPushButton("Browse", hbox);
00069   QObject::connect(chooseDynamoDirButton,SIGNAL(clicked(void)),this,SLOT(chooseDynamoDir(void)));
00070 
00071   hbox = new QHBox(dirGroup);
00072   (void) (new QLabel("Default HDF5 data directory:", hbox))->setFixedWidth(250);
00073   dataDirEdit = new QLineEdit(hbox);
00074   dataDirEdit->setText(userprefs.readEntry("/dirs/data", getenv("HOME")));
00075   QPushButton *chooseDataDirButton = new QPushButton("Browse", hbox);
00076   QObject::connect(chooseDataDirButton,SIGNAL(clicked(void)),this,SLOT(chooseDataDir(void)));
00077 
00078   layout->addWidget(dirGroup);
00079 
00080   hbox = new QHBox(this);
00081   (void) (new QLabel("HDF Data Recorder Buffer Size (MB):", hbox))->setFixedWidth(250);
00082   HDFBufferEdit = new QLineEdit(hbox);
00083   HDFBufferEdit->setText(QString::number(userprefs.readNumEntry("/system/HDFbuffer", 10)));
00084 
00085   layout->addWidget(hbox);
00086 
00087   hbox = new QHBox(this);
00088 
00089   QPushButton *resetButton = new QPushButton("Reset and Close", hbox);
00090   QObject::connect(resetButton,SIGNAL(clicked(void)),this,SLOT(reset(void)));
00091 
00092   QPushButton *applyButton = new QPushButton("Save and Close", hbox);
00093   QObject::connect(applyButton,SIGNAL(clicked(void)),this,SLOT(apply(void)));
00094 
00095   QPushButton *cancelButton = new QPushButton("Close without Saving", hbox);
00096   QObject::connect(cancelButton,SIGNAL(clicked(void)),this,SLOT(cancel(void)));
00097 
00098   layout->addWidget(hbox);
00099 
00100   show();
00101 
00102   //setActive(true);
00103 }
00104 
00105 UserPrefs::Panel::~Panel(void)
00106 {
00107   Prefs::getInstance()->panel = 0;
00108 }
00109 
00110 void
00111 UserPrefs::Panel::reset(void)
00112 {
00113   settingsDirEdit->setText(getenv("HOME"));
00114   dynamoDirEdit->setText(getenv("HOME"));
00115   dataDirEdit->setText(getenv("HOME"));
00116   HDFBufferEdit->setText(QString::number(10));
00117 
00118   apply();
00119 }
00120 
00121 void
00122 UserPrefs::Panel::apply(void)
00123 {
00124   userprefs.writeEntry("/dirs/setfiles", settingsDirEdit->text());
00125   userprefs.writeEntry("/dirs/dynamomodels", dynamoDirEdit->text());
00126   userprefs.writeEntry("/dirs/data", dataDirEdit->text());
00127   bool ok;
00128   QString buffer = HDFBufferEdit->text();
00129   userprefs.writeEntry("/system/HDFbuffer", buffer.toInt(&ok));
00130 
00131   this->close();
00132 
00133 }
00134 
00135 void
00136 UserPrefs::Panel::cancel(void)
00137 {
00138   this->close();
00139 }
00140 
00141 void
00142 UserPrefs::Panel::chooseSettingsDir(void)
00143 {
00144   QString dir_name = QFileDialog::getExistingDirectory(userprefs.readEntry(
00145       "/dirs/setfiles", getenv("HOME")), MainWindow::getInstance(),
00146       "get existing directory",
00147       "Choose a default directory for settings files", TRUE);
00148   settingsDirEdit->setText(dir_name);
00149 }
00150 
00151 void
00152 UserPrefs::Panel::chooseDynamoDir(void)
00153 {
00154   QString dir_name = QFileDialog::getExistingDirectory(userprefs.readEntry(
00155       "/dirs/setfiles", getenv("HOME")), MainWindow::getInstance(),
00156       "get existing directory", "Choose a default directory for DYNAMO models",
00157       TRUE);
00158   dynamoDirEdit->setText(dir_name);
00159 }
00160 
00161 void
00162 UserPrefs::Panel::chooseDataDir(void)
00163 {
00164   QString dir_name = QFileDialog::getExistingDirectory(userprefs.readEntry(
00165       "/dirs/data", getenv("HOME")), MainWindow::getInstance(),
00166       "get existing directory",
00167       "Choose a default directory for HDF5 data files", TRUE);
00168   dataDirEdit->setText(dir_name);
00169 }
00170 
00171 extern "C" Plugin::Object *
00172 createRTXIPlugin(void *)
00173 {
00174   return UserPrefs::Prefs::getInstance();
00175 }
00176 
00177 UserPrefs::Prefs::Prefs(void) :
00178   panel(0)
00179 {
00180 menuID = MainWindow::getInstance()->createSystemMenuItem("Preferences",this,SLOT(createPrefsPanel(void)));
00181 }
00182 
00183 UserPrefs::Prefs::~Prefs(void)
00184 {
00185   MainWindow::getInstance()->removeSystemMenuItem(menuID);
00186   if (panel)
00187     delete panel;
00188   instance = 0;
00189   panel = 0;
00190 }
00191 
00192 void
00193 UserPrefs::Prefs::createPrefsPanel(void)
00194 {
00195   if (!panel)
00196     panel = new Panel(MainWindow::getInstance()->centralWidget());
00197   panel->show();
00198 }
00199 
00200 static Mutex mutex;
00201 UserPrefs::Prefs *UserPrefs::Prefs::instance = 0;
00202 
00203 UserPrefs::Prefs *
00204 UserPrefs::Prefs::getInstance(void)
00205 {
00206   if (instance)
00207     return instance;
00208 
00209   /*************************************************************************
00210    * Seems like alot of hoops to jump through, but allocation isn't        *
00211    *   thread-safe. So effort must be taken to ensure mutual exclusion.    *
00212    *************************************************************************/
00213 
00214   Mutex::Locker lock(&::mutex);
00215   if (!instance)
00216     instance = new Prefs();
00217 
00218   return instance;
00219 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines