Commit 72352024 authored by Jean Felder's avatar Jean Felder Committed by Jean Felder

playlistdialog: Port to GtkListBox

Replace the treeview with a listbox. This improves code readability
and this is a necessary step for the future core rewrite.
A PlaylistDialogRow class is also introduced to simplify the creation
of the listbox rows.
parent e4f55955
......@@ -19,6 +19,7 @@
<file preprocess="xml-stripblanks">ui/PlaylistContextMenu.ui</file>
<file preprocess="xml-stripblanks">ui/PlaylistControls.ui</file>
<file preprocess="xml-stripblanks">ui/PlaylistDialog.ui</file>
<file preprocess="xml-stripblanks">ui/PlaylistDialogRow.ui</file>
<file preprocess="xml-stripblanks">ui/SearchBar.ui</file>
<file preprocess="xml-stripblanks">ui/SelectionBarMenuButton.ui</file>
<file preprocess="xml-stripblanks">ui/SelectionToolbar.ui</file>
......@@ -201,19 +201,11 @@
<property name="can_focus">True</property>
<property name="vexpand">True</property>
<object class="GtkTreeView" id="_view">
<object class="GtkListBox" id="_listbox">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="model">_model</property>
<property name="headers_visible">False</property>
<property name="search_column">0</property>
<property name="activate_on_single_click">True</property>
<signal name="row-activated" handler="_on_item_activated" swapped="no"/>
<child internal-child="selection">
<object class="GtkTreeSelection" id="_selection">
<signal name="changed" handler="_on_selection_changed" swapped="no"/>
<property name="selection_mode">single</property>
<signal name="row-selected" handler="_on_row_selected" swapped="no"/>
......@@ -279,12 +271,4 @@
<object class="GtkListStore" id="_model">
<!-- column-name playlist-name -->
<column type="gchararray"/>
<!-- column-name item -->
<column type="GObject"/>
<?xml version="1.0" encoding="UTF-8"?>
<template class="PlaylistDialogRow" parent="GtkListBoxRow">
<property name="visible">True</property>
<class name="playlistdialog-row"/>
<object class="GtkLabel" id="_label">
<property name="ellipsize">end</property>
<property name="margin">8</property>
<property name="visible">True</property>
<property name="xalign">0.0</property>
......@@ -22,12 +22,12 @@
# code, but you are not obligated to do so. If you do not wish to do so,
# delete this exception statement from your version.
from gi.repository import Gtk, Pango
from gi.repository import Gtk
from gnomemusic import log
from gnomemusic.grilo import grilo
from gnomemusic.playlists import Playlists
import gnomemusic.utils as utils
from gnomemusic.widgets.playlistdialogrow import PlaylistDialogRow
......@@ -40,9 +40,7 @@ class PlaylistDialog(Gtk.Dialog):
_normal_box = Gtk.Template.Child()
_empty_box = Gtk.Template.Child()
_title_bar = Gtk.Template.Child()
_view = Gtk.Template.Child()
_selection = Gtk.Template.Child()
_model = Gtk.Template.Child()
_listbox = Gtk.Template.Child()
_cancel_button = Gtk.Template.Child()
_select_button = Gtk.Template.Child()
_new_playlist_button = Gtk.Template.Child()
......@@ -62,7 +60,6 @@ class PlaylistDialog(Gtk.Dialog):
self.props.transient_for = parent
self._playlists_todelete_ids = playlists_todelete.keys()
......@@ -74,20 +71,12 @@ class PlaylistDialog(Gtk.Dialog):
def get_selected(self):
"""Get the selected playlist"""
_iter = self._selection.get_selected()[1]
selected_row = self._listbox.get_selected_row()
if not _iter:
if not selected_row:
return None
return self._model[_iter][1]
def _add_list_renderers(self):
type_renderer = Gtk.CellRendererText(
xpad=8, ypad=8, ellipsize=Pango.EllipsizeMode.END, xalign=0.0)
col = Gtk.TreeViewColumn("Name", type_renderer, text=0)
return selected_row.props.playlist
def _set_view(self):
......@@ -109,12 +98,12 @@ class PlaylistDialog(Gtk.Dialog):
def _add_item(self, source, param, item, remaining=0, data=None):
if item:
if remaining == 0:
def _add_item_to_model(self, item):
def _add_playlist_to_listbox(self, item):
"""Adds (non-smart only) playlists to the model"""
# Hide playlists that are going to be deleted
......@@ -122,10 +111,10 @@ class PlaylistDialog(Gtk.Dialog):
return None
self._user_playlists_available = True
new_iter = self._model.insert_with_valuesv(
-1, [0, 1], [utils.get_media_title(item), item])
row = PlaylistDialogRow(item)
self._listbox.insert(row, -1)
return new_iter
return row
......@@ -139,15 +128,10 @@ class PlaylistDialog(Gtk.Dialog):
def _on_item_activated(self, view, path, column):
def _on_row_selected(self, listbox, row):
self._add_playlist_entry.props.text = ""
self._add_playlist_button.props.sensitive = False
def _on_selection_changed(self, selection):
model, _iter = self._selection.get_selected()
self._select_button.props.sensitive = _iter is not None
self._select_button.props.sensitive = row is not None
......@@ -158,13 +142,11 @@ class PlaylistDialog(Gtk.Dialog):
def _on_playlist_created(self, playlists, item):
new_iter = self._add_item_to_model(item)
if new_iter and self._view.get_columns():
col0 = self._view.get_columns()[0]
path = self._model.get_path(new_iter)
self._view.set_cursor(path, col0, False)
self._view.row_activated(path, col0)
row = self._add_playlist_to_listbox(item)
if not row:
......@@ -177,4 +159,4 @@ class PlaylistDialog(Gtk.Dialog):
def _on_add_playlist_entry_focused(self, editable, data=None):
# Copyright 2019 The GNOME Music Developers
# GNOME Music 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 2 of the License, or
# (at your option) any later version.
# GNOME Music is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along
# with GNOME Music; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
# The GNOME Music authors hereby grant permission for non-GPL compatible
# GStreamer plugins to be used and distributed together with GStreamer
# and GNOME Music. This permission is above and beyond the permissions
# granted by the GPL license by which GNOME Music is covered. If you
# modify this code, you may extend this exception to your version of the
# code, but you are not obligated to do so. If you do not wish to do so,
# delete this exception statement from your version.
from gi.repository import GObject, Grl, Gtk
import gnomemusic.utils as utils
class PlaylistDialogRow(Gtk.ListBoxRow):
__gtype_name__ = "PlaylistDialogRow"
playlist = GObject.Property(type=Grl.Media, default=None)
_label = Gtk.Template.Child()
def __repr__(self):
return "PlaylistDialogRow"
def __init__(self, playlist):
"""Create a row of the PlaylistDialog
:param Grl.Media playlist: the associated playlist
self.props.playlist = playlist
self._label.props.label = utils.get_media_title(playlist)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment