-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatbox.cpp
138 lines (123 loc) · 4.64 KB
/
chatbox.cpp
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
131
132
133
134
135
136
137
138
#include "chatbox.h"
class Widget;
/**
* @brief Construct a new Mainchatbox:: Mainchatbox object
*
* Also notifies it's success by a message to stderr.
*
*/
Mainchatbox::Mainchatbox()
{
qInfo() << "mainchatbox object constructed";
}
/**
* @brief Destroy the Mainchatbox:: Mainchatbox object
*
* Also notifies it's success by a message to stderr.
*
*/
Mainchatbox::~Mainchatbox()
{
qInfo() << "mainchatbox object destructed";
}
/**
* @brief This method once called by main widget right after construction take initilization job.
*
* It creats a line edit widget and sets up it's layout for UI as well as it's class hierarchy.
*
*/
void Mainchatbox::init()
{
inputTextBox = new QLineEdit;
QObject::connect(inputTextBox,
SIGNAL(returnPressed()),
this,
SIGNAL(RetunKeyPressed()));
parentLayout = new QHBoxLayout(this);
parentLayout->addWidget(inputTextBox);
parentLayout->setSpacing(0);
inputTextBox->setFixedHeight(50);
inputTextBox->setParent(this);
this->setStyleSheet("QLineEdit { background-color: yellow }");
inputTextBox->setStyleSheet("QLineEdit { background-color: white }");
qInfo() << "text box geometry set!";
this->inputTextBox->setParent(this);
this->inputTextBox->setPlaceholderText("Click to write");
}
/**
* @brief This is a key method and slot which has responsiblity of working with several other
* slots and signals to privide them with string value of user input when it is needed.
*
*
* This class in order to have ChatLabel class instances get created or modified which actually are are todo tasks,
* It manages to work as explained below; \n
* At a fresh start it responds to addButton::addButtonClicked,
* signal from addButton class and provides text and key to by emitting Mainchatbox::textready signal to
* main widget's Widget::controller slot which is responsible in creating chat labels. as well,
* Mainchatbox::editRequestHandler slot also once triggeered by Widget::labelObjectEditRequest signal uses
* this method to get text value and unique key for each. And once edit is done this slot
* emits Mainchatbox::editready signal which triggers serialize::edit method of class \ref serialize, letting the changes
* to be saved in to a binary file
*
*/
void Mainchatbox::getText()
{
if(!editMode){
if(QFileInfo::exists(m_fileName) && notyetchecked) //cheks wheter any record exist.
{
m_serializedFile = new QFile(m_fileName);
m_serializedFile->open(QIODevice::ReadOnly);
m_streamIn.setVersion(QDataStream::Qt_4_0);
m_streamIn.setDevice(m_serializedFile);
QDataStream &operator<< (QDataStream & m_streamIn, const QJsonObject & m_jsonobject);
QDataStream &operator>> (QDataStream & m_streamIn, QJsonObject & m_jsonobject);
m_streamIn >> m_jsonobject;
m_serializedFile->close(); //flush every thing to file
foreach(const QString& olderkeys, m_jsonobject.keys())
{
m_keyCounter++;
}
notyetchecked = false;
}
input = new QByteArray;
m_buffer.setBuffer(input);
qInfo() << "mainchat box slot connected";
m_keyCounter++;
m_buffer.open(QIODevice::WriteOnly);
m_buffer.write(this->inputTextBox->text().toUtf8()); //reads user input from ui
m_buffer.close();
inputTextBox->clear();
emit textready(input,m_keyCounter);
}
else
{
input = new QByteArray;
m_buffer.setBuffer(input);
qInfo() << "mainchat box slot connected";
m_buffer.open(QIODevice::WriteOnly);
m_buffer.write(this->inputTextBox->text().toUtf8()); //reads user input from ui
m_buffer.close();
inputTextBox->clear();
underEdit->edit(input);
emit editready(underEdit,underEditKey);
underEdit->setDisabled(false);
}
}
/**
* @brief This method takes a small role in editing procedure.
* Once Edit button clicked on UI it emits Widget::labelObjectEditRequest signal which triggres this slot
* it is responsible in copying value of a chat label to Mainchatbox class for editing.
*
* @param choice A pointer to ChatLabel class object
* @param labelkey It's correspinding key
*/
void Mainchatbox::editRequestHandler(ChatLabel* choice, int labelkey)
{ //copies label text to chatbox for edit
underEdit = choice;
underEditKey = labelkey;
qInfo() << "mainchatbox slot for label edit trigerred";
qInfo() << "value:" << underEdit->getText();
underEdit->setDisabled(true);
this->inputTextBox->setText(underEdit->getText());
editMode = true;
}