qskinny/playground/dials/Dashboard.cpp

129 lines
3.2 KiB
C++
Raw Normal View History

2019-06-20 12:02:28 +02:00
/******************************************************************************
* QSkinny - Copyright (C) 2016 Uwe Rathmann
* This file may be used under the terms of the 3-clause BSD License
*****************************************************************************/
2022-07-14 18:50:17 +02:00
#include "Dashboard.h"
#include "Dial.h"
#include <QskTextLabel.h>
2018-04-05 11:23:48 +02:00
#include <QTimer>
2020-12-13 19:38:46 +01:00
namespace
{
2022-07-14 18:50:17 +02:00
class DashboardDial : public Dial
2020-12-13 19:38:46 +01:00
{
public:
2022-07-14 18:50:17 +02:00
DashboardDial( const QString& title, QQuickItem* parent = nullptr )
: Dial( parent )
2020-12-13 19:38:46 +01:00
{
setPolishOnResize( true );
m_label = new QskTextLabel( title, this );
}
2020-12-13 19:38:46 +01:00
protected:
void updateLayout() override
{
const auto r = layoutRect();
2018-04-05 11:23:48 +02:00
2020-12-13 19:38:46 +01:00
const auto hint = m_label->sizeConstraint();
2020-12-13 19:38:46 +01:00
const qreal y = r.y() + 0.6 * r.height() - 0.5 * hint.height();
const qreal x = r.center().x() - 0.5 * hint.width();
2020-12-13 19:38:46 +01:00
m_label->setGeometry( x, y, hint.width(), hint.height() );
}
2020-12-13 19:38:46 +01:00
private:
QskTextLabel* m_label;
};
2022-07-14 18:50:17 +02:00
class RevCounter : public DashboardDial
{
2020-12-13 19:38:46 +01:00
public:
RevCounter( QQuickItem* parent = nullptr )
2022-07-14 18:50:17 +02:00
: DashboardDial( "x 1000 min^-1", parent )
2020-12-13 19:38:46 +01:00
{
setMinimum( 145 );
setMaximum( 305 );
setValue( 200 );
2020-12-13 19:38:46 +01:00
constexpr int labelsCount = 8;
2018-04-05 11:23:48 +02:00
2020-12-13 19:38:46 +01:00
QVector< QString > labels;
labels.reserve( labelsCount );
for ( int i = 0; i < labelsCount; i++ )
labels += QString::number( i );
2020-12-18 16:32:54 +01:00
setTickLabels( labels );
2020-12-13 19:38:46 +01:00
}
};
2022-07-14 18:50:17 +02:00
class SpeedoMeter : public DashboardDial
{
2020-12-13 19:38:46 +01:00
public:
2022-07-14 18:50:17 +02:00
SpeedoMeter( QQuickItem* parent = nullptr )
: DashboardDial( "km/h", parent )
2020-12-13 19:38:46 +01:00
{
setMinimum( -215 );
setMaximum( 35 );
setValue( -90 );
2020-12-18 16:32:54 +01:00
constexpr int labelsCount = 17;
2020-12-13 19:38:46 +01:00
QVector< QString > labels;
labels.reserve( labelsCount );
2020-12-13 19:38:46 +01:00
for ( int i = 0; i < labelsCount; i++ )
labels.append( QString::number( i * 10 ) );
2020-12-18 16:32:54 +01:00
setTickLabels( labels );
2020-12-13 19:38:46 +01:00
}
2020-12-13 19:38:46 +01:00
void updateValue()
{
setValue( value() + std::rand() % 3 - 0.95 );
}
};
2022-07-14 18:50:17 +02:00
class FuelGauge : public DashboardDial
2020-12-13 19:38:46 +01:00
{
public:
FuelGauge( QQuickItem* parent = nullptr )
2022-07-14 18:50:17 +02:00
: DashboardDial( "fuel", parent )
2020-12-13 19:38:46 +01:00
{
setMinimum( 195 );
setMaximum( 345 );
setValue( 330 );
2020-12-18 16:32:54 +01:00
setTickLabels( { "0", "", "1/2", "", "1/1" } );
2020-12-13 19:38:46 +01:00
}
void updateValue()
{
setValue( 0.99997 * value() );
}
};
}
2022-07-14 18:50:17 +02:00
Dashboard::Dashboard( QQuickItem* parent )
2020-12-13 19:38:46 +01:00
: QskLinearBox( Qt::Horizontal, parent )
{
2020-12-13 19:38:46 +01:00
(void ) new RevCounter( this );
2022-07-14 18:50:17 +02:00
auto speedometer = new SpeedoMeter( this );
2020-12-13 19:38:46 +01:00
auto fuelGauge = new FuelGauge( this );
setMargins( 10 );
setSpacing( 10 );
2021-08-04 09:31:16 +02:00
2020-12-13 19:38:46 +01:00
auto timer = new QTimer( this );
2022-07-14 18:50:17 +02:00
connect( timer, &QTimer::timeout, speedometer, &SpeedoMeter::updateValue );
2020-12-13 19:38:46 +01:00
connect( timer, &QTimer::timeout, fuelGauge, &FuelGauge::updateValue );
timer->setInterval( 16 );
timer->start();
}