00001 #include "klineedit.h"
00002 #include <QIcon>
00003 #include <QStyleOption>
00004
00005 KLineEdit::KLineEdit( const QString &string, QWidget *parent ) : QLineEdit( string, parent )
00006 {
00007 init();
00008 }
00009
00010 KLineEdit::KLineEdit( QWidget *parent ) : QLineEdit( parent )
00011 {
00012 init();
00013 }
00014
00015 void KLineEdit::init()
00016 {
00017 clearButton = 0;
00018 clickInClear = false;
00019 wideEnoughForClear = true;
00020 overlap = 0;
00021
00022 setClearButtonShown(true);
00023 }
00024
00025 KLineEdit::~KLineEdit()
00026 {
00027 }
00028
00029 void KLineEdit::setClearButtonShown(bool show)
00030 {
00031 if(show)
00032 {
00033 if(clearButton)
00034 {
00035 return;
00036 }
00037
00038 clearButton = new KLineEditButton(this);
00039 clearButton->setCursor(Qt::ArrowCursor);
00040
00041 updateClearButtonIcon(text());
00042 updateClearButton();
00043 connect(this, SIGNAL(textChanged(QString)), this, SLOT(updateClearButtonIcon(QString)));
00044 }
00045 else
00046 {
00047 disconnect(this, SIGNAL(textChanged(QString)), this, SLOT(updateClearButtonIcon(QString)));
00048 delete clearButton;
00049 clearButton = 0;
00050 clickInClear = false;
00051 overlap = 0;
00052 }
00053 }
00054
00055 bool KLineEdit::isClearButtonShown() const
00056 {
00057 return clearButton != 0;
00058 }
00059
00060 QSize KLineEdit::clearButtonUsedSize() const
00061 {
00062 QSize s;
00063 if(clearButton)
00064 {
00065 const int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth, 0, this);
00066 s = clearButton->sizeHint();
00067 s.rwidth() += frameWidth;
00068 }
00069 return s;
00070 }
00071
00072 void KLineEdit::updateClearButtonIcon(const QString& text)
00073 {
00074 if(!clearButton || isReadOnly())
00075 {
00076 return;
00077 }
00078
00079 if(wideEnoughForClear && text.length() > 0)
00080 {
00081 clearButton->animateVisible(true);
00082 }
00083 else
00084 {
00085 clearButton->animateVisible(false);
00086 }
00087
00088 if(!clearButton->pixmap().isNull())
00089 {
00090 return;
00091 }
00092
00093 clearButton->setPixmap(QIcon(":/edit/ClearButton").pixmap(16, 16));
00094
00095 clearButton->setVisible(text.length());
00096 }
00097
00098 void KLineEdit::updateClearButton()
00099 {
00100 if(!clearButton || isReadOnly())
00101 {
00102 return;
00103 }
00104
00105 const QSize geom = size();
00106 const int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth,0,this);
00107 const int buttonWidth = clearButton->sizeHint().width();
00108 const QSize newButtonSize(buttonWidth, geom.height());
00109 const QFontMetrics fm(font());
00110 const int em = fm.width("m");
00111
00112
00113
00114 const bool wideEnough = geom.width() > 4 * em + buttonWidth + frameWidth;
00115
00116 if(newButtonSize != clearButton->size())
00117 {
00118 clearButton->resize(newButtonSize);
00119 overlap = wideEnough ? buttonWidth + frameWidth : 0;
00120 }
00121
00122 if(layoutDirection() == Qt::LeftToRight)
00123 {
00124 clearButton->move(geom.width() - frameWidth - buttonWidth - 1, 0);
00125 }
00126 else
00127 {
00128 clearButton->move(frameWidth + 1, 0);
00129 }
00130
00131 if(wideEnough != wideEnoughForClear)
00132 {
00133
00134
00135
00136 wideEnoughForClear = wideEnough;
00137 updateClearButtonIcon(text());
00138 }
00139 }
00140
00141 void KLineEdit::resizeEvent(QResizeEvent *ev)
00142 {
00143 updateClearButton();
00144 QLineEdit::resizeEvent(ev);
00145 }
00146
00147 void KLineEdit::mousePressEvent(QMouseEvent *e)
00148 {
00149 if((e->button() == Qt::LeftButton || e->button() == Qt::MidButton ) && clearButton)
00150 {
00151 clickInClear = clearButton->underMouse();
00152 }
00153 QLineEdit::mousePressEvent(e);
00154 }
00155
00156 void KLineEdit::mouseReleaseEvent(QMouseEvent *e)
00157 {
00158 if(clickInClear)
00159 {
00160 if(clearButton->underMouse())
00161 {
00162 QString newText;
00163 setSelection(0, text().size());
00164 del();
00165 emit clearButtonClicked();
00166 emit textChanged(newText);
00167 }
00168
00169 clickInClear = false;
00170 e->accept();
00171 return;
00172 }
00173 QLineEdit::mouseReleaseEvent(e);
00174 }