Isis 3 Programmer Reference
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::WindowFlags flags) :
16  QMainWindow(parent, flags) {
17  //qDebug()<<"MainWindow::MainWindow";
18  setWindowTitle(title);
19  }
20 
21 
26  if (isVisible())
27  close();
28  }
29 
30 
37  void MainWindow::closeEvent(QCloseEvent *event) {
38  writeSettings();
39  QMainWindow::closeEvent(event);
40  }
41 
42 
43  QString MainWindow::settingsFileName(QString objectTitle) {
44  if (QCoreApplication::applicationName() == "") {
45  throw IException(IException::Programmer, "You must set QApplication's "
46  "application name before using the Isis::MainWindow class. Window "
47  "state and geometry can not be saved and restored", _FILEINFO_);
48  }
49 
50  if (objectTitle == "") {
51  throw IException(IException::Programmer,
52  tr("You must provide a valid objectTitle to MainWindow::settingsFileName"),
53  _FILEINFO_);
54  }
55 
56  QDir programSettings =
57  QDir(FileName("$HOME/.Isis/" + QCoreApplication::applicationName() + "/").path());
58  QString windowSettings = programSettings.filePath(objectTitle + ".config");
59 
60  return windowSettings;
61  }
62 
63 
70 // void MainWindow::hideEvent(QHideEvent *event) {
71 // writeSettings();
72 // }
73 
74 
80  void MainWindow::readSettings(QSize defaultSize) {
81  //qDebug()<<"MainWindow::readSettings";
82  QSettings settings(settingsFileName(), QSettings::NativeFormat);
83  restoreGeometry(settings.value("geometry").toByteArray());
84  restoreState(settings.value("windowState").toByteArray());
85 
86  // The geom/state isn't enough for main windows to correctly remember
87  // their position and size, so let's restore those on top of
88  // the geom and state.
89  if (!settings.value("pos").toPoint().isNull())
90  move(settings.value("pos").toPoint());
91 
92  resize(settings.value("size", defaultSize).toSize());
93  }
94 
95 
96  QString MainWindow::settingsFileName() const {
97  if (QCoreApplication::applicationName() == "") {
98  throw IException(IException::Programmer, "You must set QApplication's "
99  "application name before using the Isis::MainWindow class. Window "
100  "state and geometry can not be saved and restored", _FILEINFO_);
101  }
102 
103  if (objectName() == "") {
104  throw IException(IException::Programmer,
105  tr("You must set the objectName of the widget titled [%1] before "
106  "using the instance. Window state and geometry can not be saved and "
107  "restored").arg(windowTitle()), _FILEINFO_);
108  }
109 
110  QDir programSettings =
111  QDir(FileName("$HOME/.Isis/" + QCoreApplication::applicationName() + "/").path());
112  QString windowSettings = programSettings.filePath(objectName() + ".config");
113  //qDebug()<<"MainWindow::settingsFileName windowSettings = "<<windowSettings;
114  return windowSettings;
115  }
116 
117 
125  //qDebug()<<"MainWindow::writeSettings";
126  QSettings settings(settingsFileName(), QSettings::NativeFormat);
127 
128  settings.setValue("geometry", saveGeometry());
129  settings.setValue("windowState", saveState());
130  settings.setValue("size", size());
131  settings.setValue("pos", pos());
132  }
133 }
134 
virtual void readSettings(QSize defaultSize=QSize())
This method ensure that the settings get written even if the Main window was only hidden...
Definition: MainWindow.cpp:80
This error is for when a programmer made an API call that was illegal.
Definition: IException.h:162
MainWindow(QString title, QWidget *parent=0, Qt::WindowFlags flags=0)
Mainwindow constructor.
Definition: MainWindow.cpp:15
virtual void closeEvent(QCloseEvent *event)
This method is overridden so that we can be sure to write the current settings of the Main window...
Definition: MainWindow.cpp:37
#define _FILEINFO_
Macro for the filename and line number.
Definition: IException.h:40
Isis exception class.
Definition: IException.h:107
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31
virtual void writeSettings() const
This method is called when the Main window is closed or hidden to write the size and location setting...
Definition: MainWindow.cpp:124
virtual ~MainWindow()
Free allocated memory by from this instance.
Definition: MainWindow.cpp:25