Add 45-multiple degree adjustment for line, arrow and marker tools (#439)
* Add 45-multiple degree adjustment for line, arrow and marker tools * Adjustment: Ctrl press is checked + widened functionality for two-point tools
This commit is contained in:
committed by
Dharkael
parent
bd83eea7af
commit
b42f1cf01d
@@ -16,6 +16,29 @@
|
||||
// along with Flameshot. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
#include "abstracttwopointtool.h"
|
||||
#include <cmath>
|
||||
|
||||
namespace {
|
||||
|
||||
const double ADJ_UNIT = std::atan(1.0);
|
||||
const int DIRS_NUMBER = 4;
|
||||
|
||||
enum UNIT {
|
||||
HORIZ_DIR = 0,
|
||||
DIAG1_DIR = 1,
|
||||
VERT_DIR = 2,
|
||||
DIAG2_DIR = 3
|
||||
};
|
||||
|
||||
const double ADJ_DIAG_UNIT = 2 * ADJ_UNIT;
|
||||
const int DIAG_DIRS_NUMBER = 2;
|
||||
|
||||
enum DIAG_UNIT {
|
||||
DIR1 = 0,
|
||||
DIR2 = 1
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
AbstractTwoPointTool::AbstractTwoPointTool(QObject *parent) :
|
||||
CaptureTool(parent), m_thickness(0), m_padding(0)
|
||||
@@ -52,6 +75,10 @@ void AbstractTwoPointTool::drawMove(const QPoint &p) {
|
||||
m_points.second = p;
|
||||
}
|
||||
|
||||
void AbstractTwoPointTool::drawMoveWithAdjustment(const QPoint &p) {
|
||||
m_points.second = m_points.first + adjustedVector(p - m_points.first);
|
||||
}
|
||||
|
||||
void AbstractTwoPointTool::colorChanged(const QColor &c) {
|
||||
m_color = c;
|
||||
}
|
||||
@@ -70,3 +97,39 @@ QRect AbstractTwoPointTool::backupRect(const QRect &limits) const {
|
||||
r += QMargins(val, val, val, val);
|
||||
return r.intersected(limits);
|
||||
}
|
||||
|
||||
QPoint AbstractTwoPointTool::adjustedVector(QPoint v) const {
|
||||
if (m_supportsOrthogonalAdj && m_supportsDiagonalAdj) {
|
||||
int dir = ( static_cast<int>(round(atan2(-v.y(), v.x()) / ADJ_UNIT)) + DIRS_NUMBER ) % DIRS_NUMBER;
|
||||
if (dir == UNIT::HORIZ_DIR) {
|
||||
v.setY(0);
|
||||
} else if (dir == UNIT::VERT_DIR) {
|
||||
v.setX(0);
|
||||
} else if (dir == UNIT::DIAG1_DIR) {
|
||||
int newX = (v.x() - v.y()) / 2;
|
||||
int newY = -newX;
|
||||
v.setX(newX);
|
||||
v.setY(newY);
|
||||
} else {
|
||||
int newX = (v.x() + v.y()) / 2;
|
||||
int newY = newX;
|
||||
v.setX(newX);
|
||||
v.setY(newY);
|
||||
}
|
||||
} else if (m_supportsDiagonalAdj) {
|
||||
int dir = ( static_cast<int>(round((atan2(-v.y(), v.x()) - ADJ_DIAG_UNIT / 2) / ADJ_DIAG_UNIT))
|
||||
+ DIAG_DIRS_NUMBER ) % DIAG_DIRS_NUMBER;
|
||||
if (dir == DIAG_UNIT::DIR1) {
|
||||
int newX = (v.x() - v.y()) / 2;
|
||||
int newY = -newX;
|
||||
v.setX(newX);
|
||||
v.setY(newY);
|
||||
} else {
|
||||
int newX = (v.x() + v.y()) / 2;
|
||||
int newY = newX;
|
||||
v.setX(newX);
|
||||
v.setY(newY);
|
||||
}
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user