made searchedit conform to our standards

This commit is contained in:
Andreas Preikschat 2013-03-01 11:39:38 +01:00
parent 1ef6cde4e4
commit daed497a9e

View File

@ -47,34 +47,33 @@ class SearchEdit(QtGui.QLineEdit):
Constructor. Constructor.
""" """
QtGui.QLineEdit.__init__(self, parent) QtGui.QLineEdit.__init__(self, parent)
self._currentSearchType = -1 self._current_search_type = -1
self.clearButton = QtGui.QToolButton(self) self.clear_button = QtGui.QToolButton(self)
self.clearButton.setIcon(build_icon(u':/system/clear_shortcut.png')) self.clear_button.setIcon(build_icon(u':/system/clear_shortcut.png'))
self.clearButton.setCursor(QtCore.Qt.ArrowCursor) self.clear_button.setCursor(QtCore.Qt.ArrowCursor)
self.clearButton.setStyleSheet( self.clear_button.setStyleSheet(u'QToolButton { border: none; padding: 0px; }')
u'QToolButton { border: none; padding: 0px; }') self.clear_button.resize(18, 18)
self.clearButton.resize(18, 18) self.clear_button.hide()
self.clearButton.hide() QtCore.QObject.connect(self.clear_button, QtCore.SIGNAL(u'clicked()'), self._on_clear_button_clicked)
QtCore.QObject.connect(self.clearButton, QtCore.SIGNAL(u'clicked()'), self._onClearButtonClicked) QtCore.QObject.connect(self, QtCore.SIGNAL(u'textChanged(const QString&)'), self._on_search_edit_text_changed)
QtCore.QObject.connect(self, QtCore.SIGNAL(u'textChanged(const QString&)'), self._onSearchEditTextChanged) self._update_style_sheet()
self._updateStyleSheet()
self.setAcceptDrops(False) self.setAcceptDrops(False)
def _updateStyleSheet(self): def _update_style_sheet(self):
""" """
Internal method to update the stylesheet depending on which widgets are Internal method to update the stylesheet depending on which widgets are available and visible.
available and visible.
""" """
frameWidth = self.style().pixelMetric(QtGui.QStyle.PM_DefaultFrameWidth) frame_width = self.style().pixelMetric(QtGui.QStyle.PM_DefaultFrameWidth)
rightPadding = self.clearButton.width() + frameWidth right_padding = self.clear_button.width() + frame_width
if hasattr(self, u'menuButton'): if hasattr(self, u'menu_button'):
leftPadding = self.menuButton.width() left_padding = self.menu_button.width()
self.setStyleSheet(u'QLineEdit { padding-left: %spx; padding-right: %spx; } ' % (leftPadding, rightPadding)) stylesheet = u'QLineEdit { padding-left: %spx; padding-right: %spx; } ' % (left_padding, right_padding)
else: else:
self.setStyleSheet(u'QLineEdit { padding-right: %spx; } ' % rightPadding) stylesheet = u'QLineEdit { padding-right: %spx; } ' % right_padding
self.setStyleSheet(stylesheet)
msz = self.minimumSizeHint() msz = self.minimumSizeHint()
self.setMinimumSize(max(msz.width(), self.clearButton.width() + (frameWidth * 2) + 2), self.setMinimumSize(max(msz.width(), self.clear_button.width() + (frame_width * 2) + 2),
max(msz.height(), self.clearButton.height() + (frameWidth * 2) + 2)) max(msz.height(), self.clear_button.height() + (frame_width * 2) + 2))
def resizeEvent(self, event): def resizeEvent(self, event):
""" """
@ -83,19 +82,19 @@ class SearchEdit(QtGui.QLineEdit):
``event`` ``event``
The event that happened. The event that happened.
""" """
size = self.clearButton.size() size = self.clear_button.size()
frameWidth = self.style().pixelMetric(QtGui.QStyle.PM_DefaultFrameWidth) frame_width = self.style().pixelMetric(QtGui.QStyle.PM_DefaultFrameWidth)
self.clearButton.move(self.rect().right() - frameWidth - size.width(), self.clear_button.move(self.rect().right() - frame_width - size.width(),
(self.rect().bottom() + 1 - size.height()) / 2) (self.rect().bottom() + 1 - size.height()) / 2)
if hasattr(self, u'menuButton'): if hasattr(self, u'menu_button'):
size = self.menuButton.size() size = self.menu_button.size()
self.menuButton.move(self.rect().left() + frameWidth + 2, (self.rect().bottom() + 1 - size.height()) / 2) self.menu_button.move(self.rect().left() + frame_width + 2, (self.rect().bottom() + 1 - size.height()) / 2)
def currentSearchType(self): def currentSearchType(self):
""" """
Readonly property to return the current search type. Readonly property to return the current search type.
""" """
return self._currentSearchType return self._current_search_type
def setCurrentSearchType(self, identifier): def setCurrentSearchType(self, identifier):
""" """
@ -104,30 +103,28 @@ class SearchEdit(QtGui.QLineEdit):
``identifier`` ``identifier``
The search type identifier (int). The search type identifier (int).
""" """
menu = self.menuButton.menu() menu = self.menu_button.menu()
for action in menu.actions(): for action in menu.actions():
if identifier == action.data(): if identifier == action.data():
# setPlaceholderText has been implemented in Qt 4.7 and in at # setPlaceholderText has been implemented in Qt 4.7 and in at least PyQt 4.9 (I am not sure, if it was
# least PyQt 4.9 (I am not sure, if it was implemented in # implemented in PyQt 4.8).
# PyQt 4.8).
try: try:
self.setPlaceholderText(action.placeholderText) self.setPlaceholderText(action.placeholder_text)
except AttributeError: except AttributeError:
pass pass
self.menuButton.setDefaultAction(action) self.menu_button.setDefaultAction(action)
self._currentSearchType = identifier self._current_search_type = identifier
self.emit(QtCore.SIGNAL(u'searchTypeChanged(int)'), identifier) self.emit(QtCore.SIGNAL(u'searchTypeChanged(int)'), identifier)
return True return True
def setSearchTypes(self, items): def setSearchTypes(self, items):
""" """
A list of tuples to be used in the search type menu. The first item in A list of tuples to be used in the search type menu. The first item in the list will be preselected as the
the list will be preselected as the default. default.
``items`` ``items``
The list of tuples to use. The tuples should contain an integer The list of tuples to use. The tuples should contain an integer identifier, an icon (QIcon instance or
identifier, an icon (QIcon instance or string) and a title for the string) and a title for the item in the menu. In short, they should look like this::
item in the menu. In short, they should look like this::
(<identifier>, <icon>, <title>, <place holder text>) (<identifier>, <icon>, <title>, <place holder text>)
@ -142,63 +139,58 @@ class SearchEdit(QtGui.QLineEdit):
menu = QtGui.QMenu(self) menu = QtGui.QMenu(self)
first = None first = None
for identifier, icon, title, placeholder in items: for identifier, icon, title, placeholder in items:
action = create_widget_action(menu, text=title, icon=icon, action = create_widget_action(
data=identifier, triggers=self._onMenuActionTriggered) menu, text=title, icon=icon, data=identifier, triggers=self._onMenuActionTriggered)
action.placeholderText = placeholder action.placeholder_text = placeholder
if first is None: if first is None:
first = action first = action
self._currentSearchType = identifier self._current_search_type = identifier
if not hasattr(self, u'menuButton'): if not hasattr(self, u'menu_button'):
self.menuButton = QtGui.QToolButton(self) self.menu_button = QtGui.QToolButton(self)
self.menuButton.setIcon(build_icon(u':/system/clear_shortcut.png')) self.menu_button.setIcon(build_icon(u':/system/clear_shortcut.png'))
self.menuButton.setCursor(QtCore.Qt.ArrowCursor) self.menu_button.setCursor(QtCore.Qt.ArrowCursor)
self.menuButton.setPopupMode(QtGui.QToolButton.InstantPopup) self.menu_button.setPopupMode(QtGui.QToolButton.InstantPopup)
self.menuButton.setStyleSheet( self.menu_button.setStyleSheet(u'QToolButton { border: none; padding: 0px 10px 0px 0px; }')
u'QToolButton { border: none; padding: 0px 10px 0px 0px; }') self.menu_button.resize(QtCore.QSize(28, 18))
self.menuButton.resize(QtCore.QSize(28, 18)) self.menu_button.setMenu(menu)
self.menuButton.setMenu(menu) self.menu_button.setDefaultAction(first)
self.menuButton.setDefaultAction(first) self.menu_button.show()
self.menuButton.show() self._update_style_sheet()
self._updateStyleSheet()
def _onSearchEditTextChanged(self, text): def _on_search_edit_text_changed(self, text):
""" """
Internally implemented slot to react to when the text in the line edit Internally implemented slot to react to when the text in the line edit has changed so that we can show or hide
has changed so that we can show or hide the clear button. the clear button.
``text`` ``text``
A :class:`~PyQt4.QtCore.QString` instance which represents the text A :class:`~PyQt4.QtCore.QString` instance which represents the text in the line edit.
in the line edit.
""" """
self.clearButton.setVisible(bool(text)) self.clear_button.setVisible(bool(text))
def _onClearButtonClicked(self): def _on_clear_button_clicked(self):
""" """
Internally implemented slot to react to the clear button being clicked Internally implemented slot to react to the clear button being clicked to clear the line edit. Once it has
to clear the line edit. Once it has cleared the line edit, it emits the cleared the line edit, it emits the ``cleared()`` signal so that an application can react to the clearing of the
``cleared()`` signal so that an application can react to the clearing line edit.
of the line edit.
""" """
self.clear() self.clear()
self.emit(QtCore.SIGNAL(u'cleared()')) self.emit(QtCore.SIGNAL(u'cleared()'))
def _onMenuActionTriggered(self): def _onMenuActionTriggered(self):
""" """
Internally implemented slot to react to the select of one of the search Internally implemented slot to react to the select of one of the search types in the menu. Once it has set the
types in the menu. Once it has set the correct action on the button, correct action on the button, and set the current search type (using the list of identifiers provided by the
and set the current search type (using the list of identifiers provided developer), the ``searchTypeChanged(int)`` signal is emitted with the identifier.
by the developer), the ``searchTypeChanged(int)`` signal is emitted
with the identifier.
""" """
sender = self.sender() sender = self.sender()
for action in self.menuButton.menu().actions(): for action in self.menu_button.menu().actions():
action.setChecked(False) action.setChecked(False)
self.menuButton.setDefaultAction(sender) self.menu_button.setDefaultAction(sender)
self._currentSearchType = sender.data() self._current_search_type = sender.data()
# setPlaceholderText has been implemented in Qt 4.7 and in at least # setplaceholder_text has been implemented in Qt 4.7 and in at least
# PyQt 4.9 (I am not sure, if it was implemented in PyQt 4.8). # PyQt 4.9 (I am not sure, if it was implemented in PyQt 4.8).
try: try:
self.setPlaceholderText(self.menuButton.defaultAction().placeholderText) self.setPlaceholderText(self.menu_button.defaultAction().placeholder_text)
except AttributeError: except AttributeError:
pass pass
self.emit(QtCore.SIGNAL(u'searchTypeChanged(int)'), self._currentSearchType) self.emit(QtCore.SIGNAL(u'searchTypeChanged(int)'), self._current_search_type)