From 1802b00525ca6b27263762f8c65f5ed67aeaeb3b Mon Sep 17 00:00:00 2001 From: Le Tan Date: Sun, 24 Oct 2021 15:46:53 +0800 Subject: [PATCH] refine tags dialog --- src/widgets/dialogs/viewtagsdialog.cpp | 13 ++++++++++++- src/widgets/dialogs/viewtagsdialog.h | 2 ++ src/widgets/tagpopup.cpp | 2 +- src/widgets/tagviewer.cpp | 17 ++++++++++++----- src/widgets/tagviewer.h | 4 +++- 5 files changed, 30 insertions(+), 8 deletions(-) diff --git a/src/widgets/dialogs/viewtagsdialog.cpp b/src/widgets/dialogs/viewtagsdialog.cpp index fa92569d..c0886859 100644 --- a/src/widgets/dialogs/viewtagsdialog.cpp +++ b/src/widgets/dialogs/viewtagsdialog.cpp @@ -2,6 +2,7 @@ #include #include +#include #include @@ -30,7 +31,7 @@ void ViewTagsDialog::setupUI() m_nodeNameLabel = new QLabel(mainWidget); mainLayout->addRow(tr("Name:"), m_nodeNameLabel); - m_tagViewer = new TagViewer(mainWidget); + m_tagViewer = new TagViewer(false, mainWidget); mainLayout->addRow(tr("Tags:"), m_tagViewer); setDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); @@ -56,3 +57,13 @@ void ViewTagsDialog::setNode(Node *p_node) m_nodeNameLabel->setText(m_node->getName()); m_tagViewer->setNode(m_node); } + +void ViewTagsDialog::keyPressEvent(QKeyEvent *p_event) +{ + if (p_event->key() == Qt::Key_Enter || p_event->key() == Qt::Key_Return) { + // Prevent it from closing the dialog. + return; + } + + Dialog::keyPressEvent(p_event); +} diff --git a/src/widgets/dialogs/viewtagsdialog.h b/src/widgets/dialogs/viewtagsdialog.h index 4abc15c2..97eb9ce1 100644 --- a/src/widgets/dialogs/viewtagsdialog.h +++ b/src/widgets/dialogs/viewtagsdialog.h @@ -19,6 +19,8 @@ namespace vnotex protected: void acceptedButtonClicked() Q_DECL_OVERRIDE; + void keyPressEvent(QKeyEvent *p_event) Q_DECL_OVERRIDE; + private: void setupUI(); diff --git a/src/widgets/tagpopup.cpp b/src/widgets/tagpopup.cpp index 0452fdfc..065c6898 100644 --- a/src/widgets/tagpopup.cpp +++ b/src/widgets/tagpopup.cpp @@ -33,7 +33,7 @@ void TagPopup::setupUI() auto mainLayout = new QVBoxLayout(this); WidgetUtils::setContentsMargins(mainLayout); - m_tagViewer = new TagViewer(this); + m_tagViewer = new TagViewer(true, this); mainLayout->addWidget(m_tagViewer); setMinimumSize(256, 320); diff --git a/src/widgets/tagviewer.cpp b/src/widgets/tagviewer.cpp index 64ce7e90..bbab3885 100644 --- a/src/widgets/tagviewer.cpp +++ b/src/widgets/tagviewer.cpp @@ -30,8 +30,9 @@ QIcon TagViewer::s_tagIcon; QIcon TagViewer::s_selectedTagIcon; -TagViewer::TagViewer(QWidget *p_parent) - : QFrame(p_parent) +TagViewer::TagViewer(bool p_isPopup, QWidget *p_parent) + : QFrame(p_parent), + m_isPopup(p_isPopup) { initIcons(); @@ -55,7 +56,9 @@ void TagViewer::setupUI() m_searchLineEdit->setValidator(tagNameValidator); setFocusProxy(m_searchLineEdit); - m_searchLineEdit->installEventFilter(this); + if (m_isPopup) { + m_searchLineEdit->installEventFilter(this); + } m_tagList = new ListWidget(this); m_tagList->setWrapping(true); @@ -67,12 +70,15 @@ void TagViewer::setupUI() this, &TagViewer::toggleItemTag); mainLayout->addWidget(m_tagList); - m_tagList->installEventFilter(this); + if (m_isPopup) { + m_tagList->installEventFilter(this); + } } bool TagViewer::eventFilter(QObject *p_obj, QEvent *p_event) { - if ((p_obj == m_searchLineEdit || p_obj == m_tagList) + if (m_isPopup + && (p_obj == m_searchLineEdit || p_obj == m_tagList) && p_event->type() == QEvent::KeyPress) { auto keyEve = static_cast(p_event); const auto key = keyEve->key(); @@ -86,6 +92,7 @@ bool TagViewer::eventFilter(QObject *p_obj, QEvent *p_event) return true; } } + return QFrame::eventFilter(p_obj, p_event); } diff --git a/src/widgets/tagviewer.h b/src/widgets/tagviewer.h index 2da7d37f..002a99d0 100644 --- a/src/widgets/tagviewer.h +++ b/src/widgets/tagviewer.h @@ -23,7 +23,7 @@ namespace vnotex { Q_OBJECT public: - explicit TagViewer(QWidget *p_parent = nullptr); + TagViewer(bool p_isPopup, QWidget *p_parent = nullptr); void setNode(Node *p_node); @@ -61,6 +61,8 @@ namespace vnotex static void initIcons(); + bool m_isPopup = false; + // View the tags of @m_node. Node *m_node = nullptr;