2018-02-27 18:06:05 +01:00
|
|
|
/*
|
|
|
|
* Strawberry Music Player
|
|
|
|
* This file was part of Clementine.
|
|
|
|
* Copyright 2010, David Sansome <me@davidsansome.com>
|
2021-03-20 21:14:47 +01:00
|
|
|
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
|
2018-02-27 18:06:05 +01:00
|
|
|
*
|
|
|
|
* Strawberry 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.
|
|
|
|
*
|
|
|
|
* Strawberry 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 Strawberry. If not, see <http://www.gnu.org/licenses/>.
|
2018-08-09 18:39:44 +02:00
|
|
|
*
|
2018-02-27 18:06:05 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QtGlobal>
|
|
|
|
#include <QWidget>
|
|
|
|
#include <QString>
|
|
|
|
#include <QFont>
|
|
|
|
#include <QFontMetrics>
|
2018-02-27 18:06:05 +01:00
|
|
|
#include <QPainter>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QPalette>
|
|
|
|
#include <QPaintDevice>
|
|
|
|
#include <QRect>
|
|
|
|
#include <QSize>
|
2018-02-27 18:06:05 +01:00
|
|
|
#include <QStyle>
|
|
|
|
#include <QStyleOption>
|
|
|
|
#include <QToolButton>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QLineEdit>
|
|
|
|
#include <QPlainTextEdit>
|
|
|
|
#include <QSpinBox>
|
|
|
|
#include <QFlags>
|
2020-02-09 02:29:35 +01:00
|
|
|
#include <QPaintEvent>
|
|
|
|
#include <QResizeEvent>
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-05-01 00:41:33 +02:00
|
|
|
#include "core/iconloader.h"
|
2018-02-27 18:06:05 +01:00
|
|
|
#include "lineedit.h"
|
|
|
|
|
|
|
|
ExtendedEditor::ExtendedEditor(QWidget *widget, int extra_right_padding, bool draw_hint)
|
|
|
|
: LineEditInterface(widget),
|
|
|
|
has_clear_button_(true),
|
|
|
|
clear_button_(new QToolButton(widget)),
|
|
|
|
reset_button_(new QToolButton(widget)),
|
|
|
|
extra_right_padding_(extra_right_padding),
|
|
|
|
draw_hint_(draw_hint),
|
|
|
|
font_point_size_(widget->font().pointSizeF() - 1),
|
|
|
|
is_rtl_(false) {
|
2019-04-08 18:46:11 +02:00
|
|
|
|
2018-02-27 18:06: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; }");
|
2021-06-22 14:04:18 +02:00
|
|
|
clear_button_->setToolTip(QWidget::tr("Clear"));
|
2018-02-27 18:06:05 +01:00
|
|
|
clear_button_->setFocusPolicy(Qt::NoFocus);
|
|
|
|
|
|
|
|
QStyleOption opt;
|
|
|
|
opt.initFrom(widget);
|
|
|
|
|
|
|
|
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; }");
|
2021-06-22 14:04:18 +02:00
|
|
|
reset_button_->setToolTip(QWidget::tr("Reset"));
|
2018-02-27 18:06:05 +01:00
|
|
|
reset_button_->setFocusPolicy(Qt::NoFocus);
|
|
|
|
reset_button_->hide();
|
|
|
|
|
2021-01-26 16:48:04 +01:00
|
|
|
if (LineEdit *lineedit = qobject_cast<LineEdit*>(widget)) {
|
|
|
|
QObject::connect(clear_button_, &QToolButton::clicked, lineedit, &LineEdit::set_focus);
|
|
|
|
QObject::connect(clear_button_, &QToolButton::clicked, lineedit, &LineEdit::clear);
|
|
|
|
}
|
|
|
|
else if (TextEdit *textedit = qobject_cast<TextEdit*>(widget)) {
|
|
|
|
QObject::connect(clear_button_, &QToolButton::clicked, textedit, &TextEdit::set_focus);
|
|
|
|
QObject::connect(clear_button_, &QToolButton::clicked, textedit, &TextEdit::clear);
|
|
|
|
}
|
|
|
|
else if (SpinBox *spinbox = qobject_cast<SpinBox*>(widget)) {
|
|
|
|
QObject::connect(clear_button_, &QToolButton::clicked, spinbox, &SpinBox::set_focus);
|
|
|
|
QObject::connect(clear_button_, &QToolButton::clicked, spinbox, &SpinBox::clear);
|
2020-09-23 00:52:41 +02:00
|
|
|
}
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
UpdateButtonGeometry();
|
2019-04-08 18:46:11 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
2020-09-23 00:52:41 +02:00
|
|
|
void ExtendedEditor::set_hint(const QString &hint) {
|
2018-02-27 18:06:05 +01:00
|
|
|
hint_ = hint;
|
|
|
|
widget_->update();
|
|
|
|
}
|
|
|
|
|
2020-09-23 00:52:41 +02:00
|
|
|
void ExtendedEditor::set_clear_button(const bool visible) {
|
2018-02-27 18:06:05 +01:00
|
|
|
has_clear_button_ = visible;
|
|
|
|
clear_button_->setVisible(visible);
|
|
|
|
UpdateButtonGeometry();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ExtendedEditor::has_reset_button() const {
|
|
|
|
return reset_button_->isVisible();
|
|
|
|
}
|
|
|
|
|
2020-09-23 00:52:41 +02:00
|
|
|
void ExtendedEditor::set_reset_button(const bool visible) {
|
2018-02-27 18:06:05 +01:00
|
|
|
reset_button_->setVisible(visible);
|
|
|
|
UpdateButtonGeometry();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExtendedEditor::UpdateButtonGeometry() {
|
|
|
|
|
|
|
|
const int frame_width = widget_->style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
|
|
|
|
const int left = frame_width + 1 + (has_clear_button() ? clear_button_->sizeHint().width() : 0);
|
|
|
|
const int right = frame_width + 1 + (has_reset_button() ? reset_button_->sizeHint().width() : 0);
|
|
|
|
|
2018-05-01 00:41:33 +02:00
|
|
|
widget_->setStyleSheet(QString("QLineEdit { padding-left: %1px; padding-right: %2px; }").arg(left).arg(right));
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
QSize msz = widget_->minimumSizeHint();
|
|
|
|
widget_->setMinimumSize(msz.width() + (clear_button_->sizeHint().width() + frame_width + 1) * 2 + extra_right_padding_, qMax(msz.height(), clear_button_->sizeHint().height() + frame_width * 2 + 2));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExtendedEditor::Paint(QPaintDevice *device) {
|
|
|
|
|
|
|
|
if (!widget_->hasFocus() && is_empty() && !hint_.isEmpty()) {
|
|
|
|
clear_button_->hide();
|
|
|
|
|
|
|
|
if (draw_hint_) {
|
|
|
|
QPainter p(device);
|
|
|
|
|
|
|
|
QFont font;
|
|
|
|
font.setBold(false);
|
|
|
|
font.setPointSizeF(font_point_size_);
|
|
|
|
|
|
|
|
QFontMetrics m(font);
|
|
|
|
const int kBorder = (device->height() - m.height()) / 2;
|
|
|
|
|
|
|
|
p.setPen(widget_->palette().color(QPalette::Disabled, QPalette::Text));
|
|
|
|
p.setFont(font);
|
|
|
|
|
|
|
|
QRect r(5, kBorder, device->width() - 10, device->height() - kBorder*2);
|
|
|
|
p.drawText(r, Qt::AlignLeft | Qt::AlignVCenter, m.elidedText(hint_, Qt::ElideRight, r.width()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
clear_button_->setVisible(has_clear_button_);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExtendedEditor::Resize() {
|
|
|
|
|
|
|
|
const QSize sz = clear_button_->sizeHint();
|
|
|
|
const int frame_width = widget_->style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
|
|
|
|
const int y = (widget_->rect().height() - sz.height()) / 2;
|
|
|
|
|
|
|
|
clear_button_->move(frame_width, y);
|
|
|
|
|
|
|
|
if (!is_rtl_) {
|
|
|
|
reset_button_->move(widget_->width() - frame_width - sz.width() - extra_right_padding_, y);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
reset_button_->move((has_clear_button() ? sz.width() + 4 : 0) + frame_width, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
LineEdit::LineEdit(QWidget *parent) : QLineEdit(parent), ExtendedEditor(this) {
|
2021-01-26 16:48:04 +01:00
|
|
|
QObject::connect(reset_button_, &QToolButton::clicked, this, &LineEdit::Reset);
|
|
|
|
QObject::connect(this, &LineEdit::textChanged, this, &LineEdit::text_changed);
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
2020-09-23 00:52:41 +02:00
|
|
|
void LineEdit::text_changed(const QString &text) {
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
if (text.isEmpty()) {
|
|
|
|
// Consider empty string as LTR
|
|
|
|
set_rtl(false);
|
|
|
|
}
|
|
|
|
else {
|
2018-05-01 00:41:33 +02:00
|
|
|
// For some reason Qt will detect any text with LTR at the end as LTR, so instead compare only the first character
|
2018-02-27 18:06:05 +01:00
|
|
|
set_rtl(QString(text.at(0)).isRightToLeft());
|
|
|
|
}
|
|
|
|
Resize();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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),
|
2021-01-26 16:48:04 +01:00
|
|
|
ExtendedEditor(this) {
|
|
|
|
|
|
|
|
QObject::connect(reset_button_, &QToolButton::clicked, this, &TextEdit::Reset);
|
|
|
|
QObject::connect(this, &TextEdit::textChanged, [this]() { viewport()->update(); }); // To clear the hint
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2021-01-26 16:48:04 +01:00
|
|
|
QObject::connect(reset_button_, &QToolButton::clicked, this, &SpinBox::Reset);
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SpinBox::paintEvent(QPaintEvent *e) {
|
|
|
|
QSpinBox::paintEvent(e);
|
|
|
|
Paint(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpinBox::resizeEvent(QResizeEvent *e) {
|
|
|
|
QSpinBox::resizeEvent(e);
|
|
|
|
Resize();
|
|
|
|
}
|
|
|
|
|
2020-09-23 00:52:41 +02:00
|
|
|
CheckBox::CheckBox(QWidget *parent)
|
2021-01-26 16:48:04 +01:00
|
|
|
: QCheckBox(parent), ExtendedEditor(this, 14, false) {
|
|
|
|
|
|
|
|
QObject::connect(reset_button_, &QToolButton::clicked, this, &CheckBox::Reset);
|
|
|
|
|
2020-09-23 00:52:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CheckBox::paintEvent(QPaintEvent *e) {
|
|
|
|
QCheckBox::paintEvent(e);
|
|
|
|
Paint(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckBox::resizeEvent(QResizeEvent *e) {
|
|
|
|
QCheckBox::resizeEvent(e);
|
|
|
|
Resize();
|
|
|
|
}
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
QString SpinBox::textFromValue(int val) const {
|
2020-09-23 00:52:41 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
if (val <= 0 && !hint_.isEmpty())
|
|
|
|
return "-";
|
|
|
|
return QSpinBox::textFromValue(val);
|
2020-09-23 00:52:41 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|