WIP V3: editing saves
This commit is contained in:
parent
5d19d1ed9f
commit
95c7ccbf34
@ -439,6 +439,27 @@ class PlaylistRows(Base):
|
||||
)
|
||||
session.flush()
|
||||
|
||||
@classmethod
|
||||
def deep_row(
|
||||
cls, session: scoped_session, playlist_id: int, row_number: int
|
||||
) ->"PlaylistRows":
|
||||
"""
|
||||
Return a playlist row that includes full track and lastplayed data for
|
||||
given playlist_id and row
|
||||
"""
|
||||
|
||||
stmt = (
|
||||
select(PlaylistRows)
|
||||
.options(joinedload(cls.track))
|
||||
.where(
|
||||
PlaylistRows.playlist_id == playlist_id,
|
||||
PlaylistRows.plr_rownum == row_number
|
||||
)
|
||||
# .options(joinedload(Tracks.playdates))
|
||||
)
|
||||
|
||||
return session.execute(stmt).unique().scalar_one()
|
||||
|
||||
@classmethod
|
||||
def deep_rows(
|
||||
cls, session: scoped_session, playlist_id: int
|
||||
|
||||
@ -67,7 +67,7 @@ import icons_rc # noqa F401
|
||||
import music
|
||||
from models import Base, Carts, Playdates, PlaylistRows, Playlists, Settings, Tracks
|
||||
from config import Config
|
||||
from playlists_v3 import PlaylistTab
|
||||
from playlists import PlaylistTab
|
||||
from ui.dlg_cart_ui import Ui_DialogCartEdit # type: ignore
|
||||
from ui.dlg_search_database_ui import Ui_Dialog # type: ignore
|
||||
from ui.dlg_SelectPlaylist_ui import Ui_dlgSelectPlaylist # type: ignore
|
||||
|
||||
@ -23,9 +23,7 @@ from helpers import (
|
||||
file_is_unreadable,
|
||||
)
|
||||
|
||||
from models import (
|
||||
PlaylistRows,
|
||||
)
|
||||
from models import PlaylistRows, Tracks
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from musicmuster import MusicMusterSignals
|
||||
@ -299,6 +297,12 @@ class PlaylistModel(QAbstractTableModel):
|
||||
for p in PlaylistRows.deep_rows(session, self.playlist_id):
|
||||
self.playlist_rows[p.plr_rownum] = PlaylistRowData(p)
|
||||
|
||||
def refresh_row(self, session, row_number):
|
||||
"""Populate dict for one row for data calls"""
|
||||
|
||||
p = PlaylistRows.deep_row(session, self.playlist_id, row_number)
|
||||
self.playlist_rows[p.plr_rownum] = PlaylistRowData(p)
|
||||
|
||||
def rowCount(self, index: QModelIndex = QModelIndex()) -> int:
|
||||
"""Standard function for view"""
|
||||
|
||||
@ -311,5 +315,37 @@ class PlaylistModel(QAbstractTableModel):
|
||||
Update model with edited data
|
||||
"""
|
||||
|
||||
print(f"setData({index.row()=}, {index.column()=}, {value=}, {role=})")
|
||||
return True
|
||||
if index.isValid() and role == Qt.ItemDataRole.EditRole:
|
||||
row_number = index.row()
|
||||
column = index.column()
|
||||
|
||||
with Session() as session:
|
||||
plr = session.get(PlaylistRows, self.playlist_rows[row_number].plrid)
|
||||
if not plr:
|
||||
print(
|
||||
f"Error saving data: {row_number=}, {column=}, {value=}, {self.playlist_id=}"
|
||||
)
|
||||
return False
|
||||
|
||||
if column == Col.TITLE.value or column == Col.ARTIST.value:
|
||||
track = session.get(Tracks, plr.track_id)
|
||||
if not track:
|
||||
print(f"Error retreiving track: {plr=}")
|
||||
return False
|
||||
if column == Col.TITLE.value:
|
||||
track.title = str(value)
|
||||
elif column == Col.ARTIST.value:
|
||||
track.artist = str(value)
|
||||
else:
|
||||
print(f"Error updating track: {column=}, {value=}")
|
||||
return False
|
||||
elif column == Col.NOTE.value:
|
||||
plr.note = str(value)
|
||||
|
||||
# Flush changes before refreshing data
|
||||
session.flush()
|
||||
self.refresh_row(session, row_number)
|
||||
self.dataChanged.emit(index, index, [Qt.ItemDataRole.DisplayRole, role])
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
@ -131,7 +131,6 @@ class EscapeDelegate(QStyledItemDelegate):
|
||||
|
||||
if event.type() == QEvent.Type.KeyPress:
|
||||
key_event = cast(QKeyEvent, event)
|
||||
print(key_event.key())
|
||||
if key_event.key() == Qt.Key.Key_Return:
|
||||
if key_event.modifiers() == (Qt.KeyboardModifier.ControlModifier):
|
||||
self.commitData.emit(editor)
|
||||
@ -147,17 +146,14 @@ class EscapeDelegate(QStyledItemDelegate):
|
||||
return False
|
||||
|
||||
def setEditorData(self, editor, index):
|
||||
print("setEditorData()")
|
||||
value = index.model().data(index, Qt.ItemDataRole.EditRole)
|
||||
editor.setPlainText(value.value())
|
||||
|
||||
def setModelData(self, editor, model, index):
|
||||
print("setModelData")
|
||||
value = editor.toPlainText()
|
||||
model.setData(index, value, Qt.ItemDataRole.EditRole)
|
||||
|
||||
def updateEditorGeometry(self, editor, option, index):
|
||||
print("updateEditorGeometry")
|
||||
editor.setGeometry(option.rect)
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user