Vidalia
0.2.15
|
00001 /* 00002 ** This file is part of Vidalia, and is subject to the license terms in the 00003 ** LICENSE file, found in the top level directory of this distribution. If you 00004 ** did not receive the LICENSE file with this file, you may obtain it from the 00005 ** Vidalia source package distributed by the Vidalia Project at 00006 ** http://www.torproject.org/projects/vidalia.html. No part of Vidalia, 00007 ** including this file, may be copied, modified, propagated, or distributed 00008 ** except according to the terms described in the LICENSE file. 00009 */ 00010 00011 /* 00012 ** \file VidaliaWindow.cpp 00013 ** \brief Common superclass for all Vidalia windows 00014 */ 00015 00016 #include "VidaliaWindow.h" 00017 #include "Vidalia.h" 00018 00019 #include <QPoint> 00020 #include <QSize> 00021 #include <QShortcut> 00022 #include <QByteArray> 00023 #include <QKeySequence> 00024 #include <QDesktopWidget> 00025 00026 00027 /** Default constructor. */ 00028 VidaliaWindow::VidaliaWindow(const QString &name, QWidget *parent, 00029 Qt::WFlags flags) 00030 : QMainWindow(parent, flags) 00031 { 00032 _name = name; 00033 _settings = new VSettings(name); 00034 } 00035 00036 /** Destructor. */ 00037 VidaliaWindow::~VidaliaWindow() 00038 { 00039 saveWindowState(); 00040 delete _settings; 00041 } 00042 00043 /** Associates a shortcut key sequence with a slot. */ 00044 void 00045 VidaliaWindow::setShortcut(const QString &shortcut, const char *slot) 00046 { 00047 vApp->createShortcut(QKeySequence(shortcut), this, this, slot); 00048 } 00049 00050 /** Saves the size and location of the window. */ 00051 void 00052 VidaliaWindow::saveWindowState() 00053 { 00054 #if QT_VERSION >= 0x040200 00055 saveSetting("Geometry", saveGeometry()); 00056 #else 00057 saveSetting("Size", size()); 00058 saveSetting("Position", pos()); 00059 #endif 00060 } 00061 00062 /** Restores the last size and location of the window. */ 00063 void 00064 VidaliaWindow::restoreWindowState() 00065 { 00066 #if QT_VERSION >= 0x040200 00067 QByteArray geometry = getSetting("Geometry", QByteArray()).toByteArray(); 00068 if (geometry.isEmpty()) 00069 adjustSize(); 00070 else 00071 restoreGeometry(geometry); 00072 #else 00073 QRect screen = QDesktopWidget().availableGeometry(); 00074 00075 /* Restore the window size. */ 00076 QSize size = getSetting("Size", QSize()).toSize(); 00077 if (!size.isEmpty()) { 00078 size = size.boundedTo(screen.size()); 00079 resize(size); 00080 } 00081 00082 /* Restore the window position. */ 00083 QPoint pos = getSetting("Position", QPoint()).toPoint(); 00084 if (!pos.isNull() && screen.contains(pos)) { 00085 move(pos); 00086 } 00087 #endif 00088 } 00089 00090 /** Gets the saved value of a property associated with this window object. 00091 * If no value was saved, the default value is returned. */ 00092 QVariant 00093 VidaliaWindow::getSetting(QString setting, QVariant defaultValue) 00094 { 00095 return _settings->value(setting, defaultValue); 00096 } 00097 00098 /** Saves a value associated with a property name for this window object. */ 00099 void 00100 VidaliaWindow::saveSetting(QString prop, QVariant value) 00101 { 00102 _settings->setValue(prop, value); 00103 } 00104 00105 /** Overloaded QWidget::setVisible(). If this window is already visible and 00106 * <b>visible</b> is true, this window will be brought to the top and given 00107 * focus. If <b>visible</b> is false, then the window state will be saved and 00108 * this window will be hidden. */ 00109 void 00110 VidaliaWindow::setVisible(bool visible) 00111 { 00112 if (visible) { 00113 /* Bring the window to the top, if it's already open. Otherwise, make the 00114 * window visible. */ 00115 if (isVisible()) { 00116 activateWindow(); 00117 setWindowState(windowState() & ~Qt::WindowMinimized | Qt::WindowActive); 00118 raise(); 00119 } else { 00120 restoreWindowState(); 00121 } 00122 } else { 00123 /* Save the last size and position of this window. */ 00124 saveWindowState(); 00125 } 00126 QMainWindow::setVisible(visible); 00127 } 00128 00129 /** Reimplement the windows' changeEvent() method to check if the event 00130 * is a QEvent::LanguageChange event. If so, call retranslateUi(), which 00131 * subclasses of VidaliaWindow can reimplement to update their UI. */ 00132 void 00133 VidaliaWindow::changeEvent(QEvent *e) 00134 { 00135 if (e->type() == QEvent::LanguageChange) { 00136 retranslateUi(); 00137 e->accept(); 00138 return; 00139 } 00140 QMainWindow::changeEvent(e); 00141 } 00142 00143 /** Called when the user wants to change the currently visible language. 00144 * Subclasses can reimplement this to update their UI. */ 00145 void 00146 VidaliaWindow::retranslateUi() 00147 { 00148 /* The default retranslateUi() implementation does nothing */ 00149 } 00150