From 8851f7f2f20e53515908370ce03ed5dc7b117ff9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andr=C3=A9=20Gl=C3=BCpker?= <git@wgmd.de>
Date: Tue, 22 Mar 2016 16:37:29 +0100
Subject: First prototype of clickable images (find all trees)

Only works on one of those. (The one with the black borders)
---
 9kwpyqt.py | 40 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 38 insertions(+), 2 deletions(-)

diff --git a/9kwpyqt.py b/9kwpyqt.py
index d92398d..d676013 100755
--- a/9kwpyqt.py
+++ b/9kwpyqt.py
@@ -2,7 +2,7 @@
 
 import sys
 import time
-from PyQt5.QtCore import Qt, QTimer, QUrl
+from PyQt5.QtCore import Qt, QTimer, QUrl, pyqtSignal
 from PyQt5.QtWidgets import QApplication, QWidget
 from PyQt5.QtWidgets import QLayout, QHBoxLayout, QVBoxLayout, QSizePolicy
 from PyQt5.QtWidgets import QLabel, QLineEdit, QPushButton, QProgressBar
@@ -14,6 +14,35 @@ API_URL = "http://www.9kw.eu/index.cgi"
 API_KEY = ""
 soundfile = "notify.wav"
 
+class ExtendedQLabel(QLabel):
+
+    clicked = pyqtSignal(int)
+
+    def __init(self, parent):
+        QLabel.__init(self, parent)
+
+    def mouseReleaseEvent(self, ev):
+        if not self.pixmap():
+            return
+
+        # get size of pixmap
+        imgx = self.pixmap().size().width()
+        imgy = self.pixmap().size().height()
+
+        # get cursor size relative to pixmap
+        x = ev.pos().x() - self.size().width() / 2 + imgx/2
+        y = ev.pos().y() - self.size().height() / 2 + imgy/2
+
+        # check if on pixmap (upper 19% = info)
+        if x > imgx or y > imgy or x < 0 or y < 0.19*imgy:
+            print("Not on image")
+            return
+
+        # which piece is below the mouse?
+        xpiece = x // (imgx / 3) + 1
+        ypiece = (y-0.19*imgy) // (imgy*0.81 / 3)
+
+        self.clicked.emit(3*ypiece+xpiece)
 
 class CaptchaGUI(QWidget):
     sound = False # QSound()
@@ -56,13 +85,14 @@ class CaptchaGUI(QWidget):
         self.LayoutStats.sizeConstraint = QLayout.SetMinimumSize
 
         # Middle: Captchaimage
-        self.captchaImage = QLabel("")
+        self.captchaImage = ExtendedQLabel("")
         self.captchaImage.setAlignment(Qt.AlignCenter)
         self.captchaImage.setMinimumHeight(400)
         self.captchaImage.setSizePolicy(
             QSizePolicy.MinimumExpanding,
             QSizePolicy.MinimumExpanding
         )
+        self.captchaImage.clicked.connect(self.captchaClicked)
 
         self.captchaBox = QHBoxLayout()
         self.captchaBox.addWidget(self.captchaImage)
@@ -129,6 +159,12 @@ class CaptchaGUI(QWidget):
                                             Qt.KeepAspectRatio)
             self.captchaImage.setPixmap(self.imageR)
 
+    def captchaClicked(self, clickedpiece):
+        if self.image:
+            text = self.captchaInputLine.text()
+            text += str(clickedpiece)
+            self.captchaInputLine.setText(text)
+
     def updateStats(self):
         if self.startCredits:
             credittext = "%d ¥ (%s%d)" % (self.currentCredits, (self.currentCredits - self.startCredits > 0) and '+' or '', (self.currentCredits - self.startCredits))
-- 
cgit v1.2.3