Skip to content
Snippets Groups Projects
TextInputWidget.cc 4.81 KiB
Newer Older
Konstantinos Sideris's avatar
Konstantinos Sideris committed
/*
 * 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>
#include <QFile>
Konstantinos Sideris's avatar
Konstantinos Sideris committed
#include <QPainter>
#include <QStyleOption>

#include "Config.h"
Konstantinos Sideris's avatar
Konstantinos Sideris committed
#include "TextInputWidget.h"

FilteredTextEdit::FilteredTextEdit(QWidget *parent)
  : QTextEdit(parent)
        setAcceptRichText(false);
void
FilteredTextEdit::keyPressEvent(QKeyEvent *event)
        if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter)
                emit enterPressed();
        else
                QTextEdit::keyPressEvent(event);
Konstantinos Sideris's avatar
Konstantinos Sideris committed
TextInputWidget::TextInputWidget(QWidget *parent)
  : QFrame(parent)
Konstantinos Sideris's avatar
Konstantinos Sideris committed
{
        setFont(QFont("Emoji One"));

        setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
        setCursor(Qt::ArrowCursor);
        setStyleSheet("background-color: #f8fbfe; height: 45px;");

        top_layout_ = new QHBoxLayout();
        top_layout_->setSpacing(0);
        top_layout_->setMargin(0);

        send_file_button_ = new FlatButton(this);

        QIcon send_file_icon;
        send_file_icon.addFile(":/icons/icons/clip-dark.png", QSize(), QIcon::Normal, QIcon::Off);
        send_file_button_->setForegroundColor(QColor("#acc7dc"));
        send_file_button_->setIcon(send_file_icon);
        send_file_button_->setIconSize(QSize(24, 24));

        QFont font;
        font.setPixelSize(conf::fontSize);

        input_ = new FilteredTextEdit(this);
        input_->setFixedHeight(45);
        input_->setFont(font);
        input_->setPlaceholderText(tr("Write a message..."));
        input_->setStyleSheet("color: #333333; border-radius: 0; padding-top: 10px;");

        send_message_button_ = new FlatButton(this);
        send_message_button_->setForegroundColor(QColor("#acc7dc"));

        QIcon send_message_icon;
        send_message_icon.addFile(
          ":/icons/icons/share-dark.png", QSize(), QIcon::Normal, QIcon::Off);
        send_message_button_->setIcon(send_message_icon);
        send_message_button_->setIconSize(QSize(24, 24));

        emoji_button_ = new EmojiPickButton(this);
        emoji_button_->setForegroundColor(QColor("#acc7dc"));

        QIcon emoji_icon;
        emoji_icon.addFile(":/icons/icons/smile.png", QSize(), QIcon::Normal, QIcon::Off);
        emoji_button_->setIcon(emoji_icon);
        emoji_button_->setIconSize(QSize(24, 24));

        top_layout_->addWidget(send_file_button_);
        top_layout_->addWidget(input_);
        top_layout_->addWidget(emoji_button_);
        top_layout_->addWidget(send_message_button_);

        setLayout(top_layout_);

        connect(send_message_button_, SIGNAL(clicked()), this, SLOT(onSendButtonClicked()));
        connect(input_, SIGNAL(enterPressed()), send_message_button_, SIGNAL(clicked()));
        connect(emoji_button_,
                SIGNAL(emojiSelected(const QString &)),
                this,
                SLOT(addSelectedEmoji(const QString &)));
void
TextInputWidget::addSelectedEmoji(const QString &emoji)
        QTextCursor cursor = input_->textCursor();
        QFont emoji_font("Emoji One");
        emoji_font.setPixelSize(conf::emojiSize);
        QFont text_font("Open Sans");
        text_font.setPixelSize(conf::fontSize);
        QTextCharFormat charfmt;
        charfmt.setFont(emoji_font);
        input_->setCurrentCharFormat(charfmt);
        input_->insertPlainText(emoji);
        cursor.movePosition(QTextCursor::End);
        charfmt.setFont(text_font);
        input_->setCurrentCharFormat(charfmt);
void
TextInputWidget::onSendButtonClicked()
Konstantinos Sideris's avatar
Konstantinos Sideris committed
{
        auto msgText = input_->document()->toPlainText().trimmed();

        if (msgText.isEmpty())
                return;
        if (msgText.startsWith(EMOTE_COMMAND)) {
                auto text = parseEmoteCommand(msgText);
                if (!text.isEmpty())
                        emit sendEmoteMessage(text);
        } else {
                emit sendTextMessage(msgText);
        }
        input_->clear();
QString
TextInputWidget::parseEmoteCommand(const QString &cmd)
Konstantinos Sideris's avatar
Konstantinos Sideris committed
{
        auto text = cmd.right(cmd.size() - EMOTE_COMMAND.size()).trimmed();
        if (!text.isEmpty())
                return text;
        return QString("");
Konstantinos Sideris's avatar
Konstantinos Sideris committed
}

TextInputWidget::~TextInputWidget()
{
}