AudioBook Plugin: Actions, item selection

This commit is contained in:
Jakub Melka
2021-08-20 17:52:42 +02:00
parent 9daaa92e90
commit 1aa60dca40
24 changed files with 1652 additions and 40 deletions

View File

@@ -787,6 +787,25 @@ void PDFDocumentTextFlowEditor::setTextFlow(PDFDocumentTextFlow textFlow)
createEditedFromOriginalTextFlow();
}
void PDFDocumentTextFlowEditor::setSelectionActive(bool active)
{
for (auto& item : m_editedTextFlow)
{
if (item.editedItemFlags.testFlag(Selected))
{
item.editedItemFlags.setFlag(Removed, !active);
}
}
}
void PDFDocumentTextFlowEditor::deselect()
{
for (auto& item : m_editedTextFlow)
{
item.editedItemFlags.setFlag(Selected, false);
}
}
void PDFDocumentTextFlowEditor::removeItem(size_t index)
{
getEditedItem(index)->editedItemFlags.setFlag(Removed, true);
@@ -810,6 +829,23 @@ void PDFDocumentTextFlowEditor::setText(const QString& text, size_t index)
updateModifiedFlag(index);
}
void PDFDocumentTextFlowEditor::selectByRectangle(QRectF rectangle)
{
for (auto& item : m_editedTextFlow)
{
const QRectF& boundingRectangle = item.boundingRect;
if (boundingRectangle.isEmpty())
{
item.editedItemFlags.setFlag(Selected, false);
continue;
}
const bool isContained = rectangle.contains(boundingRectangle);
item.editedItemFlags.setFlag(Selected, isContained);
}
}
void PDFDocumentTextFlowEditor::createEditedFromOriginalTextFlow()
{
const size_t count = m_originalTextFlow.getSize();

View File

@@ -142,6 +142,13 @@ public:
/// \param textFlow Text flow
void setTextFlow(PDFDocumentTextFlow textFlow);
/// Marks selected item as active or inactive
/// \param active Active
void setSelectionActive(bool active);
/// Deselects all selected items
void deselect();
void removeItem(size_t index);
void addItem(size_t index);
@@ -151,7 +158,8 @@ public:
{
None = 0x0000,
Removed = 0x0001,
Modified = 0x0002
Modified = 0x0002,
Selected = 0x0004
};
Q_DECLARE_FLAGS(EditedItemFlags, EditedItemFlag)
@@ -175,6 +183,10 @@ public:
/// \param index Index
bool isModified(size_t index) const { return getEditedItem(index)->editedItemFlags.testFlag(Modified); }
/// Returns true, if item is selected
/// \param index Index
bool isSelected(size_t index) const { return getEditedItem(index)->editedItemFlags.testFlag(Selected); }
/// Returns edited text (or original, if edited text is not modified)
/// for a given index.
/// \param index Index
@@ -198,6 +210,10 @@ public:
bool isItemTypeTitle(size_t index) const { return getEditedItem(index)->isTitle(); }
bool isItemTypeLanguage(size_t index) const { return getEditedItem(index)->isLanguage(); }
/// Selects items contained in a rectangle
/// \param rectangle Selection rectangle
void selectByRectangle(QRectF rectangle);
private:
void createEditedFromOriginalTextFlow();
void updateModifiedFlag(size_t index);

View File

@@ -18,6 +18,9 @@
#include "pdfdocumenttextfloweditormodel.h"
#include "pdfdocumenttextflow.h"
#include <QColor>
#include <QBrush>
namespace pdf
{
@@ -97,6 +100,14 @@ QVariant PDFDocumentTextFlowEditorModel::data(const QModelIndex& index, int role
return QVariant();
}
if (role == Qt::BackgroundRole)
{
if (m_editor->isSelected(index.row()))
{
return QBrush(QColor(255, 255, 200));
}
}
if (role == Qt::DisplayRole || role == Qt::EditRole)
{
switch (index.column())
@@ -218,4 +229,27 @@ void PDFDocumentTextFlowEditorModel::endFlowChange()
endResetModel();
}
void PDFDocumentTextFlowEditorModel::setSelectionActivated(bool activate)
{
if (!m_editor || m_editor->isEmpty())
{
return;
}
m_editor->setSelectionActive(activate);
m_editor->deselect();
emit dataChanged(index(0, 0), index(rowCount(QModelIndex()) - 1, ColumnLast));
}
void PDFDocumentTextFlowEditorModel::selectByRectangle(QRectF rectangle)
{
if (!m_editor || m_editor->isEmpty())
{
return;
}
m_editor->selectByRectangle(rectangle);
emit dataChanged(index(0, 0), index(rowCount(QModelIndex()) - 1, ColumnLast));
}
} // namespace pdf

View File

@@ -61,6 +61,9 @@ public:
void beginFlowChange();
void endFlowChange();
void setSelectionActivated(bool activate);
void selectByRectangle(QRectF rectangle);
private:
PDFDocumentTextFlowEditor* m_editor;
};

View File

@@ -722,6 +722,8 @@ PDFToolManager::PDFToolManager(PDFDrawWidgetProxy* proxy, Actions actions, QObje
BaseClass(parent),
m_predefinedTools()
{
auto pickTool = new PDFPickTool(proxy, PDFPickTool::Mode::Rectangles, this);
m_predefinedTools[PickRectangleTool] = pickTool;
m_predefinedTools[FindTextTool] = new PDFFindTextTool(proxy, actions.findPrevAction, actions.findNextAction, this, parentDialog);
m_predefinedTools[SelectTextTool] = new PDFSelectTextTool(proxy, actions.selectTextToolAction, actions.copyTextAction, actions.selectAllAction, actions.deselectAction, this);
m_predefinedTools[MagnifierTool] = new PDFMagnifierTool(proxy, actions.magnifierAction, this);
@@ -732,6 +734,8 @@ PDFToolManager::PDFToolManager(PDFDrawWidgetProxy* proxy, Actions actions, QObje
{
addTool(tool);
}
connect(pickTool, &PDFPickTool::rectanglePicked, this, &PDFToolManager::onRectanglePicked);
}
void PDFToolManager::addTool(PDFWidgetTool* tool)
@@ -748,6 +752,13 @@ void PDFToolManager::addTool(PDFWidgetTool* tool)
connect(tool, &PDFWidgetTool::toolActivityChanged, this, &PDFToolManager::onToolActivityChanged);
}
void PDFToolManager::pickRectangle(std::function<void (PDFInteger, QRectF)> callback)
{
setActiveTool(nullptr);
m_pickRectangleCallback = callback;
setActiveTool(m_predefinedTools[PickRectangleTool]);
}
void PDFToolManager::setDocument(const PDFModifiedDocument& document)
{
for (PDFWidgetTool* tool : m_tools)
@@ -889,10 +900,11 @@ const std::optional<QCursor>& PDFToolManager::getCursor() const
void PDFToolManager::onToolActivityChanged(bool active)
{
PDFWidgetTool* tool = qobject_cast<PDFWidgetTool*>(sender());
if (active)
{
// When tool is activated outside, we must deactivate old active tool
PDFWidgetTool* tool = qobject_cast<PDFWidgetTool*>(sender());
for (PDFWidgetTool* currentTool : m_tools)
{
if (currentTool->isActive() && currentTool != tool)
@@ -901,6 +913,14 @@ void PDFToolManager::onToolActivityChanged(bool active)
}
}
}
else
{
// Clear callback, if we are deactivating a tool
if (tool == m_predefinedTools[PickRectangleTool])
{
m_pickRectangleCallback = nullptr;
}
}
}
void PDFToolManager::onToolActionTriggered(bool checked)
@@ -916,6 +936,16 @@ void PDFToolManager::onToolActionTriggered(bool checked)
}
}
void PDFToolManager::onRectanglePicked(PDFInteger pageIndex, QRectF pageRectangle)
{
if (m_pickRectangleCallback)
{
m_pickRectangleCallback(pageIndex, pageRectangle);
}
setActiveTool(nullptr);
}
PDFMagnifierTool::PDFMagnifierTool(PDFDrawWidgetProxy* proxy, QAction* action, QObject* parent) :
BaseClass(proxy, action, parent),
m_magnifierSize(200),
@@ -1127,7 +1157,7 @@ void PDFPickTool::mousePressEvent(QWidget* widget, QMouseEvent* event)
}
buildSnapData();
getProxy()->repaintNeeded();
emit getProxy()->repaintNeeded();
}
}
else
@@ -1161,7 +1191,7 @@ void PDFPickTool::mouseMoveEvent(QWidget* widget, QMouseEvent* event)
{
m_mousePosition = mousePos;
m_snapper.updateSnappedPoint(m_mousePosition);
getProxy()->repaintNeeded();
emit getProxy()->repaintNeeded();
}
}

View File

@@ -447,6 +447,7 @@ public:
enum PredefinedTools
{
PickRectangleTool,
FindTextTool,
SelectTextTool,
MagnifierTool,
@@ -461,6 +462,11 @@ public:
/// Adds a new tool to tool manager
void addTool(PDFWidgetTool* tool);
/// Picks rectangle, if rectangle is successfully picked,
/// then callback is called.
/// \param callback Callback function
void pickRectangle(std::function<void(PDFInteger, QRectF)> callback);
/// Returns first active tool from tool set. If no tool is active,
/// then nullptr is returned.
PDFWidgetTool* getActiveTool() const;
@@ -526,15 +532,17 @@ signals:
/// This signal is emitted, when tool changes the document by some way
/// \param documet Modified document
void documentModified(PDFModifiedDocument document);
void documentModified(pdf::PDFModifiedDocument document);
private:
void onToolActivityChanged(bool active);
void onToolActionTriggered(bool checked);
void onRectanglePicked(PDFInteger pageIndex, QRectF pageRectangle);
std::set<PDFWidgetTool*> m_tools;
std::array<PDFWidgetTool*, ToolEnd> m_predefinedTools;
std::map<QAction*, PDFWidgetTool*> m_actionsToTools;
std::function<void(PDFInteger, QRectF)> m_pickRectangleCallback;
};
} // namespace pdf