File indexing completed on 2024-10-06 06:48:48
0001 /*************************************************************************** 0002 KsirkChatItem.cpp - description 0003 ------------------- 0004 begin : Mon Sep 26 2006 0005 copyright : (C) 2006-2007 by Gaƫl de Chalendar (aka Kleag) 0006 email : kleag@free.fr 0007 ***************************************************************************/ 0008 0009 /*************************************************************************** 0010 * * 0011 * This program is free software; you can redistribute it and/or modify * 0012 * it under the terms of the GNU General Public License as published by * 0013 * the Free Software Foundation; either either version 2 0014 of the License, or (at your option) any later version.of the License, or * 0015 * (at your option) any later version. * 0016 * * 0017 * You should have received a copy of the GNU General Public License 0018 * along with this program; if not, write to the Free Software 0019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 0020 * 02110-1301, USA 0021 ***************************************************************************/ 0022 0023 #include "KsirkChatItem.h" 0024 #include "player.h" 0025 0026 #include <QLayout> 0027 #include <QApplication> 0028 #include <QPainter> 0029 #include <QColorDialog> 0030 #include <QInputDialog> 0031 #include <QMessageBox> 0032 #include <QTextDocument> 0033 #include <iostream> 0034 0035 KsirkChatItem::KsirkChatItem () : KChatBaseMessage() 0036 { 0037 } 0038 0039 KsirkChatItem::KsirkChatItem(const QString& sender, const QString& message): KChatBaseMessage(sender,message) 0040 { 0041 } 0042 0043 KsirkChatItem::KsirkChatItem(const KsirkChatItem& item) : KChatBaseMessage(item), 0044 m_pixmaps(item.m_pixmaps), 0045 m_strings(item.m_strings), 0046 m_order(item.m_order) 0047 0048 { 0049 } 0050 0051 void KsirkChatItem::paint(QPainter* p, 0052 const QStyleOptionViewItem &option, int row) 0053 { 0054 // qCCritical(KSIRK_LOG) << "KsirkChatItem::paint"; 0055 Q_UNUSED(row); 0056 0057 QTextDocument fake; // used to allow to compute lines height 0058 fake.setHtml("gpl"); 0059 fake.setDefaultFont(option.font); 0060 fake.adjustSize(); 0061 fake.setTextWidth ( -1 ); 0062 0063 qreal h = fake.size().height(); 0064 unsigned int x = 0; 0065 for (int i = 0 ; i < m_order.size(); i++) 0066 { 0067 QTextDocument rt; 0068 rt.setHtml(m_strings[i]); 0069 rt.setDefaultFont(option.font); 0070 rt.adjustSize(); 0071 rt.setTextWidth ( -1 ); 0072 QPixmap px(rt.size().toSize()); 0073 px.fill(); 0074 QPainter pa(&px); 0075 rt.drawContents(&pa); 0076 switch (m_order[i]) 0077 { 0078 case Text: 0079 // qCDebug(KSIRK_LOG) << " paint string '" << m_strings[i] << "' at " << x << ", " << row*h ; 0080 p->drawPixmap(option.rect.x()+x,option.rect.y(),px); 0081 x += px.width(); 0082 break; 0083 case Pixmap: 0084 if (! m_pixmaps[i].isNull()) 0085 { 0086 // qCDebug(KSIRK_LOG) << " paint pixmap at " << x << ", " << row*h ; 0087 QPixmap scaled = m_pixmaps[i].scaledToHeight((int)h); 0088 p->drawPixmap(option.rect.x()+x,option.rect.y(),scaled); 0089 x+= scaled.width(); 0090 } 0091 break; 0092 default: ; 0093 } 0094 } 0095 } 0096 0097 QSize KsirkChatItem::sizeHint(const QStyleOptionViewItem &option) 0098 { 0099 unsigned int w = 0; 0100 QTextDocument fake; // used to allow to compute lines height 0101 fake.setHtml("gpl"); 0102 fake.setDefaultFont(option.font); 0103 fake.adjustSize(); 0104 fake.setTextWidth ( -1 ); 0105 0106 qreal h = fake.size().height(); 0107 for (int i = 0 ; i < m_order.size(); i++) 0108 { 0109 QTextDocument rt; 0110 rt.setHtml(m_strings[i]); 0111 rt.adjustSize(); 0112 rt.setTextWidth ( -1 ); 0113 QPixmap px(rt.size().toSize()); 0114 switch (m_order[i]) 0115 { 0116 case Text: 0117 w += px.width(); 0118 break; 0119 case Pixmap: 0120 if (! m_pixmaps[i].isNull()) 0121 { 0122 QPixmap scaled = m_pixmaps[i].scaledToHeight((int)h); 0123 w+= scaled.width(); 0124 } 0125 break; 0126 default: ; 0127 } 0128 } 0129 // qCDebug(KSIRK_LOG) << "KsirkChatItem::sizeHint: " << QSize(w,h); 0130 return QSize(w,(int)h); 0131 } 0132 0133 KsirkChatItem& KsirkChatItem::operator<<(const QString& text) 0134 { 0135 m_strings.push_back(text); 0136 m_pixmaps.push_back(QPixmap ()); 0137 m_order.push_back(Text); 0138 return *this; 0139 } 0140 0141 KsirkChatItem& KsirkChatItem::operator<<(const QPixmap& pixmap) 0142 { 0143 m_strings.push_back(""); 0144 m_pixmaps.push_back(pixmap); 0145 m_order.push_back(Pixmap); 0146 return *this; 0147 }