Debian Codegraph @

src/collectiontree.cpp

raw symbols SCIP
1 /*
2 Copyright (C) 2005-2014 Mario Stephan <[email protected]>
3
4 This library is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published
6 by the Free Software Foundation; either version 2.1 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "collectiontree.h"
19 #include "collectiontreeitem.h"
20 #include <QApplication>
21 #include <QHeaderView>
22 #include <QMenu>
23 #include <QMessageBox>
24 #include <QMouseEvent>
25 #include <QtGui>
26 #if QT_VERSION >= 0x050000
27 #include <QtConcurrent/QtConcurrent>
28 #else
29 #include <QtConcurrentRun>
30 #endif
31
32 struct CollectionTreePrivate {
33 CollectionDB* database;
34 QList<Track*> tracks;
35 QString filterString;
36 QMutex mutex;
37 };
38
39 CollectionTree::CollectionTree(QWidget* parent)
40 : QTreeWidget(parent)
41 , p(new CollectionTreePrivate)
42 {
43
44 p->database = new CollectionDB();
45 p->database->executeSql("PRAGMA synchronous = OFF;");
46
47 setSelectionMode(QAbstractItemView::ExtendedSelection);
48 setDragEnabled(true);
49 setRootIsDecorated(false);
50 setAcceptDrops(false);
51 setRootIsDecorated(true);
52
53 setAttribute(Qt::WA_MacShowFocusRect, false);
54
55 QStringList headers;
56 headers << tr("Artist");
57
58 QTreeWidgetItem* headeritem = new QTreeWidgetItem(headers);
59 headeritem->setTextAlignment(0, Qt::AlignLeft);
60
61 setHeaderItem(headeritem);
62 setHeaderLabels(headers);
63 header()->resizeSection(0, this->width() - 50);
64 header()->setMinimumHeight(18);
65
66 connect(this, SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)),
67 this, SLOT(on_currentItemChanged(QTreeWidgetItem*)));
68
69 connect(this, SIGNAL(itemExpanded(QTreeWidgetItem*)),
70 this, SLOT(on_itemExpanded(QTreeWidgetItem*)));
71 }
72
73 CollectionTree::~CollectionTree()
74 {
75 p->mutex.tryLock(2000);
76 delete p;
77 }
78
79 void CollectionTree::createTrunk()
80 {
81 //qDebug() << Q_FUNC_INFO;
82 CollectionTreeItem* item = nullptr;
83
84 clear();
85
86 //add "ALL" node and select it
87 int countAll = p->database->getCount();
88 if (countAll < 1000 && countAll > 0) {
89 item = new CollectionTreeItem(this);
90 setCurrentItem(item);
91 }
92
93 QList<QStringList> tags;
94
95 //Retrieve root nodes from database
96 switch (treeMode) {
97 case MODEGENRE:
98 tags = p->database->selectGenres();
99 if (item)
100 item->setGenre(QString::null);
101 foreach (QStringList tag, tags) {
102 item = new CollectionTreeItem(this);
103 item->setGenre(tag[0]);
104 }
105 headerItem()->setText(0, QString(" %1 (%2)").arg(tr("Genre")).arg(tags.count()));
106 break;
107 break;
108 case MODEYEAR:
109 tags = p->database->selectYears();
110 if (item)
111 item->setYear(QString::null);
112 foreach (QStringList tag, tags) {
113 item = new CollectionTreeItem(this);
114 item->setYear(tag[0]);
115 }
116 headerItem()->setText(0, QString(" %1 (%2)").arg(tr("Year")).arg(tags.count()));
117 break;
118 break;
119 default:
120 tags = p->database->selectArtists();
121 if (item)
122 item->setArtist(QString::null);
123 foreach (QStringList tag, tags) {
124 item = new CollectionTreeItem(this);
125 item->setArtist(tag[0]);
126 }
127 headerItem()->setText(0, QString(" %1 (%2)").arg(tr("Artist")).arg(tags.count()));
128 break;
129 }
130 }
131
132 void CollectionTree::on_itemExpanded(QTreeWidgetItem* item)
133 {
134 qDebug() << Q_FUNC_INFO << endl;
135 if (!item)
136 return;
137
138 if (item->childCount() == 0) {
139
140 QList<QStringList> tags;
141 CollectionTreeItem* collItem = static_cast<CollectionTreeItem*>(item);
142
143 QTreeWidgetItem::ChildIndicatorPolicy policy = QTreeWidgetItem::ShowIndicator;
144
145 //Retrieve children from database
146 if (collItem->artist() != QString::null) {
147 tags = p->database->selectAlbums(collItem->year(), collItem->genre(), collItem->artist());
148 policy = QTreeWidgetItem::DontShowIndicator;
149 } else {
150 tags = p->database->selectArtists(collItem->year(), collItem->genre());
151 }
152
153 foreach (QStringList tag, tags) {
154 CollectionTreeItem* child = new CollectionTreeItem(item);
155
156 child->setYear(collItem->year());
157 child->setGenre(collItem->genre());
158 child->setArtist(collItem->artist());
159
160 if (collItem->artist() == QString::null)
161 child->setArtist(tag[0]);
162 else
163 child->setAlbum(tag[0]);
164
165 child->setChildIndicatorPolicy(policy);
166 item->addChild(child);
167 }
168 }
169 }
170
171 void CollectionTree::triggerRandomSelection()
172 {
173 QFuture<void> future = QtConcurrent::run(this, &CollectionTree::asynchronTriggerRandomSelection);
174 }
175
176 void CollectionTree::asynchronTriggerRandomSelection()
177 {
178 QMutexLocker locker(&p->mutex);
179 p->tracks.clear();
180
181 // init qrand
182 QTime time = QTime::currentTime();
183 qsrand((uint)time.msec());
184
185 for (int i = 0; i < 15; i++) {
186
187 Track* track;
188 int r = 0;
189
190 do {
191 QStringList tag = p->database->getRandomEntry();
192 track = new Track(tag);
193 r++;
194 } while (track->prettyLength() == "?" && r < 3);
195
196 p->tracks.append(track);
197 }
198
199 qDebug() << Q_FUNC_INFO << p->tracks.count();
200 emit selectionChanged(p->tracks);
201 }
202
203 void CollectionTree::on_currentItemChanged(QTreeWidgetItem* item)
204 {
205 QFuture<void> future = QtConcurrent::run(this, &CollectionTree::asynchronCurrentItemChanged, item);
206 }
207
208 void CollectionTree::asynchronCurrentItemChanged(QTreeWidgetItem* item)
209 {
210 QMutexLocker locker(&p->mutex);
211
212 if (!item)
213 return;
214
215 QList<QStringList> tags;
216
217 CollectionTreeItem* collItem = static_cast<CollectionTreeItem*>(item);
218 qDebug() << Q_FUNC_INFO << "Artist: " << collItem->artist() << " Album: " << collItem->album() << endl;
219
220 //Retrieve songs from database
221 tags = p->database->selectTracks(collItem->year(), collItem->genre(), collItem->artist(), collItem->album());
222
223 //Show songs in parent's tracklist
224 p->tracks.clear();
225
226 qDebug() << Q_FUNC_INFO << "Song count: " << tags.count();
227
228 //add tags to this track list
229 foreach (QStringList tag, tags) {
230 //qDebug() << Q_FUNC_INFO <<": is playlistitem; tags:"<<tags;
231 p->tracks.append(new Track(tag));
232 }
233
234 emit selectionChanged(p->tracks);
235
236 //qDebug() << Q_FUNC_INFO << "[End]" << endl;
237 }
238
239 QString CollectionTree::filter()
240 {
241 //ToDo: do we need this private variable
242 return p->filterString;
243 }
244
245 void CollectionTree::setFilter(QString filter)
246 {
247 p->filterString = filter;
248 p->database->setFilterString(filter);
249 }
250
251 void CollectionTree::mousePressEvent(QMouseEvent* e)
252 {
253 if (e->button() == Qt::LeftButton)
254 startPos = e->pos();
255
256 QTreeWidget::mousePressEvent(e);
257
258 if (e->button() == Qt::RightButton)
259 showContextMenu(currentItem(), currentColumn());
260 }
261
262 void CollectionTree::mouseMoveEvent(QMouseEvent* event)
263 {
264
265 if (event->buttons() & Qt::LeftButton) {
266 int distance = (event->pos() - startPos).manhattanLength();
267 if (distance >= QApplication::startDragDistance()) {
268 performDrag();
269 }
270 }
271 }
272
273 void CollectionTree::performDrag()
274 {
275
276 QByteArray itemData;
277 QDataStream dataStream(&itemData, QIODevice::WriteOnly);
278 QVector<QStringList> tags;
279 QPixmap cover;
280 int i = 0;
281
282 //iterate selected items
283 QListIterator<Track*> it(p->tracks);
284 while (it.hasNext()) {
285 Track* track = dynamic_cast<Track*>(it.next());
286 if (track->isValid()) {
287 qDebug() << Q_FUNC_INFO << ": send Data:" << track->url();
288 QStringList tag = track->tagList();
289 tags << tag;
290 if (i == 0)
291 cover = QPixmap::fromImage(track->coverImage());
292 i++;
293 }
294 }
295
296 dataStream << tags;
297
298 QMimeData* mimeData = new QMimeData;
299 mimeData->setData("text/playlistitem", itemData);
300
301 QDrag* drag = new QDrag(this);
302 drag->setMimeData(mimeData);
303
304 if (!cover.isNull())
305 drag->setPixmap(cover.scaled(50, 50));
306
307 if (drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction) == Qt::MoveAction) {
308 }
309 }
310
311 void CollectionTree::showContextMenu(QTreeWidgetItem* item, int col)
312 {
313
314 Q_UNUSED(col);
315
316 //ToDo: create popup before first use
317 enum Id { LOAD1,
318 LOAD2 };
319
320 if (item == NULL)
321 return;
322
323 QMenu popup(this);
324
325 popup.setTitle(item->text(0));
326 popup.addAction(style()->standardPixmap(QStyle::SP_MediaPlay), tr("Add to PlayList&1"),
327 this, SLOT(onLoad1Triggered()), Qt::Key_1); //, LOAD1
328 popup.addAction(style()->standardPixmap(QStyle::SP_MediaPlay), tr("Add to PlayList&2"),
329 this, SLOT(onLoad2Triggered()), Qt::Key_2); //, LOAD2
330 popup.addSeparator();
331 popup.addAction(style()->standardPixmap(QStyle::SP_BrowserReload), tr("Re-scan collection"),
332 this, SLOT(onRescanTriggered()), Qt::Key_R);
333
334 popup.exec(QCursor::pos());
335 }
336
337 void CollectionTree::onLoad1Triggered()
338 {
339 Q_EMIT wantLoad(p->tracks, "Left");
340 }
341
342 void CollectionTree::onLoad2Triggered()
343 {
344 Q_EMIT wantLoad(p->tracks, "Right");
345 }
346
347 void CollectionTree::onRescanTriggered()
348 {
349 Q_EMIT rescan();
350 }
351
352 void CollectionTree::showTrackInfo(Track* mb)
353 {
354 const QString body = "<tr><td>%1</td><td>%2</td></tr>";
355
356 QString
357 str
358 = "<html><body><table STYLE=\"border-collapse: collapse\"> width=\"100%\" border=\"1\">";
359 str += body.arg(tr("Title"), mb->title());
360 str += body.arg(tr("Artist"), mb->artist());
361 str += body.arg(tr("Album"), mb->album());
362 str += body.arg(tr("Genre"), mb->genre());
363 str += body.arg(tr("Year"), mb->year());
364 str += body.arg(tr("Location"), mb->url().toString());
365 str += "</table></body></html>";
366
367 QMessageBox::information(0, tr("Meta Information"), str);
368 }
369
370 void CollectionTree::resizeEvent(QResizeEvent*)
371 {
372 //QRect rec = p->collectiontree->geometry();
373 header()->resizeSection(0, this->width() - 100);
374 }