mirror of
https://github.com/JakubMelka/PDF4QT.git
synced 2025-01-06 21:32:19 +01:00
Editing images
This commit is contained in:
parent
83adf1e49d
commit
b481b3afcc
@ -22,6 +22,11 @@
|
||||
#include "pdfpagecontenteditorprocessor.h"
|
||||
#include "pdfwidgetutils.h"
|
||||
|
||||
#include <QDir>
|
||||
#include <QFileDialog>
|
||||
#include <QImageReader>
|
||||
#include <QStandardPaths>
|
||||
|
||||
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<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;
|
||||
|
||||
void loadFromElement(PDFPageContentElementEdited* editedElement);
|
||||
void saveToElement(PDFPageContentElementEdited* editedElement);
|
||||
|
||||
private:
|
||||
void setImage(QImage image);
|
||||
void selectImage();
|
||||
|
||||
Ui::PDFPageContentEditorEditedItemSettings* ui;
|
||||
QImage m_image;
|
||||
};
|
||||
|
||||
} // namespace pdf
|
||||
|
@ -17,7 +17,7 @@
|
||||
<item>
|
||||
<widget class="QTabWidget" name="tabWidget">
|
||||
<property name="currentIndex">
|
||||
<number>0</number>
|
||||
<number>1</number>
|
||||
</property>
|
||||
<widget class="QWidget" name="textTab">
|
||||
<attribute name="title">
|
||||
@ -48,14 +48,87 @@
|
||||
<property name="title">
|
||||
<string>Image Content</string>
|
||||
</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>
|
||||
<widget class="QLabel" name="imageLabel">
|
||||
<property name="autoFillBackground">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string/>
|
||||
</property>
|
||||
</widget>
|
||||
</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>
|
||||
</widget>
|
||||
</item>
|
||||
|
@ -282,7 +282,7 @@ bool PDFPageContentEditorStyleSettings::showEditElementStyleDialog(QWidget* pare
|
||||
|
||||
if (dialog.exec() == QDialog::Accepted)
|
||||
{
|
||||
|
||||
widget->saveToElement(editedElement);
|
||||
}
|
||||
}
|
||||
else
|
||||
|
@ -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())
|
||||
|
@ -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<PDFEditedPageContentElement> m_element;
|
||||
|
Loading…
Reference in New Issue
Block a user