Krita

Krita.cpp
1/*
2 * SPDX-FileCopyrightText: 2016 Boudewijn Rempt <boud@valdyas.org>
3 *
4 * SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6#include "Krita.h"
7
8#include <QPointer>
9#include <QVariant>
10#include <QStringList>
11
12#include <ksharedconfig.h>
13#include <kconfiggroup.h>
14#include <klocalizedstring.h>
15
16#include <KoColorSpaceRegistry.h>
17#include <KoColorProfile.h>
18#include <KoColorSpace.h>
19#include <KoDockRegistry.h>
20#include <KoColorSpaceEngine.h>
21#include <KoColorModelStandardIds.h>
22#include <KoID.h>
23
24#include <kis_filter_strategy.h>
25#include <kactioncollection.h>
26#include <KisPart.h>
27#include <KisMainWindow.h>
28#include <KisDocument.h>
29#include <kis_image.h>
30#include <kis_action.h>
31#include <KisViewManager.h>
32#include <KritaVersionWrapper.h>
33#include <kis_filter_registry.h>
34#include <kis_filter.h>
35#include <kis_filter_configuration.h>
36#include <kis_properties_configuration.h>
37#include <kis_config.h>
38#include <kis_workspace_resource.h>
39#include <brushengine/kis_paintop_preset.h>
40#include <KisBrushServerProvider.h>
41#include <KoResourceServerProvider.h>
42#include <KisResourceServerProvider.h>
43#include <KisBrushServerProvider.h>
44#include <kis_action_registry.h>
45#include <kis_icon_utils.h>
46
47#include <KisResourceModel.h>
48#include <KisGlobalResourcesInterface.h>
49
50#include "View.h"
51#include "Document.h"
52#include "Window.h"
53#include "Extension.h"
54#include "DockWidgetFactoryBase.h"
55#include "Filter.h"
56#include "InfoObject.h"
57#include "Resource.h"
58
59Krita* Krita::s_instance = 0;
60
61struct Krita::Private {
62 Private() {}
63 QList<Extension*> extensions;
64 bool batchMode {false};
65 Notifier *notifier{new Notifier()};
66};
67
68Krita::Krita(QObject *parent)
69 : QObject(parent)
70 , d(new Private)
71{
72 qRegisterMetaType<Notifier*>();
73 connect(KisPart::instance(), SIGNAL(sigMainWindowIsBeingCreated(KisMainWindow*)), SLOT(mainWindowIsBeingCreated(KisMainWindow*)));
74}
75
76Krita::~Krita()
77{
78 qDeleteAll(d->extensions);
79 delete d->notifier;
80 delete d;
81}
82
84{
85 KisMainWindow *mainWindow = KisPart::instance()->currentMainwindow();
86 if (!mainWindow) {
87 return QList<QAction*>();
88 }
89 KisKActionCollection *actionCollection = mainWindow->actionCollection();
90 return actionCollection->actions();
91}
92
93QAction *Krita::action(const QString &name) const
94{
95 KisMainWindow *mainWindow = KisPart::instance()->currentMainwindow();
96 if (!mainWindow) {
97 return 0;
98 }
99 KisKActionCollection *actionCollection = mainWindow->actionCollection();
100 QAction *action = actionCollection->action(name);
101 return action;
102}
103
105{
106 KisMainWindow *mainWindow = KisPart::instance()->currentMainwindow();
107 if (!mainWindow) {
108 return 0;
109 }
110 KisView *view = mainWindow->activeView();
111 if (!view) {
112 return 0;
113 }
114 KisDocument *document = view->document();
115 Document *d = new Document(document, false);
116 return d;
117}
118
120{
121 Q_FOREACH(KisView *view, KisPart::instance()->views()) {
122 if (view->document() == value->document().data()) {
123 view->activateWindow();
124 break;
125 }
126 }
127}
128
130{
131 return d->batchMode;
132}
133
134void Krita::setBatchmode(bool value)
135{
136 d->batchMode = value;
137}
138
139
141{
143 foreach(QPointer<KisDocument> doc, KisPart::instance()->documents()) {
144 ret << new Document(doc, false);
145 }
146 return ret;
147}
148
150{
151 QStringList ls = KisFilterRegistry::instance()->keys();
152 std::sort(ls.begin(), ls.end());
153 return ls;
154}
155
156Filter *Krita::filter(const QString &name) const
157{
158 if (!filters().contains(name)) return 0;
159
160 Filter *filter = new Filter();
161 filter->setName(name);
162 KisFilterSP f = KisFilterRegistry::instance()->value(name);
163 KisFilterConfigurationSP fc = f->defaultConfiguration(KisGlobalResourcesInterface::instance());
164 InfoObject *info = new InfoObject(fc);
165 filter->setConfiguration(info);
166 return filter;
167}
168
170{
171 QSet<QString> colorModelsIds;
172 QList<KoID> ids = KoColorSpaceRegistry::instance()->colorModelsList(KoColorSpaceRegistry::AllColorSpaces);
173 Q_FOREACH(KoID id, ids) {
174 colorModelsIds << id.id();
175 }
176 return QStringList(colorModelsIds.begin(), colorModelsIds.end());
177}
178
179QStringList Krita::colorDepths(const QString &colorModel) const
180{
181 QSet<QString> colorDepthsIds;
182 QList<KoID> ids = KoColorSpaceRegistry::instance()->colorDepthList(colorModel, KoColorSpaceRegistry::AllColorSpaces);
183 Q_FOREACH(KoID id, ids) {
184 colorDepthsIds << id.id();
185 }
186 return QStringList(colorDepthsIds.begin(), colorDepthsIds.end());
187}
188
190{
191 return KisFilterStrategyRegistry::instance()->keys();
192}
193
194QStringList Krita::profiles(const QString &colorModel, const QString &colorDepth) const
195{
196 QSet<QString> profileNames;
197 QString id = KoColorSpaceRegistry::instance()->colorSpaceId(colorModel, colorDepth);
198 QList<const KoColorProfile *> profiles = KoColorSpaceRegistry::instance()->profilesFor(id);
199 Q_FOREACH(const KoColorProfile *profile, profiles) {
200 profileNames << profile->name();
201 }
202 QStringList r(profileNames.begin(), profileNames.end());
203 r.sort();
204 return r;
205}
206
207bool Krita::addProfile(const QString &profilePath)
208{
209 KoColorSpaceEngine *iccEngine = KoColorSpaceEngineRegistry::instance()->get("icc");
210 KIS_ASSERT(iccEngine);
211 return iccEngine->addProfile(profilePath);
212}
213
215{
216 return d->notifier;
217}
218
220{
221 return KritaVersionWrapper::versionString(true);
222}
223
225{
226 QList<View *> ret;
227 foreach(QPointer<KisView> view, KisPart::instance()->views()) {
228 ret << new View(view);
229 }
230 return ret;
231}
232
234{
235 KisMainWindow *mainWindow = KisPart::instance()->currentMainwindow();
236 if (!mainWindow) {
237 return 0;
238 }
239 return new Window(mainWindow);
240}
241
243{
244 QList<Window*> ret;
245 foreach(QPointer<KisMainWindow> mainWin, KisPart::instance()->mainWindows()) {
246 ret << new Window(mainWin);
247 }
248 return ret;
249}
250
252{
254 KisResourceModel *resourceModel = 0;
255 if (type == "pattern") {
256 resourceModel = KoResourceServerProvider::instance()->patternServer()->resourceModel();
257 type = ResourceType::Patterns;
258 }
259 else if (type == "gradient") {
260 type = ResourceType::Gradients;
261 resourceModel = KoResourceServerProvider::instance()->gradientServer()->resourceModel();
262 }
263 else if (type == "brush") {
264 resourceModel = KisBrushServerProvider::instance()->brushServer()->resourceModel();
265 type = ResourceType::Brushes;
266 }
267 else if (type == "palette") {
268 resourceModel = KoResourceServerProvider::instance()->paletteServer()->resourceModel();
269 type = ResourceType::Palettes;
270 }
271 else if (type == "workspace") {
272 resourceModel = KisResourceServerProvider::instance()->workspaceServer()->resourceModel();
273 type = ResourceType::Workspaces;
274 }
275 else if (type == "preset") {
276 resourceModel = KisResourceServerProvider::instance()->paintOpPresetServer()->resourceModel();
277 }
278
279 if (resourceModel) {
280 for (int i = 0; i < resourceModel->rowCount(); ++i) {
281
282 QModelIndex idx = resourceModel->index(i, 0);
283 int id = resourceModel->data(idx, Qt::UserRole + KisAbstractResourceModel::Id).toInt();
284 QString name = resourceModel->data(idx, Qt::UserRole + KisAbstractResourceModel::Name).toString();
285 QString filename = resourceModel->data(idx, Qt::UserRole + KisAbstractResourceModel::Filename).toString();
286 QImage image = resourceModel->data(idx, Qt::UserRole + KisAbstractResourceModel::Thumbnail).value<QImage>();
287
288 resources[name] = new Resource(id, type, name, filename, image, 0);
289 }
290 }
291
292 return resources;
293}
294
295
297{
298 KisMainWindow *mainWindow = KisPart::instance()->currentMainwindow();
299
300 if (!mainWindow) return {};
301 return mainWindow->dockWidgets();
302}
303
304
306{
307 KConfigGroup grp = KSharedConfig::openConfig()->group(QString("RecentFiles"));
308 QStringList keys = grp.keyList();
310
311 for(int i = 0; i <= keys.filter("File").count(); i++)
312 recentDocuments << grp.readEntry(QString("File%1").arg(i), QString(""));
313
314 return recentDocuments;
315}
316
317Document* Krita::createDocument(int width, int height, const QString &name, const QString &colorModel, const QString &colorDepth, const QString &profile, double resolution)
318{
319 KisDocument *document = KisPart::instance()->createDocument();
320 document->setObjectName(name);
321
322 KisPart::instance()->addDocument(document, false);
323 const KoColorSpace *cs = KoColorSpaceRegistry::instance()->colorSpace(colorModel, colorDepth, profile);
324 Q_ASSERT(cs);
325
326 QColor qc(Qt::white);
327 KoColor bgColor(qc, cs);
328
329 if (!document->newImage(name, width, height, cs, bgColor, KisConfig::RASTER_LAYER, 1, "", double(resolution / 72) )) {
330 return 0;
331 }
332
333 Q_ASSERT(document->image());
334 Document *doc = new Document(document, true);
335
336 return doc;
337}
338
340{
341 KisDocument *document = KisPart::instance()->createDocument();
342 document->setFileBatchMode(this->batchmode());
343 if (!document->openPath(filename, KisDocument::DontAddToRecent)) {
344 delete document;
345 return 0;
346 }
347 KisPart::instance()->addDocument(document);
348 document->setFileBatchMode(false);
349 return new Document(document, true);
350}
351
353{
354 KisMainWindow *mw = KisPart::instance()->createMainWindow();
355 return new Window(mw);
356}
357
359{
360 d->extensions.append(extension);
361}
362
364{
365 return d->extensions;
366}
367
368void Krita::writeSetting(const QString &group, const QString &name, const QString &value)
369{
371 grp.writeEntry(name, value);
372}
373
374QString Krita::readSetting(const QString &group, const QString &name, const QString &defaultValue)
375{
377 return grp.readEntry(name, defaultValue);
378}
379
380QIcon Krita::icon(QString &iconName) const
381{
382 return KisIconUtils::loadIcon(iconName);
383}
384
386{
387 KoDockRegistry::instance()->add(factory);
388}
389
391{
392 if (!s_instance)
393 {
394 s_instance = new Krita;
395 }
396 return s_instance;
397}
398
399/**
400 * Scripter.fromVariant(variant)
401 * variant is a QVariant
402 * returns instance of QObject-subclass
403 *
404 * This is a helper method for PyQt because PyQt cannot cast a variant to a QObject or QWidget
405 */
407{
408
409 if (v.canConvert< QWidget* >())
410 {
411 QObject* obj = qvariant_cast< QWidget* >(v);
412 return obj;
413 }
414 else if (v.canConvert< QObject* >())
415 {
416 QObject* obj = qvariant_cast< QObject* >(v);
417 return obj;
418 }
419 else
420 return 0;
421}
422
423QString Krita::krita_i18n(const QString &text)
424{
425 return i18n(text.toUtf8().constData());
426}
427
428QString Krita::krita_i18nc(const QString &context, const QString &text)
429{
430 return i18nc(context.toUtf8().constData(), text.toUtf8().constData());
431}
432
433QString Krita::getAppDataLocation()
434{
435 return KoResourcePaths::getAppDataLocation();
436}
437
438void Krita::mainWindowIsBeingCreated(KisMainWindow *kisWindow)
439{
440 Q_FOREACH(Extension *extension, d->extensions) {
441 Window window(kisWindow);
442 extension->createActions(&window);
443 }
444}
445
446#include "moc_Krita.cpp"
The DockWidgetFactoryBase class is the base class for plugins that want to add a dock widget to every...
The Document class encapsulates a Krita Document/Image.
Definition Document.h:37
An Extension is the base for classes that extend Krita.
Definition Extension.h:50
InfoObject wrap a properties map.
Definition InfoObject.h:20
KConfigGroup group(const QString &group)
void writeEntry(const char *key, const char *value, WriteConfigFlags pFlags=Normal)
QString readEntry(const char *key, const char *aDefault=nullptr) const
QStringList keyList() const
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
Krita is a singleton class that offers the root access to the Krita object hierarchy.
Definition Krita.h:28
void writeSetting(const QString &group, const QString &name, const QString &value)
writeSetting write the given setting under the given name to the kritarc file in the given settings g...
Definition Krita.cpp:368
QString version() const
version Determine the version of Krita
Definition Krita.cpp:219
Notifier * notifier() const
notifier the Notifier singleton emits signals when documents are opened and closed,...
Definition Krita.cpp:214
QIcon icon(QString &iconName) const
icon This allows you to get icons from Krita's internal icons.
Definition Krita.cpp:380
QList< QAction * > actions() const
Definition Krita.cpp:83
void addDockWidgetFactory(DockWidgetFactoryBase *factory)
addDockWidgetFactory Add the given docker factory to the application.
Definition Krita.cpp:385
Window * openWindow()
openWindow create a new main window.
Definition Krita.cpp:352
Document * activeDocument() const
Definition Krita.cpp:104
static QObject * fromVariant(const QVariant &v)
Scripter.fromVariant(variant) variant is a QVariant returns instance of QObject-subclass.
Definition Krita.cpp:406
QList< Window * > windows() const
Definition Krita.cpp:242
QStringList profiles(const QString &colorModel, const QString &colorDepth) const
profiles creates a list with the names of all color profiles compatible with the given color model an...
Definition Krita.cpp:194
QList< QDockWidget * > dockers() const
Definition Krita.cpp:296
QStringList recentDocuments() const
return all recent documents registered in the RecentFiles group of the kritarc
Definition Krita.cpp:305
void setBatchmode(bool value)
setBatchmode sets the batchmode to
Definition Krita.cpp:134
QStringList colorDepths(const QString &colorModel) const
colorDepths creates a list with the names of all color depths compatible with the given color model.
Definition Krita.cpp:179
QStringList filterStrategies() const
filterStrategies Retrieves all installed filter strategies.
Definition Krita.cpp:189
QList< View * > views() const
Definition Krita.cpp:224
Document * openDocument(const QString &filename)
openDocument creates a new Document, registers it with the Krita application and loads the given file...
Definition Krita.cpp:339
Filter * filter(const QString &name) const
filter construct a Filter object with a default configuration.
Definition Krita.cpp:156
QMap< QString, Resource * > resources(QString &type) const
resources returns a list of Resource objects of the given type
Definition Krita.cpp:251
static Krita * instance()
instance retrieve the singleton instance of the Application object.
Definition Krita.cpp:390
void setActiveDocument(Document *value)
setActiveDocument activates the first view that shows the given document
Definition Krita.cpp:119
QList< Document * > documents() const
Definition Krita.cpp:140
QAction * action(const QString &name) const
Definition Krita.cpp:93
QString readSetting(const QString &group, const QString &name, const QString &defaultValue)
readSetting read the given setting value from the kritarc file.
Definition Krita.cpp:374
Document * createDocument(int width, int height, const QString &name, const QString &colorModel, const QString &colorDepth, const QString &profile, double resolution)
Definition Krita.cpp:317
Window * activeWindow() const
Definition Krita.cpp:233
QList< Extension * > extensions()
return a list with all registered extension objects.
Definition Krita.cpp:363
QStringList colorModels() const
colorModels creates a list with all color models id's registered.
Definition Krita.cpp:169
QStringList filters() const
Filters are identified by an internal name.
Definition Krita.cpp:149
bool batchmode() const
batchmode determines whether the script is run in batch mode.
Definition Krita.cpp:129
void addExtension(Extension *extension)
addExtension add the given plugin to Krita.
Definition Krita.cpp:358
bool addProfile(const QString &profilePath)
addProfile load the given profile into the profile registry.
Definition Krita.cpp:207
The Notifier can be used to be informed of state changes in the Krita application.
Definition Notifier.h:23
A Resource represents a gradient, pattern, brush tip, brush preset, palette or workspace definition.
Definition Resource.h:31
View represents one view on a document.
Definition View.h:25
Window represents one Krita mainwindow.
Definition Window.h:23
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
QWidget * window(QObject *job)
const char * constData() const const
iterator begin()
iterator end()
QVariant data(int role) const const
QObject(QObject *parent)
void setObjectName(QAnyStringView name)
T * data() const const
iterator begin()
iterator end()
QChar * data()
QByteArray toUtf8() const const
QStringList filter(QStringView str, Qt::CaseSensitivity cs) const const
void sort(Qt::CaseSensitivity cs)
UserRole
Window
Definition Document.h:1055
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
bool canConvert() const const
int toInt(bool *ok) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Jan 24 2025 11:51:04 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.