Allow building as monolithic app (no plugins) for easier developement.
[tagua/yd.git] / src / constrainedtext.cpp
blob7210d3822578ec84dc8195dfb245bc4390321fb8
1 /*
2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@gmail.com>
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.
9 */
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)
19 , m_text(text)
20 , m_color(color)
21 , m_font(font)
22 , m_constr(rect) {
23 calcBoundingRect();
26 ConstrainedText::ConstrainedText(KGameCanvasAbstract* Constrained)
27 : KGameCanvasItem(Constrained)
28 //, m_text("")
29 , m_color(Qt::black)
30 , m_font(QApplication::font()) {
34 ConstrainedText::~ConstrainedText() {
38 void ConstrainedText::calcBoundingRect() {
39 QString test;
40 for(int i=0;i<m_text.length();i++)
41 test += 'H';
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) {
48 if(m_constr == rect)
49 return;
51 m_constr = rect;
52 if(visible() && canvas() )
53 changed();
56 void ConstrainedText::setText(const QString& text) {
57 if(m_text == text)
58 return;
59 m_text = text;
60 calcBoundingRect();
62 if(visible() && canvas() )
63 changed();
66 void ConstrainedText::setColor(const QColor& color) {
67 m_color = color;
70 void ConstrainedText::setFont(const QFont& font) {
71 m_font = font;
72 calcBoundingRect();
74 if(visible() && canvas() )
75 changed();
78 void ConstrainedText::paint(QPainter* p) {
79 if(m_bounding_rect_max.width() == 0 || m_bounding_rect_max.height() == 0)
80 return;
82 p->setPen(m_color);
83 p->setFont(m_font);
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());
90 p->scale(fact, fact);
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);
94 p->setMatrix(savem);
97 QRect ConstrainedText::rect() const {
98 return m_constr; //suboptimal. oh, well...