19 #include "kedittagsdialog_p.h"
22 #include <klineedit.h>
23 #include <klocalizedstring.h>
24 #include <kmessagebox.h>
27 #include <QHBoxLayout>
29 #include <QListWidget>
30 #include <QPushButton>
32 #include <QVBoxLayout>
35 KEditTagsDialog::KEditTagsDialog(
const QVector<Nepomuk2::Tag>& tags,
37 Qt::WindowFlags flags) :
38 KDialog(parent, flags),
44 m_deleteButtonTimer(0)
47 const QString caption = ( tags.count() > 0 ) ?
48 i18nc(
"@title:window",
"Change Tags" ) :
49 i18nc(
"@title:window",
"Add Tags" );
50 setCaption( caption );
51 setButtons( KDialog::Ok | KDialog::Cancel );
52 setDefaultButton( KDialog::Ok );
54 QWidget *mainWidget =
new QWidget(
this );
55 QVBoxLayout *topLayout =
new QVBoxLayout( mainWidget );
57 QLabel *label =
new QLabel( i18nc(
"@label:textbox",
58 "Configure which tags should "
59 "be applied." ),
this );
61 m_tagsList =
new QListWidget(
this );
62 m_tagsList->setMouseTracking(
true );
63 m_tagsList->setSortingEnabled(
true );
64 m_tagsList->setSelectionMode( QAbstractItemView::NoSelection );
65 m_tagsList->installEventFilter(
this );
66 connect( m_tagsList, SIGNAL(itemEntered(QListWidgetItem*)),
67 this, SLOT(slotItemEntered(QListWidgetItem*)) );
68 connect( m_tagsList, SIGNAL(itemEntered(QListWidgetItem*)),
69 this, SLOT(slotItemEntered(QListWidgetItem*)) );
71 QLabel *newTagLabel =
new QLabel( i18nc(
"@label",
"Create new tag:" ) );
72 m_newTagEdit =
new KLineEdit(
this );
73 m_newTagEdit->setClearButtonShown(
true );
74 connect( m_newTagEdit, SIGNAL(textEdited(QString)),
75 this, SLOT(slotTextEdited(QString)) );
77 QHBoxLayout *newTagLayout =
new QHBoxLayout();
78 newTagLayout->addWidget( newTagLabel );
79 newTagLayout->addWidget( m_newTagEdit, 1 );
81 topLayout->addWidget( label );
82 topLayout->addWidget( m_tagsList );
83 topLayout->addLayout( newTagLayout );
85 setMainWidget( mainWidget );
91 m_deleteButton =
new QPushButton( m_tagsList->viewport() );
92 m_deleteButton->setIcon( KIcon( QLatin1String(
"edit-delete" ) ) );
93 m_deleteButton->setToolTip( i18nc(
"@info",
"Delete tag" ) );
94 m_deleteButton->hide();
95 connect( m_deleteButton, SIGNAL(clicked()),
this, SLOT(deleteTag()) );
97 m_deleteButtonTimer =
new QTimer(
this );
98 m_deleteButtonTimer->setSingleShot(
true );
99 m_deleteButtonTimer->setInterval( 500 );
100 connect( m_deleteButtonTimer, SIGNAL(timeout()),
this, SLOT(showDeleteButton()) );
104 KEditTagsDialog::~KEditTagsDialog()
109 void KEditTagsDialog::readConfig()
111 KConfigGroup group( KGlobal::config(),
"KEditTagsDialog" );
112 const QSize sizeDialog = group.readEntry(
"Size", QSize(500,400) );
113 if ( sizeDialog.isValid() ) {
114 resize( sizeDialog );
118 void KEditTagsDialog::writeConfig()
120 KConfigGroup group( KGlobal::config(),
"KEditTagsDialog" );
121 group.writeEntry(
"Size", size() );
124 QVector<Nepomuk2::Tag> KEditTagsDialog::tags()
const
129 bool KEditTagsDialog::eventFilter(QObject* watched, QEvent* event)
131 if ( ( watched == m_tagsList ) && ( event->type() == QEvent::Leave ) ) {
132 m_deleteButtonTimer->stop();
133 m_deleteButton->hide();
135 return KDialog::eventFilter( watched, event );
138 void KEditTagsDialog::slotButtonClicked(
int button)
140 if ( button == KDialog::Ok ) {
146 const int count = m_tagsList->count();
147 for (
int i = 0; i < count; ++i ) {
148 QListWidgetItem* item = m_tagsList->item( i );
149 if ( item->checkState() == Qt::Checked ) {
150 const QString label = item->data( Qt::UserRole ).toString();
151 const QString uri = item->data(UrlTag).toString();
153 Nepomuk2::Tag tag( label );
154 tag.setLabel( label );
155 m_tags.append( tag );
157 Nepomuk2::Tag tag( uri );
158 m_tags.append( tag );
165 KDialog::slotButtonClicked( button );
169 void KEditTagsDialog::slotTextEdited(
const QString& text)
174 const QString tagText = text.simplified();
175 if ( tagText.isEmpty() ) {
182 const int count = m_tagsList->count();
183 for (
int i = 0; i < count; ++i ) {
184 const QListWidgetItem* item = m_tagsList->item( i );
185 const bool remove = ( item->text() == tagText ) &&
186 ( ( m_newTagItem == 0 ) || ( m_newTagItem != item ) );
188 m_tagsList->scrollToItem( item );
195 if ( m_newTagItem == 0 ) {
196 m_newTagItem =
new QListWidgetItem( tagText, m_tagsList );
198 m_newTagItem->setText( tagText );
200 m_newTagItem->setData( Qt::UserRole, tagText );
201 m_newTagItem->setCheckState( Qt::Checked );
202 m_tagsList->scrollToItem( m_newTagItem );
205 void KEditTagsDialog::slotItemEntered(QListWidgetItem* item)
209 const QRect rect = m_tagsList->visualItemRect( item );
210 const int size = rect.height();
211 const int x = rect.right() - size;
212 const int y = rect.top();
213 m_deleteButton->move( x, y );
214 m_deleteButton->resize( size, size );
216 m_deleteCandidate = item;
217 m_deleteButtonTimer->start();
220 void KEditTagsDialog::showDeleteButton()
222 m_deleteButton->show();
225 void KEditTagsDialog::deleteTag()
227 Q_ASSERT( m_deleteCandidate != 0 );
228 const QString text = i18nc(
"@info",
229 "Should the tag <resource>%1</resource> really be deleted for all files?",
230 m_deleteCandidate->text() );
231 const QString caption = i18nc(
"@title",
"Delete tag" );
232 const KGuiItem deleteItem( i18nc(
"@action:button",
"Delete" ), KIcon( QLatin1String(
"edit-delete" ) ) );
233 const KGuiItem cancelItem( i18nc(
"@action:button",
"Cancel" ), KIcon( QLatin1String(
"dialog-cancel" ) ) );
234 if ( KMessageBox::warningYesNo(
this, text, caption, deleteItem, cancelItem ) == KMessageBox::Yes ) {
235 const QString label = m_deleteCandidate->data( Qt::UserRole ).toString();
236 Nepomuk2::Tag tag( label );
240 for (
int i = m_tagsList->count() - 1; i >= 0; --i ) {
241 QListWidgetItem* item = m_tagsList->takeItem( i );
248 void KEditTagsDialog::loadTags()
252 foreach (
const Nepomuk2::Tag& tag, Nepomuk2::Tag::allTags() ) {
253 const QString label = tag.label();
255 QListWidgetItem *item =
new QListWidgetItem( label, m_tagsList );
256 item->setData( Qt::UserRole, label );
257 item->setData( UrlTag, tag.uri().toString());
260 foreach (
const Nepomuk2::Tag& selectedTag, m_tags ) {
261 if ( selectedTag.label() == label ) {
266 item->setCheckState( check ? Qt::Checked : Qt::Unchecked );
270 void KEditTagsDialog::removeNewTagItem()
272 if ( m_newTagItem != 0 ) {
273 const int row = m_tagsList->row( m_newTagItem );
274 m_tagsList->takeItem( row );
280 #include "moc_kedittagsdialog_p.cpp"