Mouse interactions in Library

This commit is contained in:
Chris Cochrun 2022-02-25 13:53:25 -06:00
parent b2ea6ab22a
commit 7f432212cd
4 changed files with 105 additions and 75 deletions

View file

@ -140,23 +140,45 @@ Item {
Component { Component {
id: songDelegate id: songDelegate
Item{
implicitWidth: ListView.view.width
height: selectedLibrary == "songs" ? 50 : 0
Kirigami.BasicListItem { Kirigami.BasicListItem {
id: songListItem id: songListItem
property bool rightMenu: false
implicitWidth: ListView.view.width implicitWidth: ListView.view.width
height: selectedLibrary == "songs" ? 40 : 0 height: selectedLibrary == "songs" ? 50 : 0
clip: true clip: true
label: title label: title
subtitle: author subtitle: author
hoverEnabled: true supportsMouseEvents: false
ListView.onAdd: { backgroundColor: {
showPassiveNotification(title, 3000) if (parent.ListView.isCurrentItem) {
songLibraryList.currentIndex = index Kirigami.Theme.highlightColor;
song = index } else if (dragHandler.containsMouse){
songTitle = title Kirigami.Theme.hoverColor;
songLyrics = lyrics } else {
songAuthor = author Kirigami.Theme.backgroundColor;
songVorder = vorder
} }
}
textColor: {
if (parent.ListView.isCurrentItem || dragHandler.containsMouse)
activeTextColor;
else
Kirigami.Theme.textColor;
}
/* onAdd: { */
/* showPassiveNotification(title, 3000) */
/* songLibraryList.currentIndex = index */
/* song = index */
/* songTitle = title */
/* songLyrics = lyrics */
/* songAuthor = author */
/* songVorder = vorder */
/* } */
Behavior on height { Behavior on height {
NumberAnimation { NumberAnimation {
@ -164,39 +186,6 @@ Item {
duration: 300 duration: 300
} }
} }
MouseArea {
id: dragHandler
anchors.fill: parent
acceptedButtons: Qt.LeftButton | Qt.RightButton
/* width: parent.width */
/* height: parent.height */
/* Layout.alignment: Qt.AlignTop */
/* x: parent.x */
/* y: parent.y */
drag {
target: songListItem
onActiveChanged: {
if (dragHandler.drag.active) {
draggedLibraryItem = songLibraryList.currentItem
showPassiveNotification(index)
}
}
}
onClicked: {
if(mouse.button == Qt.RightButton)
showPassiveNotification("Delete me!");
else{
showPassiveNotification(title, 3000)
songLibraryList.currentIndex = index
song = index
songTitle = title
songLyrics = lyrics
songAuthor = author
songVorder = vorder
}
}
}
Drag.active: dragHandler.drag.active Drag.active: dragHandler.drag.active
Drag.hotSpot.x: width / 2 Drag.hotSpot.x: width / 2
Drag.hotSpot.y: height / 2 Drag.hotSpot.y: height / 2
@ -210,6 +199,53 @@ Item {
y: y y: y
} }
} }
}
MouseArea {
id: dragHandler
anchors.fill: parent
hoverEnabled: true
drag {
target: songListItem
onActiveChanged: {
if (dragHandler.drag.active) {
draggedLibraryItem = songLibraryList.currentItem
showPassiveNotification(index)
}
}
filterChildren: true
threshold: 10
}
MouseArea {
id: clickHandler
anchors.fill: parent
acceptedButtons: Qt.LeftButton | Qt.RightButton
onClicked: {
if(mouse.button == Qt.RightButton)
rightClickSongMenu.popup()
else{
showPassiveNotification(title, 3000)
songLibraryList.currentIndex = index
song = index
songTitle = title
songLyrics = lyrics
songAuthor = author
songVorder = vorder
}
}
}
}
Controls.Menu {
id: rightClickSongMenu
x: clickHandler.mouseX
y: clickHandler.mouseY + 10
Kirigami.Action {
text: "delete"
onTriggered: songsqlmodel.deleteSong(index)
}
}
} }
} }
@ -464,9 +500,4 @@ Item {
} }
} }
} }
function updateSongLyrics(lyrics) {
showPassiveNotification("library function" + lyrics)
showPassiveNotification("WE DID IT!")
}
} }

View file

@ -67,6 +67,7 @@ Item {
background: Rectangle { background: Rectangle {
Kirigami.Theme.colorSet: Kirigami.Theme.Tooltip Kirigami.Theme.colorSet: Kirigami.Theme.Tooltip
color: Kirigami.Theme.backgroundColor color: Kirigami.Theme.backgroundColor
radius: 10
border.color: Kirigami.Theme.activeBackgroundColor border.color: Kirigami.Theme.activeBackgroundColor
border.width: 2 border.width: 2
} }

View file

@ -22,7 +22,7 @@ static void createTable()
QSqlQuery query; QSqlQuery query;
if (!query.exec("CREATE TABLE IF NOT EXISTS 'songs' (" if (!query.exec("CREATE TABLE IF NOT EXISTS 'songs' ("
" 'id' INT NOT NULL," " 'id' INTEGER NOT NULL,"
" 'title' TEXT NOT NULL," " 'title' TEXT NOT NULL,"
" 'lyrics' TEXT," " 'lyrics' TEXT,"
" 'author' TEXT," " 'author' TEXT,"
@ -36,10 +36,10 @@ static void createTable()
qDebug() << query.lastQuery(); qDebug() << query.lastQuery();
qDebug() << "inserting into songs"; qDebug() << "inserting into songs";
query.exec("INSERT INTO songs VALUES (1, '10,000 Reasons', '10,000 reasons for my heart to sing', 'Matt Redman', '13470183', '', '')"); query.exec("INSERT INTO songs (title, lyrics, author, ccli, audio, vorder) VALUES ('10,000 Reasons', '10,000 reasons for my heart to sing', 'Matt Redman', '13470183', '', '')");
qDebug() << query.lastQuery(); qDebug() << query.lastQuery();
query.exec("INSERT INTO songs VALUES (2, 'River', 'Im going down to the river', 'Jordan Feliz', '13470183', '', '')"); query.exec("INSERT INTO songs (title, lyrics, author, ccli, audio, vorder) VALUES ('River', 'Im going down to the river', 'Jordan Feliz', '13470183', '', '')");
query.exec("INSERT INTO songs VALUES (3, 'Marvelous Light', 'Into marvelous " query.exec("INSERT INTO songs (title, lyrics, author, ccli, audio, vorder) VALUES ('Marvelous Light', 'Into marvelous "
"light Im running', 'Chris Tomlin', '13470183', '', '')"); "light Im running', 'Chris Tomlin', '13470183', '', '')");
query.exec("select * from songs"); query.exec("select * from songs");
@ -52,7 +52,7 @@ SongSqlModel::SongSqlModel(QObject *parent)
qDebug() << "creating table"; qDebug() << "creating table";
createTable(); createTable();
setTable(songsTableName); setTable(songsTableName);
setEditStrategy(QSqlTableModel::OnFieldChange); setEditStrategy(QSqlTableModel::OnManualSubmit);
// make sure to call select else the model won't fill // make sure to call select else the model won't fill
select(); select();
} }
@ -86,19 +86,26 @@ void SongSqlModel::newSong() {
int rows = rowCount(); int rows = rowCount();
qDebug() << rows; qDebug() << rows;
QSqlRecord recorddata = record(rows); QSqlRecord recorddata = record();
recorddata.setValue("id", rows + 1);
recorddata.setValue("title", "new song"); recorddata.setValue("title", "new song");
qDebug() << recorddata; qDebug() << recorddata;
if (insertRecord(rows, recorddata)) { if (insertRecord(rows, recorddata)) {
submitAll(); submitAll();
select();
}else { }else {
qDebug() << lastError(); qDebug() << lastError();
} }
} }
void SongSqlModel::deleteSong(const int &row) {
QSqlRecord recordData = record(row);
if (recordData.isEmpty())
return;
removeRow(row);
submitAll();
}
int SongSqlModel::id() const { int SongSqlModel::id() const {
return m_id; return m_id;
} }
@ -121,11 +128,11 @@ void SongSqlModel::setTitle(const QString &title) {
void SongSqlModel::updateTitle(const int &row, const QString &title) { void SongSqlModel::updateTitle(const int &row, const QString &title) {
qDebug() << "Row is " << row; qDebug() << "Row is " << row;
QSqlRecord rowdata = record(row); QSqlRecord rowdata = record(row);
qDebug() << rowdata;
rowdata.setValue("title", title); rowdata.setValue("title", title);
setRecord(row, rowdata); setRecord(row, rowdata);
qDebug() << rowdata;
submitAll(); submitAll();
select();
emit titleChanged(); emit titleChanged();
} }
@ -150,8 +157,6 @@ void SongSqlModel::updateAuthor(const int &row, const QString &author) {
rowdata.setValue("author", author); rowdata.setValue("author", author);
setRecord(row, rowdata); setRecord(row, rowdata);
submitAll(); submitAll();
select();
emit authorChanged(); emit authorChanged();
} }
@ -176,8 +181,6 @@ void SongSqlModel::updateLyrics(const int &row, const QString &lyrics) {
rowdata.setValue("lyrics", lyrics); rowdata.setValue("lyrics", lyrics);
setRecord(row, rowdata); setRecord(row, rowdata);
submitAll(); submitAll();
select();
emit lyricsChanged(); emit lyricsChanged();
} }
@ -202,8 +205,6 @@ void SongSqlModel::updateCcli(const int &row, const QString &ccli) {
rowdata.setValue("ccli", ccli); rowdata.setValue("ccli", ccli);
setRecord(row, rowdata); setRecord(row, rowdata);
submitAll(); submitAll();
select();
emit ccliChanged(); emit ccliChanged();
} }
@ -228,8 +229,6 @@ void SongSqlModel::updateAudio(const int &row, const QString &audio) {
rowdata.setValue("audio", audio); rowdata.setValue("audio", audio);
setRecord(row, rowdata); setRecord(row, rowdata);
submitAll(); submitAll();
select();
emit audioChanged(); emit audioChanged();
} }
@ -252,7 +251,5 @@ void SongSqlModel::updateVerseOrder(const int &row, const QString &vorder) {
rowdata.setValue("vorder", vorder); rowdata.setValue("vorder", vorder);
setRecord(row, rowdata); setRecord(row, rowdata);
submitAll(); submitAll();
select();
emit vorderChanged(); emit vorderChanged();
} }

View file

@ -45,6 +45,7 @@ public:
Q_INVOKABLE void updateVerseOrder(const int &row, const QString &vorder); Q_INVOKABLE void updateVerseOrder(const int &row, const QString &vorder);
Q_INVOKABLE void newSong(); Q_INVOKABLE void newSong();
Q_INVOKABLE void deleteSong(const int &row);
QVariant data(const QModelIndex &index, int role) const override; QVariant data(const QModelIndex &index, int role) const override;
QHash<int, QByteArray> roleNames() const override; QHash<int, QByteArray> roleNames() const override;