summaryrefslogtreecommitdiffstats
path: root/src/TextInputWidget.cc
blob: 62b25423ac8c40c746db3a0d04bf0a72e9e0c6dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
 * 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>
#include <QFileDialog>
#include <QImageReader>
#include <QPainter>
#include <QStyleOption>

#include "Config.h"
#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);
}

TextInputWidget::TextInputWidget(QWidget *parent)
  : QFrame(parent)
{
        setFont(QFont("Emoji One"));

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

        topLayout_ = new QHBoxLayout();
        topLayout_->setSpacing(2);
        topLayout_->setMargin(4);

        QIcon send_file_icon;
        send_file_icon.addFile(":/icons/icons/clip-dark.png", QSize(), QIcon::Normal, QIcon::Off);

        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");
        spinner_->setFixedHeight(40);
        spinner_->setFixedWidth(40);
        spinner_->hide();

        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;");

        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);
        sendMessageBtn_->setIcon(send_message_icon);
        sendMessageBtn_->setIconSize(QSize(24, 24));

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

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

        topLayout_->addWidget(sendFileBtn_);
        topLayout_->addWidget(input_);
        topLayout_->addWidget(emojiBtn_);
        topLayout_->addWidget(sendMessageBtn_);

        setLayout(topLayout_);

        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 &)));
}

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);

        input_->show();
}

void
TextInputWidget::onSendButtonClicked()
{
        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)
{
        auto text = cmd.right(cmd.size() - EMOTE_COMMAND.size()).trimmed();

        if (!text.isEmpty())
                return text;

        return QString("");
}

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();
}

TextInputWidget::~TextInputWidget()
{
}