From 366389c6bc8c3bd52303e791e42966a1ef6455b0 Mon Sep 17 00:00:00 2001 From: Cerapter Date: Fri, 27 Jul 2018 23:39:56 +0200 Subject: [PATCH] The log now has an option to go both ways. - Due to the log's nature, this must be set manually in one's `config.ini`. --- aoapplication.h | 4 +++ courtroom.cpp | 74 ++++++++++++++++++++++++++++------------- text_file_functions.cpp | 12 +++++++ 3 files changed, 67 insertions(+), 23 deletions(-) diff --git a/aoapplication.h b/aoapplication.h index 947bb1d..d94cd2a 100644 --- a/aoapplication.h +++ b/aoapplication.h @@ -133,6 +133,10 @@ public: //may contain, from config.ini. int get_max_log_size(); + // Returns whether the log should go upwards (new behaviour) + // or downwards (vanilla behaviour). + bool get_log_goes_downwards(); + // Returns the username the user may have set in config.ini. QString get_default_username(); diff --git a/courtroom.cpp b/courtroom.cpp index 4e1529b..fda29ce 100644 --- a/courtroom.cpp +++ b/courtroom.cpp @@ -1175,15 +1175,14 @@ void Courtroom::handle_chatmessage_3() void Courtroom::append_ic_text(QString p_text, QString p_name) { + bool downwards = ao_app->get_log_goes_downwards(); + QTextCharFormat bold; QTextCharFormat normal; bold.setFontWeight(QFont::Bold); normal.setFontWeight(QFont::Normal); const QTextCursor old_cursor = ui_ic_chatlog->textCursor(); const int old_scrollbar_value = ui_ic_chatlog->verticalScrollBar()->value(); - const bool is_scrolled_down = old_scrollbar_value == ui_ic_chatlog->verticalScrollBar()->maximum(); - - ui_ic_chatlog->moveCursor(QTextCursor::End); // Get rid of centering. if(p_text.startsWith(": ~~")) @@ -1313,29 +1312,58 @@ void Courtroom::append_ic_text(QString p_text, QString p_name) // After all of that, let's jot down the message into the IC chatlog. - if (!first_message_sent) + if (downwards) { - ui_ic_chatlog->textCursor().insertText(p_name, bold); - first_message_sent = true; - } - else - { - ui_ic_chatlog->textCursor().insertText('\n' + p_name, bold); - } + const bool is_scrolled_down = old_scrollbar_value == ui_ic_chatlog->verticalScrollBar()->maximum(); - ui_ic_chatlog->textCursor().insertText(p_text, normal); - - if (old_cursor.hasSelection() || !is_scrolled_down) - { - // The user has selected text or scrolled away from the top: maintain position. - ui_ic_chatlog->setTextCursor(old_cursor); - ui_ic_chatlog->verticalScrollBar()->setValue(old_scrollbar_value); - } - else - { - // The user hasn't selected any text and the scrollbar is at the top: scroll to the top. ui_ic_chatlog->moveCursor(QTextCursor::End); - ui_ic_chatlog->verticalScrollBar()->setValue(ui_ic_chatlog->verticalScrollBar()->maximum()); + + if (!first_message_sent) + { + ui_ic_chatlog->textCursor().insertText(p_name, bold); + first_message_sent = true; + } + else + { + ui_ic_chatlog->textCursor().insertText('\n' + p_name, bold); + } + + ui_ic_chatlog->textCursor().insertText(p_text, normal); + + if (old_cursor.hasSelection() || !is_scrolled_down) + { + // The user has selected text or scrolled away from the bottom: maintain position. + ui_ic_chatlog->setTextCursor(old_cursor); + ui_ic_chatlog->verticalScrollBar()->setValue(old_scrollbar_value); + } + else + { + // The user hasn't selected any text and the scrollbar is at the bottom: scroll to the top. + ui_ic_chatlog->moveCursor(QTextCursor::End); + ui_ic_chatlog->verticalScrollBar()->setValue(ui_ic_chatlog->verticalScrollBar()->maximum()); + } + } + else + { + const bool is_scrolled_up = old_scrollbar_value == ui_ic_chatlog->verticalScrollBar()->minimum(); + + ui_ic_chatlog->moveCursor(QTextCursor::Start); + + ui_ic_chatlog->textCursor().insertText(p_name, bold); + ui_ic_chatlog->textCursor().insertText(p_text + '\n', normal); + + if (old_cursor.hasSelection() || !is_scrolled_up) + { + // The user has selected text or scrolled away from the top: maintain position. + ui_ic_chatlog->setTextCursor(old_cursor); + ui_ic_chatlog->verticalScrollBar()->setValue(old_scrollbar_value); + } + else + { + // The user hasn't selected any text and the scrollbar is at the top: scroll to the top. + ui_ic_chatlog->moveCursor(QTextCursor::Start); + ui_ic_chatlog->verticalScrollBar()->setValue(ui_ic_chatlog->verticalScrollBar()->minimum()); + } } } diff --git a/text_file_functions.cpp b/text_file_functions.cpp index db858cb..1389cf5 100644 --- a/text_file_functions.cpp +++ b/text_file_functions.cpp @@ -99,6 +99,18 @@ int AOApplication::get_max_log_size() else return f_result.toInt(); } +bool AOApplication::get_log_goes_downwards() +{ + QString f_result = read_config("log_goes_downwards"); + + if (f_result == "true") + return true; + else if (f_result == "false") + return false; + else + return true; +} + QString AOApplication::get_default_username() { QString f_result = read_config("default_username");