vidaliawindow.cpp

Go to the documentation of this file.
00001 /****************************************************************
00002  *  Vidalia is distributed under the following license:
00003  *
00004  *  Copyright (C) 2006,  Matt Edman, Justin Hipple
00005  *
00006  *  This program is free software; you can redistribute it and/or
00007  *  modify it under the terms of the GNU General Public License
00008  *  as published by the Free Software Foundation; either version 2
00009  *  of the License, or (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, 
00019  *  Boston, MA  02110-1301, USA.
00020  ****************************************************************/
00021 
00022 /** 
00023  * \file vidaliawindow.cpp
00024  * \version $Id: vidaliawindow.cpp 1708 2007-04-10 04:35:01Z edmanm $
00025  * \brief Common superclass for all Vidalia windows
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 /** Default constructor. */
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 /** Destructor. */
00048 VidaliaWindow::~VidaliaWindow()
00049 {
00050   saveWindowState();
00051   delete _settings;
00052 }
00053 
00054 /** Associates a shortcut key sequence with a slot. */
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 /** Saves the size and location of the window. */
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 /** Restores the last size and location of the window. */
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   /* Restore the window size. */
00085   QSize size = getSetting("Size", QSize()).toSize();
00086   if (!size.isEmpty()) {
00087     size = size.boundedTo(screen.size());
00088     resize(size);
00089   }
00090 
00091   /* Restore the window position. */
00092   QPoint pos = getSetting("Position", QPoint()).toPoint();
00093   if (!pos.isNull() && screen.contains(pos)) {
00094     move(pos);
00095   }
00096 #endif
00097 }
00098 
00099 /** Gets the saved value of a property associated with this window object.
00100  * If no value was saved, the default value is returned. */
00101 QVariant
00102 VidaliaWindow::getSetting(QString setting, QVariant defaultValue)
00103 {
00104   QString key = _name + "/" + setting;
00105   return _settings->value(key, defaultValue);
00106 }
00107 
00108 /** Saves a value associated with a property name for this window object. */
00109 void
00110 VidaliaWindow::saveSetting(QString prop, QVariant value)
00111 {
00112   QString key = _name + "/" + prop;
00113   _settings->setValue(key, value);
00114 }
00115 
00116 /** Overloaded QWidget::setVisible(). If this window is already visible and
00117  * <b>visible</b> is true, this window will be brought to the top and given 
00118  * focus. If <b>visible</b> is false, then the window state will be saved and
00119  * this window will be hidden. */
00120 void
00121 VidaliaWindow::setVisible(bool visible)
00122 {
00123   if (visible) {
00124     /* If this is the first time this window is shown, restore its window
00125      * position and size. */
00126     if (!_previouslyShown) {
00127 #if !defined (Q_WS_WIN)
00128       /* Use the standard palette on non-Windows, overriding whatever was 
00129        * specified in the .ui file for this dialog. */
00130       setPalette(QPalette());
00131 #endif
00132       _previouslyShown = true;
00133     }
00134 
00135     /* Bring the window to the top, if it's already open. Otherwise, make the
00136      * window visible. */
00137     if (isVisible()) {
00138       activateWindow();
00139       setWindowState(windowState() & ~Qt::WindowMinimized | Qt::WindowActive);
00140       raise();
00141     } else {
00142       restoreWindowState();
00143     }
00144   } else {
00145     /* Save the last size and position of this window. */
00146     saveWindowState();
00147   }
00148   QMainWindow::setVisible(visible);
00149 }
00150 

Generated on Wed Sep 5 15:49:28 2007 for Vidalia by  doxygen 1.5.3