Add possibility to change only selected text in chat.

This commit is contained in:
Phenom 2021-01-25 12:22:03 +01:00
parent 67d1265eb2
commit a4623af383
2 changed files with 69 additions and 46 deletions

View File

@ -19,23 +19,9 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * * along with this program. If not, see <https://www.gnu.org/licenses/>. *
* * * *
*******************************************************************************/ *******************************************************************************/
#include <QApplication>
#include <QBuffer>
#include <QColorDialog>
#include <QKeyEvent>
#include <QMenu>
#include <QMessageBox>
#include <QScrollBar>
#include <QStringListModel>
#include <QTextCodec>
#include <QTextDocumentFragment>
#include <QTextStream>
#include <QTimer>
#include <QToolTip>
#include <QInputDialog>
#include "ChatWidget.h" #include "ChatWidget.h"
#include "ui_ChatWidget.h" #include "ui_ChatWidget.h"
#include "gui/MainWindow.h" #include "gui/MainWindow.h"
#include "gui/notifyqt.h" #include "gui/notifyqt.h"
#include "gui/RetroShareLink.h" #include "gui/RetroShareLink.h"
@ -61,6 +47,21 @@
#include <retroshare/rsmsgs.h> #include <retroshare/rsmsgs.h>
#include <retroshare/rsplugin.h> #include <retroshare/rsplugin.h>
#include <QApplication>
#include <QBuffer>
#include <QColorDialog>
#include <QKeyEvent>
#include <QMenu>
#include <QMessageBox>
#include <QScrollBar>
#include <QStringListModel>
#include <QTextCodec>
#include <QTextDocumentFragment>
#include <QTextStream>
#include <QTimer>
#include <QToolTip>
#include <QInputDialog>
#include <time.h> #include <time.h>
#define FMM 2.5//fontMetricsMultiplicator #define FMM 2.5//fontMetricsMultiplicator
@ -405,19 +406,18 @@ void ChatWidget::init(const ChatId &chat_id, const QString &title)
if (chatId.isLobbyId() || chatId.isDistantChatId()) if (chatId.isLobbyId() || chatId.isDistantChatId())
{ {
RsIdentityDetails details; RsIdentityDetails details;
time_t start = time(nullptr);
if (rsIdentity->getIdDetails(RsGxsId(historyIt->peerId), details)) if (rsIdentity->getIdDetails(RsGxsId(historyIt->peerId), details))
name = QString::fromUtf8(details.mNickname.c_str()); name = QString::fromUtf8(details.mNickname.c_str());
else if(!historyIt->peerName.empty()) else if(!historyIt->peerName.empty())
name = QString::fromUtf8(historyIt->peerName.c_str()); name = QString::fromUtf8(historyIt->peerName.c_str());
else else
name = QString::fromUtf8(historyIt->peerId.toStdString().c_str()); name = QString::fromUtf8(historyIt->peerId.toStdString().c_str());
} else { } else {
name = QString::fromUtf8(historyIt->peerId.toStdString().c_str()); name = QString::fromUtf8(historyIt->peerId.toStdString().c_str());
} }
addChatMsg(historyIt->incoming, name, RsGxsId(historyIt->peerId.toStdString().c_str()), QDateTime::fromTime_t(historyIt->sendTime), QDateTime::fromTime_t(historyIt->recvTime), QString::fromUtf8(historyIt->message.c_str()), MSGTYPE_HISTORY); addChatMsg(historyIt->incoming, name, RsGxsId(historyIt->peerId.toStdString().c_str()), QDateTime::fromTime_t(historyIt->sendTime), QDateTime::fromTime_t(historyIt->recvTime), QString::fromUtf8(historyIt->message.c_str()), MSGTYPE_HISTORY);
} }
} }
} }
@ -911,13 +911,13 @@ void ChatWidget::showEvent(QShowEvent */*event*/)
// otherwise focus will be get even not chat itself // otherwise focus will be get even not chat itself
ui->textBrowser->setFocus(); ui->textBrowser->setFocus();
} }
ChatUserNotify::clearWaitingChat(chatId); ChatUserNotify::clearWaitingChat(chatId);
if (firstShow) { if (firstShow) {
// Workaround: now the scroll position is correct calculated // Workaround: now the scroll position is correct calculated
firstShow = false; firstShow = false;
QScrollBar *scrollbar = ui->textBrowser->verticalScrollBar(); QScrollBar *scrollbar2 = ui->textBrowser->verticalScrollBar();
scrollbar->setValue(scrollbar->maximum()); scrollbar2->setValue(scrollbar2->maximum());
} }
} }
@ -1359,8 +1359,6 @@ bool ChatWidget::findText(const QString& qsStringToFind, bool bBackWard, bool bF
{ {
QTextDocument *qtdDocument = ui->textBrowser->document(); QTextDocument *qtdDocument = ui->textBrowser->document();
bool bFound = false; bool bFound = false;
bool bFirstFound = true;
uint uiFoundCount = 0;
removeFoundText(); removeFoundText();
@ -1370,6 +1368,9 @@ bool ChatWidget::findText(const QString& qsStringToFind, bool bBackWard, bool bF
if (!qsStringToFind.isEmpty()) if (!qsStringToFind.isEmpty())
{ {
bool bFirstFound = true;
uint uiFoundCount = 0;
QPalette qpBackGround=ui->leSearch->palette(); QPalette qpBackGround=ui->leSearch->palette();
QTextCursor qtcHighLight(qtdDocument); QTextCursor qtcHighLight(qtdDocument);
@ -1488,10 +1489,10 @@ void ChatWidget::on_markButton_clicked(bool bValue)
void ChatWidget::chooseColor() void ChatWidget::chooseColor()
{ {
bool ok; bool ok;
QRgb color = QColorDialog::getRgba(ui->chatTextEdit->textColor().rgba(), &ok, window()); QRgb color = QColorDialog::getRgba(currentColor.rgba(), &ok, window());
if (ok) { if (ok) {
currentColor = QColor(color); currentColor = QColor(color);
PeerSettings->setPrivateChatColor(chatId, currentColor.name()); PeerSettings->setPrivateChatColor(chatId, currentColor.name());
colorChanged(); colorChanged();
setColorAndFont(false); setColorAndFont(false);
} }
@ -1510,9 +1511,24 @@ void ChatWidget::chooseFont()
//Use NULL as parent as with this QFontDialog don't take care of title nether options. //Use NULL as parent as with this QFontDialog don't take care of title nether options.
QFont font = misc::getFont(&ok, currentFont, nullptr, tr("Choose your font.")); QFont font = misc::getFont(&ok, currentFont, nullptr, tr("Choose your font."));
QTextCursor cursor = ui->chatTextEdit->textCursor();
if (ok) { if (ok) {
currentFont = font; if (cursor.selection().isEmpty()){
setFont(); currentFont = font;
setFont();
} else {
//Merge Format doesn't works for only selection so add a new block.
QString text = "<p style=\"";
text += " color: " + currentColor.name() + ";";
text += " font-family:" + font.family() + ";";
text += font.bold() ? " font-weight: bold;" : "";
text += font.italic() ? " font-style: italic;" : "";
text += " font-size:" + QString::number(font.pointSize()) + "px;";
text += font.strikeOut() ? " text-decoration: line-through;" : "";
text += font.underline() ? " text-decoration: underline;" : "";
text += "\">" + cursor.selectedText().toHtmlEscaped() + "</p>";
cursor.insertHtml(text);
}
} }
} }
@ -1531,16 +1547,20 @@ void ChatWidget::resetFonts()
void ChatWidget::setColorAndFont(bool both) void ChatWidget::setColorAndFont(bool both)
{ {
QTextCharFormat format;
format.setForeground(currentColor);
format.setFont(currentFont);
ui->chatTextEdit->mergeCurrentCharFormat(format);
ui->chatTextEdit->setFont(currentFont); if (both)
QStringList fontdata=currentFont.toString().split(","); {
QString stylesheet="font-family: '"+fontdata[0]+"'; font-size: "+fontdata[1]+"pt;"; QStringList fontdata=currentFont.toString().split(",");
if (currentFont.bold()) stylesheet+=" font-weight: bold;"; QString stylesheet="font-family: '"+fontdata[0]+"'; font-size: "+fontdata[1]+"pt;";
if (currentFont.italic()) stylesheet+=" font-style: italic;"; if (currentFont.bold()) stylesheet+=" font-weight: bold;";
if (currentFont.underline()) stylesheet+=" text-decoration: underline;"; if (currentFont.italic()) stylesheet+=" font-style: italic;";
if (both) ui->textBrowser->setStyleSheet(stylesheet); if (currentFont.underline()) stylesheet+=" text-decoration: underline;";
ui->chatTextEdit->setStyleSheet(stylesheet); ui->textBrowser->setStyleSheet(stylesheet);
ui->chatTextEdit->setTextColor(currentColor); }
ui->chatTextEdit->setFocus(); ui->chatTextEdit->setFocus();
} }
@ -1548,7 +1568,7 @@ void ChatWidget::setColorAndFont(bool both)
void ChatWidget::setFont() void ChatWidget::setFont()
{ {
setColorAndFont(false); setColorAndFont(false);
PeerSettings->setPrivateChatFont(chatId, currentFont.toString()); PeerSettings->setPrivateChatFont(chatId, currentFont.toString());
} }
void ChatWidget::smileyWidget() void ChatWidget::smileyWidget()
@ -1562,8 +1582,11 @@ void ChatWidget::addSmiley()
// add trailing space // add trailing space
smiley += QString(" "); smiley += QString(" ");
// add preceding space when needed (not at start of text or preceding space already exists) // add preceding space when needed (not at start of text or preceding space already exists)
if(!ui->chatTextEdit->textCursor().atStart() && ui->chatTextEdit->toPlainText()[ui->chatTextEdit->textCursor().position() - 1] != QChar(' ')) QString plainText = ui->chatTextEdit->toPlainText();
QChar start = plainText[ui->chatTextEdit->textCursor().position() - 1];
if(!ui->chatTextEdit->textCursor().atStart() && start != QChar(' '))
smiley = QString(" ") + smiley; smiley = QString(" ") + smiley;
ui->chatTextEdit->textCursor().insertText(smiley); ui->chatTextEdit->textCursor().insertText(smiley);
} }
@ -1911,7 +1934,7 @@ void ChatWidget::updateCMPreview()
void ChatWidget::quote() void ChatWidget::quote()
{ {
QString text = RsHtml::makeQuotedText(ui->textBrowser); QString text = RsHtml::makeQuotedText(ui->textBrowser);
emit ui->chatTextEdit->append(text); ui->chatTextEdit->append(text);
} }
void ChatWidget::dropPlacemark() void ChatWidget::dropPlacemark()

View File

@ -57,7 +57,7 @@ class ChatWidget;
class ChatWidgetHolder class ChatWidgetHolder
{ {
public: public:
ChatWidgetHolder(ChatWidget *chatWidget) : mChatWidget(chatWidget) {} explicit ChatWidgetHolder(ChatWidget *chatWidget) : mChatWidget(chatWidget) {}
virtual ~ChatWidgetHolder() {} virtual ~ChatWidgetHolder() {}
// status comes from notifyPeerStatusChanged // status comes from notifyPeerStatusChanged