logplus/Workflow/WFEngine/ObjectModel/ObjectModelBase/src/PaiHtmlProperties.cpp
2026-01-16 17:18:41 +08:00

261 lines
8.4 KiB
C++

/**
* @file PaiHtmlProperties.cpp
*/
#include <QDateTime>
#include "PaiHtmlProperties.h"
#include "PAIConst.h"
#include "Utils.h"
using namespace pai::ios::property;
using namespace pai::objectmodel;
PaiHtmlProperties::PaiHtmlProperties(const QString & icon,
const QString & mainTitle,
const QString & subTitle,
const int tableWithPercent)
{
Init();
m_TableWidth = tableWithPercent;
m_html = QString("\
<html><body> \
<table> \
<tr> \
<td rowspan=2 width='36px;'><img src=%1 width=36 height=36></td> \
<td colspan=1 style=font-size:14px;font-weight:bold;>&nbsp;%2</td> \
</tr> \
<tr> \
<td colspan=1 style=font-size:11px;color:#797979;>&nbsp;%3</td> \
</tr> \
</table> \
<table width=%4%></table> \
</body></html>")
.arg(icon)
.arg(ReplaceToNBSP(mainTitle))
.arg(ReplaceToNBSP(subTitle))
.arg(m_TableWidth);
m_PropertyUnitMap.insert(NOUNIT, "");
m_PropertyUnitMap.insert(Byte, " Byte");
m_PropertyUnitMap.insert(KB, " KB");
m_PropertyUnitMap.insert(MB, " MB");
m_PropertyUnitMap.insert(GB, " GB");
m_PropertyUnitMap.insert(TB, " TB");
m_PropertyUnitMap.insert(PB, " PB");
m_PropertyUnitMap.insert(g, " g");
m_PropertyUnitMap.insert(kg, " kg");
m_PropertyUnitMap.insert(MM, " mm");
m_PropertyUnitMap.insert(CM, " cm");
m_PropertyUnitMap.insert(M, " m");
m_PropertyUnitMap.insert(KM, " km");
m_PropertyUnitMap.insert(SQUAREMETER, " m<span style=vertical-align:super;>2</span>");
m_PropertyUnitMap.insert(CUBICMETER, " m<span style=vertical-align:super;>3</span>");
m_PropertyUnitMap.insert(SQUAREKILOMETERS, " km<span style=vertical-align:super;>2</span>");
}
PaiHtmlProperties::PaiHtmlProperties(const int tableWithPercent)
{
Init();
m_TableWidth = tableWithPercent;
m_html = QString("\
<html><body> \
<table width=%1%></table> \
</body></html>")
.arg(m_TableWidth);
}
PaiHtmlProperties::PaiHtmlProperties(const QString & html)
{
m_html = html;
}
PaiHtmlProperties::~PaiHtmlProperties()
{
}
void PaiHtmlProperties::AddItem(const QString & name, const QString & content, bool isHtml)
{
QString iName = isHtml ? name : ReplaceToNBSP(name);
QString iContent = isHtml ? content : ReplaceToNBSP(content);
QString contentColor = "#333333";
if((iContent == "Failed") || (iContent == ("Terminated")))
{
contentColor = "#FF0000";
}
QStringList typeContenList = iContent.split("\n");
if(typeContenList.empty())
{
typeContenList.append("");
}
for(int i = 0; i < typeContenList.size(); ++i)
{
QString typeName = "";
if(i == 0)
{
typeName = iName;
}
InsertItem(QString("\
<tr> \
<td align=right colspan=1 style=width:%1px;font-size:%2px;color:%3;background:%4;padding-right:5px;height:%5px;margin:1px;min-width:185px >%6</td> \
<td colspan=1 style=font-size:%7px;font-weight:%8;color:%9;width=75%;padding-left:5px;height:%10px;margin:1px 0px;>%11</td> \
</tr>")
.arg(m_NameWidth)
.arg(m_FontSize)
.arg(m_NameColor.name())
.arg(m_NameBackgroundColor.name())
.arg(m_LineHeight)
.arg(typeName)
.arg(m_FontSize)
.arg(m_FontWeight)
.arg(contentColor)
.arg(m_LineHeight)
.arg(typeContenList.at(i)));
}
}
void PaiHtmlProperties::AddItem(const QString & content, bool isHtml)
{
QString iContent = isHtml ? content : ReplaceToNBSP(content);
InsertItem(QString("\
<tr> \
<td colspan=2 style=font-size:12px;color:#797979; align=left>%1</td> \
</tr>")
.arg(iContent));
}
void PaiHtmlProperties::AddSeparator(bool dashed, bool newTable)
{
if(dashed)
{
InsertItem(QString("\
<tr> \
<td colspan=2><hr style='border:1px dashed #C9D5DC;position:relative;top:1px;font-size:1px' width='100%'></td> \
</tr>"));
}
else
{
InsertItem(QString("\
<tr> \
<td colspan=2><hr width=95% size=1px></td> \
</tr>"));
}
if(newTable)
{
m_html.insert(m_html.lastIndexOf("</body>"), QString("<table width=%1% ></table>").arg(m_TableWidth));
}
}
QString PaiHtmlProperties::GetHtml() const
{
return m_html;
}
QString PaiHtmlProperties::GetHtml(const std::vector< std::vector< PropertyItem > > & propertyGroup)
{
unsigned int count = 0;
foreach(std::vector<PropertyItem> items, propertyGroup)
{
foreach(PropertyItem item, items)
{
if(item.GetPropertyName() == "Type")
{
continue;
}
QString name = QString(item.GetPropertyName().c_str()) + ":";
QString valuve;
switch(item.GetType())
{
case pai::ios::property::DATE:
{
valuve = QDateTime::fromMSecsSinceEpoch(
pai::utils::CUtils::StrToLL(item.GetPropertyValue()) / 1000).toString(LONG_TIME_FORMAT);
break;
}
case pai::ios::property::STRING:
case pai::ios::property::FLOAT:
case pai::ios::property::NUMBER:
default:
{
valuve = QString(item.GetPropertyValue().c_str());
}
}
valuve = valuve + m_PropertyUnitMap.value(item.GetUnit());
if((item.GetUnit() >= SQUAREMETER) && (item.GetUnit() <= SQUAREKILOMETERS))
{
AddItem(name, valuve, true);
}
else
{
AddItem(name, valuve);
}
}
count++;
if(count != propertyGroup.size())
{
AddSeparator();
}
}
return m_html;
}
QString PaiHtmlProperties::ReplaceToNBSP(const QString & replace)
{
QString newReplace(replace);
return newReplace.replace(' ', "&nbsp;");
}
void PaiHtmlProperties::InsertItem(const QString & item)
{
m_html.insert(m_html.lastIndexOf("</table>"), item);
}
void PaiHtmlProperties::SetNameBackgroundColor(const QColor & color)
{
m_NameBackgroundColor = color;
}
void PaiHtmlProperties::SetFontSize(const int fontSize)
{
m_FontSize = fontSize;
}
void PaiHtmlProperties::SetNameColor(const QColor & nameColor)
{
m_NameColor = nameColor;
}
void PaiHtmlProperties::SetNameWidth(int nameWidth)
{
m_NameWidth = nameWidth;
}
void PaiHtmlProperties::SetFontWeight(int fontWeight)
{
m_FontWeight = fontWeight;
}
void PaiHtmlProperties::SetLineHeight(int lineHeight)
{
m_LineHeight = lineHeight;
}
void PaiHtmlProperties::Init()
{
m_NameBackgroundColor = QColor("#FFFFFF");
m_FontSize = 12;
m_NameColor = QColor("#797979");
m_NameWidth = 0;
m_FontWeight = 0;
m_LineHeight = 0;
}