2014-09-06 03:27:26 +08:00
|
|
|
/*
|
|
|
|
Copyright (C) 2014 by Project Tox <https://tox.im>
|
|
|
|
|
|
|
|
This file is part of qTox, a Qt-based graphical interface for Tox.
|
|
|
|
|
|
|
|
This program is libre software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the COPYING file for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "chatareawidget.h"
|
2014-09-24 00:35:06 +08:00
|
|
|
#include "widget/tool/chatactions/chataction.h"
|
2014-09-07 19:42:06 +08:00
|
|
|
#include <QScrollBar>
|
2014-09-10 22:03:53 +08:00
|
|
|
#include <QDesktopServices>
|
2014-09-11 21:44:34 +08:00
|
|
|
#include <QTextTable>
|
|
|
|
#include <QAbstractTextDocumentLayout>
|
2014-09-13 03:38:38 +08:00
|
|
|
#include <QCoreApplication>
|
2014-09-18 01:55:43 +08:00
|
|
|
#include <QDebug>
|
2014-09-06 03:27:26 +08:00
|
|
|
|
2014-10-01 23:03:59 +08:00
|
|
|
ChatAreaWidget::ChatAreaWidget(QWidget *parent)
|
|
|
|
: QTextBrowser(parent)
|
2014-10-07 15:12:05 +08:00
|
|
|
, tableFrmt(nullptr)
|
2014-10-07 18:20:53 +08:00
|
|
|
, nameWidth(75)
|
2014-09-06 03:27:26 +08:00
|
|
|
{
|
|
|
|
setReadOnly(true);
|
|
|
|
viewport()->setCursor(Qt::ArrowCursor);
|
|
|
|
setContextMenuPolicy(Qt::CustomContextMenu);
|
2014-09-10 04:36:34 +08:00
|
|
|
setUndoRedoEnabled(false);
|
2014-09-10 22:03:53 +08:00
|
|
|
|
|
|
|
setOpenExternalLinks(false);
|
|
|
|
setOpenLinks(false);
|
|
|
|
setAcceptRichText(false);
|
2014-09-27 01:16:27 +08:00
|
|
|
setFrameStyle(QFrame::NoFrame);
|
2014-09-10 22:03:53 +08:00
|
|
|
|
2014-10-01 22:17:09 +08:00
|
|
|
nameFormat.setAlignment(Qt::AlignRight);
|
|
|
|
nameFormat.setNonBreakableLines(true);
|
|
|
|
dateFormat.setAlignment(Qt::AlignLeft);
|
|
|
|
dateFormat.setNonBreakableLines(true);
|
2014-09-11 03:22:00 +08:00
|
|
|
|
2014-09-10 22:03:53 +08:00
|
|
|
connect(this, &ChatAreaWidget::anchorClicked, this, &ChatAreaWidget::onAnchorClicked);
|
2014-09-11 02:28:05 +08:00
|
|
|
connect(verticalScrollBar(), SIGNAL(rangeChanged(int,int)), this, SLOT(onSliderRangeChanged()));
|
2014-09-06 03:27:26 +08:00
|
|
|
}
|
|
|
|
|
2014-09-07 19:42:06 +08:00
|
|
|
ChatAreaWidget::~ChatAreaWidget()
|
|
|
|
{
|
2014-09-11 00:30:03 +08:00
|
|
|
for (ChatAction* action : messages)
|
|
|
|
delete action;
|
|
|
|
messages.clear();
|
2014-10-07 15:12:05 +08:00
|
|
|
|
|
|
|
if (tableFrmt)
|
|
|
|
delete tableFrmt;
|
2014-09-07 19:42:06 +08:00
|
|
|
}
|
|
|
|
|
2014-09-06 03:27:26 +08:00
|
|
|
void ChatAreaWidget::mouseReleaseEvent(QMouseEvent * event)
|
|
|
|
{
|
|
|
|
QTextEdit::mouseReleaseEvent(event);
|
2014-09-18 01:55:43 +08:00
|
|
|
QPointF documentHitPost(event->pos().x() + horizontalScrollBar()->value(), event->pos().y() + verticalScrollBar()->value());
|
|
|
|
int pos = this->document()->documentLayout()->hitTest(documentHitPost, Qt::ExactHit);
|
2014-09-06 03:27:26 +08:00
|
|
|
if (pos > 0)
|
|
|
|
{
|
|
|
|
QTextCursor cursor(document());
|
2014-09-18 01:55:43 +08:00
|
|
|
cursor.setPosition(pos);
|
|
|
|
|
|
|
|
if(!cursor.atEnd())
|
2014-09-06 03:27:26 +08:00
|
|
|
{
|
|
|
|
cursor.setPosition(pos+1);
|
|
|
|
|
|
|
|
QTextFormat format = cursor.charFormat();
|
|
|
|
if (format.isImageFormat())
|
|
|
|
{
|
2014-09-07 19:42:06 +08:00
|
|
|
QString imageName = format.toImageFormat().name();
|
|
|
|
if (QRegExp("^data:ftrans.*").exactMatch(imageName))
|
2014-09-06 03:27:26 +08:00
|
|
|
{
|
2014-09-07 19:42:06 +08:00
|
|
|
QString data = imageName.right(imageName.length() - 12);
|
|
|
|
int endpos = data.indexOf("/png;base64");
|
|
|
|
data = data.left(endpos);
|
|
|
|
int middlepos = data.indexOf(".");
|
|
|
|
QString widgetID = data.left(middlepos);
|
|
|
|
QString widgetBtn = data.right(data.length() - middlepos - 1);
|
2014-09-18 01:55:43 +08:00
|
|
|
qDebug() << "ChatAreaWidget::mouseReleaseEvent:" << widgetID << widgetBtn;
|
2014-09-07 19:42:06 +08:00
|
|
|
emit onFileTranfertInterract(widgetID, widgetBtn);
|
2014-09-06 03:27:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-09-07 19:42:06 +08:00
|
|
|
|
2014-09-10 22:03:53 +08:00
|
|
|
void ChatAreaWidget::onAnchorClicked(const QUrl &url)
|
|
|
|
{
|
|
|
|
QDesktopServices::openUrl(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChatAreaWidget::insertMessage(ChatAction *msgAction)
|
2014-09-07 19:42:06 +08:00
|
|
|
{
|
|
|
|
if (msgAction == nullptr)
|
|
|
|
return;
|
|
|
|
|
2014-09-11 02:28:05 +08:00
|
|
|
checkSlider();
|
|
|
|
|
2014-10-07 15:12:05 +08:00
|
|
|
QTextTable *chatTextTable = getMsgTable();
|
2014-10-07 18:20:53 +08:00
|
|
|
QTextCursor cur = chatTextTable->cellAt(0, 2).firstCursorPosition();
|
2014-09-11 00:30:03 +08:00
|
|
|
cur.clearSelection();
|
|
|
|
cur.setKeepPositionOnInsert(true);
|
2014-10-07 15:12:05 +08:00
|
|
|
chatTextTable->cellAt(0, 0).firstCursorPosition().setBlockFormat(nameFormat);
|
|
|
|
chatTextTable->cellAt(0, 0).firstCursorPosition().insertHtml(msgAction->getName());
|
|
|
|
chatTextTable->cellAt(0, 2).firstCursorPosition().insertHtml(msgAction->getMessage());
|
|
|
|
chatTextTable->cellAt(0, 4).firstCursorPosition().setBlockFormat(dateFormat);
|
|
|
|
chatTextTable->cellAt(0, 4).firstCursorPosition().insertHtml(msgAction->getDate());
|
2014-09-11 03:22:00 +08:00
|
|
|
|
2014-09-27 19:23:00 +08:00
|
|
|
msgAction->setup(cur, this);
|
2014-09-10 21:42:22 +08:00
|
|
|
|
2014-09-11 00:30:03 +08:00
|
|
|
messages.append(msgAction);
|
2014-09-07 19:42:06 +08:00
|
|
|
}
|
2014-09-11 02:28:05 +08:00
|
|
|
|
|
|
|
void ChatAreaWidget::onSliderRangeChanged()
|
|
|
|
{
|
|
|
|
QScrollBar* scroll = verticalScrollBar();
|
|
|
|
if (lockSliderToBottom)
|
|
|
|
scroll->setValue(scroll->maximum());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChatAreaWidget::checkSlider()
|
|
|
|
{
|
|
|
|
QScrollBar* scroll = verticalScrollBar();
|
|
|
|
lockSliderToBottom = scroll && scroll->value() == scroll->maximum();
|
|
|
|
}
|
2014-09-29 00:39:26 +08:00
|
|
|
|
2014-10-07 15:12:05 +08:00
|
|
|
QTextTable *ChatAreaWidget::getMsgTable()
|
|
|
|
{
|
|
|
|
if (tableFrmt == nullptr)
|
|
|
|
{
|
|
|
|
tableFrmt = new QTextTableFormat();
|
|
|
|
tableFrmt->setCellSpacing(2);
|
|
|
|
tableFrmt->setBorderStyle(QTextFrameFormat::BorderStyle_None);
|
|
|
|
tableFrmt->setColumnWidthConstraints({QTextLength(QTextLength::FixedLength,nameWidth),
|
|
|
|
QTextLength(QTextLength::FixedLength,2),
|
|
|
|
QTextLength(QTextLength::PercentageLength,100),
|
|
|
|
QTextLength(QTextLength::FixedLength,2),
|
|
|
|
QTextLength(QTextLength::VariableLength,0)});
|
|
|
|
}
|
|
|
|
|
2014-10-07 18:20:53 +08:00
|
|
|
QTextCursor tc = textCursor();
|
|
|
|
tc.movePosition(QTextCursor::End);
|
2014-10-11 01:32:10 +08:00
|
|
|
|
2014-10-07 18:20:53 +08:00
|
|
|
QTextTable *chatTextTable = tc.insertTable(1, 5, *tableFrmt);
|
2014-10-07 15:12:05 +08:00
|
|
|
|
|
|
|
return chatTextTable;
|
|
|
|
}
|
|
|
|
|
2014-09-29 00:39:26 +08:00
|
|
|
void ChatAreaWidget::setNameColWidth(int w)
|
|
|
|
{
|
2014-10-07 15:12:05 +08:00
|
|
|
if (tableFrmt != nullptr)
|
|
|
|
{
|
|
|
|
delete tableFrmt;
|
|
|
|
tableFrmt = nullptr;
|
|
|
|
}
|
2014-09-29 00:39:26 +08:00
|
|
|
|
2014-10-07 15:12:05 +08:00
|
|
|
nameWidth = w;
|
2014-09-29 00:39:26 +08:00
|
|
|
}
|
2014-10-11 00:54:21 +08:00
|
|
|
|
|
|
|
void ChatAreaWidget::clearChatArea()
|
|
|
|
{
|
|
|
|
QList<ChatAction*> newMsgs;
|
|
|
|
for (ChatAction* message : messages)
|
|
|
|
{
|
|
|
|
if (message->isInteractive())
|
|
|
|
{
|
|
|
|
newMsgs.append(message);
|
|
|
|
} else {
|
|
|
|
delete message;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
messages.clear();
|
|
|
|
this->clear();
|
|
|
|
|
|
|
|
for (ChatAction* message : newMsgs)
|
|
|
|
{
|
|
|
|
insertMessage(message);
|
|
|
|
}
|
|
|
|
}
|