-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathListForm.cpp
211 lines (196 loc) · 6.51 KB
/
ListForm.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include "ListForm.h"
#include "ui_ListForm.h"
#include <QDir>
#include <QList>
#include <QMessageBox>
#include <QXmlStreamReader>
#include <QColor>
#include <QBrush>
#include <QDebug>
#include "texts.h"
static const QColor bckgrcol0(0, 102, 153);
static const QColor bckgrcol1(51, 204, 204);
static const QColor bckgrhdr0(0, 51, 102);
static const QColor bckgrhdr1(0, 102, 102);
ListForm::ListForm(QWidget *parent) :
QWidget(parent),
ui(new Ui::ListForm)
{
ui->setupUi(this);
QTreeWidgetItem* hdr;
hdr = new QTreeWidgetItem;
hdr->setText(0, "Listanév");
hdr->setText(1, "Tartalom");
ui->tree->setHeaderItem(hdr);
ui->tree->setHeaderHidden(false);
ui->btnDelete->setEnabled(false);
ui->btnPlay->setEnabled(false);
loadList();
}
ListForm::~ListForm()
{
delete ui;
}
void ListForm::loadList()
{
QDir dir;
dir.setPath(strPlaylistPath);
QStringList filter;
filter << tr("*.%1").arg(strPlaylistExt);
QStringList entries = dir.entryList(filter, QDir::Files, QDir::Name);
QTreeWidgetItem* root = new QTreeWidgetItem;
root->setText(0, "Lejátszási listák");
root->setBackground(0, QBrush(bckgrhdr1));
root->setTextColor(0, Qt::white);
foreach (QString fname, entries)
{
QString label = fname.left(fname.lastIndexOf(tr(".%1").arg(strPlaylistExt)));
fname.prepend(tr("%1/").arg(strPlaylistPath));
QTreeWidgetItem* item = new QTreeWidgetItem(root);
item->setText(0, label);
item->setBackgroundColor(0, bckgrcol0);
item->setTextColor(0, Qt::white);
item->setData(0, Qt::UserRole, label);
loadFile(item, fname);
}
ui->tree->addTopLevelItem(root);
ui->tree->adjustSize();
}
void ListForm::loadFile(QTreeWidgetItem *parent, QString fname)
{
QList<QTreeWidgetItem*> list;
QTreeWidgetItem* child;
QFile file(fname);
if (!file.open((QIODevice::ReadOnly)))
{
QMessageBox::critical(this, strfileerror, QString(stropenrror).arg(fname));
return;
}
QXmlStreamReader xml;
xml.setDevice(&file);
if (xml.readNextStartElement()) {
if (xml.name() != strplaylistdocname || xml.attributes().value(strversion) != strplaylistversion)
{
QMessageBox::critical(this, strxmlerror, QString(strxmldocerror).arg(fname));
file.close();
return;
}
}
if (xml.readNextStartElement() && xml.name() == strdatabase)
xml.skipCurrentElement();
if (!xml.readNextStartElement() || xml.name() != strlist)
{
QMessageBox::critical(this, strxmlerror, QString(strnolisterror).arg(fname).arg(xml.name()));
file.close();
return;
}
while (xml.readNextStartElement() && xml.name() == stritem)
{
child = new QTreeWidgetItem(parent);
child->setBackgroundColor(0, bckgrcol1);
QTreeWidgetItem* grandchild1 = new QTreeWidgetItem;
QTreeWidgetItem* grandchild2 = new QTreeWidgetItem;
QTreeWidgetItem* grandchild3 = new QTreeWidgetItem;
if (xml.readNextStartElement() && xml.name() == strsource)
{
child->setText(0, xml.attributes().value(strword).toString());
grandchild2->setText(0, "példamondat:");
grandchild2->setText(1, xml.attributes().value(strsentence).toString());
}
xml.skipCurrentElement();
if (xml.readNextStartElement() && xml.name() == strtarget)
{
grandchild1->setText(0, "jelentés:");
grandchild1->setText(1, xml.attributes().value(strword).toString());
grandchild3->setText(0, "fordítás:");
grandchild3->setText(1, xml.attributes().value(strsentence).toString());
}
child->addChild(grandchild1);
child->addChild(grandchild2);
child->addChild(grandchild3);
xml.skipCurrentElement();
list.append(child);
xml.skipCurrentElement();
}
file.close();
parent->addChildren(list);
}
void ListForm::onPlay(QString fname)
{
QString label = fname.mid(strlen(strPlaylistPath)+1);
label = label.left(label.lastIndexOf(strPlaylistExt)-1);
QTreeWidgetItem* item = new QTreeWidgetItem;
item->setText(0, label);
item->setData(0, Qt::UserRole, label);
item->setBackgroundColor(0, bckgrcol0);
item->setTextColor(0, Qt::white);
loadFile(item, fname);
QTreeWidgetItem* root = ui->tree->topLevelItem(0);
for (int i = 0; i < root->childCount(); i++)
{
QTreeWidgetItem* child = root->child(i);
int c = QString::compare(label, child->text(0), Qt::CaseInsensitive);
if (c <= 0)
{
root->insertChild(i, item); // first insert, i is fine
if (c == 0) // same listname, remove
root->removeChild(child);
return;
}
}
root->addChild(item);
}
void ListForm::onFinished()
{
ui->tree->setEnabled(true);
on_tree_itemSelectionChanged(); // for restroring the proper state of buttons
}
void ListForm::on_btnExit_clicked()
{
QApplication::exit();
}
void ListForm::on_tree_itemSelectionChanged()
{
QList<QTreeWidgetItem*> list = ui->tree->selectedItems();
QTreeWidgetItem* item = 0;
if (list.size())
{
item = ui->tree->selectedItems().at(0);
while (item && item->data(0, Qt::UserRole).toString().isEmpty())
item = item->parent();
}
ui->edtListName->setText(item ? item->text(0) : "");
ui->btnPlay->setEnabled(item != 0);
ui->btnDelete->setEnabled(item != 0);
}
void ListForm::on_btnPlay_clicked()
{
if (!ui->edtListName->text().isEmpty())
{
ui->tree->setEnabled(false);
ui->btnDelete->setEnabled(false);
ui->btnPlay->setEnabled(false);
emit play(tr("%1/%2.%3").arg(strPlaylistPath).arg(ui->edtListName->text()).arg(strPlaylistExt));
}
}
void ListForm::on_btnDelete_clicked()
{
QString fname = ui->edtListName->text();
if (!fname.isEmpty() &&
QMessageBox::question(this, strdeleteplaylist, QString(strdeleteplaylistquestion).arg(fname),
QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes)
{
fname.prepend(QString("%1/").arg(strPlaylistPath)).append(QString(".%1").arg(strPlaylistExt));
QFile::remove(fname);
QTreeWidgetItem* item = ui->tree->selectedItems().at(0);
while (item && item->data(0, Qt::UserRole).toString().isEmpty())
{
item = item->parent();
}
if (item && item->parent())
{
item->parent()->removeChild(item);
delete item;
}
}
}