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

#include <wdgtagselection.h>

+ Inheritance diagram for KisWdgTagSelectionControllerOneResource:

Public Member Functions

 KisWdgTagSelectionControllerOneResource (KisTagSelectionWidget *widget, bool editable)
 
void setResourceIds (QString resourceType, QList< int > resourceIds)
 
 ~KisWdgTagSelectionControllerOneResource () override
 

Private Slots

void slotAddTag (KoID tag)
 
void slotCreateNewTag (QString tag)
 
void slotRemoveTag (KoID tag)
 

Private Member Functions

void updateView ()
 

Private Attributes

bool m_editable {true}
 
QList< int > m_resourceIds
 
QString m_resourceType {""}
 
QSharedPointer< KisTagModelm_tagModel
 
QSharedPointer< KisTagResourceModelm_tagResourceModel
 
KisTagSelectionWidgetm_tagSelectionWidget {0}
 

Detailed Description

Definition at line 20 of file wdgtagselection.h.

Constructor & Destructor Documentation

◆ KisWdgTagSelectionControllerOneResource()

KisWdgTagSelectionControllerOneResource::KisWdgTagSelectionControllerOneResource ( KisTagSelectionWidget * widget,
bool editable )

Definition at line 35 of file wdgtagselection.cpp.

36 : QObject(widget)
37 , m_tagSelectionWidget(widget)
38 , m_editable(editable)
39{
40 connect(widget, SIGNAL(sigAddTagToSelection(KoID)), this, SLOT(slotAddTag(KoID)));
41 connect(widget, SIGNAL(sigRemoveTagFromSelection(KoID)), this, SLOT(slotRemoveTag(KoID)));
42 connect(widget, SIGNAL(sigCreateNewTag(QString)), this, SLOT(slotCreateNewTag(QString)));
43 m_tagSelectionWidget->setEnabled(false); // because there is no resource selected yet
44
45}
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))
KisTagSelectionWidget * m_tagSelectionWidget
Definition KoID.h:30

References connect(), m_tagSelectionWidget, slotAddTag(), slotCreateNewTag(), and slotRemoveTag().

◆ ~KisWdgTagSelectionControllerOneResource()

KisWdgTagSelectionControllerOneResource::~KisWdgTagSelectionControllerOneResource ( )
override

Definition at line 47 of file wdgtagselection.cpp.

48{
49
50}

Member Function Documentation

◆ setResourceIds()

void KisWdgTagSelectionControllerOneResource::setResourceIds ( QString resourceType,
QList< int > resourceIds )

Definition at line 52 of file wdgtagselection.cpp.

53{
54 QString oldResourceType = m_resourceType;
55 m_resourceIds = resourceIds;
56 m_resourceType = resourceType;
57
58 if (resourceType != "" && (oldResourceType != resourceType || !m_tagResourceModel || !m_tagModel)) {
59 m_tagResourceModel.reset(new KisTagResourceModel(resourceType));
61 m_tagModel.reset(new KisTagModel(resourceType));
63 }
64
65 if (resourceIds.count() == 0) {
66 QList<KoID> emptyList;
67 m_tagSelectionWidget->setTagList(m_editable, emptyList, emptyList);
68 m_tagSelectionWidget->setEnabled(false);
69 } else {
70 m_tagResourceModel->setResourcesFilter(m_resourceIds.toVector());
71 m_tagSelectionWidget->setEnabled(true);
72 updateView();
73 }
74}
The KisTagResourceModel class makes it possible to retrieve the resources for certain tags or the tag...
void setTagList(bool editable, QList< KoID > &selected, QList< KoID > &notSelected)
QSharedPointer< KisTagResourceModel > m_tagResourceModel
QSharedPointer< KisTagModel > m_tagModel

References m_editable, m_resourceIds, m_resourceType, m_tagModel, m_tagResourceModel, m_tagSelectionWidget, KisAllTagsModel::Name, KisTagSelectionWidget::setTagList(), KisAbstractResourceFilterInterface::ShowAllResources, and updateView().

◆ slotAddTag

void KisWdgTagSelectionControllerOneResource::slotAddTag ( KoID tag)
privateslot

Definition at line 86 of file wdgtagselection.cpp.

87{
88 if (m_resourceIds.count() == 0) return;
89
90 KisTagSP tagsp = m_tagModel->tagForUrl(tag.id());
91
92 m_tagResourceModel->tagResources(tagsp, m_resourceIds.toVector());
93 updateView();
94}
QString id() const
Definition KoID.cpp:63

References KoID::id(), m_resourceIds, m_tagModel, m_tagResourceModel, and updateView().

◆ slotCreateNewTag

void KisWdgTagSelectionControllerOneResource::slotCreateNewTag ( QString tag)
privateslot

Definition at line 96 of file wdgtagselection.cpp.

97{
98 if (m_resourceIds.count() == 0 || m_resourceType == "" || tag == "") return;
99
100 KisTagSP tagsp = m_tagModel->tagForUrl(tag);
101 if (tagsp.isNull()) {
103 m_tagModel->addTag(tag, false, vec);
104 tagsp = m_tagModel->tagForUrl(tag);
105 } else if (!tagsp->active()) { // if tag is active, simply use that tag
106 // if you use this simple cast, the order of buttons must match order of options in the enum
107 int response = QMessageBox::question(0, i18nc("Dialog title", "Overwrite tag?"), i18nc("Question to the user in a dialog about creating a tag",
108 "A tag with this unique name already exists. Do you want to replace it?"),
109 i18nc("Option in a dialog to discard the previously existing tag and creating a new one in its place", "Replace (overwrite) tag"),
110 i18nc("Option in a dialog to undelete (reactivate) existing tag with its old assigned resources", "Restore previous tag"), i18n("Cancel"));
111 if (response == 0) { // Overwrite
112 m_tagModel->addTag(tag, true, QVector<KoResourceSP>()); // will overwrite the tag
113 tagsp = m_tagModel->tagForUrl(tag);
114 } else if (response == 1) { // Restore/use previously existing one
115 m_tagModel->setTagActive(tagsp);
116 } else {
117 updateView();
118 return;
119 }
120 }
121
123 m_tagResourceModel->tagResources(tagsp, m_resourceIds.toVector());
124 updateView();
125}
#define KIS_ASSERT_RECOVER_RETURN(cond)
Definition kis_assert.h:75

References KIS_ASSERT_RECOVER_RETURN, m_resourceIds, m_resourceType, m_tagModel, m_tagResourceModel, and updateView().

◆ slotRemoveTag

void KisWdgTagSelectionControllerOneResource::slotRemoveTag ( KoID tag)
privateslot

Definition at line 76 of file wdgtagselection.cpp.

77{
78 if (m_resourceIds.count() == 0) return;
79
80 KisTagSP tagsp = m_tagModel->tagForUrl(tag.id());
81
82 m_tagResourceModel->untagResources(tagsp, m_resourceIds.toVector());
83 updateView();
84}

References KoID::id(), m_resourceIds, m_tagModel, m_tagResourceModel, and updateView().

◆ updateView()

void KisWdgTagSelectionControllerOneResource::updateView ( )
private

Definition at line 127 of file wdgtagselection.cpp.

128{
129 if (m_resourceIds.count() == 0) {
130 QList<KoID> emptyList;
131 m_tagSelectionWidget->setTagList(m_editable, emptyList, emptyList);
132 return;
133 }
134
135 QMap<QString, int> tagsCounts;
136 for (int i = 0; i < m_tagModel->rowCount(); i++) {
137 QModelIndex idx = m_tagModel->index(i, 0);
138 int id = m_tagModel->data(idx, Qt::UserRole + KisAllTagsModel::Id).toInt();
139 if (id < 0) {
140 continue;
141 }
142 QString tagUrl = m_tagModel->data(idx, Qt::UserRole + KisAllTagsModel::Url).toString();
143 if (!tagsCounts.contains(tagUrl)) {
144 tagsCounts.insert(tagUrl, 0);
145 }
146 }
147
148 // IMPORTANT: this only works correctly because there was setResourcesFilter() called in setResourceIds() function
149 // if at any moment there is situation this needs to work without setResourceIds(),
150 // call m_tagResourceModel->setResourcesFilter(m_resourceIds.toVector()); before this loop
151 // (it will make it slightly slower since it invalidates filter in the proxy model)
152 for (int i = 0; i < m_tagResourceModel->rowCount(); i++) {
153 QModelIndex idx = m_tagResourceModel->index(i, 0);
154 KisTagSP tag = m_tagResourceModel->data(idx, Qt::UserRole + KisAllTagResourceModel::Tag).value<KisTagSP>();
155 tagsCounts[tag->url()] += 1;
156 }
157 QList<KoID> semiSelected;
158 QList<KoID> selected;
159 QList<KoID> toSelect;
160
161 for (int i = 0; i < m_tagModel->rowCount(); i++) {
162 QModelIndex idx = m_tagModel->index(i, 0);
163 int id = m_tagModel->data(idx, Qt::UserRole + KisAllTagsModel::Id).toInt();
164 if (id < 0) {
165 continue;
166 }
167 QString tagUrl = m_tagModel->data(idx, Qt::UserRole + KisAllTagsModel::Url).toString();
168 QString tagName = m_tagModel->data(idx, Qt::UserRole + KisAllTagsModel::Name).toString();
169 KoID tag(tagUrl, tagName);
170 if (tagsCounts[tagUrl] == m_resourceIds.count()) {
171 selected << tag;
172 } else if (tagsCounts[tagUrl] > 0) {
173 semiSelected << tag;
174 toSelect << tag; // we want to be able to add a tag to every resource even though some are already tagged
175 } else {
176 toSelect << tag;
177 }
178 }
179
180 m_tagSelectionWidget->setTagList(m_editable, selected, toSelect, semiSelected);
181}

References KisAllTagsModel::Id, m_editable, m_resourceIds, m_tagModel, m_tagResourceModel, m_tagSelectionWidget, KisAllTagsModel::Name, KisTagSelectionWidget::setTagList(), KisAllTagResourceModel::Tag, and KisAllTagsModel::Url.

Member Data Documentation

◆ m_editable

bool KisWdgTagSelectionControllerOneResource::m_editable {true}
private

Definition at line 40 of file wdgtagselection.h.

40{true};

◆ m_resourceIds

QList<int> KisWdgTagSelectionControllerOneResource::m_resourceIds
private

Definition at line 41 of file wdgtagselection.h.

◆ m_resourceType

QString KisWdgTagSelectionControllerOneResource::m_resourceType {""}
private

Definition at line 42 of file wdgtagselection.h.

42{""};

◆ m_tagModel

QSharedPointer<KisTagModel> KisWdgTagSelectionControllerOneResource::m_tagModel
private

Definition at line 44 of file wdgtagselection.h.

◆ m_tagResourceModel

QSharedPointer<KisTagResourceModel> KisWdgTagSelectionControllerOneResource::m_tagResourceModel
private

Definition at line 45 of file wdgtagselection.h.

◆ m_tagSelectionWidget

KisTagSelectionWidget* KisWdgTagSelectionControllerOneResource::m_tagSelectionWidget {0}
private

Definition at line 39 of file wdgtagselection.h.

39{0};

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