1#!/usr/local/bin/python3.8 2# 3# Electron Cash - lightweight Bitcoin client 4# Copyright (C) 2019 Axel Gembe <derago@gmail.com> 5# 6# Permission is hereby granted, free of charge, to any person 7# obtaining a copy of this software and associated documentation files 8# (the "Software"), to deal in the Software without restriction, 9# including without limitation the rights to use, copy, modify, merge, 10# publish, distribute, sublicense, and/or sell copies of the Software, 11# and to permit persons to whom the Software is furnished to do so, 12# subject to the following conditions: 13# 14# The above copyright notice and this permission notice shall be 15# included in all copies or substantial portions of the Software. 16# 17# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24# SOFTWARE. 25 26from typing import List, Dict, Callable, Any 27from abc import ABC, abstractmethod 28 29from PyQt5.QtGui import QColor 30from PyQt5.QtCore import Qt 31 32from electrum.i18n import _ 33from electrum.qrreader import QrCodeResult 34 35from electrum.gui.qt.util import ColorScheme, QColorLerp 36 37 38class QrReaderValidatorResult(): 39 """ 40 Result of a QR code validator 41 """ 42 43 def __init__(self): 44 self.accepted: bool = False 45 46 self.message: str = None 47 self.message_color: QColor = None 48 49 self.simple_result : str = None 50 51 self.result_usable: Dict[QrCodeResult, bool] = {} 52 self.result_colors: Dict[QrCodeResult, QColor] = {} 53 self.result_messages: Dict[QrCodeResult, str] = {} 54 55 self.selected_results: List[QrCodeResult] = [] 56 57 58class AbstractQrReaderValidator(ABC): 59 """ 60 Abstract base class for QR code result validators. 61 """ 62 63 @abstractmethod 64 def validate_results(self, results: List[QrCodeResult]) -> QrReaderValidatorResult: 65 """ 66 Checks a list of QR code results for usable codes. 67 """ 68 69class QrReaderValidatorCounting(AbstractQrReaderValidator): 70 """ 71 This QR code result validator doesn't directly accept any results but maintains a dictionary 72 of detection counts in `result_counts`. 73 """ 74 75 result_counts: Dict[QrCodeResult, int] = {} 76 77 def validate_results(self, results: List[QrCodeResult]) -> QrReaderValidatorResult: 78 res = QrReaderValidatorResult() 79 80 for result in results: 81 # Increment the detection count 82 if not result in self.result_counts: 83 self.result_counts[result] = 0 84 self.result_counts[result] += 1 85 86 # Search for missing results, iterate over a copy because the loop might modify the dict 87 for result in self.result_counts.copy(): 88 # Count down missing results 89 if result in results: 90 continue 91 self.result_counts[result] -= 2 92 # When the count goes to zero, remove 93 if self.result_counts[result] < 1: 94 del self.result_counts[result] 95 96 return res 97 98class QrReaderValidatorColorizing(QrReaderValidatorCounting): 99 """ 100 This QR code result validator doesn't directly accept any results but colorizes the results 101 based on the counts maintained by `QrReaderValidatorCounting`. 102 """ 103 104 WEAK_COLOR: QColor = QColor(Qt.red) 105 STRONG_COLOR: QColor = QColor(Qt.green) 106 107 strong_count: int = 10 108 109 def validate_results(self, results: List[QrCodeResult]) -> QrReaderValidatorResult: 110 res = super().validate_results(results) 111 112 # Colorize the QR code results by their detection counts 113 for result in results: 114 # Enforce strong_count as upper limit 115 self.result_counts[result] = min(self.result_counts[result], self.strong_count) 116 117 # Interpolate between WEAK_COLOR and STRONG_COLOR based on count / strong_count 118 lerp_factor = (self.result_counts[result] - 1) / self.strong_count 119 lerped_color = QColorLerp(self.WEAK_COLOR, self.STRONG_COLOR, lerp_factor) 120 res.result_colors[result] = lerped_color 121 122 return res 123 124class QrReaderValidatorStrong(QrReaderValidatorColorizing): 125 """ 126 This QR code result validator doesn't directly accept any results but passes every strong 127 detection in the return values `selected_results`. 128 """ 129 130 def validate_results(self, results: List[QrCodeResult]) -> QrReaderValidatorResult: 131 res = super().validate_results(results) 132 133 for result in results: 134 if self.result_counts[result] >= self.strong_count: 135 res.selected_results.append(result) 136 break 137 138 return res 139 140class QrReaderValidatorCounted(QrReaderValidatorStrong): 141 """ 142 This QR code result validator accepts a result as soon as there is at least `minimum` and at 143 most `maximum` QR code(s) with strong detection. 144 """ 145 146 def __init__(self, minimum: int = 1, maximum: int = 1): 147 super().__init__() 148 self.minimum = minimum 149 self.maximum = maximum 150 151 def validate_results(self, results: List[QrCodeResult]) -> QrReaderValidatorResult: 152 res = super().validate_results(results) 153 154 num_results = len(res.selected_results) 155 if num_results < self.minimum: 156 if num_results > 0: 157 res.message = _('Too few QR codes detected.') 158 res.message_color = ColorScheme.RED.as_color() 159 elif num_results > self.maximum: 160 res.message = _('Too many QR codes detected.') 161 res.message_color = ColorScheme.RED.as_color() 162 else: 163 res.accepted = True 164 res.simple_result = (results and results[0].data) or '' # hack added by calin just to take the first one 165 166 return res 167