diff --git a/libs/resourcewidgets/KisTagChooserWidget.cpp b/libs/resourcewidgets/KisTagChooserWidget.cpp index 4d1dd57abd..66bd88214c 100644 --- a/libs/resourcewidgets/KisTagChooserWidget.cpp +++ b/libs/resourcewidgets/KisTagChooserWidget.cpp @@ -1,274 +1,265 @@ /* * This file is part of the KDE project * Copyright (c) 2002 Patrick Julien * Copyright (c) 2007 Jan Hambrecht * Copyright (c) 2007 Sven Langkamp * Copyright (C) 2011 Srikanth Tiyyagura * Copyright (c) 2011 José Luis Vergara * Copyright (c) 2013 Sascha Suelzer * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #include "KisTagChooserWidget.h" #include #include #include #include #include #include #include #include "KisResourceItemChooserContextMenu.h" #include "KisTagToolButton.h" #include "kis_debug.h" #include class Q_DECL_HIDDEN KisTagChooserWidget::Private { public: QComboBox *comboBox; KisTagToolButton *tagToolButton; QList readOnlyTags; QList tags; KisTagModel* model; QScopedPointer activeFilterModel; Private(KisTagModel* model) : activeFilterModel(new KisActiveFilterTagProxyModel(0)) { activeFilterModel->setSourceModel(model); } }; KisTagChooserWidget::KisTagChooserWidget(KisTagModel* model, QWidget* parent) : QWidget(parent) , d(new Private(model)) { d->comboBox = new QComboBox(this); d->comboBox->setToolTip(i18n("Tag")); d->comboBox->setSizePolicy(QSizePolicy::MinimumExpanding , QSizePolicy::Fixed ); d->comboBox->setModel(d->activeFilterModel.get()); d->model = model; QGridLayout* comboLayout = new QGridLayout(this); comboLayout->addWidget(d->comboBox, 0, 0); d->tagToolButton = new KisTagToolButton(this); comboLayout->addWidget(d->tagToolButton, 0, 1); comboLayout->setSpacing(0); comboLayout->setMargin(0); comboLayout->setColumnStretch(0, 3); this->setEnabled(true); connect(d->comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(tagChanged(int))); connect(d->tagToolButton, SIGNAL(popupMenuAboutToShow()), this, SLOT (tagOptionsContextMenuAboutToShow())); connect(d->tagToolButton, SIGNAL(newTagRequested(KisTagSP)), this, SLOT(insertItem(KisTagSP))); connect(d->tagToolButton, SIGNAL(deletionOfCurrentTagRequested()), this, SLOT(contextDeleteCurrentTag())); connect(d->tagToolButton, SIGNAL(renamingOfCurrentTagRequested(KisTagSP)), this, SLOT(tagRenamingRequested(KisTagSP))); connect(d->tagToolButton, SIGNAL(undeletionOfTagRequested(KisTagSP)), this, SIGNAL(tagUndeletionRequested(KisTagSP))); connect(d->tagToolButton, SIGNAL(purgingOfTagUndeleteListRequested()), this, SIGNAL(tagUndeletionListPurgeRequested())); } KisTagChooserWidget::~KisTagChooserWidget() { delete d; } void KisTagChooserWidget::contextDeleteCurrentTag() { ENTER_FUNCTION(); fprintf(stderr, "void KisTagChooserWidget::contextDeleteCurrentTag()\n"); KisTagSP currentTag = currentlySelectedTag(); if (!currentTag.isNull() && currentTag->id() < 0) { fprintf(stderr, "trying to remove item: %s\n", currentTag->name().toUtf8().toStdString().c_str()); - removeItem(currentTag); + d->model->removeTag(currentTag); } setCurrentIndex(0); } void KisTagChooserWidget::tagChanged(int tagIndex) { ENTER_FUNCTION(); fprintf(stderr, "void KisTagChooserWidget::tagChanged(int) %d\n", tagIndex); if (tagIndex >= 0) { emit tagChosen(currentlySelectedTag()); KisTagSP tag = currentlySelectedTag(); if (tag->id() < 0) { fprintf(stderr, "tag name: %s, url: %s, id: %d", tag->name().toUtf8().toStdString().c_str(), tag->url().toUtf8().toStdString().c_str(), tag->id()); } } else { fprintf(stderr, "Requested -1 index; previous: %d\n", d->comboBox->currentIndex()); } } void KisTagChooserWidget::tagRenamingRequested(const KisTagSP newName) { // TODO: RESOURCES: it should use QString, not KisTagSP int previousIndex = d->comboBox->currentIndex(); ENTER_FUNCTION(); KisTagSP currentTag = currentlySelectedTag(); QString name = newName.isNull() ? "" : newName->name(); bool canRenameCurrentTag = !currentTag.isNull() && currentTag->id() < 0; fprintf(stderr, "renaming tag requested! to: %s\n", name.toUtf8().toStdString().c_str()); if (canRenameCurrentTag && !name.isEmpty()) { d->model->renameTag(currentTag, newName->name()); setCurrentIndex(previousIndex); } } void KisTagChooserWidget::setUndeletionCandidate(const KisTagSP tag) { ENTER_FUNCTION(); d->tagToolButton->setUndeletionCandidate(tag); } void KisTagChooserWidget::setCurrentIndex(int index) { fprintf(stderr, "set current index: %d", index); ENTER_FUNCTION(); d->comboBox->setCurrentIndex(index); } int KisTagChooserWidget::currentIndex() const { return d->comboBox->currentIndex(); } void KisTagChooserWidget::addReadOnlyItem(KisTagSP tag) { d->model->addTag(tag); ENTER_FUNCTION(); } KisTagSP KisTagChooserWidget::insertItem(KisTagSP tag) { // TODO: RESOURCES: this function should use QString, not KisTagSP int previous = d->comboBox->currentIndex(); if(tag.isNull() || tag->name().isNull() || tag->name().isEmpty()) { fprintf(stderr, "inserting item is empty\n"); return KisTagSP(); } fprintf(stderr, "inserting item!!! %s\n", tag->name().toUtf8().toStdString().c_str()); tag->setUrl(tag->name()); tag->setComment(tag->name()); tag->setActive(true); tag->setValid(true); ENTER_FUNCTION(); bool added = d->model->addTag(tag); fprintf(stderr, "added = %d\n", added); if (added) { for (int i = 0; i < d->model->rowCount(); i++) { QModelIndex index = d->model->index(i, 0); KisTagSP temp = d->model->tagForIndex(index); if (!temp.isNull() && temp->url() == tag->url()) { setCurrentIndex(i); return temp; } } } setCurrentIndex(previous); return KisTagSP(); } KisTagSP KisTagChooserWidget::currentlySelectedTag() { int row = d->comboBox->currentIndex(); if (row < 0) { return KisTagSP(); } if (d->comboBox->currentData().data()) { fprintf(stderr, "current data type = %s\n", d->comboBox->currentData().typeName()); } else { fprintf(stderr, "current data type = (null)\n"); } QModelIndex index = d->model->index(row, 0); KisTagSP tag = d->model->tagForIndex(index); fprintf(stderr, "current tag: %s\n", tag.isNull() ? "(null)" : tag->name().toStdString().c_str()); fprintf(stderr, "current index = %d\n", row); ENTER_FUNCTION() << tag; return tag; } bool KisTagChooserWidget::selectedTagIsReadOnly() { ENTER_FUNCTION(); return false; } void KisTagChooserWidget::addItems(QList tags) { ENTER_FUNCTION(); warnKrita << "not implemented"; Q_FOREACH(KisTagSP tag, tags) { insertItem(tag); } } void KisTagChooserWidget::clear() { ENTER_FUNCTION(); } -void KisTagChooserWidget::removeItem(KisTagSP item) -{ - fprintf(stderr, "removing item: %s\n", item->name().toUtf8().toStdString().c_str()); - ENTER_FUNCTION(); - if (!item.isNull()) { - d->model->removeTag(item); - } -} - void KisTagChooserWidget::tagOptionsContextMenuAboutToShow() { ENTER_FUNCTION(); /* only enable the save button if the selected tag set is editable */ d->tagToolButton->readOnlyMode(selectedTagIsReadOnly()); emit popupMenuAboutToShow(); } void KisTagChooserWidget::showTagToolButton(bool show) { ENTER_FUNCTION(); d->tagToolButton->setVisible(show); } diff --git a/libs/resourcewidgets/KisTagChooserWidget.h b/libs/resourcewidgets/KisTagChooserWidget.h index c2cf96e7a1..48d5dff084 100644 --- a/libs/resourcewidgets/KisTagChooserWidget.h +++ b/libs/resourcewidgets/KisTagChooserWidget.h @@ -1,108 +1,107 @@ /* * This file is part of the KDE project * Copyright (c) 2002 Patrick Julien * Copyright (c) 2007 Jan Hambrecht * Copyright (c) 2007 Sven Langkamp * Copyright (C) 2011 Srikanth Tiyyagura * Copyright (c) 2011 José Luis Vergara * Copyright (c) 2013 Sascha Suelzer * Copyright (c) 2019 Boudewijn Rempt * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #ifndef KISTAGCHOOSERWIDGET_H #define KISTAGCHOOSERWIDGET_H #include #include "kritaresourcewidgets_export.h" #include #include /// /// \brief The KisTagChooserWidget class is responsible /// /// for all the logic that tags combobox has in various resource choosers. /// (Example of usage: tag combobox in Brushes docker). /// It uses KisTagModel as a model for items in the combobox. /// It is also responsible for the popup for tag removal, renaming and creation /// that appears on the right side of the tag combobox. /// All the logic for adding and removing tags is done through KisTagModel. /// /// For logic related to tagging and untagging resources, check KisTaggingManager /// and KisItemChooserContextMenu. /// class KRITARESOURCEWIDGETS_EXPORT KisTagChooserWidget : public QWidget { Q_OBJECT public: explicit KisTagChooserWidget(KisTagModel* model, QWidget* parent); ~KisTagChooserWidget() override; /// \brief setCurrentIndex sets the current index in the combobox /// \param index index is the /// void setCurrentIndex(int index); int currentIndex() const; /// \brief currentlySelectedTag returns the current tag from combobox /// \return the tag that is currently selected in the tag combobox /// KisTagSP currentlySelectedTag(); /// /// \brief selectedTagIsReadOnly checks whether the tag is readonly (generated by Krita) /// \return true if the tag was generated by Krita, false if it's just a normal tag /// bool selectedTagIsReadOnly(); - void removeItem(KisTagSP item); void addItems(QList tagNames); void addReadOnlyItem(KisTagSP tagName); void clear(); void setUndeletionCandidate(const KisTagSP tag); void showTagToolButton(bool show); Q_SIGNALS: void newTagRequested(const KisTagSP tag); void tagDeletionRequested(const KisTagSP tag); void tagRenamingRequested(const KisTagSP oldTag, const KisTagSP newTag); void tagUndeletionRequested(const KisTagSP tag); void tagUndeletionListPurgeRequested(); void popupMenuAboutToShow(); void tagChosen(const KisTagSP tag); public Q_SLOTS: KisTagSP insertItem(KisTagSP tag); void tagChanged(int index); private Q_SLOTS: void tagRenamingRequested(const KisTagSP newName); void tagOptionsContextMenuAboutToShow(); void contextDeleteCurrentTag(); private: class Private; Private* const d; }; ; #endif // KOTAGCHOOSERWIDGET_H