Skip to content

Commit 047742a

Browse files
authored
Add the ability to "favorite" songs and pin them to the the top of the songlist (#1066)
* add song favoriting * remove incorrectly placed sort() * store as qstringlist instead of using keys
1 parent 115c1cc commit 047742a

4 files changed

Lines changed: 77 additions & 4 deletions

File tree

src/aoapplication.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,9 @@ class AOApplication : public QObject
321321

322322
const QString default_theme = "default"; // don't change this!!! don't do it!!!
323323

324+
// The name of the currently connected server.
325+
QString server_name;
326+
324327
// The file name of the log file in base/logs.
325328
QString log_filename;
326329

src/courtroom.cpp

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,9 @@ Courtroom::Courtroom(AOApplication *p_ao_app)
150150
ui_area_list->setObjectName("ui_area_list");
151151

152152
ui_music_list = new QTreeWidget(this);
153-
ui_music_list->setColumnCount(2);
153+
ui_music_list->setColumnCount(3);
154154
ui_music_list->hideColumn(1);
155+
ui_music_list->hideColumn(2);
155156
ui_music_list->setHeaderHidden(true);
156157
ui_music_list->header()->setStretchLastSection(false);
157158
ui_music_list->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
@@ -1703,6 +1704,40 @@ void Courtroom::list_music()
17031704
QBrush missing_brush(ao_app->get_color("missing_song_color", f_file));
17041705

17051706
QTreeWidgetItem *parent = nullptr;
1707+
1708+
// Handle favorites first so they're at the top of the list
1709+
QSettings favorite_songs_ini(get_base_path() + "favorite_songs.ini", QSettings::IniFormat);
1710+
const QStringList &favorite_songs = favorite_songs_ini.value(ao_app->server_name).toStringList();
1711+
if (!favorite_songs.isEmpty())
1712+
{
1713+
QTreeWidgetItem *favCategory;
1714+
favCategory = new QTreeWidgetItem(ui_music_list);
1715+
favCategory->setText(0, tr("== FAVORITES =="));
1716+
favCategory->setText(1, tr("== FAVORITES =="));
1717+
favCategory->setText(2, "1");
1718+
favCategory->setBackground(0, missing_brush);
1719+
for (const QString &song : favorite_songs)
1720+
{
1721+
QTreeWidgetItem *favSong = new QTreeWidgetItem(favCategory);
1722+
QString f_song_listname = song.left(song.lastIndexOf("."));
1723+
1724+
favSong->setText(0, f_song_listname);
1725+
favSong->setText(1, song);
1726+
favSong->setText(2, "1");
1727+
1728+
QString song_path = ao_app->get_real_path(ao_app->get_music_path(song));
1729+
1730+
if (file_exists(song_path))
1731+
{
1732+
favSong->setBackground(0, found_brush);
1733+
}
1734+
else
1735+
{
1736+
favSong->setBackground(0, missing_brush);
1737+
}
1738+
}
1739+
}
1740+
17061741
for (int n_song = 0; n_song < music_list.size(); ++n_song)
17071742
{
17081743
QString i_song = music_list.at(n_song);
@@ -1730,6 +1765,7 @@ void Courtroom::list_music()
17301765
}
17311766
treeItem->setText(0, i_song_listname);
17321767
treeItem->setText(1, i_song);
1768+
treeItem->setText(2, "0");
17331769

17341770
QString song_path = ao_app->get_real_path(ao_app->get_music_path(i_song));
17351771

@@ -5749,6 +5785,18 @@ void Courtroom::on_music_list_context_menu_requested(const QPoint &pos)
57495785
menu->addAction(QString(tr("Collapse All Categories")), this, &Courtroom::music_list_collapse_all);
57505786
menu->addSeparator();
57515787

5788+
QTreeWidgetItem *current_song = ui_music_list->currentItem();
5789+
if (ui_music_list->currentItem()->text(2) == "1")
5790+
{
5791+
menu->addAction(QString(tr("Remove Favorite")), this, [this, current_song] { Courtroom::remove_favorite_song(current_song); });
5792+
menu->addSeparator();
5793+
}
5794+
else
5795+
{
5796+
menu->addAction(QString(tr("Add Favorite")), this, [this, current_song] { Courtroom::add_favorite_song(current_song); });
5797+
menu->addSeparator();
5798+
}
5799+
57525800
menu->addAction(new QAction(tr("Fade Out Previous"), this));
57535801
menu->actions().back()->setCheckable(true);
57545802
menu->actions().back()->setChecked(music_flags & FADE_OUT);
@@ -5777,6 +5825,26 @@ void Courtroom::on_music_list_context_menu_requested(const QPoint &pos)
57775825
menu->popup(ui_music_list->mapToGlobal(pos));
57785826
}
57795827

5828+
void Courtroom::add_favorite_song(QTreeWidgetItem *p_item)
5829+
{
5830+
QSettings favorite_songs_ini(get_base_path() + "favorite_songs.ini", QSettings::IniFormat);
5831+
QStringList favorite_songs = favorite_songs_ini.value(ao_app->server_name).toStringList();
5832+
favorite_songs.append(p_item->text(1));
5833+
5834+
favorite_songs_ini.setValue(ao_app->server_name, favorite_songs);
5835+
list_music();
5836+
}
5837+
5838+
void Courtroom::remove_favorite_song(QTreeWidgetItem *p_item)
5839+
{
5840+
QSettings favorite_songs_ini(get_base_path() + "favorite_songs.ini", QSettings::IniFormat);
5841+
QStringList favorite_songs = favorite_songs_ini.value(ao_app->server_name).toStringList();
5842+
favorite_songs.removeAll(p_item->text(1));
5843+
5844+
favorite_songs_ini.setValue(ao_app->server_name, favorite_songs);
5845+
list_music();
5846+
}
5847+
57805848
void Courtroom::music_fade_out(bool toggle)
57815849
{
57825850
if (toggle)

src/courtroom.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,8 @@ private Q_SLOTS:
834834
void on_music_search_edited(QString p_text);
835835
void on_music_list_double_clicked(QTreeWidgetItem *p_item, int column);
836836
void on_music_list_context_menu_requested(const QPoint &pos);
837+
void add_favorite_song(QTreeWidgetItem *p_item);
838+
void remove_favorite_song(QTreeWidgetItem *p_item);
837839
void music_fade_out(bool toggle);
838840
void music_fade_in(bool toggle);
839841
void music_synchronize(bool toggle);

src/packet_distribution.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ void AOApplication::server_packet_received(AOPacket packet)
120120

121121
int selected_server = w_lobby->get_selected_server();
122122
QString server_address;
123-
QString server_name;
124123
switch (w_lobby->pageSelected())
125124
{
126125
case 0:
@@ -161,10 +160,11 @@ void AOApplication::server_packet_received(AOPacket packet)
161160

162161
// Remove any characters not accepted in folder names for the server_name
163162
// here
163+
QString server_name_stripped = server_name;
164164
if (Options::getInstance().logToDemoFileEnabled() && server_name != "Demo playback")
165165
{
166-
this->log_filename = QDateTime::currentDateTime().toUTC().toString("'logs/" + server_name.remove(QRegularExpression("[\\\\/:*?\"<>|\']")) + "/'yyyy-MM-dd hh-mm-ss t'.log'");
167-
this->write_to_file("Joined server " + server_name + " hosted on address " + server_address + " on " + QDateTime::currentDateTime().toUTC().toString(), log_filename, true);
166+
this->log_filename = QDateTime::currentDateTime().toUTC().toString("'logs/" + server_name_stripped.remove(QRegularExpression("[\\\\/:*?\"<>|\']")) + "/'yyyy-MM-dd hh-mm-ss t'.log'");
167+
this->write_to_file("Joined server " + server_name_stripped + " hosted on address " + server_address + " on " + QDateTime::currentDateTime().toUTC().toString(), log_filename, true);
168168
}
169169
else
170170
{

0 commit comments

Comments
 (0)