mirror of
https://github.com/JakubMelka/PDF4QT.git
synced 2025-04-25 23:48:41 +02:00
Editor plugin: Update of text element
This commit is contained in:
parent
b5f102abbd
commit
ec31f56981
@ -188,37 +188,11 @@ QString EditorPlugin::getPluginMenuName() const
|
|||||||
return tr("Edi&tor");
|
return tr("Edi&tor");
|
||||||
}
|
}
|
||||||
|
|
||||||
bool EditorPlugin::save()
|
bool EditorPlugin::updatePageContent(pdf::PDFInteger pageIndex,
|
||||||
|
const std::vector<const pdf::PDFPageContentElement*>& elements,
|
||||||
|
pdf::PDFDocumentBuilder* builder)
|
||||||
{
|
{
|
||||||
pdf::PDFTemporaryValueChange guard(&m_isSaving, true);
|
|
||||||
|
|
||||||
auto answer = QMessageBox::question(m_dataExchangeInterface->getMainWindow(), tr("Confirm Changes"), tr("The changes to the page content will be written to the document. Do you want to continue?"), QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel, QMessageBox::Cancel);
|
|
||||||
|
|
||||||
if (answer == QMessageBox::Cancel)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (answer == QMessageBox::Yes)
|
|
||||||
{
|
|
||||||
pdf::PDFDocumentModifier modifier(m_document);
|
|
||||||
|
|
||||||
pdf::PDFColorConvertor convertor;
|
pdf::PDFColorConvertor convertor;
|
||||||
|
|
||||||
std::set<pdf::PDFInteger> pageIndices;
|
|
||||||
for (const auto& item : m_editedPageContent)
|
|
||||||
{
|
|
||||||
pageIndices.insert(item.first);
|
|
||||||
}
|
|
||||||
|
|
||||||
auto elementsByPage = m_scene.getElementsByPage();
|
|
||||||
for (pdf::PDFInteger pageIndex : pageIndices)
|
|
||||||
{
|
|
||||||
if (m_editedPageContent.count(pageIndex) == 0)
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
const pdf::PDFPage* page = m_document->getCatalog()->getPage(pageIndex);
|
const pdf::PDFPage* page = m_document->getCatalog()->getPage(pageIndex);
|
||||||
const pdf::PDFEditedPageContent& editedPageContent = m_editedPageContent.at(pageIndex);
|
const pdf::PDFEditedPageContent& editedPageContent = m_editedPageContent.at(pageIndex);
|
||||||
|
|
||||||
@ -228,10 +202,7 @@ bool EditorPlugin::save()
|
|||||||
pdf::PDFPageContentEditorContentStreamBuilder contentStreamBuilder(m_document);
|
pdf::PDFPageContentEditorContentStreamBuilder contentStreamBuilder(m_document);
|
||||||
contentStreamBuilder.setFontDictionary(editedPageContent.getFontDictionary());
|
contentStreamBuilder.setFontDictionary(editedPageContent.getFontDictionary());
|
||||||
|
|
||||||
auto it = elementsByPage.find(pageIndex);
|
for (const pdf::PDFPageContentElement* element : elements)
|
||||||
if (it != elementsByPage.cend())
|
|
||||||
{
|
|
||||||
for (const pdf::PDFPageContentElement* element : it->second)
|
|
||||||
{
|
{
|
||||||
const pdf::PDFPageContentElementEdited* editedElement = element->asElementEdited();
|
const pdf::PDFPageContentElementEdited* editedElement = element->asElementEdited();
|
||||||
const pdf::PDFPageContentElementRectangle* elementRectangle = element->asElementRectangle();
|
const pdf::PDFPageContentElementRectangle* elementRectangle = element->asElementRectangle();
|
||||||
@ -322,7 +293,6 @@ bool EditorPlugin::save()
|
|||||||
elementTextBox->drawPage(&painter, &m_scene, pageIndex, nullptr, textLayoutGetter, QTransform(), convertor, errors);
|
elementTextBox->drawPage(&painter, &m_scene, pageIndex, nullptr, textLayoutGetter, QTransform(), convertor, errors);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
QStringList errors = contentStreamBuilder.getErrors();
|
QStringList errors = contentStreamBuilder.getErrors();
|
||||||
contentStreamBuilder.clearErrors();
|
contentStreamBuilder.clearErrors();
|
||||||
@ -342,8 +312,6 @@ bool EditorPlugin::save()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pdf::PDFDocumentBuilder* builder = modifier.getBuilder();
|
|
||||||
|
|
||||||
pdf::PDFDictionary fontDictionary = contentStreamBuilder.getFontDictionary();
|
pdf::PDFDictionary fontDictionary = contentStreamBuilder.getFontDictionary();
|
||||||
pdf::PDFDictionary xobjectDictionary = contentStreamBuilder.getXObjectDictionary();
|
pdf::PDFDictionary xobjectDictionary = contentStreamBuilder.getXObjectDictionary();
|
||||||
pdf::PDFDictionary graphicStateDictionary = contentStreamBuilder.getGraphicStateDictionary();
|
pdf::PDFDictionary graphicStateDictionary = contentStreamBuilder.getGraphicStateDictionary();
|
||||||
@ -402,6 +370,51 @@ bool EditorPlugin::save()
|
|||||||
pageObject = pdf::PDFObjectManipulator::merge(pageObject, factory.takeObject(), pdf::PDFObjectManipulator::RemoveNullObjects);
|
pageObject = pdf::PDFObjectManipulator::merge(pageObject, factory.takeObject(), pdf::PDFObjectManipulator::RemoveNullObjects);
|
||||||
builder->setObject(page->getPageReference(), std::move(pageObject));
|
builder->setObject(page->getPageReference(), std::move(pageObject));
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool EditorPlugin::save()
|
||||||
|
{
|
||||||
|
pdf::PDFTemporaryValueChange guard(&m_isSaving, true);
|
||||||
|
|
||||||
|
auto answer = QMessageBox::question(m_dataExchangeInterface->getMainWindow(), tr("Confirm Changes"), tr("The changes to the page content will be written to the document. Do you want to continue?"), QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel, QMessageBox::Cancel);
|
||||||
|
|
||||||
|
if (answer == QMessageBox::Cancel)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (answer == QMessageBox::Yes)
|
||||||
|
{
|
||||||
|
pdf::PDFDocumentModifier modifier(m_document);
|
||||||
|
pdf::PDFDocumentBuilder* builder = modifier.getBuilder();
|
||||||
|
|
||||||
|
std::set<pdf::PDFInteger> pageIndices;
|
||||||
|
for (const auto& item : m_editedPageContent)
|
||||||
|
{
|
||||||
|
pageIndices.insert(item.first);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::map<pdf::PDFInteger, std::vector<const pdf::PDFPageContentElement*>> elementsByPage = m_scene.getElementsByPage();
|
||||||
|
for (pdf::PDFInteger pageIndex : pageIndices)
|
||||||
|
{
|
||||||
|
if (m_editedPageContent.count(pageIndex) == 0)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<const pdf::PDFPageContentElement*> elements;
|
||||||
|
auto it = elementsByPage.find(pageIndex);
|
||||||
|
if (it != elementsByPage.cend())
|
||||||
|
{
|
||||||
|
elements = std::move(it->second);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!updatePageContent(pageIndex, elements, builder))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
modifier.markReset();
|
modifier.markReset();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -517,6 +530,15 @@ void EditorPlugin::onSceneEditElement(const std::set<pdf::PDFInteger>& elements)
|
|||||||
|
|
||||||
if (pdf::PDFPageContentEditorStyleSettings::showEditElementStyleDialog(m_dataExchangeInterface->getMainWindow(), element))
|
if (pdf::PDFPageContentEditorStyleSettings::showEditElementStyleDialog(m_dataExchangeInterface->getMainWindow(), element))
|
||||||
{
|
{
|
||||||
|
if (element->asElementEdited())
|
||||||
|
{
|
||||||
|
pdf::PDFPageContentElementEdited* editedElement = dynamic_cast<pdf::PDFPageContentElementEdited*>(element);
|
||||||
|
if (editedElement->getElement()->asText())
|
||||||
|
{
|
||||||
|
updateTextElement(editedElement);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
updateGraphics();
|
updateGraphics();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -751,6 +773,55 @@ void EditorPlugin::updateEditedPages()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool EditorPlugin::updateTextElement(pdf::PDFPageContentElementEdited* element)
|
||||||
|
{
|
||||||
|
pdf::PDFPageContentElementEdited* elementEdited = dynamic_cast<pdf::PDFPageContentElementEdited*>(element);
|
||||||
|
pdf::PDFEditedPageContentElementText* targetTextElement = elementEdited->getElement()->asText();
|
||||||
|
|
||||||
|
if (!targetTextElement)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
pdf::PDFDocumentModifier modifier(m_document);
|
||||||
|
pdf::PDFDocumentBuilder* builder = modifier.getBuilder();
|
||||||
|
|
||||||
|
if (!updatePageContent(element->getPageIndex(), { element }, builder))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (modifier.finalize())
|
||||||
|
{
|
||||||
|
pdf::PDFDocument* document = modifier.getDocument().get();
|
||||||
|
|
||||||
|
const pdf::PDFPage* page = document->getCatalog()->getPage(element->getPageIndex());
|
||||||
|
auto cms = m_widget->getDrawWidgetProxy()->getCMSManager()->getCurrentCMS();
|
||||||
|
pdf::PDFFontCache fontCache(64, 64);
|
||||||
|
pdf::PDFOptionalContentActivity activity(document, pdf::OCUsage::View, nullptr);
|
||||||
|
fontCache.setDocument(pdf::PDFModifiedDocument(document, &activity));
|
||||||
|
pdf::PDFPageContentEditorProcessor processor(page, document, &fontCache, cms.data(), &activity, QTransform(), pdf::PDFMeshQualitySettings());
|
||||||
|
|
||||||
|
QList<pdf::PDFRenderError> errors = processor.processContents();
|
||||||
|
Q_UNUSED(errors);
|
||||||
|
|
||||||
|
pdf::PDFEditedPageContent content = processor.takeEditedPageContent();
|
||||||
|
if (content.getElementCount() == 1)
|
||||||
|
{
|
||||||
|
pdf::PDFEditedPageContentElement* sourceElement = content.getElement(0);
|
||||||
|
pdf::PDFEditedPageContentElementText* sourceElementText = sourceElement->asText();
|
||||||
|
targetTextElement->setState(sourceElementText->getState());
|
||||||
|
targetTextElement->setTextPath(sourceElementText->getTextPath());
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
void EditorPlugin::onDrawSpaceChanged()
|
void EditorPlugin::onDrawSpaceChanged()
|
||||||
{
|
{
|
||||||
updateEditedPages();
|
updateEditedPages();
|
||||||
|
@ -112,6 +112,11 @@ private:
|
|||||||
void updateDockWidget();
|
void updateDockWidget();
|
||||||
void updateEditedPages();
|
void updateEditedPages();
|
||||||
|
|
||||||
|
bool updatePageContent(pdf::PDFInteger pageIndex,
|
||||||
|
const std::vector<const pdf::PDFPageContentElement*>& elements,
|
||||||
|
pdf::PDFDocumentBuilder* builder);
|
||||||
|
bool updateTextElement(pdf::PDFPageContentElementEdited* element);
|
||||||
|
|
||||||
void onDrawSpaceChanged();
|
void onDrawSpaceChanged();
|
||||||
|
|
||||||
pdf::PDFWidgetTool* getActiveTool();
|
pdf::PDFWidgetTool* getActiveTool();
|
||||||
|
@ -283,6 +283,7 @@ bool PDFPageContentEditorStyleSettings::showEditElementStyleDialog(QWidget* pare
|
|||||||
if (dialog.exec() == QDialog::Accepted)
|
if (dialog.exec() == QDialog::Accepted)
|
||||||
{
|
{
|
||||||
widget->saveToElement(editedElement);
|
widget->saveToElement(editedElement);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -36,6 +36,11 @@
|
|||||||
namespace pdf
|
namespace pdf
|
||||||
{
|
{
|
||||||
|
|
||||||
|
PDFPageContentElement::~PDFPageContentElement()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
PDFInteger PDFPageContentElement::getPageIndex() const
|
PDFInteger PDFPageContentElement::getPageIndex() const
|
||||||
{
|
{
|
||||||
return m_pageIndex;
|
return m_pageIndex;
|
||||||
|
@ -51,7 +51,7 @@ class PDF4QTLIBWIDGETSSHARED_EXPORT PDFPageContentElement
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
explicit PDFPageContentElement() = default;
|
explicit PDFPageContentElement() = default;
|
||||||
virtual ~PDFPageContentElement() = default;
|
virtual ~PDFPageContentElement();
|
||||||
|
|
||||||
virtual PDFPageContentElement* clone() const = 0;
|
virtual PDFPageContentElement* clone() const = 0;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user