1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00
This commit is contained in:
krepa098 2014-11-13 18:27:32 +01:00
parent 3a34678900
commit 808f8b1cc5
11 changed files with 75 additions and 40 deletions

View File

@ -111,6 +111,10 @@ void ChatLine::replaceContent(int col, ChatLineContent *lineContent)
content[col] = lineContent; content[col] = lineContent;
scene->addItem(content[col]); scene->addItem(content[col]);
layout(width, pos);
content[col]->visibilityChanged(isVisible);
content[col]->update();
} }
} }
@ -145,15 +149,9 @@ void ChatLine::layout(qreal w, QPointF scenePos)
else else
width = format[i].size / varWidth * leftover; width = format[i].size / varWidth * leftover;
// set the width of the current column as // set the width of the current column
// firstLineVOffset() may depend on the current width
content[i]->setWidth(width); content[i]->setWidth(width);
// calculate vertical alignment
qreal yOffset = 0.0;
if(format[i].vAlignCol >= 0 && format[i].vAlignCol < content.size())
yOffset = content[format[i].vAlignCol]->firstLineVOffset() - content[i]->firstLineVOffset();
// calculate horizontal alignment // calculate horizontal alignment
qreal xAlign = 0.0; qreal xAlign = 0.0;
switch(format[i].hAlign) switch(format[i].hAlign)
@ -169,11 +167,23 @@ void ChatLine::layout(qreal w, QPointF scenePos)
} }
// reposition // reposition
content[i]->setPos(pos.x() + xOffset + xAlign, pos.y() + yOffset); content[i]->setPos(pos.x() + xOffset + xAlign, pos.y());
xOffset += width; xOffset += width;
} }
for(int i = 0; i < content.size(); ++i)
{
// calculate vertical alignment
// vertical alignment may depend on width, so we do it in a second pass
qreal yOffset = 0.0;
if(format[i].vAlignCol >= 0 && format[i].vAlignCol < content.size())
yOffset = content[format[i].vAlignCol]->firstLineVOffset() - content[i]->firstLineVOffset();
// reposition
content[i]->setPos(content[i]->pos().x(), content[i]->pos().y() + yOffset);
}
updateBBox(); updateBBox();
} }

View File

@ -68,3 +68,8 @@ void ChatLineContent::visibilityChanged(bool)
{ {
} }
QString ChatLineContent::toString() const
{
return QString();
}

View File

@ -36,6 +36,8 @@ public:
virtual void visibilityChanged(bool visible); virtual void visibilityChanged(bool visible);
virtual QString toString() const;
private: private:
friend class ChatLine; friend class ChatLine;

View File

@ -10,6 +10,8 @@ ChatMessage::ChatMessage(QGraphicsScene *scene, const QString& author ,ChatLineC
addColumn(new Text(author, true), ColumnFormat(75.0, ColumnFormat::FixedSize, 1, ColumnFormat::Right)); addColumn(new Text(author, true), ColumnFormat(75.0, ColumnFormat::FixedSize, 1, ColumnFormat::Right));
addColumn(content, ColumnFormat(1.0, ColumnFormat::VariableSize)); addColumn(content, ColumnFormat(1.0, ColumnFormat::VariableSize));
addColumn(new Spinner(QSizeF(16, 16)), ColumnFormat(50.0, ColumnFormat::FixedSize, 1, ColumnFormat::Right)); addColumn(new Spinner(QSizeF(16, 16)), ColumnFormat(50.0, ColumnFormat::FixedSize, 1, ColumnFormat::Right));
midColumn = content;
} }
void ChatMessage::markAsSent(const QDateTime &time) void ChatMessage::markAsSent(const QDateTime &time)
@ -17,3 +19,8 @@ void ChatMessage::markAsSent(const QDateTime &time)
// remove the spinner and replace it by $time // remove the spinner and replace it by $time
replaceContent(2, new Text(time.toString("hh:mm"), true)); replaceContent(2, new Text(time.toString("hh:mm"), true));
} }
QString ChatMessage::toString() const
{
return midColumn->toString();
}

View File

@ -11,6 +11,10 @@ public:
ChatMessage(QGraphicsScene* scene, const QString &author, ChatLineContent* content); ChatMessage(QGraphicsScene* scene, const QString &author, ChatLineContent* content);
void markAsSent(const QDateTime& time); void markAsSent(const QDateTime& time);
QString toString() const;
private:
ChatLineContent* midColumn = nullptr;
}; };
#endif // CHATMESSAGE_H #endif // CHATMESSAGE_H

View File

@ -11,14 +11,15 @@
#include <QFontMetrics> #include <QFontMetrics>
#include <QDesktopServices> #include <QDesktopServices>
int Text::count = 0;
Text::Text(const QString& txt, bool enableElide) Text::Text(const QString& txt, bool enableElide)
: ChatLineContent() : ChatLineContent()
, elide(enableElide) , elide(enableElide)
{ {
setText(txt); setText(txt);
setAcceptedMouseButtons(Qt::LeftButton | Qt::RightButton); setAcceptedMouseButtons(Qt::LeftButton | Qt::RightButton);
ensureIntegrity();
freeResources();
//setCacheMode(QGraphicsItem::DeviceCoordinateCache); //setCacheMode(QGraphicsItem::DeviceCoordinateCache);
} }
@ -161,6 +162,11 @@ void Text::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
QDesktopServices::openUrl(anchor); QDesktopServices::openUrl(anchor);
} }
QString Text::toString() const
{
return text;
}
void Text::ensureIntegrity() void Text::ensureIntegrity()
{ {
if(!doc) if(!doc)
@ -179,18 +185,16 @@ void Text::ensureIntegrity()
opt.setWrapMode(QTextOption::NoWrap); opt.setWrapMode(QTextOption::NoWrap);
doc->setDefaultTextOption(opt); doc->setDefaultTextOption(opt);
doc->setPlainText(elidedText); doc->setPlainText(elidedText);
} }
cursor = QTextCursor(doc); cursor = QTextCursor(doc);
count++;
} }
doc->setTextWidth(width); doc->setTextWidth(width);
doc->documentLayout()->update(); doc->documentLayout()->update();
if(doc->firstBlock().layout()->lineCount() > 0) if(doc->firstBlock().layout()->lineCount() > 0)
vOffset = doc->firstBlock().layout()->lineAt(0).ascent(); vOffset = doc->firstBlock().layout()->lineAt(0).height();
if(size != idealSize()) if(size != idealSize())
{ {
@ -206,8 +210,6 @@ void Text::freeResources()
delete doc; delete doc;
doc = nullptr; doc = nullptr;
cursor = QTextCursor(); cursor = QTextCursor();
count--;
} }
} }

View File

@ -36,7 +36,7 @@ public:
virtual void mousePressEvent(QGraphicsSceneMouseEvent *event); virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
static int count; virtual QString toString() const;
protected: protected:
// dynamic resource management // dynamic resource management

View File

@ -198,7 +198,7 @@ void SmileyPack::cacheSmiley(const QString &name)
// The -1 is to avoid having the space for descenders under images move the text down // The -1 is to avoid having the space for descenders under images move the text down
// We can't remove it because Qt doesn't support CSS display or vertical-align // We can't remove it because Qt doesn't support CSS display or vertical-align
int fontHeight = QFontInfo(Style::getFont(Style::Big)).pixelSize() - 1; int fontHeight = QFontInfo(Style::getFont(Style::Big)).pixelSize() - 1;
QSize size(fontHeight, fontHeight); QSize size(16, 16);
QString filename = QDir(path).filePath(name); QString filename = QDir(path).filePath(name);
QImage img(filename); QImage img(filename);

View File

@ -732,7 +732,7 @@ void ChatForm::loadHistory(QDateTime since, bool processUndelivered)
ToxID storedPrevId; ToxID storedPrevId;
std::swap(storedPrevId, previousId); std::swap(storedPrevId, previousId);
//QList<ChatActionPtr> historyMessages; // QList<ChatMessage*> historyMessages;
// QDate lastDate(1,0,0); // QDate lastDate(1,0,0);
// for (const auto &it : msgs) // for (const auto &it : msgs)
@ -743,11 +743,11 @@ void ChatForm::loadHistory(QDateTime since, bool processUndelivered)
// if (msgDate > lastDate) // if (msgDate > lastDate)
// { // {
// lastDate = msgDate; // lastDate = msgDate;
// historyMessages.append(genSystemInfoAction(msgDate.toString(),"",QDateTime())); // //TODO: historyMessages.append(genSystemInfoAction(msgDate.toString(),"",QDateTime()));
// } // }
// // Show each messages // // Show each messages
// MessageActionPtr ca = genMessageActionAction(ToxID::fromString(it.sender), it.message, false, msgDateTime); // ChatMessage* ca = genMessageActionAction(ToxID::fromString(it.sender), it.message, false, msgDateTime);
// if (it.isSent) // if (it.isSent)
// { // {
// ca->markAsSent(); // ca->markAsSent();
@ -869,26 +869,26 @@ void ChatForm::dischargeReceipt(int receipt)
void ChatForm::clearReciepts() void ChatForm::clearReciepts()
{ {
// receipts.clear(); receipts.clear();
// undeliveredMsgs.clear(); undeliveredMsgs.clear();
} }
void ChatForm::deliverOfflineMsgs() void ChatForm::deliverOfflineMsgs()
{ {
// if (!Settings::getInstance().getFauxOfflineMessaging()) if (!Settings::getInstance().getFauxOfflineMessaging())
// return; return;
// QMap<int, MessageActionPtr> msgs = undeliveredMsgs; QMap<int, ChatMessage*> msgs = undeliveredMsgs;
// clearReciepts(); clearReciepts();
// for (auto iter = msgs.begin(); iter != msgs.end(); iter++) for (auto iter = msgs.begin(); iter != msgs.end(); iter++)
// { {
// QString messageText = iter.value()->getRawMessage(); QString messageText = iter.value()->toString();
// int rec; int rec;
// if (iter.value()->isAction()) //if (iter.value()->isAction())
// rec = Core::getInstance()->sendAction(f->getFriendID(), messageText); ;//TODO: rec = Core::getInstance()->sendAction(f->getFriendID(), messageText);
// else //else
// rec = Core::getInstance()->sendMessage(f->getFriendID(), messageText); rec = Core::getInstance()->sendMessage(f->getFriendID(), messageText);
// registerReceipt(rec, iter.key(), iter.value()); registerReceipt(rec, iter.key(), iter.value());
// } }
} }

View File

@ -29,6 +29,7 @@
#include "src/friendlist.h" #include "src/friendlist.h"
#include "src/friend.h" #include "src/friend.h"
#include "src/chatlog/content/text.h" #include "src/chatlog/content/text.h"
#include "src/chatlog/chatmessage.h"
GenericChatForm::GenericChatForm(QWidget *parent) : GenericChatForm::GenericChatForm(QWidget *parent) :
QWidget(parent), QWidget(parent),
@ -205,12 +206,16 @@ void GenericChatForm::onSaveLogClicked()
ChatMessage* GenericChatForm::addMessage(const ToxID& author, const QString &message, bool isAction, ChatMessage* GenericChatForm::addMessage(const ToxID& author, const QString &message, bool isAction,
const QDateTime &datetime, bool isSent) const QDateTime &datetime, bool isSent)
{ {
return chatWidget->addChatMessage(Core::getInstance()->getPeerName(author), new Text(message), datetime); ChatMessage* msg = chatWidget->addChatMessage(Core::getInstance()->getPeerName(author), new Text(SmileyPack::getInstance().smileyfied(message)), datetime);
if(isSent)
msg->markAsSent(datetime);
return msg;
} }
ChatMessage* GenericChatForm::addSelfMessage(const QString &message, bool isAction, const QDateTime &datetime, bool isSent) ChatMessage* GenericChatForm::addSelfMessage(const QString &message, bool isAction, const QDateTime &datetime, bool isSent)
{ {
return chatWidget->addChatMessage(Core::getInstance()->getUsername(), new Text(message)); return chatWidget->addChatMessage(Core::getInstance()->getUsername(), new Text(SmileyPack::getInstance().smileyfied(message)));
} }
/** /**

View File

@ -743,7 +743,7 @@ void Widget::onFriendMessageReceived(int friendId, const QString& message, bool
return; return;
QDateTime timestamp = QDateTime::currentDateTime(); QDateTime timestamp = QDateTime::currentDateTime();
//TODO: f->getChatForm()->addMessage(f->getToxID(), message, isAction, timestamp, true); f->getChatForm()->addMessage(f->getToxID(), message, isAction, timestamp, true);
if (isAction) if (isAction)
HistoryKeeper::getInstance()->addChatEntry(f->getToxID().publicKey, "/me " + message, f->getToxID().publicKey, timestamp, true); HistoryKeeper::getInstance()->addChatEntry(f->getToxID().publicKey, "/me " + message, f->getToxID().publicKey, timestamp, true);
@ -773,7 +773,7 @@ void Widget::onReceiptRecieved(int friendId, int receipt)
if (!f) if (!f)
return; return;
//TODO: f->getChatForm()->dischargeReceipt(receipt); f->getChatForm()->dischargeReceipt(receipt);
} }
void Widget::newMessageAlert(GenericChatroomWidget* chat) void Widget::newMessageAlert(GenericChatroomWidget* chat)