Repository navigation
Expand file tree
/
Copy pathcatalog_table.cpp
More file actions
224 lines (202 loc) · 7.68 KB
/
Copy pathcatalog_table.cpp
File metadata and controls
224 lines (202 loc) · 7.68 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// MIT License
//
// Copyright (c) 2024 Piotr Pszczółkowski
//
// 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:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// 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.
//
// Created by Piotr Pszczółkowski on 25.10.2024.
// piotr@beesoft.pl
/*------- include files:
-------------------------------------------------------------------*/
#include "model/selection.h"
#include "catalog_table.h"
#include "shared/event.hh"
#include "shared/event_controller.hh"
#include <QDir>
#include <QMenu>
#include <QEvent>
#include <QAction>
#include <QFileInfo>
#include <QMouseEvent>
#include <QHeaderView>
#include <QTableWidgetItem>
#include <QContextMenuEvent>
#include <fmt/core.h>
FilesTable::FilesTable(QWidget* const parent) : QTableWidget(parent) {
setRowCount(0);
setColumnCount(1);
setEditTriggers(NoEditTriggers);
setSelectionBehavior(SelectRows);
setHorizontalHeaderItem(0, new QTableWidgetItem("Title"));
// When user double-clicked the row we play this song (one-shot).
connect(this, &QTableWidget::cellDoubleClicked, [&] (auto const row, auto const col){
if (auto const selected_item = item(row, 0)) {
auto const path = selected_item->data(PATH).toString();
EventController::self().send(event::SongOneShot, path);
}
});
// When user clicked the row it bekome current row.
// But important is checking is the row is checked.
connect(this, &QTableWidget::itemClicked, this, [this] (QTableWidgetItem* const item) {
if (item) {
auto checked = item->checkState();
auto const path = item->data(PATH).toString();
if (checked)
Selection::self().insert(path);
else
Selection::self().erase(path);
setCurrentItem(item);
update_parent();
}
});
EventController::self()
.append(this,
event::DirSelected,
event::CheckingAllSongs);
}
FilesTable::~FilesTable() {
EventController::self().remove(this);
}
void FilesTable::contextMenuEvent(QContextMenuEvent* const event) {
auto const menu = new QMenu(this);
auto const check_all_action = menu->addAction("Select all");
auto const uncheck_all_action = menu->addAction("Unselect all");
menu->addSeparator();
auto const invert_action = menu->addAction("Invert selection");
connect(check_all_action, &QAction::triggered, this, [this] (auto _) {
for (auto i = 0; i < rowCount(); ++i) {
auto const it = item(i, 0);
if (it->checkState() != Qt::Checked) {
it->setCheckState(Qt::Checked);
Selection::self().insert(it->data(PATH).toString());
}
}
update_parent();
});
connect(uncheck_all_action, &QAction::triggered, this, [this] (auto _) {
for (auto i = 0; i < rowCount(); ++i) {
auto const it = item(i, 0);
if (it->checkState() != Qt::Unchecked) {
it->setCheckState(Qt::Unchecked);
Selection::self().erase(it->data(PATH).toString());
}
}
update_parent();
});
connect(invert_action, &QAction::triggered, this, [this] (auto _) {
for (auto i = 0; i < rowCount(); ++i) {
auto const it = item(i, 0);
if (it->checkState() == Qt::Checked) {
it->setCheckState(Qt::Unchecked);
Selection::self().erase(it->data(PATH).toString());
}
else {
it->setCheckState(Qt::Checked);
Selection::self().insert(it->data(PATH).toString());
}
}
});
menu->exec(event->globalPos());
delete menu;
}
void FilesTable::mousePressEvent(QMouseEvent* const event) {
if (event->button() == Qt::RightButton)
return;
QTableWidget::mousePressEvent(event);
}
// Handle my own events.
void FilesTable::customEvent(QEvent* const event) {
auto const e = dynamic_cast<Event*>(event);
switch (int(e->type())) {
// Event with information that a new album has been selected.
case event::DirSelected:
if (auto const data = e->data(); !data.empty()) {
auto dir = data[0].toString();
dir_ = dir;
clear_content();
new_content_for(std::move(dir));
}
break;
// Event with a request to check/uncheck ALL items.
case event::CheckingAllSongs:
if (auto const data = e->data(); !data.empty()) {
auto const state = data[0].toBool() ? Qt::Checked : Qt::Unchecked;
auto const n = rowCount();
for (auto i = 0; i < n; ++i) {
auto const row = item(i, 0);
row->setCheckState(state);
if (state == Qt::Checked)
Selection::self().insert(row->data(PATH).toString());
else
Selection::self().erase(row->data(PATH).toString());
}
}
break;
}
}
/// New table content (new songs) for new selected directory.
void FilesTable::new_content_for(QString&& path) {
QDir const dir{path};
auto const info_list = dir.entryInfoList();
std::vector<QTableWidgetItem*> data{};
data.reserve(info_list.size());
for (auto const& fi : info_list) {
if (!fi.isDir() && fi.isFile()) {
auto const fname = fi.fileName();
if (fname[0] !='.' && (fname.endsWith(".m4a") || fname.endsWith(".mp3"))) {
auto item = new QTableWidgetItem(fname);
auto const path = fi.filePath();
if (Selection::self().contains(path))
item->setCheckState(Qt::Checked);
else
item->setCheckState(Qt::Unchecked);
item->setData(PATH, path);
data.push_back(item);
}
}
}
int row = 0;
setRowCount(data.size());
std::ranges::for_each(data, [&] (auto item) {
setItem(row++, 0, item);
});
horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
}
void FilesTable::update_parent() const noexcept {
if (are_all_unchecked())
EventController::self().send(event::NoSongsSelected, dir_);
else if (are_all_checked())
EventController::self().send(event::AllSongsSelected, dir_);
else
EventController::self().send(event::PartlySongsSelected, dir_);
}
bool FilesTable::are_all_checked() const noexcept {
auto const n = rowCount();
for (auto i = 0; i < n; ++i)
if (item(i, 0)->checkState() != Qt::Checked)
return false;
return true;
}
bool FilesTable::are_all_unchecked() const noexcept {
auto const n = rowCount();
for (auto i = 0; i < n; ++i)
if (item(i, 0)->checkState() != Qt::Unchecked)
return false;
return true;
}