Commit be2d6fb1 authored by Dan Pascu's avatar Dan Pascu

Moved SearchBox in blink.widgets.lineedit

parent 60abae98
# Copyright (c) 2010 AG Projects. See LICENSE for details.
#
__all__ = ['LineEdit', 'ValidatingLineEdit']
__all__ = ['LineEdit', 'ValidatingLineEdit', 'SearchBox']
import re
from PyQt4.QtCore import Qt, QEvent, pyqtSignal
from PyQt4.QtGui import QLineEdit, QBoxLayout, QHBoxLayout, QLabel, QLayout, QPainter, QPalette, QPixmap, QSpacerItem, QSizePolicy, QStyle, QWidget, QStyleOptionFrameV2
from PyQt4.QtGui import QAbstractButton, QLineEdit, QBoxLayout, QHBoxLayout, QLabel, QLayout, QPainter, QPalette, QPixmap, QSpacerItem, QSizePolicy, QStyle, QWidget, QStyleOptionFrameV2
from blink.resources import Resources
from blink.widgets.util import QtDynamicProperty
......@@ -165,3 +165,97 @@ class ValidatingLineEdit(LineEdit):
self.statusChanged.emit()
class SearchIcon(QWidget):
def __init__(self, parent=None, size=16):
QWidget.__init__(self, parent)
self.setFocusPolicy(Qt.NoFocus)
self.setVisible(True)
self.setMinimumSize(size+2, size+2)
pixmap = QPixmap()
if pixmap.load(Resources.get("icons/search.svg")):
self.icon = pixmap.scaled(size, size, Qt.KeepAspectRatio, Qt.SmoothTransformation)
else:
self.icon = None
def paintEvent(self, event):
painter = QPainter(self)
if self.icon is not None:
x = (self.width() - self.icon.width()) / 2
y = (self.height() - self.icon.height()) / 2
painter.drawPixmap(x, y, self.icon)
class ClearButton(QAbstractButton):
def __init__(self, parent=None, size=16):
QAbstractButton.__init__(self, parent)
self.setCursor(Qt.ArrowCursor)
self.setFocusPolicy(Qt.NoFocus)
self.setToolTip(u"Clear")
self.setVisible(False)
self.setMinimumSize(size+2, size+2)
pixmap = QPixmap()
if pixmap.load(Resources.get("icons/delete.svg")):
self.icon = pixmap.scaled(size, size, Qt.KeepAspectRatio, Qt.SmoothTransformation)
# Use QImage because QPainter using a QPixmap does not support CompositionMode_Multiply -Dan
image = self.icon.toImage()
painter = QPainter(image)
painter.setRenderHint(QPainter.Antialiasing, True)
painter.setCompositionMode(QPainter.CompositionMode_Multiply)
painter.drawPixmap(0, 0, self.icon)
painter.end()
self.icon_pressed = QPixmap(image)
else:
self.icon = self.icon_pressed = None
def paintEvent(self, event):
painter = QPainter(self)
icon = self.icon_pressed if self.isDown() else self.icon
if icon is not None:
x = (self.width() - icon.width()) / 2
y = (self.height() - icon.height()) / 2
painter.drawPixmap(x, y, icon)
else:
width = self.width()
height = self.height()
padding = width / 5
radius = width - 2*padding
palette = self.palette()
# Mid is darker than Dark. Go figure... -Dan
bg_color = palette.color(QPalette.Mid) if self.isDown() else palette.color(QPalette.Dark)
fg_color = palette.color(QPalette.Window) # or QPalette.Base for white
painter.setRenderHint(QPainter.Antialiasing, True)
painter.setBrush(bg_color)
painter.setPen(bg_color)
painter.drawEllipse(padding, padding, radius, radius)
padding = padding * 2
painter.setPen(fg_color)
painter.drawLine(padding, padding, width-padding, height-padding)
painter.drawLine(padding, height-padding, width-padding, padding)
class SearchBox(LineEdit):
def __init__(self, parent=None):
LineEdit.__init__(self, parent=parent)
self.search_icon = SearchIcon(self)
self.clear_button = ClearButton(self)
self.addHeadWidget(self.search_icon)
self.addTailWidget(self.clear_button)
option = QStyleOptionFrameV2()
self.initStyleOption(option)
frame_width = self.style().pixelMetric(QStyle.PM_DefaultFrameWidth, option, self)
widgets_height = max(self.search_icon.minimumHeight(), self.clear_button.minimumHeight())
self.setMinimumHeight(widgets_height + 2 + 2*frame_width)
self.clear_button.hide()
self.clear_button.clicked.connect(self.clear)
self.textChanged.connect(self.text_changed)
self.inactiveText = u"Search"
def text_changed(self, text):
self.clear_button.setVisible(not text.isEmpty())
# Copyright (c) 2010 AG Projects. See LICENSE for details.
#
from PyQt4.QtCore import Qt
from PyQt4.QtGui import QAbstractButton, QPainter, QPalette, QPixmap, QStyle, QWidget, QStyleOptionFrameV2
from blink.resources import Resources
from blink.widgets.lineedit import LineEdit
class SearchIcon(QWidget):
def __init__(self, parent=None, size=16):
QWidget.__init__(self, parent)
self.setFocusPolicy(Qt.NoFocus)
self.setVisible(True)
self.setMinimumSize(size+2, size+2)
pixmap = QPixmap()
if pixmap.load(Resources.get("icons/search.svg")):
self.icon = pixmap.scaled(size, size, Qt.KeepAspectRatio, Qt.SmoothTransformation)
else:
self.icon = None
def paintEvent(self, event):
painter = QPainter(self)
if self.icon is not None:
x = (self.width() - self.icon.width()) / 2
y = (self.height() - self.icon.height()) / 2
painter.drawPixmap(x, y, self.icon)
class ClearButton(QAbstractButton):
def __init__(self, parent=None, size=16):
QAbstractButton.__init__(self, parent)
self.setCursor(Qt.ArrowCursor)
self.setFocusPolicy(Qt.NoFocus)
self.setToolTip(u"Clear")
self.setVisible(False)
self.setMinimumSize(size+2, size+2)
pixmap = QPixmap()
if pixmap.load(Resources.get("icons/delete.svg")):
self.icon = pixmap.scaled(size, size, Qt.KeepAspectRatio, Qt.SmoothTransformation)
# Use QImage because QPainter using a QPixmap does not support CompositionMode_Multiply -Dan
image = self.icon.toImage()
painter = QPainter(image)
painter.setRenderHint(QPainter.Antialiasing, True)
painter.setCompositionMode(QPainter.CompositionMode_Multiply)
painter.drawPixmap(0, 0, self.icon)
painter.end()
self.icon_pressed = QPixmap(image)
else:
self.icon = self.icon_pressed = None
def paintEvent(self, event):
painter = QPainter(self)
icon = self.icon_pressed if self.isDown() else self.icon
if icon is not None:
x = (self.width() - icon.width()) / 2
y = (self.height() - icon.height()) / 2
painter.drawPixmap(x, y, icon)
else:
width = self.width()
height = self.height()
padding = width / 5
radius = width - 2*padding
palette = self.palette()
# Mid is darker than Dark. Go figure... -Dan
bg_color = palette.color(QPalette.Mid) if self.isDown() else palette.color(QPalette.Dark)
fg_color = palette.color(QPalette.Window) # or QPalette.Base for white
painter.setRenderHint(QPainter.Antialiasing, True)
painter.setBrush(bg_color)
painter.setPen(bg_color)
painter.drawEllipse(padding, padding, radius, radius)
padding = padding * 2
painter.setPen(fg_color)
painter.drawLine(padding, padding, width-padding, height-padding)
painter.drawLine(padding, height-padding, width-padding, padding)
class SearchBox(LineEdit):
def __init__(self, parent=None):
LineEdit.__init__(self, parent=parent)
self.search_icon = SearchIcon(self)
self.clear_button = ClearButton(self)
self.addHeadWidget(self.search_icon)
self.addTailWidget(self.clear_button)
option = QStyleOptionFrameV2()
self.initStyleOption(option)
frame_width = self.style().pixelMetric(QStyle.PM_DefaultFrameWidth, option, self)
widgets_height = max(self.search_icon.minimumHeight(), self.clear_button.minimumHeight())
self.setMinimumHeight(widgets_height + 2 + 2*frame_width)
self.clear_button.hide()
self.clear_button.clicked.connect(self.clear)
self.textChanged.connect(self.text_changed)
self.inactiveText = u"Search"
def text_changed(self, text):
self.clear_button.setVisible(not text.isEmpty())
......@@ -1222,7 +1222,7 @@ padding: 2px;</string>
<customwidget>
<class>SearchBox</class>
<extends>QLineEdit</extends>
<header>blink.widgets.searchbox</header>
<header>blink.widgets.lineedit</header>
</customwidget>
<customwidget>
<class>ContactListView</class>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment