| 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 |
from random import normalvariate |
| 26 |
|
| 27 |
from .objet import Objet_avec_coordonnees_modifiables, Argument, Ref |
| 28 |
from .textes import Texte_editable_generique, Texte |
| 29 |
from ..pylib import print_error |
| 30 |
from .. import param |
| 31 |
|
| 32 |
|
| 33 |
class Bouton(Texte_editable_generique, Objet_avec_coordonnees_modifiables): |
| 34 |
"""Un bouton cliquable. |
| 35 |
|
| 36 |
Un bouton avec texte. Typiquement, on lui associe une action |
| 37 |
lorsque l'on clique dessus, via la méthode `onLeftClick`. |
| 38 |
""" |
| 39 |
|
| 40 |
_style_defaut = param.boutons |
| 41 |
|
| 42 |
texte = __texte = Argument("str") |
| 43 |
abscisse = x = __x = Argument("Variable_generique", defaut=0) |
| 44 |
ordonnee = y = __y = Argument("Variable_generique", defaut=0) |
| 45 |
|
| 46 |
def __init__(self, texte=' ', x=None, y=None, **styles): |
| 47 |
x, y, styles = self._recuperer_x_y(x, y, styles) |
| 48 |
texte = str(texte) |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
self.__texte = texte = Ref(texte) |
| 54 |
self.__x = x = Ref(x) |
| 55 |
self.__y = y = Ref(y) |
| 56 |
|
| 57 |
Objet_avec_coordonnees_modifiables.__init__(self, x, y, **styles) |
| 58 |
Texte_editable_generique(self, texte, **styles) |
| 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 |
def _creer_figure(self): |
| 86 |
x, y = self.coordonnees |
| 87 |
if not self._representation: |
| 88 |
self._representation = [self.rendu.texte(), self.rendu.rectangle()] |
| 89 |
Texte_editable_generique._creer_figure(self) |
| 90 |
text, rect = self._representation |
| 91 |
rect.set_visible(True) |
| 92 |
box = self.canvas.txt_box(text) |
| 93 |
w, h = self.feuille.dpix2coo(box.width, -box.height) |
| 94 |
niveau = self.style("niveau") |
| 95 |
marge = self.style("marge") |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
mx, my = self.feuille.dpix2coo(marge, -marge) |
| 105 |
rect.set_width(w + 2*mx) |
| 106 |
rect.set_height(h + 2*my) |
| 107 |
rect.set_x(self.x - .5*w - mx) |
| 108 |
rect.set_y(self.y - .5*h - my) |
| 109 |
fond = self.style('couleur_fond') |
| 110 |
rect.set(facecolor=fond, edgecolor=self._foncer(fond)) |
| 111 |
rect.zorder = niveau |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
def _boite(self): |
| 122 |
. |
| 123 |
can = self.canvas |
| 124 |
l, h = can.dimensions |
| 125 |
box = can.txt_box(self.figure[1]) |
| 126 |
xmin = box.xmin |
| 127 |
ymax = h - box.ymin |
| 128 |
xmax = box.xmax |
| 129 |
ymin = h - box.ymax |
| 130 |
return xmin, xmax, ymin, ymax |
| 131 |
|
| 132 |
def _distance_inf(self, x, y, d): |
| 133 |
|
| 134 |
return Texte_editable_generique._distance_inf(self, x, y, 0) |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
def _en_gras(self, booleen): |
| 142 |
fond = self.style('couleur_fond') |
| 143 |
if booleen: |
| 144 |
self._representation[1].set_facecolor(self._claircir(fond)) |
| 145 |
else: |
| 146 |
self._representation[1].set_facecolor(fond) |
| 147 |
|
| 148 |
@staticmethod |
| 149 |
def _claircir(couleur): |
| 150 |
r, g, b = couleur |
| 151 |
r = 1 - .5*(1 - r) |
| 152 |
g = 1 - .5*(1 - g) |
| 153 |
b = 1 - .5*(1 - b) |
| 154 |
return r, g, b |
| 155 |
|
| 156 |
@staticmethod |
| 157 |
def _foncer(couleur): |
| 158 |
r, g, b = couleur |
| 159 |
r = .75*r |
| 160 |
g = .75*g |
| 161 |
b = .75*b |
| 162 |
return r, g, b |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
class Champ(Texte): |
| 167 |
"""Un champ de texte. |
| 168 |
|
| 169 |
Un champ de texte éditable en double-cliquant dessus. |
| 170 |
|
| 171 |
Le champ peut être encadré par du texte, via les mots-clefs `prefixe` |
| 172 |
(texte à gauche) et `suffixe` (texte à droite). |
| 173 |
On peut également lui associer un résultat attendu, |
| 174 |
via le mot-clef `attendu`:: |
| 175 |
|
| 176 |
>>> from wxgeometrie import Champ |
| 177 |
>>> c = Champ('modifiez moi', 10, 5, prefixe="1+1=", attendu="2", |
| 178 |
... formatage='rien', suffixe=" (entrer le resultat)") |
| 179 |
>>> c.style("attendu") |
| 180 |
'2' |
| 181 |
>>> c.texte = '?' |
| 182 |
>>> print(c.label()) |
| 183 |
1+1=? (entrer le resultat) |
| 184 |
|
| 185 |
Si un résultat est attendu, alors le texte rentré par l'utilisateur passera |
| 186 |
un test de validation. Si le résultat passe le test, un check vert est |
| 187 |
affiché à côté, sinon, un smiley :-( rouge est affiché. |
| 188 |
|
| 189 |
Par défaut, pour être validé, il faut que le résultat soit exactement |
| 190 |
conforme au texte attendu:: |
| 191 |
|
| 192 |
>>> c.texte = "2.0" |
| 193 |
>>> c.correct |
| 194 |
False |
| 195 |
>>> c.texte = "2" |
| 196 |
>>> c.correct |
| 197 |
True |
| 198 |
|
| 199 |
Cependant, il est possible de définir un test de validation personnalisé:: |
| 200 |
|
| 201 |
>>> def test(reponse, attendu): |
| 202 |
... return float(reponse) == float(attendu) |
| 203 |
>>> c.valider = test |
| 204 |
>>> c.texte = "2.0" |
| 205 |
>>> c.correct |
| 206 |
True |
| 207 |
|
| 208 |
On peut aussi associer une action à la validation:: |
| 209 |
|
| 210 |
>>> def action(**kw): |
| 211 |
... if kw['correct']: |
| 212 |
... if not kw['correct_old']: |
| 213 |
... # Ce n'était pas correct avant |
| 214 |
... print('Bravo !') |
| 215 |
... else: |
| 216 |
... # C'était déjà correct |
| 217 |
... print("Oui, c'est bon aussi !") |
| 218 |
... else: |
| 219 |
... print('Essaie encore...') |
| 220 |
>>> c.evt_valider = action |
| 221 |
>>> c.texte = "3" |
| 222 |
Essaie encore... |
| 223 |
>>> c.texte = "2" |
| 224 |
Bravo ! |
| 225 |
>>> c.texte = "2.0" |
| 226 |
Oui, c'est bon aussi ! |
| 227 |
|
| 228 |
Cet action sera effectuée lorsque le texte du champ est modifié, si |
| 229 |
celui-ci passe le test de validation. |
| 230 |
|
| 231 |
Enfin, une liste des propositions peut être passée via le mot-clef `choix`:: |
| 232 |
|
| 233 |
>>> c = Champ('', 10, 5, choix=['oui', 'non', 'sans opinion']) |
| 234 |
>>> c.style('choix') |
| 235 |
['oui', 'non', 'sans opinion'] |
| 236 |
""" |
| 237 |
|
| 238 |
_style_defaut = param.champs |
| 239 |
|
| 240 |
def _test_correct(self, txt): |
| 241 |
attendu = self.style('attendu') |
| 242 |
if attendu is None: |
| 243 |
return |
| 244 |
if self.valider is None: |
| 245 |
t |
| 246 |
return (txt == attendu) |
| 247 |
else: |
| 248 |
ée |
| 249 |
return self.valider(txt, attendu) |
| 250 |
|
| 251 |
def _set_texte(self, value): |
| 252 |
value = Texte._set_texte(self, value) |
| 253 |
if self._initialise: |
| 254 |
nt |
| 255 |
le |
| 256 |
) |
| 257 |
txt = self.texte |
| 258 |
if value != txt: |
| 259 |
. |
| 260 |
et |
| 261 |
n. |
| 262 |
self._correct_old = self.correct |
| 263 |
self.correct = self._test_correct(value) |
| 264 |
if getattr(self, 'evt_valider', None) is not None: |
| 265 |
self.evt_valider(champ=self, correct=self.correct, |
| 266 |
correct_old=self._correct_old) |
| 267 |
return value |
| 268 |
|
| 269 |
|
| 270 |
texte = __texte = Argument("str", Texte._get_texte, _set_texte) |
| 271 |
abscisse = x = __x = Argument("Variable_generique", defaut = lambda: normalvariate(0,10)) |
| 272 |
ordonnee = y = __y = Argument("Variable_generique", defaut = lambda: normalvariate(0,10)) |
| 273 |
|
| 274 |
t. |
| 275 |
valider = None |
| 276 |
ès |
| 277 |
|
| 278 |
evt_valider = None |
| 279 |
|
| 280 |
correct = None |
| 281 |
|
| 282 |
def __init__(self, texte="", x=None, y=None, **styles): |
| 283 |
self.__texte = texte = Ref(texte) |
| 284 |
self.__x = x = Ref(x) |
| 285 |
self.__y = y = Ref(y) |
| 286 |
|
| 287 |
Texte.__init__(self, texte, x, y, **styles) |
| 288 |
|
| 289 |
|
| 290 |
i |
| 291 |
est |
| 292 |
. |
| 293 |
self._correct_old = self.correct = self._test_correct(self.texte) |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
def label(self, *args, **kw): |
| 298 |
txt = Texte.label(self, *args, **kw) |
| 299 |
if txt is None: |
| 300 |
return |
| 301 |
if not txt.strip('$\n'): |
| 302 |
txt = '...' |
| 303 |
prefixe = self.style('prefixe') |
| 304 |
suffixe = self.style('suffixe') |
| 305 |
return (prefixe or '') + txt + (suffixe or '') |
| 306 |
|
| 307 |
|
| 308 |
def style(self, *args, **kw): |
| 309 |
label_change = ('label' in kw and kw['label'] != self._style['label']) |
| 310 |
val = Texte.style(self, *args, **kw) |
| 311 |
if label_change: |
| 312 |
self._correct_old = self.correct |
| 313 |
self.correct = self._test_correct() |
| 314 |
if getattr(self, 'evt_valider', None) is not None: |
| 315 |
self.evt_valider(champ=self, correct=self.correct, |
| 316 |
correct_old=self._correct_old) |
| 317 |
return val |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
def _creer_figure(self): |
| 322 |
|
| 323 |
if not self._representation: |
| 324 |
self._representation = [self.rendu.texte(), self.rendu.decoration_texte(), self.rendu.texte()] |
| 325 |
Texte._creer_figure(self) |
| 326 |
can = self.canvas |
| 327 |
box = can.txt_box(self._representation[0]) |
| 328 |
px, py = box.max |
| 329 |
x, y = self.feuille.pix2coo(px + 5, can.hauteur - py) |
| 330 |
txt = self._representation[2] |
| 331 |
attendu = self.style('attendu') |
| 332 |
if attendu is None or not self.texte.strip(): |
| 333 |
txt.set(visible=False) |
| 334 |
else: |
| 335 |
txt.set(visible=True, x=x, y=y, va='top', ha='left') |
| 336 |
if self.correct: |
| 337 |
txt.set(text='\u2713', color='g') |
| 338 |
else: |
| 339 |
txt.set(text='\u2639', color='r') |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|