mirror of
https://github.com/JakubMelka/PDF4QT.git
synced 2025-01-22 21:30:06 +01:00
Editing images
This commit is contained in:
parent
83adf1e49d
commit
b481b3afcc
@ -22,6 +22,11 @@
|
|||||||
#include "pdfpagecontenteditorprocessor.h"
|
#include "pdfpagecontenteditorprocessor.h"
|
||||||
#include "pdfwidgetutils.h"
|
#include "pdfwidgetutils.h"
|
||||||
|
|
||||||
|
#include <QDir>
|
||||||
|
#include <QFileDialog>
|
||||||
|
#include <QImageReader>
|
||||||
|
#include <QStandardPaths>
|
||||||
|
|
||||||
namespace pdf
|
namespace pdf
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -30,6 +35,7 @@ PDFPageContentEditorEditedItemSettings::PDFPageContentEditorEditedItemSettings(Q
|
|||||||
ui(new Ui::PDFPageContentEditorEditedItemSettings)
|
ui(new Ui::PDFPageContentEditorEditedItemSettings)
|
||||||
{
|
{
|
||||||
ui->setupUi(this);
|
ui->setupUi(this);
|
||||||
|
connect(ui->loadImageButton, &QPushButton::clicked, this, &PDFPageContentEditorEditedItemSettings::selectImage);
|
||||||
}
|
}
|
||||||
|
|
||||||
PDFPageContentEditorEditedItemSettings::~PDFPageContentEditorEditedItemSettings()
|
PDFPageContentEditorEditedItemSettings::~PDFPageContentEditorEditedItemSettings()
|
||||||
@ -48,15 +54,98 @@ void PDFPageContentEditorEditedItemSettings::loadFromElement(PDFPageContentEleme
|
|||||||
|
|
||||||
if (const PDFEditedPageContentElementImage* imageElement = contentElement->asImage())
|
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->tabWidget->addTab(ui->imageTab, tr("Image"));
|
||||||
ui->imageLabel->setPixmap(QPixmap::fromImage(image));
|
m_image = imageElement->getImage();
|
||||||
ui->imageLabel->setFixedSize(PDFWidgetUtils::scaleDPI(this, imageSize));
|
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<QByteArray> 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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -37,9 +37,14 @@ public:
|
|||||||
virtual ~PDFPageContentEditorEditedItemSettings() override;
|
virtual ~PDFPageContentEditorEditedItemSettings() override;
|
||||||
|
|
||||||
void loadFromElement(PDFPageContentElementEdited* editedElement);
|
void loadFromElement(PDFPageContentElementEdited* editedElement);
|
||||||
|
void saveToElement(PDFPageContentElementEdited* editedElement);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void setImage(QImage image);
|
||||||
|
void selectImage();
|
||||||
|
|
||||||
Ui::PDFPageContentEditorEditedItemSettings* ui;
|
Ui::PDFPageContentEditorEditedItemSettings* ui;
|
||||||
|
QImage m_image;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace pdf
|
} // namespace pdf
|
||||||
|
@ -17,7 +17,7 @@
|
|||||||
<item>
|
<item>
|
||||||
<widget class="QTabWidget" name="tabWidget">
|
<widget class="QTabWidget" name="tabWidget">
|
||||||
<property name="currentIndex">
|
<property name="currentIndex">
|
||||||
<number>0</number>
|
<number>1</number>
|
||||||
</property>
|
</property>
|
||||||
<widget class="QWidget" name="textTab">
|
<widget class="QWidget" name="textTab">
|
||||||
<attribute name="title">
|
<attribute name="title">
|
||||||
@ -48,14 +48,87 @@
|
|||||||
<property name="title">
|
<property name="title">
|
||||||
<string>Image Content</string>
|
<string>Image Content</string>
|
||||||
</property>
|
</property>
|
||||||
<layout class="QVBoxLayout" name="verticalLayout_5">
|
<layout class="QVBoxLayout" name="verticalLayout_5" stretch="0,1,0">
|
||||||
|
<item>
|
||||||
|
<layout class="QGridLayout" name="gridLayout">
|
||||||
|
<item row="1" column="0">
|
||||||
|
<widget class="QLabel" name="imageHeightLabel">
|
||||||
|
<property name="text">
|
||||||
|
<string>Height</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item row="0" column="0">
|
||||||
|
<widget class="QLabel" name="imageWidthLabel">
|
||||||
|
<property name="text">
|
||||||
|
<string>Width</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item row="1" column="2">
|
||||||
|
<widget class="QLineEdit" name="imageHeightEdit">
|
||||||
|
<property name="readOnly">
|
||||||
|
<bool>true</bool>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item row="0" column="2">
|
||||||
|
<widget class="QLineEdit" name="imageWidthEdit">
|
||||||
|
<property name="readOnly">
|
||||||
|
<bool>true</bool>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item row="2" column="0">
|
||||||
|
<widget class="QLabel" name="imageRatioLabel">
|
||||||
|
<property name="text">
|
||||||
|
<string>Ratio</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item row="2" column="2">
|
||||||
|
<widget class="QLineEdit" name="imageRatioEdit">
|
||||||
|
<property name="readOnly">
|
||||||
|
<bool>true</bool>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
</layout>
|
||||||
|
</item>
|
||||||
<item>
|
<item>
|
||||||
<widget class="QLabel" name="imageLabel">
|
<widget class="QLabel" name="imageLabel">
|
||||||
|
<property name="autoFillBackground">
|
||||||
|
<bool>true</bool>
|
||||||
|
</property>
|
||||||
<property name="text">
|
<property name="text">
|
||||||
<string/>
|
<string/>
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
|
<item>
|
||||||
|
<layout class="QHBoxLayout" name="horizontalLayout">
|
||||||
|
<item>
|
||||||
|
<widget class="QPushButton" name="loadImageButton">
|
||||||
|
<property name="text">
|
||||||
|
<string>Load Image</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<spacer name="horizontalSpacer">
|
||||||
|
<property name="orientation">
|
||||||
|
<enum>Qt::Horizontal</enum>
|
||||||
|
</property>
|
||||||
|
<property name="sizeHint" stdset="0">
|
||||||
|
<size>
|
||||||
|
<width>40</width>
|
||||||
|
<height>20</height>
|
||||||
|
</size>
|
||||||
|
</property>
|
||||||
|
</spacer>
|
||||||
|
</item>
|
||||||
|
</layout>
|
||||||
|
</item>
|
||||||
</layout>
|
</layout>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
|
@ -282,7 +282,7 @@ bool PDFPageContentEditorStyleSettings::showEditElementStyleDialog(QWidget* pare
|
|||||||
|
|
||||||
if (dialog.exec() == QDialog::Accepted)
|
if (dialog.exec() == QDialog::Accepted)
|
||||||
{
|
{
|
||||||
|
widget->saveToElement(editedElement);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -2579,8 +2579,11 @@ void PDFPageContentElementEdited::drawPage(QPainter* painter,
|
|||||||
painter->translate(rect.bottomLeft());
|
painter->translate(rect.bottomLeft());
|
||||||
painter->scale(1.0, -1.0);
|
painter->scale(1.0, -1.0);
|
||||||
|
|
||||||
|
QRect transformedRect(0.0, 0.0, rect.width(), rect.height());
|
||||||
|
|
||||||
QImage image = imageElement->getImage();
|
QImage image = imageElement->getImage();
|
||||||
painter->drawImage(rect, image);
|
painter->fillRect(transformedRect, Qt::white);
|
||||||
|
painter->drawImage(transformedRect, image);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (const PDFEditedPageContentElementPath* pathElement = m_element->asPath())
|
if (const PDFEditedPageContentElementPath* pathElement = m_element->asPath())
|
||||||
|
@ -370,6 +370,7 @@ public:
|
|||||||
virtual QString getDescription() const override;
|
virtual QString getDescription() const override;
|
||||||
|
|
||||||
const PDFEditedPageContentElement* getElement() const { return m_element.get(); }
|
const PDFEditedPageContentElement* getElement() const { return m_element.get(); }
|
||||||
|
PDFEditedPageContentElement* getElement() { return m_element.get(); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::unique_ptr<PDFEditedPageContentElement> m_element;
|
std::unique_ptr<PDFEditedPageContentElement> m_element;
|
||||||
|
Loading…
Reference in New Issue
Block a user