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  *                                                                         *
9  *   This program is free software; you can redistribute it and/or modify  *
10  *   it under the terms of the GNU General Public License as published by  *
11  *   the Free Software Foundation; either version 2 of the License, or     *
12  *   (at your option) any later version.                                   *
13  *                                                                         *
14  ***************************************************************************/
15 #include "applytemplatedialog.h"
16 
17 #include <QCheckBox>
18 #include <QComboBox>
19 #include <QEvent>
20 #include <QGroupBox>
21 #include <QHBoxLayout>
22 #include <QLabel>
23 #include <QPushButton>
24 #include <QRadioButton>
25 #include <QSpacerItem>
26 #include <QSpinBox>
27 #include <QToolTip>
28 #include <QVBoxLayout>
29 
30 #include "commonstrings.h"
31 #include "scpage.h"
32 #include "scribusdoc.h"
33 #include "iconmanager.h"
34 
35 enum {
36     CurrentPage,
37     EvenPages,
38     OddPages,
39     AllPages
40 };
41 
42 /*
43  *  Constructs a ApplyMasterPageDialog as a child of 'parent', with the
44  *  name 'name' and widget flags set to 'f'.
45  *
46  *  The dialog will by default be modeless, unless you set 'modal' to
47  *  true to construct a modal dialog.
48  */
ApplyMasterPageDialog(QWidget * parent)49 ApplyMasterPageDialog::ApplyMasterPageDialog( QWidget* parent ) : QDialog( parent )
50 {
51 	setModal(true);
52 	setWindowTitle( tr( "Apply Master Page" ));
53 	setWindowIcon(IconManager::instance().loadIcon("AppIcon.png"));
54 	ApplyMasterPageDialogLayout = new QVBoxLayout(this);
55 	ApplyMasterPageDialogLayout->setContentsMargins(9, 9, 9, 9);
56 	ApplyMasterPageDialogLayout->setSpacing(6);
57 
58 	templateNameLayout = new QHBoxLayout;
59 	templateNameLayout->setContentsMargins(0, 0, 0, 0);
60 	templateNameLayout->setSpacing(6);
61 
62 	masterPageLabel = new QLabel( this );
63 	templateNameLayout->addWidget( masterPageLabel );
64 	spacer2 = new QSpacerItem( 1, 1, QSizePolicy::Expanding, QSizePolicy::Minimum );
65 	templateNameLayout->addItem( spacer2 );
66 
67 	masterPageComboBox = new QComboBox(this);
68 	masterPageComboBox->setEditable(false);
69 	templateNameLayout->addWidget( masterPageComboBox );
70 	ApplyMasterPageDialogLayout->addLayout( templateNameLayout );
71 
72 	applyToPageButtonGroup = new QGroupBox(this);
73 	applyToPageButtonGroup->setMinimumSize( QSize( 250, 0 ) );
74 	applyToPageButtonGroupLayout = new QVBoxLayout(applyToPageButtonGroup);
75 	applyToPageButtonGroupLayout->setSpacing(6);
76 	applyToPageButtonGroupLayout->setContentsMargins(9, 9, 9, 9);
77 
78 	currentPageRadioButton = new QRadioButton( applyToPageButtonGroup );
79 	currentPageRadioButton->setChecked( true );
80 	applyToPageButtonGroupLayout->addWidget( currentPageRadioButton );
81 
82 	evenPagesRadioButton = new QRadioButton( applyToPageButtonGroup );
83 	applyToPageButtonGroupLayout->addWidget( evenPagesRadioButton );
84 
85 	oddPagesRadioButton = new QRadioButton( applyToPageButtonGroup );
86 	applyToPageButtonGroupLayout->addWidget( oddPagesRadioButton );
87 
88 	allPagesRadioButton = new QRadioButton( applyToPageButtonGroup );
89 	applyToPageButtonGroupLayout->addWidget( allPagesRadioButton );
90 
91 	rangeLayout = new QHBoxLayout;
92 	rangeLayout->setSpacing(6);
93 	rangeLayout->setContentsMargins(0, 0, 0, 0);
94 
95 	useRangeCheckBox = new QCheckBox( applyToPageButtonGroup );
96 	useRangeCheckBox->setEnabled( false );
97 	rangeLayout->addWidget( useRangeCheckBox );
98 
99 	fromPageSpinBox = new ScrSpinBox( applyToPageButtonGroup );
100 	fromPageSpinBox->setEnabled( false );
101 	fromPageSpinBox->setMinimum( 1 );
102 	fromPageSpinBox->setDecimals(0);
103 	fromPageSpinBox->setSuffix("");
104 	rangeLayout->addWidget( fromPageSpinBox );
105 
106 	toPageLabel = new QLabel( applyToPageButtonGroup );
107 	rangeLayout->addWidget( toPageLabel );
108 
109 	toPageSpinBox = new ScrSpinBox( applyToPageButtonGroup );
110 	toPageSpinBox->setEnabled( false );
111 	toPageSpinBox->setMinimum( 1 );
112 	toPageSpinBox->setDecimals(0);
113 	toPageSpinBox->setSuffix("");
114 	rangeLayout->addWidget( toPageSpinBox );
115 	spacer3 = new QSpacerItem( 1, 1, QSizePolicy::Expanding, QSizePolicy::Minimum );
116 	rangeLayout->addItem( spacer3 );
117 	applyToPageButtonGroupLayout->addLayout( rangeLayout );
118 	ApplyMasterPageDialogLayout->addWidget( applyToPageButtonGroup );
119 
120 	layout8 = new QHBoxLayout;
121 	layout8->setSpacing(6);
122 	layout8->setContentsMargins(0, 0, 0, 0);
123 	spacer1 = new QSpacerItem( 1, 1, QSizePolicy::Expanding, QSizePolicy::Minimum );
124 	layout8->addItem( spacer1 );
125 
126 	okButton = new QPushButton( this );
127 	layout8->addWidget( okButton );
128 
129 	cancelButton = new QPushButton( this );
130 	layout8->addWidget( cancelButton );
131 	ApplyMasterPageDialogLayout->addLayout( layout8 );
132 	languageChange();
133 	resize( QSize(268, 230).expandedTo(minimumSizeHint()) );
134 
135 	// signals and slots connections
136 	connect( useRangeCheckBox, SIGNAL( toggled(bool) ), this, SLOT( enableRange(bool) ) );
137 	connect( currentPageRadioButton, SIGNAL( clicked() ), this, SLOT( singleSelectable() ) );
138 	connect( evenPagesRadioButton, SIGNAL( clicked() ), this, SLOT( rangeSelectable() ) );
139 	connect( oddPagesRadioButton, SIGNAL( clicked() ), this, SLOT( rangeSelectable() ) );
140 	connect( allPagesRadioButton, SIGNAL( clicked() ), this, SLOT( rangeSelectable() ) );
141 	connect( fromPageSpinBox, SIGNAL( valueChanged(double) ), this, SLOT( checkRangeFrom() ) );
142 	connect( toPageSpinBox, SIGNAL( valueChanged(double) ), this, SLOT( checkRangeTo() ) );
143 	connect( okButton, SIGNAL( clicked() ), this, SLOT( accept() ) );
144 	connect( cancelButton, SIGNAL( clicked() ), this, SLOT( reject() ) );
145 
146 	// buddies
147 	masterPageLabel->setBuddy( masterPageComboBox );
148 }
149 
150 /*
151  *  Destroys the object and frees any allocated resources
152  */
~ApplyMasterPageDialog()153 ApplyMasterPageDialog::~ApplyMasterPageDialog()
154 {
155 	// no need to delete child widgets, Qt does it all for us
156 }
157 
setup(ScribusDoc * doc,const QString & Nam)158 void ApplyMasterPageDialog::setup(ScribusDoc *doc, const QString& Nam)
159 {
160 	QString na = (Nam == CommonStrings::masterPageNormal) ? CommonStrings::trMasterPageNormal : Nam, in;
161 	int cc = 0;
162 	for (QMap<QString,int>::Iterator it = doc->MasterNames.begin(); it != doc->MasterNames.end(); ++it)
163 	{
164 		in = (it.key() == CommonStrings::masterPageNormal) ? CommonStrings::trMasterPageNormal : it.key();
165 		masterPageComboBox->addItem(in, it.key());
166 		if (in == na)
167 			masterPageComboBox->setCurrentIndex(cc);
168 		++cc;
169 	}
170 	const unsigned int docPagesCount = doc->Pages->count();
171 	if (docPagesCount < 2)
172 		evenPagesRadioButton->setEnabled(false);
173 	fromPageSpinBox->setMaximum(docPagesCount);
174 	fromPageSpinBox->setValue(doc->currentPage()->pageNr()+1);
175 	toPageSpinBox->setMaximum(docPagesCount);
176 	toPageSpinBox->setValue(static_cast<int>(docPagesCount));
177 }
178 
179 
getMasterPageName()180 QString ApplyMasterPageDialog::getMasterPageName()
181 {
182 	int currentIndex = masterPageComboBox->currentIndex();
183 	return masterPageComboBox->itemData(currentIndex).toString();
184 }
185 
186 
getPageSelection()187 int ApplyMasterPageDialog::getPageSelection()
188 {
189 	if (currentPageRadioButton->isChecked())
190 		return CurrentPage;
191 	if (evenPagesRadioButton->isChecked())
192 		return EvenPages;
193 	if (oddPagesRadioButton->isChecked())
194 		return OddPages;
195 	return AllPages;
196 }
197 
checkRangeFrom()198 void ApplyMasterPageDialog::checkRangeFrom()
199 {
200 	disconnect(fromPageSpinBox, SIGNAL(valueChanged(double)), this, SLOT(checkRangeFrom()));
201 	disconnect(toPageSpinBox, SIGNAL(valueChanged(double)), this, SLOT(checkRangeTo()));
202 	if (fromPageSpinBox->value() > toPageSpinBox->value())
203 		toPageSpinBox->setValue(fromPageSpinBox->value());
204 	connect(fromPageSpinBox, SIGNAL(valueChanged(double)), this, SLOT(checkRangeFrom()));
205 	connect(toPageSpinBox, SIGNAL(valueChanged(double)), this, SLOT(checkRangeTo()));
206 }
207 
checkRangeTo()208 void ApplyMasterPageDialog::checkRangeTo()
209 {
210 	disconnect(fromPageSpinBox, SIGNAL(valueChanged(double)), this, SLOT(checkRangeFrom()));
211 	disconnect(toPageSpinBox, SIGNAL(valueChanged(double)), this, SLOT(checkRangeTo()));
212 	if (toPageSpinBox->value() < fromPageSpinBox->value())
213 		fromPageSpinBox->setValue(toPageSpinBox->value());
214 	connect(fromPageSpinBox, SIGNAL(valueChanged(double)), this, SLOT(checkRangeFrom()));
215 	connect(toPageSpinBox, SIGNAL(valueChanged(double)), this, SLOT(checkRangeTo()));
216 }
217 
enableRange(bool enabled)218 void ApplyMasterPageDialog::enableRange( bool enabled )
219 {
220 	fromPageSpinBox->setEnabled(enabled);
221 	toPageSpinBox->setEnabled(enabled);
222 }
223 
rangeSelectable()224 void ApplyMasterPageDialog::rangeSelectable()
225 {
226 	useRangeCheckBox->setEnabled(true);
227 	enableRange(useRangeCheckBox->isChecked());
228 }
229 
singleSelectable()230 void ApplyMasterPageDialog::singleSelectable()
231 {
232 	useRangeCheckBox->setEnabled(false);
233 	fromPageSpinBox->setEnabled(false);
234 	toPageSpinBox->setEnabled(false);
235 }
236 
237 
usingRange()238 bool ApplyMasterPageDialog::usingRange()
239 {
240 	return useRangeCheckBox->isChecked();
241 }
242 
243 
getFromPage()244 int ApplyMasterPageDialog::getFromPage()
245 {
246 	if (useRangeCheckBox->isChecked())
247 		return static_cast<int>(fromPageSpinBox->value());
248 	return -1;
249 }
250 
251 
getToPage()252 int ApplyMasterPageDialog::getToPage()
253 {
254 	if (useRangeCheckBox->isChecked())
255 		return static_cast<int>(toPageSpinBox->value());
256 	return -1;
257 }
258 
changeEvent(QEvent * e)259 void ApplyMasterPageDialog::changeEvent(QEvent *e)
260 {
261 	if (e->type() == QEvent::LanguageChange)
262 	{
263 		languageChange();
264 	}
265 	else
266 		QWidget::changeEvent(e);
267 }
268 
languageChange()269 void ApplyMasterPageDialog::languageChange()
270 {
271 	setWindowTitle( tr( "Apply Master Page" ) );
272 	masterPageLabel->setText( tr( "&Master Page:" ) );
273 	applyToPageButtonGroup->setTitle( tr( "Apply to" ) );
274 	currentPageRadioButton->setText( tr( "Current &Page" ) );
275 	currentPageRadioButton->setShortcut( QKeySequence( tr( "Alt+P" ) ) );
276 	evenPagesRadioButton->setText( tr( "&Even Pages" ) );
277 	evenPagesRadioButton->setShortcut( QKeySequence( tr( "Alt+E" ) ) );
278 	oddPagesRadioButton->setText( tr( "O&dd Pages" ) );
279 	oddPagesRadioButton->setShortcut( QKeySequence( tr( "Alt+D" ) ) );
280 	allPagesRadioButton->setText( tr( "&All Pages" ) );
281 	allPagesRadioButton->setShortcut( QKeySequence( tr( "Alt+A" ) ) );
282 	useRangeCheckBox->setText( tr( "&Within Range" ) );
283 	useRangeCheckBox->setShortcut( QKeySequence( tr( "Alt+W" ) ) );
284 	useRangeCheckBox->setToolTip( "<qt>" + tr( "Apply the selected master page to even, odd or all pages within the following range") + "</qt>" );
285 	toPageLabel->setText( tr( "to" ) );
286 	okButton->setText( CommonStrings::tr_OK );
287 	okButton->setShortcut( QKeySequence( tr( "Alt+O" ) ) );
288 	cancelButton->setText( CommonStrings::tr_Cancel );
289 	cancelButton->setShortcut( QKeySequence( tr( "Alt+C" ) ) );
290 }
291