1 | /* |
2 | * Copyright (C) 2010 Parker Coates <coates@kde.org> |
3 | * |
4 | * This program is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU General Public License as |
6 | * published by the Free Software Foundation; either version 2 of |
7 | * the License, or (at your option) any later version. |
8 | * |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 | */ |
17 | |
18 | #include "messagebox.h" |
19 | |
20 | #include "renderer.h" |
21 | |
22 | #include <QtGui/QFontMetrics> |
23 | #include <QtGui/QPainter> |
24 | |
25 | |
26 | namespace |
27 | { |
28 | const qreal textToBoxSizeRatio = 0.8; |
29 | const int MessageBoxMinimumFontSize = 5; |
30 | const int maximumFontSize = 36; |
31 | } |
32 | |
33 | |
34 | MessageBox::MessageBox() |
35 | : KGameRenderedItem( Renderer::self(), "message_frame" ), |
36 | m_fontCached( false ) |
37 | { |
38 | |
39 | } |
40 | |
41 | |
42 | void MessageBox::setMessage( const QString & message ) |
43 | { |
44 | if ( m_message != message ) |
45 | { |
46 | m_message = message; |
47 | m_fontCached = false; |
48 | update(); |
49 | } |
50 | } |
51 | |
52 | |
53 | QString MessageBox::message() const |
54 | { |
55 | return m_message; |
56 | } |
57 | |
58 | |
59 | void MessageBox::setSize( const QSize & size ) |
60 | { |
61 | if ( size != renderSize() ) |
62 | { |
63 | setRenderSize( size ); |
64 | m_fontCached = false; |
65 | } |
66 | } |
67 | |
68 | |
69 | QSize MessageBox::size() const |
70 | { |
71 | return renderSize(); |
72 | } |
73 | |
74 | |
75 | void MessageBox::paint( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget ) |
76 | { |
77 | QRect rect = pixmap().rect(); |
78 | |
79 | if ( rect.isEmpty() ) |
80 | return; |
81 | |
82 | KGameRenderedItem::paint( painter, option, widget ); |
83 | |
84 | painter->setFont( m_font ); |
85 | |
86 | if ( !m_fontCached ) |
87 | { |
88 | int availableWidth = rect.width() * textToBoxSizeRatio; |
89 | int availableHeight = rect.height() * textToBoxSizeRatio; |
90 | int size = maximumFontSize; |
91 | |
92 | QFont f = painter->font(); |
93 | f.setPointSize( size ); |
94 | painter->setFont( f ); |
95 | do |
96 | { |
97 | QRect br = painter->boundingRect( rect, Qt::AlignLeft | Qt::AlignTop, m_message ); |
98 | if ( br.width() < availableWidth && br.height() < availableHeight ) |
99 | break; |
100 | |
101 | size = qBound( MessageBoxMinimumFontSize, |
102 | qMin( size * availableWidth / br.width(), |
103 | size * availableHeight / br.height() ), |
104 | size - 1 ); |
105 | |
106 | QFont f = painter->font(); |
107 | f.setPointSize( size ); |
108 | painter->setFont( f ); |
109 | } |
110 | while ( size > MessageBoxMinimumFontSize ); |
111 | |
112 | m_font = painter->font(); |
113 | m_fontCached = true; |
114 | } |
115 | |
116 | painter->setPen( Renderer::self()->colorOfElement( "message_text_color" ) ); |
117 | painter->drawText( rect, Qt::AlignCenter, m_message ); |
118 | } |
119 | |
120 | |
121 | |