• Skip to content
  • Skip to link menu
KDE 4.7 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • KDE Home
  • Contact Us
 

akonadi/contact

kedittagsdialog.cpp
00001 /*****************************************************************************
00002  * Copyright (C) 2009 by Peter Penz <peter.penz@gmx.at>                      *
00003  *                                                                           *
00004  * This library is free software; you can redistribute it and/or             *
00005  * modify it under the terms of the GNU Library General Public               *
00006  * License version 2 as published by the Free Software Foundation.           *
00007  *                                                                           *
00008  * This library is distributed in the hope that it will be useful,           *
00009  * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
00011  * Library General Public License for more details.                          *
00012  *                                                                           *
00013  * You should have received a copy of the GNU Library General Public License *
00014  * along with this library; see the file COPYING.LIB.  If not, write to      *
00015  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,      *
00016  * Boston, MA 02110-1301, USA.                                               *
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 QVector<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     // create the delete button, which is shown when
00090     // hovering the items
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 QVector<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         // update m_tags with the checked values, so
00125         // that the caller of the KEditTagsDialog can
00126         // receive the tags by KEditTagsDialog::tags()
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     // Remove unnecessary spaces from a new tag is
00149     // mandatory, as the user cannot see the difference
00150     // between a tag "Test" and "Test ".
00151     const QString tagText = text.simplified();
00152     if (tagText.isEmpty()) {
00153         removeNewTagItem();
00154         return;
00155     }
00156 
00157     // Check whether the new tag already exists. If this
00158     // is the case, remove the new tag item.
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     // There is no tag in the list with the the passed text.
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     // align the delete-button to stay on the right border
00185     // of the item
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         // clear list and reload it
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     // load all available tags and mark those tags as checked
00228     // that have been passed to the KEditTagsDialog
00229     foreach (const Nepomuk::Tag& tag, Nepomuk::Tag::allTags()) {
00230         const QString label = tag.label();
00231 
00232         QListWidgetItem* item = new QListWidgetItem(label, m_tagsList);
00233         item->setData(Qt::UserRole, label);
00234 
00235         bool check = false;
00236         foreach (const Nepomuk::Tag& selectedTag, m_tags) {
00237             if (selectedTag.label() == label) {
00238                 check = true;
00239                 break;
00240             }
00241         }
00242         item->setCheckState(check ? Qt::Checked : Qt::Unchecked);
00243     }
00244 }
00245 
00246 void KEditTagsDialog::removeNewTagItem()
00247 {
00248     if (m_newTagItem != 0) {
00249         const int row = m_tagsList->row(m_newTagItem);
00250         m_tagsList->takeItem(row);
00251         delete m_newTagItem;
00252         m_newTagItem = 0;
00253     }
00254 }
00255 
00256 #include "kedittagsdialog_p.moc"

akonadi/contact

Skip menu "akonadi/contact"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Related Pages

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.7.5
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal