AudioBook Plugin: Item selection by text, regular expression, page indices

This commit is contained in:
Jakub Melka
2021-08-21 20:18:47 +02:00
parent 1aa60dca40
commit 8ccfbe291d
8 changed files with 199 additions and 2 deletions

View File

@ -18,9 +18,12 @@
#include "audiobookplugin.h"
#include "pdfdrawwidget.h"
#include "pdfwidgettool.h"
#include "pdfutils.h"
#include <QAction>
#include <QMainWindow>
#include <QMessageBox>
#include <QRegularExpression>
namespace pdfplugin
{
@ -54,6 +57,7 @@ void AudioBookPlugin::setWidget(pdf::PDFWidget* widget)
m_actionCreateTextStream = new QAction(QIcon(":/pdfplugins/audiobook/create-text-stream.svg"), tr("Create Text Stream for Audio Book"), this);
m_actionCreateTextStream->setObjectName("actionAudioBook_CreateTextStream");
connect(m_actionCreateTextStream, &QAction::triggered, this, &AudioBookPlugin::onCreateTextStreamTriggered);
m_actionSynchronizeFromTableToGraphics = new QAction(QIcon(":/pdfplugins/audiobook/synchronize-from-table-to-graphics.svg"), tr("Synchronize Selection from Table to Graphics"), this);
m_actionSynchronizeFromTableToGraphics->setObjectName("actionAudioBook_SynchronizeFromTableToGraphics");
@ -77,12 +81,15 @@ void AudioBookPlugin::setWidget(pdf::PDFWidget* widget)
m_actionSelectByContainedText = new QAction(QIcon(":/pdfplugins/audiobook/select-by-contained-text.svg"), tr("Select by Contained Text"), this);
m_actionSelectByContainedText->setObjectName("actionAudioBook_SelectByContainedText");
connect(m_actionSelectByContainedText, &QAction::triggered, this, &AudioBookPlugin::onSelectByContainedText);
m_actionSelectByRegularExpression = new QAction(QIcon(":/pdfplugins/audiobook/select-by-regular-expression.svg"), tr("Select by Regular Expression"), this);
m_actionSelectByRegularExpression->setObjectName("actionAudioBook_SelectByRegularExpression");
connect(m_actionSelectByRegularExpression, &QAction::triggered, this, &AudioBookPlugin::onSelectByRegularExpression);
m_actionSelectByPageList = new QAction(QIcon(":/pdfplugins/audiobook/select-by-page-list.svg"), tr("Select by Page List"), this);
m_actionSelectByPageList->setObjectName("actionAudioBook_SelectByPageList");
connect(m_actionSelectByPageList, &QAction::triggered, this, &AudioBookPlugin::onSelectByPageList);
m_actionRestoreOriginalText = new QAction(QIcon(":/pdfplugins/audiobook/restore-original-text.svg"), tr("Restore Original Text"), this);
m_actionRestoreOriginalText->setObjectName("actionAudioBook_RestoreOriginalText");
@ -96,8 +103,6 @@ void AudioBookPlugin::setWidget(pdf::PDFWidget* widget)
m_actionCreateAudioBook = new QAction(QIcon(":/pdfplugins/audiobook/create-audio-book.svg"), tr("Create Audio Book"), this);
m_actionCreateAudioBook->setObjectName("actionAudioBook_CreateAudioBook");
connect(m_actionCreateTextStream, &QAction::triggered, this, &AudioBookPlugin::onCreateTextStreamTriggered);
updateActions();
}
@ -107,7 +112,16 @@ void AudioBookPlugin::setDocument(const pdf::PDFModifiedDocument& document)
if (document.hasReset())
{
if (m_audioTextStreamEditorModel)
{
m_audioTextStreamEditorModel->beginFlowChange();
}
m_textFlowEditor.clear();
if (m_audioTextStreamEditorModel)
{
m_audioTextStreamEditorModel->endFlowChange();
}
updateActions();
}
}
@ -150,6 +164,8 @@ void AudioBookPlugin::onCreateTextStreamTriggered()
m_audioTextStreamEditorModel = new pdf::PDFDocumentTextFlowEditorModel(m_audioTextStreamDockWidget);
m_audioTextStreamEditorModel->setEditor(&m_textFlowEditor);
m_audioTextStreamDockWidget->setModel(m_audioTextStreamEditorModel);
connect(m_audioTextStreamEditorModel, &pdf::PDFDocumentTextFlowEditorModel::modelReset, this, &AudioBookPlugin::updateActions);
connect(m_audioTextStreamEditorModel, &pdf::PDFDocumentTextFlowEditorModel::dataChanged, this, &AudioBookPlugin::updateActions);
}
m_audioTextStreamDockWidget->show();
@ -183,6 +199,70 @@ void AudioBookPlugin::onSelectByRectangle()
m_widget->getToolManager()->pickRectangle(std::bind(&AudioBookPlugin::onRectanglePicked, this, std::placeholders::_1, std::placeholders::_2));
}
void AudioBookPlugin::onSelectByContainedText()
{
QString text = m_audioTextStreamDockWidget->getSelectionText();
m_audioTextStreamDockWidget->clearSelectionText();
if (!text.isEmpty())
{
m_audioTextStreamEditorModel->selectByContainedText(text);
}
else
{
QMessageBox::critical(m_audioTextStreamDockWidget, tr("Error"), tr("Cannot select items by text, because text is empty."));
}
}
void AudioBookPlugin::onSelectByRegularExpression()
{
QString pattern = m_audioTextStreamDockWidget->getSelectionText();
m_audioTextStreamDockWidget->clearSelectionText();
if (!pattern.isEmpty())
{
QRegularExpression expression(pattern);
if (expression.isValid())
{
m_audioTextStreamEditorModel->selectByRegularExpression(expression);
}
else
{
QMessageBox::critical(m_audioTextStreamDockWidget, tr("Error"), tr("Regular expression is not valid. %1").arg(expression.errorString()));
}
}
else
{
QMessageBox::critical(m_audioTextStreamDockWidget, tr("Error"), tr("Cannot select items by regular expression, because regular expression definition is empty."));
}
}
void AudioBookPlugin::onSelectByPageList()
{
QString pageIndicesText = m_audioTextStreamDockWidget->getSelectionText();
m_audioTextStreamDockWidget->clearSelectionText();
if (!pageIndicesText.isEmpty())
{
QString errorMessage;
auto pageIndices = pdf::PDFClosedIntervalSet::parse(1, m_document->getCatalog()->getPageCount(), pageIndicesText, &errorMessage);
if (errorMessage.isEmpty())
{
m_audioTextStreamEditorModel->selectByPageIndices(pageIndices);
}
else
{
QMessageBox::critical(m_audioTextStreamDockWidget, tr("Error"), tr("Cannot select items by page indices, because page indices are invalid. %1").arg(errorMessage));
}
}
else
{
QMessageBox::critical(m_audioTextStreamDockWidget, tr("Error"), tr("Cannot select items by page indices, because page indices are empty."));
}
}
void AudioBookPlugin::onRectanglePicked(pdf::PDFInteger pageIndex, QRectF rectangle)
{
Q_UNUSED(pageIndex);
@ -192,6 +272,18 @@ void AudioBookPlugin::onRectanglePicked(pdf::PDFInteger pageIndex, QRectF rectan
void AudioBookPlugin::updateActions()
{
m_actionCreateTextStream->setEnabled(m_document);
m_actionSynchronizeFromTableToGraphics->setEnabled(true);
m_actionSynchronizeFromGraphicsToTable->setEnabled(true);
m_actionActivateSelection->setEnabled(!m_textFlowEditor.isSelectionEmpty());
m_actionDeactivateSelection->setEnabled(!m_textFlowEditor.isSelectionEmpty());
m_actionSelectByRectangle->setEnabled(!m_textFlowEditor.isEmpty());
m_actionSelectByContainedText->setEnabled(!m_textFlowEditor.isEmpty());
m_actionSelectByRegularExpression->setEnabled(!m_textFlowEditor.isEmpty());
m_actionSelectByPageList->setEnabled(!m_textFlowEditor.isEmpty());
m_actionRestoreOriginalText->setEnabled(!m_textFlowEditor.isEmpty());
m_actionMoveSelectionUp->setEnabled(!m_textFlowEditor.isEmpty());
m_actionMoveSelectionDown->setEnabled(!m_textFlowEditor.isEmpty());
m_actionCreateAudioBook->setEnabled(!m_textFlowEditor.isEmpty());
}
} // namespace pdfplugin