Speedometer: Add some API

This commit is contained in:
Peter Hartmann 2018-04-05 11:23:48 +02:00
parent 554f07c5b4
commit b7c54d6916
6 changed files with 65 additions and 21 deletions

View File

@ -13,7 +13,8 @@
#include <QDate>
#include <QImage>
MainWindow::MainWindow()
MainWindow::MainWindow() :
m_layout( nullptr )
{
const QImage image( QStringLiteral( ":/images/background.jpg" ) );
@ -21,27 +22,27 @@ MainWindow::MainWindow()
backgroundImage->setGraphic( QskGraphic::fromImage( image ) );
backgroundImage->setFillMode( QskGraphicLabel::Stretch );
m_layout = new QskLinearBox( Qt::Vertical, contentItem() );
auto header = headerBar();
auto content = mainContent();
auto footer = footerBar();
auto layout = new QskLinearBox( Qt::Vertical, contentItem() );
m_layout->addItem( header );
m_layout->setStretchFactor( header, 1 );
layout->addItem( header );
layout->setStretchFactor( header, 1 );
m_layout->addItem( content );
m_layout->setStretchFactor( content, 10 );
layout->addItem( content );
layout->setStretchFactor( content, 10 );
layout->addItem( footer );
layout->setStretchFactor( footer, 1 );
m_layout->addItem( footer );
m_layout->setStretchFactor( footer, 1 );
setAutoLayoutChildren( true );
}
QQuickItem* MainWindow::headerBar() const
{
auto* header = new ButtonBar();
auto* header = new ButtonBar( m_layout );
header->addIndicator( "bluetooth" );
header->addIndicator( "location" );
header->addIndicator( "phone" );
@ -57,13 +58,13 @@ QQuickItem* MainWindow::headerBar() const
QQuickItem* MainWindow::mainContent() const
{
return new SpeedometerDisplay();
return new SpeedometerDisplay( m_layout );
//return new SoundControl(); ###
}
QQuickItem* MainWindow::footerBar() const
{
auto* footer = new ButtonBar();
auto* footer = new ButtonBar( m_layout );
footer->addIndicator( "cloud" );
footer->addIndicator( "man" );

View File

@ -3,6 +3,8 @@
#include <QskWindow.h>
class QskLinearBox;
class QQuickItem;
class MainWindow : public QskWindow
@ -14,6 +16,8 @@ private:
QQuickItem* headerBar() const;
QQuickItem* mainContent() const;
QQuickItem* footerBar() const;
QskLinearBox* m_layout;
};
#endif

View File

@ -9,13 +9,25 @@ QSK_SUBCONTROL( Speedometer, Numbers )
QSK_SUBCONTROL( Speedometer, Needle )
Speedometer::Speedometer( QQuickItem* parent ) :
QskControl( parent )
QskControl( parent ),
m_value( 0.0 )
{
}
QSizeF Speedometer::contentsSizeHint() const
{
return QSizeF( 300, 300 );
return { 300, 300 };
}
float Speedometer::value() const
{
return m_value;
}
void Speedometer::setValue( float value )
{
m_value = value;
update();
}
#include "moc_Speedometer.cpp"

View File

@ -13,6 +13,12 @@ public:
Speedometer( QQuickItem* parent = nullptr );
virtual QSizeF contentsSizeHint() const override;
float value() const;
void setValue( float value ); // angle; should be within a set range
private:
float m_value;
};
#endif // SPEEDOMETER_H

View File

@ -5,19 +5,40 @@
#include <QskLinearBox.h>
#include <QskTextLabel.h>
#include <QTime>
#include <QTimer>
#include <QtGlobal>
SpeedometerDisplay::SpeedometerDisplay( QQuickItem *parent ) :
QskControl( parent )
{
QskLinearBox* box = new QskLinearBox( Qt::Horizontal, this );
qsrand( QTime::currentTime().msec() );
auto box = new QskLinearBox( Qt::Horizontal, this );
box->setAutoAddChildren( true );
box->setAutoLayoutChildren( true );
box->setMargins( QMarginsF( 40, 20, 40, 20 ) );
box->setAlignment( 0, Qt::AlignHCenter );
QskTextLabel* label = new QskTextLabel( QStringLiteral( "text" ), box );
label->setFixedSize( QSizeF( 300, 300 ) );
label->setAlignment( Qt::AlignHCenter | Qt::AlignCenter );
auto revCounter = new Speedometer( box );
revCounter->setFixedSize( QSizeF( 300, 300 ) );
// revCounter->setSizePolicy( QskSizePolicy::Maximum, QskSizePolicy::Maximum );
revCounter->setValue( 270 );
Speedometer* speedometer = new Speedometer( box );
speedometer->setFixedSize( QSizeF( 400, 400 ) );
auto speedometer = new Speedometer( box );
speedometer->setFixedSize( QSizeF( 300, 300 ) );
// speedometer->setSizePolicy( QskSizePolicy::Maximum, QskSizePolicy::Maximum );
speedometer->setValue( 270 );
auto timer = new QTimer( this );
connect( timer, &QTimer::timeout, this, [ speedometer ]() {
int newValue = speedometer->value() + qrand() % 3 - 0.8;
speedometer->setValue( newValue );
});
timer->setInterval( 16 );
timer->start();
auto fuelGauge = new Speedometer( box );
fuelGauge->setFixedSize( QSizeF( 200, 200 ) );
fuelGauge->setValue( 270 );
}

View File

@ -243,7 +243,7 @@ QSGNode* SpeedometerSkinlet::updateNeedleNode( const Speedometer* speedometer, Q
float xStart = center.x() - needleWidth ;
float yStart = center.y();
float angle = 315; // ### API
float angle = speedometer->value();
qreal cosine = qCos( qDegreesToRadians( angle ) );
qreal sine = qSin( qDegreesToRadians( angle ) );