From 040a833d56cbced60ed5f094da3a39507752486e Mon Sep 17 00:00:00 2001 From: sudden6 Date: Tue, 7 Jul 2020 21:44:39 +0200 Subject: [PATCH] refactor: remove getInstance from filetransferwidget.cpp --- src/chatlog/chatmessage.cpp | 9 +++---- src/chatlog/chatmessage.h | 5 ++-- src/chatlog/content/filetransferwidget.cpp | 22 ++++++++--------- src/chatlog/content/filetransferwidget.h | 4 +++- src/widget/form/genericchatform.cpp | 28 ++++++++++++---------- src/widget/form/genericchatform.h | 1 + 6 files changed, 38 insertions(+), 31 deletions(-) diff --git a/src/chatlog/chatmessage.cpp b/src/chatlog/chatmessage.cpp index e1a3193ae..e14672eef 100644 --- a/src/chatlog/chatmessage.cpp +++ b/src/chatlog/chatmessage.cpp @@ -157,19 +157,20 @@ ChatMessage::Ptr ChatMessage::createChatInfoMessage(const QString& rawMessage, return msg; } -ChatMessage::Ptr ChatMessage::createFileTransferMessage(const QString& sender, ToxFile file, - bool isMe, const QDateTime& date) +ChatMessage::Ptr ChatMessage::createFileTransferMessage(const QString& sender, CoreFile& coreFile, + ToxFile file, bool isMe, const QDateTime& date) { ChatMessage::Ptr msg = ChatMessage::Ptr(new ChatMessage); QFont baseFont = Settings::getInstance().getChatMessageFont(); QFont authorFont = baseFont; - if (isMe) + if (isMe) { authorFont.setBold(true); + } msg->addColumn(new Text(sender, authorFont, true), ColumnFormat(NAME_COL_WIDTH, ColumnFormat::FixedSize, ColumnFormat::Right)); - msg->addColumn(new ChatLineContentProxy(new FileTransferWidget(nullptr, file), 320, 0.6f), + msg->addColumn(new ChatLineContentProxy(new FileTransferWidget(nullptr, coreFile, file), 320, 0.6f), ColumnFormat(1.0, ColumnFormat::VariableSize)); msg->addColumn(new Timestamp(date, Settings::getInstance().getTimestampFormat(), baseFont), ColumnFormat(TIME_COL_WIDTH, ColumnFormat::FixedSize, ColumnFormat::Right)); diff --git a/src/chatlog/chatmessage.h b/src/chatlog/chatmessage.h index 8eb6e3dd0..cc3087b44 100644 --- a/src/chatlog/chatmessage.h +++ b/src/chatlog/chatmessage.h @@ -25,6 +25,7 @@ #include +class CoreFile; class QGraphicsScene; class ChatMessage : public ChatLine @@ -53,8 +54,8 @@ public: const QDateTime& date, bool colorizeName = false); static ChatMessage::Ptr createChatInfoMessage(const QString& rawMessage, SystemMessageType type, const QDateTime& date); - static ChatMessage::Ptr createFileTransferMessage(const QString& sender, ToxFile file, - bool isMe, const QDateTime& date); + static ChatMessage::Ptr createFileTransferMessage(const QString& sender, CoreFile& coreFile, + ToxFile file, bool isMe, const QDateTime& date); static ChatMessage::Ptr createTypingNotification(); static ChatMessage::Ptr createBusyNotification(); diff --git a/src/chatlog/content/filetransferwidget.cpp b/src/chatlog/content/filetransferwidget.cpp index f99ecbffc..ef5599cba 100644 --- a/src/chatlog/content/filetransferwidget.cpp +++ b/src/chatlog/content/filetransferwidget.cpp @@ -20,7 +20,6 @@ #include "filetransferwidget.h" #include "ui_filetransferwidget.h" -#include "src/core/core.h" #include "src/core/corefile.h" #include "src/persistence/settings.h" #include "src/widget/gui.h" @@ -48,8 +47,9 @@ // The rightButton is used to cancel a file transfer, or to open the directory a file was // downloaded to. -FileTransferWidget::FileTransferWidget(QWidget* parent, ToxFile file) +FileTransferWidget::FileTransferWidget(QWidget* parent, CoreFile& _coreFile, ToxFile file) : QWidget(parent) + , coreFile{_coreFile} , ui(new Ui::FileTransferWidget) , fileInfo(file) , backgroundColor(Style::getColor(Style::TransferMiddle)) @@ -142,8 +142,7 @@ void FileTransferWidget::acceptTransfer(const QString& filepath) } // everything ok! - CoreFile* coreFile = Core::getInstance()->getCoreFile(); - coreFile->acceptFileRecvRequest(fileInfo.friendId, fileInfo.fileNum, filepath); + coreFile.acceptFileRecvRequest(fileInfo.friendId, fileInfo.fileNum, filepath); } void FileTransferWidget::setBackgroundColor(const QColor& c, bool whiteFont) @@ -385,7 +384,7 @@ void FileTransferWidget::updateSignals(ToxFile const& file) case ToxFile::BROKEN: case ToxFile::FINISHED: active = false; - disconnect(Core::getInstance()->getCoreFile(), nullptr, this, nullptr); + disconnect(&coreFile, nullptr, this, nullptr); break; case ToxFile::INITIALIZING: case ToxFile::PAUSED: @@ -474,23 +473,22 @@ void FileTransferWidget::setupButtons(ToxFile const& file) void FileTransferWidget::handleButton(QPushButton* btn) { - CoreFile* coreFile = Core::getInstance()->getCoreFile(); if (fileInfo.direction == ToxFile::SENDING) { if (btn->objectName() == "cancel") { - coreFile->cancelFileSend(fileInfo.friendId, fileInfo.fileNum); + coreFile.cancelFileSend(fileInfo.friendId, fileInfo.fileNum); } else if (btn->objectName() == "pause") { - coreFile->pauseResumeFile(fileInfo.friendId, fileInfo.fileNum); + coreFile.pauseResumeFile(fileInfo.friendId, fileInfo.fileNum); } else if (btn->objectName() == "resume") { - coreFile->pauseResumeFile(fileInfo.friendId, fileInfo.fileNum); + coreFile.pauseResumeFile(fileInfo.friendId, fileInfo.fileNum); } } else // receiving or paused { if (btn->objectName() == "cancel") { - coreFile->cancelFileRecv(fileInfo.friendId, fileInfo.fileNum); + coreFile.cancelFileRecv(fileInfo.friendId, fileInfo.fileNum); } else if (btn->objectName() == "pause") { - coreFile->pauseResumeFile(fileInfo.friendId, fileInfo.fileNum); + coreFile.pauseResumeFile(fileInfo.friendId, fileInfo.fileNum); } else if (btn->objectName() == "resume") { - coreFile->pauseResumeFile(fileInfo.friendId, fileInfo.fileNum); + coreFile.pauseResumeFile(fileInfo.friendId, fileInfo.fileNum); } else if (btn->objectName() == "accept") { QString path = QFileDialog::getSaveFileName(Q_NULLPTR, diff --git a/src/chatlog/content/filetransferwidget.h b/src/chatlog/content/filetransferwidget.h index 46ac03bdb..087969d3a 100644 --- a/src/chatlog/content/filetransferwidget.h +++ b/src/chatlog/content/filetransferwidget.h @@ -26,6 +26,7 @@ #include "src/chatlog/toxfileprogress.h" #include "src/core/toxfile.h" +class CoreFile; namespace Ui { class FileTransferWidget; @@ -39,7 +40,7 @@ class FileTransferWidget : public QWidget Q_OBJECT public: - explicit FileTransferWidget(QWidget* parent, ToxFile file); + explicit FileTransferWidget(QWidget* parent, CoreFile& _coreFile, ToxFile file); virtual ~FileTransferWidget(); bool isActive() const; static QString getHumanReadableSize(qint64 size); @@ -77,6 +78,7 @@ private: void updateWidget(ToxFile const& file); private: + CoreFile& coreFile; Ui::FileTransferWidget* ui; ToxFileProgress fileProgress; ToxFile fileInfo; diff --git a/src/widget/form/genericchatform.cpp b/src/widget/form/genericchatform.cpp index 13eba2508..078706e1c 100644 --- a/src/widget/form/genericchatform.cpp +++ b/src/widget/form/genericchatform.cpp @@ -195,18 +195,7 @@ void renderMessageRaw(const QString& displayName, bool isSelf, bool colorizeName } } -void renderFile(QString displayName, ToxFile file, bool isSelf, QDateTime timestamp, - ChatMessage::Ptr& chatMessage) -{ - if (!chatMessage) { - chatMessage = ChatMessage::createFileTransferMessage(displayName, file, isSelf, timestamp); - } else { - auto proxy = static_cast(chatMessage->getContent(1)); - assert(proxy->getWidgetType() == ChatLineContentProxy::FileTransferWidgetType); - auto ftWidget = static_cast(proxy->getWidget()); - ftWidget->onFileTransferUpdate(file); - } -} + ChatLogIdx firstItemAfterDate(QDate date, const IChatLog& chatLog) { @@ -371,6 +360,21 @@ GenericChatForm::~GenericChatForm() delete searchForm; } +void GenericChatForm::renderFile(QString displayName, ToxFile file, bool isSelf, QDateTime timestamp, + ChatMessage::Ptr& chatMessage) +{ + if (!chatMessage) { + CoreFile* coreFile = core.getCoreFile(); + assert(coreFile); + chatMessage = ChatMessage::createFileTransferMessage(displayName, *coreFile, file, isSelf, timestamp); + } else { + auto proxy = static_cast(chatMessage->getContent(1)); + assert(proxy->getWidgetType() == ChatLineContentProxy::FileTransferWidgetType); + auto ftWidget = static_cast(proxy->getWidget()); + ftWidget->onFileTransferUpdate(file); + } +} + void GenericChatForm::adjustFileMenuPosition() { QPoint pos = fileButton->mapTo(bodySplitter, QPoint()); diff --git a/src/widget/form/genericchatform.h b/src/widget/form/genericchatform.h index 5c10038d4..8173f896a 100644 --- a/src/widget/form/genericchatform.h +++ b/src/widget/form/genericchatform.h @@ -136,6 +136,7 @@ private: void removeLastsMessages(const int num); void renderItem(const ChatLogItem &item, bool hideName, bool colorizeNames, ChatMessage::Ptr &chatMessage); + void renderFile(QString displayName, ToxFile file, bool isSelf, QDateTime timestamp, ChatMessage::Ptr &chatMessage); protected: ChatMessage::Ptr createMessage(const ToxPk& author, const QString& message, const QDateTime& datetime, bool isAction, bool isSent, bool colorizeName = false);