qskinny/examples/automotive/SpeedometerDisplay.cpp

134 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
*****************************************************************************/
#include "SpeedometerDisplay.h"
#include "Speedometer.h"
#include <QskTextLabel.h>
2018-04-05 11:23:48 +02:00
#include <QTime>
#include <QTimer>
2020-03-12 09:53:55 +01:00
#include <cstdlib>
2020-12-13 19:38:46 +01:00
namespace
{
2020-12-13 19:38:46 +01:00
class Dial : public Speedometer
{
public:
Dial( const QString& title, QQuickItem* parent = nullptr )
: Speedometer( parent )
{
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;
};
2020-12-13 19:38:46 +01:00
class RevCounter : public Dial
{
2020-12-13 19:38:46 +01:00
public:
RevCounter( QQuickItem* parent = nullptr )
: Dial( "x 1000 min^-1", parent )
{
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
}
};
2020-12-13 19:38:46 +01:00
class Speedo : public Dial
{
2020-12-13 19:38:46 +01:00
public:
Speedo( QQuickItem* parent = nullptr )
: Dial( "km/h", parent )
{
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 );
}
};
2020-12-13 19:38:46 +01:00
class FuelGauge : public Dial
{
public:
FuelGauge( QQuickItem* parent = nullptr )
: Dial( "fuel", parent )
{
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() );
}
};
}
2020-12-13 19:38:46 +01:00
SpeedometerDisplay::SpeedometerDisplay( QQuickItem* parent )
: QskLinearBox( Qt::Horizontal, parent )
{
2020-12-13 19:38:46 +01:00
std::srand( static_cast< uint >( QTime::currentTime().msec() ) );
(void ) new RevCounter( this );
auto speedometer = new Speedo( this );
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 );
connect( timer, &QTimer::timeout, speedometer, &Speedo::updateValue );
connect( timer, &QTimer::timeout, fuelGauge, &FuelGauge::updateValue );
timer->setInterval( 16 );
timer->start();
}