2011-03-02 16:57:41 +01:00
|
|
|
#include "lineedit.h"
|
|
|
|
|
|
|
|
#include <QEvent>
|
|
|
|
#include <QLayout>
|
|
|
|
#include <QStyleOption>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QFocusEvent>
|
|
|
|
|
|
|
|
#include <qdebug.h>
|
2011-11-06 17:01:23 +01:00
|
|
|
SideWidget::SideWidget(QWidget* parent)
|
2011-03-02 16:57:41 +01:00
|
|
|
: QWidget(parent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
bool SideWidget::event(QEvent* event)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2011-11-06 17:01:23 +01:00
|
|
|
if (event->type() == QEvent::LayoutRequest) {
|
2011-03-02 16:57:41 +01:00
|
|
|
emit sizeHintChanged();
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
return QWidget::event(event);
|
|
|
|
}
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
LineEdit::LineEdit(QWidget* parent)
|
2011-03-02 16:57:41 +01:00
|
|
|
: QLineEdit(parent)
|
|
|
|
, m_leftLayout(0)
|
|
|
|
, m_rightLayout(0)
|
|
|
|
, m_leftMargin(0)
|
|
|
|
{
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
LineEdit::LineEdit(const QString &contents, QWidget* parent)
|
2011-03-02 16:57:41 +01:00
|
|
|
: QLineEdit(contents, parent)
|
|
|
|
, m_leftWidget(0)
|
|
|
|
, m_rightWidget(0)
|
|
|
|
, m_leftLayout(0)
|
|
|
|
, m_rightLayout(0)
|
|
|
|
, m_leftMargin(0)
|
|
|
|
{
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LineEdit::setLeftMargin(int margin)
|
|
|
|
{
|
|
|
|
m_leftMargin = margin;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LineEdit::init()
|
|
|
|
{
|
|
|
|
m_leftWidget = new SideWidget(this);
|
|
|
|
m_leftWidget->resize(0, 0);
|
|
|
|
m_leftLayout = new QHBoxLayout(m_leftWidget);
|
|
|
|
m_leftLayout->setContentsMargins(0, 0, 0, 0);
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
if (isRightToLeft()) {
|
2011-03-02 16:57:41 +01:00
|
|
|
m_leftLayout->setDirection(QBoxLayout::RightToLeft);
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
else {
|
2011-03-02 16:57:41 +01:00
|
|
|
m_leftLayout->setDirection(QBoxLayout::LeftToRight);
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
m_leftLayout->setSizeConstraint(QLayout::SetFixedSize);
|
|
|
|
|
|
|
|
m_rightWidget = new SideWidget(this);
|
|
|
|
m_rightWidget->resize(0, 0);
|
|
|
|
m_rightLayout = new QHBoxLayout(m_rightWidget);
|
2011-11-06 17:01:23 +01:00
|
|
|
if (isRightToLeft()) {
|
2011-03-02 16:57:41 +01:00
|
|
|
m_rightLayout->setDirection(QBoxLayout::RightToLeft);
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
else {
|
2011-03-02 16:57:41 +01:00
|
|
|
m_rightLayout->setDirection(QBoxLayout::LeftToRight);
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
m_rightLayout->setContentsMargins(0, 0, 0, 0);
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
QSpacerItem* horizontalSpacer = new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum);
|
2011-03-02 16:57:41 +01:00
|
|
|
m_rightLayout->addItem(horizontalSpacer);
|
|
|
|
|
|
|
|
setWidgetSpacing(3);
|
|
|
|
connect(m_leftWidget, SIGNAL(sizeHintChanged()),
|
|
|
|
this, SLOT(updateTextMargins()));
|
|
|
|
connect(m_rightWidget, SIGNAL(sizeHintChanged()),
|
|
|
|
this, SLOT(updateTextMargins()));
|
|
|
|
}
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
bool LineEdit::event(QEvent* event)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
|
|
|
if (event->type() == QEvent::LayoutDirectionChange) {
|
|
|
|
if (isRightToLeft()) {
|
|
|
|
m_leftLayout->setDirection(QBoxLayout::RightToLeft);
|
|
|
|
m_rightLayout->setDirection(QBoxLayout::RightToLeft);
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
else {
|
2011-03-02 16:57:41 +01:00
|
|
|
m_leftLayout->setDirection(QBoxLayout::LeftToRight);
|
|
|
|
m_rightLayout->setDirection(QBoxLayout::LeftToRight);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QLineEdit::event(event);
|
|
|
|
}
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
void LineEdit::addWidget(QWidget* widget, WidgetPosition position)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!widget) {
|
2011-03-02 16:57:41 +01:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
|
|
|
|
bool rtl = isRightToLeft();
|
2011-11-06 17:01:23 +01:00
|
|
|
if (rtl) {
|
2011-03-02 16:57:41 +01:00
|
|
|
position = (position == LeftSide) ? RightSide : LeftSide;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
if (position == LeftSide) {
|
|
|
|
m_leftLayout->addWidget(widget);
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
else {
|
2011-03-02 16:57:41 +01:00
|
|
|
m_rightLayout->insertWidget(1, widget);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
void LineEdit::removeWidget(QWidget* widget)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!widget) {
|
2011-03-02 16:57:41 +01:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
|
|
|
|
m_leftLayout->removeWidget(widget);
|
|
|
|
m_rightLayout->removeWidget(widget);
|
|
|
|
widget->hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LineEdit::setWidgetSpacing(int spacing)
|
|
|
|
{
|
|
|
|
m_leftLayout->setSpacing(spacing);
|
|
|
|
m_rightLayout->setSpacing(spacing);
|
|
|
|
updateTextMargins();
|
|
|
|
}
|
|
|
|
|
|
|
|
int LineEdit::widgetSpacing() const
|
|
|
|
{
|
|
|
|
return m_leftLayout->spacing();
|
|
|
|
}
|
|
|
|
|
|
|
|
int LineEdit::textMargin(WidgetPosition position) const
|
|
|
|
{
|
|
|
|
int spacing = m_rightLayout->spacing();
|
|
|
|
int w = 0;
|
2011-11-06 17:01:23 +01:00
|
|
|
if (position == LeftSide) {
|
2011-03-02 16:57:41 +01:00
|
|
|
w = m_leftWidget->sizeHint().width();
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
else {
|
2011-03-02 16:57:41 +01:00
|
|
|
w = m_rightWidget->sizeHint().width();
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
if (w == 0) {
|
2011-03-02 16:57:41 +01:00
|
|
|
return 0;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
return w + spacing * 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LineEdit::updateTextMargins()
|
|
|
|
{
|
|
|
|
int left;
|
2011-11-06 17:01:23 +01:00
|
|
|
if (m_leftMargin == 0) {
|
2011-03-02 16:57:41 +01:00
|
|
|
left = textMargin(LineEdit::LeftSide);
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
else {
|
2011-03-02 16:57:41 +01:00
|
|
|
left = m_leftMargin;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
int right = textMargin(LineEdit::RightSide);
|
|
|
|
int top = 0;
|
|
|
|
int bottom = 0;
|
|
|
|
setTextMargins(left, top, right, bottom);
|
|
|
|
updateSideWidgetLocations();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LineEdit::updateSideWidgetLocations()
|
|
|
|
{
|
|
|
|
QStyleOptionFrameV2 opt;
|
|
|
|
initStyleOption(&opt);
|
|
|
|
QRect textRect = style()->subElementRect(QStyle::SE_LineEditContents, &opt, this);
|
|
|
|
int spacing = m_rightLayout->spacing();
|
|
|
|
textRect.adjust(spacing, 0, -spacing, 0);
|
|
|
|
|
|
|
|
int left = textMargin(LineEdit::LeftSide);
|
|
|
|
|
|
|
|
int midHeight = textRect.center().y() + 1;
|
|
|
|
|
|
|
|
if (m_leftLayout->count() > 0) {
|
|
|
|
int leftHeight = midHeight - m_leftWidget->height() / 2;
|
|
|
|
int leftWidth = m_leftWidget->width();
|
2011-11-06 17:01:23 +01:00
|
|
|
if (leftWidth == 0) {
|
2011-03-02 16:57:41 +01:00
|
|
|
leftHeight = midHeight - m_leftWidget->sizeHint().height() / 2;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
m_leftWidget->move(textRect.x(), leftHeight);
|
|
|
|
}
|
|
|
|
textRect.setX(left);
|
|
|
|
textRect.setY(midHeight - m_rightWidget->sizeHint().height() / 2);
|
|
|
|
textRect.setHeight(m_rightWidget->sizeHint().height());
|
|
|
|
m_rightWidget->setGeometry(textRect);
|
|
|
|
}
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
void LineEdit::resizeEvent(QResizeEvent* event)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
|
|
|
updateSideWidgetLocations();
|
|
|
|
QLineEdit::resizeEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString LineEdit::inactiveText() const
|
|
|
|
{
|
|
|
|
return m_inactiveText;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LineEdit::setInactiveText(const QString &text)
|
|
|
|
{
|
|
|
|
m_inactiveText = text;
|
|
|
|
}
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
void LineEdit::paintEvent(QPaintEvent* event)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
|
|
|
QLineEdit::paintEvent(event);
|
|
|
|
if (text().isEmpty() && !m_inactiveText.isEmpty() && !hasFocus()) {
|
|
|
|
QStyleOptionFrameV2 panel;
|
|
|
|
initStyleOption(&panel);
|
|
|
|
QRect textRect = style()->subElementRect(QStyle::SE_LineEditContents, &panel, this);
|
|
|
|
int horizontalMargin = 2;
|
|
|
|
textRect.adjust(horizontalMargin, 0, 0, 0);
|
|
|
|
int left = textMargin(LineEdit::LeftSide);
|
|
|
|
int right = textMargin(LineEdit::RightSide);
|
|
|
|
textRect.adjust(left, 0, -right, 0);
|
|
|
|
QPainter painter(this);
|
|
|
|
painter.setPen(palette().brush(QPalette::Disabled, QPalette::Text).color());
|
|
|
|
painter.drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter, m_inactiveText);
|
|
|
|
}
|
|
|
|
}
|