7aaa42e2be8f0424cda02e6e04afb0797ff0ad62
[situare] / src / ui / tagsdialog.cpp
1 #include <QDebug>
2 #include <QDialogButtonBox>
3 #include <QGridLayout>
4 #include <QLineEdit>
5 #include <QListWidget>
6 #include <QListWidgetItem>
7 #include <QPushButton>
8 #include <QLabel>
9
10 #include "tagcompletion.h"
11
12 #include "tagsdialog.h"
13
14 TagsDialog::TagsDialog(const QHash<QString, QString> &tags, QWidget *parent)
15     : QDialog(parent)
16 {
17     qDebug() << __PRETTY_FUNCTION__;
18
19     setWindowTitle(tr("Edit tags"));
20
21 #ifdef Q_WS_MAEMO_5
22     setAttribute(Qt::WA_Maemo5StackedWindow);
23     setWindowFlags(Qt::Window);
24 #endif
25
26     QVBoxLayout *mainLayout = new QVBoxLayout;
27     QHBoxLayout *lineEditLayout = new QHBoxLayout;
28     QGridLayout *listLayout = new QGridLayout;
29     QVBoxLayout *buttonLayout = new QVBoxLayout;
30
31     m_addTagEdit = new QLineEdit("");
32     QPushButton *addButton = new QPushButton(tr("Add"));
33     lineEditLayout->addWidget(m_addTagEdit);
34     lineEditLayout->addWidget(addButton);
35
36     QPushButton *deleteButton = new QPushButton(tr("Delete"));
37     QPushButton *okButton = new QPushButton(tr("Update"));
38     buttonLayout->addWidget(deleteButton, 0, Qt::AlignBottom);
39     buttonLayout->addWidget(okButton, 0, Qt::AlignBottom);
40
41     QLabel *yourTagsLabel = new QLabel(tr("Your tags:"));
42     yourTagsLabel->setAlignment(Qt::AlignCenter);
43     QLabel *popularTagsLabel = new QLabel(tr("Popular tags:"));
44     popularTagsLabel->setAlignment(Qt::AlignCenter);
45     m_userTagsView = new QListWidget();
46     m_popularTagsView = new QListWidget();
47     m_popularTagsView->setSelectionMode(QAbstractItemView::NoSelection);
48
49     listLayout->addWidget(yourTagsLabel, 0, 0);
50     listLayout->addWidget(popularTagsLabel, 0, 1);
51     listLayout->addWidget(m_userTagsView, 1, 0);
52     listLayout->addWidget(m_popularTagsView, 1, 1);
53     listLayout->addLayout(buttonLayout, 0, 2, -1, 1);
54
55     mainLayout->addLayout(lineEditLayout);
56     mainLayout->addLayout(listLayout);
57
58     setLayout(mainLayout);
59
60     connect(okButton, SIGNAL(clicked()),
61             this, SLOT(accept()));
62
63     connect(addButton, SIGNAL(clicked()),
64             this, SLOT(addTagToList()));
65
66     connect(deleteButton, SIGNAL(clicked()),
67             this, SLOT(deleteTagFromList()));
68
69     populateUserTags(tags);
70 }
71
72 void TagsDialog::addTagToList()
73 {
74     qDebug() << __PRETTY_FUNCTION__;
75
76     QList<QListWidgetItem *> findItems = m_userTagsView->findItems(m_addTagEdit->text(),
77                                                                Qt::MatchExactly);
78
79     if (findItems.isEmpty()) {
80         m_userTagsView->insertItem(0, m_addTagEdit->text());
81         m_newTags.append(m_addTagEdit->text());
82     }
83
84     m_addTagEdit->setText("");
85 }
86
87 void TagsDialog::deleteTagFromList()
88 {
89     qDebug() << __PRETTY_FUNCTION__;
90
91     QList<QListWidgetItem *> selectedItems = m_userTagsView->selectedItems();
92
93     if (!selectedItems.isEmpty()) {
94         QListWidgetItem *selectedItem = selectedItems.at(0);
95         m_userTagsView->takeItem(m_userTagsView->row(selectedItem));
96
97         if (m_newTags.contains(selectedItem->text()))
98             m_newTags.removeOne(selectedItem->text());
99         else
100             m_removedTags.append(selectedItem->text());
101     }
102 }
103
104 QStringList TagsDialog::newTags()
105 {
106     qDebug() << __PRETTY_FUNCTION__;
107
108     return m_newTags;
109 }
110
111 QStringList TagsDialog::removedTags()
112 {
113     qDebug() << __PRETTY_FUNCTION__;
114
115     return m_removedTags;
116 }
117
118 void TagsDialog::populatePopularTags(const QHash<QString, QString> &popularTags)
119 {
120     qDebug() << __PRETTY_FUNCTION__;
121
122     m_popularTagsView->clear();
123
124     foreach (QString tag, popularTags)
125         m_popularTagsView->addItem(tag);
126 }
127
128 void TagsDialog::populateUserTags(const QHash<QString, QString> &userTags)
129 {
130     qDebug() << __PRETTY_FUNCTION__;
131
132     m_userTagsView->clear();
133
134     foreach (QString tag, m_newTags) {
135         if (!userTags.values().contains(tag))
136             m_userTagsView->addItem(tag);
137     }
138
139     foreach (QString tag, userTags)
140         m_userTagsView->addItem(tag);
141 }