Changeset View
Changeset View
Standalone View
Standalone View
libdiscover/ActionsModel.cpp
- This file was moved from libdiscover/MessageActionsModel.cpp.
Show All 12 Lines | |||||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||
15 | * GNU General Public License for more details. * | 15 | * GNU General Public License for more details. * | ||
16 | * * | 16 | * * | ||
17 | * You should have received a copy of the GNU General Public License * | 17 | * You should have received a copy of the GNU General Public License * | ||
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. * | 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. * | ||
19 | ***************************************************************************/ | 19 | ***************************************************************************/ | ||
20 | 20 | | |||
21 | #include "MessageActionsModel.h" | 21 | #include "ActionsModel.h" | ||
22 | #include "resources/ResourcesModel.h" | 22 | #include "resources/ResourcesModel.h" | ||
23 | #include "utils.h" | 23 | #include "utils.h" | ||
24 | #include <QAction> | 24 | #include <QAction> | ||
25 | 25 | | |||
26 | MessageActionsModel::MessageActionsModel(QObject* parent) | 26 | ActionsModel::ActionsModel(QObject* parent) | ||
27 | : QAbstractListModel(parent) | 27 | : QAbstractListModel(parent) | ||
28 | , m_priority(-1) | 28 | , m_priority(-1) | ||
29 | { | 29 | { | ||
30 | connect(ResourcesModel::global(), &ResourcesModel::backendsChanged, this, &MessageActionsModel::reload); | 30 | connect(ResourcesModel::global(), &ResourcesModel::backendsChanged, this, &ActionsModel::reload); | ||
31 | } | 31 | } | ||
32 | 32 | | |||
33 | QHash< int, QByteArray > MessageActionsModel::roleNames() const | 33 | QHash< int, QByteArray > ActionsModel::roleNames() const | ||
34 | { | 34 | { | ||
35 | return { { Qt::UserRole, "action" }}; | 35 | return { { Qt::UserRole, "action" }}; | ||
36 | } | 36 | } | ||
37 | 37 | | |||
38 | QVariant MessageActionsModel::data(const QModelIndex& index, int role) const | 38 | QVariant ActionsModel::data(const QModelIndex& index, int role) const | ||
39 | { | 39 | { | ||
40 | if(!index.isValid() || role!=Qt::UserRole) | 40 | if(!index.isValid() || role!=Qt::UserRole) | ||
41 | return QVariant(); | 41 | return QVariant(); | ||
42 | return QVariant::fromValue<QObject*>(m_actions[index.row()]); | 42 | return QVariant::fromValue<QObject*>(m_actions[index.row()]); | ||
43 | } | 43 | } | ||
44 | 44 | | |||
45 | int MessageActionsModel::rowCount(const QModelIndex& parent) const | 45 | int ActionsModel::rowCount(const QModelIndex& parent) const | ||
46 | { | 46 | { | ||
47 | return parent.isValid() ? 0 : m_actions.count(); | 47 | return parent.isValid() ? 0 : m_actions.count(); | ||
48 | } | 48 | } | ||
49 | 49 | | |||
50 | void MessageActionsModel::reload() | 50 | void ActionsModel::setActions(const QList<QAction *>& actions) | ||
51 | { | 51 | { | ||
52 | auto actions = ResourcesModel::global()->messageActions(); | 52 | if (m_actions == actions) { | ||
53 | return; | ||||
54 | } | ||||
55 | | ||||
56 | reload(); | ||||
57 | Q_EMIT actionsChanged(m_actions); | ||||
58 | } | ||||
59 | | ||||
60 | void ActionsModel::reload() | ||||
61 | { | ||||
62 | auto actions = m_filteredActions; | ||||
53 | if (m_priority>=0) { | 63 | if (m_priority>=0) { | ||
54 | actions = kFilter<QList<QAction*>>(actions, [this](QAction* action){ return action->priority() == m_priority; }); | 64 | actions = kFilter<QList<QAction*>>(actions, [this](QAction* action){ return action->priority() == m_priority; }); | ||
55 | } | 65 | } | ||
66 | actions = kFilter<QList<QAction*>>(actions, [](QAction* action){ return action->isVisible(); }); | ||||
56 | if (actions == m_actions) | 67 | if (actions == m_actions) | ||
57 | return; | 68 | return; | ||
58 | 69 | | |||
59 | beginResetModel(); | 70 | beginResetModel(); | ||
60 | m_actions = actions; | 71 | m_actions = actions; | ||
61 | endResetModel(); | 72 | endResetModel(); | ||
62 | } | 73 | } | ||
63 | 74 | | |||
64 | int MessageActionsModel::filterPriority() const | 75 | int ActionsModel::filterPriority() const | ||
65 | { | 76 | { | ||
66 | return m_priority; | 77 | return m_priority; | ||
67 | } | 78 | } | ||
68 | 79 | | |||
69 | void MessageActionsModel::setFilterPriority(int p) | 80 | void ActionsModel::setFilterPriority(int p) | ||
70 | { | 81 | { | ||
71 | if (m_priority != p) { | 82 | if (m_priority != p) { | ||
72 | m_priority = p; | 83 | m_priority = p; | ||
73 | reload(); | 84 | reload(); | ||
74 | } | 85 | } | ||
75 | } | 86 | } | ||
76 | | ||||
77 | void MessageActionsModel::componentComplete() | | |||
78 | { | | |||
79 | reload(); | | |||
80 | } | |