Skip to content

Commit

Permalink
Implement ui-file reload and RCC mounting.
Browse files Browse the repository at this point in the history
  • Loading branch information
Salanto committed Jan 29, 2023
1 parent b961344 commit fc0156a
Show file tree
Hide file tree
Showing 12 changed files with 174 additions and 91 deletions.
7 changes: 6 additions & 1 deletion include/lobby.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ class Lobby : public QMainWindow {
int get_selected_server();

signals:
void settings_requested();


private:
AOApplication *ao_app;
NetworkManager *net_manager;

const QString DEFAULT_UI = "lobby.ui";

void list_favorites();
void list_demos();
void get_motd();
Expand Down Expand Up @@ -79,6 +80,8 @@ class Lobby : public QMainWindow {
QTextBrowser *ui_server_description_text;
QPushButton *ui_connect_button;

void loadUI();

private slots:
void on_tab_changed(int index);
void on_refresh_released();
Expand All @@ -93,6 +96,8 @@ private slots:
void on_favorite_tree_clicked(QTreeWidgetItem *p_item, int column);
void on_server_search_edited(QString p_text);
void on_demo_clicked(QTreeWidgetItem *item, int column);
void onReloadThemeRequested(); // Oh boy.
void onSettingsRequested();
};

#endif // LOBBY_H
9 changes: 9 additions & 0 deletions include/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class Options {
*/
Options();

QString m_server_subtheme;

public:
Options(Options const &) = delete;
void operator=(Options const &) = delete;
Expand Down Expand Up @@ -228,6 +230,10 @@ class Options {
QString subTheme() const;
void setSubTheme(QString value);

// Returns the server-
QString serverSubTheme() const;
void setServerSubTheme(QString value);

// Get if the theme is animated
bool animatedThemeEnabled() const;
void setAnimatedThemeEnabled(bool value);
Expand Down Expand Up @@ -274,6 +280,9 @@ class Options {
// Interactions with favorite servers
void removeFavorite(int index);
void addFavorite(server_type server);

// Theming Nonesense!
QString getUIAsset(QString f_asset_name);
};

#endif // OPTIONS_H
2 changes: 1 addition & 1 deletion include/widgets/add_server_dialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public :

const int TCP_INDEX = 0;

const QString DEFAULT_UI = ":/resource/ui/add_server_dialog.ui";
const QString DEFAULT_UI = "add_server_dialog.ui";

private slots:
void savePressed();
Expand Down
2 changes: 1 addition & 1 deletion include/widgets/direct_connect_dialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private slots:

const int TCP_INDEX = 0;
const int CONNECT_TIMEOUT = 5 * 1000;
const QString DEFAULT_UI = ":/resource/ui/direct_connect_dialog.ui";;
const QString DEFAULT_UI = "direct_connect_dialog.ui";;

};

Expand Down
2 changes: 0 additions & 2 deletions src/aoapplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ void AOApplication::construct_lobby()
if (demo_server)
demo_server->deleteLater();
demo_server = new DemoServer(this);

connect(w_lobby, &Lobby::settings_requested, this, &AOApplication::call_settings_menu);
w_lobby->show();
}

Expand Down
159 changes: 92 additions & 67 deletions src/lobby.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,88 @@
; \
ui_##name = findChild<type *>(#name);

#define COMBO_RELOAD() \
list_servers(); \
list_favorites(); \
list_demos(); \
get_motd(); \
check_for_updates(); \
reset_selection();

Lobby::Lobby(AOApplication *p_ao_app, NetworkManager *p_net_manager)
: QMainWindow()
{
ao_app = p_ao_app;
net_manager = p_net_manager;

loadUI();
COMBO_RELOAD()
}

void Lobby::on_tab_changed(int index)
{
switch (index) { // Implicit conversion cause FUCK ALL OF YOU.
case SERVER:
ui_add_to_favorite_button->setVisible(true);
ui_remove_from_favorites_button->setVisible(false);
ui_add_server_button->setVisible(false);
ui_direct_connect_button->setVisible(true);
reset_selection();
break;
case FAVORITES:
ui_add_to_favorite_button->setVisible(false);
ui_remove_from_favorites_button->setVisible(true);
ui_add_server_button->setVisible(true);
ui_direct_connect_button->setVisible(true);
reset_selection();
break;
case DEMOS:
ui_add_to_favorite_button->setVisible(false);
ui_add_server_button->setVisible(false);
ui_remove_from_favorites_button->setVisible(false);
ui_direct_connect_button->setVisible(false);
reset_selection();
break;
default:
break;
}
}

int Lobby::get_selected_server()
{
switch (ui_connections_tabview->currentIndex()) {
case SERVER:
if (auto item = ui_serverlist_tree->currentItem()) {
return item->text(0).toInt();
}
case FAVORITES:
if (auto item = ui_favorites_tree->currentItem()) {
return item->text(0).toInt();
}
default:
break;
}
return -1;
}

void Lobby::reset_selection()
{
last_index = -1;
ui_server_player_count_lbl->setText(tr("Offline"));
ui_server_description_text->clear();

ui_connect_button->setEnabled(false);
}

void Lobby::loadUI()
{
this->setWindowTitle(
tr("Attorney Online %1").arg(ao_app->applicationVersion()));
this->setWindowIcon(QIcon(":/logo.png"));
this->setWindowFlags((this->windowFlags() | Qt::CustomizeWindowHint));

QUiLoader l_loader(this);
QFile l_uiFile(":/resource/ui/lobby.ui");
QFile l_uiFile(Options::getInstance().getUIAsset(DEFAULT_UI));
if (!l_uiFile.open(QFile::ReadOnly)) {
qCritical() << "Unable to open file " << l_uiFile.fileName();
return;
Expand All @@ -41,7 +110,7 @@ Lobby::Lobby(AOApplication *p_ao_app, NetworkManager *p_net_manager)

FROM_UI(QPushButton, settings_button);
connect(ui_settings_button, &QPushButton::clicked, this,
&Lobby::settings_requested);
&Lobby::onSettingsRequested);

FROM_UI(QPushButton, about_button);
connect(ui_about_button, &QPushButton::clicked, this,
Expand Down Expand Up @@ -81,8 +150,8 @@ Lobby::Lobby(AOApplication *p_ao_app, NetworkManager *p_net_manager)
&Lobby::on_refresh_released);

FROM_UI(QPushButton, direct_connect_button);
connect(ui_direct_connect_button, &QPushButton::released,
this, &Lobby::on_direct_connect_released);
connect(ui_direct_connect_button, &QPushButton::released, this,
&Lobby::on_direct_connect_released);

FROM_UI(QPushButton, add_to_favorite_button)
connect(ui_add_to_favorite_button, &QPushButton::released, this,
Expand All @@ -107,67 +176,6 @@ Lobby::Lobby(AOApplication *p_ao_app, NetworkManager *p_net_manager)
&QPushButton::setEnabled);

FROM_UI(QTextBrowser, motd_text);

list_servers();
list_favorites();
list_demos();
get_motd();
check_for_updates();
}

void Lobby::on_tab_changed(int index)
{
switch (index) { // Implicit conversion cause FUCK ALL OF YOU.
case SERVER:
ui_add_to_favorite_button->setVisible(true);
ui_remove_from_favorites_button->setVisible(false);
ui_add_server_button->setVisible(false);
ui_direct_connect_button->setVisible(true);
reset_selection();
break;
case FAVORITES:
ui_add_to_favorite_button->setVisible(false);
ui_remove_from_favorites_button->setVisible(true);
ui_add_server_button->setVisible(true);
ui_direct_connect_button->setVisible(true);
reset_selection();
break;
case DEMOS:
ui_add_to_favorite_button->setVisible(false);
ui_add_server_button->setVisible(false);
ui_remove_from_favorites_button->setVisible(false);
ui_direct_connect_button->setVisible(false);
reset_selection();
break;
default:
break;
}
}

int Lobby::get_selected_server()
{
switch (ui_connections_tabview->currentIndex()) {
case SERVER:
if (auto item = ui_serverlist_tree->currentItem()) {
return item->text(0).toInt();
}
case FAVORITES:
if (auto item = ui_favorites_tree->currentItem()) {
return item->text(0).toInt();
}
default:
break;
}
return -1;
}

void Lobby::reset_selection()
{
last_index = -1;
ui_server_player_count_lbl->setText(tr("Offline"));
ui_server_description_text->clear();

ui_connect_button->setEnabled(false);
}

void Lobby::on_refresh_released()
Expand All @@ -179,8 +187,8 @@ void Lobby::on_refresh_released()

void Lobby::on_direct_connect_released()
{
DirectConnectDialog connect_dialog(net_manager);
connect_dialog.exec();
DirectConnectDialog connect_dialog(net_manager);
connect_dialog.exec();
}

void Lobby::on_add_to_fav_released()
Expand Down Expand Up @@ -382,6 +390,23 @@ void Lobby::on_demo_clicked(QTreeWidgetItem *item, int column)
net_manager->connect_to_server(demo_server);
}

void Lobby::onReloadThemeRequested()
{
// This is destructive to the active widget data.
// Whatever, this is lobby. Nothing here is worth saving.
delete centralWidget();
loadUI();
COMBO_RELOAD()
}

void Lobby::onSettingsRequested()
{
AOOptionsDialog options(nullptr, ao_app);
connect(&options, &AOOptionsDialog::reloadThemeRequest, this,
&Lobby::onReloadThemeRequested);
options.exec();
}

void Lobby::list_servers()
{
ui_serverlist_tree->setSortingEnabled(false);
Expand Down
2 changes: 2 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <QLibraryInfo>
#include <QPluginLoader>
#include <QTranslator>
#include <QResource>

int main(int argc, char *argv[])
{
Expand All @@ -17,6 +18,7 @@ int main(int argc, char *argv[])
AOApplication main_app(argc, argv);

AOApplication::addLibraryPath(AOApplication::applicationDirPath() + "/lib");
QResource::registerResource(main_app.get_asset("themes/" + Options::getInstance().theme() + ".rcc"));

QFontDatabase fontDatabase;
QDirIterator it(main_app.get_base_path() + "fonts",
Expand Down
60 changes: 45 additions & 15 deletions src/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,10 @@ QString Options::subTheme() const

void Options::setSubTheme(QString value) { config.setValue("subtheme", value); }

QString Options::serverSubTheme() const { return m_server_subtheme; }

void Options::setServerSubTheme(QString value) { m_server_subtheme = value; }

bool Options::animatedThemeEnabled() const
{
return config.value("animated_theme", true).toBool();
Expand Down Expand Up @@ -624,25 +628,51 @@ void Options::setFavorites(QVector<server_type> value)

void Options::removeFavorite(int index)
{
QVector<server_type> l_favorites = favorites();
l_favorites.remove(index);
setFavorites(l_favorites);
QVector<server_type> l_favorites = favorites();
l_favorites.remove(index);
setFavorites(l_favorites);
}

void Options::addFavorite(server_type server)
{
int index = favorites().size();
favorite.beginGroup(QString::number(index));
favorite.setValue("name", server.name);
favorite.setValue("address", server.ip);
favorite.setValue("port", server.port);
favorite.setValue("desc", server.desc);
if (server.socket_type == TCP) {
favorite.setValue("protocol", "tcp");
int index = favorites().size();
favorite.beginGroup(QString::number(index));
favorite.setValue("name", server.name);
favorite.setValue("address", server.ip);
favorite.setValue("port", server.port);
favorite.setValue("desc", server.desc);
if (server.socket_type == TCP) {
favorite.setValue("protocol", "tcp");
}
else {
favorite.setValue("protocol", "ws");
}
favorite.endGroup();
favorite.sync();
}

QString Options::getUIAsset(QString f_asset_name)
{
QStringList l_paths{":/base/themes/" + Options::getInstance().theme() + "/" +
f_asset_name};

if (Options::getInstance().subTheme() == "server") {
if (!Options::getInstance().serverSubTheme().isEmpty()) {
l_paths.prepend(":/base/themes/" + Options::getInstance().theme() + "/" +
Options::getInstance().subTheme() + "/" + f_asset_name);
}
else {
favorite.setValue("protocol", "ws");
}
else {
l_paths.prepend(":/base/themes/" + Options::getInstance().theme() + "/" +
Options::getInstance().subTheme() + "/" + f_asset_name);
}

for (const QString &l_path : qAsConst(l_paths)) {
if (QFile::exists(l_path)) {
return l_path;
}
favorite.endGroup();
favorite.sync();
}
qWarning() << "Unable to locate ui-asset" << f_asset_name << "in theme"
<< theme() << "Defaulting to embeeded asset.";
return QString(":/resource/ui/" + f_asset_name);
}
Loading

0 comments on commit fc0156a

Please sign in to comment.