Debian Codegraph @

BambooTracker/gui/hide_tracks_dialog.cpp

raw
1 /*
2 * Copyright (C) 2020-2021 Rerrah
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use,
8 * copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following
11 * conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 #include "hide_tracks_dialog.hpp"
27 #include "ui_hide_tracks_dialog.h"
28 #include <algorithm>
29 #include <QListWidgetItem>
30 #include "song.hpp"
31 #include "gui/gui_utils.hpp"
32
33 HideTracksDialog::HideTracksDialog(const SongStyle& style, const std::vector<int>& tracks, QWidget *parent)
34 : QDialog(parent),
35 ui(new Ui::HideTracksDialog)
36 {
37 ui->setupUi(this);
38
39 setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
40
41 for (const auto& attrib : style.trackAttribs) {
42 auto item = new QListWidgetItem(gui_utils::getTrackName(style.type, attrib.source, attrib.channelInSource), ui->listWidget);
43 int n = attrib.number;
44 item->setData(Qt::UserRole, n); // Track numbers are sorted
45 bool exists = std::any_of(tracks.begin(), tracks.end(), [n](int t) { return n == t; });
46 item->setCheckState(exists ? Qt::Checked : Qt::Unchecked);
47 }
48 QObject::connect(ui->listWidget, &QListWidget::itemChanged,
49 this, [&](QListWidgetItem* item) {
50 if (item->checkState() == Qt::Unchecked) {
51 --checkCounter_;
52 if (!checkCounter_) ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
53 }
54 else {
55 ++checkCounter_;
56 ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
57 }
58 });
59 checkCounter_ = style.trackAttribs.size();
60 }
61
62 HideTracksDialog::~HideTracksDialog()
63 {
64 delete ui;
65 }
66
67 std::vector<int> HideTracksDialog::getVisibleTracks() const
68 {
69 std::vector<int> tracks;
70 for (int i = 0; i < ui->listWidget->count(); ++i) {
71 auto item = ui->listWidget->item(i);
72 if (item->checkState() == Qt::Checked)
73 tracks.push_back(item->data(Qt::UserRole).toInt());
74 }
75 return tracks;
76 }
77
78 void HideTracksDialog::on_reversePushButton_clicked()
79 {
80 for (int i = 0; i < ui->listWidget->count(); ++i) {
81 auto item = ui->listWidget->item(i);
82 item->setCheckState((item->checkState() == Qt::Checked) ? Qt::Unchecked : Qt::Checked);
83 }
84 }
85
86 void HideTracksDialog::on_checkAllPushButton_clicked()
87 {
88 for (int i = 0; i < ui->listWidget->count(); ++i) {
89 auto item = ui->listWidget->item(i);
90 item->setCheckState(Qt::Checked);
91 }
92 }