Skip to content

Commit

Permalink
Merge pull request #35 from falbru/add-face-attributes
Browse files Browse the repository at this point in the history
Add face attributes
  • Loading branch information
falbru authored May 21, 2024
2 parents 8809937 + e02b4ab commit 98e1f2f
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/rpc/atom.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "atom.hpp"
#include "attribute.hpp"

namespace RPC
{
Expand All @@ -25,12 +26,25 @@ void Atom::draw(const DrawContext &context, const QPoint &position, const Face &
int height = context.cell_size.height();

context.painter.setPen(fg);

QFont font = QFont{context.painter.font()};
font.setItalic(default_face.hasAttribute(Attribute::italic) || m_face.hasAttribute(Attribute::italic));
font.setBold(default_face.hasAttribute(Attribute::bold) || m_face.hasAttribute(Attribute::bold));
context.painter.setFont(font);

context.painter.fillRect(position.x(), position.y(), width, height, bg);

for (int i = 0; i < m_contents.size(); i++)
{
context.painter.drawText(QRect(position.x() + i * context.cell_size.width(), position.y(), width, height),
Qt::AlignTop, m_contents.at(i));
}

if (default_face.hasAttribute(Attribute::underline) || m_face.hasAttribute(Attribute::underline))
{
context.painter.drawLine(position.x(), position.y() + context.cell_size.height() - 1,
position.x() + context.cell_size.width() * m_contents.size(),
position.y() + context.cell_size.height() - 1);
}
}
} // namespace RPC
8 changes: 8 additions & 0 deletions src/rpc/face.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "face.hpp"
#include "attribute.hpp"
#include <qdebug.h>

namespace RPC
{
Expand All @@ -19,4 +21,10 @@ Color Face::getBg() const
{
return m_bg;
}

bool Face::hasAttribute(Attribute attribute) const
{
return m_attributes.contains(attribute);
}

} // namespace RPC
2 changes: 2 additions & 0 deletions src/rpc/face.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class Face
Color getFg() const;
Color getBg() const;

bool hasAttribute(Attribute attribute) const;

private:
Color m_fg;
Color m_bg;
Expand Down
56 changes: 55 additions & 1 deletion src/rpc/rpc.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "rpc.hpp"
#include "attribute.hpp"
#include <qjsondocument.h>

namespace RPC
Expand All @@ -10,7 +11,60 @@ Coord deserializeCoord(QJsonObject coord_serialized)

Face deserializeFace(QJsonObject face_serialized)
{
return Face{face_serialized["fg"].toString(), face_serialized["bg"].toString(), QList<Attribute>()};
return Face{face_serialized["fg"].toString(), face_serialized["bg"].toString(),
deserializeAttributes(face_serialized["attributes"].toArray())};
}

Attribute deserializeAttribute(QString attribute)
{
if (attribute == "underline")
{
return Attribute::underline;
}
if (attribute == "reverse")
{
return Attribute::reverse;
}
if (attribute == "blink")
{
return Attribute::blink;
}
if (attribute == "bold")
{
return Attribute::bold;
}
if (attribute == "dim")
{
return Attribute::dim;
}
if (attribute == "italic")
{
return Attribute::italic;
}
if (attribute == "final_fg")
{
return Attribute::final_fg;
}
if (attribute == "final_bg")
{
return Attribute::final_bg;
}
if (attribute == "final_attr")
{
return Attribute::final_attr;
}
qDebug() << "Unknown attribute: " << attribute;
return Attribute::bold;
}

QList<Attribute> deserializeAttributes(QJsonArray attributes_serialized)
{
QList<Attribute> attributes;
for (const QJsonValue &atom_serialized : attributes_serialized)
{
attributes.append(deserializeAttribute(atom_serialized.toString()));
}
return attributes;
}

Atom deserializeAtom(QJsonObject atom_serialized)
Expand Down
2 changes: 2 additions & 0 deletions src/rpc/rpc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ struct SetUIOptionsRequest

Coord deserializeCoord(QJsonObject coord_serialized);
Face deserializeFace(QJsonObject face_serialized);
Attribute deserializeAttribute(QString attribute);
QList<Attribute> deserializeAttributes(QJsonArray attributes_serialized);
Atom deserializeAtom(QJsonObject atom_serialized);
Line deserializeLine(QJsonArray line_serialized);
Line deserializeLine(QJsonArray line_serialized);
Expand Down

0 comments on commit 98e1f2f

Please sign in to comment.