Isis 3 Programmer Reference
MainWindow.cpp
1#include "MainWindow.h"
2
3#include "IException.h"
4#include "IString.h"
5
6namespace 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) {
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
Isis exception class.
Definition IException.h:91
@ Programmer
This error is for when a programmer made an API call that was illegal.
Definition IException.h:146
virtual void closeEvent(QCloseEvent *event)
This method is overridden so that we can be sure to write the current settings of the Main window.
virtual ~MainWindow()
Free allocated memory by from this instance.
MainWindow(QString title, QWidget *parent=0, Qt::WindowFlags flags=0)
Mainwindow constructor.
virtual void readSettings(QSize defaultSize=QSize())
This method ensure that the settings get written even if the Main window was only hidden,...
virtual void writeSettings() const
This method is called when the Main window is closed or hidden to write the size and location setting...
This is free and unencumbered software released into the public domain.
Definition Apollo.h:16