1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
/* This file is (c) 2012 Tvangeste <i.4m.l33t@yandex.ru>
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
#include "extlineedit.hh"
#include <QPainter>
#include <QPropertyAnimation>
ExtLineEdit::ExtLineEdit(QWidget *parent) :
QLineEdit(parent)
{
for (int i = 0; i < 2; ++i) {
iconButtons[i] = new IconButton(this);
iconButtons[i]->installEventFilter(this);
iconButtons[i]->hide();
iconButtons[i]->setAutoHide(false);
iconEnabled[i] = false;
}
ensurePolished();
updateMargins();
connect( iconButtons[ Left ], &QAbstractButton::clicked, this, &ExtLineEdit::iconClicked );
connect( iconButtons[ Right ], &QAbstractButton::clicked, this, &ExtLineEdit::iconClicked );
connect( this, &QLineEdit::textChanged, this, &ExtLineEdit::updateButtons );
}
ExtLineEdit::~ExtLineEdit()
{
}
void ExtLineEdit::setButtonVisible(Side side, bool visible)
{
iconButtons[side]->setVisible(visible);
iconEnabled[side] = visible;
updateMargins();
}
bool ExtLineEdit::isButtonVisible(Side side) const
{
return iconEnabled[side];
}
void ExtLineEdit::setButtonAutoHide(Side side, bool autohide)
{
iconButtons[side]->setAutoHide(autohide);
if (autohide)
{
iconButtons[side]->setOpacity( text().isEmpty() ? 0.0 : 1.0 );
}
else
{
iconButtons[side]->setOpacity( 1.0 );
}
}
void ExtLineEdit::updateButtons(QString text)
{
if ( oldText.isEmpty() || text.isEmpty() ) {
for (int i = 0; i < 2; ++i) {
if ( iconButtons[i]->isAutoHide() )
{
iconButtons[i]->animate( !text.isEmpty() );
}
}
oldText = text;
}
}
void ExtLineEdit::iconClicked()
{
IconButton * button = qobject_cast<IconButton *>( sender() );
int index = -1;
for (int i = 0; i < 2; ++i)
if (iconButtons[i] == button)
index = i;
if (index == -1)
return;
if (index == Left)
emit leftButtonClicked();
else if (index == Right)
emit rightButtonClicked();
}
void ExtLineEdit::updateMargins()
{
bool leftToRight = (layoutDirection() == Qt::LeftToRight);
Side realLeft = (leftToRight ? Left : Right);
Side realRight = (leftToRight ? Right : Left);
int widgetHeight=height();
int leftMargin = widgetHeight + 8;
int rightMargin = widgetHeight + 8;
setTextMargins(
(iconEnabled[realLeft] ? leftMargin : 0), 1,
(iconEnabled[realRight] ? rightMargin : 0), 1);
}
void ExtLineEdit::updateButtonPositions()
{
QRect contentRect = rect();
for (int i = 0; i < 2; ++i) {
Side iconPos = (Side)i;
if (layoutDirection() == Qt::RightToLeft)
iconPos = (iconPos == Left ? Right : Left);
if (iconPos == ExtLineEdit::Right) {
QMargins qm=textMargins();
const int iconoffset = qm.right() + 4;
iconButtons[i]->setGeometry(contentRect.adjusted(width() - iconoffset, 0, 0, 0));
} else {
QMargins qm=textMargins();
const int iconoffset = qm.left() + 4;
iconButtons[i]->setGeometry(contentRect.adjusted(0, 0, -width() + iconoffset, 0));
}
}
}
void ExtLineEdit::resizeEvent(QResizeEvent *)
{
updateButtonPositions();
}
void ExtLineEdit::setButtonPixmap(Side side, const QPixmap &buttonPixmap)
{
iconButtons[side]->setPixmap(buttonPixmap);
updateMargins();
updateButtonPositions();
update();
}
QPixmap ExtLineEdit::buttonPixmap(Side side) const
{
return pixmaps[side];
}
void ExtLineEdit::setButtonToolTip(Side side, const QString &tip)
{
iconButtons[side]->setToolTip(tip);
}
void ExtLineEdit::setButtonFocusPolicy(Side side, Qt::FocusPolicy policy)
{
iconButtons[side]->setFocusPolicy(policy);
}
IconButton::IconButton(QWidget *parent)
: QAbstractButton(parent)
{
setCursor(Qt::ArrowCursor);
setFocusPolicy(Qt::NoFocus);
setFocusProxy(parent);
}
void IconButton::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setRenderHint(QPainter::RenderHint::Antialiasing);
painter.setRenderHint(QPainter::RenderHint::TextAntialiasing);
painter.setRenderHint(QPainter::RenderHint::SmoothPixmapTransform);
painter.setRenderHint(QPainter::RenderHint::LosslessImageRendering);
QRect pixmapRect = QRect(0, 0, height(), height());
pixmapRect.moveCenter(rect().center());
if (m_autohide)
{
painter.setOpacity(m_opacity);
}
painter.drawPixmap(pixmapRect, m_pixmap);
}
void IconButton::animate(bool visible)
{
QPropertyAnimation *animation = new QPropertyAnimation(this, "opacity");
animation->setDuration(250);
if (visible)
{
animation->setEndValue(1.0);
}
else
{
animation->setEndValue(0.0);
}
animation->start(QAbstractAnimation::DeleteWhenStopped);
}
|