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 #include <algorithm>
8
9 #include <QButtonGroup>
10 #include <QFileDialog>
11
12 #include "ui/prefs_documentsetup.h"
13 #include "commonstrings.h"
14 #include "ui/newmarginwidget.h"
15 #include "langmgr.h"
16 #include "pagesize.h"
17 #include "prefsfile.h"
18 #include "prefsmanager.h"
19 #include "prefsstructs.h"
20 #include "undomanager.h"
21 #include "units.h"
22 #include "util.h"
23 #include "util_text.h"
24
Prefs_DocumentSetup(QWidget * parent,ScribusDoc * doc)25 Prefs_DocumentSetup::Prefs_DocumentSetup(QWidget* parent, ScribusDoc* doc)
26 : Prefs_Pane(parent),
27 m_doc(doc)
28 {
29 setupUi(this);
30
31 scrollArea->viewport()->setAutoFillBackground(false);
32 scrollArea->widget()->setAutoFillBackground(false);
33
34 m_caption = tr("Document Setup");
35 m_icon = "scribusdoc16.png";
36
37 unitRatio = 1.0;
38 pageW = pageH = 1.0;
39
40 if (!m_doc)
41 {
42 applySizesToAllPagesCheckBox->hide();
43 applySizesToAllMasterPagesCheckBox->hide();
44 applyMarginsToAllPagesCheckBox->hide();
45 applyMarginsToAllMasterPagesCheckBox->hide();
46 pageSizeLinkToolButton->hide(); //temp
47 // connect(pageSizeLinkToolButton, SIGNAL(clicked()), this, SLOT(emitSectionChange()));
48 }
49 else
50 {
51 pageSizeLinkToolButton->hide();
52 emergencyCheckBox->hide();
53 }
54
55 QStringList languageList;
56 LanguageManager::instance()->fillInstalledStringList(&languageList);
57 std::sort(languageList.begin(), languageList.end(), localeAwareLessThan);
58 languageComboBox->addItems( languageList );
59
60 pageLayoutButtonGroup->setId(singlePageRadioButton,0);
61 pageLayoutButtonGroup->setId(facingPagesRadioButton,1);
62 pageLayoutButtonGroup->setId(threeFoldRadioButton,2);
63 pageLayoutButtonGroup->setId(fourFoldRadioButton,3);
64 singlePageRadioButton->setChecked(true);
65 layoutFirstPageIsComboBox->clear();
66 layoutFirstPageIsComboBox->addItem(" ");
67 layoutFirstPageIsComboBox->setCurrentIndex(0);
68 layoutFirstPageIsComboBox->setEnabled(false);
69
70 pageWidthSpinBox->setMaximum(16777215);
71 pageHeightSpinBox->setMaximum(16777215);
72 languageChange();
73
74 connect(pageSizeComboBox, SIGNAL(activated(const QString &)), this, SLOT(setPageSize()));
75 connect(pageOrientationComboBox, SIGNAL(activated(int)), this, SLOT(setPageOrientation(int)));
76 connect(pageWidthSpinBox, SIGNAL(valueChanged(double)), this, SLOT(setPageWidth(double)));
77 connect(pageHeightSpinBox, SIGNAL(valueChanged(double)), this, SLOT(setPageHeight(double)));
78 connect(pageLayoutButtonGroup, SIGNAL(buttonClicked(int)), this, SLOT(pageLayoutChanged(int)));
79 connect(pageUnitsComboBox, SIGNAL(activated(int)), this, SLOT(unitChange()));
80 connect(undoCheckBox, SIGNAL(toggled(bool)), this, SLOT(slotUndo(bool)));
81 connect(changeAutoDir, SIGNAL(clicked()), this, SLOT(changeAutoDocDir()));
82 }
83
~Prefs_DocumentSetup()84 Prefs_DocumentSetup::~Prefs_DocumentSetup()
85 {
86 }
87
unitChange()88 void Prefs_DocumentSetup::unitChange()
89 {
90 pageWidthSpinBox->blockSignals(true);
91 pageHeightSpinBox->blockSignals(true);
92
93 int docUnitIndex = pageUnitsComboBox->currentIndex();
94 pageWidthSpinBox->setNewUnit(docUnitIndex);
95 pageHeightSpinBox->setNewUnit(docUnitIndex);
96 unitRatio = unitGetRatioFromIndex(docUnitIndex);
97 pageWidthSpinBox->setValue(pageW * unitRatio);
98 pageHeightSpinBox->setValue(pageH * unitRatio);
99 marginsWidget->setNewUnit(docUnitIndex);
100 marginsWidget->setPageHeight(pageH);
101 marginsWidget->setPageWidth(pageW);
102 bleedsWidget->setNewUnit(docUnitIndex);
103 bleedsWidget->setPageHeight(pageH);
104 bleedsWidget->setPageWidth(pageW);
105
106 pageWidthSpinBox->blockSignals(false);
107 pageHeightSpinBox->blockSignals(false);
108
109 emit prefsChangeUnits(docUnitIndex);
110 }
111
languageChange()112 void Prefs_DocumentSetup::languageChange()
113 {
114 int i=0;
115
116 i=pageOrientationComboBox->currentIndex();
117 pageOrientationComboBox->clear();
118 pageOrientationComboBox->addItem( tr( "Portrait" ) );
119 pageOrientationComboBox->addItem( tr( "Landscape" ) );
120 pageOrientationComboBox->setCurrentIndex(i<0?0:i);
121
122 i=pageUnitsComboBox->currentIndex();
123 pageUnitsComboBox->clear();
124 pageUnitsComboBox->addItems(unitGetTextUnitList());
125 pageUnitsComboBox->setCurrentIndex(i<0?0:i);
126
127 setupPageSets();
128
129 pageWidthSpinBox->setToolTip( "<qt>" + tr( "Width of document pages, editable if you have chosen a custom page size" ) + "</qt>" );
130 pageHeightSpinBox->setToolTip( "<qt>" + tr( "Height of document pages, editable if you have chosen a custom page size" ) + "</qt>" );
131 pageSizeComboBox->setToolTip( "<qt>" + tr( "Default page size, either a standard size or a custom size. More page sizes can be made visible by activating them in Preferences." ) + "</qt>" );
132 pageSizeLinkToolButton->setToolTip( "<qt>" + tr( "Enable or disable more page sizes by jumping to Page Size preferences" ) + "</qt>" );
133 pageOrientationComboBox->setToolTip( "<qt>" + tr( "Default orientation of document pages" ) + "</qt>" );
134 pageUnitsComboBox->setToolTip( "<qt>" + tr( "Default unit of measurement for document editing" ) + "</qt>" );
135 autosaveCheckBox->setToolTip( "<qt>" + tr( "When enabled, Scribus saves backup copys of your file each time the time period elapses" ) + "</qt>" );
136 autosaveIntervalSpinBox->setToolTip( "<qt>" + tr( "Time period between saving automatically" ) + "</qt>" );
137 undoLengthSpinBox->setToolTip( "<qt>" + tr("Set the length of the action history in steps. If set to 0 infinite amount of actions will be stored.") + "</qt>");
138 applySizesToAllPagesCheckBox->setToolTip( "<qt>" + tr( "Apply the page size changes to all existing pages in the document" ) + "</qt>" );
139 applyMarginsToAllPagesCheckBox->setToolTip( "<qt>" + tr( "Apply the page size changes to all existing master pages in the document" ) + "</qt>" );
140 autosaveCountSpinBox->setToolTip("<qt>" + tr("Keep this many files during the editing session. Backup files will be removed when you close the document.") + "</qt>");
141 }
142
restoreDefaults(struct ApplicationPrefs * prefsData)143 void Prefs_DocumentSetup::restoreDefaults(struct ApplicationPrefs *prefsData)
144 {
145 pageWidthSpinBox->blockSignals(true);
146 pageHeightSpinBox->blockSignals(true);
147 pageOrientationComboBox->blockSignals(true);
148 pageSizeComboBox->blockSignals(true);
149
150 setCurrentComboItem(languageComboBox, LanguageManager::instance()->getLangFromAbbrev(prefsData->docSetupPrefs.language, true));
151
152 unitRatio = unitGetRatioFromIndex(prefsData->docSetupPrefs.docUnitIndex);
153 setupPageSizes(prefsData);
154
155 pageOrientationComboBox->setCurrentIndex(prefsData->docSetupPrefs.pageOrientation);
156 pageUnitsComboBox->setCurrentIndex(prefsData->docSetupPrefs.docUnitIndex);
157 pageW = prefsData->docSetupPrefs.pageWidth;
158 pageH = prefsData->docSetupPrefs.pageHeight;
159 pageWidthSpinBox->setValue(pageW * unitRatio);
160 pageHeightSpinBox->setValue(pageH * unitRatio);
161 pageSets=prefsData->pageSets;
162 if (prefsData->docSetupPrefs.pagePositioning < 2)
163 {
164 threeFoldRadioButton->hide();
165 fourFoldRadioButton->hide();
166 }
167 switch (prefsData->docSetupPrefs.pagePositioning)
168 {
169 case 0:
170 singlePageRadioButton->setChecked(true);
171 break;
172 case 1:
173 facingPagesRadioButton->setChecked(true);
174 break;
175 case 2:
176 threeFoldRadioButton->setChecked(true);
177 break;
178 case 3:
179 fourFoldRadioButton->setChecked(true);
180 break;
181 }
182 setupPageSets();
183
184 layoutFirstPageIsComboBox->setCurrentIndex(prefsData->pageSets[prefsData->docSetupPrefs.pagePositioning].FirstPage);
185
186 pageWidthSpinBox->blockSignals(false);
187 pageHeightSpinBox->blockSignals(false);
188 pageOrientationComboBox->blockSignals(false);
189 pageSizeComboBox->blockSignals(false);
190
191 marginsWidget->setup(prefsData->docSetupPrefs.margins, prefsData->docSetupPrefs.pagePositioning, prefsData->docSetupPrefs.docUnitIndex, NewMarginWidget::MarginWidgetFlags);
192 marginsWidget->setPageWidth(prefsData->docSetupPrefs.pageWidth);
193 marginsWidget->setPageHeight(prefsData->docSetupPrefs.pageHeight);
194 // marginsWidget->setPageSize(prefsPageSizeName);
195 marginsWidget->setMarginPreset(prefsData->docSetupPrefs.marginPreset);
196 bleedsWidget->setup(prefsData->docSetupPrefs.bleeds, prefsData->docSetupPrefs.pagePositioning, prefsData->docSetupPrefs.docUnitIndex, NewMarginWidget::BleedWidgetFlags);
197 bleedsWidget->setPageWidth(prefsData->docSetupPrefs.pageWidth);
198 bleedsWidget->setPageHeight(prefsData->docSetupPrefs.pageHeight);
199 // bleedsWidget->setPageSize(prefsPageSizeName);
200 bleedsWidget->setMarginPreset(prefsData->docSetupPrefs.marginPreset);
201 saveCompressedCheckBox->setChecked(prefsData->docSetupPrefs.saveCompressed);
202 emergencyCheckBox->setChecked(prefsData->miscPrefs.saveEmergencyFile);
203 autosaveCheckBox->setChecked( prefsData->docSetupPrefs.AutoSave );
204 autosaveIntervalSpinBox->setValue(prefsData->docSetupPrefs.AutoSaveTime / 1000 / 60);
205 autosaveCountSpinBox->setValue(prefsData->docSetupPrefs.AutoSaveCount);
206 autosaveKeepCheckBox->setChecked(prefsData->docSetupPrefs.AutoSaveKeep);
207 autosaveDocRadio->setChecked(prefsData->docSetupPrefs.AutoSaveLocation);
208 autosaveDirRadio->setChecked(!prefsData->docSetupPrefs.AutoSaveLocation);
209 autosaveDirEdit->setText(prefsData->docSetupPrefs.AutoSaveDir);
210 autosaveDirEdit->setEnabled(!prefsData->docSetupPrefs.AutoSaveLocation);
211 changeAutoDir->setEnabled(!prefsData->docSetupPrefs.AutoSaveLocation);
212 showAutosaveClockOnCanvasCheckBox->setChecked(prefsData->displayPrefs.showAutosaveClockOnCanvas);
213 undoCheckBox->setChecked(PrefsManager::instance().prefsFile->getContext("undo")->getBool("enabled", true));
214 int undoLength = UndoManager::instance()->getHistoryLength();
215 if (undoLength == -1)
216 undoLengthSpinBox->setEnabled(false);
217 else
218 undoLengthSpinBox->setValue(undoLength);
219 unitChange();
220 }
221
saveGuiToPrefs(struct ApplicationPrefs * prefsData) const222 void Prefs_DocumentSetup::saveGuiToPrefs(struct ApplicationPrefs *prefsData) const
223 {
224 prefsData->docSetupPrefs.language = LanguageManager::instance()->getAbbrevFromLang(languageComboBox->currentText(), false);
225 prefsData->docSetupPrefs.pageSize = prefsPageSizeName;
226 prefsData->docSetupPrefs.pageOrientation = pageOrientationComboBox->currentIndex();
227 prefsData->docSetupPrefs.docUnitIndex = pageUnitsComboBox->currentIndex();
228 prefsData->docSetupPrefs.pageWidth = pageW;
229 prefsData->docSetupPrefs.pageHeight = pageH;
230 prefsData->docSetupPrefs.pagePositioning = pageLayoutButtonGroup->checkedId();
231 prefsData->pageSets[prefsData->docSetupPrefs.pagePositioning].FirstPage = layoutFirstPageIsComboBox->currentIndex();
232
233 prefsData->docSetupPrefs.margins = marginsWidget->margins();
234 prefsData->docSetupPrefs.bleeds = bleedsWidget->margins();
235 prefsData->docSetupPrefs.saveCompressed = saveCompressedCheckBox->isChecked();
236 prefsData->miscPrefs.saveEmergencyFile = emergencyCheckBox->isChecked();
237 prefsData->docSetupPrefs.AutoSave=autosaveCheckBox->isChecked();
238 prefsData->docSetupPrefs.AutoSaveTime = autosaveIntervalSpinBox->value() * 1000 * 60;
239 prefsData->docSetupPrefs.AutoSaveCount = autosaveCountSpinBox->value();
240 prefsData->docSetupPrefs.AutoSaveKeep = autosaveKeepCheckBox->isChecked();
241 prefsData->docSetupPrefs.AutoSaveLocation = autosaveDocRadio->isChecked();
242 prefsData->docSetupPrefs.AutoSaveDir = autosaveDirEdit->text();
243 prefsData->displayPrefs.showAutosaveClockOnCanvas=showAutosaveClockOnCanvasCheckBox->isChecked();
244 bool undoActive = undoCheckBox->isChecked();
245 if (!undoActive)
246 UndoManager::instance()->clearStack();
247 UndoManager::instance()->setUndoEnabled(undoActive);
248 UndoManager::instance()->setAllHistoryLengths(undoLengthSpinBox->value());
249 static PrefsContext *undoPrefs = PrefsManager::instance().prefsFile->getContext("undo");
250 undoPrefs->set("enabled", undoActive);
251 }
252
setupPageSets()253 void Prefs_DocumentSetup::setupPageSets()
254 {
255 int i = layoutFirstPageIsComboBox->currentIndex();
256 if (!layoutFirstPageIsComboBox->isEnabled())
257 i = -1;
258 int currIndex = pageLayoutButtonGroup->checkedId() < 0 ? 0 :pageLayoutButtonGroup->checkedId();
259 layoutFirstPageIsComboBox->clear();
260 if (currIndex > 0 && currIndex < pageSets.count())
261 {
262 const PageSet& pageSet = pageSets.at(currIndex);
263 const QStringList& pageNames = pageSet.pageNames;
264 layoutFirstPageIsComboBox->setEnabled(true);
265 for (const QString& pageName : pageNames)
266 layoutFirstPageIsComboBox->addItem(CommonStrings::translatePageSetLocString(pageName));
267 int firstPageIndex = i < 0 ? pageSet.FirstPage : i;
268 firstPageIndex = qMax(0, qMin(firstPageIndex, pageSet.pageNames.count() - 1));
269 layoutFirstPageIsComboBox->setCurrentIndex(firstPageIndex);
270 }
271 else
272 {
273 layoutFirstPageIsComboBox->addItem(" ");
274 layoutFirstPageIsComboBox->setCurrentIndex(0);
275 layoutFirstPageIsComboBox->setEnabled(false);
276 }
277 }
278
setupPageSizes(struct ApplicationPrefs * prefsData)279 void Prefs_DocumentSetup::setupPageSizes(struct ApplicationPrefs *prefsData)
280 {
281 PageSize ps(prefsData->docSetupPrefs.pageSize);
282 QStringList insertList(ps.activeSizeList());
283 QStringList insertTrList(ps.activeSizeTRList());
284
285 prefsPageSizeName = prefsData->docSetupPrefs.pageSize;
286 if (prefsPageSizeName == CommonStrings::trCustomPageSize)
287 prefsPageSizeName = CommonStrings::customPageSize;
288 if ((prefsPageSizeName != CommonStrings::customPageSize) &&
289 (prefsPageSizeName != CommonStrings::trCustomPageSize) &&
290 (insertList.indexOf(prefsPageSizeName) == -1))
291 {
292 insertList << prefsPageSizeName;
293 insertTrList << prefsPageSizeName;
294 }
295
296 QMap<QString, QString> insertMap;
297 for (int i = 0; i < insertTrList.count(); ++i)
298 {
299 const QString& key = insertTrList.at(i);
300 insertMap[key] = insertList.at(i);
301 }
302 insertTrList.sort();
303
304 pageSizeComboBox->clear();
305 for (int i = 0; i < insertList.count(); ++i)
306 {
307 const QString& key = insertTrList.at(i);
308 pageSizeComboBox->addItem(key, insertMap[key]);
309 }
310 pageSizeComboBox->addItem(CommonStrings::trCustomPageSize, CommonStrings::customPageSize);
311
312 QString pageSizeName = CommonStrings::trCustomPageSize;
313 int index = pageSizeComboBox->findData(prefsPageSizeName);
314 if (index >= 0)
315 pageSizeName = pageSizeComboBox->itemText(index);
316 setCurrentComboItem(pageSizeComboBox, pageSizeName);
317 marginsWidget->setPageSize(prefsPageSizeName);
318 bleedsWidget->setPageSize(prefsPageSizeName);
319 }
320
pageLayoutChanged(int i)321 void Prefs_DocumentSetup::pageLayoutChanged(int i)
322 {
323 setupPageSets();
324 marginsWidget->setFacingPages(!(i == singlePage));
325 //layoutFirstPageIsComboBox->setCurrentIndex(pageSets[pageLayoutButtonGroup->checkedId()].FirstPage);
326 }
327
setPageWidth(double w)328 void Prefs_DocumentSetup::setPageWidth(double w)
329 {
330 pageW = pageWidthSpinBox->value() / unitRatio;
331 marginsWidget->setPageWidth(pageW);
332 QString psText=pageSizeComboBox->currentText();
333 if (psText!=CommonStrings::trCustomPageSize && psText!=CommonStrings::customPageSize)
334 pageSizeComboBox->setCurrentIndex(pageSizeComboBox->count()-1);
335 int newOrientation = (pageWidthSpinBox->value() > pageHeightSpinBox->value()) ? landscapePage : portraitPage;
336 if (newOrientation != pageOrientationComboBox->currentIndex())
337 {
338 pageOrientationComboBox->blockSignals(true);
339 pageOrientationComboBox->setCurrentIndex(newOrientation);
340 pageOrientationComboBox->blockSignals(false);
341 }
342 }
343
setPageHeight(double h)344 void Prefs_DocumentSetup::setPageHeight(double h)
345 {
346 pageH = pageHeightSpinBox->value() / unitRatio;
347 marginsWidget->setPageHeight(pageH);
348 QString psText=pageSizeComboBox->currentText();
349 if (psText!=CommonStrings::trCustomPageSize && psText!=CommonStrings::customPageSize)
350 pageSizeComboBox->setCurrentIndex(pageSizeComboBox->count()-1);
351 int newOrientation = (pageWidthSpinBox->value() > pageHeightSpinBox->value()) ? landscapePage : portraitPage;
352 if (newOrientation != pageOrientationComboBox->currentIndex())
353 {
354 pageOrientationComboBox->blockSignals(true);
355 pageOrientationComboBox->setCurrentIndex(newOrientation);
356 pageOrientationComboBox->blockSignals(false);
357 }
358 }
359
setPageOrientation(int orientation)360 void Prefs_DocumentSetup::setPageOrientation(int orientation)
361 {
362 setSize(pageSizeComboBox->currentText());
363 pageWidthSpinBox->blockSignals(true);
364 pageHeightSpinBox->blockSignals(true);
365 if ((orientation==0 && pageSizeComboBox->currentText() == CommonStrings::trCustomPageSize) || orientation!=0)
366 {
367 double w = pageWidthSpinBox->value(), h = pageHeightSpinBox->value();
368 pageWidthSpinBox->setValue((orientation == portraitPage) ? qMin(w, h) : qMax(w, h));
369 pageHeightSpinBox->setValue((orientation == portraitPage) ? qMax(w, h) : qMin(w, h));
370 }
371 pageW = pageWidthSpinBox->value() / unitRatio;
372 pageH = pageHeightSpinBox->value() / unitRatio;
373 pageWidthSpinBox->blockSignals(false);
374 pageHeightSpinBox->blockSignals(false);
375 }
376
setPageSize()377 void Prefs_DocumentSetup::setPageSize()
378 {
379 setPageOrientation(pageOrientationComboBox->currentIndex());
380 }
381
setSize(const QString & newSize)382 void Prefs_DocumentSetup::setSize(const QString &newSize)
383 {
384 pageW = pageWidthSpinBox->value() / unitRatio;
385 pageH = pageHeightSpinBox->value() / unitRatio;
386 PageSize *ps2=new PageSize(newSize);
387
388 prefsPageSizeName=ps2->name();
389 if (newSize != CommonStrings::trCustomPageSize)
390 {
391 pageW = ps2->width();
392 pageH = ps2->height();
393 }
394 else
395 prefsPageSizeName = CommonStrings::customPageSize;
396 pageWidthSpinBox->blockSignals(true);
397 pageHeightSpinBox->blockSignals(true);
398 pageWidthSpinBox->setValue(pageW * unitRatio);
399 pageHeightSpinBox->setValue(pageH * unitRatio);
400 marginsWidget->setPageHeight(pageH);
401 marginsWidget->setPageWidth(pageW);
402 marginsWidget->setPageSize(newSize);
403 pageWidthSpinBox->blockSignals(false);
404 pageHeightSpinBox->blockSignals(false);
405 delete ps2;
406 }
407
slotUndo(bool isEnabled)408 void Prefs_DocumentSetup::slotUndo(bool isEnabled)
409 {
410 undoLengthSpinBox->setEnabled(isEnabled);
411 }
412
getResizeDocumentPages(bool & resizePages,bool & resizeMasterPages,bool & resizePageMargins,bool & resizeMasterPageMargins)413 void Prefs_DocumentSetup::getResizeDocumentPages(bool &resizePages, bool &resizeMasterPages, bool &resizePageMargins, bool &resizeMasterPageMargins)
414 {
415 resizePages=applySizesToAllPagesCheckBox->isChecked();
416 resizeMasterPages=applySizesToAllMasterPagesCheckBox->isChecked();
417 resizePageMargins=applyMarginsToAllPagesCheckBox->isChecked();
418 resizeMasterPageMargins=applyMarginsToAllMasterPagesCheckBox->isChecked();
419 }
420
changeAutoDocDir()421 void Prefs_DocumentSetup::changeAutoDocDir()
422 {
423 QString s = QFileDialog::getExistingDirectory(this, tr("Choose a Directory"), autosaveDirEdit->text());
424 if (!s.isEmpty())
425 autosaveDirEdit->setText( QDir::toNativeSeparators(s) );
426 }
427
emitSectionChange()428 void Prefs_DocumentSetup::emitSectionChange()
429 {
430 emit changeToOtherSection("Prefs_PageSizes");
431 }
432