diff --git a/libs/widgets/KoResourceItemChooserContextMenu.cpp b/libs/widgets/KoResourceItemChooserContextMenu.cpp index da882ded67..24ff410121 100644 --- a/libs/widgets/KoResourceItemChooserContextMenu.cpp +++ b/libs/widgets/KoResourceItemChooserContextMenu.cpp @@ -1,213 +1,216 @@ /* This file is part of the KDE project * 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 "KoResourceItemChooserContextMenu.h" #include #include #include #include #include -#include #include KoLineEditAction::KoLineEditAction(QObject* parent) : QWidgetAction(parent) , m_closeParentOnTrigger(false) { QWidget* pWidget = new QWidget (0); QHBoxLayout* pLayout = new QHBoxLayout(); m_label = new QLabel(0); m_editBox = new QLineEdit(0); + m_editBox->setClearButtonEnabled(true); + m_AddButton = new QPushButton(); + m_AddButton->setIcon(koIcon("list-add")); pLayout->addWidget(m_label); pLayout->addWidget(m_editBox); + pLayout->addWidget(m_AddButton); pWidget->setLayout(pLayout); setDefaultWidget(pWidget); - connect (m_editBox, SIGNAL(returnPressed()), - this, SLOT(onTriggered())); + connect (m_editBox, &QLineEdit::returnPressed, this, &KoLineEditAction::onTriggered); + connect (m_AddButton, &QPushButton::clicked, this, &KoLineEditAction::onTriggered); } KoLineEditAction::~KoLineEditAction() { } void KoLineEditAction::setIcon(const QIcon &icon) { QPixmap pixmap = QPixmap(icon.pixmap(16,16)); m_label->setPixmap(pixmap); } void KoLineEditAction::closeParentOnTrigger(bool closeParent) { m_closeParentOnTrigger = closeParent; } bool KoLineEditAction::closeParentOnTrigger() { return m_closeParentOnTrigger; } void KoLineEditAction::onTriggered() { if (! m_editBox->text().isEmpty()) { emit triggered( m_editBox->text()); m_editBox->text().clear(); if (m_closeParentOnTrigger) { this->parentWidget()->close(); m_editBox->clearFocus(); } } } void KoLineEditAction::setPlaceholderText(const QString& clickMessage) { m_editBox->setPlaceholderText(clickMessage); } void KoLineEditAction::setText(const QString& text) { m_editBox->setText(text); } void KoLineEditAction::setVisible(bool showAction) { QLayout* currentLayout = defaultWidget()->layout(); this->QAction::setVisible(showAction); for(int i=0;icount();i++) { currentLayout->itemAt(i)->widget()->setVisible(showAction); } defaultWidget()->setVisible(showAction); } ContextMenuExistingTagAction::ContextMenuExistingTagAction(KoResource* resource, QString tag, QObject* parent) : QAction(parent) , m_resource(resource) , m_tag(tag) { setText(tag); connect (this, SIGNAL(triggered()), this, SLOT(onTriggered())); } ContextMenuExistingTagAction::~ContextMenuExistingTagAction() { } void ContextMenuExistingTagAction::onTriggered() { emit triggered(m_resource, m_tag); } NewTagAction::~NewTagAction() { } NewTagAction::NewTagAction(KoResource* resource, QMenu* parent) :KoLineEditAction (parent) { m_resource = resource; setIcon(koIcon("document-new")); setPlaceholderText(i18n("New tag")); closeParentOnTrigger(true); connect (this, SIGNAL(triggered(QString)), this, SLOT(onTriggered(QString))); } void NewTagAction::onTriggered(const QString & tagName) { emit triggered(m_resource,tagName); } KoResourceItemChooserContextMenu::KoResourceItemChooserContextMenu(KoResource* resource, const QStringList& resourceTags, const QString& currentlySelectedTag, const QStringList& allTags) { QImage image = resource->image(); QIcon icon(QPixmap::fromImage(image)); QAction * label = new QAction(resource->name(), this); label->setIcon(icon); addAction(label); QMenu * removableTagsMenu; QMenu * assignableTagsMenu; QStringList removables = resourceTags; QStringList assignables = allTags; removables.sort(); assignables.sort(); assignableTagsMenu = addMenu(koIcon("list-add"),i18n("Assign to tag")); if (!removables.isEmpty()) { addSeparator(); QString currentTag = currentlySelectedTag; if (removables.contains(currentTag)) { assignables.removeAll(currentTag); removables.removeAll(currentTag); ContextMenuExistingTagAction * removeTagAction = new ContextMenuExistingTagAction(resource, currentTag, this); removeTagAction->setText(i18n("Remove from this tag")); removeTagAction->setIcon(koIcon("list-remove")); connect(removeTagAction, SIGNAL(triggered(KoResource*,QString)), this, SIGNAL(resourceTagRemovalRequested(KoResource*,QString))); addAction(removeTagAction); } if (!removables.isEmpty()) { removableTagsMenu = addMenu(koIcon("list-remove"),i18n("Remove from other tag")); foreach (const QString &tag, removables) { assignables.removeAll(tag); ContextMenuExistingTagAction * removeTagAction = new ContextMenuExistingTagAction(resource, tag, this); connect(removeTagAction, SIGNAL(triggered(KoResource*,QString)), this, SIGNAL(resourceTagRemovalRequested(KoResource*,QString))); removableTagsMenu->addAction(removeTagAction); } } } foreach (const QString &tag, assignables) { ContextMenuExistingTagAction * addTagAction = new ContextMenuExistingTagAction(resource, tag, this); connect(addTagAction, SIGNAL(triggered(KoResource*,QString)), this, SIGNAL(resourceTagAdditionRequested(KoResource*,QString))); assignableTagsMenu->addAction(addTagAction); } assignableTagsMenu->addSeparator(); NewTagAction * addTagAction = new NewTagAction(resource, this); connect(addTagAction, SIGNAL(triggered(KoResource*,QString)), this, SIGNAL(resourceAssignmentToNewTagRequested(KoResource*,QString))); assignableTagsMenu->addAction(addTagAction); } KoResourceItemChooserContextMenu::~KoResourceItemChooserContextMenu() { } diff --git a/libs/widgets/KoResourceItemChooserContextMenu.h b/libs/widgets/KoResourceItemChooserContextMenu.h index 970a930a6d..fc58380c6b 100644 --- a/libs/widgets/KoResourceItemChooserContextMenu.h +++ b/libs/widgets/KoResourceItemChooserContextMenu.h @@ -1,120 +1,121 @@ /* * This file is part of the KDE project * 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. * */ #ifndef KORESOURCEITEMCHOOSERCONTEXTMENU_H #define KORESOURCEITEMCHOOSERCONTEXTMENU_H #include #include #include -#include +#include +#include -class QLineEdit; class KoResource; class ContextMenuExistingTagAction : public QAction { Q_OBJECT public: explicit ContextMenuExistingTagAction( KoResource * resource, QString tag, QObject* parent = 0); ~ContextMenuExistingTagAction() override; Q_SIGNALS: void triggered(KoResource * resource, QString tag); protected Q_SLOTS: void onTriggered(); private: KoResource * m_resource; QString m_tag; }; /*! * A line edit QWidgetAction. * Default behavior: Closes its parent upon triggering. */ class KoLineEditAction : public QWidgetAction { Q_OBJECT public: explicit KoLineEditAction(QObject* parent); ~KoLineEditAction() override; void setIcon(const QIcon &icon); void closeParentOnTrigger(bool closeParent); bool closeParentOnTrigger(); void setPlaceholderText(const QString& clickMessage); void setText(const QString& text); void setVisible(bool showAction); Q_SIGNALS: void triggered(const QString &tag); protected Q_SLOTS: void onTriggered(); private: bool m_closeParentOnTrigger; QLabel * m_label; QLineEdit * m_editBox; + QPushButton * m_AddButton; }; class NewTagAction : public KoLineEditAction { Q_OBJECT public: explicit NewTagAction (KoResource* resource, QMenu* parent); ~NewTagAction() override; Q_SIGNALS: void triggered(KoResource * resource, const QString &tag); protected Q_SLOTS: void onTriggered(const QString& tagName); private: KoResource * m_resource; }; class KoResourceItemChooserContextMenu : public QMenu { Q_OBJECT public: explicit KoResourceItemChooserContextMenu ( KoResource* resource, const QStringList& resourceTags, const QString& currentlySelectedTag, const QStringList& allTags ); ~KoResourceItemChooserContextMenu() override; Q_SIGNALS: /// Emitted when a resource should be added to an existing tag. void resourceTagAdditionRequested(KoResource* resource, const QString& tag); /// Emitted when a resource should be removed from an existing tag. void resourceTagRemovalRequested(KoResource* resource, const QString& tag); /// Emitted when a resource should be added to a new tag, which will need to be created. void resourceAssignmentToNewTagRequested(KoResource* resource, const QString& tag); }; #endif // KORESOURCEITEMCHOOSERCONTEXTMENU_H