From b481b3afcc621cb2b062d171448a21a9ecb0e449 Mon Sep 17 00:00:00 2001 From: Jakub Melka Date: Sun, 7 Apr 2024 18:10:53 +0200 Subject: [PATCH] Editing images --- ...pdfpagecontenteditorediteditemsettings.cpp | 105 ++++++++++++++++-- .../pdfpagecontenteditorediteditemsettings.h | 5 + .../pdfpagecontenteditorediteditemsettings.ui | 77 ++++++++++++- .../pdfpagecontenteditorstylesettings.cpp | 2 +- .../sources/pdfpagecontentelements.cpp | 5 +- .../sources/pdfpagecontentelements.h | 1 + 6 files changed, 183 insertions(+), 12 deletions(-) diff --git a/Pdf4QtLibWidgets/sources/pdfpagecontenteditorediteditemsettings.cpp b/Pdf4QtLibWidgets/sources/pdfpagecontenteditorediteditemsettings.cpp index 7ea370e..0935e84 100644 --- a/Pdf4QtLibWidgets/sources/pdfpagecontenteditorediteditemsettings.cpp +++ b/Pdf4QtLibWidgets/sources/pdfpagecontenteditorediteditemsettings.cpp @@ -22,6 +22,11 @@ #include "pdfpagecontenteditorprocessor.h" #include "pdfwidgetutils.h" +#include +#include +#include +#include + namespace pdf { @@ -30,6 +35,7 @@ PDFPageContentEditorEditedItemSettings::PDFPageContentEditorEditedItemSettings(Q ui(new Ui::PDFPageContentEditorEditedItemSettings) { ui->setupUi(this); + connect(ui->loadImageButton, &QPushButton::clicked, this, &PDFPageContentEditorEditedItemSettings::selectImage); } PDFPageContentEditorEditedItemSettings::~PDFPageContentEditorEditedItemSettings() @@ -48,15 +54,98 @@ void PDFPageContentEditorEditedItemSettings::loadFromElement(PDFPageContentEleme if (const PDFEditedPageContentElementImage* imageElement = contentElement->asImage()) { - QSize imageSize = QSize(200, 200); - - QImage image = imageElement->getImage(); - image.setDevicePixelRatio(this->devicePixelRatioF()); - image = image.scaled(imageSize * this->devicePixelRatioF(), Qt::KeepAspectRatio); - ui->tabWidget->addTab(ui->imageTab, tr("Image")); - ui->imageLabel->setPixmap(QPixmap::fromImage(image)); - ui->imageLabel->setFixedSize(PDFWidgetUtils::scaleDPI(this, imageSize)); + m_image = imageElement->getImage(); + setImage(imageElement->getImage()); + } +} + +void PDFPageContentEditorEditedItemSettings::saveToElement(PDFPageContentElementEdited* editedElement) +{ + if (PDFEditedPageContentElementImage* imageElement = editedElement->getElement()->asImage()) + { + imageElement->setImage(m_image); + imageElement->setImageObject(PDFObject()); + } +} + +static int PDF_gcd(int a, int b) +{ + if (b == 0) + { + return a; + } + + return PDF_gcd(b, a % b); +} + +void PDFPageContentEditorEditedItemSettings::setImage(QImage image) +{ + QSize imageSize = QSize(200, 200); + + int width = image.width(); + int height = image.height(); + + int n = width; + int d = height; + + int divisor = PDF_gcd(n, d); + if (divisor > 1) + { + n /= divisor; + d /= divisor; + } + + ui->imageWidthEdit->setText(QString("%1 px").arg(width)); + ui->imageHeightEdit->setText(QString("%1 px").arg(height)); + ui->imageRatioEdit->setText(QString("%1 : %2").arg(n).arg(d)); + + image.setDevicePixelRatio(this->devicePixelRatioF()); + image = image.scaled(imageSize * this->devicePixelRatioF(), Qt::KeepAspectRatio); + + ui->imageLabel->setPixmap(QPixmap::fromImage(image)); + ui->imageLabel->setFixedSize(PDFWidgetUtils::scaleDPI(this, imageSize)); +} + +void PDFPageContentEditorEditedItemSettings::selectImage() +{ + QString imageDirectory; + + QStringList pictureDirectiories = QStandardPaths::standardLocations(QStandardPaths::PicturesLocation); + if (!pictureDirectiories.isEmpty()) + { + imageDirectory = pictureDirectiories.last(); + } + else + { + imageDirectory = QDir::currentPath(); + } + + QList mimeTypes = QImageReader::supportedMimeTypes(); + QStringList mimeTypeFilters; + for (const QByteArray& mimeType : mimeTypes) + { + mimeTypeFilters.append(mimeType); + } + + QFileDialog dialog(this, tr("Select Image")); + dialog.setDirectory(imageDirectory); + dialog.setMimeTypeFilters(mimeTypeFilters); + dialog.selectMimeTypeFilter("image/svg+xml"); + dialog.setAcceptMode(QFileDialog::AcceptOpen); + dialog.setFileMode(QFileDialog::ExistingFile); + + if (dialog.exec() == QFileDialog::Accepted) + { + QString fileName = dialog.selectedFiles().constFirst(); + QImageReader reader(fileName); + QImage image = reader.read(); + + if (!image.isNull()) + { + setImage(image); + m_image = std::move(image); + } } } diff --git a/Pdf4QtLibWidgets/sources/pdfpagecontenteditorediteditemsettings.h b/Pdf4QtLibWidgets/sources/pdfpagecontenteditorediteditemsettings.h index 6923993..c978df8 100644 --- a/Pdf4QtLibWidgets/sources/pdfpagecontenteditorediteditemsettings.h +++ b/Pdf4QtLibWidgets/sources/pdfpagecontenteditorediteditemsettings.h @@ -37,9 +37,14 @@ public: virtual ~PDFPageContentEditorEditedItemSettings() override; void loadFromElement(PDFPageContentElementEdited* editedElement); + void saveToElement(PDFPageContentElementEdited* editedElement); private: + void setImage(QImage image); + void selectImage(); + Ui::PDFPageContentEditorEditedItemSettings* ui; + QImage m_image; }; } // namespace pdf diff --git a/Pdf4QtLibWidgets/sources/pdfpagecontenteditorediteditemsettings.ui b/Pdf4QtLibWidgets/sources/pdfpagecontenteditorediteditemsettings.ui index b3a263a..d393030 100644 --- a/Pdf4QtLibWidgets/sources/pdfpagecontenteditorediteditemsettings.ui +++ b/Pdf4QtLibWidgets/sources/pdfpagecontenteditorediteditemsettings.ui @@ -17,7 +17,7 @@ - 0 + 1 @@ -48,14 +48,87 @@ Image Content - + + + + + + + Height + + + + + + + Width + + + + + + + true + + + + + + + true + + + + + + + Ratio + + + + + + + true + + + + + + + true + + + + + + + Load Image + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + diff --git a/Pdf4QtLibWidgets/sources/pdfpagecontenteditorstylesettings.cpp b/Pdf4QtLibWidgets/sources/pdfpagecontenteditorstylesettings.cpp index 4db6b9d..3a8b595 100644 --- a/Pdf4QtLibWidgets/sources/pdfpagecontenteditorstylesettings.cpp +++ b/Pdf4QtLibWidgets/sources/pdfpagecontenteditorstylesettings.cpp @@ -282,7 +282,7 @@ bool PDFPageContentEditorStyleSettings::showEditElementStyleDialog(QWidget* pare if (dialog.exec() == QDialog::Accepted) { - + widget->saveToElement(editedElement); } } else diff --git a/Pdf4QtLibWidgets/sources/pdfpagecontentelements.cpp b/Pdf4QtLibWidgets/sources/pdfpagecontentelements.cpp index 1f069c2..5df2f0c 100644 --- a/Pdf4QtLibWidgets/sources/pdfpagecontentelements.cpp +++ b/Pdf4QtLibWidgets/sources/pdfpagecontentelements.cpp @@ -2579,8 +2579,11 @@ void PDFPageContentElementEdited::drawPage(QPainter* painter, painter->translate(rect.bottomLeft()); painter->scale(1.0, -1.0); + QRect transformedRect(0.0, 0.0, rect.width(), rect.height()); + QImage image = imageElement->getImage(); - painter->drawImage(rect, image); + painter->fillRect(transformedRect, Qt::white); + painter->drawImage(transformedRect, image); } if (const PDFEditedPageContentElementPath* pathElement = m_element->asPath()) diff --git a/Pdf4QtLibWidgets/sources/pdfpagecontentelements.h b/Pdf4QtLibWidgets/sources/pdfpagecontentelements.h index 6716afb..7d5a4a8 100644 --- a/Pdf4QtLibWidgets/sources/pdfpagecontentelements.h +++ b/Pdf4QtLibWidgets/sources/pdfpagecontentelements.h @@ -370,6 +370,7 @@ public: virtual QString getDescription() const override; const PDFEditedPageContentElement* getElement() const { return m_element.get(); } + PDFEditedPageContentElement* getElement() { return m_element.get(); } private: std::unique_ptr m_element;