|
QGIS API Documentation
master-59fd5e0
|
00001 /*************************************************************************** 00002 qgsfilterlineedit.cpp 00003 ------------------------ 00004 begin : October 27, 2012 00005 copyright : (C) 2012 by Alexander Bruy 00006 email : alexander dot bruy at gmail dot com 00007 ***************************************************************************/ 00008 00009 /*************************************************************************** 00010 * * 00011 * This program is free software; you can redistribute it and/or modify * 00012 * it under the terms of the GNU General Public License as published by * 00013 * the Free Software Foundation; either version 2 of the License, or * 00014 * (at your option) any later version. * 00015 * * 00016 ***************************************************************************/ 00017 00018 #include "qgsfilterlineedit.h" 00019 #include "qgsapplication.h" 00020 00021 #include <QToolButton> 00022 #include <QStyle> 00023 00024 QgsFilterLineEdit::QgsFilterLineEdit( QWidget* parent, QString nullValue ) 00025 : QLineEdit( parent ) 00026 , mNullValue( nullValue ) 00027 { 00028 btnClear = new QToolButton( this ); 00029 btnClear->setIcon( QgsApplication::getThemeIcon( "/mIconClear.svg" ) ); 00030 btnClear->setCursor( Qt::ArrowCursor ); 00031 btnClear->setFocusPolicy( Qt::NoFocus ); 00032 btnClear->setStyleSheet( "QToolButton { border: none; padding: 0px; }" ); 00033 btnClear->hide(); 00034 00035 connect( btnClear, SIGNAL( clicked() ), this, SLOT( clear() ) ); 00036 connect( btnClear, SIGNAL( clicked() ), this, SIGNAL( cleared() ) ); 00037 connect( this, SIGNAL( textChanged( const QString& ) ), this, 00038 SLOT( toggleClearButton( const QString& ) ) ); 00039 00040 int frameWidth = style()->pixelMetric( QStyle::PM_DefaultFrameWidth ); 00041 setStyleSheet( QString( "QLineEdit { padding-right: %1px; } " ) 00042 .arg( btnClear->sizeHint().width() + frameWidth + 1 ) ); 00043 00044 QSize msz = minimumSizeHint(); 00045 setMinimumSize( qMax( msz.width(), btnClear->sizeHint().height() + frameWidth * 2 + 2 ), 00046 qMax( msz.height(), btnClear->sizeHint().height() + frameWidth * 2 + 2 ) ); 00047 } 00048 00049 void QgsFilterLineEdit::resizeEvent( QResizeEvent * ) 00050 { 00051 QSize sz = btnClear->sizeHint(); 00052 int frameWidth = style()->pixelMetric( QStyle::PM_DefaultFrameWidth ); 00053 btnClear->move( rect().right() - frameWidth - sz.width(), 00054 ( rect().bottom() + 1 - sz.height() ) / 2 ); 00055 } 00056 00057 void QgsFilterLineEdit::clear() 00058 { 00059 setText( mNullValue ); 00060 setModified( true ); 00061 } 00062 00063 void QgsFilterLineEdit::changeEvent( QEvent *e ) 00064 { 00065 QLineEdit::changeEvent( e ); 00066 if ( !isEnabled() ) 00067 btnClear->setVisible( false ); 00068 else 00069 btnClear->setVisible( text() != mNullValue ); 00070 } 00071 00072 void QgsFilterLineEdit::toggleClearButton( const QString &text ) 00073 { 00074 btnClear->setVisible( !isReadOnly() && text != mNullValue ); 00075 }