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 <QCoreApplication>
#include <QDebug>
#include <algorithm>
ChatAreaWidget::ChatAreaWidget(QWidget *parent)
: QTextBrowser(parent)
@ -49,10 +50,6 @@ ChatAreaWidget::ChatAreaWidget(QWidget *parent)
ChatAreaWidget::~ChatAreaWidget()
{
for (ChatAction* action : messages)
delete action;
messages.clear();
if (tableFrmt)
delete tableFrmt;
}
@ -96,14 +93,14 @@ void ChatAreaWidget::onAnchorClicked(const QUrl &url)
QDesktopServices::openUrl(url);
}
void ChatAreaWidget::insertMessage(ChatAction *msgAction)
void ChatAreaWidget::insertMessage(ChatActionPtr msgAction, QTextCursor::MoveOperation pos)
{
if (msgAction == nullptr)
return;
checkSlider();
QTextTable *chatTextTable = getMsgTable();
QTextTable *chatTextTable = getMsgTable(pos);
QTextCursor cur = chatTextTable->cellAt(0, 2).firstCursorPosition();
cur.clearSelection();
cur.setKeepPositionOnInsert(true);
@ -115,7 +112,8 @@ void ChatAreaWidget::insertMessage(ChatAction *msgAction)
msgAction->setup(cur, this);
messages.append(msgAction);
if (msgAction->isInteractive())
messages.append(msgAction);
}
int ChatAreaWidget::getNumberOfMessages()
@ -136,7 +134,7 @@ void ChatAreaWidget::checkSlider()
lockSliderToBottom = scroll && scroll->value() == scroll->maximum();
}
QTextTable *ChatAreaWidget::getMsgTable()
QTextTable *ChatAreaWidget::getMsgTable(QTextCursor::MoveOperation pos)
{
if (tableFrmt == nullptr)
{
@ -151,7 +149,7 @@ QTextTable *ChatAreaWidget::getMsgTable()
}
QTextCursor tc = textCursor();
tc.movePosition(QTextCursor::End);
tc.movePosition(pos);
QTextTable *chatTextTable = tc.insertTable(1, 5, *tableFrmt);
@ -171,21 +169,29 @@ void ChatAreaWidget::setNameColWidth(int w)
void ChatAreaWidget::clearChatArea()
{
QList<ChatAction*> newMsgs;
for (ChatAction* message : messages)
QList<ChatActionPtr> newMsgs;
for (ChatActionPtr message : messages)
{
if (message->isInteractive())
{
newMsgs.append(message);
} else {
delete message;
}
}
messages.clear();
this->clear();
for (ChatAction* message : newMsgs)
for (ChatActionPtr message : newMsgs)
{
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 <QList>
#include <src/widget/tool/chatactions/chataction.h>
class ChatAction;
class QTextTable;
class ChatAreaWidget : public QTextBrowser
@ -29,8 +29,8 @@ class ChatAreaWidget : public QTextBrowser
public:
explicit ChatAreaWidget(QWidget *parent = 0);
virtual ~ChatAreaWidget();
void insertMessage(ChatAction *msgAction);
QList<ChatAction*>& getMesages() {return messages;}
void insertMessage(ChatActionPtr msgAction, QTextCursor::MoveOperation pos = QTextCursor::End);
void insertMessagesTop(QList<ChatActionPtr> &list);
int nameColWidth() {return nameWidth;}
void setNameColWidth(int w);
@ -51,10 +51,10 @@ private slots:
private:
void checkSlider();
QTextTable* getMsgTable();
QTextTable* getMsgTable(QTextCursor::MoveOperation pos = QTextCursor::End);
QTextTableFormat* tableFrmt;
QList<ChatAction*> messages;
QList<ChatActionPtr> messages;
bool lockSliderToBottom;
int sliderPosition;
int nameWidth;

View File

@ -153,7 +153,8 @@ void ChatForm::startFileSend(ToxFile file)
name = "";
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)
@ -185,7 +186,8 @@ void ChatForm::onFileRecvRequest(ToxFile file)
name = "";
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()
|| !Settings::getInstance().getGlobalAutoAcceptDir().isEmpty())
@ -587,7 +589,7 @@ void ChatForm::onLoadHistory()
QString storedPrevName = previousName;
previousName = "";
QList<ChatAction*> historyMessages;
QList<ChatActionPtr> historyMessages;
for (const auto &it : msgs)
{
@ -595,23 +597,17 @@ void ChatForm::onLoadHistory()
if (it.sender == Core::getInstance()->getSelfId().publicKey)
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);
}
previousName = storedPrevName;
for (ChatAction *ca : chatWidget->getMesages())
historyMessages.append(ca);
int savedSliderPos = chatWidget->verticalScrollBar()->maximum() - chatWidget->verticalScrollBar()->value();
chatWidget->getMesages().clear();
chatWidget->clear();
if (earliestMessage != nullptr)
*earliestMessage = fromTime;
for (ChatAction *ca : historyMessages)
chatWidget->insertMessage(ca);
chatWidget->insertMessagesTop(historyMessages);
savedSliderPos = chatWidget->verticalScrollBar()->maximum() - 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)
{
ChatAction *ca = genMessageActionAction(author, message, isAction, datetime);
ChatActionPtr ca = genMessageActionAction(author, message, isAction, datetime);
chatWidget->insertMessage(ca);
}
void GenericChatForm::addAlertMessage(QString author, QString message, QDateTime datetime)
{
QString date = datetime.toString(Settings::getInstance().getTimestampFormat());
chatWidget->insertMessage(new AlertAction(author, message, date));
chatWidget->insertMessage(ChatActionPtr(new AlertAction(author, message, date)));
previousName = author;
}
@ -232,7 +232,7 @@ void GenericChatForm::focusInput()
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);
}
@ -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)
{
@ -278,23 +278,23 @@ ChatAction* GenericChatForm::genMessageActionAction(const QString &author, QStri
if (isAction)
{
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)
res = (new MessageAction("", message, date, isMe));
res = ChatActionPtr(new MessageAction("", message, date, isMe));
else
res = (new MessageAction(getElidedName(author), message, date, isMe));
res = ChatActionPtr(new MessageAction(getElidedName(author), message, date, isMe));
previousName = author;
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 = "";
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 <QDateTime>
#include <QMenu>
#include "src/widget/tool/chatactions/chataction.h"
// 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?
@ -32,7 +33,6 @@ class CroppingLabel;
class ChatTextEdit;
class ChatAreaWidget;
class MaskablePixmapWidget;
class ChatAction;
namespace Ui {
class MainWindow;
@ -67,8 +67,8 @@ protected slots:
protected:
QString getElidedName(const QString& name);
ChatAction* genMessageActionAction(const QString &author, QString message, bool isAction, const QDateTime &datetime);
ChatAction* genSystemInfoAction(const QString &message, const QString &type, const QDateTime &datetime);
ChatActionPtr genMessageActionAction(const QString &author, QString message, bool isAction, const QDateTime &datetime);
ChatActionPtr genSystemInfoAction(const QString &message, const QString &type, const QDateTime &datetime);
QMenu menu;
CroppingLabel *nameLabel;

View File

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