Skip to content

Commit

Permalink
Add context menu
Browse files Browse the repository at this point in the history
  • Loading branch information
Salanto committed Jan 20, 2024
1 parent df2b723 commit 86d8587
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 5 deletions.
2 changes: 2 additions & 0 deletions include/courtroom.h
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,8 @@ private slots:

void preview_emote(QString emote);
void update_emote_preview();

void onPlayerActionTriggered(QStringList args);
};

#endif // COURTROOM_H
31 changes: 30 additions & 1 deletion include/widgets/player_presence.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
#include <QDebug>
#include <QListWidget>
#include <QMap>
#include <QMenu>

class AOApplication;
class Courtroom;

class PlayerItem : public QListWidgetItem {
public:
Expand All @@ -31,22 +33,49 @@ class PlayerItem : public QListWidgetItem {
bool m_isSpecial;
};

class PlayerMenu : public QListWidget {
class PlayerContextMenu : public QMenu {
Q_OBJECT
public:
PlayerContextMenu(QWidget *parent, PlayerItem *f_player,
bool f_authentication_state);
~PlayerContextMenu() = default;

signals:
void actionTriggered(QStringList args);

private slots:
void onPairClicked();
void onKickClicked();
void onBanClicked();

private:
PlayerItem *player;
};

class PlayerMenu : public QListWidget {
Q_OBJECT
public:
PlayerMenu(QWidget *parent = nullptr, AOApplication *p_ao_app = nullptr);
~PlayerMenu() = default;

public:
void setAuthenticated(bool p_state);
void addPlayer(QStringList f_content);
void updatePlayer(PlayerItem *f_player, QString f_name, QString f_character,
bool f_isSpecial);
void removePlayer(int f_id);
void resetList();

signals:
void actionTriggered(QStringList args);

private:
QMap<int, PlayerItem *> players;
AOApplication *ao_app;
bool is_authenticated = false;

private slots:
void onCustomContextMenuRequested(const QPoint &pos);
};

#endif // PLAYER_PRESENCE_H
14 changes: 14 additions & 0 deletions src/courtroom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,8 @@ Courtroom::Courtroom(AOApplication *p_ao_app) : QMainWindow()
ui_player_menu = new PlayerMenu(this, ao_app);
ui_player_menu->setContextMenuPolicy(Qt::CustomContextMenu);
ui_player_menu->setObjectName("ui_player_menu");
connect(ui_player_menu, &PlayerMenu::actionTriggered, this,
&Courtroom::onPlayerActionTriggered);

initialize_emotes();
initialize_evidence();
Expand Down Expand Up @@ -1792,9 +1794,11 @@ void Courtroom::append_server_chatmessage(QString p_name, QString p_message,

void Courtroom::on_authentication_state_received(int p_state)
{
bool is_authenticated = false;
if (p_state >= 1) {
ui_guard->show();
append_server_chatmessage(tr("CLIENT"), tr("You were granted the Disable Modcalls button."), "1");
is_authenticated = true;
}
else if (p_state == 0) {
append_server_chatmessage(tr("CLIENT"), tr("Login unsuccessful."), "1");
Expand All @@ -1803,6 +1807,7 @@ void Courtroom::on_authentication_state_received(int p_state)
ui_guard->hide();
append_server_chatmessage(tr("CLIENT"), tr("You were logged out."), "1");
}
ui_player_menu->setAuthenticated(is_authenticated);
}

void Courtroom::addPlayerPresence(QStringList f_content)
Expand Down Expand Up @@ -5835,6 +5840,15 @@ void Courtroom::truncate_label_text(QWidget *p_widget, QString p_identifier)
<< truncated_px_width << "px)";
}

void Courtroom::onPlayerActionTriggered(QStringList args)
{
QStringList packet_args;
packet_args.append(ui_ooc_chat_name->text());
packet_args.append(args.join(" "));
AOPacket packet("CT", packet_args);
ao_app->net_manager->ship_server_packet(packet.to_string(false));
}

Courtroom::~Courtroom()
{
//save sound settings
Expand Down
54 changes: 50 additions & 4 deletions src/widgets/player_presence.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "include/widgets/player_presence.h"
#include "aoapplication.h"
#include <QMenu>

PlayerItem::PlayerItem(QListWidget *parent, AOApplication *p_ao_app)
: QListWidgetItem{parent}, ao_app{p_ao_app}
Expand Down Expand Up @@ -47,9 +48,14 @@ void PlayerItem::styleEntry()
PlayerMenu::PlayerMenu(QWidget *parent, AOApplication *p_ao_app)
: QListWidget{parent}, ao_app{p_ao_app}
{
setContextMenuPolicy(Qt::CustomContextMenu);
setIconSize(QSize(25, 25));
connect(this, &PlayerMenu::customContextMenuRequested, this,
&PlayerMenu::onCustomContextMenuRequested);
}

void PlayerMenu::setAuthenticated(bool p_state) { is_authenticated = p_state; }

void PlayerMenu::addPlayer(QStringList f_content)
{
int id = f_content[0].toInt();
Expand All @@ -62,7 +68,6 @@ void PlayerMenu::addPlayer(QStringList f_content)
updatePlayer(l_player, name, character, is_special);
return;
}
qDebug() << "Creating player entry for" << id;
l_player = new PlayerItem(this, ao_app);
l_player->setID(id);
l_player->setName(name);
Expand All @@ -75,7 +80,6 @@ void PlayerMenu::addPlayer(QStringList f_content)
void PlayerMenu::updatePlayer(PlayerItem *f_player, QString f_name,
QString f_character, bool f_isSpecial)
{
qDebug() << "Updating player entry for" << f_player->id();
f_player->setName(f_name);
f_player->setCharacter(f_character);
f_player->setIsSpecial(f_isSpecial);
Expand All @@ -85,17 +89,59 @@ void PlayerMenu::removePlayer(int f_id)
{
PlayerItem *f_player = players.take(f_id);
if (f_player == nullptr) {
qDebug() << "Attempted to remove non-existant player at" << f_id;
return;
}
delete f_player;
}

void PlayerMenu::resetList()
{
qDebug() << "Clearing player menu after server request.";
for (PlayerItem *player : qAsConst(players)) {
delete player;
}
players.clear();
}

void PlayerMenu::onCustomContextMenuRequested(const QPoint &pos)
{
QPoint l_pos = mapToGlobal(pos);
PlayerItem *l_item = static_cast<PlayerItem *>(itemAt(pos));
if (l_item == nullptr) {
qDebug() << "Unable to obtain PlayerItem object pointer.";
return;
}
PlayerContextMenu *l_context_menu =
new PlayerContextMenu(this, l_item, is_authenticated);
l_context_menu->setAttribute(Qt::WA_DeleteOnClose);
connect(l_context_menu, &PlayerContextMenu::actionTriggered, this,
&PlayerMenu::actionTriggered);
l_context_menu->popup(l_pos);
}

PlayerContextMenu::PlayerContextMenu(QWidget *parent, PlayerItem *f_player,
bool f_authentication_state)
: QMenu{parent}, player{f_player}
{
addAction("Pair", this, &PlayerContextMenu::onPairClicked);

if (f_authentication_state) {
addSeparator();
addAction("Kick", this, &PlayerContextMenu::onKickClicked);
addAction("Ban", this, &PlayerContextMenu::onBanClicked);
}
}

void PlayerContextMenu::onPairClicked()
{
emit actionTriggered({"/pair", QString::number(player->id())});
}

void PlayerContextMenu::onKickClicked()
{
// TODO : Implement me!
}

void PlayerContextMenu::onBanClicked()
{
// TODO : Implement me!
}

0 comments on commit 86d8587

Please sign in to comment.