Debian Codegraph @

wxgeometrie/GUI/reperage.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2
3 ##--------------------------------------##
4 # WxGeometrie #
5 ##--------------------------------------##
6 # WxGeometrie
7 # Dynamic geometry, graph plotter, and more for french mathematic teachers.
8 # Copyright (C) 2005-2013 Nicolas Pourcelot
9 #
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23
24 from PyQt5.QtWidgets import QDialog, QDialogButtonBox
25
26 from .reperage_ui import Ui_DialogReperage
27 from .calibrage_ui import Ui_DialogCalibrage
28
29 from .. import param
30
31
32
33
34 class DialogCalibrage(QDialog, Ui_DialogCalibrage):
35 def __init__(self, parent):
36 QDialog.__init__(self, parent)
37 # Set up the user interface from Designer.
38 self.setupUi(self)
39 self.parent = parent
40
41 def accept(self):
42 longueur = float(self.longueur.text().replace(param.separateur_decimal, '.'))
43 # La barre étalon mesure 300 pixels.
44 # 1 inch = 2,54 cm
45 self.parent.resolution.setText(str(2.54*300/longueur).replace('.', param.separateur_decimal))
46 self.close()
47
48
49
50 class DialogReperage(QDialog, Ui_DialogReperage):
51 def __init__(self, parent):
52 QDialog.__init__(self, parent)
53 # Set up the user interface from Designer.
54 self.setupUi(self)
55 self.canvas = canvas = parent
56 gradu = canvas.gradu
57 repere = canvas.repere
58 # Dimensions en pixels.
59 width, height = canvas.dimensions
60 # Fenêtre, en coordonnées cette fois.
61 xmin, xmax, ymin, ymax = canvas.fenetre
62 # Résolution de l'écran (pixels per inch)
63 dpi = canvas.dpi_ecran
64 # Mesure en pixels d'une unité
65 pixels_per_unit_x = width/(xmax - xmin)
66 pixels_per_unit_y = height/(ymax - ymin)
67 # Mesure en cm d'une unité
68 # 1 inch = 2.54 cm
69 cm_per_unit_x = 2.54*pixels_per_unit_x/dpi
70 cm_per_unit_y = 2.54*pixels_per_unit_y/dpi
71
72 # Valeurs initiales des champs de texte.
73 self.remplir_champs(repere_O=repere[0],
74 repere_I=repere[1],
75 repere_J=repere[2],
76 gradu_x=gradu[0],
77 gradu_y=gradu[1],
78 echelle_x=cm_per_unit_x,
79 echelle_y=cm_per_unit_y,
80 resolution=dpi,
81 )
82
83 self.rapport.setChecked(canvas.ratio is not None)
84
85 self.boutons.button(QDialogButtonBox.Reset).clicked.connect(self.restaurer)
86 self.boutons.button(QDialogButtonBox.Apply).clicked.connect(self.appliquer)
87 self.calibrage.clicked.connect(self.calibrer)
88
89
90 def remplir_champs(self, **champs):
91 for nom, valeur in champs.items():
92 string = (str(valeur) if not isinstance(valeur, str) else valeur)
93 if isinstance(valeur, float):
94 if string.endswith('.0'):
95 string = string[:-2]
96 string = string.replace('.', param.separateur_decimal)
97 getattr(self, nom).setText(string)
98
99
100 def accept(self):
101 self.appliquer()
102 self.close()
103
104 def appliquer(self):
105 canvas = self.canvas
106
107 def str2num(string):
108 string = string.replace(param.separateur_decimal, '.').strip()
109 if string.endswith('.0'):
110 string = string[:-2]
111 return (float(string) if '.' in string else int(string))
112
113 repere = (self.repere_O.text().strip(),
114 self.repere_I.text().strip(),
115 self.repere_J.text().strip(),
116 )
117 gradu = (str2num(self.gradu_x.text()),
118 str2num(self.gradu_y.text()),
119 )
120
121 canvas.dpi_ecran = dpi = str2num(self.resolution.text())
122
123 # Nouvelle fenêtre d'affichage.
124 # À partir de l'échelle en abscisse et en ordonnée, on détermine
125 # quelles dimensions (en coordonnées) doit avoir la fenêtre d'affichage,
126 # et donc les coefficients d'agrandissement en abscisse et en ordonnée.
127 # Reste à choisir le point fixe de la transformation, par exemple le centre
128 # de la fenêtre précédente.
129
130 # Centre de la fenêtre précédente.
131 xmin, xmax, ymin, ymax = canvas.fenetre
132 x_centre = (xmin + xmax)/2
133 y_centre = (ymin + ymax)/2
134
135 cm_per_unit_x = str2num(self.echelle_x.text())
136 cm_per_unit_y = str2num(self.echelle_y.text())
137 dpcm = dpi/2.54
138 pixels_per_unit_x = cm_per_unit_x*dpcm
139 pixels_per_unit_y = cm_per_unit_y*dpcm
140 # Dimensions en pixels
141 width, height = canvas.dimensions
142 # Dimensions en unités (coordonnées) divisées par 2.
143 demi_largeur = width/(2*pixels_per_unit_x)
144 demi_hauteur = height/(2*pixels_per_unit_y)
145 fenetre = (x_centre - demi_largeur,
146 x_centre + demi_largeur,
147 y_centre - demi_hauteur,
148 y_centre + demi_hauteur,
149 )
150
151 ratio = (cm_per_unit_x/cm_per_unit_y if self.rapport.isChecked() else None)
152
153 commande = ("repere = %s\n"
154 "gradu = %s\n"
155 "fenetre = %s\n"
156 "ratio = %s"
157 ) % (repere, gradu, fenetre, ratio)
158
159 self.canvas.executer(commande)
160
161
162 def restaurer(self):
163 "Remet les valeurs par défaut dans les champs."
164 self.remplir_champs(repere_O=param.repere[0],
165 repere_I=param.repere[1],
166 repere_J=param.repere[2],
167 gradu_x=param.gradu[0],
168 gradu_y=param.gradu[1],
169 #~ echelle_x=cm_per_unit_x,
170 #~ echelle_y=cm_per_unit_y,
171 resolution=param.dpi_ecran,
172 )
173 self.rapport.setChecked(param.ratio is not None)
174
175
176 def calibrer(self):
177 DialogCalibrage(self).exec_()