LineEdit.cpp 8.0 KB

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