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 #include "newmarginwidget.h"
9 #include "scrspinbox.h"
10 #include "units.h"
11 #include "ui/marginpresetlayout.h"
12 #include "ui/useprintermarginsdialog.h"
13 
NewMarginWidget(QWidget * parent)14 NewMarginWidget::NewMarginWidget(QWidget* parent)
15 	: QWidget(parent)
16 {
17 	setupUi(this);
18 }
19 
setup(const MarginStruct & margs,int layoutType,int unitIndex,int flags)20 void NewMarginWidget::setup(const MarginStruct& margs, int layoutType, int unitIndex, int flags)
21 {
22 	m_marginData = m_savedMarginData=margs;
23 	m_unitIndex = unitIndex;
24 	m_unitRatio = unitGetRatioFromIndex(unitIndex);
25 	m_flags = flags;
26 	leftMarginSpinBox->setMaximum(1000);
27 	rightMarginSpinBox->setMaximum(1000);
28 	topMarginSpinBox->setMaximum(1000);
29 	bottomMarginSpinBox->setMaximum(1000);
30 	leftMarginSpinBox->init(unitIndex);
31 	rightMarginSpinBox->init(unitIndex);
32 	topMarginSpinBox->init(unitIndex);
33 	bottomMarginSpinBox->init(unitIndex);
34 	updateMarginSpinValues();
35 	if ((m_flags & ShowPreset) == 0)
36 	{
37 		presetLayoutComboBox->blockSignals(true);
38 		presetLayoutComboBox->resize(0,0);
39 		presetLayoutLabel->resize(0,0);
40 		presetLayoutComboBox->hide();
41 		presetLayoutLabel->hide();
42 		gridLayout->removeWidget(presetLayoutComboBox);
43 		gridLayout->removeWidget(presetLayoutLabel);
44 	}
45 	if ((m_flags & ShowPrinterMargins) == 0)
46 	{
47 		printerMarginsPushButton->blockSignals(true);
48 		printerMarginsPushButton->resize(0,0);
49 		printerMarginsPushButton->hide();
50 		gridLayout->removeWidget(printerMarginsPushButton);
51 	}
52 	setFacingPages(!(layoutType == singlePage));
53 
54 	gridLayout->invalidate();
55 
56 	languageChange();
57 
58 	connect(topMarginSpinBox, SIGNAL(valueChanged(double)), this, SLOT(setTop()));
59 	connect(bottomMarginSpinBox, SIGNAL(valueChanged(double)), this, SLOT(setBottom()));
60 	connect(leftMarginSpinBox, SIGNAL(valueChanged(double)), this, SLOT(setLeft()));
61 	connect(rightMarginSpinBox, SIGNAL(valueChanged(double)), this, SLOT(setRight()));
62 	connect(presetLayoutComboBox, SIGNAL(activated(int)), this, SLOT(setPreset()));
63 	connect(marginLinkButton, SIGNAL(clicked()), this, SLOT(slotLinkMargins()));
64 	connect(printerMarginsPushButton, SIGNAL(clicked()), this, SLOT(setMarginsToPrinterMargins()));
65 }
66 
languageChange()67 void NewMarginWidget::languageChange()
68 {
69 	if (m_flags & MarginWidgetFlags)
70 	{
71 		topMarginSpinBox->setToolTip( "<qt>" + tr( "Distance between the top margin guide and the edge of the page" ) + "</qt>");
72 		bottomMarginSpinBox->setToolTip( "<qt>" + tr( "Distance between the bottom margin guide and the edge of the page" ) + "</qt>");
73 		leftMarginSpinBox->setToolTip( "<qt>" + tr( "Distance between the left margin guide and the edge of the page. If a double-sided, 3 or 4-fold layout is selected, this margin space can be used to achieve the correct margins for binding.") + "</qt>");
74 		rightMarginSpinBox->setToolTip( "<qt>" + tr( "Distance between the right margin guide and the edge of the page. If a double-sided, 3 or 4-fold layout is selected, this margin space can be used to achieve the correct margins for binding.") + "</qt>");
75 		marginLinkButton->setToolTip( "<qt>" + tr( "Ensure all margins have the same value" ) + "</qt>");
76 	}
77 	else
78 	{
79 		topMarginSpinBox->setToolTip( "<qt>" + tr( "Distance for bleed from the top of the physical page" ) + "</qt>" );
80 		bottomMarginSpinBox->setToolTip( "<qt>" + tr( "Distance for bleed from the bottom of the physical page" ) + "</qt>" );
81 		leftMarginSpinBox->setToolTip( "<qt>" + tr( "Distance for bleed from the left of the physical page" ) + "</qt>" );
82 		rightMarginSpinBox->setToolTip( "<qt>" + tr( "Distance for bleed from the right of the physical page" )  + "</qt>");
83 		marginLinkButton->setToolTip( "<qt>" + tr( "Ensure all bleeds have the same value" ) + "</qt>");
84 	}
85 	printerMarginsPushButton->setToolTip( "<qt>" + tr( "Import the margins for the selected page size from the available printers" ) + "</qt>");
86 }
87 
setNewValues(const MarginStruct & margs)88 void NewMarginWidget::setNewValues(const MarginStruct& margs)
89 {
90 	m_marginData = m_savedMarginData = margs;
91 	updateMarginSpinValues();
92 }
93 
setPageWidth(double newWidth)94 void NewMarginWidget::setPageWidth(double newWidth)
95 {
96 	leftMarginSpinBox->setMaximum(qMax(0.0, newWidth * m_unitRatio - rightMarginSpinBox->value()));
97 	rightMarginSpinBox->setMaximum(qMax(0.0, newWidth * m_unitRatio - leftMarginSpinBox->value()));
98 	m_pageWidth = newWidth;
99 	setPreset();
100 }
101 
setPageHeight(double newHeight)102 void NewMarginWidget::setPageHeight(double newHeight)
103 {
104 	topMarginSpinBox->setMaximum(qMax(0.0, newHeight * m_unitRatio - bottomMarginSpinBox->value()));
105 	bottomMarginSpinBox->setMaximum(qMax(0.0,newHeight * m_unitRatio - topMarginSpinBox->value()));
106 	m_pageHeight = newHeight;
107 	setPreset();
108 }
109 
setTop()110 void NewMarginWidget::setTop()
111 {
112 	double newVal=topMarginSpinBox->value() / m_unitRatio;
113 	bottomMarginSpinBox->setMaximum(qMax(0.0, m_pageHeight * m_unitRatio - topMarginSpinBox->value()));
114 	if (marginLinkButton->isChecked() && m_savedPresetItem == PresetLayout::none)
115 	{
116 		m_marginData.set(newVal, newVal, newVal, newVal);
117 		updateMarginSpinValues();
118 	}
119 	else
120 		m_marginData.setTop(newVal);
121 	setPreset();
122 }
123 
setBottom()124 void NewMarginWidget::setBottom()
125 {
126 	double newVal = bottomMarginSpinBox->value() / m_unitRatio;
127 	topMarginSpinBox->setMaximum(qMax(0.0, m_pageHeight * m_unitRatio - bottomMarginSpinBox->value()));
128 	if (marginLinkButton->isChecked() && m_savedPresetItem == PresetLayout::none)
129 	{
130 		m_marginData.set(newVal, newVal, newVal, newVal);
131 		updateMarginSpinValues();
132 	}
133 	else
134 		m_marginData.setBottom(newVal);
135 	setPreset();
136 }
137 
setLeft()138 void NewMarginWidget::setLeft()
139 {
140 	double newVal = leftMarginSpinBox->value() / m_unitRatio;
141 	rightMarginSpinBox->setMaximum(qMax(0.0, m_pageWidth * m_unitRatio - leftMarginSpinBox->value()));
142 	if (marginLinkButton->isChecked() && m_savedPresetItem == PresetLayout::none)
143 	{
144 		m_marginData.set(newVal, newVal, newVal, newVal);
145 		updateMarginSpinValues();
146 	}
147 	else
148 		m_marginData.setLeft(newVal);
149 	setPreset();
150 }
151 
setRight()152 void NewMarginWidget::setRight()
153 {
154 	double newVal = rightMarginSpinBox->value() / m_unitRatio;
155 	leftMarginSpinBox->setMaximum(qMax(0.0, m_pageWidth * m_unitRatio - rightMarginSpinBox->value()));
156 	if (marginLinkButton->isChecked() && m_savedPresetItem == PresetLayout::none)
157 	{
158 		m_marginData.set(newVal, newVal, newVal, newVal);
159 		updateMarginSpinValues();
160 	}
161 	else
162 		m_marginData.setRight(newVal);
163 	setPreset();
164 }
165 
setNewUnit(int newUnitIndex)166 void NewMarginWidget::setNewUnit(int newUnitIndex)
167 {
168 	bool leftSigBlocked   = leftMarginSpinBox->blockSignals(true);
169 	bool rightSigBlocked  = rightMarginSpinBox->blockSignals(true);
170 	bool topSigBlocked    = topMarginSpinBox->blockSignals(true);
171 	bool bottomSigBlocked = bottomMarginSpinBox->blockSignals(true);
172 
173 	m_unitIndex = newUnitIndex;
174 	m_unitRatio = unitGetRatioFromIndex(newUnitIndex);
175 	topMarginSpinBox->setNewUnit(newUnitIndex);
176 	bottomMarginSpinBox->setNewUnit(newUnitIndex);
177 	leftMarginSpinBox->setNewUnit(newUnitIndex);
178 	rightMarginSpinBox->setNewUnit(newUnitIndex);
179 
180 	leftMarginSpinBox->blockSignals(leftSigBlocked);
181 	rightMarginSpinBox->blockSignals(rightSigBlocked);
182 	topMarginSpinBox->blockSignals(topSigBlocked);
183 	bottomMarginSpinBox->blockSignals(bottomSigBlocked);
184 }
185 
setPreset()186 void NewMarginWidget::setPreset()
187 {
188 	if ((m_flags & ShowPreset) == 0)
189 		return;
190 	leftMarginSpinBox->blockSignals(true);
191 	rightMarginSpinBox->blockSignals(true);
192 	topMarginSpinBox->blockSignals(true);
193 	bottomMarginSpinBox->blockSignals(true);
194 	if (m_savedPresetItem == PresetLayout::none)
195 		m_savedMarginData = m_marginData;
196 	int item = presetLayoutComboBox->currentIndex();
197 
198 	MarginStruct marg = presetLayoutComboBox->getMargins(item, m_pageWidth, m_pageHeight, leftMarginSpinBox->value() / m_unitRatio);
199 	presetLayoutComboBox->setEnabled(m_facingPages);
200 
201 	bool restoringValues = false;
202 	if (item == PresetLayout::none && m_savedPresetItem != PresetLayout::none)
203 	{
204 		marg = m_savedMarginData;
205 		restoringValues = true;
206 	}
207 	if (restoringValues || (presetLayoutComboBox->needUpdate() && m_facingPages))
208 	{
209 		m_marginData.set(qMax(0.0, marg.top()), qMax(0.0, marg.left()), qMax(0.0, marg.bottom()), qMax(0.0, marg.right()));
210 		updateMarginSpinValues();
211 
212 		bottomMarginSpinBox->setMaximum(qMax(0.0, m_pageHeight * m_unitRatio - topMarginSpinBox->value()));
213 		topMarginSpinBox->setMaximum(qMax(0.0, m_pageHeight * m_unitRatio - bottomMarginSpinBox->value()));
214 		rightMarginSpinBox->setMaximum(qMax(0.0, m_pageWidth * m_unitRatio - leftMarginSpinBox->value()));
215 		leftMarginSpinBox->setMaximum(qMax(0.0, m_pageWidth * m_unitRatio - rightMarginSpinBox->value()));
216 		rightMarginSpinBox->setEnabled(restoringValues);
217 		topMarginSpinBox->setEnabled(restoringValues);
218 		bottomMarginSpinBox->setEnabled(restoringValues);
219 	}
220 	else
221 	{
222 		rightMarginSpinBox->setEnabled(true);
223 		topMarginSpinBox->setEnabled(true);
224 		bottomMarginSpinBox->setEnabled(true);
225 	}
226 	if (m_pageType == 1)
227 		rightMarginSpinBox->setEnabled(false);
228 	leftMarginSpinBox->setEnabled(item != PresetLayout::nineparts);
229 	if (item != PresetLayout::none)
230 		marginLinkButton->setChecked(false);
231 	marginLinkButton->setEnabled(item == PresetLayout::none || !presetLayoutComboBox->isEnabled());
232 	leftMarginSpinBox->blockSignals(false);
233 	rightMarginSpinBox->blockSignals(false);
234 	topMarginSpinBox->blockSignals(false);
235 	bottomMarginSpinBox->blockSignals(false);
236 	m_savedPresetItem = item;
237 }
238 
setPageSize(const QString & pageSize)239 void NewMarginWidget::setPageSize(const QString& pageSize)
240 {
241 	m_pageSize = pageSize;
242 }
243 
244 
updateMarginSpinValues()245 void NewMarginWidget::updateMarginSpinValues()
246 {
247 	bool leftBlocked = leftMarginSpinBox->blockSignals(true);
248 	bool rightBlocked = rightMarginSpinBox->blockSignals(true);
249 	bool topBlocked = topMarginSpinBox->blockSignals(true);
250 	bool bottomBlocked = bottomMarginSpinBox->blockSignals(true);
251 
252 	topMarginSpinBox->setValue(m_marginData.top() * m_unitRatio);
253 	rightMarginSpinBox->setValue(m_marginData.right() * m_unitRatio);
254 	bottomMarginSpinBox->setValue(m_marginData.bottom() * m_unitRatio);
255 	leftMarginSpinBox->setValue(m_marginData.left() * m_unitRatio);
256 
257 	leftMarginSpinBox->blockSignals(leftBlocked);
258 	rightMarginSpinBox->blockSignals(rightBlocked);
259 	topMarginSpinBox->blockSignals(topBlocked);
260 	bottomMarginSpinBox->blockSignals(bottomBlocked);
261 }
262 
slotLinkMargins()263 void NewMarginWidget::slotLinkMargins()
264 {
265 	bool leftBlocked = leftMarginSpinBox->blockSignals(true);
266 	bool rightBlocked = rightMarginSpinBox->blockSignals(true);
267 	bool topBlocked = topMarginSpinBox->blockSignals(true);
268 	bool bottomBlocked = bottomMarginSpinBox->blockSignals(true);
269 
270 	if (marginLinkButton->isChecked())
271 	{
272 		bottomMarginSpinBox->setValue(leftMarginSpinBox->value());
273 		topMarginSpinBox->setValue(leftMarginSpinBox->value());
274 		rightMarginSpinBox->setValue(leftMarginSpinBox->value());
275 		double newVal = leftMarginSpinBox->value() / m_unitRatio;
276 		m_marginData.set(newVal, newVal, newVal, newVal);
277 	}
278 
279 	leftMarginSpinBox->blockSignals(leftBlocked);
280 	rightMarginSpinBox->blockSignals(rightBlocked);
281 	topMarginSpinBox->blockSignals(topBlocked);
282 	bottomMarginSpinBox->blockSignals(bottomBlocked);
283 }
284 
setMarginPreset(int p)285 void NewMarginWidget::setMarginPreset(int p)
286 {
287 	if ((m_flags & ShowPreset) == 0)
288 		return;
289 	presetLayoutComboBox->blockSignals(true);
290 	m_savedPresetItem = p;
291 	presetLayoutComboBox->setCurrentIndex(p);
292 	if (m_savedPresetItem == PresetLayout::none)
293 		m_savedMarginData = m_marginData;
294 	int item = presetLayoutComboBox->currentIndex();
295 	presetLayoutComboBox->setEnabled(m_facingPages);
296 
297 	bool restoringValues = false;
298 	if ((item == PresetLayout::none) && (m_savedPresetItem != PresetLayout::none))
299 	{
300 		restoringValues = true;
301 	}
302 	if (restoringValues || (presetLayoutComboBox->needUpdate() && m_facingPages))
303 	{
304 		rightMarginSpinBox->setEnabled(restoringValues);
305 		topMarginSpinBox->setEnabled(restoringValues);
306 		bottomMarginSpinBox->setEnabled(restoringValues);
307 	}
308 	else
309 	{
310 		rightMarginSpinBox->setEnabled(true);
311 		topMarginSpinBox->setEnabled(true);
312 		bottomMarginSpinBox->setEnabled(true);
313 	}
314 	if (m_pageType == 1)
315 		rightMarginSpinBox->setEnabled(false);
316 	leftMarginSpinBox->setEnabled(item != PresetLayout::nineparts);
317 	if (item != PresetLayout::none)
318 		marginLinkButton->setChecked(false);
319 	marginLinkButton->setEnabled(item == PresetLayout::none);
320 	presetLayoutComboBox->blockSignals(false);
321 }
322 
setFacingPages(bool facing,int pageType)323 void NewMarginWidget::setFacingPages(bool facing, int pageType)
324 {
325 	m_facingPages = facing;
326 	m_pageType = pageType;
327 	leftMarginLabel->setText(facing ? tr( "&Inside:" ) : tr( "&Left:" ));
328 	rightMarginLabel->setText(facing ? tr( "O&utside:" ) : tr( "&Right:" ));
329 	setPreset();
330 }
331 
setMarginsToPrinterMargins()332 void NewMarginWidget::setMarginsToPrinterMargins()
333 {
334 	QSizeF pageDimensions(m_pageWidth, m_pageHeight);
335 	UsePrinterMarginsDialog upm(parentWidget(), pageDimensions, m_pageSize, unitGetRatioFromIndex(m_unitIndex), unitGetSuffixFromIndex(m_unitIndex));
336 	if (upm.exec() != QDialog::Accepted)
337 		return;
338 
339 	double t, b, l, r;
340 	upm.getNewPrinterMargins(t, b, l, r);
341 	presetLayoutComboBox->setCurrentIndex(PresetLayout::none);
342 	m_marginData.set(t, l, b, r);
343 
344 	updateMarginSpinValues();
345 
346 	bottomMarginSpinBox->setMaximum((qMax(0.0, m_pageHeight - t) * m_unitRatio));
347 	topMarginSpinBox->setMaximum((qMax(0.0, m_pageHeight - b) * m_unitRatio));
348 	rightMarginSpinBox->setMaximum((qMax(0.0, m_pageWidth - l) * m_unitRatio));
349 	leftMarginSpinBox->setMaximum((qMax(0.0, m_pageWidth - r) * m_unitRatio));
350 
351 	rightMarginSpinBox->setEnabled(true);
352 	topMarginSpinBox->setEnabled(true);
353 	bottomMarginSpinBox->setEnabled(true);
354 }
355 
356