From d41ec17fe72dbcf1b5277ab7b33b6374a9fb9e60 Mon Sep 17 00:00:00 2001 From: in1tiate <32779090+in1tiate@users.noreply.github.com> Date: Mon, 18 Jan 2021 14:10:45 -0600 Subject: [PATCH] Add a new "char_list" QTreeWidget that allows for categorizing characters (#399) * initial commit * use a "category" value instead * add filtering and taken indicator * hide serverlist header * set the items to disabled in place of the [X] text Co-authored-by: stonedDiscord <10584181+stonedDiscord@users.noreply.github.com> --- include/aoapplication.h | 3 ++ include/courtroom.h | 4 +++ src/charselect.cpp | 71 ++++++++++++++++++++++++++++++++++--- src/lobby.cpp | 1 + src/text_file_functions.cpp | 6 ++++ 5 files changed, 81 insertions(+), 4 deletions(-) diff --git a/include/aoapplication.h b/include/aoapplication.h index 5a02c3b..4607fa6 100644 --- a/include/aoapplication.h +++ b/include/aoapplication.h @@ -342,6 +342,9 @@ public: // Returns the showname from the ini of p_char QString get_showname(QString p_char); + // Returns the category of this character + QString get_category(QString p_char); + // Returns the value of chat image from the specific p_char's ini file QString get_chat(QString p_char); diff --git a/include/courtroom.h b/include/courtroom.h index 31b90c9..e86330e 100644 --- a/include/courtroom.h +++ b/include/courtroom.h @@ -715,6 +715,9 @@ private: AOImage *ui_char_select_background; + // pretty list of characters + QTreeWidget *ui_char_list; + // abstract widget to hold char buttons QWidget *ui_char_buttons; @@ -908,6 +911,7 @@ private slots: void on_back_to_lobby_clicked(); + void on_char_list_double_clicked(QTreeWidgetItem *p_item, int column); void on_char_select_left_clicked(); void on_char_select_right_clicked(); void on_char_search_changed(); diff --git a/src/charselect.cpp b/src/charselect.cpp index 33cc517..5495b07 100644 --- a/src/charselect.cpp +++ b/src/charselect.cpp @@ -11,6 +11,15 @@ void Courtroom::construct_char_select() ui_char_select_background = new AOImage(this, ao_app); + ui_char_list = new QTreeWidget(ui_char_select_background); + ui_char_list->setColumnCount(2); + ui_char_list->setHeaderLabels({"Name", "ID"}); + ui_char_list->setHeaderHidden(true); + ui_char_list->header()->setSectionResizeMode(QHeaderView::ResizeToContents); + ui_char_list->hideColumn(1); + ui_char_list->setDropIndicatorShown(true); + + ui_char_buttons = new QWidget(ui_char_select_background); ui_selector = new AOImage(ui_char_select_background, ao_app); @@ -45,6 +54,10 @@ void Courtroom::construct_char_select() ui_char_passworded->setChecked(true); set_size_and_pos(ui_char_buttons, "char_buttons"); + set_size_and_pos(ui_char_list, "char_list"); + + connect(ui_char_list, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)), + this, SLOT(on_char_list_double_clicked(QTreeWidgetItem *, int))); connect(ui_back_to_lobby, SIGNAL(clicked()), this, SLOT(on_back_to_lobby_clicked())); @@ -126,6 +139,20 @@ void Courtroom::set_char_select_page() put_button_in_place(current_char_page * max_chars_on_page, chars_on_page); } +void Courtroom::on_char_list_double_clicked(QTreeWidgetItem *p_item, int column) +{ + int cid = p_item->text(1).toInt(); + if (cid == -1 && !p_item->isExpanded()) { + p_item->setExpanded(true); + return; + } + else if (cid == -1) { + p_item->setExpanded(false); + return; + } + char_clicked(cid); +} + void Courtroom::char_clicked(int n_char) { if (n_char != -1) @@ -218,7 +245,32 @@ void Courtroom::character_loading_finished() char_button->set_image(char_list.at(n).name); char_button->setToolTip(char_list.at(n).name); ui_char_button_list.append(char_button); - + QString char_category = ao_app->get_category(char_list.at(n).name); + QList matching_list = ui_char_list->findItems(char_category, Qt::MatchFixedString, 0); + // create the character tree item + QTreeWidgetItem *treeItem = new QTreeWidgetItem(); + treeItem->setText(0, char_list.at(n).name); + treeItem->setIcon(0, QIcon(ao_app->get_static_image_suffix( + ao_app->get_character_path(char_list.at(n).name, "char_icon")))); + treeItem->setData(1, Qt::DisplayRole, n); + // category logic + QTreeWidgetItem *category; + if (char_category == "") // no category + ui_char_list->addTopLevelItem(treeItem); + else if (!matching_list.isEmpty()) { // our category already exists + category = matching_list[0]; + category->addChild(treeItem); + } + else { // we need to make a new category + category = new QTreeWidgetItem(); + category->setText(0, char_category); + category->setData(1, Qt::DisplayRole, -1); + category->setChildIndicatorPolicy(QTreeWidgetItem::DontShowIndicatorWhenChildless); + ui_char_list->insertTopLevelItem(0, category); + category->addChild(treeItem); + } + + connect(char_button, &AOCharButton::clicked, [this, n]() { this->char_clicked(n); }); @@ -241,7 +293,7 @@ void Courtroom::character_loading_finished() .arg(QString::number(ao_app->char_list_size))); } } - + ui_char_list->expandAll(); filter_character_list(); } @@ -250,24 +302,35 @@ void Courtroom::filter_character_list() ui_char_button_list_filtered.clear(); for (int i = 0; i < char_list.size(); i++) { AOCharButton *current_char = ui_char_button_list.at(i); + QTreeWidgetItem *current_char_list_item = ui_char_list->findItems(QString::number(i), Qt::MatchFixedString, 1)[0]; // It seems passwording characters is unimplemented yet? // Until then, this will stay here, I suppose. // if (ui_char_passworded->isChecked() && character_is_passworded??) // continue; - if (!ui_char_taken->isChecked() && char_list.at(i).taken) + if (!ui_char_taken->isChecked() && char_list.at(i).taken) { + current_char_list_item->setHidden(true); continue; + } if (!char_list.at(i).name.contains(ui_char_search->text(), - Qt::CaseInsensitive)) + Qt::CaseInsensitive)) { + current_char_list_item->setHidden(true); continue; + } // We only really need to update the fact that a character is taken // for the buttons that actually appear. // You'd also update the passwordedness and etc. here later. current_char->reset(); + current_char_list_item->setHidden(false); current_char->set_taken(char_list.at(i).taken); + current_char_list_item->setText(0, char_list.at(i).name); + // reset disabled + current_char_list_item->setDisabled(false); + if (char_list.at(i).taken) // woops, we are taken + current_char_list_item->setDisabled(true); ui_char_button_list_filtered.append(current_char); } diff --git a/src/lobby.cpp b/src/lobby.cpp index f1a61f4..4528dc7 100644 --- a/src/lobby.cpp +++ b/src/lobby.cpp @@ -29,6 +29,7 @@ Lobby::Lobby(AOApplication *p_ao_app) : QMainWindow() ui_server_list = new QTreeWidget(this); ui_server_list->setHeaderLabels({"#", "Name"}); //, "Players"}); ui_server_list->hideColumn(0); + ui_server_list->setHeaderHidden(true); ui_server_search = new QLineEdit(this); ui_server_search->setFrame(false); diff --git a/src/text_file_functions.cpp b/src/text_file_functions.cpp index e79411a..43a72ec 100644 --- a/src/text_file_functions.cpp +++ b/src/text_file_functions.cpp @@ -667,6 +667,12 @@ QString AOApplication::get_blips(QString p_char) return f_result; } +QString AOApplication::get_category(QString p_char) +{ + QString f_result = read_char_ini(p_char, "category", "Options"); + return f_result; +} + QString AOApplication::get_chat(QString p_char) { if (p_char == "default")