Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions include/courtroom.h
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,9 @@ class Courtroom : public QMainWindow {
int color = 0, bool selfname = false, QDateTime timestamp = QDateTime::currentDateTime(),
bool ghost = false);

void append_pinned_text(QString p_text, QString p_name = "", QString action = "", int color = 0, bool selfname = false, QDateTime timestamp = QDateTime::currentDateTime());
void clear_pinned_message();

// clear sent messages that appear on the IC log but haven't been delivered
// yet to other players
void pop_ic_ghost();
Expand Down Expand Up @@ -762,6 +765,7 @@ class Courtroom : public QMainWindow {
SplashLayer *ui_vp_objection;

QTextEdit *ui_ic_chatlog;
QTextEdit *ui_ic_chatlog_pinned;

AOTextArea *ui_debug_log;
AOTextArea *ui_server_chatlog;
Expand Down Expand Up @@ -840,6 +844,7 @@ class Courtroom : public QMainWindow {
AOButton *ui_call_mod;
AOButton *ui_settings;
AOButton *ui_switch_area_music;
AOButton *ui_switch_logs;

QCheckBox *ui_pre;
QCheckBox *ui_flip;
Expand Down Expand Up @@ -1129,6 +1134,7 @@ private slots:
void on_spectator_clicked();

void on_switch_area_music_clicked();
void on_switch_logs_clicked();

void on_application_state_changed(Qt::ApplicationState state);

Expand Down
185 changes: 185 additions & 0 deletions src/courtroom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ Courtroom::Courtroom(AOApplication *p_ao_app) : QMainWindow()
ui_ic_chatlog->setReadOnly(true);
ui_ic_chatlog->setObjectName("ui_ic_chatlog");

ui_ic_chatlog_pinned = new QTextEdit(this);
ui_ic_chatlog_pinned->setReadOnly(true);
ui_ic_chatlog_pinned->setObjectName("ui_ic_chatlog_pinned");
ui_ic_chatlog_pinned->hide();

log_maximum_blocks = Options::getInstance().maxLogSize();
log_goes_downwards = Options::getInstance().logDirectionDownwards();
log_colors = Options::getInstance().colorLogEnabled();
Expand Down Expand Up @@ -336,6 +341,9 @@ Courtroom::Courtroom(AOApplication *p_ao_app) : QMainWindow()
ui_switch_area_music = new AOButton(this, ao_app);
ui_switch_area_music->setObjectName("ui_switch_area_music");

ui_switch_logs = new AOButton(this, ao_app);
ui_switch_logs->setObjectName("ui_switch_logs");

ui_pre = new QCheckBox(this);
ui_pre->setText(tr("Pre"));
ui_pre->setObjectName("ui_pre");
Expand Down Expand Up @@ -834,6 +842,8 @@ Courtroom::Courtroom(AOApplication *p_ao_app) : QMainWindow()
connect(ui_settings, &AOButton::clicked, this, &Courtroom::on_settings_clicked);
connect(ui_switch_area_music, &AOButton::clicked, this,
&Courtroom::on_switch_area_music_clicked);
connect(ui_switch_logs, &AOButton::clicked, this,
&Courtroom::on_switch_logs_clicked);

connect(ui_pre, &AOButton::clicked, this, &Courtroom::on_pre_clicked);
connect(ui_flip, &AOButton::clicked, this, &Courtroom::on_flip_clicked);
Expand Down Expand Up @@ -1085,6 +1095,12 @@ void Courtroom::set_widgets()
ui_ic_chatlog->setPlaceholderText(log_goes_downwards ? "▼ " + tr("Log goes down") + " ▼"
: "▲ " + tr("Log goes up") + " ▲");

set_size_and_pos(ui_ic_chatlog_pinned, "ic_chatlog");
ui_ic_chatlog_pinned->setFrameShape(QFrame::NoFrame);
ui_ic_chatlog_pinned->setPlaceholderText(log_goes_downwards
? "▼📌 " + tr("Pinned Message") + " 📌▼"
: "▲📌 " + tr("Pinned Message") + " 📌▲");

set_size_and_pos(ui_debug_log, "ms_chatlog"); // Old name, still use it to not break compatibility
ui_debug_log->setFrameShape(QFrame::NoFrame);

Expand Down Expand Up @@ -1331,6 +1347,11 @@ void Courtroom::set_widgets()
ui_switch_area_music->set_image("switch_area_music");
ui_switch_area_music->setToolTip(tr("Switch between Areas and Music lists"));

set_size_and_pos(ui_switch_logs, "switch_logs");
ui_switch_logs->setText(tr("L/P"));
ui_switch_logs->set_image("switch_logs");
ui_switch_logs->setToolTip(tr("Switch between Log message chat and Log pinned chat"));

set_size_and_pos(ui_pre, "pre");
ui_pre->setText(tr("Preanim"));
ui_pre->setToolTip(
Expand Down Expand Up @@ -1476,6 +1497,7 @@ void Courtroom::set_fonts(QString p_char)
set_font(ui_vp_showname, "", "showname", p_char);
set_font(ui_vp_message, "", "message", p_char);
set_font(ui_ic_chatlog, "", "ic_chatlog", p_char);
set_font(ui_ic_chatlog_pinned, "", "ic_chatlog_pinned", p_char);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add ic_chatlog_pinned to the description so it's known what should be added to themes

set_font(ui_debug_log, "", "debug_log", p_char);
set_font(ui_server_chatlog, "", "server_chatlog", p_char);
set_font(ui_music_list, "", "music_list", p_char);
Expand Down Expand Up @@ -4008,6 +4030,157 @@ void Courtroom::append_ic_text(QString p_text, QString p_name, QString p_action,
}
}

void Courtroom::append_pinned_text(QString p_text, QString p_name, QString p_action, int color, bool selfname, QDateTime timestamp)
{
QColor chatlog_color = ao_app->get_color("ic_chatlog_color", "courtroom_fonts.ini");
QTextCharFormat bold;
QTextCharFormat normal;
QTextCharFormat italics;
QTextCharFormat own_name;
QTextCharFormat other_name;
QTextCharFormat timestamp_format;
QTextCharFormat selftimestamp_format;
QTextBlockFormat format;
bold.setFontWeight(QFont::Bold);
normal.setFontWeight(QFont::Normal);
italics.setFontItalic(true);
own_name.setFontWeight(QFont::Bold);
own_name.setForeground(
ao_app->get_color("ic_chatlog_selfname_color", "courtroom_fonts.ini"));
other_name.setFontWeight(QFont::Bold);
other_name.setForeground(
ao_app->get_color("ic_chatlog_showname_color", "courtroom_fonts.ini"));
timestamp_format.setForeground(
ao_app->get_color("ic_chatlog_timestamp_color", "courtroom_fonts.ini"));
selftimestamp_format.setForeground(ao_app->get_color(
"ic_chatlog_selftimestamp_color", "courtroom_fonts.ini"));
format.setTopMargin(log_margin);
const QTextCursor old_cursor = ui_ic_chatlog_pinned->textCursor();
const int old_scrollbar_value = ui_ic_chatlog_pinned->verticalScrollBar()->value();
const bool need_newline = !ui_ic_chatlog_pinned->document()->isEmpty();
const int scrollbar_target_value =
log_goes_downwards ? ui_ic_chatlog_pinned->verticalScrollBar()->maximum()
: ui_ic_chatlog_pinned->verticalScrollBar()->minimum();


ui_ic_chatlog_pinned->moveCursor(log_goes_downwards ? QTextCursor::End
: QTextCursor::Start);

// Only prepend with newline if log goes downwards
if (log_goes_downwards && need_newline) {
ui_ic_chatlog_pinned->textCursor().insertBlock(format);
}

// Timestamp if we're doing that meme
if (log_timestamp) {
// Format the timestamp
QTextCharFormat format = selfname ? selftimestamp_format : timestamp_format;
if (timestamp.isValid()) {
ui_ic_chatlog_pinned->textCursor().insertText(
"[" + timestamp.toString(log_timestamp_format) + "] ", format);
}
else {
qCritical() << "could not insert invalid timestamp" << timestamp;
}
}

// Format the name of the actor
QTextCharFormat name_format = selfname ? own_name : other_name;
ui_ic_chatlog_pinned->textCursor().insertText("📌" + p_name + "📌", name_format);
// Special case for stopping the music
if (p_action == tr("has stopped the music")) {
ui_ic_chatlog_pinned->textCursor().insertText(" " + p_action + ".", normal);
}
// Make shout text bold
else if (p_action == tr("shouts") && log_ic_actions) {
ui_ic_chatlog_pinned->textCursor().insertText(" " + p_action + " ", normal);
if (log_colors) {
ui_ic_chatlog_pinned->textCursor().insertHtml(
"<b>" +
filter_ic_text(p_text, true, -1, 0)
.replace("$c0", chatlog_color.name(QColor::HexArgb)) +
"</b>");
}
else
ui_ic_chatlog_pinned->textCursor().insertText(" " + p_text, italics);
}
// If action not blank:
else if (p_action != "" && log_ic_actions) {
// Format the action in normal
ui_ic_chatlog_pinned->textCursor().insertText(" " + p_action, normal);
if (log_newline)
// For some reason, we're forced to use <br> instead of the more sensible
// \n. Why? Because \n is treated as a new Block instead of a soft newline
// within a paragraph!
ui_ic_chatlog_pinned->textCursor().insertHtml("<br>");
else
ui_ic_chatlog_pinned->textCursor().insertText(": ", normal);
// Format the result in italics
ui_ic_chatlog->textCursor().insertText(p_text + ".", italics);
}
else {
if (log_newline) {
// For some reason, we're forced to use <br> instead of the more sensible
// \n. Why? Because \n is treated as a new Block instead of a soft newline
// within a paragraph!
ui_ic_chatlog_pinned->textCursor().insertHtml("<br>");
}
else {
ui_ic_chatlog_pinned->textCursor().insertText(": ", normal);
}
// Format the result according to html
if (log_colors) {
QString p_text_filtered = filter_ic_text(p_text, true, -1, color);
p_text_filtered =
p_text_filtered.replace("$c0", chatlog_color.name(QColor::HexArgb));
for (int c = 1; c < max_colors; ++c) {
QColor color_result = default_color_rgb_list.at(c);
p_text_filtered = p_text_filtered.replace(
"$c" + QString::number(c), color_result.name(QColor::HexArgb));
}
ui_ic_chatlog_pinned->textCursor().insertHtml(p_text_filtered);
}
else {
ui_ic_chatlog_pinned->textCursor().insertText(filter_ic_text(p_text, false),
normal);
}
}

// Only append with newline if log goes upwards
if (!log_goes_downwards && need_newline) {
ui_ic_chatlog_pinned->textCursor().insertBlock(format);
}

// If we got too many blocks in the current log, delete some.
while (ui_ic_chatlog_pinned->document()->blockCount() > log_maximum_blocks &&
log_maximum_blocks > 0) {
QTextCursor temp_curs = ui_ic_chatlog_pinned->textCursor();
temp_curs.movePosition(log_goes_downwards ? QTextCursor::Start
: QTextCursor::End);
temp_curs.select(QTextCursor::BlockUnderCursor);
temp_curs.removeSelectedText();
if (log_goes_downwards)
temp_curs.deleteChar();
else
temp_curs.deletePreviousChar();
}

// Finally, scroll the scrollbar to the correct position.
if (old_cursor.hasSelection() ||
old_scrollbar_value != scrollbar_target_value) {
// The user has selected text or scrolled away from the bottom: maintain
// position.
ui_ic_chatlog_pinned->setTextCursor(old_cursor);
ui_ic_chatlog_pinned->verticalScrollBar()->setValue(old_scrollbar_value);
}
else {
ui_ic_chatlog_pinned->verticalScrollBar()->setValue(
log_goes_downwards ? ui_ic_chatlog->verticalScrollBar()->maximum() : 0);
}
}

void Courtroom::clear_pinned_message() { ui_ic_chatlog_pinned->clear(); }

Comment on lines +4033 to +4183
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚨 COPY PASTE ALERT 🚨

please revise, never copy-paste this much code! Adjust the existing function(s) instead and refactor them to allow your use case.

void Courtroom::pop_ic_ghost()
{
QTextCursor ghost = ui_ic_chatlog->textCursor();
Expand Down Expand Up @@ -6651,6 +6824,18 @@ void Courtroom::on_switch_area_music_clicked()

}

void Courtroom::on_switch_logs_clicked()
{
if (ui_ic_chatlog->isHidden()) {
ui_ic_chatlog->show();
ui_ic_chatlog_pinned->hide();
}
else {
ui_ic_chatlog->hide();
ui_ic_chatlog_pinned->show();
}
}

void Courtroom::ping_server()
{
ping_timer.start();
Expand Down
11 changes: 11 additions & 0 deletions src/packet_distribution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,17 @@ void AOApplication::server_packet_received(AOPacket *p_packet)
w_courtroom->chatmessage_enqueue(p_packet->get_contents());
}
}
else if (header == "PM") {
if (courtroom_constructed && courtroom_loaded) {
w_courtroom->append_pinned_text(f_contents.at(0), f_contents.at(1),
f_contents.at(2));
}
}
else if (header == "CPM") {
if (courtroom_constructed && courtroom_loaded) {
w_courtroom->clear_pinned_message();
}
}
else if (header == "MC") {
if (courtroom_constructed && courtroom_loaded)
{
Expand Down