From c6c6e11bec4a7473c950aac3e499c52e4a388dc9 Mon Sep 17 00:00:00 2001 From: chelovechishko Date: Tue, 3 May 2022 19:26:45 +0900 Subject: [PATCH] gui: chat: add auto shrink chattextedit To shrink text edit form when it is convenient for browsing text browser. It just shrink/unshrink the edit here and there when switching between chat rooms. P.S. user should not resize chattextedit when it's shrinked P.P.S. okey, there is no "unshrink" maybe, but hope it's not big deal --- retroshare-gui/src/gui/chat/ChatWidget.cpp | 73 ++++++++++++++++++- retroshare-gui/src/gui/chat/ChatWidget.h | 5 ++ retroshare-gui/src/gui/chat/ChatWidget.ui | 2 +- retroshare-gui/src/gui/settings/ChatPage.cpp | 3 + retroshare-gui/src/gui/settings/ChatPage.ui | 10 +++ .../src/gui/settings/rsharesettings.cpp | 10 +++ .../src/gui/settings/rsharesettings.h | 3 + 7 files changed, 104 insertions(+), 2 deletions(-) diff --git a/retroshare-gui/src/gui/chat/ChatWidget.cpp b/retroshare-gui/src/gui/chat/ChatWidget.cpp index a909e3e27..ab198fa33 100644 --- a/retroshare-gui/src/gui/chat/ChatWidget.cpp +++ b/retroshare-gui/src/gui/chat/ChatWidget.cpp @@ -474,6 +474,7 @@ void ChatWidget::processSettings(bool load) // state of splitter ui->chatVSplitter->restoreState(Settings->value("ChatSplitter").toByteArray()); } else { + shrinkChatTextEdit(false); // save settings // state of splitter @@ -643,6 +644,16 @@ bool ChatWidget::eventFilter(QObject *obj, QEvent *event) } } else if (obj == ui->chatTextEdit) { + if (chatType() == CHATTYPE_LOBBY) { + #define EVENT_IS(q_event) (event->type() == QEvent::q_event) + if (EVENT_IS(FocusIn)) { + if (was_shrinked) { + shrinkChatTextEdit(false); + } + } + #undef EVENT_IS + } + if (event->type() == QEvent::KeyPress) { QKeyEvent *keyEvent = static_cast(event); @@ -905,11 +916,19 @@ void ChatWidget::showEvent(QShowEvent */*event*/) QScrollBar *scrollbar = ui->textBrowser->verticalScrollBar(); bool is_scrollbar_at_end = scrollbar->value() == scrollbar->maximum(); bool is_chat_text_edit_empty = ui->chatTextEdit->toPlainText().isEmpty(); + // show event will not be called on every change of focus if (is_scrollbar_at_end || !is_chat_text_edit_empty) { + if (!firstShow) { + shrinkChatTextEdit(false); + } focusDialog(); } else { - // otherwise focus will be get even not chat itself + // otherwise, focus will not even be gotten by chat itself ui->textBrowser->setFocus(); + + if (!firstShow && !was_shrinked) { + shrinkChatTextEdit(true); + } } ChatUserNotify::clearWaitingChat(chatId); @@ -923,6 +942,11 @@ void ChatWidget::showEvent(QShowEvent */*event*/) void ChatWidget::resizeEvent(QResizeEvent */*event*/) { + // it's about resize all chat window, not about chattextedit + // just unshrink it and do not bother + if (was_shrinked) { + shrinkChatTextEdit(false); + } // Workaround: now the scroll position is correct calculated QScrollBar *scrollbar = ui->textBrowser->verticalScrollBar(); scrollbar->setValue(scrollbar->maximum()); @@ -1967,3 +1991,50 @@ void ChatWidget::saveSticker() filename = Emoticons::importedStickerPath() + "/" + filename + ".png"; ImageUtil::extractImage(window(), cursor, filename); } + +void ChatWidget::shrinkChatTextEdit(bool shrink_me) +{ + // here and at eventfiltert check + if (chatType() != CHATTYPE_LOBBY) + return; + if (!Settings->getShrinkChatTextEdit()) { + if (was_shrinked) { + ui->chatVSplitter->setSizes(_chatvsplitter_saved_size); + } + _chatvsplitter_saved_size.clear(); + was_shrinked = false; + } + + if (Settings->getShrinkChatTextEdit()) { + if (shrink_me) { + if (!was_shrinked) { + _chatvsplitter_saved_size = ui->chatVSplitter->sizes(); + + QList shrinked_v_splitter_size = _chatvsplitter_saved_size; + // #define TEXT_BROWSER ui->chatVSplitter->indexOf(ui->textBrowser) + #define TEXT_BROWSER 0 + // when you will update the layout one more time change this appropriately + // #define BELOW_TEXT_BROWSER ui->chatVSplitter->indexOf(ui->chatVSplitter->widget(1)) + #define BELOW_TEXT_BROWSER 1 + int height_diff = shrinked_v_splitter_size[BELOW_TEXT_BROWSER] - ui->chatTextEdit->minimumHeight(); + shrinked_v_splitter_size[BELOW_TEXT_BROWSER] = ui->chatTextEdit->minimumHeight(); + shrinked_v_splitter_size[TEXT_BROWSER] += height_diff; + ui->chatVSplitter->setSizes( shrinked_v_splitter_size ); + #undef TEXT_BROWSER + #undef BELOW_TEXT_BROWSER + was_shrinked = true; + } + } else { // (!shrink_me) + if (was_shrinked) { + // to not shrink/unshrink at every entry into chat + // when unshrinked state is enough to a browser be scrollable, but shrinked - not + QScrollBar *scrollbar = ui->textBrowser->verticalScrollBar(); + bool is_scrollbar_at_end = scrollbar->value() == scrollbar->maximum(); + ui->chatVSplitter->setSizes(_chatvsplitter_saved_size); + if (is_scrollbar_at_end) + scrollbar->setValue(scrollbar->maximum()); + was_shrinked = false; + } + } + } +} diff --git a/retroshare-gui/src/gui/chat/ChatWidget.h b/retroshare-gui/src/gui/chat/ChatWidget.h index b9ff7086f..82fb6a486 100644 --- a/retroshare-gui/src/gui/chat/ChatWidget.h +++ b/retroshare-gui/src/gui/chat/ChatWidget.h @@ -215,6 +215,8 @@ private: void completeNickname(bool reverse); QAbstractItemModel *modelFromPeers(); + void shrinkChatTextEdit(bool shrink_me); + ChatId chatId; QString title; QString name; @@ -264,6 +266,9 @@ private: QCompleter *completer; ImHistoryBrowser* imBrowser; + QList _chatvsplitter_saved_size; + bool was_shrinked = false; + QList mChatWidgetHolder; ChatLobbyUserNotify* notify; diff --git a/retroshare-gui/src/gui/chat/ChatWidget.ui b/retroshare-gui/src/gui/chat/ChatWidget.ui index 879c99d00..d46093071 100644 --- a/retroshare-gui/src/gui/chat/ChatWidget.ui +++ b/retroshare-gui/src/gui/chat/ChatWidget.ui @@ -661,7 +661,7 @@ border-image: url(:/images/closepressed.png) 0 - 30 + 40 diff --git a/retroshare-gui/src/gui/settings/ChatPage.cpp b/retroshare-gui/src/gui/settings/ChatPage.cpp index b240e216d..86cfaf816 100644 --- a/retroshare-gui/src/gui/settings/ChatPage.cpp +++ b/retroshare-gui/src/gui/settings/ChatPage.cpp @@ -135,6 +135,7 @@ void ChatPage::updateChatParams() Settings->setChatSendAsPlainTextByDef(ui.sendAsPlainTextByDef->isChecked()); Settings->setChatLoadEmbeddedImages(ui.loadEmbeddedImages->isChecked()); Settings->setChatDoNotSendIsTyping(ui.DontSendTyping->isChecked()); + Settings->setShrinkChatTextEdit(ui.shrinkChatTextEdit->isChecked()); } void ChatPage::updateChatSearchParams() @@ -248,6 +249,7 @@ ChatPage::ChatPage(QWidget * parent, Qt::WindowFlags flags) connect(ui.sendAsPlainTextByDef, SIGNAL(toggled(bool)), this, SLOT(updateChatParams())); connect(ui.loadEmbeddedImages, SIGNAL(toggled(bool)), this, SLOT(updateChatParams())); connect(ui.DontSendTyping, SIGNAL(toggled(bool)), this, SLOT(updateChatParams())); + connect(ui.shrinkChatTextEdit, SIGNAL(toggled(bool)), this, SLOT(updateChatParams())); connect(ui.sbSearch_CharToStart, SIGNAL(valueChanged(int)), this, SLOT(updateChatSearchParams())); connect(ui.cbSearch_CaseSensitively, SIGNAL(toggled(bool)), this, SLOT(updateChatSearchParams())); @@ -403,6 +405,7 @@ ChatPage::load() whileBlocking(ui.sendAsPlainTextByDef)->setChecked(Settings->getChatSendAsPlainTextByDef()); whileBlocking(ui.loadEmbeddedImages)->setChecked(Settings->getChatLoadEmbeddedImages()); whileBlocking(ui.DontSendTyping)->setChecked(Settings->getChatDoNotSendIsTyping()); + whileBlocking(ui.shrinkChatTextEdit)->setChecked(Settings->getShrinkChatTextEdit()); std::string advsetting; if(rsConfig->getConfigurationOption(RS_CONFIG_ADVANCED, advsetting) && (advsetting == "YES")) diff --git a/retroshare-gui/src/gui/settings/ChatPage.ui b/retroshare-gui/src/gui/settings/ChatPage.ui index f33362563..0f60752ac 100644 --- a/retroshare-gui/src/gui/settings/ChatPage.ui +++ b/retroshare-gui/src/gui/settings/ChatPage.ui @@ -95,6 +95,16 @@ + + + + When focus on text browser after showing chat room + + + Shrink text edit field when not needed + + + diff --git a/retroshare-gui/src/gui/settings/rsharesettings.cpp b/retroshare-gui/src/gui/settings/rsharesettings.cpp index 3411c78f7..586d4df4f 100644 --- a/retroshare-gui/src/gui/settings/rsharesettings.cpp +++ b/retroshare-gui/src/gui/settings/rsharesettings.cpp @@ -537,6 +537,16 @@ void RshareSettings::setChatSendAsPlainTextByDef(bool bValue) setValueToGroup("Chat", "SendAsPlainTextByDef", bValue); } +bool RshareSettings::getShrinkChatTextEdit() +{ + return valueFromGroup("Chat", "ShrinkChatTextEdit", false).toBool(); +} + +void RshareSettings::setShrinkChatTextEdit(bool bValue) +{ + setValueToGroup("Chat", "ShrinkChatTextEdit", bValue); +} + bool RshareSettings::getChatSearchShowBarByDefault() { return valueFromGroup("Chat", "SearchShowBarByDefault", false).toBool(); diff --git a/retroshare-gui/src/gui/settings/rsharesettings.h b/retroshare-gui/src/gui/settings/rsharesettings.h index abf728391..362e52606 100644 --- a/retroshare-gui/src/gui/settings/rsharesettings.h +++ b/retroshare-gui/src/gui/settings/rsharesettings.h @@ -217,6 +217,9 @@ public: bool getChatSendAsPlainTextByDef(); void setChatSendAsPlainTextByDef(bool bValue); + bool getShrinkChatTextEdit(); + void setShrinkChatTextEdit(bool bValue); + bool getChatSearchShowBarByDefault(); void setChatSearchShowBarByDefault(bool bValue);