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 "picsearch.h"
8 #include <QPixmap>
9 #include <QPainter>
10 
11 #include "cmsettings.h"
12 #include "commonstrings.h"
13 #include "iconmanager.h"
14 #include "scimage.h"
15 #include "scpaths.h"
16 #include "scribusstructs.h"
17 #include "util.h"
18 #include "util_color.h"
19 #include "util_formats.h"
20 
21 
22 
PicSearch(QWidget * parent,const QString & fileName,const QStringList & avalableFiles,bool brokenLinksOnly)23 PicSearch::PicSearch(QWidget* parent, const QString & fileName, const QStringList & avalableFiles, bool brokenLinksOnly) :
24 	QDialog(parent), brokenLinksOnly{brokenLinksOnly}
25 {
26 	setupUi(this);
27 	setModal(true);
28 	cancelButton->setText(CommonStrings::tr_Cancel);
29 	previewLabel->hide();
30 	fileNameLabel->setText(fileName);
31 
32 	for (const auto& file: avalableFiles)
33 		foundFilesBox->addItem(QDir::toNativeSeparators(file));
34 
35 	foundFilesBox->setCurrentRow(0);
36 
37 	matchWarningLabel->setVisible(false);
38 
39 	// signals and slots connections
40 	connect(cancelButton, SIGNAL( clicked() ), this, SLOT( reject() ) );
41 	connect(useButton, SIGNAL( clicked() ), this, SLOT( accept() ) );
42 	connect(previewCheckBox, SIGNAL( clicked() ), this, SLOT( previewCheckBox_clicked() ) );
43 	connect(matchCheckBox, SIGNAL( clicked() ), this, SLOT( matchCheckBox_clicked() ) );
44 	connect(foundFilesBox, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(foundFilesBox_clicked(QListWidgetItem*)));
45 }
46 
previewCheckBox_clicked()47 void PicSearch::previewCheckBox_clicked()
48 {
49 	if (previewCheckBox->isChecked())
50 	{
51 		previewLabel->show();
52 		createPreview();
53 	}
54 	else
55 		previewLabel->hide();
56 }
57 
matchCheckBox_clicked()58 void PicSearch::matchCheckBox_clicked()
59 {
60 	matchWarningLabel->setVisible(brokenLinksOnly && matchCheckBox->isChecked());
61 }
62 
foundFilesBox_clicked(QListWidgetItem * c)63 void PicSearch::foundFilesBox_clicked(QListWidgetItem *c)
64 {
65 	if (c == nullptr)
66 		return;
67 	if (previewCheckBox->isChecked())
68 		createPreview();
69 }
70 
createPreview()71 void PicSearch::createPreview()
72 {
73 	const auto currentImage = foundFilesBox->currentItem()->text();
74 	QPixmap pm(200, 200);
75 	QFileInfo fi = QFileInfo(currentImage);
76 	int w = 200;
77 	int h = 200;
78 	bool mode = false;
79 	QString ext = fi.suffix().toLower();
80 	if (ext.isEmpty())
81 		ext = getImageType(currentImage);
82 	ScImage im;
83 	//No doc to send data anyway, so no doc to get into scimage.
84 	CMSettings cms(nullptr, "", Intent_Perceptual);
85 	cms.allowColorManagement(false);
86 	if (im.loadPicture(currentImage, 1, cms, ScImage::Thumbnail, 72, &mode))
87 	{
88 		int ix,iy;
89 		if ((im.imgInfo.exifDataValid) && (!im.imgInfo.exifInfo.thumbnail.isNull()))
90 		{
91 			ix = im.imgInfo.exifInfo.width;
92 			iy = im.imgInfo.exifInfo.height;
93 		}
94 		else
95 		{
96 			ix = im.width();
97 			iy = im.height();
98 		}
99 		int xres = im.imgInfo.xres;
100 		int yres = im.imgInfo.yres;
101 		QString tmp = "";
102 		QString tmp2 = "";
103 		QImage im2;
104 		if ((ix > w-5) || (iy > h-44))
105 		{
106 			double sx = im.width() / static_cast<double>(w-5);
107 			double sy = im.height() / static_cast<double>(h-44);
108 			im2 = sy < sx ? im.scaled(qRound(im.width() / sx), qRound(im.height() / sx), Qt::IgnoreAspectRatio, Qt::SmoothTransformation)
109 						: im.scaled(qRound(im.width() / sy), qRound(im.height() / sy), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
110 		}
111 		else
112 			im2 = im.qImage(); // no need to copy
113 		QPainter p;
114 		QBrush b(QColor(205,205,205), IconManager::instance().loadPixmap("testfill.png"));
115 		p.begin(&pm);
116 		p.fillRect(0, 0, w, h-44, b);
117 		p.fillRect(0, h-44, w, 44, QColor(255, 255, 255));
118 		p.drawImage((w - im2.width()) / 2, (h - 44 - im2.height()) / 2, im2);
119 		p.drawText(2, h-29, tr("Size:")+" "+tmp.setNum(ix)+" x "+tmp2.setNum(iy));
120 		p.drawText(2, h-17, tr("Resolution:")+" "+tmp.setNum(xres)+" x "+tmp2.setNum(yres)+" "+ tr("DPI"));
121 		QString cSpace;
122 		if ((extensionIndicatesPDF(ext) || extensionIndicatesEPSorPS(ext)) && (im.imgInfo.type != ImageType7))
123 			cSpace = tr("Unknown");
124 		else
125 			cSpace=colorSpaceText(im.imgInfo.colorspace);
126 		p.drawText(2, h-5, tr("Colorspace:")+" "+cSpace);
127 		p.end();
128 		repaint();
129 	}
130 	previewLabel->setPixmap(pm);
131 }
132