diff options
author | Dominik Holland <dominik.holland@pelagicore.com> | 2016-07-07 15:56:21 +0200 |
---|---|---|
committer | Dominik Holland <dominik.holland@pelagicore.com> | 2016-07-11 09:32:42 +0000 |
commit | d03dfaa1e05693b9a55166551657430314eef70a (patch) | |
tree | 856594d3c2d19fd1f6d0ae180b9992535a64bdc8 | |
parent | 8825fc4cca24d174a806500662c5f4b928291ff2 (diff) | |
download | qtivi-d03dfaa1e05693b9a55166551657430314eef70a.tar.gz |
Fixed insert,move,remove in the mediplayer and tuner examples and backends
The mediaplayer backend is now correctly updating the internal sql database
for the queue and also updating the model thru the dataChanged signal.
The mediaplayer ui got improved to support moving and remove items in the
queue.
The tuner backend was improved to now also support a preset list, which
supports editing (insert, move, remove) and the example was improved
to add stations to the presets list and organizing it
Change-Id: I5ff8ba1dbbbb7ed40b3f441845dbe40641ac6f54
Reviewed-by: Johan Thelin <johan.thelin@pelagicore.com>
-rw-r--r-- | examples/media/mediaplayer/main.qml | 54 | ||||
-rw-r--r-- | examples/media/tuner/main.qml | 100 | ||||
-rw-r--r-- | src/plugins/ivimedia/media_simulator/mediaplayerbackend.cpp | 67 | ||||
-rw-r--r-- | src/plugins/ivimedia/tuner_simulator/searchandbrowsebackend.cpp | 50 | ||||
-rw-r--r-- | src/plugins/ivimedia/tuner_simulator/searchandbrowsebackend.h | 2 |
5 files changed, 213 insertions, 60 deletions
diff --git a/examples/media/mediaplayer/main.qml b/examples/media/mediaplayer/main.qml index 4376122..c9a4a3f 100644 --- a/examples/media/mediaplayer/main.qml +++ b/examples/media/mediaplayer/main.qml @@ -122,21 +122,59 @@ ApplicationWindow { height: column.height color: "#efefef" - Column { - id: column - width: parent.width + MouseArea { + anchors.fill: parent + onClicked: { - Text { text: "Index: " + index } - Text { text: "Name: " + model.name } - Text { text: "Type: " + model.item.type } + } } - MouseArea { + Row { anchors.fill: parent - onClicked: { + Column { + id: column + width: parent.width * 7 / 10 + + Text { text: "Index: " + index } + Text { text: "Name: " + model.name } + Text { text: "Type: " + model.item.type } + } + Button { + text: "\u2227" + width: parent.width / 10 + height: parent.height + + enabled: index > 0 + + onClicked: { + mediaPlayer.playQueue.move(index, index - 1) + } + } + + Button { + text: "\u2228" + width: parent.width / 10 + height: parent.height + + enabled: index < mediaPlayer.playQueue.count -1 + + onClicked: { + mediaPlayer.playQueue.move(index, index + 1) + } + } + + Button { + text: "X" + width: parent.width / 10 + height: parent.height + + onClicked: { + mediaPlayer.playQueue.remove(index) + } } } + } } } diff --git a/examples/media/tuner/main.qml b/examples/media/tuner/main.qml index 3f19bcc..fe5313f 100644 --- a/examples/media/tuner/main.qml +++ b/examples/media/tuner/main.qml @@ -58,8 +58,8 @@ import QtIvi.Media 1.0 ApplicationWindow { visible: true - width: 500 - height: 250 + width: 1000 + height: 500 title: qsTr("Tuner") AmFmTuner { @@ -159,8 +159,9 @@ ApplicationWindow { ListView { spacing: 8 clip: true + + width: 300 Layout.fillHeight: true - Layout.fillWidth: true model: SearchAndBrowseModel { serviceObject: tuner.serviceObject @@ -172,13 +173,51 @@ ApplicationWindow { height: column.height color: "#efefef" - Column { - id: column - width: parent.width + MouseArea { + anchors.fill: parent + onClicked: { + tuner.tune(model.item) + } + } + + Row { + anchors.fill: parent + Column { + id: column + width: parent.width * 9 / 10 + + Text { text: "Name: " + model.item.stationName } + Text { text: "Type: " + model.item.frequency } + } - Text { text: "Name: " + model.item.stationName } - Text { text: "Type: " + model.item.frequency } + Button { + text: "+" + width: parent.width / 10 + height: parent.height + + onClicked: { + presetsModel.insert(0, model.item) + } + } } + } + } + + ListView { + spacing: 8 + clip: true + Layout.fillWidth: true + + model: SearchAndBrowseModel { + id: presetsModel + serviceObject: tuner.serviceObject + contentType: "presets" + } + + delegate: Rectangle { + width: ListView.view.width + height: column.height + color: "#efefef" MouseArea { anchors.fill: parent @@ -186,6 +225,51 @@ ApplicationWindow { tuner.tune(model.item) } } + + Row { + anchors.fill: parent + Column { + id: column + width: parent.width * 7 / 10 + + Text { text: "Name: " + model.item.stationName } + Text { text: "Type: " + model.item.frequency } + } + + Button { + text: "\u2227" + width: parent.width / 10 + height: parent.height + + enabled: index > 0 + + onClicked: { + presetsModel.move(index, index - 1) + } + } + + Button { + text: "\u2228" + width: parent.width / 10 + height: parent.height + + enabled: index < presetsModel.count -1 + + onClicked: { + presetsModel.move(index, index + 1) + } + } + + Button { + text: "X" + width: parent.width / 10 + height: parent.height + + onClicked: { + presetsModel.remove(index) + } + } + } } } } diff --git a/src/plugins/ivimedia/media_simulator/mediaplayerbackend.cpp b/src/plugins/ivimedia/media_simulator/mediaplayerbackend.cpp index 74fc723..8e18e27 100644 --- a/src/plugins/ivimedia/media_simulator/mediaplayerbackend.cpp +++ b/src/plugins/ivimedia/media_simulator/mediaplayerbackend.cpp @@ -127,7 +127,8 @@ void MediaPlayerBackend::insert(int index, const QIviPlayableItem *item) int track_index = item->id().toInt(); QString queryString = QString(QLatin1String("UPDATE queue SET qindex = qindex + 1 WHERE qindex >= %1;" - "INSERT INTO queue(qindex, track_index) VALUES( %1, %2)")) + "INSERT INTO queue(qindex, track_index) VALUES( %1, %2);" + "SELECT track.id, artistName, albumName, trackName, genre, number, file FROM track JOIN queue ON queue.track_index=track.id WHERE qindex=%1")) .arg(index) .arg(track_index); QStringList queries = queryString.split(';'); @@ -135,7 +136,7 @@ void MediaPlayerBackend::insert(int index, const QIviPlayableItem *item) QtConcurrent::run(this, &MediaPlayerBackend::doSqlOperation, MediaPlayerBackend::Insert, - queries, 0, 0); + queries, index, 0); } void MediaPlayerBackend::remove(int index) @@ -148,58 +149,66 @@ void MediaPlayerBackend::remove(int index) QtConcurrent::run(this, &MediaPlayerBackend::doSqlOperation, MediaPlayerBackend::Remove, - queries, 0, 0); + queries, index, 1); } void MediaPlayerBackend::move(int cur_index, int new_index) { - QString queryString = QString(QLatin1String("INSERT INTO queue(qindex, track_index) VALUES( %1, %2);" - "DELETE FROM queue WHERE qindex=%1;" - "UPDATE queue SET qindex = qindex + 1 WHERE qindex >= %1 AND qindex <= %2;")) + int delta = new_index - cur_index; + if (delta == 0) + return; + + QString queryString = QString(QLatin1String("UPDATE queue SET qindex = ( SELECT MAX(qindex) + 1 FROM queue) WHERE qindex=%1;" + "UPDATE queue SET qindex = qindex %5 1 WHERE qindex >= %3 AND qindex <= %4;" + "UPDATE queue SET qindex = %2 WHERE qindex= ( SELECT MAX(qindex) FROM queue);" + "SELECT track.id, artistName, albumName, trackName, genre, number, file FROM track JOIN queue ON queue.track_index=track.id WHERE qindex >= %3 AND qindex <= %4 ORDER BY qindex")) .arg(cur_index) - .arg(new_index); + .arg(new_index) + .arg(qMin(cur_index, new_index)) + .arg(qMax(cur_index, new_index)) + .arg(delta > 0 ? "-" : "+"); QStringList queries = queryString.split(';'); QtConcurrent::run(this, &MediaPlayerBackend::doSqlOperation, MediaPlayerBackend::Move, - queries, 0, 0); + queries, qMin(cur_index, new_index), qAbs(delta) + 1); } void MediaPlayerBackend::doSqlOperation(MediaPlayerBackend::OperationType type, const QStringList &queries, int start, int count) { m_db.transaction(); QSqlQuery query(m_db); + QVariantList list; for (const QString& queryString : queries) { if (query.exec(queryString)) { - if (type == MediaPlayerBackend::Select) { - QVariantList list; - while (query.next()) { - QString id = query.value(0).toString(); - QString artist = query.value(1).toString(); - QString album = query.value(2).toString(); - - //Creating the TrackItem in an factory with would make this more performant - QIviAudioTrackItem item; - item.setId(id); - item.setTitle(query.value(3).toString()); - item.setArtist(artist); - item.setAlbum(album); - item.setUrl(QUrl::fromLocalFile(query.value(6).toString())); - list.append(QVariant::fromValue(item)); - } - - emit dataFetched(list, start, list.count() >= count); - } else if (type == MediaPlayerBackend::Insert) { - + while (query.next()) { + QString id = query.value(0).toString(); + QString artist = query.value(1).toString(); + QString album = query.value(2).toString(); + + //Creating the TrackItem in an factory with would make this more performant + QIviAudioTrackItem item; + item.setId(id); + item.setTitle(query.value(3).toString()); + item.setArtist(artist); + item.setAlbum(album); + item.setUrl(QUrl::fromLocalFile(query.value(6).toString())); + list.append(QVariant::fromValue(item)); } } else { - qDebug() << queries; + qDebug() << queryString; qDebug() << query.lastError().text(); m_db.rollback(); break; } } + + if (type == MediaPlayerBackend::Select) + emit dataFetched(list, start, list.count() >= count); + else + emit dataChanged(list, start, count); + m_db.commit(); } diff --git a/src/plugins/ivimedia/tuner_simulator/searchandbrowsebackend.cpp b/src/plugins/ivimedia/tuner_simulator/searchandbrowsebackend.cpp index 03563ff..13cf0a6 100644 --- a/src/plugins/ivimedia/tuner_simulator/searchandbrowsebackend.cpp +++ b/src/plugins/ivimedia/tuner_simulator/searchandbrowsebackend.cpp @@ -50,23 +50,30 @@ SearchAndBrowseBackend::SearchAndBrowseBackend(AmFmTunerBackend *tunerBackend, Q { qRegisterMetaType<QIviAmFmTunerStation>(); registerContentType<QIviAmFmTunerStation>("station"); + registerContentType<QIviAmFmTunerStation>("presets"); } void SearchAndBrowseBackend::fetchData(const QUuid &identifier, const QString &type, QIviAbstractQueryTerm *term, const QList<QIviOrderTerm> &orderTerms, int start, int count) { emit supportedCapabilitiesChanged(identifier, QIviSearchAndBrowseModel::Capabilities( QIviSearchAndBrowseModel::SupportsStatelessNavigation | - QIviSearchAndBrowseModel::SupportsGetSize + QIviSearchAndBrowseModel::SupportsGetSize | + QIviSearchAndBrowseModel::SupportsInsert | + QIviSearchAndBrowseModel::SupportsMove | + QIviSearchAndBrowseModel::SupportsRemove )); Q_UNUSED(term) Q_UNUSED(orderTerms) + QVector<QIviAmFmTunerStation> stations; - if (type != "station") + if (type == "station") + stations = m_tunerBackend->m_bandHash[QIviAmFmTuner::AMBand].m_stations + m_tunerBackend->m_bandHash[QIviAmFmTuner::FMBand].m_stations; + else if (type == "presets") + stations = m_presets; + else return; - QVector<QIviAmFmTunerStation> stations = m_tunerBackend->m_bandHash[QIviAmFmTuner::AMBand].m_stations + m_tunerBackend->m_bandHash[QIviAmFmTuner::FMBand].m_stations; - emit countChanged(identifier, stations.length()); QVariantList requestedStations; @@ -109,23 +116,36 @@ QString SearchAndBrowseBackend::goForward(const QUuid &identifier, const QString void SearchAndBrowseBackend::insert(const QUuid &identifier, const QString &type, int index, const QIviSearchAndBrowseModelItem *item) { - Q_UNUSED(identifier) - Q_UNUSED(type) - Q_UNUSED(index) - Q_UNUSED(item) + if (type != "presets" || item->type() != "amfmtunerstation") + return; + + QIviAmFmTunerStation station = *static_cast<const QIviAmFmTunerStation*>(item); + m_presets.insert(index, station); + QVariantList stations = { QVariant::fromValue(station) }; + emit dataChanged(identifier, stations, index, 0); } void SearchAndBrowseBackend::remove(const QUuid &identifier, const QString &type, int index) { - Q_UNUSED(identifier) - Q_UNUSED(type) - Q_UNUSED(index) + if (type != "presets") + return; + + m_presets.removeAt(index); + emit dataChanged(identifier, QVariantList(), index, 1); } void SearchAndBrowseBackend::move(const QUuid &identifier, const QString &type, int currentIndex, int newIndex) { - Q_UNUSED(identifier) - Q_UNUSED(type) - Q_UNUSED(currentIndex) - Q_UNUSED(newIndex) + if (type != "presets") + return; + + int min = qMin(currentIndex, newIndex); + int max = qMax(currentIndex, newIndex); + + m_presets.move(currentIndex, newIndex); + QVariantList stations; + for (int i = min; i <= max; i++) + stations.append(QVariant::fromValue(m_presets.at(i))); + + emit dataChanged(identifier, stations, min, max - min + 1); } diff --git a/src/plugins/ivimedia/tuner_simulator/searchandbrowsebackend.h b/src/plugins/ivimedia/tuner_simulator/searchandbrowsebackend.h index 6149ec9..8a9db7b 100644 --- a/src/plugins/ivimedia/tuner_simulator/searchandbrowsebackend.h +++ b/src/plugins/ivimedia/tuner_simulator/searchandbrowsebackend.h @@ -44,6 +44,7 @@ #include <QtIviCore/QIviSearchAndBrowseModelInterface> #include <QtIviCore/QIviSearchAndBrowseModel> +#include <QtIviMedia/QIviAmFmTunerStation> class AmFmTunerBackend; @@ -65,6 +66,7 @@ public: private: AmFmTunerBackend *m_tunerBackend; + QVector<QIviAmFmTunerStation> m_presets; }; #endif // SEARCHBACKEND_H |