|
17 | 17 | #include "widget/wlibrarysidebar.h"
|
18 | 18 | #include "widget/wtracktableview.h"
|
19 | 19 |
|
| 20 | +namespace { |
| 21 | +constexpr int kNumDirectHistoryEntries = 5; |
| 22 | +} |
| 23 | + |
20 | 24 | SetlogFeature::SetlogFeature(
|
21 | 25 | Library* pLibrary,
|
22 | 26 | UserSettingsPointer pConfig)
|
@@ -101,8 +105,13 @@ void SetlogFeature::onRightClickChild(const QPoint& globalPos, QModelIndex index
|
101 | 105 | //Save the model index so we can get it in the action slots...
|
102 | 106 | m_lastRightClickedIndex = index;
|
103 | 107 |
|
104 |
| - QString playlistName = index.data().toString(); |
105 |
| - int playlistId = m_playlistDao.getPlaylistIdFromName(playlistName); |
| 108 | + // QString playlistName = index.data().toString(); |
| 109 | + // int playlistId = m_playlistDao.getPlaylistIdFromName(playlistName); |
| 110 | + int playlistId = index.data(TreeItemModel::kDataRole).toInt(); |
| 111 | + // not a real entry |
| 112 | + if (playlistId == -1) { |
| 113 | + return; |
| 114 | + } |
106 | 115 |
|
107 | 116 | bool locked = m_playlistDao.isPlaylistLocked(playlistId);
|
108 | 117 | m_pDeletePlaylistAction->setEnabled(!locked);
|
@@ -137,34 +146,95 @@ void SetlogFeature::onRightClickChild(const QPoint& globalPos, QModelIndex index
|
137 | 146 | }
|
138 | 147 |
|
139 | 148 | QList<BasePlaylistFeature::IdAndLabel> SetlogFeature::createPlaylistLabels() {
|
140 |
| - QList<BasePlaylistFeature::IdAndLabel> playlistLabels; |
| 149 | + // QList<BasePlaylistFeature::IdAndLabel> playlistLabels; |
| 150 | + return QList<BasePlaylistFeature::IdAndLabel>(); |
| 151 | +} |
| 152 | + |
| 153 | +/** |
| 154 | + * Purpose: When inserting or removing playlists, |
| 155 | + * we require the sidebar model not to reset. |
| 156 | + * This method queries the database and does dynamic insertion |
| 157 | + * Use a custom model in the history for grouping by year |
| 158 | + */ |
| 159 | +QModelIndex SetlogFeature::constructChildModel(int selectedId) { |
| 160 | + int selected_row = -1; |
| 161 | + |
141 | 162 | // Setup the sidebar playlist model
|
142 | 163 | QSqlTableModel playlistTableModel(this, m_pLibrary->trackCollections()->internalCollection()->database());
|
143 | 164 | playlistTableModel.setTable("Playlists");
|
144 | 165 | playlistTableModel.setFilter("hidden=2"); // PLHT_SET_LOG
|
145 |
| - playlistTableModel.setSort(playlistTableModel.fieldIndex("id"), |
146 |
| - Qt::AscendingOrder); |
| 166 | + playlistTableModel.setSort( |
| 167 | + playlistTableModel.fieldIndex("id"), |
| 168 | + Qt::DescendingOrder); |
147 | 169 | playlistTableModel.select();
|
148 | 170 | while (playlistTableModel.canFetchMore()) {
|
149 | 171 | playlistTableModel.fetchMore();
|
150 | 172 | }
|
151 | 173 | QSqlRecord record = playlistTableModel.record();
|
152 | 174 | int nameColumn = record.indexOf("name");
|
153 | 175 | int idColumn = record.indexOf("id");
|
| 176 | + int createdColumn = record.indexOf("date_created"); |
| 177 | + |
| 178 | + auto groups = QMap<int, TreeItem*>(); |
| 179 | + |
| 180 | + QList<TreeItem*> data_list; |
| 181 | + data_list.reserve(playlistTableModel.rowCount()); |
154 | 182 |
|
155 | 183 | for (int row = 0; row < playlistTableModel.rowCount(); ++row) {
|
156 | 184 | int id = playlistTableModel.data(
|
157 | 185 | playlistTableModel.index(row, idColumn))
|
158 | 186 | .toInt();
|
159 |
| - QString name = playlistTableModel.data( |
160 |
| - playlistTableModel.index(row, nameColumn)) |
| 187 | + QString name = playlistTableModel |
| 188 | + .data(playlistTableModel.index(row, nameColumn)) |
161 | 189 | .toString();
|
162 |
| - BasePlaylistFeature::IdAndLabel idAndLabel; |
163 |
| - idAndLabel.id = id; |
164 |
| - idAndLabel.label = name; |
165 |
| - playlistLabels.append(idAndLabel); |
| 190 | + |
| 191 | + QDateTime created = |
| 192 | + playlistTableModel |
| 193 | + .data(playlistTableModel.index(row, createdColumn)) |
| 194 | + .toDateTime(); |
| 195 | + |
| 196 | + if (selectedId == id) { |
| 197 | + // save index for selection |
| 198 | + selected_row = row; |
| 199 | + } |
| 200 | + |
| 201 | + // Create the TreeItem whose parent is the invisible root item |
| 202 | + if (row >= kNumDirectHistoryEntries) { |
| 203 | + int year = created.date().year(); |
| 204 | + |
| 205 | + QMap<int, TreeItem*>::const_iterator i = groups.find(year); |
| 206 | + TreeItem* groupItem; |
| 207 | + if (i != groups.end() && i.key() == year) { |
| 208 | + groupItem = i.value(); |
| 209 | + } else { |
| 210 | + groupItem = new TreeItem(QString("%1").arg(year), -1); |
| 211 | + groups.insert(year, groupItem); |
| 212 | + data_list.append(groupItem); |
| 213 | + } |
| 214 | + |
| 215 | + std::unique_ptr<TreeItem> item(new TreeItem(name, id)); |
| 216 | + item->setBold(m_playlistsSelectedTrackIsIn.contains(id)); |
| 217 | + |
| 218 | + decorateChild(item.get(), id); |
| 219 | + |
| 220 | + groupItem->appendChild(std::move(item)); |
| 221 | + } else { |
| 222 | + TreeItem* item = new TreeItem(name, id); |
| 223 | + item->setBold(m_playlistsSelectedTrackIsIn.contains(id)); |
| 224 | + |
| 225 | + decorateChild(item, id); |
| 226 | + |
| 227 | + data_list.append(item); |
| 228 | + } |
| 229 | + } |
| 230 | + |
| 231 | + // Append all the newly created TreeItems in a dynamic way to the childmodel |
| 232 | + m_childModel.insertTreeItemRows(data_list, 0); |
| 233 | + if (selected_row == -1) { |
| 234 | + return QModelIndex(); |
166 | 235 | }
|
167 |
| - return playlistLabels; |
| 236 | + // return playlistLabels; |
| 237 | + return m_childModel.index(selected_row, 0); |
168 | 238 | }
|
169 | 239 |
|
170 | 240 | QString SetlogFeature::fetchPlaylistLabel(int playlistId) {
|
@@ -384,20 +454,40 @@ void SetlogFeature::slotPlaylistTableRenamed(
|
384 | 454 | }
|
385 | 455 | }
|
386 | 456 |
|
| 457 | +void SetlogFeature::activate() { |
| 458 | + activatePlaylist(m_playlistId); |
| 459 | +} |
| 460 | + |
| 461 | +void SetlogFeature::activatePlaylist(int playlistId) { |
| 462 | + //qDebug() << "BasePlaylistFeature::activatePlaylist()" << playlistId; |
| 463 | + QModelIndex index = indexFromPlaylistId(playlistId); |
| 464 | + if (playlistId != -1 && index.isValid()) { |
| 465 | + m_pPlaylistTableModel->setTableModel(playlistId); |
| 466 | + emit showTrackModel(m_pPlaylistTableModel); |
| 467 | + emit enableCoverArtDisplay(true); |
| 468 | + // Update selection only, if it is not the current playlist |
| 469 | + if (playlistId != m_playlistId) { |
| 470 | + emit featureSelect(this, m_lastRightClickedIndex); |
| 471 | + activateChild(m_lastRightClickedIndex); |
| 472 | + } |
| 473 | + } |
| 474 | +} |
| 475 | + |
387 | 476 | QString SetlogFeature::getRootViewHtml() const {
|
388 |
| - QString playlistsTitle = tr("History"); |
389 |
| - QString playlistsSummary = tr("The history section automatically keeps a list of tracks you play in your DJ sets."); |
390 |
| - QString playlistsSummary2 = tr("This is handy for remembering what worked in your DJ sets, posting set-lists, or reporting your plays to licensing organizations."); |
391 |
| - QString playlistsSummary3 = tr("Every time you start Mixxx, a new history section is created. You can export it as a playlist in various formats or play it again with Auto DJ."); |
392 |
| - QString playlistsSummary4 = tr("You can join the current history session with a previous one by right-clicking and selecting \"Join with previous\"."); |
393 |
| - |
394 |
| - QString html; |
395 |
| - html.append(QString("<h2>%1</h2>").arg(playlistsTitle)); |
396 |
| - html.append("<table border=\"0\" cellpadding=\"5\"><tr><td>"); |
397 |
| - html.append(QString("<p>%1</p>").arg(playlistsSummary)); |
398 |
| - html.append(QString("<p>%1</p>").arg(playlistsSummary2)); |
399 |
| - html.append(QString("<p>%1</p>").arg(playlistsSummary3)); |
400 |
| - html.append(QString("<p>%1</p>").arg(playlistsSummary4)); |
401 |
| - html.append("</td></tr></table>"); |
402 |
| - return html; |
| 477 | + // QString playlistsTitle = tr("History"); |
| 478 | + // QString playlistsSummary = tr("The history section automatically keeps a list of tracks you play in your DJ sets."); |
| 479 | + // QString playlistsSummary2 = tr("This is handy for remembering what worked in your DJ sets, posting set-lists, or reporting your plays to licensing organizations."); |
| 480 | + // QString playlistsSummary3 = tr("Every time you start Mixxx, a new history section is created. You can export it as a playlist in various formats or play it again with Auto DJ."); |
| 481 | + // QString playlistsSummary4 = tr("You can join the current history session with a previous one by right-clicking and selecting \"Join with previous\"."); |
| 482 | + // |
| 483 | + // QString html; |
| 484 | + // html.append(QString("<h2>%1</h2>").arg(playlistsTitle)); |
| 485 | + // html.append("<table border=\"0\" cellpadding=\"5\"><tr><td>"); |
| 486 | + // html.append(QString("<p>%1</p>").arg(playlistsSummary)); |
| 487 | + // html.append(QString("<p>%1</p>").arg(playlistsSummary2)); |
| 488 | + // html.append(QString("<p>%1</p>").arg(playlistsSummary3)); |
| 489 | + // html.append(QString("<p>%1</p>").arg(playlistsSummary4)); |
| 490 | + // html.append("</td></tr></table>"); |
| 491 | + // return html; |
| 492 | + return QString(); |
403 | 493 | }
|
0 commit comments