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 "movepage.h"
9 #include <QPixmap>
10 #include <QLabel>
11 #include <QComboBox>
12 #include <QPushButton>
13 
14 #include "commonstrings.h"
15 #include "iconmanager.h"
16 #include "ui/scrspinbox.h"
17 
18 /*
19  *  Constructs a MovePages which is a child of 'parent', with the
20  *  name 'name' and widget flags set to 'f'
21  *
22  *  The dialog will by default be modeless, unless you set 'modal' to
23  *  true to construct a modal dialog.
24  */
MovePages(QWidget * parent,int currentPage,int maxPages,bool moving)25 MovePages::MovePages( QWidget* parent, int currentPage, int maxPages, bool moving ) : QDialog( parent )
26 {
27 	move = moving;
28 	setWindowTitle (move ? tr("Move Pages") : tr("Copy Page"));
29 	setWindowIcon(IconManager::instance().loadIcon("AppIcon.png"));
30 	setModal(true);
31 	dialogLayout = new QVBoxLayout( this );
32 	dialogLayout->setSpacing(6);
33 	dialogLayout->setContentsMargins(9, 9, 9, 9);
34 	fromToLayout = new QGridLayout();
35 	fromToLayout->setSpacing(6);
36 	fromToLayout->setContentsMargins(0, 0, 0, 0);
37 	moveLabel = new QLabel( (move ? tr("Move Page(s)") : tr("Copy Page")) + ":", this );
38 	fromPageData = new ScrSpinBox(this);
39 	fromPageData->setDecimals(0);
40 	fromPageData->setMinimum(1);
41 	fromPageData->setMaximum(maxPages);
42 	fromPageData->setValue( currentPage );
43 	fromPageData->setSuffix("");
44 	uint currentRow = 0;
45 	fromToLayout->addWidget( moveLabel, currentRow, 0);
46 	fromToLayout->addWidget( fromPageData, currentRow, 1);
47 
48 	toLabel = nullptr;
49 	toPageData = nullptr;
50 	numberOfCopiesLabel = nullptr;
51 	numberOfCopiesData  = nullptr;
52 	if (move)
53 	{
54 		toLabel = new QLabel( tr("To:"), this );
55 		toPageData = new ScrSpinBox( this );
56 		toPageData->setDecimals(0);
57 		toPageData->setMinimum(1);
58 		toPageData->setMaximum(maxPages);
59 		toPageData->setValue( currentPage );
60 		toPageData->setSuffix("");
61 		fromToLayout->addWidget( toLabel, currentRow, 2);
62 		fromToLayout->addWidget( toPageData, currentRow, 3);
63 	}
64 	else
65 	{
66 		numberOfCopiesLabel = new QLabel( tr("Number of Copies:"), this );
67 		numberOfCopiesData = new ScrSpinBox(this );
68 		numberOfCopiesData->setDecimals(0);
69 		numberOfCopiesData->setMinimum(1);
70 		numberOfCopiesData->setMaximum(999);
71 		numberOfCopiesData->setSuffix("");
72 		++currentRow;
73 		fromToLayout->addWidget(numberOfCopiesLabel, currentRow, 0);
74 		fromToLayout->addWidget(numberOfCopiesData, currentRow, 1);
75 	}
76 	++currentRow;
77 	mvWhereData = new QComboBox( this );
78 	mvWhereData->addItem( tr("Before Page"));
79 	mvWhereData->addItem( tr("After Page"));
80 	mvWhereData->addItem( tr("At End"));
81 	if (move)
82 	{
83 		mvWhereData->addItem( tr("Swap with Page"));
84 	}
85 	mvWhereData->setCurrentIndex(2);
86 	mvWherePageData = new ScrSpinBox( this );
87 	mvWherePageData->setMinimum(1);
88 	mvWherePageData->setDecimals(0);
89 	mvWherePageData->setMaximum(maxPages);
90 	mvWherePageData->setValue( currentPage );
91 	mvWherePageData->setSuffix("");
92 	mvWherePageData->setDisabled( true );
93 	fromToLayout->addWidget( mvWhereData, currentRow, 0 );
94 	fromToLayout->addItem(new QSpacerItem(moveLabel->fontMetrics().horizontalAdvance( tr( "Move Page(s):" )), 0), currentRow, 1);
95 	fromToLayout->addWidget( mvWherePageData, currentRow, 2 );
96 //	fromToLayout->addColumnSpacing(0, moveLabel->fontMetrics().width( tr( "Move Page(s):" )));
97 	dialogLayout->addLayout( fromToLayout );
98 
99 	okCancelLayout = new QHBoxLayout();
100 	okCancelLayout->setSpacing(6);
101 	okCancelLayout->setContentsMargins(0, 0, 0, 0);
102 	QSpacerItem* spacer = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
103 	okCancelLayout->addItem( spacer );
104 	okButton = new QPushButton( CommonStrings::tr_OK, this );
105 	okButton->setDefault( true );
106 	okCancelLayout->addWidget(okButton);
107 	cancelButton = new QPushButton( CommonStrings::tr_Cancel, this );
108 	okCancelLayout->addWidget(cancelButton);
109 	dialogLayout->addLayout( okCancelLayout );
110 	setMaximumSize(sizeHint());
111 
112 	// signals and slots connections
113 	if (move)
114 	{
115 		connect( fromPageData, SIGNAL( valueChanged(double) ), this, SLOT( fromChanged() ) );
116 		connect( toPageData, SIGNAL( valueChanged(double) ), this, SLOT( toChanged() ) );
117 	}
118 	connect( mvWhereData, SIGNAL( activated(int) ), this, SLOT( mvWherePageDataDisable(int) ) );
119 	connect( okButton, SIGNAL( clicked() ), this, SLOT( accept() ) );
120 	connect( cancelButton, SIGNAL( clicked() ), this, SLOT( reject() ) );
121 }
122 
fromChanged()123 void MovePages::fromChanged()
124 {
125 	if (!move)
126 		return;
127 	int pageNumber = static_cast<int>(fromPageData->value());
128 	if (pageNumber > toPageData->value() || mvWhereData->currentIndex() == 3)
129 		toPageData->setValue(pageNumber);
130 	if ((pageNumber == 1) && (toPageData->value() == toPageData->maximum()))
131 		toPageData->setValue(toPageData->maximum()-1);
132 }
133 
toChanged()134 void MovePages::toChanged()
135 {
136 	if (!move)
137 		return;
138 	int pageNumber = static_cast<int>(toPageData->value());
139 	if (pageNumber < fromPageData->value())
140 		fromPageData->setValue(pageNumber);
141 	if ((fromPageData->value() == 1) && (pageNumber == toPageData->maximum()))
142 		fromPageData->setValue(2);
143 }
144 
mvWherePageDataDisable(int index)145 void MovePages::mvWherePageDataDisable(int index)
146 {
147 	mvWherePageData->setDisabled(index==2);
148 	if (toPageData)
149 	{
150 		toPageData->setDisabled(index==3);
151 		if (index==3)
152 			toPageData->setValue(fromPageData->value());
153 	}
154 }
155 
156 
getFromPage()157 int MovePages::getFromPage()
158 {
159 	return static_cast<int>(fromPageData->value());
160 }
161 
getToPage()162 int MovePages::getToPage()
163 {
164 	return static_cast<int>(toPageData->value());
165 }
166 
getWhere()167 int MovePages::getWhere()
168 {
169 	return mvWhereData->currentIndex();
170 }
171 
getWherePage()172 int MovePages::getWherePage()
173 {
174 	return static_cast<int>(mvWherePageData->value());
175 }
176 
getCopyCount()177 int MovePages::getCopyCount()
178 {
179 	return static_cast<int>(numberOfCopiesData->value());
180 }
181