From c13cf6536e06717ed1ad142068c3e820f414f6e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9ry=20Mathieu=20=28Mathius=29?= Date: Mon, 19 Feb 2024 20:54:41 +0100 Subject: [PATCH 1/2] feat(qt): Add TreeView with keyEvent listener --- CMakeLists.txt | 2 ++ src/qtcomponents.cpp | 22 ++++++++++++++++++++++ src/qtcomponents.h | 29 +++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 src/qtcomponents.cpp create mode 100644 src/qtcomponents.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 27f787b5..74d731f6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -71,6 +71,7 @@ set(SOURCES src/modelaccounttree.cpp src/modelnewemails.cpp src/morkparser.cpp + src/qtcomponents.cpp src/setting_newemail.cpp src/settings.cpp src/trayicon.cpp @@ -99,6 +100,7 @@ set(HEADERS src/modelaccounttree.h src/modelnewemails.h src/morkparser.h + src/qtcomponents.h src/setting_newemail.h src/settings.h src/trayicon.h diff --git a/src/qtcomponents.cpp b/src/qtcomponents.cpp new file mode 100644 index 00000000..841fe0fd --- /dev/null +++ b/src/qtcomponents.cpp @@ -0,0 +1,22 @@ +#include "qtcomponents.h" + +QTreeViewWithKeyEvents::QTreeViewWithKeyEvents( QWidget *parent ) + : QTreeView(parent) +{ + +} + +void QTreeViewWithKeyEvents::onKeyPressed( void * handle, TreeViewKeyPressedEvent callback) +{ + mHandle = handle; + mCallback = callback; +} + +void QTreeViewWithKeyEvents::keyPressEvent( QKeyEvent *event ) +{ + if (mCallback != NULL && mHandle != NULL) + { + mCallback(mHandle, event); + } + QTreeView::keyPressEvent(event); +} \ No newline at end of file diff --git a/src/qtcomponents.h b/src/qtcomponents.h new file mode 100644 index 00000000..054e7e87 --- /dev/null +++ b/src/qtcomponents.h @@ -0,0 +1,29 @@ +#ifndef QTCOMPONENTS_H +#define QTCOMPONENTS_H + +#include +#include + +typedef void ( * TreeViewKeyPressedEvent)(void * handle, QKeyEvent * event); + +/** + * A QTreeView that allow to define listener of keyEvent received + */ +class QTreeViewWithKeyEvents : public QTreeView { + Q_OBJECT + + public: + explicit QTreeViewWithKeyEvents(QWidget *parent = NULL); + + public slots: + void onKeyPressed(void * handleUsed, TreeViewKeyPressedEvent callback); + + protected: + void keyPressEvent(QKeyEvent * event) override; + + private: + TreeViewKeyPressedEvent mCallback; + void * mHandle; +}; + +#endif // QTCOMPONENTS_H \ No newline at end of file From ecec774c83e5ab11c5d7b28ef526301b60863439 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9ry=20Mathieu=20=28Mathius=29?= Date: Mon, 19 Feb 2024 20:55:41 +0100 Subject: [PATCH 2/2] feat(dialogSettings): Allow to remove folder in monitoring tab with Delete or BackSpace key --- src/dialogsettings.cpp | 28 ++++++++++++++++++++++++++-- src/dialogsettings.h | 5 +++++ src/dialogsettings.ui | 7 ++++++- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/dialogsettings.cpp b/src/dialogsettings.cpp index f23dca5d..6eefe19b 100644 --- a/src/dialogsettings.cpp +++ b/src/dialogsettings.cpp @@ -1,3 +1,5 @@ +#include + #include #include #include @@ -117,6 +119,10 @@ DialogSettings::DialogSettings( QWidget *parent) mAccountModel = new ModelAccountTree(this, treeAccounts); treeAccounts->header()->setSectionResizeMode(QHeaderView::ResizeToContents); treeAccounts->setCurrentIndex(mAccountModel->index(0, 0)); + treeAccounts->onKeyPressed(this, [](void * handle, QKeyEvent * event) + { + static_cast(handle)->onKeyPressedOnTreeAccount(event); + }); // New emails tab mModelNewEmails = new ModelNewEmails( this ); @@ -150,6 +156,14 @@ DialogSettings::DialogSettings( QWidget *parent) #endif } +void DialogSettings::onKeyPressedOnTreeAccount(QKeyEvent * event) +{ + if (event->key() == Qt::Key_Delete || event->key() == Qt::Key_Backspace) + { + accountRemove(); + } +} + void DialogSettings::accept() { BirdtrayApp* app = BirdtrayApp::get(); @@ -285,8 +299,13 @@ void DialogSettings::accountEditIndex(const QModelIndex &index) void DialogSettings::accountRemove() { - QModelIndex index = treeAccounts->currentIndex(); + if ( treeAccounts->currentIndex().isValid() ) + accountRemoveIndex( treeAccounts->currentIndex() ); +} + +void DialogSettings::accountRemoveIndex(const QModelIndex &index) +{ if ( !index.isValid() ) return; @@ -311,7 +330,12 @@ void DialogSettings::newEmailEditIndex(const QModelIndex &index) void DialogSettings::newEmailRemove() { - mModelNewEmails->remove( treeNewEmails->currentIndex() ); + newEmailRemoveIndex( treeNewEmails->currentIndex() ); +} + +void DialogSettings::newEmailRemoveIndex(const QModelIndex &index) +{ + mModelNewEmails->remove( index ); } void DialogSettings::onCheckUpdateButton() { diff --git a/src/dialogsettings.h b/src/dialogsettings.h index df83f153..35cb4790 100644 --- a/src/dialogsettings.h +++ b/src/dialogsettings.h @@ -2,6 +2,7 @@ #define SETTINGSDIALOG_H #include +#include #include #include #include @@ -51,12 +52,14 @@ class DialogSettings : public QDialog, public Ui::DialogSettings void accountEdit(); void accountEditIndex( const QModelIndex& index ); void accountRemove(); + void accountRemoveIndex( const QModelIndex& index ); // New Email buttons void newEmailAdd(); void newEmailEdit(); void newEmailEditIndex( const QModelIndex& index ); void newEmailRemove(); + void newEmailRemoveIndex( const QModelIndex& index ); // Advanced buttons void onCheckUpdateButton(); @@ -77,6 +80,8 @@ class DialogSettings : public QDialog, public Ui::DialogSettings */ static void onTranslatorsDialog(); + void onKeyPressedOnTreeAccount(QKeyEvent * event); + private: void changeIcon(QToolButton * button ); diff --git a/src/dialogsettings.ui b/src/dialogsettings.ui index 1a7029b8..391269c2 100644 --- a/src/dialogsettings.ui +++ b/src/dialogsettings.ui @@ -291,7 +291,7 @@ - + true @@ -1167,6 +1167,11 @@ p, li { white-space: pre-wrap; } QPushButton
colorbutton.h
+ + QTreeViewWithKeyEvents + QTreeView +
qtcomponents.h
+
tabWidget