aboutsummaryrefslogtreecommitdiff
path: root/examples/GuiClient/chatDialog.cpp
blob: b37d51b27b6aafd255f0e8d881373c26ee5b9174 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "chatDialog.h"
#include "ui_chatDialog.h"

#include "chatGraphicsView.h"
#include "chatGraphicsScene.h"
#include "QXmppClient.h"
#include <QPainter>
#include <QPushButton>

chatDialog::chatDialog(QWidget *parent): QDialog(parent, Qt::Window),
    ui(new Ui::chatDialogClass), m_scene(0), m_view(0), m_client(0), m_pushButtonSend(0)
{
    ui->setupUi(this);
    m_view = new chatGraphicsView(this);
    m_scene = new chatGraphicsScene(this);
    m_view->setChatGraphicsScene(m_scene);
    m_pushButtonSend = new QPushButton("Send", this);
//    m_pushButtonSend->setFixedHeight();
//    m_pushButtonSend->setFixedWidth();
    QRect rect = ui->lineEdit->geometry();
    rect.setLeft(rect.right());
    rect.setWidth(60);
    m_pushButtonSend->setGeometry(rect);
    ui->lineEdit->setFocus();
    ui->verticalLayout->insertWidget(0, m_view);
    bool check = connect(m_pushButtonSend, SIGNAL(clicked(bool)), SLOT(sendMessage()));
    Q_ASSERT(check);
    updateSendButtonGeomerty();
}

void chatDialog::show()
{
    QDialog::show();
}

QString chatDialog::getBareJid() const
{
    return m_bareJid;
}

QString chatDialog::getDisplayName() const
{
    return m_displayName;
}

void chatDialog::setBareJid(const QString& str)
{
    m_bareJid = str;
}

void chatDialog::setDisplayName(const QString& str)
{
    m_displayName = str;
    setWindowTitle(QString("Chat with %1").arg(m_displayName));

    QFont font;
    font.setBold(true);
    QFontMetrics fontMetrics(font);
    QRect rect = fontMetrics.boundingRect(m_displayName);
    int width = rect.width();

    if(m_scene)
        m_scene->setBoxStartLength(width);
//    ui->horizontalSpacer_2->changeSize(width+20, 10);
    ui->lineEdit->setFixedWidth(350 - width - 25);
    updateSendButtonGeomerty();
}

void chatDialog::setQXmppClient(QXmppClient* client)
{
    m_client = client;
}

void chatDialog::sendMessage()
{
    if(m_client)
        m_client->sendMessage(getBareJid(), ui->lineEdit->text());

    m_view->addMessage("Me", ui->lineEdit->text());
    ui->lineEdit->clear();
}

void chatDialog::messageReceived(const QString& msg)
{
    m_view->addMessage(getDisplayName(), msg);
}

void chatDialog::keyPressEvent(QKeyEvent* event1)
{
    ui->lineEdit->setFocus();
    ui->lineEdit->event(event1);

    if(event1->key() == Qt::Key_Return)
    {
        m_pushButtonSend->click();
    }
    else if(event1->key() == Qt::Key_Escape)
    {
        hide();
    }
}

void chatDialog::paintEvent(QPaintEvent* event)
{
    QDialog::paintEvent(event);
    QPainter p(this);
    p.setPen(Qt::gray);
    p.drawRect(rect().adjusted(5, 5, -6, -6));
}

void chatDialog::resizeEvent(QResizeEvent *)
{
    updateSendButtonGeomerty();
}

void chatDialog::moveEvent(QMoveEvent *)
{
    updateSendButtonGeomerty();
}

void chatDialog::updateSendButtonGeomerty()
{
    QRect rect = ui->lineEdit->geometry();
    rect.setLeft(rect.right() + 6);
    rect.setWidth(60);
    QRect rect2 = rect;
    rect2.setHeight(25);
    rect2.moveCenter(rect.center());
    m_pushButtonSend->setGeometry(rect2);
}