refactored progress

This commit is contained in:
Blue 2019-10-30 16:47:21 +03:00
parent 566fc1f2fb
commit f3515f1104
5 changed files with 149 additions and 48 deletions

View File

@ -36,6 +36,7 @@ set(squawkUI_SRC
utils/image.cpp
utils/flowlayout.cpp
utils/badge.cpp
utils/progress.cpp
)
# Tell CMake to create the helloworld executable

View File

@ -31,35 +31,11 @@ MessageLine::MessageLine(bool p_room, QWidget* parent):
palNames(),
views(),
room(p_room),
busyPixmap(new QGraphicsPixmapItem(Shared::icon("view-refresh", true).pixmap(70))),
busyScene(),
busyLabel(&busyScene),
busyLayout(),
busyShown(false),
rotation()
progress()
{
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()
@ -201,28 +177,21 @@ QString MessageLine::firstMessageId() const
void MessageLine::showBusyIndicator()
{
if (!busyShown) {
layout->insertLayout(0, &busyLayout);
layout->insertWidget(0, &progress);
progress.start();
busyShown = true;
rotation.start();
busyLabel.show();
}
}
void MessageLine::hideBusyIndicator()
{
if (busyShown) {
busyLabel.hide();
rotation.stop();
layout->removeItem(&busyLayout);
progress.stop();
layout->removeWidget(&progress);
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);

View File

@ -25,13 +25,10 @@
#include <QLabel>
#include <QResizeEvent>
#include <QIcon>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsPixmapItem>
#include <QVariantAnimation>
#include "../../global.h"
#include "message.h"
#include "progress.h"
class MessageLine : public QWidget
{
@ -85,15 +82,8 @@ private:
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);
Progress progress;
};
#endif // MESSAGELINE_H

84
ui/utils/progress.cpp Normal file
View File

@ -0,0 +1,84 @@
/*
* 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 "progress.h"
Progress::Progress(quint16 p_size, QWidget* parent):
QWidget(parent),
pixmap(new QGraphicsPixmapItem(Shared::icon("view-refresh", true).pixmap(p_size))),
scene(),
label(&scene),
progress(false),
animation(),
size(p_size)
{
scene.addItem(pixmap);
label.setMaximumSize(size, size);
label.setMinimumSize(size, size);
label.setSceneRect(0, 0, size, size);
label.setFrameStyle(0);
label.setContentsMargins(0, 0, 0, 0);
label.setInteractive(false);
pixmap->setTransformOriginPoint(size / 2, size / 2);
pixmap->setTransformationMode(Qt::SmoothTransformation);
pixmap->setOffset(0, 0);;
animation.setDuration(500);
animation.setStartValue(0.0f);
animation.setEndValue(180.0f);
animation.setLoopCount(-1);
connect(&animation, &QVariantAnimation::valueChanged, this, &Progress::onValueChanged);
QGridLayout* layout = new QGridLayout();
setLayout(layout);
layout->setMargin(0);
layout->setVerticalSpacing(0);
layout->setHorizontalSpacing(0);
setContentsMargins(0, 0, 0, 0);
layout->addWidget(&label, 0, 0, 1, 1);
label.hide();
}
Progress::~Progress()
{
}
void Progress::onValueChanged(const QVariant& value)
{
pixmap->setRotation(value.toReal());
}
void Progress::start()
{
if (!progress) {
label.show();
animation.start();
progress = true;
}
}
void Progress::stop()
{
if (progress) {
label.hide();
animation.stop();
progress = false;
}
}

57
ui/utils/progress.h Normal file
View File

@ -0,0 +1,57 @@
/*
* 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 PROGRESS_H
#define PROGRESS_H
#include <QWidget>
#include <QGraphicsScene>
#include <QIcon>
#include <QGraphicsView>
#include <QGraphicsPixmapItem>
#include <QVariantAnimation>
#include <QGridLayout>
#include "../../global.h"
/**
* @todo write docs
*/
class Progress : public QWidget
{
Q_OBJECT
public:
Progress(quint16 p_size = 70, QWidget* parent = nullptr);
~Progress();
void start();
void stop();
private slots:
void onValueChanged(const QVariant& value);
private:
QGraphicsPixmapItem* pixmap;
QGraphicsScene scene;
QGraphicsView label;
bool progress;
QVariantAnimation animation;
quint16 size;
};
#endif // PROGRESS_H