Krita

Krita.cpp
1 /*
2  * SPDX-FileCopyrightText: 2016 Boudewijn Rempt <[email protected]>
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 
59 Krita* Krita::s_instance = 0;
60 
61 struct Krita::Private {
62  Private() {}
64  bool batchMode {false};
65  Notifier *notifier{new Notifier()};
66 };
67 
68 Krita::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 
76 Krita::~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 
93 QAction *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 
129 bool Krita::batchmode() const
130 {
131  return d->batchMode;
132 }
133 
134 void Krita::setBatchmode(bool value)
135 {
136  d->batchMode = value;
137 }
138 
139 
141 {
142  QList<Document *> ret;
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 
156 Filter *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 #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 
183 QStringList 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 
202 QStringList 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 
219 bool 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 
329 Document* 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  qc.setAlpha(0);
340  KoColor bgColor(qc, cs);
341 
342  if (!document->newImage(name, width, height, cs, bgColor, KisConfig::RASTER_LAYER, 1, "", double(resolution / 72) )) {
343  return 0;
344  }
345 
346  Q_ASSERT(document->image());
347  Document *doc = new Document(document, true);
348 
349  return doc;
350 }
351 
353 {
354  KisDocument *document = KisPart::instance()->createDocument();
355  document->setFileBatchMode(this->batchmode());
356  if (!document->openPath(filename, KisDocument::DontAddToRecent)) {
357  delete document;
358  return 0;
359  }
360  KisPart::instance()->addDocument(document);
361  document->setFileBatchMode(false);
362  return new Document(document, true);
363 }
364 
366 {
367  KisMainWindow *mw = KisPart::instance()->createMainWindow();
368  return new Window(mw);
369 }
370 
372 {
373  d->extensions.append(extension);
374 }
375 
377 {
378  return d->extensions;
379 }
380 
381 void Krita::writeSetting(const QString &group, const QString &name, const QString &value)
382 {
383  KConfigGroup grp = KSharedConfig::openConfig()->group(group);
384  grp.writeEntry(name, value);
385 }
386 
387 QString Krita::readSetting(const QString &group, const QString &name, const QString &defaultValue)
388 {
389  KConfigGroup grp = KSharedConfig::openConfig()->group(group);
390  return grp.readEntry(name, defaultValue);
391 }
392 
393 QIcon Krita::icon(QString &iconName) const
394 {
395  return KisIconUtils::loadIcon(iconName);
396 }
397 
399 {
400  KoDockRegistry::instance()->add(factory);
401 }
402 
404 {
405  if (!s_instance)
406  {
407  s_instance = new Krita;
408  }
409  return s_instance;
410 }
411 
412 /**
413  * Scripter.fromVariant(variant)
414  * variant is a QVariant
415  * returns instance of QObject-subclass
416  *
417  * This is a helper method for PyQt because PyQt cannot cast a variant to a QObject or QWidget
418  */
420 {
421 
422  if (v.canConvert< QWidget* >())
423  {
424  QObject* obj = qvariant_cast< QWidget* >(v);
425  return obj;
426  }
427  else if (v.canConvert< QObject* >())
428  {
429  QObject* obj = qvariant_cast< QObject* >(v);
430  return obj;
431  }
432  else
433  return 0;
434 }
435 
436 QString Krita::krita_i18n(const QString &text)
437 {
438  return i18n(text.toUtf8().constData());
439 }
440 
441 QString Krita::krita_i18nc(const QString &context, const QString &text)
442 {
443  return i18nc(context.toUtf8().constData(), text.toUtf8().constData());
444 }
445 
446 QString Krita::getAppDataLocation()
447 {
448  return KoResourcePaths::getAppDataLocation();
449 }
450 
451 void Krita::mainWindowIsBeingCreated(KisMainWindow *kisWindow)
452 {
453  Q_FOREACH(Extension *extension, d->extensions) {
454  Window window(kisWindow);
455  extension->createActions(&window);
456  }
457 }
458 
459 #include "moc_Krita.cpp"
QString readEntry(const char *key, const char *aDefault=nullptr) const
bool addProfile(const QString &profilePath)
addProfile load the given profile into the profile registry.
Definition: Krita.cpp:219
void writeEntry(const char *key, const char *value, WriteConfigFlags pFlags=Normal)
QList< View * > views() const
Definition: Krita.cpp:236
Window * openWindow()
openWindow create a new main window.
Definition: Krita.cpp:365
UserRole
InfoObject wrap a properties map.
Definition: InfoObject.h:19
A Resource represents a gradient, pattern, brush tip, brush preset, palette or workspace definition.
Definition: Resource.h:30
Filter * filter(const QString &name) const
filter construct a Filter object with a default configuration.
Definition: Krita.cpp:156
Window represents one Krita mainwindow.
Definition: Window.h:22
Document * openDocument(const QString &filename)
openDocument creates a new Document, registers it with the Krita application and loads the given file...
Definition: Krita.cpp:352
QMap< QString, Resource * > resources(QString &type) const
resources returns a list of Resource objects of the given type
Definition: Krita.cpp:263
The Document class encapsulates a Krita Document/Image.
Definition: Document.h:33
QList< Document * > documents() const
Definition: Krita.cpp:140
View represents one view on a document.
Definition: View.h:24
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
QVariant data(int role) const const
QStringList filter(QStringView str, Qt::CaseSensitivity cs) const const
QString i18n(const char *text, const TYPE &arg...)
QStringList colorModels() const
colorModels creates a list with all color models id's registered.
Definition: Krita.cpp:169
void setBatchmode(bool value)
setBatchmode sets the batchmode to
Definition: Krita.cpp:134
Window * activeWindow() const
Definition: Krita.cpp:245
Filter: represents a filter and its configuration.
Definition: Filter.h:30
static Krita * instance()
instance retrieve the singleton instance of the Application object.
Definition: Krita.cpp:403
Krita is a singleton class that offers the root access to the Krita object hierarchy.
Definition: Krita.h:27
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:381
QByteArray toUtf8() const const
void setAlpha(int alpha)
int toInt(bool *ok) const const
The Notifier can be used to be informed of state changes in the Krita application.
Definition: Notifier.h:22
void addExtension(Extension *extension)
addExtension add the given plugin to Krita.
Definition: Krita.cpp:371
QSet::iterator begin()
An Extension is the base for classes that extend Krita.
Definition: Extension.h:49
QStringList filters() const
Filters are identified by an internal name.
Definition: Krita.cpp:149
Notifier * notifier() const
notifier the Notifier singleton emits signals when documents are opened and closed,...
Definition: Krita.cpp:226
QList< Extension * > extensions()
return a list with all registered extension objects.
Definition: Krita.cpp:376
Document * createDocument(int width, int height, const QString &name, const QString &colorModel, const QString &colorDepth, const QString &profile, double resolution)
Definition: Krita.cpp:329
bool canConvert(int targetTypeId) const const
QList< Window * > windows() const
Definition: Krita.cpp:254
Document * activeDocument() const
Definition: Krita.cpp:104
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:387
QStringList filterStrategies() const
filterStrategies Retrieves all installed filter strategies.
Definition: Krita.cpp:197
QStringList keyList() const
const char * constData() const const
QList< QDockWidget * > dockers() const
Definition: Krita.cpp:308
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
void setObjectName(const QString &name)
QIcon icon(QString &iconName) const
icon This allows you to get icons from Krita's internal icons.
Definition: Krita.cpp:393
QSet::iterator end()
bool batchmode() const
batchmode determines whether the script is run in batch mode.
Definition: Krita.cpp:129
The DockWidgetFactoryBase class is the base class for plugins that want to add a dock widget to every...
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString version() const
version Determine the version of Krita
Definition: Krita.cpp:231
QList::iterator begin()
QChar * data()
QStringList recentDocuments() const
return all recent documents registered in the RecentFiles group of the kritarc
Definition: Krita.cpp:317
QList< T > fromSet(const QSet< T > &set)
QList< QAction * > actions() const
Definition: Krita.cpp:83
T * data() const const
KJOBWIDGETS_EXPORT QWidget * window(KJob *job)
QList::iterator end()
void setActiveDocument(Document *value)
setActiveDocument activates the first view that shows the given document
Definition: Krita.cpp:119
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
void addDockWidgetFactory(DockWidgetFactoryBase *factory)
addDockWidgetFactory Add the given docker factory to the application.
Definition: Krita.cpp:398
void sort(Qt::CaseSensitivity cs)
static QObject * fromVariant(const QVariant &v)
Scripter.fromVariant(variant) variant is a QVariant returns instance of QObject-subclass.
Definition: Krita.cpp:419
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Tue Aug 16 2022 03:57:18 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.