PickerBase.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #ifndef PICKERBASE_H
  2. #define PICKERBASE_H
  3. #include <QWidget>
  4. #include <QListWidget>
  5. #include <QPushButton>
  6. #include <QBoxLayout>
  7. #include <QPropertyAnimation>
  8. #include <QGraphicsDropShadowEffect>
  9. #include "Common/StyleSheet.h"
  10. class CycleListWidget;
  11. class TransparentToolButton;
  12. class PickerPanel;
  13. class SeparatorWidget : public QWidget
  14. {
  15. Q_OBJECT
  16. public:
  17. explicit SeparatorWidget(Qt::Orientation orient, QWidget *parent = nullptr) : QWidget(parent)
  18. {
  19. if (orient == Qt::Horizontal) {
  20. setFixedHeight(1);
  21. } else {
  22. setFixedWidth(1);
  23. }
  24. setAttribute(Qt::WA_StyledBackground);
  25. FluentStyleSheet::apply("TIME_PICKER", this);
  26. }
  27. };
  28. class ItemMaskWidget : public QWidget
  29. {
  30. Q_OBJECT
  31. public:
  32. explicit ItemMaskWidget(const QList<CycleListWidget *> &list, QWidget *parent = nullptr);
  33. QList<CycleListWidget *> listWidgets;
  34. protected:
  35. virtual void paintEvent(QPaintEvent *event) override;
  36. void drawText(QListWidgetItem *item, QPainter *p, int y);
  37. };
  38. class PickerColumnFormatter : public QObject
  39. {
  40. public:
  41. PickerColumnFormatter(QObject *parent = nullptr) : QObject(parent) { }
  42. virtual QString encode(const QVariant &value) { return value.toString(); }
  43. virtual QString decode(const QVariant &value) { return value.toString(); }
  44. };
  45. class DigitFormatter : public PickerColumnFormatter
  46. {
  47. Q_OBJECT
  48. public:
  49. DigitFormatter(QObject *parent = nullptr) : PickerColumnFormatter(parent) { }
  50. virtual QString decode(const QVariant &value) { return QString::number(value.toInt()); }
  51. };
  52. class PickerColumnButton : public QPushButton
  53. {
  54. Q_OBJECT
  55. public:
  56. explicit PickerColumnButton(const QString &name, const QVariantList &items, int width,
  57. Qt::AlignmentFlag align = Qt::AlignLeft, PickerColumnFormatter *formatter = nullptr,
  58. QWidget *parent = nullptr);
  59. QVariantList items() const;
  60. void setItems(const QVariantList &items);
  61. QString value() const;
  62. void setValue(const QString &value);
  63. Qt::AlignmentFlag alignment() const;
  64. void setAlignment(const Qt::AlignmentFlag &align);
  65. PickerColumnFormatter *formatter() const;
  66. void setFormatter(PickerColumnFormatter *formatter);
  67. QString name() const;
  68. void setName(const QString &name);
  69. signals:
  70. private:
  71. QString m_name;
  72. QString m_value;
  73. QVariantList m_items;
  74. Qt::AlignmentFlag m_align;
  75. PickerColumnFormatter *m_formatter;
  76. };
  77. class PickerBase : public QPushButton
  78. {
  79. Q_OBJECT
  80. public:
  81. explicit PickerBase(QWidget *parent = nullptr);
  82. void addColumn(const QString &name, const QVariantList &items, int width, Qt::AlignmentFlag align = Qt::AlignLeft,
  83. PickerColumnFormatter *formatter = nullptr);
  84. void setColumnAlignment(int index, Qt::AlignmentFlag align = Qt::AlignCenter);
  85. void setColumnWidth(int index, int width);
  86. void setColumnTight(int index);
  87. void setColumnVisible(int index, bool visible);
  88. QStringList value() const;
  89. void setColumnValue(int index, const QString &value);
  90. void setColumnFormatter(int index, PickerColumnFormatter *formatter);
  91. void setColumnItems(int index, const QVariantList &items);
  92. QVariant encodeValue(int index, const QVariant &value);
  93. QVariant decodeValue(int index, const QVariant &value);
  94. QStringList panelInitialValue();
  95. void setColumn(int index, const QString &name, const QVariantList &items, int width,
  96. Qt::AlignmentFlag align = Qt::AlignCenter);
  97. void clearColumns();
  98. public:
  99. QList<PickerColumnButton *> columns;
  100. QHBoxLayout *hBoxLayout;
  101. signals:
  102. public slots:
  103. void showPanel();
  104. virtual void onConfirmed(const QStringList &value);
  105. virtual void onColumnValueChanged(PickerPanel *panel, int index, const QString &value);
  106. // QWidget interface
  107. protected:
  108. void mousePressEvent(QMouseEvent *event) override;
  109. void mouseReleaseEvent(QMouseEvent *event) override;
  110. void enterEvent(QEvent *event) override;
  111. void leaveEvent(QEvent *event) override;
  112. private:
  113. void setButtonProperty(const char *name, const QVariant &value);
  114. };
  115. class PickerPanel : public QWidget
  116. {
  117. Q_OBJECT
  118. public:
  119. explicit PickerPanel(QWidget *parent = nullptr);
  120. void setShadowEffect(int blurRadius = 30, const QPointF &offset = QPointF(0, 8),
  121. const QColor &color = QColor(0, 0, 0, 30));
  122. void addColumn(const QVariantList &items, int width, Qt::AlignmentFlag align = Qt::AlignCenter);
  123. QStringList value() const;
  124. void setValue(const QStringList &value);
  125. QString columnValue(int index) const;
  126. void setColumnValue(int index, const QString &value);
  127. CycleListWidget *column(int index) const;
  128. void exec(const QPoint &pos, bool ani = true);
  129. protected:
  130. void resizeEvent(QResizeEvent *event) override;
  131. signals:
  132. void confirmed(const QStringList &);
  133. void columnValueChanged(int, const QString &);
  134. private:
  135. void initWidget();
  136. private slots:
  137. void fadeOut();
  138. void onAniValueChanged(const QVariant &value);
  139. private:
  140. int m_itemHeight;
  141. QList<CycleListWidget *> m_listWidgets;
  142. QFrame *m_view;
  143. ItemMaskWidget *m_itemMaskWidget;
  144. SeparatorWidget *m_hSeparatorWidget;
  145. TransparentToolButton *m_yesButton;
  146. TransparentToolButton *m_cancelButton;
  147. QHBoxLayout *m_hBoxLayout;
  148. QHBoxLayout *m_listLayout;
  149. QHBoxLayout *m_buttonLayout;
  150. QVBoxLayout *m_vBoxLayout;
  151. bool m_isExpanded;
  152. QPropertyAnimation *m_ani;
  153. QGraphicsDropShadowEffect *m_shadowEffect;
  154. };
  155. #endif // PICKERBASE_H