LineEdit.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. setIsClearButtonEnabled(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. PasswordLineEdit::PasswordLineEdit(QWidget *parent) : LineEdit(parent)
  138. {
  139. m_echoButton = new LineEditButton(NEWFLICON(FluentIcon, VIEW), this);
  140. QHBoxLayout *hLayout = hBoxLayout();
  141. hLayout->addWidget(m_echoButton, 0, Qt::AlignRight);
  142. setIsClearButtonEnabled(true);
  143. setTextMargins(0, 0, 59, 0);
  144. connect(m_echoButton, &LineEditButton::clicked, this, &PasswordLineEdit::switchEchoMode);
  145. connect(clearButton(), &LineEditButton::clicked, this, &PasswordLineEdit::clearSignal);
  146. }
  147. void PasswordLineEdit::switchEchoMode()
  148. {
  149. setEchoMode(echoMode() == QLineEdit::Normal ? QLineEdit::Password : QLineEdit::Normal);
  150. emit echoModeChanged();
  151. }
  152. TextEdit::TextEdit(QWidget *parent) : QTextEdit(parent)
  153. {
  154. m_verticalSmoothScroll = new SmoothScroll(this, Qt::Vertical);
  155. m_horizonSmoothScroll = new SmoothScroll(this, Qt::Horizontal);
  156. FluentStyleSheet::apply("LINE_EDIT", this);
  157. }
  158. void TextEdit::wheelEvent(QWheelEvent *event)
  159. {
  160. if (event->modifiers() == Qt::NoModifier) {
  161. if ((m_verticalSmoothScroll->smoothMode() == SmoothMode::NO_SMOOTH)
  162. || (abs(event->angleDelta().y()) % 120 != 0)) {
  163. QTextEdit::wheelEvent(event);
  164. } else {
  165. m_verticalSmoothScroll->wheelEvent(event);
  166. }
  167. } else {
  168. if ((m_horizonSmoothScroll->smoothMode() == SmoothMode::NO_SMOOTH)
  169. || (abs(event->angleDelta().y()) % 120 != 0)) {
  170. QTextEdit::wheelEvent(event);
  171. } else {
  172. m_horizonSmoothScroll->wheelEvent(event);
  173. }
  174. }
  175. }
  176. void TextEdit::contextMenuEvent(QContextMenuEvent *event)
  177. {
  178. QScopedPointer<TextEditMenu> menu(new TextEditMenu(this));
  179. connect(menu->cutAct, &QAction::triggered, this, &TextEdit::cut);
  180. connect(menu->copyAct, &QAction::triggered, this, &TextEdit::copy);
  181. connect(menu->pasteAct, &QAction::triggered, this, &TextEdit::paste);
  182. connect(menu->cancelAct, &QAction::triggered, this, &TextEdit::undo);
  183. connect(menu->selectAllAct, &QAction::triggered, this, &TextEdit::selectAll);
  184. menu->exec(event->globalPos(), true);
  185. }
  186. PlainTextEdit::PlainTextEdit(QWidget *parent) : QPlainTextEdit(parent)
  187. {
  188. m_verticalSmoothScroll = new SmoothScroll(this, Qt::Vertical);
  189. m_horizonSmoothScroll = new SmoothScroll(this, Qt::Horizontal);
  190. FluentStyleSheet::apply("LINE_EDIT", this);
  191. }
  192. void PlainTextEdit::wheelEvent(QWheelEvent *event)
  193. {
  194. if (event->modifiers() == Qt::NoModifier) {
  195. if ((m_verticalSmoothScroll->smoothMode() == SmoothMode::NO_SMOOTH)
  196. || (abs(event->angleDelta().y()) % 120 != 0)) {
  197. QPlainTextEdit::wheelEvent(event);
  198. } else {
  199. m_verticalSmoothScroll->wheelEvent(event);
  200. }
  201. } else {
  202. if ((m_horizonSmoothScroll->smoothMode() == SmoothMode::NO_SMOOTH)
  203. || (abs(event->angleDelta().y()) % 120 != 0)) {
  204. QPlainTextEdit::wheelEvent(event);
  205. } else {
  206. m_horizonSmoothScroll->wheelEvent(event);
  207. }
  208. }
  209. }
  210. void PlainTextEdit::contextMenuEvent(QContextMenuEvent *event)
  211. {
  212. QScopedPointer<TextEditMenu> menu(new TextEditMenu(this));
  213. menu->exec(event->globalPos(), true);
  214. }