Signature plugin: Keypress handling

This commit is contained in:
Jakub Melka 2022-03-09 20:42:05 +01:00
parent a401b8c031
commit 2b7d5939bd
2 changed files with 69 additions and 3 deletions

View File

@ -361,13 +361,57 @@ void PDFPageContentScene::clear()
void PDFPageContentScene::shortcutOverrideEvent(QWidget* widget, QKeyEvent* event) void PDFPageContentScene::shortcutOverrideEvent(QWidget* widget, QKeyEvent* event)
{ {
Q_UNUSED(widget); 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) void PDFPageContentScene::keyPressEvent(QWidget* widget, QKeyEvent* event)
{ {
Q_UNUSED(widget); Q_UNUSED(widget);
event->ignore(); 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) void PDFPageContentScene::keyReleaseEvent(QWidget* widget, QKeyEvent* event)
@ -765,6 +809,18 @@ void PDFPageContentScene::setActive(bool newIsActive)
} }
} }
std::set<PDFInteger> PDFPageContentScene::getElementIds() const
{
std::set<PDFInteger> result;
for (const auto& element : m_elements)
{
result.insert(element->getElementId());
}
return result;
}
void PDFPageContentScene::removeElementsById(const std::set<PDFInteger>& selection) void PDFPageContentScene::removeElementsById(const std::set<PDFInteger>& selection)
{ {
const size_t oldSize = m_elements.size(); const size_t oldSize = m_elements.size();
@ -1356,6 +1412,12 @@ void PDFPageContentElementManipulator::deselect(const std::set<PDFInteger>& ids)
update(ids, Deselect); update(ids, Deselect);
} }
void PDFPageContentElementManipulator::selectAll()
{
std::set<PDFInteger> ids = m_scene->getElementIds();
update(ids, Select);
}
void PDFPageContentElementManipulator::deselectAll() void PDFPageContentElementManipulator::deselectAll()
{ {
update(-1, Clear); update(-1, Clear);
@ -1377,7 +1439,7 @@ bool PDFPageContentElementManipulator::isManipulationAllowed(PDFInteger pageInde
return false; return false;
} }
void PDFPageContentElementManipulator::manipulateDeleteSelection() void PDFPageContentElementManipulator::performDeleteSelection()
{ {
cancelManipulation(); cancelManipulation();
m_scene->removeElementsById(m_selection); m_scene->removeElementsById(m_selection);

View File

@ -318,12 +318,13 @@ public:
void selectNew(const std::set<PDFInteger>& ids); void selectNew(const std::set<PDFInteger>& ids);
void deselect(PDFInteger id); void deselect(PDFInteger id);
void deselect(const std::set<PDFInteger>& ids); void deselect(const std::set<PDFInteger>& ids);
void selectAll();
void deselectAll(); void deselectAll();
bool isManipulationAllowed(PDFInteger pageIndex) const; bool isManipulationAllowed(PDFInteger pageIndex) const;
bool isManipulationInProgress() const { return m_isManipulationInProgress; } bool isManipulationInProgress() const { return m_isManipulationInProgress; }
void manipulateDeleteSelection(); void performDeleteSelection();
void startManipulation(PDFInteger pageIndex, void startManipulation(PDFInteger pageIndex,
const QPointF& startPoint, const QPointF& startPoint,
@ -394,6 +395,9 @@ public:
bool isActive() const; bool isActive() const;
void setActive(bool newIsActive); void setActive(bool newIsActive);
/// Returns set of all element ids
std::set<PDFInteger> getElementIds() const;
/// Removes elements specified in selection /// Removes elements specified in selection
/// \param selection Items to be removed /// \param selection Items to be removed
void removeElementsById(const std::set<PDFInteger>& selection); void removeElementsById(const std::set<PDFInteger>& selection);