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 1238 2006-09-25 17:50:57Z 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 <QKeySequence>
00033 #include "vidaliawindow.h"
00034 
00035 #include <QtDebug>
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   saveSetting("Size", size());
00067   saveSetting("Position", pos());
00068 }
00069 
00070 /** Restores the last size and location of the window. */
00071 void
00072 VidaliaWindow::restoreWindowState()
00073 {
00074   /* Restore the window size. */
00075   QSize size = getSetting("Size", QSize()).toSize();
00076   if (!size.isEmpty()) {
00077     resize(size);
00078   }
00079 
00080   /* Restore the window position. */
00081   QPoint pos = getSetting("Position", QPoint()).toPoint();
00082   if (!pos.isNull()) {
00083     move(pos);
00084   }
00085 }
00086 
00087 /** Gets the saved value of a property associated with this window object.
00088  * If no value was saved, the default value is returned. */
00089 QVariant
00090 VidaliaWindow::getSetting(QString setting, QVariant defaultValue)
00091 {
00092   QString key = _name + "/" + setting;
00093   return _settings->value(key, defaultValue);
00094 }
00095 
00096 /** Saves a value associated with a property name for this window object. */
00097 void
00098 VidaliaWindow::saveSetting(QString prop, QVariant value)
00099 {
00100   QString key = _name + "/" + prop;
00101   _settings->setValue(key, value);
00102 }
00103 
00104 /** Overloaded QWidget::close() method. Saves the window state and closes the
00105  * window. Returns true if the window was closed. */
00106 bool
00107 VidaliaWindow::close()
00108 {
00109   saveWindowState();
00110   return QMainWindow::close();
00111 }
00112 
00113 /** Overloaded QWidget::show() */
00114 void
00115 VidaliaWindow::show()
00116 {
00117   /* If this is the first time this window is shown, restore its window
00118    * position and size. */
00119   if (!_previouslyShown) {
00120 #if defined (Q_WS_MAC)
00121     /* Use the standard palette on Mac, overriding whatever was specified in
00122      * the .ui file for this dialog. */
00123     setPalette(QPalette());
00124 #endif
00125     
00126     restoreWindowState();
00127     _previouslyShown = true;
00128   }
00129 
00130   /* Bring the window to the top, if it's already open. Otherwise, make the
00131    * window visible. */
00132   if (!this->isVisible()) {
00133     QMainWindow::show();
00134   } else {
00135     activateWindow();
00136     setWindowState(windowState() & ~Qt::WindowMinimized | Qt::WindowActive);
00137     raise();
00138   }
00139 }
00140 

Generated on Mon Oct 23 20:08:16 2006 for Vidalia by  doxygen 1.5.0