Debian Codegraph @

wxgeometrie/GUI/ligne_commande.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2
3 ##--------------------------------------##
4 # Widget LigneCommande #
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.QtCore import Qt
25 from PyQt5.QtWidgets import QHBoxLayout, QWidget, QLineEdit, QLabel, QPushButton
26
27
28
29 class LigneCommande(QWidget):
30 """Un TextCtrl muni d'un historique et associé à un bouton pour valider.
31
32 On peut personnaliser le texte du bouton (via `texte="Mon texte"`),
33 ou même directement le bouton, en utilisant `bouton=...`.
34 """
35 def __init__(self, parent, longueur = 500, texte = None, bouton = None,
36 action = (lambda *args, **kw: True), afficher_bouton = True,
37 legende = None):
38 self.parent = parent
39 self.action = action
40 QWidget.__init__(self, parent)
41
42 sizer = QHBoxLayout()
43 self.texte = QLineEdit()
44 self.texte.setMinimumWidth(longueur)
45 if bouton is None:
46 self.bouton = QPushButton('OK' if texte is None else texte)
47 else:
48 self.bouton = bouton
49 self.bouton.setVisible(afficher_bouton)
50 self.bouton.clicked.connect(self.valider)
51
52 if legende is not None:
53 sizer.addWidget(QLabel(legende, self))
54 sizer.addWidget(self.texte)
55 sizer.addWidget(self.bouton)
56 self.setLayout(sizer)
57 self.initialiser()
58
59
60 def initialiser(self):
61 self.historique = []
62 self.position = None
63 self.setFocus()
64 self.clear()
65
66 def text(self):
67 return self.texte.text()
68
69 def setText(self, value):
70 self.texte.setText(value)
71
72 def setFocus(self):
73 self.texte.setFocus()
74
75 def clear(self):
76 self.texte.clear()
77
78 def getSelection(self):
79 if self.texte.hasSelectedText():
80 start = self.texte.selectionStart()
81 length = len(self.texte.selectedText())
82 end = start + length
83 else:
84 start = end = self.texte.cursorPosition()
85 return start, end
86
87 def cursorPosition(self):
88 return self.texte.cursorPosition()
89
90 def setCursorPosition(self, num):
91 return self.texte.setCursorPosition(num)
92
93 def insert(self, texte):
94 self.texte.insert(texte)
95
96 def setSelection(self, deb, fin):
97 self.texte.setSelection(deb, fin)
98
99 def setToolTip(self, tip):
100 self.texte.setToolTip(tip)
101
102 def valider(self, **kw):
103 # kw: shift|alt|meta|control=True|False'
104 commande = str(self.text())
105 self.position = None
106 if commande:
107 self.historique.append(commande)
108 elif self.historique:
109 # Appuyer une deuxième fois sur [Entrée] permet de répéter l'action précédente.
110 commande = self.historique[-1]
111 self.action(commande, **kw)
112
113
114 def keyPressEvent(self, event):
115 key = event.key()
116 commande = self.text()
117
118 if key == Qt.Key_Up and self.historique:
119 # On remonte dans l'historique (-> entrées plus anciennes)
120 if self.position is None:
121 # cas d'une commande en cours d'édition :
122 if commande:
123 if commande != self.historique[-1]:
124 # on enregistre la commande en cours
125 self.historique.append(commande)
126 self.position = len(self.historique) - 1
127 else:
128 self.position = len(self.historique)
129 if self.position > 0:
130 self.position -= 1
131 self.texte.setText(self.historique[self.position])
132
133 elif key == Qt.Key_Down and self.historique:
134 # On redescend dans l'historique (-> entrées plus récentes)
135 if self.position is None or self.position == len(self.historique) - 1:
136 if commande and commande != self.historique[-1]:
137 self.historique.append(commande)
138 self.texte.clear()
139 self.position = len(self.historique)
140 elif self.position < len(self.historique) - 1:
141 self.position += 1
142 self.texte.setText(self.historique[self.position])
143 elif key in (Qt.Key_Enter, Qt.Key_Return):
144 modifiers = event.modifiers()
145 kw = {'shift': modifiers & Qt.ShiftModifier,
146 'alt': modifiers & Qt.AltModifier,
147 'meta': modifiers & Qt.MetaModifier,
148 'control': modifiers & Qt.ControlModifier,
149 }
150 self.valider(**kw)
151 else:
152 self.position = None