1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

Merge branch 'pr544'

This commit is contained in:
Tux3 / Mlkj / !Lev.uXFMLA 2014-10-26 16:25:07 +01:00
commit 8da7b48f3c
No known key found for this signature in database
GPG Key ID: 7E086DD661263264
6 changed files with 48 additions and 43 deletions

View File

@ -22,6 +22,7 @@
#include <QAbstractTextDocumentLayout> #include <QAbstractTextDocumentLayout>
#include <QCoreApplication> #include <QCoreApplication>
#include <QDebug> #include <QDebug>
#include <algorithm>
ChatAreaWidget::ChatAreaWidget(QWidget *parent) ChatAreaWidget::ChatAreaWidget(QWidget *parent)
: QTextBrowser(parent) : QTextBrowser(parent)
@ -49,10 +50,6 @@ ChatAreaWidget::ChatAreaWidget(QWidget *parent)
ChatAreaWidget::~ChatAreaWidget() ChatAreaWidget::~ChatAreaWidget()
{ {
for (ChatAction* action : messages)
delete action;
messages.clear();
if (tableFrmt) if (tableFrmt)
delete tableFrmt; delete tableFrmt;
} }
@ -96,14 +93,14 @@ void ChatAreaWidget::onAnchorClicked(const QUrl &url)
QDesktopServices::openUrl(url); QDesktopServices::openUrl(url);
} }
void ChatAreaWidget::insertMessage(ChatAction *msgAction) void ChatAreaWidget::insertMessage(ChatActionPtr msgAction, QTextCursor::MoveOperation pos)
{ {
if (msgAction == nullptr) if (msgAction == nullptr)
return; return;
checkSlider(); checkSlider();
QTextTable *chatTextTable = getMsgTable(); QTextTable *chatTextTable = getMsgTable(pos);
QTextCursor cur = chatTextTable->cellAt(0, 2).firstCursorPosition(); QTextCursor cur = chatTextTable->cellAt(0, 2).firstCursorPosition();
cur.clearSelection(); cur.clearSelection();
cur.setKeepPositionOnInsert(true); cur.setKeepPositionOnInsert(true);
@ -115,7 +112,8 @@ void ChatAreaWidget::insertMessage(ChatAction *msgAction)
msgAction->setup(cur, this); msgAction->setup(cur, this);
messages.append(msgAction); if (msgAction->isInteractive())
messages.append(msgAction);
} }
int ChatAreaWidget::getNumberOfMessages() int ChatAreaWidget::getNumberOfMessages()
@ -136,7 +134,7 @@ void ChatAreaWidget::checkSlider()
lockSliderToBottom = scroll && scroll->value() == scroll->maximum(); lockSliderToBottom = scroll && scroll->value() == scroll->maximum();
} }
QTextTable *ChatAreaWidget::getMsgTable() QTextTable *ChatAreaWidget::getMsgTable(QTextCursor::MoveOperation pos)
{ {
if (tableFrmt == nullptr) if (tableFrmt == nullptr)
{ {
@ -151,7 +149,7 @@ QTextTable *ChatAreaWidget::getMsgTable()
} }
QTextCursor tc = textCursor(); QTextCursor tc = textCursor();
tc.movePosition(QTextCursor::End); tc.movePosition(pos);
QTextTable *chatTextTable = tc.insertTable(1, 5, *tableFrmt); QTextTable *chatTextTable = tc.insertTable(1, 5, *tableFrmt);
@ -171,21 +169,29 @@ void ChatAreaWidget::setNameColWidth(int w)
void ChatAreaWidget::clearChatArea() void ChatAreaWidget::clearChatArea()
{ {
QList<ChatAction*> newMsgs; QList<ChatActionPtr> newMsgs;
for (ChatAction* message : messages) for (ChatActionPtr message : messages)
{ {
if (message->isInteractive()) if (message->isInteractive())
{ {
newMsgs.append(message); newMsgs.append(message);
} else {
delete message;
} }
} }
messages.clear(); messages.clear();
this->clear(); this->clear();
for (ChatAction* message : newMsgs) for (ChatActionPtr message : newMsgs)
{ {
insertMessage(message); insertMessage(message);
} }
} }
void ChatAreaWidget::insertMessagesTop(QList<ChatActionPtr> &list)
{
std::reverse(list.begin(), list.end());
for (ChatActionPtr it : list)
{
insertMessage(it, QTextCursor::Start);
}
}

View File

@ -19,8 +19,8 @@
#include <QTextBrowser> #include <QTextBrowser>
#include <QList> #include <QList>
#include <src/widget/tool/chatactions/chataction.h>
class ChatAction;
class QTextTable; class QTextTable;
class ChatAreaWidget : public QTextBrowser class ChatAreaWidget : public QTextBrowser
@ -29,8 +29,8 @@ class ChatAreaWidget : public QTextBrowser
public: public:
explicit ChatAreaWidget(QWidget *parent = 0); explicit ChatAreaWidget(QWidget *parent = 0);
virtual ~ChatAreaWidget(); virtual ~ChatAreaWidget();
void insertMessage(ChatAction *msgAction); void insertMessage(ChatActionPtr msgAction, QTextCursor::MoveOperation pos = QTextCursor::End);
QList<ChatAction*>& getMesages() {return messages;} void insertMessagesTop(QList<ChatActionPtr> &list);
int nameColWidth() {return nameWidth;} int nameColWidth() {return nameWidth;}
void setNameColWidth(int w); void setNameColWidth(int w);
@ -51,10 +51,10 @@ private slots:
private: private:
void checkSlider(); void checkSlider();
QTextTable* getMsgTable(); QTextTable* getMsgTable(QTextCursor::MoveOperation pos = QTextCursor::End);
QTextTableFormat* tableFrmt; QTextTableFormat* tableFrmt;
QList<ChatAction*> messages; QList<ChatActionPtr> messages;
bool lockSliderToBottom; bool lockSliderToBottom;
int sliderPosition; int sliderPosition;
int nameWidth; int nameWidth;

View File

@ -153,7 +153,8 @@ void ChatForm::startFileSend(ToxFile file)
name = ""; name = "";
previousName = Widget::getInstance()->getUsername(); previousName = Widget::getInstance()->getUsername();
chatWidget->insertMessage(new FileTransferAction(fileTrans, getElidedName(name), QTime::currentTime().toString("hh:mm"), true)); chatWidget->insertMessage(ChatActionPtr(new FileTransferAction(fileTrans, getElidedName(name),
QTime::currentTime().toString("hh:mm"), true)));
} }
void ChatForm::onFileRecvRequest(ToxFile file) void ChatForm::onFileRecvRequest(ToxFile file)
@ -185,7 +186,8 @@ void ChatForm::onFileRecvRequest(ToxFile file)
name = ""; name = "";
previousName = f->getName(); previousName = f->getName();
chatWidget->insertMessage(new FileTransferAction(fileTrans, getElidedName(name), QTime::currentTime().toString("hh:mm"), false)); chatWidget->insertMessage(ChatActionPtr(new FileTransferAction(fileTrans, getElidedName(name),
QTime::currentTime().toString("hh:mm"), false)));
if (!Settings::getInstance().getAutoAcceptDir(Core::getInstance()->getFriendAddress(f->friendId)).isEmpty() if (!Settings::getInstance().getAutoAcceptDir(Core::getInstance()->getFriendAddress(f->friendId)).isEmpty()
|| !Settings::getInstance().getGlobalAutoAcceptDir().isEmpty()) || !Settings::getInstance().getGlobalAutoAcceptDir().isEmpty())
@ -587,7 +589,7 @@ void ChatForm::onLoadHistory()
QString storedPrevName = previousName; QString storedPrevName = previousName;
previousName = ""; previousName = "";
QList<ChatAction*> historyMessages; QList<ChatActionPtr> historyMessages;
for (const auto &it : msgs) for (const auto &it : msgs)
{ {
@ -595,23 +597,17 @@ void ChatForm::onLoadHistory()
if (it.sender == Core::getInstance()->getSelfId().publicKey) if (it.sender == Core::getInstance()->getSelfId().publicKey)
name = Core::getInstance()->getUsername(); name = Core::getInstance()->getUsername();
ChatAction *ca = genMessageActionAction(name, it.message, false, it.timestamp.toLocalTime()); ChatActionPtr ca = genMessageActionAction(name, it.message, false, it.timestamp.toLocalTime());
historyMessages.append(ca); historyMessages.append(ca);
} }
previousName = storedPrevName; previousName = storedPrevName;
for (ChatAction *ca : chatWidget->getMesages())
historyMessages.append(ca);
int savedSliderPos = chatWidget->verticalScrollBar()->maximum() - chatWidget->verticalScrollBar()->value(); int savedSliderPos = chatWidget->verticalScrollBar()->maximum() - chatWidget->verticalScrollBar()->value();
chatWidget->getMesages().clear();
chatWidget->clear();
if (earliestMessage != nullptr) if (earliestMessage != nullptr)
*earliestMessage = fromTime; *earliestMessage = fromTime;
for (ChatAction *ca : historyMessages) chatWidget->insertMessagesTop(historyMessages);
chatWidget->insertMessage(ca);
savedSliderPos = chatWidget->verticalScrollBar()->maximum() - savedSliderPos; savedSliderPos = chatWidget->verticalScrollBar()->maximum() - savedSliderPos;
chatWidget->verticalScrollBar()->setValue(savedSliderPos); chatWidget->verticalScrollBar()->setValue(savedSliderPos);

View File

@ -187,14 +187,14 @@ void GenericChatForm::onSaveLogClicked()
void GenericChatForm::addMessage(const QString &author, const QString &message, bool isAction, const QDateTime &datetime) void GenericChatForm::addMessage(const QString &author, const QString &message, bool isAction, const QDateTime &datetime)
{ {
ChatAction *ca = genMessageActionAction(author, message, isAction, datetime); ChatActionPtr ca = genMessageActionAction(author, message, isAction, datetime);
chatWidget->insertMessage(ca); chatWidget->insertMessage(ca);
} }
void GenericChatForm::addAlertMessage(QString author, QString message, QDateTime datetime) void GenericChatForm::addAlertMessage(QString author, QString message, QDateTime datetime)
{ {
QString date = datetime.toString(Settings::getInstance().getTimestampFormat()); QString date = datetime.toString(Settings::getInstance().getTimestampFormat());
chatWidget->insertMessage(new AlertAction(author, message, date)); chatWidget->insertMessage(ChatActionPtr(new AlertAction(author, message, date)));
previousName = author; previousName = author;
} }
@ -232,7 +232,7 @@ void GenericChatForm::focusInput()
void GenericChatForm::addSystemInfoMessage(const QString &message, const QString &type, const QDateTime &datetime) void GenericChatForm::addSystemInfoMessage(const QString &message, const QString &type, const QDateTime &datetime)
{ {
ChatAction *ca = genSystemInfoAction(message, type, datetime); ChatActionPtr ca = genSystemInfoAction(message, type, datetime);
chatWidget->insertMessage(ca); chatWidget->insertMessage(ca);
} }
@ -259,7 +259,7 @@ void GenericChatForm::clearChatArea(bool notinform)
} }
} }
ChatAction* GenericChatForm::genMessageActionAction(const QString &author, QString message, bool isAction, const QDateTime &datetime) ChatActionPtr GenericChatForm::genMessageActionAction(const QString &author, QString message, bool isAction, const QDateTime &datetime)
{ {
if (earliestMessage == nullptr) if (earliestMessage == nullptr)
{ {
@ -278,23 +278,23 @@ ChatAction* GenericChatForm::genMessageActionAction(const QString &author, QStri
if (isAction) if (isAction)
{ {
previousName = ""; // next msg has a name regardless previousName = ""; // next msg has a name regardless
return (new ActionAction (getElidedName(author), message, date, isMe)); return ChatActionPtr(new ActionAction (getElidedName(author), message, date, isMe));
} }
ChatAction *res; ChatActionPtr res;
if (previousName == author) if (previousName == author)
res = (new MessageAction("", message, date, isMe)); res = ChatActionPtr(new MessageAction("", message, date, isMe));
else else
res = (new MessageAction(getElidedName(author), message, date, isMe)); res = ChatActionPtr(new MessageAction(getElidedName(author), message, date, isMe));
previousName = author; previousName = author;
return res; return res;
} }
ChatAction* GenericChatForm::genSystemInfoAction(const QString &message, const QString &type, const QDateTime &datetime) ChatActionPtr GenericChatForm::genSystemInfoAction(const QString &message, const QString &type, const QDateTime &datetime)
{ {
previousName = ""; previousName = "";
QString date = datetime.toString(Settings::getInstance().getTimestampFormat()); QString date = datetime.toString(Settings::getInstance().getTimestampFormat());
return (new SystemMessageAction(message, type, date)); return ChatActionPtr(new SystemMessageAction(message, type, date));
} }

View File

@ -21,6 +21,7 @@
#include <QPoint> #include <QPoint>
#include <QDateTime> #include <QDateTime>
#include <QMenu> #include <QMenu>
#include "src/widget/tool/chatactions/chataction.h"
// Spacing in px inserted when the author of the last message changes // Spacing in px inserted when the author of the last message changes
#define AUTHOR_CHANGE_SPACING 5 // why the hell is this a thing? surely the different font is enough? #define AUTHOR_CHANGE_SPACING 5 // why the hell is this a thing? surely the different font is enough?
@ -32,7 +33,6 @@ class CroppingLabel;
class ChatTextEdit; class ChatTextEdit;
class ChatAreaWidget; class ChatAreaWidget;
class MaskablePixmapWidget; class MaskablePixmapWidget;
class ChatAction;
namespace Ui { namespace Ui {
class MainWindow; class MainWindow;
@ -67,8 +67,8 @@ protected slots:
protected: protected:
QString getElidedName(const QString& name); QString getElidedName(const QString& name);
ChatAction* genMessageActionAction(const QString &author, QString message, bool isAction, const QDateTime &datetime); ChatActionPtr genMessageActionAction(const QString &author, QString message, bool isAction, const QDateTime &datetime);
ChatAction* genSystemInfoAction(const QString &message, const QString &type, const QDateTime &datetime); ChatActionPtr genSystemInfoAction(const QString &message, const QString &type, const QDateTime &datetime);
QMenu menu; QMenu menu;
CroppingLabel *nameLabel; CroppingLabel *nameLabel;

View File

@ -19,6 +19,7 @@
#include <QString> #include <QString>
#include <QTextCursor> #include <QTextCursor>
#include <QSharedPointer>
class FileTransferInstance; class FileTransferInstance;
class QTextEdit; class QTextEdit;
@ -44,4 +45,6 @@ protected:
QString name, date; QString name, date;
}; };
typedef QSharedPointer<ChatAction> ChatActionPtr;
#endif // CHATACTION_H #endif // CHATACTION_H