1
0
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:
Tux3 / Mlkj / !Lev.uXFMLA 2014-10-07 19:58:18 +02:00
commit 6aa974c764
2 changed files with 40 additions and 24 deletions

View File

@ -25,6 +25,7 @@
ChatAreaWidget::ChatAreaWidget(QWidget *parent)
: QTextBrowser(parent)
, tableFrmt(nullptr)
, nameWidth(75)
{
setReadOnly(true);
@ -37,15 +38,6 @@ ChatAreaWidget::ChatAreaWidget(QWidget *parent)
setAcceptRichText(false);
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.setNonBreakableLines(true);
dateFormat.setAlignment(Qt::AlignLeft);
@ -60,6 +52,9 @@ ChatAreaWidget::~ChatAreaWidget()
for (ChatAction* action : messages)
delete action;
messages.clear();
if (tableFrmt)
delete tableFrmt;
}
void ChatAreaWidget::mouseReleaseEvent(QMouseEvent * event)
@ -108,16 +103,15 @@ void ChatAreaWidget::insertMessage(ChatAction *msgAction)
checkSlider();
int row = chatTextTable->rows() - 1;
QTextCursor cur = chatTextTable->cellAt(row,1).firstCursorPosition();
QTextTable *chatTextTable = getMsgTable();
QTextCursor cur = chatTextTable->cellAt(0, 2).firstCursorPosition();
cur.clearSelection();
cur.setKeepPositionOnInsert(true);
chatTextTable->appendRows(1);
chatTextTable->cellAt(row,0).firstCursorPosition().setBlockFormat(nameFormat);
chatTextTable->cellAt(row,0).firstCursorPosition().insertHtml(msgAction->getName());
chatTextTable->cellAt(row,1).firstCursorPosition().insertHtml(msgAction->getMessage());
chatTextTable->cellAt(row,2).firstCursorPosition().setBlockFormat(dateFormat);
chatTextTable->cellAt(row,2).firstCursorPosition().insertHtml(msgAction->getDate());
chatTextTable->cellAt(0, 0).firstCursorPosition().setBlockFormat(nameFormat);
chatTextTable->cellAt(0, 0).firstCursorPosition().insertHtml(msgAction->getName());
chatTextTable->cellAt(0, 2).firstCursorPosition().insertHtml(msgAction->getMessage());
chatTextTable->cellAt(0, 4).firstCursorPosition().setBlockFormat(dateFormat);
chatTextTable->cellAt(0, 4).firstCursorPosition().insertHtml(msgAction->getDate());
msgAction->setup(cur, this);
@ -137,13 +131,34 @@ void ChatAreaWidget::checkSlider()
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)
{
nameWidth = w;
if (tableFrmt != nullptr)
{
delete tableFrmt;
tableFrmt = nullptr;
}
QTextTableFormat tableFormat = chatTextTable->format();
tableFormat.setColumnWidthConstraints({QTextLength(QTextLength::FixedLength, 100),
QTextLength(QTextLength::PercentageLength, 100),
QTextLength(QTextLength::FixedLength, 40)});
chatTextTable->setFormat(tableFormat);
nameWidth = w;
}

View File

@ -46,12 +46,13 @@ private slots:
private:
void checkSlider();
QTextTable* getMsgTable();
QTextTableFormat* tableFrmt;
QList<ChatAction*> messages;
bool lockSliderToBottom;
int sliderPosition;
int nameWidth;
QTextTable *chatTextTable;
QTextBlockFormat nameFormat, dateFormat;
};