diff --git a/src/Mod/TechDraw/App/DrawViewImage.cpp b/src/Mod/TechDraw/App/DrawViewImage.cpp index 6341688432..cb6004810c 100644 --- a/src/Mod/TechDraw/App/DrawViewImage.cpp +++ b/src/Mod/TechDraw/App/DrawViewImage.cpp @@ -23,16 +23,16 @@ #include "PreCompiled.h" #ifndef _PreComp_ -# include -# include +#include +#include #endif #include #include #include -#include "DrawViewImage.h" #include "DrawUtil.h" +#include "DrawViewImage.h" using namespace TechDraw; @@ -46,13 +46,13 @@ PROPERTY_SOURCE(TechDraw::DrawViewImage, TechDraw::DrawView) DrawViewImage::DrawViewImage() { - static const char *vgroup = "Image"; + static const char* vgroup = "Image"; ADD_PROPERTY_TYPE(ImageFile, (""), vgroup, App::Prop_None, "The file containing this bitmap"); 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"); + "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"); Scale.setStatus(App::Property::Hidden, false); @@ -66,6 +66,7 @@ void DrawViewImage::onChanged(const App::Property* prop) { if (isRestoring()) { TechDraw::DrawView::onChanged(prop); + return; } if (prop == &ImageFile) { @@ -76,20 +77,17 @@ void DrawViewImage::onChanged(const App::Property* prop) TechDraw::DrawView::onChanged(prop); } -App::DocumentObjectExecReturn *DrawViewImage::execute() +App::DocumentObjectExecReturn* DrawViewImage::execute() { requestPaint(); return DrawView::execute(); } -QRectF DrawViewImage::getRect() const -{ - return { 0.0, 0.0, Width.getValue(), Height.getValue()}; -} +QRectF DrawViewImage::getRect() const { return {0.0, 0.0, Width.getValue(), Height.getValue()}; } void DrawViewImage::replaceImageIncluded(std::string newImageFile) { -// Base::Console().Message("DVI::replaceImageIncluded(%s)\n", newImageFile.c_str()); + // Base::Console().Message("DVI::replaceImageIncluded(%s)\n", newImageFile.c_str()); if (newImageFile.empty()) { return; } @@ -97,27 +95,30 @@ void DrawViewImage::replaceImageIncluded(std::string newImageFile) Base::FileInfo tfi(newImageFile); if (tfi.isReadable()) { ImageIncluded.setValue(newImageFile.c_str()); - } else { + } + else { throw Base::RuntimeError("Could not read the new image file"); } } void DrawViewImage::setupObject() { -// Base::Console().Message("DVI::setupObject()\n"); + // Base::Console().Message("DVI::setupObject()\n"); replaceImageIncluded(ImageFile.getValue()); } // Python Drawing feature --------------------------------------------------------- -namespace App { +namespace App +{ /// @cond DOXERR PROPERTY_SOURCE_TEMPLATE(TechDraw::DrawViewImagePython, TechDraw::DrawViewImage) -template<> const char* TechDraw::DrawViewImagePython::getViewProviderName() const { +template<> const char* TechDraw::DrawViewImagePython::getViewProviderName() const +{ return "TechDrawGui::ViewProviderImage"; } /// @endcond // explicit template instantiation template class TechDrawExport FeaturePythonT; -} +}// namespace App diff --git a/src/Mod/TechDraw/Gui/TaskActiveView.cpp b/src/Mod/TechDraw/Gui/TaskActiveView.cpp index 9d54ff4619..dbfcc122e8 100644 --- a/src/Mod/TechDraw/Gui/TaskActiveView.cpp +++ b/src/Mod/TechDraw/Gui/TaskActiveView.cpp @@ -23,10 +23,10 @@ #include "PreCompiled.h" #ifndef _PreComp_ -# include -# include -# include -#endif // #ifndef _PreComp_ +#include +#include +#include +#endif // #ifndef _PreComp_ #include #include @@ -41,10 +41,10 @@ #include #include -#include "ui_TaskActiveView.h" -#include "TaskActiveView.h" #include "Grabber3d.h" +#include "TaskActiveView.h" #include "ViewProviderImage.h" +#include "ui_TaskActiveView.h" using namespace Gui; @@ -52,12 +52,9 @@ using namespace TechDraw; using namespace TechDrawGui; //ctor for creation -TaskActiveView::TaskActiveView(TechDraw::DrawPage* pageFeat) : - ui(new Ui_TaskActiveView), - m_pageFeat(pageFeat), - m_imageFeat(nullptr), - m_btnOK(nullptr), - m_btnCancel(nullptr) +TaskActiveView::TaskActiveView(TechDraw::DrawPage* pageFeat) + : ui(new Ui_TaskActiveView), m_pageFeat(pageFeat), m_imageFeat(nullptr), m_btnOK(nullptr), + m_btnCancel(nullptr) { ui->setupUi(this); @@ -67,18 +64,16 @@ TaskActiveView::TaskActiveView(TechDraw::DrawPage* pageFeat) : setUiPrimary(); } -TaskActiveView::~TaskActiveView() -{ -} +TaskActiveView::~TaskActiveView() {} void TaskActiveView::updateTask() { -// blockUpdate = true; + // blockUpdate = true; -// blockUpdate = false; + // blockUpdate = false; } -void TaskActiveView::changeEvent(QEvent *e) +void TaskActiveView::changeEvent(QEvent* e) { if (e->type() == QEvent::LanguageChange) { ui->retranslateUi(this); @@ -87,18 +82,15 @@ void TaskActiveView::changeEvent(QEvent *e) void TaskActiveView::setUiPrimary() { -// Base::Console().Message("TAV::setUiPrimary()\n"); + // Base::Console().Message("TAV::setUiPrimary()\n"); setWindowTitle(QObject::tr("ActiveView to TD View")); } -void TaskActiveView::blockButtons(bool b) -{ - Q_UNUSED(b); -} +void TaskActiveView::blockButtons(bool b) { Q_UNUSED(b); } TechDraw::DrawViewImage* TaskActiveView::createActiveView() { -// Base::Console().Message("TAV::createActiveView()\n"); + // Base::Console().Message("TAV::createActiveView()\n"); //make sure there is an 3D MDI to grab!! if (!Gui::getMainWindow()) { @@ -109,7 +101,8 @@ TechDraw::DrawViewImage* TaskActiveView::createActiveView() App::Document* pageDocument = m_pageFeat->getDocument(); std::string documentName = m_pageFeat->getDocument()->getName(); - Gui::Document* pageGuiDocument = Gui::Application::Instance->getDocument(pageDocument->getName()); + Gui::Document* pageGuiDocument = + Gui::Application::Instance->getDocument(pageDocument->getName()); //if the active view is a 3d window, use that. View3DInventor* view3d = qobject_cast(Gui::getMainWindow()->activeWindow()); @@ -118,7 +111,8 @@ TechDraw::DrawViewImage* TaskActiveView::createActiveView() auto views3dAll = pageGuiDocument->getMDIViewsOfType(Gui::View3DInventor::getClassTypeId()); if (!views3dAll.empty()) { view3d = qobject_cast(views3dAll.front()); - } else { + } + else { //this code is only for the rare case where the page's document does not have a //3D window. It might occur if the user closes the 3D window, but leaves, for //example, a DrawPage window open. @@ -148,48 +142,58 @@ TechDraw::DrawViewImage* TaskActiveView::createActiveView() //the Page's document may not be the active one, so we need to get the right //document by name instead of using ActiveDocument - Command::doCommand(Command::Doc,"App.getDocument('%s').addObject('%s','%s')", - documentName.c_str(), - imageType.c_str(), imageName.c_str()); - Command::doCommand(Command::Doc,"App.getDocument('%s').%s.addView(App.getDocument('%s').%s)", - documentName.c_str(), pageName.c_str(), - documentName.c_str(), imageName.c_str()); + Command::doCommand(Command::Doc, "App.getDocument('%s').addObject('%s','%s')", + documentName.c_str(), imageType.c_str(), imageName.c_str()); + Command::doCommand(Command::Doc, "App.getDocument('%s').%s.addView(App.getDocument('%s').%s)", + documentName.c_str(), pageName.c_str(), documentName.c_str(), + imageName.c_str()); App::Document* doc = m_pageFeat->getDocument(); std::string special = "/" + imageName + "image.png"; std::string dir = doc->TransientDir.getValue(); std::string fileSpec = dir + special; + //fixes fail to create 2nd Active view with same name in old docs + Base::FileInfo fi(fileSpec); + if (fi.exists()) { + //old filename were unique by pageName + imageName only + fi.deleteFile(); + } + + //better way of making temp file name + std::string baseName = pageName + imageName; + std::string tempName = + Base::FileInfo::getTempFileName(baseName.c_str(), doc->TransientDir.getValue()) + ".png"; + QColor bg = ui->ccBgColor->color(); if (ui->cbUse3d->isChecked()) { bg = QColor(); - } else if (ui->cbNoBG->isChecked()) { + } + else if (ui->cbNoBG->isChecked()) { bg = QColor(Qt::transparent); } - QImage image(100, 100, QImage::Format_RGB32); //arbitrary initial image size. quickView will use - //MdiView size in pixels + QImage image(100, 100, + QImage::Format_RGB32); //arbitrary initial image size. quickView will use + //MdiView size in pixels image.fill(QColor(Qt::transparent)); - Grabber3d:: quickView(view3d, bg, image); - bool success = image.save(Base::Tools::fromStdString(fileSpec)); + Grabber3d::quickView(view3d, bg, image); + bool success = image.save(Base::Tools::fromStdString(tempName)); if (!success) { Base::Console().Error("ActiveView could not save file: %s\n", fileSpec.c_str()); } + //backslashes in windows fileSpec upsets python - std::regex rxBackslash("\\\\"); - std::string noBackslash = std::regex_replace(fileSpec, rxBackslash, "/"); - Command::doCommand(Command::Doc,"App.getDocument('%s').%s.ImageFile = '%s'", - documentName.c_str(), - imageName.c_str(), noBackslash.c_str()); - Command::doCommand(Command::Doc,"App.getDocument('%s').%s.Width = %.5f", - documentName.c_str(), - imageName.c_str(), - ui->qsbWidth->rawValue()); - Command::doCommand(Command::Doc,"App.getDocument('%s').%s.Height = %.5f", - documentName.c_str(), - imageName.c_str(), - ui->qsbHeight->rawValue()); + std::regex rxBackslash("\\\\"); //this rx really means match to a single '\' + std::string noBackslash = std::regex_replace(tempName, rxBackslash, "/"); + + Command::doCommand(Command::Doc, "App.getDocument('%s').%s.ImageFile = '%s'", + documentName.c_str(), imageName.c_str(), noBackslash.c_str()); + Command::doCommand(Command::Doc, "App.getDocument('%s').%s.Width = %.5f", documentName.c_str(), + imageName.c_str(), ui->qsbWidth->rawValue()); + Command::doCommand(Command::Doc, "App.getDocument('%s').%s.Height = %.5f", documentName.c_str(), + imageName.c_str(), ui->qsbHeight->rawValue()); App::DocumentObject* newObj = m_pageFeat->getDocument()->getObject(imageName.c_str()); TechDraw::DrawViewImage* newImg = dynamic_cast(newObj); @@ -211,8 +215,7 @@ TechDraw::DrawViewImage* TaskActiveView::createActiveView() //****************************************************************************** -void TaskActiveView::saveButtons(QPushButton* btnOK, - QPushButton* btnCancel) +void TaskActiveView::saveButtons(QPushButton* btnOK, QPushButton* btnCancel) { m_btnOK = btnOK; m_btnCancel = btnCancel; @@ -228,10 +231,10 @@ void TaskActiveView::enableTaskButtons(bool b) bool TaskActiveView::accept() { -// Base::Console().Message("TAV::accept()\n"); + // Base::Console().Message("TAV::accept()\n"); Gui::Command::openCommand(QT_TRANSLATE_NOOP("Command", "Create ActiveView")); m_imageFeat = createActiveView(); -// m_imageFeat->requestPaint(); + // m_imageFeat->requestPaint(); if (m_imageFeat) { m_imageFeat->recomputeFeature(); } @@ -245,8 +248,8 @@ bool TaskActiveView::accept() bool TaskActiveView::reject() { -// Base::Console().Message("TAV::reject()\n"); - //nothing to remove. + // Base::Console().Message("TAV::reject()\n"); + //nothing to remove. Gui::Command::doCommand(Gui::Command::Gui, "App.activeDocument().recompute()"); Gui::Command::doCommand(Gui::Command::Gui, "Gui.ActiveDocument.resetEdit()"); @@ -254,23 +257,20 @@ bool TaskActiveView::reject() return false; } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -TaskDlgActiveView::TaskDlgActiveView(TechDraw::DrawPage* page) - : TaskDialog() +TaskDlgActiveView::TaskDlgActiveView(TechDraw::DrawPage* page) : TaskDialog() { - widget = new TaskActiveView(page); + widget = new TaskActiveView(page); taskbox = new Gui::TaskView::TaskBox(Gui::BitmapFactory().pixmap("actions/TechDraw_ActiveView"), - widget->windowTitle(), true, nullptr); + widget->windowTitle(), true, nullptr); taskbox->groupLayout()->addWidget(widget); Content.push_back(taskbox); } -TaskDlgActiveView::~TaskDlgActiveView() -{ -} +TaskDlgActiveView::~TaskDlgActiveView() {} void TaskDlgActiveView::update() { -// widget->updateTask(); + // widget->updateTask(); } void TaskDlgActiveView::modifyStandardButtons(QDialogButtonBox* box) @@ -281,13 +281,9 @@ void TaskDlgActiveView::modifyStandardButtons(QDialogButtonBox* box) } //==== calls from the TaskView =============================================================== -void TaskDlgActiveView::open() -{ -} +void TaskDlgActiveView::open() {} -void TaskDlgActiveView::clicked(int) -{ -} +void TaskDlgActiveView::clicked(int) {} bool TaskDlgActiveView::accept() {