Warning, file /office/calligra/libs/widgets/KoResourceItemChooserContextMenu.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 
0002 /* This file is part of the KDE project
0003  *    Copyright (c) 2013 Sascha Suelzer <s.suelzer@gmail.com>
0004  *
0005  *    This library is free software; you can redistribute it and/or
0006  *    modify it under the terms of the GNU Library General Public
0007  *    License as published by the Free Software Foundation; either
0008  *    version 2 of the License, or (at your option) any later version.
0009  *
0010  *    This library is distributed in the hope that it will be useful,
0011  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013  *    Library General Public License for more details.
0014  *
0015  *    You should have received a copy of the GNU Library General Public License
0016  *    along with this library; see the file COPYING.LIB.  If not, write to
0017  *    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  *  * Boston, MA 02110-1301, USA.
0019  * */
0020 
0021 #include "KoResourceItemChooserContextMenu.h"
0022 
0023 #include <QDebug>
0024 #include <QLabel>
0025 #include <QGridLayout>
0026 
0027 #include <KoIcon.h>
0028 #include <klocalizedstring.h>
0029 #include <klineedit.h>
0030 
0031 #include "KoResource.h"
0032 
0033 KoLineEditAction::KoLineEditAction(QObject* parent)
0034     : QWidgetAction(parent)
0035     , m_closeParentOnTrigger(false)
0036 {
0037     QWidget* pWidget = new QWidget (nullptr);
0038     QHBoxLayout* pLayout = new QHBoxLayout();
0039     m_label = new QLabel(nullptr);
0040     m_editBox = new KLineEdit(nullptr);
0041     pLayout->addWidget(m_label);
0042     pLayout->addWidget(m_editBox);
0043     pWidget->setLayout(pLayout);
0044     setDefaultWidget(pWidget);
0045 
0046     connect (m_editBox, SIGNAL(returnPressed(QString)),
0047              this, SLOT(onTriggered(QString)));
0048 }
0049 
0050 void KoLineEditAction::setIcon(const QIcon &icon)
0051 {
0052     QPixmap pixmap = QPixmap(icon.pixmap(16,16));
0053     m_label->setPixmap(pixmap);
0054 }
0055 
0056 void KoLineEditAction::closeParentOnTrigger(bool closeParent)
0057 {
0058     m_closeParentOnTrigger = closeParent;
0059 }
0060 
0061 bool KoLineEditAction::closeParentOnTrigger()
0062 {
0063     return m_closeParentOnTrigger;
0064 }
0065 
0066 void KoLineEditAction::onTriggered(const QString& text)
0067 {
0068     if (!text.isEmpty()) {
0069         emit triggered(text);
0070         m_editBox->clear();
0071 
0072         if (m_closeParentOnTrigger) {
0073             this->parentWidget()->close();
0074             m_editBox->clearFocus();
0075         }
0076     }
0077 }
0078 
0079 void KoLineEditAction::setPlaceholderText(const QString& clickMessage)
0080 {
0081     m_editBox->setPlaceholderText(clickMessage);
0082 }
0083 
0084 void KoLineEditAction::setText(const QString& text)
0085 {
0086     m_editBox->setText(text);
0087 }
0088 
0089 void KoLineEditAction::setVisible(bool showAction)
0090 {
0091     QLayout* currentLayout = defaultWidget()->layout();
0092 
0093     this->QAction::setVisible(showAction);
0094 
0095     for(int i=0;i<currentLayout->count();i++) {
0096         currentLayout->itemAt(i)->widget()->setVisible(showAction);
0097     }
0098     defaultWidget()->setVisible(showAction);
0099 }
0100 
0101 ContextMenuExistingTagAction::ContextMenuExistingTagAction(KoResource* resource, const QString &tag, QObject* parent)
0102     : QAction(parent)
0103     , m_resource(resource)
0104     , m_tag(tag)
0105 {
0106     setText(tag);
0107     connect (this, SIGNAL(triggered()),
0108              this, SLOT(onTriggered()));
0109 }
0110 
0111 void ContextMenuExistingTagAction::onTriggered()
0112 {
0113     emit triggered(m_resource, m_tag);
0114 }
0115 NewTagAction::NewTagAction(KoResource* resource, QMenu* parent)
0116     :KoLineEditAction (parent)
0117 {
0118     m_resource = resource;
0119     setIcon(koIcon("document-new"));
0120     setPlaceholderText(i18n("New tag"));
0121     closeParentOnTrigger(true);
0122 
0123     connect (this, SIGNAL(triggered(QString)),
0124              this, SLOT(onTriggered(QString)));
0125 }
0126 
0127 void NewTagAction::onTriggered(const QString & tagName)
0128 {
0129     emit triggered(m_resource,tagName);
0130 }
0131 
0132 KoResourceItemChooserContextMenu::KoResourceItemChooserContextMenu(KoResource* resource,
0133                                                                    const QStringList& resourceTags,
0134                                                                    const QString& currentlySelectedTag,
0135                                                                    const QStringList& allTags)
0136 {
0137     QImage image = resource->image();
0138     QIcon icon(QPixmap::fromImage(image));
0139     QAction * label = new QAction(resource->name(), this);
0140     label->setIcon(icon);
0141 
0142     addAction(label);
0143 
0144     QMenu * removableTagsMenu;
0145     QMenu * assignableTagsMenu;
0146 
0147     QStringList removables = resourceTags;
0148     QStringList assignables = allTags;
0149 
0150     removables.sort();
0151     assignables.sort();
0152 
0153     assignableTagsMenu = addMenu(koIcon("list-add"),i18n("Assign to tag"));
0154 
0155     if (!removables.isEmpty()) {
0156         addSeparator();
0157         QString currentTag = currentlySelectedTag;
0158         if (removables.contains(currentTag)) {
0159             assignables.removeAll(currentTag);
0160             removables.removeAll(currentTag);
0161             ContextMenuExistingTagAction * removeTagAction = new ContextMenuExistingTagAction(resource, currentTag, this);
0162             removeTagAction->setText(i18n("Remove from this tag"));
0163             removeTagAction->setIcon(koIcon("list-remove"));
0164 
0165             connect(removeTagAction, SIGNAL(triggered(KoResource*,QString)),
0166                     this, SIGNAL(resourceTagRemovalRequested(KoResource*,QString)));
0167             addAction(removeTagAction);
0168         }
0169         if (!removables.isEmpty()) {
0170             removableTagsMenu = addMenu(koIcon("list-remove"),i18n("Remove from other tag"));
0171             foreach (const QString &tag, removables) {
0172                 assignables.removeAll(tag);
0173                 ContextMenuExistingTagAction * removeTagAction = new ContextMenuExistingTagAction(resource, tag, this);
0174 
0175                 connect(removeTagAction, SIGNAL(triggered(KoResource*,QString)),
0176                         this, SIGNAL(resourceTagRemovalRequested(KoResource*,QString)));
0177                 removableTagsMenu->addAction(removeTagAction);
0178             }
0179         }
0180     }
0181 
0182     foreach (const QString &tag, assignables) {
0183         ContextMenuExistingTagAction * addTagAction = new ContextMenuExistingTagAction(resource, tag, this);
0184 
0185         connect(addTagAction, SIGNAL(triggered(KoResource*,QString)),
0186                 this, SIGNAL(resourceTagAdditionRequested(KoResource*,QString)));
0187         assignableTagsMenu->addAction(addTagAction);
0188     }
0189     assignableTagsMenu->addSeparator();
0190 
0191     NewTagAction * addTagAction = new NewTagAction(resource, this);
0192     connect(addTagAction, SIGNAL(triggered(KoResource*,QString)),
0193             this, SIGNAL(resourceAssignmentToNewTagRequested(KoResource*,QString)));
0194     assignableTagsMenu->addAction(addTagAction);
0195 }