qskinny/playground/models/Window.cpp

129 lines
3.5 KiB
C++
Raw Normal View History

/******************************************************************************
* QSkinny - Copyright (C) The authors
* SPDX-License-Identifier: BSD-3-Clause
*****************************************************************************/
#include "Window.h"
#include <QskLinearBox.h>
#include <QskPushButton.h>
#include <QskTextInput.h>
#include <QskSpinBox.h>
#include <QskModelObjectBinder.h>
#include <QStandardItemModel>
namespace
{
class Model : public QStandardItemModel
{
public:
Model( QObject* parent = nullptr )
: QStandardItemModel( 2, 2, parent )
{
2024-02-26 11:16:56 +00:00
initValue( 0, 0, 1 );
initValue( 0, 1, "HELLO" );
2024-02-26 11:16:56 +00:00
initValue( 1, 0, 2 );
initValue( 1, 1, "WORLD" );
}
2024-02-26 11:16:56 +00:00
void dump() const
{
qDebug() << "Model";
for ( int row = 0; row < rowCount(); row++ )
{
for ( int col = 0; col < columnCount(); col++ )
{
qDebug() << '\t' << row << col
<< data( index( row, col ), Qt::EditRole );
}
}
}
2024-02-26 11:16:56 +00:00
private:
void initValue( int row, int col, const QVariant& value )
{
setData( index( row, col ), value, Qt::EditRole );
}
};
2024-02-26 11:16:56 +00:00
class View : public QskLinearBox
{
public:
View( QQuickItem* parent = nullptr )
: QskLinearBox( Qt::Vertical, parent )
{
setPanel( true );
2024-02-26 11:16:56 +00:00
auto model = new Model( this );
2024-02-26 11:16:56 +00:00
auto textInput = new QskTextInput();
auto spinBox = new QskSpinBox( -100.0, +100.0, 1.0 );
2024-02-26 11:16:56 +00:00
m_binder = new QskModelObjectBinder( model, this );
m_binder->bindObject( spinBox, 0 );
m_binder->bindObject( textInput, 1 );
2024-02-26 11:16:56 +00:00
auto hBox = new QskLinearBox( Qt::Horizontal );
hBox->setSection( QskAspect::Header );
hBox->setSizePolicy( Qt::Vertical, QskSizePolicy::Fixed );
2024-02-26 11:16:56 +00:00
{
auto rowButton = new QskPushButton( "Toggle Row", hBox );
auto counterButton = new QskPushButton( "Invert Counter", hBox );
auto submitButton = new QskPushButton( "Submit Changes", hBox );
2024-02-26 11:16:56 +00:00
connect( rowButton, &QskPushButton::clicked,
this, &View::toogleRow );
2024-02-26 11:16:56 +00:00
connect( counterButton, &QskPushButton::clicked,
this, &View::invertCounter );
2024-02-26 11:16:56 +00:00
connect( submitButton, &QskPushButton::clicked,
this, &View::updateModel );
2024-02-26 11:16:56 +00:00
hBox->addStretch( 1 );
}
2024-02-26 11:16:56 +00:00
addItem( hBox );
addSpacer( 5 );
addItem( spinBox );
addItem( textInput );
addStretch( 1 );
}
2024-02-26 11:16:56 +00:00
private:
void toogleRow()
{
m_binder->setCurrentRow( m_binder->currentRow() == 0 ? 1 : 0 );
}
2024-02-26 11:16:56 +00:00
void updateModel()
{
m_binder->submit();
const auto model = dynamic_cast< const Model* >( m_binder->model() );
model->dump();
}
void invertCounter()
{
auto model = m_binder->model();
2024-02-26 11:16:56 +00:00
const auto index = model->index( m_binder->currentRow(), 0 );
auto value = model->data( index, Qt::EditRole );
model->setData( index, -value.toDouble(), Qt::EditRole );
}
QskModelObjectBinder* m_binder;
};
}
Window::Window()
{
addItem( new View() );
}