| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2011 Jürgen Riegel ([email protected]) * |
| 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 <sstream> |
| 25 |
#include <QAction> |
| 26 |
#include <QFileInfo> |
| 27 |
#include <QImage> |
| 28 |
#include <QMenu> |
| 29 |
#include <QString> |
| 30 |
#include <QSvgRenderer> |
| 31 |
#include <Inventor/nodes/SoCoordinate3.h> |
| 32 |
#include <Inventor/nodes/SoFaceSet.h> |
| 33 |
#include <Inventor/nodes/SoLightModel.h> |
| 34 |
#include <Inventor/nodes/SoMaterial.h> |
| 35 |
#include <Inventor/nodes/SoSeparator.h> |
| 36 |
#include <Inventor/nodes/SoShapeHints.h> |
| 37 |
#include <Inventor/nodes/SoTexture2.h> |
| 38 |
#include <Inventor/nodes/SoTextureCoordinate2.h> |
| 39 |
|
| 40 |
|
| 41 |
#include <App/Document.h> |
| 42 |
#include <Gui/ActionFunction.h> |
| 43 |
#include <Gui/BitmapFactory.h> |
| 44 |
#include <Gui/Control.h> |
| 45 |
#include <Gui/TaskView/TaskImage.h> |
| 46 |
#include <App/ImagePlane.h> |
| 47 |
|
| 48 |
#include "ViewProviderImagePlane.h" |
| 49 |
|
| 50 |
|
| 51 |
using namespace Gui; |
| 52 |
|
| 53 |
|
| 54 |
PROPERTY_SOURCE(Gui::ViewProviderImagePlane, Gui::ViewProviderGeometryObject) |
| 55 |
const char* ViewProviderImagePlane::LightingEnums[] = {"One side", "Two side", nullptr}; |
| 56 |
|
| 57 |
ViewProviderImagePlane::ViewProviderImagePlane() |
| 58 |
{ |
| 59 |
ADD_PROPERTY_TYPE(Lighting, (1L), "Object Style", App::Prop_None, "Set object lighting."); |
| 60 |
Lighting.setEnums(LightingEnums); |
| 61 |
|
| 62 |
texture = new SoTexture2; |
| 63 |
texture->ref(); |
| 64 |
|
| 65 |
pcCoords = new SoCoordinate3(); |
| 66 |
pcCoords->ref(); |
| 67 |
|
| 68 |
shapeHints = new SoShapeHints; |
| 69 |
shapeHints->shapeType = SoShapeHints::UNKNOWN_SHAPE_TYPE; |
| 70 |
shapeHints->vertexOrdering = SoShapeHints::COUNTERCLOCKWISE; |
| 71 |
shapeHints->ref(); |
| 72 |
|
| 73 |
sPixmap = "image-plane"; |
| 74 |
|
| 75 |
ShapeAppearance.setDiffuseColor(1.0F, 1.0F, 1.0F); |
| 76 |
} |
| 77 |
|
| 78 |
ViewProviderImagePlane::~ViewProviderImagePlane() |
| 79 |
{ |
| 80 |
pcCoords->unref(); |
| 81 |
texture->unref(); |
| 82 |
shapeHints->unref(); |
| 83 |
} |
| 84 |
|
| 85 |
void ViewProviderImagePlane::attach(App::DocumentObject* pcObj) |
| 86 |
{ |
| 87 |
ViewProviderGeometryObject::attach(pcObj); |
| 88 |
|
| 89 |
// NOTE: SoFCSelection node has beem removed because it led to |
| 90 |
// problems using the image as a construction plane with the |
| 91 |
// draft commands |
| 92 |
SoSeparator* shading = new SoSeparator; |
| 93 |
shading->addChild(pcCoords); |
| 94 |
|
| 95 |
SoTextureCoordinate2* textCoord = new SoTextureCoordinate2; |
| 96 |
textCoord->point.set1Value(0, 0, 0); |
| 97 |
textCoord->point.set1Value(1, 1, 0); |
| 98 |
textCoord->point.set1Value(2, 1, 1); |
| 99 |
textCoord->point.set1Value(3, 0, 1); |
| 100 |
shading->addChild(textCoord); |
| 101 |
|
| 102 |
// texture |
| 103 |
texture->model = SoTexture2::MODULATE; |
| 104 |
shading->addChild(texture); |
| 105 |
|
| 106 |
shading->addChild(shapeHints); |
| 107 |
shading->addChild(pcShapeMaterial); |
| 108 |
|
| 109 |
// plane |
| 110 |
pcCoords->point.set1Value(0, 0, 0, 0); |
| 111 |
pcCoords->point.set1Value(1, 1, 0, 0); |
| 112 |
pcCoords->point.set1Value(2, 1, 1, 0); |
| 113 |
pcCoords->point.set1Value(3, 0, 1, 0); |
| 114 |
SoFaceSet* faceset = new SoFaceSet; |
| 115 |
faceset->numVertices.set1Value(0, 4); |
| 116 |
shading->addChild(faceset); |
| 117 |
|
| 118 |
addDisplayMaskMode(shading, "Shading"); |
| 119 |
|
| 120 |
SoLightModel* lightmodel = new SoLightModel; |
| 121 |
lightmodel->model = SoLightModel::BASE_COLOR; |
| 122 |
|
| 123 |
SoSeparator* noshading = new SoSeparator; |
| 124 |
noshading->addChild(pcCoords); |
| 125 |
noshading->addChild(textCoord); |
| 126 |
noshading->addChild(texture); |
| 127 |
noshading->addChild(shapeHints); |
| 128 |
noshading->addChild(pcShapeMaterial); |
| 129 |
noshading->addChild(lightmodel); |
| 130 |
noshading->addChild(faceset); |
| 131 |
|
| 132 |
addDisplayMaskMode(noshading, "No shading"); |
| 133 |
} |
| 134 |
|
| 135 |
void ViewProviderImagePlane::setDisplayMode(const char* ModeName) |
| 136 |
{ |
| 137 |
if (strcmp("Shading", ModeName) == 0) { |
| 138 |
setDisplayMaskMode("Shading"); |
| 139 |
} |
| 140 |
else if (strcmp("No shading", ModeName) == 0) { |
| 141 |
setDisplayMaskMode("No shading"); |
| 142 |
} |
| 143 |
ViewProviderGeometryObject::setDisplayMode(ModeName); |
| 144 |
} |
| 145 |
|
| 146 |
std::vector<std::string> ViewProviderImagePlane::getDisplayModes() const |
| 147 |
{ |
| 148 |
std::vector<std::string> StrList; |
| 149 |
StrList.emplace_back("Shading"); |
| 150 |
StrList.emplace_back("No shading"); |
| 151 |
return StrList; |
| 152 |
} |
| 153 |
|
| 154 |
void ViewProviderImagePlane::onChanged(const App::Property* prop) |
| 155 |
{ |
| 156 |
if (prop == &Lighting) { |
| 157 |
if (Lighting.getValue() == 0) { |
| 158 |
shapeHints->vertexOrdering = SoShapeHints::UNKNOWN_ORDERING; |
| 159 |
} |
| 160 |
else { |
| 161 |
shapeHints->vertexOrdering = SoShapeHints::COUNTERCLOCKWISE; |
| 162 |
} |
| 163 |
} |
| 164 |
ViewProviderGeometryObject::onChanged(prop); |
| 165 |
} |
| 166 |
|
| 167 |
void ViewProviderImagePlane::setupContextMenu(QMenu* menu, QObject* receiver, const char* member) |
| 168 |
{ |
| 169 |
Gui::ActionFunction* func = new Gui::ActionFunction(menu); |
| 170 |
QAction* action = menu->addAction(QObject::tr("Change Image")); |
| 171 |
action->setIcon(QIcon(QLatin1String("images:image-scaling.svg"))); |
| 172 |
func->trigger(action, [this]() { this->manipulateImage(); }); |
| 173 |
|
| 174 |
ViewProviderGeometryObject::setupContextMenu(menu, receiver, member); |
| 175 |
} |
| 176 |
|
| 177 |
bool ViewProviderImagePlane::doubleClicked() |
| 178 |
{ |
| 179 |
manipulateImage(); |
| 180 |
return true; |
| 181 |
} |
| 182 |
|
| 183 |
void ViewProviderImagePlane::manipulateImage() |
| 184 |
{ |
| 185 |
auto dialog = new TaskImageDialog(getObject<Image::ImagePlane>()); |
| 186 |
|
| 187 |
Gui::Control().showDialog(dialog); |
| 188 |
} |
| 189 |
|
| 190 |
void ViewProviderImagePlane::resizePlane(float xsize, float ysize) |
| 191 |
{ |
| 192 |
pcCoords->point.set1Value(0, -(xsize / 2), -(ysize / 2), 0.0); |
| 193 |
pcCoords->point.set1Value(1, +(xsize / 2), -(ysize / 2), 0.0); |
| 194 |
pcCoords->point.set1Value(2, +(xsize / 2), +(ysize / 2), 0.0); |
| 195 |
pcCoords->point.set1Value(3, -(xsize / 2), +(ysize / 2), 0.0); |
| 196 |
} |
| 197 |
|
| 198 |
void ViewProviderImagePlane::loadImage() |
| 199 |
{ |
| 200 |
Image::ImagePlane* imagePlane = static_cast<Image::ImagePlane*>(pcObject); |
| 201 |
std::string fileName = imagePlane->ImageFile.getValue(); |
| 202 |
|
| 203 |
if (!fileName.empty()) { |
| 204 |
QImage impQ; |
| 205 |
if (isSvgFile(fileName.c_str())) { |
| 206 |
impQ = loadSvg(fileName.c_str()); |
| 207 |
} |
| 208 |
else { |
| 209 |
impQ = loadRaster(fileName.c_str()); |
| 210 |
} |
| 211 |
|
| 212 |
QSizeF size = getSizeInMM(impQ); |
| 213 |
setPlaneSize(size, impQ); |
| 214 |
convertToSFImage(impQ); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
void ViewProviderImagePlane::setPlaneSize(const QSizeF& size, const QImage& img) |
| 219 |
{ |
| 220 |
if (!img.isNull()) { |
| 221 |
Image::ImagePlane* imagePlane = static_cast<Image::ImagePlane*>(pcObject); |
| 222 |
// When restoring the document or importing a ImagePlane by eg. pasting it |
| 223 |
// preserve the X and Y size. |
| 224 |
if (!isRestoring() && !pcObject->testStatus(App::ObjectStatus::ObjImporting)) { |
| 225 |
imagePlane->XSize.setValue(size.width()); |
| 226 |
imagePlane->YSize.setValue(size.height()); |
| 227 |
} |
| 228 |
|
| 229 |
imagePlane->XPixelsPerMeter = img.dotsPerMeterX(); |
| 230 |
imagePlane->YPixelsPerMeter = img.dotsPerMeterY(); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
QImage ViewProviderImagePlane::loadRaster(const char* fileName) const |
| 235 |
{ |
| 236 |
QImage img; |
| 237 |
img.load(QString::fromUtf8(fileName)); |
| 238 |
return img; |
| 239 |
} |
| 240 |
|
| 241 |
void ViewProviderImagePlane::reloadIfSvg() |
| 242 |
{ |
| 243 |
Image::ImagePlane* imagePlane = static_cast<Image::ImagePlane*>(pcObject); |
| 244 |
std::string fileName = imagePlane->ImageFile.getValue(); |
| 245 |
|
| 246 |
if (isSvgFile(fileName.c_str())) { |
| 247 |
double xsize = imagePlane->XSize.getValue(); |
| 248 |
double ysize = imagePlane->YSize.getValue(); |
| 249 |
|
| 250 |
QImage impQ = loadSvgOfSize(fileName.c_str(), QSizeF(xsize, ysize)); |
| 251 |
convertToSFImage(impQ); |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
QImage ViewProviderImagePlane::loadSvg(const char* filename) const |
| 256 |
{ |
| 257 |
QSizeF defaultSize = defaultSizeOfSvg(filename); |
| 258 |
QPixmap px = BitmapFactory().pixmapFromSvg(filename, defaultSize); |
| 259 |
return px.toImage(); |
| 260 |
} |
| 261 |
|
| 262 |
QImage ViewProviderImagePlane::loadSvgOfSize(const char* filename, const QSizeF& size) const |
| 263 |
{ |
| 264 |
QSizeF psize = pixelSize(filename, size); |
| 265 |
QPixmap px = BitmapFactory().pixmapFromSvg(filename, psize); |
| 266 |
return px.toImage(); |
| 267 |
} |
| 268 |
|
| 269 |
bool ViewProviderImagePlane::isSvgFile(const char* filename) const |
| 270 |
{ |
| 271 |
QFileInfo fi(QString::fromUtf8(filename)); |
| 272 |
return (fi.suffix().toLower() == QLatin1String("svg")); |
| 273 |
} |
| 274 |
|
| 275 |
QSizeF ViewProviderImagePlane::defaultSizeOfSvg(const char* filename) const |
| 276 |
{ |
| 277 |
QSvgRenderer svg; |
| 278 |
svg.load(QString::fromUtf8(filename)); |
| 279 |
return svg.defaultSize(); |
| 280 |
} |
| 281 |
|
| 282 |
QSizeF ViewProviderImagePlane::getSizeInMM(const QImage& img) const |
| 283 |
{ |
| 284 |
double xPixelsPerM = img.dotsPerMeterX(); |
| 285 |
double width = img.width(); |
| 286 |
width = width * 1000 / xPixelsPerM; |
| 287 |
|
| 288 |
double yPixelsPerM = img.dotsPerMeterY(); |
| 289 |
double height = img.height(); |
| 290 |
height = height * 1000 / yPixelsPerM; |
| 291 |
|
| 292 |
QSizeF sizef; |
| 293 |
sizef.setWidth(width); |
| 294 |
sizef.setHeight(height); |
| 295 |
return sizef; |
| 296 |
} |
| 297 |
|
| 298 |
QSizeF ViewProviderImagePlane::pixelSize(const char* filename, const QSizeF& size) const |
| 299 |
{ |
| 300 |
QImage tmp; |
| 301 |
if (tmp.load(QString::fromUtf8(filename))) { |
| 302 |
double xPixelsPerM = tmp.dotsPerMeterX(); |
| 303 |
double yPixelsPerM = tmp.dotsPerMeterY(); |
| 304 |
|
| 305 |
double width = size.width(); |
| 306 |
double height = size.height(); |
| 307 |
|
| 308 |
width = width * xPixelsPerM / 1000; |
| 309 |
height = height * yPixelsPerM / 1000; |
| 310 |
|
| 311 |
return {width, height}; |
| 312 |
} |
| 313 |
|
| 314 |
return size; |
| 315 |
} |
| 316 |
|
| 317 |
void ViewProviderImagePlane::convertToSFImage(const QImage& img) |
| 318 |
{ |
| 319 |
if (!img.isNull()) { |
| 320 |
SoSFImage sfimg; |
| 321 |
// convert to Coin bitmap |
| 322 |
BitmapFactory().convert(img, sfimg); |
| 323 |
texture->image = sfimg; |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
void ViewProviderImagePlane::updateData(const App::Property* prop) |
| 328 |
{ |
| 329 |
Image::ImagePlane* pcPlaneObj = static_cast<Image::ImagePlane*>(pcObject); |
| 330 |
if (prop == &pcPlaneObj->XSize || prop == &pcPlaneObj->YSize) { |
| 331 |
float xsize = pcPlaneObj->XSize.getValue(); |
| 332 |
float ysize = pcPlaneObj->YSize.getValue(); |
| 333 |
|
| 334 |
resizePlane(xsize, ysize); |
| 335 |
reloadIfSvg(); |
| 336 |
} |
| 337 |
else if (prop == &pcPlaneObj->ImageFile) { |
| 338 |
loadImage(); |
| 339 |
} |
| 340 |
else { |
| 341 |
Gui::ViewProviderGeometryObject::updateData(prop); |
| 342 |
} |
| 343 |
} |