Skip to content

Commit f80aa24

Browse files
committed
Merge pull request #6 from buttleofx/ScreenPicker
Screen picker
2 parents 1bced24 + 8a80dcd commit f80aa24

File tree

8 files changed

+180
-1
lines changed

8 files changed

+180
-1
lines changed

example/screenPicker/main.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import sys
2+
from PyQt5 import QtCore, QtWidgets, QtQuick
3+
#Fix execution shader bug
4+
from OpenGL import GL
5+
6+
7+
import os
8+
9+
currentFilePath = os.path.dirname(os.path.abspath(__file__))
10+
quickmambaPath = os.path.join(currentFilePath, '../QuickMamba')
11+
sys.path.append(quickmambaPath)
12+
13+
import quickmamba
14+
15+
if __name__ == '__main__':
16+
quickmamba.qmlRegister()
17+
app = QtWidgets.QApplication(sys.argv)
18+
view = QtQuick.QQuickView()
19+
view.setResizeMode(QtQuick.QQuickView.SizeRootObjectToView)
20+
# Create a declarative view
21+
view.setSource(QtCore.QUrl("source.qml"))
22+
23+
view.show()
24+
app.exec_()

example/screenPicker/source.qml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import QtQuick 2.0
2+
import QtQuick.Controls 1.1
3+
import "../../qml/QuickMamba/." // Qt-BUG import qmldir
4+
import QtQuick.Layouts 1.1
5+
6+
Rectangle
7+
{
8+
id: root
9+
color: "black"
10+
width: 300
11+
height: 250
12+
13+
RowLayout {
14+
anchors.centerIn: parent
15+
spacing: 20
16+
17+
ScreenPicker {
18+
id:screenPicker
19+
20+
onGrabbedColor: colorPreview.color = color
21+
}
22+
23+
Rectangle {
24+
id:colorPreview
25+
color:"#00b2a1"
26+
27+
radius: 3
28+
Layout.minimumWidth: 80
29+
Layout.minimumHeight: 80
30+
}
31+
}
32+
}

qml/QuickMamba/ScreenPicker.qml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import QtQuick 2.0
2+
import QuickMamba 1.0
3+
4+
Rectangle
5+
{
6+
id:root
7+
property string pickerImg: "assets/img/screenpicker/screenPicker.png"
8+
property string pickerImgHover: "assets/img/screenpicker/screenPickerHover.png"
9+
// Default style
10+
color: "#212121"
11+
border.width: 2
12+
border.color: "#333"
13+
radius: 3
14+
height: pickerImg.height * 2
15+
width: pickerImg.width * 2
16+
17+
signal accepted
18+
signal grabbedColor(var color)
19+
20+
Image {
21+
id: pickerImg
22+
source: root.pickerImg
23+
anchors.centerIn: parent
24+
}
25+
26+
MouseArea {
27+
anchors.fill: root
28+
hoverEnabled: true
29+
30+
onEntered: pickerImg.source = root.pickerImgHover
31+
onExited: pickerImg.source = root.pickerImg
32+
33+
onPressed: screenPicker.grabbing = true
34+
cursorShape: Qt.OpenHandCursor
35+
}
36+
37+
ColorPicker {
38+
id: screenPicker
39+
40+
onAccepted: root.accepted()
41+
onCurrentColorChanged: root.grabbedColor(currentColor)
42+
}
43+
}
970 Bytes
Loading
1.25 KB
Loading

qml/QuickMamba/qmldir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ WheelArea 1.0 WheelArea.qml
55
ExternDropArea 1.0 ExternDropArea.qml
66
QuickEditableNumberInput 1.0 QuickEditableNumberInput.qml
77
BlinkCursor 1.0 BlinkCursor.qml
8+
ScreenPicker 1.0 ScreenPicker.qml

quickmamba/gui/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
from .wheelArea import WheelArea
22
from .externDropArea import ExternDropArea
33
from .colorExtended import ColorExtended
4+
from .colorPicker import ColorPicker
45

56
from PyQt5 import QtQml
67

78
def qmlRegister():
89
QtQml.qmlRegisterType(WheelArea, "QuickMamba", 1, 0, "WheelAreaImpl")
910
QtQml.qmlRegisterType(ExternDropArea, "QuickMamba", 1, 0, "ExternDropAreaImpl")
1011
QtQml.qmlRegisterType(ColorExtended, "QuickMamba", 1, 0, "ColorExtended")
11-
12+
QtQml.qmlRegisterType(ColorPicker, "QuickMamba", 1, 0, "ColorPicker")
1213

quickmamba/gui/colorPicker.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
from PyQt5 import QtCore
2+
from PyQt5 import QtWidgets
3+
from PyQt5 import QtQuick
4+
from PyQt5.QtGui import QCursor, QColor, QGuiApplication
5+
6+
class ColorPickingEventFilter(QtCore.QObject):
7+
def __init__(self, screenpicker):
8+
QtCore.QObject.__init__(self, screenpicker)
9+
self._screenpicker = screenpicker
10+
11+
def eventFilter(self, obj, event):
12+
if(event.type() == QtCore.QEvent.MouseMove):
13+
self._screenpicker.updateCurrentColor()
14+
elif(event.type() == QtCore.QEvent.MouseButtonRelease):
15+
self._screenpicker.setGrabbing(False)
16+
self._screenpicker.accepted.emit()
17+
18+
return False
19+
20+
21+
class ColorPicker(QtQuick.QQuickItem):
22+
"""
23+
Define the common methods and fields for screenPicker.
24+
"""
25+
26+
def __init__(self, parent = None):
27+
QtQuick.QQuickItem.__init__(self, parent)
28+
self._currentColor = QColor("#FFFFFF")
29+
self._grabbing = False
30+
self._desktop = QtWidgets.QDesktopWidget()
31+
self._colorPickingEventFilter = ColorPickingEventFilter(self)
32+
33+
# ######################################## Methods private to this class ####################################### #
34+
35+
# ## Getters ## #
36+
37+
def getCurrentColor(self):
38+
return self._currentColor
39+
40+
def setCurrentColor(self, currentColor):
41+
self._currentColor = currentColor
42+
self.currentColorChanged.emit()
43+
44+
def isGrabbing(self):
45+
return self._grabbing
46+
47+
def setGrabbing(self, grabbing):
48+
if(self._grabbing != grabbing):
49+
self._grabbing = grabbing
50+
if(self._grabbing):
51+
self.installEventFilter(self._colorPickingEventFilter)
52+
QGuiApplication.setOverrideCursor(QCursor(QtCore.Qt.CrossCursor))
53+
self.grabMouse()
54+
else:
55+
self.ungrabMouse()
56+
self.removeEventFilter(self._colorPickingEventFilter)
57+
QGuiApplication.restoreOverrideCursor()
58+
self.grabbingChanged.emit()
59+
60+
# ## Others ## #
61+
62+
def updateCurrentColor(self):
63+
cursorPos = QCursor.pos()
64+
# Catch the pixel pointed by the mouse on a pixmap
65+
pixmap = QGuiApplication.screens()[self._desktop.screenNumber()].grabWindow(self._desktop.winId(), cursorPos.x(), cursorPos.y(), 1, 1)
66+
qImage = pixmap.toImage()
67+
qColor = QColor(qImage.pixel(0, 0))
68+
self.setCurrentColor(qColor)
69+
70+
# ############################################# Data exposed to QML ############################################## #
71+
72+
accepted = QtCore.pyqtSignal()
73+
74+
currentColorChanged = QtCore.pyqtSignal()
75+
currentColor = QtCore.pyqtProperty(QColor, getCurrentColor, setCurrentColor, notify=currentColorChanged)
76+
77+
grabbingChanged = QtCore.pyqtSignal()
78+
grabbing = QtCore.pyqtProperty(bool, isGrabbing, setGrabbing, notify=grabbingChanged)

0 commit comments

Comments
 (0)