GProgressDialog

.. in an attempt to avoid the QT 5.3.1 QProgressSialog
   bug and also put a bit of our own styling onto this
   a GC implementation of QProgressDialog

.. it has no abort button DELIBERATELY

.. not tested on OS X .. yet
This commit is contained in:
Mark Liversedge
2014-06-30 16:38:03 +01:00
parent 82b1678b0a
commit 5c174a1cf9
4 changed files with 161 additions and 18 deletions

95
src/GProgressDialog.cpp Normal file
View File

@@ -0,0 +1,95 @@
/*
* Copyright (c) 2014 Mark Liversedge (liversedge@gmail.com)
*
* 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 2 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, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "GProgressDialog.h"
GProgressDialog::GProgressDialog(QString title, int min, int max, QWidget *parent) :
// sheet on mac and no window manager chrome
QDialog(parent, Qt::Sheet | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint),
// defaults
title(title), min(min), max(max)
{
// only block mainwindow
setWindowModality(Qt::WindowModal); // only block mainwindow
// zap me when closed and make me see through
setAttribute(Qt::WA_DeleteOnClose, true);
setAttribute(Qt::WA_TranslucentBackground, true);
// not too big dude
setFixedSize(200, 130);
}
void GProgressDialog::paintEvent(QPaintEvent *)
{
// setup the colors
QColor translucentGray = GColor(CPLOTMARKER);
translucentGray.setAlpha(240);
QColor translucentWhite = GColor(CPLOTBACKGROUND);
translucentWhite.setAlpha(240);
QColor black = GCColor::invertColor(translucentWhite);
// setup a painter and the area to paint
QPainter painter(this);
painter.save();
QRect all(0,0,width(),height());
// fill
painter.setPen(Qt::NoPen);
painter.fillRect(all, translucentWhite);
// border
QRect border(0,0,width()-1,height()-1);
black.setAlpha(50);
painter.setPen(QPen(black, 0.5f));
painter.drawRect(border);
// heading
QRectF titlebox(0,0,width(),25);
painter.fillRect(titlebox, black);
black.setAlpha(240);
painter.setPen(QPen(black));
painter.drawText(titlebox, title, Qt::AlignVCenter | Qt::AlignCenter);
// informative text
QRectF labelbox(0,25,width(),height()-33);
painter.drawText(labelbox, text, Qt::AlignVCenter | Qt::AlignCenter);
// progressbar
QRectF progress(0, height()-8, double(value) / double(max) * double(width()), 8);
painter.fillRect(progress, translucentGray);
painter.restore();
}
void GProgressDialog::setValue(int x)
{
value = x;
if (max < x) max = x;
repaint();
}
// set the progress text
void GProgressDialog::setLabelText(QString label)
{
text = label;
repaint();
}

55
src/GProgressDialog.h Normal file
View File

@@ -0,0 +1,55 @@
/*
* Copyright (c) 2014 Mark Liversedge (liversedge@gmail.com)
*
* 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 2 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, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef _GC_GProgressDialog_h
#define _GC_GProgressDialog_h 1
#include "GoldenCheetah.h"
#include "Colors.h"
#include <QWidget>
#include <QPaintEvent>
#include <QString>
#include <QDialog>
class GProgressDialog : public QDialog
{
Q_OBJECT
public:
// no frame, translucent, no button, no parent - always modal with chrome heading
GProgressDialog(QString title, int min, int max, QWidget *parent = NULL);
// set value, which in turn repaints the progress at the bottom
void setValue(int x);
// set the progress text
void setLabelText(QString text);
// compatibility
bool wasCanceled() { return false; }
// painting
void paintEvent(QPaintEvent *);
private:
QString title, text;
int min, max, value;
};
#endif

View File

@@ -36,6 +36,8 @@
#include <QtXml/QtXml>
#include <QProgressDialog>
#include "GProgressDialog.h"
MetricAggregator::MetricAggregator(Context *context) : QObject(context), context(context), first(true)
{
colorEngine = new ColorEngine(context);
@@ -132,8 +134,8 @@ void MetricAggregator::refreshMetrics(QDateTime forceAfterThisDate)
// showing a progress bar as we go
QTime elapsed;
elapsed.start();
QString title = tr("Updating Statistics\nStarted");
QProgressDialog *bar = NULL;
QString title = context->athlete->cyclist;
GProgressDialog *bar = NULL;
int processed=0;
int updates=0;
@@ -163,10 +165,7 @@ void MetricAggregator::refreshMetrics(QDateTime forceAfterThisDate)
// create the dialog if we need to show progress for long running uodate
long elapsedtime = elapsed.elapsed();
if ((!forceAfterThisDate.isNull() || first || elapsedtime > 6000) && bar == NULL) {
bar = new QProgressDialog(title, tr("Abort"), 0, filenames.count()); // not owned by mainwindow
bar->setWindowFlags(bar->windowFlags() | Qt::FramelessWindowHint);
bar->setWindowModality(Qt::WindowModal);
bar->setMinimumDuration(0);
bar = new GProgressDialog(title, 0, filenames.count()); // not owned by mainwindow
bar->show(); // lets hide until elapsed time is > 6 seconds
// lets make sure it goes to the center!
@@ -180,7 +179,7 @@ void MetricAggregator::refreshMetrics(QDateTime forceAfterThisDate)
QString elapsedString = QString("%1:%2:%3").arg(elapsedtime/3600000,2)
.arg((elapsedtime%3600000)/60000,2,10,QLatin1Char('0'))
.arg((elapsedtime%60000)/1000,2,10,QLatin1Char('0'));
QString title = tr("%1\n\nUpdate Statistics\nElapsed: %2\n\n%3").arg(context->athlete->cyclist).arg(elapsedString).arg(name);
QString title = tr("Update Statistics\nElapsed: %1\n\n%2").arg(elapsedString).arg(name);
bar->setLabelText(title);
bar->setValue(processed);
}
@@ -515,18 +514,13 @@ MetricAggregator::refreshCPModelMetrics(bool bg)
// if we have a progress dialog lets update the bar to show
// progress for the model parameters
#if (!defined Q_OS_MAC) || (defined QT_NOBUG39038) || (QT_VERSION < 0x050300) // QTBUG 39038 !!!
QProgressDialog *bar = NULL;
GProgressDialog *bar = NULL;
if (!bg) {
bar = new QProgressDialog(tr("Update Model Estimates"), tr("Abort"), 1, (lastYear*12 + lastMonth) - (year*12 + month));
bar->setWindowFlags(bar->windowFlags() | Qt::FramelessWindowHint);
bar->setWindowModality(Qt::WindowModal);
bar->setMinimumDuration(0);
bar = new GProgressDialog(tr("Update Model Estimates"), 1, (lastYear*12 + lastMonth) - (year*12 + month));
bar->setValue(1);
bar->show(); // lets hide until elapsed time is > 6 seconds
QApplication::processEvents();
}
#endif
QList< QVector<float> > months;
QList< QVector<float> > monthsKG;
@@ -662,14 +656,11 @@ MetricAggregator::refreshCPModelMetrics(bool bg)
month ++;
}
#if (!defined Q_OS_MAC) || (defined QT_NOBUG39038) || (QT_VERSION < 0x050300) // QTBUG 39038 !!!
// show some progress
if (!bg) bar->setValue(count);
#endif
}
#if (!defined Q_OS_MAC) || (defined QT_NOBUG39038) || (QT_VERSION < 0x050300) // QTBUG 39038 !!!
if (!bg) delete bar;
#endif
// add a dummy entry if we have no estimates to stop constantly trying to refresh
if (context->athlete->PDEstimates.count() == 0) {

View File

@@ -325,6 +325,7 @@ HEADERS += \
GcWindowRegistry.h \
GoldenCheetah.h \
GoogleMapControl.h \
GProgressDialog.h \
GpxParser.h \
GpxRideFile.h \
HelpWindow.h \
@@ -523,6 +524,7 @@ SOURCES += \
GcWindowRegistry.cpp \
GoldenCheetah.cpp \
GoogleMapControl.cpp \
GProgressDialog.cpp \
GpxParser.cpp \
GpxRideFile.cpp \
HelpWindow.cpp \