|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +""" |
| 4 | +*************************************************************************** |
| 5 | + FilterEdit.py |
| 6 | + --------------------- |
| 7 | + Date : October 2012 |
| 8 | + Copyright : (C) 2012 by Alexander Bruy |
| 9 | + Email : alexander dot bruy at gmail dot com |
| 10 | +*************************************************************************** |
| 11 | +* * |
| 12 | +* This program is free software; you can redistribute it and/or modify * |
| 13 | +* it under the terms of the GNU General Public License as published by * |
| 14 | +* the Free Software Foundation; either version 2 of the License, or * |
| 15 | +* (at your option) any later version. * |
| 16 | +* * |
| 17 | +*************************************************************************** |
| 18 | +""" |
| 19 | + |
| 20 | +__author__ = 'Alexander Bruy' |
| 21 | +__date__ = 'October 2012' |
| 22 | +__copyright__ = '(C) 2012, Alexander Bruy' |
| 23 | +# This will get replaced with a git SHA1 when you do a git archive |
| 24 | +__revision__ = '$Format:%H$' |
| 25 | + |
| 26 | +from PyQt4.QtCore import * |
| 27 | +from PyQt4.QtGui import * |
| 28 | + |
| 29 | +import sextante.resources_rc |
| 30 | + |
| 31 | +class FilterLineEdit(QLineEdit): |
| 32 | + def __init__(self, parent=None, placeholder="Filter"): |
| 33 | + QLineEdit.__init__(self, parent) |
| 34 | + |
| 35 | + if hasattr(self, "setPlaceholderText"): |
| 36 | + self.setPlaceholderText(placeholder) |
| 37 | + |
| 38 | + self.btnClear = QToolButton(self) |
| 39 | + self.btnClear.setIcon(QIcon(":/sextante/images/clear.png")) |
| 40 | + self.btnClear.setCursor(Qt.ArrowCursor) |
| 41 | + self.btnClear.setStyleSheet("QToolButton { border: none; padding: 0px; }") |
| 42 | + self.btnClear.hide() |
| 43 | + |
| 44 | + self.btnClear.clicked.connect(self.clear) |
| 45 | + self.textChanged.connect(self.updateClearButton) |
| 46 | + |
| 47 | + frameWidth = self.style().pixelMetric(QStyle.PM_DefaultFrameWidth) |
| 48 | + self.setStyleSheet(QString("QLineEdit { padding-right: %1px; } ").arg(self.btnClear.sizeHint().width() + frameWidth + 1)) |
| 49 | + msz = self.minimumSizeHint() |
| 50 | + self.setMinimumSize(max(msz.width(), self.btnClear.sizeHint().height() + frameWidth * 2 + 2), |
| 51 | + max(msz.height(), self.btnClear.sizeHint().height() + frameWidth * 2 + 2)) |
| 52 | + |
| 53 | + def resizeEvent(self, event): |
| 54 | + sz = self.btnClear.sizeHint() |
| 55 | + frameWidth = self.style().pixelMetric(QStyle.PM_DefaultFrameWidth) |
| 56 | + self.btnClear.move(self.rect().right() - frameWidth - sz.width(), |
| 57 | + (self.rect().bottom() + 1 - sz.height())/2) |
| 58 | + |
| 59 | + def updateClearButton(self, text): |
| 60 | + self.btnClear.setVisible(not text.isEmpty()) |
0 commit comments