]> git.sesse.net Git - vlc/blobdiff - modules/gui/qt4/util/customwidgets.cpp
Qt: it's Qt::Key_Backspace not Qt::Key_Back
[vlc] / modules / gui / qt4 / util / customwidgets.cpp
index 52a32f5184423d9673b294f6a424088074ef60d1..3ccfbf001dbb09d519e32b700eedbb4c467ba4e5 100644 (file)
 #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,46 +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 );
+    setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred );
+}
+
+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 );
+}
 
-    QHBoxLayout *frameLayout = new QHBoxLayout( this );
-    frameLayout->setMargin( 0 );
-    frameLayout->setSpacing( 0 );
+QSize QVLCFramelessButton::sizeHint() const
+{
+    return iconSize();
+}
 
-    QPalette palette;
-    QBrush brush( QColor(255, 255, 255, 255) );
-    brush.setStyle(Qt::SolidPattern);
-    palette.setBrush(QPalette::Active, QPalette::Window, brush); //Qt::white
+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();
 
-    setPalette(palette);
-    setAutoFillBackground(true);
+    CONNECT( clearButton, clicked(), this, clear() );
 
-    searchLine = new  ClickLineEdit( qtr(I_PL_FILTER), 0 );
-    searchLine->setFrame( false );
-    searchLine->setMinimumWidth( 80 );
+    int frameWidth = style()->pixelMetric( QStyle::PM_DefaultFrameWidth, 0, this );
 
-    CONNECT( searchLine, textChanged( const QString ),
-             this, updateText( const QString ) );
-    frameLayout->addWidget( searchLine );
+    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 );
 
-    clearButton = new QToolButton;
-    clearButton->setAutoRaise( true );
-    clearButton->setMaximumWidth( 30 );
-    clearButton->setIcon( QIcon( ":/clear" ) );
-    clearButton->setToolTip( qtr( "Clear" ) );
+    setMessageVisible( true );
+
+    CONNECT( this, textEdited( const QString& ),
+             this, updateText( const QString& ) );
+}
+
+void SearchLineEdit::clear()
+{
+    QLineEdit::clear();
     clearButton->hide();
+    setMessageVisible( true );
+}
 
-    CONNECT( clearButton, clicked(), searchLine, clear() );
-    frameLayout->addWidget( clearButton );
+void SearchLineEdit::setMessageVisible( bool on )
+{
+    message = on;
+    repaint();
+    return;
 }
 
-void SearchLineEdit::updateText( const QString text )
+void SearchLineEdit::updateText( const QString& text )
 {
     clearButton->setVisible( !text.isEmpty() );
-    emit textChanged( text );
+}
+
+void SearchLineEdit::resizeEvent ( QResizeEvent * event )
+{
+  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 SearchLineEdit::focusInEvent( QFocusEvent *event )
+{
+  if( message )
+  {
+      setMessageVisible( false );
+  }
+  QLineEdit::focusInEvent( event );
+}
+
+void SearchLineEdit::focusOutEvent( QFocusEvent *event )
+{
+  if( text().isEmpty() )
+  {
+      setMessageVisible( true );
+  }
+  QLineEdit::focusOutEvent( event );
+}
+
+void SearchLineEdit::paintEvent( QPaintEvent *event )
+{
+  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 ) );
 }
 
 /***************************************************************************
@@ -174,7 +237,7 @@ int qtEventToVLCKey( QKeyEvent *e )
         HANDLE( Key_Right, KEY_RIGHT );
         HANDLE( Key_Up, KEY_UP );
         HANDLE( Key_Down, KEY_DOWN );
-        HANDLE( Key_Space, KEY_SPACE );
+        HANDLE( Key_Space, ' ' );
         HANDLE( Key_Escape, KEY_ESC );
         HANDLE( Key_Return, KEY_ENTER );
         HANDLE( Key_Enter, KEY_ENTER );
@@ -231,16 +294,23 @@ int qtWheelEventToVLCKey( QWheelEvent *e )
 
 QString VLCKeyToString( int val )
 {
-    const char *base = KeyToString (val & ~KEY_MODIFIER);
+    char *base = KeyToString (val & ~KEY_MODIFIER);
 
     QString r = "";
     if( val & KEY_MODIFIER_CTRL )
-        r+= "Ctrl+";
+        r+= qfu( "Ctrl+" );
     if( val & KEY_MODIFIER_ALT )
-        r+= "Alt+";
+        r+= qfu( "Alt+" );
     if( val & KEY_MODIFIER_SHIFT )
-        r+= "Shift+";
+        r+= qfu( "Shift+" );
 
-    return r + (base ? base : "Unset");
+    if (base)
+    {
+        r += qfu( base );
+        free( base );
+    }
+    else
+        r += qtr( "Unset" );
+    return r;
 }