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

QSharedPointer<ChatAction> instead ChatAction*

This commit is contained in:
apprb 2014-10-27 00:04:39 +09:00
parent 8300f42e02
commit 77d65a3cad
No known key found for this signature in database
GPG Key ID: B001911B5B22FB9B
6 changed files with 32 additions and 35 deletions

View File

@ -50,10 +50,6 @@ ChatAreaWidget::ChatAreaWidget(QWidget *parent)
ChatAreaWidget::~ChatAreaWidget()
{
for (ChatAction* action : messages)
delete action;
messages.clear();
if (tableFrmt)
delete tableFrmt;
}
@ -97,7 +93,7 @@ void ChatAreaWidget::onAnchorClicked(const QUrl &url)
QDesktopServices::openUrl(url);
}
void ChatAreaWidget::insertMessage(ChatAction *msgAction, QTextCursor::MoveOperation pos)
void ChatAreaWidget::insertMessage(ChatActionPtr msgAction, QTextCursor::MoveOperation pos)
{
if (msgAction == nullptr)
return;
@ -118,8 +114,6 @@ void ChatAreaWidget::insertMessage(ChatAction *msgAction, QTextCursor::MoveOpera
if (msgAction->isInteractive())
messages.append(msgAction);
else
delete msgAction;
}
int ChatAreaWidget::getNumberOfMessages()
@ -175,30 +169,28 @@ 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<ChatAction*> &list)
void ChatAreaWidget::insertMessagesTop(QList<ChatActionPtr> &list)
{
std::reverse(list.begin(), list.end());
for (ChatAction* it : list)
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, QTextCursor::MoveOperation pos = QTextCursor::End);
void insertMessagesTop(QList<ChatAction *> &list);
void insertMessage(ChatActionPtr msgAction, QTextCursor::MoveOperation pos = QTextCursor::End);
void insertMessagesTop(QList<ChatActionPtr> &list);
int nameColWidth() {return nameWidth;}
void setNameColWidth(int w);
@ -54,7 +54,7 @@ private:
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())
@ -575,7 +577,7 @@ void ChatForm::onLoadHistory()
QString storedPrevName = previousName;
previousName = "";
QList<ChatAction*> historyMessages;
QList<ChatActionPtr> historyMessages;
for (const auto &it : msgs)
{
@ -583,7 +585,7 @@ 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;

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