/******************************************************************************
 * QSkinny - Copyright (C) The authors
 *           SPDX-License-Identifier: BSD-3-Clause
 *****************************************************************************/

#ifndef QSK_LINEAR_LAYOUT_ENGINE_H
#define QSK_LINEAR_LAYOUT_ENGINE_H

#include "QskGlobal.h"
#include "QskLayoutEngine2D.h"

#include <qnamespace.h>
#include <memory>

class QQuickItem;
class QSizeF;
class QRectF;

class QSK_EXPORT QskLinearLayoutEngine : public QskLayoutEngine2D
{
  public:
    QskLinearLayoutEngine( Qt::Orientation, uint dimension );
    ~QskLinearLayoutEngine() override;

    Qt::Orientation orientation() const;
    bool setOrientation( Qt::Orientation );

    bool setDimension( uint dimension );
    uint dimension() const;

    int count() const override final;

    int insertItem( QQuickItem*, int index );
    int addItem( QQuickItem* );

    int insertSpacerAt( int index, qreal spacing );
    int addSpacer( qreal spacing );

    bool removeAt( int index );
    bool clear();

    int indexOf( const QQuickItem* ) const;

    QQuickItem* itemAt( int index ) const;
    qreal spacerAt( int index ) const;

    bool setStretchFactorAt( int index, int stretchFactor );
    int stretchFactorAt( int index ) const;

  private:
    Q_DISABLE_COPY(QskLinearLayoutEngine)

    QskSizePolicy sizePolicyAt( int index ) const override final;
    void layoutItems() override;

    int effectiveCount() const;
    int effectiveCount( Qt::Orientation ) const override;

    void invalidateElementCache() override;

    virtual void setupChain( Qt::Orientation, const QskLayoutChain::Segments&,
        QskLayoutChain& ) const override final;

    class PrivateData;
    std::unique_ptr< PrivateData > m_data;
};

inline int QskLinearLayoutEngine::addItem( QQuickItem* item )
{
    return insertItem( item, -1 );
}

inline int QskLinearLayoutEngine::addSpacer( qreal spacing )
{
    return insertSpacerAt( -1, spacing );
}

#endif
