1 /* ColorCode, a free MasterMind clone with built in solver
2  * Copyright (C) 2009  Dirk Laebisch
3  * http://www.laebisch.com/
4  *
5  * ColorCode 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 3 of the License, or
8  * (at your option) any later version.
9  *
10  * ColorCode is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with ColorCode. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #include <QtWidgets>
20 #include "msg.h"
21 
22 using namespace std;
23 
Msg()24 Msg::Msg()
25 {
26     mFont = QFont("Arial", 12, QFont::Bold, false);
27     mFont.setStyleHint(QFont::SansSerif);
28     mFont.setPixelSize(16);
29     mLay = new QTextLayout();
30     mLay->setFont(mFont);
31     mLay->setTextOption(QTextOption(Qt::AlignHCenter));
32     mUpdateRect = QRectF(0, 0, 10, 10);
33 }
34 
~Msg()35 Msg::~Msg()
36 {
37 }
38 
ShowMsg(const QString str)39 void Msg::ShowMsg(const QString str)
40 {
41     mUpdateRect = boundingRect();
42 
43     mLay->setText(str);
44     int leading = -3;
45     qreal h = 0;
46     qreal maxw = 0;
47     qreal maxh = 0;
48     mLay->beginLayout();
49 
50     while (1)
51     {
52         QTextLine line = mLay->createLine();
53         if (!line.isValid())
54         {
55             break;
56         }
57 
58         line.setLineWidth(280);
59         h += leading;
60         line.setPosition(QPointF(0, h));
61         h += line.height();
62         maxw = qMax(maxw, line.naturalTextWidth());
63     }
64     mLay->endLayout();
65 
66     float ypos = 4 + (70 - mLay->boundingRect().height()) / 2;
67 
68     maxw = qMax(mUpdateRect.width(), mLay->boundingRect().width());
69     maxh = qMax(mUpdateRect.height(), mLay->boundingRect().height() + ypos);
70 
71     mUpdateRect = QRectF(0, 0, maxw, maxh + ypos);
72 
73     update(boundingRect());
74 }
75 
boundingRect() const76 QRectF Msg::boundingRect() const
77 {
78     return mUpdateRect;
79 }
80 
paint(QPainter * painter,const QStyleOptionGraphicsItem *,QWidget *)81 void Msg::paint(QPainter* painter, const QStyleOptionGraphicsItem* , QWidget* )
82 {
83     painter->setRenderHint(QPainter::TextAntialiasing, true);
84     painter->setPen(QPen(QColor("#303133")));
85     float ypos = 4 + (70 - mLay->boundingRect().height()) / 2;
86     mLay->draw(painter, QPointF(0, ypos));
87 }
88