00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "kedittagsdialog_p.h"
00020
00021 #include <kicon.h>
00022 #include <klineedit.h>
00023 #include <klocale.h>
00024 #include <kmessagebox.h>
00025
00026 #include <QEvent>
00027 #include <QHBoxLayout>
00028 #include <QLabel>
00029 #include <QListWidget>
00030 #include <QPushButton>
00031 #include <QTimer>
00032 #include <QVBoxLayout>
00033 #include <QWidget>
00034
00035 KEditTagsDialog::KEditTagsDialog(const QList<Nepomuk::Tag>& tags,
00036 QWidget* parent,
00037 Qt::WFlags flags) :
00038 KDialog(parent, flags),
00039 m_tags(tags),
00040 m_tagsList(0),
00041 m_newTagItem(0),
00042 m_deleteCandidate(0),
00043 m_newTagEdit(0),
00044 m_deleteButtonTimer(0)
00045 {
00046
00047 const QString caption = (tags.count() > 0) ?
00048 i18nc("@title:window", "Change Tags") :
00049 i18nc("@title:window", "Add Tags");
00050 setCaption(caption);
00051 setButtons(KDialog::Ok | KDialog::Cancel);
00052 setDefaultButton(KDialog::Ok);
00053
00054 QWidget* mainWidget = new QWidget(this);
00055 QVBoxLayout* topLayout = new QVBoxLayout(mainWidget);
00056
00057 QLabel* label = new QLabel(i18nc("@label:textbox",
00058 "Configure which tags should "
00059 "be applied."), this);
00060
00061 m_tagsList = new QListWidget(this);
00062 m_tagsList->setMouseTracking(true);
00063 m_tagsList->setSortingEnabled(true);
00064 m_tagsList->setSelectionMode(QAbstractItemView::NoSelection);
00065 m_tagsList->installEventFilter(this);
00066 connect(m_tagsList, SIGNAL(itemEntered(QListWidgetItem*)),
00067 this, SLOT(slotItemEntered(QListWidgetItem*)));
00068 connect(m_tagsList, SIGNAL(itemEntered(QListWidgetItem*)),
00069 this, SLOT(slotItemEntered(QListWidgetItem*)));
00070
00071 QLabel* newTagLabel = new QLabel(i18nc("@label", "Create new tag:"));
00072 m_newTagEdit = new KLineEdit(this);
00073 m_newTagEdit->setClearButtonShown(true);
00074 connect(m_newTagEdit, SIGNAL(textEdited(const QString&)),
00075 this, SLOT(slotTextEdited(const QString&)));
00076
00077 QHBoxLayout* newTagLayout = new QHBoxLayout();
00078 newTagLayout->addWidget(newTagLabel);
00079 newTagLayout->addWidget(m_newTagEdit, 1);
00080
00081 topLayout->addWidget(label);
00082 topLayout->addWidget(m_tagsList);
00083 topLayout->addLayout(newTagLayout);
00084
00085 setMainWidget(mainWidget);
00086
00087 loadTags();
00088
00089
00090
00091 m_deleteButton = new QPushButton(m_tagsList->viewport());
00092 m_deleteButton->setIcon(KIcon(QLatin1String("edit-delete")));
00093 m_deleteButton->setToolTip(i18nc("@info", "Delete tag"));
00094 m_deleteButton->hide();
00095 connect(m_deleteButton, SIGNAL(clicked()), this, SLOT(deleteTag()));
00096
00097 m_deleteButtonTimer = new QTimer(this);
00098 m_deleteButtonTimer->setSingleShot(true);
00099 m_deleteButtonTimer->setInterval(500);
00100 connect(m_deleteButtonTimer, SIGNAL(timeout()), this, SLOT(showDeleteButton()));
00101 }
00102
00103 KEditTagsDialog::~KEditTagsDialog()
00104 {
00105 }
00106
00107 QList<Nepomuk::Tag> KEditTagsDialog::tags() const
00108 {
00109 return m_tags;
00110 }
00111
00112 bool KEditTagsDialog::eventFilter(QObject* watched, QEvent* event)
00113 {
00114 if ((watched == m_tagsList) && (event->type() == QEvent::Leave)) {
00115 m_deleteButtonTimer->stop();
00116 m_deleteButton->hide();
00117 }
00118 return KDialog::eventFilter(watched, event);
00119 }
00120
00121 void KEditTagsDialog::slotButtonClicked(int button)
00122 {
00123 if (button == KDialog::Ok) {
00124
00125
00126
00127 m_tags.clear();
00128
00129 const int count = m_tagsList->count();
00130 for (int i = 0; i < count; ++i) {
00131 QListWidgetItem* item = m_tagsList->item(i);
00132 if (item->checkState() == Qt::Checked) {
00133 const QString label = item->data(Qt::UserRole).toString();
00134 Nepomuk::Tag tag(label);
00135 tag.setLabel(label);
00136 m_tags.append(tag);
00137 }
00138 }
00139
00140 accept();
00141 } else {
00142 KDialog::slotButtonClicked(button);
00143 }
00144 }
00145
00146 void KEditTagsDialog::slotTextEdited(const QString& text)
00147 {
00148
00149
00150
00151 const QString tagText = text.simplified();
00152 if (tagText.isEmpty()) {
00153 removeNewTagItem();
00154 return;
00155 }
00156
00157
00158
00159 const int count = m_tagsList->count();
00160 for (int i = 0; i < count; ++i) {
00161 const QListWidgetItem* item = m_tagsList->item(i);
00162 const bool remove = (item->text() == tagText) &&
00163 ((m_newTagItem == 0) || (m_newTagItem != item));
00164 if (remove) {
00165 m_tagsList->scrollToItem(item);
00166 removeNewTagItem();
00167 return;
00168 }
00169 }
00170
00171
00172 if (m_newTagItem == 0) {
00173 m_newTagItem = new QListWidgetItem(tagText, m_tagsList);
00174 } else {
00175 m_newTagItem->setText(tagText);
00176 }
00177 m_newTagItem->setData(Qt::UserRole, tagText);
00178 m_newTagItem->setCheckState(Qt::Checked);
00179 m_tagsList->scrollToItem(m_newTagItem);
00180 }
00181
00182 void KEditTagsDialog::slotItemEntered(QListWidgetItem* item)
00183 {
00184
00185
00186 const QRect rect = m_tagsList->visualItemRect(item);
00187 const int size = rect.height();
00188 const int x = rect.right() - size;
00189 const int y = rect.top();
00190 m_deleteButton->move(x, y);
00191 m_deleteButton->resize(size, size);
00192
00193 m_deleteCandidate = item;
00194 m_deleteButtonTimer->start();
00195 }
00196
00197 void KEditTagsDialog::showDeleteButton()
00198 {
00199 m_deleteButton->show();
00200 }
00201
00202 void KEditTagsDialog::deleteTag()
00203 {
00204 Q_ASSERT(m_deleteCandidate != 0);
00205 const QString text = i18nc("@info",
00206 "Should the tag <resource>%1</resource> really be deleted for all files?",
00207 m_deleteCandidate->text());
00208 const QString caption = i18nc("@title", "Delete tag");
00209 const KGuiItem deleteItem(i18nc("@action:button", "Delete"), KIcon(QLatin1String("edit-delete")));
00210 const KGuiItem cancelItem(i18nc("@action:button", "Cancel"), KIcon(QLatin1String("dialog-cancel")));
00211 if (KMessageBox::warningYesNo(this, text, caption, deleteItem, cancelItem) == KMessageBox::Yes) {
00212 const QString label = m_deleteCandidate->data(Qt::UserRole).toString();
00213 Nepomuk::Tag tag(label);
00214 tag.remove();
00215
00216
00217 for (int i = m_tagsList->count() - 1; i >= 0; --i) {
00218 QListWidgetItem* item = m_tagsList->takeItem(i);
00219 delete item;
00220 }
00221 loadTags();
00222 }
00223 }
00224
00225 void KEditTagsDialog::loadTags()
00226 {
00227
00228
00229 const QList<Nepomuk::Tag> tags = Nepomuk::Tag::allTags();
00230 foreach (const Nepomuk::Tag& tag, tags) {
00231 const QString label = tag.label();
00232
00233 QListWidgetItem* item = new QListWidgetItem(label, m_tagsList);
00234 item->setData(Qt::UserRole, label);
00235
00236 bool check = false;
00237 foreach (const Nepomuk::Tag& selectedTag, m_tags) {
00238 if (selectedTag.label() == label) {
00239 check = true;
00240 break;
00241 }
00242 }
00243 item->setCheckState(check ? Qt::Checked : Qt::Unchecked);
00244 }
00245 }
00246
00247 void KEditTagsDialog::removeNewTagItem()
00248 {
00249 if (m_newTagItem != 0) {
00250 const int row = m_tagsList->row(m_newTagItem);
00251 m_tagsList->takeItem(row);
00252 delete m_newTagItem;
00253 m_newTagItem = 0;
00254 }
00255 }
00256
00257 #include "kedittagsdialog_p.moc"