USGS

Isis 3.0 Object Programmers' Reference

Home

MainWindow.cpp
1 #include "MainWindow.h"
2 
3 #include "IException.h"
4 #include "IString.h"
5 
6 namespace Isis {
15  MainWindow::MainWindow(QString title, QWidget *parent, Qt::WFlags flags) :
16  QMainWindow(parent, flags) {
17  setWindowTitle(title);
18  }
19 
20 
25  if (isVisible())
26  close();
27  }
28 
29 
36  void MainWindow::closeEvent(QCloseEvent *event) {
37  writeSettings();
39  }
40 
41 
42  QString MainWindow::settingsFileName(QString objectTitle) {
43  if (QApplication::applicationName() == "") {
44  throw IException(IException::Programmer, "You must set QApplication's "
45  "application name before using the Isis::MainWindow class. Window "
46  "state and geometry can not be saved and restored", _FILEINFO_);
47  }
48 
49  if (objectTitle == "") {
50  throw IException(IException::Programmer,
51  tr("You must provide a valid objectTitle to MainWindow::settingsFileName"),
52  _FILEINFO_);
53  }
54 
55  QDir programSettings =
56  QDir(FileName("$HOME/.Isis/" + QApplication::applicationName() + "/").path());
57  QString windowSettings = programSettings.filePath(objectTitle + ".config");
58 
59  return windowSettings;
60  }
61 
62 
69 // void MainWindow::hideEvent(QHideEvent *event) {
70 // writeSettings();
71 // }
72 
73 
79  void MainWindow::readSettings(QSize defaultSize) {
80  QSettings settings(settingsFileName(), QSettings::NativeFormat);
81  restoreGeometry(settings.value("geometry").toByteArray());
82  restoreState(settings.value("windowState").toByteArray());
83 
84  // The geom/state isn't enough for main windows to correctly remember
85  // their position and size, so let's restore those on top of
86  // the geom and state.
87  if (!settings.value("pos").toPoint().isNull())
88  move(settings.value("pos").toPoint());
89 
90  resize(settings.value("size", defaultSize).toSize());
91  }
92 
93 
94  QString MainWindow::settingsFileName() const {
95  if (QApplication::applicationName() == "") {
96  throw IException(IException::Programmer, "You must set QApplication's "
97  "application name before using the Isis::MainWindow class. Window "
98  "state and geometry can not be saved and restored", _FILEINFO_);
99  }
100 
101  if (objectName() == "") {
102  throw IException(IException::Programmer,
103  tr("You must set the objectName of the widget titled [%1] before "
104  "using the instance. Window state and geometry can not be saved and "
105  "restored").arg(windowTitle()), _FILEINFO_);
106  }
107 
108  QDir programSettings =
109  QDir(FileName("$HOME/.Isis/" + QApplication::applicationName() + "/").path());
110  QString windowSettings = programSettings.filePath(objectName() + ".config");
111 
112  return windowSettings;
113  }
114 
115 
123  QSettings settings(settingsFileName(), QSettings::NativeFormat);
124 
125  settings.setValue("geometry", saveGeometry());
126  settings.setValue("windowState", saveState());
127  settings.setValue("size", size());
128  settings.setValue("pos", pos());
129  }
130 }
131