diff --git a/pglab/NotificationListWidget.cpp b/pglab/NotificationListWidget.cpp new file mode 100644 index 0000000..cb83182 --- /dev/null +++ b/pglab/NotificationListWidget.cpp @@ -0,0 +1,9 @@ +#include "NotificationListWidget.h" +#include + + +NotificationListWidget::NotificationListWidget() + : m_notificationList(new QListView(this)) +{ + +} diff --git a/pglab/NotificationListWidget.h b/pglab/NotificationListWidget.h new file mode 100644 index 0000000..2d9d097 --- /dev/null +++ b/pglab/NotificationListWidget.h @@ -0,0 +1,22 @@ +#ifndef NOTIFICATIONLISTWIDGET_H +#define NOTIFICATIONLISTWIDGET_H + +#include + +class QListView; +class QTextEdit; +class Notification; +class NotificationListModel; + +class NotificationListWidget: QWidget { +public: + NotificationListWidget(); + +private: + QListView *m_notificationList = nullptr; + QTextEdit *m_detailMessage; + NotificationListModel *m_notificationListModel = nullptr; + +}; + +#endif // NOTIFICATIONLISTWIDGET_H diff --git a/pglab/NotificationModel.cpp b/pglab/NotificationModel.cpp new file mode 100644 index 0000000..f912d2a --- /dev/null +++ b/pglab/NotificationModel.cpp @@ -0,0 +1,25 @@ +#include "NotificationModel.h" +#include "NotificationService.h" + +NotificationModel::NotificationModel() +{ + +} + +int NotificationModel::rowCount(const QModelIndex &parent) const +{ + return m_notifications->count(); +} + +int NotificationModel::columnCount(const QModelIndex &) const +{ + return static_cast(Col::Count); +} + +QVariant NotificationModel::data(const QModelIndex &index, int role) const +{ +} + +QVariant NotificationModel::headerData(int section, Qt::Orientation orientation, int role) const +{ +} diff --git a/pglab/NotificationModel.h b/pglab/NotificationModel.h new file mode 100644 index 0000000..68cbcc7 --- /dev/null +++ b/pglab/NotificationModel.h @@ -0,0 +1,31 @@ +#ifndef NOTIFICATIONMODEL_H +#define NOTIFICATIONMODEL_H + +#include +#include + +class NotificationService; + +class NotificationModel: public QAbstractTableModel { +public: + enum class Col { + Severity, + ShortMessage, + DetailMessage, + + Count // SHOULD BE LAST + }; + + NotificationModel(); + + virtual int rowCount(const QModelIndex &parent = {}) const override; + virtual int columnCount(const QModelIndex &parent = {}) const override; + + virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; + virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; + +private: + std::shared_ptr m_notifications; +}; + +#endif // NOTIFICATIONMODEL_H diff --git a/pglab/NotificationService.cpp b/pglab/NotificationService.cpp new file mode 100644 index 0000000..975c910 --- /dev/null +++ b/pglab/NotificationService.cpp @@ -0,0 +1,59 @@ +#include "NotificationService.h" + +namespace { + + class registerMetaTypes { + public: + registerMetaTypes() + { + qRegisterMetaType(); + qRegisterMetaType(); + } + } registerMetaTypes_instance; + +} + +std::shared_ptr NotificationService::s_instance; + +std::shared_ptr NotificationService::instance() +{ + if (!s_instance) { + s_instance = std::make_shared(); + } + return s_instance; +} + +NotificationService::NotificationService() +{ +} + +void NotificationService::addInformational(const QString &msg, const QString &detail) +{ + add(NotificationSeverity::Informational, msg, detail); +} + +void NotificationService::addWarning(const QString &msg, const QString &detail) +{ + add(NotificationSeverity::Warning, msg, detail); +} + +void NotificationService::addError(const QString &msg, const QString &detail) +{ + add(NotificationSeverity::Error, msg, detail); +} + +void NotificationService::add(NotificationSeverity severity, const QString &msg, const QString &detail) +{ + m_notifications.push_back({ severity, msg, detail }); +} + +int NotificationService::count() const +{ + return m_notifications.size(); +} + +const Notification& NotificationService::notification(int index) +{ + return m_notifications.at(index); +} + diff --git a/pglab/NotificationService.h b/pglab/NotificationService.h new file mode 100644 index 0000000..9101f8e --- /dev/null +++ b/pglab/NotificationService.h @@ -0,0 +1,64 @@ +#ifndef NOTIFICATIONSERVICE_H +#define NOTIFICATIONSERVICE_H + +#include +#include +#include +#include +#include + +enum NotificationSeverity { + Informational, ///< Use for notifications of succesful completion and other things the user likely does not need to be bothered with + Warning, ///< + Error, ///< Use for failures like not being able to save the users data + Critical ///< Don't think you should ever need this in a correct program.... +}; + +class Notification: public QObject { + Q_OBJECT +public: + Notification(int64_t id, NotificationSeverity severity, const QString &msg, const QString &detail = {}); + + int64_t id() const { return m_id; } + NotificationSeverity severity() const { return m_severity; } + QString shortMessage() const { return m_shortMessage; } + QString detailMessage() const { return m_detailMessage; } + +private: + int64_t m_id; + NotificationSeverity m_severity; + QString m_shortMessage; + QString m_detailMessage; + +}; + +class NotificationService { +public: + static std::shared_ptr instance(); + + NotificationService(); + + void addInformational(const QString &msg, const QString &detail = {}); + void addWarning(const QString &msg, const QString &detail = {}); + void addError(const QString &msg, const QString &detail = {}); + void add(NotificationSeverity severity, const QString &msg, const QString &detail = {}); + + /// Returns the number of notifications + int count() const; + const Notification& notification(int index); + +signals: + void onNewNotification(const Notification ¬ification); +private: + static std::shared_ptr s_instance; + + using NotificationContainer = QVector; + + std::shared_mutex m_nofiticationsMutex; + NotificationContainer m_notifications; +}; + +Q_DECLARE_METATYPE(NotificationSeverity) +Q_DECLARE_METATYPE(Notification) + +#endif // NOTIFICATIONSERVICE_H diff --git a/pglab/pglab.pro b/pglab/pglab.pro index 9e43dae..7fd7126 100644 --- a/pglab/pglab.pro +++ b/pglab/pglab.pro @@ -21,6 +21,9 @@ DEFINES += _WIN32_WINNT=0x0501 win32:RC_ICONS += pglab.ico SOURCES += main.cpp\ + NotificationListWidget.cpp \ + NotificationModel.cpp \ + NotificationService.cpp \ QueryResultModel.cpp \ QueryExplainModel.cpp \ CreateDatabaseDialog.cpp \ @@ -93,6 +96,9 @@ PropertyProxyModel.cpp \ widgets/CatalogSequencesPage.cpp HEADERS += \ + NotificationListWidget.h \ + NotificationModel.h \ + NotificationService.h \ QueryResultModel.h \ QueryExplainModel.h \ CreateDatabaseDialog.h \