Skip to content

Commit c442fef

Browse files
committed
table with songs, playback of selected song
1 parent 68496cd commit c442fef

9 files changed

+186
-23
lines changed

catalog.cpp

+15-6
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,27 @@
22
#include "dirs_tree.h"
33
#include "files_table.h"
44
#include <QSplitter>
5+
#include <QSplitter>
6+
#include <QShowEvent>
57
#include <QVBoxLayout>
68

7-
Catalog::Catalog(QWidget *parent):
8-
QWidget{parent},
9+
Catalog::Catalog(QWidget *parent) : QWidget{parent},
10+
splitter_{new QSplitter(Qt::Horizontal)},
911
dirs_{new DirsTree},
1012
files_{new FilesTable}
1113
{
12-
auto const splitter = new QSplitter(Qt::Horizontal);
13-
splitter->addWidget(dirs_);
14-
splitter->addWidget(files_);
14+
splitter_->setHandleWidth(SPLITTER_HANDLE_WIFTH);
15+
splitter_->addWidget(dirs_);
16+
splitter_->addWidget(files_);
1517

1618
auto const main = new QVBoxLayout;
17-
main->addWidget(splitter);
19+
main->addWidget(splitter_);
1820
setLayout(main);
1921
}
22+
23+
void Catalog::showEvent(QShowEvent* e) {
24+
auto const width = size().width();
25+
auto const w0 = 25. * width / 100.;
26+
auto const w1 = width - splitter_->handleWidth() - w0;
27+
splitter_->setSizes({static_cast<int>(w0), static_cast<int>(w1)});
28+
}

catalog.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@
66

77
class DirsTree;
88
class FilesTable;
9-
9+
class QSplitter;
10+
class QShowEvent;
1011

1112
class Catalog : public QWidget {
1213
Q_OBJECT
14+
static constexpr int SPLITTER_HANDLE_WIFTH{3};
15+
QSplitter* const splitter_;
1316
DirsTree* const dirs_;
1417
FilesTable* const files_;
1518
public:
1619
explicit Catalog(QWidget *parent = nullptr);
17-
20+
private:
21+
void showEvent(QShowEvent*) override;
1822
};

control_bar.cpp

+18-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22
/*------- include files:
33
-------------------------------------------------------------------*/
44
#include "control_bar.h"
5+
#include "shared/event.hh"
6+
#include "shared/event_controller.hh"
57
#include <QUrl>
68
#include <QDir>
79
#include <QIcon>
810
#include <QLabel>
11+
#include <QEvent>
912
#include <QSlider>
1013
#include <QStyle>
1114
#include <QFileInfo>
@@ -136,7 +139,21 @@ ControlBar::ControlBar(QWidget *parent) :
136139
layout->setContentsMargins(0, 0, 0, 0);
137140
setLayout(layout);
138141

139-
set_song("/home/piotr/Music/Achim Reichel/Melancholie und Sturmflut/05 Aloha Heja He.m4a");
142+
EventController::instance().append(this, event::SongSelected);
143+
}
144+
145+
ControlBar::~ControlBar() {
146+
EventController::instance().remove(this);
147+
}
148+
149+
void ControlBar::customEvent(QEvent* const event) {
150+
auto const e = dynamic_cast<Event*>(event);
151+
switch (int(e->type())) {
152+
case event::SongSelected:
153+
if (auto const data = e->data(); !data.empty())
154+
set_song(data[0].toString());
155+
break;
156+
}
140157
}
141158

142159
void ControlBar::set_song(QString const& path) noexcept {

control_bar.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
-------------------------------------------------------------------*/
1010
class QLabel;
1111
class QSlider;
12+
class QEvent;
1213
class QPushButton;
1314
class QMediaPlayer;
1415
class QAudioOutput;
@@ -41,8 +42,9 @@ class ControlBar : public QWidget {
4142
QString song_path_{};
4243
public:
4344
explicit ControlBar(QWidget *parent = nullptr);
44-
45+
~ControlBar();
4546
private:
4647
void set_song(QString const& path) noexcept;
48+
void customEvent(QEvent*) override;
4749
};
4850

dirs_tree.cpp

+5-8
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
// SOFTWARE.
2222
//
23-
// Created by Piotr Pszczółkowski on 24.10.2024.
23+
// Created by Piotr Pszczółkowski on 25.10.2024.
2424
// piotr@beesoft.pl
2525

2626
/*------- include files:
@@ -49,8 +49,8 @@ DirsTree::DirsTree(QWidget* const parent) :
4949
timer_->setSingleShot(true);
5050
connect(timer_, &QTimer::timeout, this, [this]() {
5151
if (auto item = currentItem(); item) {
52-
auto const path{item->data(0, PATH).toString()};
53-
EventController::instance().send(event::DirSelected, path);
52+
auto path{item->data(0, PATH).toString()};
53+
EventController::instance().send(event::DirSelected, std::move(path));
5454
}
5555
});
5656

@@ -73,6 +73,8 @@ DirsTree::DirsTree(QWidget* const parent) :
7373
setCurrentItem(root_);
7474
}
7575

76+
77+
7678
void DirsTree::update_content(QString const& path) {
7779
clear();
7880
root_ = new QTreeWidgetItem(this);
@@ -87,8 +89,6 @@ void DirsTree::update_content(QString const& path) {
8789

8890
void DirsTree::add_items_for(QTreeWidgetItem* const parent) {
8991
auto parent_path = parent->data(0, PATH).toString();
90-
// fmt::print(stderr, "parent path: {}\n", parent_path.toStdString());
91-
9292
QDir const dir{parent_path};
9393
auto const content = dir.entryInfoList();
9494

@@ -99,9 +99,6 @@ void DirsTree::add_items_for(QTreeWidgetItem* const parent) {
9999
auto const fname = fi.fileName();
100100
if (fname[0] !='.') {
101101
auto const item = new QTreeWidgetItem(parent);
102-
// fmt::print(stderr, "{}\n", fi.fileName().toStdString());
103-
// fmt::print(stderr, "{}\n", fi.filePath().toStdString());
104-
105102
item->setText(0, fname);
106103
item->setData(0, PATH, fi.filePath());
107104
item->setData(0, PID, pid);

dirs_tree.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
// SOFTWARE.
2222
//
23-
// Created by Piotr Pszczółkowski on 24.10.2024.
23+
// Created by Piotr Pszczółkowski on 25.10.2024.
2424
// piotr@beesoft.pl
2525
#pragma once
2626

@@ -32,6 +32,7 @@
3232
-------------------------------------------------------------------*/
3333
class QTimer;
3434
class QEvent;
35+
class QShowEvent;
3536
class QMouseEvent;
3637
class QTreeWidgetItem;
3738

files_table.cpp

+92-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,94 @@
1+
// MIT License
2+
//
3+
// Copyright (c) 2024 Piotr Pszczółkowski
4+
//
5+
// Permission is hereby granted, free of charge, to any person obtaining a copy
6+
// of this software and associated documentation files (the "Software"), to deal
7+
// in the Software without restriction, including without limitation the rights
8+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// copies of the Software, and to permit persons to whom the Software is
10+
// furnished to do so, subject to the following conditions:
11+
//
12+
// The above copyright notice and this permission notice shall be included in all
13+
// copies or substantial portions of the Software.
14+
//
15+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
// SOFTWARE.
22+
//
23+
// Created by Piotr Pszczółkowski on 25.10.2024.
24+
// piotr@beesoft.pl
25+
26+
/*------- include files:
27+
-------------------------------------------------------------------*/
128
#include "files_table.h"
29+
#include "shared/event.hh"
30+
#include "shared/event_controller.hh"
31+
#include <QDir>
32+
#include <QHeaderView>
33+
#include <QTableWidgetItem>
34+
#include <fmt/core.h>
35+
36+
FilesTable::FilesTable(QWidget* const parent) : QTableWidget(parent) {
37+
setRowCount(0);
38+
setColumnCount(1);
39+
setEditTriggers(NoEditTriggers);
40+
setSelectionBehavior(SelectRows);
41+
setHorizontalHeaderItem(0, new QTableWidgetItem("Title"));
42+
43+
connect(this, &QTableWidget::cellDoubleClicked, [&] (auto const row, auto const col){
44+
if (auto const selected_item = item(row, 0)) {
45+
auto const path = selected_item->data(PATH).toString();
46+
EventController::instance().send(event::SongSelected, path);
47+
}
48+
});
49+
50+
EventController::instance().append(this,
51+
event::DirSelected
52+
);
53+
}
54+
55+
FilesTable::~FilesTable() {
56+
EventController::instance().remove(this);
57+
}
58+
59+
void FilesTable::customEvent(QEvent* const event) {
60+
auto const e = dynamic_cast<Event*>(event);
61+
switch (int(e->type())) {
62+
case event::DirSelected:
63+
if (auto const data = e->data(); !data.empty()) {
64+
clear_content();
65+
new_content_for(data[0].toString());
66+
}
67+
break;
68+
}
69+
}
70+
71+
void FilesTable::new_content_for(QString&& path) {
72+
QDir const dir{path};
73+
auto const info_list = dir.entryInfoList();
74+
75+
std::vector<QTableWidgetItem*> data{};
76+
data.reserve(info_list.size());
77+
for (auto const& fi : info_list) {
78+
if (!fi.isDir() && fi.isFile()) {
79+
auto const fname = fi.fileName();
80+
if (fname[0] !='.' && (fname.endsWith(".m4a") || fname.endsWith(".mp3"))) {
81+
auto item = new QTableWidgetItem(fname);
82+
item->setData(PATH, fi.filePath());
83+
data.push_back(item);
84+
}
85+
}
86+
}
287

3-
FilesTable::FilesTable(QWidget* const parent):
4-
QTableWidget(parent)
5-
{}
88+
int row = 0;
89+
setRowCount(data.size());
90+
std::ranges::for_each(data, [&] (auto item) {
91+
setItem(row++, 0, item);
92+
});
93+
horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
94+
}

files_table.h

+44
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,53 @@
1+
// MIT License
2+
//
3+
// Copyright (c) 2024 Piotr Pszczółkowski
4+
//
5+
// Permission is hereby granted, free of charge, to any person obtaining a copy
6+
// of this software and associated documentation files (the "Software"), to deal
7+
// in the Software without restriction, including without limitation the rights
8+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// copies of the Software, and to permit persons to whom the Software is
10+
// furnished to do so, subject to the following conditions:
11+
//
12+
// The above copyright notice and this permission notice shall be included in all
13+
// copies or substantial portions of the Software.
14+
//
15+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
// SOFTWARE.
22+
//
23+
// Created by Piotr Pszczółkowski on 25.10.2024.
24+
// piotr@beesoft.pl
25+
126
#pragma once
227

28+
/*------- include files:
29+
-------------------------------------------------------------------*/
330
#include <QTableWidget>
31+
#include <vector>
32+
33+
/*------- forward declarations:
34+
-------------------------------------------------------------------*/
35+
class QEvent;
36+
437

538
class FilesTable : public QTableWidget {
639
Q_OBJECT
40+
enum {PATH = Qt::UserRole + 1};
41+
742
public:
843
FilesTable(QWidget* = nullptr);
44+
~FilesTable();
45+
private:
46+
void customEvent(QEvent*) override;
47+
void new_content_for(QString&& path);
48+
49+
void clear_content() noexcept {
50+
clearContents();
51+
setRowCount(0);
52+
}
953
};

shared/event.hh

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@ namespace event {
5757
PlaybackPoition,
5858

5959
DirSelected,
60-
FileSeleced,
60+
SongSelected,
6161
};
6262
}

0 commit comments

Comments
 (0)