From 9ac549d08653c4ca25b4ff90c029e26f7d7a169e Mon Sep 17 00:00:00 2001 From: David Mitchell Date: Thu, 29 Oct 2020 14:45:55 -0500 Subject: [PATCH] capturewidget refactor (#1114) * Renaming methods for better file outline * Moving redundant code into helper functions * Combine implementation of move and resize * Remove checks from move --- src/widgets/capture/capturewidget.cpp | 117 +++++++++----------------- src/widgets/capture/capturewidget.h | 20 +++-- 2 files changed, 54 insertions(+), 83 deletions(-) diff --git a/src/widgets/capture/capturewidget.cpp b/src/widgets/capture/capturewidget.cpp index 56dd49cf..0b7d4dd8 100644 --- a/src/widgets/capture/capturewidget.cpp +++ b/src/widgets/capture/capturewidget.cpp @@ -551,46 +551,29 @@ void CaptureWidget::mouseReleaseEvent(QMouseEvent* e) updateCursor(); } -void CaptureWidget::leftMove() +void CaptureWidget::moveSelection(QPoint p) { - if (m_selection->geometry().left() > rect().left()) { - m_selection->move(QPoint(m_selection->x() - 1, m_selection->y())); - m_buttonHandler->updatePosition(m_selection->geometry()); - update(); - } + adjustSelection(QMargins(-p.x(), -p.y(), p.x(), p.y())); } -void CaptureWidget::rightMove() +void CaptureWidget::moveLeft() { - if (m_selection->geometry().right() < rect().right()) { - m_selection->move(QPoint(m_selection->x() + 1, m_selection->y())); - QRect newGeometry = m_selection->geometry().intersected(rect()); - m_context.selection = extendedRect(&newGeometry); - m_buttonHandler->updatePosition(m_selection->geometry()); - update(); - } + moveSelection(QPoint(-1, 0)); } -void CaptureWidget::upMove() +void CaptureWidget::moveRight() { - if (m_selection->geometry().top() > rect().top()) { - m_selection->move(QPoint(m_selection->x(), m_selection->y() - 1)); - QRect newGeometry = m_selection->geometry().intersected(rect()); - m_context.selection = extendedRect(&newGeometry); - m_buttonHandler->updatePosition(m_selection->geometry()); - update(); - } + moveSelection(QPoint(1, 0)); } -void CaptureWidget::downMove() +void CaptureWidget::moveUp() { - if (m_selection->geometry().bottom() < rect().bottom()) { - m_selection->move(QPoint(m_selection->x(), m_selection->y() + 1)); - QRect newGeometry = m_selection->geometry().intersected(rect()); - m_context.selection = extendedRect(&newGeometry); - m_buttonHandler->updatePosition(m_selection->geometry()); - update(); - } + moveSelection(QPoint(0, -1)); +} + +void CaptureWidget::moveDown() +{ + moveSelection(QPoint(0, 1)); } void CaptureWidget::keyPressEvent(QKeyEvent* e) @@ -897,12 +880,10 @@ void CaptureWidget::setDrawThickness(const int& t) emit thicknessChanged(m_context.thickness); } -void CaptureWidget::leftResize() +void CaptureWidget::repositionSelection(QRect r) { - if (m_selection->isVisible() && - m_selection->geometry().right() > m_selection->geometry().left()) { - m_selection->setGeometry(m_selection->geometry() + - QMargins(0, 0, -1, 0)); + if (m_selection->isVisible()) { + m_selection->setGeometry(r); QRect newGeometry = m_selection->geometry().intersected(rect()); m_context.selection = extendedRect(&newGeometry); m_buttonHandler->updatePosition(m_selection->geometry()); @@ -911,46 +892,32 @@ void CaptureWidget::leftResize() } } -void CaptureWidget::rightResize() +void CaptureWidget::adjustSelection(QMargins m) { - if (m_selection->isVisible() && - m_selection->geometry().right() < rect().right()) { - m_selection->setGeometry(m_selection->geometry() + - QMargins(0, 0, 1, 0)); - QRect newGeometry = m_selection->geometry().intersected(rect()); - m_context.selection = extendedRect(&newGeometry); - m_buttonHandler->updatePosition(m_selection->geometry()); - updateSizeIndicator(); - update(); + QRect newGeometry = m_selection->geometry() + m; + if (rect().contains(newGeometry)) { + repositionSelection(newGeometry); } } -void CaptureWidget::upResize() +void CaptureWidget::resizeLeft() { - if (m_selection->isVisible() && - m_selection->geometry().bottom() > m_selection->geometry().top()) { - m_selection->setGeometry(m_selection->geometry() + - QMargins(0, 0, 0, -1)); - QRect newGeometry = m_selection->geometry().intersected(rect()); - m_context.selection = extendedRect(&newGeometry); - m_buttonHandler->updatePosition(m_selection->geometry()); - updateSizeIndicator(); - update(); - } + adjustSelection(QMargins(0, 0, -1, 0)); } -void CaptureWidget::downResize() +void CaptureWidget::resizeRight() { - if (m_selection->isVisible() && - m_selection->geometry().bottom() < rect().bottom()) { - m_selection->setGeometry(m_selection->geometry() + - QMargins(0, 0, 0, 1)); - QRect newGeometry = m_selection->geometry().intersected(rect()); - m_context.selection = extendedRect(&newGeometry); - m_buttonHandler->updatePosition(m_selection->geometry()); - updateSizeIndicator(); - update(); - } + adjustSelection(QMargins(0, 0, 1, 0)); +} + +void CaptureWidget::resizeUp() +{ + adjustSelection(QMargins(0, 0, 0, -1)); +} + +void CaptureWidget::resizeDown() +{ + adjustSelection(QMargins(0, 0, 0, 1)); } void CaptureWidget::initShortcuts() @@ -985,29 +952,29 @@ void CaptureWidget::initShortcuts() new QShortcut(QKeySequence(ConfigHandler().shortcut("TYPE_RESIZE_LEFT")), this, - SLOT(leftResize())); + SLOT(resizeLeft())); new QShortcut(QKeySequence(ConfigHandler().shortcut("TYPE_RESIZE_RIGHT")), this, - SLOT(rightResize())); + SLOT(resizeRight())); new QShortcut(QKeySequence(ConfigHandler().shortcut("TYPE_RESIZE_UP")), this, - SLOT(upResize())); + SLOT(resizeUp())); new QShortcut(QKeySequence(ConfigHandler().shortcut("TYPE_RESIZE_DOWN")), this, - SLOT(downResize())); + SLOT(resizeDown())); new QShortcut(QKeySequence(ConfigHandler().shortcut("TYPE_MOVE_LEFT")), this, - SLOT(leftMove())); + SLOT(moveLeft())); new QShortcut(QKeySequence(ConfigHandler().shortcut("TYPE_MOVE_RIGHT")), this, - SLOT(rightMove())); + SLOT(moveRight())); new QShortcut(QKeySequence(ConfigHandler().shortcut("TYPE_MOVE_UP")), this, - SLOT(upMove())); + SLOT(moveUp())); new QShortcut(QKeySequence(ConfigHandler().shortcut("TYPE_MOVE_DOWN")), this, - SLOT(downMove())); + SLOT(moveDown())); new QShortcut( QKeySequence(ConfigHandler().shortcut("TYPE_COMMIT_CURRENT_TOOL")), diff --git a/src/widgets/capture/capturewidget.h b/src/widgets/capture/capturewidget.h index 7b83a214..c1d40a65 100644 --- a/src/widgets/capture/capturewidget.h +++ b/src/widgets/capture/capturewidget.h @@ -80,15 +80,15 @@ private slots: void childEnter(); void childLeave(); - void leftResize(); - void rightResize(); - void upResize(); - void downResize(); + void resizeLeft(); + void resizeRight(); + void resizeUp(); + void resizeDown(); - void leftMove(); - void rightMove(); - void upMove(); - void downMove(); + void moveLeft(); + void moveRight(); + void moveUp(); + void moveDown(); void setState(CaptureToolButton* b); void processTool(CaptureTool* t); @@ -140,6 +140,10 @@ private: void pushToolToStack(); void makeChild(QWidget* w); + void repositionSelection(QRect r); + void adjustSelection(QMargins m); + void moveSelection(QPoint p); + private: QRect extendedSelection() const; QRect extendedRect(QRect* r) const;