test: Add build system support and a main function for unit tests
[quassel.git] / src / uisupport / networkmodelcontroller.h
index ea7ab65..1d691f7 100644 (file)
@@ -22,6 +22,9 @@
 
 #include "uisupport-export.h"
 
+#include <functional>
+#include <type_traits>
+
 #include <QDialog>
 
 #include "action.h"
@@ -37,8 +40,7 @@ class UISUPPORT_EXPORT NetworkModelController : public QObject
     Q_OBJECT
 
 public:
-    NetworkModelController(QObject *parent = 0);
-    virtual ~NetworkModelController();
+    NetworkModelController(QObject *parent = nullptr);
 
     // don't change enums without doublechecking masks etc. in code
     enum ActionType {
@@ -133,14 +135,24 @@ protected:
     inline QList<QModelIndex> indexList() const;
     inline MessageFilter *messageFilter() const;
     inline QString contextItem() const;  ///< Channel name or nick to provide context menu for
-    inline QObject *receiver() const;
-    inline const char *method() const;
 
     void setIndexList(const QModelIndex &);
     void setIndexList(const QList<QModelIndex> &);
     void setMessageFilter(MessageFilter *);
     void setContextItem(const QString &);
-    void setSlot(QObject *receiver, const char *method);
+
+    using ActionSlot = std::function<void(QAction*)>;
+
+    template<typename Receiver, typename Slot>
+    ActionSlot buildActionSlot(Receiver *receiver, Slot slot)
+    {
+        static_assert(!std::is_same<Slot, const char*>::value, "Old-style slots not supported");
+        return [receiver, slot = std::move(slot)](QAction *action) {
+            (receiver->*slot)(action);
+        };
+    }
+
+    void setSlot(ActionSlot slot);
 
     Action *registerAction(ActionType type, const QString &text, bool checkable = false);
     Action *registerAction(NetworkModelController::ActionType type, const QIcon &icon, const QString &text, bool checkable = false);
@@ -185,10 +197,9 @@ private:
     QHash<ActionType, Action *> _actionByType;
 
     QList<QModelIndex> _indexList;
-    MessageFilter *_messageFilter;
+    MessageFilter *_messageFilter{nullptr};
     QString _contextItem; ///< Channel name or nick to provide context menu for
-    QObject *_receiver;
-    const char *_method;
+    ActionSlot _actionSlot;
 };
 
 
@@ -198,7 +209,7 @@ class NetworkModelController::JoinDlg : public QDialog
     Q_OBJECT
 
 public:
-    JoinDlg(const QModelIndex &index, QWidget *parent = 0);
+    JoinDlg(const QModelIndex &index, QWidget *parent = nullptr);
 
     QString channelName() const;
     QString channelPassword() const;
@@ -221,5 +232,3 @@ Action *NetworkModelController::action(ActionType type) const { return _actionBy
 QList<QModelIndex> NetworkModelController::indexList() const { return _indexList; }
 MessageFilter *NetworkModelController::messageFilter() const { return _messageFilter; }
 QString NetworkModelController::contextItem() const { return _contextItem; }
-QObject *NetworkModelController::receiver() const { return _receiver; }
-const char *NetworkModelController::method() const { return _method; }