LineEdit.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #include "LineEdit.h"
  2. #include "Menu.h"
  3. #include "Common/StyleSheet.h"
  4. #include "Common/SmoothScroll.h"
  5. #include "QFluentWidgets.h"
  6. #include <QPainter>
  7. #include <QHBoxLayout>
  8. #include <QMouseEvent>
  9. #include <QDebug>
  10. LineEditButton::LineEditButton(FluentIconBase *ficon, QWidget *parent) : QToolButton(parent), m_ficon(ficon)
  11. {
  12. setFixedSize(31, 23);
  13. setIconSize(QSize(10, 10));
  14. setCursor(Qt::PointingHandCursor);
  15. setObjectName("lineEditButton");
  16. FluentStyleSheet::apply("LINE_EDIT", this);
  17. }
  18. void LineEditButton::paintEvent(QPaintEvent *event)
  19. {
  20. QToolButton::paintEvent(event);
  21. if (!m_ficon) {
  22. return;
  23. }
  24. QPainter painter(this);
  25. painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
  26. int iw = iconSize().width();
  27. int ih = iconSize().height();
  28. int w = width();
  29. int h = height();
  30. QRectF rect = QRectF((w - iw) / 2, (h - ih) / 2, iw, ih);
  31. if (QFWIns.isDarkTheme()) {
  32. m_ficon->render(&painter, rect.toRect());
  33. } else {
  34. QHash<QString, QString> attributes;
  35. attributes.insert("fill", "#656565");
  36. m_ficon->render(&painter, rect.toRect(), QVector<int>(), attributes);
  37. }
  38. }
  39. LineEdit::LineEdit(QWidget *parent) : QLineEdit(parent), m_isClearButtonEnabled(false), m_clearButton(nullptr)
  40. {
  41. FluentStyleSheet::apply("LINE_EDIT", this);
  42. setFixedHeight(33);
  43. setAttribute(Qt::WA_MacShowFocusRect, false);
  44. m_hBoxLayout = new QHBoxLayout(this);
  45. m_clearButton = new LineEditButton(NEWFLICON(FluentIcon, CLOSE), this);
  46. m_clearButton->setFixedSize(29, 25);
  47. m_clearButton->hide();
  48. m_hBoxLayout->setSpacing(3);
  49. m_hBoxLayout->setContentsMargins(4, 4, 4, 4);
  50. m_hBoxLayout->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
  51. m_hBoxLayout->addWidget(m_clearButton, 0, Qt::AlignRight);
  52. connect(m_clearButton, &LineEditButton::clicked, this, &LineEdit::clear);
  53. connect(this, &LineEdit::textChanged, this, &LineEdit::onTextChanged);
  54. }
  55. bool LineEdit::isClearButtonEnabled() const
  56. {
  57. return m_isClearButtonEnabled;
  58. }
  59. void LineEdit::setIsClearButtonEnabled(bool enable)
  60. {
  61. m_isClearButtonEnabled = enable;
  62. setTextMargins(0, 0, 28 * int(enable), 0);
  63. }
  64. void LineEdit::onTextChanged(const QString &text)
  65. {
  66. if (m_isClearButtonEnabled) {
  67. m_clearButton->setVisible((!text.isEmpty() && hasFocus()));
  68. }
  69. }
  70. void LineEdit::focusInEvent(QFocusEvent *event)
  71. {
  72. QLineEdit::focusInEvent(event);
  73. if (m_isClearButtonEnabled) {
  74. m_clearButton->setVisible(!text().isEmpty());
  75. }
  76. }
  77. void LineEdit::focusOutEvent(QFocusEvent *event)
  78. {
  79. QLineEdit::focusOutEvent(event);
  80. m_clearButton->hide();
  81. }
  82. void LineEdit::paintEvent(QPaintEvent *event)
  83. {
  84. QLineEdit::paintEvent(event);
  85. if (!hasFocus()) {
  86. return;
  87. }
  88. QPainter painter(this);
  89. painter.setRenderHints(QPainter::Antialiasing);
  90. painter.setPen(Qt::NoPen);
  91. QPainterPath path;
  92. int w = width();
  93. int h = height();
  94. path.addRoundedRect(QRectF(0, h - 10, w, 10), 5, 5);
  95. QPainterPath rectPath;
  96. rectPath.addRect(0, h - 10, w, 8);
  97. path = path.subtracted(rectPath);
  98. painter.fillPath(path, themeColor());
  99. }
  100. void LineEdit::contextMenuEvent(QContextMenuEvent *event)
  101. {
  102. QScopedPointer<LineEditMenu> menu(new LineEditMenu(this));
  103. connect(menu->cutAct, &QAction::triggered, this, &LineEdit::cut);
  104. connect(menu->copyAct, &QAction::triggered, this, &LineEdit::copy);
  105. connect(menu->pasteAct, &QAction::triggered, this, &LineEdit::paste);
  106. connect(menu->cancelAct, &QAction::triggered, this, &LineEdit::undo);
  107. connect(menu->selectAllAct, &QAction::triggered, this, &LineEdit::selectAll);
  108. menu->exec(event->globalPos(), true);
  109. }
  110. LineEditButton *LineEdit::clearButton() const
  111. {
  112. return m_clearButton;
  113. }
  114. QHBoxLayout *LineEdit::hBoxLayout() const
  115. {
  116. return m_hBoxLayout;
  117. }
  118. SearchLineEdit::SearchLineEdit(QWidget *parent) : LineEdit(parent)
  119. {
  120. m_searchButton = new LineEditButton(NEWFLICON(FluentIcon, SEARCH), this);
  121. QHBoxLayout *hLayout = hBoxLayout();
  122. hLayout->addWidget(m_searchButton, 0, Qt::AlignRight);
  123. setClearButtonEnabled(true);
  124. setTextMargins(0, 0, 59, 0);
  125. connect(m_searchButton, &LineEditButton::clicked, this, &SearchLineEdit::search);
  126. connect(clearButton(), &LineEditButton::clicked, this, &SearchLineEdit::clearSignal);
  127. }
  128. void SearchLineEdit::search()
  129. {
  130. QString txt = text().trimmed();
  131. if (!txt.isEmpty()) {
  132. emit searchSignal(txt);
  133. } else {
  134. emit clearSignal();
  135. }
  136. }
  137. TextEdit::TextEdit(QWidget *parent) : QTextEdit(parent)
  138. {
  139. m_verticalSmoothScroll = new SmoothScroll(this, Qt::Vertical);
  140. m_horizonSmoothScroll = new SmoothScroll(this, Qt::Horizontal);
  141. FluentStyleSheet::apply("LINE_EDIT", this);
  142. }
  143. void TextEdit::wheelEvent(QWheelEvent *event)
  144. {
  145. if (event->modifiers() == Qt::NoModifier) {
  146. if ((m_verticalSmoothScroll->smoothMode() == SmoothMode::NO_SMOOTH)
  147. || (abs(event->angleDelta().y()) % 120 != 0)) {
  148. QTextEdit::wheelEvent(event);
  149. } else {
  150. m_verticalSmoothScroll->wheelEvent(event);
  151. }
  152. } else {
  153. if ((m_horizonSmoothScroll->smoothMode() == SmoothMode::NO_SMOOTH)
  154. || (abs(event->angleDelta().y()) % 120 != 0)) {
  155. QTextEdit::wheelEvent(event);
  156. } else {
  157. m_horizonSmoothScroll->wheelEvent(event);
  158. }
  159. }
  160. }
  161. void TextEdit::contextMenuEvent(QContextMenuEvent *event)
  162. {
  163. QScopedPointer<TextEditMenu> menu(new TextEditMenu(this));
  164. connect(menu->cutAct, &QAction::triggered, this, &TextEdit::cut);
  165. connect(menu->copyAct, &QAction::triggered, this, &TextEdit::copy);
  166. connect(menu->pasteAct, &QAction::triggered, this, &TextEdit::paste);
  167. connect(menu->cancelAct, &QAction::triggered, this, &TextEdit::undo);
  168. connect(menu->selectAllAct, &QAction::triggered, this, &TextEdit::selectAll);
  169. menu->exec(event->globalPos(), true);
  170. }
  171. PlainTextEdit::PlainTextEdit(QWidget *parent) : QPlainTextEdit(parent)
  172. {
  173. m_verticalSmoothScroll = new SmoothScroll(this, Qt::Vertical);
  174. m_horizonSmoothScroll = new SmoothScroll(this, Qt::Horizontal);
  175. FluentStyleSheet::apply("LINE_EDIT", this);
  176. }
  177. void PlainTextEdit::wheelEvent(QWheelEvent *event)
  178. {
  179. if (event->modifiers() == Qt::NoModifier) {
  180. if ((m_verticalSmoothScroll->smoothMode() == SmoothMode::NO_SMOOTH)
  181. || (abs(event->angleDelta().y()) % 120 != 0)) {
  182. QPlainTextEdit::wheelEvent(event);
  183. } else {
  184. m_verticalSmoothScroll->wheelEvent(event);
  185. }
  186. } else {
  187. if ((m_horizonSmoothScroll->smoothMode() == SmoothMode::NO_SMOOTH)
  188. || (abs(event->angleDelta().y()) % 120 != 0)) {
  189. QPlainTextEdit::wheelEvent(event);
  190. } else {
  191. m_horizonSmoothScroll->wheelEvent(event);
  192. }
  193. }
  194. }
  195. void PlainTextEdit::contextMenuEvent(QContextMenuEvent *event)
  196. {
  197. QScopedPointer<TextEditMenu> menu(new TextEditMenu(this));
  198. menu->exec(event->globalPos(), true);
  199. }