1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00
This commit is contained in:
Tux3 / Mlkj / !Lev.uXFMLA 2014-09-10 18:30:03 +02:00
parent 5773996e15
commit 7f2edfae7d
7 changed files with 68 additions and 51 deletions

View File

@ -23,6 +23,7 @@
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QDateTime>
#include <QTextCursor>
#include "core.h"
@ -35,6 +36,7 @@ public:
explicit FileTransferInstance(ToxFile File);
QString getHtmlImage();
uint getId(){return id;}
void setTextCursor(QTextCursor cursor);
public slots:
void onFileTransferInfo(int FriendId, int FileNum, int64_t Filesize, int64_t BytesSent, ToxFile::FileDirection Direction);

View File

@ -15,9 +15,11 @@
*/
#include "chatareawidget.h"
#include "widget/tool/chataction.h"
#include <QAbstractTextDocumentLayout>
#include <QMessageBox>
#include <QScrollBar>
#include <QDebug>
ChatAreaWidget::ChatAreaWidget(QWidget *parent) :
QTextEdit(parent)
@ -30,8 +32,9 @@ ChatAreaWidget::ChatAreaWidget(QWidget *parent) :
ChatAreaWidget::~ChatAreaWidget()
{
for (ChatAction *it : messages)
delete it;
for (ChatAction* action : messages)
delete action;
messages.clear();
}
void ChatAreaWidget::mouseReleaseEvent(QMouseEvent * event)
@ -65,50 +68,18 @@ void ChatAreaWidget::mouseReleaseEvent(QMouseEvent * event)
}
}
QString ChatAreaWidget::getHtmledMessages()
{
QString res("<table width=100%>\n");
for (ChatAction *it : messages)
{
res += it->getHtml();
}
res += "</table>";
return res;
}
void ChatAreaWidget::insertMessage(ChatAction* msgAction)
{
if (msgAction == nullptr)
return;
messages.append(msgAction);
//updateChatContent();
moveCursor(QTextCursor::End);
moveCursor(QTextCursor::PreviousCell);
QTextCursor cur = textCursor();
cur.clearSelection();
cur.setKeepPositionOnInsert(true);
insertHtml(msgAction->getHtml());
msgAction->setTextCursor(cur);
//delete msgAction;
}
void ChatAreaWidget::updateChatContent()
{
QScrollBar* scroll = verticalScrollBar();
lockSliderToBottom = scroll && scroll->value() == scroll->maximum();
setUpdatesEnabled(false);
setHtml(getHtmledMessages());
setUpdatesEnabled(true);
if (lockSliderToBottom)
sliderPosition = scroll->maximum();
scroll->setValue(sliderPosition);
}
void ChatAreaWidget::clearMessages()
{
for (ChatAction *it : messages)
delete it;
updateChatContent();
messages.append(msgAction);
}

View File

@ -19,7 +19,9 @@
#include <QTextEdit>
#include <QList>
#include "widget/tool/chataction.h"
#include <QMap>
class ChatAction;
class ChatAreaWidget : public QTextEdit
{
@ -28,7 +30,6 @@ public:
explicit ChatAreaWidget(QWidget *parent = 0);
virtual ~ChatAreaWidget();
void insertMessage(ChatAction *msgAction);
void clearMessages();
signals:
void onFileTranfertInterract(QString widgetName, QString buttonName);
@ -36,11 +37,7 @@ signals:
protected:
void mouseReleaseEvent(QMouseEvent * event);
public slots:
void updateChatContent();
private:
QString getHtmledMessages();
QList<ChatAction*> messages;
bool lockSliderToBottom;
int sliderPosition;

View File

@ -19,6 +19,7 @@
#include "widget/friendwidget.h"
#include "filetransferinstance.h"
#include "widget/widget.h"
#include "widget/tool/chataction.h"
#include <QScrollBar>
#include <QFileDialog>
#include <QMessageBox>
@ -101,7 +102,6 @@ void ChatForm::startFileSend(ToxFile file)
connect(Widget::getInstance()->getCore(), &Core::fileTransferInfo, fileTrans, &FileTransferInstance::onFileTransferInfo);
connect(Widget::getInstance()->getCore(), &Core::fileTransferCancelled, fileTrans, &FileTransferInstance::onFileTransferCancelled);
connect(Widget::getInstance()->getCore(), &Core::fileTransferFinished, fileTrans, &FileTransferInstance::onFileTransferFinished);
connect(fileTrans, SIGNAL(stateUpdated()), chatWidget, SLOT(updateChatContent()));
QString name = Widget::getInstance()->getUsername();
if (name == previousName)
@ -122,7 +122,6 @@ void ChatForm::onFileRecvRequest(ToxFile file)
connect(Widget::getInstance()->getCore(), &Core::fileTransferInfo, fileTrans, &FileTransferInstance::onFileTransferInfo);
connect(Widget::getInstance()->getCore(), &Core::fileTransferCancelled, fileTrans, &FileTransferInstance::onFileTransferCancelled);
connect(Widget::getInstance()->getCore(), &Core::fileTransferFinished, fileTrans, &FileTransferInstance::onFileTransferFinished);
connect(fileTrans, SIGNAL(stateUpdated()), chatWidget, SLOT(updateChatContent()));
Widget* w = Widget::getInstance();
if (!w->isFriendWidgetCurActiveWidget(f)|| w->getIsWindowMinimized() || !w->isActiveWindow())

View File

@ -22,6 +22,7 @@
#include "style.h"
#include "widget/widget.h"
#include "settings.h"
#include "widget/tool/chataction.h"
GenericChatForm::GenericChatForm(QObject *parent) :
QObject(parent)

View File

@ -18,6 +18,8 @@
#include "smileypack.h"
#include <QStringList>
#include <QBuffer>
#include <QDebug>
#include "filetransferinstance.h"
QString ChatAction::toHtmlChars(const QString &str)
{
@ -84,6 +86,17 @@ MessageAction::MessageAction(const QString &author, const QString &message, cons
content = wrapWholeLine(wrapName(author) + wrapMessage(message_) + wrapDate(date));
}
void MessageAction::setTextCursor(QTextCursor cursor)
{
// When this function is called, we're supposed to only update ourselve when needed
// Nobody should ask us to do anything with our content, we're on our own
// Except we never udpate on our own, so we can safely free our resources
(void) cursor;
content.clear();
content.squeeze();
}
QString MessageAction::getHtml()
{
return content;
@ -95,11 +108,12 @@ FileTransferAction::FileTransferAction(FileTransferInstance *widget, const QStri
timestamp(date)
{
w = widget;
connect(w, &FileTransferInstance::stateUpdated, this, &FileTransferAction::updateHtml);
}
FileTransferAction::~FileTransferAction()
{
}
QString FileTransferAction::getHtml()
@ -109,7 +123,7 @@ QString FileTransferAction::getHtml()
widgetHtml = w->getHtmlImage();
else
widgetHtml = "<div class=quote>EMPTY CONTENT</div>";
QString res = wrapWholeLine(wrapName(sender) + wrapMessage(widgetHtml) + wrapDate(timestamp));;
QString res = wrapWholeLine(wrapName(sender) + wrapMessage(widgetHtml) + wrapDate(timestamp));
return res;
}
@ -118,3 +132,26 @@ QString FileTransferAction::wrapMessage(const QString &message)
QString res = "<td width=100%>" + message + "</td>\n";
return res;
}
void FileTransferAction::setTextCursor(QTextCursor cursor)
{
cur = cursor;
cur.setKeepPositionOnInsert(true);
int end=cur.selectionEnd();
cur.setPosition(cur.position());
cur.setPosition(end, QTextCursor::KeepAnchor);
}
void FileTransferAction::updateHtml()
{
if (cur.isNull())
return;
int pos = cur.selectionStart();
cur.removeSelectedText();
cur.setKeepPositionOnInsert(false);
cur.insertHtml(getHtml());
cur.setKeepPositionOnInsert(true);
int end = cur.position();
cur.setPosition(pos);
cur.setPosition(end, QTextCursor::KeepAnchor);
}

View File

@ -18,14 +18,17 @@
#define CHATACTION_H
#include <QString>
#include "filetransferinstance.h"
#include <QTextCursor>
class ChatAction
class FileTransferInstance;
class ChatAction : public QObject
{
public:
ChatAction(const bool &me) : isMe(me) {;}
virtual ~ChatAction(){;}
virtual QString getHtml() = 0;
virtual void setTextCursor(QTextCursor cursor){(void)cursor;} ///< Call once, and then you MUST let the object update itself
protected:
QString toHtmlChars(const QString &str);
@ -46,6 +49,7 @@ public:
MessageAction(const QString &author, const QString &message, const QString &date, const bool &me);
virtual ~MessageAction(){;}
virtual QString getHtml();
virtual void setTextCursor(QTextCursor cursor) final;
private:
QString content;
@ -53,15 +57,21 @@ private:
class FileTransferAction : public ChatAction
{
Q_OBJECT
public:
FileTransferAction(FileTransferInstance *widget, const QString &author, const QString &date, const bool &me);
virtual ~FileTransferAction();
virtual QString getHtml();
virtual QString wrapMessage(const QString &message);
virtual void setTextCursor(QTextCursor cursor) final;
private slots:
void updateHtml();
private:
FileTransferInstance *w;
QString sender, timestamp;
QTextCursor cur;
};
#endif // CHATACTION_H