openlp/openlp/core/lib/searchedit.py

178 lines
8.0 KiB
Python
Raw Normal View History

2010-12-28 21:12:20 +00:00
# -*- coding: utf-8 -*-
2012-12-28 22:06:43 +00:00
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
2010-12-28 21:12:20 +00:00
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
2015-01-18 13:39:21 +00:00
# Copyright (c) 2008-2015 OpenLP Developers #
2010-12-28 21:12:20 +00:00
# --------------------------------------------------------------------------- #
# This program is free software; you can redistribute it and/or modify it #
# under the terms of the GNU General Public License as published by the Free #
# Software Foundation; version 2 of the License. #
# #
# This program is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
# more details. #
# #
# You should have received a copy of the GNU General Public License along #
# with this program; if not, write to the Free Software Foundation, Inc., 59 #
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
###############################################################################
2013-08-31 18:17:38 +00:00
2010-12-28 21:12:20 +00:00
import logging
2012-03-25 21:34:02 +00:00
from PyQt4 import QtCore, QtGui
2010-12-28 21:12:20 +00:00
2012-03-25 21:36:09 +00:00
from openlp.core.lib import build_icon
from openlp.core.lib.ui import create_widget_action
2010-12-28 21:12:20 +00:00
log = logging.getLogger(__name__)
2013-02-01 19:58:18 +00:00
2010-12-28 21:12:20 +00:00
class SearchEdit(QtGui.QLineEdit):
"""
This is a specialised QLineEdit with a "clear" button inside for searches.
"""
def __init__(self, parent):
"""
Constructor.
"""
2013-07-18 11:19:20 +00:00
super(SearchEdit, self).__init__(parent)
self._current_search_type = -1
self.clear_button = QtGui.QToolButton(self)
2013-08-31 18:17:38 +00:00
self.clear_button.setIcon(build_icon(':/system/clear_shortcut.png'))
self.clear_button.setCursor(QtCore.Qt.ArrowCursor)
2013-08-31 18:17:38 +00:00
self.clear_button.setStyleSheet('QToolButton { border: none; padding: 0px; }')
self.clear_button.resize(18, 18)
self.clear_button.hide()
2013-03-05 13:55:50 +00:00
self.clear_button.clicked.connect(self._on_clear_button_clicked)
self.textChanged.connect(self._on_search_edit_text_changed)
self._update_style_sheet()
2011-05-12 15:44:20 +00:00
self.setAcceptDrops(False)
2010-12-28 21:12:20 +00:00
def _update_style_sheet(self):
2010-12-28 21:12:20 +00:00
"""
Internal method to update the stylesheet depending on which widgets are available and visible.
2010-12-28 21:12:20 +00:00
"""
frame_width = self.style().pixelMetric(QtGui.QStyle.PM_DefaultFrameWidth)
right_padding = self.clear_button.width() + frame_width
2013-08-31 18:17:38 +00:00
if hasattr(self, 'menu_button'):
left_padding = self.menu_button.width()
2013-08-31 18:17:38 +00:00
stylesheet = 'QLineEdit { padding-left: %spx; padding-right: %spx; } ' % (left_padding, right_padding)
2010-12-28 21:12:20 +00:00
else:
2013-08-31 18:17:38 +00:00
stylesheet = 'QLineEdit { padding-right: %spx; } ' % right_padding
self.setStyleSheet(stylesheet)
2011-01-06 20:48:22 +00:00
msz = self.minimumSizeHint()
self.setMinimumSize(max(msz.width(), self.clear_button.width() + (frame_width * 2) + 2),
2013-12-24 08:56:50 +00:00
max(msz.height(), self.clear_button.height() + (frame_width * 2) + 2))
2010-12-28 21:12:20 +00:00
def resizeEvent(self, event):
"""
Reimplemented method to react to resizing of the widget.
2014-03-17 19:05:55 +00:00
:param event: The event that happened.
2010-12-28 21:12:20 +00:00
"""
size = self.clear_button.size()
frame_width = self.style().pixelMetric(QtGui.QStyle.PM_DefaultFrameWidth)
self.clear_button.move(self.rect().right() - frame_width - size.width(),
2013-12-24 08:56:50 +00:00
(self.rect().bottom() + 1 - size.height()) // 2)
2013-08-31 18:17:38 +00:00
if hasattr(self, 'menu_button'):
size = self.menu_button.size()
2013-04-24 19:05:34 +00:00
self.menu_button.move(self.rect().left() + frame_width + 2, (self.rect().bottom() + 1 - size.height()) // 2)
2010-12-28 21:12:20 +00:00
2013-03-06 22:11:30 +00:00
def current_search_type(self):
2010-12-28 21:12:20 +00:00
"""
Readonly property to return the current search type.
"""
return self._current_search_type
2010-12-28 21:12:20 +00:00
2013-03-06 22:11:30 +00:00
def set_current_search_type(self, identifier):
"""
Set a new current search type.
2014-03-17 19:05:55 +00:00
:param identifier: The search type identifier (int).
"""
menu = self.menu_button.menu()
for action in menu.actions():
2012-05-19 09:13:32 +00:00
if identifier == action.data():
# setPlaceholderText 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).
try:
self.setPlaceholderText(action.placeholder_text)
except AttributeError:
pass
self.menu_button.setDefaultAction(action)
self._current_search_type = identifier
2013-08-31 18:17:38 +00:00
self.emit(QtCore.SIGNAL('searchTypeChanged(int)'), identifier)
return True
2013-03-06 22:11:30 +00:00
def set_search_types(self, items):
2010-12-28 21:12:20 +00:00
"""
A list of tuples to be used in the search type menu. The first item in the list will be preselected as the
default.
2010-12-28 21:12:20 +00:00
2014-03-20 19:10:31 +00:00
:param items: The list of tuples to use. The tuples should contain an integer identifier, an icon (QIcon
instance or string) and a title for the item in the menu. In short, they should look like this::
2010-12-28 21:12:20 +00:00
(<identifier>, <icon>, <title>, <place holder text>)
2010-12-28 21:12:20 +00:00
For instance::
(1, <QIcon instance>, "Titles", "Search Song Titles...")
2010-12-28 21:12:20 +00:00
Or::
(2, ":/songs/authors.png", "Authors", "Search Authors...")
2010-12-28 21:12:20 +00:00
"""
menu = QtGui.QMenu(self)
first = None
for identifier, icon, title, placeholder in items:
action = create_widget_action(
2013-03-06 22:11:30 +00:00
menu, text=title, icon=icon, data=identifier, triggers=self._on_menu_action_triggered)
action.placeholder_text = placeholder
2010-12-28 21:12:20 +00:00
if first is None:
first = action
self._current_search_type = identifier
2013-08-31 18:17:38 +00:00
if not hasattr(self, 'menu_button'):
self.menu_button = QtGui.QToolButton(self)
2013-08-31 18:17:38 +00:00
self.menu_button.setIcon(build_icon(':/system/clear_shortcut.png'))
self.menu_button.setCursor(QtCore.Qt.ArrowCursor)
self.menu_button.setPopupMode(QtGui.QToolButton.InstantPopup)
2013-08-31 18:17:38 +00:00
self.menu_button.setStyleSheet('QToolButton { border: none; padding: 0px 10px 0px 0px; }')
self.menu_button.resize(QtCore.QSize(28, 18))
self.menu_button.setMenu(menu)
self.menu_button.setDefaultAction(first)
self.menu_button.show()
self._update_style_sheet()
def _on_search_edit_text_changed(self, text):
"""
Internally implemented slot to react to when the text in the line edit has changed so that we can show or hide
the clear button.
2010-12-28 21:12:20 +00:00
2014-03-17 19:05:55 +00:00
:param text: A :class:`~PyQt4.QtCore.QString` instance which represents the text in the line edit.
2010-12-28 21:12:20 +00:00
"""
self.clear_button.setVisible(bool(text))
2010-12-28 21:12:20 +00:00
def _on_clear_button_clicked(self):
2010-12-28 21:12:20 +00:00
"""
Internally implemented slot to react to the clear button being clicked to clear the line edit. Once it has
cleared the line edit, it emits the ``cleared()`` signal so that an application can react to the clearing of the
line edit.
2010-12-28 21:12:20 +00:00
"""
self.clear()
2013-08-31 18:17:38 +00:00
self.emit(QtCore.SIGNAL('cleared()'))
2010-12-28 21:12:20 +00:00
2013-03-06 22:11:30 +00:00
def _on_menu_action_triggered(self):
2010-12-28 21:12:20 +00:00
"""
Internally implemented slot to react to the select of one of the search types in the menu. Once it has set the
correct action on the button, and set the current search type (using the list of identifiers provided by the
developer), the ``searchTypeChanged(int)`` signal is emitted with the identifier.
2010-12-28 21:12:20 +00:00
"""
for action in self.menu_button.menu().actions():
2014-03-23 10:49:03 +00:00
# Why is this needed?
2010-12-28 21:12:20 +00:00
action.setChecked(False)
2014-03-31 12:13:50 +00:00
self.set_current_search_type(self.sender().data())