summaryrefslogtreecommitdiff
path: root/Userland/Applications/SoundPlayer/PlaylistWidget.cpp
blob: 8ef466c6f9b8996c34520665a8c973d6475f9b68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
 * Copyright (c) 2021, Cesar Torres <shortanemoia@protonmail.com>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include "PlaylistWidget.h"
#include "Player.h"
#include <AK/LexicalPath.h>
#include <LibGUI/BoxLayout.h>
#include <LibGUI/HeaderView.h>
#include <LibGUI/Model.h>
#include <LibGUI/Window.h>

PlaylistWidget::PlaylistWidget()
{
    set_layout<GUI::VerticalBoxLayout>();
    m_table_view = add<PlaylistTableView>();
    m_table_view->set_selection_mode(GUI::AbstractView::SelectionMode::SingleSelection);
    m_table_view->set_selection_behavior(GUI::AbstractView::SelectionBehavior::SelectRows);
    m_table_view->set_highlight_selected_rows(true);
    m_table_view->on_doubleclick = [&](Gfx::Point<int> const& point) {
        auto player = dynamic_cast<Player*>(window()->main_widget());
        auto index = m_table_view->index_at_event_position(point);
        if (!index.is_valid())
            return;
        player->play_file_path(m_table_view->model()->data(index, static_cast<GUI::ModelRole>(PlaylistModelCustomRole::FilePath)).as_string());
    };
}

GUI::Variant PlaylistModel::data(const GUI::ModelIndex& index, GUI::ModelRole role) const
{
    if (role == GUI::ModelRole::TextAlignment)
        return "CenterLeft";
    if (role == GUI::ModelRole::Display) {
        switch (index.column()) {
        case 0:
            return m_playlist_items[index.row()].extended_info->track_display_title.value_or(LexicalPath::title(m_playlist_items[index.row()].path));
        case 1:
            return format_duration(m_playlist_items[index.row()].extended_info->track_length_in_seconds.value_or(0));
        case 2:
            return m_playlist_items[index.row()].extended_info->group_name.value_or("");
        case 3:
            return m_playlist_items[index.row()].extended_info->album_title.value_or("");
        case 4:
            return m_playlist_items[index.row()].extended_info->album_artist.value_or("");
        case 5:
            return format_filesize(m_playlist_items[index.row()].extended_info->file_size_in_bytes.value_or(0));
        }
    }
    if (role == GUI::ModelRole::Sort)
        return data(index, GUI::ModelRole::Display);
    if (role == static_cast<GUI::ModelRole>(PlaylistModelCustomRole::FilePath)) // path
        return m_playlist_items[index.row()].path;

    return {};
}

DeprecatedString PlaylistModel::format_filesize(u64 size_in_bytes)
{
    if (size_in_bytes > GiB)
        return DeprecatedString::formatted("{:.2f} GiB", (double)size_in_bytes / GiB);
    else if (size_in_bytes > MiB)
        return DeprecatedString::formatted("{:.2f} MiB", (double)size_in_bytes / MiB);
    else if (size_in_bytes > KiB)
        return DeprecatedString::formatted("{:.2f} KiB", (double)size_in_bytes / KiB);
    else
        return DeprecatedString::formatted("{} B", size_in_bytes);
}

DeprecatedString PlaylistModel::format_duration(u32 duration_in_seconds)
{
    return DeprecatedString::formatted("{:02}:{:02}:{:02}", duration_in_seconds / 3600, duration_in_seconds / 60, duration_in_seconds % 60);
}

DeprecatedString PlaylistModel::column_name(int column) const
{
    switch (column) {
    case 0:
        return "Title";
    case 1:
        return "Duration";
    case 2:
        return "Group";
    case 3:
        return "Album";
    case 4:
        return "Artist";
    case 5:
        return "Filesize";
    }
    VERIFY_NOT_REACHED();
}

PlaylistTableView::PlaylistTableView() = default;

void PlaylistTableView::doubleclick_event(GUI::MouseEvent& event)
{
    AbstractView::doubleclick_event(event);
    if (event.button() == GUI::Primary) {
        if (on_doubleclick)
            on_doubleclick(event.position());
    }
}