Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to remove account folder with Delete or Backspace keystroke #588

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
28 changes: 26 additions & 2 deletions src/dialogsettings.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <Qt>

#include <QFileDialog>
#include <QColorDialog>
#include <QMessageBox>
Expand Down Expand Up @@ -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<DialogSettings*>(handle)->onKeyPressedOnTreeAccount(event);
});

// New emails tab
mModelNewEmails = new ModelNewEmails( this );
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;

Expand All @@ -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() {
Expand Down
5 changes: 5 additions & 0 deletions src/dialogsettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define SETTINGSDIALOG_H

#include <QDialog>
#include <QKeyEvent>
#include <QProgressDialog>
#include <QPalette>
#include <QtCore/QStringListModel>
Expand Down Expand Up @@ -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();
Expand All @@ -77,6 +80,8 @@ class DialogSettings : public QDialog, public Ui::DialogSettings
*/
static void onTranslatorsDialog();

void onKeyPressedOnTreeAccount(QKeyEvent * event);

private:
void changeIcon(QToolButton * button );

Expand Down
7 changes: 6 additions & 1 deletion src/dialogsettings.ui
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QTreeView" name="treeAccounts">
<widget class="QTreeViewWithKeyEvents" name="treeAccounts">
<property name="enabled">
<bool>true</bool>
</property>
Expand Down Expand Up @@ -1167,6 +1167,11 @@ p, li { white-space: pre-wrap; }
<extends>QPushButton</extends>
<header>colorbutton.h</header>
</customwidget>
<customwidget>
<class>QTreeViewWithKeyEvents</class>
<extends>QTreeView</extends>
<header>qtcomponents.h</header>
</customwidget>
</customwidgets>
<tabstops>
<tabstop>tabWidget</tabstop>
Expand Down
22 changes: 22 additions & 0 deletions src/qtcomponents.cpp
Original file line number Diff line number Diff line change
@@ -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);
}
29 changes: 29 additions & 0 deletions src/qtcomponents.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef QTCOMPONENTS_H
#define QTCOMPONENTS_H

#include <QKeyEvent>
#include <QTreeView>

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;
};
Comment on lines +7 to +27
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about we just use the Qt event system instead of building our own, that way we can avoid some casts and manually storing listeners.

Suggested change
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;
};
/**
* A QTreeView that allow to define listener of keyEvent received.
*/
class QTreeViewWithKeyEvents : public QTreeView {
Q_OBJECT
public:
explicit QTreeViewWithKeyEvents(QWidget* parent = nullptr);
signals:
void onKeyPressed(QKeyEvent* event);
protected:
void keyPressEvent(QKeyEvent* event) override;
};

with

void QTreeViewWithKeyEvents::keyPressEvent( QKeyEvent *event )
{
    emit onKeyPressed(event);
    QTreeView::keyPressEvent(event);
}

and

connect(treeAccounts, &QTreeViewWithKeyEvents::onKeyPressed, this, &DialogSettings::onKeyPressedOnTreeAccount);

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine with me. I mean, those are rare operations, hardly worthy optimizing, but as you wish :)


#endif // QTCOMPONENTS_H
Loading