diff --git a/widget/form/chatform.cpp b/widget/form/chatform.cpp index 3cf6c99e0..9c87bc4c5 100644 --- a/widget/form/chatform.cpp +++ b/widget/form/chatform.cpp @@ -7,6 +7,7 @@ #include #include #include +#include ChatForm::ChatForm(Friend* chatFriend) : f(chatFriend), curRow{0}, lockSliderToBottom{true} @@ -30,6 +31,7 @@ ChatForm::ChatForm(Friend* chatFriend) chatAreaWidget->setLayout(mainChatLayout); chatArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded); chatArea->setWidgetResizable(true); + chatArea->setContextMenuPolicy(Qt::CustomContextMenu); mainChatLayout->setColumnStretch(1,1); mainChatLayout->setSpacing(10); @@ -96,6 +98,7 @@ ChatForm::ChatForm(Friend* chatFriend) connect(callButton, SIGNAL(clicked()), this, SLOT(onCallTriggered())); connect(msgEdit, SIGNAL(enterPressed()), this, SLOT(onSendTriggered())); connect(chatArea->verticalScrollBar(), SIGNAL(rangeChanged(int,int)), this, SLOT(onSliderRangeChanged())); + connect(chatArea, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(onChatContextMenuRequested(QPoint))); } ChatForm::~ChatForm() @@ -169,16 +172,24 @@ void ChatForm::addMessage(QLabel* author, QLabel* message, QLabel* date) { mainChatLayout->setRowStretch(curRow, 0); mainChatLayout->addItem(new QSpacerItem(0,AUTHOR_CHANGE_SPACING),curRow,0,1,3); - curRow++; } - mainChatLayout->addWidget(author, curRow, 0); + previousName = author->text(); + curRow++; } - previousName = author->text(); + else if (curRow)// onSaveLogClicked expects 0 or 3 QLabel per line + author->setText(""); + mainChatLayout->addWidget(author, curRow, 0); mainChatLayout->addWidget(message, curRow, 1); mainChatLayout->addWidget(date, curRow, 3); mainChatLayout->setRowStretch(curRow+1, 1); mainChatLayout->setRowStretch(curRow, 0); curRow++; + author->setContextMenuPolicy(Qt::CustomContextMenu); + message->setContextMenuPolicy(Qt::CustomContextMenu); + date->setContextMenuPolicy(Qt::CustomContextMenu); + connect(author, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(onChatContextMenuRequested(QPoint))); + connect(message, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(onChatContextMenuRequested(QPoint))); + connect(date, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(onChatContextMenuRequested(QPoint))); } void ChatForm::onAttachClicked() @@ -400,3 +411,44 @@ void ChatForm::onCancelCallTriggered() connect(callButton, SIGNAL(clicked()), this, SLOT(onCallTriggered())); emit cancelCall(callId, f->friendId); } + +void ChatForm::onChatContextMenuRequested(QPoint pos) +{ + QWidget* sender = (QWidget*)QObject::sender(); + pos = sender->mapToGlobal(pos); + QMenu menu; + menu.addAction("Save chat log", this, SLOT(onSaveLogClicked())); + menu.exec(pos); +} + +void ChatForm::onSaveLogClicked() +{ + QString path = QFileDialog::getSaveFileName(0,"Save chat log"); + if (path.isEmpty()) + return; + + QFile file(path); + if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) + return; + + QString log; + QList labels = chatAreaWidget->findChildren(); + int i=0; + for (QLabel* label : labels) + { + log += label->text(); + if (i==2) + { + i=0; + log += '\n'; + } + else + { + log += '\t'; + i++; + } + } + + file.write(log.toUtf8()); + file.close(); +} diff --git a/widget/form/chatform.h b/widget/form/chatform.h index 75276dcfc..3c4872adf 100644 --- a/widget/form/chatform.h +++ b/widget/form/chatform.h @@ -9,6 +9,7 @@ #include #include #include +#include #include "widget/tool/chattextedit.h" #include "ui_widget.h" @@ -61,6 +62,8 @@ private slots: void onAnswerCallTriggered(); void onHangupCallTriggered(); void onCancelCallTriggered(); + void onChatContextMenuRequested(QPoint pos); + void onSaveLogClicked(); private: Friend* f; diff --git a/widget/form/groupchatform.cpp b/widget/form/groupchatform.cpp index ea3496f2c..190cb7ca8 100644 --- a/widget/form/groupchatform.cpp +++ b/widget/form/groupchatform.cpp @@ -7,6 +7,9 @@ #include #include #include +#include +#include +#include GroupChatForm::GroupChatForm(Group* chatGroup) : group(chatGroup), curRow{0}, lockSliderToBottom{true} @@ -38,6 +41,7 @@ GroupChatForm::GroupChatForm(Group* chatGroup) chatAreaWidget->setLayout(mainChatLayout); chatArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded); chatArea->setWidgetResizable(true); + chatArea->setContextMenuPolicy(Qt::CustomContextMenu); mainChatLayout->setColumnStretch(1,1); mainChatLayout->setHorizontalSpacing(10); @@ -78,6 +82,7 @@ GroupChatForm::GroupChatForm(Group* chatGroup) connect(sendButton, SIGNAL(clicked()), this, SLOT(onSendTriggered())); connect(msgEdit, SIGNAL(enterPressed()), this, SLOT(onSendTriggered())); connect(chatArea->verticalScrollBar(), SIGNAL(rangeChanged(int,int)), this, SLOT(onSliderRangeChanged())); + connect(chatArea, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(onChatContextMenuRequested(QPoint))); } GroupChatForm::~GroupChatForm() @@ -149,16 +154,24 @@ void GroupChatForm::addMessage(QLabel* author, QLabel* message, QLabel* date) { mainChatLayout->setRowStretch(curRow, 0); mainChatLayout->addItem(new QSpacerItem(0,AUTHOR_CHANGE_SPACING),curRow,0,1,3); - curRow++; } - mainChatLayout->addWidget(author, curRow, 0); + previousName = author->text(); + curRow++; } - previousName = author->text(); + else if (curRow)// onSaveLogClicked expects 0 or 3 QLabel per line + author->setText(""); + mainChatLayout->addWidget(author, curRow, 0); mainChatLayout->addWidget(message, curRow, 1); mainChatLayout->addWidget(date, curRow, 3); mainChatLayout->setRowStretch(curRow+1, 1); mainChatLayout->setRowStretch(curRow, 0); curRow++; + author->setContextMenuPolicy(Qt::CustomContextMenu); + message->setContextMenuPolicy(Qt::CustomContextMenu); + date->setContextMenuPolicy(Qt::CustomContextMenu); + connect(author, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(onChatContextMenuRequested(QPoint))); + connect(message, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(onChatContextMenuRequested(QPoint))); + connect(date, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(onChatContextMenuRequested(QPoint))); } void GroupChatForm::onSliderRangeChanged() @@ -177,3 +190,44 @@ void GroupChatForm::onUserListChanged() names.chop(2); namesList->setText(names); } + +void GroupChatForm::onChatContextMenuRequested(QPoint pos) +{ + QWidget* sender = (QWidget*)QObject::sender(); + pos = sender->mapToGlobal(pos); + QMenu menu; + menu.addAction("Save chat log", this, SLOT(onSaveLogClicked())); + menu.exec(pos); +} + +void GroupChatForm::onSaveLogClicked() +{ + QString path = QFileDialog::getSaveFileName(0,"Save chat log"); + if (path.isEmpty()) + return; + + QFile file(path); + if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) + return; + + QString log; + QList labels = chatAreaWidget->findChildren(); + int i=0; + for (QLabel* label : labels) + { + log += label->text(); + if (i==2) + { + i=0; + log += '\n'; + } + else + { + log += '\t'; + i++; + } + } + + file.write(log.toUtf8()); + file.close(); +} diff --git a/widget/form/groupchatform.h b/widget/form/groupchatform.h index b309ddf10..d04c086b2 100644 --- a/widget/form/groupchatform.h +++ b/widget/form/groupchatform.h @@ -37,6 +37,8 @@ signals: private slots: void onSendTriggered(); void onSliderRangeChanged(); + void onChatContextMenuRequested(QPoint pos); + void onSaveLogClicked(); private: Group* group; diff --git a/widget/widget.cpp b/widget/widget.cpp index 9563ee7f4..26db50087 100644 --- a/widget/widget.cpp +++ b/widget/widget.cpp @@ -405,7 +405,7 @@ void Widget::removeFriend(int friendId) onAddClicked(); } -void Widget::onGroupInviteReceived(int friendId, uint8_t* publicKey) +void Widget::onGroupInviteReceived(int32_t friendId, uint8_t* publicKey) { int groupId = core->joinGroupchat(friendId, publicKey); if (groupId == -1) @@ -413,7 +413,6 @@ void Widget::onGroupInviteReceived(int friendId, uint8_t* publicKey) qWarning() << "Widget::onGroupInviteReceived: Unable to accept invitation"; return; } - //createGroup(groupId); } void Widget::onGroupMessageReceived(int groupnumber, int friendgroupnumber, const QString& message) diff --git a/widget/widget.h b/widget/widget.h index a082da09a..5951475d8 100644 --- a/widget/widget.h +++ b/widget/widget.h @@ -62,7 +62,7 @@ private slots: void onFriendWidgetClicked(FriendWidget* widget); void onFriendMessageReceived(int friendId, const QString& message); void onFriendRequestReceived(const QString& userId, const QString& message); - void onGroupInviteReceived(int friendId, uint8_t *publicKey); + void onGroupInviteReceived(int32_t friendId, uint8_t *publicKey); void onGroupMessageReceived(int groupnumber, int friendgroupnumber, const QString& message); void onGroupNamelistChanged(int groupnumber, int peernumber, uint8_t change); void onGroupWidgetClicked(GroupWidget* widget);