vidaliawindow.cpp

Go to the documentation of this file.
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 2362 2008-02-29 04:30:11Z edmanm $
00014 ** \brief Common superclass for all Vidalia windows
00015 */
00016 
00017 #include <QPoint>
00018 #include <QSize>
00019 #include <QShortcut>
00020 #include <QByteArray>
00021 #include <QKeySequence>
00022 #include <QDesktopWidget>
00023 #include <vidalia.h>
00024 #include "vidaliawindow.h"
00025 
00026 
00027 /** Default constructor. */
00028 VidaliaWindow::VidaliaWindow(QString name, QWidget *parent, Qt::WFlags flags)
00029  : QMainWindow(parent, flags)
00030 {
00031   _name     = name;
00032   _settings = new VSettings(name);
00033 }
00034 
00035 /** Destructor. */
00036 VidaliaWindow::~VidaliaWindow()
00037 {
00038   saveWindowState();
00039   delete _settings;
00040 }
00041 
00042 /** Associates a shortcut key sequence with a slot. */
00043 void
00044 VidaliaWindow::setShortcut(QString shortcut, const char *slot)
00045 {
00046   vApp->createShortcut(QKeySequence(shortcut), this, this, slot);
00047 }
00048 
00049 /** Saves the size and location of the window. */
00050 void
00051 VidaliaWindow::saveWindowState()
00052 {
00053 #if QT_VERSION >= 0x040200
00054   saveSetting("Geometry", saveGeometry());
00055 #else
00056   saveSetting("Size", size());
00057   saveSetting("Position", pos());
00058 #endif
00059 }
00060 
00061 /** Restores the last size and location of the window. */
00062 void
00063 VidaliaWindow::restoreWindowState()
00064 {
00065 #if QT_VERSION >= 0x040200
00066   QByteArray geometry = getSetting("Geometry", QByteArray()).toByteArray();
00067   if (geometry.isEmpty())
00068     adjustSize();
00069   else
00070     restoreGeometry(geometry);
00071 #else
00072   QRect screen = QDesktopWidget().availableGeometry();
00073 
00074   /* Restore the window size. */
00075   QSize size = getSetting("Size", QSize()).toSize();
00076   if (!size.isEmpty()) {
00077     size = size.boundedTo(screen.size());
00078     resize(size);
00079   }
00080 
00081   /* Restore the window position. */
00082   QPoint pos = getSetting("Position", QPoint()).toPoint();
00083   if (!pos.isNull() && screen.contains(pos)) {
00084     move(pos);
00085   }
00086 #endif
00087 }
00088 
00089 /** Gets the saved value of a property associated with this window object.
00090  * If no value was saved, the default value is returned. */
00091 QVariant
00092 VidaliaWindow::getSetting(QString setting, QVariant defaultValue)
00093 {
00094   return _settings->value(setting, defaultValue);
00095 }
00096 
00097 /** Saves a value associated with a property name for this window object. */
00098 void
00099 VidaliaWindow::saveSetting(QString prop, QVariant value)
00100 {
00101   _settings->setValue(prop, value);
00102 }
00103 
00104 /** Overloaded QWidget::setVisible(). If this window is already visible and
00105  * <b>visible</b> is true, this window will be brought to the top and given 
00106  * focus. If <b>visible</b> is false, then the window state will be saved and
00107  * this window will be hidden. */
00108 void
00109 VidaliaWindow::setVisible(bool visible)
00110 {
00111   if (visible) {
00112     /* Bring the window to the top, if it's already open. Otherwise, make the
00113      * window visible. */
00114     if (isVisible()) {
00115       activateWindow();
00116       setWindowState(windowState() & ~Qt::WindowMinimized | Qt::WindowActive);
00117       raise();
00118     } else {
00119       restoreWindowState();
00120     }
00121   } else {
00122     /* Save the last size and position of this window. */
00123     saveWindowState();
00124   }
00125   QMainWindow::setVisible(visible);
00126 }
00127 

Generated on Sat Aug 16 17:38:36 2008 for Vidalia by  doxygen 1.5.6