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

The KisResourceItemChooserContextMenu class is responsible for the context menu in ResourceItemChooser. More...

#include <KisResourceItemChooserContextMenu.h>

+ Inheritance diagram for KisResourceItemChooserContextMenu:

Public Slots

void addResourceExistingTag (const KisTagSP tag, KoResourceSP resource)
 addResourceExistingTag slot for a signal from the action to add the tag to the resource
 
void removeResourceExistingTag (const KisTagSP tag, KoResourceSP resource)
 removeResourceExistingTag slot for a signal from the action to remove the tag from the resource
 

Signals

void resourceAssignmentToNewTagRequested (const QString &tag, KoResourceSP resource)
 Emitted when a resource should be added to a new tag, which will need to be created.
 
void resourceTagAdditionRequested (const KisTagSP tag, KoResourceSP resource)
 Emitted when a resource should be added to an existing tag.
 
void resourceTagRemovalRequested (KoResourceSP resource, const KisTagSP tag)
 Emitted when a resource should be removed from an existing tag.
 

Public Member Functions

 KisResourceItemChooserContextMenu (KoResourceSP resource, const KisTagSP currentlySelectedTag, KisTagChooserWidget *tagChooser)
 KisResourceItemChooserContextMenu the constructor for the KisResourceItemChooserContextMenu class.
 
 ~KisResourceItemChooserContextMenu () override
 the destructor
 

Private Attributes

KisTagChooserWidgetm_tagChooserWidget {0}
 
KisTagModelm_tagModel {0}
 m_tagModel data model for tags (for tagging and untagging resources and create lists of tags)
 

Detailed Description

The KisResourceItemChooserContextMenu class is responsible for the context menu in ResourceItemChooser.

The context menu for the resource item in the resource item chooser (see: main area in the Brush Presets docker) contains actions to tag and untag the selected resource. In case of tagging the user can choose to create a new tag or select one of the existing ones. In case of untagging the user can untag from the current selected tag (in the combobox) or from some other tags. This class needs to provide correct lists of tags and take into account that "All" and "All Untagged" (and possibly other generated tags) are special and the user cannot untag the resource from it.

Definition at line 34 of file KisResourceItemChooserContextMenu.h.

Constructor & Destructor Documentation

◆ KisResourceItemChooserContextMenu()

KisResourceItemChooserContextMenu::KisResourceItemChooserContextMenu ( KoResourceSP resource,
const KisTagSP currentlySelectedTag,
KisTagChooserWidget * tagChooser )
explicit

KisResourceItemChooserContextMenu the constructor for the KisResourceItemChooserContextMenu class.

Parameters
resourcethe resource that the context menu is called for
currentlySelectedTagthe currently selected tag in the combobox over the resource item chooser

Definition at line 29 of file KisResourceItemChooserContextMenu.cpp.

32 : m_tagChooserWidget(tagChooser)
33{
34 QImage image = resource->image();
35 QIcon icon(QPixmap::fromImage(image));
36 QAction *label = new QAction(resource->name(), this);
37 label->setIcon(icon);
38
39 addAction(label);
40
41 QMenu *removableTagsMenu;
42 QMenu *assignableTagsMenu;
43
44 m_tagModel = new KisTagModel(resource->resourceType().first);
46 KisResourceModel resourceModel(resource->resourceType().first);
47 KisTagResourceModel tagResourceModel(resource->resourceType().first);
48
49 tagResourceModel.setResourcesFilter(QVector<KoResourceSP>() << resource);
50 tagResourceModel.sort(KisAllTagResourceModel::TagName);
51
52 QList<KisTagSP> removableTags;
53 for (int i = 0; i < tagResourceModel.rowCount(); ++i) {
54 KisTagSP tag = tagResourceModel.data(tagResourceModel.index(i, 0), Qt::UserRole + KisAllTagResourceModel::Tag).value<KisTagSP>();
55 if (!tag.isNull() && tag->valid() && !isSpecialTag(tag)) {
56 removableTags << tag;
57 }
58 }
59
60 QList<KisTagSP> assignableTags;
61 for (int i = 0; i < m_tagModel->rowCount(); i++) {
62 QModelIndex index = m_tagModel->index(i, 0);
63 KisTagSP tag = m_tagModel->tagForIndex(index);
64 if (!tag.isNull() && tag->valid() && !isSpecialTag(tag)) {
65 assignableTags << tag;
66 }
67 }
68
69 CompareWithOtherTagFunctor comparer(currentlySelectedTag);
70
71 bool currentTagInRemovables = !currentlySelectedTag.isNull();
72 currentTagInRemovables = currentTagInRemovables
73 && (std::find_if(removableTags.begin(), removableTags.end(), comparer) != removableTags.end());
74
75 assignableTagsMenu = addMenu(koIcon("list-add"),i18n("Assign to tag"));
76
77 if (!removableTags.isEmpty()) {
78 addSeparator();
79 KisTagSP currentTag = currentlySelectedTag;
80
81 if (!currentTag.isNull() && currentTagInRemovables) {
82 // remove the current tag from both "Remove from tag: " and "Assign to tag: " lists
83 removableTags.erase(std::remove_if(removableTags.begin(),
84 removableTags.end(),
85 comparer),
86 removableTags.end());
87 assignableTags.erase(std::remove_if(assignableTags.begin(),
88 assignableTags.end(),
89 comparer),
90 assignableTags.end());
91
92 SimpleExistingTagAction *removeTagAction = new SimpleExistingTagAction(resource, currentTag, this);
93 removeTagAction->setText(i18n("Remove from this tag"));
94 removeTagAction->setIcon(koIcon("list-remove"));
95
96 connect(removeTagAction, SIGNAL(triggered(const KisTagSP, KoResourceSP)),
98 addAction(removeTagAction);
99 }
100
101 if (!removableTags.isEmpty()) {
102 removableTagsMenu = addMenu(koIcon("list-remove"),i18n("Remove from other tag"));
103
104 KisTagSP empty;
105 CompareWithOtherTagFunctor compareWithRemovable(empty);
106 foreach (const KisTagSP tag, removableTags) {
107
108 if (tag.isNull()) {
109 continue;
110 }
111
112 compareWithRemovable.setReferenceTag(tag);
113 assignableTags.erase(std::remove_if(assignableTags.begin(),
114 assignableTags.end(),
115 compareWithRemovable),
116 assignableTags.end());
117
118 SimpleExistingTagAction *removeTagAction = new SimpleExistingTagAction(resource, tag, this);
119
120 connect(removeTagAction, SIGNAL(triggered(const KisTagSP, KoResourceSP)),
122 removableTagsMenu->addAction(removeTagAction);
123 }
124 }
125
126 }
127
128 foreach (const KisTagSP &tag, assignableTags) {
129 if (tag.isNull()) {
130 continue;
131 }
132
133 SimpleExistingTagAction * addTagAction = new SimpleExistingTagAction(resource, tag, this);
134
135 connect(addTagAction, SIGNAL(triggered(const KisTagSP, KoResourceSP)),
136 this, SLOT(addResourceExistingTag(const KisTagSP, KoResourceSP)));
137
138 assignableTagsMenu->addAction(addTagAction);
139 }
140
141 assignableTagsMenu->addSeparator();
142
143 NewTagResourceAction *addNewTagAction = new NewTagResourceAction(resource, this);
144 connect(addNewTagAction, SIGNAL(triggered(QString, KoResourceSP)), m_tagChooserWidget, SLOT(addTag(QString, KoResourceSP)));
145
146 assignableTagsMenu->addAction(addNewTagAction);
147
148}
bool isSpecialTag(KisTagSP tag)
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))
The CompareWithOtherTagFunctor class defines a comparer for tags.
Definition TagActions.h:186
void removeResourceExistingTag(const KisTagSP tag, KoResourceSP resource)
removeResourceExistingTag slot for a signal from the action to remove the tag from the resource
KisTagModel * m_tagModel
m_tagModel data model for tags (for tagging and untagging resources and create lists of tags)
void addResourceExistingTag(const KisTagSP tag, KoResourceSP resource)
addResourceExistingTag slot for a signal from the action to add the tag to the resource
The KisResourceModel class provides the main access to resources. It is possible to filter the resour...
KisTagSP tagForIndex(QModelIndex index=QModelIndex()) const override
The KisTagResourceModel class makes it possible to retrieve the resources for certain tags or the tag...
The NewTagResourceAction class defines an action that sends a signal with QString and a saved resourc...
Definition TagActions.h:156
The SimpleExistingTagAction class defines an action that holds a resource and a tag.
Definition TagActions.h:31
#define koIcon(name)
Use these macros for icons without any issues.
Definition kis_icon.h:25

References addResourceExistingTag(), connect(), isSpecialTag(), koIcon, m_tagChooserWidget, m_tagModel, KisAllTagsModel::Name, removeResourceExistingTag(), CompareWithOtherTagFunctor::setReferenceTag(), KisTagResourceModel::setResourcesFilter(), KisAllTagResourceModel::Tag, KisTagModel::tagForIndex(), and KisAllTagResourceModel::TagName.

◆ ~KisResourceItemChooserContextMenu()

KisResourceItemChooserContextMenu::~KisResourceItemChooserContextMenu ( )
override

the destructor

Definition at line 150 of file KisResourceItemChooserContextMenu.cpp.

151{
152 delete m_tagModel;
153}

References m_tagModel.

Member Function Documentation

◆ addResourceExistingTag

void KisResourceItemChooserContextMenu::addResourceExistingTag ( const KisTagSP tag,
KoResourceSP resource )
slot

addResourceExistingTag slot for a signal from the action to add the tag to the resource

Parameters
resourceresource that the tag needs to be add to
tagtag that needs to be add to the resource

Definition at line 161 of file KisResourceItemChooserContextMenu.cpp.

162{
163 KisTagResourceModel tagResourceModel(resource->resourceType().first);
164 tagResourceModel.tagResources(tag, QVector<int>() << resource->resourceId());
165}

References KisTagResourceModel::tagResources().

◆ removeResourceExistingTag

void KisResourceItemChooserContextMenu::removeResourceExistingTag ( const KisTagSP tag,
KoResourceSP resource )
slot

removeResourceExistingTag slot for a signal from the action to remove the tag from the resource

Parameters
resourceresource that the tag needs to be removed from
tagtag that needs to be removed from the resource

Definition at line 155 of file KisResourceItemChooserContextMenu.cpp.

156{
157 KisTagResourceModel tagResourceModel(resource->resourceType().first);
158 tagResourceModel.untagResources(tag, QVector<int>() << resource->resourceId());
159}

References KisTagResourceModel::untagResources().

◆ resourceAssignmentToNewTagRequested

void KisResourceItemChooserContextMenu::resourceAssignmentToNewTagRequested ( const QString & tag,
KoResourceSP resource )
signal

Emitted when a resource should be added to a new tag, which will need to be created.

◆ resourceTagAdditionRequested

void KisResourceItemChooserContextMenu::resourceTagAdditionRequested ( const KisTagSP tag,
KoResourceSP resource )
signal

Emitted when a resource should be added to an existing tag.

◆ resourceTagRemovalRequested

void KisResourceItemChooserContextMenu::resourceTagRemovalRequested ( KoResourceSP resource,
const KisTagSP tag )
signal

Emitted when a resource should be removed from an existing tag.

Member Data Documentation

◆ m_tagChooserWidget

KisTagChooserWidget* KisResourceItemChooserContextMenu::m_tagChooserWidget {0}
private

Definition at line 81 of file KisResourceItemChooserContextMenu.h.

81{0};

◆ m_tagModel

KisTagModel* KisResourceItemChooserContextMenu::m_tagModel {0}
private

m_tagModel data model for tags (for tagging and untagging resources and create lists of tags)

Definition at line 80 of file KisResourceItemChooserContextMenu.h.

80{0};

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