diff --git a/Pdf4QtLib/sources/pdfpagecontentelements.cpp b/Pdf4QtLib/sources/pdfpagecontentelements.cpp index 2aa7da9..33baf1e 100644 --- a/Pdf4QtLib/sources/pdfpagecontentelements.cpp +++ b/Pdf4QtLib/sources/pdfpagecontentelements.cpp @@ -361,13 +361,57 @@ void PDFPageContentScene::clear() void PDFPageContentScene::shortcutOverrideEvent(QWidget* widget, QKeyEvent* event) { Q_UNUSED(widget); - event->ignore(); + + constexpr QKeySequence::StandardKey acceptedKeys[] = { QKeySequence::Delete, + QKeySequence::SelectAll, + QKeySequence::Deselect, + QKeySequence::Cancel }; + + if (std::any_of(std::begin(acceptedKeys), std::end(acceptedKeys), [event](QKeySequence::StandardKey standardKey) { return event == standardKey; })) + { + event->accept(); + return; + } } void PDFPageContentScene::keyPressEvent(QWidget* widget, QKeyEvent* event) { Q_UNUSED(widget); event->ignore(); + + if (event == QKeySequence::Delete) + { + if (!m_manipulator.isSelectionEmpty()) + { + m_manipulator.performDeleteSelection(); + event->accept(); + } + } + else if (event == QKeySequence::SelectAll) + { + if (!isEmpty()) + { + m_manipulator.selectAll(); + event->accept(); + } + } + else if (event == QKeySequence::Deselect) + { + if (!m_manipulator.isSelectionEmpty()) + { + m_manipulator.deselectAll(); + event->accept(); + } + } + else if (event == QKeySequence::Cancel) + { + if (m_manipulator.isManipulationInProgress()) + { + m_manipulator.cancelManipulation(); + m_manipulator.deselectAll(); + event->accept(); + } + } } void PDFPageContentScene::keyReleaseEvent(QWidget* widget, QKeyEvent* event) @@ -765,6 +809,18 @@ void PDFPageContentScene::setActive(bool newIsActive) } } +std::set PDFPageContentScene::getElementIds() const +{ + std::set result; + + for (const auto& element : m_elements) + { + result.insert(element->getElementId()); + } + + return result; +} + void PDFPageContentScene::removeElementsById(const std::set& selection) { const size_t oldSize = m_elements.size(); @@ -1356,6 +1412,12 @@ void PDFPageContentElementManipulator::deselect(const std::set& ids) update(ids, Deselect); } +void PDFPageContentElementManipulator::selectAll() +{ + std::set ids = m_scene->getElementIds(); + update(ids, Select); +} + void PDFPageContentElementManipulator::deselectAll() { update(-1, Clear); @@ -1377,7 +1439,7 @@ bool PDFPageContentElementManipulator::isManipulationAllowed(PDFInteger pageInde return false; } -void PDFPageContentElementManipulator::manipulateDeleteSelection() +void PDFPageContentElementManipulator::performDeleteSelection() { cancelManipulation(); m_scene->removeElementsById(m_selection); diff --git a/Pdf4QtLib/sources/pdfpagecontentelements.h b/Pdf4QtLib/sources/pdfpagecontentelements.h index 40e7f12..5833220 100644 --- a/Pdf4QtLib/sources/pdfpagecontentelements.h +++ b/Pdf4QtLib/sources/pdfpagecontentelements.h @@ -318,12 +318,13 @@ public: void selectNew(const std::set& ids); void deselect(PDFInteger id); void deselect(const std::set& ids); + void selectAll(); void deselectAll(); bool isManipulationAllowed(PDFInteger pageIndex) const; bool isManipulationInProgress() const { return m_isManipulationInProgress; } - void manipulateDeleteSelection(); + void performDeleteSelection(); void startManipulation(PDFInteger pageIndex, const QPointF& startPoint, @@ -394,6 +395,9 @@ public: bool isActive() const; void setActive(bool newIsActive); + /// Returns set of all element ids + std::set getElementIds() const; + /// Removes elements specified in selection /// \param selection Items to be removed void removeElementsById(const std::set& selection);