Debian Codegraph @

src/Gui/TextureMapping.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2010 Werner Mayer <wmayer[at]users.sourceforge.net> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Library General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU Library General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Library General Public *
17 * License along with this library; see the file COPYING.LIB. If not, *
18 * write to the Free Software Foundation, Inc., 59 Temple Place, *
19 * Suite 330, Boston, MA 02111-1307, USA *
20 * *
21 ***************************************************************************/
22
23
24 #include <Inventor/nodes/SoGroup.h>
25 #include <Inventor/nodes/SoTexture2.h>
26 #include <QImage>
27 #include <QImageReader>
28 #include <QKeyEvent>
29 #include <QMessageBox>
30
31
32 #include <Inventor/nodes/SoTextureCoordinateEnvironment.h>
33
34 #include "TextureMapping.h"
35 #include "ui_TextureMapping.h"
36 #include "Application.h"
37 #include "BitmapFactory.h"
38 #include "Document.h"
39 #include "View3DInventor.h"
40 #include "View3DInventorViewer.h"
41
42
43 using namespace Gui::Dialog;
44
45 /* TRANSLATOR Gui::Dialog::TextureMapping */
46
47 TextureMapping::TextureMapping(QWidget* parent, Qt::WindowFlags fl)
48 : QDialog(parent, fl)
49 , grp(nullptr)
50 , tex(nullptr)
51 , env(nullptr)
52 {
53 ui = new Ui_TextureMapping();
54 ui->setupUi(this);
55 connect(
56 ui->fileChooser,
57 &FileChooser::fileNameSelected,
58 this,
59 &TextureMapping::onFileChooserFileNameSelected
60 );
61 connect(ui->checkEnv, &QCheckBox::toggled, this, &TextureMapping::onCheckEnvToggled);
62
63 ui->checkGlobal->hide();
64
65 // set a dummy string which is not a valid file name
66 fileName = QLatin1String("<>");
67
68 // add all supported QImage formats
69 QStringList formats;
70 QList<QByteArray> qtformats = QImageReader::supportedImageFormats();
71 for (const auto& it : qtformats) {
72 formats << QStringLiteral("*.%1").arg(QLatin1String(it));
73 }
74
75 ui->fileChooser->setFilter(tr("Image files (%1)").arg(formats.join(QLatin1String(" "))));
76
77 this->tex = new SoTexture2();
78 this->tex->ref();
79 this->env = new SoTextureCoordinateEnvironment();
80 this->env->ref();
81
82 std::string path = App::GetApplication().Config()["TextureImage"];
83 if (!path.empty()) {
84 QString file = QString::fromUtf8(path.c_str());
85 ui->fileChooser->setFileName(file);
86 onFileChooserFileNameSelected(file);
87 }
88 }
89
90 TextureMapping::~TextureMapping()
91 {
92 this->tex->unref();
93 this->env->unref();
94 delete ui;
95 }
96
97 void TextureMapping::accept()
98 {
99 QDialog::accept();
100 }
101
102 void TextureMapping::reject()
103 {
104 if (this->grp) {
105 this->grp->removeChild(this->tex);
106 if (this->grp->findChild(this->env) > -1) {
107 this->grp->removeChild(this->env);
108 }
109 this->grp->unref();
110 }
111
112 QDialog::reject();
113 }
114
115 void TextureMapping::changeEvent(QEvent* e)
116 {
117 if (e->type() == QEvent::LanguageChange) {
118 ui->retranslateUi(this);
119 }
120 else {
121 QDialog::changeEvent(e);
122 }
123 }
124
125 void TextureMapping::keyPressEvent(QKeyEvent* e)
126 {
127 // The texture mapping dialog is embedded into a task panel
128 // which is a parent widget and will handle the event
129 e->ignore();
130 }
131
132 void TextureMapping::onFileChooserFileNameSelected(const QString& s)
133 {
134 QImage image;
135 if (!image.load(s)) {
136 // This slot is always invoked when leaving the focus of the line edit
137 // and to avoid to run into an infinite loop of popping up the dialog
138 // there is a check to report the last selected file name (which might
139 // be an empty string) only once.
140 if (fileName != s) {
141 QMessageBox::warning(
142 this,
143 tr("No image"),
144 tr("The specified file is not a valid image file.")
145 );
146 fileName = s;
147 }
148 return;
149 }
150
151 if (!this->grp) {
152 Gui::Document* doc = Gui::Application::Instance->activeDocument();
153 if (doc) {
154 auto view = qobject_cast<View3DInventor*>(doc->getActiveView());
155 if (view) {
156 this->grp = static_cast<SoGroup*>(view->getViewer()->getSceneGraph());
157 this->grp->ref();
158 this->grp->insertChild(this->tex, 1);
159 if (ui->checkEnv->isChecked()) {
160 this->grp->insertChild(this->env, 2);
161 }
162 }
163 }
164 }
165
166 if (!this->grp) {
167 QMessageBox::warning(this, tr("No 3D view"), tr("No active 3D view found."));
168 return;
169 }
170
171 SoSFImage texture;
172 Gui::BitmapFactory().convert(image, texture);
173 this->tex->image = texture;
174 // this->tex->filename = (const char*)s.toUtf8();
175 App::GetApplication().Config()["TextureImage"] = (const char*)s.toUtf8();
176 }
177
178 void TextureMapping::onCheckEnvToggled(bool b)
179 {
180 if (!this->grp) {
181 return;
182 }
183 if (b) {
184 this->grp->insertChild(this->env, 2);
185 }
186 else {
187 this->grp->removeChild(this->env);
188 }
189 }
190
191 // ---------------------------------------
192
193 TaskTextureMapping::TaskTextureMapping()
194 {
195 dialog = new TextureMapping();
196 addTaskBox(dialog);
197 }
198
199 TaskTextureMapping::~TaskTextureMapping() = default;
200
201 bool TaskTextureMapping::accept()
202 {
203 dialog->accept();
204 return (dialog->result() == QDialog::Accepted);
205 }
206
207 bool TaskTextureMapping::reject()
208 {
209 dialog->reject();
210 return (dialog->result() == QDialog::Rejected);
211 }
212
213 #include "moc_TextureMapping.cpp"