Krita Source Code Documentation
Loading...
Searching...
No Matches
PythonPluginsModel.cpp
Go to the documentation of this file.
1/*
2 * This file is part of PyKrita, Krita' Python scripting plugin.
3 *
4 * SPDX-FileCopyrightText: 2013 Alex Turbov <i.zaufi@gmail.com>
5 * SPDX-FileCopyrightText: 2014-2016 Boudewijn Rempt <boud@valdyas.org>
6 * SPDX-FileCopyrightText: 2017 Jouni Pentikäinen (joupent@gmail.com)
7 *
8 * SPDX-License-Identifier: LGPL-2.0-or-later
9 */
10
11#include "PythonPluginsModel.h"
12
13#include <kcolorscheme.h>
14#include <KLocalizedString>
15
16#include "PythonPluginManager.h"
17
19 : QAbstractTableModel(parent)
20 , m_pluginManager(pluginManager)
21{
22}
23
24int PythonPluginsModel::columnCount(const QModelIndex&) const
25{
26 return COLUMN_COUNT;
27}
28
29int PythonPluginsModel::rowCount(const QModelIndex&) const
30{
31 return m_pluginManager->plugins().size();
32}
33
34QModelIndex PythonPluginsModel::index(const int row, const int column, const QModelIndex& parent) const
35{
36 if (!parent.isValid() && column < COLUMN_COUNT) {
37 auto *plugin = m_pluginManager->plugin(row);
38 if (plugin) {
39 return createIndex(row, column, plugin);
40 }
41 }
42
43 return QModelIndex();
44}
45
46QVariant PythonPluginsModel::headerData(const int section, const Qt::Orientation orientation, const int role) const
47{
48 if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
49 switch (section) {
50 case COl_NAME:
51 return i18nc("@title:column", "Name");
52 case COL_COMMENT:
53 return i18nc("@title:column", "Comment");
54 default:
55 break;
56 }
57 }
58 return QVariant();
59}
60
61QVariant PythonPluginsModel::data(const QModelIndex& index, const int role) const
62{
63 if (index.isValid()) {
64 PythonPlugin *plugin = static_cast<PythonPlugin*>(index.internalPointer());
66
67 switch (role) {
68 case Qt::DisplayRole:
69 switch (index.column()) {
70 case COl_NAME:
71 return plugin->name();
72 case COL_COMMENT:
73 return plugin->comment();
74 default:
75 break;
76 }
77 break;
78 case Qt::CheckStateRole:
79 if (index.column() == COl_NAME) {
80 const bool checked = plugin->isEnabled();
81 return checked ? Qt::Checked : Qt::Unchecked;
82 }
83 break;
84 case Qt::ToolTipRole:
85 {
86 auto error = plugin->errorReason();
87 if (!error.isEmpty()) {
88 return error;
89 }
90 }
91 break;
92 case Qt::ForegroundRole:
93 if (plugin->isUnstable()) {
94 KColorScheme scheme(QPalette::Inactive, KColorScheme::View);
95 return scheme.foreground(KColorScheme::NegativeText).color();
96 }
97 break;
98 default:
99 break;
100 }
101 }
102
103 return QVariant();
104}
105
106Qt::ItemFlags PythonPluginsModel::flags(const QModelIndex& index) const
107{
108 PythonPlugin *plugin = static_cast<PythonPlugin*>(index.internalPointer());
109 KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(plugin, Qt::ItemIsSelectable);
110
111 int result = Qt::ItemIsSelectable;
112 if (index.column() == COl_NAME) {
113 result |= Qt::ItemIsUserCheckable;
114 }
115
116 // Disable UI for broken modules
117 if (!plugin->isBroken()) {
118 result |= Qt::ItemIsEnabled;
119 }
120
121 return static_cast<Qt::ItemFlag>(result);
122}
123
124bool PythonPluginsModel::setData(const QModelIndex& index, const QVariant& value, const int role)
125{
126 PythonPlugin *plugin = static_cast<PythonPlugin*>(index.internalPointer());
128
129 if (role == Qt::CheckStateRole) {
131
132 const bool enabled = value.toBool();
134 }
135 return true;
136}
137
138PythonPlugin *PythonPluginsModel::plugin(const QModelIndex &index) const
139{
140 if (index.isValid()) {
141 PythonPlugin * plugin = static_cast<PythonPlugin*>(index.internalPointer());
142 return plugin;
143 }
144 return 0;
145}
float value(const T *src, size_t ch)
QBrush foreground(ForegroundRole=NormalText) const
void setPluginEnabled(PythonPlugin &plugin, bool enabled)
PythonPlugin * plugin(int index)
const QList< PythonPlugin > & plugins() const
QString comment() const
QString name() const
bool isBroken() const
bool isEnabled() const
bool isUnstable() const
const QString & errorReason() const
bool setData(const QModelIndex &, const QVariant &, int) override
PythonPlugin * plugin(const QModelIndex &) const
Qt::ItemFlags flags(const QModelIndex &) const override
PythonPluginsModel(QObject *parent, PythonPluginManager *pluginManager)
QModelIndex index(int row, int column, const QModelIndex &parent) const override
QVariant headerData(int, Qt::Orientation, int) const override
int columnCount(const QModelIndex &) const override
PythonPluginManager * m_pluginManager
QVariant data(const QModelIndex &, int) const override
int rowCount(const QModelIndex &) const override
#define KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(cond, val)
Definition kis_assert.h:129