-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy patheyedropPreview.cpp
More file actions
106 lines (83 loc) · 3.63 KB
/
eyedropPreview.cpp
File metadata and controls
106 lines (83 loc) · 3.63 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "eyedropPreview.h"
#include <QPainter>
#include <QPainterPath>
#include <QGuiApplication>
#include <QScreen>
EyedropPreview::EyedropPreview( QQuickItem* parent )
: QQuickPaintedItem( parent )
, m_previewSize { 151 }
, m_previewMaxPixels { 5 }
, m_mouseX { 0 }
, m_mouseY { 0 }
, m_showPreview { true } {
setAcceptHoverEvents( true );
connect( this, &EyedropPreview::mousePositionChanged, this, &EyedropPreview::eyedrop );
}
void EyedropPreview::paint( QPainter* painter ) {
if ( !m_showPreview ) {
return;
}
const auto windowSize { QGuiApplication::screens().at( 0 )->availableSize() };
const bool flipX { m_mouseX > ( windowSize.width() - m_previewSize ) };
const bool flipY { m_mouseY > ( windowSize.height() - m_previewSize ) };
const double sizeHalf = ( m_previewMaxPixels - 1 ) / 2.0;
const double sizePreviewHalf = ( m_previewSize - 1 ) / 2.0;
const auto pixmap
= QGuiApplication::primaryScreen()->grabWindow( 0, static_cast<int>( m_mouseX - sizeHalf ), static_cast<int>( m_mouseY - sizeHalf ),
static_cast<int>( m_previewMaxPixels ), static_cast<int>( m_previewMaxPixels ) );
painter->setRenderHints( QPainter::Antialiasing, true );
painter->setPen( QPen( QBrush( Qt::black ), 4.0, Qt::SolidLine ) );
const QPointF coord( flipX ? m_mouseX - m_previewMaxPixels - m_previewSize : m_mouseX + m_previewMaxPixels,
flipY ? m_mouseY - m_previewMaxPixels - m_previewSize : m_mouseY + m_previewMaxPixels );
QPainterPath path;
path.addEllipse( coord.x(), coord.y(), m_previewSize, m_previewSize );
painter->setClipPath( path );
painter->drawPixmap( static_cast<int>( coord.x() ), static_cast<int>( coord.y() ), static_cast<int>( m_previewSize ),
static_cast<int>( m_previewSize ), pixmap );
painter->drawEllipse( static_cast<int>( coord.x() ), static_cast<int>( coord.y() ), static_cast<int>( m_previewSize ),
static_cast<int>( m_previewSize ) );
// Center Point
const auto fSize = m_previewSize / m_previewMaxPixels;
painter->setPen( Qt::white );
painter->drawRect( static_cast<int>( coord.x() + sizePreviewHalf - ( fSize / 2 ) ),
static_cast<int>( coord.y() + sizePreviewHalf - ( fSize / 2 ) ), static_cast<int>( fSize ), static_cast<int>( fSize ) );
}
qreal EyedropPreview::previewSize() const {
return m_previewSize;
}
void EyedropPreview::setPreviewSize( const qreal newPreviewSize ) {
if ( qFuzzyCompare( m_previewSize, newPreviewSize ) ) {
return;
}
const auto tmp { 2 * ( ( int )( newPreviewSize / 2.0F ) ) + 1 };
m_previewSize = tmp;
Q_EMIT previewSizeChanged();
}
void EyedropPreview::setPreviewMaxPixels( const qreal newSize ) {
if ( qFuzzyCompare( m_previewMaxPixels, newSize ) ) {
return;
}
// nearest odd
const auto tmp { 2 * ( ( int )( newSize / 2.0F ) ) + 1 };
m_previewMaxPixels = std::clamp( tmp, 3, 15 );
Q_EMIT sizeChanged();
}
void EyedropPreview::startPicking() {
Q_EMIT pickingStarted();
m_oldColor = m_color;
}
void EyedropPreview::cancelPicking() {
m_color = m_oldColor;
Q_EMIT colorChanged();
Q_EMIT pickingFinished();
}
void EyedropPreview::eyedrop() {
const auto pixmap = QGuiApplication::primaryScreen()->grabWindow( 0, static_cast<int>( m_mouseX ), static_cast<int>( m_mouseY ), 1, 1 );
const QImage img { pixmap.toImage() };
m_color = QColor( img.pixel( 0, 0 ) );
Q_EMIT colorChanged();
update();
}
QColor EyedropPreview::color() const noexcept {
return m_color;
}