2013-02-10 12:28:53 +01:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
|
|
|
* Copyright (C) 2010-2013 David Rosca <nowrep@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
* ============================================================ */
|
2011-03-02 16:57:41 +01:00
|
|
|
#include "lineedit.h"
|
2013-02-12 08:16:05 +01:00
|
|
|
#include "qzsettings.h"
|
2011-03-02 16:57:41 +01:00
|
|
|
|
|
|
|
#include <QEvent>
|
|
|
|
#include <QLayout>
|
|
|
|
#include <QStyleOption>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QFocusEvent>
|
|
|
|
|
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)
|
2013-02-12 08:16:05 +01:00
|
|
|
, m_leftMargin(-1)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
|
|
|
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()
|
|
|
|
{
|
2013-02-10 12:28:53 +01:00
|
|
|
// We use setTextMargins() instead of padding property, and we should
|
|
|
|
// uncomment following line or just update padding property of LineEdit's
|
|
|
|
// subclasses in all themes and use same value for padding-left and padding-right,
|
|
|
|
// with this new implementation padding-left and padding-right show padding from
|
|
|
|
// edges of m_leftWidget and m_rightWidget.
|
2012-08-13 15:41:08 +02:00
|
|
|
|
2012-08-11 13:13:37 +02:00
|
|
|
mainLayout = new QHBoxLayout(this);
|
|
|
|
mainLayout->setContentsMargins(0, 0, 0, 0);
|
|
|
|
mainLayout->setSpacing(0);
|
|
|
|
|
2011-03-02 16:57:41 +01:00
|
|
|
m_leftWidget = new SideWidget(this);
|
|
|
|
m_leftWidget->resize(0, 0);
|
|
|
|
m_leftLayout = new QHBoxLayout(m_leftWidget);
|
2012-08-11 13:13:37 +02:00
|
|
|
m_leftLayout->setContentsMargins(0, 0, 2, 0);
|
2011-03-02 16:57:41 +01:00
|
|
|
|
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
|
|
|
|
2012-08-11 13:13:37 +02:00
|
|
|
m_rightLayout->setContentsMargins(0, 0, 2, 0);
|
2011-11-06 17:01:23 +01:00
|
|
|
QSpacerItem* horizontalSpacer = new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum);
|
2012-08-11 13:13:37 +02:00
|
|
|
|
2012-08-13 15:41:08 +02:00
|
|
|
mainLayout->addWidget(m_leftWidget, 0, Qt::AlignVCenter | Qt::AlignLeft);
|
2012-08-11 13:13:37 +02:00
|
|
|
mainLayout->addItem(horizontalSpacer);
|
2012-08-13 15:41:08 +02:00
|
|
|
mainLayout->addWidget(m_rightWidget, 0, Qt::AlignVCenter | Qt::AlignRight);
|
2012-12-20 14:45:35 +01:00
|
|
|
// by this we undo reversing of layout when direction is RTL.
|
|
|
|
// TODO: don't do this and show reversed icon when needed
|
2012-08-11 13:13:37 +02:00
|
|
|
mainLayout->setDirection(isRightToLeft() ? QBoxLayout::RightToLeft : QBoxLayout::LeftToRight);
|
2011-03-02 16:57:41 +01:00
|
|
|
|
|
|
|
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) {
|
2012-08-11 13:13:37 +02:00
|
|
|
//by this we undo reversing of layout when direction is RTL.
|
2011-03-02 16:57:41 +01:00
|
|
|
if (isRightToLeft()) {
|
2012-08-11 13:13:37 +02:00
|
|
|
mainLayout->setDirection(QBoxLayout::RightToLeft);
|
2011-03-02 16:57:41 +01:00
|
|
|
m_leftLayout->setDirection(QBoxLayout::RightToLeft);
|
|
|
|
m_rightLayout->setDirection(QBoxLayout::RightToLeft);
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
else {
|
2012-08-11 13:13:37 +02:00
|
|
|
mainLayout->setDirection(QBoxLayout::LeftToRight);
|
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
|
|
|
if (position == LeftSide) {
|
|
|
|
m_leftLayout->addWidget(widget);
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
else {
|
2012-08-11 13:13:37 +02:00
|
|
|
m_rightLayout->addWidget(widget);
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
{
|
2013-02-12 08:16:05 +01:00
|
|
|
int left = m_leftMargin < 0 ? m_leftWidget->sizeHint().width() : m_leftMargin;
|
2012-08-11 13:13:37 +02:00
|
|
|
int right = m_rightWidget->sizeHint().width();
|
2011-03-02 16:57:41 +01:00
|
|
|
int top = 0;
|
|
|
|
int bottom = 0;
|
|
|
|
setTextMargins(left, top, right, bottom);
|
2013-02-10 12:28:53 +01:00
|
|
|
// updateSideWidgetLocations();
|
|
|
|
}
|
|
|
|
|
2013-02-12 08:16:05 +01:00
|
|
|
void LineEdit::mousePressEvent(QMouseEvent* event)
|
|
|
|
{
|
|
|
|
if (cursorPosition() == 0 && qzSettings->selectAllOnClick) {
|
|
|
|
selectAll();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QLineEdit::mousePressEvent(event);
|
|
|
|
}
|
|
|
|
|
2013-02-10 12:28:53 +01:00
|
|
|
void LineEdit::mouseReleaseEvent(QMouseEvent* event)
|
|
|
|
{
|
|
|
|
// Workaround issue in QLineEdit::setDragEnabled(true)
|
|
|
|
// It will incorrectly set cursor position at the end
|
|
|
|
// of selection when clicking (and not dragging) into selected text
|
|
|
|
|
|
|
|
if (!dragEnabled()) {
|
|
|
|
QLineEdit::mouseReleaseEvent(event);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool wasSelectedText = !selectedText().isEmpty();
|
|
|
|
|
|
|
|
QLineEdit::mouseReleaseEvent(event);
|
|
|
|
|
|
|
|
bool isSelectedText = !selectedText().isEmpty();
|
|
|
|
|
|
|
|
if (wasSelectedText && !isSelectedText) {
|
|
|
|
QMouseEvent ev(QEvent::MouseButtonPress, event->pos(), event->button(),
|
|
|
|
event->buttons(), event->modifiers());
|
|
|
|
mousePressEvent(&ev);
|
|
|
|
}
|
2012-08-11 13:13:37 +02:00
|
|
|
}
|
|
|
|
|
2013-02-12 08:16:05 +01:00
|
|
|
void LineEdit::mouseDoubleClickEvent(QMouseEvent* event)
|
|
|
|
{
|
|
|
|
if (event->button() == Qt::LeftButton && qzSettings->selectAllOnDoubleClick) {
|
|
|
|
selectAll();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QLineEdit::mouseDoubleClickEvent(event);
|
|
|
|
}
|