From 808f8b1cc58d73fc4101be3193d6f47ba57f45ae Mon Sep 17 00:00:00 2001 From: krepa098 Date: Thu, 13 Nov 2014 18:27:32 +0100 Subject: [PATCH] progress --- src/chatlog/chatline.cpp | 26 ++++++++++++++------ src/chatlog/chatlinecontent.cpp | 5 ++++ src/chatlog/chatlinecontent.h | 2 ++ src/chatlog/chatmessage.cpp | 7 ++++++ src/chatlog/chatmessage.h | 4 +++ src/chatlog/content/text.cpp | 16 ++++++------ src/chatlog/content/text.h | 2 +- src/misc/smileypack.cpp | 2 +- src/widget/form/chatform.cpp | 38 ++++++++++++++--------------- src/widget/form/genericchatform.cpp | 9 +++++-- src/widget/widget.cpp | 4 +-- 11 files changed, 75 insertions(+), 40 deletions(-) diff --git a/src/chatlog/chatline.cpp b/src/chatlog/chatline.cpp index 39254da0f..cad1b0f82 100644 --- a/src/chatlog/chatline.cpp +++ b/src/chatlog/chatline.cpp @@ -111,6 +111,10 @@ void ChatLine::replaceContent(int col, ChatLineContent *lineContent) content[col] = lineContent; 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 width = format[i].size / varWidth * leftover; - // set the width of the current column as - // firstLineVOffset() may depend on the current width + // set the width of the current column 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 qreal xAlign = 0.0; switch(format[i].hAlign) @@ -169,11 +167,23 @@ void ChatLine::layout(qreal w, QPointF scenePos) } // reposition - content[i]->setPos(pos.x() + xOffset + xAlign, pos.y() + yOffset); + content[i]->setPos(pos.x() + xOffset + xAlign, pos.y()); 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(); } diff --git a/src/chatlog/chatlinecontent.cpp b/src/chatlog/chatlinecontent.cpp index 1cfc61dc6..de5a8358e 100644 --- a/src/chatlog/chatlinecontent.cpp +++ b/src/chatlog/chatlinecontent.cpp @@ -68,3 +68,8 @@ void ChatLineContent::visibilityChanged(bool) { } + +QString ChatLineContent::toString() const +{ + return QString(); +} diff --git a/src/chatlog/chatlinecontent.h b/src/chatlog/chatlinecontent.h index 32d08d7c8..64e0ef5f6 100644 --- a/src/chatlog/chatlinecontent.h +++ b/src/chatlog/chatlinecontent.h @@ -36,6 +36,8 @@ public: virtual void visibilityChanged(bool visible); + virtual QString toString() const; + private: friend class ChatLine; diff --git a/src/chatlog/chatmessage.cpp b/src/chatlog/chatmessage.cpp index 1136d453c..c241ea4e8 100644 --- a/src/chatlog/chatmessage.cpp +++ b/src/chatlog/chatmessage.cpp @@ -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(content, ColumnFormat(1.0, ColumnFormat::VariableSize)); addColumn(new Spinner(QSizeF(16, 16)), ColumnFormat(50.0, ColumnFormat::FixedSize, 1, ColumnFormat::Right)); + + midColumn = content; } void ChatMessage::markAsSent(const QDateTime &time) @@ -17,3 +19,8 @@ void ChatMessage::markAsSent(const QDateTime &time) // remove the spinner and replace it by $time replaceContent(2, new Text(time.toString("hh:mm"), true)); } + +QString ChatMessage::toString() const +{ + return midColumn->toString(); +} diff --git a/src/chatlog/chatmessage.h b/src/chatlog/chatmessage.h index f61b744b5..dee1714cc 100644 --- a/src/chatlog/chatmessage.h +++ b/src/chatlog/chatmessage.h @@ -11,6 +11,10 @@ public: ChatMessage(QGraphicsScene* scene, const QString &author, ChatLineContent* content); void markAsSent(const QDateTime& time); + QString toString() const; + +private: + ChatLineContent* midColumn = nullptr; }; #endif // CHATMESSAGE_H diff --git a/src/chatlog/content/text.cpp b/src/chatlog/content/text.cpp index 37e0ed74c..4b17526aa 100644 --- a/src/chatlog/content/text.cpp +++ b/src/chatlog/content/text.cpp @@ -11,14 +11,15 @@ #include #include -int Text::count = 0; - Text::Text(const QString& txt, bool enableElide) : ChatLineContent() , elide(enableElide) { setText(txt); setAcceptedMouseButtons(Qt::LeftButton | Qt::RightButton); + + ensureIntegrity(); + freeResources(); //setCacheMode(QGraphicsItem::DeviceCoordinateCache); } @@ -161,6 +162,11 @@ void Text::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) QDesktopServices::openUrl(anchor); } +QString Text::toString() const +{ + return text; +} + void Text::ensureIntegrity() { if(!doc) @@ -179,18 +185,16 @@ void Text::ensureIntegrity() opt.setWrapMode(QTextOption::NoWrap); doc->setDefaultTextOption(opt); doc->setPlainText(elidedText); - } cursor = QTextCursor(doc); - count++; } doc->setTextWidth(width); doc->documentLayout()->update(); if(doc->firstBlock().layout()->lineCount() > 0) - vOffset = doc->firstBlock().layout()->lineAt(0).ascent(); + vOffset = doc->firstBlock().layout()->lineAt(0).height(); if(size != idealSize()) { @@ -206,8 +210,6 @@ void Text::freeResources() delete doc; doc = nullptr; cursor = QTextCursor(); - - count--; } } diff --git a/src/chatlog/content/text.h b/src/chatlog/content/text.h index edcdd486d..22e75ac70 100644 --- a/src/chatlog/content/text.h +++ b/src/chatlog/content/text.h @@ -36,7 +36,7 @@ public: virtual void mousePressEvent(QGraphicsSceneMouseEvent *event); virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); - static int count; + virtual QString toString() const; protected: // dynamic resource management diff --git a/src/misc/smileypack.cpp b/src/misc/smileypack.cpp index 1ec70f954..152a391d5 100644 --- a/src/misc/smileypack.cpp +++ b/src/misc/smileypack.cpp @@ -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 // We can't remove it because Qt doesn't support CSS display or vertical-align int fontHeight = QFontInfo(Style::getFont(Style::Big)).pixelSize() - 1; - QSize size(fontHeight, fontHeight); + QSize size(16, 16); QString filename = QDir(path).filePath(name); QImage img(filename); diff --git a/src/widget/form/chatform.cpp b/src/widget/form/chatform.cpp index 0245e8343..e12cc9d60 100644 --- a/src/widget/form/chatform.cpp +++ b/src/widget/form/chatform.cpp @@ -732,7 +732,7 @@ void ChatForm::loadHistory(QDateTime since, bool processUndelivered) ToxID storedPrevId; std::swap(storedPrevId, previousId); - //QList historyMessages; +// QList historyMessages; // QDate lastDate(1,0,0); // for (const auto &it : msgs) @@ -743,11 +743,11 @@ void ChatForm::loadHistory(QDateTime since, bool processUndelivered) // if (msgDate > lastDate) // { // lastDate = msgDate; -// historyMessages.append(genSystemInfoAction(msgDate.toString(),"",QDateTime())); +// //TODO: historyMessages.append(genSystemInfoAction(msgDate.toString(),"",QDateTime())); // } // // 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) // { // ca->markAsSent(); @@ -869,26 +869,26 @@ void ChatForm::dischargeReceipt(int receipt) void ChatForm::clearReciepts() { -// receipts.clear(); -// undeliveredMsgs.clear(); + receipts.clear(); + undeliveredMsgs.clear(); } void ChatForm::deliverOfflineMsgs() { -// if (!Settings::getInstance().getFauxOfflineMessaging()) -// return; + if (!Settings::getInstance().getFauxOfflineMessaging()) + return; -// QMap msgs = undeliveredMsgs; -// clearReciepts(); + QMap msgs = undeliveredMsgs; + clearReciepts(); -// for (auto iter = msgs.begin(); iter != msgs.end(); iter++) -// { -// QString messageText = iter.value()->getRawMessage(); -// int rec; -// if (iter.value()->isAction()) -// rec = Core::getInstance()->sendAction(f->getFriendID(), messageText); -// else -// rec = Core::getInstance()->sendMessage(f->getFriendID(), messageText); -// registerReceipt(rec, iter.key(), iter.value()); -// } + for (auto iter = msgs.begin(); iter != msgs.end(); iter++) + { + QString messageText = iter.value()->toString(); + int rec; + //if (iter.value()->isAction()) + ;//TODO: rec = Core::getInstance()->sendAction(f->getFriendID(), messageText); + //else + rec = Core::getInstance()->sendMessage(f->getFriendID(), messageText); + registerReceipt(rec, iter.key(), iter.value()); + } } diff --git a/src/widget/form/genericchatform.cpp b/src/widget/form/genericchatform.cpp index adbf37767..5180424fd 100644 --- a/src/widget/form/genericchatform.cpp +++ b/src/widget/form/genericchatform.cpp @@ -29,6 +29,7 @@ #include "src/friendlist.h" #include "src/friend.h" #include "src/chatlog/content/text.h" +#include "src/chatlog/chatmessage.h" GenericChatForm::GenericChatForm(QWidget *parent) : QWidget(parent), @@ -205,12 +206,16 @@ void GenericChatForm::onSaveLogClicked() ChatMessage* GenericChatForm::addMessage(const ToxID& author, const QString &message, bool isAction, 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) { - return chatWidget->addChatMessage(Core::getInstance()->getUsername(), new Text(message)); + return chatWidget->addChatMessage(Core::getInstance()->getUsername(), new Text(SmileyPack::getInstance().smileyfied(message))); } /** diff --git a/src/widget/widget.cpp b/src/widget/widget.cpp index 7623baa25..48c3b3024 100644 --- a/src/widget/widget.cpp +++ b/src/widget/widget.cpp @@ -743,7 +743,7 @@ void Widget::onFriendMessageReceived(int friendId, const QString& message, bool return; 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) 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) return; - //TODO: f->getChatForm()->dischargeReceipt(receipt); + f->getChatForm()->dischargeReceipt(receipt); } void Widget::newMessageAlert(GenericChatroomWidget* chat)