Added new flags to the html optimizer to remove font and color from html and used this for displaying the message in the toaster.

Fixed layout of the toasters.

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@5068 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
thunder2 2012-03-31 15:20:19 +00:00
parent 5b5d5b5c43
commit 25a266bcbf
9 changed files with 125 additions and 67 deletions

View File

@ -174,7 +174,15 @@ QString formatText(const QString &text, unsigned int flag)
}
QString formattedText = doc.toString(-1); // -1 removes any annoying carriage return misinterpreted by QTextEdit
optimizeHtml(formattedText);
unsigned int optimizeFlag = 0;
if (flag & RSHTML_FORMATTEXT_REMOVE_FONT) {
optimizeFlag |= RSHTML_OPTIMIZEHTML_REMOVE_FONT;
}
if (flag & RSHTML_FORMATTEXT_REMOVE_COLOR) {
optimizeFlag |= RSHTML_OPTIMIZEHTML_REMOVE_COLOR;
}
optimizeHtml(formattedText, optimizeFlag);
return formattedText;
}
@ -230,7 +238,7 @@ static void removeElement(QDomElement& parentElement, QDomElement& element)
parentElement.removeChild(element);
}
static void optimizeHtml(QDomDocument& doc, QDomElement& currentElement)
static void optimizeHtml(QDomDocument& doc, QDomElement& currentElement, unsigned int flag)
{
if (currentElement.tagName().toLower() == "html") {
// change <html> to <span>
@ -238,7 +246,6 @@ static void optimizeHtml(QDomDocument& doc, QDomElement& currentElement)
}
QDomNode styleNode;
QDomAttr styleAttr;
bool addBR = false;
QDomNodeList children = currentElement.childNodes();
@ -248,11 +255,46 @@ static void optimizeHtml(QDomDocument& doc, QDomElement& currentElement)
// compress style attribute
styleNode = node.attributes().namedItem("style");
if (styleNode.isAttr()) {
styleAttr = styleNode.toAttr();
QString value = styleAttr.value().simplified();
value.replace("margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px;", "margin:0px 0px 0px 0px;");
value.replace("; ", ";");
styleAttr.setValue(value);
QDomAttr styleAttr = styleNode.toAttr();
QString style = styleAttr.value().simplified();
style.replace("margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px;", "margin:0px 0px 0px 0px;");
style.replace("; ", ";");
if (flag & (RSHTML_OPTIMIZEHTML_REMOVE_FONT | RSHTML_OPTIMIZEHTML_REMOVE_COLOR)) {
QStringList styles = style.split(';');
style.clear();
foreach (QString pair, styles) {
if (!pair.trimmed().isEmpty()) {
QStringList keyvalue = pair.split(':');
if (keyvalue.length() == 2) {
QString key = keyvalue.at(0).trimmed();
if (flag & RSHTML_OPTIMIZEHTML_REMOVE_FONT) {
if (key == "font-family" ||
key == "font-size" ||
key == "font-weight" ||
key == "font-style") {
continue;
}
}
if (flag & RSHTML_OPTIMIZEHTML_REMOVE_COLOR) {
if (key == "color") {
continue;
}
}
style += key + ":" + keyvalue.at(1).trimmed() + ";";
} else {
style += pair + ";";
}
}
}
}
if (style.isEmpty()) {
node.attributes().removeNamedItem("style");
styleNode.clear();
} else {
styleAttr.setValue(style);
}
}
if (node.isElement()) {
@ -288,14 +330,14 @@ static void optimizeHtml(QDomDocument& doc, QDomElement& currentElement)
}
// iterate children
optimizeHtml(doc, element);
optimizeHtml(doc, element, flag);
// <p>
if (element.tagName().toLower() == "p") {
// <p style="...">
//styleNode = element.attributes().namedItem("style");
if (element.attributes().size() == 1 && styleNode.isAttr()) {
QString value = styleAttr.toAttr().value().simplified();
QString value = styleNode.toAttr().value().simplified();
if (value == "margin:0px 0px 0px 0px;-qt-block-indent:0;text-indent:0px;" ||
value.startsWith("-qt-paragraph-type:empty;margin:0px 0px 0px 0px;-qt-block-indent:0;text-indent:0px;")) {
@ -319,7 +361,7 @@ static void optimizeHtml(QDomDocument& doc, QDomElement& currentElement)
}
}
void optimizeHtml(QTextEdit *textEdit, QString &text)
void optimizeHtml(QTextEdit *textEdit, QString &text, unsigned int flag)
{
if (textEdit->toHtml() == QTextDocument(textEdit->toPlainText()).toHtml()) {
text = textEdit->toPlainText();
@ -329,10 +371,10 @@ void optimizeHtml(QTextEdit *textEdit, QString &text)
text = textEdit->toHtml();
optimizeHtml(text);
optimizeHtml(text, flag);
}
void optimizeHtml(QString &text)
void optimizeHtml(QString &text, unsigned int flag)
{
int originalLength = text.length();
@ -345,7 +387,7 @@ void optimizeHtml(QString &text)
}
QDomElement body = doc.documentElement();
optimizeHtml(doc, body);
optimizeHtml(doc, body, flag);
text = doc.toString(-1);
std::cerr << "Optimized text to " << text.length() << " bytes , instead of " << originalLength << std::endl;

View File

@ -36,6 +36,13 @@
/* Flags for RsHtml::formatText */
#define RSHTML_FORMATTEXT_EMBED_SMILEYS 1
#define RSHTML_FORMATTEXT_EMBED_LINKS 2
#define RSHTML_FORMATTEXT_REMOVE_FONT 4
#define RSHTML_FORMATTEXT_REMOVE_COLOR 8
#define RSHTML_FORMATTEXT_CLEANSTYLE (RSHTML_FORMATTEXT_REMOVE_FONT | RSHTML_FORMATTEXT_REMOVE_COLOR)
/* Flags for RsHtml::formatText */
#define RSHTML_OPTIMIZEHTML_REMOVE_FONT 2
#define RSHTML_OPTIMIZEHTML_REMOVE_COLOR 1
class QTextEdit;
@ -113,8 +120,8 @@ extern EmbedInHtmlImg defEmbedImg;
QString formatText(const QString &text, unsigned int flag);
bool findAnchors(const QString &text, QStringList& urls);
void optimizeHtml(QTextEdit *textEdit, QString &text);
void optimizeHtml(QString &text);
void optimizeHtml(QTextEdit *textEdit, QString &text, unsigned int flag = 0);
void optimizeHtml(QString &text, unsigned int flag = 0);
QString toHtml(QString text, bool realHtml = true);
} // namespace RsHtml

View File

@ -36,7 +36,9 @@ ChatLobbyToaster::ChatLobbyToaster(const std::string &peerId, const QString &nam
connect(ui.closeButton, SIGNAL(clicked()), SLOT(hide()));
/* set informations */
ui.messageLabel->setText(RsHtml::formatText(message, RSHTML_FORMATTEXT_EMBED_SMILEYS | RSHTML_FORMATTEXT_EMBED_LINKS));
ui.messageLabel->setText(RsHtml::formatText(message, RSHTML_FORMATTEXT_EMBED_SMILEYS | RSHTML_FORMATTEXT_EMBED_LINKS | RSHTML_FORMATTEXT_CLEANSTYLE));
ui.avatarWidget->setFrameType(AvatarWidget::NORMAL_FRAME);
ui.avatarWidget->setDefaultAvatar(":images/user/agt_forum64.png");
QString lobbyName = name;

View File

@ -32,7 +32,7 @@
<item>
<widget class="QFrame" name="windowFrame">
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
<enum>QFrame::WinPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
@ -131,6 +131,12 @@
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
</spacer>
</item>
</layout>
@ -143,8 +149,11 @@
<property name="rightMargin">
<number>4</number>
</property>
<property name="bottomMargin">
<number>4</number>
</property>
<property name="spacing">
<number>0</number>
<number>2</number>
</property>
<item row="0" column="1">
<widget class="QPushButton" name="chatButton">
@ -160,8 +169,8 @@
<widget class="QLabel" name="messageLabel">
<property name="maximumSize">
<size>
<width>220</width>
<height>47</height>
<width>218</width>
<height>45</height>
</size>
</property>
<property name="wordWrap">
@ -170,7 +179,7 @@
</widget>
</item>
<item row="0" column="0" rowspan="3">
<widget class="QLabel" name="avatar">
<widget class="AvatarWidget" name="avatarWidget" native="true">
<property name="minimumSize">
<size>
<width>70</width>
@ -183,15 +192,6 @@
<height>70</height>
</size>
</property>
<property name="frameShape">
<enum>QFrame::Panel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Sunken</enum>
</property>
<property name="pixmap">
<pixmap resource="../images.qrc">:/images/user/agt_forum64.png</pixmap>
</property>
</widget>
</item>
</layout>
@ -201,6 +201,14 @@
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>AvatarWidget</class>
<extends>QWidget</extends>
<header>gui/common/AvatarWidget.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources>
<include location="../images.qrc"/>
</resources>

View File

@ -36,7 +36,7 @@ ChatToaster::ChatToaster(const std::string &peerId, const QString &message) : QW
connect(ui.closeButton, SIGNAL(clicked()), SLOT(hide()));
/* set informations */
ui.messageLabel->setText(RsHtml::formatText(message, RSHTML_FORMATTEXT_EMBED_SMILEYS | RSHTML_FORMATTEXT_EMBED_LINKS));
ui.messageLabel->setText(RsHtml::formatText(message, RSHTML_FORMATTEXT_EMBED_SMILEYS | RSHTML_FORMATTEXT_EMBED_LINKS | RSHTML_FORMATTEXT_CLEANSTYLE));
ui.nameLabel->setText(QString::fromUtf8(rsPeers->getPeerName(peerId).c_str()));
ui.avatarWidget->setFrameType(AvatarWidget::STATUS_FRAME);
ui.avatarWidget->setId(peerId, false);

View File

@ -32,7 +32,7 @@
<item>
<widget class="QFrame" name="windowFrame">
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
<enum>QFrame::WinPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
@ -92,6 +92,12 @@
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="1">
@ -144,7 +150,7 @@
<number>4</number>
</property>
<property name="spacing">
<number>0</number>
<number>2</number>
</property>
<item row="0" column="1">
<widget class="QPushButton" name="chatButton">
@ -160,8 +166,8 @@
<widget class="QLabel" name="messageLabel">
<property name="maximumSize">
<size>
<width>220</width>
<height>47</height>
<width>218</width>
<height>45</height>
</size>
</property>
<property name="wordWrap">

View File

@ -34,7 +34,7 @@ GroupChatToaster::GroupChatToaster(const std::string &peerId, const QString &mes
connect(ui.closeButton, SIGNAL(clicked()), SLOT(hide()));
/* set informations */
ui.messageLabel->setText(RsHtml::formatText(message, RSHTML_FORMATTEXT_EMBED_SMILEYS | RSHTML_FORMATTEXT_EMBED_LINKS));
ui.messageLabel->setText(RsHtml::formatText(message, RSHTML_FORMATTEXT_EMBED_SMILEYS | RSHTML_FORMATTEXT_EMBED_LINKS | RSHTML_FORMATTEXT_CLEANSTYLE));
ui.nameLabel->setText(QString::fromUtf8(rsPeers->getPeerName(peerId).c_str()));
ui.avatarWidget->setFrameType(AvatarWidget::STATUS_FRAME);
ui.avatarWidget->setDefaultAvatar(":/images/user/personal64.png");

View File

@ -32,7 +32,7 @@
<item>
<widget class="QFrame" name="windowFrame">
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
<enum>QFrame::WinPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
@ -149,8 +149,14 @@
<property name="rightMargin">
<number>4</number>
</property>
<property name="spacing">
<number>0</number>
<property name="bottomMargin">
<number>4</number>
</property>
<property name="horizontalSpacing">
<number>2</number>
</property>
<property name="verticalSpacing">
<number>3</number>
</property>
<item row="0" column="0" rowspan="3">
<widget class="AvatarWidget" name="avatarWidget" native="true">
@ -172,8 +178,8 @@
<widget class="QLabel" name="messageLabel">
<property name="maximumSize">
<size>
<width>220</width>
<height>47</height>
<width>218</width>
<height>45</height>
</size>
</property>
<property name="wordWrap">

View File

@ -10,17 +10,23 @@
<height>100</height>
</rect>
</property>
<layout class="QGridLayout">
<property name="margin">
<number>0</number>
</property>
<property name="maximumSize">
<size>
<width>300</width>
<height>100</height>
</size>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="spacing">
<number>0</number>
</property>
<item row="0" column="0">
<property name="margin">
<number>0</number>
</property>
<item>
<widget class="QFrame" name="windowFrame">
<property name="frameShape">
<enum>QFrame::NoFrame</enum>
<enum>QFrame::WinPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
@ -86,12 +92,6 @@
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>22</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="0">
@ -209,19 +209,6 @@
</item>
</layout>
</item>
<item row="2" column="0">
<spacer>
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>100</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>