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

137 lines
4.6 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"
#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>
#include <QCoreApplication>
#include <QDebug>
2014-09-06 03:27:26 +08:00
ChatAreaWidget::ChatAreaWidget(QWidget *parent) :
2014-09-10 22:03:53 +08:00
QTextBrowser(parent)
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);
chatTextTable = textCursor().insertTable(1,3);
QTextTableFormat tableFormat;
tableFormat.setColumnWidthConstraints({QTextLength(QTextLength::VariableLength,0),
QTextLength(QTextLength::PercentageLength,100),
QTextLength(QTextLength::VariableLength,0)});
tableFormat.setBorderStyle(QTextFrameFormat::BorderStyle_None);
chatTextTable->setFormat(tableFormat);
chatTextTable->format().setCellSpacing(2);
chatTextTable->format().setWidth(QTextLength(QTextLength::PercentageLength,100));
// 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()
{
2014-09-11 00:30:03 +08:00
for (ChatAction* action : messages)
delete action;
messages.clear();
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
}
}
}
}
}
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();
int row = chatTextTable->rows() - 1;
QTextCursor cur = chatTextTable->cellAt(row,1).firstCursorPosition();
2014-09-11 00:30:03 +08:00
cur.clearSelection();
cur.setKeepPositionOnInsert(true);
chatTextTable->appendRows(1);
chatTextTable->cellAt(row,0).firstCursorPosition().insertHtml(msgAction->getName());
chatTextTable->cellAt(row,1).firstCursorPosition().insertHtml(msgAction->getMessage());
chatTextTable->cellAt(row,2).firstCursorPosition().insertText(msgAction->getDate());
2014-09-11 00:30:03 +08:00
msgAction->setTextCursor(cur);
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();
}