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  *   Riku Leino, tsoots@gmail.com                                          *
9  ***************************************************************************/
10 
11 #include <QAction>
12 #include <QPainter>
13 
14 #include "nftwidget.h"
15 #include "scconfig.h"
16 #include "scribusapi.h"
17 #include "iconmanager.h"
18 
19 
nftwidget(QWidget * parent)20 nftwidget::nftwidget(QWidget* parent) : QWidget(parent)
21 {
22 	setupUi(this);
23 	currentDocumentTemplate = nullptr;
24 	settings = nullptr;
25 	openAction = removeAction = nullptr;
26 }
27 
setupSettings(const QString & lang)28 void nftwidget::setupSettings(const QString& lang)
29 {
30 	settings = new nftsettings(lang);
31 	// context menu
32 	removeAction = new QAction(tr("&Remove"), tnailGrid);
33 	openAction = new QAction(tr("&Open"), tnailGrid);
34 	tnailGrid->addAction(removeAction);
35 	tnailGrid->addAction(openAction);
36 	setupAbout();
37 	toolBox->setItemIcon(0, IconManager::instance().loadIcon("16/information.png"));
38 	toolBox->setItemIcon(1, IconManager::instance().loadIcon("16/image-x-generic.png"));
39 	toolBox->setItemIcon(2, IconManager::instance().loadIcon("16/help-browser.png"));
40 	tnailGrid->setIconSize(QSize(60, 60));
41 	// Signals and Slots Connections
42 	connect(categoryList, SIGNAL(itemSelectionChanged()), this, SLOT(setThumbnails()));
43 	connect(tnailGrid, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SIGNAL(leaveOK()));
44 	connect(tnailGrid, SIGNAL(itemSelectionChanged()), this, SLOT(setInfo()));
45 	connect(removeAction, SIGNAL(triggered()), this, SLOT(removeTemplate()));
46 	connect(openAction, SIGNAL(triggered()), this, SIGNAL(leaveOK()));
47 	setupListItems();
48 	setupCategories();
49 }
50 
setupCategories()51 void nftwidget::setupCategories()
52 {
53 	QStringList categories;
54 	categoryList->clear();
55 	for (uint i = 0; i < settings->templates.size(); i++)
56 	{
57 		if ((!categories.contains(settings->templates[i]->templateCategory)) &&
58 		    (!settings->templates[i]->isDeleted))
59 		{
60 			categoryList->addItem(settings->templates[i]->templateCategory);
61 			categories << settings->templates[i]->templateCategory;
62 		}
63 	}
64 	categoryList->sortItems();
65 	categoryList->insertItem(0, tr("All"));
66 	categoryList->setCurrentRow(0);
67 }
68 
setupListItems()69 void nftwidget::setupListItems()
70 {
71 	iconItems.clear();
72 	for (uint i = 0; i < settings->templates.size(); ++i)
73 	{
74 		if (!settings->templates[i]->isDeleted)
75 		{
76 			ListItem* tmp = new ListItem(settings->templates[i], (QListWidgetItem*) nullptr);
77 			iconItems.push_back(tmp);
78 		}
79 	}
80 }
81 
setThumbnails()82 void nftwidget::setThumbnails()
83 {
84 	if (categoryList->currentRow() == 0)
85 	{
86 		tnailGrid->clear();
87 		for (uint i = 0; i < iconItems.size(); ++i)
88 		{
89 			QPixmap pm(iconItems[i]->first->tnail);
90 			if (pm.width() > 60)
91 				pm = pm.scaledToWidth(60, Qt::SmoothTransformation);
92 			if (pm.height() > 60)
93 				pm = pm.scaledToHeight(60, Qt::SmoothTransformation);
94 			QPixmap pmd(60, 60);
95 			pmd.fill(palette().color(QPalette::Base));
96 			QPainter p;
97 			p.begin(&pmd);
98 			p.drawPixmap(30 - pm.width() / 2, 30 - pm.height() / 2, pm);
99 			p.end();
100 			QListWidgetItem* tmpQIVI = new QListWidgetItem(pmd, iconItems[i]->first->name, tnailGrid);
101 			iconItems[i]->second = tmpQIVI;
102 		}
103 		tnailGrid->sortItems();
104 		return;
105 	}
106 
107 	QString curtype(categoryList->currentItem()->text());
108 	if (!curtype.isNull())
109 	{
110 		tnailGrid->clear();
111 		for (uint i = 0; i < iconItems.size(); ++i)
112 		{
113 			if (curtype==iconItems[i]->first->templateCategory)
114 			{
115 				QPixmap pm(iconItems[i]->first->tnail);
116 				if (pm.width() > 60)
117 					pm = pm.scaledToWidth(60, Qt::SmoothTransformation);
118 				if (pm.height() > 60)
119 					pm = pm.scaledToHeight(60, Qt::SmoothTransformation);
120 				QPixmap pmd(60, 60);
121 				pmd.fill(palette().color(QPalette::Base));
122 				QPainter p;
123 				p.begin(&pmd);
124 				p.drawPixmap(30 - pm.width() / 2, 30 - pm.height() / 2, pm);
125 				p.end();
126 				QListWidgetItem* tmpQIVI = new QListWidgetItem(pmd, iconItems[i]->first->name, tnailGrid);
127 				iconItems[i]->second = tmpQIVI;
128 			}
129 			else
130 				iconItems[i]->second = nullptr;
131 		}
132 		tnailGrid->sortItems();
133 	}
134 }
135 
setInfo()136 void nftwidget::setInfo()
137 {
138 	QList<QListWidgetItem *> items = tnailGrid->selectedItems();
139 	if (items.count() <= 0)
140 		return;
141 	getCurrentDocumentTemplate(items.at(0));
142 	QString infoText = "<b>"+ tr("Name")+"</b><br>";
143 	infoText += currentDocumentTemplate->name + "<br>";
144 	infoText += "<b>"+ tr("Page Size")+"</b><br>";
145 	infoText += currentDocumentTemplate->psize + "<br>";
146 	infoText += "<b>"+ tr("Colors")+"</b><br>";
147 	infoText += currentDocumentTemplate->color + "<br>";
148 	infoText += "<b>"+ tr("Description")+"</b><br>";
149 	infoText += currentDocumentTemplate->descr + "<br>";
150 	infoText += "<b>"+ tr("Usage")+"</b><br>";
151 	infoText += currentDocumentTemplate->usage + "<br>";
152 	infoText += "<b>"+ tr("Created with")+"</b><br>";
153 	infoText += "Scribus " + currentDocumentTemplate->scribusVersion + "<br>";
154 	infoText += "<b>"+ tr("Date")+"</b><br>";
155 	infoText += currentDocumentTemplate->date + "<br>";
156 	infoText += "<b>"+ tr("Author")+"</b><br>";
157 	infoText += currentDocumentTemplate->author + "<br>";
158 	infoText += currentDocumentTemplate->email + "<br>";
159 	textBrowser->setText(infoText);
160 	imageView->clear();
161 	QPixmap tmplImg(currentDocumentTemplate->img);
162 	imageView->setIconSize(tmplImg.size());
163 	new QListWidgetItem(tmplImg, currentDocumentTemplate->name, imageView);
164 	emit ButtonBoxEnable(true);
165 }
166 
setupAbout()167 void nftwidget::setupAbout()
168 {
169 	QString text = "New From Template - 0.0.7<br><br>";
170 	text += "<b>";
171 	text += tr("Downloading Templates");
172 	text += "</b><br>";
173 	text += tr("Document templates can be found at "
174 	           "http://www.scribus.net/ in the Downloads section.");
175 	text += "<br><br>";
176 	text += "<b>";
177 	text +=  tr("Installing Templates");
178 	text +=  "</b><br>";
179 	text += tr("Extract the package to the template directory "
180 	           "~/.scribus/templates "
181 	           "for the current user or "
182 	           "PREFIX/share/scribus/templates "
183 	           "for all users in the system.");
184 	text +=  "<br><br>";
185 	text += "<b>";
186 	text +=  tr("Preparing a template");
187 	text +=  "</b><br>";
188 	text +=  tr("Make sure images and fonts you use can be used freely. If fonts cannot be shared do not collect them when saving as a template.");
189 	text += tr("The template creator should also make sure that the Installing Templates section above applies to their templates as well. This means a user should be able to download a template package and be able to extract them to the template directory and start using them.");
190 	text +=  "<br><br><b>";
191 	text +=  tr("Removing a template");
192 	text +=  "</b><br>";
193 	text += tr("Removing a template from the New From Template dialog will only remove the entry from the template.xml, it will not delete the document files. A popup menu with remove is only shown if you have write access to the template.xml file.");
194 	text +=  "<br><br>";
195 	text += "<b>";
196 	text +=  tr("Translating template.xml");
197 	text +=  "</b><br>";
198 	text += tr("Copy an existing template.xml to a file called template.lang_COUNTRY.xml (use the same lang code that is present in the qm file for your language), for example template.fi.xml for Finnish language template.xml. The copy must be located in the same directory as the original template.xml so Scribus can load it.");
199 	helpBrowser->setText(text);
200 }
201 
removeTemplate()202 void nftwidget::removeTemplate()
203 {
204 	currentDocumentTemplate->isDeleted = true;
205 	textBrowser->clear();
206 	imageView->clear();
207 	currentDocumentTemplate = nullptr;
208 	emit ButtonBoxEnable(false);
209 	setupListItems();
210 	setupCategories();
211 	setThumbnails();
212 }
213 
getCurrentDocumentTemplate(QListWidgetItem * item)214 void nftwidget::getCurrentDocumentTemplate(QListWidgetItem* item)
215 {
216 	for (uint i = 0; i < iconItems.size(); ++i)
217 	{
218 		if (iconItems[i]->second == item)
219 		{
220 			currentDocumentTemplate = iconItems[i]->first;
221 			break;
222 		}
223 	}
224 }
225 
~nftwidget()226 nftwidget::~nftwidget()
227 {
228 	// TODO Get the window size and position information and save with settings
229 	delete settings;
230 	for (uint i = 0; i < iconItems.size(); i++)
231 	{
232 		if (iconItems[i] != nullptr)
233 			delete iconItems[i];
234 	}
235 }
236