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

242 lines
8.0 KiB
C++
Raw Normal View History

/*
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 <QFileDialog>
2014-09-30 02:28:59 +08:00
#include "misc/smileypack.h"
2014-09-05 23:05:57 +08:00
#include "widget/emoticonswidget.h"
2014-09-30 02:28:59 +08:00
#include "misc/style.h"
2014-09-05 23:24:29 +08:00
#include "widget/widget.h"
2014-09-30 02:28:59 +08:00
#include "misc/settings.h"
#include "widget/tool/chatactions/messageaction.h"
#include "widget/tool/chatactions/systemmessageaction.h"
#include "widget/tool/chatactions/actionaction.h"
2014-09-11 21:44:34 +08:00
#include "widget/chatareawidget.h"
#include "widget/tool/chattextedit.h"
#include "widget/maskablepixmapwidget.h"
2014-09-26 00:03:25 +08:00
GenericChatForm::GenericChatForm(QWidget *parent) :
QWidget(parent)
{
curRow = 0;
2014-09-26 00:03:25 +08:00
headWidget = new QWidget();
2014-09-10 00:28:07 +08:00
nameLabel = new CroppingLabel();
nameLabel->setFont(Style::getFont(Style::MediumBold));
QPalette pal; pal.setColor(QPalette::WindowText, Style::getColor(Style::DarkGrey));
nameLabel->setPalette(pal);
avatar = new MaskablePixmapWidget(this, QSize(40,40), ":/img/avatar_mask.png");
QHBoxLayout *headLayout = new QHBoxLayout(), *mainFootLayout = new QHBoxLayout();
headTextLayout = new QVBoxLayout();
QVBoxLayout *mainLayout = new QVBoxLayout();
QVBoxLayout *footButtonsSmall = new QVBoxLayout(), *volMicLayout = new QVBoxLayout();
2014-09-07 19:42:06 +08:00
chatWidget = new ChatAreaWidget();
chatWidget->document()->setDefaultStyleSheet(Style::getStylesheet(":ui/chatArea/innerStyle.css"));
chatWidget->setStyleSheet(Style::getStylesheet(":/ui/chatArea/chatArea.css"));
2014-09-06 03:27:26 +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();
QFont bold;
bold.setBold(true);
nameLabel->setFont(bold);
footButtonsSmall->setSpacing(2);
msgEdit->setStyleSheet(Style::getStylesheet(":/ui/msgEdit/msgEdit.css"));
msgEdit->setFixedHeight(50);
msgEdit->setFrameStyle(QFrame::NoFrame);
sendButton->setStyleSheet(Style::getStylesheet(":/ui/sendButton/sendButton.css"));
fileButton->setStyleSheet(Style::getStylesheet(":/ui/fileButton/fileButton.css"));
emoteButton->setStyleSheet(Style::getStylesheet(":/ui/emoteButton/emoteButton.css"));
callButton->setObjectName("green");
callButton->setStyleSheet(Style::getStylesheet(":/ui/callButton/callButton.css"));
videoButton->setObjectName("green");
videoButton->setStyleSheet(Style::getStylesheet(":/ui/videoButton/videoButton.css"));
QString volButtonStylesheet = Style::getStylesheet(":/ui/volButton/volButton.css");
volButton->setObjectName("green");
volButton->setStyleSheet(volButtonStylesheet);
QString micButtonStylesheet = Style::getStylesheet(":/ui/micButton/micButton.css");
micButton->setObjectName("green");
micButton->setStyleSheet(micButtonStylesheet);
2014-09-26 00:03:25 +08:00
setLayout(mainLayout);
2014-09-07 19:42:06 +08:00
mainLayout->addWidget(chatWidget);
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(avatar);
headLayout->addLayout(headTextLayout);
headLayout->addLayout(volMicLayout);
headLayout->addWidget(callButton);
headLayout->addWidget(videoButton);
volMicLayout->addWidget(micButton);
volMicLayout->addWidget(volButton);
headTextLayout->addStretch();
headTextLayout->addWidget(nameLabel);
//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-10-09 22:36:24 +08:00
menu.addAction(tr("Save chat log"), this, SLOT(onSaveLogClicked()));
2014-09-05 23:05:57 +08:00
connect(emoteButton, SIGNAL(clicked()), this, SLOT(onEmoteButtonClicked()));
2014-09-07 19:42:06 +08:00
connect(chatWidget, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(onChatContextMenuRequested(QPoint)));
}
void GenericChatForm::setName(const QString &newName)
{
nameLabel->setText(newName);
nameLabel->setToolTip(newName); // for overlength names
}
void GenericChatForm::show(Ui::MainWindow &ui)
{
2014-09-26 00:03:25 +08:00
ui.mainContent->layout()->addWidget(this);
ui.mainHead->layout()->addWidget(headWidget);
headWidget->show();
2014-09-26 00:03:25 +08:00
QWidget::show();
}
void GenericChatForm::onChatContextMenuRequested(QPoint pos)
{
QWidget* sender = (QWidget*)QObject::sender();
pos = sender->mapToGlobal(pos);
menu.exec(pos);
}
void GenericChatForm::onSaveLogClicked()
{
2014-09-06 03:27:26 +08:00
QString path = QFileDialog::getSaveFileName(0, tr("Save chat log"));
if (path.isEmpty())
return;
QFile file(path);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
return;
QString log;
2014-09-07 19:42:06 +08:00
log = chatWidget->toPlainText();
file.write(log.toUtf8());
file.close();
}
void GenericChatForm::addMessage(QString author, QString message, bool isAction, QDateTime datetime)
{
2014-09-06 03:27:26 +08:00
QString date = datetime.toString(Settings::getInstance().getTimestampFormat());
2014-09-07 19:42:06 +08:00
bool isMe = (author == Widget::getInstance()->getUsername());
if (!isAction && message.startsWith("/me "))
{ // always render actions regardless of what core thinks
isAction = true;
message = message.right(message.length()-4);
}
if (isAction)
{
chatWidget->insertMessage(new ActionAction (getElidedName(author), message, date, isMe));
previousName = ""; // next msg has a name regardless
return;
}
else if (previousName == author)
2014-09-07 19:42:06 +08:00
chatWidget->insertMessage(new MessageAction("", message, date, isMe));
2014-10-01 23:03:59 +08:00
else
chatWidget->insertMessage(new MessageAction(getElidedName(author), message, date, isMe));
2014-10-01 23:03:59 +08:00
2014-09-06 03:27:26 +08:00
previousName = author;
}
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-11 05:00:45 +08:00
void GenericChatForm::focusInput()
{
msgEdit->setFocus();
}
void GenericChatForm::addSystemInfoMessage(const QString &message, const QString &type, const QDateTime &datetime)
{
previousName = "";
QString date = datetime.toString(Settings::getInstance().getTimestampFormat());
chatWidget->insertMessage(new SystemMessageAction(message, type, date));
}
2014-09-29 00:39:26 +08:00
QString GenericChatForm::getElidedName(const QString& name)
{
// update this whenever you change the font in innerStyle.css
QFontMetrics fm(Style::getFont(Style::BigBold));
2014-09-29 00:39:26 +08:00
return fm.elidedText(name, Qt::ElideRight, chatWidget->nameColWidth());
}