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}
KisTagSelectionWidget * m_tagSelectionWidget
Definition KoID.h:30

References 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 QMessageBox question = QMessageBox(QMessageBox::Question,
107 i18nc("Dialog title", "Overwrite tag?"),
108 i18nc("Question to the user in a dialog about creating a tag",
109 "A tag with this unique name already exists. Do you want to replace it?")
110 );
111 question.addButton(
112 i18nc("Option in a dialog to discard the previously existing tag and creating a new one in its place",
113 "Replace (overwrite) tag"),
114 QMessageBox::DestructiveRole
115 );
116 question.addButton(
117 i18nc("Option in a dialog to undelete (reactivate) existing tag with its old assigned resources",
118 "Restore previous tag"),
119 QMessageBox::AcceptRole);
120 question.addButton(QMessageBox::Cancel);
121 question.exec();
122 QMessageBox::ButtonRole response = question.buttonRole(question.clickedButton());
123 if (response == QMessageBox::DestructiveRole) { // Overwrite
124 m_tagModel->addTag(tag, true, QVector<KoResourceSP>()); // will overwrite the tag
125 tagsp = m_tagModel->tagForUrl(tag);
126 } else if (response == QMessageBox::AcceptRole) { // Restore/use previously existing one
127 m_tagModel->setTagActive(tagsp);
128 } else {
129 updateView();
130 return;
131 }
132 }
133
135 m_tagResourceModel->tagResources(tagsp, m_resourceIds.toVector());
136 updateView();
137}
#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 139 of file wdgtagselection.cpp.

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

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: