[graphics/drawy] src: fix: image tool not preserving aspect ratio

Prayag Jain <[email protected]>
Newsgroups gmane.comp.kde.cvs
Message-ID <[email protected]>
Git commit 923742d8a932ebce90d389bf6d764ea706ba18cb by Prayag Jain, on behalf of Nikolay Kochulin.
Committed on 16/08/2026 at 11:32.
Pushed by prayag into branch 'master'.

fix: image tool not preserving aspect ratio

BUG: 524264

M  +18   -0    src/gui/item/image.cpp
M  +4    -0    src/gui/item/image.hpp
M  +2    -15   src/widgets/mime/imagemimehandler.cpp
M  +1    -1    src/widgets/tools/imagetool.cpp

https://invent.kde.org/graphics/drawy/-/commit/923742d8a932ebce90d389bf6d764ea706ba18cb

diff --git a/src/gui/item/image.cpp b/src/gui/item/image.cpp
index 47bec97d..a537b097 100644
--- a/src/gui/item/image.cpp
+++ b/src/gui/item/image.cpp
@@ -68,6 +68,24 @@ void ImageItem::setPixmap(QPixmap pixmap)
     m_pixmap = std::move(pixmap);
 }
 
+QSizeF ImageItem::pastedSize() const
+{
+    qreal width{static_cast<qreal>(m_pixmap.width())};
+    qreal height{static_cast<qreal>(m_pixmap.height())};
+
+    if (std::max(width, height) > Common::pastedImageBoxSize) {
+        if (width > height) {
+            height = height / width * Common::pastedImageBoxSize;
+            width = Common::pastedImageBoxSize;
+        } else {
+            width = width / height * Common::pastedImageBoxSize;
+            height = Common::pastedImageBoxSize;
+        }
+    }
+
+    return QSizeF{width, height};
+}
+
 void ImageItem::setBox(QRectF box)
 {
     m_boundingBox = box;
diff --git a/src/gui/item/image.hpp b/src/gui/item/image.hpp
index e90e3a09..141bbdd7 100644
--- a/src/gui/item/image.hpp
+++ b/src/gui/item/image.hpp
@@ -7,6 +7,8 @@
 
 #include "item.hpp"
 
+#include <QSizeF>
+
 class QPixmap;
 
 class LIBDRAWYGUI_EXPORT ImageItem : public Item
@@ -29,6 +31,8 @@ public:
     [[nodiscard]] const QPixmap &pixmap() const;
     void setPixmap(QPixmap pixmap);
 
+    QSizeF pastedSize() const;
+
     void setBox(QRectF box);
 
 protected:
diff --git a/src/widgets/mime/imagemimehandler.cpp b/src/widgets/mime/imagemimehandler.cpp
index 0583e1b2..adeb9b3a 100644
--- a/src/widgets/mime/imagemimehandler.cpp
+++ b/src/widgets/mime/imagemimehandler.cpp
@@ -40,23 +40,10 @@ QList<std::shared_ptr<Item>> ImageMimeHandler::tryReadData(const QMimeData &mime
     const std::shared_ptr<ImageItem> image = std::make_shared<ImageItem>();
     image->setPixmap(pixmap);
 
-    qreal width{static_cast<qreal>(pixmap.width())};
-    qreal height{static_cast<qreal>(pixmap.height())};
+    const QSizeF boxSize{image->pastedSize() / m_context->renderingContext()->zoomFactor()};
 
-    if (std::max(width, height) > Common::pastedImageBoxSize) {
-        if (width > height) {
-            height = height / width * Common::pastedImageBoxSize;
-            width = Common::pastedImageBoxSize;
-        } else {
-            width = width / height * Common::pastedImageBoxSize;
-            height = Common::pastedImageBoxSize;
-        }
-    }
-
-    width /= m_context->renderingContext()->zoomFactor();
-    height /= m_context->renderingContext()->zoomFactor();
+    image->setBox({0, 0, boxSize.width(), boxSize.height()});
 
-    image->setBox({0, 0, width, height});
     return {image};
 }
 
diff --git a/src/widgets/tools/imagetool.cpp b/src/widgets/tools/imagetool.cpp
index 7e3d960a..ed443684 100644
--- a/src/widgets/tools/imagetool.cpp
+++ b/src/widgets/tools/imagetool.cpp
@@ -66,10 +66,10 @@ void ImageTool::mouseReleased(ApplicationContext *context)
         pixmap.load(fileName);
 
         const std::shared_ptr<ImageItem> curItem = std::dynamic_pointer_cast<ImageItem>(m_itemFactory->create());
-        curItem->setBox(QRectF(transformer.viewToWorld(lastPoint), transformer.viewToWorld(QSizeF(100, 100))));
 
         curItem->setProperty(Property::Type::Opacity, uiContext->propertyManager()->value(Property::Type::Opacity));
         curItem->setPixmap(pixmap);
+        curItem->setBox(QRectF(transformer.viewToWorld(lastPoint), transformer.viewToWorld(curItem->pastedSize())));
 
         QList<std::shared_ptr<Item>> lst{curItem};
         commandHistory->push(std::make_shared<InsertItemCommand>(lst));
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.