File indexing completed on 2024-05-12 08:00:35
0001 /* 0002 * Copyright (C) 2010 Parker Coates <coates@kde.org> 0003 * 0004 * This program is free software; you can redistribute it and/or 0005 * modify it under the terms of the GNU General Public License as 0006 * published by the Free Software Foundation; either version 2 of 0007 * the License, or (at your option) any later version. 0008 * 0009 * This program is distributed in the hope that it will be useful, 0010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0012 * GNU General Public License for more details. 0013 * 0014 * You should have received a copy of the GNU General Public License 0015 * along with this program. If not, see <http://www.gnu.org/licenses/>. 0016 */ 0017 0018 #include "messagebox.h" 0019 0020 // own 0021 #include "renderer.h" 0022 // Qt 0023 #include <QPainter> 0024 0025 namespace 0026 { 0027 const qreal textToBoxSizeRatio = 0.8; 0028 const int MessageBoxMinimumFontSize = 5; 0029 const int maximumFontSize = 36; 0030 } 0031 0032 MessageBox::MessageBox() 0033 : KGameRenderedItem(Renderer::self(), QStringLiteral("message_frame")) 0034 , m_fontCached(false) 0035 { 0036 } 0037 0038 void MessageBox::setMessage(const QString &message) 0039 { 0040 if (m_message != message) { 0041 m_message = message; 0042 m_fontCached = false; 0043 update(); 0044 } 0045 } 0046 0047 QString MessageBox::message() const 0048 { 0049 return m_message; 0050 } 0051 0052 void MessageBox::setSize(const QSize &size) 0053 { 0054 if (size != renderSize()) { 0055 setRenderSize(size); 0056 m_fontCached = false; 0057 } 0058 } 0059 0060 QSize MessageBox::size() const 0061 { 0062 return renderSize(); 0063 } 0064 0065 void MessageBox::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) 0066 { 0067 QRect rect = pixmap().rect(); 0068 rect.setWidth(rect.width() / pixmap().devicePixelRatio()); 0069 rect.setHeight(rect.height() / pixmap().devicePixelRatio()); 0070 0071 if (rect.isEmpty()) 0072 return; 0073 0074 KGameRenderedItem::paint(painter, option, widget); 0075 0076 painter->setFont(m_font); 0077 0078 if (!m_fontCached) { 0079 int availableWidth = rect.width() * textToBoxSizeRatio; 0080 int availableHeight = rect.height() * textToBoxSizeRatio; 0081 int size = maximumFontSize; 0082 0083 QFont f = painter->font(); 0084 f.setPointSize(size); 0085 painter->setFont(f); 0086 do { 0087 QRect br = painter->boundingRect(rect, Qt::AlignLeft | Qt::AlignTop, m_message); 0088 if (br.width() < availableWidth && br.height() < availableHeight) 0089 break; 0090 0091 size = qBound(MessageBoxMinimumFontSize, qMin(size * availableWidth / br.width(), size * availableHeight / br.height()), size - 1); 0092 0093 QFont f = painter->font(); 0094 f.setPointSize(size); 0095 painter->setFont(f); 0096 } while (size > MessageBoxMinimumFontSize); 0097 0098 m_font = painter->font(); 0099 m_fontCached = true; 0100 } 0101 0102 painter->setPen(Renderer::self()->colorOfElement(QStringLiteral("message_text_color"))); 0103 painter->drawText(rect, Qt::AlignCenter, m_message); 0104 }