123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- #include "LineEdit.h"
- #include "Menu.h"
- #include "Common/StyleSheet.h"
- #include "Common/SmoothScroll.h"
- #include "QFluentWidgets.h"
- #include <QPainter>
- #include <QHBoxLayout>
- #include <QMouseEvent>
- #include <QDebug>
- LineEditButton::LineEditButton(FluentIconBase *ficon, QWidget *parent) : QToolButton(parent), m_ficon(ficon)
- {
- setFixedSize(31, 23);
- setIconSize(QSize(10, 10));
- setCursor(Qt::PointingHandCursor);
- setObjectName("lineEditButton");
- FluentStyleSheet::apply("LINE_EDIT", this);
- }
- void LineEditButton::setIcon(FluentIconBase *ficon)
- {
- m_ficon.reset(ficon);
- update();
- }
- FluentIconBase *LineEditButton::ficon() const
- {
- return m_ficon.data();
- }
- QIcon LineEditButton::icon() const
- {
- if (!m_ficon) {
- return QIcon();
- }
- return m_ficon->icon();
- }
- void LineEditButton::paintEvent(QPaintEvent *event)
- {
- QToolButton::paintEvent(event);
- if (!m_ficon) {
- return;
- }
- QPainter painter(this);
- painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
- int iw = iconSize().width();
- int ih = iconSize().height();
- int w = width();
- int h = height();
- QRectF rect = QRectF((w - iw) / 2, (h - ih) / 2, iw, ih);
- if (QFWIns.isDarkTheme()) {
- m_ficon->render(&painter, rect.toRect());
- } else {
- QHash<QString, QString> attributes;
- attributes.insert("fill", "#656565");
- m_ficon->render(&painter, rect.toRect(), QVector<int>(), attributes);
- }
- }
- LineEdit::LineEdit(QWidget *parent) : QLineEdit(parent), m_isClearButtonEnabled(false), m_clearButton(nullptr)
- {
- FluentStyleSheet::apply("LINE_EDIT", this);
- setFixedHeight(33);
- setAttribute(Qt::WA_MacShowFocusRect, false);
- m_hBoxLayout = new QHBoxLayout(this);
- m_clearButton = new LineEditButton(NEWFLICON(FluentIcon, CLOSE), this);
- m_clearButton->setFixedSize(29, 25);
- m_clearButton->hide();
- m_hBoxLayout->setSpacing(3);
- m_hBoxLayout->setContentsMargins(4, 4, 4, 4);
- m_hBoxLayout->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
- m_hBoxLayout->addWidget(m_clearButton, 0, Qt::AlignRight);
- connect(m_clearButton, &LineEditButton::clicked, this, &LineEdit::clear);
- connect(this, &LineEdit::textChanged, this, &LineEdit::onTextChanged);
- }
- bool LineEdit::isClearButtonEnabled() const
- {
- return m_isClearButtonEnabled;
- }
- void LineEdit::setIsClearButtonEnabled(bool enable)
- {
- m_isClearButtonEnabled = enable;
- setTextMargins(0, 0, 28 * int(enable), 0);
- }
- void LineEdit::onTextChanged(const QString &text)
- {
- if (m_isClearButtonEnabled) {
- m_clearButton->setVisible((!text.isEmpty() && hasFocus()));
- }
- }
- void LineEdit::focusInEvent(QFocusEvent *event)
- {
- QLineEdit::focusInEvent(event);
- if (m_isClearButtonEnabled) {
- m_clearButton->setVisible(!text().isEmpty());
- }
- }
- void LineEdit::focusOutEvent(QFocusEvent *event)
- {
- QLineEdit::focusOutEvent(event);
- m_clearButton->hide();
- }
- void LineEdit::paintEvent(QPaintEvent *event)
- {
- QLineEdit::paintEvent(event);
- if (!hasFocus()) {
- return;
- }
- QPainter painter(this);
- painter.setRenderHints(QPainter::Antialiasing);
- painter.setPen(Qt::NoPen);
- QPainterPath path;
- int w = width();
- int h = height();
- path.addRoundedRect(QRectF(0, h - 10, w, 10), 5, 5);
- QPainterPath rectPath;
- rectPath.addRect(0, h - 10, w, 8);
- path = path.subtracted(rectPath);
- painter.fillPath(path, themeColor());
- }
- void LineEdit::contextMenuEvent(QContextMenuEvent *event)
- {
- QScopedPointer<LineEditMenu> menu(new LineEditMenu(this));
- connect(menu->cutAct, &QAction::triggered, this, &LineEdit::cut);
- connect(menu->copyAct, &QAction::triggered, this, &LineEdit::copy);
- connect(menu->pasteAct, &QAction::triggered, this, &LineEdit::paste);
- connect(menu->cancelAct, &QAction::triggered, this, &LineEdit::undo);
- connect(menu->selectAllAct, &QAction::triggered, this, &LineEdit::selectAll);
- menu->exec(event->globalPos(), true);
- }
- LineEditButton *LineEdit::clearButton() const
- {
- return m_clearButton;
- }
- QHBoxLayout *LineEdit::hBoxLayout() const
- {
- return m_hBoxLayout;
- }
- SearchLineEdit::SearchLineEdit(QWidget *parent) : LineEdit(parent)
- {
- m_searchButton = new LineEditButton(NEWFLICON(FluentIcon, SEARCH), this);
- QHBoxLayout *hLayout = hBoxLayout();
- hLayout->addWidget(m_searchButton, 0, Qt::AlignRight);
- setIsClearButtonEnabled(true);
- setTextMargins(0, 0, 59, 0);
- connect(m_searchButton, &LineEditButton::clicked, this, &SearchLineEdit::search);
- connect(clearButton(), &LineEditButton::clicked, this, &SearchLineEdit::clearSignal);
- }
- void SearchLineEdit::search()
- {
- QString txt = text().trimmed();
- if (!txt.isEmpty()) {
- emit searchSignal(txt);
- } else {
- emit clearSignal();
- }
- }
- PasswordLineEdit::PasswordLineEdit(QWidget *parent) : LineEdit(parent)
- {
- m_echoButton = new LineEditButton(NEWFLICON(FluentIcon, HIDE), this);
- QHBoxLayout *hLayout = hBoxLayout();
- hLayout->addWidget(m_echoButton, 0, Qt::AlignRight);
- setIsClearButtonEnabled(true);
- setTextMargins(0, 0, 59, 0);
- setEchoMode(QLineEdit::Password);
- connect(m_echoButton, &LineEditButton::clicked, this, &PasswordLineEdit::switchEchoMode);
- connect(clearButton(), &LineEditButton::clicked, this, &PasswordLineEdit::clearSignal);
- }
- void PasswordLineEdit::switchEchoMode()
- {
- setEchoMode(echoMode() == QLineEdit::Normal ? QLineEdit::Password : QLineEdit::Normal);
- m_echoButton->setIcon(echoMode() == QLineEdit::Normal ? NEWFLICON(FluentIcon, VIEW) : NEWFLICON(FluentIcon, HIDE));
- emit echoModeChanged();
- }
- TextEdit::TextEdit(QWidget *parent) : QTextEdit(parent)
- {
- m_verticalSmoothScroll = new SmoothScroll(this, Qt::Vertical);
- m_horizonSmoothScroll = new SmoothScroll(this, Qt::Horizontal);
- FluentStyleSheet::apply("LINE_EDIT", this);
- }
- void TextEdit::wheelEvent(QWheelEvent *event)
- {
- if (event->modifiers() == Qt::NoModifier) {
- if ((m_verticalSmoothScroll->smoothMode() == SmoothMode::NO_SMOOTH)
- || (abs(event->angleDelta().y()) % 120 != 0)) {
- QTextEdit::wheelEvent(event);
- } else {
- m_verticalSmoothScroll->wheelEvent(event);
- }
- } else {
- if ((m_horizonSmoothScroll->smoothMode() == SmoothMode::NO_SMOOTH)
- || (abs(event->angleDelta().y()) % 120 != 0)) {
- QTextEdit::wheelEvent(event);
- } else {
- m_horizonSmoothScroll->wheelEvent(event);
- }
- }
- }
- void TextEdit::contextMenuEvent(QContextMenuEvent *event)
- {
- QScopedPointer<TextEditMenu> menu(new TextEditMenu(this));
- connect(menu->cutAct, &QAction::triggered, this, &TextEdit::cut);
- connect(menu->copyAct, &QAction::triggered, this, &TextEdit::copy);
- connect(menu->pasteAct, &QAction::triggered, this, &TextEdit::paste);
- connect(menu->cancelAct, &QAction::triggered, this, &TextEdit::undo);
- connect(menu->selectAllAct, &QAction::triggered, this, &TextEdit::selectAll);
- menu->exec(event->globalPos(), true);
- }
- PlainTextEdit::PlainTextEdit(QWidget *parent) : QPlainTextEdit(parent)
- {
- m_verticalSmoothScroll = new SmoothScroll(this, Qt::Vertical);
- m_horizonSmoothScroll = new SmoothScroll(this, Qt::Horizontal);
- FluentStyleSheet::apply("LINE_EDIT", this);
- }
- void PlainTextEdit::wheelEvent(QWheelEvent *event)
- {
- if (event->modifiers() == Qt::NoModifier) {
- if ((m_verticalSmoothScroll->smoothMode() == SmoothMode::NO_SMOOTH)
- || (abs(event->angleDelta().y()) % 120 != 0)) {
- QPlainTextEdit::wheelEvent(event);
- } else {
- m_verticalSmoothScroll->wheelEvent(event);
- }
- } else {
- if ((m_horizonSmoothScroll->smoothMode() == SmoothMode::NO_SMOOTH)
- || (abs(event->angleDelta().y()) % 120 != 0)) {
- QPlainTextEdit::wheelEvent(event);
- } else {
- m_horizonSmoothScroll->wheelEvent(event);
- }
- }
- }
- void PlainTextEdit::contextMenuEvent(QContextMenuEvent *event)
- {
- QScopedPointer<TextEditMenu> menu(new TextEditMenu(this));
- menu->exec(event->globalPos(), true);
- }
|