-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
comboboxes.py
405 lines (335 loc) · 13.2 KB
/
comboboxes.py
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# -*- coding: utf-8 -*-
#
# Copyright © Spyder Project Contributors
# Licensed under the terms of the MIT License
# (see spyder/__init__.py for details)
"""Customized combobox widgets."""
# pylint: disable=C0103
# pylint: disable=R0903
# pylint: disable=R0911
# pylint: disable=R0201
# Standard library imports
import glob
import os
import os.path as osp
# Third party imports
import qdarkstyle
from qtpy.QtCore import QEvent, Qt, QTimer, QUrl, Signal, QSize
from qtpy.QtGui import QFont
from qtpy.QtWidgets import (QComboBox, QCompleter, QLineEdit,
QSizePolicy, QToolTip)
# Local imports
from spyder.config.base import _
from spyder.config.gui import is_dark_interface
from spyder.py3compat import to_text_string
from spyder.widgets.helperwidgets import IconLineEdit
class BaseComboBox(QComboBox):
"""Editable combo box base class"""
valid = Signal(bool, bool)
sig_tab_pressed = Signal(bool)
sig_resized = Signal(QSize, QSize)
"""
This signal is emitted to inform the widget has been resized.
Parameters
----------
size: QSize
The new size of the widget.
old_size: QSize
The previous size of the widget.
"""
def __init__(self, parent):
QComboBox.__init__(self, parent)
self.setEditable(True)
self.setCompleter(QCompleter(self))
self.selected_text = self.currentText()
# --- Qt overrides
def event(self, event):
"""Qt Override.
Filter tab keys and process double tab keys.
"""
if (event.type() == QEvent.KeyPress) and (event.key() == Qt.Key_Tab):
self.sig_tab_pressed.emit(True)
return True
return QComboBox.event(self, event)
def keyPressEvent(self, event):
"""Qt Override.
Handle key press events.
"""
if event.key() == Qt.Key_Return or event.key() == Qt.Key_Enter:
if self.add_current_text_if_valid():
self.selected()
self.hide_completer()
elif event.key() == Qt.Key_Escape:
self.set_current_text(self.selected_text)
self.hide_completer()
else:
QComboBox.keyPressEvent(self, event)
def resizeEvent(self, event):
"""
Emit a resize signal for widgets that need to adapt its size.
"""
super().resizeEvent(event)
self.sig_resized.emit(event.size(), event.oldSize())
# --- Own methods
def is_valid(self, qstr):
"""
Return True if string is valid
Return None if validation can't be done
"""
pass
def selected(self):
"""Action to be executed when a valid item has been selected"""
self.valid.emit(True, True)
def add_text(self, text):
"""Add text to combo box: add a new item if text is not found in
combo box items."""
index = self.findText(text)
while index != -1:
self.removeItem(index)
index = self.findText(text)
self.insertItem(0, text)
index = self.findText('')
if index != -1:
self.removeItem(index)
self.insertItem(0, '')
if text != '':
self.setCurrentIndex(1)
else:
self.setCurrentIndex(0)
else:
self.setCurrentIndex(0)
def set_current_text(self, text):
"""Sets the text of the QLineEdit of the QComboBox."""
self.lineEdit().setText(to_text_string(text))
def add_current_text(self):
"""Add current text to combo box history (convenient method)"""
text = self.currentText()
self.add_text(text)
def add_current_text_if_valid(self):
"""Add current text to combo box history if valid"""
valid = self.is_valid(self.currentText())
if valid or valid is None:
self.add_current_text()
return True
else:
self.set_current_text(self.selected_text)
def hide_completer(self):
"""Hides the completion widget."""
self.setCompleter(QCompleter([], self))
class PatternComboBox(BaseComboBox):
"""Search pattern combo box"""
def __init__(self, parent, items=None, tip=None,
adjust_to_minimum=True):
BaseComboBox.__init__(self, parent)
if hasattr(self.lineEdit(), 'setClearButtonEnabled'): # only Qt >= 5.2
self.lineEdit().setClearButtonEnabled(True)
if adjust_to_minimum:
self.setSizeAdjustPolicy(QComboBox.AdjustToMinimumContentsLength)
self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
if items is not None:
self.addItems(items)
if tip is not None:
self.setToolTip(tip)
class EditableComboBox(BaseComboBox):
"""
Editable combo box + Validate
"""
def __init__(self, parent):
BaseComboBox.__init__(self, parent)
self.font = QFont()
self.selected_text = self.currentText()
# Widget setup
self.setSizeAdjustPolicy(QComboBox.AdjustToMinimumContentsLength)
# Signals
self.editTextChanged.connect(self.validate)
self.tips = {True: _("Press enter to validate this entry"),
False: _('This entry is incorrect')}
def show_tip(self, tip=""):
"""Show tip"""
QToolTip.showText(self.mapToGlobal(self.pos()), tip, self)
def selected(self):
"""Action to be executed when a valid item has been selected"""
BaseComboBox.selected(self)
self.selected_text = self.currentText()
def validate(self, qstr, editing=True):
"""Validate entered path"""
if self.selected_text == qstr and qstr != '':
self.valid.emit(True, True)
return
valid = self.is_valid(qstr)
if editing:
if valid:
self.valid.emit(True, False)
else:
self.valid.emit(False, False)
class PathComboBox(EditableComboBox):
"""
QComboBox handling path locations
"""
open_dir = Signal(str)
def __init__(self, parent, adjust_to_contents=False):
EditableComboBox.__init__(self, parent)
# Replace the default lineedit by a custom one with icon display
lineedit = IconLineEdit(self)
# Widget setup
if adjust_to_contents:
self.setSizeAdjustPolicy(QComboBox.AdjustToContents)
else:
self.setSizeAdjustPolicy(QComboBox.AdjustToMinimumContentsLength)
self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
self.tips = {True: _("Press enter to validate this path"),
False: ''}
self.setLineEdit(lineedit)
# Signals
self.highlighted.connect(self.add_tooltip_to_highlighted_item)
self.sig_tab_pressed.connect(self.tab_complete)
self.valid.connect(lineedit.update_status)
# --- Qt overrides
def focusInEvent(self, event):
"""Handle focus in event restoring to display the status icon."""
show_status = getattr(self.lineEdit(), 'show_status_icon', None)
if show_status:
show_status()
QComboBox.focusInEvent(self, event)
def focusOutEvent(self, event):
"""Handle focus out event restoring the last valid selected path."""
# Calling asynchronously the 'add_current_text' to avoid crash
# https://groups.google.com/group/spyderlib/browse_thread/thread/2257abf530e210bd
if not self.is_valid():
lineedit = self.lineEdit()
QTimer.singleShot(50, lambda: lineedit.setText(self.selected_text))
hide_status = getattr(self.lineEdit(), 'hide_status_icon', None)
if hide_status:
hide_status()
QComboBox.focusOutEvent(self, event)
# --- Own methods
def _complete_options(self):
"""Find available completion options."""
text = to_text_string(self.currentText())
opts = glob.glob(text + "*")
opts = sorted([opt for opt in opts if osp.isdir(opt)])
completer = QCompleter(opts, self)
if is_dark_interface():
dark_qss = qdarkstyle.load_stylesheet_from_environment()
completer.popup().setStyleSheet(dark_qss)
self.setCompleter(completer)
return opts
def tab_complete(self):
"""
If there is a single option available one tab completes the option.
"""
opts = self._complete_options()
if len(opts) == 1:
self.set_current_text(opts[0] + os.sep)
self.hide_completer()
else:
self.completer().complete()
def is_valid(self, qstr=None):
"""Return True if string is valid"""
if qstr is None:
qstr = self.currentText()
return osp.isdir(to_text_string(qstr))
def selected(self):
"""Action to be executed when a valid item has been selected"""
self.selected_text = self.currentText()
self.valid.emit(True, True)
self.open_dir.emit(self.selected_text)
def add_current_text(self):
"""
Add current text to combo box history (convenient method).
If path ends in os separator ("\" windows, "/" unix) remove it.
"""
text = self.currentText()
if osp.isdir(text) and text:
if text[-1] == os.sep:
text = text[:-1]
self.add_text(text)
def add_tooltip_to_highlighted_item(self, index):
"""
Add a tooltip showing the full path of the currently highlighted item
of the PathComboBox.
"""
self.setItemData(index, self.itemText(index), Qt.ToolTipRole)
class UrlComboBox(PathComboBox):
"""
QComboBox handling urls
"""
def __init__(self, parent, adjust_to_contents=False):
PathComboBox.__init__(self, parent, adjust_to_contents)
line_edit = QLineEdit(self)
self.setLineEdit(line_edit)
self.editTextChanged.disconnect(self.validate)
def is_valid(self, qstr=None):
"""Return True if string is valid"""
if qstr is None:
qstr = self.currentText()
return QUrl(qstr).isValid()
class FileComboBox(PathComboBox):
"""
QComboBox handling File paths
"""
def __init__(self, parent=None, adjust_to_contents=False,
default_line_edit=False):
PathComboBox.__init__(self, parent, adjust_to_contents)
if default_line_edit:
line_edit = QLineEdit(self)
self.setLineEdit(line_edit)
# Widget setup
if adjust_to_contents:
self.setSizeAdjustPolicy(QComboBox.AdjustToContents)
else:
self.setSizeAdjustPolicy(QComboBox.AdjustToContentsOnFirstShow)
self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
def is_valid(self, qstr=None):
"""Return True if string is valid."""
if qstr is None:
qstr = self.currentText()
valid = (osp.isfile(to_text_string(qstr)) or
osp.isdir(to_text_string(qstr)))
return valid
def tab_complete(self):
"""
If there is a single option available one tab completes the option.
"""
opts = self._complete_options()
if len(opts) == 1:
text = opts[0]
if osp.isdir(text):
text = text + os.sep
self.set_current_text(text)
self.hide_completer()
else:
self.completer().complete()
def _complete_options(self):
"""Find available completion options."""
text = to_text_string(self.currentText())
opts = glob.glob(text + "*")
opts = sorted([opt for opt in opts
if osp.isdir(opt) or osp.isfile(opt)])
completer = QCompleter(opts, self)
if is_dark_interface():
dark_qss = qdarkstyle.load_stylesheet_from_environment()
completer.popup().setStyleSheet(dark_qss)
self.setCompleter(completer)
return opts
def is_module_or_package(path):
"""Return True if path is a Python module/package"""
is_module = osp.isfile(path) and osp.splitext(path)[1] in ('.py', '.pyw')
is_package = osp.isdir(path) and osp.isfile(osp.join(path, '__init__.py'))
return is_module or is_package
class PythonModulesComboBox(PathComboBox):
"""
QComboBox handling Python modules or packages path
(i.e. .py, .pyw files *and* directories containing __init__.py)
"""
def __init__(self, parent, adjust_to_contents=False):
PathComboBox.__init__(self, parent, adjust_to_contents)
def is_valid(self, qstr=None):
"""Return True if string is valid"""
if qstr is None:
qstr = self.currentText()
return is_module_or_package(to_text_string(qstr))
def selected(self):
"""Action to be executed when a valid item has been selected"""
EditableComboBox.selected(self)
self.open_dir.emit(self.currentText())