matrixion/src/TextInputWidget.cc

219 lines
6.3 KiB
C++
Raw Normal View History

2017-04-06 02:06:42 +03:00
/*
* nheko Copyright (C) 2017 Konstantinos Sideris <siderisk@auth.gr>
*
* This program is free 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
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QDebug>
2017-04-23 21:31:08 +03:00
#include <QFile>
2017-09-10 12:58:00 +03:00
#include <QFileDialog>
#include <QImageReader>
2017-04-06 02:06:42 +03:00
#include <QPainter>
#include <QStyleOption>
#include "Config.h"
2017-04-06 02:06:42 +03:00
#include "TextInputWidget.h"
2017-04-23 21:31:08 +03:00
FilteredTextEdit::FilteredTextEdit(QWidget *parent)
2017-08-20 13:47:22 +03:00
: QTextEdit(parent)
2017-04-23 21:31:08 +03:00
{
setAcceptRichText(false);
2017-04-23 21:31:08 +03:00
}
2017-08-20 13:47:22 +03:00
void
FilteredTextEdit::keyPressEvent(QKeyEvent *event)
2017-04-23 21:31:08 +03:00
{
if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter)
emit enterPressed();
else
QTextEdit::keyPressEvent(event);
2017-04-23 21:31:08 +03:00
}
2017-04-06 02:06:42 +03:00
TextInputWidget::TextInputWidget(QWidget *parent)
: QFrame(parent)
2017-04-06 02:06:42 +03:00
{
setFont(QFont("Emoji One"));
2017-10-04 11:33:34 +03:00
setFixedHeight(45);
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
setCursor(Qt::ArrowCursor);
2017-10-04 11:33:34 +03:00
setStyleSheet("background-color: #fff;");
2017-09-10 12:58:00 +03:00
topLayout_ = new QHBoxLayout();
2017-10-04 11:33:34 +03:00
topLayout_->setSpacing(0);
topLayout_->setContentsMargins(5, 15, 0, 5);
QIcon send_file_icon;
send_file_icon.addFile(":/icons/icons/clip-dark.png", QSize(), QIcon::Normal, QIcon::Off);
2017-09-10 12:58:00 +03:00
sendFileBtn_ = new FlatButton(this);
sendFileBtn_->setForegroundColor(QColor("#acc7dc"));
sendFileBtn_->setIcon(send_file_icon);
sendFileBtn_->setIconSize(QSize(24, 24));
spinner_ = new LoadingIndicator(this);
spinner_->setColor("#acc7dc");
2017-10-04 11:33:34 +03:00
spinner_->setFixedHeight(32);
spinner_->setFixedWidth(32);
2017-09-10 12:58:00 +03:00
spinner_->hide();
QFont font;
font.setPixelSize(conf::fontSize);
input_ = new FilteredTextEdit(this);
2017-10-04 11:33:34 +03:00
input_->setFixedHeight(32);
input_->setFont(font);
2017-10-04 11:33:34 +03:00
input_->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
input_->setPlaceholderText(tr("Write a message..."));
2017-10-04 11:33:34 +03:00
input_->setStyleSheet("color: #333333; border: none; margin: 0 5px");
2017-09-10 12:58:00 +03:00
sendMessageBtn_ = new FlatButton(this);
sendMessageBtn_->setForegroundColor(QColor("#acc7dc"));
QIcon send_message_icon;
send_message_icon.addFile(
":/icons/icons/share-dark.png", QSize(), QIcon::Normal, QIcon::Off);
2017-09-10 12:58:00 +03:00
sendMessageBtn_->setIcon(send_message_icon);
sendMessageBtn_->setIconSize(QSize(24, 24));
2017-09-10 12:58:00 +03:00
emojiBtn_ = new EmojiPickButton(this);
emojiBtn_->setForegroundColor(QColor("#acc7dc"));
QIcon emoji_icon;
emoji_icon.addFile(":/icons/icons/smile.png", QSize(), QIcon::Normal, QIcon::Off);
2017-09-10 12:58:00 +03:00
emojiBtn_->setIcon(emoji_icon);
emojiBtn_->setIconSize(QSize(24, 24));
2017-09-10 12:58:00 +03:00
topLayout_->addWidget(sendFileBtn_);
topLayout_->addWidget(input_);
topLayout_->addWidget(emojiBtn_);
topLayout_->addWidget(sendMessageBtn_);
2017-09-10 12:58:00 +03:00
setLayout(topLayout_);
2017-09-10 12:58:00 +03:00
connect(sendMessageBtn_, SIGNAL(clicked()), this, SLOT(onSendButtonClicked()));
connect(sendFileBtn_, SIGNAL(clicked()), this, SLOT(openFileSelection()));
connect(input_, SIGNAL(enterPressed()), sendMessageBtn_, SIGNAL(clicked()));
connect(emojiBtn_,
SIGNAL(emojiSelected(const QString &)),
this,
SLOT(addSelectedEmoji(const QString &)));
2017-04-23 21:31:08 +03:00
}
2017-08-20 13:47:22 +03:00
void
TextInputWidget::addSelectedEmoji(const QString &emoji)
2017-04-23 21:31:08 +03:00
{
QTextCursor cursor = input_->textCursor();
2017-04-23 21:31:08 +03:00
QFont emoji_font("Emoji One");
emoji_font.setPixelSize(conf::emojiSize);
2017-04-23 21:31:08 +03:00
QFont text_font("Open Sans");
text_font.setPixelSize(conf::fontSize);
2017-04-23 21:31:08 +03:00
QTextCharFormat charfmt;
charfmt.setFont(emoji_font);
input_->setCurrentCharFormat(charfmt);
2017-04-23 21:31:08 +03:00
input_->insertPlainText(emoji);
cursor.movePosition(QTextCursor::End);
2017-04-23 21:31:08 +03:00
charfmt.setFont(text_font);
input_->setCurrentCharFormat(charfmt);
2017-04-23 21:31:08 +03:00
input_->show();
2017-04-06 02:06:42 +03:00
}
2017-08-20 13:47:22 +03:00
void
TextInputWidget::onSendButtonClicked()
2017-04-06 02:06:42 +03:00
{
auto msgText = input_->document()->toPlainText().trimmed();
if (msgText.isEmpty())
return;
2017-04-06 02:06:42 +03:00
if (msgText.startsWith(EMOTE_COMMAND)) {
auto text = parseEmoteCommand(msgText);
2017-04-06 02:06:42 +03:00
if (!text.isEmpty())
emit sendEmoteMessage(text);
} else {
emit sendTextMessage(msgText);
}
2017-04-23 21:31:08 +03:00
input_->clear();
2017-04-06 02:06:42 +03:00
}
QString
TextInputWidget::parseEmoteCommand(const QString &cmd)
2017-04-06 02:06:42 +03:00
{
auto text = cmd.right(cmd.size() - EMOTE_COMMAND.size()).trimmed();
2017-04-06 02:06:42 +03:00
if (!text.isEmpty())
return text;
2017-04-06 02:06:42 +03:00
return QString("");
2017-04-06 02:06:42 +03:00
}
2017-09-10 12:58:00 +03:00
void
TextInputWidget::openFileSelection()
{
QStringList supportedFiles;
supportedFiles << "jpeg"
<< "gif"
<< "png"
<< "bmp"
<< "tiff"
<< "webp";
auto fileName = QFileDialog::getOpenFileName(
this,
tr("Select an image"),
"",
tr("Image Files (*.bmp *.gif *.jpg *.jpeg *.png *.tiff *.webp)"));
if (fileName.isEmpty())
return;
auto imageFormat = QString(QImageReader::imageFormat(fileName));
if (!supportedFiles.contains(imageFormat)) {
qDebug() << "Unsupported image format for" << fileName;
return;
}
emit uploadImage(fileName);
showUploadSpinner();
}
void
TextInputWidget::showUploadSpinner()
{
topLayout_->removeWidget(sendFileBtn_);
sendFileBtn_->hide();
topLayout_->insertWidget(0, spinner_);
spinner_->start();
}
void
TextInputWidget::hideUploadSpinner()
{
topLayout_->removeWidget(spinner_);
topLayout_->insertWidget(0, sendFileBtn_);
sendFileBtn_->show();
spinner_->stop();
}
2017-10-01 12:11:33 +03:00
TextInputWidget::~TextInputWidget() {}