#include "widget.h"
#include <QHBoxLayout>
#include <QTextBrowser>
#include <QToolButton>
#include <QPainter>
#include <QPushBUtton>
#include <QDialog>
#include <QScrollBar>
Widget::Widget(QWidget *parent) :
QWidget(parent)
{
ChatMessageWidget *pChatMessage = new ChatMessageWidget(this);
pChatMessage->insertMessage(ChatMessageWidget::MySelf, "zhangsan:kaihuila");
pChatMessage->insertMessage(ChatMessageWidget::Other, "wangwu:haode");
QHBoxLayout *pMainLayout = new QHBoxLayout;
pMainLayout->addWidget(pChatMessage);
this->setLayout(pMainLayout);
}
Widget::~Widget()
{
}
ChatMessageWidget::ChatMessageWidget(QWidget *parent)
: QWidget(parent)
{
m_pScrollWidget = new QWidget(this);
m_pScrollWidget->setObjectName("ChatMessageWidget_ScrollWidget");
m_pScrollWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum);
m_pScrollArea = new QScrollArea(this);
m_pScrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_pScrollArea->setWidget(m_pScrollWidget);
m_pScrollArea->setWidgetResizable(true);
QHBoxLayout *hBoxLayout = new QHBoxLayout;
hBoxLayout->setMargin(0);
hBoxLayout->addWidget(m_pScrollArea);
this->setLayout(hBoxLayout);
m_pScrollWidgetLayout = new QVBoxLayout;
QVBoxLayout *pMainLayout = new QVBoxLayout;
pMainLayout->setMargin(2);
pMainLayout->addLayout(m_pScrollWidgetLayout);
pMainLayout->addStretch();
m_pScrollWidget->setLayout(pMainLayout);
m_pScrollTimer = new QTimer(this);
m_pScrollTimer->setSingleShot(true);
}
ChatMessageWidget::~ChatMessageWidget()
{
}
void ChatMessageWidget::insertMessage(enMessageType messageType, QString strMessText)
{
switch (messageType)
{
case MySelf:
{
dealMySelfMessage(strMessText);
break;
}
case Other:
{
dealOtherMessage(strMessText);
break;
}
default:
break;
}
m_pScrollTimer->start(10);
connect(m_pScrollTimer, SIGNAL(timeout()), this, SLOT(updateScrollBar()));
//m_pScrollArea->verticalScrollBar()->setValue(m_pScrollArea->verticalScrollBar()->maximum());
}
void ChatMessageWidget::updateScrollBar()
{
m_pScrollArea->verticalScrollBar()->setValue(m_pScrollArea->verticalScrollBar()->maximum());
}
void ChatMessageWidget::dealMySelfMessage(QString &str)
{
QString strName;
QString strContent;
resolveNameAndContent(str, strName, strContent);
CRightBubbleWidget *pMyBubble = new CRightBubbleWidget(this);
pMyBubble->setMessage(strName, strContent);
m_pScrollWidgetLayout->addWidget(pMyBubble);
}
void ChatMessageWidget::dealOtherMessage(QString &str)
{
QString strName;
QString strContent;
resolveNameAndContent(str, strName, strContent);
CLeftBubbleWidget *pMyBubble = new CLeftBubbleWidget(this);
pMyBubble->setMessage(strName, strContent);
m_pScrollWidgetLayout->addWidget(pMyBubble);
}
void ChatMessageWidget::resolveNameAndContent(QString &strOrig, QString &strName, QString &strContent)
{
QStringList list = strOrig.split(":");
strName = list[0];
for (int i = 1; i < list.count(); ++i)
{
if (1 != i)
{
strContent.append(":");
}
strContent.append(list[i]);
}
}
CLeftBubbleWidget::CLeftBubbleWidget(QWidget *parent) : QWidget(parent)
{
init();
}
CLeftBubbleWidget::~CLeftBubbleWidget()
{
}
void CLeftBubbleWidget::setMessage(QString &strName, QString &strMessage)
{
m_pNameLabel->setText(strName);
m_pContentLabel->setText(strMessage);
}
void CLeftBubbleWidget::init()
{
m_pNameLabel = new QLabel(this);
m_pNameLabel->setObjectName("ChatOtherOldNameLabel");
m_pContentLabel = new QLabel(this);
m_pContentLabel->setWordWrap(true);
m_pContentLabel->setObjectName("ChatOtherOldMessageLabel");
//m_pContentLabel->setStyleSheet("font-size: 14px;font-family:Microsoft YaHei;border-width: 10px 7px 5px 10px;border-image: url(:/skin/Resources/otherBubble.png) 10 7 5 10 no-repeat stretch;");
QHBoxLayout *pNameLayout = new QHBoxLayout;
pNameLayout->setMargin(0);
pNameLayout->addWidget(m_pNameLabel);
pNameLayout->addStretch();
QHBoxLayout *pContentLayout = new QHBoxLayout;
pContentLayout->setMargin(0);
pContentLayout->addWidget(m_pContentLabel);
pContentLayout->addStretch();
QVBoxLayout *pMainLayout = new QVBoxLayout;
pMainLayout->setMargin(0);
pMainLayout->setSpacing(0);
pMainLayout->addLayout(pNameLayout);
pMainLayout->addLayout(pContentLayout);
this->setLayout(pMainLayout);
}
CRightBubbleWidget::CRightBubbleWidget(QWidget *parent) : QWidget(parent)
{
init();
}
CRightBubbleWidget::~CRightBubbleWidget()
{
}
void CRightBubbleWidget::setMessage(QString &strName, QString &strMessage)
{
m_pNameLabel->setText(strName);
m_pContentLabel->setText(strMessage);
}
void CRightBubbleWidget::init()
{
m_pNameLabel = new QLabel(this);
m_pNameLabel->setObjectName("ChatMyOldNameLabel");
m_pContentLabel = new QLabel(this);
m_pContentLabel->setObjectName("ChatMyOldMessageLabel");
m_pContentLabel->setWordWrap(true);
m_pContentLabel->setMaximumWidth(400);
//m_pContentLabel->setStyleSheet("font-size: 14px;font-family:Microsoft YaHei;border-width: 10px 10px 5px 7px;border-image: url(:/skin/Resources/myBubble.png) 10 10 5 7 no-repeat stretch;");
QHBoxLayout *pNameLayout = new QHBoxLayout;
pNameLayout->setMargin(0);
pNameLayout->addStretch();
pNameLayout->addWidget(m_pNameLabel);
QHBoxLayout *pContentLayout = new QHBoxLayout;
pContentLayout->setMargin(0);
pContentLayout->addStretch();
pContentLayout->addWidget(m_pContentLabel);
QVBoxLayout *pMainLayout = new QVBoxLayout;
pMainLayout->setMargin(0);
pMainLayout->setSpacing(0);
pMainLayout->addLayout(pNameLayout);
pMainLayout->addLayout(pContentLayout);
this->setLayout(pMainLayout);
}