Debian Codegraph @

wxgeometrie/GUI/qtlib.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2
3 # WxGeometrie
4 # Dynamic geometry, graph plotter, and more for french mathematic teachers.
5 # Copyright (C) 2005-2013 Nicolas Pourcelot
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
21
22 import os
23 from PyQt5.QtCore import Qt, QThread, QEvent, pyqtSignal, QSize
24 from PyQt5.QtGui import QCursor, QPixmap, QFont, QIcon, QPalette
25 from PyQt5.QtWidgets import QVBoxLayout, QDialog, QPushButton, QColorDialog,\
26 QMenu, QLabel, QListWidget, QDialogButtonBox, QAbstractItemView
27
28 from .. import param
29 from .app import app
30
31 #class PseudoEvent(object):
32 # u"Cette classe est destinée à maintenir en vie un évènement périmé."
33
34 # _methodes = ("AltDown", "ControlDown", "ShiftDown", "GetPositionTuple",
35 # "RightIsDown", "RightIsUp", "LeftIsDown", "LeftIsUp", "GetWheelRotation")
36
37 # def __init__(self, event):
38 # self._dict = {}
39 # for methode in self._methodes:
40 # if hasattr(event, methode):
41 # self._dict[methode] = getattr(event, methode)()
42
43 # def __getattr__(self, name):
44 # try:
45 # return lambda:self._dict[name]
46 # except KeyError:
47 # print(u"La méthode " + name + " n'est actuellement pas définie pour les pseudo-évènements.")
48 # raise
49
50 # def Skip(self):
51 # pass
52
53
54 #TransmitEvent, EVT_TRANSMIT = wx.lib.newevent.NewEvent()
55
56
57 class BusyCursor(object):
58 def __enter__(self):
59 app.setOverrideCursor(QCursor(Qt.WaitCursor))
60
61 def __exit__(self, type, value, traceback):
62 app.restoreOverrideCursor()
63
64
65 class MyMiniFrame(QDialog):
66 def __init__(self, parent, titre=''):
67 QDialog.__init__(self, parent)
68 self.setModal(False)
69 self.setWindowTitle(titre)
70
71 def png_pth(nom):
72 """Adresse complète de l'image `nom`.png du repertoire 'images/'.
73
74 Le nom doit être indiqué sans l'extension '.png'."""
75 return os.path.normpath(os.path.join(param.EMPLACEMENT, 'wxgeometrie/images', nom + ".png"))
76
77
78 def png(nom):
79 """Charge l'image <nom>.png depuis le repertoire 'images/'.
80
81 L'image doit être au format png, et son nom doit indiqué sans l'extension '.png'."""
82 pixmap = QPixmap()
83 pixmap.load(png_pth(nom), 'PNG')
84 return pixmap
85
86
87 class GenericThread(QThread):
88 def __init__(self, function, *args, **kwargs):
89 QThread.__init__(self)
90 self.function = function
91 self.args = args
92 self.kwargs = kwargs
93
94 def __del__(self):
95 self.wait()
96
97 def run(self):
98 self.function(*self.args,**self.kwargs)
99
100
101 #class MyFont(wx.Font):
102 # u"""Créé une nouvelle police, héritant par défaut ses attributs de 'widget'."""
103 # def __init__(self, widget, size=None, family=None, style=None, weight=None, underline=None):
104 # font = widget.GetFont()
105 # if size is None:
106 # size = font.GetPointSize()
107 # if family is None:
108 # family = font.GetFamily()
109 # if style is None:
110 # style = font.GetStyle()
111 # if weight is None:
112 # weight = font.GetWeight()
113 # if underline is None:
114 # underline = font.GetUnderlined()
115 # wx.Font.__init__(self, size, family, style, weight, underline)
116
117 def shift_down(event):
118 return event.modifiers() & Qt.ShiftModifier
119
120 def alt_down(event):
121 return event.modifiers() & Qt.AltModifier
122
123 def ctrl_down(event):
124 return event.modifiers() & Qt.ControlModifier
125
126 def meta_down(event):
127 return event.modifiers() & Qt.MetaModifier
128
129 def left_down(event):
130 return event.buttons() & Qt.LeftButton
131
132 def right_down(event):
133 return event.buttons() & Qt.RightButton
134
135 def lieu(event_or_widget):
136 if isinstance(event_or_widget, QEvent):
137 p = event_or_widget.pos()
138 else:
139 p = event_or_widget.mapFromGlobal(QCursor.pos())
140 return p.x(), p.y()
141
142
143 class ColorSelecter(QPushButton):
144 "A bouton used to select a color."
145
146 colorSelected = pyqtSignal('QColor')
147
148 def __init__(self, parent, color=None):
149 QPushButton.__init__(self, parent)
150 self.parent = parent
151 self.setColor(color)
152 self.clicked.connect(self.onClick)
153 self.setMinimumSize(QSize(25, 25))
154 self.setMaximumSize(QSize(25, 25))
155
156 def onClick(self):
157 self.setColor(QColorDialog.getColor(self.color, self, "Choisissez une couleur"))
158
159 def setColor(self, color):
160 if color is None:
161 color = self.parent.palette().color(QPalette.Active, QPalette.Window)
162 if color.isValid():
163 # Couleur invalide -> l'utilisateur a cliqué sur "Annuler"
164 self.color = color
165 if color is not None:
166 self.setStyleSheet("ColorSelecter { border-radius: 4px; \
167 background-color: %s ; border-color:#AAAAAA #AAAAAA #444444 #444444; \
168 border-style:solid; border-width:1px;} \
169 ColorSelecter:disabled {border-color:#AAAAAA #AAAAAA #444444 #444444;} \
170 ColorSelecter:hover { background-color: %s }"
171 % (color.name(), color.lighter(125).name()))
172 self.colorSelected.emit(color)
173
174
175 class PopUpMenu(QMenu):
176 """Un menu avec un titre visible."""
177
178 def __init__(self, title, parent, icon=None):
179 QMenu.__init__(self, title, parent)
180 if icon is None:
181 title = '\u2022 ' + title
182 self._title = self.addAction(title)
183 else:
184 if isinstance(icon, str):
185 ##icon = QIcon(png(icon))
186 icon = QIcon(png_pth(icon))
187 self._title = self.addAction(icon, title)
188 self._title.setEnabled(False)
189 self._title.setIconVisibleInMenu(True)
190 font = QFont()
191 font.setBold(True)
192 font.setStyle(QFont.StyleItalic)
193 self._title.setFont(font)
194 self.addSeparator()
195
196 def setTitle(self, title):
197 self._title.setText(title)
198 QMenu.setTitle(title)
199
200
201 class MultipleChoiceDialog(QDialog):
202 def __init__(self, parent, title, text, items):
203 QDialog.__init__(self, parent)
204 self.setWindowTitle(title)
205 self.layout = QVBoxLayout()
206 label = QLabel(text)
207 self.layout.addWidget(label)
208 self.listwidget = QListWidget(self)
209 self.listwidget.setSelectionMode(QAbstractItemView.ExtendedSelection)
210 for item in items:
211 self.listwidget.addItem(item)
212 self.layout.addWidget(self.listwidget)
213 buttonBox = QDialogButtonBox(QDialogButtonBox.Ok|QDialogButtonBox.Cancel)
214 buttonBox.accepted.connect(self.accept)
215 buttonBox.rejected.connect(self.reject)
216 self.layout.addWidget(buttonBox)
217 self.setLayout(self.layout)
218
219 def selectedItems(self):
220 return self.listwidget.selectedItems()