From eecbf33a49c6bfd4f8487d2b06cf65a7acada01a Mon Sep 17 00:00:00 2001 From: lupoDharkael Date: Tue, 13 Jun 2017 15:25:51 +0200 Subject: [PATCH] Refactor for consistent button position arround the selection --- src/capture/button.h | 14 ++++----- src/capture/buttonhandler.cpp | 55 ++++++++++++++++++----------------- src/capture/buttonhandler.h | 6 ++-- 3 files changed, 39 insertions(+), 36 deletions(-) diff --git a/src/capture/button.h b/src/capture/button.h index db17a7c4..bdd74339 100644 --- a/src/capture/button.h +++ b/src/capture/button.h @@ -30,20 +30,20 @@ class Button : public QPushButton { public: enum class Type { - selectionIndicator, - exit, - copy, - save, pencil, line, arrow, + selection, rectangle, circle, marker, - undo, - imageUploader, + selectionIndicator, move, - selection, + undo, + copy, + save, + exit, + imageUploader, last, // used for iteration over the enum text, mouseVisibility, diff --git a/src/capture/buttonhandler.cpp b/src/capture/buttonhandler.cpp index 84f9df86..744939b6 100644 --- a/src/capture/buttonhandler.cpp +++ b/src/capture/buttonhandler.cpp @@ -157,7 +157,7 @@ void ButtonHandler::updatePosition(const QRect &selection, } } - QVector positions = getHPoints(center, addCounter); + QVector positions = getHPoints(center, addCounter, true); for (QPoint p: positions) { m_vectorButtons[elemIndicator]->move(p); ++elemIndicator; @@ -171,7 +171,7 @@ void ButtonHandler::updatePosition(const QRect &selection, QPoint center = QPoint(baseArea.right() + SEPARATION, baseArea.center().y()); - QVector positions = getVPoints(center, addCounter); + QVector positions = getVPoints(center, addCounter, false); for (QPoint p: positions) { m_vectorButtons[elemIndicator]->move(p); ++elemIndicator; @@ -199,7 +199,7 @@ void ButtonHandler::updatePosition(const QRect &selection, center.setX(center.x() - (baseWidth+SEPARATION)/2); } } - QVector positions = getHPoints(center, addCounter); + QVector positions = getHPoints(center, addCounter, false); for (QPoint p: positions) { m_vectorButtons[elemIndicator]->move(p); ++elemIndicator; @@ -216,7 +216,7 @@ void ButtonHandler::updatePosition(const QRect &selection, } QPoint center = QPoint(baseArea.left() - (SEPARATION+baseWidth), baseArea.center().y()); - QVector positions = getVPoints(center, addCounter); + QVector positions = getVPoints(center, addCounter, true); for (QPoint p: positions) { m_vectorButtons[elemIndicator]->move(p); ++elemIndicator; @@ -251,24 +251,24 @@ void ButtonHandler::updatePosition(const QRect &selection, // starts from a known center and keeps adding elements horizontally // and returns the computed positions. QVector ButtonHandler::getHPoints( - const QPoint ¢er, const int elements) const + const QPoint ¢er, const int elements, const bool leftToRight) const { - QVector res; - QPoint left, right; + // distance from the center to start adding buttons + int shift = 0; if (elements % 2 == 0) { - left = QPoint(center.x()-m_distance + SEPARATION/2, center.y()); - right = QPoint(center.x()+SEPARATION/2, center.y()); + shift = m_distance * (elements / 2) - (SEPARATION / 2); } else { - res.append(QPoint(center.x()-(m_distance-SEPARATION)/2, center.y())); - left = QPoint(res[0].x()-m_distance, res[0].y()); - right = QPoint(res[0].x()+m_distance, res[0].y()); + shift = m_distance * ((elements-1) / 2) + Button::getButtonBaseSize() / 2; } + if (!leftToRight) { shift -= Button::getButtonBaseSize(); } + int x = leftToRight ? center.x() - shift : + center.x() + shift; + QPoint i(x, center.y()); while (elements > res.length()) { - res.append(left); - res.append(right); - left.setX(left.x()-m_distance); - right.setX(right.x()+m_distance); + res.append(i); + leftToRight ? i.setX(i.x() + m_distance) : + i.setX(i.x() - m_distance); } return res; } @@ -277,23 +277,24 @@ QVector ButtonHandler::getHPoints( // starts from a known center and keeps adding elements vertically // and returns the computed positions. QVector ButtonHandler::getVPoints( - const QPoint ¢er, const int elements) const + const QPoint ¢er, const int elements,const bool upToDown) const { QVector res; - QPoint up, down; + // distance from the center to start adding buttons + int shift = 0; if (elements % 2 == 0) { - up = QPoint(center.x(), center.y()-m_distance + SEPARATION/2); - down = QPoint(center.x(), center.y()+SEPARATION/2); + shift = m_distance * (elements / 2) - (SEPARATION / 2); } else { - res.append(QPoint(center.x(), center.y()-(m_distance-SEPARATION)/2)); - up = QPoint(res[0].x(), res[0].y()-m_distance); - down = QPoint(res[0].x(), res[0].y()+m_distance); + shift = m_distance * ((elements-1) / 2) + Button::getButtonBaseSize() / 2; } + if (!upToDown) { shift -= Button::getButtonBaseSize(); } + int y = upToDown ? center.y() - shift : + center.y() + shift; + QPoint i(center.x(), y); while (elements > res.length()) { - res.append(up); - res.append(down); - up.setY(up.y()-m_distance); - down.setY(down.y()+m_distance); + res.append(i); + upToDown ? i.setY(i.y() + m_distance) : + i.setY(i.y() - m_distance); } return res; } diff --git a/src/capture/buttonhandler.h b/src/capture/buttonhandler.h index 7c25094c..e6be2bc9 100644 --- a/src/capture/buttonhandler.h +++ b/src/capture/buttonhandler.h @@ -40,8 +40,10 @@ public: void setButtons(const QVector); private: - QVector getHPoints(const QPoint ¢er, const int elements) const; - QVector getVPoints(const QPoint ¢er, const int elements) const; + QVector getHPoints(const QPoint ¢er, const int elements, + const bool leftToRight) const; + QVector getVPoints(const QPoint ¢er, const int elements, + const bool upToDown) const; QVector m_vectorButtons; int m_distance;