Vidalia  0.2.21
VidaliaWindow.cpp
Go to the documentation of this file.
1 /*
2 ** This file is part of Vidalia, and is subject to the license terms in the
3 ** LICENSE file, found in the top level directory of this distribution. If you
4 ** did not receive the LICENSE file with this file, you may obtain it from the
5 ** Vidalia source package distributed by the Vidalia Project at
6 ** http://www.torproject.org/projects/vidalia.html. No part of Vidalia,
7 ** including this file, may be copied, modified, propagated, or distributed
8 ** except according to the terms described in the LICENSE file.
9 */
10 
11 /*
12 ** \file VidaliaWindow.cpp
13 ** \brief Common superclass for all Vidalia windows
14 */
15 
16 #include "VidaliaWindow.h"
17 #include "Vidalia.h"
18 
19 #include <QPoint>
20 #include <QSize>
21 #include <QShortcut>
22 #include <QByteArray>
23 #include <QKeySequence>
24 #include <QDesktopWidget>
25 
26 
27 /** Default constructor. */
28 VidaliaWindow::VidaliaWindow(const QString &name, QWidget *parent,
29  Qt::WFlags flags)
30  : QMainWindow(parent, flags)
31 {
32  _name = name;
33  _settings = new VSettings(name);
34 }
35 
36 /** Destructor. */
38 {
40  delete _settings;
41 }
42 
43 /** Associates a shortcut key sequence with a slot. */
44 void
45 VidaliaWindow::setShortcut(const QString &shortcut, const char *slot)
46 {
47  vApp->createShortcut(QKeySequence(shortcut), this, this, slot);
48 }
49 
50 /** Saves the size and location of the window. */
51 void
53 {
54 #if QT_VERSION >= 0x040200
55  saveSetting("Geometry", saveGeometry());
56 #else
57  saveSetting("Size", size());
58  saveSetting("Position", pos());
59 #endif
60 }
61 
62 /** Restores the last size and location of the window. */
63 void
65 {
66 #if QT_VERSION >= 0x040200
67  QByteArray geometry = getSetting("Geometry", QByteArray()).toByteArray();
68  if (geometry.isEmpty())
69  adjustSize();
70  else
71  restoreGeometry(geometry);
72 #else
73  QRect screen = QDesktopWidget().availableGeometry();
74 
75  /* Restore the window size. */
76  QSize size = getSetting("Size", QSize()).toSize();
77  if (!size.isEmpty()) {
78  size = size.boundedTo(screen.size());
79  resize(size);
80  }
81 
82  /* Restore the window position. */
83  QPoint pos = getSetting("Position", QPoint()).toPoint();
84  if (!pos.isNull() && screen.contains(pos)) {
85  move(pos);
86  }
87 #endif
88 }
89 
90 /** Gets the saved value of a property associated with this window object.
91  * If no value was saved, the default value is returned. */
92 QVariant
93 VidaliaWindow::getSetting(QString setting, QVariant defaultValue)
94 {
95  return _settings->value(setting, defaultValue);
96 }
97 
98 /** Saves a value associated with a property name for this window object. */
99 void
100 VidaliaWindow::saveSetting(QString prop, QVariant value)
101 {
102  _settings->setValue(prop, value);
103 }
104 
105 /** Overloaded QWidget::setVisible(). If this window is already visible and
106  * <b>visible</b> is true, this window will be brought to the top and given
107  * focus. If <b>visible</b> is false, then the window state will be saved and
108  * this window will be hidden. */
109 void
111 {
112  if (visible) {
113  /* Bring the window to the top, if it's already open. Otherwise, make the
114  * window visible. */
115  if (isVisible()) {
116  activateWindow();
117  setWindowState(windowState() & ~Qt::WindowMinimized | Qt::WindowActive);
118  raise();
119  } else {
121  }
122  } else {
123  /* Save the last size and position of this window. */
124  saveWindowState();
125  }
126  QMainWindow::setVisible(visible);
127 }
128 
129 /** Reimplement the windows' changeEvent() method to check if the event
130  * is a QEvent::LanguageChange event. If so, call retranslateUi(), which
131  * subclasses of VidaliaWindow can reimplement to update their UI. */
132 void
134 {
135  if (e->type() == QEvent::LanguageChange) {
136  retranslateUi();
137  e->accept();
138  return;
139  }
140  QMainWindow::changeEvent(e);
141 }
142 
143 /** Called when the user wants to change the currently visible language.
144  * Subclasses can reimplement this to update their UI. */
145 void
147 {
148  /* The default retranslateUi() implementation does nothing */
149 }
150