Browse Source

Merge remote-tracking branch 'benjaminlong/ctksearchbox'

* benjaminlong/ctksearchbox:
  Add ctkSearchBox::placeholderText with Qt < 4.7
  Modification to fix a probleme under Windows
Julien Finet 14 years ago
parent
commit
e6831f3b26
2 changed files with 92 additions and 4 deletions
  1. 82 4
      Libs/Widgets/ctkSearchBox.cpp
  2. 10 0
      Libs/Widgets/ctkSearchBox.h

+ 82 - 4
Libs/Widgets/ctkSearchBox.cpp

@@ -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);
 

+ 10 - 0
Libs/Widgets/ctkSearchBox.h

@@ -45,6 +45,11 @@ class ctkSearchBoxPrivate;
 class CTK_WIDGETS_EXPORT ctkSearchBox : public QLineEdit
 {
   Q_OBJECT
+#if QT_VERSION < 0x040700
+  /// Qt < 4.7 don't have a placeholderText property, as we need it, we define it
+  /// manually.
+  Q_PROPERTY(QString placeholderText READ placeholderText WRITE setPlaceholderText)
+#endif
 
 public:
   /// Superclass typedef
@@ -53,6 +58,11 @@ public:
   ctkSearchBox(QWidget *parent = 0);
   virtual ~ctkSearchBox();
 
+#if QT_VERSION < 0x040700
+  QString placeholderText()const;
+  void setPlaceholderText(const QString& defaultText);
+#endif
+
 protected slots:
   /// Change the clear icon's state to enabled or disabled.
   void updateClearButtonState();