Skip to content

Commit

Permalink
KSTChat: Chat shows correctly special chars in messages and UserList
Browse files Browse the repository at this point in the history
  • Loading branch information
foldynl committed Sep 14, 2023
1 parent f9baf5e commit 5517aad
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 13 deletions.
16 changes: 3 additions & 13 deletions core/KSTChat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,21 +222,11 @@ QStringList KSTChat::joinLines(const QByteArray &data)

qCDebug(function_parameters) << data;

QByteArray::const_iterator iter = data.begin();
QByteArray fixedData;
QByteArray fixedData(data);
QStringList retList;

/* Remove received '\0' chars */
/* Received usually at the begining of the session */
while( iter != data.end() )
{
QChar c = *iter;
if (c != '\0')
fixedData.append(QString(c).toUtf8());
iter++;
}

receiveBuffer.append(fixedData);
fixedData.replace('\0', "");
receiveBuffer.append(QString::fromUtf8(fixedData));
int index = receiveBuffer.indexOf("\n");

while ( index != -1 )
Expand Down
39 changes: 39 additions & 0 deletions ui/KSTChatWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ KSTChatWidget::KSTChatWidget(int chatRoomIndex,
ui->usersTableView->sortByColumn(0, Qt::AscendingOrder);
ui->usersTableView->setItemDelegateForColumn(2, new UnitFormatDelegate("km", 1, 0.1, ui->usersTableView));
ui->usersTableView->setItemDelegateForColumn(3, new UnitFormatDelegate("°", 0, 0.1, ui->usersTableView));
ui->usersTableView->setItemDelegateForColumn(4, new HTMLDelegate(ui->usersTableView));

ui->usersTableView->horizontalHeader()->setSectionsMovable(true);
ui->usersTableView->addAction(ui->actionPrefillQSO);
Expand Down Expand Up @@ -674,3 +675,41 @@ KSTUsersInfo UserListModel::getUserInfo(const QModelIndex &index) const
{
return userData.at(index.row());
}

void HTMLDelegate::paint(QPainter* painter, const QStyleOptionViewItem & inOption, const QModelIndex &index) const
{
QStyleOptionViewItem option = inOption;
initStyleOption(&option, index);

QStyle *style = option.widget? option.widget->style() : QApplication::style();
QTextDocument doc;

doc.setHtml(option.text);
option.text = QString();
style->drawControl(QStyle::CE_ItemViewItem, &option, painter);

QAbstractTextDocumentLayout::PaintContext ctx;

// Highlighting text if item is selected
if (option.state & QStyle::State_Selected)
ctx.palette.setColor(QPalette::Text,
option.palette.color(QPalette::Active, QPalette::HighlightedText));

QRect textRect = style->subElementRect(QStyle::SE_ItemViewItemText, &option);
painter->save();
painter->translate(textRect.topLeft());
painter->setClipRect(textRect.translated(-textRect.topLeft()));
doc.documentLayout()->draw(painter, ctx);
painter->restore();
}

QSize HTMLDelegate::sizeHint ( const QStyleOptionViewItem & inOption, const QModelIndex & index ) const
{
QStyleOptionViewItem option = inOption;
initStyleOption(&option, index);

QTextDocument doc;
doc.setHtml(option.text);
doc.setTextWidth(option.rect.width());
return QSize(doc.idealWidth(), doc.size().height());
}
11 changes: 11 additions & 0 deletions ui/KSTChatWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ class ChatMessageModel : public QAbstractListModel
QList<QPair<int, KSTChatMsg>> messages;
};

class HTMLDelegate : public QStyledItemDelegate
{
public:
explicit HTMLDelegate(QObject* parent = nullptr) :
QStyledItemDelegate(parent){};

protected:
void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const;
QSize sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const;
};

class MessageDelegate : public QStyledItemDelegate
{

Expand Down

0 comments on commit 5517aad

Please sign in to comment.