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

refactoring

This commit is contained in:
krepa098 2015-01-04 18:21:35 +01:00
parent a74b9ce11c
commit b09805f9bf
12 changed files with 112 additions and 116 deletions

View File

@ -24,8 +24,7 @@
#define CELL_SPACING 15
ChatLine::ChatLine(QGraphicsScene* grScene)
: scene(grScene)
ChatLine::ChatLine()
{
}
@ -35,7 +34,7 @@ ChatLine::~ChatLine()
for(ChatLineContent* c : content)
{
if(c->scene())
scene->removeItem(c);
c->scene()->removeItem(c);
delete c;
}
@ -78,10 +77,19 @@ void ChatLine::removeFromScene()
for(ChatLineContent* c : content)
{
if(c->scene())
scene->removeItem(c);
c->scene()->removeItem(c);
}
}
void ChatLine::addToScene(QGraphicsScene *scene)
{
if(!scene)
return;
for(ChatLineContent* c : content)
scene->addItem(c);
}
void ChatLine::selectionCleared()
{
for(ChatLineContent* c : content)
@ -133,7 +141,6 @@ void ChatLine::addColumn(ChatLineContent* item, ColumnFormat fmt)
return;
item->setChatLine(this);
scene->addItem(item);
format << fmt;
content << item;
@ -143,11 +150,13 @@ void ChatLine::replaceContent(int col, ChatLineContent *lineContent)
{
if(col >= 0 && col < content.size() && lineContent)
{
scene->removeItem(content[col]);
QGraphicsScene* scene = content[col]->scene();
delete content[col];
content[col] = lineContent;
lineContent->setIndex(rowIndex, col);
if(scene)
scene->addItem(content[col]);
layout(width, pos);

View File

@ -57,12 +57,11 @@ class ChatLine
public:
using Ptr = std::shared_ptr<ChatLine>;
explicit ChatLine(QGraphicsScene* scene);
explicit ChatLine();
virtual ~ChatLine();
virtual QRectF boundingSceneRect() const;
void addColumn(ChatLineContent* item, ColumnFormat fmt);
void replaceContent(int col, ChatLineContent* lineContent);
void layout(qreal width, QPointF scenePos);
@ -80,9 +79,11 @@ public:
bool isOverSelection(QPointF scenePos);
void removeFromScene();
void addToScene(QGraphicsScene* scene);
private:
protected:
QPointF mapToContent(ChatLineContent* c, QPointF pos);
void addColumn(ChatLineContent* item, ColumnFormat fmt);
void updateBBox();
friend class ChatLog;
@ -91,7 +92,6 @@ private:
private:
int rowIndex = -1;
QGraphicsScene* scene = nullptr;
QVector<ChatLineContent*> content; // 3 columns
QVector<ColumnFormat> format;
qreal width;

View File

@ -76,54 +76,6 @@ ChatLog::~ChatLog()
}
ChatMessage::Ptr ChatLog::addChatMessage(const QString& sender, const QString &msg, bool self, bool alert)
{
ChatMessage::Ptr line = ChatMessage::createChatMessage(scene, sender, msg, false, alert, self);
insertChatline(std::dynamic_pointer_cast<ChatLine>(line));
return line;
}
ChatMessage::Ptr ChatLog::addChatMessage(const QString& sender, const QString& msg, const QDateTime& timestamp, bool self, bool alert)
{
ChatMessage::Ptr line = ChatMessage::createChatMessage(scene, sender, msg, false, alert, self, timestamp);
insertChatline(std::dynamic_pointer_cast<ChatLine>(line));
return line;
}
ChatMessage::Ptr ChatLog::addChatAction(const QString &sender, const QString &msg, const QDateTime &timestamp)
{
ChatMessage::Ptr line = ChatMessage::createChatMessage(scene, sender, msg, true, false, false, timestamp);
insertChatline(std::dynamic_pointer_cast<ChatLine>(line));
return line;
}
ChatMessage::Ptr ChatLog::addChatAction(const QString &sender, const QString &msg)
{
ChatMessage::Ptr line = ChatMessage::createChatMessage(scene, sender, msg, true, false, false);
insertChatline(std::dynamic_pointer_cast<ChatLine>(line));
return line;
}
ChatMessage::Ptr ChatLog::addSystemMessage(const QString &msg, const QDateTime& timestamp)
{
ChatMessage::Ptr line = ChatMessage::createChatInfoMessage(scene, msg, "", timestamp);
insertChatline(std::dynamic_pointer_cast<ChatLine>(line));
return line;
}
ChatMessage::Ptr ChatLog::addFileTransferMessage(const QString &sender, const ToxFile &file, const QDateTime& timestamp, bool self)
{
ChatMessage::Ptr line = ChatMessage::createFileTransferMessage(scene, sender, "", file, self, timestamp);
insertChatline(std::dynamic_pointer_cast<ChatLine>(line));
return line;
}
void ChatLog::clearSelection()
{
for(int i=selFirstRow; i<=selLastRow && i<lines.size() && i >= 0; ++i)
@ -403,6 +355,11 @@ void ChatLog::repositionDownTo(int start, qreal end)
void ChatLog::insertChatline(ChatLine::Ptr l)
{
if(!l.get())
return;
l->addToScene(scene);
stickToBtm = stickToBottom();
l->setRowIndex(lines.size());

View File

@ -37,16 +37,7 @@ public:
explicit ChatLog(QWidget* parent = 0);
virtual ~ChatLog();
ChatMessage::Ptr addChatMessage(const QString& sender, const QString& msg, const QDateTime& timestamp, bool self, bool alert);
ChatMessage::Ptr addChatMessage(const QString& sender, const QString& msg, bool self, bool alert);
ChatMessage::Ptr addChatAction(const QString& sender, const QString& msg, const QDateTime& timestamp);
ChatMessage::Ptr addChatAction(const QString& sender, const QString& msg);
ChatMessage::Ptr addSystemMessage(const QString& msg, const QDateTime& timestamp);
ChatMessage::Ptr addFileTransferMessage(const QString& sender, const ToxFile& file, const QDateTime &timestamp, bool self);
void insertChatline(ChatLine::Ptr l);
void clearSelection();
void clear();
void copySelectedText() const;

View File

@ -28,16 +28,14 @@
#define NAME_COL_WIDTH 75.0
#define TIME_COL_WIDTH 85.0
ChatMessage::ChatMessage(QGraphicsScene* scene, const QString& rawMessage)
: ChatLine(scene)
, rawString(rawMessage)
ChatMessage::ChatMessage()
{
}
ChatMessage::Ptr ChatMessage::createChatMessage(QGraphicsScene *scene, const QString &sender, const QString &rawMessage, bool isAction, bool alert, bool isMe, const QDateTime &date)
ChatMessage::Ptr ChatMessage::createChatMessage(const QString &sender, const QString &rawMessage, bool isAction, bool alert, bool isMe, const QDateTime &date)
{
ChatMessage::Ptr msg = ChatMessage::Ptr(new ChatMessage(scene, rawMessage));
ChatMessage::Ptr msg = ChatMessage::Ptr(new ChatMessage);
QString text = toHtmlChars(rawMessage);
@ -57,7 +55,7 @@ ChatMessage::Ptr ChatMessage::createChatMessage(QGraphicsScene *scene, const QSt
}
msg->addColumn(new Text(isAction ? "*" : sender, isMe ? Style::getFont(Style::BigBold) : Style::getFont(Style::Big), true), ColumnFormat(NAME_COL_WIDTH, ColumnFormat::FixedSize, ColumnFormat::Right));
msg->addColumn(new Text(text, Style::getFont(Style::Big)), ColumnFormat(1.0, ColumnFormat::VariableSize));
msg->addColumn(new Text(text, Style::getFont(Style::Big), false, rawMessage), ColumnFormat(1.0, ColumnFormat::VariableSize));
msg->addColumn(new Spinner(QSizeF(16, 16)), ColumnFormat(TIME_COL_WIDTH, ColumnFormat::FixedSize, ColumnFormat::Right));
if(!date.isNull())
@ -66,12 +64,12 @@ ChatMessage::Ptr ChatMessage::createChatMessage(QGraphicsScene *scene, const QSt
return msg;
}
ChatMessage::Ptr ChatMessage::createChatInfoMessage(QGraphicsScene *scene, const QString &rawMessage, const QString &type, const QDateTime &date)
ChatMessage::Ptr ChatMessage::createChatInfoMessage(const QString &rawMessage, SystemMessageType type, const QDateTime &date)
{
ChatMessage::Ptr msg = ChatMessage::Ptr(new ChatMessage(scene, rawMessage));
ChatMessage::Ptr msg = ChatMessage::Ptr(new ChatMessage);
msg->addColumn(new Image(QSizeF(16, 16), ":/ui/chatArea/info.png"), ColumnFormat(NAME_COL_WIDTH, ColumnFormat::FixedSize, ColumnFormat::Right));
msg->addColumn(new Text(rawMessage, Style::getFont(Style::Big)), ColumnFormat(1.0, ColumnFormat::VariableSize));
msg->addColumn(new Text(rawMessage, Style::getFont(Style::Big), false, rawMessage), ColumnFormat(1.0, ColumnFormat::VariableSize));
msg->addColumn(new Text(date.toString(Settings::getInstance().getTimestampFormat()), Style::getFont(Style::Big)), ColumnFormat(TIME_COL_WIDTH, ColumnFormat::FixedSize, ColumnFormat::Right));
Q_UNUSED(type)
@ -79,9 +77,9 @@ ChatMessage::Ptr ChatMessage::createChatInfoMessage(QGraphicsScene *scene, const
return msg;
}
ChatMessage::Ptr ChatMessage::createFileTransferMessage(QGraphicsScene* scene, const QString& sender, const QString& rawMessage, ToxFile file, bool isMe, const QDateTime& date)
ChatMessage::Ptr ChatMessage::createFileTransferMessage(const QString& sender, ToxFile file, bool isMe, const QDateTime& date)
{
ChatMessage::Ptr msg = ChatMessage::Ptr(new ChatMessage(scene, rawMessage));
ChatMessage::Ptr msg = ChatMessage::Ptr(new ChatMessage);
msg->addColumn(new Text(sender, isMe ? Style::getFont(Style::BigBold) : Style::getFont(Style::Big), true), ColumnFormat(NAME_COL_WIDTH, ColumnFormat::FixedSize, ColumnFormat::Right));
msg->addColumn(new ChatLineContentProxy(new FileTransferWidget(0, file), 380, 0.6f), ColumnFormat(1.0, ColumnFormat::VariableSize));
@ -98,7 +96,11 @@ void ChatMessage::markAsSent(const QDateTime &time)
QString ChatMessage::toString() const
{
return rawString;
ChatLineContent* c = getContent(1);
if(c)
return c->getText();
return QString();
}
bool ChatMessage::isAction() const
@ -111,6 +113,20 @@ void ChatMessage::setAsAction()
action = true;
}
void ChatMessage::hideSender()
{
ChatLineContent* c = getContent(0);
if(c)
c->hide();
}
void ChatMessage::hideDate()
{
ChatLineContent* c = getContent(2);
if(c)
c->hide();
}
QString ChatMessage::detectAnchors(const QString &str)
{
QString out = str;

View File

@ -28,16 +28,24 @@ class ChatMessage : public ChatLine
public:
using Ptr = std::shared_ptr<ChatMessage>;
ChatMessage(QGraphicsScene* scene, const QString& rawMessage);
enum SystemMessageType
{
INFO,
ERROR,
};
static ChatMessage::Ptr createChatMessage(QGraphicsScene* scene, const QString& sender, const QString& rawMessage, bool isAction, bool alert, bool isMe, const QDateTime& date = QDateTime());
static ChatMessage::Ptr createChatInfoMessage(QGraphicsScene* scene, const QString& rawMessage, const QString& type, const QDateTime& date);
static ChatMessage::Ptr createFileTransferMessage(QGraphicsScene* scene, const QString& sender, const QString& rawMessage, ToxFile file, bool isMe, const QDateTime& date);
ChatMessage();
static ChatMessage::Ptr createChatMessage(const QString& sender, const QString& rawMessage, bool isAction, bool alert, bool isMe, const QDateTime& date = QDateTime());
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);
void markAsSent(const QDateTime& time);
QString toString() const;
bool isAction() const;
void setAsAction();
void hideSender();
void hideDate();
protected:
static QString detectAnchors(const QString& str);
@ -45,8 +53,6 @@ protected:
static QString toHtmlChars(const QString& str);
private:
ChatLineContent* midColumn = nullptr;
QString rawString;
bool action = false;
};

View File

@ -29,10 +29,11 @@
#include <QRegExp>
#include <QDesktopServices>
Text::Text(const QString& txt, QFont font, bool enableElide)
Text::Text(const QString& txt, QFont font, bool enableElide, const QString &rwText)
: ChatLineContent()
, elide(enableElide)
, defFont(font)
, rawText(rwText)
{
setText(txt);
setAcceptedMouseButtons(Qt::LeftButton | Qt::RightButton);
@ -197,7 +198,7 @@ void Text::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
QString Text::getText() const
{
return text;
return rawText;
}
void Text::ensureIntegrity()

View File

@ -27,7 +27,7 @@ class CustomTextDocument;
class Text : public ChatLineContent
{
public:
Text(const QString& txt = "", QFont font = QFont(), bool enableElide = false);
Text(const QString& txt = "", QFont font = QFont(), bool enableElide = false, const QString& rawText = "");
virtual ~Text();
void setText(const QString& txt);
@ -64,6 +64,7 @@ protected:
private:
CustomTextDocument* doc = nullptr;
QString text;
QString rawText;
QString elidedText;
QString selectedText;
QSizeF size;

View File

@ -40,6 +40,7 @@
#include "src/misc/cstring.h"
#include "src/chatlog/chatmessage.h"
#include "src/chatlog/content/filetransferwidget.h"
#include "src/chatlog/chatlog.h"
ChatForm::ChatForm(Friend* chatFriend)
: f(chatFriend)
@ -172,8 +173,7 @@ void ChatForm::startFileSend(ToxFile file)
previousId = core->getSelfId();
}
chatWidget->addFileTransferMessage(name, file, QDateTime::currentDateTime(), true);
insertChatMessage(ChatMessage::createFileTransferMessage(name, file, true, QDateTime::currentDateTime()));
}
void ChatForm::onFileRecvRequest(ToxFile file)
@ -197,7 +197,9 @@ void ChatForm::onFileRecvRequest(ToxFile file)
previousId = friendId;
}
ChatMessage::Ptr msg = chatWidget->addFileTransferMessage(name, file, QDateTime::currentDateTime(), false);
ChatMessage::Ptr msg = ChatMessage::createFileTransferMessage(name, file, false, QDateTime::currentDateTime());
insertChatMessage(msg);
if (!Settings::getInstance().getAutoAcceptDir(f->getToxID()).isEmpty()
|| Settings::getInstance().getAutoSaveEnabled())
{
@ -233,7 +235,7 @@ void ChatForm::onAvInvite(int FriendId, int CallId, bool video)
connect(callButton, SIGNAL(clicked()), this, SLOT(onAnswerCallTriggered()));
}
chatWidget->addSystemMessage(tr("%1 calling").arg(f->getDisplayedName()), QDateTime::currentDateTime());
insertChatMessage(ChatMessage::createChatInfoMessage(tr("%1 calling").arg(f->getDisplayedName()), ChatMessage::INFO, QDateTime::currentDateTime()));
Widget* w = Widget::getInstance();
if (!w->isFriendWidgetCurActiveWidget(f)|| w->isMinimized() || !w->isActiveWindow())
@ -304,7 +306,7 @@ void ChatForm::onAvCancel(int FriendId, int)
netcam->hide();
addSystemInfoMessage(tr("%1 stopped calling").arg(f->getDisplayedName()), "white", QDateTime::currentDateTime());
addSystemInfoMessage(tr("%1 stopped calling").arg(f->getDisplayedName()), ChatMessage::INFO, QDateTime::currentDateTime());
}
void ChatForm::onAvEnd(int FriendId, int)
@ -360,7 +362,7 @@ void ChatForm::onAvRinging(int FriendId, int CallId, bool video)
connect(callButton, SIGNAL(clicked()), this, SLOT(onCancelCallTriggered()));
}
addSystemInfoMessage(tr("Calling to %1").arg(f->getDisplayedName()), "white", QDateTime::currentDateTime());
addSystemInfoMessage(tr("Calling to %1").arg(f->getDisplayedName()), ChatMessage::INFO, QDateTime::currentDateTime());
}
void ChatForm::onAvStarting(int FriendId, int CallId, bool video)
@ -501,7 +503,7 @@ void ChatForm::onAvRejected(int FriendId, int)
connect(callButton, SIGNAL(clicked()), this, SLOT(onCallTriggered()));
connect(videoButton, SIGNAL(clicked()), this, SLOT(onVideoCallTriggered()));
chatWidget->addSystemMessage(tr("Call rejected"), QDateTime::currentDateTime());
insertChatMessage(ChatMessage::createChatInfoMessage(tr("Call rejected"), ChatMessage::INFO, QDateTime::currentDateTime()));
netcam->hide();
}
@ -640,7 +642,7 @@ void ChatForm::onFileSendFailed(int FriendId, const QString &fname)
if (FriendId != f->getFriendID())
return;
addSystemInfoMessage(tr("Failed to send file \"%1\"").arg(fname), "red", QDateTime::currentDateTime());
addSystemInfoMessage(tr("Failed to send file \"%1\"").arg(fname), ChatMessage::ERROR, QDateTime::currentDateTime());
}
void ChatForm::onAvatarChange(int FriendId, const QPixmap &pic)
@ -712,7 +714,7 @@ void ChatForm::loadHistory(QDateTime since, bool processUndelivered)
if (msgDate > lastDate)
{
lastDate = msgDate;
chatWidget->addSystemMessage(msgDate.toString(), QDateTime());
insertChatMessage(ChatMessage::createChatInfoMessage(msgDate.toString(), ChatMessage::INFO, QDateTime::currentDateTime()));
}
// Show each messages
@ -784,7 +786,7 @@ void ChatForm::stopCounter()
if (timer)
{
addSystemInfoMessage(tr("Call with %1 ended. %2").arg(f->getDisplayedName(),secondsToDHMS(timeElapsed.elapsed()/1000)),
"white", QDateTime::currentDateTime());
ChatMessage::INFO, QDateTime::currentDateTime());
timer->stop();
callDuration->setText("");
callDuration->hide();

View File

@ -30,8 +30,7 @@
#include "src/group.h"
#include "src/friendlist.h"
#include "src/friend.h"
#include "src/chatlog/content/text.h"
#include "src/chatlog/chatmessage.h"
#include "src/chatlog/chatlog.h"
GenericChatForm::GenericChatForm(QWidget *parent) :
QWidget(parent),
@ -198,9 +197,17 @@ ChatMessage::Ptr GenericChatForm::addMessage(const ToxID& author, const QString
ChatMessage::Ptr msg;
if(isAction)
msg = chatWidget->addChatAction(authorStr, message);
{
msg = ChatMessage::createChatMessage(authorStr, message, true, false, false);
}
else
msg = chatWidget->addChatMessage(author != previousId ? authorStr : QString(), message, author.isMine(), false);
{
msg = ChatMessage::createChatMessage(authorStr, message, false, false, author.isMine());
if(author == previousId)
msg->hideSender();
}
insertChatMessage(msg);
if(isSent)
msg->markAsSent(datetime);
@ -219,7 +226,11 @@ ChatMessage::Ptr GenericChatForm::addSelfMessage(const QString &message, bool is
void GenericChatForm::addAlertMessage(const ToxID &author, QString message, QDateTime datetime)
{
QString authorStr = resolveToxID(author);
chatWidget->addChatMessage(author != previousId ? authorStr : QString(), message, datetime, author.isMine(), true);
ChatMessage::Ptr msg = ChatMessage::createChatMessage(authorStr, message, false, true, author.isMine(), datetime);
insertChatMessage(msg);
if(author == previousId)
msg->hideSender();
previousId = author;
}
@ -275,13 +286,10 @@ void GenericChatForm::focusInput()
msgEdit->setFocus();
}
void GenericChatForm::addSystemInfoMessage(const QString &message, const QString &type, const QDateTime &datetime)
void GenericChatForm::addSystemInfoMessage(const QString &message, ChatMessage::SystemMessageType type, const QDateTime &datetime)
{
//TODO: respect type
previousId.clear();
chatWidget->addSystemMessage(message, datetime);
Q_UNUSED(type)
insertChatMessage(ChatMessage::createChatInfoMessage(message, type, datetime));
}
void GenericChatForm::clearChatArea(bool notinform)
@ -290,7 +298,7 @@ void GenericChatForm::clearChatArea(bool notinform)
previousId = ToxID();
if (!notinform)
addSystemInfoMessage(tr("Cleared"), "white", QDateTime::currentDateTime());
addSystemInfoMessage(tr("Cleared"), ChatMessage::INFO, QDateTime::currentDateTime());
if (earliestMessage)
{
@ -318,3 +326,8 @@ QString GenericChatForm::resolveToxID(const ToxID &id)
return QString();
}
void GenericChatForm::insertChatMessage(ChatMessage::Ptr msg)
{
chatWidget->insertChatline(std::dynamic_pointer_cast<ChatLine>(msg));
}

View File

@ -22,7 +22,7 @@
#include <QDateTime>
#include <QMenu>
#include "src/corestructs.h"
#include "src/chatlog/chatlog.h"
#include "src/chatlog/chatmessage.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?
@ -33,7 +33,6 @@ class QPushButton;
class CroppingLabel;
class ChatTextEdit;
class ChatLog;
class ChatMessage;
class MaskablePixmapWidget;
struct ToxID;
@ -53,7 +52,7 @@ public:
ChatMessage::Ptr addMessage(const ToxID& author, const QString &message, bool isAction, const QDateTime &datetime, bool isSent);
ChatMessage::Ptr addSelfMessage(const QString &message, bool isAction, const QDateTime &datetime, bool isSent);
void addSystemInfoMessage(const QString &message, const QString &type, const QDateTime &datetime);
void addSystemInfoMessage(const QString &message, ChatMessage::SystemMessageType type, const QDateTime &datetime);
void addAlertMessage(const ToxID& author, QString message, QDateTime datetime);
bool isEmpty();
@ -77,6 +76,7 @@ protected slots:
protected:
QString resolveToxID(const ToxID &id);
void insertChatMessage(ChatMessage::Ptr msg);
ToxID previousId;
QMenu menu;

View File

@ -749,7 +749,7 @@ void Widget::onFriendStatusChanged(int friendId, Status status)
}
if (isActualChange)
f->getChatForm()->addSystemInfoMessage(tr("%1 is now %2", "e.g. \"Dubslow is now online\"").arg(f->getDisplayedName()).arg(fStatus),
"white", QDateTime::currentDateTime());
ChatMessage::INFO, QDateTime::currentDateTime());
}
if (isActualChange && status != Status::Offline)
@ -1011,7 +1011,7 @@ void Widget::onGroupTitleChanged(int groupnumber, const QString& author, const Q
g->setName(title);
if (!author.isEmpty())
g->getChatForm()->addSystemInfoMessage(tr("%1 has set the title to %2").arg(author, title), "silver", QDateTime::currentDateTime());
g->getChatForm()->addSystemInfoMessage(tr("%1 has set the title to %2").arg(author, title), ChatMessage::INFO, QDateTime::currentDateTime());
}
void Widget::removeGroup(Group* g, bool fake)
@ -1162,7 +1162,7 @@ void Widget::onGroupSendResult(int groupId, const QString& message, int result)
return;
if (result == -1)
g->getChatForm()->addSystemInfoMessage(tr("Message failed to send"), "white", QDateTime::currentDateTime());
g->getChatForm()->addSystemInfoMessage(tr("Message failed to send"), ChatMessage::INFO, QDateTime::currentDateTime());
}
void Widget::getPassword(QString info, int passtype, uint8_t* salt)