2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@sns.it>
3 (c) 2006 Maurizio Monge <maurizio.monge@kdemail.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
11 #include "constrainedtext.h"
12 #include <QApplication>
15 ConstrainedText::ConstrainedText(const QString
& text
, const QColor
& color
,
16 const QFont
& font
, const QRect
& rect
,
17 KGameCanvasAbstract
* Constrained
)
18 : KGameCanvasItem(Constrained
)
26 ConstrainedText::ConstrainedText(KGameCanvasAbstract
* Constrained
)
27 : KGameCanvasItem(Constrained
)
30 , m_font(QApplication::font()) {
34 ConstrainedText::~ConstrainedText() {
38 void ConstrainedText::calcBoundingRect() {
40 for(int i
=0;i
<m_text
.length();i
++)
42 m_bounding_rect_max
= QFontMetrics(m_font
).boundingRect(test
);
44 m_bounding_rect
= QFontMetrics(m_font
).boundingRect(m_text
);
47 void ConstrainedText::setConstrainRect(const QRect
& rect
) {
52 if(visible() && canvas() )
56 void ConstrainedText::setText(const QString
& text
) {
62 if(visible() && canvas() )
66 void ConstrainedText::setColor(const QColor
& color
) {
70 void ConstrainedText::setFont(const QFont
& font
) {
74 if(visible() && canvas() )
78 void ConstrainedText::paint(QPainter
* p
) {
79 if(m_bounding_rect_max
.width() == 0 || m_bounding_rect_max
.height() == 0)
85 double fact
= qMin(double(m_constr
.width())/m_bounding_rect_max
.width(),
86 double(m_constr
.height())/m_bounding_rect_max
.height());
87 QMatrix savem
= p
->matrix();
88 //p->fillRect( m_constr, Qt::blue );
89 p
->translate(QRectF(m_constr
).center());
91 p
->translate(-QRectF(m_bounding_rect_max
).center());
92 //p->fillRect( m_bounding_rect_max, Qt::red );
93 p
->drawText( QPoint((m_bounding_rect_max
.width()-m_bounding_rect
.width())/2,0), m_text
);
97 QRect
ConstrainedText::rect() const {
98 return m_constr
; //suboptimal. oh, well...