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

209 lines
6.1 KiB
C++
Raw Normal View History

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-10-08 12:26:25 +08:00
#include "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>
#include <QCoreApplication>
#include <QDebug>
#include <algorithm>
2014-09-06 03:27:26 +08:00
2014-10-01 23:03:59 +08:00
ChatAreaWidget::ChatAreaWidget(QWidget *parent)
: QTextBrowser(parent)
, tableFrmt(nullptr)
, nameWidth(75)
2014-11-08 21:46:26 +08:00
, empty{true}
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-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()
{
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);
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());
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);
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
}
}
}
}
emit onClick();
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(ChatActionPtr msgAction, QTextCursor::MoveOperation pos)
2014-09-07 19:42:06 +08:00
{
if (msgAction == nullptr)
return;
2014-09-11 02:28:05 +08:00
checkSlider();
QTextTable *chatTextTable = getMsgTable(pos);
2014-11-10 00:07:15 +08:00
msgAction->assignPlace(chatTextTable, this);
msgAction->dispaly();
/*
QTextCursor cur = chatTextTable->cellAt(0, 2).firstCursorPosition();
2014-09-11 00:30:03 +08:00
cur.clearSelection();
cur.setKeepPositionOnInsert(true);
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-27 19:23:00 +08:00
msgAction->setup(cur, this);
2014-11-10 00:07:15 +08:00
*/
2014-09-10 21:42:22 +08:00
if (msgAction->isInteractive())
messages.append(msgAction);
2014-11-08 21:46:26 +08:00
empty = false;
2014-09-07 19:42:06 +08:00
}
2014-09-11 02:28:05 +08:00
2014-11-08 21:46:26 +08:00
bool ChatAreaWidget::isEmpty()
{
2014-11-08 21:46:26 +08:00
return empty;
}
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
QTextTable *ChatAreaWidget::getMsgTable(QTextCursor::MoveOperation pos)
{
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)});
}
QTextCursor tc = textCursor();
tc.movePosition(pos);
2014-10-11 01:32:10 +08:00
QTextTable *chatTextTable = tc.insertTable(1, 5, *tableFrmt);
return chatTextTable;
}
2014-09-29 00:39:26 +08:00
void ChatAreaWidget::setNameColWidth(int w)
{
if (tableFrmt != nullptr)
{
delete tableFrmt;
tableFrmt = nullptr;
}
2014-09-29 00:39:26 +08:00
nameWidth = w;
2014-09-29 00:39:26 +08:00
}
2014-10-11 00:54:21 +08:00
void ChatAreaWidget::clearChatArea()
{
QList<ChatActionPtr> newMsgs;
for (ChatActionPtr message : messages)
2014-10-11 00:54:21 +08:00
{
if (message->isInteractive())
{
newMsgs.append(message);
}
}
messages.clear();
this->clear();
2014-11-08 21:46:26 +08:00
empty = true;
2014-10-11 00:54:21 +08:00
for (ChatActionPtr message : newMsgs)
2014-10-11 00:54:21 +08:00
{
insertMessage(message);
}
}
void ChatAreaWidget::insertMessagesTop(QList<ChatActionPtr> &list)
{
std::reverse(list.begin(), list.end());
for (ChatActionPtr it : list)
{
insertMessage(it, QTextCursor::Start);
}
2014-11-08 21:46:26 +08:00
empty = false;
}