SpinBox.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. #include "SpinBox.h"
  2. #include "QFluentWidgets.h"
  3. #include "Menu.h"
  4. #include <QPainter>
  5. QString SpinIcon::iconName(SpinIcon::IconType type)
  6. {
  7. switch (type) {
  8. case UP:
  9. return "Up";
  10. case DOWN:
  11. return "Down";
  12. }
  13. return "Unknown";
  14. }
  15. SpinIcon::SpinIcon(IconType type, Qfw::Theme t) : FluentIconBase(""), m_theme(t), m_type(type)
  16. {
  17. iconEngine->setIconPath(iconPath());
  18. }
  19. SpinIcon::~SpinIcon() { }
  20. QString SpinIcon::iconPath()
  21. {
  22. QString colorName;
  23. if (m_theme == Qfw::Theme::AUTO) {
  24. colorName = QFWIns.isDarkTheme() ? "white" : "black";
  25. } else {
  26. if (m_theme == Qfw::DARK) {
  27. colorName = "white";
  28. } else {
  29. colorName = "black";
  30. }
  31. }
  32. return QString(":/qfluentwidgets/images/spin_box/%1_%2.svg").arg(iconName(m_type)).arg(colorName);
  33. }
  34. QIcon SpinIcon::icon()
  35. {
  36. return QIcon(iconEngine->clone());
  37. }
  38. QString SpinIcon::typeName() const
  39. {
  40. return iconName(m_type);
  41. }
  42. QString SpinIcon::enumName() const
  43. {
  44. QMetaEnum metaEnum = QMetaEnum::fromType<IconType>();
  45. return metaEnum.valueToKey(m_type);
  46. }
  47. FluentIconBase *SpinIcon::clone()
  48. {
  49. return new SpinIcon(m_type, m_theme);
  50. }
  51. Qfw::Theme SpinIcon::theme() const
  52. {
  53. return m_theme;
  54. }
  55. void SpinIcon::setTheme(const Qfw::Theme &theme)
  56. {
  57. m_theme = theme;
  58. iconEngine->setIconPath(iconPath());
  59. }
  60. SpinButton::SpinButton(FluentIconBase *icon, QWidget *parent) : QToolButton(parent), m_icon(icon)
  61. {
  62. setFixedSize(31, 23);
  63. setIconSize(QSize(10, 10));
  64. FluentStyleSheet::apply("SPIN_BOX", this);
  65. }
  66. void SpinButton::paintEvent(QPaintEvent *event)
  67. {
  68. QToolButton::paintEvent(event);
  69. QPainter painter(this);
  70. painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
  71. m_icon->render(&painter, QRect(10, 9, 11, 11));
  72. }
  73. void SpinBoxBase::setUpUi(QAbstractSpinBox *spinbox)
  74. {
  75. FluentStyleSheet::apply("SPIN_BOX", spinbox);
  76. spinbox->setButtonSymbols(QSpinBox::NoButtons);
  77. spinbox->setFixedHeight(33);
  78. hBoxLayout = new QHBoxLayout(spinbox);
  79. upButton = new SpinButton(NEWFLICON(SpinIcon, UP), spinbox);
  80. downButton = new SpinButton(NEWFLICON(SpinIcon, DOWN), spinbox);
  81. hBoxLayout->setContentsMargins(0, 4, 4, 4);
  82. hBoxLayout->setSpacing(5);
  83. hBoxLayout->addWidget(upButton, 0, Qt::AlignRight);
  84. hBoxLayout->addWidget(downButton, 0, Qt::AlignRight);
  85. hBoxLayout->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
  86. // connect(upButton, &SpinButton::clicked, m_parent, &QAbstractSpinBox::stepUp);
  87. // connect(downButton, &SpinButton::clicked, m_parent, &QAbstractSpinBox::stepDown);
  88. spinbox->setAttribute(Qt::WA_MacShowFocusRect, false);
  89. spinbox->setContextMenuPolicy(Qt::CustomContextMenu);
  90. // connect(m_parent, &QAbstractSpinBox::customContextMenuRequested, this, &SpinBoxBase::showContextMenu);
  91. }
  92. void SpinBoxBase::showContextMenu(QAbstractSpinBox *spinbox, const QPoint &pos)
  93. {
  94. QScopedPointer<LineEditMenu> menu(new LineEditMenu(lineEdit()));
  95. menu->exec(spinbox->mapToGlobal(pos));
  96. }
  97. void SpinBoxBase::drawBorderBottom(QAbstractSpinBox *spinbox)
  98. {
  99. if (!spinbox->hasFocus()) {
  100. return;
  101. }
  102. QPainter painter(spinbox);
  103. painter.setRenderHints(QPainter::Antialiasing);
  104. painter.setPen(Qt::NoPen);
  105. QPainterPath path;
  106. int w = spinbox->width();
  107. int h = spinbox->height();
  108. path.addRoundedRect(QRectF(0, h - 10, w, 10), 5, 5);
  109. QPainterPath rectPath;
  110. rectPath.addRect(0, h - 10, w, 8);
  111. path = path.subtracted(rectPath);
  112. painter.fillPath(path, themeColor());
  113. }
  114. SpinBox::SpinBox(QWidget *parent) : QSpinBox(parent), SpinBoxBase()
  115. {
  116. setUpUi(this);
  117. connect(upButton, &SpinButton::clicked, this, &QAbstractSpinBox::stepUp);
  118. connect(downButton, &SpinButton::clicked, this, &QAbstractSpinBox::stepDown);
  119. connect(this, &QAbstractSpinBox::customContextMenuRequested, this,
  120. [this](const QPoint &pos) { showContextMenu(this, pos); });
  121. }
  122. QLineEdit *SpinBox::lineEdit() const
  123. {
  124. return QSpinBox::lineEdit();
  125. }
  126. void SpinBox::paintEvent(QPaintEvent *event)
  127. {
  128. QSpinBox::paintEvent(event);
  129. drawBorderBottom(this);
  130. }
  131. DoubleSpinBox::DoubleSpinBox(QWidget *parent) : QDoubleSpinBox(parent), SpinBoxBase()
  132. {
  133. setUpUi(this);
  134. connect(upButton, &SpinButton::clicked, this, &QAbstractSpinBox::stepUp);
  135. connect(downButton, &SpinButton::clicked, this, &QAbstractSpinBox::stepDown);
  136. connect(this, &QAbstractSpinBox::customContextMenuRequested, this,
  137. [this](const QPoint &pos) { showContextMenu(this, pos); });
  138. }
  139. QLineEdit *DoubleSpinBox::lineEdit() const
  140. {
  141. return QDoubleSpinBox::lineEdit();
  142. }
  143. void DoubleSpinBox::paintEvent(QPaintEvent *event)
  144. {
  145. QDoubleSpinBox::paintEvent(event);
  146. drawBorderBottom(this);
  147. }
  148. TimeEdit::TimeEdit(QWidget *parent) : QTimeEdit(parent), SpinBoxBase()
  149. {
  150. setUpUi(this);
  151. connect(upButton, &SpinButton::clicked, this, &QAbstractSpinBox::stepUp);
  152. connect(downButton, &SpinButton::clicked, this, &QAbstractSpinBox::stepDown);
  153. connect(this, &QAbstractSpinBox::customContextMenuRequested, this,
  154. [this](const QPoint &pos) { showContextMenu(this, pos); });
  155. }
  156. QLineEdit *TimeEdit::lineEdit() const
  157. {
  158. return QTimeEdit::lineEdit();
  159. }
  160. void TimeEdit::paintEvent(QPaintEvent *event)
  161. {
  162. QTimeEdit::paintEvent(event);
  163. drawBorderBottom(this);
  164. }
  165. DateTimeEdit::DateTimeEdit(QWidget *parent) : QDateTimeEdit(parent), SpinBoxBase()
  166. {
  167. setUpUi(this);
  168. connect(upButton, &SpinButton::clicked, this, &QAbstractSpinBox::stepUp);
  169. connect(downButton, &SpinButton::clicked, this, &QAbstractSpinBox::stepDown);
  170. connect(this, &QAbstractSpinBox::customContextMenuRequested, this,
  171. [this](const QPoint &pos) { showContextMenu(this, pos); });
  172. }
  173. QLineEdit *DateTimeEdit::lineEdit() const
  174. {
  175. return QDateTimeEdit::lineEdit();
  176. }
  177. void DateTimeEdit::paintEvent(QPaintEvent *event)
  178. {
  179. QDateTimeEdit::paintEvent(event);
  180. drawBorderBottom(this);
  181. }
  182. DateEdit::DateEdit(QWidget *parent) : QDateEdit(parent), SpinBoxBase()
  183. {
  184. setUpUi(this);
  185. connect(upButton, &SpinButton::clicked, this, &QAbstractSpinBox::stepUp);
  186. connect(downButton, &SpinButton::clicked, this, &QAbstractSpinBox::stepDown);
  187. connect(this, &QAbstractSpinBox::customContextMenuRequested, this,
  188. [this](const QPoint &pos) { showContextMenu(this, pos); });
  189. }
  190. QLineEdit *DateEdit::lineEdit() const
  191. {
  192. return QDateEdit::lineEdit();
  193. }
  194. void DateEdit::paintEvent(QPaintEvent *event)
  195. {
  196. QDateEdit::paintEvent(event);
  197. drawBorderBottom(this);
  198. }