File: nftwidget.cpp

package info (click to toggle)
scribus 1.4.8%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 244,532 kB
  • sloc: cpp: 274,439; xml: 12,534; python: 3,448; ansic: 3,438; makefile: 1,201; perl: 95; sh: 41
file content (232 lines) | stat: -rw-r--r-- 7,990 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
For general Scribus (>=1.3.2) copyright and licensing information please refer
to the COPYING file provided with the program. Following this notice may exist
a copyright and/or license notice that predates the release of Scribus 1.3.2
for which a new license (GPL+exception) is in place.
*/
/***************************************************************************
 *   Riku Leino, tsoots@gmail.com                                          *
 ***************************************************************************/
#include <QAction>
#include <QPainter>

#include "nftwidget.h"
#include "scconfig.h"
#include "scribusapi.h"
#include "util_icon.h"


nftwidget::nftwidget(QWidget* parent) : QWidget(parent)
{
	setupUi(this);
	currentDocumentTemplate = NULL;
}

void nftwidget::setupSettings(QString lang)
{
	settings = new nftsettings(lang);
	// context menu
	removeAction = new QAction(tr("&Remove"), tnailGrid);
	openAction = new QAction(tr("&Open"), tnailGrid);
	tnailGrid->addAction(removeAction);
	tnailGrid->addAction(openAction);
	setupAbout();
	toolBox->setItemIcon(0, loadIcon("16/information.png"));
	toolBox->setItemIcon(1, loadIcon("16/image-x-generic.png"));
	toolBox->setItemIcon(2, loadIcon("16/help-browser.png"));
	tnailGrid->setIconSize(QSize(60, 60));
	// Signals and Slots Connections
	connect(categoryList, SIGNAL(itemSelectionChanged()), this, SLOT(setThumbnails()));
	connect(tnailGrid, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SIGNAL(leaveOK()));
	connect(tnailGrid, SIGNAL(itemSelectionChanged()), this, SLOT(setInfo()));
	connect(removeAction, SIGNAL(triggered()), this, SLOT(removeTemplate()));
	connect(openAction, SIGNAL(triggered()), this, SIGNAL(leaveOK()));
	setupListItems();
	setupCategories();
}

void nftwidget::setupCategories() 
{
	QStringList categories;
	categoryList->clear();
	for (uint i = 0; i < settings->templates.size(); i++)
	{
		if ((!categories.contains(settings->templates[i]->templateCategory)) &&
		    (!settings->templates[i]->isDeleted))
		{
			categoryList->addItem(settings->templates[i]->templateCategory);
			categories << settings->templates[i]->templateCategory;
		}
	}
	categoryList->sortItems();
	categoryList->insertItem(0, tr("All"));
	categoryList->setCurrentRow(0);
}

void nftwidget::setupListItems()
{
	iconItems.clear();
	for (uint i = 0; i < settings->templates.size(); ++i)
	{
		if (!settings->templates[i]->isDeleted)
		{
			ListItem* tmp = new ListItem(settings->templates[i], (QListWidgetItem *) NULL);
			iconItems.push_back(tmp);
		}
	}
}

void nftwidget::setThumbnails()
{
	if (categoryList->currentRow() == 0)
	{
		tnailGrid->clear();
		for (uint i = 0; i < iconItems.size(); ++i) 
		{
			QPixmap pm(iconItems[i]->first->tnail);
			if (pm.width() > 60)
				pm = pm.scaledToWidth(60, Qt::SmoothTransformation);
			if (pm.height() > 60)
				pm = pm.scaledToHeight(60, Qt::SmoothTransformation);
			QPixmap pmd(60, 60);
			pmd.fill(palette().color(QPalette::Base));
			QPainter p;
			p.begin(&pmd);
			p.drawPixmap(30 - pm.width() / 2, 30 - pm.height() / 2, pm);
			p.end();
			QListWidgetItem* tmpQIVI = new QListWidgetItem(pmd, iconItems[i]->first->name, tnailGrid);
			iconItems[i]->second = tmpQIVI;
		}
		tnailGrid->sortItems();
		return;
	}
	
	QString curtype(categoryList->currentItem()->text());
	if (curtype != QString::null)
	{
		tnailGrid->clear();
		for (uint i = 0; i < iconItems.size(); ++i)
		{
			if (curtype==iconItems[i]->first->templateCategory)
			{
				QPixmap pm(iconItems[i]->first->tnail);
				if (pm.width() > 60)
					pm = pm.scaledToWidth(60, Qt::SmoothTransformation);
				if (pm.height() > 60)
					pm = pm.scaledToHeight(60, Qt::SmoothTransformation);
				QPixmap pmd(60, 60);
				pmd.fill(palette().color(QPalette::Base));
				QPainter p;
				p.begin(&pmd);
				p.drawPixmap(30 - pm.width() / 2, 30 - pm.height() / 2, pm);
				p.end();
				QListWidgetItem* tmpQIVI = new QListWidgetItem(pmd, iconItems[i]->first->name, tnailGrid);
				iconItems[i]->second = tmpQIVI;
			} 
			else
				iconItems[i]->second = NULL;
		}
		tnailGrid->sortItems();
	}
}

void nftwidget::setInfo() 
{
	QList<QListWidgetItem *> items = tnailGrid->selectedItems();
	if (items.count() <= 0)
		return;
	getCurrentDocumentTemplate(items.at(0));
	QString infoText = "<b>"+ tr("Name")+"</b><br>";
	infoText += currentDocumentTemplate->name + "<br>";
	infoText += "<b>"+ tr("Page Size")+"</b><br>";
	infoText += currentDocumentTemplate->psize + "<br>";
	infoText += "<b>"+ tr("Colors")+"</b><br>";
	infoText += currentDocumentTemplate->color + "<br>";
	infoText += "<b>"+ tr("Description")+"</b><br>";
	infoText += currentDocumentTemplate->descr + "<br>";
	infoText += "<b>"+ tr("Usage")+"</b><br>";
	infoText += currentDocumentTemplate->usage + "<br>";
	infoText += "<b>"+ tr("Created with")+"</b><br>";
	infoText += "Scribus " + currentDocumentTemplate->scribusVersion + "<br>";
	infoText += "<b>"+ tr("Date")+"</b><br>";
	infoText += currentDocumentTemplate->date + "<br>";
	infoText += "<b>"+ tr("Author")+"</b><br>";
	infoText += currentDocumentTemplate->author + "<br>";
	infoText += currentDocumentTemplate->email + "<br>";
	textBrowser->setText(infoText);
	imageView->clear();
	QPixmap tmplImg(currentDocumentTemplate->img);
	imageView->setIconSize(tmplImg.size());
	new QListWidgetItem(tmplImg, currentDocumentTemplate->name, imageView);
	emit ButtonBoxEnable(true);
}

void nftwidget::setupAbout()
{
	QString text = "New From Template - 0.0.7<br><br>";
	text += "<b>";
	text += tr("Downloading Templates");
	text += "</b><br>";
	text += tr("Document templates can be found at "
	           "http://www.scribus.net/ in the Downloads section.");
	text += "<br><br>";
	text += "<b>";
	text +=  tr("Installing Templates");
	text +=  "</b><br>";
	text += tr("Extract the package to the template directory " 
	           "~/.scribus/templates "
	           "for the current user or "
	           "PREFIX/share/scribus/templates "
	           "for all users in the system.");
	text +=  "<br><br>";
	text += "<b>";
	text +=  tr("Preparing a template");
	text +=  "</b><br>";
	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.");
	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.");
	text +=  "<br><br><b>";
	text +=  tr("Removing a template");
	text +=  "</b><br>";
	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.");
	text +=  "<br><br>";
	text += "<b>";
	text +=  tr("Translating template.xml");
	text +=  "</b><br>";
	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.");
	helpBrowser->setText(text);
}

void nftwidget::removeTemplate()
{
	currentDocumentTemplate->isDeleted = true;
	textBrowser->clear();
	imageView->clear();
	currentDocumentTemplate = NULL;
	emit ButtonBoxEnable(false);
	setupListItems();
	setupCategories();
	setThumbnails();
}

void nftwidget::getCurrentDocumentTemplate(QListWidgetItem* item)
{
	for (uint i = 0; i < iconItems.size(); ++i)
	{
		if (iconItems[i]->second == item)
		{
			currentDocumentTemplate = iconItems[i]->first;
			break;
		}
	}
}

nftwidget::~nftwidget()
{
	// TODO Get the window size and position information and save with settings
	delete settings;
	for (uint i = 0; i < iconItems.size(); i++)
	{
		if (iconItems[i] != NULL)
			delete iconItems[i];
	}
}