Rev 17824 | Rev 18194 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
16546 | jghali | 1 | /* |
2 | For general Scribus (>=1.3.2) copyright and licensing information please refer |
||
3 | to the COPYING file provided with the program. Following this notice may exist |
||
4 | a copyright and/or license notice that predates the release of Scribus 1.3.2 |
||
5 | for which a new license (GPL+exception) is in place. |
||
6 | */ |
||
7 | /*************************************************************************** |
||
8 | * Copyright (C) 2005 by Craig Bradney * |
||
9 | * cbradney@scribus.info * |
||
10 | * * |
||
11 | * This program is free software; you can redistribute it and/or modify * |
||
12 | * it under the terms of the GNU General Public License as published by * |
||
13 | * the Free Software Foundation; either version 2 of the License, or * |
||
14 | * (at your option) any later version. * |
||
15 | * * |
||
16 | * This program is distributed in the hope that it will be useful, * |
||
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
||
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
||
19 | * GNU General Public License for more details. * |
||
20 | * * |
||
21 | * You should have received a copy of the GNU General Public License * |
||
22 | * along with this program; if not, write to the * |
||
23 | * Free Software Foundation, Inc., * |
||
18122 | mrdocs | 24 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * |
16546 | jghali | 25 | ***************************************************************************/ |
26 | |||
27 | #include <QApplication> |
||
28 | #include <QCloseEvent> |
||
29 | #include <QDesktopWidget> |
||
30 | #include <QHideEvent> |
||
31 | #include <QMainWindow> |
||
32 | #include <QPoint> |
||
33 | |||
34 | #include "prefscontext.h" |
||
35 | #include "prefsfile.h" |
||
36 | #include "prefsmanager.h" |
||
37 | #include "scdockpalette.h" |
||
38 | #include "scribus.h" |
||
39 | #include "util.h" |
||
40 | #include "util_icon.h" |
||
41 | |||
42 | |||
17776 | jghali | 43 | ScDockPalette::ScDockPalette( QWidget * parent, const QString& prefsContext, Qt::WFlags f) |
17824 | jghali | 44 | #if defined(Q_OS_MAC) || defined(Q_OS_WIN) |
17785 | jghali | 45 | : QDockWidget ( parent, f | Qt::Tool | Qt::CustomizeWindowHint | Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint ), |
46 | #else |
||
17776 | jghali | 47 | : QDockWidget ( parent, f | Qt::Tool | Qt::CustomizeWindowHint ), |
17785 | jghali | 48 | #endif |
16546 | jghali | 49 | palettePrefs(0), |
50 | prefsContextName(QString::null), |
||
51 | visibleOnStartup(false) |
||
52 | { |
||
53 | if (PrefsManager::instance()->appPrefs.uiPrefs.useSmallWidgets) |
||
54 | { |
||
55 | setStyleSheet(" QToolButton { margin: 1px; padding: 0px; font-size: 10px; } \ |
||
56 | QToolButton:pressed { padding-top: 2px; padding-left: 2px } \ |
||
57 | QPushButton { margin: 1px; padding: 0px; font-size: 10px; } \ |
||
58 | QPushButton:pressed { padding-top: 2px; padding-left: 2px } \ |
||
59 | QRadioButton, QComboBox, QLineEdit \ |
||
60 | QListView, QLabel { margin:1px; padding: 0px; font-size: 10px; } \ |
||
61 | QCheckBox, QSpinBox, QDoubleSpinBox \ |
||
62 | { margin:1px; padding: 0px; font-size: 10px; } \ |
||
63 | QTabWidget, QTabBar, QTableView, QGroupBox, QTreeView \ |
||
64 | { font-size: 10px ; } \ |
||
65 | QToolBox::tab { font-size: 10px; padding: 0px; margin: 0px; } \ |
||
66 | "); |
||
67 | } |
||
68 | originalParent=parent; |
||
69 | tempParent=0; |
||
70 | setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea); |
||
71 | setWindowIcon(loadIcon("AppIcon.png")); |
||
72 | setPrefsContext(prefsContext); |
||
16611 | fschmid | 73 | setObjectName(prefsContext); |
16546 | jghali | 74 | connect(PrefsManager::instance(), SIGNAL(prefsChanged()), this, SLOT(setFontSize())); |
75 | } |
||
76 | |||
77 | void ScDockPalette::setPrefsContext(QString context) |
||
78 | { |
||
79 | if (prefsContextName.isEmpty()) |
||
80 | { |
||
81 | prefsContextName=context; |
||
82 | if (!prefsContextName.isEmpty()) |
||
83 | { |
||
84 | palettePrefs = PrefsManager::instance()->prefsFile->getContext(prefsContextName); |
||
85 | if (palettePrefs) |
||
86 | visibleOnStartup = palettePrefs->getBool("visible"); |
||
87 | } |
||
88 | else |
||
89 | palettePrefs = NULL; |
||
90 | } |
||
91 | } |
||
92 | |||
93 | void ScDockPalette::startup() |
||
94 | { |
||
95 | setFontSize(); |
||
96 | if (visibleOnStartup) |
||
97 | { |
||
98 | QMainWindow* mainWindow = dynamic_cast<QMainWindow*>(parent()); |
||
99 | if (palettePrefs && mainWindow) |
||
100 | { |
||
101 | Qt::DockWidgetArea area = Qt::NoDockWidgetArea; |
||
102 | area = (Qt::DockWidgetArea) palettePrefs->getInt("area", (int) Qt::NoDockWidgetArea); |
||
103 | Qt::DockWidgetAreas areas = this->allowedAreas(); |
||
104 | if (areas.testFlag(area)) |
||
105 | { |
||
106 | mainWindow->addDockWidget(area, this); |
||
107 | setFloating (palettePrefs->getBool("floating")); |
||
108 | } |
||
109 | } |
||
110 | show(); |
||
111 | } |
||
112 | else |
||
113 | hide(); |
||
114 | emit paletteShown(visibleOnStartup); |
||
115 | } |
||
116 | |||
117 | void ScDockPalette::setPaletteShown(bool visible) |
||
118 | { |
||
119 | storeVisibility(visible); |
||
120 | storeDockState(); |
||
121 | // setShown(visible); |
||
122 | if (!visible) |
||
123 | hide(); |
||
124 | else if (!isVisible()) |
||
125 | { |
||
126 | show(); |
||
127 | activateWindow(); |
||
128 | } |
||
129 | } |
||
130 | |||
131 | void ScDockPalette::setFontSize() |
||
132 | { |
||
133 | QFont newfont(font()); |
||
134 | newfont.setPointSize(PrefsManager::instance()->appPrefs.uiPrefs.paletteFontSize); |
||
135 | setFont(newfont); |
||
136 | } |
||
137 | |||
138 | void ScDockPalette::closeEvent(QCloseEvent *closeEvent) |
||
139 | { |
||
140 | emit paletteShown(false); |
||
141 | closeEvent->ignore(); |
||
142 | hide(); |
||
143 | } |
||
144 | |||
145 | void ScDockPalette::hideEvent(QHideEvent*) |
||
146 | { |
||
147 | storePosition(); |
||
148 | storeSize(); |
||
149 | storeDockState(); |
||
150 | } |
||
151 | |||
152 | void ScDockPalette::showEvent(QShowEvent *showEvent) |
||
153 | { |
||
154 | // According to Qt doc, non-spontaneous show events are sent to widgets |
||
155 | // immediately before they are shown. We want to restore geometry for those |
||
156 | // events as spontaneous events are delivered after dialog has been shown |
||
17776 | jghali | 157 | if (palettePrefs && !showEvent->spontaneous() && isFloating()) |
16546 | jghali | 158 | { |
159 | QDesktopWidget *d = QApplication::desktop(); |
||
160 | QSize gStrut = QApplication::globalStrut(); |
||
161 | if (palettePrefs->contains("left")) |
||
162 | { |
||
163 | QRect scr = QApplication::desktop()->availableGeometry(this); |
||
164 | // all palettes should have enough room for 3x3 min widgets |
||
165 | int vwidth = qMin(qMax(3*gStrut.width(), palettePrefs->getInt("width")), |
||
166 | d->width()); |
||
167 | int vheight = qMin(qMax(3*gStrut.height(), palettePrefs->getInt("height")), |
||
168 | d->height()); |
||
169 | // palettes should not use too much screen space |
||
170 | if (vwidth > d->width()/3 && vheight > d->height()/3) |
||
171 | vwidth = d->width()/3; |
||
172 | // and should be partly visible |
||
173 | int vleft = qMin(qMax(scr.left() - vwidth + gStrut.width(), palettePrefs->getInt("left")), |
||
174 | scr.right() - gStrut.width()); |
||
175 | int vtop = qMin(palettePrefs->getInt("top"), d->height() - gStrut.height()); |
||
176 | #if defined(Q_OS_MAC) || defined(_WIN32) |
||
177 | // on Mac and Windows you're dead if the titlebar is not on screen |
||
178 | vtop = qMax(64, vtop); |
||
179 | #else |
||
180 | vtop = qMax(-vheight + gStrut.height(), vtop); |
||
181 | #endif |
||
182 | // Check values against current screen size |
||
183 | if ( vleft <= scr.left() ) |
||
184 | vleft = scr.left(); |
||
185 | if ( vleft >= scr.right() ) |
||
186 | vleft = scr.left(); |
||
187 | if ( vtop >= scr.bottom() ) |
||
188 | vtop = 64; |
||
189 | if ( vtop <= scr.top() ) |
||
190 | vtop = scr.top(); |
||
191 | if ( vwidth >= scr.width() ) |
||
192 | vwidth = qMax( gStrut.width(), scr.width() - vleft ); |
||
193 | if ( vheight >= scr.height() ) |
||
194 | vheight = qMax( gStrut.height(), scr.height() - vtop ); |
||
195 | // qDebug() << QString("root %1x%2 %7 palette %3x%4 @ (%5,%6)").arg(d->width()).arg(d->height()) |
||
196 | // .arg(vwidth).arg(vheight).arg(vleft).arg(vtop).arg(name()); |
||
197 | // setGeometry(vleft, vtop, vwidth, vheight); |
||
198 | resize(vwidth, vheight); |
||
199 | move(vleft, vtop); |
||
200 | } |
||
201 | storeDockState(); |
||
202 | storeVisibility(true); |
||
203 | } |
||
204 | QDockWidget::showEvent(showEvent); |
||
205 | } |
||
206 | |||
207 | void ScDockPalette::hide() |
||
208 | { |
||
209 | if (isVisible()) |
||
210 | { |
||
211 | storePosition(); |
||
212 | storeSize(); |
||
213 | QDockWidget::hide(); |
||
214 | } |
||
215 | } |
||
216 | |||
217 | void ScDockPalette::storePosition() |
||
218 | { |
||
219 | if (palettePrefs) |
||
220 | { |
||
221 | QPoint geo = pos(); |
||
222 | palettePrefs->set("left", geo.x()); |
||
223 | palettePrefs->set("top", geo.y()); |
||
224 | /* |
||
225 | #if QT_VERSION >= 0x040300 && !defined(_WIN32) |
||
226 | QRect geo = geometry(); |
||
227 | #else |
||
228 | QRect geo = frameGeometry(); |
||
229 | #endif |
||
230 | palettePrefs->set("left", geo.left()); |
||
231 | palettePrefs->set("top", geo.top()); |
||
232 | */ |
||
233 | } |
||
234 | } |
||
235 | |||
236 | void ScDockPalette::storePosition(int newX, int newY) |
||
237 | { |
||
238 | if (palettePrefs) |
||
239 | { |
||
240 | palettePrefs->set("left", newX); |
||
241 | palettePrefs->set("top", newY); |
||
242 | } |
||
243 | } |
||
244 | |||
245 | void ScDockPalette::storeSize() |
||
246 | { |
||
247 | if (palettePrefs) |
||
248 | { |
||
249 | palettePrefs->set("width", width()); |
||
250 | palettePrefs->set("height", height()); |
||
251 | } |
||
252 | } |
||
253 | |||
254 | void ScDockPalette::storeVisibility(bool vis) |
||
255 | { |
||
256 | if (palettePrefs) |
||
257 | palettePrefs->set("visible", vis); |
||
258 | } |
||
259 | |||
260 | void ScDockPalette::storeDockState() |
||
261 | { |
||
262 | if (palettePrefs) |
||
263 | { |
||
264 | palettePrefs->set("floating", isFloating()); |
||
265 | Qt::DockWidgetArea area = Qt::NoDockWidgetArea; |
||
266 | QMainWindow* mainWindow = dynamic_cast<QMainWindow*>(parent()); |
||
267 | if (mainWindow) |
||
268 | area = mainWindow->dockWidgetArea(this); |
||
269 | palettePrefs->set("area", (int) area); |
||
270 | } |
||
271 | } |
||
272 |