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 "commonstrings.h"
9 #include "fileloader.h"
10 #include "iconmanager.h"
11 #include "mergedoc.h"
12 #include "prefsfile.h"
13 #include "prefsmanager.h"
14 #include "scconfig.h"
15 #include "scpaths.h"
16 #include "ui/customfdialog.h"
17
18 #include <QApplication>
19 #include <QCheckBox>
20 #include <QComboBox>
21 #include <QCursor>
22 #include <QDir>
23 #include <QGridLayout>
24 #include <QHBoxLayout>
25 #include <QLabel>
26 #include <QLineEdit>
27 #include <QListWidget>
28 #include <QPixmap>
29 #include <QPushButton>
30 #include <QSpacerItem>
31 #include <QSpinBox>
32 #include <QToolTip>
33 #include <QVBoxLayout>
34
MergeDoc(QWidget * parent,bool importMasterPages,int targetDocPageCount,int currentPage)35 MergeDoc::MergeDoc(QWidget* parent, bool importMasterPages, int targetDocPageCount, int currentPage) : QDialog(parent)
36 {
37 masterPages = importMasterPages;
38 setModal(true);
39 setWindowTitle((masterPages) ? tr("Import Master Page") : tr( "Import Page(s)" ));
40 setWindowIcon(IconManager::instance().loadIcon("AppIcon.png"));
41
42 dialogLayout = new QVBoxLayout(this);
43 dialogLayout->setContentsMargins(9, 9, 9, 9);
44 dialogLayout->setSpacing(6);
45 fromInfoLayout = new QGridLayout;
46 fromInfoLayout->setContentsMargins(0, 0, 0, 0);
47 fromInfoLayout->setSpacing(6);
48 fromDocData = new QLineEdit( this );
49 fromDocData->setMinimumWidth(QWidget::fontMetrics().horizontalAdvance('a') * 50);
50 fromDocLabel = new QLabel( tr( "&From Document:"), this );
51 fromDocLabel->setBuddy( fromDocData );
52 fromInfoLayout->addWidget( fromDocLabel, 0, 0 );
53 fromInfoLayout->addWidget( fromDocData, 0, 1 );
54 changeButton = new QPushButton( tr( "&Select..." ), this );
55 changeButton->setAutoDefault( false );
56 fromInfoLayout->addWidget( changeButton, 0, 2 );
57 importPageLabel = new QLabel( tr( "&Import Page(s):" ), this );
58 fromInfoLayout->addWidget( importPageLabel, 1, 0 );
59
60 if (masterPages)
61 {
62 importPageLabel->setText( tr("&Import Master Page:") );
63 masterPageNameData = new QListWidget(this);
64 masterPageNameData->setEnabled(false);
65 masterPageNameData->setSelectionMode(QAbstractItemView::ExtendedSelection);
66 importPageLabel->setBuddy( masterPageNameData );
67 fromInfoLayout->addWidget( masterPageNameData, 1, 1, 1, 2);
68 }
69 else
70 {
71 pageNumberData = new QLineEdit( this );
72 pageNumberData->setEnabled(false);
73 importPageLabel->setBuddy( pageNumberData );
74 masterPageNameData = nullptr;
75 pageNumberData->setToolTip( "<qt>" + tr( "Insert a comma separated list of tokens import where "
76 "a token can be * for all the pages, 1-5 for "
77 "a range of pages or a single page number") + "</qt>");
78 fromInfoLayout->addWidget( pageNumberData, 1, 1 );
79 fromLabel = new QLabel(this);
80 fromInfoLayout->addWidget( fromLabel, 1, 2 );
81 createPageData = new QCheckBox( this );
82 createPageData->setText( tr("Create Page(s)"));
83 fromInfoLayout->addWidget( createPageData, 2, 0 );
84 importWhereData = new QComboBox( this );
85 importWhereData->setEnabled(false);
86 importWhereData->addItem( tr("Before Page"));
87 importWhereData->addItem( tr("After Page"));
88 importWhereData->addItem( tr("At End"));
89 importWhereData->setCurrentIndex( 2 );
90 fromInfoLayout->addWidget( importWhereData, 2, 1 );
91 importWherePageData = new QSpinBox( this );
92 importWherePageData->setMinimum(1);
93 importWherePageData->setMaximum(targetDocPageCount);
94 importWherePageData->setValue( currentPage );
95 importWherePageData->setEnabled(false);
96 fromInfoLayout->addWidget( importWherePageData, 2, 2 );
97 }
98
99 dialogLayout->addLayout( fromInfoLayout );
100 importCancelLayout = new QHBoxLayout;
101 importCancelLayout->setContentsMargins(0, 0, 0, 0);
102 importCancelLayout->setSpacing(6);
103 QSpacerItem* spacer = new QSpacerItem( 1, 1, QSizePolicy::Expanding, QSizePolicy::Minimum );
104 importCancelLayout->addItem( spacer );
105 importButton = new QPushButton( tr( "&Import" ), this );
106 importButton->setEnabled(false);
107 importCancelLayout->addWidget( importButton );
108 cancelButton = new QPushButton( CommonStrings::tr_Cancel, this );
109 cancelButton->setDefault(true);
110 importCancelLayout->addWidget( cancelButton );
111 dialogLayout->addLayout( importCancelLayout );
112 resize(minimumSizeHint());
113
114 connect( importButton, SIGNAL( clicked() ), this, SLOT( accept() ) );
115 connect( cancelButton, SIGNAL( clicked() ), this, SLOT( reject() ) );
116 connect( changeButton, SIGNAL( clicked() ), this, SLOT( changeFile() ) );
117 if (!masterPages)
118 {
119 connect( importWhereData, SIGNAL( activated(int) ), this, SLOT( checkDestPageStatus(int) ) );
120 connect( createPageData, SIGNAL( clicked() ), this, SLOT( enableCreateWidgets() ) );
121 }
122 }
123
124 /*
125 * Destroys the object and frees any allocated resources
126 */
~MergeDoc()127 MergeDoc::~MergeDoc()
128 {
129 // no need to delete child widgets, Qt does it all for us
130 }
131
changeFile()132 void MergeDoc::changeFile()
133 {
134 count = 0;
135 PrefsContext* dirs = PrefsManager::instance().prefsFile->getContext("dirs");
136 QString wdir = dirs->get("merge", ".");
137 CustomFDialog *dia = new CustomFDialog(this, wdir, tr("Open"), tr("Documents (*.sla *.sla.gz *.scd *.scd.gz);;All Files (*)"));
138 if (!fromDocData->text().isEmpty())
139 dia->setSelection(fromDocData->text());
140 if (dia->exec() == QDialog::Accepted)
141 {
142 QString fn(dia->selectedFile());
143 if (!fn.isEmpty())
144 {
145 dirs->set("merge", fn.left(fn.lastIndexOf("/")));
146 qApp->setOverrideCursor(QCursor(Qt::WaitCursor));
147 FileLoader fl(fn);
148 if (fl.testFile() == -1)
149 //TODO put in nice user warning
150 return;
151 QStringList masterPageNames;
152 bool ret = false;
153 int dummy;
154 if (masterPages)
155 ret = fl.readPageCount(&dummy, &count, masterPageNames);
156 else
157 ret = fl.readPageCount(&count, &dummy, masterPageNames);
158 qApp->restoreOverrideCursor();
159 if (ret && count > 0)
160 {
161 fromDocData->setText( QDir::toNativeSeparators(fn) );
162 importButton->setEnabled(true);
163 importButton->setDefault(true);
164 if (masterPages)
165 {
166 masterPageNameData->clear();
167 masterPageNameData->setEnabled(true);
168 masterPageNameData->addItems(masterPageNames);
169 masterPageNameData->setCurrentRow(0);
170 }
171 else
172 {
173 pageNumberData->setEnabled(true);
174 pageNumberData->setText("*");
175 }
176 if (!masterPages)
177 fromLabel->setText( tr(" from %1").arg(count));
178 }
179 }
180 }
181 else
182 {
183 fromDocData->setText("");
184 count = 0;
185 importButton->setEnabled(false);
186 cancelButton->setDefault(true);
187 if (masterPages)
188 {
189 masterPageNameData->clear();
190 masterPageNameData->setEnabled(false);
191 }
192 else
193 {
194 pageNumberData->setEnabled(false);
195 }
196 if (!masterPages)
197 fromLabel->setText( tr(" from %1").arg(count));
198 }
199 delete dia;
200 }
201
checkDestPageStatus(int positionComboSelection)202 void MergeDoc::checkDestPageStatus(int positionComboSelection)
203 {
204 importWherePageData->setDisabled( positionComboSelection == 2 );
205 }
206
enableCreateWidgets()207 void MergeDoc::enableCreateWidgets()
208 {
209 if (createPageData->isChecked())
210 {
211 importWhereData->setEnabled(true);
212 if (importWhereData->currentIndex() != 2)
213 importWherePageData->setEnabled(true);
214 }
215 else
216 {
217 importWhereData->setEnabled(false);
218 importWherePageData->setEnabled(false);
219 }
220 }
221
getFromDoc() const222 QString MergeDoc::getFromDoc() const
223 {
224 return QDir::fromNativeSeparators(fromDocData->text());
225 }
226
getMasterPageNames() const227 QStringList MergeDoc::getMasterPageNames() const
228 {
229 QStringList result;
230 for (const auto item : masterPageNameData->selectedItems())
231 result << item->text();
232 return result;
233 }
234
getMasterPageIndexes() const235 QList<int> MergeDoc::getMasterPageIndexes() const
236 {
237 QList<int> result;
238 for (const auto& row: masterPageNameData->selectionModel()->selectedRows())
239 result << row.row();
240 return result;
241 }
242
getImportWhere() const243 int MergeDoc::getImportWhere() const
244 {
245 return importWhereData->currentIndex();
246 }
247
getImportWherePage() const248 int MergeDoc::getImportWherePage() const
249 {
250 return importWherePageData->value();
251 }
252
getCreatePageChecked() const253 bool MergeDoc::getCreatePageChecked() const
254 {
255 return createPageData->isChecked();
256 }
257
getPageNumbers() const258 QString MergeDoc::getPageNumbers() const
259 {
260 return pageNumberData->text();
261 }
262
getPageCounter() const263 int MergeDoc::getPageCounter() const
264 {
265 return count;
266 }
267