2010-03-24 00:11:46 +01:00
|
|
|
/* This file is part of Clementine.
|
2010-11-20 14:27:10 +01:00
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
2010-03-24 00:11:46 +01:00
|
|
|
|
|
|
|
Clementine 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.
|
|
|
|
|
|
|
|
Clementine 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 Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2010-01-16 17:12:47 +01:00
|
|
|
#include "lineedit.h"
|
|
|
|
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QPaintEvent>
|
2010-12-18 21:32:05 +01:00
|
|
|
#include <QStyle>
|
2010-12-20 00:40:36 +01:00
|
|
|
#include <QStyleOption>
|
2010-12-18 21:32:05 +01:00
|
|
|
#include <QToolButton>
|
2010-12-20 00:40:36 +01:00
|
|
|
#include <QtDebug>
|
2010-01-16 17:12:47 +01:00
|
|
|
|
2010-12-20 00:40:36 +01:00
|
|
|
ExtendedEditor::ExtendedEditor(QWidget* widget, int extra_right_padding,
|
|
|
|
bool draw_hint)
|
|
|
|
: LineEditInterface(widget),
|
2010-12-18 21:32:05 +01:00
|
|
|
has_clear_button_(true),
|
2010-12-20 00:40:36 +01:00
|
|
|
clear_button_(new QToolButton(widget)),
|
|
|
|
reset_button_(new QToolButton(widget)),
|
|
|
|
extra_right_padding_(extra_right_padding),
|
2011-09-17 15:04:34 +02:00
|
|
|
draw_hint_(draw_hint),
|
2014-01-11 09:45:13 +01:00
|
|
|
font_point_size_(widget->font().pointSizeF() - 1),
|
|
|
|
is_rtl_(false)
|
2010-01-16 17:12:47 +01:00
|
|
|
{
|
2010-12-18 21:32:05 +01:00
|
|
|
clear_button_->setIcon(IconLoader::Load("edit-clear-locationbar-ltr"));
|
|
|
|
clear_button_->setIconSize(QSize(16, 16));
|
|
|
|
clear_button_->setCursor(Qt::ArrowCursor);
|
|
|
|
clear_button_->setStyleSheet("QToolButton { border: none; padding: 0px; }");
|
2010-12-20 00:40:36 +01:00
|
|
|
clear_button_->setToolTip(widget->tr("Clear"));
|
|
|
|
clear_button_->setFocusPolicy(Qt::NoFocus);
|
|
|
|
|
|
|
|
QStyleOption opt;
|
|
|
|
opt.initFrom(widget);
|
2010-12-18 21:32:05 +01:00
|
|
|
|
2010-12-20 00:40:36 +01:00
|
|
|
reset_button_->setIcon(widget->style()->standardIcon(QStyle::SP_DialogResetButton, &opt, widget));
|
|
|
|
reset_button_->setIconSize(QSize(16, 16));
|
|
|
|
reset_button_->setCursor(Qt::ArrowCursor);
|
|
|
|
reset_button_->setStyleSheet("QToolButton { border: none; padding: 0px; }");
|
|
|
|
reset_button_->setToolTip(widget->tr("Reset"));
|
|
|
|
reset_button_->setFocusPolicy(Qt::NoFocus);
|
|
|
|
reset_button_->hide();
|
|
|
|
|
|
|
|
widget->connect(clear_button_, SIGNAL(clicked()), widget, SLOT(clear()));
|
|
|
|
widget->connect(clear_button_, SIGNAL(clicked()), widget, SLOT(setFocus()));
|
|
|
|
|
|
|
|
UpdateButtonGeometry();
|
2010-01-16 17:12:47 +01:00
|
|
|
}
|
|
|
|
|
2010-12-20 00:40:36 +01:00
|
|
|
void ExtendedEditor::set_hint(const QString& hint) {
|
2010-01-16 17:12:47 +01:00
|
|
|
hint_ = hint;
|
2010-12-20 00:40:36 +01:00
|
|
|
widget_->update();
|
2010-01-16 17:12:47 +01:00
|
|
|
}
|
|
|
|
|
2010-12-20 00:40:36 +01:00
|
|
|
void ExtendedEditor::set_clear_button(bool visible) {
|
2010-12-18 21:32:05 +01:00
|
|
|
has_clear_button_ = visible;
|
|
|
|
clear_button_->setVisible(visible);
|
2010-12-20 00:40:36 +01:00
|
|
|
UpdateButtonGeometry();
|
|
|
|
}
|
2010-12-18 21:32:05 +01:00
|
|
|
|
2010-12-20 00:40:36 +01:00
|
|
|
bool ExtendedEditor::has_reset_button() const {
|
|
|
|
return reset_button_->isVisible();
|
|
|
|
}
|
2010-12-18 21:32:05 +01:00
|
|
|
|
2010-12-20 00:40:36 +01:00
|
|
|
void ExtendedEditor::set_reset_button(bool visible) {
|
|
|
|
reset_button_->setVisible(visible);
|
|
|
|
UpdateButtonGeometry();
|
2010-12-18 21:32:05 +01:00
|
|
|
}
|
|
|
|
|
2010-12-20 00:40:36 +01:00
|
|
|
void ExtendedEditor::UpdateButtonGeometry() {
|
|
|
|
const int frame_width = widget_->style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
|
2014-01-12 20:50:03 +01:00
|
|
|
int left = frame_width + 1 + (
|
2010-12-20 00:40:36 +01:00
|
|
|
has_clear_button() ? clear_button_->sizeHint().width() : 0);
|
2014-01-12 20:50:03 +01:00
|
|
|
int right = frame_width + 1 + (
|
2010-12-20 00:40:36 +01:00
|
|
|
has_reset_button() ? reset_button_->sizeHint().width() : 0);
|
2014-01-12 20:50:03 +01:00
|
|
|
if (is_rtl()) {
|
|
|
|
qSwap(left, right);
|
|
|
|
}
|
2010-12-20 00:40:36 +01:00
|
|
|
widget_->setStyleSheet(
|
|
|
|
QString("QLineEdit { padding-left: %1px; padding-right: %2px; }")
|
|
|
|
.arg(left).arg(right));
|
|
|
|
|
|
|
|
QSize msz = widget_->minimumSizeHint();
|
2014-01-12 20:50:03 +01:00
|
|
|
int width = msz.width() + (clear_button_->sizeHint().width() + frame_width + 1) * 2 + extra_right_padding_;
|
|
|
|
int height = qMax(msz.height(), clear_button_->sizeHint().height() + frame_width * 2 + 2);
|
|
|
|
widget_->setMinimumSize(width, height);
|
2010-12-20 00:40:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ExtendedEditor::Paint(QPaintDevice* device) {
|
|
|
|
if (!widget_->hasFocus() && is_empty() && !hint_.isEmpty()) {
|
2010-12-18 21:32:05 +01:00
|
|
|
clear_button_->hide();
|
|
|
|
|
2010-12-20 00:40:36 +01:00
|
|
|
if (draw_hint_) {
|
|
|
|
QPainter p(device);
|
2010-01-16 17:12:47 +01:00
|
|
|
|
2010-12-20 00:40:36 +01:00
|
|
|
QFont font;
|
|
|
|
font.setItalic(true);
|
2011-09-17 15:04:34 +02:00
|
|
|
font.setBold(false);
|
|
|
|
font.setPointSizeF(font_point_size_);
|
2010-01-16 17:12:47 +01:00
|
|
|
|
2010-12-20 00:40:36 +01:00
|
|
|
QFontMetrics m(font);
|
|
|
|
const int kBorder = (device->height() - m.height()) / 2;
|
2010-01-16 17:12:47 +01:00
|
|
|
|
2010-12-20 00:40:36 +01:00
|
|
|
p.setPen(widget_->palette().color(QPalette::Disabled, QPalette::Text));
|
|
|
|
p.setFont(font);
|
2010-01-16 17:12:47 +01:00
|
|
|
|
2010-12-20 00:40:36 +01:00
|
|
|
QRect r(5, kBorder, device->width() - 10, device->height() - kBorder*2);
|
2011-11-08 19:22:01 +01:00
|
|
|
p.drawText(r, Qt::AlignLeft | Qt::AlignVCenter,
|
|
|
|
m.elidedText(hint_, Qt::ElideRight, r.width()));
|
2010-12-20 00:40:36 +01:00
|
|
|
}
|
2010-12-18 21:32:05 +01:00
|
|
|
} else {
|
|
|
|
clear_button_->setVisible(has_clear_button_);
|
2010-01-16 17:12:47 +01:00
|
|
|
}
|
|
|
|
}
|
2010-12-18 21:32:05 +01:00
|
|
|
|
2010-12-20 00:40:36 +01:00
|
|
|
void ExtendedEditor::Resize() {
|
2010-12-18 21:32:05 +01:00
|
|
|
const QSize sz = clear_button_->sizeHint();
|
2010-12-20 00:40:36 +01:00
|
|
|
const int frame_width = widget_->style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
|
2014-01-11 09:45:13 +01:00
|
|
|
const int y = (widget_->rect().height() - sz.height()) / 2;
|
|
|
|
|
2014-01-11 21:56:39 +01:00
|
|
|
clear_button_->move(frame_width, y);
|
2014-01-12 20:50:03 +01:00
|
|
|
reset_button_->move(widget_->width() - frame_width - sz.width() - extra_right_padding_, y);
|
2010-12-20 00:40:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LineEdit::LineEdit(QWidget* parent)
|
|
|
|
: QLineEdit(parent),
|
|
|
|
ExtendedEditor(this)
|
|
|
|
{
|
|
|
|
connect(reset_button_, SIGNAL(clicked()), SIGNAL(Reset()));
|
2014-01-12 17:40:28 +01:00
|
|
|
connect(this, SIGNAL(textChanged(QString)), SLOT(text_changed(QString)));
|
2010-12-20 00:40:36 +01:00
|
|
|
}
|
|
|
|
|
2014-01-18 00:22:46 +01:00
|
|
|
void LineEdit::set_rtl(bool rtl) {
|
|
|
|
if (rtl != is_rtl_) {
|
|
|
|
is_rtl_ = rtl;
|
|
|
|
UpdateButtonGeometry();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-12 17:40:28 +01:00
|
|
|
void LineEdit::text_changed(const QString& text) {
|
|
|
|
if (text.isEmpty()) {
|
|
|
|
// Consider empty string as LTR
|
|
|
|
set_rtl(false);
|
|
|
|
} else {
|
|
|
|
// For some reason Qt will detect any text with LTR at the end as LTR, so instead
|
|
|
|
// compare only the first character
|
2014-01-11 12:45:45 +01:00
|
|
|
set_rtl(QString(text.at(0)).isRightToLeft());
|
|
|
|
}
|
2014-01-11 09:45:13 +01:00
|
|
|
}
|
|
|
|
|
2010-12-20 00:40:36 +01:00
|
|
|
void LineEdit::paintEvent(QPaintEvent* e) {
|
|
|
|
QLineEdit::paintEvent(e);
|
|
|
|
Paint(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LineEdit::resizeEvent(QResizeEvent* e) {
|
|
|
|
QLineEdit::resizeEvent(e);
|
|
|
|
Resize();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TextEdit::TextEdit(QWidget* parent)
|
|
|
|
: QPlainTextEdit(parent),
|
|
|
|
ExtendedEditor(this)
|
|
|
|
{
|
|
|
|
connect(reset_button_, SIGNAL(clicked()), SIGNAL(Reset()));
|
|
|
|
connect(this, SIGNAL(textChanged()), viewport(), SLOT(update())); // To clear the hint
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextEdit::paintEvent(QPaintEvent* e) {
|
|
|
|
QPlainTextEdit::paintEvent(e);
|
|
|
|
Paint(viewport());
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextEdit::resizeEvent(QResizeEvent* e) {
|
|
|
|
QPlainTextEdit::resizeEvent(e);
|
|
|
|
Resize();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SpinBox::SpinBox(QWidget* parent)
|
|
|
|
: QSpinBox(parent),
|
|
|
|
ExtendedEditor(this, 14, false)
|
|
|
|
{
|
|
|
|
connect(reset_button_, SIGNAL(clicked()), SIGNAL(Reset()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpinBox::paintEvent(QPaintEvent* e) {
|
|
|
|
QSpinBox::paintEvent(e);
|
|
|
|
Paint(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpinBox::resizeEvent(QResizeEvent* e) {
|
|
|
|
QSpinBox::resizeEvent(e);
|
|
|
|
Resize();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString SpinBox::textFromValue(int val) const {
|
|
|
|
if (val <= 0 && !hint_.isEmpty())
|
|
|
|
return "-";
|
|
|
|
return QSpinBox::textFromValue(val);
|
2010-12-18 21:32:05 +01:00
|
|
|
}
|