-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathselection.cpp
56 lines (44 loc) · 1.21 KB
/
selection.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <QtCore>
#include <QtGui>
#include "selection.h"
#include "utils.h"
constexpr int SELECTION_MIN_WIDTH = 6;
constexpr int SELECTION_MIN_HEIGHT = 6;
Selection::Selection()
: m_hasSelection(false)
{}
bool Selection::selectionIsFine() const
{
const QRect &selection = m_selection.normalized();
return m_hasSelection && selection.width() > SELECTION_MIN_WIDTH && selection.height() > SELECTION_MIN_HEIGHT;
}
void Selection::reset()
{
m_hasSelection = false;
m_selection = QRect();
}
void Selection::start(const QPoint &pos)
{
if(!m_referenceRect.contains(pos))
return;
m_hasSelection = true;
m_selection = QRect(pos, pos);
qDebug() << "Starting selection" << m_selection;
}
void Selection::move(const QPoint &pos)
{
m_selection.setBottomRight(Utils::fitPoint(m_referenceRect, pos));
}
void Selection::draw(QPainter *painter)
{
if(!painter)
return;
// selection must be normalized and checked against a minimal size
const QRect &selection = m_selection.normalized();
if(selectionIsFine())
{
painter->setCompositionMode(QPainter::RasterOp_SourceXorDestination);
painter->setPen(Qt::white);
painter->drawRect(selection);
}
}