File indexing completed on 2024-05-12 15:59:58

0001 /*
0002  *    This file is part of the KDE project
0003  *    SPDX-FileCopyrightText: 2002 Patrick Julien <freak@codepimps.org>
0004  *    SPDX-FileCopyrightText: 2007 Jan Hambrecht <jaham@gmx.net>
0005  *    SPDX-FileCopyrightText: 2007 Sven Langkamp <sven.langkamp@gmail.com>
0006  *    SPDX-FileCopyrightText: 2011 Srikanth Tiyyagura <srikanth.tulasiram@gmail.com>
0007  *    SPDX-FileCopyrightText: 2011 José Luis Vergara <pentalis@gmail.com>
0008  *    SPDX-FileCopyrightText: 2013 Sascha Suelzer <s.suelzer@gmail.com>
0009  *    SPDX-FileCopyrightText: 2020 Agata Cacko <cacko.azh@gmail.com>
0010  *
0011  *    SPDX-License-Identifier: LGPL-2.0-or-later
0012  */
0013 
0014 #include "KisTagToolButton.h"
0015 
0016 #include <QToolButton>
0017 #include <QGridLayout>
0018 
0019 #include <klocalizedstring.h>
0020 
0021 #include <KoIcon.h>
0022 
0023 #include <kis_debug.h>
0024 #include <KisTagModel.h>
0025 
0026 #include "KisResourceItemChooserContextMenu.h"
0027 
0028 
0029 class KisTagToolButton::Private
0030 {
0031 public:
0032     QToolButton *tagToolButton {0};
0033 
0034     QAction *undeleteTagAction {0};
0035     QAction *deleteTagAction {0};
0036     UserInputTagAction *renameTagAction {0};
0037     UserInputTagAction *addTagAction {0};
0038 
0039     KisTagSP undeleteCandidate;
0040     KisTagSP currentTag;
0041 
0042 };
0043 
0044 KisTagToolButton::KisTagToolButton(QWidget* parent)
0045     : QWidget(parent)
0046     , d(new Private())
0047 {
0048     QGridLayout* buttonLayout = new QGridLayout(this);
0049     buttonLayout->setMargin(0);
0050     buttonLayout->setSpacing(0);
0051 
0052     d->tagToolButton = new QToolButton(this);
0053     loadIcon();
0054     d->tagToolButton->setText(i18n("Tag"));
0055     d->tagToolButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
0056     d->tagToolButton->setToolTip(i18nc("@info:tooltip", "<qt>Show the tag box options.</qt>"));
0057     d->tagToolButton->setPopupMode(QToolButton::InstantPopup);
0058     d->tagToolButton->setEnabled(true);
0059 
0060     QMenu* popup = new QMenu(this);
0061 
0062     d->addTagAction = new UserInputTagAction(popup);
0063     d->addTagAction->setPlaceholderText(i18n("New tag"));
0064     d->addTagAction->setIcon(koIcon("document-new"));
0065     d->addTagAction->setCloseParentOnTrigger(true);
0066     popup->addAction(d->addTagAction);
0067 
0068     connect(d->addTagAction, SIGNAL(triggered(QString)), this, SIGNAL(newTagRequested(QString)));
0069 
0070     d->renameTagAction = new UserInputTagAction(popup);
0071     d->renameTagAction->setPlaceholderText(i18n("Rename tag"));
0072     d->renameTagAction->setIcon(koIcon("edit-rename"));
0073     d->renameTagAction->setCloseParentOnTrigger(true);
0074     popup->addAction(d->renameTagAction);
0075 
0076     connect(d->renameTagAction, SIGNAL(triggered(QString)), this, SIGNAL(renamingOfCurrentTagRequested(QString)));
0077 
0078     popup->addSeparator();
0079 
0080     d->deleteTagAction = new QAction(popup);
0081     d->deleteTagAction->setText(i18n("Delete this tag"));
0082     d->deleteTagAction->setIcon(koIcon("edit-delete"));
0083     popup->addAction(d->deleteTagAction);
0084 
0085     connect(d->deleteTagAction, SIGNAL(triggered()), this, SIGNAL(deletionOfCurrentTagRequested()));
0086 
0087     popup->addSeparator();
0088 
0089     d->undeleteTagAction = new QAction(popup);
0090     d->undeleteTagAction->setIcon(koIcon("edit-redo"));
0091     d->undeleteTagAction->setVisible(false);
0092     popup->addAction(d->undeleteTagAction);
0093 
0094     connect(d->undeleteTagAction, SIGNAL(triggered()), this, SLOT(onTagUndeleteClicked()));
0095 
0096     connect(popup, SIGNAL(aboutToShow()), this, SIGNAL(popupMenuAboutToShow()));
0097 
0098     d->tagToolButton->setMenu(popup);
0099     buttonLayout->addWidget(d->tagToolButton);
0100 }
0101 
0102 KisTagToolButton::~KisTagToolButton()
0103 {
0104     delete d;
0105 }
0106 
0107 void KisTagToolButton::readOnlyMode(bool activate)
0108 {
0109     activate = !activate;
0110     d->renameTagAction->setVisible(activate);
0111     d->deleteTagAction->setVisible(activate);
0112 }
0113 
0114 void KisTagToolButton::setUndeletionCandidate(const KisTagSP deletedTag)
0115 {
0116     if (deletedTag.isNull() || deletedTag->name().isEmpty()) {
0117         d->undeleteTagAction->setVisible(false);
0118         d->undeleteCandidate.clear();
0119     } else {
0120         d->undeleteCandidate = deletedTag;
0121         d->undeleteTagAction->setText(i18n("Undelete") +" "+ deletedTag->name());
0122         d->undeleteTagAction->setVisible(true);
0123     }
0124 }
0125 
0126 KisTagSP KisTagToolButton::undeletionCandidate() const
0127 {
0128     return d->undeleteCandidate;
0129 }
0130 
0131 void KisTagToolButton::setCurrentTag(const KisTagSP tag)
0132 {
0133     d->currentTag = tag;
0134     d->deleteTagAction->setProperty("currentTag", QVariant::fromValue<KisTagSP>(tag));
0135 }
0136 
0137 void KisTagToolButton::loadIcon()
0138 {
0139     d->tagToolButton->setIcon(koIcon("bookmarks"));
0140 }
0141 
0142 void KisTagToolButton::onTagUndeleteClicked()
0143 {
0144     emit undeletionOfTagRequested(d->undeleteCandidate);
0145 }
0146 
0147