00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include <QPoint>
00029 #include <QSize>
00030 #include <QPalette>
00031 #include <QShortcut>
00032 #include <QByteArray>
00033 #include <QKeySequence>
00034 #include <QDesktopWidget>
00035 #include "vidaliawindow.h"
00036
00037
00038
00039 VidaliaWindow::VidaliaWindow(QString name, QWidget *parent, Qt::WFlags flags)
00040 : QMainWindow(parent, flags)
00041 {
00042 _name = name;
00043 _settings = new VidaliaSettings();
00044 _previouslyShown = false;
00045 }
00046
00047
00048 VidaliaWindow::~VidaliaWindow()
00049 {
00050 saveWindowState();
00051 delete _settings;
00052 }
00053
00054
00055 void
00056 VidaliaWindow::setShortcut(QString shortcut, const char *slot)
00057 {
00058 QShortcut *s = new QShortcut(QKeySequence(shortcut), this, slot, 0);
00059 Q_UNUSED(s);
00060 }
00061
00062
00063 void
00064 VidaliaWindow::saveWindowState()
00065 {
00066 #if QT_VERSION >= 0x040200
00067 saveSetting("Geometry", saveGeometry());
00068 #else
00069 saveSetting("Size", size());
00070 saveSetting("Position", pos());
00071 #endif
00072 }
00073
00074
00075 void
00076 VidaliaWindow::restoreWindowState()
00077 {
00078 #if QT_VERSION >= 0x040200
00079 QByteArray geometry = getSetting("Geometry", QByteArray()).toByteArray();
00080 restoreGeometry(geometry);
00081 #else
00082 QRect screen = QDesktopWidget().availableGeometry();
00083
00084
00085 QSize size = getSetting("Size", QSize()).toSize();
00086 if (!size.isEmpty()) {
00087 size = size.boundedTo(screen.size());
00088 resize(size);
00089 }
00090
00091
00092 QPoint pos = getSetting("Position", QPoint()).toPoint();
00093 if (!pos.isNull() && screen.contains(pos)) {
00094 move(pos);
00095 }
00096 #endif
00097 }
00098
00099
00100
00101 QVariant
00102 VidaliaWindow::getSetting(QString setting, QVariant defaultValue)
00103 {
00104 QString key = _name + "/" + setting;
00105 return _settings->value(key, defaultValue);
00106 }
00107
00108
00109 void
00110 VidaliaWindow::saveSetting(QString prop, QVariant value)
00111 {
00112 QString key = _name + "/" + prop;
00113 _settings->setValue(key, value);
00114 }
00115
00116
00117
00118
00119
00120 void
00121 VidaliaWindow::setVisible(bool visible)
00122 {
00123 if (visible) {
00124
00125
00126 if (!_previouslyShown) {
00127 #if !defined (Q_WS_WIN)
00128
00129
00130 setPalette(QPalette());
00131 #endif
00132 _previouslyShown = true;
00133 }
00134
00135
00136
00137 if (isVisible()) {
00138 activateWindow();
00139 setWindowState(windowState() & ~Qt::WindowMinimized | Qt::WindowActive);
00140 raise();
00141 } else {
00142 restoreWindowState();
00143 }
00144 } else {
00145
00146 saveWindowState();
00147 }
00148 QMainWindow::setVisible(visible);
00149 }
00150