1 /* This file is part of Clementine. 2 Copyright 2010, David Sansome <me@davidsansome.com> 3 4 Clementine is free software: you can redistribute it and/or modify 5 it under the terms of the GNU General Public License as published by 6 the Free Software Foundation, either version 3 of the License, or 7 (at your option) any later version. 8 9 Clementine is distributed in the hope that it will be useful, 10 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 GNU General Public License for more details. 13 14 You should have received a copy of the GNU General Public License 15 along with Clementine. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 #ifndef LINEEDIT_H 19 #define LINEEDIT_H 20 21 #include <QLineEdit> 22 #include <QPlainTextEdit> 23 #include <QSpinBox> 24 25 #include "ui/iconloader.h" 26 27 class QToolButton; 28 29 class LineEditInterface { 30 public: LineEditInterface(QWidget * widget)31 LineEditInterface(QWidget* widget) : widget_(widget) {} 32 widget()33 QWidget* widget() const { return widget_; } 34 ~LineEditInterface()35 virtual ~LineEditInterface() {} 36 clear()37 virtual void clear() { set_text(QString()); } 38 virtual void set_focus() = 0; 39 virtual QString text() const = 0; 40 virtual void set_text(const QString& text) = 0; 41 42 virtual QString hint() const = 0; 43 virtual void set_hint(const QString& hint) = 0; 44 virtual void clear_hint() = 0; 45 46 virtual void set_enabled(bool enabled) = 0; 47 48 protected: 49 QWidget* widget_; 50 }; 51 52 class ExtendedEditor : public LineEditInterface { 53 public: 54 ExtendedEditor(QWidget* widget, int extra_right_padding = 0, 55 bool draw_hint = true); ~ExtendedEditor()56 virtual ~ExtendedEditor() {} 57 is_empty()58 virtual bool is_empty() const { return text().isEmpty(); } 59 hint()60 QString hint() const { return hint_; } 61 void set_hint(const QString& hint); clear_hint()62 void clear_hint() { set_hint(QString()); } 63 has_clear_button()64 bool has_clear_button() const { return has_clear_button_; } 65 void set_clear_button(bool visible); 66 67 bool has_reset_button() const; 68 void set_reset_button(bool visible); 69 font_point_size()70 qreal font_point_size() const { return font_point_size_; } set_font_point_size(qreal size)71 void set_font_point_size(qreal size) { font_point_size_ = size; } 72 73 protected: 74 void Paint(QPaintDevice* device); 75 void Resize(); 76 77 private: 78 void UpdateButtonGeometry(); 79 80 protected: 81 QString hint_; 82 83 bool has_clear_button_; 84 QToolButton* clear_button_; 85 QToolButton* reset_button_; 86 87 int extra_right_padding_; 88 bool draw_hint_; 89 qreal font_point_size_; 90 bool is_rtl_; 91 }; 92 93 class LineEdit : public QLineEdit, public ExtendedEditor { 94 Q_OBJECT 95 Q_PROPERTY(QString hint READ hint WRITE set_hint) 96 Q_PROPERTY(qreal font_point_size READ font_point_size WRITE 97 set_font_point_size) 98 Q_PROPERTY(bool has_clear_button READ has_clear_button WRITE 99 set_clear_button) 100 Q_PROPERTY(bool has_reset_button READ has_reset_button WRITE 101 set_reset_button) 102 103 public: 104 LineEdit(QWidget* parent = nullptr); 105 106 // ExtendedEditor set_focus()107 void set_focus() { QLineEdit::setFocus(); } text()108 QString text() const { return QLineEdit::text(); } set_text(const QString & text)109 void set_text(const QString& text) { QLineEdit::setText(text); } set_enabled(bool enabled)110 void set_enabled(bool enabled) { QLineEdit::setEnabled(enabled); } 111 112 protected: 113 void paintEvent(QPaintEvent*); 114 void resizeEvent(QResizeEvent*); 115 116 private: is_rtl()117 bool is_rtl() const { return is_rtl_; } set_rtl(bool rtl)118 void set_rtl(bool rtl) { is_rtl_ = rtl; } 119 120 private slots: 121 void text_changed(const QString& text); 122 123 signals: 124 void Reset(); 125 }; 126 127 class TextEdit : public QPlainTextEdit, public ExtendedEditor { 128 Q_OBJECT 129 Q_PROPERTY(QString hint READ hint WRITE set_hint) 130 Q_PROPERTY(bool has_clear_button READ has_clear_button WRITE 131 set_clear_button) 132 Q_PROPERTY(bool has_reset_button READ has_reset_button WRITE 133 set_reset_button) 134 135 public: 136 TextEdit(QWidget* parent = nullptr); 137 138 // ExtendedEditor set_focus()139 void set_focus() { QPlainTextEdit::setFocus(); } text()140 QString text() const { return QPlainTextEdit::toPlainText(); } set_text(const QString & text)141 void set_text(const QString& text) { QPlainTextEdit::setPlainText(text); } set_enabled(bool enabled)142 void set_enabled(bool enabled) { QPlainTextEdit::setEnabled(enabled); } 143 144 protected: 145 void paintEvent(QPaintEvent*); 146 void resizeEvent(QResizeEvent*); 147 148 signals: 149 void Reset(); 150 }; 151 152 class SpinBox : public QSpinBox, public ExtendedEditor { 153 Q_OBJECT 154 Q_PROPERTY(QString hint READ hint WRITE set_hint) 155 Q_PROPERTY(bool has_clear_button READ has_clear_button WRITE 156 set_clear_button) 157 Q_PROPERTY(bool has_reset_button READ has_reset_button WRITE 158 set_reset_button) 159 160 public: 161 SpinBox(QWidget* parent = nullptr); 162 163 // QSpinBox 164 QString textFromValue(int val) const; 165 166 // ExtendedEditor is_empty()167 bool is_empty() const { return text().isEmpty() || text() == "0"; } set_focus()168 void set_focus() { QSpinBox::setFocus(); } text()169 QString text() const { return QSpinBox::text(); } set_text(const QString & text)170 void set_text(const QString& text) { QSpinBox::setValue(text.toInt()); } set_enabled(bool enabled)171 void set_enabled(bool enabled) { QSpinBox::setEnabled(enabled); } 172 173 protected: 174 void paintEvent(QPaintEvent*); 175 void resizeEvent(QResizeEvent*); 176 177 signals: 178 void Reset(); 179 }; 180 181 #endif // LINEEDIT_H 182