|
|
|
@ -28,7 +28,7 @@
|
|
|
|
|
* Ulrich Van Den Hekke ( 21/03/2007 ) Simplified version of KLineEdit from KDE4.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "xinxlineedit.h"
|
|
|
|
|
#include "xinxlineedit_p.h"
|
|
|
|
|
|
|
|
|
|
#include <QStyleOption>
|
|
|
|
|
#include <QApplication>
|
|
|
|
@ -86,72 +86,25 @@ void XinxLineEditButton::paintEvent(QPaintEvent *event)
|
|
|
|
|
p.drawPixmap((width() - m_pixmap.width()) / 2, (height() - m_pixmap.height()) / 2, m_pixmap);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* XinxLineEditPrivate */
|
|
|
|
|
|
|
|
|
|
/* XinxLineEdit */
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \ingroup Components
|
|
|
|
|
* \class XinxLineEdit
|
|
|
|
|
* \since 0.9.0.0
|
|
|
|
|
*
|
|
|
|
|
* \brief The class XinxLineEdit provides a line edit with a clear button.
|
|
|
|
|
*
|
|
|
|
|
* QLineEdit with an include button to clear the line edit.This class is inspired
|
|
|
|
|
* from the class of the KDE project.
|
|
|
|
|
*
|
|
|
|
|
* \image html xinxlineedit.png
|
|
|
|
|
* \image latex xinxlineedit.png
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \brief Constructs a XinxLineEdit object with a default text, a parent, and a name.
|
|
|
|
|
*
|
|
|
|
|
* \param string Text to be shown in the edit widget.
|
|
|
|
|
* \param parent The parent widget of the line edit.
|
|
|
|
|
*/
|
|
|
|
|
XinxLineEdit::XinxLineEdit(const QString &string, QWidget *parent) : QLineEdit(string, parent)
|
|
|
|
|
{
|
|
|
|
|
init();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \brief Constructs a line edit
|
|
|
|
|
* \param parent The parent widget of the line edit.
|
|
|
|
|
*/
|
|
|
|
|
XinxLineEdit::XinxLineEdit(QWidget *parent) : QLineEdit(parent)
|
|
|
|
|
{
|
|
|
|
|
init();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//! Destroys the object
|
|
|
|
|
XinxLineEdit::~XinxLineEdit()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* \fn void XinxLineEdit::clearButtonClicked();
|
|
|
|
|
* \brief Emitted when the user clicked on the clear button
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
void XinxLineEdit::init()
|
|
|
|
|
void XinxLineEditPrivate::init()
|
|
|
|
|
{
|
|
|
|
|
clearButton = 0;
|
|
|
|
|
clickInClear = false;
|
|
|
|
|
wideEnoughForClear = true;
|
|
|
|
|
|
|
|
|
|
clearButton = new XinxLineEditButton(this);
|
|
|
|
|
clearButton = new XinxLineEditButton(edit);
|
|
|
|
|
clearButton->setCursor(Qt::ArrowCursor);
|
|
|
|
|
|
|
|
|
|
updateClearButtonIcon(text());
|
|
|
|
|
updateClearButtonIcon(edit->text());
|
|
|
|
|
//updateClearButton();
|
|
|
|
|
connect(this, SIGNAL(textChanged(QString)), this, SLOT(updateClearButtonIcon(QString)));
|
|
|
|
|
connect(edit, SIGNAL(textChanged(QString)), this, SLOT(updateClearButtonIcon(QString)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void XinxLineEdit::updateClearButtonIcon(const QString& text)
|
|
|
|
|
void XinxLineEditPrivate::updateClearButtonIcon(const QString& text)
|
|
|
|
|
{
|
|
|
|
|
if (isReadOnly())
|
|
|
|
|
if (edit->isReadOnly())
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
@ -170,7 +123,7 @@ void XinxLineEdit::updateClearButtonIcon(const QString& text)
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (layoutDirection() == Qt::LeftToRight)
|
|
|
|
|
if (edit->layoutDirection() == Qt::LeftToRight)
|
|
|
|
|
{
|
|
|
|
|
clearButton->setPixmap(QPixmap(":/images/edit-clear-locationbar-rtl.png"));
|
|
|
|
|
}
|
|
|
|
@ -182,20 +135,20 @@ void XinxLineEdit::updateClearButtonIcon(const QString& text)
|
|
|
|
|
clearButton->setVisible(text.length());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void XinxLineEdit::updateClearButton()
|
|
|
|
|
void XinxLineEditPrivate::updateClearButton()
|
|
|
|
|
{
|
|
|
|
|
if (isReadOnly())
|
|
|
|
|
if (edit->isReadOnly())
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const QSize geom = size();
|
|
|
|
|
const QSize geom = edit->size();
|
|
|
|
|
const int buttonWidth = clearButton->sizeHint().width();
|
|
|
|
|
const QSize newButtonSize(buttonWidth, geom.height());
|
|
|
|
|
|
|
|
|
|
const int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth, 0, this);
|
|
|
|
|
const int frameWidth = edit->style()->pixelMetric(QStyle::PM_DefaultFrameWidth, 0, edit);
|
|
|
|
|
|
|
|
|
|
const QFontMetrics fm(font());
|
|
|
|
|
const QFontMetrics fm(edit->font());
|
|
|
|
|
const int em = fm.width("m");
|
|
|
|
|
|
|
|
|
|
const bool wideEnough = geom.width() > 4 * em + buttonWidth + frameWidth;
|
|
|
|
@ -205,7 +158,7 @@ void XinxLineEdit::updateClearButton()
|
|
|
|
|
clearButton->resize(newButtonSize);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (layoutDirection() == Qt::LeftToRight)
|
|
|
|
|
if (edit->layoutDirection() == Qt::LeftToRight)
|
|
|
|
|
{
|
|
|
|
|
clearButton->move(geom.width() - frameWidth - buttonWidth - 1, 0);
|
|
|
|
|
}
|
|
|
|
@ -217,17 +170,67 @@ void XinxLineEdit::updateClearButton()
|
|
|
|
|
if (wideEnough != wideEnoughForClear)
|
|
|
|
|
{
|
|
|
|
|
wideEnoughForClear = wideEnough;
|
|
|
|
|
updateClearButtonIcon(text());
|
|
|
|
|
updateClearButtonIcon(edit->text());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* XinxLineEdit */
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \ingroup Components
|
|
|
|
|
* \class XinxLineEdit
|
|
|
|
|
* \since 0.9.0.0
|
|
|
|
|
*
|
|
|
|
|
* \brief The class XinxLineEdit provides a line edit with a clear button.
|
|
|
|
|
*
|
|
|
|
|
* QLineEdit with an include button to clear the line edit.This class is inspired
|
|
|
|
|
* from the class of the KDE project.
|
|
|
|
|
*
|
|
|
|
|
* \image html xinxlineedit.png
|
|
|
|
|
* \image latex xinxlineedit.png
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \brief Constructs a XinxLineEdit object with a default text, a parent, and a name.
|
|
|
|
|
*
|
|
|
|
|
* \param string Text to be shown in the edit widget.
|
|
|
|
|
* \param parent The parent widget of the line edit.
|
|
|
|
|
*/
|
|
|
|
|
XinxLineEdit::XinxLineEdit(const QString &string, QWidget *parent) : QLineEdit(string, parent), d(new XinxLineEditPrivate)
|
|
|
|
|
{
|
|
|
|
|
d->edit = this;
|
|
|
|
|
d->init();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \brief Constructs a line edit
|
|
|
|
|
* \param parent The parent widget of the line edit.
|
|
|
|
|
*/
|
|
|
|
|
XinxLineEdit::XinxLineEdit(QWidget *parent) : QLineEdit(parent), d(new XinxLineEditPrivate)
|
|
|
|
|
{
|
|
|
|
|
d->edit = this;
|
|
|
|
|
d->init();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//! Destroys the object
|
|
|
|
|
XinxLineEdit::~XinxLineEdit()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* \fn void XinxLineEdit::clearButtonClicked();
|
|
|
|
|
* \brief Emitted when the user clicked on the clear button
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* Re-implemented for internal reasons. API not affected.
|
|
|
|
|
* \sa QLineEdit::resizeEvent().
|
|
|
|
|
*/
|
|
|
|
|
void XinxLineEdit::resizeEvent(QResizeEvent * ev)
|
|
|
|
|
{
|
|
|
|
|
updateClearButton();
|
|
|
|
|
d->updateClearButton();
|
|
|
|
|
QLineEdit::resizeEvent(ev);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -237,9 +240,9 @@ void XinxLineEdit::resizeEvent(QResizeEvent * ev)
|
|
|
|
|
*/
|
|
|
|
|
void XinxLineEdit::mousePressEvent(QMouseEvent* e)
|
|
|
|
|
{
|
|
|
|
|
if ((e->button() == Qt::LeftButton || e->button() == Qt::MidButton) && clearButton)
|
|
|
|
|
if ((e->button() == Qt::LeftButton || e->button() == Qt::MidButton) && d->clearButton)
|
|
|
|
|
{
|
|
|
|
|
clickInClear = clearButton->underMouse();
|
|
|
|
|
d->clickInClear = d->clearButton->underMouse();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QLineEdit::mousePressEvent(e);
|
|
|
|
@ -251,9 +254,9 @@ void XinxLineEdit::mousePressEvent(QMouseEvent* e)
|
|
|
|
|
*/
|
|
|
|
|
void XinxLineEdit::mouseReleaseEvent(QMouseEvent* e)
|
|
|
|
|
{
|
|
|
|
|
if (clickInClear)
|
|
|
|
|
if (d->clickInClear)
|
|
|
|
|
{
|
|
|
|
|
if (clearButton->underMouse())
|
|
|
|
|
if (d->clearButton->underMouse())
|
|
|
|
|
{
|
|
|
|
|
setSelection(0, text().size());
|
|
|
|
|
del();
|
|
|
|
@ -261,7 +264,7 @@ void XinxLineEdit::mouseReleaseEvent(QMouseEvent* e)
|
|
|
|
|
emit textChanged(QString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
clickInClear = false;
|
|
|
|
|
d->clickInClear = false;
|
|
|
|
|
e->accept();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|