1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

editable CroppingLabel

This commit is contained in:
krepa098 2014-09-01 11:00:06 +02:00
parent c54cfd6ff3
commit 7c0cdd710f
2 changed files with 91 additions and 1 deletions

View File

@ -15,11 +15,36 @@
*/
#include "croppinglabel.h"
#include <QResizeEvent>
CroppingLabel::CroppingLabel(QWidget* parent)
: QLabel(parent)
, blockPaintEvents(false)
, editable(false)
, elideMode(Qt::ElideRight)
{
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
textEdit = new QLineEdit(this);
textEdit->hide();
installEventFilter(this);
textEdit->installEventFilter(this);
}
void CroppingLabel::setEditable(bool editable)
{
this->editable = editable;
if (editable)
setCursor(Qt::PointingHandCursor);
else
unsetCursor();
}
void CroppingLabel::setEdlideMode(Qt::TextElideMode elide)
{
elideMode = elide;
}
void CroppingLabel::setText(const QString& text)
@ -31,6 +56,8 @@ void CroppingLabel::setText(const QString& text)
void CroppingLabel::resizeEvent(QResizeEvent* ev)
{
setElidedText();
textEdit->resize(ev->size());
QLabel::resizeEvent(ev);
}
@ -44,9 +71,50 @@ QSize CroppingLabel::minimumSizeHint() const
return QSize(fontMetrics().width("..."), QLabel::minimumSizeHint().height());
}
void CroppingLabel::mouseReleaseEvent(QMouseEvent *e)
{
if (editable)
{
blockPaintEvents = true;
textEdit->show();
textEdit->setFocus();
}
QLabel::mouseReleaseEvent(e);
}
bool CroppingLabel::eventFilter(QObject *obj, QEvent *e)
{
// catch paint events if needed
if (obj == this)
{
if (e->type() == QEvent::Paint && blockPaintEvents)
return true;
}
// events fired by the QLineEdit
if (obj == textEdit)
{
if (e->type() == QEvent::KeyPress)
{
QKeyEvent* keyEvent = static_cast<QKeyEvent*>(e);
if (keyEvent->key() == Qt::Key_Return)
hideTextEdit(true);
if (keyEvent->key() == Qt::Key_Escape)
hideTextEdit(false);
}
if (e->type() == QEvent::FocusOut)
hideTextEdit(true);
}
return false;
}
void CroppingLabel::setElidedText()
{
QString elidedText = fontMetrics().elidedText(origText, Qt::ElideRight, width());
QString elidedText = fontMetrics().elidedText(origText, elideMode, width());
if (elidedText != origText)
setToolTip(origText);
else
@ -54,3 +122,12 @@ void CroppingLabel::setElidedText()
QLabel::setText(elidedText);
}
void CroppingLabel::hideTextEdit(bool acceptText)
{
if (acceptText)
setText(textEdit->text());
textEdit->hide();
blockPaintEvents = false;
}

View File

@ -18,6 +18,7 @@
#define CROPPINGLABEL_H
#include <QLabel>
#include <QLineEdit>
class CroppingLabel : public QLabel
{
@ -25,16 +26,28 @@ class CroppingLabel : public QLabel
public:
explicit CroppingLabel(QWidget *parent = 0);
void setEditable(bool editable);
void setEdlideMode(Qt::TextElideMode elide);
virtual void setText(const QString& text);
virtual void resizeEvent(QResizeEvent *ev);
virtual QSize sizeHint() const;
virtual QSize minimumSizeHint() const;
virtual void mouseReleaseEvent(QMouseEvent *e);
virtual bool eventFilter(QObject *obj, QEvent *e);
protected:
void setElidedText();
void acceptText();
void rejectText();
void hideTextEdit(bool acceptText);
private:
QString origText;
QLineEdit* textEdit;
bool blockPaintEvents;
bool editable;
Qt::TextElideMode elideMode;
};
#endif // CROPPINGLABEL_H