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);
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#if QT_VERSION >= QT_VERSION_CHECK(5,14,0)
177 return QStringList(colorModelsIds.begin(), colorModelsIds.end());
178#else
179 return QStringList::fromSet(colorModelsIds);
180#endif
181}
182
183QStringList Krita::colorDepths(const QString &colorModel) const
184{
185 QSet<QString> colorDepthsIds;
186 QList<KoID> ids = KoColorSpaceRegistry::instance()->colorDepthList(colorModel, KoColorSpaceRegistry::AllColorSpaces);
187 Q_FOREACH(KoID id, ids) {
188 colorDepthsIds << id.id();
189 }
190#if QT_VERSION >= QT_VERSION_CHECK(5,14,0)
191 return QStringList(colorDepthsIds.begin(), colorDepthsIds.end());
192#else
193 return QStringList::fromSet(colorDepthsIds);
194#endif
195}
196
198{
199 return KisFilterStrategyRegistry::instance()->keys();
200}
201
202QStringList Krita::profiles(const QString &colorModel, const QString &colorDepth) const
203{
204 QSet<QString> profileNames;
205 QString id = KoColorSpaceRegistry::instance()->colorSpaceId(colorModel, colorDepth);
206 QList<const KoColorProfile *> profiles = KoColorSpaceRegistry::instance()->profilesFor(id);
207 Q_FOREACH(const KoColorProfile *profile, profiles) {
208 profileNames << profile->name();
209 }
210#if QT_VERSION >= QT_VERSION_CHECK(5,14,0)
211 QStringList r(profileNames.begin(), profileNames.end());
212#else
213 QStringList r = QStringList::fromSet(profileNames);
214#endif
215 r.sort();
216 return r;
217}
218
219bool Krita::addProfile(const QString &profilePath)
220{
221 KoColorSpaceEngine *iccEngine = KoColorSpaceEngineRegistry::instance()->get("icc");
222 KIS_ASSERT(iccEngine);
223 return iccEngine->addProfile(profilePath);
224}
225
227{
228 return d->notifier;
229}
230
232{
233 return KritaVersionWrapper::versionString(true);
234}
235
237{
238 QList<View *> ret;
239 foreach(QPointer<KisView> view, KisPart::instance()->views()) {
240 ret << new View(view);
241 }
242 return ret;
243}
244
246{
247 KisMainWindow *mainWindow = KisPart::instance()->currentMainwindow();
248 if (!mainWindow) {
249 return 0;
250 }
251 return new Window(mainWindow);
252}
253
255{
256 QList<Window*> ret;
257 foreach(QPointer<KisMainWindow> mainWin, KisPart::instance()->mainWindows()) {
258 ret << new Window(mainWin);
259 }
260 return ret;
261}
262
264{
266 KisResourceModel *resourceModel = 0;
267 if (type == "pattern") {
268 resourceModel = KoResourceServerProvider::instance()->patternServer()->resourceModel();
269 type = ResourceType::Patterns;
270 }
271 else if (type == "gradient") {
272 type = ResourceType::Gradients;
273 resourceModel = KoResourceServerProvider::instance()->gradientServer()->resourceModel();
274 }
275 else if (type == "brush") {
276 resourceModel = KisBrushServerProvider::instance()->brushServer()->resourceModel();
277 type = ResourceType::Brushes;
278 }
279 else if (type == "palette") {
280 resourceModel = KoResourceServerProvider::instance()->paletteServer()->resourceModel();
281 type = ResourceType::Palettes;
282 }
283 else if (type == "workspace") {
284 resourceModel = KisResourceServerProvider::instance()->workspaceServer()->resourceModel();
285 type = ResourceType::Workspaces;
286 }
287 else if (type == "preset") {
288 resourceModel = KisResourceServerProvider::instance()->paintOpPresetServer()->resourceModel();
289 }
290
291 if (resourceModel) {
292 for (int i = 0; i < resourceModel->rowCount(); ++i) {
293
294 QModelIndex idx = resourceModel->index(i, 0);
295 int id = resourceModel->data(idx, Qt::UserRole + KisAbstractResourceModel::Id).toInt();
296 QString name = resourceModel->data(idx, Qt::UserRole + KisAbstractResourceModel::Name).toString();
297 QString filename = resourceModel->data(idx, Qt::UserRole + KisAbstractResourceModel::Filename).toString();
298 QImage image = resourceModel->data(idx, Qt::UserRole + KisAbstractResourceModel::Thumbnail).value<QImage>();
299
300 resources[name] = new Resource(id, type, name, filename, image, 0);
301 }
302 }
303
304 return resources;
305}
306
307
309{
310 KisMainWindow *mainWindow = KisPart::instance()->currentMainwindow();
311
312 if (!mainWindow) return {};
313 return mainWindow->dockWidgets();
314}
315
316
318{
319 KConfigGroup grp = KSharedConfig::openConfig()->group(QString("RecentFiles"));
320 QStringList keys = grp.keyList();
322
323 for(int i = 0; i <= keys.filter("File").count(); i++)
324 recentDocuments << grp.readEntry(QString("File%1").arg(i), QString(""));
325
326 return recentDocuments;
327}
328
329Document* Krita::createDocument(int width, int height, const QString &name, const QString &colorModel, const QString &colorDepth, const QString &profile, double resolution)
330{
331 KisDocument *document = KisPart::instance()->createDocument();
332 document->setObjectName(name);
333
334 KisPart::instance()->addDocument(document, false);
335 const KoColorSpace *cs = KoColorSpaceRegistry::instance()->colorSpace(colorModel, colorDepth, profile);
336 Q_ASSERT(cs);
337
338 QColor qc(Qt::white);
339 KoColor bgColor(qc, cs);
340
341 if (!document->newImage(name, width, height, cs, bgColor, KisConfig::RASTER_LAYER, 1, "", double(resolution / 72) )) {
342 return 0;
343 }
344
345 Q_ASSERT(document->image());
346 Document *doc = new Document(document, true);
347
348 return doc;
349}
350
352{
353 KisDocument *document = KisPart::instance()->createDocument();
354 document->setFileBatchMode(this->batchmode());
355 if (!document->openPath(filename, KisDocument::DontAddToRecent)) {
356 delete document;
357 return 0;
358 }
359 KisPart::instance()->addDocument(document);
360 document->setFileBatchMode(false);
361 return new Document(document, true);
362}
363
365{
366 KisMainWindow *mw = KisPart::instance()->createMainWindow();
367 return new Window(mw);
368}
369
371{
372 d->extensions.append(extension);
373}
374
376{
377 return d->extensions;
378}
379
380void Krita::writeSetting(const QString &group, const QString &name, const QString &value)
381{
383 grp.writeEntry(name, value);
384}
385
386QString Krita::readSetting(const QString &group, const QString &name, const QString &defaultValue)
387{
389 return grp.readEntry(name, defaultValue);
390}
391
392QIcon Krita::icon(QString &iconName) const
393{
394 return KisIconUtils::loadIcon(iconName);
395}
396
398{
399 KoDockRegistry::instance()->add(factory);
400}
401
403{
404 if (!s_instance)
405 {
406 s_instance = new Krita;
407 }
408 return s_instance;
409}
410
411/**
412 * Scripter.fromVariant(variant)
413 * variant is a QVariant
414 * returns instance of QObject-subclass
415 *
416 * This is a helper method for PyQt because PyQt cannot cast a variant to a QObject or QWidget
417 */
419{
420
421 if (v.canConvert< QWidget* >())
422 {
423 QObject* obj = qvariant_cast< QWidget* >(v);
424 return obj;
425 }
426 else if (v.canConvert< QObject* >())
427 {
428 QObject* obj = qvariant_cast< QObject* >(v);
429 return obj;
430 }
431 else
432 return 0;
433}
434
435QString Krita::krita_i18n(const QString &text)
436{
437 return i18n(text.toUtf8().constData());
438}
439
440QString Krita::krita_i18nc(const QString &context, const QString &text)
441{
442 return i18nc(context.toUtf8().constData(), text.toUtf8().constData());
443}
444
445QString Krita::getAppDataLocation()
446{
447 return KoResourcePaths::getAppDataLocation();
448}
449
450void Krita::mainWindowIsBeingCreated(KisMainWindow *kisWindow)
451{
452 Q_FOREACH(Extension *extension, d->extensions) {
453 Window window(kisWindow);
454 extension->createActions(&window);
455 }
456}
457
458#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
Filter: represents a filter and its configuration.
Definition Filter.h:31
void setConfiguration(InfoObject *value)
setConfiguration set the configuration object for the filter
Definition Filter.cpp:80
void setName(const QString &name)
setName set the filter's name to the given name.
Definition Filter.cpp:66
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:380
QString version() const
version Determine the version of Krita
Definition Krita.cpp:231
Notifier * notifier() const
notifier the Notifier singleton emits signals when documents are opened and closed,...
Definition Krita.cpp:226
QIcon icon(QString &iconName) const
icon This allows you to get icons from Krita's internal icons.
Definition Krita.cpp:392
QList< QAction * > actions() const
Definition Krita.cpp:83
void addDockWidgetFactory(DockWidgetFactoryBase *factory)
addDockWidgetFactory Add the given docker factory to the application.
Definition Krita.cpp:397
Window * openWindow()
openWindow create a new main window.
Definition Krita.cpp:364
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:418
QList< Window * > windows() const
Definition Krita.cpp:254
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:202
QList< QDockWidget * > dockers() const
Definition Krita.cpp:308
QStringList recentDocuments() const
return all recent documents registered in the RecentFiles group of the kritarc
Definition Krita.cpp:317
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:183
QStringList filterStrategies() const
filterStrategies Retrieves all installed filter strategies.
Definition Krita.cpp:197
QList< View * > views() const
Definition Krita.cpp:236
Document * openDocument(const QString &filename)
openDocument creates a new Document, registers it with the Krita application and loads the given file...
Definition Krita.cpp:351
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:263
static Krita * instance()
instance retrieve the singleton instance of the Application object.
Definition Krita.cpp:402
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:386
Document * createDocument(int width, int height, const QString &name, const QString &colorModel, const QString &colorDepth, const QString &profile, double resolution)
Definition Krita.cpp:329
Window * activeWindow() const
Definition Krita.cpp:245
QList< Extension * > extensions()
return a list with all registered extension objects.
Definition Krita.cpp:375
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:370
bool addProfile(const QString &profilePath)
addProfile load the given profile into the profile registry.
Definition Krita.cpp:219
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...)
KGUIADDONS_EXPORT QWindow * window(QObject *job)
const char * constData() const const
void append(QList< T > &&value)
iterator begin()
iterator end()
QVariant data(int role) const const
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
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-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:02:09 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.