Still, it'd be preferrable to operate on the Document itself. QTextCursor won't operate on the Html. Selected text is always rich text and never any of the XML elements. That makes it fairly useless.
Here's what I tried:
.h
#ifndef STYLEDCHATTEXTBROWSER_H
#define STYLEDCHATTEXTBROWSER_H
#include <QTextBrowser>
#include <QTextDocument>
#include <QTextCursor>
#include <QMap>
#include <QLinkedList>
#include <QColor>
{
Q_OBJECT
public:
explicit StyledChatTextBrowser
(QWidget *parent
= 0);
void setColorizeClientNames(bool doColor);
bool getColorizeClientNames();
signals:
public slots:
private:
bool doColor;
QMap<QString, QLinkedList<int>* >* clientMessagePositions;
void saveClientColorPosition
(QString client,
int position
);
};
#endif // STYLEDCHATTEXTBROWSER_H
#ifndef STYLEDCHATTEXTBROWSER_H
#define STYLEDCHATTEXTBROWSER_H
#include <QTextBrowser>
#include <QTextDocument>
#include <QTextCursor>
#include <QMap>
#include <QLinkedList>
#include <QColor>
class StyledChatTextBrowser : public QTextBrowser
{
Q_OBJECT
public:
explicit StyledChatTextBrowser(QWidget *parent = 0);
void logClientMessage(QString clientName, QString message);
void logEventMessage(QString eventType, QString message);
void setColorizeClientNames(bool doColor);
bool getColorizeClientNames();
signals:
public slots:
private:
QTextDocument* doc;
QTextCursor* cursor;
bool doColor;
QMap<QString, QLinkedList<int>* >* clientMessagePositions;
void saveClientColorPosition(QString client, int position);
QColor makeColorFromClientName(QString clientName);
};
#endif // STYLEDCHATTEXTBROWSER_H
To copy to clipboard, switch view to plain text mode
.cpp
#include "styledchattextbrowser.h"
#include <QDebug>
StyledChatTextBrowser
::StyledChatTextBrowser(QWidget *parent
) :{
doc->clear();
this->setDocument(doc);
this->doColor = false;
clientMessagePositions = new QMap<QString, QLinkedList<int>* >();
}
void StyledChatTextBrowser
::logClientMessage(QString clientName,
QString message
) { QString timestamp
= "[DUMMYSTAMP]";
QString tagOpen
= "<font style=\"color:#000000\">";
QString line
= timestamp
+ " " + tagOpen
+ clientName
+ tagClose
+ ": " + message
+ "\n";
int chars = doc->characterCount();
qDebug() << "Chars: " << chars;
cursor->setPosition(chars - 1);
cursor->insertHtml(line);
cursor->setPosition(chars - 1);
int pos = searchCursor.position() + 13;
cursor->setPosition(chars - 1);
saveClientColorPosition(clientName, pos);
qDebug() << doc->toHtml();
}
void StyledChatTextBrowser
::logEventMessage(QString eventType,
QString message
) {
}
void StyledChatTextBrowser::setColorizeClientNames(bool doColor) {
if (doColor != this->doColor) {
QList<QString> clients = clientMessagePositions->keys();
for (int i = 0; i < clientMessagePositions->size(); i++) {
QLinkedList<int>* positions = clientMessagePositions->value(clients.at(i));
QString hexColor
= doColor ? makeColorFromClientName
(clients.
at(i
)).
name() : "#000000";
QLinkedList<int>::iterator it;
for (it = positions->begin(); it != positions->end(); it++) {
cursor->insertText(hexColor);
}
}
}
this->doColor = doColor;
}
bool StyledChatTextBrowser::getColorizeClientNames() {
return doColor;
}
QColor StyledChatTextBrowser
::makeColorFromClientName(QString clientName
) { int r = 70; int g = 120; int b = 170;
for (int i = 0; i < clientName.length(); i++) {
switch (i % 3) {
case 0: r = ((r + b + clientName.at(i).unicode()) % 200) + 50; break;
case 1: g = ((g + b + clientName.at(i).unicode()) % 200) + 50; break;
case 2: b = ((b + r + g + clientName.at(i).unicode()) % 200) + 50; break;
}
}
c.setRed(r);
c.setGreen(g);
c.setBlue(b);
return c;
}
void StyledChatTextBrowser
::saveClientColorPosition(QString client,
int position
) { QLinkedList<int>* clientMessages = clientMessagePositions->value(client);
if (clientMessages == NULL || clientMessages->empty()) { // Nothing would have been in there if it was empty
clientMessages = new QLinkedList<int>();
clientMessagePositions->insert(client, clientMessages);
}
clientMessages->append(position);
}
#include "styledchattextbrowser.h"
#include <QDebug>
StyledChatTextBrowser::StyledChatTextBrowser(QWidget *parent) :
QTextBrowser(parent)
{
doc = new QTextDocument(this);
doc->clear();
this->setDocument(doc);
cursor = new QTextCursor(doc);
cursor->setPosition(0, QTextCursor::MoveAnchor);
this->doColor = false;
clientMessagePositions = new QMap<QString, QLinkedList<int>* >();
}
void StyledChatTextBrowser::logClientMessage(QString clientName, QString message) {
QString timestamp = "[DUMMYSTAMP]";
QString tagOpen = "<font style=\"color:#000000\">";
QString tagClose = "</font>";
QString line = timestamp + " " + tagOpen + clientName + tagClose + ": " + message + "\n";
int chars = doc->characterCount();
qDebug() << "Chars: " << chars;
cursor->setPosition(chars - 1);
cursor->insertHtml(line);
cursor->setPosition(chars - 1);
QTextCursor searchCursor = doc->find("style=\"color:#", *cursor, QTextDocument::FindCaseSensitively);
int pos = searchCursor.position() + 13;
cursor->setPosition(chars - 1);
saveClientColorPosition(clientName, pos);
qDebug() << doc->toHtml();
}
void StyledChatTextBrowser::logEventMessage(QString eventType, QString message) {
}
void StyledChatTextBrowser::setColorizeClientNames(bool doColor) {
if (doColor != this->doColor) {
QList<QString> clients = clientMessagePositions->keys();
for (int i = 0; i < clientMessagePositions->size(); i++) {
QLinkedList<int>* positions = clientMessagePositions->value(clients.at(i));
QString hexColor = doColor ? makeColorFromClientName(clients.at(i)).name() : "#000000";
QLinkedList<int>::iterator it;
for (it = positions->begin(); it != positions->end(); it++) {
cursor->setPosition(*it + 7, QTextCursor::MoveAnchor);
cursor->movePosition(QTextCursor::Left, QTextCursor::KeepAnchor, 7);
cursor->insertText(hexColor);
}
}
}
this->doColor = doColor;
}
bool StyledChatTextBrowser::getColorizeClientNames() {
return doColor;
}
QColor StyledChatTextBrowser::makeColorFromClientName(QString clientName) {
QColor c = QColor();
int r = 70; int g = 120; int b = 170;
for (int i = 0; i < clientName.length(); i++) {
switch (i % 3) {
case 0: r = ((r + b + clientName.at(i).unicode()) % 200) + 50; break;
case 1: g = ((g + b + clientName.at(i).unicode()) % 200) + 50; break;
case 2: b = ((b + r + g + clientName.at(i).unicode()) % 200) + 50; break;
}
}
c.setRed(r);
c.setGreen(g);
c.setBlue(b);
return c;
}
void StyledChatTextBrowser::saveClientColorPosition(QString client, int position) {
QLinkedList<int>* clientMessages = clientMessagePositions->value(client);
if (clientMessages == NULL || clientMessages->empty()) { // Nothing would have been in there if it was empty
clientMessages = new QLinkedList<int>();
clientMessagePositions->insert(client, clientMessages);
}
clientMessages->append(position);
}
To copy to clipboard, switch view to plain text mode
Gives output such as:
Starting D:\Projects\Qt\TestTextEdit-build-desktop-Qt_4_7_4_for_Desktop_-_MinGW_4_4__Qt_SDK__Release\release\TestTextEdit.e xe...
Chars: 1
"<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
<html><head><meta name="qrichtext" content="1" /><style type="text/css">
p, li { white-space: pre-wrap; }
</style></head><body style=" font-family:'MS Shell Dlg 2'; font-size:8.25pt; font-weight:400; font-style:normal;">
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">[DUMMYSTAMP] <span style=" color:#000000;">asd</span>: qwe </p></body></html>"
Chars: 23
"<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
<html><head><meta name="qrichtext" content="1" /><style type="text/css">
p, li { white-space: pre-wrap; }
</style></head><body style=" font-family:'MS Shell Dlg 2'; font-size:8.25pt; font-weight:400; font-style:normal;">
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">[DUMMYSTAMP]#bb37a2we [DUMMYSTAMP] <span style=" color:#000000;">asd</span>: </p></body></html>"
D:\Projects\Qt\TestTextEdit-build-desktop-Qt_4_7_4_for_Desktop_-_MinGW_4_4__Qt_SDK__Release\release\TestTextEdit.e xe exited with code 0
, when doing logClientMessage("asd", "qwe"), setColorizeClientNames(true), logClientMessage("asd", "").
Bookmarks