mirror of
https://github.com/qTox/qTox.git
synced 2024-03-22 14:00:36 +08:00
Merge pull request #395 from apprb/chat
Reduction of CPU usage on new message insert
This commit is contained in:
commit
6aa974c764
|
@ -25,6 +25,7 @@
|
||||||
|
|
||||||
ChatAreaWidget::ChatAreaWidget(QWidget *parent)
|
ChatAreaWidget::ChatAreaWidget(QWidget *parent)
|
||||||
: QTextBrowser(parent)
|
: QTextBrowser(parent)
|
||||||
|
, tableFrmt(nullptr)
|
||||||
, nameWidth(75)
|
, nameWidth(75)
|
||||||
{
|
{
|
||||||
setReadOnly(true);
|
setReadOnly(true);
|
||||||
|
@ -37,15 +38,6 @@ ChatAreaWidget::ChatAreaWidget(QWidget *parent)
|
||||||
setAcceptRichText(false);
|
setAcceptRichText(false);
|
||||||
setFrameStyle(QFrame::NoFrame);
|
setFrameStyle(QFrame::NoFrame);
|
||||||
|
|
||||||
QTextTableFormat tableFormat;
|
|
||||||
tableFormat.setCellSpacing(5);
|
|
||||||
tableFormat.setBorderStyle(QTextFrameFormat::BorderStyle_None);
|
|
||||||
tableFormat.setColumnWidthConstraints({QTextLength(QTextLength::FixedLength,nameWidth),
|
|
||||||
QTextLength(QTextLength::PercentageLength,100),
|
|
||||||
QTextLength(QTextLength::VariableLength,0)});
|
|
||||||
|
|
||||||
chatTextTable = textCursor().insertTable(1,3,tableFormat);
|
|
||||||
|
|
||||||
nameFormat.setAlignment(Qt::AlignRight);
|
nameFormat.setAlignment(Qt::AlignRight);
|
||||||
nameFormat.setNonBreakableLines(true);
|
nameFormat.setNonBreakableLines(true);
|
||||||
dateFormat.setAlignment(Qt::AlignLeft);
|
dateFormat.setAlignment(Qt::AlignLeft);
|
||||||
|
@ -60,6 +52,9 @@ ChatAreaWidget::~ChatAreaWidget()
|
||||||
for (ChatAction* action : messages)
|
for (ChatAction* action : messages)
|
||||||
delete action;
|
delete action;
|
||||||
messages.clear();
|
messages.clear();
|
||||||
|
|
||||||
|
if (tableFrmt)
|
||||||
|
delete tableFrmt;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ChatAreaWidget::mouseReleaseEvent(QMouseEvent * event)
|
void ChatAreaWidget::mouseReleaseEvent(QMouseEvent * event)
|
||||||
|
@ -108,16 +103,15 @@ void ChatAreaWidget::insertMessage(ChatAction *msgAction)
|
||||||
|
|
||||||
checkSlider();
|
checkSlider();
|
||||||
|
|
||||||
int row = chatTextTable->rows() - 1;
|
QTextTable *chatTextTable = getMsgTable();
|
||||||
QTextCursor cur = chatTextTable->cellAt(row,1).firstCursorPosition();
|
QTextCursor cur = chatTextTable->cellAt(0, 2).firstCursorPosition();
|
||||||
cur.clearSelection();
|
cur.clearSelection();
|
||||||
cur.setKeepPositionOnInsert(true);
|
cur.setKeepPositionOnInsert(true);
|
||||||
chatTextTable->appendRows(1);
|
chatTextTable->cellAt(0, 0).firstCursorPosition().setBlockFormat(nameFormat);
|
||||||
chatTextTable->cellAt(row,0).firstCursorPosition().setBlockFormat(nameFormat);
|
chatTextTable->cellAt(0, 0).firstCursorPosition().insertHtml(msgAction->getName());
|
||||||
chatTextTable->cellAt(row,0).firstCursorPosition().insertHtml(msgAction->getName());
|
chatTextTable->cellAt(0, 2).firstCursorPosition().insertHtml(msgAction->getMessage());
|
||||||
chatTextTable->cellAt(row,1).firstCursorPosition().insertHtml(msgAction->getMessage());
|
chatTextTable->cellAt(0, 4).firstCursorPosition().setBlockFormat(dateFormat);
|
||||||
chatTextTable->cellAt(row,2).firstCursorPosition().setBlockFormat(dateFormat);
|
chatTextTable->cellAt(0, 4).firstCursorPosition().insertHtml(msgAction->getDate());
|
||||||
chatTextTable->cellAt(row,2).firstCursorPosition().insertHtml(msgAction->getDate());
|
|
||||||
|
|
||||||
msgAction->setup(cur, this);
|
msgAction->setup(cur, this);
|
||||||
|
|
||||||
|
@ -137,13 +131,34 @@ void ChatAreaWidget::checkSlider()
|
||||||
lockSliderToBottom = scroll && scroll->value() == scroll->maximum();
|
lockSliderToBottom = scroll && scroll->value() == scroll->maximum();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QTextTable *ChatAreaWidget::getMsgTable()
|
||||||
|
{
|
||||||
|
if (tableFrmt == nullptr)
|
||||||
|
{
|
||||||
|
tableFrmt = new QTextTableFormat();
|
||||||
|
tableFrmt->setCellSpacing(2);
|
||||||
|
tableFrmt->setBorderStyle(QTextFrameFormat::BorderStyle_None);
|
||||||
|
tableFrmt->setColumnWidthConstraints({QTextLength(QTextLength::FixedLength,nameWidth),
|
||||||
|
QTextLength(QTextLength::FixedLength,2),
|
||||||
|
QTextLength(QTextLength::PercentageLength,100),
|
||||||
|
QTextLength(QTextLength::FixedLength,2),
|
||||||
|
QTextLength(QTextLength::VariableLength,0)});
|
||||||
|
}
|
||||||
|
|
||||||
|
QTextCursor tc = textCursor();
|
||||||
|
tc.movePosition(QTextCursor::End);
|
||||||
|
QTextTable *chatTextTable = tc.insertTable(1, 5, *tableFrmt);
|
||||||
|
|
||||||
|
return chatTextTable;
|
||||||
|
}
|
||||||
|
|
||||||
void ChatAreaWidget::setNameColWidth(int w)
|
void ChatAreaWidget::setNameColWidth(int w)
|
||||||
{
|
{
|
||||||
nameWidth = w;
|
if (tableFrmt != nullptr)
|
||||||
|
{
|
||||||
QTextTableFormat tableFormat = chatTextTable->format();
|
delete tableFrmt;
|
||||||
tableFormat.setColumnWidthConstraints({QTextLength(QTextLength::FixedLength, 100),
|
tableFrmt = nullptr;
|
||||||
QTextLength(QTextLength::PercentageLength, 100),
|
}
|
||||||
QTextLength(QTextLength::FixedLength, 40)});
|
|
||||||
chatTextTable->setFormat(tableFormat);
|
nameWidth = w;
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,12 +46,13 @@ private slots:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void checkSlider();
|
void checkSlider();
|
||||||
|
QTextTable* getMsgTable();
|
||||||
|
|
||||||
|
QTextTableFormat* tableFrmt;
|
||||||
QList<ChatAction*> messages;
|
QList<ChatAction*> messages;
|
||||||
bool lockSliderToBottom;
|
bool lockSliderToBottom;
|
||||||
int sliderPosition;
|
int sliderPosition;
|
||||||
int nameWidth;
|
int nameWidth;
|
||||||
QTextTable *chatTextTable;
|
|
||||||
QTextBlockFormat nameFormat, dateFormat;
|
QTextBlockFormat nameFormat, dateFormat;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user