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 <QApplication>
14
15#include <kcolorscheme.h>
16#include <KLocalizedString>
17
18#include "PythonPluginManager.h"
19
21 : QAbstractTableModel(parent)
22 , m_pluginManager(pluginManager)
23{
24}
25
26int PythonPluginsModel::columnCount(const QModelIndex&) const
27{
28 return COLUMN_COUNT;
29}
30
31int PythonPluginsModel::rowCount(const QModelIndex&) const
32{
33 return m_pluginManager->plugins().size();
34}
35
36QModelIndex PythonPluginsModel::index(const int row, const int column, const QModelIndex& parent) const
37{
38 if (!parent.isValid() && column < COLUMN_COUNT) {
40 if (plugin) {
41 return createIndex(row, column, plugin);
42 }
43 }
44
45 return QModelIndex();
46}
47
48QVariant PythonPluginsModel::headerData(const int section, const Qt::Orientation orientation, const int role) const
49{
50 if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
51 switch (section) {
52 case COL_NAME:
53 return i18nc("@title:column", "Name");
54 case COL_COMMENT:
55 return i18nc("@title:column", "Comment");
56 case COL_STATUS:
57 return i18nc("@title:column", "Status");
58 default:
59 break;
60 }
61 }
62 return QVariant();
63}
64
65QVariant PythonPluginsModel::data(const QModelIndex& index, const int role) const
66{
67 if (index.isValid()) {
68 PythonPlugin *plugin = static_cast<PythonPlugin*>(index.internalPointer());
70
71 switch (role) {
72 case Qt::DisplayRole:
73 switch (index.column()) {
74 case COL_NAME:
75 return plugin->name();
76 case COL_COMMENT:
77 return plugin->comment();
78 case COL_STATUS:
79 return plugin->statusText();
80 default:
81 break;
82 }
83 break;
84 case Qt::CheckStateRole:
85 if (index.column() == COL_ENABLED) {
86 return plugin->isEnabled() ? Qt::Checked : Qt::Unchecked;
87 }
88 break;
89 case Qt::ToolTipRole:
90 if (index.column() == COL_COMMENT) {
91 // Show comment in case it was elided
92 return plugin->comment();
93 } else {
94 const QString error = plugin->errorReason();
95 if (!error.isEmpty()) {
96 return error;
97 }
98 }
99 break;
100 case Qt::ForegroundRole:
101 if (plugin->isUnstable() || !plugin->errorReason().isEmpty()) {
102 if (plugin->isEnabled()) {
103 KColorScheme scheme(QPalette::Active, KColorScheme::View);
104 return scheme.foreground(KColorScheme::NegativeText).color();
105 } else {
106 KColorScheme scheme(QPalette::Disabled, KColorScheme::View);
107 return scheme.foreground(KColorScheme::NegativeText).color();
108 }
109 } else if (!plugin->isEnabled()) {
110 // Show disabled plugins with a disabled color,
111 // without actually disabling the widget (which prevents selecting it)
112 return qApp->palette().color(QPalette::Disabled, QPalette::WindowText);
113 }
114 break;
115 default:
116 break;
117 }
118 }
119
120 return QVariant();
121}
122
123Qt::ItemFlags PythonPluginsModel::flags(const QModelIndex& index) const
124{
125 PythonPlugin *plugin = static_cast<PythonPlugin*>(index.internalPointer());
126 KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(plugin, Qt::ItemIsSelectable);
127
128 int result = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
129 if (index.column() == COL_ENABLED) {
130 result |= Qt::ItemIsUserCheckable;
131 }
132
133 return static_cast<Qt::ItemFlag>(result);
134}
135
136bool PythonPluginsModel::setData(const QModelIndex& index, const QVariant& value, const int role)
137{
138 PythonPlugin *plugin = static_cast<PythonPlugin*>(index.internalPointer());
140
141 if (role == Qt::CheckStateRole) {
143 }
144 return true;
145}
146
147PythonPlugin *PythonPluginsModel::plugin(const QModelIndex &index) const
148{
149 if (index.isValid()) {
150 PythonPlugin * plugin = static_cast<PythonPlugin*>(index.internalPointer());
151 return plugin;
152 }
153 return 0;
154}
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 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