[TD]fix scaling of bitmap

This commit is contained in:
Wanderer Fan
2022-06-23 10:39:59 -04:00
committed by WandererFan
parent ab9ff476e1
commit 11c6f4a9e9
7 changed files with 97 additions and 17 deletions

View File

@@ -34,7 +34,9 @@
#include <Base/Exception.h>
#include <Base/FileInfo.h>
#include <Base/Console.h>
#include <App/Document.h>
#include "DrawUtil.h"
#include "DrawViewImage.h"
using namespace TechDraw;
@@ -53,8 +55,10 @@ DrawViewImage::DrawViewImage(void)
static const char *vgroup = "Image";
ADD_PROPERTY_TYPE(ImageFile,(""),vgroup,App::Prop_None,"The file containing this bitmap");
ADD_PROPERTY_TYPE(Width ,(100),vgroup,App::Prop_None,"The width of the image view");
ADD_PROPERTY_TYPE(Height ,(100),vgroup,App::Prop_None,"The height of the view");
ADD_PROPERTY_TYPE(ImageIncluded, (""), vgroup,App::Prop_None,
"Embedded image file. System use only."); // n/a to end users
ADD_PROPERTY_TYPE(Width ,(100),vgroup,App::Prop_None,"The width of cropped image");
ADD_PROPERTY_TYPE(Height ,(100),vgroup,App::Prop_None,"The height of cropped image");
ScaleType.setValue("Custom");
std::string imgFilter("Image files (*.jpg *.jpeg *.png);;All files (*)");
@@ -67,14 +71,36 @@ DrawViewImage::~DrawViewImage()
void DrawViewImage::onChanged(const App::Property* prop)
{
if (prop == &ImageFile) {
if (!isRestoring()) {
App::Document* doc = getDocument();
if (!isRestoring()) {
if ((prop == &ImageFile) &&
(doc != nullptr) ) {
if (!ImageFile.isEmpty()) {
replaceImageIncluded(ImageFile.getValue());
}
requestPaint();
} else if (prop == &Scale) {
requestPaint();
}
}
TechDraw::DrawView::onChanged(prop);
}
short DrawViewImage::mustExecute() const
{
short result = 0;
if (!isRestoring()) {
result = (Height.isTouched() ||
Width.isTouched());
}
if (result) {
return result;
}
return App::DocumentObject::mustExecute();
}
App::DocumentObjectExecReturn *DrawViewImage::execute(void)
{
requestPaint();
@@ -87,6 +113,41 @@ QRectF DrawViewImage::getRect() const
return result;
}
void DrawViewImage::replaceImageIncluded(std::string newFileName)
{
Base::Console().Message("DVI::replaceImageIncluded(%s)\n", newFileName.c_str());
if (ImageIncluded.isEmpty()) {
setupImageIncluded();
} else {
std::string tempName = ImageIncluded.getExchangeTempFile();
DrawUtil::copyFile(newFileName, tempName);
ImageIncluded.setValue(tempName.c_str());
}
}
void DrawViewImage::setupImageIncluded(void)
{
Base::Console().Message("DVI::setupImageIncluded()\n");
App::Document* doc = getDocument();
std::string dir = doc->TransientDir.getValue();
std::string special = getNameInDocument();
special += "Image.bitmap";
std::string imageName = dir + "/" + special;
//setupImageIncluded is only called when ImageIncluded is empty, so
//just set up the empty file first
DrawUtil::copyFile(std::string(), imageName);
ImageIncluded.setValue(imageName.c_str());
if (!ImageFile.isEmpty()) {
Base::FileInfo fi(ImageFile.getValue());
if (fi.isReadable()) {
std::string exchName = ImageIncluded.getExchangeTempFile();
DrawUtil::copyFile(ImageFile.getValue(), exchName);
ImageIncluded.setValue(exchName.c_str(), special.c_str());
}
}
}
// Python Drawing feature ---------------------------------------------------------

View File

@@ -23,6 +23,8 @@
#ifndef _DrawViewImage_h_
#define _DrawViewImage_h_
#include <Mod/TechDraw/TechDrawGlobal.h>
#include <App/DocumentObject.h>
#include <App/FeaturePython.h>
#include <App/PropertyFile.h>
@@ -36,31 +38,37 @@ namespace TechDraw
class TechDrawExport DrawViewImage : public TechDraw::DrawView
{
PROPERTY_HEADER(TechDraw::DrawViewImage);
PROPERTY_HEADER_WITH_OVERRIDE(TechDraw::DrawViewImage);
public:
/// Constructor
DrawViewImage(void);
virtual ~DrawViewImage();
App::PropertyFile ImageFile;
App::PropertyFloat Width;
App::PropertyFloat Height;
App::PropertyFile ImageFile;
App::PropertyFileIncluded ImageIncluded;
App::PropertyFloat Width;
App::PropertyFloat Height;
/** @name methods override Feature */
//@{
/// recalculate the Feature
virtual App::DocumentObjectExecReturn *execute(void);
App::DocumentObjectExecReturn *execute(void) override;
//@}
short mustExecute() const override;
/// returns the type name of the ViewProvider
virtual const char* getViewProviderName(void) const {
const char* getViewProviderName(void) const override {
return "TechDrawGui::ViewProviderImage";
}
virtual QRectF getRect() const;
QRectF getRect() const override;
protected:
virtual void onChanged(const App::Property* prop);
void setupImageIncluded(void);
void replaceImageIncluded(std::string newFileName);
void onChanged(const App::Property* prop) override;
Base::BoundBox3d bbox;
};

View File

@@ -23,6 +23,8 @@
#ifndef DRAWINGGUI_QGCUSTOMCLIP_H
#define DRAWINGGUI_QGCUSTOMCLIP_H
#include <Mod/TechDraw/TechDrawGlobal.h>
#include <QGraphicsItem>
#include <QPointF>
#include <QRectF>

View File

@@ -85,7 +85,7 @@ bool QGCustomImage::load(QPixmap map)
QSize QGCustomImage::imageSize(void)
{
QSize result = m_px.size();
QSize result = m_px.size() * scale();
return result;
}

View File

@@ -23,6 +23,8 @@
#ifndef DRAWINGGUI_QGCUSTOMIMAGE_H
#define DRAWINGGUI_QGCUSTOMIMAGE_H
#include <Mod/TechDraw/TechDrawGlobal.h>
#include <QByteArray>
#include <QGraphicsItem>
#include <QGraphicsPixmapItem>

View File

@@ -145,8 +145,8 @@ void QGIViewImage::drawImage()
return;
}
if (!viewImage->ImageFile.isEmpty()) {
QString fileSpec = QString::fromUtf8(viewImage->ImageFile.getValue(),strlen(viewImage->ImageFile.getValue()));
if (!viewImage->ImageIncluded.isEmpty()) {
QString fileSpec = QString::fromUtf8(viewImage->ImageIncluded.getValue(),strlen(viewImage->ImageIncluded.getValue()));
m_imageItem->load(fileSpec);
m_imageItem->setScale(viewImage->getScale());
QRectF br = m_cliparea->rect();
@@ -164,5 +164,3 @@ void QGIViewImage::rotateView(void)
double rot = getViewObject()->Rotation.getValue();
m_cliparea->setRotation(-rot);
}

View File

@@ -70,6 +70,15 @@ std::vector<std::string> ViewProviderImage::getDisplayModes(void) const
void ViewProviderImage::updateData(const App::Property* prop)
{
if (prop == &(getViewObject()->Width) ||
prop == &(getViewObject()->Height) ||
prop == &(getViewObject()->Scale) ) {
QGIView* qgiv = getQView();
if (qgiv) {
qgiv->QGIView::updateView(true);
}
}
ViewProviderDrawingView::updateData(prop);
}