2014-09-05 02:09:55 +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 "genericchatform.h"
|
|
|
|
#include "ui_mainwindow.h"
|
|
|
|
#include <QScrollBar>
|
|
|
|
#include <QFileDialog>
|
|
|
|
#include <QTextStream>
|
2014-09-05 23:05:57 +08:00
|
|
|
#include "smileypack.h"
|
|
|
|
#include "widget/emoticonswidget.h"
|
2014-09-05 02:09:55 +08:00
|
|
|
#include "style.h"
|
2014-09-05 23:24:29 +08:00
|
|
|
#include "widget/widget.h"
|
2014-09-06 03:27:26 +08:00
|
|
|
#include "settings.h"
|
2014-09-05 02:09:55 +08:00
|
|
|
|
|
|
|
GenericChatForm::GenericChatForm(QObject *parent) :
|
|
|
|
QObject(parent)
|
|
|
|
{
|
|
|
|
lockSliderToBottom = true;
|
|
|
|
curRow = 0;
|
|
|
|
|
|
|
|
mainWidget = new QWidget(); headWidget = new QWidget(); chatAreaWidget = new QWidget();
|
|
|
|
|
|
|
|
nameLabel = new QLabel();
|
|
|
|
avatarLabel = new QLabel();
|
|
|
|
QHBoxLayout *headLayout = new QHBoxLayout(), *mainFootLayout = new QHBoxLayout();
|
|
|
|
headTextLayout = new QVBoxLayout();
|
|
|
|
QVBoxLayout *mainLayout = new QVBoxLayout();
|
|
|
|
QVBoxLayout *footButtonsSmall = new QVBoxLayout(), *volMicLayout = new QVBoxLayout();
|
|
|
|
mainChatLayout = new QGridLayout();
|
|
|
|
|
2014-09-06 03:27:26 +08:00
|
|
|
newChatForm = new ChatAreaWidget();
|
|
|
|
newChatForm->document()->setDefaultStyleSheet(Style::get(":ui/chatArea/innerStyle.css"));
|
|
|
|
newChatForm->setStyleSheet(Style::get(":/ui/chatArea/chatArea.css"));
|
|
|
|
|
2014-09-05 02:09:55 +08:00
|
|
|
msgEdit = new ChatTextEdit();
|
|
|
|
|
|
|
|
sendButton = new QPushButton();
|
|
|
|
emoteButton = new QPushButton();
|
|
|
|
|
|
|
|
fileButton = new QPushButton();
|
|
|
|
callButton = new QPushButton();
|
|
|
|
videoButton = new QPushButton();
|
|
|
|
volButton = new QPushButton();
|
|
|
|
micButton = new QPushButton();
|
|
|
|
|
|
|
|
chatArea = new QScrollArea();
|
|
|
|
|
|
|
|
QFont bold;
|
|
|
|
bold.setBold(true);
|
|
|
|
nameLabel->setFont(bold);
|
|
|
|
|
|
|
|
chatAreaWidget->setLayout(mainChatLayout);
|
|
|
|
chatAreaWidget->setStyleSheet(Style::get(":/ui/chatArea/chatArea.css"));
|
|
|
|
|
|
|
|
chatArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
|
|
|
|
chatArea->setWidgetResizable(true);
|
|
|
|
chatArea->setContextMenuPolicy(Qt::CustomContextMenu);
|
|
|
|
chatArea->setFrameStyle(QFrame::NoFrame);
|
|
|
|
|
|
|
|
mainChatLayout->setColumnStretch(1,1);
|
|
|
|
mainChatLayout->setSpacing(5);
|
|
|
|
|
|
|
|
footButtonsSmall->setSpacing(2);
|
|
|
|
|
|
|
|
msgEdit->setStyleSheet(Style::get(":/ui/msgEdit/msgEdit.css"));
|
|
|
|
msgEdit->setFixedHeight(50);
|
|
|
|
msgEdit->setFrameStyle(QFrame::NoFrame);
|
|
|
|
|
|
|
|
sendButton->setStyleSheet(Style::get(":/ui/sendButton/sendButton.css"));
|
|
|
|
fileButton->setStyleSheet(Style::get(":/ui/fileButton/fileButton.css"));
|
|
|
|
emoteButton->setStyleSheet(Style::get(":/ui/emoteButton/emoteButton.css"));
|
|
|
|
|
|
|
|
callButton->setObjectName("green");
|
|
|
|
callButton->setStyleSheet(Style::get(":/ui/callButton/callButton.css"));
|
|
|
|
|
|
|
|
videoButton->setObjectName("green");
|
|
|
|
videoButton->setStyleSheet(Style::get(":/ui/videoButton/videoButton.css"));
|
|
|
|
|
|
|
|
QString volButtonStylesheet = "";
|
|
|
|
try
|
|
|
|
{
|
|
|
|
QFile f(":/ui/volButton/volButton.css");
|
|
|
|
f.open(QFile::ReadOnly | QFile::Text);
|
|
|
|
QTextStream volButtonStylesheetStream(&f);
|
|
|
|
volButtonStylesheet = volButtonStylesheetStream.readAll();
|
|
|
|
}
|
|
|
|
catch (int e) {}
|
|
|
|
|
|
|
|
volButton->setObjectName("green");
|
|
|
|
volButton->setStyleSheet(volButtonStylesheet);
|
|
|
|
|
|
|
|
QString micButtonStylesheet = "";
|
|
|
|
try
|
|
|
|
{
|
|
|
|
QFile f(":/ui/micButton/micButton.css");
|
|
|
|
f.open(QFile::ReadOnly | QFile::Text);
|
|
|
|
QTextStream micButtonStylesheetStream(&f);
|
|
|
|
micButtonStylesheet = micButtonStylesheetStream.readAll();
|
|
|
|
}
|
|
|
|
catch (int e) {}
|
|
|
|
|
|
|
|
micButton->setObjectName("green");
|
|
|
|
micButton->setStyleSheet(micButtonStylesheet);
|
|
|
|
|
|
|
|
mainWidget->setLayout(mainLayout);
|
|
|
|
mainLayout->addWidget(chatArea);
|
2014-09-06 03:27:26 +08:00
|
|
|
mainLayout->addWidget(newChatForm);
|
2014-09-05 02:09:55 +08:00
|
|
|
mainLayout->addLayout(mainFootLayout);
|
|
|
|
mainLayout->setMargin(0);
|
|
|
|
|
|
|
|
footButtonsSmall->addWidget(emoteButton);
|
|
|
|
footButtonsSmall->addWidget(fileButton);
|
|
|
|
|
|
|
|
mainFootLayout->addWidget(msgEdit);
|
|
|
|
mainFootLayout->addLayout(footButtonsSmall);
|
|
|
|
mainFootLayout->addSpacing(5);
|
|
|
|
mainFootLayout->addWidget(sendButton);
|
|
|
|
mainFootLayout->setSpacing(0);
|
|
|
|
|
|
|
|
headWidget->setLayout(headLayout);
|
|
|
|
headLayout->addWidget(avatarLabel);
|
|
|
|
headLayout->addLayout(headTextLayout);
|
|
|
|
headLayout->addStretch();
|
|
|
|
headLayout->addLayout(volMicLayout);
|
|
|
|
headLayout->addWidget(callButton);
|
|
|
|
headLayout->addWidget(videoButton);
|
|
|
|
|
|
|
|
volMicLayout->addWidget(micButton);
|
|
|
|
volMicLayout->addWidget(volButton);
|
|
|
|
|
|
|
|
headTextLayout->addStretch();
|
|
|
|
headTextLayout->addWidget(nameLabel);
|
|
|
|
|
|
|
|
chatArea->setWidget(chatAreaWidget);
|
|
|
|
|
|
|
|
//Fix for incorrect layouts on OS X as per
|
|
|
|
//https://bugreports.qt-project.org/browse/QTBUG-14591
|
|
|
|
sendButton->setAttribute(Qt::WA_LayoutUsesWidgetRect);
|
|
|
|
fileButton->setAttribute(Qt::WA_LayoutUsesWidgetRect);
|
|
|
|
emoteButton->setAttribute(Qt::WA_LayoutUsesWidgetRect);
|
|
|
|
|
2014-09-05 23:05:57 +08:00
|
|
|
connect(emoteButton, SIGNAL(clicked()), this, SLOT(onEmoteButtonClicked()));
|
2014-09-06 03:27:26 +08:00
|
|
|
connect(newChatForm, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(onChatContextMenuRequested(QPoint)));
|
|
|
|
connect(newChatForm->verticalScrollBar(), SIGNAL(rangeChanged(int,int)), this, SLOT(onSliderRangeChanged()));
|
2014-09-05 02:09:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void GenericChatForm::setName(const QString &newName)
|
|
|
|
{
|
|
|
|
nameLabel->setText(newName);
|
|
|
|
nameLabel->setToolTip(newName); // for overlength names
|
|
|
|
}
|
|
|
|
|
|
|
|
void GenericChatForm::show(Ui::MainWindow &ui)
|
|
|
|
{
|
|
|
|
ui.mainContent->layout()->addWidget(mainWidget);
|
|
|
|
ui.mainHead->layout()->addWidget(headWidget);
|
|
|
|
mainWidget->show();
|
|
|
|
headWidget->show();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GenericChatForm::onChatContextMenuRequested(QPoint pos)
|
|
|
|
{
|
|
|
|
QWidget* sender = (QWidget*)QObject::sender();
|
|
|
|
pos = sender->mapToGlobal(pos);
|
|
|
|
QMenu menu;
|
|
|
|
menu.addAction(tr("Save chat log"), this, SLOT(onSaveLogClicked()));
|
|
|
|
menu.exec(pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GenericChatForm::onSliderRangeChanged()
|
|
|
|
{
|
2014-09-06 03:27:26 +08:00
|
|
|
QScrollBar* scroll = newChatForm->verticalScrollBar();
|
2014-09-05 02:09:55 +08:00
|
|
|
if (lockSliderToBottom)
|
|
|
|
scroll->setValue(scroll->maximum());
|
|
|
|
}
|
|
|
|
|
|
|
|
void GenericChatForm::onSaveLogClicked()
|
|
|
|
{
|
2014-09-06 03:27:26 +08:00
|
|
|
QString path = QFileDialog::getSaveFileName(0, tr("Save chat log"));
|
2014-09-05 02:09:55 +08:00
|
|
|
if (path.isEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
QFile file(path);
|
|
|
|
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
|
|
|
|
return;
|
|
|
|
|
|
|
|
QString log;
|
2014-09-06 03:27:26 +08:00
|
|
|
log = newChatForm->toPlainText();
|
2014-09-05 02:09:55 +08:00
|
|
|
|
|
|
|
file.write(log.toUtf8());
|
|
|
|
file.close();
|
|
|
|
}
|
|
|
|
|
2014-09-06 03:27:26 +08:00
|
|
|
void GenericChatForm::addMessage(QString author, QString message, QDateTime datetime)
|
2014-09-05 02:09:55 +08:00
|
|
|
{
|
2014-09-06 03:27:26 +08:00
|
|
|
QString date = datetime.toString(Settings::getInstance().getTimestampFormat());
|
|
|
|
if (previousName == author)
|
|
|
|
messages.append(new MessageAction("", message, date));
|
|
|
|
else messages.append(new MessageAction(author , message, date));
|
|
|
|
previousName = author;
|
|
|
|
updateChatContent();
|
2014-09-05 02:09:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
GenericChatForm::~GenericChatForm()
|
|
|
|
{
|
|
|
|
delete mainWidget;
|
|
|
|
delete headWidget;
|
2014-09-06 03:27:26 +08:00
|
|
|
for (ChatAction *it : messages)
|
|
|
|
delete it;
|
2014-09-05 02:09:55 +08:00
|
|
|
}
|
2014-09-05 23:05:57 +08:00
|
|
|
|
|
|
|
void GenericChatForm::onEmoteButtonClicked()
|
|
|
|
{
|
|
|
|
// don't show the smiley selection widget if there are no smileys available
|
|
|
|
if (SmileyPack::getInstance().getEmoticons().empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
EmoticonsWidget widget;
|
|
|
|
connect(&widget, SIGNAL(insertEmoticon(QString)), this, SLOT(onEmoteInsertRequested(QString)));
|
|
|
|
|
|
|
|
QWidget* sender = qobject_cast<QWidget*>(QObject::sender());
|
|
|
|
if (sender)
|
|
|
|
{
|
|
|
|
QPoint pos = -QPoint(widget.sizeHint().width() / 2, widget.sizeHint().height()) - QPoint(0, 10);
|
|
|
|
widget.exec(sender->mapToGlobal(pos));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GenericChatForm::onEmoteInsertRequested(QString str)
|
|
|
|
{
|
|
|
|
// insert the emoticon
|
|
|
|
QWidget* sender = qobject_cast<QWidget*>(QObject::sender());
|
|
|
|
if (sender)
|
|
|
|
msgEdit->insertPlainText(str);
|
|
|
|
|
|
|
|
msgEdit->setFocus(); // refocus so that we can continue typing
|
|
|
|
}
|
2014-09-05 23:29:14 +08:00
|
|
|
|
2014-09-06 03:27:26 +08:00
|
|
|
QString GenericChatForm::getHtmledMessages()
|
2014-09-05 23:29:14 +08:00
|
|
|
{
|
2014-09-06 03:27:26 +08:00
|
|
|
QString res("<table width=100%>\n");
|
2014-09-05 23:29:14 +08:00
|
|
|
|
2014-09-06 03:27:26 +08:00
|
|
|
for (ChatAction *it : messages)
|
|
|
|
{
|
|
|
|
res += it->getHtml();
|
|
|
|
}
|
|
|
|
res += "</table>";
|
2014-09-05 23:29:14 +08:00
|
|
|
return res;
|
|
|
|
}
|
2014-09-06 03:27:26 +08:00
|
|
|
|
|
|
|
void GenericChatForm::updateChatContent()
|
|
|
|
{
|
|
|
|
QScrollBar* scroll = newChatForm->verticalScrollBar();
|
|
|
|
lockSliderToBottom = scroll && scroll->value() == scroll->maximum();
|
|
|
|
|
|
|
|
newChatForm->setHtml(getHtmledMessages());
|
|
|
|
}
|