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

refactor: remove getInstance from filetransferwidget.cpp

This commit is contained in:
sudden6 2020-07-07 21:44:39 +02:00
parent 7dfd93bd91
commit 040a833d56
No known key found for this signature in database
GPG Key ID: 279509B499E032B9
6 changed files with 38 additions and 31 deletions

View File

@ -157,19 +157,20 @@ ChatMessage::Ptr ChatMessage::createChatInfoMessage(const QString& rawMessage,
return msg; return msg;
} }
ChatMessage::Ptr ChatMessage::createFileTransferMessage(const QString& sender, ToxFile file, ChatMessage::Ptr ChatMessage::createFileTransferMessage(const QString& sender, CoreFile& coreFile,
bool isMe, const QDateTime& date) ToxFile file, bool isMe, const QDateTime& date)
{ {
ChatMessage::Ptr msg = ChatMessage::Ptr(new ChatMessage); ChatMessage::Ptr msg = ChatMessage::Ptr(new ChatMessage);
QFont baseFont = Settings::getInstance().getChatMessageFont(); QFont baseFont = Settings::getInstance().getChatMessageFont();
QFont authorFont = baseFont; QFont authorFont = baseFont;
if (isMe) if (isMe) {
authorFont.setBold(true); authorFont.setBold(true);
}
msg->addColumn(new Text(sender, authorFont, true), msg->addColumn(new Text(sender, authorFont, true),
ColumnFormat(NAME_COL_WIDTH, ColumnFormat::FixedSize, ColumnFormat::Right)); 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)); ColumnFormat(1.0, ColumnFormat::VariableSize));
msg->addColumn(new Timestamp(date, Settings::getInstance().getTimestampFormat(), baseFont), msg->addColumn(new Timestamp(date, Settings::getInstance().getTimestampFormat(), baseFont),
ColumnFormat(TIME_COL_WIDTH, ColumnFormat::FixedSize, ColumnFormat::Right)); ColumnFormat(TIME_COL_WIDTH, ColumnFormat::FixedSize, ColumnFormat::Right));

View File

@ -25,6 +25,7 @@
#include <QDateTime> #include <QDateTime>
class CoreFile;
class QGraphicsScene; class QGraphicsScene;
class ChatMessage : public ChatLine class ChatMessage : public ChatLine
@ -53,8 +54,8 @@ public:
const QDateTime& date, bool colorizeName = false); const QDateTime& date, bool colorizeName = false);
static ChatMessage::Ptr createChatInfoMessage(const QString& rawMessage, SystemMessageType type, static ChatMessage::Ptr createChatInfoMessage(const QString& rawMessage, SystemMessageType type,
const QDateTime& date); const QDateTime& date);
static ChatMessage::Ptr createFileTransferMessage(const QString& sender, ToxFile file, static ChatMessage::Ptr createFileTransferMessage(const QString& sender, CoreFile& coreFile,
bool isMe, const QDateTime& date); ToxFile file, bool isMe, const QDateTime& date);
static ChatMessage::Ptr createTypingNotification(); static ChatMessage::Ptr createTypingNotification();
static ChatMessage::Ptr createBusyNotification(); static ChatMessage::Ptr createBusyNotification();

View File

@ -20,7 +20,6 @@
#include "filetransferwidget.h" #include "filetransferwidget.h"
#include "ui_filetransferwidget.h" #include "ui_filetransferwidget.h"
#include "src/core/core.h"
#include "src/core/corefile.h" #include "src/core/corefile.h"
#include "src/persistence/settings.h" #include "src/persistence/settings.h"
#include "src/widget/gui.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 // The rightButton is used to cancel a file transfer, or to open the directory a file was
// downloaded to. // downloaded to.
FileTransferWidget::FileTransferWidget(QWidget* parent, ToxFile file) FileTransferWidget::FileTransferWidget(QWidget* parent, CoreFile& _coreFile, ToxFile file)
: QWidget(parent) : QWidget(parent)
, coreFile{_coreFile}
, ui(new Ui::FileTransferWidget) , ui(new Ui::FileTransferWidget)
, fileInfo(file) , fileInfo(file)
, backgroundColor(Style::getColor(Style::TransferMiddle)) , backgroundColor(Style::getColor(Style::TransferMiddle))
@ -142,8 +142,7 @@ void FileTransferWidget::acceptTransfer(const QString& filepath)
} }
// everything ok! // 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) void FileTransferWidget::setBackgroundColor(const QColor& c, bool whiteFont)
@ -385,7 +384,7 @@ void FileTransferWidget::updateSignals(ToxFile const& file)
case ToxFile::BROKEN: case ToxFile::BROKEN:
case ToxFile::FINISHED: case ToxFile::FINISHED:
active = false; active = false;
disconnect(Core::getInstance()->getCoreFile(), nullptr, this, nullptr); disconnect(&coreFile, nullptr, this, nullptr);
break; break;
case ToxFile::INITIALIZING: case ToxFile::INITIALIZING:
case ToxFile::PAUSED: case ToxFile::PAUSED:
@ -474,23 +473,22 @@ void FileTransferWidget::setupButtons(ToxFile const& file)
void FileTransferWidget::handleButton(QPushButton* btn) void FileTransferWidget::handleButton(QPushButton* btn)
{ {
CoreFile* coreFile = Core::getInstance()->getCoreFile();
if (fileInfo.direction == ToxFile::SENDING) { if (fileInfo.direction == ToxFile::SENDING) {
if (btn->objectName() == "cancel") { if (btn->objectName() == "cancel") {
coreFile->cancelFileSend(fileInfo.friendId, fileInfo.fileNum); coreFile.cancelFileSend(fileInfo.friendId, fileInfo.fileNum);
} else if (btn->objectName() == "pause") { } else if (btn->objectName() == "pause") {
coreFile->pauseResumeFile(fileInfo.friendId, fileInfo.fileNum); coreFile.pauseResumeFile(fileInfo.friendId, fileInfo.fileNum);
} else if (btn->objectName() == "resume") { } else if (btn->objectName() == "resume") {
coreFile->pauseResumeFile(fileInfo.friendId, fileInfo.fileNum); coreFile.pauseResumeFile(fileInfo.friendId, fileInfo.fileNum);
} }
} else // receiving or paused } else // receiving or paused
{ {
if (btn->objectName() == "cancel") { if (btn->objectName() == "cancel") {
coreFile->cancelFileRecv(fileInfo.friendId, fileInfo.fileNum); coreFile.cancelFileRecv(fileInfo.friendId, fileInfo.fileNum);
} else if (btn->objectName() == "pause") { } else if (btn->objectName() == "pause") {
coreFile->pauseResumeFile(fileInfo.friendId, fileInfo.fileNum); coreFile.pauseResumeFile(fileInfo.friendId, fileInfo.fileNum);
} else if (btn->objectName() == "resume") { } else if (btn->objectName() == "resume") {
coreFile->pauseResumeFile(fileInfo.friendId, fileInfo.fileNum); coreFile.pauseResumeFile(fileInfo.friendId, fileInfo.fileNum);
} else if (btn->objectName() == "accept") { } else if (btn->objectName() == "accept") {
QString path = QString path =
QFileDialog::getSaveFileName(Q_NULLPTR, QFileDialog::getSaveFileName(Q_NULLPTR,

View File

@ -26,6 +26,7 @@
#include "src/chatlog/toxfileprogress.h" #include "src/chatlog/toxfileprogress.h"
#include "src/core/toxfile.h" #include "src/core/toxfile.h"
class CoreFile;
namespace Ui { namespace Ui {
class FileTransferWidget; class FileTransferWidget;
@ -39,7 +40,7 @@ class FileTransferWidget : public QWidget
Q_OBJECT Q_OBJECT
public: public:
explicit FileTransferWidget(QWidget* parent, ToxFile file); explicit FileTransferWidget(QWidget* parent, CoreFile& _coreFile, ToxFile file);
virtual ~FileTransferWidget(); virtual ~FileTransferWidget();
bool isActive() const; bool isActive() const;
static QString getHumanReadableSize(qint64 size); static QString getHumanReadableSize(qint64 size);
@ -77,6 +78,7 @@ private:
void updateWidget(ToxFile const& file); void updateWidget(ToxFile const& file);
private: private:
CoreFile& coreFile;
Ui::FileTransferWidget* ui; Ui::FileTransferWidget* ui;
ToxFileProgress fileProgress; ToxFileProgress fileProgress;
ToxFile fileInfo; ToxFile fileInfo;

View File

@ -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<ChatLineContentProxy*>(chatMessage->getContent(1));
assert(proxy->getWidgetType() == ChatLineContentProxy::FileTransferWidgetType);
auto ftWidget = static_cast<FileTransferWidget*>(proxy->getWidget());
ftWidget->onFileTransferUpdate(file);
}
}
ChatLogIdx firstItemAfterDate(QDate date, const IChatLog& chatLog) ChatLogIdx firstItemAfterDate(QDate date, const IChatLog& chatLog)
{ {
@ -371,6 +360,21 @@ GenericChatForm::~GenericChatForm()
delete searchForm; 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<ChatLineContentProxy*>(chatMessage->getContent(1));
assert(proxy->getWidgetType() == ChatLineContentProxy::FileTransferWidgetType);
auto ftWidget = static_cast<FileTransferWidget*>(proxy->getWidget());
ftWidget->onFileTransferUpdate(file);
}
}
void GenericChatForm::adjustFileMenuPosition() void GenericChatForm::adjustFileMenuPosition()
{ {
QPoint pos = fileButton->mapTo(bodySplitter, QPoint()); QPoint pos = fileButton->mapTo(bodySplitter, QPoint());

View File

@ -136,6 +136,7 @@ private:
void removeLastsMessages(const int num); void removeLastsMessages(const int num);
void renderItem(const ChatLogItem &item, bool hideName, bool colorizeNames, ChatMessage::Ptr &chatMessage); 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: protected:
ChatMessage::Ptr createMessage(const ToxPk& author, const QString& message, ChatMessage::Ptr createMessage(const ToxPk& author, const QString& message,
const QDateTime& datetime, bool isAction, bool isSent, bool colorizeName = false); const QDateTime& datetime, bool isAction, bool isSent, bool colorizeName = false);