Krita Source Code Documentation
Loading...
Searching...
No Matches
KisInputProfileManager Class Reference

A class to manage a list of profiles and actions. More...

#include <kis_input_profile_manager.h>

+ Inheritance diagram for KisInputProfileManager:

Signals

void currentProfileChanged ()
 
void profilesChanged ()
 

Public Member Functions

QList< KisAbstractInputAction * > actions ()
 
KisInputProfileaddProfile (const QString &name)
 
void createActions ()
 
KisInputProfilecurrentProfile () const
 
void duplicateProfile (const QString &name, const QString &newName)
 
QList< KisShortcutConfiguration * > getConflictingShortcuts (KisInputProfile *profile)
 
 KisInputProfileManager (QObject *parent=0)
 
void loadProfiles ()
 
 Private ()
 
KisInputProfileprofile (const QString &name) const
 
QString profileFileName (const QString &profileName)
 
QStringList profileNames () const
 
QList< KisInputProfile * > profiles () const
 
void removeProfile (const QString &name)
 
bool renameProfile (const QString &oldName, const QString &newName)
 
void resetAll ()
 
void saveProfile (KisInputProfile *profile, QString storagePath)
 
void saveProfiles ()
 
void setCurrentProfile (KisInputProfile *profile)
 
 ~KisInputProfileManager () override
 

Static Public Member Functions

static KisInputProfileManagerinstance ()
 

Public Attributes

QList< KisAbstractInputAction * > actions
 
KisInputProfilecurrentProfile
 
QMap< QString, KisInputProfile * > profiles
 

Private Attributes

Private *const d
 
- Private Attributes inherited from Private
KisCanvas2canvas
 
int displayedFrame
 
int intendedFrame
 

Additional Inherited Members

- Private Member Functions inherited from Private
 Private (KisCanvas2 *c)
 

Detailed Description

A class to manage a list of profiles and actions.

Definition at line 39 of file kis_input_profile_manager.cpp.

Constructor & Destructor Documentation

◆ KisInputProfileManager()

KisInputProfileManager::KisInputProfileManager ( QObject * parent = 0)

Definition at line 423 of file kis_input_profile_manager.cpp.

424 : QObject(parent), d(new Private())
425{
426 d->createActions();
427}

References d.

◆ ~KisInputProfileManager()

KisInputProfileManager::~KisInputProfileManager ( )
override

Definition at line 429 of file kis_input_profile_manager.cpp.

430{
431 qDeleteAll(d->profiles);
432 qDeleteAll(d->actions);
433 delete d;
434}

References d.

Member Function Documentation

◆ actions()

QList< KisAbstractInputAction * > KisInputProfileManager::actions ( )
Returns
The list of all available actions.

◆ addProfile()

KisInputProfile * KisInputProfileManager::addProfile ( const QString & name)

Add a profile.

Parameters
nameThe name of the new profile.
Returns
The new, empty profile or the non-empty profile if it already exists.

Definition at line 93 of file kis_input_profile_manager.cpp.

94{
95 if (d->profiles.contains(name)) {
96 return d->profiles.value(name);
97 }
98
100 profile->setName(name);
101 d->profiles.insert(name, profile);
102
103 Q_EMIT profilesChanged();
104
105 return profile;
106}
KisInputProfile * profile(const QString &name) const
A container class for sets of shortcuts associated with an action.
void setName(const QString &name)

References d, profile(), profilesChanged(), and KisInputProfile::setName().

◆ createActions()

void KisInputProfileManager::createActions ( )

◆ currentProfile()

KisInputProfile * KisInputProfileManager::currentProfile ( ) const
Returns
The current active profile.

◆ currentProfileChanged

void KisInputProfileManager::currentProfileChanged ( )
signal

Emitted when the current active profile changes.

◆ duplicateProfile()

void KisInputProfileManager::duplicateProfile ( const QString & name,
const QString & newName )

Duplicate a profile.

This creates a new profile with the given name and copies all data from the old profile to the new profile.

Parameters
nameThe name of the profile to duplicate.
newNameThe name of the new profile.

Definition at line 153 of file kis_input_profile_manager.cpp.

154{
155 if (!d->profiles.contains(name) || d->profiles.contains(newName)) {
156 return;
157 }
158
159 KisInputProfile *newProfile = new KisInputProfile(this);
160 newProfile->setName(newName);
161 d->profiles.insert(newName, newProfile);
162
163 KisInputProfile *profile = d->profiles.value(name);
165 Q_FOREACH(KisShortcutConfiguration * shortcut, shortcuts) {
166 newProfile->addShortcut(new KisShortcutConfiguration(*shortcut));
167 }
168
169 Q_EMIT profilesChanged();
170}
QList< KisShortcutConfiguration * > allShortcuts() const
void addShortcut(KisShortcutConfiguration *shortcut)
A class encapsulating all settings for a single shortcut.

References KisInputProfile::addShortcut(), KisInputProfile::allShortcuts(), d, profile(), profilesChanged(), and KisInputProfile::setName().

◆ getConflictingShortcuts()

QList< KisShortcutConfiguration * > KisInputProfileManager::getConflictingShortcuts ( KisInputProfile * profile)

Return the configurations that have conflicts.

Definition at line 386 of file kis_input_profile_manager.cpp.

387{
388 QSet<KisShortcutConfiguration *> conflictedShortcuts;
390 for (auto startIt = shortcuts.constBegin(); startIt != shortcuts.constEnd(); ++startIt) {
391 KisShortcutConfiguration *first = *startIt;
392 for (auto index = startIt + 1; index != shortcuts.constEnd(); ++index) {
393 KisShortcutConfiguration *second = *index;
394 // since there can be multiple no-ops in the config and because no-ops are something a user can't
395 // perform, there should be no conflicts.
396 if (*first == *second && !first->isNoOp()) {
397 conflictedShortcuts.insert(first);
398 conflictedShortcuts.insert(second);
399 }
400 }
401 }
402 return conflictedShortcuts.values();
403}

References KisInputProfile::allShortcuts(), KisShortcutConfiguration::isNoOp(), and profile().

◆ instance()

KisInputProfileManager * KisInputProfileManager::instance ( )
static
Returns
The singleton instance of this class.

Definition at line 56 of file kis_input_profile_manager.cpp.

57{
58 return inputProfileManager;
59}

◆ loadProfiles()

void KisInputProfileManager::loadProfiles ( )

Load all profiles from the configuration stored on disk.

Definition at line 177 of file kis_input_profile_manager.cpp.

178{
179 //Remove any profiles that already exist
180 d->currentProfile = nullptr;
181 qDeleteAll(d->profiles);
182 d->profiles.clear();
183
184 //Look up all profiles (this includes those installed to $prefix as well as the user's local data dir)
186
187 dbgKrita << "profiles" << profiles;
188
189 // We don't use list here, because we're assuming we are only going to be changing the user directory and
190 // there can only be one of a profile name.
191 QMap<QString, ProfileEntry> profileEntriesToMigrate;
192 QMap<QString, QList<ProfileEntry>> profileEntries;
193
194 KisConfig cfg(true);
195
196 // Get only valid entries...
197 Q_FOREACH(const QString & p, profiles) {
198
199 ProfileEntry entry;
200 entry.fullpath = p;
201
202 KConfig config(p, KConfig::SimpleConfig);
203 if (!config.hasGroup("General") || !config.group("General").hasKey("name") || !config.group("General").hasKey("version")) {
204 //Skip if we don't have the proper settings.
205 continue;
206 }
207
208 // Only entries of exactly the right version can be considered
209 entry.version = config.group("General").readEntry("version", 0);
210 entry.name = config.group("General").readEntry("name");
211
212 // NOTE: Migrating profiles doesn't just mean porting them to new version. Migrating a profile
213 // may override the existing newer profile file.
214 if (entry.version == PROFILE_VERSION - 1) {
215 // we only utilize the first entry, because it is the most local one and the one which has to be
216 // migrated.
217 profileEntriesToMigrate[entry.name] = entry;
218
219 } else if (entry.version == PROFILE_VERSION) {
220 if (!profileEntries.contains(entry.name)) {
221 profileEntries[entry.name] = QList<ProfileEntry>();
222 }
223
224 // let all the current version entries pile up in the list, it is only later where we check if it
225 // is something we will use or a migrated entry.
226 profileEntries[entry.name].append(entry);
227 }
228 }
229
230 {
231 const QString userLocalSaveLocation = KoResourcePaths::saveLocation("data", "input/");
232 auto entriesIt = profileEntriesToMigrate.begin();
233 while (entriesIt != profileEntriesToMigrate.end()) {
234 ProfileEntry entry = *entriesIt;
235 // if entry doesn't exist in profileEntries, means there is no corresponding new version of the
236 // entry in user directory. Meaning, it is a certain candidate for migration.
237
238 if (profileEntries.contains(entry.name)) {
239
240 // we only need first() because if a user-local entry exists, it will be the first.
241 ProfileEntry existingEntry = profileEntries[entry.name].first();
242
243 // check if the entry's fullpath is a saveLocation, if so, we remove it from migration list.
244 if (existingEntry.fullpath.startsWith(userLocalSaveLocation)) {
245 entriesIt = profileEntriesToMigrate.erase(entriesIt);
246 } else {
247 // if the entry's fullpath is not a saveLocation, we will migrate it. Because (user's
248 // previous configuration + current default touch shortcuts) are better than. (All default
249 // shortcuts).
250 entriesIt++;
251
252 // Because this entry is supposed to be migrated, it will clash with an already existing
253 // default entry. So remove it.
254 profileEntries.remove(existingEntry.name);
255 }
256 } else {
257 entriesIt++;
258 }
259 }
260 }
261
262 {
263 KisInputProfileMigrator5To6 migrator(this);
264 QMap<ProfileEntry, QList<KisShortcutConfiguration>> parsedProfilesToMigrate =
265 migrator.migrate(profileEntriesToMigrate);
266
267 for (ProfileEntry profileEntry : parsedProfilesToMigrate.keys()) {
268 const QString storagePath = KoResourcePaths::saveLocation("data", "input/", true);
269
270 {
271 // the profile we have here uses the previous config, the only thing we need to make sure is
272 // it doesn't overwrite the existing profile to preserve backwards compatibility.
273 const QString profilePath = profileEntry.fullpath;
274 QString oldProfileName = QFileInfo(profilePath).fileName();
275 oldProfileName.replace(".profile", QString::number(PROFILE_VERSION - 1) + ".profile");
276
277 QString oldProfilePath = storagePath + oldProfileName;
278 // copy the profile to a new file but add version number to the name
279 QFile::copy(profilePath, oldProfilePath);
280
281 KConfig config(oldProfilePath, KConfig::SimpleConfig);
282 config.group("General").writeEntry("migrated", PROFILE_VERSION);
283 }
284
285 KisInputProfile *newProfile = addProfile(profileEntry.name);
286 QList<KisShortcutConfiguration> shortcuts = parsedProfilesToMigrate.value(profileEntry);
287 for (const auto &shortcut : shortcuts) {
288 newProfile->addShortcut(new KisShortcutConfiguration(shortcut));
289 }
290
291 // save the new profile with migrated shortcuts. We overwrite the previous version of file (which
292 // previously has been moved for backward compatibility).
293 saveProfile(newProfile, storagePath);
294 }
295 }
296
297 Q_FOREACH(const QString & profileName, profileEntries.keys()) {
298
299 if (profileEntries[profileName].isEmpty()) {
300 continue;
301 }
302
303 // we have one or more entries for this profile name. We'll take the first,
304 // because that's the most local one.
305 ProfileEntry entry = profileEntries[profileName].first();
306
307 KConfig config(entry.fullpath, KConfig::SimpleConfig);
308
309 KisInputProfile *newProfile = addProfile(entry.name);
310 Q_FOREACH(KisAbstractInputAction * action, d->actions) {
311 if (!config.hasGroup(action->id())) {
312 continue;
313 }
314
315 KConfigGroup grp = config.group(action->id());
316 //Read the settings for the action and create the appropriate shortcuts.
317 Q_FOREACH(const QString & entry, grp.entryMap()) {
319 shortcut->setAction(action);
320
321 if (shortcut->unserialize(entry)) {
322 newProfile->addShortcut(shortcut);
323 }
324 else {
325 delete shortcut;
326 }
327 }
328 }
329 }
330
331 QString currentProfile = cfg.currentInputProfile();
332 if (d->profiles.size() > 0) {
333 if (currentProfile.isEmpty() || !d->profiles.contains(currentProfile)) {
334 QString kritaDefault = QStringLiteral("Krita Default");
335 if (d->profiles.contains(kritaDefault)) {
336 d->currentProfile = d->profiles.value(kritaDefault);
337 } else {
338 d->currentProfile = d->profiles.begin().value();
339 }
340 }
341 else {
342 d->currentProfile = d->profiles.value(currentProfile);
343 }
344 }
345 if (d->currentProfile) {
346 Q_EMIT currentProfileChanged();
347 }
348}
const Params2D p
Abstract base class for input actions.
KisInputProfile * addProfile(const QString &name)
void saveProfile(KisInputProfile *profile, QString storagePath)
QMap< QString, KisInputProfile * > profiles
bool unserialize(const QString &serialized)
void setAction(KisAbstractInputAction *newAction)
static QStringList findAllAssets(const QString &type, const QString &filter=QString(), SearchOptions options=NoSearchOptions)
static QString saveLocation(const QString &type, const QString &suffix=QString(), bool create=true)
#define dbgKrita
Definition kis_debug.h:45
#define PROFILE_VERSION

References addProfile(), KisInputProfile::addShortcut(), KisConfig::currentInputProfile(), currentProfile, currentProfileChanged(), d, dbgKrita, KoResourcePaths::findAllAssets(), ProfileEntry::fullpath, KisAbstractInputAction::id, KisInputProfileMigrator5To6::migrate(), ProfileEntry::name, p, PROFILE_VERSION, profiles, KoResourcePaths::Recursive, KoResourcePaths::saveLocation(), saveProfile(), KisShortcutConfiguration::setAction(), KisShortcutConfiguration::unserialize(), and ProfileEntry::version.

◆ Private()

KisInputProfileManager::Private ( )
inline

Definition at line 42 of file kis_input_profile_manager.cpp.

42: currentProfile(0) { }

◆ profile()

KisInputProfile * KisInputProfileManager::profile ( const QString & name) const

Retrieve a profile by name.

Parameters
nameThe name of the profile to retrieve.
Returns
The profile with the given name, or 0 if not found.

Definition at line 71 of file kis_input_profile_manager.cpp.

72{
73 if (d->profiles.contains(name)) {
74 return d->profiles.value(name);
75 }
76
77 return 0;
78}

References d.

◆ profileFileName()

QString KisInputProfileManager::profileFileName ( const QString & profileName)

◆ profileNames()

QStringList KisInputProfileManager::profileNames ( ) const
Returns
A list of the names of all profiles.

Definition at line 66 of file kis_input_profile_manager.cpp.

67{
68 return d->profiles.keys();
69}

References d.

◆ profiles()

QList< KisInputProfile * > KisInputProfileManager::profiles ( ) const
Returns
A list of all profiles.

◆ profilesChanged

void KisInputProfileManager::profilesChanged ( )
signal

Emitted when the list of profiles changes.

◆ removeProfile()

void KisInputProfileManager::removeProfile ( const QString & name)

Remove a profile.

This will remove the given profile from the list of profiles and delete it.

Parameters
nameThe profile to remove.

Definition at line 108 of file kis_input_profile_manager.cpp.

109{
110 if (d->profiles.contains(name)) {
111 QString currentProfileName = d->currentProfile->name();
112
113 delete d->profiles.value(name);
114 d->profiles.remove(name);
115
116 //Delete the settings file for the removed profile, if it exists
117 QDir userDir(KoResourcePaths::saveLocation("data", "input/"));
118
119 if (userDir.exists(d->profileFileName(name))) {
120 userDir.remove(d->profileFileName(name));
121 }
122
123 if (currentProfileName == name) {
124 d->currentProfile = d->profiles.begin().value();
125 Q_EMIT currentProfileChanged();
126 }
127
128 Q_EMIT profilesChanged();
129 }
130}

References currentProfileChanged(), d, profilesChanged(), and KoResourcePaths::saveLocation().

◆ renameProfile()

bool KisInputProfileManager::renameProfile ( const QString & oldName,
const QString & newName )

Rename a profile.

Parameters
oldNameThe current name of the profile.
newNameThe new name of the profile.
Returns
true if successful, false if not.

Definition at line 132 of file kis_input_profile_manager.cpp.

133{
134 if (!d->profiles.contains(oldName)) {
135 return false;
136 }
137
138 KisInputProfile *profile = d->profiles.value(oldName);
139 if (profile) {
140 d->profiles.remove(oldName);
141 profile->setName(newName);
142 d->profiles.insert(newName, profile);
143
144 Q_EMIT profilesChanged();
145
146
147 return true;
148 }
149
150 return false;
151}

References d, profile(), profilesChanged(), and KisInputProfile::setName().

◆ resetAll()

void KisInputProfileManager::resetAll ( )

Reset all profiles to the default state.

This will remove all custom profiles the user created and reset any changed profiles.

Definition at line 405 of file kis_input_profile_manager.cpp.

406{
407 QString kdeHome = KoResourcePaths::getAppDataLocation();
409
410 Q_FOREACH (const QString &profile, profiles) {
411 if(profile.contains(kdeHome)) {
412 //This is a local file, remove it.
413 QFile::remove(profile);
414 }
415 }
416
417 //Load the profiles again, this should now only load those shipped with Krita.
418 loadProfiles();
419
420 Q_EMIT profilesChanged();
421}
static QString getAppDataLocation()

References KoResourcePaths::findAllAssets(), KoResourcePaths::getAppDataLocation(), loadProfiles(), profile(), profiles, profilesChanged(), and KoResourcePaths::Recursive.

◆ saveProfile()

void KisInputProfileManager::saveProfile ( KisInputProfile * profile,
QString storagePath )

Save profile to storagePath

Definition at line 364 of file kis_input_profile_manager.cpp.

365{
366 const QString profilePath = storagePath + d->profileFileName(profile->name());
367 KConfig config(profilePath, KConfig::SimpleConfig);
368
369 config.group("General").writeEntry("name", profile->name());
370 config.group("General").writeEntry("version", PROFILE_VERSION);
371
372 Q_FOREACH(KisAbstractInputAction * action, d->actions) {
373 KConfigGroup grp = config.group(action->id());
374 grp.deleteGroup(); //Clear the group of any existing shortcuts.
375
376 int index = 0;
378 Q_FOREACH(KisShortcutConfiguration * shortcut, shortcuts) {
379 grp.writeEntry(QString("%1").arg(index++), shortcut->serialize());
380 }
381 }
382
383 config.sync();
384}
QList< KisShortcutConfiguration * > shortcutsForAction(KisAbstractInputAction *action) const
QString name() const

References d, KisAbstractInputAction::id, KisInputProfile::name(), profile(), PROFILE_VERSION, KisShortcutConfiguration::serialize(), and KisInputProfile::shortcutsForAction().

◆ saveProfiles()

void KisInputProfileManager::saveProfiles ( )

Save all profiles to configuration on disk.

Definition at line 350 of file kis_input_profile_manager.cpp.

351{
352 QString storagePath = KoResourcePaths::saveLocation("data", "input/", true);
353 Q_FOREACH(KisInputProfile * p, d->profiles) {
354 saveProfile(p, storagePath);
355 }
356
357 KisConfig config(false);
358 config.setCurrentInputProfile(d->currentProfile->name());
359
360 //Force a reload of the current profile in input manager and whatever else uses the profile.
361 Q_EMIT currentProfileChanged();
362}

References currentProfileChanged(), d, p, KoResourcePaths::saveLocation(), saveProfile(), and KisConfig::setCurrentInputProfile().

◆ setCurrentProfile()

void KisInputProfileManager::setCurrentProfile ( KisInputProfile * profile)

Set the current active profile.

Parameters
profileThe profile to set as current.

Definition at line 85 of file kis_input_profile_manager.cpp.

86{
87 if (profile && profile != d->currentProfile) {
88 d->currentProfile = profile;
89 Q_EMIT currentProfileChanged();
90 }
91}

References currentProfileChanged(), d, and profile().

Member Data Documentation

◆ actions

QList< KisAbstractInputAction * > KisInputProfileManager::actions

Definition at line 51 of file kis_input_profile_manager.cpp.

◆ currentProfile

KisInputProfile * KisInputProfileManager::currentProfile

Definition at line 47 of file kis_input_profile_manager.cpp.

◆ d

Private* const KisInputProfileManager::d
private

Definition at line 146 of file kis_input_profile_manager.h.

◆ profiles

QList< KisInputProfile * > KisInputProfileManager::profiles

Definition at line 49 of file kis_input_profile_manager.cpp.


The documentation for this class was generated from the following files: