aspectRation handling for images, other then images types of downloads, progress fix, labels, context menu to open files

This commit is contained in:
Blue 2019-09-18 01:05:32 +03:00
parent 94a3766aaf
commit cc54c6393a
10 changed files with 307 additions and 80 deletions

View File

@ -115,8 +115,12 @@ void Core::NetworkAccess::onDownloadProgress(qint64 bytesReceived, qint64 bytesT
qDebug() << "an error downloading" << url << ": the request had some progress but seems like noone is waiting for it, skipping"; qDebug() << "an error downloading" << url << ": the request had some progress but seems like noone is waiting for it, skipping";
} else { } else {
Download* dwn = itr->second; Download* dwn = itr->second;
qreal received = bytesReceived;
qreal total = bytesTotal;
qreal progress = received/total;
dwn->progress = progress;
for (std::set<QString>::const_iterator mItr = dwn->messages.begin(), end = dwn->messages.end(); mItr != end; ++mItr) { for (std::set<QString>::const_iterator mItr = dwn->messages.begin(), end = dwn->messages.end(); mItr != end; ++mItr) {
emit downloadFileProgress(*mItr, bytesReceived/bytesTotal); emit downloadFileProgress(*mItr, progress);
} }
} }
} }

View File

@ -31,6 +31,8 @@ set(squawkUI_SRC
widgets/account.cpp widgets/account.cpp
widgets/joinconference.cpp widgets/joinconference.cpp
widgets/message.cpp widgets/message.cpp
utils/resizer.cpp
utils/image.cpp
) )
# Tell CMake to create the helloworld executable # Tell CMake to create the helloworld executable

52
ui/utils/image.cpp Normal file
View File

@ -0,0 +1,52 @@
/*
* 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 "image.h"
Image::Image(const QString& path, QWidget* parent):
QLabel(parent),
pixmap(path),
aspectRatio(0)
{
qreal height = pixmap.height();
qreal width = pixmap.width();
aspectRatio = width / height;
setPixmap(pixmap);
setScaledContents(true);
setMinimumHeight(50 / aspectRatio);
setMinimumWidth(50);
}
Image::~Image()
{
}
int Image::heightForWidth(int width) const
{
int height = width / aspectRatio;
//qDebug() << height << width << aspectRatio;
return height;
}
bool Image::hasHeightForWidth() const
{
return true;
}

61
ui/utils/image.h Normal file
View File

@ -0,0 +1,61 @@
/*
* 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 IMAGE_H
#define IMAGE_H
#include <QLabel>
#include <QPixmap>
/**
* @todo write docs
*/
class Image : public QLabel
{
public:
/**
* Default constructor
*/
Image(const QString& path, QWidget* parent = nullptr);
/**
* Destructor
*/
~Image();
/**
* @todo write docs
*
* @param TODO
* @return TODO
*/
int heightForWidth(int width) const override;
/**
* @todo write docs
*
* @return TODO
*/
virtual bool hasHeightForWidth() const;
private:
QPixmap pixmap;
qreal aspectRatio;
};
#endif // IMAGE_H

34
ui/utils/resizer.cpp Normal file
View File

@ -0,0 +1,34 @@
/*
* 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 "resizer.h"
Resizer::Resizer(QWidget* parent):
QObject(parent)
{
}
bool Resizer::eventFilter(QObject* obj, QEvent* event)
{
if (event->type() == QEvent::Resize) {
emit resized();
}
return false;
}

41
ui/utils/resizer.h Normal file
View File

@ -0,0 +1,41 @@
/*
* 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 RESIZER_H
#define RESIZER_H
#include <QObject>
#include <QWidget>
#include <QEvent>
/**
* @todo write docs
*/
class Resizer : public QObject {
Q_OBJECT
public:
Resizer(QWidget* parent = nullptr);
protected:
bool eventFilter(QObject* obj, QEvent* event) override;
signals:
void resized();
};
#endif // RESIZER_H

View File

@ -295,21 +295,6 @@ void Conversation::responseLocalFile(const QString& messageId, const QString& pa
line->responseLocalFile(messageId, path); line->responseLocalFile(messageId, path);
} }
Resizer::Resizer(QWidget* parent):
QObject(parent)
{
}
bool Resizer::eventFilter(QObject* obj, QEvent* event)
{
if (event->type() == QEvent::Resize) {
emit resized();
}
return false;
}
bool VisibilityCatcher::eventFilter(QObject* obj, QEvent* event) bool VisibilityCatcher::eventFilter(QObject* obj, QEvent* event)
{ {
if (event->type() == QEvent::Show) { if (event->type() == QEvent::Show) {

View File

@ -23,6 +23,7 @@
#include <QScopedPointer> #include <QScopedPointer>
#include "../../global.h" #include "../../global.h"
#include "messageline.h" #include "messageline.h"
#include "../utils/resizer.h"
namespace Ui namespace Ui
{ {
@ -42,18 +43,6 @@ signals:
void enterPressed(); void enterPressed();
}; };
class Resizer : public QObject {
Q_OBJECT
public:
Resizer(QWidget* parent = nullptr);
protected:
bool eventFilter(QObject* obj, QEvent* event) override;
signals:
void resized();
};
class VisibilityCatcher : public QObject { class VisibilityCatcher : public QObject {
Q_OBJECT Q_OBJECT
public: public:

View File

@ -17,6 +17,9 @@
*/ */
#include <QDebug> #include <QDebug>
#include <QMimeDatabase>
#include <QPixmap>
#include <QFileInfo>
#include "message.h" #include "message.h"
const QRegExp urlReg("^(?!<img\\ssrc=\")((?:https?|ftp)://\\S+)"); const QRegExp urlReg("^(?!<img\\ssrc=\")((?:https?|ftp)://\\S+)");
@ -34,10 +37,11 @@ Message::Message(const Shared::Message& source, bool outgoing, const QString& p_
downloadButton(0), downloadButton(0),
file(0), file(0),
progress(0), progress(0),
fileComment(0), fileComment(new QLabel()),
hasDownloadButton(false), hasDownloadButton(false),
hasProgress(false), hasProgress(false),
hasFile(false) hasFile(false),
commentAdded(false)
{ {
body->setBackgroundRole(QPalette::AlternateBase); body->setBackgroundRole(QPalette::AlternateBase);
body->setAutoFillBackground(true); body->setAutoFillBackground(true);
@ -86,6 +90,9 @@ Message::Message(const Shared::Message& source, bool outgoing, const QString& p_
Message::~Message() Message::~Message()
{ {
if (!commentAdded) {
delete fileComment;
}
} }
QString Message::getId() const QString Message::getId() const
@ -100,27 +107,23 @@ void Message::setSender(const QString& p_sender)
void Message::addDownloadDialog() void Message::addDownloadDialog()
{ {
if (hasFile) { hideFile();
file->deleteLater(); hideProgress();
file = 0;
hasFile = false;
}
if (hasProgress) {
progress->deleteLater();
progress = 0;
hasProgress = false;;
}
if (!hasDownloadButton) { if (!hasDownloadButton) {
hideComment();
if (msg.getBody() == msg.getOutOfBandUrl()) { if (msg.getBody() == msg.getOutOfBandUrl()) {
text->setText(""); text->setText("");
text->hide(); text->hide();
} }
downloadButton = new QPushButton(QIcon::fromTheme("download"), "Download"); downloadButton = new QPushButton(QIcon::fromTheme("download"), "Download");
fileComment = new QLabel(sender->text() + " is offering you to download a file"); downloadButton->setToolTip("<a href=\"" + msg.getOutOfBandUrl() + "\">" + msg.getOutOfBandUrl() + "</a>");
fileComment->setText(sender->text() + " is offering you to download a file");
fileComment->show();
connect(downloadButton, SIGNAL(clicked()), this, SLOT(onDownload())); connect(downloadButton, SIGNAL(clicked()), this, SLOT(onDownload()));
bodyLayout->insertWidget(2, fileComment); bodyLayout->insertWidget(2, fileComment);
bodyLayout->insertWidget(3, downloadButton); bodyLayout->insertWidget(3, downloadButton);
hasDownloadButton = true; hasDownloadButton = true;
commentAdded = true;
} }
} }
@ -130,54 +133,98 @@ void Message::onDownload()
} }
void Message::setProgress(qreal value) 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;
}
}
void Message::hideFile()
{ {
if (hasFile) { if (hasFile) {
file->deleteLater(); file->deleteLater();
file = 0; file = 0;
hasFile = false; hasFile = false;
} }
if (hasDownloadButton) {
downloadButton->deleteLater();
fileComment->deleteLater();
downloadButton = 0;
fileComment = 0;
hasDownloadButton = false;
}
if (!hasProgress) {
if (msg.getBody() == msg.getOutOfBandUrl()) {
text->setText("");
text->hide();
}
progress = new QLabel(std::to_string(value).c_str());
bodyLayout->insertWidget(2, progress);
hasProgress = true;
}
} }
void Message::showFile(const QString& path) void Message::hideProgress()
{ {
if (hasDownloadButton) {
downloadButton->deleteLater();
fileComment->deleteLater();
downloadButton = 0;
fileComment = 0;
hasDownloadButton = false;
}
if (hasProgress) { if (hasProgress) {
progress->deleteLater(); progress->deleteLater();
progress = 0; progress = 0;
hasProgress = false; hasProgress = false;;
}
if (!hasFile) {
if (msg.getBody() == msg.getOutOfBandUrl()) {
text->setText("");
text->hide();
}
//file = new QLabel("<img src=\"" + path + "\">");
file = new QLabel();
file->setPixmap(QPixmap(path));
//file->setScaledContents(true);
bodyLayout->insertWidget(2, file);
hasFile = true;
} }
} }

View File

@ -25,8 +25,14 @@
#include <QLabel> #include <QLabel>
#include <QGraphicsDropShadowEffect> #include <QGraphicsDropShadowEffect>
#include <QPushButton> #include <QPushButton>
#include <QProgressBar>
#include <QAction>
#include <QDesktopServices>
#include <QUrl>
#include "../../global.h" #include "../../global.h"
#include "../utils/resizer.h"
#include "../utils/image.h"
/** /**
* @todo write docs * @todo write docs
@ -58,15 +64,21 @@ private:
QGraphicsDropShadowEffect* shadow; QGraphicsDropShadowEffect* shadow;
QPushButton* downloadButton; QPushButton* downloadButton;
QLabel* file; QLabel* file;
QLabel* progress; QProgressBar* progress;
QLabel* fileComment; QLabel* fileComment;
bool hasDownloadButton; bool hasDownloadButton;
bool hasProgress; bool hasProgress;
bool hasFile; bool hasFile;
bool commentAdded;
private slots: private slots:
void onDownload(); void onDownload();
private:
void hideDownload();
void hideProgress();
void hideFile();
void hideComment();
}; };
#endif // MESSAGE_H #endif // MESSAGE_H