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.,                                       *
24  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.             *
25  ***************************************************************************/
26 
27 #include <QApplication>
28 #include <QCloseEvent>
29 #include <QDesktopWidget>
30 #include <QHideEvent>
31 #include <QPoint>
32 
33 #include "iconmanager.h"
34 #include "prefscontext.h"
35 #include "prefsfile.h"
36 #include "prefsmanager.h"
37 #include "scrpalettebase.h"
38 #include "util.h"
39 
ScrPaletteBase(QWidget * parent,const QString & prefsContext,bool modal,Qt::WindowFlags f)40 ScrPaletteBase::ScrPaletteBase(  QWidget * parent, const QString& prefsContext, bool modal, Qt::WindowFlags f)
41 	: QDialog ( parent, f | Qt::Tool | Qt::CustomizeWindowHint
42 			| Qt::WindowTitleHint | Qt::WindowMinMaxButtonsHint
43 			| Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint )
44 {
45 	if (PrefsManager::instance().appPrefs.uiPrefs.useSmallWidgets)
46 	{
47 		setStyleSheet("	QToolButton { margin: 1px; padding: 0px; font-size: 10px; } \
48 						QToolButton:pressed { padding-top: 2px; padding-left: 2px } \
49 						QPushButton { margin: 1px; padding: 0px; font-size: 10px; } \
50 						QPushButton:pressed { padding-top: 2px; padding-left: 2px } \
51 						QRadioButton, QComboBox, QLineEdit \
52 							QListView, QLabel { margin:1px; padding: 0px; font-size: 10px; } \
53 						QCheckBox, QSpinBox, QDoubleSpinBox \
54 							{ margin:1px; padding: 0px; font-size: 10px; } \
55 						QTabWidget, QTabBar, QTableView, QGroupBox, QTreeView \
56 							{ font-size: 10px ; } \
57 						QToolBox::tab { font-size: 10px; padding: 0px; margin: 0px; } \
58 			  		");
59 	}
60 	m_originalParent=parent;
61 	m_tempParent=nullptr;
62 	setWindowIcon(IconManager::instance().loadIcon("AppIcon.png"));
63 	setPrefsContext(prefsContext);
64 	setModal(modal);
65 	connect(&PrefsManager::instance(), SIGNAL(prefsChanged()), this, SLOT(setFontSize()));
66 }
67 
setPrefsContext(const QString & context)68 void ScrPaletteBase::setPrefsContext(const QString& context)
69 {
70 	if (m_prefsContextName.isEmpty())
71 	{
72 		m_prefsContextName=context;
73 		if (!m_prefsContextName.isEmpty())
74 		{
75 			m_palettePrefs = PrefsManager::instance().prefsFile->getContext(m_prefsContextName);
76 			if (m_palettePrefs)
77 				m_visibleOnStartup = m_palettePrefs->getBool("visible");
78 		}
79 		else
80 			m_palettePrefs = nullptr;
81 	}
82 }
83 
startup()84 void ScrPaletteBase::startup()
85 {
86 	setFontSize();
87 	if (m_visibleOnStartup)
88 		show();
89 	else
90 		hide();
91 	emit paletteShown(m_visibleOnStartup);
92 }
93 
setPaletteShown(bool visible)94 void ScrPaletteBase::setPaletteShown(bool visible)
95 {
96 	storeVisibility(visible);
97 	if (!visible)
98 		hide();
99 	else if (!isVisible())
100 	{
101 		show();
102 		activateWindow();
103 	}
104 }
105 
setFontSize()106 void ScrPaletteBase::setFontSize()
107 {
108 	QFont newfont(font());
109 	newfont.setPointSize(PrefsManager::instance().appPrefs.uiPrefs.paletteFontSize);
110 	setFont(newfont);
111 }
112 
113 /*
114 void ScrPaletteBase::keyPressEvent(QKeyEvent *keyEvent)
115 {
116 	// Sample code to use in overriding classes
117 	int keyMod;
118 	switch (keyEvent->modifiers())
119 	{
120 		case Qt::ShiftModifier:
121 			keyMod = Qt::SHIFT;
122 			break;
123 		case Qt::AltModifier:
124 			keyMod = Qt::ALT;
125 			break;
126 		case Qt::ControlModifier:
127 			keyMod = Qt::CTRL;
128 			break;
129 		default:
130 			keyMod = 0;
131 			break;
132 	}
133 	// Tell our action to be off
134 	//	if (keyMod==0 && keyEvent->key()==Key_Escape)
135 	//	emit paletteShown(false);
136 
137 	QDialog::keyPressEvent(keyEvent);
138 }
139 	*/
closeEvent(QCloseEvent * closeEvent)140 void ScrPaletteBase::closeEvent(QCloseEvent *closeEvent)
141 {
142 	closeEvent->ignore();
143 	hide();
144 	emit paletteShown(false);
145 }
146 
hideEvent(QHideEvent *)147 void ScrPaletteBase::hideEvent(QHideEvent*)
148 {
149 	storePosition();
150 	storeSize();
151 }
152 
showEvent(QShowEvent * showEvent)153 void ScrPaletteBase::showEvent(QShowEvent *showEvent)
154 {
155 	// According to Qt doc, non-spontaneous show events are sent to widgets
156 	// immediately before they are shown. We want to restore geometry for those
157 	// events as spontaneous events are delivered after dialog has been shown
158 	if (m_palettePrefs && !showEvent->spontaneous())
159 	{
160 		QDesktopWidget *d = QApplication::desktop();
161 		if (m_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(0, m_palettePrefs->getInt("width")),
166 			                   d->width());
167 			int vheight = qMin(qMax(0, m_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, m_palettePrefs->getInt("left")),
174 							   scr.right());
175 			int vtop = qMin(m_palettePrefs->getInt("top"), d->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, 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( 0, scr.width() - vleft );
193 			if ( vheight >= scr.height() )
194 				vheight = qMax( 0, 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 		storeVisibility(true);
202 	}
203 	QDialog::showEvent(showEvent);
204 }
205 
hide()206 void ScrPaletteBase::hide()
207 {
208 	if (isVisible())
209 	{
210 		storePosition();
211 		storeSize();
212 		QDialog::hide();
213 	}
214 }
215 
reject()216 void ScrPaletteBase::reject()
217 {
218 	QDialog::reject();
219 	emit paletteShown(false);
220 }
221 
storePosition()222 void ScrPaletteBase::storePosition()
223 {
224 	if (m_palettePrefs)
225 	{
226 		QPoint geo = pos();
227 		m_palettePrefs->set("left", geo.x());
228 		m_palettePrefs->set("top", geo.y());
229 	}
230 }
231 
storePosition(int newX,int newY)232 void ScrPaletteBase::storePosition(int newX, int newY)
233 {
234 	if (m_palettePrefs)
235 	{
236 		m_palettePrefs->set("left", newX);
237 		m_palettePrefs->set("top", newY);
238 	}
239 }
240 
storeSize()241 void ScrPaletteBase::storeSize()
242 {
243 	if (m_palettePrefs)
244 	{
245 		m_palettePrefs->set("width", width());
246 		m_palettePrefs->set("height", height());
247 	}
248 }
249 
storeVisibility(bool vis)250 void ScrPaletteBase::storeVisibility(bool vis)
251 {
252 	if (m_palettePrefs)
253 		m_palettePrefs->set("visible", vis);
254 }
255 
exec(QWidget * newParent)256 int ScrPaletteBase::exec(QWidget* newParent)
257 {
258 	Q_ASSERT(m_tempParent==nullptr && newParent!=nullptr);
259 	m_tempParent=newParent;
260 	Qt::WindowFlags wflags = windowFlags();
261 	setParent(newParent, wflags);
262 	int i=QDialog::exec();
263 	setParent(m_originalParent, wflags);
264 	m_tempParent=nullptr;
265 	return i;
266 }
267