1 /**
2  * Mandelbulber v2, a 3D fractal generator       ,=#MKNmMMKmmßMNWy,
3  *                                             ,B" ]L,,p%%%,,,§;, "K
4  * Copyright (C) 2016-20 Mandelbulber Team     §R-==%w["'~5]m%=L.=~5N
5  *                                        ,=mm=§M ]=4 yJKA"/-Nsaj  "Bw,==,,
6  * This file is part of Mandelbulber.    §R.r= jw",M  Km .mM  FW ",§=ß., ,TN
7  *                                     ,4R =%["w[N=7]J '"5=],""]]M,w,-; T=]M
8  * Mandelbulber is free software:     §R.ß~-Q/M=,=5"v"]=Qf,'§"M= =,M.§ Rz]M"Kw
9  * you can redistribute it and/or     §w "xDY.J ' -"m=====WeC=\ ""%""y=%"]"" §
10  * modify it under the terms of the    "§M=M =D=4"N #"%==A%p M§ M6  R' #"=~.4M
11  * GNU General Public License as        §W =, ][T"]C  §  § '§ e===~ U  !§[Z ]N
12  * published by the                    4M",,Jm=,"=e~  §  §  j]]""N  BmM"py=ßM
13  * Free Software Foundation,          ]§ T,M=& 'YmMMpM9MMM%=w=,,=MT]M m§;'§,
14  * either version 3 of the License,    TWw [.j"5=~N[=§%=%W,T ]R,"=="Y[LFT ]N
15  * or (at your option)                   TW=,-#"%=;[  =Q:["V""  ],,M.m == ]N
16  * any later version.                      J§"mr"] ,=,," =="""J]= M"M"]==ß"
17  *                                          §= "=C=4 §"eM "=B:m|4"]#F,§~
18  * Mandelbulber is distributed in            "9w=,,]w em%wJ '"~" ,=,,ß"
19  * the hope that it will be useful,                 . "K=  ,=RMMMßM"""
20  * but WITHOUT ANY WARRANTY;                            .'''
21  * without even the implied warranty
22  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
23  *
24  * See the GNU General Public License for more details.
25  * You should have received a copy of the GNU General Public License
26  * along with Mandelbulber. If not, see <http://www.gnu.org/licenses/>.
27  *
28  * ###########################################################################
29  *
30  * Authors: Sebastian Jennen (jenzebas@gmail.com)
31  *
32  * MyFileSelectLabel class - promoted QLabel widget for selecting a file
33  * showing the selected file in a MyLineEdit and opening the QFileDialog by pressing the QPushButton
34  */
35 
36 #include "file_select_widget.h"
37 
38 #include "preview_file_dialog.h"
39 
40 #include "src/animation_flight.hpp"
41 #include "src/files.h"
42 #include "src/radiance_hdr.h"
43 #include "src/resource_http_provider.hpp"
44 
FileSelectWidget(QWidget * parent)45 FileSelectWidget::FileSelectWidget(QWidget *parent) : QWidget(parent), CommonMyWidgetWrapper(this)
46 {
47 	QFrame *frameTextAndButton = new QFrame(this);
48 	QHBoxLayout *layoutTextAndButton = new QHBoxLayout();
49 	QVBoxLayout *layout = new QVBoxLayout(this);
50 	layout->setContentsMargins(0, 0, 0, 0);
51 	layout->setSpacing(2);
52 	layoutTextAndButton->setContentsMargins(0, 0, 0, 0);
53 	layoutTextAndButton->setSpacing(2);
54 	lineEdit = new QLineEdit(this);
55 	button = new QPushButton(this);
56 	labelImage = new QLabel(this);
57 	labelImage->setAlignment(Qt::AlignTop | Qt::AlignHCenter);
58 	labelImage->setContentsMargins(0, 0, 0, 0);
59 	labelImage->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
60 	labelImage->setMaximumHeight(150);
61 	labelImage->setMaximumWidth(300);
62 	labelImage->setStyleSheet(
63 		"QLabel { color: red; font-weight: bold; border: 1px solid black;"
64 		"background-color: white; padding: 1px; }");
65 	QIcon iconFolder = QIcon::fromTheme("folder", QIcon(":/system/icons/folder.svg"));
66 	button->setIcon(iconFolder);
67 	layoutTextAndButton->addWidget(lineEdit);
68 	layoutTextAndButton->addWidget(button);
69 	frameTextAndButton->setLayout(layoutTextAndButton);
70 
71 	layout->addWidget(frameTextAndButton);
72 	layout->addWidget(labelImage);
73 
74 	defaultValue = "";
75 	connect(button, SIGNAL(clicked()), this, SLOT(slotSelectFile()));
76 	connect(lineEdit, SIGNAL(editingFinished()), this, SLOT(slotChangedFile()));
77 }
78 
79 FileSelectWidget::~FileSelectWidget() = default;
80 
resetToDefault()81 void FileSelectWidget::resetToDefault()
82 {
83 	SetPath(defaultValue);
84 }
85 
getDefaultAsString()86 QString FileSelectWidget::getDefaultAsString()
87 {
88 	return defaultValue;
89 }
90 
getFullParameterName()91 QString FileSelectWidget::getFullParameterName()
92 {
93 	return parameterName;
94 }
95 
contextMenuEvent(QContextMenuEvent * event)96 void FileSelectWidget::contextMenuEvent(QContextMenuEvent *event)
97 {
98 	CommonMyWidgetWrapper::contextMenuEvent(event);
99 }
100 
paintEvent(QPaintEvent * event)101 void FileSelectWidget::paintEvent(QPaintEvent *event)
102 {
103 	QFont f = font();
104 	f.setBold(lineEdit->text() != GetDefault());
105 	QWidget::paintEvent(event);
106 }
107 
GetDefault()108 QString FileSelectWidget::GetDefault()
109 {
110 	if (parameterContainer && !gotDefault)
111 	{
112 		QString val = parameterContainer->GetDefault<QString>(parameterName);
113 		defaultValue = val;
114 		gotDefault = true;
115 		setToolTipText();
116 	}
117 	return defaultValue;
118 }
119 
slotSelectFile()120 void FileSelectWidget::slotSelectFile()
121 {
122 	PreviewFileDialog dialog(this);
123 	dialog.setFileMode(QFileDialog::ExistingFile);
124 	dialog.setNameFilter(tr("Images (*.jpg *.jpeg *.png *.bmp *.hdr)"));
125 	dialog.setDirectory(QDir::toNativeSeparators(lineEdit->text()));
126 	dialog.selectFile(QDir::toNativeSeparators(lineEdit->text()));
127 	dialog.setAcceptMode(QFileDialog::AcceptOpen);
128 	dialog.setWindowTitle(tr("Select file for %1").arg(objectName()));
129 	QStringList filenames;
130 	if (dialog.exec())
131 	{
132 		filenames = dialog.selectedFiles();
133 		QString filename = QDir::toNativeSeparators(filenames.first());
134 		SetPath(filename);
135 	}
136 }
137 
SetPath(QString path)138 void FileSelectWidget::SetPath(QString path)
139 {
140 	lineEdit->setText(path);
141 	slotChangedFile();
142 }
143 
slotChangedFile()144 void FileSelectWidget::slotChangedFile()
145 {
146 	if (lineEdit->text() != actualText)
147 	{
148 		actualText = lineEdit->text();
149 		QString filename = AnimatedFileName(actualText, 0);
150 
151 		cResourceHttpProvider httpProvider(filename);
152 		if (httpProvider.IsUrl()) filename = httpProvider.cacheAndGetFilename();
153 
154 		QPixmap pixmap;
155 
156 		cRadianceHDR radianceHDR;
157 		int w;
158 		int h;
159 		if (radianceHDR.Init(filename, &w, &h))
160 		{
161 			radianceHDR.LoadToQPixmap(&pixmap);
162 		}
163 		else
164 		{
165 			pixmap.load(filename);
166 		}
167 
168 		if (pixmap.isNull())
169 		{
170 			labelImage->setText(QObject::tr("file path invalid"));
171 		}
172 		else
173 		{
174 			if (pixmap.height() / pixmap.width() > 150 / 300)
175 			{
176 				labelImage->setPixmap(pixmap.scaledToHeight(150, Qt::SmoothTransformation));
177 			}
178 			else
179 			{
180 				labelImage->setPixmap(pixmap.scaledToWidth(300, Qt::SmoothTransformation));
181 			}
182 		}
183 	}
184 }
185