diff --git a/src/app/seamly2d/core/vtooloptionspropertybrowser.cpp b/src/app/seamly2d/core/vtooloptionspropertybrowser.cpp index 4579a4756d17..443f6baf5273 100644 --- a/src/app/seamly2d/core/vtooloptionspropertybrowser.cpp +++ b/src/app/seamly2d/core/vtooloptionspropertybrowser.cpp @@ -506,14 +506,14 @@ void VToolOptionsPropertyBrowser::itemClicked(QGraphicsItem *item) { if (item != nullptr) { - if (item->type() == QGraphicsItem::UserType + static_cast(Tool::BackgroundImage)) + if (item->isEnabled()==false) { return; } - if (item->isEnabled()==false) + if(item->type() == QGraphicsItem::UserType + static_cast(Tool::BackgroundImage)) { - return; + item = nullptr; } } diff --git a/src/app/seamly2d/mainwindow.cpp b/src/app/seamly2d/mainwindow.cpp index 26c4bc96831d..fed1a0061bc8 100644 --- a/src/app/seamly2d/mainwindow.cpp +++ b/src/app/seamly2d/mainwindow.cpp @@ -1688,11 +1688,9 @@ void MainWindow::addImage(DraftImage image) { image.width = image.pixmap.width(); image.height = image.pixmap.height(); - image.xScale = 100; - image.yScale = 100; } - ImageItem *item = new ImageItem(image); + ImageItem *item = new ImageItem(doc, image); doc->addBackgroundImage(image.id, item); draftScene->addItem(item); //Need error dialog @@ -1796,8 +1794,6 @@ void MainWindow::updateImage(DraftImage image) qCDebug(vMainWindow, "YPos = %f", image.yPos); qCDebug(vMainWindow, "Width = %f", image.width); qCDebug(vMainWindow, "Height = %f", image.height); - qCDebug(vMainWindow, "XScale = %f", image.xScale); - qCDebug(vMainWindow, "YScale = %f", image.yScale); qCDebug(vMainWindow, "lock Image Aspect = %s", image.aspectLocked ? "True" : "False"); qCDebug(vMainWindow, "Units = %d", static_cast(image.units)); qCDebug(vMainWindow, "Rotation = %f", image.rotation); @@ -1816,11 +1812,12 @@ void MainWindow::updateImage(DraftImage image) //--------------------------------------------------------------------------------------------------------------------- QString MainWindow::getImageFilename() { - const QString filter = tr("PNG") + QLatin1String(" (*.png);;") + - tr("JPG") + QLatin1String(" (*.jpg);;") + - tr("SVG") + QLatin1String(" (*.svg);;") + - tr("BMP") + QLatin1String(" (*.bmp);;") + - tr("TIF") + QLatin1String(" (*.tf)"); + const QString filter = tr("Images") + QLatin1String(" (*.bmp *.jpg *.png *.svg *.tf);;") + + "BMP" + QLatin1String(" (*.bmp);;") + + "JPG" + QLatin1String(" (*.jpg);;") + + "PNG" + QLatin1String(" (*.png);;") + + "SVG" + QLatin1String(" (*.svg);;") + + "TIF" + QLatin1String(" (*.tf)"); const QString path = qApp->Seamly2DSettings()->getImageFilePath(); @@ -4401,6 +4398,7 @@ void MainWindow::Clear() #endif CleanLayout(); pieceList.clear(); // don't move to CleanLayout() + doc->clearBackgroundImageMap(); qApp->getUndoStack()->clear(); toolProperties->clearPropertyBrowser(); toolProperties->itemClicked(nullptr); diff --git a/src/libs/ifc/xml/vabstractpattern.cpp b/src/libs/ifc/xml/vabstractpattern.cpp index 54da314851c6..b14fa56299b4 100644 --- a/src/libs/ifc/xml/vabstractpattern.cpp +++ b/src/libs/ifc/xml/vabstractpattern.cpp @@ -3130,25 +3130,30 @@ QString VAbstractPattern::useGroupLineWeight(quint32 toolId, QString weight) } -QMap VAbstractPattern::getBackgroundImageMap() +QMap VAbstractPattern::getBackgroundImageMap() { return m_imageMap; } -void VAbstractPattern::addBackgroundImage(qint32 id, ImageItem *item) +void VAbstractPattern::addBackgroundImage(qint32 id, ImageItem *item) { m_imageMap.insert(id, item); } -void VAbstractPattern::removeBackgroundImage(qint32 id) +void VAbstractPattern::removeBackgroundImage(qint32 id) { m_imageMap.remove(id); } -ImageItem* VAbstractPattern::getBackgroundImage(qint32 id) +ImageItem* VAbstractPattern::getBackgroundImage(qint32 id) { return m_imageMap.value(id); } + +void VAbstractPattern::clearBackgroundImageMap() +{ + m_imageMap.clear(); +} diff --git a/src/libs/ifc/xml/vabstractpattern.h b/src/libs/ifc/xml/vabstractpattern.h index f061a53fb8be..6b5f88d86d9f 100644 --- a/src/libs/ifc/xml/vabstractpattern.h +++ b/src/libs/ifc/xml/vabstractpattern.h @@ -179,6 +179,7 @@ class VAbstractPattern : public QObject, public VDomDocument ImageItem * getBackgroundImage(qint32 id); void addBackgroundImage(qint32 id, ImageItem *item); void removeBackgroundImage(qint32 id); + void clearBackgroundImageMap(); QMap GetGradationHeights() const; void SetGradationHeights(const QMap &options); diff --git a/src/libs/tools/image_dialog.cpp b/src/libs/tools/image_dialog.cpp index 39d4396553f7..4a95644c7849 100644 --- a/src/libs/tools/image_dialog.cpp +++ b/src/libs/tools/image_dialog.cpp @@ -62,42 +62,28 @@ //--------------------------------------------------------------------------------------------------------------------- -ImageDialog::ImageDialog(DraftImage image) +ImageDialog::ImageDialog(DraftImage image, qreal minDimension, qreal maxDimension) : ui(new Ui::ImageDialog) , m_image(image) , m_pixmapWidth(image.pixmap.width()) , m_pixmapHeight(image.pixmap.height()) - , m_minScale(.03) - , m_maxScale(1.0) + , m_maxDimension(maxDimension) + , m_minDimension(minDimension) , m_minOpacity(5) , m_flagName(true) { ui->setupUi(this); setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint); - m_minDimension = ui->width_DoubleSpinBox->minimum(); - ui->visibility_CheckBox->hide(); ui->showLabel_Label->hide(); ui->attributes_GroupBox->layout()->update(); - if (m_pixmapWidth >= m_pixmapHeight) - { - m_maxScale = 16000.0 / m_pixmapWidth; - m_minScale = m_minDimension / m_pixmapHeight; - } - else - { - m_maxScale = 16000.0 / m_pixmapHeight; - m_minScale = m_minDimension / m_pixmapWidth; - } - ui->xScale_DoubleSpinBox->setMinimum(m_minScale * 100); - ui->yScale_DoubleSpinBox->setMinimum(m_minScale * 100); - ui->xScale_DoubleSpinBox->setMaximum(m_maxScale * 100); - ui->yScale_DoubleSpinBox->setMaximum(m_maxScale * 100); - - updateSpinboxesRanges(); updateUnits(); + + setXScale(m_image.width / m_pixmapWidth * 100); + setYScale(m_image.height / m_pixmapHeight * 100); + updateImage(); connect(ui->name_LineEdit, &QLineEdit::textChanged, this, &ImageDialog::nameChanged); @@ -152,8 +138,6 @@ void ImageDialog::updateImage() setYPos(m_image.yPos); setWidth(m_image.width); setHeight(m_image.height); - setXScale(m_image.xScale); - setYScale(m_image.yScale); setAspectLocked(m_image.aspectLocked); setRotation(m_image.rotation); setVisibility(m_image.visible); @@ -161,9 +145,6 @@ void ImageDialog::updateImage() enableWidgets(); blockSignals(false); - qDebug("MinScale = %f", m_minScale); - qDebug("MaxScale = %f", m_maxScale); - qDebug("Id = %d", m_image.id); qDebug("Name = %s", qUtf8Printable(m_image.name)); qDebug("Filename = %s", qUtf8Printable(m_image.filename)); @@ -173,8 +154,6 @@ void ImageDialog::updateImage() qDebug("YPos = %f", m_image.yPos); qDebug("Width = %f", m_image.width); qDebug("Height = %f", m_image.height); - qDebug("XScale = %f", m_image.xScale); - qDebug("YScale = %f", m_image.yScale); qDebug("lock Image Aspect = %s", m_image.aspectLocked ? "True" : "False"); qDebug("Units = %d", static_cast(m_image.units)); qDebug("Rotation = %f", m_image.rotation); @@ -278,15 +257,18 @@ qreal ImageDialog::getWidth() const void ImageDialog::setWidth(const qreal &value) { ui->yPosition_DoubleSpinBox->blockSignals(true); + m_image.width = value; + if (m_image.units == Unit::Px) { - ui->width_DoubleSpinBox->setValue(value); + if (ui->width_DoubleSpinBox->value() != value) {ui->width_DoubleSpinBox->setValue(value);}; } else { - ui->width_DoubleSpinBox->setValue(qApp->fromPixel(value)); + if (ui->width_DoubleSpinBox->value() != qApp->fromPixel(value)) {ui->width_DoubleSpinBox->setValue(qApp->fromPixel(value));}; } + ui->yPosition_DoubleSpinBox->blockSignals(false); } @@ -300,44 +282,46 @@ qreal ImageDialog::getHeight() const void ImageDialog::setHeight(const qreal &value) { ui->height_DoubleSpinBox->blockSignals(true); + m_image.height = value; if (m_image.units == Unit::Px) { - ui->height_DoubleSpinBox->setValue(value); + if (ui->height_DoubleSpinBox->value() != value) {ui->height_DoubleSpinBox->setValue(value);}; } else { - ui->height_DoubleSpinBox->setValue(qApp->fromPixel(value)); + if (ui->height_DoubleSpinBox->value() != qApp->fromPixel(value)) {ui->height_DoubleSpinBox->setValue(qApp->fromPixel(value));}; } + ui->height_DoubleSpinBox->blockSignals(false); } //--------------------------------------------------------------------------------------------------------------------- qreal ImageDialog::getXScale() const { - return m_image.xScale; + return m_xScale; } //--------------------------------------------------------------------------------------------------------------------- void ImageDialog::setXScale(const qreal &scale) { ui->xScale_DoubleSpinBox->blockSignals(true); - m_image.xScale = scale; - ui->xScale_DoubleSpinBox->setValue(scale); + m_xScale = scale; + if (ui->xScale_DoubleSpinBox->value() != scale) {ui->xScale_DoubleSpinBox->setValue(scale);}; ui->xScale_DoubleSpinBox->blockSignals(false); } //--------------------------------------------------------------------------------------------------------------------- qreal ImageDialog::getYScale() const { - return m_image.yScale; + return m_yScale; } //--------------------------------------------------------------------------------------------------------------------- void ImageDialog::setYScale(const qreal &scale) { ui->yScale_DoubleSpinBox->blockSignals(true); - m_image.yScale = scale; - ui->yScale_DoubleSpinBox->setValue(scale); + m_yScale = scale; + if (ui->yScale_DoubleSpinBox->value() != scale) {ui->yScale_DoubleSpinBox->setValue(scale);}; ui->yScale_DoubleSpinBox->blockSignals(false); } @@ -503,66 +487,120 @@ void ImageDialog::yPosChanged(qreal value) } //--------------------------------------------------------------------------------------------------------------------- -void ImageDialog::widthChanged(qreal value) +void ImageDialog::widthChanged(qreal width) { blockSignals(true); qreal oldWidth = m_image.width; - if (m_image.units == Unit::Px) - { - m_image.width = value; - } - else + qreal height = m_image.height; + + if (m_image.units != Unit::Px) { - m_image.width = qApp->toPixel(value); + width = qApp->toPixel(width); } - setXScale(m_image.width / m_pixmapWidth * 100); + + width = width < m_minDimension ? m_minDimension : width; + width = width > m_maxDimension ? m_maxDimension : width; + if (m_image.aspectLocked) { - qreal newHeight = m_image.height * m_image.width / oldWidth; - setHeight(newHeight); - setYScale(newHeight / m_pixmapHeight * 100); + height = m_image.height * width / oldWidth; + + if (height > m_maxDimension) + { + width = m_maxDimension * oldWidth / m_image.height; + height = m_maxDimension; + } + else if (height < m_minDimension) + { + width = m_minDimension * oldWidth / m_image.height; + height = m_minDimension; + } } + + setWidth(width); + setXScale(m_image.width / m_pixmapWidth * 100); + setHeight(height); + setYScale(m_image.height / m_pixmapHeight * 100); + blockSignals(false); emit imageUpdated(m_image); } //--------------------------------------------------------------------------------------------------------------------- -void ImageDialog::heightChanged(qreal value) +void ImageDialog::heightChanged(qreal height) { blockSignals(true); qreal oldHeight = m_image.height; - if (m_image.units == Unit::Px) - { - m_image.height = value; - } - else + qreal width = m_image.width; + + if (m_image.units != Unit::Px) { - m_image.height = qApp->toPixel(value); + height = qApp->toPixel(height); } - setYScale(m_image.height / m_pixmapHeight * 100); + + height = height < m_minDimension ? m_minDimension : height; + height = height > m_maxDimension ? m_maxDimension : height; + if (m_image.aspectLocked) { - qreal newWidth = m_image.width * m_image.height / oldHeight; - setWidth(newWidth); - setXScale(newWidth / m_pixmapWidth * 100); + width = m_image.width * height / oldHeight; + + if (width > m_maxDimension) + { + height = m_maxDimension * oldHeight / m_image.width; + width = m_maxDimension; + } + else if (width < m_minDimension) + { + height = m_minDimension * oldHeight / m_image.width; + width = m_minDimension; + } } + + setWidth(width); + setXScale(m_image.width / m_pixmapWidth * 100); + setHeight(height); + setYScale(m_image.height / m_pixmapHeight * 100); + blockSignals(false); emit imageUpdated(m_image); } //--------------------------------------------------------------------------------------------------------------------- -void ImageDialog::xScaleChanged(qreal scale) +void ImageDialog::xScaleChanged(qreal xScale) { blockSignals(true); - qreal oldXScale = m_image.xScale; - m_image.xScale = scale; - setWidth(m_pixmapWidth * m_image.xScale / 100); + qreal oldYScale = m_xScale; + qreal yScale = m_yScale; + + qreal maxXScale = m_maxDimension / m_pixmapWidth * 100; + qreal minXScale = m_minDimension / m_pixmapWidth * 100; + qreal maxYScale = m_maxDimension / m_pixmapHeight * 100; + qreal minYScale = m_minDimension / m_pixmapHeight * 100; + + xScale = xScale < minXScale ? minXScale : xScale; + xScale = xScale > maxXScale ? maxXScale : xScale; + if (m_image.aspectLocked) { - qreal newYScale = m_image.yScale * m_image.xScale / oldXScale; - setYScale(newYScale); - setHeight(m_pixmapHeight * newYScale / 100); + yScale = m_yScale * xScale / oldYScale; + if (yScale > maxYScale) + { + xScale = maxYScale * oldYScale / m_yScale; + yScale = maxYScale; + } + else if (yScale < minYScale) + { + xScale = minYScale * oldYScale / m_yScale; + yScale = minYScale; + } } + + setXScale(xScale); + setWidth(m_pixmapWidth * m_xScale / 100); + setYScale(yScale); + setHeight(m_pixmapHeight * m_yScale / 100); + blockSignals(false); emit imageUpdated(m_image); } @@ -571,15 +609,37 @@ void ImageDialog::xScaleChanged(qreal scale) void ImageDialog::yScaleChanged(qreal scale) { blockSignals(true); - qreal oldYScale = m_image.yScale; - m_image.yScale = scale; - setHeight(m_pixmapHeight * m_image.yScale / 100); + qreal oldXScale = m_yScale; + qreal xScale = m_xScale; + + qreal maxXScale = m_maxDimension / m_pixmapWidth * 100; + qreal minXScale = m_minDimension / m_pixmapWidth * 100; + qreal maxYScale = m_maxDimension / m_pixmapHeight * 100; + qreal minYScale = m_minDimension / m_pixmapHeight * 100; + + scale = scale < minYScale ? minYScale : scale; + scale = scale > maxYScale ? maxYScale : scale; + if (m_image.aspectLocked) { - qreal newXScale = m_image.xScale * m_image.yScale / oldYScale; - setXScale(newXScale); - setWidth(m_pixmapWidth * newXScale / 100); + xScale = m_xScale * scale / oldXScale; + if (xScale > maxXScale) + { + scale = maxXScale * oldXScale / m_xScale; + xScale = maxXScale; + } + else if (xScale < minXScale) + { + scale = minXScale * oldXScale / m_xScale; + xScale = minXScale; + } } + + setYScale(scale); + setHeight(m_pixmapHeight * m_yScale / 100); + setXScale(xScale); + setWidth(m_pixmapWidth * m_xScale / 100); + blockSignals(false); emit imageUpdated(m_image); } @@ -613,11 +673,30 @@ void ImageDialog::updateSpinboxesRanges() { ui->width_DoubleSpinBox->setMinimum(m_minDimension); ui->height_DoubleSpinBox->setMinimum(m_minDimension); + + ui->xScale_DoubleSpinBox->setMinimum(m_minDimension / m_pixmapWidth * 100); + ui->yScale_DoubleSpinBox->setMinimum(m_minDimension / m_pixmapHeight * 100); + + ui->width_DoubleSpinBox->setMaximum(m_maxDimension); + ui->height_DoubleSpinBox->setMaximum(m_maxDimension); + + ui->xScale_DoubleSpinBox->setMaximum(m_maxDimension / m_pixmapWidth * 100); + ui->yScale_DoubleSpinBox->setMaximum(m_maxDimension / m_pixmapHeight * 100); + } else { ui->width_DoubleSpinBox->setMinimum(qApp->fromPixel(m_minDimension)); ui->height_DoubleSpinBox->setMinimum(qApp->fromPixel(m_minDimension)); + + ui->xScale_DoubleSpinBox->setMinimum(qApp->fromPixel(m_minDimension / m_pixmapWidth * 100)); + ui->yScale_DoubleSpinBox->setMinimum(qApp->fromPixel(m_minDimension / m_pixmapHeight * 100)); + + ui->width_DoubleSpinBox->setMaximum(qApp->fromPixel(m_maxDimension)); + ui->height_DoubleSpinBox->setMaximum(qApp->fromPixel(m_maxDimension)); + + ui->xScale_DoubleSpinBox->setMaximum(qApp->fromPixel(m_maxDimension / m_pixmapWidth * 100)); + ui->yScale_DoubleSpinBox->setMaximum(qApp->fromPixel(m_maxDimension / m_pixmapHeight * 100)); } } @@ -633,7 +712,6 @@ void ImageDialog::unitsChanged() if (m_image.units != Unit::Px) { m_image.units = Unit::Px; - updateSpinboxesRanges(); updateUnits(); ui->width_DoubleSpinBox->setValue(tempWidth); @@ -644,7 +722,6 @@ void ImageDialog::unitsChanged() else { m_image.units = qApp->patternUnit(); - updateSpinboxesRanges(); updateUnits(); ui->width_DoubleSpinBox->setValue(qApp->fromPixel(tempWidth)); diff --git a/src/libs/tools/image_dialog.h b/src/libs/tools/image_dialog.h index aec10d33001e..bd01a5ed1a73 100644 --- a/src/libs/tools/image_dialog.h +++ b/src/libs/tools/image_dialog.h @@ -50,7 +50,7 @@ class ImageDialog : public QDialog Q_OBJECT public: - explicit ImageDialog(DraftImage image); + explicit ImageDialog(DraftImage image, qreal minDimension, qreal maxDimension); virtual ~ImageDialog(); DraftImage getImage() const; @@ -104,8 +104,8 @@ private slots: void alignmentChanged(); void xPosChanged(qreal value); void yPosChanged(qreal value); - void widthChanged(qreal value); - void heightChanged(qreal value); + void widthChanged(qreal width); + void heightChanged(qreal height); void xScaleChanged(qreal value); void yScaleChanged(qreal value); void lockAspectChanged(bool checked); @@ -135,10 +135,11 @@ private slots: DraftImage m_image; qreal m_pixmapWidth; qreal m_pixmapHeight; - qreal m_minScale; - qreal m_maxScale; + qreal m_maxDimension; qreal m_minDimension; qreal m_minOpacity; + qreal m_xScale; + qreal m_yScale; bool m_flagName; }; diff --git a/src/libs/tools/image_dialog.ui b/src/libs/tools/image_dialog.ui index 7bc22bca717d..2471d0dd701d 100644 --- a/src/libs/tools/image_dialog.ui +++ b/src/libs/tools/image_dialog.ui @@ -605,10 +605,10 @@ QToolButton:!checked { 0 - 16.000000000000000 + 0.000000000000000 - 16000.000000000000000 + 99999.000000000000000 @@ -674,10 +674,10 @@ QToolButton:!checked { 0 - 16.000000000000000 + 0.000000000000000 - 16000.000000000000000 + 99999.000000000000000 @@ -743,7 +743,7 @@ QToolButton:!checked { 2 - 100.000000000000000 + 99999.000000000000000 @@ -806,7 +806,10 @@ QToolButton:!checked { % - 0 + 2 + + + 99999.000000000000000 diff --git a/src/libs/tools/image_item.cpp b/src/libs/tools/image_item.cpp index 5072bfd33cfc..ee9da89b001f 100644 --- a/src/libs/tools/image_item.cpp +++ b/src/libs/tools/image_item.cpp @@ -31,7 +31,6 @@ #include "vmaingraphicsscene.h" #include "vmaingraphicsview.h" #include "global.h" -#include "../vmisc/vabstractapplication.h" #include "../vmisc/vcommonsettings.h" #include "../vwidgets/resize_handle.h" @@ -51,15 +50,14 @@ #include #include -QList ImageItem::allImageItems; - //--------------------------------------------------------------------------------------------------------------------- /** * @brief ImageItem default constructor. * @param parent parent object. */ -ImageItem::ImageItem(DraftImage image, QGraphicsItem *parent) +ImageItem::ImageItem(VAbstractPattern *doc, DraftImage image, QGraphicsItem *parent) : QGraphicsItem(parent) + , m_doc(doc) , m_offset(QPointF(0.0, 0.0)) , m_boundingRect(QRectF()) , m_handleRect(QRectF()) @@ -77,18 +75,18 @@ ImageItem::ImageItem(DraftImage image, QGraphicsItem *parent) , m_pixmapWidth() , m_pixmapHeight() , m_selectable(true) + , m_minDimension(16) + , m_maxDimension(60000) { initializeItem(); m_boundingRect = QRectF(m_image.xPos, m_image.yPos, m_image.xPos + m_image.width, m_image.yPos + m_image.height); m_handleRect = m_boundingRect.adjusted(HANDLE_SIZE/2, HANDLE_SIZE/2, -HANDLE_SIZE/2, -HANDLE_SIZE/2); - allImageItems.append(this); - if (m_image.order == 0) { qreal minZValue = maxImageZvalue+1; - foreach (ImageItem *item, allImageItems) + foreach (ImageItem *item, m_doc->getBackgroundImageMap().values()) { minZValue = qMin(minZValue, item->m_image.order); } @@ -98,10 +96,11 @@ ImageItem::ImageItem(DraftImage image, QGraphicsItem *parent) updateImage(); - m_resizeHandles = new ResizeHandlesItem(this); + m_resizeHandles = new ResizeHandlesItem(this, m_minDimension, m_maxDimension); m_resizeHandles->setLockAspectRatio(m_image.aspectLocked); m_resizeHandles->setParentRotation(m_image.rotation); - m_resizeHandles->hide(); + m_resizeHandles->parentIsLocked(m_image.locked); + m_resizeHandles->setVisible(m_image.locked); connect(m_resizeHandles, &ResizeHandlesItem::sizeChanged, this, &ImageItem::updateFromHandles); connect(m_resizeHandles, &ResizeHandlesItem::setStatusMessage, this, [this](QString message) {emit setStatusMessage(message);}); } @@ -124,8 +123,6 @@ void ImageItem::setPixmap(const QPixmap &pixmap) m_pixmapHeight = pixmap.height(); m_image.width = pixmap.width(); m_image.height = pixmap.height(); - m_image.xScale = m_image.width / m_pixmapWidth * 100; - m_image.yScale = m_image.height / m_pixmapHeight * 100; m_boundingRect = QRectF(m_image.xPos, m_image.yPos, m_image.xPos + m_pixmapWidth, m_image.yPos + m_pixmapHeight); @@ -170,7 +167,6 @@ void ImageItem::setLock(bool checked) { setAcceptedMouseButtons(Qt::RightButton); emit setStatusMessage(""); - if (m_resizeHandles != nullptr) {m_resizeHandles->hide();} } else { @@ -178,7 +174,6 @@ void ImageItem::setLock(bool checked) } setFlag(QGraphicsItem::ItemIsMovable, !m_image.locked); //enableSelection(!m_image.locked); - emit imageUpdated(m_image); } @@ -346,7 +341,7 @@ void ImageItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) if (selectedAction == actionProperties) { - ImageDialog *dialog = new ImageDialog(m_image); + ImageDialog *dialog = new ImageDialog(m_image, m_minDimension, m_maxDimension); connect(dialog, &ImageDialog::applyClicked, this, &ImageItem::updateImageAndHandles); if (dialog->exec() == QDialog::Accepted) @@ -356,7 +351,8 @@ void ImageItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) } else if (selectedAction == actionLock) { - setLock(!m_image.locked); + m_image.locked = !m_image.locked; + updateImageAndHandles(m_image); } // else if (selectedAction == actionShow) // { @@ -517,13 +513,13 @@ void ImageItem::updateImageAndHandles(DraftImage image) m_resizeHandles->setParentRect(m_boundingRect); m_resizeHandles->setLockAspectRatio(m_image.aspectLocked); m_resizeHandles->setParentRotation(m_image.rotation); - emit imageUpdated(m_image); + m_resizeHandles->parentIsLocked(m_image.locked); + m_resizeHandles->setVisible(m_image.locked); } void ImageItem::deleteImageItem() { moveToBottom(); //so that there is no gap in zValue - allImageItems.removeOne(this); scene()->removeItem(this); deleteLater(); } @@ -531,7 +527,7 @@ void ImageItem::deleteImageItem() void ImageItem::moveToBottom() { qreal minZValue = m_image.order; - foreach (ImageItem *item, allImageItems) + foreach (ImageItem *item, m_doc->getBackgroundImageMap().values()) { if (item != this && item->m_image.order < m_image.order) { @@ -547,7 +543,7 @@ void ImageItem::moveToBottom() void ImageItem::moveToTop() { - foreach (ImageItem *item, allImageItems) + foreach (ImageItem *item, m_doc->getBackgroundImageMap().values()) { if (item != this && item->m_image.order > m_image.order) { @@ -566,7 +562,7 @@ void ImageItem::moveUp() return; } - foreach (ImageItem *item, allImageItems) + foreach (ImageItem *item, m_doc->getBackgroundImageMap().values()) { if (item->m_image.order == m_image.order + 1) { @@ -581,12 +577,12 @@ void ImageItem::moveUp() void ImageItem::moveDown() { - if (m_image.order == maxImageZvalue-allImageItems.size()+1) + if (m_image.order == maxImageZvalue-m_doc->getBackgroundImageMap().values().size()+1) { return; } - foreach (ImageItem *item, allImageItems) + foreach (ImageItem *item, m_doc->getBackgroundImageMap().values()) { if (item->m_image.order == m_image.order - 1) { diff --git a/src/libs/tools/image_item.h b/src/libs/tools/image_item.h index df97b077a2f9..3792133615ca 100644 --- a/src/libs/tools/image_item.h +++ b/src/libs/tools/image_item.h @@ -32,6 +32,7 @@ #include "../vmisc/def.h" #include "../vwidgets/resize_handle.h" +#include "../vmisc/vabstractapplication.h" #include #include @@ -52,14 +53,12 @@ class ImageItem : public QObject, public QGraphicsItem Q_INTERFACES(QGraphicsItem) public: - explicit ImageItem(DraftImage image, QGraphicsItem *parent = nullptr); + explicit ImageItem(VAbstractPattern *doc, DraftImage image, QGraphicsItem *parent = nullptr); virtual ~ImageItem() = default; virtual int type() const override {return Type;} enum {Type = UserType + static_cast(Tool::BackgroundImage)}; - static QList allImageItems; - virtual QRectF boundingRect() const override; virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, @@ -103,6 +102,7 @@ class ImageItem : public QObject, public QGraphicsItem virtual void keyReleaseEvent (QKeyEvent *event) override; private: + VAbstractPattern *m_doc; QPointF m_offset; QRectF m_boundingRect; QRectF m_handleRect; @@ -120,6 +120,8 @@ class ImageItem : public QObject, public QGraphicsItem qreal m_pixmapWidth; qreal m_pixmapHeight; bool m_selectable; + qreal m_minDimension; + qreal m_maxDimension; void initializeItem(); void updateFromHandles(QRectF rect); diff --git a/src/libs/vmisc/def.h b/src/libs/vmisc/def.h index eb3fe5b8312c..2cdfec296016 100644 --- a/src/libs/vmisc/def.h +++ b/src/libs/vmisc/def.h @@ -171,8 +171,6 @@ struct DraftImage yPos(0.0), width(0.0), height(0.0), - xScale(100.0), - yScale(100.0), aspectLocked(false), units(Unit::Px), rotation(0.0), diff --git a/src/libs/vwidgets/resize_handle.cpp b/src/libs/vwidgets/resize_handle.cpp index 219672e06f65..e017612501ec 100644 --- a/src/libs/vwidgets/resize_handle.cpp +++ b/src/libs/vwidgets/resize_handle.cpp @@ -40,10 +40,12 @@ /** * ResizeHandlesItem Constructor. */ -ResizeHandlesItem::ResizeHandlesItem(QGraphicsItem *parent) +ResizeHandlesItem::ResizeHandlesItem(QGraphicsItem *parent, qreal minDimension, qreal maxDimension) : QGraphicsItem(parent) , m_handleItems() , m_parentRect() + , m_minDimension(minDimension) + , m_maxDimension(maxDimension) { if (parentItem()) { @@ -87,12 +89,35 @@ void ResizeHandlesItem::setLockAspectRatio(bool lock) m_lockAspectRatio = lock; } +//------------------------------------------------------------------------------ +void ResizeHandlesItem::parentIsLocked(bool lock) +{ + m_parentIsLocked = lock; + qreal lock_size = qMax(m_parentRect.width(), m_parentRect.height()) / 30; + + foreach (HandleItem *handleItem, m_handleItems) + { + handleItem->setFlag(QGraphicsItem::ItemIgnoresTransformations, !lock); + handleItem->setAcceptedMouseButtons(lock ? Qt::NoButton : Qt::AllButtons); + handleItem->setRect(lock ? QRect(-lock_size / 2, -lock_size / 2, lock_size, lock_size) : QRect(-HANDLE_SIZE/2, -HANDLE_SIZE/2, HANDLE_SIZE, HANDLE_SIZE)); + } +} + + //------------------------------------------------------------------------------ void ResizeHandlesItem::setParentRotation(qreal rotation) { m_parentRotation = rotation; } + +//------------------------------------------------------------------------------ +void ResizeHandlesItem::setLimitDimensions(qreal min, qreal max) +{ + m_minDimension = min; + m_maxDimension = max; +} + /** * @brief paint handle item painting. * @param QPainter painter. @@ -180,12 +205,14 @@ ResizeHandlesItem::HandleItem::HandleItem(Position position, ResizeHandlesItem* , m_parent(parent) , m_handlePosition(position) , m_isHovered(false) + , m_minDimension(parent->m_minDimension) + , m_maxDimension(parent->m_maxDimension) { this->setBrush(QBrush(Qt::lightGray)); this->setFlag(QGraphicsItem::ItemIsMovable, true); this->setFlag(QGraphicsItem::ItemSendsGeometryChanges, true); this->setFlag(QGraphicsItem::ItemIsFocusable, true); // For keyboard input focus - this->setFlag(QGraphicsItem::ItemIgnoresTransformations, true); + this->setFlag(QGraphicsItem::ItemIgnoresTransformations, !m_parent->m_parentIsLocked); setAcceptHoverEvents(true); } @@ -210,9 +237,17 @@ void ResizeHandlesItem::HandleItem::paint(QPainter *painter, const QStyleOptionG Q_UNUSED(option); Q_UNUSED(widget); - painter->setPen(QPen(Qt::white, 1, Qt::SolidLine)); - painter->setBrush(m_isHovered ? QColor(Qt::red) : QColor(Qt::lightGray)); - painter->drawEllipse(boundingRect()); + if (!m_parent->m_parentIsLocked) + { + painter->setPen(QPen(Qt::white, 1, Qt::SolidLine)); + painter->setBrush(m_isHovered ? QColor(Qt::red) : QColor(Qt::darkGray)); + painter->drawEllipse(boundingRect()); + } + else + { + QPixmap pixmap("://icon/32x32/lock_on.png"); + painter->drawPixmap(boundingRect().toRect(), pixmap); + } } /** @@ -462,34 +497,110 @@ QPointF ResizeHandlesItem::HandleItem::limitPosition(const QPointF& newPos) point.setX(newPos.x()); } - if ((m_handlePosition == Position::Top || - m_handlePosition == Position::TopLeft || - m_handlePosition == Position::TopRight) && - point.y() > m_parent->m_parentRect.bottom() - HANDLE_SIZE * 2) + bool isMovingTop = m_handlePosition == Position::Top || + m_handlePosition == Position::TopLeft || + m_handlePosition == Position::TopRight; + bool isMovingBottom = m_handlePosition == Position::Bottom || + m_handlePosition == Position::BottomLeft || + m_handlePosition == Position::BottomRight; + bool isMovingLeft = m_handlePosition == Position::Left || + m_handlePosition == Position::TopLeft || + m_handlePosition == Position::BottomLeft; + bool isMovingRight = m_handlePosition == Position::Right || + m_handlePosition == Position::TopRight || + m_handlePosition == Position::BottomRight; + + qreal height; + qreal width; + + if (isMovingTop) { - point.setY(m_parent->m_parentRect.bottom() - HANDLE_SIZE * 2); + height = m_parent->m_parentRect.bottom() - point.y(); + + if (height < m_minDimension) + { + point.setY(m_parent->m_parentRect.bottom() - m_minDimension); + } + else if (height > m_maxDimension) + { + point.setY(m_parent->m_parentRect.bottom() - m_maxDimension); + } + + if (m_parent->m_lockAspectRatio && m_scalingFactor < 1 && height * m_scalingFactor < m_minDimension) + { + point.setY(m_parent->m_parentRect.bottom() - m_minDimension / m_scalingFactor); + } + else if (m_parent->m_lockAspectRatio && m_scalingFactor > 1 && height * m_scalingFactor > m_maxDimension) + { + point.setY(m_parent->m_parentRect.bottom() - m_maxDimension / m_scalingFactor); + } } - else if ((m_handlePosition == Position::Bottom || - m_handlePosition == Position::BottomLeft || - m_handlePosition == Position::BottomRight) && - point.y() < m_parent->m_parentRect.top() + HANDLE_SIZE * 2) + else if (isMovingBottom) { - point.setY(m_parent->m_parentRect.top() + HANDLE_SIZE * 2); + height = point.y() - m_parent->m_parentRect.top(); + + if (height < m_minDimension) + { + point.setY(m_parent->m_parentRect.top() + m_minDimension); + } + else if (height > m_maxDimension) + { + point.setY(m_parent->m_parentRect.top() + m_maxDimension); + } + + if (m_parent->m_lockAspectRatio && m_scalingFactor < 1 && height * m_scalingFactor < m_minDimension) + { + point.setY(m_parent->m_parentRect.top() + m_minDimension / m_scalingFactor); + } + else if (m_parent->m_lockAspectRatio && m_scalingFactor > 1 && height * m_scalingFactor > m_maxDimension) + { + point.setY(m_parent->m_parentRect.top() + m_maxDimension / m_scalingFactor); + } } - if ((m_handlePosition == Position::Left || - m_handlePosition == Position::TopLeft || - m_handlePosition == Position::BottomLeft) && - point.x() > m_parent->m_parentRect.right() - HANDLE_SIZE * 2) + if (isMovingLeft) { - point.setX(m_parent->m_parentRect.right() - HANDLE_SIZE * 2); + width = m_parent->m_parentRect.right() - point.x(); + + if (width < m_minDimension) + { + point.setX(m_parent->m_parentRect.right() - m_minDimension); + } + else if (width > m_maxDimension) + { + point.setX(m_parent->m_parentRect.right() - m_maxDimension); + } + + if (m_parent->m_lockAspectRatio && m_scalingFactor > 1 && width / m_scalingFactor < m_minDimension) + { + point.setX(m_parent->m_parentRect.right() - m_minDimension * m_scalingFactor); + } + else if (m_parent->m_lockAspectRatio && m_scalingFactor < 1 && width / m_scalingFactor > m_maxDimension) + { + point.setX(m_parent->m_parentRect.right() - m_maxDimension * m_scalingFactor); + } } - else if ((m_handlePosition == Position::Right || - m_handlePosition == Position::TopRight || - m_handlePosition == Position::BottomRight) && - point.x() < m_parent->m_parentRect.left() + HANDLE_SIZE * 2) + else if (isMovingRight) { - point.setX(m_parent->m_parentRect.left() + HANDLE_SIZE * 2); + width = point.x() - m_parent->m_parentRect.left(); + + if (width < m_minDimension) + { + point.setX(m_parent->m_parentRect.left() + m_minDimension); + } + else if (width > m_maxDimension) + { + point.setX(m_parent->m_parentRect.left() + m_maxDimension); + } + + if (m_parent->m_lockAspectRatio && m_scalingFactor > 1 && width / m_scalingFactor < m_minDimension) + { + point.setX(m_parent->m_parentRect.left() + m_minDimension * m_scalingFactor); + } + else if (m_parent->m_lockAspectRatio && m_scalingFactor < 1 && width / m_scalingFactor > m_maxDimension) + { + point.setX(m_parent->m_parentRect.left() + m_maxDimension * m_scalingFactor); + } } return point; @@ -504,26 +615,29 @@ void ResizeHandlesItem::HandleItem::hoverEnterEvent(QGraphicsSceneHoverEvent *ev { m_isHovered = true; - QPixmap pixmap(cursorResizeArrow); - QTransform transform; - transform.rotate(m_parent->m_parentRotation + (static_cast(m_handlePosition) - 1) * 45); - pixmap = pixmap.transformed(transform); - QPointF offset = pixmap.rect().center(); - setCursor(QCursor(pixmap, offset.x(), offset.y())); - - QString message; - if(m_parent->m_lockAspectRatio) + if (!m_parent->m_parentIsLocked) { - message = tr("Press CTRL to scale around the center - Aspect ratio locked "); - } - else - { - message = tr("Press CTRL to scale around the center," - " SHIFT to scale uniformly."); + QPixmap pixmap(cursorResizeArrow); + QTransform transform; + transform.rotate(m_parent->m_parentRotation + (static_cast(m_handlePosition) - 1) * 45); + pixmap = pixmap.transformed(transform); + QPointF offset = pixmap.rect().center(); + setCursor(QCursor(pixmap, offset.x(), offset.y())); + + QString message; + if(m_parent->m_lockAspectRatio) + { + message = tr("Press CTRL to scale around the center - Aspect ratio locked "); + } + else + { + message = tr("Press CTRL to scale around the center," + " SHIFT to scale uniformly."); + } + + emit m_parent->setStatusMessage(message); } - - emit m_parent->setStatusMessage(message); - + QGraphicsItem::hoverEnterEvent(event); } @@ -535,10 +649,13 @@ void ResizeHandlesItem::HandleItem::hoverEnterEvent(QGraphicsSceneHoverEvent *ev void ResizeHandlesItem::HandleItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) { m_isHovered = false; - - setCursor(QCursor(Qt::ArrowCursor)); - emit m_parent->setStatusMessage(""); - + + if (!m_parent->m_parentIsLocked) + { + setCursor(QCursor(Qt::ArrowCursor)); + emit m_parent->setStatusMessage(""); + } + QGraphicsItem::hoverLeaveEvent(event); } @@ -550,6 +667,11 @@ void ResizeHandlesItem::HandleItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *ev //--------------------------------------------------------------------------------------------------------------------- void ResizeHandlesItem::HandleItem::mousePressEvent(QGraphicsSceneMouseEvent *event) { + if (m_parent->m_parentIsLocked) + { + return; + } + m_scalingFactor = m_parent->m_parentRect.width() / m_parent->m_parentRect.height(); QGraphicsItem::mousePressEvent(event); @@ -563,6 +685,11 @@ void ResizeHandlesItem::HandleItem::mousePressEvent(QGraphicsSceneMouseEvent *ev //--------------------------------------------------------------------------------------------------------------------- void ResizeHandlesItem::HandleItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { + if (m_parent->m_parentIsLocked) + { + return; + } + QGraphicsItem::mouseMoveEvent(event); } @@ -575,6 +702,11 @@ void ResizeHandlesItem::HandleItem::mouseMoveEvent(QGraphicsSceneMouseEvent *eve //--------------------------------------------------------------------------------------------------------------------- void ResizeHandlesItem::HandleItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { + if (m_parent->m_parentIsLocked) + { + return; + } + if (event->button() == Qt::LeftButton && event->type() != QEvent::GraphicsSceneMouseDoubleClick) { } @@ -591,5 +723,10 @@ void ResizeHandlesItem::HandleItem::mouseReleaseEvent(QGraphicsSceneMouseEvent * //--------------------------------------------------------------------------------------------------------------------- void ResizeHandlesItem::HandleItem::keyReleaseEvent(QKeyEvent *event) { + if (m_parent->m_parentIsLocked) + { + return; + } + QGraphicsItem::keyReleaseEvent(event); } diff --git a/src/libs/vwidgets/resize_handle.h b/src/libs/vwidgets/resize_handle.h index 21ac533975f6..be3c90671982 100644 --- a/src/libs/vwidgets/resize_handle.h +++ b/src/libs/vwidgets/resize_handle.h @@ -69,10 +69,12 @@ class ResizeHandlesItem : public QObject, public QGraphicsItem Position m_handlePosition; bool m_isHovered; qreal m_scalingFactor; + qreal m_minDimension; + qreal m_maxDimension; }; public: - explicit ResizeHandlesItem(QGraphicsItem *parent = nullptr); + explicit ResizeHandlesItem(QGraphicsItem *parent = nullptr, qreal minDimension = 16, qreal maxDimension = 16000); virtual ~ResizeHandlesItem() = default; virtual int type() const Q_DECL_OVERRIDE {return Type;} @@ -90,7 +92,10 @@ class ResizeHandlesItem : public QObject, public QGraphicsItem void setLeft(qreal x); void setParentRect(const QRectF &rect); void setLockAspectRatio(bool lock); + void parentIsLocked(bool lock); void setParentRotation(qreal rotation); + void setLimitDimensions(qreal min, qreal max); + signals: void sizeChanged(QRectF rect); @@ -102,7 +107,10 @@ class ResizeHandlesItem : public QObject, public QGraphicsItem QList m_handleItems; QRectF m_parentRect; bool m_lockAspectRatio; + bool m_parentIsLocked; qreal m_parentRotation; + qreal m_minDimension; + qreal m_maxDimension; }; #endif //RESIZE_HANDLE_H