1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
|
3
|
|
|
""" |
4
|
|
|
Helper widgets. |
5
|
|
|
""" |
6
|
|
|
|
7
|
|
|
# Standard library imports |
8
|
|
|
from __future__ import absolute_import, division, print_function |
9
|
|
|
|
10
|
|
|
# Third party imports |
11
|
|
|
from qtpy.QtCore import QSize, Qt |
12
|
|
|
from qtpy.QtWidgets import QHBoxLayout, QLineEdit, QPushButton |
13
|
|
|
import qtawesome as qta |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
class ButtonSearch(QPushButton): |
17
|
|
|
pass |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
class LineEditSearch(QLineEdit): |
21
|
|
|
def __init__(self, *args, **kwargs): |
22
|
|
|
super(LineEditSearch, self).__init__(*args, **kwargs) |
23
|
|
|
self._empty = True |
24
|
|
|
self._show_icons = False |
25
|
|
|
self.button_icon = ButtonSearch() |
26
|
|
|
|
27
|
|
|
self.button_icon.setDefault(True) |
28
|
|
|
self.button_icon.setFocusPolicy(Qt.NoFocus) |
29
|
|
|
|
30
|
|
|
# layout = QHBoxLayout() |
31
|
|
|
# layout.addWidget(self.button_icon, 0, Qt.AlignRight) |
32
|
|
|
# layout.setSpacing(0) |
33
|
|
|
# layout.addSpacing(2) |
34
|
|
|
# layout.setContentsMargins(0, 0, 0, 0) |
35
|
|
|
# self.setLayout(layout) |
36
|
|
|
|
37
|
|
|
# Signals |
38
|
|
|
self.textEdited.connect(self.update_box) |
39
|
|
|
self.button_icon.clicked.connect(self.clear_text) |
40
|
|
|
self.button_icon.setVisible(False) |
41
|
|
|
|
42
|
|
|
self.update_box(None) |
43
|
|
|
# self.set_icon_size(16, 16) |
44
|
|
|
self.setTabOrder(self, self.button_icon) |
45
|
|
|
|
46
|
|
|
def set_icon_size(self, width, height): |
47
|
|
|
self.button_icon.setMaximumSize(QSize(width, height)) |
48
|
|
|
self.setStyleSheet('LineEditSearch ' |
49
|
|
|
'{{padding-right: {0}px;}}'.format(width)) |
50
|
|
|
|
51
|
|
|
def set_icon_visibility(self, value): |
52
|
|
|
self._show_icons = value |
53
|
|
|
self.update_box() |
54
|
|
|
|
55
|
|
|
def setProperty(self, name, value): |
56
|
|
|
super(LineEditSearch, self).setProperty(name, value) |
57
|
|
|
self.style().unpolish(self) |
58
|
|
|
self.style().polish(self) |
59
|
|
|
self.update() |
60
|
|
|
|
61
|
|
|
def update_box(self, text=None): |
62
|
|
|
if text: |
63
|
|
|
if self._show_icons: |
64
|
|
|
self.button_icon.setIcon(qta.icon('fa.remove')) |
65
|
|
|
self.button_icon.setProperty('_remove', True) |
66
|
|
|
else: |
67
|
|
|
if self._show_icons: |
68
|
|
|
self.button_icon.setIcon(qta.icon('fa.search')) |
69
|
|
|
self.button_icon.setProperty('_remove', False) |
70
|
|
|
self._empty = not bool(text) |
71
|
|
|
self.button_icon.setDisabled(self._empty) |
72
|
|
|
|
73
|
|
|
# right = self.button_icon.width() |
74
|
|
|
# top = self.contentsMargins().top() |
75
|
|
|
# left = self.contentsMargins().left() |
76
|
|
|
# bottom = self.contentsMargins().bottom() |
77
|
|
|
# self.setContentsMargins(left, top, right, bottom) |
78
|
|
|
|
79
|
|
|
def clear_text(self): |
80
|
|
|
self.setText('') |
81
|
|
|
self.setFocus() |
82
|
|
|
self.update_box() |
83
|
|
|
|
84
|
|
|
def update_style_sheet(self, style_sheet=None): |
85
|
|
|
if style_sheet is not None: |
86
|
|
|
self.button_icon.setStyleSheet(style_sheet) |
87
|
|
|
|
88
|
|
|
def keyPressEvent(self, event): |
89
|
|
|
""" |
90
|
|
|
Qt override. |
91
|
|
|
""" |
92
|
|
|
key = event.key() |
93
|
|
|
if key in [Qt.Key_Escape]: |
94
|
|
|
self.clear_text() |
95
|
|
|
else: |
96
|
|
|
super(LineEditSearch, self).keyPressEvent(event) |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
def test(): |
100
|
|
|
from conda_manager.utils.qthelpers import qapplication |
101
|
|
|
app = qapplication() |
102
|
|
|
|
103
|
|
|
w = LineEditSearch() |
104
|
|
|
w.show() |
105
|
|
|
app.exec_() |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
if __name__ == '__main__': |
109
|
|
|
test() |
110
|
|
|
|