Commit 8ee85555 authored by Ahmad Samir's avatar Ahmad Samir Committed by Ingo Klöcker
Browse files

src/view/key* : port to PMF signal/slot syntax

- Use a vector<Connection> to hold handles to the created connections
- Don't use sender(), intead capture the object and pass it to the slot
parent 0f14794f
Pipeline #181477 passed with stage
in 2 minutes and 27 seconds
......@@ -137,7 +137,7 @@ public:
Q_EMIT q->message(what);
}
}
void slotActionTriggered();
void slotActionTriggered(QAction *action);
void slotCurrentViewChanged(QAbstractItemView *view)
{
if (view && !std::binary_search(views.cbegin(), views.cend(), view)) {
......@@ -167,6 +167,7 @@ private:
QPointer<TabWidget> tabWidget;
QPointer<QAbstractItemView> currentView;
QPointer<AbstractKeyListModel> flatModel, hierarchicalModel;
std::vector<QMetaObject::Connection> m_connections;
};
KeyListController::Private::Private(KeyListController *qq)
......@@ -300,16 +301,6 @@ QWidget *KeyListController::parentWidget() const
return d->parentWidget;
}
static const struct {
const char *signal;
const char *slot;
} tabs2controller[] = {
{ SIGNAL(viewAdded(QAbstractItemView*)), SLOT(addView(QAbstractItemView*)) },
{ SIGNAL(viewAboutToBeRemoved(QAbstractItemView*)), SLOT(removeView(QAbstractItemView*)) },
{ SIGNAL(currentViewChanged(QAbstractItemView*)), SLOT(slotCurrentViewChanged(QAbstractItemView*)) },
};
static const unsigned int numTabs2Controller = sizeof tabs2controller / sizeof * tabs2controller;
void KeyListController::Private::connectTabWidget()
{
if (!tabWidget) {
......@@ -318,9 +309,11 @@ void KeyListController::Private::connectTabWidget()
const auto views = tabWidget->views();
std::for_each(views.cbegin(), views.cend(),
[this](QAbstractItemView *view) { addView(view); });
for (unsigned int i = 0; i < numTabs2Controller; ++i) {
connect(tabWidget, tabs2controller[i].signal, q, tabs2controller[i].slot);
}
m_connections.reserve(3);
m_connections.push_back(connect(tabWidget, &TabWidget::viewAdded, q, [this](QAbstractItemView *view) { addView(view);}));
m_connections.push_back(connect(tabWidget, &TabWidget::viewAboutToBeRemoved, q, [this](QAbstractItemView *view) { removeView(view); }));
m_connections.push_back(connect(tabWidget, &TabWidget::currentViewChanged, q, [this](QAbstractItemView *view) { slotCurrentViewChanged(view); }));
}
void KeyListController::Private::disconnectTabWidget()
......@@ -328,9 +321,11 @@ void KeyListController::Private::disconnectTabWidget()
if (!tabWidget) {
return;
}
for (unsigned int i = 0; i < numTabs2Controller; ++i) {
disconnect(tabWidget, tabs2controller[i].signal, q, tabs2controller[i].slot);
for (const auto &connection : m_connections) {
disconnect(connection);
}
m_connections.clear();
const auto views = tabWidget->views();
std::for_each(views.cbegin(), views.cend(),
[this](QAbstractItemView *view) { removeView(view); });
......@@ -586,7 +581,7 @@ void KeyListController::registerAction(QAction *action, Command::Restrictions re
const Private::action_item ai = {
action, restrictions, create
};
connect(action, SIGNAL(triggered()), this, SLOT(slotActionTriggered()));
connect(action, &QAction::triggered, this, [this, action]() { d->slotActionTriggered(action); });
d->actions.push_back(ai);
}
......@@ -810,25 +805,21 @@ Command::Restrictions KeyListController::Private::calculateRestrictionsMask(cons
return result;
}
void KeyListController::Private::slotActionTriggered()
{
if (const QObject *const s = q->sender()) {
const auto it = std::find_if(actions.cbegin(), actions.cend(),
[this](const action_item &item) { return item.action == q->sender(); });
if (it != actions.end())
if (Command *const c = it->createCommand(this->currentView, q)) {
if (parentWidget) {
c->setParentWidget(parentWidget);
}
c->start();
} else
qCDebug(KLEOPATRA_LOG) << "createCommand() == NULL for action(?) \""
<< qPrintable(s->objectName()) << "\"";
else {
qCDebug(KLEOPATRA_LOG) << "I don't know anything about action(?) \"%s\"", qPrintable(s->objectName());
}
} else {
qCDebug(KLEOPATRA_LOG) << "not called through a signal/slot connection (sender() == NULL)";
void KeyListController::Private::slotActionTriggered(QAction *sender)
{
const auto it = std::find_if(actions.cbegin(), actions.cend(),
[sender](const action_item &item) { return item.action == sender; });
if (it != actions.end())
if (Command *const c = it->createCommand(this->currentView, q)) {
if (parentWidget) {
c->setParentWidget(parentWidget);
}
c->start();
} else
qCDebug(KLEOPATRA_LOG) << "createCommand() == NULL for action(?) \""
<< qPrintable(sender->objectName()) << "\"";
else {
qCDebug(KLEOPATRA_LOG) << "I don't know anything about action(?) \"%s\"", qPrintable(sender->objectName());
}
}
......
......@@ -95,9 +95,6 @@ Q_SIGNALS:
private:
class Private;
kdtools::pimpl_ptr<Private> d;
Q_PRIVATE_SLOT(d, void slotActionTriggered())
Q_PRIVATE_SLOT(d, void slotCurrentViewChanged(QAbstractItemView *))
};
}
......
......@@ -10,6 +10,7 @@
#include <config-kleopatra.h>
#include "keytreeview.h"
#include "searchbar.h"
#include <Libkleo/KeyList>
#include <Libkleo/KeyListModel>
......@@ -765,37 +766,25 @@ void KeyTreeView::removeKeys(const std::vector<Key> &keys)
}
static const struct {
const char *signal;
const char *slot;
} connections[] = {
{
SIGNAL(stringFilterChanged(QString)),
SLOT(setStringFilter(QString))
},
{
SIGNAL(keyFilterChanged(std::shared_ptr<Kleo::KeyFilter>)),
SLOT(setKeyFilter(std::shared_ptr<Kleo::KeyFilter>))
},
};
static const unsigned int numConnections = sizeof connections / sizeof * connections;
void KeyTreeView::disconnectSearchBar(const QObject *bar)
void KeyTreeView::disconnectSearchBar()
{
for (unsigned int i = 0; i < numConnections; ++i) {
disconnect(this, connections[i].signal, bar, connections[i].slot);
disconnect(bar, connections[i].signal, this, connections[i].slot);
for (const auto &connection : m_connections) {
disconnect(connection);
}
m_connections.clear();
}
bool KeyTreeView::connectSearchBar(const QObject *bar)
bool KeyTreeView::connectSearchBar(const SearchBar *bar)
{
for (unsigned int i = 0; i < numConnections; ++i)
if (!connect(this, connections[i].signal, bar, connections[i].slot) ||
!connect(bar, connections[i].signal, this, connections[i].slot)) {
return false;
}
return true;
m_connections.reserve(4);
m_connections.push_back(connect(this, &KeyTreeView::stringFilterChanged, bar, &SearchBar::setStringFilter));
m_connections.push_back(connect(bar, &SearchBar::stringFilterChanged, this, &KeyTreeView::setStringFilter));
m_connections.push_back(connect(this, &KeyTreeView::keyFilterChanged, bar, &SearchBar::setKeyFilter));
m_connections.push_back(connect(bar, &SearchBar::keyFilterChanged, this, &KeyTreeView::setKeyFilter));
return std::all_of(m_connections.cbegin(), m_connections.cend(), [](const QMetaObject::Connection &conn) {
return conn;
});
}
void KeyTreeView::resizeColumns()
......
......@@ -30,6 +30,7 @@ class KeyFilter;
class AbstractKeyListModel;
class AbstractKeyListSortFilterProxyModel;
class KeyListSortFilterProxyModel;
class SearchBar;
class KeyTreeView : public QWidget
{
......@@ -106,8 +107,8 @@ public:
return new KeyTreeView(*this);
}
void disconnectSearchBar(const QObject *bar);
bool connectSearchBar(const QObject *bar);
void disconnectSearchBar();
bool connectSearchBar(const SearchBar *bar);
void resizeColumns();
void saveLayout(KConfigGroup &group);
......@@ -148,6 +149,8 @@ private:
QStringList m_expandedKeys;
std::vector<QMetaObject::Connection> m_connections;
KConfigGroup m_group;
bool m_isHierarchical : 1;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment