fix table selection things with proxymodel

This commit is contained in:
billypom on debian 2025-04-18 20:41:54 -04:00
parent d2992215f6
commit 89804aaccb
3 changed files with 31 additions and 27 deletions

29
LICENSE
View File

@ -1,21 +1,16 @@
MIT License
MusicPom - Qt music library manager and audio player for Linux
Copyright (c) 2024 billypom
Copyright (C) 2025 billypom
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.

View File

@ -482,6 +482,10 @@ class MusicTable(QTableView):
def delete_songs(self):
"""Asks to delete the currently selected songs from the db and music table (not the filesystem)"""
# FIXME: determine if we are in a playlist or not
# then delete songs from only the playlist
# or provide extra questionbox option
# | Delete from playlist & lib | Delete from playlist only | Cancel |
selected_filepaths = self.get_selected_songs_filepaths()
formatted_selected_filepaths = "\n".join(selected_filepaths)
question_dialog = QuestionBoxDetails(
@ -708,12 +712,12 @@ class MusicTable(QTableView):
else:
std_item = QStandardItem(str(item) if item else "")
items.append(std_item)
self.model2.appendRow(items)
# store database id in the row object using setData
# - useful for fast db fetching and other model operations
for item in items:
item.setData(id, Qt.ItemDataRole.UserRole)
self.model2.appendRow(items)
self.model2.layoutChanged.emit() # emits a signal that the view should be updated
self.playlistStatsSignal.emit(f"Songs: {row_count} | Total time: {total_time}")
self.connect_data_changed()
@ -808,7 +812,7 @@ class MusicTable(QTableView):
filepaths = []
for row in selected_rows:
idx = self.proxymodel.index(
row, list(self.headers.gui.values()).index("path")
row, self.headers.user_headers.index("filepath")
)
filepaths.append(idx.data())
return filepaths
@ -824,7 +828,7 @@ class MusicTable(QTableView):
return []
selected_rows = set(index.row() for index in indexes)
id_list = [
self.model2.data(self.model2.index(row, 0), Qt.ItemDataRole.UserRole)
self.proxymodel.data(self.proxymodel.index(row, 0), Qt.ItemDataRole.UserRole)
for row in selected_rows
]
return id_list
@ -847,11 +851,16 @@ class MusicTable(QTableView):
def set_selected_song_filepath(self) -> None:
"""Sets the filepath of the currently selected song"""
self.selected_song_filepath = (
self.currentIndex()
.siblingAtColumn(list(self.headers.gui.values()).index("path"))
.data()
)
try:
table_index = self.headers.user_headers.index("filepath")
filepath = self.currentIndex().siblingAtColumn(table_index).data()
except ValueError:
# if the user doesnt have filepath selected as a header, retrieve the file from db
row = self.currentIndex().row()
id = self.proxymodel.data(self.proxymodel.index(row, 0), Qt.ItemDataRole.UserRole)
with DBA.DBAccess() as db:
filepath = db.query('SELECT filepath FROM song WHERE id = ?', (id,))[0][0]
self.selected_song_filepath = filepath
def set_current_song_filepath(self) -> None:
"""

View File

@ -424,7 +424,7 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow):
Start playback of `tableView.current_song_filepath` & moves playback slider
"""
if not filepath:
filepath = self.tableView.get_current_song_filepath()
filepath = self.tableView.get_selected_song_filepath()
# get metadata
metadata = get_tags(filepath)[0]
# read the file