-
Notifications
You must be signed in to change notification settings - Fork 65
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
gallery: basic model/view for notifications #2765
Closed
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
import QtQuick | ||
import "../Style.js" as Style | ||
|
||
Row { | ||
id: notification | ||
|
||
width: listView.width * 0.95 | ||
height: 25 | ||
anchors.horizontalCenter: parent ? parent.horizontalCenter : undefined | ||
|
||
Rectangle { | ||
width: parent.width | ||
height: parent.height | ||
color: "#BBFFFFFF" // Style.white | ||
border.color: Style.mediumGreen | ||
border.width: 1 | ||
radius: 5 | ||
Text { | ||
anchors.fill: parent | ||
anchors.leftMargin: 5 | ||
text: message | ||
color: Style.forest | ||
verticalAlignment: Text.AlignVCenter | ||
horizontalAlignment: Text.AlignRight | ||
rightPadding: 20 | ||
} | ||
Text { | ||
anchors.right: parent.right | ||
anchors.rightMargin: 5 | ||
height: parent.height | ||
verticalAlignment: Text.AlignVCenter | ||
scale: maRemove.containsMouse ? 1.2 : 1 | ||
text: "✘" | ||
color: maRemove.containsMouse ? Style.forest : Style.mediumGreen | ||
MouseArea { | ||
id: maRemove | ||
anchors.fill: parent | ||
hoverEnabled: true | ||
onClicked: _notificationModel.remove(id) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
import QtQuick | ||
import "../Style.js" as Style | ||
|
||
Item { | ||
id: control | ||
|
||
z: 100 | ||
anchors.top: parent.top | ||
width: parent.width | ||
height: parent.height | ||
|
||
Rectangle { | ||
anchors.bottom: parent.bottom | ||
width: control.width | ||
height: 20 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From style |
||
color: Style.white | ||
|
||
Text { | ||
text: listView.count | ||
anchors.centerIn: parent | ||
color: Style.forest | ||
} | ||
} | ||
|
||
ListView { | ||
id: listView | ||
|
||
anchors.top: parent.top | ||
width: parent.width | ||
height: 25 * listView.count + spacing * (listView.count - 1) | ||
spacing: 3 | ||
clip: true | ||
model: _notificationModel | ||
delegate: MMNotification { | ||
|
||
} | ||
|
||
add: Transition { | ||
NumberAnimation { property: "opacity"; from: 0; to: 1.0; duration: 200 } | ||
NumberAnimation { property: "scale"; easing.type: Easing.OutCubic; from: 0; to: 1.0; duration: 200 } | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#include "notificationmodel.h" | ||
|
||
Notification::Notification(uint id, const QString &message, uint interval, MessageType type = Information) | ||
{ | ||
mId = id; | ||
mMessage = message; | ||
mInterval = interval; | ||
mType = type; | ||
} | ||
|
||
NotificationModel::NotificationModel(QObject *parent) : QAbstractListModel{parent} | ||
{ | ||
// Initial data | ||
_notifications << Notification{ nextId(), "Ahoj", 10 }; | ||
_notifications << Notification{ nextId(), "Hello all", 5 }; | ||
} | ||
|
||
QHash<int, QByteArray> NotificationModel::roleNames() const | ||
{ | ||
return { | ||
{ IdRole, "id" }, | ||
{ MessageRole, "message" }, | ||
{ TypeRole, "type" } | ||
}; | ||
} | ||
|
||
int NotificationModel::rowCount(const QModelIndex &parent) const | ||
{ | ||
Q_UNUSED(parent) | ||
return _notifications.size(); | ||
} | ||
|
||
QVariant NotificationModel::data(const QModelIndex &index, int role) const | ||
{ | ||
if (!hasIndex(index.row(), index.column(), index.parent())) | ||
return {}; | ||
|
||
Notification notification = _notifications.at(index.row()); | ||
if (role == IdRole) return notification.id(); | ||
if (role == MessageRole) return notification.message(); | ||
if (role == TypeRole) return notification.type(); | ||
|
||
return {}; | ||
} | ||
|
||
// remove item by message | ||
void NotificationModel::remove(uint id) | ||
{ | ||
for(int i=0; i<_notifications.count(); i++) { | ||
if(_notifications[i].id() == id) { | ||
beginRemoveRows(QModelIndex(), i, i); | ||
_notifications.removeAt(i); | ||
endRemoveRows(); | ||
|
||
emit dataChanged(createIndex(0, 0), createIndex(rowCount(), 0)); // refresh whole model | ||
emit rowCountChanged(); | ||
return; | ||
} | ||
} | ||
} | ||
|
||
// add new unique message with interval | ||
void NotificationModel::add(const QString &message, uint interval, Notification::MessageType type = Notification::Information) | ||
{ | ||
for(Notification ¬ification : _notifications) { | ||
if(notification.message() == message) | ||
return; | ||
} | ||
|
||
beginInsertRows(QModelIndex(), rowCount(), rowCount()); | ||
_notifications << Notification{ nextId(), message, interval, type }; | ||
endInsertRows(); | ||
|
||
emit rowCountChanged(); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#ifndef NOTIFICATIONMODEL_H | ||
#define NOTIFICATIONMODEL_H | ||
|
||
#include "qqmlintegration.h" | ||
#include <QAbstractListModel> | ||
|
||
class Notification | ||
{ | ||
Q_GADGET | ||
|
||
public: | ||
enum MessageType { | ||
Information, | ||
Warning, | ||
Error | ||
}; | ||
Q_ENUM(MessageType) | ||
|
||
Notification(uint id, const QString &message, uint interval, MessageType type); | ||
uint id() { return mId; } | ||
QString message() { return mMessage; } | ||
MessageType type() { return mType; } | ||
|
||
private: | ||
uint mId; | ||
QString mMessage; | ||
uint mInterval; // [seconds] | ||
MessageType mType = Information; | ||
}; | ||
|
||
class NotificationModel : public QAbstractListModel | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
enum MyRoles { | ||
IdRole = Qt::UserRole + 1, MessageRole, TypeRole | ||
}; | ||
Q_ENUM(MyRoles) | ||
|
||
NotificationModel(QObject *parent = nullptr); | ||
|
||
QHash<int,QByteArray> roleNames() const override; | ||
int rowCount(const QModelIndex & parent = QModelIndex()) const override; | ||
QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const override; | ||
|
||
Q_PROPERTY(int rowCount READ rowCount NOTIFY rowCountChanged); | ||
Q_INVOKABLE void remove(uint id); | ||
Q_INVOKABLE void add(const QString &message, uint interval, Notification::MessageType type); | ||
|
||
private: | ||
uint nextId() { static uint id = 0; return id++; } | ||
|
||
signals: | ||
void rowCountChanged(); | ||
|
||
private: | ||
QList<Notification> _notifications; | ||
}; | ||
|
||
#endif // NOTIFICATIONMODEL_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
z