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