some creepy file attaching gui, not sending yet
This commit is contained in:
parent
2089d6af86
commit
100b2e8943
12 changed files with 424 additions and 6 deletions
60
ui/utils/badge.cpp
Normal file
60
ui/utils/badge.cpp
Normal file
|
@ -0,0 +1,60 @@
|
|||
/*
|
||||
* Squawk messenger.
|
||||
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
||||
*
|
||||
* 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 "badge.h"
|
||||
|
||||
Badge::Badge(const QString& p_id, const QString& p_text, const QIcon& icon, QWidget* parent):
|
||||
QFrame(parent),
|
||||
id(p_id),
|
||||
image(new QLabel()),
|
||||
text(new QLabel(p_text)),
|
||||
closeButton(new QPushButton()),
|
||||
layout(new QHBoxLayout(this))
|
||||
{
|
||||
setBackgroundRole(QPalette::Base);
|
||||
//setAutoFillBackground(true);
|
||||
setFrameStyle(QFrame::StyledPanel);
|
||||
setFrameShadow(QFrame::Raised);
|
||||
|
||||
image->setPixmap(icon.pixmap(25, 25));
|
||||
closeButton->setIcon(QIcon::fromTheme("tab-close"));
|
||||
closeButton->setMaximumHeight(25);
|
||||
closeButton->setMaximumWidth(25);
|
||||
|
||||
layout->addWidget(image);
|
||||
layout->addWidget(text);
|
||||
layout->addWidget(closeButton);
|
||||
|
||||
layout->setContentsMargins(2, 2, 2, 2);
|
||||
|
||||
connect(closeButton, SIGNAL(clicked()), this, SIGNAL(close()));
|
||||
}
|
||||
|
||||
Badge::~Badge()
|
||||
{
|
||||
}
|
||||
|
||||
bool Badge::Comparator::operator()(const Badge* a, const Badge* b) const
|
||||
{
|
||||
return a->id < b->id;
|
||||
}
|
||||
|
||||
bool Badge::Comparator::operator()(const Badge& a, const Badge& b) const
|
||||
{
|
||||
return a.id < b.id;
|
||||
}
|
56
ui/utils/badge.h
Normal file
56
ui/utils/badge.h
Normal file
|
@ -0,0 +1,56 @@
|
|||
/*
|
||||
* Squawk messenger.
|
||||
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#ifndef BADGE_H
|
||||
#define BADGE_H
|
||||
|
||||
#include <QFrame>
|
||||
#include <QLabel>
|
||||
#include <QHBoxLayout>
|
||||
#include <QIcon>
|
||||
#include <QPushButton>
|
||||
|
||||
/**
|
||||
* @todo write docs
|
||||
*/
|
||||
class Badge : public QFrame
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
Badge(const QString& id, const QString& text, const QIcon& icon, QWidget* parent = nullptr);
|
||||
~Badge();
|
||||
|
||||
const QString id;
|
||||
|
||||
signals:
|
||||
void close();
|
||||
|
||||
private:
|
||||
QLabel* image;
|
||||
QLabel* text;
|
||||
QPushButton* closeButton;
|
||||
QHBoxLayout* layout;
|
||||
|
||||
public:
|
||||
struct Comparator {
|
||||
bool operator()(const Badge& a, const Badge& b) const;
|
||||
bool operator()(const Badge* a, const Badge* b) const;
|
||||
};
|
||||
};
|
||||
|
||||
#endif // BADGE_H
|
172
ui/utils/flowlayout.cpp
Normal file
172
ui/utils/flowlayout.cpp
Normal file
|
@ -0,0 +1,172 @@
|
|||
/*
|
||||
* Squawk messenger.
|
||||
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
||||
*
|
||||
* 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 "flowlayout.h"
|
||||
|
||||
FlowLayout::FlowLayout(QWidget *parent, int margin, int hSpacing, int vSpacing):
|
||||
QLayout(parent),
|
||||
m_hSpace(hSpacing),
|
||||
m_vSpace(vSpacing)
|
||||
{
|
||||
setContentsMargins(margin, margin, margin, margin);
|
||||
}
|
||||
|
||||
FlowLayout::FlowLayout(int margin, int hSpacing, int vSpacing):
|
||||
m_hSpace(hSpacing),
|
||||
m_vSpace(vSpacing)
|
||||
{
|
||||
setContentsMargins(margin, margin, margin, margin);
|
||||
}
|
||||
|
||||
FlowLayout::~FlowLayout()
|
||||
{
|
||||
QLayoutItem *item;
|
||||
while ((item = takeAt(0))) {
|
||||
delete item;
|
||||
}
|
||||
}
|
||||
|
||||
void FlowLayout::addItem(QLayoutItem *item)
|
||||
{
|
||||
itemList.append(item);
|
||||
}
|
||||
|
||||
int FlowLayout::horizontalSpacing() const
|
||||
{
|
||||
if (m_hSpace >= 0) {
|
||||
return m_hSpace;
|
||||
} else {
|
||||
return smartSpacing(QStyle::PM_LayoutHorizontalSpacing);
|
||||
}
|
||||
}
|
||||
|
||||
int FlowLayout::verticalSpacing() const
|
||||
{
|
||||
if (m_vSpace >= 0) {
|
||||
return m_vSpace;
|
||||
} else {
|
||||
return smartSpacing(QStyle::PM_LayoutVerticalSpacing);
|
||||
}
|
||||
}
|
||||
|
||||
int FlowLayout::count() const
|
||||
{
|
||||
return itemList.size();
|
||||
}
|
||||
|
||||
QLayoutItem *FlowLayout::itemAt(int index) const
|
||||
{
|
||||
return itemList.value(index);
|
||||
}
|
||||
|
||||
QLayoutItem *FlowLayout::takeAt(int index)
|
||||
{
|
||||
if (index >= 0 && index < itemList.size()) {
|
||||
return itemList.takeAt(index);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Qt::Orientations FlowLayout::expandingDirections() const
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool FlowLayout::hasHeightForWidth() const
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
int FlowLayout::heightForWidth(int width) const
|
||||
{
|
||||
int height = doLayout(QRect(0, 0, width, 0), true);
|
||||
return height;
|
||||
}
|
||||
|
||||
void FlowLayout::setGeometry(const QRect &rect)
|
||||
{
|
||||
QLayout::setGeometry(rect);
|
||||
doLayout(rect, false);
|
||||
}
|
||||
|
||||
QSize FlowLayout::sizeHint() const
|
||||
{
|
||||
return minimumSize();
|
||||
}
|
||||
|
||||
QSize FlowLayout::minimumSize() const
|
||||
{
|
||||
QSize size;
|
||||
for (const QLayoutItem *item : qAsConst(itemList)) {
|
||||
size = size.expandedTo(item->minimumSize());
|
||||
}
|
||||
|
||||
const QMargins margins = contentsMargins();
|
||||
size += QSize(margins.left() + margins.right(), margins.top() + margins.bottom());
|
||||
return size;
|
||||
}
|
||||
|
||||
int FlowLayout::doLayout(const QRect &rect, bool testOnly) const
|
||||
{
|
||||
int left, top, right, bottom;
|
||||
getContentsMargins(&left, &top, &right, &bottom);
|
||||
QRect effectiveRect = rect.adjusted(+left, +top, -right, -bottom);
|
||||
int x = effectiveRect.x();
|
||||
int y = effectiveRect.y();
|
||||
int lineHeight = 0;
|
||||
|
||||
for (QLayoutItem *item : qAsConst(itemList)) {
|
||||
const QWidget *wid = item->widget();
|
||||
int spaceX = horizontalSpacing();
|
||||
if (spaceX == -1) {
|
||||
spaceX = wid->style()->layoutSpacing(QSizePolicy::PushButton, QSizePolicy::PushButton, Qt::Horizontal);
|
||||
}
|
||||
int spaceY = verticalSpacing();
|
||||
if (spaceY == -1) {
|
||||
spaceY = wid->style()->layoutSpacing(QSizePolicy::PushButton, QSizePolicy::PushButton, Qt::Vertical);
|
||||
}
|
||||
int nextX = x + item->sizeHint().width() + spaceX;
|
||||
if (nextX - spaceX > effectiveRect.right() && lineHeight > 0) {
|
||||
x = effectiveRect.x();
|
||||
y = y + lineHeight + spaceY;
|
||||
nextX = x + item->sizeHint().width() + spaceX;
|
||||
lineHeight = 0;
|
||||
}
|
||||
|
||||
if (!testOnly) {
|
||||
item->setGeometry(QRect(QPoint(x, y), item->sizeHint()));
|
||||
}
|
||||
|
||||
x = nextX;
|
||||
lineHeight = qMax(lineHeight, item->sizeHint().height());
|
||||
}
|
||||
return y + lineHeight - rect.y() + bottom;
|
||||
}
|
||||
|
||||
int FlowLayout::smartSpacing(QStyle::PixelMetric pm) const
|
||||
{
|
||||
QObject *parent = this->parent();
|
||||
if (!parent) {
|
||||
return -1;
|
||||
} else if (parent->isWidgetType()) {
|
||||
QWidget *pw = static_cast<QWidget *>(parent);
|
||||
return pw->style()->pixelMetric(pm, nullptr, pw);
|
||||
} else {
|
||||
return static_cast<QLayout *>(parent)->spacing();
|
||||
}
|
||||
}
|
59
ui/utils/flowlayout.h
Normal file
59
ui/utils/flowlayout.h
Normal file
|
@ -0,0 +1,59 @@
|
|||
/*
|
||||
* Squawk messenger.
|
||||
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#ifndef FLOWLAYOUT_H
|
||||
#define FLOWLAYOUT_H
|
||||
|
||||
#include <QLayout>
|
||||
#include <QWidget>
|
||||
#include <QStyle>
|
||||
|
||||
/**
|
||||
* @todo write docs
|
||||
*/
|
||||
class FlowLayout : public QLayout
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit FlowLayout(QWidget *parent, int margin = -1, int hSpacing = -1, int vSpacing = -1);
|
||||
explicit FlowLayout(int margin = -1, int hSpacing = -1, int vSpacing = -1);
|
||||
~FlowLayout();
|
||||
|
||||
void addItem(QLayoutItem *item) override;
|
||||
int horizontalSpacing() const;
|
||||
int verticalSpacing() const;
|
||||
Qt::Orientations expandingDirections() const override;
|
||||
bool hasHeightForWidth() const override;
|
||||
int heightForWidth(int) const override;
|
||||
int count() const override;
|
||||
QLayoutItem *itemAt(int index) const override;
|
||||
QSize minimumSize() const override;
|
||||
void setGeometry(const QRect &rect) override;
|
||||
QSize sizeHint() const override;
|
||||
QLayoutItem *takeAt(int index) override;
|
||||
|
||||
private:
|
||||
int doLayout(const QRect &rect, bool testOnly) const;
|
||||
int smartSpacing(QStyle::PixelMetric pm) const;
|
||||
|
||||
QList<QLayoutItem *> itemList;
|
||||
int m_hSpace;
|
||||
int m_vSpace;
|
||||
};
|
||||
|
||||
#endif // FLOWLAYOUT_H
|
245
ui/utils/message.cpp
Normal file
245
ui/utils/message.cpp
Normal file
|
@ -0,0 +1,245 @@
|
|||
/*
|
||||
* Squawk messenger.
|
||||
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
||||
*
|
||||
* 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 <QMimeDatabase>
|
||||
#include <QPixmap>
|
||||
#include <QFileInfo>
|
||||
#include "message.h"
|
||||
|
||||
const QRegExp urlReg("^(?!<img\\ssrc=\")((?:https?|ftp)://\\S+)");
|
||||
const QRegExp imgReg("((?:https?|ftp)://\\S+\\.(?:jpg|jpeg|png|svg|gif))");
|
||||
|
||||
Message::Message(const Shared::Message& source, bool outgoing, const QString& p_sender, QWidget* parent):
|
||||
QHBoxLayout(parent),
|
||||
msg(source),
|
||||
body(new QWidget()),
|
||||
bodyLayout(new QVBoxLayout(body)),
|
||||
date(new QLabel(msg.getTime().toLocalTime().toString())),
|
||||
sender(new QLabel(p_sender)),
|
||||
text(new QLabel()),
|
||||
shadow(new QGraphicsDropShadowEffect()),
|
||||
downloadButton(0),
|
||||
file(0),
|
||||
progress(0),
|
||||
fileComment(new QLabel()),
|
||||
errorText(""),
|
||||
hasDownloadButton(false),
|
||||
hasProgress(false),
|
||||
hasFile(false),
|
||||
commentAdded(false),
|
||||
errorDownloadingFile(false)
|
||||
{
|
||||
body->setBackgroundRole(QPalette::AlternateBase);
|
||||
body->setAutoFillBackground(true);
|
||||
|
||||
QString bd = msg.getBody();
|
||||
//bd.replace(imgReg, "<img src=\"\\1\"/>");
|
||||
bd.replace(urlReg, "<a href=\"\\1\">\\1</a>");
|
||||
text->setText(bd);;
|
||||
text->setTextInteractionFlags(text->textInteractionFlags() | Qt::TextSelectableByMouse | Qt::LinksAccessibleByMouse);
|
||||
text->setWordWrap(true);
|
||||
text->setOpenExternalLinks(true);
|
||||
if (bd.size() == 0) {
|
||||
text->hide();
|
||||
}
|
||||
|
||||
QFont dFont = date->font();
|
||||
dFont.setItalic(true);
|
||||
dFont.setPointSize(dFont.pointSize() - 2);
|
||||
date->setFont(dFont);
|
||||
date->setForegroundRole(QPalette::ToolTipText);
|
||||
|
||||
QFont f;
|
||||
f.setBold(true);
|
||||
sender->setFont(f);
|
||||
|
||||
bodyLayout->addWidget(sender);
|
||||
bodyLayout->addWidget(text);
|
||||
bodyLayout->addWidget(date);
|
||||
|
||||
shadow->setBlurRadius(10);
|
||||
shadow->setXOffset(1);
|
||||
shadow->setYOffset(1);
|
||||
shadow->setColor(Qt::black);
|
||||
body->setGraphicsEffect(shadow);
|
||||
|
||||
if (outgoing) {
|
||||
addWidget(body);
|
||||
addStretch();
|
||||
} else {
|
||||
sender->setAlignment(Qt::AlignRight);
|
||||
date->setAlignment(Qt::AlignRight);
|
||||
addStretch();
|
||||
addWidget(body);
|
||||
}
|
||||
}
|
||||
|
||||
Message::~Message()
|
||||
{
|
||||
if (!commentAdded) {
|
||||
delete fileComment;
|
||||
}
|
||||
}
|
||||
|
||||
QString Message::getId() const
|
||||
{
|
||||
return msg.getId();
|
||||
}
|
||||
|
||||
void Message::setSender(const QString& p_sender)
|
||||
{
|
||||
sender->setText(p_sender);
|
||||
}
|
||||
|
||||
void Message::addDownloadDialog()
|
||||
{
|
||||
hideFile();
|
||||
hideProgress();
|
||||
if (!hasDownloadButton) {
|
||||
hideComment();
|
||||
if (msg.getBody() == msg.getOutOfBandUrl()) {
|
||||
text->setText("");
|
||||
text->hide();
|
||||
}
|
||||
downloadButton = new QPushButton(QIcon::fromTheme("download"), "Download");
|
||||
downloadButton->setToolTip("<a href=\"" + msg.getOutOfBandUrl() + "\">" + msg.getOutOfBandUrl() + "</a>");
|
||||
if (errorDownloadingFile) {
|
||||
fileComment->setWordWrap(true);
|
||||
fileComment->setText("Error downloading file: " + errorText + "\nYou can try again");
|
||||
} else {
|
||||
fileComment->setText(sender->text() + " is offering you to download a file");
|
||||
}
|
||||
fileComment->show();
|
||||
connect(downloadButton, SIGNAL(clicked()), this, SLOT(onDownload()));
|
||||
bodyLayout->insertWidget(2, fileComment);
|
||||
bodyLayout->insertWidget(3, downloadButton);
|
||||
hasDownloadButton = true;
|
||||
commentAdded = true;
|
||||
}
|
||||
}
|
||||
|
||||
void Message::onDownload()
|
||||
{
|
||||
emit downloadFile(msg.getId(), msg.getOutOfBandUrl());
|
||||
}
|
||||
|
||||
void Message::setProgress(qreal value)
|
||||
{
|
||||
hideFile();
|
||||
hideDownload();
|
||||
if (!hasProgress) {
|
||||
hideComment();
|
||||
if (msg.getBody() == msg.getOutOfBandUrl()) {
|
||||
text->setText("");
|
||||
text->hide();
|
||||
}
|
||||
progress = new QProgressBar();
|
||||
progress->setRange(0, 100);
|
||||
fileComment->setText("Downloading...");
|
||||
fileComment->show();
|
||||
bodyLayout->insertWidget(2, progress);
|
||||
bodyLayout->insertWidget(3, fileComment);
|
||||
hasProgress = true;
|
||||
commentAdded = true;
|
||||
}
|
||||
progress->setValue(value * 100);
|
||||
}
|
||||
|
||||
void Message::showFile(const QString& path)
|
||||
{
|
||||
hideDownload();
|
||||
hideProgress();
|
||||
if (!hasFile) {
|
||||
hideComment();
|
||||
if (msg.getBody() == msg.getOutOfBandUrl()) {
|
||||
text->setText("");
|
||||
text->hide();
|
||||
}
|
||||
QMimeDatabase db;
|
||||
QMimeType type = db.mimeTypeForFile(path);
|
||||
QStringList parts = type.name().split("/");
|
||||
QString big = parts.front();
|
||||
QFileInfo info(path);
|
||||
fileComment = new QLabel();
|
||||
if (big == "image") {
|
||||
file = new Image(path);
|
||||
} else {
|
||||
file = new QLabel();
|
||||
file->setPixmap(QIcon::fromTheme(type.iconName()).pixmap(50));
|
||||
file->setAlignment(Qt::AlignCenter);
|
||||
fileComment->setText(info.fileName());
|
||||
fileComment->setWordWrap(true);
|
||||
fileComment->show();
|
||||
}
|
||||
file->setContextMenuPolicy(Qt::ActionsContextMenu);
|
||||
QAction* openAction = new QAction(QIcon::fromTheme("document-new-from-template"), "Open", file);
|
||||
connect(openAction, &QAction::triggered, [path]() { //TODO need to get rid of this shame
|
||||
QDesktopServices::openUrl(QUrl::fromLocalFile(path));
|
||||
});
|
||||
file->addAction(openAction);
|
||||
bodyLayout->insertWidget(2, file);
|
||||
bodyLayout->insertWidget(3, fileComment);
|
||||
hasFile = true;
|
||||
commentAdded = true;
|
||||
}
|
||||
}
|
||||
|
||||
void Message::hideComment()
|
||||
{
|
||||
if (commentAdded) {
|
||||
bodyLayout->removeWidget(fileComment);
|
||||
fileComment->hide();
|
||||
fileComment->setWordWrap(false);
|
||||
}
|
||||
}
|
||||
|
||||
void Message::hideDownload()
|
||||
{
|
||||
if (hasDownloadButton) {
|
||||
downloadButton->deleteLater();
|
||||
downloadButton = 0;
|
||||
hasDownloadButton = false;
|
||||
errorDownloadingFile = false;
|
||||
}
|
||||
}
|
||||
|
||||
void Message::hideFile()
|
||||
{
|
||||
if (hasFile) {
|
||||
file->deleteLater();
|
||||
file = 0;
|
||||
hasFile = false;
|
||||
}
|
||||
}
|
||||
|
||||
void Message::hideProgress()
|
||||
{
|
||||
if (hasProgress) {
|
||||
progress->deleteLater();
|
||||
progress = 0;
|
||||
hasProgress = false;;
|
||||
}
|
||||
}
|
||||
|
||||
void Message::showError(const QString& error)
|
||||
{
|
||||
errorDownloadingFile = true;
|
||||
errorText = error;
|
||||
addDownloadDialog();
|
||||
}
|
87
ui/utils/message.h
Normal file
87
ui/utils/message.h
Normal file
|
@ -0,0 +1,87 @@
|
|||
/*
|
||||
* Squawk messenger.
|
||||
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#ifndef MESSAGE_H
|
||||
#define MESSAGE_H
|
||||
|
||||
#include <QWidget>
|
||||
#include <QHBoxLayout>
|
||||
#include <QVBoxLayout>
|
||||
#include <QLabel>
|
||||
#include <QGraphicsDropShadowEffect>
|
||||
#include <QPushButton>
|
||||
#include <QProgressBar>
|
||||
#include <QAction>
|
||||
#include <QDesktopServices>
|
||||
#include <QUrl>
|
||||
|
||||
#include "../../global.h"
|
||||
#include "../utils/resizer.h"
|
||||
#include "../utils/image.h"
|
||||
|
||||
/**
|
||||
* @todo write docs
|
||||
*/
|
||||
class Message : public QHBoxLayout
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
Message(const Shared::Message& source, bool outgoing, const QString& sender, QWidget* parent = nullptr);
|
||||
~Message();
|
||||
|
||||
void setSender(const QString& sender);
|
||||
QString getId() const;
|
||||
|
||||
void addDownloadDialog();
|
||||
void showFile(const QString& path);
|
||||
void showError(const QString& error);
|
||||
void setProgress(qreal value);
|
||||
|
||||
signals:
|
||||
void downloadFile(const QString& messageId, const QString& url);
|
||||
|
||||
private:
|
||||
Shared::Message msg;
|
||||
QWidget* body;
|
||||
QVBoxLayout* bodyLayout;
|
||||
QLabel* date;
|
||||
QLabel* sender;
|
||||
QLabel* text;
|
||||
QGraphicsDropShadowEffect* shadow;
|
||||
QPushButton* downloadButton;
|
||||
QLabel* file;
|
||||
QProgressBar* progress;
|
||||
QLabel* fileComment;
|
||||
QString errorText;
|
||||
bool hasDownloadButton;
|
||||
bool hasProgress;
|
||||
bool hasFile;
|
||||
bool commentAdded;
|
||||
bool errorDownloadingFile;
|
||||
|
||||
private slots:
|
||||
void onDownload();
|
||||
|
||||
private:
|
||||
void hideDownload();
|
||||
void hideProgress();
|
||||
void hideFile();
|
||||
void hideComment();
|
||||
};
|
||||
|
||||
#endif // MESSAGE_H
|
259
ui/utils/messageline.cpp
Normal file
259
ui/utils/messageline.cpp
Normal file
|
@ -0,0 +1,259 @@
|
|||
/*
|
||||
* Squawk messenger.
|
||||
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
||||
*
|
||||
* 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 "messageline.h"
|
||||
#include <QDebug>
|
||||
#include <cmath>
|
||||
|
||||
MessageLine::MessageLine(bool p_room, QWidget* parent):
|
||||
QWidget(parent),
|
||||
messageIndex(),
|
||||
messageOrder(),
|
||||
myMessages(),
|
||||
palMessages(),
|
||||
layout(new QVBoxLayout(this)),
|
||||
myName(),
|
||||
palNames(),
|
||||
views(),
|
||||
room(p_room),
|
||||
busyPixmap(new QGraphicsPixmapItem(Shared::icon("view-refresh", true).pixmap(70))),
|
||||
busyScene(),
|
||||
busyLabel(&busyScene),
|
||||
busyLayout(),
|
||||
busyShown(false),
|
||||
rotation()
|
||||
{
|
||||
setBackgroundRole(QPalette::Base);
|
||||
layout->addStretch();
|
||||
|
||||
busyScene.addItem(busyPixmap);
|
||||
busyLayout.addStretch();
|
||||
busyLayout.addWidget(&busyLabel);
|
||||
busyLayout.addStretch();
|
||||
busyLabel.setMaximumSize(70, 70);
|
||||
busyLabel.setMinimumSize(70, 70);
|
||||
busyLabel.setSceneRect(0, 0, 70, 70);
|
||||
busyLabel.setFrameStyle(0);
|
||||
busyLabel.setContentsMargins(0, 0, 0, 0);
|
||||
busyLabel.setInteractive(false);
|
||||
busyPixmap->setTransformOriginPoint(35, 35);
|
||||
busyPixmap->setTransformationMode(Qt::SmoothTransformation);
|
||||
busyPixmap->setOffset(0, 0);;
|
||||
|
||||
rotation.setDuration(500);
|
||||
rotation.setStartValue(0.0f);
|
||||
rotation.setEndValue(180.0f);
|
||||
rotation.setLoopCount(-1);
|
||||
connect(&rotation, SIGNAL(valueChanged(const QVariant&)), this, SLOT(onAnimationValueChanged(const QVariant&)));
|
||||
}
|
||||
|
||||
MessageLine::~MessageLine()
|
||||
{
|
||||
for (Index::const_iterator itr = messageIndex.begin(), end = messageIndex.end(); itr != end; ++itr) {
|
||||
delete itr->second;
|
||||
}
|
||||
}
|
||||
|
||||
MessageLine::Position MessageLine::message(const Shared::Message& msg)
|
||||
{
|
||||
QString id = msg.getId();
|
||||
Index::iterator itr = messageIndex.find(id);
|
||||
if (itr != messageIndex.end()) {
|
||||
qDebug() << "received more then one message with the same id, skipping yet the new one";
|
||||
return invalid;
|
||||
}
|
||||
|
||||
QString sender;
|
||||
bool outgoing;
|
||||
|
||||
if (room) {
|
||||
if (msg.getFromResource() == myName) {
|
||||
sender = myName;
|
||||
outgoing = false;
|
||||
} else {
|
||||
sender = msg.getFromResource();
|
||||
outgoing = true;
|
||||
}
|
||||
} else {
|
||||
if (msg.getOutgoing()) {
|
||||
sender = myName;
|
||||
outgoing = false;
|
||||
} else {
|
||||
QString jid = msg.getFromJid();
|
||||
std::map<QString, QString>::iterator itr = palNames.find(jid);
|
||||
if (itr != palNames.end()) {
|
||||
sender = itr->second;
|
||||
} else {
|
||||
sender = jid;
|
||||
}
|
||||
outgoing = true;
|
||||
}
|
||||
}
|
||||
|
||||
Message* message = new Message(msg, outgoing, sender);
|
||||
|
||||
std::pair<Order::const_iterator, bool> result = messageOrder.insert(std::make_pair(msg.getTime(), message));
|
||||
if (!result.second) {
|
||||
qDebug() << "Error appending a message into a message list - seems like the time of that message exactly matches the time of some other message, can't put them in order, skipping yet";
|
||||
delete message;
|
||||
return invalid;
|
||||
}
|
||||
if (outgoing) {
|
||||
if (room) {
|
||||
|
||||
} else {
|
||||
QString jid = msg.getFromJid();
|
||||
std::map<QString, Index>::iterator pItr = palMessages.find(jid);
|
||||
if (pItr == palMessages.end()) {
|
||||
pItr = palMessages.insert(std::make_pair(jid, Index())).first;
|
||||
}
|
||||
pItr->second.insert(std::make_pair(id, message));
|
||||
}
|
||||
} else {
|
||||
myMessages.insert(std::make_pair(id, message));
|
||||
}
|
||||
messageIndex.insert(std::make_pair(id, message));
|
||||
int index = std::distance<Order::const_iterator>(messageOrder.begin(), result.first); //need to make with binary indexed tree
|
||||
Position res = invalid;
|
||||
if (index == 0) {
|
||||
res = beggining;
|
||||
} else if (index == messageIndex.size() - 1) {
|
||||
res = end;
|
||||
} else {
|
||||
res = middle;
|
||||
}
|
||||
|
||||
if (busyShown) {
|
||||
index += 1;
|
||||
}
|
||||
|
||||
|
||||
if (res == end) {
|
||||
layout->addLayout(message);
|
||||
} else {
|
||||
layout->insertLayout(index, message);
|
||||
}
|
||||
|
||||
if (msg.hasOutOfBandUrl()) {\
|
||||
emit requestLocalFile(msg.getId(), msg.getOutOfBandUrl());
|
||||
connect(message, SIGNAL(downloadFile(const QString&, const QString&)), this, SIGNAL(downloadFile(const QString&, const QString&)));
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
void MessageLine::setMyName(const QString& name)
|
||||
{
|
||||
myName = name;
|
||||
for (Index::const_iterator itr = myMessages.begin(), end = myMessages.end(); itr != end; ++itr) {
|
||||
itr->second->setSender(name);
|
||||
}
|
||||
}
|
||||
|
||||
void MessageLine::setPalName(const QString& jid, const QString& name)
|
||||
{
|
||||
std::map<QString, QString>::iterator itr = palNames.find(jid);
|
||||
if (itr == palNames.end()) {
|
||||
palNames.insert(std::make_pair(jid, name));
|
||||
} else {
|
||||
itr->second = name;
|
||||
}
|
||||
|
||||
std::map<QString, Index>::iterator pItr = palMessages.find(jid);
|
||||
if (pItr != palMessages.end()) {
|
||||
for (Index::const_iterator itr = pItr->second.begin(), end = pItr->second.end(); itr != end; ++itr) {
|
||||
itr->second->setSender(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MessageLine::resizeEvent(QResizeEvent* event)
|
||||
{
|
||||
QWidget::resizeEvent(event);
|
||||
emit resize(event->size().height() - event->oldSize().height());
|
||||
}
|
||||
|
||||
|
||||
QString MessageLine::firstMessageId() const
|
||||
{
|
||||
if (messageOrder.size() == 0) {
|
||||
return "";
|
||||
} else {
|
||||
return messageOrder.begin()->second->getId();
|
||||
}
|
||||
}
|
||||
|
||||
void MessageLine::showBusyIndicator()
|
||||
{
|
||||
if (!busyShown) {
|
||||
layout->insertLayout(0, &busyLayout);
|
||||
busyShown = true;
|
||||
rotation.start();
|
||||
busyLabel.show();
|
||||
}
|
||||
}
|
||||
|
||||
void MessageLine::hideBusyIndicator()
|
||||
{
|
||||
if (busyShown) {
|
||||
busyLabel.hide();
|
||||
rotation.stop();
|
||||
layout->removeItem(&busyLayout);
|
||||
busyShown = false;
|
||||
}
|
||||
}
|
||||
|
||||
void MessageLine::onAnimationValueChanged(const QVariant& value)
|
||||
{
|
||||
busyPixmap->setRotation(value.toReal());
|
||||
}
|
||||
|
||||
void MessageLine::responseDownloadProgress(const QString& messageId, qreal progress)
|
||||
{
|
||||
Index::const_iterator itr = messageIndex.find(messageId);
|
||||
if (itr == messageIndex.end()) {
|
||||
|
||||
} else {
|
||||
itr->second->setProgress(progress);
|
||||
}
|
||||
}
|
||||
|
||||
void MessageLine::responseLocalFile(const QString& messageId, const QString& path)
|
||||
{
|
||||
Index::const_iterator itr = messageIndex.find(messageId);
|
||||
if (itr == messageIndex.end()) {
|
||||
|
||||
} else {
|
||||
if (path.size() > 0) {
|
||||
itr->second->showFile(path);
|
||||
} else {
|
||||
itr->second->addDownloadDialog();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MessageLine::downloadError(const QString& messageId, const QString& error)
|
||||
{
|
||||
Index::const_iterator itr = messageIndex.find(messageId);
|
||||
if (itr == messageIndex.end()) {
|
||||
|
||||
} else {
|
||||
itr->second->showError(error);
|
||||
}
|
||||
}
|
||||
|
99
ui/utils/messageline.h
Normal file
99
ui/utils/messageline.h
Normal file
|
@ -0,0 +1,99 @@
|
|||
/*
|
||||
* Squawk messenger.
|
||||
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#ifndef MESSAGELINE_H
|
||||
#define MESSAGELINE_H
|
||||
|
||||
#include <QWidget>
|
||||
#include <QVBoxLayout>
|
||||
#include <QHBoxLayout>
|
||||
#include <QLabel>
|
||||
#include <QResizeEvent>
|
||||
#include <QIcon>
|
||||
#include <QGraphicsScene>
|
||||
#include <QGraphicsView>
|
||||
#include <QGraphicsPixmapItem>
|
||||
#include <QVariantAnimation>
|
||||
|
||||
#include "../global.h"
|
||||
#include "message.h"
|
||||
|
||||
class MessageLine : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
enum Position {
|
||||
beggining,
|
||||
middle,
|
||||
end,
|
||||
invalid
|
||||
};
|
||||
MessageLine(bool p_room, QWidget* parent = 0);
|
||||
~MessageLine();
|
||||
|
||||
Position message(const Shared::Message& msg);
|
||||
void setMyName(const QString& name);
|
||||
void setPalName(const QString& jid, const QString& name);
|
||||
QString firstMessageId() const;
|
||||
void showBusyIndicator();
|
||||
void hideBusyIndicator();
|
||||
void responseLocalFile(const QString& messageId, const QString& path);
|
||||
void downloadError(const QString& messageId, const QString& error);
|
||||
void responseDownloadProgress(const QString& messageId, qreal progress);
|
||||
|
||||
signals:
|
||||
void resize(int amount);
|
||||
void downloadFile(const QString& messageId, const QString& url);
|
||||
void requestLocalFile(const QString& messageId, const QString& url);
|
||||
|
||||
protected:
|
||||
void resizeEvent(QResizeEvent * event) override;
|
||||
|
||||
private:
|
||||
struct Comparator {
|
||||
bool operator()(const Shared::Message& a, const Shared::Message& b) const {
|
||||
return a.getTime() < b.getTime();
|
||||
}
|
||||
bool operator()(const Shared::Message* a, const Shared::Message* b) const {
|
||||
return a->getTime() < b->getTime();
|
||||
}
|
||||
};
|
||||
typedef std::map<QDateTime, Message*> Order;
|
||||
typedef std::map<QString, Message*> Index;
|
||||
Index messageIndex;
|
||||
Order messageOrder;
|
||||
Index myMessages;
|
||||
std::map<QString, Index> palMessages;
|
||||
QVBoxLayout* layout;
|
||||
|
||||
QString myName;
|
||||
std::map<QString, QString> palNames;
|
||||
std::deque<QHBoxLayout*> views;
|
||||
bool room;
|
||||
QGraphicsPixmapItem* busyPixmap;
|
||||
QGraphicsScene busyScene;
|
||||
QGraphicsView busyLabel;
|
||||
QHBoxLayout busyLayout;
|
||||
bool busyShown;
|
||||
QVariantAnimation rotation;
|
||||
|
||||
private slots:
|
||||
void onAnimationValueChanged(const QVariant& value);
|
||||
};
|
||||
|
||||
#endif // MESSAGELINE_H
|
Loading…
Add table
Add a link
Reference in a new issue