Warning, file /office/calligra/libs/widgets/KoTagChooserWidget.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) 2002 Patrick Julien <freak@codepimps.org>
0004  *    Copyright (c) 2007 Jan Hambrecht <jaham@gmx.net>
0005  *    Copyright (c) 2007 Sven Langkamp <sven.langkamp@gmail.com>
0006  *    Copyright (C) 2011 Srikanth Tiyyagura <srikanth.tulasiram@gmail.com>
0007  *    Copyright (c) 2011 José Luis Vergara <pentalis@gmail.com>
0008  *    Copyright (c) 2013 Sascha Suelzer <s.suelzer@gmail.com>
0009  *
0010  *    This library is free software; you can redistribute it and/or
0011  *    modify it under the terms of the GNU Library General Public
0012  *    License as published by the Free Software Foundation; either
0013  *    version 2 of the License, or (at your option) any later version.
0014  *
0015  *    This library is distributed in the hope that it will be useful,
0016  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
0017  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0018  *    Library General Public License for more details.
0019  *
0020  *    You should have received a copy of the GNU Library General Public License
0021  *    along with this library; see the file COPYING.LIB.  If not, write to
0022  *    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0023  *    Boston, MA 02110-1301, USA.
0024  */
0025 
0026 #include "KoTagChooserWidget.h"
0027 
0028 #include <QDebug>
0029 #include <QToolButton>
0030 #include <QGridLayout>
0031 
0032 #include <klocalizedstring.h>
0033 #include <kcombobox.h>
0034 
0035 #include <KoIcon.h>
0036 
0037 #include "KoResourceItemChooserContextMenu.h"
0038 
0039 #include "KoTagToolButton.h"
0040 
0041 class Q_DECL_HIDDEN KoTagChooserWidget::Private
0042 {
0043 public:
0044     KComboBox* comboBox;
0045     KoTagToolButton* tagToolButton;
0046     QList<QString> readOnlyTags;
0047     QList<QString> tags;
0048 };
0049 
0050 KoTagChooserWidget::KoTagChooserWidget(QWidget* parent): QWidget(parent)
0051 , d(new Private())
0052 {
0053     d->comboBox = new KComboBox(this);
0054     d->comboBox->setToolTip(i18n("Tag"));
0055     d->comboBox->setInsertPolicy(KComboBox::InsertAlphabetically);
0056     d->comboBox->setSizePolicy(QSizePolicy::MinimumExpanding , QSizePolicy::Fixed );
0057 
0058 
0059     QGridLayout* comboLayout = new QGridLayout(this);
0060 
0061     comboLayout->addWidget(d->comboBox, 0, 0);
0062 
0063     d->tagToolButton = new KoTagToolButton(this);
0064     comboLayout->addWidget(d->tagToolButton, 0, 1);
0065 
0066     comboLayout->setSpacing(0);
0067     comboLayout->setMargin(0);
0068     comboLayout->setColumnStretch(0, 3);
0069     this->setEnabled(true);
0070     clear();
0071 
0072     connect(d->comboBox, SIGNAL(currentIndexChanged(QString)),
0073             this, SIGNAL(tagChosen(QString)));
0074 
0075     connect(d->tagToolButton, SIGNAL(popupMenuAboutToShow()),
0076             this, SLOT(tagOptionsContextMenuAboutToShow()));
0077     connect(d->tagToolButton, SIGNAL(newTagRequested(QString)),
0078             this, SIGNAL(newTagRequested(QString)));
0079     connect(d->tagToolButton, SIGNAL(deletionOfCurrentTagRequested()),
0080             this, SLOT(contextDeleteCurrentTag()));
0081     connect(d->tagToolButton, SIGNAL(renamingOfCurrentTagRequested(QString)),
0082             this, SLOT(tagRenamingRequested(QString)));
0083     connect(d->tagToolButton, SIGNAL(undeletionOfTagRequested(QString)),
0084             this, SIGNAL(tagUndeletionRequested(QString)));
0085     connect(d->tagToolButton, SIGNAL(purgingOfTagUndeleteListRequested()),
0086             this, SIGNAL(tagUndeletionListPurgeRequested()));
0087 
0088 }
0089 
0090 KoTagChooserWidget::~KoTagChooserWidget()
0091 {
0092     delete d;
0093 }
0094 
0095 void KoTagChooserWidget::contextDeleteCurrentTag()
0096 {
0097     if (selectedTagIsReadOnly()) {
0098         return;
0099     }
0100     emit tagDeletionRequested(currentlySelectedTag());
0101 }
0102 
0103 void KoTagChooserWidget::tagRenamingRequested(const QString& newName)
0104 {
0105     if (newName.isEmpty() || selectedTagIsReadOnly()) {
0106         return;
0107     }
0108     emit tagRenamingRequested(currentlySelectedTag(), newName);
0109 }
0110 
0111 void KoTagChooserWidget::setUndeletionCandidate(const QString& tag)
0112 {
0113     d->tagToolButton->setUndeletionCandidate(tag);
0114 }
0115 
0116 void KoTagChooserWidget::setCurrentIndex(int index)
0117 {
0118     d->comboBox->setCurrentIndex(index);
0119 }
0120 
0121 int KoTagChooserWidget::findIndexOf(const QString &tagName)
0122 {
0123     return d->comboBox->findText(tagName);
0124 }
0125 
0126 void KoTagChooserWidget::addReadOnlyItem(const QString &tagName)
0127 {
0128     d->readOnlyTags.append(tagName);
0129 }
0130 
0131 void KoTagChooserWidget::insertItem(const QString &tagName)
0132 {
0133     QStringList tags = allTags();
0134     tags.append(tagName);
0135     tags.sort();
0136     foreach (const QString &readOnlyTag, d->readOnlyTags) {
0137         tags.prepend(readOnlyTag);
0138     }
0139 
0140     int index = tags.indexOf(tagName);
0141     if (d->comboBox->findText(tagName) == -1) {
0142         insertItemAt(index, tagName);
0143         d->tags.append(tagName);
0144     }
0145 
0146 }
0147 
0148 void KoTagChooserWidget::insertItemAt(int index, const QString &tag)
0149 {
0150     d->comboBox->insertItem(index,tag);
0151 }
0152 
0153 QString KoTagChooserWidget::currentlySelectedTag()
0154 {
0155     return d->comboBox->currentText();
0156 }
0157 
0158 QStringList KoTagChooserWidget::allTags()
0159 {
0160     return d->tags;
0161 }
0162 
0163 bool KoTagChooserWidget::selectedTagIsReadOnly()
0164 {
0165     return d->readOnlyTags.contains(d->comboBox->currentText()) ;
0166 }
0167 
0168 void KoTagChooserWidget::addItems(QStringList tagNames)
0169 {
0170     tagNames.sort();
0171     QStringList items;
0172 
0173     foreach(const QString & readOnlyTag, d->readOnlyTags) {
0174         items.append(readOnlyTag);
0175     }
0176 
0177     items.append(tagNames);
0178     d->tags.append(tagNames);
0179 
0180     d->comboBox->addItems(items);
0181 }
0182 
0183 void KoTagChooserWidget::clear()
0184 {
0185     d->comboBox->clear();
0186 }
0187 
0188 void KoTagChooserWidget::removeItem(const QString &item)
0189 {
0190     int pos = d->comboBox->findText(item);
0191     if (pos >= 0) {
0192         d->comboBox->removeItem(pos);
0193     }
0194 }
0195 
0196 void KoTagChooserWidget::tagOptionsContextMenuAboutToShow()
0197 {
0198     /* only enable the save button if the selected tag set is editable */
0199     d->tagToolButton->readOnlyMode(selectedTagIsReadOnly());
0200     emit popupMenuAboutToShow();
0201 }
0202 
0203 void KoTagChooserWidget::showTagToolButton(bool show)
0204 {
0205     d->tagToolButton->setVisible(show);
0206 }