diff --git a/src/chatlog/chatlog.cpp b/src/chatlog/chatlog.cpp index 309828756..a6d08e6ae 100644 --- a/src/chatlog/chatlog.cpp +++ b/src/chatlog/chatlog.cpp @@ -32,6 +32,8 @@ #include #include #include +#include +#include template T clamp(T x, T min, T max) @@ -459,6 +461,27 @@ QString ChatLog::getSelectedText() const return QString(); } +QString ChatLog::toPlainText() const +{ + QString out; + QString lastSender; + + for(ChatLine* l : lines) + { + if(lastSender != l->content[0]->getText() && !l->content[0]->getText().isEmpty()) + { + //author changed + out += l->content[0]->getText() + ":\n"; + lastSender = l->content[0]->getText(); + } + + out += l->content[1]->getText(); + out += "\n\n"; + } + + return out; +} + bool ChatLog::isEmpty() const { return lines.isEmpty(); @@ -469,9 +492,10 @@ void ChatLog::showContextMenu(const QPoint& globalPos, const QPointF& scenePos) QMenu menu; // populate - QAction* copyAction = menu.addAction(QIcon::fromTheme("edit-copy"), "Copy"); + QAction* copyAction = menu.addAction(QIcon::fromTheme("edit-copy"), tr("Copy")); menu.addSeparator(); - QAction* clearAction = menu.addAction("Clear log"); + QAction* clearAction = menu.addAction(QIcon::fromTheme("edit-clear") ,tr("Clear chat log")); + QAction* saveAction = menu.addAction(QIcon::fromTheme("document-save") ,tr("Save chat log")); if(!isOverSelection(scenePos)) copyAction->setDisabled(true); @@ -484,6 +508,20 @@ void ChatLog::showContextMenu(const QPoint& globalPos, const QPointF& scenePos) if(action == clearAction) clear(); + + if(action == saveAction) + { + QString path = QFileDialog::getSaveFileName(0, tr("Save chat log")); + if (path.isEmpty()) + return; + + QFile file(path); + if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) + return; + + file.write(toPlainText().toUtf8()); + file.close(); + } } void ChatLog::clear() diff --git a/src/chatlog/chatlog.h b/src/chatlog/chatlog.h index 7a6a5b14f..714ec9963 100644 --- a/src/chatlog/chatlog.h +++ b/src/chatlog/chatlog.h @@ -49,6 +49,7 @@ public: void clear(); void copySelectedText() const; QString getSelectedText() const; + QString toPlainText() const; bool isEmpty() const; diff --git a/src/widget/form/genericchatform.cpp b/src/widget/form/genericchatform.cpp index 076690924..8fc54c439 100644 --- a/src/widget/form/genericchatform.cpp +++ b/src/widget/form/genericchatform.cpp @@ -143,10 +143,6 @@ GenericChatForm::GenericChatForm(QWidget *parent) : fileButton->setAttribute(Qt::WA_LayoutUsesWidgetRect); emoteButton->setAttribute(Qt::WA_LayoutUsesWidgetRect); - menu.addAction(tr("Save chat log"), this, SLOT(onSaveLogClicked())); - menu.addAction(tr("Clear displayed messages"), this, SLOT(clearChatArea(bool))); - menu.addSeparator(); - connect(emoteButton, SIGNAL(clicked()), this, SLOT(onEmoteButtonClicked())); connect(chatWidget, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(onChatContextMenuRequested(QPoint))); connect(chatWidget, SIGNAL(onClick()), this, SLOT(onChatWidgetClicked())); @@ -189,23 +185,6 @@ void GenericChatForm::onChatContextMenuRequested(QPoint pos) menu.exec(pos); } -void GenericChatForm::onSaveLogClicked() -{ -// QString path = QFileDialog::getSaveFileName(0, tr("Save chat log")); -// if (path.isEmpty()) -// return; - -// QFile file(path); -// if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) -// return; - -// QString log; -// log = chatWidget->toPlainText(); - -// file.write(log.toUtf8()); -// file.close(); -} - ChatMessage* GenericChatForm::addMessage(const ToxID& author, const QString &message, bool isAction, const QDateTime &datetime, bool isSent) { diff --git a/src/widget/form/genericchatform.h b/src/widget/form/genericchatform.h index 570bff8fd..37a63faee 100644 --- a/src/widget/form/genericchatform.h +++ b/src/widget/form/genericchatform.h @@ -69,7 +69,6 @@ public slots: protected slots: void onChatContextMenuRequested(QPoint pos); - void onSaveLogClicked(); void onEmoteButtonClicked(); void onEmoteInsertRequested(QString str); void clearChatArea(bool);