1
0
Fork 0
forked from len0rd/rockbox

Theme Editor: Implemented text alignment

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@27191 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Robert Bieber 2010-06-30 07:00:05 +00:00
parent ce4da595e1
commit d2ed594246
3 changed files with 114 additions and 6 deletions

View file

@ -33,7 +33,7 @@ RBViewport::RBViewport(skin_element* node, const RBRenderInfo& info)
: QGraphicsItem(info.screen()), font(info.screen()->getFont(0)), : QGraphicsItem(info.screen()), font(info.screen()->getFont(0)),
foreground(info.screen()->foreground()), foreground(info.screen()->foreground()),
background(info.screen()->background()), textOffset(0,0), background(info.screen()->background()), textOffset(0,0),
screen(info.screen()) screen(info.screen()), textAlign(Left)
{ {
if(!node->tag) if(!node->tag)
{ {
@ -108,6 +108,8 @@ RBViewport::RBViewport(skin_element* node, const RBRenderInfo& info)
size = QRectF(0, 0, w, h); size = QRectF(0, 0, w, h);
debug = info.device()->data("showviewports").toBool(); debug = info.device()->data("showviewports").toBool();
} }
lineHeight = font->lineHeight();
} }
RBViewport::~RBViewport() RBViewport::~RBViewport()
@ -142,17 +144,85 @@ void RBViewport::paint(QPainter *painter,
void RBViewport::newLine() void RBViewport::newLine()
{ {
if(textOffset.x() > 0) if(leftText.count() != 0
|| centerText.count() != 0
|| rightText.count() != 0)
{ {
textOffset.setY(textOffset.y() + lineHeight); textOffset.setY(textOffset.y() + lineHeight);
textOffset.setX(0); textOffset.setX(0);
textAlign = Left;
leftText.clear();
rightText.clear();
centerText.clear();
} }
} }
void RBViewport::write(QString text) void RBViewport::write(QString text)
{ {
QGraphicsItem* graphic = font->renderText(text, foreground, this); if(textAlign == Left)
graphic->setPos(textOffset.x(), textOffset.y()); {
textOffset.setX(textOffset.x() + graphic->boundingRect().width()); leftText.append(font->renderText(text, foreground, this));
lineHeight = font->lineHeight(); alignLeft();
}
else if(textAlign == Center)
{
centerText.append(font->renderText(text, foreground, this));
alignCenter();
}
else if(textAlign == Right)
{
rightText.append(font->renderText(text, foreground, this));
alignRight();
}
} }
void RBViewport::alignLeft()
{
int y = textOffset.y();
int x = 0;
for(int i = 0; i < leftText.count(); i++)
{
leftText[i]->setPos(x, y);
x += leftText[i]->boundingRect().width();
}
}
void RBViewport::alignCenter()
{
int y = textOffset.y();
int x = 0;
int width = 0;
for(int i = 0; i < centerText.count(); i++)
width += centerText[i]->boundingRect().width();
x = (size.width() - width) / 2;
for(int i = 0; i < centerText.count(); i++)
{
centerText[i]->setPos(x, y);
x += centerText[i]->boundingRect().width();
}
}
void RBViewport::alignRight()
{
int y = textOffset.y();
int x = 0;
int width = 0;
for(int i = 0; i < rightText.count(); i++)
width += rightText[i]->boundingRect().width();
x = size.width() - width;
for(int i = 0; i < rightText.count(); i++)
{
rightText[i]->setPos(x, y);
x += rightText[i]->boundingRect().width();
}
}

View file

@ -33,6 +33,13 @@ class RBRenderInfo;
class RBViewport : public QGraphicsItem class RBViewport : public QGraphicsItem
{ {
public: public:
enum Alignment
{
Left,
Center,
Right
};
RBViewport(skin_element* node, const RBRenderInfo& info); RBViewport(skin_element* node, const RBRenderInfo& info);
virtual ~RBViewport(); virtual ~RBViewport();
@ -48,9 +55,14 @@ public:
void newLine(); void newLine();
void write(QString text); void write(QString text);
void alignText(Alignment align){ textAlign = align; }
private: private:
void alignLeft();
void alignCenter();
void alignRight();
QRectF size; QRectF size;
RBFont* font; RBFont* font;
QColor foreground; QColor foreground;
@ -62,6 +74,11 @@ private:
int lineHeight; int lineHeight;
RBScreen* screen; RBScreen* screen;
QList<QGraphicsItem*> leftText;
QList<QGraphicsItem*> centerText;
QList<QGraphicsItem*> rightText;
Alignment textAlign;
}; };
#endif // RBVIEWPORT_H #endif // RBVIEWPORT_H

View file

@ -590,6 +590,27 @@ bool ParseTreeNode::execTag(const RBRenderInfo& info, RBViewport* viewport)
switch(element->tag->name[0]) switch(element->tag->name[0])
{ {
case 'a':
switch(element->tag->name[1])
{
case 'c':
/* %ac */
viewport->alignText(RBViewport::Center);
return true;
case 'l':
/* %al */
viewport->alignText(RBViewport::Left);
return true;
case 'r':
/* %ar */
viewport->alignText(RBViewport::Right);
return true;
}
break;
case 'x': case 'x':
switch(element->tag->name[1]) switch(element->tag->name[1])
{ {