1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
/*****************************************************************************
* Copyright (C) 2009 by Peter Penz <peter.penz@gmx.at> *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License version 2 as published by the Free Software Foundation. *
* *
* 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 "kedittagsdialog_p.h"
#include <kicon.h>
#include <klineedit.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <QEvent>
#include <QHBoxLayout>
#include <QLabel>
#include <QListWidget>
#include <QPushButton>
#include <QTimer>
#include <QVBoxLayout>
#include <QWidget>
KEditTagsDialog::KEditTagsDialog(const QList<Nepomuk2::Tag>& tags,
QWidget* parent,
Qt::WFlags flags) :
KDialog(parent, flags),
m_tags(tags),
m_tagsList(0),
m_newTagItem(0),
m_autoCheckedItem(0),
m_deleteCandidate(0),
m_newTagEdit(0),
m_deleteButtonTimer(0)
{
const QString caption = (tags.count() > 0) ?
i18nc("@title:window", "Change Tags") :
i18nc("@title:window", "Add Tags");
setCaption(caption);
setButtons(KDialog::Ok | KDialog::Cancel);
setDefaultButton(KDialog::Ok);
QWidget* mainWidget = new QWidget(this);
QVBoxLayout* topLayout = new QVBoxLayout(mainWidget);
QLabel* label = new QLabel(i18nc("@label:textbox",
"Configure which tags should "
"be applied."), this);
m_tagsList = new QListWidget(mainWidget);
m_tagsList->setMouseTracking(true);
m_tagsList->setSortingEnabled(true);
m_tagsList->setSelectionMode(QAbstractItemView::NoSelection);
m_tagsList->installEventFilter(this);
connect(m_tagsList, SIGNAL(itemEntered(QListWidgetItem*)),
this, SLOT(slotItemEntered(QListWidgetItem*)));
QLabel* newTagLabel = new QLabel(i18nc("@label", "Create new tag:"));
m_newTagEdit = new KLineEdit(this);
m_newTagEdit->setClearButtonShown(true);
connect(m_newTagEdit, SIGNAL(textEdited(QString)),
this, SLOT(slotTextEdited(QString)));
QHBoxLayout* newTagLayout = new QHBoxLayout();
newTagLayout->addWidget(newTagLabel);
newTagLayout->addWidget(m_newTagEdit, 1);
topLayout->addWidget(label);
topLayout->addWidget(m_tagsList);
topLayout->addLayout(newTagLayout);
setMainWidget(mainWidget);
loadTags();
// create the delete button, which is shown when
// hovering the items
m_deleteButton = new QPushButton(m_tagsList->viewport());
m_deleteButton->setIcon(KIcon("edit-delete"));
m_deleteButton->setToolTip(i18nc("@info", "Delete tag"));
m_deleteButton->hide();
connect(m_deleteButton, SIGNAL(clicked()), this, SLOT(deleteTag()));
m_deleteButtonTimer = new QTimer(this);
m_deleteButtonTimer->setSingleShot(true);
m_deleteButtonTimer->setInterval(500);
connect(m_deleteButtonTimer, SIGNAL(timeout()), this, SLOT(showDeleteButton()));
}
KEditTagsDialog::~KEditTagsDialog()
{
}
QList<Nepomuk2::Tag> KEditTagsDialog::tags() const
{
return m_tags;
}
bool KEditTagsDialog::eventFilter(QObject* watched, QEvent* event)
{
if ((watched == m_tagsList) && (event->type() == QEvent::Leave)) {
m_deleteButtonTimer->stop();
m_deleteButton->hide();
}
return KDialog::eventFilter(watched, event);
}
void KEditTagsDialog::slotButtonClicked(int button)
{
if (button == KDialog::Ok) {
// update m_tags with the checked values, so
// that the caller of the KEditTagsDialog can
// receive the tags by KEditTagsDialog::tags()
m_tags.clear();
const int count = m_tagsList->count();
for (int i = 0; i < count; ++i) {
QListWidgetItem* item = m_tagsList->item(i);
if (item->checkState() == Qt::Checked) {
const QUrl uri = item->data(Qt::UserRole).toUrl();
if( uri.isEmpty() ) {
Nepomuk2::Tag tag( item->text() );
tag.setLabel( item->text() );
m_tags.append( tag );
}
else {
m_tags.append( Nepomuk2::Tag(uri) );
}
}
}
accept();
} else {
KDialog::slotButtonClicked(button);
}
}
void KEditTagsDialog::slotTextEdited(const QString& text)
{
// Remove unnecessary spaces from a new tag is
// mandatory, as the user cannot see the difference
// between a tag "Test" and "Test ".
const QString tagText = text.simplified();
if (tagText.isEmpty()) {
removeNewTagItem();
return;
}
// Check whether the new tag already exists. If this
// is the case, remove the new tag item.
const int count = m_tagsList->count();
for (int i = 0; i < count; ++i) {
QListWidgetItem* item = m_tagsList->item(i);
const bool remove = (item->text() == tagText) &&
((m_newTagItem == 0) || (m_newTagItem != item));
if (remove) {
m_tagsList->scrollToItem(item);
if (item->checkState() == Qt::Unchecked) {
item->setCheckState(Qt::Checked);
// Remember the checked item, so that it can be unchecked
// again if the user changes the tag-text.
m_autoCheckedItem = item;
}
removeNewTagItem();
return;
}
}
// There is no tag in the list with the the passed text.
if (m_newTagItem == 0) {
m_newTagItem = new QListWidgetItem(tagText, m_tagsList);
} else {
m_newTagItem->setText(tagText);
}
if (m_autoCheckedItem != 0) {
m_autoCheckedItem->setCheckState(Qt::Unchecked);
m_autoCheckedItem = 0;
}
m_newTagItem->setData(Qt::UserRole, QUrl());
m_newTagItem->setCheckState(Qt::Checked);
m_tagsList->scrollToItem(m_newTagItem);
}
void KEditTagsDialog::slotItemEntered(QListWidgetItem* item)
{
// align the delete-button to stay on the right border
// of the item
const QRect rect = m_tagsList->visualItemRect(item);
const int size = rect.height();
const int x = rect.right() - size;
const int y = rect.top();
m_deleteButton->setGeometry(x, y, size, size);
m_deleteCandidate = item;
m_deleteButtonTimer->start();
}
void KEditTagsDialog::showDeleteButton()
{
m_deleteButton->show();
}
void KEditTagsDialog::deleteTag()
{
Q_ASSERT(m_deleteCandidate != 0);
const QString text = i18nc("@info",
"Should the tag <resource>%1</resource> really be deleted for all files?",
m_deleteCandidate->text());
const QString caption = i18nc("@title", "Delete tag");
const KGuiItem deleteItem(i18nc("@action:button", "Delete"), KIcon("edit-delete"));
const KGuiItem cancelItem(i18nc("@action:button", "Cancel"), KIcon("dialog-cancel"));
if (KMessageBox::warningYesNo(this, text, caption, deleteItem, cancelItem) == KMessageBox::Yes) {
int row = m_tagsList->row( m_deleteCandidate );
const QUrl uri = m_deleteCandidate->data(Qt::UserRole).toUrl();
Nepomuk2::Tag tag(uri);
tag.remove();
delete m_deleteCandidate;
m_deleteCandidate = 0;
// Give the delete Candidate an appropriate value.
// This is required cause when the mouse button doesn't move at all then m_deleteCandidate
// stays 0 and clicking on the delete button executes deleteTag() which then asserts.
if( row == m_tagsList->count() )
row = m_tagsList->count() - 1;
// The deleteCandidate is now the next item in the row
m_deleteCandidate = m_tagsList->item( row );
}
}
static bool tagLabelLessThan( const Nepomuk2::Tag& t1, const Nepomuk2::Tag& t2 )
{
return t1.genericLabel() < t2.genericLabel();
}
void KEditTagsDialog::loadTags()
{
// load all available tags and mark those tags as checked
// that have been passed to the KEditTagsDialog
QList<Nepomuk2::Tag> tags = Nepomuk2::Tag::allTags();
qSort( tags.begin(), tags.end(), tagLabelLessThan );
foreach (const Nepomuk2::Tag& tag, tags) {
const QString label = tag.genericLabel();
QListWidgetItem* item = new QListWidgetItem(label, m_tagsList);
item->setData(Qt::UserRole, tag.uri());
const bool check = m_tags.contains( tag );
item->setCheckState(check ? Qt::Checked : Qt::Unchecked);
}
}
void KEditTagsDialog::removeNewTagItem()
{
if (m_newTagItem != 0) {
const int row = m_tagsList->row(m_newTagItem);
m_tagsList->takeItem(row);
delete m_newTagItem;
m_newTagItem = 0;
}
}
#include "kedittagsdialog_p.moc"
|