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 "newdocdialog.h"
8 
9 #include <QGridLayout>
10 #include <QHBoxLayout>
11 #include <QVBoxLayout>
12 
13 #include <QDir>
14 #include <QCheckBox>
15 #include <QComboBox>
16 #include <QFileDialog>
17 #include <QFrame>
18 #include <QGroupBox>
19 #include <QLabel>
20 #include <QListWidgetItem>
21 #include <QPixmap>
22 #include <QPoint>
23 #include <QPushButton>
24 #include <QSpacerItem>
25 #include <QSpinBox>
26 #include <QStandardPaths>
27 #include <QStringList>
28 #include <QTabWidget>
29 #include <QToolTip>
30 
31 #include "scconfig.h"
32 
33 #include "commonstrings.h"
34 #include "fileloader.h"
35 #include "marginwidget.h"
36 #include "pagesize.h"
37 #include "scribuscore.h"
38 #include "pagelayout.h"
39 #include "pagestructs.h"
40 #include "prefsfile.h"
41 #include "prefsmanager.h"
42 #include "filedialogeventcatcher.h"
43 #include "scrspinbox.h"
44 #include "units.h"
45 #include "iconmanager.h"
46 
PageLayoutsWidget(QWidget * parent)47 PageLayoutsWidget::PageLayoutsWidget(QWidget* parent) :
48 	QListWidget(parent)
49 {
50 	setDragEnabled(false);
51 	setViewMode(QListView::IconMode);
52 	setFlow(QListView::LeftToRight);
53 	setSortingEnabled(false);
54 	setWrapping(false);
55 	setWordWrap(true);
56 	setAcceptDrops(false);
57 	setDropIndicatorShown(false);
58 	setDragDropMode(QAbstractItemView::NoDragDrop);
59 	setResizeMode(QListView::Adjust);
60 	setSelectionMode(QAbstractItemView::SingleSelection);
61 	setFocusPolicy(Qt::NoFocus);
62 	setIconSize(QSize(32, 32));
63 	clear();
64 	setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
65 }
66 
arrangeIcons()67 void PageLayoutsWidget::arrangeIcons()
68 {
69 	QListWidgetItem* ic;
70 	int startY = 5;
71 	int startX = 5;
72 	setResizeMode(QListView::Fixed);
73 	int maxSizeY = 0;
74 	for (int cc = 0; cc < count(); ++cc)
75 	{
76 		ic = item(cc);
77 		QRect ir = visualItemRect(ic);
78 		setPositionForIndex(QPoint(startX, startY), indexFromItem(ic));
79 		startX += ir.width() + 5;
80 		maxSizeY = qMax(maxSizeY, ir.height());
81 	}
82 	maxX = startX;
83 	maxY = maxSizeY + 10;
84 }
85 
minimumSizeHint() const86 QSize PageLayoutsWidget::minimumSizeHint() const
87 {
88 	return QSize(maxX, maxY);
89 }
90 
NewDocDialog(QWidget * parent,const QStringList & recentDocs,bool startUp,const QString & lang)91 NewDocDialog::NewDocDialog(QWidget* parent, const QStringList& recentDocs, bool startUp, const QString& lang) : QDialog(parent),
92 	prefsManager(PrefsManager::instance())
93 {
94 	setObjectName(QString::fromLocal8Bit("NewDocumentWindow"));
95 	setModal(true);
96 
97 	m_onStartup = startUp;
98 	m_unitIndex = prefsManager.appPrefs.docSetupPrefs.docUnitIndex;
99 	m_unitRatio = unitGetRatioFromIndex(m_unitIndex);
100 	m_unitSuffix = unitGetSuffixFromIndex(m_unitIndex);
101 	m_orientation = prefsManager.appPrefs.docSetupPrefs.pageOrientation;
102 
103 	setWindowTitle( tr( "New Document" ) );
104 	setWindowIcon(IconManager::instance().loadIcon("AppIcon.png"));
105 	if (startUp)
106 		setContentsMargins(0, 0, 0, 0);
107 	else
108 		setContentsMargins(9, 9, 9, 9);
109 	TabbedNewDocLayout = new QVBoxLayout( this );
110 	if (startUp)
111 		TabbedNewDocLayout->setContentsMargins(9, 9, 9, 9);
112 	else
113 		TabbedNewDocLayout->setContentsMargins(0, 0, 0, 0);
114 	TabbedNewDocLayout->setSpacing(6);
115 	createNewDocPage();
116 	if (startUp)
117 	{
118 		tabWidget = new QTabWidget( this );
119 		tabWidget->addTab(newDocFrame, tr("&New Document"));
120 		createNewFromTempPage();
121 		nftGui->setupSettings(lang);
122 		tabWidget->addTab(newFromTempFrame, tr("New &from Template"));
123 		createOpenDocPage();
124 		tabWidget->addTab(openDocFrame, tr("Open &Existing Document"));
125 		recentDocList = recentDocs;
126  		createRecentDocPage();
127  		tabWidget->addTab(recentDocFrame, tr("Open Recent &Document"));
128  		TabbedNewDocLayout->addWidget(tabWidget);
129 	}
130 	else
131 		TabbedNewDocLayout->addWidget(newDocFrame);
132 
133 	Layout1 = new QHBoxLayout;
134 	Layout1->setContentsMargins(0, 0, 0, 0);
135 	Layout1->setSpacing(6);
136 	if (startUp)
137 	{
138 		startUpDialog = new QCheckBox( tr( "Do not show this dialog again" ), this );
139 		startUpDialog->setChecked(!prefsManager.appPrefs.uiPrefs.showStartupDialog);
140 		Layout1->addWidget( startUpDialog );
141 	}
142 	QSpacerItem* spacer = new QSpacerItem( 2, 2, QSizePolicy::Expanding, QSizePolicy::Minimum );
143 	Layout1->addItem( spacer );
144 	OKButton = new QPushButton( CommonStrings::tr_OK, this );
145 	OKButton->setDefault( true );
146 	Layout1->addWidget( OKButton );
147 	CancelB = new QPushButton( CommonStrings::tr_Cancel, this );
148 	CancelB->setAutoDefault( false );
149 	Layout1->addWidget( CancelB );
150 	TabbedNewDocLayout->addLayout( Layout1 );
151 	//tooltips
152 	pageSizeComboBox->setToolTip( tr( "Document page size, either a standard size or a custom size" ) );
153 	pageOrientationComboBox->setToolTip( tr( "Orientation of the document's pages" ) );
154 	widthSpinBox->setToolTip( tr( "Width of the document's pages, editable if you have chosen a custom page size" ) );
155 	heightSpinBox->setToolTip( tr( "Height of the document's pages, editable if you have chosen a custom page size" ) );
156 	pageCountSpinBox->setToolTip( tr( "Initial number of pages of the document" ) );
157 	unitOfMeasureComboBox->setToolTip( tr( "Default unit of measurement for document editing" ) );
158 	autoTextFrame->setToolTip( tr( "Create text frames automatically when new pages are added" ) );
159 	numberOfCols->setToolTip( tr( "Number of columns to create in automatically created text frames" ) );
160 	Distance->setToolTip( tr( "Distance between automatically created columns" ) );
161 
162 	// signals and slots connections
163 	connect( OKButton, SIGNAL( clicked() ), this, SLOT( ExitOK() ) );
164 	connect( CancelB, SIGNAL( clicked() ), this, SLOT( reject() ) );
165 	connect(pageSizeComboBox, SIGNAL(activated(const QString &)), this, SLOT(setPageSize(const QString &)));
166 	connect(pageOrientationComboBox, SIGNAL(activated(int)), this, SLOT(setOrientation(int)));
167 	connect(unitOfMeasureComboBox, SIGNAL(activated(int)), this, SLOT(setUnit(int)));
168 	connect(Distance, SIGNAL(valueChanged(double)), this, SLOT(setDistance(double)));
169 	connect(autoTextFrame, SIGNAL(clicked()), this, SLOT(handleAutoFrame()));
170 	connect(layoutsView, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(itemSelected(QListWidgetItem* )));
171 	connect(layoutsView, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(itemSelected(QListWidgetItem* )));
172 	connect(layoutsView, SIGNAL(itemActivated(QListWidgetItem*)), this, SLOT(itemSelected(QListWidgetItem* )));
173 	connect(layoutsView, SIGNAL(itemPressed(QListWidgetItem*)), this, SLOT(itemSelected(QListWidgetItem* )));
174 	if (startUp)
175 	{
176 		connect(nftGui, SIGNAL(leaveOK()), this, SLOT(ExitOK()));
177 		connect(recentDocListBox, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(recentDocListBox_doubleClicked()));
178 		connect(tabWidget, SIGNAL(currentChanged(int)), this, SLOT(adjustTitles(int)));
179 	}
180 }
181 
createNewDocPage()182 void NewDocDialog::createNewDocPage()
183 {
184 	newDocFrame = new QFrame(this);
185 
186 	pageSizeGroupBox = new QGroupBox(newDocFrame );
187 	pageSizeGroupBox->setTitle( tr( "Document Layout" ) );
188 	pageSizeGroupBoxLayout = new QGridLayout( pageSizeGroupBox );
189 	pageSizeGroupBoxLayout->setContentsMargins(9, 9, 9, 9);
190 	pageSizeGroupBoxLayout->setSpacing(6);
191 	pageSizeGroupBoxLayout->setAlignment( Qt::AlignTop );
192 
193 	layoutsView = new PageLayoutsWidget( pageSizeGroupBox );
194 	layoutsView->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum);
195 	for (int i = 0; i < prefsManager.appPrefs.pageSets.count(); ++i)
196 	{
197 		QListWidgetItem *ic;
198 		QString psname=CommonStrings::translatePageSetString(prefsManager.appPrefs.pageSets[i].Name);
199 		if (i == 0)
200 		{
201 			ic = new QListWidgetItem( IconManager::instance().loadIcon("32/page-simple.png"), psname, layoutsView );
202 			ic->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
203 		}
204 		else if (i == 1)
205 		{
206 			ic = new QListWidgetItem( IconManager::instance().loadIcon("32/page-doublesided.png"), psname, layoutsView );
207 			ic->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
208 		}
209 		else if (i == 2 && prefsManager.appPrefs.docSetupPrefs.pagePositioning == 2)
210 		{
211 			ic = new QListWidgetItem( IconManager::instance().loadIcon("32/page-3fold.png"), psname, layoutsView );
212 			ic->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
213 		}
214 		else if (i == 3 && prefsManager.appPrefs.docSetupPrefs.pagePositioning == 3)
215 		{
216 			ic = new QListWidgetItem( IconManager::instance().loadIcon("32/page-4fold.png"), psname, layoutsView );
217 			ic->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
218 		}
219 	}
220 	layoutsView->arrangeIcons();
221 	pageSizeGroupBoxLayout->addWidget( layoutsView, 0, 0, 5, 1 );
222 	layoutsView->arrangeIcons();
223 
224 	TextLabel1 = new QLabel( tr( "&Size:" ), pageSizeGroupBox );
225 	pageSizeGroupBoxLayout->addWidget( TextLabel1, 0, 1 );
226 	PageSize ps(prefsManager.appPrefs.docSetupPrefs.pageSize);
227 	pageSizeComboBox = new QComboBox( pageSizeGroupBox );
228 	pageSizeComboBox->addItems(ps.activeSizeTRList());
229 	pageSizeComboBox->addItem( CommonStrings::trCustomPageSize );
230 	pageSizeComboBox->setEditable(false);
231 	TextLabel1->setBuddy(pageSizeComboBox);
232 	pageSizeGroupBoxLayout->addWidget(pageSizeComboBox, 0, 2 );
233 	TextLabel2 = new QLabel( tr( "Orie&ntation:" ), pageSizeGroupBox );
234 	pageSizeGroupBoxLayout->addWidget( TextLabel2, 1, 1 );
235 	pageOrientationComboBox = new QComboBox( pageSizeGroupBox );
236 	pageOrientationComboBox->addItem( tr( "Portrait" ) );
237 	pageOrientationComboBox->addItem( tr( "Landscape" ) );
238 	pageOrientationComboBox->setEditable(false);
239 	pageOrientationComboBox->setCurrentIndex(prefsManager.appPrefs.docSetupPrefs.pageOrientation);
240 	TextLabel2->setBuddy(pageOrientationComboBox);
241 	pageSizeGroupBoxLayout->addWidget( pageOrientationComboBox, 1, 2 );
242 
243 	TextLabel1_2 = new QLabel( tr( "&Width:" ), pageSizeGroupBox );
244 	pageSizeGroupBoxLayout->addWidget(TextLabel1_2, 2, 1 );
245 	widthSpinBox = new ScrSpinBox(pts2value(1.0, m_unitIndex), 16777215, pageSizeGroupBox, m_unitIndex );
246 	widthSpinBox->setSuffix(m_unitSuffix);
247 	TextLabel1_2->setBuddy(widthSpinBox);
248 	pageSizeGroupBoxLayout->addWidget(widthSpinBox, 2, 2 );
249 	TextLabel2_2 = new QLabel( tr( "&Height:" ), pageSizeGroupBox );
250 	pageSizeGroupBoxLayout->addWidget(TextLabel2_2, 3, 1 );
251 	heightSpinBox = new ScrSpinBox( pts2value(1.0, m_unitIndex), 16777215, pageSizeGroupBox, m_unitIndex );
252 	heightSpinBox->setSuffix(m_unitSuffix);
253 	TextLabel2_2->setBuddy(heightSpinBox);
254 	pageSizeGroupBoxLayout->addWidget(heightSpinBox, 3, 2 );
255 
256 	unitOfMeasureLabel = new QLabel( tr( "&Default Unit:" ), pageSizeGroupBox );
257 	unitOfMeasureComboBox = new QComboBox( pageSizeGroupBox );
258 	unitOfMeasureComboBox->addItems(unitGetTextUnitList());
259 	unitOfMeasureComboBox->setCurrentIndex(m_unitIndex);
260 	unitOfMeasureComboBox->setEditable(false);
261 	unitOfMeasureLabel->setBuddy(unitOfMeasureComboBox);
262 	pageSizeGroupBoxLayout->addWidget( unitOfMeasureLabel, 4, 1 );
263 	pageSizeGroupBoxLayout->addWidget( unitOfMeasureComboBox, 4, 2 );
264 
265 	MarginStruct marg(prefsManager.appPrefs.docSetupPrefs.margins);
266 	marginGroup = new MarginWidget(newDocFrame,  tr( "Margin Guides" ), &marg, m_unitIndex );
267 	marginGroup->setPageWidthHeight(prefsManager.appPrefs.docSetupPrefs.pageWidth, prefsManager.appPrefs.docSetupPrefs.pageHeight);
268 	marginGroup->setFacingPages(!(prefsManager.appPrefs.docSetupPrefs.pagePositioning == singlePage));
269 	widthSpinBox->setValue(prefsManager.appPrefs.docSetupPrefs.pageWidth * m_unitRatio);
270 	heightSpinBox->setValue(prefsManager.appPrefs.docSetupPrefs.pageHeight * m_unitRatio);
271 	QStringList pageSizes = ps.activeSizeTRList();
272 	int sizeIndex = pageSizes.indexOf(ps.nameTR());
273 	if (sizeIndex != -1)
274 		pageSizeComboBox->setCurrentIndex(sizeIndex);
275 	else
276 		pageSizeComboBox->setCurrentIndex(pageSizeComboBox->count() - 1);
277 	marginGroup->setPageSize(pageSizeComboBox->currentText());
278 	marginGroup->setNewBleeds(prefsManager.appPrefs.docSetupPrefs.bleeds);
279 	marginGroup->setMarginPreset(prefsManager.appPrefs.docSetupPrefs.marginPreset);
280 
281 	optionsGroupBox = new QGroupBox( newDocFrame );
282 	optionsGroupBox->setTitle( tr( "Options" ) );
283 	optionsGroupBoxLayout = new QGridLayout( optionsGroupBox );
284 	optionsGroupBoxLayout->setSpacing(6);
285 	optionsGroupBoxLayout->setContentsMargins(9, 9, 9, 9);
286 	optionsGroupBoxLayout->setAlignment( Qt::AlignTop );
287 	pageCountLabel = new QLabel( tr( "N&umber of Pages:" ), optionsGroupBox );
288 
289 	pageCountSpinBox = new QSpinBox( optionsGroupBox );
290 	pageCountSpinBox->setMaximum( 10000 );
291 	pageCountSpinBox->setMinimum( 1 );
292 	pageCountLabel->setBuddy(pageCountSpinBox);
293 	optionsGroupBoxLayout->addWidget(pageCountLabel, 0, 0, Qt::AlignLeft);
294 	optionsGroupBoxLayout->addWidget(pageCountSpinBox, 0, 1);
295 
296 	layoutLabel1 = new QLabel( optionsGroupBox );
297 	layoutLabel1->setText( tr( "First Page is:" ) );
298 	firstPage = new QComboBox( optionsGroupBox );
299 	firstPage->clear();
300 	selectItem(prefsManager.appPrefs.docSetupPrefs.pagePositioning);
301 	optionsGroupBoxLayout->addWidget(layoutLabel1, 1, 0, Qt::AlignLeft);
302 	optionsGroupBoxLayout->addWidget(firstPage, 1, 1);
303 	firstPage->setCurrentIndex(prefsManager.appPrefs.pageSets[prefsManager.appPrefs.docSetupPrefs.pagePositioning].FirstPage);
304 
305 	setDocLayout(prefsManager.appPrefs.docSetupPrefs.pagePositioning);
306 	setSize(prefsManager.appPrefs.docSetupPrefs.pageSize);
307 	setOrientation(prefsManager.appPrefs.docSetupPrefs.pageOrientation);
308 
309 	autoTextFrame = new QCheckBox( optionsGroupBox );
310 	autoTextFrame->setText( tr( "&Automatic Text Frames" ) );
311 	optionsGroupBoxLayout->addWidget(autoTextFrame, 2, 0, 1, 2, Qt::AlignLeft);
312 
313 	TextLabel3 = new QLabel( tr( "Colu&mns:" ), optionsGroupBox );
314 	numberOfCols = new QSpinBox( optionsGroupBox );
315 	numberOfCols->setButtonSymbols( QSpinBox::UpDownArrows );
316 	numberOfCols->setMinimum( 1 );
317 	numberOfCols->setValue( 1 );
318 	TextLabel3->setBuddy(numberOfCols);
319 	optionsGroupBoxLayout->addWidget(TextLabel3, 3, 0, Qt::AlignLeft);
320 	optionsGroupBoxLayout->addWidget(numberOfCols, 3, 1);
321 
322 	TextLabel4 = new QLabel( tr( "&Gap:" ), optionsGroupBox );
323 	Distance = new ScrSpinBox( 0, 1000, optionsGroupBox, m_unitIndex );
324 	Distance->setValue(11 * m_unitRatio);
325 	TextLabel4->setBuddy(Distance);
326 	optionsGroupBoxLayout->addWidget(TextLabel4, 4, 0, Qt::AlignLeft);
327 	optionsGroupBoxLayout->addWidget(Distance, 4, 1);
328 
329 	firstPage->setMinimumWidth(Distance->width());
330 
331 	TextLabel3->setEnabled(false);
332 	TextLabel4->setEnabled(false);
333 	Distance->setEnabled(false);
334 	numberOfCols->setEnabled(false);
335 	startDocSetup = new QCheckBox( optionsGroupBox );
336 	startDocSetup->setText( tr( "Show Document Settings After Creation" ) );
337 	startDocSetup->setChecked(false);
338 	optionsGroupBoxLayout->addWidget(startDocSetup, 5, 0, 1, 2, Qt::AlignLeft);
339 
340 	NewDocLayout = new QGridLayout( newDocFrame );
341 	if (m_onStartup)
342 		NewDocLayout->setContentsMargins(9, 9, 9, 9);
343 	else
344 		NewDocLayout->setContentsMargins(0, 0, 0, 0);
345 	NewDocLayout->setSpacing(6);
346 	NewDocLayout->addWidget( marginGroup, 1, 0 );
347 	NewDocLayout->addWidget( optionsGroupBox, 1, 1 );
348 	NewDocLayout->addWidget( pageSizeGroupBox, 0, 0, 1, 2);
349 }
350 
createNewFromTempPage()351 void NewDocDialog::createNewFromTempPage()
352 {
353 	newFromTempFrame = new QFrame(this);
354 	verticalLayout = new QVBoxLayout(newFromTempFrame);
355 	verticalLayout->setContentsMargins(9, 9, 9, 9);
356 	nftGui = new nftwidget(newFromTempFrame);
357 	verticalLayout->addWidget(nftGui);
358 }
359 
createOpenDocPage()360 void NewDocDialog::createOpenDocPage()
361 {
362 	PrefsContext* docContext = prefsManager.prefsFile->getContext("docdirs", false);
363 	QString docDir = ".";
364 	QString prefsDocDir = prefsManager.documentDir();
365 	if (!prefsDocDir.isEmpty())
366 		docDir = docContext->get("docsopen", prefsDocDir);
367 	else
368 		docDir = docContext->get("docsopen", ".");
369 	QString formats(FileLoader::getLoadFilterString());
370 //	formats.remove("PDF (*.pdf *.PDF);;");
371 	openDocFrame = new QFrame(this);
372 	openDocLayout = new QVBoxLayout(openDocFrame);
373 	openDocLayout->setContentsMargins(0, 0, 0, 0);
374 	openDocLayout->setSpacing(6);
375 	m_selectedFile = "";
376 
377 	// With Qt 5.15 we have to be in careful so that new document dialog doesn't display too large on startup.
378 	// To avoid this we have to use QFileDialog(QWidget *parent, Qt::WindowFlags flags) constructor, then
379 	// set the QFileDialog::DontUseNativeDialog option as early as possible, and nonetheless set again
380 	// the Qt::Widget window flag before adding the widget to layout.
381 	fileDialog = new QFileDialog(openDocFrame, Qt::Widget);
382 	fileDialog->setOption(QFileDialog::DontUseNativeDialog);
383 	fileDialog->setWindowTitle(tr("Open"));
384 	fileDialog->setDirectory(docDir);
385 	fileDialog->setNameFilter(formats);
386 	fileDialog->setFileMode(QFileDialog::ExistingFile);
387 	fileDialog->setAcceptMode(QFileDialog::AcceptOpen);
388 	fileDialog->setIconProvider(new ImIconProvider());
389 	fileDialog->setOption(QFileDialog::HideNameFilterDetails, true);
390 	fileDialog->setOption(QFileDialog::ReadOnly, true);
391 	fileDialog->setSizeGripEnabled(false);
392 	fileDialog->setModal(false);
393 	QList<QPushButton *> pushButtons = fileDialog->findChildren<QPushButton *>();
394 	for (auto pushButton : qAsConst(pushButtons))
395 		pushButton->setVisible(false);
396 	fileDialog->setWindowFlags(Qt::Widget);
397 	openDocLayout->addWidget(fileDialog);
398 
399 	FileDialogEventCatcher* keyCatcher = new FileDialogEventCatcher(this);
400 	QList<QListView *> listViews = fileDialog->findChildren<QListView *>();
401 	for (auto listView : qAsConst(listViews))
402 		listView->installEventFilter(keyCatcher);
403 	connect(keyCatcher, SIGNAL(escapePressed()), this, SLOT(reject()));
404 	connect(keyCatcher, SIGNAL(dropLocation(QString)), this, SLOT(locationDropped(QString)));
405 	connect(keyCatcher, SIGNAL(desktopPressed()), this, SLOT(gotoDesktopDirectory()));
406 	connect(keyCatcher, SIGNAL(homePressed()), this, SLOT(gotoHomeDirectory()));
407 	connect(keyCatcher, SIGNAL(parentPressed()), this, SLOT(gotoParentDirectory()));
408 	connect(keyCatcher, SIGNAL(enterSelectedPressed()), this, SLOT(gotoSelectedDirectory()));
409 	connect(fileDialog, SIGNAL(currentChanged(const QString &)), this, SLOT(openFileDialogFileClicked(const QString &)));
410 	connect(fileDialog, SIGNAL(filesSelected(const QStringList &)), this, SLOT(openFile()));
411 	connect(fileDialog, SIGNAL(rejected()), this, SLOT(reject()));
412 }
413 
openFile()414 void NewDocDialog::openFile()
415 {
416 	ExitOK();
417 }
418 
createRecentDocPage()419 void NewDocDialog::createRecentDocPage()
420 {
421 	recentDocFrame = new QFrame(this);
422 	recentDocLayout = new QVBoxLayout(recentDocFrame);
423 	recentDocLayout->setContentsMargins(9, 9, 9, 9);
424 	recentDocLayout->setSpacing(6);
425 	recentDocListBox = new QListWidget(recentDocFrame);
426 	recentDocLayout->addWidget(recentDocListBox);
427 	int max = qMin(prefsManager.appPrefs.uiPrefs.recentDocCount, recentDocList.count());
428 	for (int i = 0; i < max; ++i)
429 		recentDocListBox->addItem(QDir::toNativeSeparators(recentDocList[i]));
430 	if (max>0)
431 		recentDocListBox->setCurrentRow(0);
432 }
433 
setWidth(double)434 void NewDocDialog::setWidth(double)
435 {
436 	m_pageWidth = widthSpinBox->value() / m_unitRatio;
437 	marginGroup->setPageWidth(m_pageWidth);
438 	QString psText=pageSizeComboBox->currentText();
439 	if (psText!=CommonStrings::trCustomPageSize && psText!=CommonStrings::customPageSize)
440 		pageSizeComboBox->setCurrentIndex(pageSizeComboBox->count() - 1);
441 	int newOrientation = (widthSpinBox->value() > heightSpinBox->value()) ? landscapePage : portraitPage;
442 	if (newOrientation != m_orientation)
443 	{
444 		pageOrientationComboBox->blockSignals(true);
445 		pageOrientationComboBox->setCurrentIndex(newOrientation);
446 		pageOrientationComboBox->blockSignals(false);
447 		m_orientation = newOrientation;
448 	}
449 }
450 
setHeight(double)451 void NewDocDialog::setHeight(double)
452 {
453 	m_pageHeight = heightSpinBox->value() / m_unitRatio;
454 	marginGroup->setPageHeight(m_pageHeight);
455 	QString psText=pageSizeComboBox->currentText();
456 	if (psText!=CommonStrings::trCustomPageSize && psText!=CommonStrings::customPageSize)
457 		pageSizeComboBox->setCurrentIndex(pageSizeComboBox->count() - 1);
458 	int newOrientation = (widthSpinBox->value() > heightSpinBox->value()) ? landscapePage : portraitPage;
459 	if (newOrientation != m_orientation)
460 	{
461 		pageOrientationComboBox->blockSignals(true);
462 		pageOrientationComboBox->setCurrentIndex(newOrientation);
463 		pageOrientationComboBox->blockSignals(false);
464 		m_orientation = newOrientation;
465 	}
466 }
467 
selectItem(uint nr)468 void NewDocDialog::selectItem(uint nr)
469 {
470 	disconnect(layoutsView, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(itemSelected(QListWidgetItem* )));
471 	disconnect(layoutsView, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(itemSelected(QListWidgetItem* )));
472 	disconnect(layoutsView, SIGNAL(itemActivated(QListWidgetItem*)), this, SLOT(itemSelected(QListWidgetItem* )));
473 	disconnect(layoutsView, SIGNAL(itemPressed(QListWidgetItem*)), this, SLOT(itemSelected(QListWidgetItem* )));
474 	if (nr > 0)
475 	{
476 		const QStringList& pageNames = prefsManager.appPrefs.pageSets[nr].pageNames;
477 		firstPage->setEnabled(true);
478 		firstPage->clear();
479 		for (auto pNames = pageNames.begin(); pNames != pageNames.end(); ++pNames)
480 			firstPage->addItem(CommonStrings::translatePageSetLocString((*pNames)));
481 	}
482 	else
483 	{
484 		firstPage->clear();
485 		firstPage->addItem(" ");
486 		firstPage->setEnabled(false);
487 	}
488 	layoutsView->setCurrentRow(nr);
489 	layoutsView->item(nr)->setSelected(true);
490 	connect(layoutsView, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(itemSelected(QListWidgetItem* )));
491 	connect(layoutsView, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(itemSelected(QListWidgetItem* )));
492 	connect(layoutsView, SIGNAL(itemActivated(QListWidgetItem*)), this, SLOT(itemSelected(QListWidgetItem* )));
493 	connect(layoutsView, SIGNAL(itemPressed(QListWidgetItem*)), this, SLOT(itemSelected(QListWidgetItem* )));
494 }
495 
itemSelected(QListWidgetItem * ic)496 void NewDocDialog::itemSelected(QListWidgetItem* ic)
497 {
498 	if (ic == nullptr)
499 		return;
500 	selectItem(layoutsView->row(ic));
501 	setDocLayout(layoutsView->row(ic));
502 }
503 
handleAutoFrame()504 void NewDocDialog::handleAutoFrame()
505 {
506 	if (autoTextFrame->isChecked())
507 	{
508 		TextLabel3->setEnabled(true);
509 		TextLabel4->setEnabled(true);
510 		Distance->setEnabled(true);
511 		numberOfCols->setEnabled(true);
512 	}
513 	else
514 	{
515 		TextLabel3->setEnabled(false);
516 		TextLabel4->setEnabled(false);
517 		Distance->setEnabled(false);
518 		numberOfCols->setEnabled(false);
519 	}
520 }
521 
setDistance(double)522 void NewDocDialog::setDistance(double)
523 {
524 	m_distance = Distance->value() / m_unitRatio;
525 }
526 
setUnit(int newUnitIndex)527 void NewDocDialog::setUnit(int newUnitIndex)
528 {
529 	disconnect(widthSpinBox, SIGNAL(valueChanged(double)), this, SLOT(setWidth(double)));
530 	disconnect(heightSpinBox, SIGNAL(valueChanged(double)), this, SLOT(setHeight(double)));
531 	widthSpinBox->setNewUnit(newUnitIndex);
532 	heightSpinBox->setNewUnit(newUnitIndex);
533 	Distance->setNewUnit(newUnitIndex);
534 	m_unitRatio = unitGetRatioFromIndex(newUnitIndex);
535 	m_unitIndex = newUnitIndex;
536 	widthSpinBox->setValue(m_pageWidth * m_unitRatio);
537 	heightSpinBox->setValue(m_pageHeight * m_unitRatio);
538 
539 	marginGroup->setNewUnit(m_unitIndex);
540 	marginGroup->setPageHeight(m_pageHeight);
541 	marginGroup->setPageWidth(m_pageWidth);
542 	connect(widthSpinBox, SIGNAL(valueChanged(double)), this, SLOT(setWidth(double)));
543 	connect(heightSpinBox, SIGNAL(valueChanged(double)), this, SLOT(setHeight(double)));
544 }
545 
ExitOK()546 void NewDocDialog::ExitOK()
547 {
548 	m_pageWidth = widthSpinBox->value() / m_unitRatio;
549 	m_pageHeight = heightSpinBox->value() / m_unitRatio;
550 	m_bleedBottom = marginGroup->bottomBleed();
551 	m_bleedTop = marginGroup->topBleed();
552 	m_bleedLeft = marginGroup->leftBleed();
553 	m_bleedRight = marginGroup->rightBleed();
554 	if (m_onStartup)
555 	{
556 		m_tabSelected = tabWidget->currentIndex();
557 		if (m_tabSelected == NewDocDialog::NewFromTemplateTab) // new doc from template
558 		{
559 			if (nftGui->currentDocumentTemplate)
560 			{
561 				m_selectedFile = QDir::fromNativeSeparators(nftGui->currentDocumentTemplate->file);
562 				m_selectedFile = QDir::cleanPath(m_selectedFile);
563 			}
564 		}
565 		else if (m_tabSelected == NewDocDialog::OpenExistingTab) // open existing doc
566 		{
567 			QStringList files = fileDialog->selectedFiles();
568 			if (files.count() != 0)
569 				m_selectedFile = QDir::fromNativeSeparators(files[0]);
570 			QFileInfo fi(m_selectedFile);
571 			if (fi.isDir())
572 			{
573 				fileDialog->setDirectory(fi.absoluteFilePath());
574 				return;
575 			}
576 		}
577 		else if (m_tabSelected == NewDocDialog::OpenRecentTab) // open recent doc
578 		{
579 			if (recentDocListBox->currentItem() != nullptr)
580 			{
581 				QString fileName(recentDocListBox->currentItem()->text());
582 				if (!fileName.isEmpty())
583 					m_selectedFile = QDir::fromNativeSeparators(fileName);
584 			}
585 		}
586 	}
587 	else
588 		m_tabSelected = NewDocDialog::NewDocumentTab;
589 	accept();
590 }
591 
setOrientation(int ori)592 void NewDocDialog::setOrientation(int ori)
593 {
594 	disconnect(widthSpinBox, SIGNAL(valueChanged(double)), this, SLOT(setWidth(double)));
595 	disconnect(heightSpinBox, SIGNAL(valueChanged(double)), this, SLOT(setHeight(double)));
596 	if (ori != m_orientation)
597 	{
598 		double w  = widthSpinBox->value(), h = heightSpinBox->value();
599 		double pw = m_pageWidth, ph = m_pageHeight;
600 		widthSpinBox->setValue((ori == portraitPage) ? qMin(w, h) : qMax(w, h));
601 		heightSpinBox->setValue((ori == portraitPage) ? qMax(w, h) : qMin(w, h));
602 		m_pageWidth  = (ori == portraitPage) ? qMin(pw, ph) : qMax(pw, ph);
603 		m_pageHeight = (ori == portraitPage) ? qMax(pw, ph) : qMin(pw, ph);
604 	}
605 	// #869 pv - defined constants added + code repeat (check w/h)
606 	(ori == portraitPage) ? m_orientation = portraitPage : m_orientation = landscapePage;
607 	if (pageSizeComboBox->currentText() == CommonStrings::trCustomPageSize)
608 	{
609 		if (widthSpinBox->value() > heightSpinBox->value())
610 			pageOrientationComboBox->setCurrentIndex(landscapePage);
611 		else
612 			pageOrientationComboBox->setCurrentIndex(portraitPage);
613 	}
614 	// end of #869
615 	marginGroup->setPageHeight(m_pageHeight);
616 	marginGroup->setPageWidth(m_pageWidth);
617 	connect(widthSpinBox, SIGNAL(valueChanged(double)), this, SLOT(setWidth(double)));
618 	connect(heightSpinBox, SIGNAL(valueChanged(double)), this, SLOT(setHeight(double)));
619 }
620 
setPageSize(const QString & size)621 void NewDocDialog::setPageSize(const QString &size)
622 {
623 	if (size == CommonStrings::trCustomPageSize)
624 		setSize(size);
625 	else
626 	{
627 		setSize(size);
628 		setOrientation(pageOrientationComboBox->currentIndex());
629 	}
630 	marginGroup->setPageSize(size);
631 }
632 
setSize(const QString & gr)633 void NewDocDialog::setSize(const QString& gr)
634 {
635 	m_pageWidth = widthSpinBox->value() / m_unitRatio;
636 	m_pageHeight = heightSpinBox->value() / m_unitRatio;
637 
638 	disconnect(widthSpinBox, SIGNAL(valueChanged(double)), this, SLOT(setWidth(double)));
639 	disconnect(heightSpinBox, SIGNAL(valueChanged(double)), this, SLOT(setHeight(double)));
640 	if (gr==CommonStrings::trCustomPageSize || gr==CommonStrings::customPageSize)
641 	{
642 		widthSpinBox->setEnabled(true);
643 		heightSpinBox->setEnabled(true);
644 	}
645 	else
646 	{
647 		PageSize *ps2 = new PageSize(gr);
648 		if (pageOrientationComboBox->currentIndex() == portraitPage)
649 		{
650 			m_pageWidth = ps2->width();
651 			m_pageHeight = ps2->height();
652 		} else {
653 			m_pageWidth = ps2->height();
654 			m_pageHeight = ps2->width();
655 		}
656 		delete ps2;
657 	}
658 	widthSpinBox->setValue(m_pageWidth * m_unitRatio);
659 	heightSpinBox->setValue(m_pageHeight * m_unitRatio);
660 	marginGroup->setPageHeight(m_pageHeight);
661 	marginGroup->setPageWidth(m_pageWidth);
662 	connect(widthSpinBox, SIGNAL(valueChanged(double)), this, SLOT(setWidth(double)));
663 	connect(heightSpinBox, SIGNAL(valueChanged(double)), this, SLOT(setHeight(double)));
664 }
665 
setDocLayout(int layout)666 void NewDocDialog::setDocLayout(int layout)
667 {
668 	marginGroup->setFacingPages(layout != singlePage);
669 	m_choosenLayout = layout;
670 	firstPage->setCurrentIndex(prefsManager.appPrefs.pageSets[m_choosenLayout].FirstPage);
671 }
672 
recentDocListBox_doubleClicked()673 void NewDocDialog::recentDocListBox_doubleClicked()
674 {
675 	/* Yep. There is nothing to solve. ScribusMainWindow handles all
676 	openings etc. It's Franz's programming style ;) */
677 	ExitOK();
678 }
679 
adjustTitles(int tab)680 void NewDocDialog::adjustTitles(int tab)
681 {
682 	if (tab == 0)
683 		setWindowTitle(tr("New Document"));
684 	else if (tab == 1)
685 		setWindowTitle(tr("New from Template"));
686 	else if (tab == 2)
687 		setWindowTitle(tr("Open Existing Document"));
688 	else if (tab == 3)
689 		setWindowTitle(tr("Open Recent Document"));
690 	else
691 		setWindowTitle(tr("New Document"));
692 	OKButton->setEnabled(tab!=2);
693 }
694 
locationDropped(const QString & fileUrl)695 void NewDocDialog::locationDropped(const QString& fileUrl)
696 {
697 	QFileInfo fi(fileUrl);
698 	if (fi.isDir())
699 		fileDialog->setDirectory(fi.absoluteFilePath());
700 	else
701 	{
702 		fileDialog->setDirectory(fi.absolutePath());
703 		fileDialog->selectFile(fi.fileName());
704 	}
705 }
706 
gotoParentDirectory()707 void NewDocDialog::gotoParentDirectory()
708 {
709 	QDir d(fileDialog->directory());
710 	d.cdUp();
711 	fileDialog->setDirectory(d);
712 }
713 
714 
gotoSelectedDirectory()715 void NewDocDialog::gotoSelectedDirectory()
716 {
717 	QStringList s(fileDialog->selectedFiles());
718 	if (s.count() <= 0)
719 		return;
720 	QFileInfo fi(s.first());
721 	if (fi.isDir())
722 		fileDialog->setDirectory(fi.absoluteFilePath());
723 }
724 
gotoDesktopDirectory()725 void NewDocDialog::gotoDesktopDirectory()
726 {
727 	QString dp = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
728 	QFileInfo fi(dp);
729 	if (fi.exists())
730 		fileDialog->setDirectory(dp);
731 }
732 
733 
gotoHomeDirectory()734 void NewDocDialog::gotoHomeDirectory()
735 {
736 	QString dp = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
737 	QFileInfo fi(dp);
738 	if (fi.exists())
739 		fileDialog->setDirectory(dp);
740 }
741 
openFileDialogFileClicked(const QString & path)742 void NewDocDialog::openFileDialogFileClicked(const QString& path)
743 {
744 	OKButton->setEnabled(!path.isEmpty());
745 }
746