matrixion/src/timeline/InputBar.h

319 lines
9.8 KiB
C
Raw Normal View History

2021-03-05 02:35:15 +03:00
// SPDX-FileCopyrightText: 2021 Nheko Contributors
// SPDX-FileCopyrightText: 2022 Nheko Contributors
2021-03-05 02:35:15 +03:00
//
// SPDX-License-Identifier: GPL-3.0-or-later
2020-11-01 01:24:07 +03:00
#pragma once
2022-03-21 02:48:27 +03:00
#include <QAbstractVideoSurface>
2022-03-21 00:49:33 +03:00
#include <QIODevice>
2022-03-21 02:48:27 +03:00
#include <QImage>
2020-11-01 01:24:07 +03:00
#include <QObject>
2022-03-21 00:49:33 +03:00
#include <QSize>
2021-12-28 22:09:08 +03:00
#include <QStringList>
2020-11-17 04:37:43 +03:00
#include <QTimer>
2022-03-21 00:49:33 +03:00
#include <QVariantList>
2020-11-09 05:12:37 +03:00
#include <deque>
2022-03-21 00:49:33 +03:00
#include <memory>
2020-11-01 01:24:07 +03:00
2020-11-15 06:52:49 +03:00
#include <mtx/common.hpp>
#include <mtx/responses/messages.hpp>
2020-11-01 01:24:07 +03:00
class TimelineModel;
2021-07-21 02:03:38 +03:00
class CombinedImagePackModel;
2020-11-15 06:52:49 +03:00
class QMimeData;
2020-11-25 19:02:23 +03:00
class QDropEvent;
2020-11-01 01:24:07 +03:00
enum class MarkdownOverride
{
2021-09-18 01:22:33 +03:00
NOT_SPECIFIED, // no override set
ON,
OFF,
};
2022-03-21 02:48:27 +03:00
class InputVideoSurface : public QAbstractVideoSurface
{
Q_OBJECT
public:
InputVideoSurface(QObject *parent)
: QAbstractVideoSurface(parent)
{}
bool present(const QVideoFrame &frame) override
{
QImage::Format format = QImage::Format_Invalid;
switch (frame.pixelFormat()) {
case QVideoFrame::Format_ARGB32:
format = QImage::Format_ARGB32;
break;
case QVideoFrame::Format_ARGB32_Premultiplied:
format = QImage::Format_ARGB32_Premultiplied;
break;
case QVideoFrame::Format_RGB24:
format = QImage::Format_RGB888;
break;
case QVideoFrame::Format_BGR24:
format = QImage::Format_BGR888;
break;
case QVideoFrame::Format_RGB32:
format = QImage::Format_RGB32;
break;
case QVideoFrame::Format_RGB565:
format = QImage::Format_RGB16;
break;
case QVideoFrame::Format_RGB555:
format = QImage::Format_RGB555;
break;
default:
format = QImage::Format_Invalid;
}
if (format == QImage::Format_Invalid) {
emit newImage({});
return false;
} else {
QVideoFrame frametodraw(frame);
if (!frametodraw.map(QAbstractVideoBuffer::ReadOnly)) {
emit newImage({});
return false;
}
// this is a shallow operation. it just refer the frame buffer
QImage image(frametodraw.bits(),
frametodraw.width(),
frametodraw.height(),
frametodraw.bytesPerLine(),
QImage::Format_RGB444);
emit newImage(std::move(image));
return true;
}
}
QList<QVideoFrame::PixelFormat>
supportedPixelFormats(QAbstractVideoBuffer::HandleType type) const override
{
if (type == QAbstractVideoBuffer::NoHandle) {
return {
QVideoFrame::Format_ARGB32,
QVideoFrame::Format_ARGB32_Premultiplied,
QVideoFrame::Format_RGB24,
QVideoFrame::Format_BGR24,
QVideoFrame::Format_RGB32,
QVideoFrame::Format_RGB565,
QVideoFrame::Format_RGB555,
};
} else {
return {};
}
}
signals:
void newImage(QImage img);
};
2022-03-21 00:49:33 +03:00
class MediaUpload : public QObject
{
Q_OBJECT
// Q_PROPERTY(bool uploading READ uploading NOTIFY uploadingChanged)
// Q_PROPERTY(MediaType mediaType READ type NOTIFY mediaTypeChanged)
// // https://stackoverflow.com/questions/33422265/pass-qimage-to-qml/68554646#68554646
// Q_PROPERTY(QUrl thumbnail READ thumbnail NOTIFY thumbnailChanged)
// Q_PROPERTY(QString humanSize READ humanSize NOTIFY huSizeChanged)
// Q_PROPERTY(QString filename READ filename NOTIFY filenameChanged)
// Q_PROPERTY(QString mimetype READ mimetype NOTIFY mimetypeChanged)
// Q_PROPERTY(int height READ height NOTIFY heightChanged)
// Q_PROPERTY(int width READ width NOTIFY widthChanged)
// thumbnail video
// https://stackoverflow.com/questions/26229633/display-on-screen-using-qabstractvideosurface
public:
enum MediaType
{
File,
Image,
Video,
Audio,
};
Q_ENUM(MediaType);
explicit MediaUpload(std::unique_ptr<QIODevice> data,
QString mimetype,
QString originalFilename,
bool encrypt,
QObject *parent = nullptr);
[[nodiscard]] QString url() const { return url_; }
[[nodiscard]] QString mimetype() const { return mimetype_; }
[[nodiscard]] QString mimeClass() const { return mimeClass_; }
[[nodiscard]] QString filename() const { return originalFilename_; }
[[nodiscard]] QString blurhash() const { return blurhash_; }
[[nodiscard]] uint64_t size() const { return size_; }
2022-03-21 02:48:27 +03:00
[[nodiscard]] uint64_t duration() const { return duration_; }
2022-03-21 00:49:33 +03:00
[[nodiscard]] std::optional<mtx::crypto::EncryptedFile> encryptedFile_()
{
return encryptedFile;
}
[[nodiscard]] QSize dimensions() const { return dimensions_; }
signals:
void uploadComplete(MediaUpload *self, QString url);
void uploadFailed(MediaUpload *self);
public slots:
void startUpload();
private slots:
void updateThumbnailUrl(QString url) { this->thumbnailUrl_ = std::move(url); }
2022-03-21 02:48:27 +03:00
void setThumbnail(QImage img) { this->thumbnail_ = std::move(img); }
2022-03-21 00:49:33 +03:00
public:
// void uploadThumbnail(QImage img);
std::unique_ptr<QIODevice> source;
QByteArray data;
QString mimetype_;
QString mimeClass_;
QString originalFilename_;
QString blurhash_;
QString thumbnailUrl_;
QString url_;
std::optional<mtx::crypto::EncryptedFile> encryptedFile;
2022-03-21 02:48:27 +03:00
QImage thumbnail_;
2022-03-21 00:49:33 +03:00
QSize dimensions_;
2022-03-21 02:48:27 +03:00
uint64_t size_ = 0;
uint64_t duration_ = 0;
2022-03-21 00:49:33 +03:00
bool encrypt_;
};
2020-11-09 05:12:37 +03:00
class InputBar : public QObject
{
2021-09-18 01:22:33 +03:00
Q_OBJECT
Q_PROPERTY(bool uploading READ uploading NOTIFY uploadingChanged)
Q_PROPERTY(bool containsAtRoom READ containsAtRoom NOTIFY containsAtRoomChanged)
Q_PROPERTY(QString text READ text NOTIFY textChanged)
2022-03-21 00:49:33 +03:00
Q_PROPERTY(QVariantList uploads READ uploads NOTIFY uploadsChanged)
2020-11-01 01:24:07 +03:00
public:
explicit InputBar(TimelineModel *parent)
2021-09-18 01:22:33 +03:00
: QObject()
, room(parent)
{
typingRefresh_.setInterval(10'000);
typingRefresh_.setSingleShot(true);
typingTimeout_.setInterval(5'000);
typingTimeout_.setSingleShot(true);
connect(&typingRefresh_, &QTimer::timeout, this, &InputBar::startTyping);
connect(&typingTimeout_, &QTimer::timeout, this, &InputBar::stopTyping);
}
2020-11-01 01:24:07 +03:00
2022-03-21 00:49:33 +03:00
QVariantList uploads() const;
2020-11-01 01:24:07 +03:00
public slots:
[[nodiscard]] QString text() const;
2021-09-18 01:22:33 +03:00
QString previousText();
QString nextText();
2021-12-03 03:54:43 +03:00
void setText(const QString &newText);
2021-09-18 01:22:33 +03:00
[[nodiscard]] bool containsAtRoom() const { return containsAtRoom_; }
2021-09-18 01:22:33 +03:00
void send();
void paste(bool fromMouse);
void insertMimeData(const QMimeData *data);
2021-12-03 03:54:43 +03:00
void updateState(int selectionStart, int selectionEnd, int cursorPosition, const QString &text);
2021-09-18 01:22:33 +03:00
void openFileSelection();
[[nodiscard]] bool uploading() const { return uploading_; }
2021-12-03 03:54:43 +03:00
void message(const QString &body,
2021-09-18 01:22:33 +03:00
MarkdownOverride useMarkdown = MarkdownOverride::NOT_SPECIFIED,
bool rainbowify = false);
void reaction(const QString &reactedEvent, const QString &reactionKey);
void sticker(CombinedImagePackModel *model, int row);
2020-11-01 01:24:07 +03:00
2022-03-21 00:49:33 +03:00
void acceptUploads();
void declineUploads();
2020-11-17 04:37:43 +03:00
private slots:
2021-09-18 01:22:33 +03:00
void startTyping();
void stopTyping();
2020-11-17 04:37:43 +03:00
2022-03-21 00:49:33 +03:00
void finalizeUpload(MediaUpload *upload, QString url);
void removeRunUpload(MediaUpload *upload);
2020-11-09 05:12:37 +03:00
signals:
2021-09-18 01:22:33 +03:00
void insertText(QString text);
void textChanged(QString newText);
void uploadingChanged(bool value);
void containsAtRoomChanged();
2022-03-21 00:49:33 +03:00
void uploadsChanged();
2020-11-09 05:12:37 +03:00
2020-11-01 01:24:07 +03:00
private:
2021-12-03 03:54:43 +03:00
void emote(const QString &body, bool rainbowify);
void notice(const QString &body, bool rainbowify);
void command(const QString &name, QString args);
2021-09-18 01:22:33 +03:00
void image(const QString &filename,
const std::optional<mtx::crypto::EncryptedFile> &file,
const QString &url,
const QString &mime,
uint64_t dsize,
const QSize &dimensions,
const QString &blurhash);
void file(const QString &filename,
const std::optional<mtx::crypto::EncryptedFile> &encryptedFile,
const QString &url,
const QString &mime,
uint64_t dsize);
void audio(const QString &filename,
const std::optional<mtx::crypto::EncryptedFile> &file,
const QString &url,
const QString &mime,
2022-03-21 02:48:27 +03:00
uint64_t dsize,
uint64_t duration);
2021-09-18 01:22:33 +03:00
void video(const QString &filename,
const std::optional<mtx::crypto::EncryptedFile> &file,
const QString &url,
const QString &mime,
2022-03-21 02:48:27 +03:00
uint64_t dsize,
uint64_t duration,
const QSize &dimensions);
2021-09-18 01:22:33 +03:00
2022-03-21 00:49:33 +03:00
void startUploadFromPath(const QString &path);
void startUploadFromMimeData(const QMimeData &source, const QString &format);
void startUpload(std::unique_ptr<QIODevice> dev, const QString &orgPath, const QString &format);
2021-09-18 01:22:33 +03:00
void setUploading(bool value)
{
if (value != uploading_) {
uploading_ = value;
emit uploadingChanged(value);
2020-11-15 06:52:49 +03:00
}
2021-09-18 01:22:33 +03:00
}
void updateAtRoom(const QString &t);
QTimer typingRefresh_;
QTimer typingTimeout_;
TimelineModel *room;
std::deque<QString> history_;
std::size_t history_index_ = 0;
int selectionStart = 0, selectionEnd = 0, cursorPosition = 0;
bool uploading_ = false;
bool containsAtRoom_ = false;
2022-03-21 00:49:33 +03:00
struct DeleteLaterDeleter
{
void operator()(QObject *p)
{
if (p)
p->deleteLater();
}
};
using UploadHandle = std::unique_ptr<MediaUpload, DeleteLaterDeleter>;
std::vector<UploadHandle> unconfirmedUploads;
std::vector<UploadHandle> runningUploads;
2020-11-01 01:24:07 +03:00
};