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 "pagelayout.h"
8 
9 #include <QComboBox>
10 #include <QVBoxLayout>
11 #include <QListWidgetItem>
12 #include <QLabel>
13 #include <QPixmap>
14 #include <QList>
15 
16 #include "commonstrings.h"
17 #include "iconmanager.h"
18 
PageListWidget(QWidget * parent)19 PageListWidget::PageListWidget(QWidget* parent) : QListWidget(parent)
20 {
21 	setDragEnabled(false);
22 	setViewMode(QListView::IconMode);
23 	setFlow(QListView::TopToBottom);
24 	setSortingEnabled(false);
25 	setWrapping(false);
26 	setWordWrap(true);
27 	setAcceptDrops(false);
28 	setDropIndicatorShown(false);
29 	setDragDropMode(QAbstractItemView::NoDragDrop);
30 	setResizeMode(QListView::Adjust);
31 	setSelectionMode(QAbstractItemView::SingleSelection);
32 	setFocusPolicy(Qt::NoFocus);
33 	setIconSize(QSize(32, 32));
34 	setMaximumWidth(100);
35 	clear();
36 	setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
37 }
38 
arrangeIcons()39 void PageListWidget::arrangeIcons()
40 {
41 	int maxWidth = 0;
42 	QListWidgetItem* ic;
43 	int startY = 5;
44 	for (int cc = 0; cc < count(); ++cc)
45 	{
46 		ic = item(cc);
47 		QRect ir = visualItemRect(ic);
48 		maxWidth = qMax(ir.width(), maxWidth);
49 	}
50 	setResizeMode(QListView::Fixed);
51 	int startX = (viewport()->width() - maxWidth) / 2;
52 	for (int cc = 0; cc < count(); ++cc)
53 	{
54 		ic = item(cc);
55 		QRect ir = visualItemRect(ic);
56 		int moveW = (maxWidth - ir.width()) / 2;
57 		setPositionForIndex(QPoint(moveW + startX, startY), indexFromItem(ic));
58 		startY += ir.height()+5;
59 	}
60 }
61 
PageLayouts(QWidget * parent)62 PageLayouts::PageLayouts(QWidget* parent)  : QGroupBox( parent )
63 {
64 	struct PageSet pageS;
65 	pageS.Name = CommonStrings::trPageSet2;
66 	pageS.FirstPage = 0;
67 	pageS.Rows = 1;
68 	pageS.Columns = 1;
69 	pageS.pageNames.clear();
70 	pageS.pageNames.append(CommonStrings::trPageLocMiddleRight);
71 	pageSets.append(pageS);
72 
73 	modus = false;
74 	layoutGroupLayout = new QVBoxLayout(this);
75 	layoutGroupLayout->setSpacing(3);
76 	layoutGroupLayout->setContentsMargins(6, 6, 6, 6);
77 
78 	layoutsCombo = new QComboBox( this );
79 	layoutGroupLayout->addWidget( layoutsCombo );
80 
81 	layoutLabel1 = new QLabel( this );
82 	layoutGroupLayout->addWidget( layoutLabel1 );
83 	firstPage = new QComboBox( this );
84 	layoutGroupLayout->addWidget( firstPage );
85 
86 	languageChange();
87 
88 	connect(layoutsCombo, SIGNAL(activated(int)), this, SLOT(itemSelected(int)));
89 	connect(firstPage, SIGNAL(activated(int)), this, SIGNAL(selectedFirstPage(int)));
90 }
91 
PageLayouts(QWidget * parent,const QList<PageSet> & pSets,bool mode)92 PageLayouts::PageLayouts(QWidget* parent, const QList<PageSet>& pSets, bool mode)  : QGroupBox( parent )
93 {
94 	pageSets = pSets;
95 	modus = mode;
96 	layoutGroupLayout = new QVBoxLayout(this);
97 	layoutGroupLayout->setSpacing(6);
98 	layoutGroupLayout->setContentsMargins(9, 9, 9, 9);
99 	if (modus)
100 	{
101 		layoutsView = new PageListWidget( this );
102 		layoutGroupLayout->addWidget( layoutsView );
103 	}
104 	else
105 	{
106 		layoutsCombo = new QComboBox( this );
107 		layoutGroupLayout->addWidget( layoutsCombo );
108 	}
109 	layoutLabel1 = new QLabel( this );
110 	layoutGroupLayout->addWidget( layoutLabel1 );
111 	firstPage = new QComboBox( this );
112 	layoutGroupLayout->addWidget( firstPage );
113 	languageChange();
114 // 	We need change combos width setMaximumWidth(minimumSizeHint().width());
115 
116 	if (modus)
117 		connect(layoutsView, SIGNAL(itemClicked(QListWidgetItem *)), this, SLOT(itemSelected(QListWidgetItem* )));
118 	else
119 		connect(layoutsCombo, SIGNAL(activated(int)), this, SLOT(itemSelected(int)));
120 	connect(firstPage, SIGNAL(activated(int)), this, SIGNAL(selectedFirstPage(int)));
121 }
122 
updateLayoutSelector(const QList<PageSet> & pSets)123 void PageLayouts::updateLayoutSelector(const QList<PageSet>& pSets)
124 {
125 	disconnect(layoutsCombo, SIGNAL(activated(int)), this, SLOT(itemSelected(int)));
126 	pageSets = pSets;
127 	layoutsCombo->clear();
128 	for (int pg = 0; pg < pageSets.count(); ++pg)
129 	{
130 		QString psname=CommonStrings::translatePageSetString(pageSets[pg].Name);
131 		if (pg == 0)
132 			layoutsCombo->addItem(IconManager::instance().loadIcon("16/page-simple.png"), psname);
133 		else if (pg == 1)
134 			layoutsCombo->addItem(IconManager::instance().loadIcon("16/page-doublesided.png"), psname);
135 		else if (pg == 2)
136 			layoutsCombo->addItem(IconManager::instance().loadIcon("16/page-3fold.png"), psname);
137 		else if (pg == 3)
138 			layoutsCombo->addItem(IconManager::instance().loadIcon("16/page-4fold.png"), psname);
139 		else
140 			layoutsCombo->addItem(IconManager::instance().loadIcon("16/page-simple.png"), psname);
141 	}
142 	connect(layoutsCombo, SIGNAL(activated(int)), this, SLOT(itemSelected(int)));
143 }
144 
selectFirstP(int nr)145 void PageLayouts::selectFirstP(int nr)
146 {
147 	disconnect(firstPage, SIGNAL(activated(int)), this, SIGNAL(selectedFirstPage(int)));
148 	firstPage->setCurrentIndex(nr);
149 	connect(firstPage, SIGNAL(activated(int)), this, SIGNAL(selectedFirstPage(int)));
150 }
151 
selectItem(uint nr)152 void PageLayouts::selectItem(uint nr)
153 {
154 	if (modus)
155 		disconnect(layoutsView, SIGNAL(itemClicked(QListWidgetItem *)), this, SLOT(itemSelected(QListWidgetItem* )));
156 	else
157 		disconnect(layoutsCombo, SIGNAL(activated(int)), this, SLOT(itemSelected(int)));
158 	disconnect(firstPage, SIGNAL(activated(int)), this, SIGNAL(selectedFirstPage(int)));
159 	if (nr > 0)
160 	{
161 		firstPage->setEnabled(true);
162 		firstPage->clear();
163 		QStringList::Iterator pNames;
164 		for (pNames = pageSets[nr].pageNames.begin(); pNames != pageSets[nr].pageNames.end(); ++pNames)
165 		{
166 			firstPage->addItem(CommonStrings::translatePageSetLocString((*pNames)));
167 		}
168 	}
169 	else
170 	{
171 		firstPage->clear();
172 		firstPage->addItem(" ");
173 		firstPage->setEnabled(false);
174 	}
175 	if (modus)
176 	{
177 		layoutsView->setCurrentRow(nr);
178 		layoutsView->item(nr)->setSelected(true);
179 		connect(layoutsView, SIGNAL(itemClicked(QListWidgetItem *)), this, SLOT(itemSelected(QListWidgetItem* )));
180 	}
181 	else
182 	{
183 		layoutsCombo->setCurrentIndex(nr);
184 		connect(layoutsCombo, SIGNAL(activated(int)), this, SLOT(itemSelected(int)));
185 	}
186 	connect(firstPage, SIGNAL(activated(int)), this, SIGNAL(selectedFirstPage(int)));
187 }
188 
itemSelectedPost(int chosen)189 void PageLayouts::itemSelectedPost(int chosen)
190 {
191 	disconnect(firstPage, SIGNAL(activated(int)), this, SIGNAL(selectedFirstPage(int)));
192 	if (chosen > 0)
193 	{
194 		firstPage->setEnabled(true);
195 		firstPage->clear();
196 		QStringList::Iterator pNames;
197 		for (pNames = pageSets[chosen].pageNames.begin(); pNames != pageSets[chosen].pageNames.end(); ++pNames)
198 		{
199 			firstPage->addItem(CommonStrings::translatePageSetLocString((*pNames)));
200 		}
201 	}
202 	else
203 	{
204 		firstPage->clear();
205 		firstPage->addItem(" ");
206 		firstPage->setEnabled(false);
207 	}
208 	connect(firstPage, SIGNAL(activated(int)), this, SIGNAL(selectedFirstPage(int)));
209 }
210 
itemSelected(int ic)211 void PageLayouts::itemSelected(int ic)
212 {
213 	itemSelectedPost(ic);
214 	emit selectedLayout(ic);
215 }
216 
itemSelected(QListWidgetItem * ic)217 void PageLayouts::itemSelected(QListWidgetItem* ic)
218 {
219 	if (ic == nullptr)
220 		return;
221 	itemSelectedPost(layoutsView->row(ic));
222 	emit selectedLayout(layoutsView->row(ic));
223 }
224 
languageChange()225 void PageLayouts::languageChange()
226 {
227 	setTitle( tr( "Document Layout" ) );
228 	if (modus)
229 	{
230 		layoutsView->clear();
231 		for (int pg = 0; pg < pageSets.count(); ++pg)
232 		{
233 			QString psname=CommonStrings::translatePageSetString(pageSets[pg].Name);
234 			QListWidgetItem *ic;
235 			if (pg == 0)
236 			{
237 				ic = new QListWidgetItem( IconManager::instance().loadIcon("32/page-simple.png"), psname, layoutsView );
238 				ic->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
239 			}
240 			else if (pg == 1)
241 			{
242 				ic = new QListWidgetItem( IconManager::instance().loadIcon("32/page-doublesided.png"), psname, layoutsView );
243 				ic->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
244 			}
245 			else if (pg == 2)
246 			{
247 				ic = new QListWidgetItem( IconManager::instance().loadIcon("32/page-3fold.png"), psname, layoutsView );
248 				ic->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
249 			}
250 			else if (pg == 3)
251 			{
252 				ic = new QListWidgetItem( IconManager::instance().loadIcon("32/page-4fold.png"), psname, layoutsView );
253 				ic->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
254 			}
255 			else
256 			{
257 				ic = new QListWidgetItem( IconManager::instance().loadIcon("32/page-simple.png"), psname, layoutsView );
258 				ic->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
259 			}
260 		}
261 		layoutsView->arrangeIcons();
262 	}
263 	else
264 	{
265 		disconnect(layoutsCombo, SIGNAL(activated(int)), this, SLOT(itemSelected(int)));
266 		int currIndex=layoutsCombo->currentIndex();
267 		layoutsCombo->clear();
268 		for (int pg = 0; pg < pageSets.count(); ++pg)
269 		{
270 			QString psname=CommonStrings::translatePageSetString(pageSets[pg].Name);
271 			if (pg == 0)
272 				layoutsCombo->addItem(IconManager::instance().loadIcon("16/page-simple.png"), psname);
273 			else if (pg == 1)
274 				layoutsCombo->addItem(IconManager::instance().loadIcon("16/page-doublesided.png"), psname);
275 			else if (pg == 2)
276 				layoutsCombo->addItem(IconManager::instance().loadIcon("16/page-3fold.png"), psname);
277 			else if (pg == 3)
278 				layoutsCombo->addItem(IconManager::instance().loadIcon("16/page-4fold.png"), psname);
279 			else
280 				layoutsCombo->addItem(IconManager::instance().loadIcon("16/page-simple.png"), psname);
281 		}
282 		layoutsCombo->setCurrentIndex(currIndex);
283 		connect(layoutsCombo, SIGNAL(activated(int)), this, SLOT(itemSelected(int)));
284 
285 		disconnect(firstPage, SIGNAL(activated(int)), this, SIGNAL(selectedFirstPage(int)));
286 		int currFirstPageIndex=firstPage->currentIndex();
287 		firstPage->clear();
288 		if (currIndex >= 0 && currIndex<pageSets.count())
289 			for (QStringList::Iterator pNames = pageSets[currIndex].pageNames.begin(); pNames != pageSets[currIndex].pageNames.end(); ++pNames)
290 			{
291 				firstPage->addItem(CommonStrings::translatePageSetLocString((*pNames)));
292 			}
293 		firstPage->setCurrentIndex(currFirstPageIndex);
294 		connect(firstPage, SIGNAL(activated(int)), this, SIGNAL(selectedFirstPage(int)));
295 	}
296 	layoutLabel1->setText( tr( "First Page is:" ) );
297 
298 	QString layoutText( tr( "Number of pages to show side-by-side on the canvas. Often used for allowing items to be placed across page spreads." ) );
299 	if (modus)
300 		layoutsView->setToolTip(layoutText);
301 	else
302 		layoutsCombo->setToolTip(layoutText);
303 	firstPage->setToolTip( tr( "Location on the canvas where the first page of the document is placed" ) );
304 }
305