|
@@ -49,6 +49,9 @@ public:
|
|
|
QIcon searchIcon;
|
|
|
|
|
|
QIcon::Mode clearIconMode;
|
|
|
+#if QT_VERSION < 0x040700
|
|
|
+ QString placeholderText;
|
|
|
+#endif
|
|
|
};
|
|
|
|
|
|
// --------------------------------------------------
|
|
@@ -117,24 +120,99 @@ ctkSearchBox::~ctkSearchBox()
|
|
|
{
|
|
|
}
|
|
|
|
|
|
+#if QT_VERSION < 0x040700
|
|
|
+// --------------------------------------------------
|
|
|
+QString ctkSearchBox::placeholderText()const
|
|
|
+{
|
|
|
+ Q_D(const ctkSearchBox);
|
|
|
+ return d->placeholderText;
|
|
|
+}
|
|
|
+
|
|
|
+// --------------------------------------------------
|
|
|
+void ctkSearchBox::setPlaceholderText(const QString &defaultText)
|
|
|
+{
|
|
|
+ Q_D(ctkSearchBox);
|
|
|
+ d->placeholderText = defaultText;
|
|
|
+ if (!this->hasFocus())
|
|
|
+ {
|
|
|
+ this->update();
|
|
|
+ }
|
|
|
+}
|
|
|
+#endif
|
|
|
+
|
|
|
// --------------------------------------------------
|
|
|
void ctkSearchBox::paintEvent(QPaintEvent * event)
|
|
|
{
|
|
|
Q_D(ctkSearchBox);
|
|
|
- QPainter p(this);
|
|
|
|
|
|
// Draw the line edit with text.
|
|
|
// Text has already been shifted to the right (in resizeEvent()) to leave
|
|
|
// space for the search icon.
|
|
|
this->Superclass::paintEvent(event);
|
|
|
|
|
|
- // Draw clearIcon
|
|
|
+ QPainter p(this);
|
|
|
+
|
|
|
QRect cRect = d->clearRect();
|
|
|
+ QRect sRect = d->searchRect();
|
|
|
+
|
|
|
+#if QT_VERSION >= 0x040700
|
|
|
+ QRect r = rect();
|
|
|
+ QPalette pal = palette();
|
|
|
+
|
|
|
+ QStyleOptionFrameV2 panel;
|
|
|
+ initStyleOption(&panel);
|
|
|
+ r = this->style()->subElementRect(QStyle::SE_LineEditContents, &panel, this);
|
|
|
+ r.setX(r.x() + this->textMargins().left());
|
|
|
+ r.setY(r.y() + this->textMargins().top());
|
|
|
+ r.setRight(r.right() - this->textMargins().right());
|
|
|
+ r.setBottom(r.bottom() - this->textMargins().bottom());
|
|
|
+ p.setClipRect(r);
|
|
|
+
|
|
|
+ QFontMetrics fm = fontMetrics();
|
|
|
+ Qt::Alignment va = QStyle::visualAlignment(this->layoutDirection(),
|
|
|
+ QFlag(this->alignment()));
|
|
|
+ int vscroll = 0;
|
|
|
+ const int verticalMargin = 1;
|
|
|
+ const int horizontalMargin = 2;
|
|
|
+ switch (va & Qt::AlignVertical_Mask) {
|
|
|
+ case Qt::AlignBottom:
|
|
|
+ vscroll = r.y() + r.height() - fm.height() - verticalMargin;
|
|
|
+ break;
|
|
|
+ case Qt::AlignTop:
|
|
|
+ vscroll = r.y() + verticalMargin;
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ //center
|
|
|
+ vscroll = r.y() + (r.height() - fm.height() + 1) / 2;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ QRect lineRect(r.x() + horizontalMargin, vscroll,
|
|
|
+ r.width() - 2*horizontalMargin, fm.height());
|
|
|
+
|
|
|
+ int minLB = qMax(0, -fm.minLeftBearing());
|
|
|
+
|
|
|
+ if (this->text().isEmpty())
|
|
|
+ {
|
|
|
+ if (!this->hasFocus() && !this->placeholderText().isEmpty())
|
|
|
+ {
|
|
|
+ QColor col = pal.text().color();
|
|
|
+ col.setAlpha(128);
|
|
|
+ QPen oldpen = p.pen();
|
|
|
+ p.setPen(col);
|
|
|
+ lineRect.adjust(minLB, 0, 0, 0);
|
|
|
+ QString elidedText = fm.elidedText(this->placeholderText(), Qt::ElideRight, lineRect.width());
|
|
|
+ p.drawText(lineRect, va, elidedText);
|
|
|
+ p.setPen(oldpen);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ p.setClipRect(this->rect());
|
|
|
+#endif
|
|
|
+
|
|
|
+ // Draw clearIcon
|
|
|
QPixmap closePixmap = d->clearIcon.pixmap(cRect.size(),d->clearIconMode);
|
|
|
this->style()->drawItemPixmap(&p, cRect, Qt::AlignCenter, closePixmap);
|
|
|
|
|
|
- // Draw searchIcon
|
|
|
- QRect sRect = d->searchRect();
|
|
|
+ //Draw searchIcon
|
|
|
QPixmap searchPixmap = d->searchIcon.pixmap(sRect.size());
|
|
|
this->style()->drawItemPixmap(&p, sRect, Qt::AlignCenter, searchPixmap);
|
|
|
|