strawberry-audio-player-win.../src/widgets/lineedit.h

185 lines
5.0 KiB
C
Raw Normal View History

2018-02-27 18:06:05 +01:00
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
*
* 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
*/
#ifndef LINEEDIT_H
#define LINEEDIT_H
#include "config.h"
#include <QtGlobal>
#include <QObject>
#include <QWidget>
#include <QString>
2018-02-27 18:06:05 +01:00
#include <QLineEdit>
#include <QPlainTextEdit>
#include <QSpinBox>
2020-02-09 02:29:35 +01:00
class QToolButton;
class QPaintDevice;
class QPaintEvent;
class QResizeEvent;
2018-02-27 18:06:05 +01:00
class LineEditInterface {
2020-02-09 02:29:35 +01:00
public:
2020-04-07 16:49:15 +02:00
explicit LineEditInterface(QWidget *widget) : widget_(widget) {}
2018-02-27 18:06:05 +01:00
QWidget *widget() const { return widget_; }
virtual ~LineEditInterface() {}
virtual void clear() { set_text(QString()); }
virtual void set_focus() = 0;
virtual QString text() const = 0;
virtual void set_text(const QString& text) = 0;
virtual QString hint() const = 0;
virtual void set_hint(const QString& hint) = 0;
virtual void clear_hint() = 0;
virtual void set_enabled(bool enabled) = 0;
2020-04-07 16:49:15 +02:00
protected:
2018-02-27 18:06:05 +01:00
QWidget *widget_;
};
class ExtendedEditor : public LineEditInterface {
2020-02-09 02:29:35 +01:00
public:
2020-04-07 16:49:15 +02:00
explicit ExtendedEditor(QWidget *widget, int extra_right_padding = 0, bool draw_hint = true);
2018-02-27 18:06:05 +01:00
virtual ~ExtendedEditor() {}
virtual bool is_empty() const { return text().isEmpty(); }
QString hint() const { return hint_; }
void set_hint(const QString& hint);
void clear_hint() { set_hint(QString()); }
bool has_clear_button() const { return has_clear_button_; }
void set_clear_button(bool visible);
bool has_reset_button() const;
void set_reset_button(bool visible);
qreal font_point_size() const { return font_point_size_; }
void set_font_point_size(qreal size) { font_point_size_ = size; }
2020-02-09 02:29:35 +01:00
protected:
2018-02-27 18:06:05 +01:00
void Paint(QPaintDevice *device);
void Resize();
2020-02-09 02:29:35 +01:00
private:
2018-02-27 18:06:05 +01:00
void UpdateButtonGeometry();
2020-02-09 02:29:35 +01:00
protected:
2018-02-27 18:06:05 +01:00
QString hint_;
bool has_clear_button_;
QToolButton *clear_button_;
QToolButton *reset_button_;
int extra_right_padding_;
bool draw_hint_;
qreal font_point_size_;
bool is_rtl_;
};
class LineEdit : public QLineEdit, public ExtendedEditor {
Q_OBJECT
2019-09-15 20:27:32 +02:00
Q_PROPERTY(QString hint READ hint WRITE set_hint)
Q_PROPERTY(qreal font_point_size READ font_point_size WRITE set_font_point_size)
Q_PROPERTY(bool has_clear_button READ has_clear_button WRITE set_clear_button)
Q_PROPERTY(bool has_reset_button READ has_reset_button WRITE set_reset_button)
2018-02-27 18:06:05 +01:00
public:
2020-04-07 16:49:15 +02:00
explicit LineEdit(QWidget *parent = nullptr);
2018-02-27 18:06:05 +01:00
// ExtendedEditor
void set_focus() { QLineEdit::setFocus(); }
QString text() const { return QLineEdit::text(); }
void set_text(const QString& text) { QLineEdit::setText(text); }
void set_enabled(bool enabled) { QLineEdit::setEnabled(enabled); }
2020-02-09 02:29:35 +01:00
protected:
2018-02-27 18:06:05 +01:00
void paintEvent(QPaintEvent*);
void resizeEvent(QResizeEvent*);
private:
bool is_rtl() const { return is_rtl_; }
void set_rtl(bool rtl) { is_rtl_ = rtl; }
private slots:
void text_changed(const QString& text);
2020-02-09 02:29:35 +01:00
signals:
2018-02-27 18:06:05 +01:00
void Reset();
};
class TextEdit : public QPlainTextEdit, public ExtendedEditor {
Q_OBJECT
2019-09-15 20:27:32 +02:00
Q_PROPERTY(QString hint READ hint WRITE set_hint)
Q_PROPERTY(bool has_clear_button READ has_clear_button WRITE set_clear_button)
Q_PROPERTY(bool has_reset_button READ has_reset_button WRITE set_reset_button)
2018-02-27 18:06:05 +01:00
public:
2020-04-07 16:49:15 +02:00
explicit TextEdit(QWidget *parent = nullptr);
2018-02-27 18:06:05 +01:00
// ExtendedEditor
void set_focus() { QPlainTextEdit::setFocus(); }
QString text() const { return QPlainTextEdit::toPlainText(); }
void set_text(const QString& text) { QPlainTextEdit::setPlainText(text); }
void set_enabled(bool enabled) { QPlainTextEdit::setEnabled(enabled); }
2020-02-09 02:29:35 +01:00
protected:
2018-02-27 18:06:05 +01:00
void paintEvent(QPaintEvent*);
void resizeEvent(QResizeEvent*);
2020-02-09 02:29:35 +01:00
signals:
2018-02-27 18:06:05 +01:00
void Reset();
};
class SpinBox : public QSpinBox, public ExtendedEditor {
Q_OBJECT
2019-09-15 20:27:32 +02:00
Q_PROPERTY(QString hint READ hint WRITE set_hint)
Q_PROPERTY(bool has_clear_button READ has_clear_button WRITE set_clear_button)
Q_PROPERTY(bool has_reset_button READ has_reset_button WRITE set_reset_button)
2018-02-27 18:06:05 +01:00
public:
2020-04-07 16:49:15 +02:00
explicit SpinBox(QWidget *parent = nullptr);
2018-02-27 18:06:05 +01:00
// QSpinBox
QString textFromValue(int val) const;
// ExtendedEditor
bool is_empty() const { return text().isEmpty() || text() == "0"; }
void set_focus() { QSpinBox::setFocus(); }
QString text() const { return QSpinBox::text(); }
void set_text(const QString& text) { QSpinBox::setValue(text.toInt()); }
void set_enabled(bool enabled) { QSpinBox::setEnabled(enabled); }
2020-02-09 02:29:35 +01:00
protected:
2018-02-27 18:06:05 +01:00
void paintEvent(QPaintEvent*);
void resizeEvent(QResizeEvent*);
2020-02-09 02:29:35 +01:00
signals:
2018-02-27 18:06:05 +01:00
void Reset();
};
#endif // LINEEDIT_H