[Digikam-devel] branches/extragear/kde3/graphics/digikam/libs/widgets/common
Gilles Caulier
caulier.gilles at gmail.com
Mon Nov 26 12:25:07 GMT 2007
SVN commit 741799 by cgilles:
digiKam from KDE3 branch : optimize space everywhere when Search Text Bar is used. No need a label on the left of line edit.
We use a gray text into edit field like Amarok. When user take focus on edit field, gray text desappear. When focus go out, gray text re-appear.
CCMAIL: digikam-devel at kde.org
M +98 -8 searchtextbar.cpp
M +35 -1 searchtextbar.h
--- branches/extragear/kde3/graphics/digikam/libs/widgets/common/searchtextbar.cpp #741798:741799
@@ -4,7 +4,7 @@
* http://www.digikam.org
*
* Date : 2007-11-25
- * Description : a bar used to search a text somewhere.
+ * Description : a bar used to search a string.
*
* Copyright (C) 2007 by Gilles Caulier <caulier dot gilles at gmail dot com>
*
@@ -25,6 +25,7 @@
#include <qcolor.h>
#include <qpalette.h>
+#include <qpainter.h>
#include <qlabel.h>
#include <qlayout.h>
#include <qtoolbutton.h>
@@ -34,7 +35,6 @@
#include <kapplication.h>
#include <kiconloader.h>
#include <klocale.h>
-#include <klineedit.h>
#include <kdialogbase.h>
// Local includes
@@ -45,6 +45,101 @@
namespace Digikam
{
+class DLineEditPriv
+{
+public:
+
+ DLineEditPriv()
+ {
+ drawMsg = true;
+ }
+
+ bool drawMsg;
+
+ QString message;
+};
+
+DLineEdit::DLineEdit(const QString &msg, QWidget *parent)
+ : KLineEdit(parent)
+{
+ d = new DLineEditPriv;
+ setClickMessage(msg);
+}
+
+DLineEdit::~DLineEdit()
+{
+ delete d;
+}
+
+void DLineEdit::setClickMessage(const QString &msg)
+{
+ d->message = msg;
+ repaint();
+}
+
+void DLineEdit::setDrawMessage(bool draw)
+{
+ d->drawMsg = draw;
+ repaint();
+}
+
+bool DLineEdit::drawMessage() const
+{
+ return d->drawMsg;
+}
+
+void DLineEdit::setText(const QString &txt)
+{
+ d->drawMsg = txt.isEmpty();
+ repaint();
+ KLineEdit::setText(txt);
+}
+
+void DLineEdit::drawContents(QPainter *p)
+{
+ KLineEdit::drawContents(p);
+
+ if (d->drawMsg && !hasFocus())
+ {
+ QPen tmp = p->pen();
+ p->setPen(palette().color( QPalette::Disabled, QColorGroup::Text));
+ QRect cr = contentsRect();
+
+ // Add two pixel margin on the left side
+ cr.rLeft() += 3;
+ p->drawText( cr, AlignAuto | AlignVCenter, d->message );
+ p->setPen( tmp );
+ }
+}
+
+void DLineEdit::dropEvent(QDropEvent *e)
+{
+ d->drawMsg = false;
+ KLineEdit::dropEvent(e);
+}
+
+void DLineEdit::focusInEvent(QFocusEvent *e)
+{
+ if (d->drawMsg)
+ {
+ d->drawMsg = false;
+ repaint();
+ }
+ QLineEdit::focusInEvent(e);
+}
+
+void DLineEdit::focusOutEvent(QFocusEvent *e)
+{
+ if (text().isEmpty())
+ {
+ d->drawMsg = true;
+ repaint();
+ }
+ QLineEdit::focusOutEvent(e);
+}
+
+// ---------------------------------------------------------------------
+
class SearchTextBarPriv
{
public:
@@ -52,12 +147,9 @@
SearchTextBarPriv()
{
searchEdit = 0;
- searchLabel = 0;
clearButton = 0;
}
- QLabel *searchLabel;
-
QToolButton *clearButton;
KLineEdit *searchEdit;
@@ -77,13 +169,11 @@
d->clearButton->setIconSet(kapp->iconLoader()->loadIcon("clear_left",
KIcon::Toolbar, KIcon::SizeSmall));
- d->searchLabel = new QLabel(i18n("Search:"), this);
- d->searchEdit = new KLineEdit(this);
+ d->searchEdit = new DLineEdit(i18n("Search..."), this);
d->searchEdit->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum));
hlay->setSpacing(0);
hlay->setMargin(0);
- hlay->addWidget(d->searchLabel);
hlay->addWidget(d->searchEdit);
hlay->addWidget(d->clearButton);
--- branches/extragear/kde3/graphics/digikam/libs/widgets/common/searchtextbar.h #741798:741799
@@ -4,7 +4,7 @@
* http://www.digikam.org
*
* Date : 2007-11-25
- * Description : a bar used to search a text somewhere.
+ * Description : a bar used to search a string.
*
* Copyright (C) 2007 by Gilles Caulier <caulier dot gilles at gmail dot com>
*
@@ -29,6 +29,10 @@
#include <qwidget.h>
#include <qstring.h>
+// KDE includes.
+
+#include <klineedit.h>
+
// Local includes.
#include "digikam_export.h"
@@ -36,8 +40,38 @@
namespace Digikam
{
+class DLineEditPriv;
class SearchTextBarPriv;
+class DIGIKAM_EXPORT DLineEdit : public KLineEdit
+{
+ Q_OBJECT
+
+public:
+
+ DLineEdit(const QString &msg, QWidget *parent);
+ ~DLineEdit();
+
+ void setClickMessage(const QString &msg);
+ QString clickMessage() const;
+
+ void setDrawMessage(bool draw);
+ bool drawMessage() const;
+
+ void setText(const QString& txt);
+
+protected:
+
+ void drawContents(QPainter *p);
+ void dropEvent(QDropEvent *e);
+ void focusInEvent(QFocusEvent *e);
+ void focusOutEvent(QFocusEvent *e);
+
+private :
+
+ DLineEditPriv* d;
+};
+
class DIGIKAM_EXPORT SearchTextBar : public QWidget
{
Q_OBJECT
More information about the Digikam-devel
mailing list