[vlc-devel] commit: Qt: properly styled SearchLineEdit (Jakob Leben )

git version control git at videolan.org
Mon Feb 8 22:23:24 CET 2010


vlc | branch: master | Jakob Leben <jleben at videolan.org> | Mon Feb  8 22:21:22 2010 +0100| [b639f729c0837ae0c46b314154af1c27b56d08c5] | committer: Jakob Leben 

Qt: properly styled SearchLineEdit

And replaced QVLCIconLabel with QVLCFramelessButton.

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=b639f729c0837ae0c46b314154af1c27b56d08c5
---

 modules/gui/qt4/components/playlist/selector.cpp |    3 +-
 modules/gui/qt4/components/playlist/selector.hpp |    4 +-
 modules/gui/qt4/util/customwidgets.cpp           |  126 +++++++++++++---------
 modules/gui/qt4/util/customwidgets.hpp           |   53 +++++-----
 4 files changed, 104 insertions(+), 82 deletions(-)

diff --git a/modules/gui/qt4/components/playlist/selector.cpp b/modules/gui/qt4/components/playlist/selector.cpp
index 7af34d2..159cac3 100644
--- a/modules/gui/qt4/components/playlist/selector.cpp
+++ b/modules/gui/qt4/components/playlist/selector.cpp
@@ -74,7 +74,8 @@ void PLSelItem::addAction( ItemAction act, const QString& tooltip )
         icon = QIcon( ":/buttons/playlist/playlist_remove" ); break;
     }
 
-    lblAction = new QVLCIconLabel( icon );
+    lblAction = new QVLCFramelessButton();
+    lblAction->setIcon( icon );
 
     if( !tooltip.isEmpty() ) lblAction->setToolTip( tooltip );
 
diff --git a/modules/gui/qt4/components/playlist/selector.hpp b/modules/gui/qt4/components/playlist/selector.hpp
index d290714..ab3c503 100644
--- a/modules/gui/qt4/components/playlist/selector.hpp
+++ b/modules/gui/qt4/components/playlist/selector.hpp
@@ -42,7 +42,7 @@
 #include "qt4.hpp"
 
 class PlaylistWidget;
-class QVLCIconLabel;
+class QVLCFramelessButton;
 
 enum SelectorItemType {
     CATEGORY_TYPE,
@@ -91,7 +91,7 @@ private:
     void enterEvent( QEvent* );
     void leaveEvent( QEvent* );
     QTreeWidgetItem* qitem;
-    QVLCIconLabel *lblAction;
+    QVLCFramelessButton *lblAction;
     QLabel *lbl;
     QHBoxLayout *layout;
 };
diff --git a/modules/gui/qt4/util/customwidgets.cpp b/modules/gui/qt4/util/customwidgets.cpp
index 2e50199..3ccfbf0 100644
--- a/modules/gui/qt4/util/customwidgets.cpp
+++ b/modules/gui/qt4/util/customwidgets.cpp
@@ -32,13 +32,13 @@
 #include "qt4.hpp" /*needed for qtr and CONNECT, but not necessary */
 
 #include <QPainter>
-#include <QLineEdit>
 #include <QColorGroup>
 #include <QRect>
 #include <QKeyEvent>
 #include <QWheelEvent>
-#include <QToolButton>
 #include <QHBoxLayout>
+#include <QStyle>
+#include <QStyleOption>
 #include <vlc_intf_strings.h>
 
 
@@ -104,85 +104,109 @@ void ClickLineEdit::focusOutEvent( QFocusEvent *ev )
     QLineEdit::focusOutEvent( ev );
 }
 
-SearchLineEdit::SearchLineEdit( QWidget *parent ) : QFrame( parent )
+QVLCFramelessButton::QVLCFramelessButton( QWidget *parent )
+  : QPushButton( parent )
 {
-    setFrameStyle( QFrame::WinPanel | QFrame::Sunken );
-    setLineWidth( 0 );
-
-    QHBoxLayout *frameLayout = new QHBoxLayout( this );
-    frameLayout->setMargin( 0 );
-    frameLayout->setSpacing( 0 );
-
-    QPalette palette;
-    QBrush brush( QColor(255, 255, 255, 255) );
-    brush.setStyle(Qt::SolidPattern);
-    palette.setBrush(QPalette::Active, QPalette::Window, brush); //Qt::white
-
-    setPalette(palette);
-    setAutoFillBackground(true);
+    setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred );
+}
 
-    searchLine = new  ClickLineEdit( qtr(I_PL_FILTER), 0 );
-    searchLine->setFrame( false );
-    searchLine->setMinimumWidth( 80 );
+void QVLCFramelessButton::paintEvent( QPaintEvent * event )
+{
+    QPainter painter( this );
+    QPixmap pix = icon().pixmap( size() );
+    QPoint pos( (width() - pix.width()) / 2, (height() - pix.height()) / 2 );
+    painter.drawPixmap( QRect( pos.x(), pos.y(), pix.width(), pix.height() ), pix );
+}
 
-    CONNECT( searchLine, textChanged( const QString& ),
-             this, updateText( const QString& ) );
-    frameLayout->addWidget( searchLine );
+QSize QVLCFramelessButton::sizeHint() const
+{
+    return iconSize();
+}
 
-    clearButton = new QToolButton;
-    clearButton->setAutoRaise( true );
-    clearButton->setMaximumWidth( 30 );
+SearchLineEdit::SearchLineEdit( QWidget *parent ) : QLineEdit( parent )
+{
+    clearButton = new QVLCFramelessButton( this );
     clearButton->setIcon( QIcon( ":/toolbar/clear" ) );
+    clearButton->setIconSize( QSize( 16, 16 ) );
+    clearButton->setCursor( Qt::ArrowCursor );
     clearButton->setToolTip( qfu(vlc_pgettext("Tooltip|Clear", "Clear")) );
     clearButton->hide();
 
-    CONNECT( clearButton, clicked(), searchLine, clear() );
-    frameLayout->addWidget( clearButton );
-}
+    CONNECT( clearButton, clicked(), this, clear() );
 
-void SearchLineEdit::updateText( const QString& text )
-{
-    clearButton->setVisible( !text.isEmpty() );
-    emit textChanged( text );
+    int frameWidth = style()->pixelMetric( QStyle::PM_DefaultFrameWidth, 0, this );
+
+    QFontMetrics metrics( font() );
+    QString styleSheet = QString( "min-height: %1px; "
+                                  "padding-top: 1px; "
+                                  "padding-bottom: 1px; "
+                                  "padding-right: %2px;" )
+                                  .arg( metrics.height() + ( 2 * frameWidth ) )
+                                  .arg( clearButton->sizeHint().width() + 1 );
+    setStyleSheet( styleSheet );
+
+    setMessageVisible( true );
+
+    CONNECT( this, textEdited( const QString& ),
+             this, updateText( const QString& ) );
 }
 
-QVLCIconLabel::QVLCIconLabel( const QIcon& i, QWidget *p )
-    : QLabel( p ), icon( i ), iconMode( QIcon::Normal )
+void SearchLineEdit::clear()
 {
-    updatePixmap();
+    QLineEdit::clear();
+    clearButton->hide();
+    setMessageVisible( true );
 }
 
-void QVLCIconLabel::setIcon( const QIcon& i )
+void SearchLineEdit::setMessageVisible( bool on )
 {
-  icon = i;
-  updatePixmap();
+    message = on;
+    repaint();
+    return;
 }
 
-void QVLCIconLabel::resizeEvent( QResizeEvent * event )
+void SearchLineEdit::updateText( const QString& text )
 {
-    updatePixmap();
+    clearButton->setVisible( !text.isEmpty() );
 }
 
-void QVLCIconLabel::enterEvent( QEvent * )
+void SearchLineEdit::resizeEvent ( QResizeEvent * event )
 {
-    iconMode = QIcon::Active;
-    updatePixmap();
+  QLineEdit::resizeEvent( event );
+  int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth,0,this);
+  clearButton->resize( clearButton->sizeHint().width(), height() );
+  clearButton->move( width() - clearButton->width() - frameWidth, 0 );
 }
 
-void QVLCIconLabel::leaveEvent( QEvent * )
+void SearchLineEdit::focusInEvent( QFocusEvent *event )
 {
-    iconMode = QIcon::Normal;
-    updatePixmap();
+  if( message )
+  {
+      setMessageVisible( false );
+  }
+  QLineEdit::focusInEvent( event );
 }
 
-void QVLCIconLabel::mouseReleaseEvent( QMouseEvent * )
+void SearchLineEdit::focusOutEvent( QFocusEvent *event )
 {
-    emit clicked();
+  if( text().isEmpty() )
+  {
+      setMessageVisible( true );
+  }
+  QLineEdit::focusOutEvent( event );
 }
 
-void QVLCIconLabel::updatePixmap()
+void SearchLineEdit::paintEvent( QPaintEvent *event )
 {
-    setPixmap( icon.pixmap( size(), iconMode ) );
+  QLineEdit::paintEvent( event );
+  if( !message ) return;
+  QStyleOption option;
+  option.initFrom( this );
+  QRect rect = style()->subElementRect( QStyle::SE_LineEditContents, &option, this )
+                  .adjusted( 3, 0, clearButton->width() + 1, 0 );
+  QPainter painter( this );
+  painter.setPen( palette().color( QPalette::Disabled, QPalette::Text ) );
+  painter.drawText( rect, Qt::AlignLeft | Qt::AlignVCenter, qtr( I_PL_FILTER ) );
 }
 
 /***************************************************************************
diff --git a/modules/gui/qt4/util/customwidgets.hpp b/modules/gui/qt4/util/customwidgets.hpp
index 132c5fc..b7d843a 100644
--- a/modules/gui/qt4/util/customwidgets.hpp
+++ b/modules/gui/qt4/util/customwidgets.hpp
@@ -28,8 +28,7 @@
 #define _CUSTOMWIDGETS_H_
 
 #include <QLineEdit>
-#include <QLabel>
-#include <QIcon>
+#include <QPushButton>
 
 /**
   This class provides a QLineEdit which contains a greyed-out hinting
@@ -58,41 +57,39 @@ private:
     bool mDrawClickMsg;
 };
 
-class QToolButton;
-class SearchLineEdit : public QFrame
+class QVLCFramelessButton : public QPushButton
 {
-    Q_OBJECT
+    Q_OBJECT;
 public:
-    SearchLineEdit( QWidget *parent );
-
+    QVLCFramelessButton( QWidget *parent = NULL );
+    QSize sizeHint() const;
 private:
-    ClickLineEdit *searchLine;
-    QToolButton   *clearButton;
-
-private slots:
-    void updateText( const QString& );
-
-signals:
-    void textChanged( const QString& );
+    void paintEvent( QPaintEvent * event );
 };
 
-class QVLCIconLabel : public QLabel
+class QLabel;
+
+class SearchLineEdit : public QLineEdit
 {
     Q_OBJECT
 public:
-    QVLCIconLabel( const QIcon&, QWidget *parent = 0 );
-    void setIcon( const QIcon& );
-signals:
-    void clicked();
-protected:
-    virtual void enterEvent( QEvent * );
-    virtual void leaveEvent( QEvent * );
-    virtual void mouseReleaseEvent( QMouseEvent * );
-    virtual void resizeEvent( QResizeEvent * );
+    SearchLineEdit( QWidget *parent = NULL );
+
 private:
-    inline void updatePixmap( );
-    QIcon icon;
-    QIcon::Mode iconMode;
+    void resizeEvent ( QResizeEvent * event );
+    void focusInEvent( QFocusEvent *event );
+    void focusOutEvent( QFocusEvent *event );
+    void paintEvent( QPaintEvent *event );
+    void setMessageVisible( bool on );
+    QVLCFramelessButton   *clearButton;
+    bool message;
+    QLabel *msg;
+
+public slots:
+    void clear();
+
+private slots:
+    void updateText( const QString& );
 };
 
 /* VLC Key/Wheel hotkeys interactions */




More information about the vlc-devel mailing list