diff --git a/ui/CMakeLists.txt b/ui/CMakeLists.txt index 0f6680a..59a0a4f 100644 --- a/ui/CMakeLists.txt +++ b/ui/CMakeLists.txt @@ -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 diff --git a/ui/utils/messageline.cpp b/ui/utils/messageline.cpp index 0560344..57894e8 100644 --- a/ui/utils/messageline.cpp +++ b/ui/utils/messageline.cpp @@ -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); diff --git a/ui/utils/messageline.h b/ui/utils/messageline.h index 3d7fb56..67280e4 100644 --- a/ui/utils/messageline.h +++ b/ui/utils/messageline.h @@ -25,13 +25,10 @@ #include #include #include -#include -#include -#include -#include #include "../../global.h" #include "message.h" +#include "progress.h" class MessageLine : public QWidget { @@ -85,15 +82,8 @@ private: std::map palNames; std::deque 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 diff --git a/ui/utils/progress.cpp b/ui/utils/progress.cpp new file mode 100644 index 0000000..9886270 --- /dev/null +++ b/ui/utils/progress.cpp @@ -0,0 +1,84 @@ +/* + * Squawk messenger. + * Copyright (C) 2019 Yury Gubich + * + * 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 . + */ + +#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; + } +} diff --git a/ui/utils/progress.h b/ui/utils/progress.h new file mode 100644 index 0000000..c6501aa --- /dev/null +++ b/ui/utils/progress.h @@ -0,0 +1,57 @@ +/* + * Squawk messenger. + * Copyright (C) 2019 Yury Gubich + * + * 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 . + */ + +#ifndef PROGRESS_H +#define PROGRESS_H + +#include +#include +#include +#include +#include +#include +#include + +#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