From 50fe02cd77408da05ee33844085c8bdd5b361c96 Mon Sep 17 00:00:00 2001 From: David Skoland Date: Wed, 18 Jan 2017 15:39:04 +0100 Subject: [PATCH] changing structure from globals to OO with a flat construction --- aoapplication.cpp | 29 ++++++++++++++++++ aoapplication.h | 42 ++++++++++++++++++++++++-- aobutton.cpp | 8 ++--- aobutton.h | 6 +++- aocharbutton.cpp | 8 +++-- aocharbutton.h | 6 +++- aoimage.cpp | 9 +++--- aoimage.h | 7 ++++- courtroom.cpp | 66 ++++++++++++++++++++--------------------- courtroom.h | 2 +- global_variables.cpp | 1 + global_variables.h | 2 +- lobby.cpp | 45 ++++++++++++++-------------- lobby.h | 2 +- path_functions.cpp | 14 ++++----- path_functions.h | 6 +--- text_file_functions.cpp | 10 +++---- text_file_functions.h | 5 +--- 18 files changed, 172 insertions(+), 96 deletions(-) diff --git a/aoapplication.cpp b/aoapplication.cpp index d337601..87525a8 100644 --- a/aoapplication.cpp +++ b/aoapplication.cpp @@ -3,6 +3,7 @@ #include "lobby.h" #include "courtroom.h" #include "networkmanager.h" +#include "text_file_functions.h" #include "aoapplication.h" @@ -68,4 +69,32 @@ void AOApplication::destruct_courtroom() courtroom_constructed = false; } +QString AOApplication::get_version_string(){ + return + QString::number(RELEASE) + "." + + QString::number(MAJOR_VERSION) + "." + + QString::number(MINOR_VERSION); +} +void AOApplication::set_user_theme(){ + user_theme = read_user_theme(); +} + +void AOApplication::set_favorite_list() +{ + favorite_list = read_serverlist_txt(); +} + +void AOApplication::add_favorite_server(int p_server) +{ + if (p_server < 0 || p_server >= server_list.size()) + return; + + server_type fav_server = server_list.at(p_server); + + QString str_port = QString::number(fav_server.port); + + QString server_line = fav_server.ip + ":" + str_port + ":" + fav_server.name; + + write_to_serverlist_txt(server_line); +} diff --git a/aoapplication.h b/aoapplication.h index 34f1fef..77d7af0 100644 --- a/aoapplication.h +++ b/aoapplication.h @@ -6,6 +6,7 @@ #include #include +#include class NetworkManager; class Lobby; @@ -32,9 +33,6 @@ public: void construct_courtroom(); void destruct_courtroom(); - QVector server_list; - QVector favorite_list; - void ms_packet_received(AOPacket *p_packet); void server_packet_received(AOPacket *p_packet); @@ -50,6 +48,44 @@ public: bool s_pv = 0; ////////////////////////////////////////////////// + + int get_release() {return RELEASE;} + int get_major_version() {return MAJOR_VERSION;} + int get_minor_version() {return MINOR_VERSION;} + QString get_version_string(); + + void set_favorite_list(); + QVector& get_favorite_list() {return favorite_list;} + void add_favorite_server(int p_server); + + void set_server_list(); + QVector& get_server_list() {return server_list;} + + void set_user_theme(); + QString get_user_theme() {return user_theme;} + + //path functions + QString get_base_path(); + QString get_theme_path(); + QString get_default_theme_path(); + QString get_character_path(QString p_character); + QString get_demothings_path(); + + //text file functions + QString read_user_theme(); + void write_to_serverlist_txt(QString p_line); + QVector read_serverlist_txt(); + pos_size_type get_pos_and_size(QString p_identifier, QString p_design_path); + +private: + const int RELEASE = 2; + const int MAJOR_VERSION = 1; + const int MINOR_VERSION = 0; + + QString user_theme = "default"; + + QVector server_list; + QVector favorite_list; }; #endif // AOAPPLICATION_H diff --git a/aobutton.cpp b/aobutton.cpp index 9f6ba23..0938f51 100644 --- a/aobutton.cpp +++ b/aobutton.cpp @@ -6,9 +6,9 @@ #include -AOButton::AOButton(QWidget *parent) : QPushButton(parent) +AOButton::AOButton(QWidget *parent, AOApplication *p_ao_app) : QPushButton(parent) { - + ao_app = p_ao_app; } AOButton::~AOButton() @@ -18,8 +18,8 @@ AOButton::~AOButton() void AOButton::set_image(QString p_image) { - QString image_path = get_theme_path() + p_image; - QString default_image_path = get_default_theme_path() + p_image; + QString image_path = ao_app->get_theme_path() + p_image; + QString default_image_path = ao_app->get_default_theme_path() + p_image; if (file_exists(image_path)) this->setStyleSheet("border-image:url(\"" + image_path + "\")"); diff --git a/aobutton.h b/aobutton.h index f0c5697..0492375 100644 --- a/aobutton.h +++ b/aobutton.h @@ -1,6 +1,8 @@ #ifndef AOBUTTON_H #define AOBUTTON_H +#include "aoapplication.h" + #include class AOButton : public QPushButton @@ -8,9 +10,11 @@ class AOButton : public QPushButton Q_OBJECT public: - AOButton(QWidget *parent); + AOButton(QWidget *parent, AOApplication *p_ao_app); ~AOButton(); + AOApplication *ao_app; + void set_image(QString p_image); }; diff --git a/aocharbutton.cpp b/aocharbutton.cpp index 1c0b4f7..79dc4e8 100644 --- a/aocharbutton.cpp +++ b/aocharbutton.cpp @@ -5,17 +5,19 @@ #include -AOCharButton::AOCharButton(QWidget *parent) +AOCharButton::AOCharButton(QWidget *parent, AOApplication *p_ao_app) { m_parent = parent; + ao_app = p_ao_app; + this->resize(60, 60); } void AOCharButton::set_image(QString p_character) { - QString image_path = get_character_path(p_character) + "char_icon.png"; - QString legacy_path = get_demothings_path() + p_character.toLower() + "_char_icon.png"; + QString image_path = ao_app->get_character_path(p_character) + "char_icon.png"; + QString legacy_path = ao_app->get_demothings_path() + p_character.toLower() + "_char_icon.png"; if (file_exists(image_path)) this->setStyleSheet("border-image:url(\"" + image_path + "\")"); diff --git a/aocharbutton.h b/aocharbutton.h index 231fe66..e91a120 100644 --- a/aocharbutton.h +++ b/aocharbutton.h @@ -1,6 +1,8 @@ #ifndef AOCHARBUTTON_H #define AOCHARBUTTON_H +#include "aoapplication.h" + #include #include #include @@ -10,7 +12,9 @@ class AOCharButton : public QPushButton Q_OBJECT public: - AOCharButton(QWidget *parent); + AOCharButton(QWidget *parent, AOApplication *p_ao_app); + + AOApplication *ao_app; void set_image(QString p_character); diff --git a/aoimage.cpp b/aoimage.cpp index 0f03a7b..2e09f6a 100644 --- a/aoimage.cpp +++ b/aoimage.cpp @@ -4,9 +4,10 @@ #include "aoimage.h" -AOImage::AOImage(QWidget *parent) : QLabel(parent) +AOImage::AOImage(QWidget *parent, AOApplication *p_ao_app) : QLabel(parent) { - + m_parent = parent; + ao_app = p_ao_app; } AOImage::~AOImage() @@ -16,8 +17,8 @@ AOImage::~AOImage() void AOImage::set_image(QString p_image) { - QString theme_image_path = get_theme_path() + p_image; - QString default_image_path = get_default_theme_path() + p_image; + QString theme_image_path = ao_app->get_theme_path() + p_image; + QString default_image_path = ao_app->get_default_theme_path() + p_image; if (file_exists(theme_image_path)) this->setPixmap(theme_image_path); diff --git a/aoimage.h b/aoimage.h index d83751c..95f10ca 100644 --- a/aoimage.h +++ b/aoimage.h @@ -3,14 +3,19 @@ #ifndef AOIMAGE_H #define AOIMAGE_H +#include "aoapplication.h" + #include class AOImage : public QLabel { public: - AOImage(QWidget *parent); + AOImage(QWidget *parent, AOApplication *p_ao_app); ~AOImage(); + QWidget *m_parent; + AOApplication *ao_app; + void set_image(QString p_image); void set_size_and_pos(QString identifier); }; diff --git a/courtroom.cpp b/courtroom.cpp index 98b544e..bc1bf1f 100644 --- a/courtroom.cpp +++ b/courtroom.cpp @@ -7,11 +7,11 @@ #include -Courtroom::Courtroom(AOApplication *parent) : QMainWindow() +Courtroom::Courtroom(AOApplication *p_ao_app) : QMainWindow() { - ao_app = parent; + ao_app = p_ao_app; - ui_background = new AOImage(this); + ui_background = new AOImage(this, ao_app); //viewport elements like background, desk, etc. @@ -35,41 +35,41 @@ Courtroom::Courtroom(AOApplication *parent) : QMainWindow() //emote buttons - ui_emote_left = new AOButton(this); - ui_emote_right = new AOButton(this); + ui_emote_left = new AOButton(this, ao_app); + ui_emote_right = new AOButton(this, ao_app); - ui_defense_bar = new AOImage(this); - ui_prosecution_bar = new AOImage(this); + ui_defense_bar = new AOImage(this, ao_app); + ui_prosecution_bar = new AOImage(this, ao_app); ui_music_label = new QLabel(this); ui_sfx_label = new QLabel(this); ui_blip_label = new QLabel(this); - ui_hold_it = new AOButton(this); - ui_objection = new AOButton(this); - ui_take_that = new AOButton(this); + ui_hold_it = new AOButton(this, ao_app); + ui_objection = new AOButton(this, ao_app); + ui_take_that = new AOButton(this, ao_app); - ui_ooc_toggle = new AOButton(this); - ui_witness_testimony = new AOButton(this); - ui_cross_examination = new AOButton(this); + ui_ooc_toggle = new AOButton(this, ao_app); + ui_witness_testimony = new AOButton(this, ao_app); + ui_cross_examination = new AOButton(this, ao_app); - ui_change_character = new AOButton(this); - ui_reload_theme = new AOButton(this); - ui_call_mod = new AOButton(this); + ui_change_character = new AOButton(this, ao_app); + ui_reload_theme = new AOButton(this, ao_app); + ui_call_mod = new AOButton(this, ao_app); ui_pre = new QCheckBox(this); ui_flip = new QCheckBox(this); ui_guard = new QCheckBox(this); - ui_custom_objection = new AOButton(this); - ui_realization = new AOButton(this); - ui_mute = new AOButton(this); + ui_custom_objection = new AOButton(this, ao_app); + ui_realization = new AOButton(this, ao_app); + ui_mute = new AOButton(this, ao_app); - ui_defense_plus = new AOButton(this); - ui_defense_minus = new AOButton(this); + ui_defense_plus = new AOButton(this, ao_app); + ui_defense_minus = new AOButton(this, ao_app); - ui_prosecution_plus = new AOButton(this); - ui_prosecution_minus = new AOButton(this); + ui_prosecution_plus = new AOButton(this, ao_app); + ui_prosecution_minus = new AOButton(this, ao_app); ui_text_color = new QComboBox(this); @@ -77,22 +77,22 @@ Courtroom::Courtroom(AOApplication *parent) : QMainWindow() ui_sfx_slider = new QSlider(this); ui_blip_slider = new QSlider(this); - ui_muted = new AOImage(this); + ui_muted = new AOImage(this, ao_app); /////////////char select widgets under here/////////////// - ui_char_select_background = new AOImage(this); + ui_char_select_background = new AOImage(this, ao_app); //T0D0: allocate and position charbuttons //QVector ui_char_button_list; - ui_selector = new AOImage(ui_char_select_background); + ui_selector = new AOImage(ui_char_select_background, ao_app); - ui_back_to_lobby = new AOButton(ui_char_select_background); + ui_back_to_lobby = new AOButton(ui_char_select_background, ao_app); ui_char_password = new QLineEdit(ui_char_select_background); - ui_spectator = new AOButton(ui_char_select_background); + ui_spectator = new AOButton(ui_char_select_background, ao_app); connect(ui_reload_theme, SIGNAL(clicked()), this, SLOT(on_reload_theme_clicked())); @@ -203,14 +203,14 @@ void Courtroom::set_widgets() void Courtroom::set_size_and_pos(QWidget *p_widget, QString p_identifier) { - QString design_ini_path = get_theme_path() + "courtroom_design.ini"; - QString default_ini_path = get_base_path() + "themes/default/courtroom_design.ini"; + QString design_ini_path = ao_app->get_theme_path() + "courtroom_design.ini"; + QString default_ini_path = ao_app->get_base_path() + "themes/default/courtroom_design.ini"; - pos_size_type design_ini_result = get_pos_and_size(p_identifier, design_ini_path); + pos_size_type design_ini_result = ao_app->get_pos_and_size(p_identifier, design_ini_path); if (design_ini_result.width < 0 || design_ini_result.height < 0) { - design_ini_result = get_pos_and_size(p_identifier, default_ini_path); + design_ini_result = ao_app->get_pos_and_size(p_identifier, default_ini_path); if (design_ini_result.width < 0 || design_ini_result.height < 0) { @@ -227,7 +227,7 @@ void Courtroom::set_size_and_pos(QWidget *p_widget, QString p_identifier) void Courtroom::on_reload_theme_clicked() { - g_user_theme = get_user_theme(); + ao_app->set_user_theme(); set_widgets(); } diff --git a/courtroom.h b/courtroom.h index b20b63c..e0dee82 100644 --- a/courtroom.h +++ b/courtroom.h @@ -23,7 +23,7 @@ class Courtroom : public QMainWindow { Q_OBJECT public: - explicit Courtroom(AOApplication *parent = 0); + explicit Courtroom(AOApplication *p_ao_app); void set_widgets(); void set_size_and_pos(QWidget *p_widget, QString p_identifier); diff --git a/global_variables.cpp b/global_variables.cpp index a496a10..b3a7e12 100644 --- a/global_variables.cpp +++ b/global_variables.cpp @@ -1,4 +1,5 @@ #include "global_variables.h" +#include "text_file_functions.h" const int RELEASE = 2; const int MAJOR_VERSION = 1; diff --git a/global_variables.h b/global_variables.h index 7b8a469..941ac49 100644 --- a/global_variables.h +++ b/global_variables.h @@ -3,6 +3,6 @@ #include -extern QString g_user_theme; + #endif // GLOBAL_VARIABLES_H diff --git a/lobby.cpp b/lobby.cpp index e1704b7..17cae5f 100644 --- a/lobby.cpp +++ b/lobby.cpp @@ -9,21 +9,21 @@ #include -Lobby::Lobby(AOApplication *parent) : QMainWindow() +Lobby::Lobby(AOApplication *p_ao_app) : QMainWindow() { - ao_app = parent; + ao_app = p_ao_app; this->setWindowTitle("Attorney Online 2"); this->resize(m_lobby_width, m_lobby_height); this->setFixedSize(m_lobby_width, m_lobby_height); - ui_background = new AOImage(this); - ui_public_servers = new AOButton(this); - ui_favorites = new AOButton(this); - ui_refresh = new AOButton(this); - ui_add_to_fav = new AOButton(this); - ui_connect = new AOButton(this); - ui_about = new AOButton(this); + ui_background = new AOImage(this, ao_app); + ui_public_servers = new AOButton(this, ao_app); + ui_favorites = new AOButton(this, ao_app); + ui_refresh = new AOButton(this, ao_app); + ui_add_to_fav = new AOButton(this, ao_app); + ui_connect = new AOButton(this, ao_app); + ui_about = new AOButton(this, ao_app); ui_server_list = new QListWidget(this); ui_player_count = new QLabel(this); ui_description = new QPlainTextEdit(this); @@ -49,8 +49,7 @@ Lobby::Lobby(AOApplication *parent) : QMainWindow() //sets images, position and size void Lobby::set_widgets() { - //global to efficiently set images on button presses - g_user_theme = get_user_theme(); + ao_app->set_user_theme(); ui_background->set_image("lobbybackground.png"); ui_background->move(0, 0); @@ -130,7 +129,8 @@ void Lobby::on_favorites_clicked() ui_favorites->set_image("favorites_selected.png"); ui_public_servers->set_image("publicservers.png"); - ao_app->favorite_list = read_serverlist_txt(); + ao_app->set_favorite_list(); + //ao_app->favorite_list = read_serverlist_txt(); list_favorites(); @@ -166,18 +166,19 @@ void Lobby::on_add_to_fav_released() if (!public_servers_selected) return; - int n_server = ui_server_list->currentRow(); - - if (n_server < 0 || n_server >= ao_app->server_list.size()) + ao_app->add_favorite_server(ui_server_list->currentRow()); + /* + if (n_server < 0 || n_server >= ao_app->get_server_list().size()) return; - server_type fav_server = ao_app->server_list.at(n_server); + server_type fav_server = ao_app->get_server_list().at(n_server); QString str_port = QString::number(fav_server.port); QString server_line = fav_server.ip + ":" + str_port + ":" + fav_server.name; write_to_serverlist_txt(server_line); + */ } void Lobby::on_connect_pressed() @@ -216,17 +217,17 @@ void Lobby::on_server_list_clicked(QModelIndex p_model) if (public_servers_selected) { - if (n_server >= ao_app->server_list.size()) + if (n_server >= ao_app->get_server_list().size()) return; - f_server = ao_app->server_list.at(p_model.row()); + f_server = ao_app->get_server_list().at(p_model.row()); } else { - if (n_server >= ao_app->favorite_list.size()) + if (n_server >= ao_app->get_favorite_list().size()) return; - f_server = ao_app->favorite_list.at(p_model.row()); + f_server = ao_app->get_favorite_list().at(p_model.row()); } ui_description->clear(); @@ -258,7 +259,7 @@ void Lobby::list_servers() { ui_server_list->clear(); - for (server_type i_server : ao_app->server_list) + for (server_type i_server : ao_app->get_server_list()) { ui_server_list->addItem(i_server.name); } @@ -268,7 +269,7 @@ void Lobby::list_favorites() { ui_server_list->clear(); - for (server_type i_server : ao_app->favorite_list) + for (server_type i_server : ao_app->get_favorite_list()) { ui_server_list->addItem(i_server.name); } diff --git a/lobby.h b/lobby.h index 8ee384b..e4d6499 100644 --- a/lobby.h +++ b/lobby.h @@ -18,7 +18,7 @@ class Lobby : public QMainWindow Q_OBJECT public: - Lobby(AOApplication *parent); + Lobby(AOApplication *p_ao_app); void set_widgets(); void list_servers(); diff --git a/path_functions.cpp b/path_functions.cpp index 0282bd4..46f80ef 100644 --- a/path_functions.cpp +++ b/path_functions.cpp @@ -1,4 +1,4 @@ -#include "path_functions.h" +#include "aoapplication.h" #include "global_variables.h" #include "text_file_functions.h" @@ -6,26 +6,26 @@ #include #include -QString get_base_path(){ +QString AOApplication::get_base_path(){ return (QDir::currentPath() + "/base/"); } -QString get_theme_path() +QString AOApplication::get_theme_path() { - return get_base_path() + "themes/" + g_user_theme.toLower() + "/"; + return get_base_path() + "themes/" + user_theme.toLower() + "/"; } -QString get_default_theme_path() +QString AOApplication::get_default_theme_path() { return get_base_path() + "themes/default/"; } -QString get_character_path(QString p_character) +QString AOApplication::get_character_path(QString p_character) { return get_base_path() + "characters/" + p_character.toLower() + "/"; } -QString get_demothings_path() +QString AOApplication::get_demothings_path() { return get_base_path() + "misc/demothings/"; } diff --git a/path_functions.h b/path_functions.h index 67cbd29..2f37a0c 100644 --- a/path_functions.h +++ b/path_functions.h @@ -3,10 +3,6 @@ #include -QString get_base_path(); -QString get_theme_path(); -QString get_default_theme_path(); -QString get_character_path(QString p_character); -QString get_demothings_path(); + #endif // PATH_FUNCTIONS_H diff --git a/text_file_functions.cpp b/text_file_functions.cpp index bd5699c..db1ecc7 100644 --- a/text_file_functions.cpp +++ b/text_file_functions.cpp @@ -1,4 +1,4 @@ -#include "text_file_functions.h" +#include "aoapplication.h" #include "path_functions.h" #include "file_functions.h" @@ -8,7 +8,7 @@ #include #include -QString get_user_theme(){ +QString AOApplication::read_user_theme(){ QFile config_file(get_base_path() + "config.ini"); if (!config_file.open(QIODevice::ReadOnly)) return "default"; @@ -31,7 +31,7 @@ QString get_user_theme(){ return "default"; } -void write_to_serverlist_txt(QString p_line) +void AOApplication::write_to_serverlist_txt(QString p_line) { QFile serverlist_txt; QString serverlist_txt_path = get_base_path() + "serverlist.txt"; @@ -50,7 +50,7 @@ void write_to_serverlist_txt(QString p_line) serverlist_txt.close(); } -QVector read_serverlist_txt() +QVector AOApplication::read_serverlist_txt() { QVector f_server_list; @@ -86,7 +86,7 @@ QVector read_serverlist_txt() return f_server_list; } -pos_size_type get_pos_and_size(QString p_identifier, QString p_design_path) +pos_size_type AOApplication::get_pos_and_size(QString p_identifier, QString p_design_path) { QFile design_ini; diff --git a/text_file_functions.h b/text_file_functions.h index 5e3a0b3..54abd4d 100644 --- a/text_file_functions.h +++ b/text_file_functions.h @@ -6,9 +6,6 @@ #include #include -QString get_user_theme(); -void write_to_serverlist_txt(QString p_line); -QVector read_serverlist_txt(); -pos_size_type get_pos_and_size(QString p_identifier, QString p_design_path); + #endif // TEXT_FILE_FUNCTIONS_H