+void EditTagsDialog::slotButtonClicked(int button)
+{
+ if (button == KDialog::Ok) {
+ // update m_tags with the checked values, so
+ // that the caller of the EditTagsDialog can
+ // receive the tags by EditTagsDialog::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) {
+ Nepomuk::Tag tag;
+ tag.setLabel(item->data(Qt::UserRole).toString());
+ m_tags.append(tag);
+ }
+ }
+
+ accept();
+ } else {
+ KDialog::slotButtonClicked(button);
+ }
+}
+
+void EditTagsDialog::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) {
+ const QListWidgetItem* item = m_tagsList->item(i);
+ const bool remove = (item->text() == tagText) &&
+ ((m_newTagItem == 0) || (m_newTagItem != item));
+ if (remove) {
+ m_tagsList->scrollToItem(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);
+ }
+ m_newTagItem->setData(Qt::UserRole, tagText);
+ m_newTagItem->setCheckState(Qt::Checked);
+ m_tagsList->scrollToItem(m_newTagItem);
+}
+
+void EditTagsDialog::loadTags()
+{
+ // load all available tags and mark those tags as checked
+ // that have been passed to the EditTagsDialog
+ QList<Nepomuk::Tag> tags = Nepomuk::Tag::allTags();
+ foreach (const Nepomuk::Tag& tag, tags) {
+ const QString label = tag.label();
+
+ QListWidgetItem* item = new QListWidgetItem(label, m_tagsList);
+ item->setData(Qt::UserRole, label);
+
+ bool check = false;
+ foreach (const Nepomuk::Tag& selectedTag, m_tags) {
+ if (selectedTag.label() == label) {
+ check = true;
+ break;
+ }
+ }
+ item->setCheckState(check ? Qt::Checked : Qt::Unchecked);
+ }
+}
+
+void EditTagsDialog::removeNewTagItem()
+{
+ if (m_newTagItem != 0) {
+ const int row = m_tagsList->row(m_newTagItem);
+ m_tagsList->takeItem(row);
+ delete m_newTagItem;
+ m_newTagItem = 0;
+ }
+}
+