~daschuer/mixxx/features_setlog

« back to all changes in this revision

Viewing changes to mixxx/src/library/traktor/traktorplaylistmodel.cpp

  • Committer: daschuer at gmx
  • Date: 2011-10-04 20:29:10 UTC
  • mfrom: (2840.1.15 mixxx-trunk)
  • Revision ID: daschuer@gmx.de-20111004202910-8pktjwu7f51xl515
merged with lp:mixxx

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#include <QtCore>
2
2
#include <QtGui>
3
3
#include <QtSql>
 
4
 
4
5
#include "library/trackcollection.h"
5
6
#include "library/traktor/traktorplaylistmodel.h"
6
7
 
7
 
#include "mixxxutils.cpp"
8
 
 
9
8
TraktorPlaylistModel::TraktorPlaylistModel(QObject* parent,
10
9
                                       TrackCollection* pTrackCollection)
11
 
        : TrackModel(pTrackCollection->getDatabase(),
12
 
                     "mixxx.db.model.traktor.playlistmodel"),
13
 
          BaseSqlTableModel(parent, pTrackCollection, pTrackCollection->getDatabase()),
 
10
        : BaseSqlTableModel(parent, pTrackCollection,
 
11
                            pTrackCollection->getDatabase(),
 
12
                            "mixxx.db.model.traktor.playlistmodel"),
14
13
          m_pTrackCollection(pTrackCollection),
15
 
          m_database(m_pTrackCollection->getDatabase())
16
 
 
17
 
{
18
 
    connect(this, SIGNAL(doSearch(const QString&)), this, SLOT(slotSearch(const QString&)));
19
 
    setCaching(false);
 
14
          m_database(m_pTrackCollection->getDatabase()) {
 
15
    connect(this, SIGNAL(doSearch(const QString&)),
 
16
            this, SLOT(slotSearch(const QString&)));
20
17
}
21
18
 
22
19
TraktorPlaylistModel::~TraktorPlaylistModel() {
23
20
}
24
21
 
25
 
bool TraktorPlaylistModel::addTrack(const QModelIndex& index, QString location)
26
 
{
27
 
    return false;
28
 
}
29
 
 
30
 
TrackPointer TraktorPlaylistModel::getTrack(const QModelIndex& index) const
31
 
{
 
22
TrackPointer TraktorPlaylistModel::getTrack(const QModelIndex& index) const {
32
23
    //qDebug() << "getTraktorTrack";
33
 
    QString artist = index.sibling(index.row(), fieldIndex("artist")).data().toString();
34
 
    QString title = index.sibling(index.row(), fieldIndex("title")).data().toString();
35
 
    QString album = index.sibling(index.row(), fieldIndex("album")).data().toString();
36
 
    QString year = index.sibling(index.row(), fieldIndex("year")).data().toString();
37
 
    QString genre = index.sibling(index.row(), fieldIndex("genre")).data().toString();
38
 
    float bpm = index.sibling(index.row(), fieldIndex("bpm")).data().toString().toFloat();
39
 
 
40
 
    QString location = index.sibling(index.row(), fieldIndex("location")).data().toString();
 
24
    QString artist = index.sibling(
 
25
        index.row(), fieldIndex("artist")).data().toString();
 
26
    QString title = index.sibling(
 
27
        index.row(), fieldIndex("title")).data().toString();
 
28
    QString album = index.sibling(
 
29
        index.row(), fieldIndex("album")).data().toString();
 
30
    QString year = index.sibling(
 
31
        index.row(), fieldIndex("year")).data().toString();
 
32
    QString genre = index.sibling(
 
33
        index.row(), fieldIndex("genre")).data().toString();
 
34
    float bpm = index.sibling(
 
35
        index.row(), fieldIndex("bpm")).data().toString().toFloat();
 
36
    QString location = index.sibling(
 
37
        index.row(), fieldIndex("location")).data().toString();
41
38
 
42
39
    TrackInfoObject* pTrack = new TrackInfoObject(location);
43
40
    pTrack->setArtist(artist);
50
47
    return TrackPointer(pTrack, &QObject::deleteLater);
51
48
}
52
49
 
53
 
QString TraktorPlaylistModel::getTrackLocation(const QModelIndex& index) const
54
 
{
55
 
    QString location = index.sibling(index.row(), fieldIndex("location")).data().toString();
56
 
    return location;
57
 
}
58
 
 
59
 
int TraktorPlaylistModel::getTrackId(const QModelIndex& index) const {
60
 
    if (!index.isValid()) {
61
 
        return -1;
62
 
    }
63
 
    return index.sibling(index.row(), fieldIndex("id")).data().toInt();
64
 
}
65
 
 
66
 
const QLinkedList<int> TraktorPlaylistModel::getTrackRows(int trackId) const {
67
 
    return BaseSqlTableModel::getTrackRows(trackId);
68
 
}
69
 
 
70
 
void TraktorPlaylistModel::removeTrack(const QModelIndex& index)
71
 
{
72
 
 
73
 
}
74
 
 
75
 
void TraktorPlaylistModel::removeTracks(const QModelIndexList& indices) {
76
 
 
77
 
}
78
 
 
79
 
void TraktorPlaylistModel::moveTrack(const QModelIndex& sourceIndex, const QModelIndex& destIndex)
80
 
{
81
 
 
82
 
}
83
 
 
84
50
void TraktorPlaylistModel::search(const QString& searchText) {
85
51
    // qDebug() << "TraktorPlaylistModel::search()" << searchText
86
52
    //          << QThread::currentThread();
91
57
    BaseSqlTableModel::search(searchText);
92
58
}
93
59
 
94
 
const QString TraktorPlaylistModel::currentSearch() {
95
 
    return BaseSqlTableModel::currentSearch();
96
 
}
97
 
 
98
60
bool TraktorPlaylistModel::isColumnInternal(int column) {
99
 
    if (column == fieldIndex(LIBRARYTABLE_ID) ||
100
 
        column == fieldIndex(LIBRARYTABLE_MIXXXDELETED) ||
101
 
        column == fieldIndex(TRACKLOCATIONSTABLE_FSDELETED) ||
102
 
        column == fieldIndex("name") ||
103
 
        column == fieldIndex("track_id"))
 
61
    if (column == fieldIndex("track_id")) {
104
62
        return true;
 
63
    }
105
64
    return false;
106
65
}
107
66
 
108
 
QMimeData* TraktorPlaylistModel::mimeData(const QModelIndexList &indexes) const {
109
 
 
110
 
   QMimeData *mimeData = new QMimeData();
111
 
    QList<QUrl> urls;
112
 
 
113
 
    //Ok, so the list of indexes we're given contains separates indexes for
114
 
    //each column, so even if only one row is selected, we'll have like 7 indexes.
115
 
    //We need to only count each row once:
116
 
    QList<int> rows;
117
 
 
118
 
    foreach (QModelIndex index, indexes) {
119
 
        if (index.isValid()) {
120
 
            if (!rows.contains(index.row())) {
121
 
                rows.push_back(index.row());
122
 
                QUrl url = QUrl::fromLocalFile(getTrackLocation(index));
123
 
                if (!url.isValid())
124
 
                    qDebug() << "ERROR invalid url\n";
125
 
                else
126
 
                    urls.append(url);
127
 
            }
128
 
        }
129
 
    }
130
 
    mimeData->setUrls(urls);
131
 
    return mimeData;
132
 
}
133
 
 
134
 
QItemDelegate* TraktorPlaylistModel::delegateForColumn(const int i) {
135
 
    return NULL;
136
 
}
137
 
 
138
 
TrackModel::CapabilitiesFlags TraktorPlaylistModel::getCapabilities() const
139
 
{
140
 
    return TRACKMODELCAPS_NONE;
141
 
}
142
 
 
143
 
Qt::ItemFlags TraktorPlaylistModel::flags(const QModelIndex &index) const
144
 
{
 
67
Qt::ItemFlags TraktorPlaylistModel::flags(const QModelIndex &index) const {
145
68
    return readOnlyFlags(index);
146
69
}
147
 
void TraktorPlaylistModel::setPlaylist(QString playlist_path)
148
 
{
 
70
 
 
71
void TraktorPlaylistModel::setPlaylist(QString playlist_path) {
149
72
    int playlistId = -1;
150
73
    QSqlQuery finder_query(m_database);
151
 
    finder_query.prepare("SELECT id from traktor_playlists where name='"+playlist_path+"'");
152
 
 
153
 
    if(finder_query.exec()){
154
 
        while (finder_query.next()) {
155
 
            playlistId = finder_query.value(finder_query.record().indexOf("id")).toInt();
156
 
        }
157
 
    }
158
 
    else
159
 
        qDebug() << "SQL Error in TraktorPlaylistModel.cpp: line" << __LINE__ << " " << finder_query.lastError();
160
 
 
 
74
    finder_query.prepare(
 
75
        "SELECT id from traktor_playlists where name='"+playlist_path+"'");
 
76
 
 
77
    if (!finder_query.exec()) {
 
78
        qDebug() << "SQL Error in TraktorPlaylistModel.cpp: line" << __LINE__
 
79
                 << finder_query.lastError();
 
80
    }
 
81
    while (finder_query.next()) {
 
82
        playlistId = finder_query.value(
 
83
            finder_query.record().indexOf("id")).toInt();
 
84
    }
161
85
 
162
86
    QString playlistID = "TraktorPlaylist_" + QString("%1").arg(playlistId);
163
 
    //Escape the playlist name
 
87
    // Escape the playlist name
164
88
    QSqlDriver* driver = m_pTrackCollection->getDatabase().driver();
165
89
    QSqlField playlistNameField("name", QVariant::String);
166
90
    playlistNameField.setValue(playlistID);
167
91
 
168
92
    QStringList columns;
169
 
    columns << "traktor_library.id"
170
 
            << "traktor_library.artist"
171
 
            << "traktor_library.title"
172
 
            << "traktor_library.album"
173
 
            << "traktor_library.year"
174
 
            << "traktor_library.genre"
175
 
            << "traktor_library.tracknumber"
176
 
            << "traktor_library.location"
177
 
            << "traktor_library.comment"
178
 
            << "traktor_library.rating"
179
 
            << "traktor_library.duration"
180
 
            << "traktor_library.bitrate"
181
 
            << "traktor_library.bpm"
182
 
            << "traktor_library.key"
183
 
            << "traktor_playlist_tracks.track_id"
184
 
            << "traktor_playlists.name";
 
93
    columns << "track_id";
185
94
 
186
95
    QSqlQuery query(m_database);
187
 
    query.prepare("CREATE TEMPORARY VIEW IF NOT EXISTS "+ driver->formatValue(playlistNameField) + " AS "
188
 
                  "SELECT "
189
 
                  + columns.join(",") +
190
 
                  " FROM traktor_library "
191
 
                  "INNER JOIN traktor_playlist_tracks "
192
 
                  "ON traktor_playlist_tracks.track_id = traktor_library.id "
193
 
                  "INNER JOIN traktor_playlists "
194
 
                  "ON traktor_playlist_tracks.playlist_id = traktor_playlists.id "
195
 
                  "where traktor_playlists.name='"+playlist_path+"'"
196
 
                  );
197
 
 
 
96
    query.prepare("CREATE TEMPORARY VIEW IF NOT EXISTS " +
 
97
                  driver->formatValue(playlistNameField) + " AS "
 
98
                  "SELECT " + columns.join(",") +
 
99
                  " FROM traktor_playlist_tracks "
 
100
                  "WHERE playlist_id = " + QString("%1").arg(playlistId));
198
101
 
199
102
    if (!query.exec()) {
200
 
        qDebug() << "Error creating temporary view for traktor playlists. TraktorPlaylistModel --> line: " << __LINE__ << " " << query.lastError();
201
 
        qDebug() << "Executed Query: " <<  query.executedQuery();
 
103
        qDebug() << "Error creating temporary view for traktor playlists."
 
104
                 << "TraktorPlaylistModel --> line: " << __LINE__
 
105
                 << query.lastError();
 
106
        qDebug() << "Executed Query: " << query.executedQuery();
202
107
        return;
203
108
    }
204
109
 
205
 
    // Strip out library. and track_locations.
206
 
    for (int i = 0; i < columns.size(); ++i) {
207
 
        columns[i] = columns[i].replace("traktor_library.", "")
208
 
                .replace("traktor_playlist_tracks.", "").replace("traktor_playlists.", "");
209
 
    }
210
 
 
211
 
    setTable(playlistID, columns, "id");
212
 
 
213
 
    //removeColumn(fieldIndex("track_id"));
214
 
    //removeColumn(fieldIndex("name"));
215
 
    //removeColumn(fieldIndex("id"));
 
110
    setTable(playlistID, columns[0], columns,
 
111
             m_pTrackCollection->getTrackSource("traktor"));
216
112
    initHeaderData();
217
 
    initDefaultSearchColumns();
218
 
    slotSearch("");
219
 
    select(); //Populate the data model.
 
113
    setSearch("");
220
114
}
221
115
 
222
116
bool TraktorPlaylistModel::isColumnHiddenByDefault(int column) {
223
 
    if (column == fieldIndex(LIBRARYTABLE_KEY))
224
 
        return true;
225
 
    if(column == fieldIndex(LIBRARYTABLE_BITRATE))
226
 
        return true;
227
 
 
 
117
    if (column == fieldIndex(LIBRARYTABLE_KEY) ||
 
118
        column == fieldIndex(LIBRARYTABLE_BITRATE)) {
 
119
        return true;
 
120
    }
228
121
    return false;
229
122
}