Adverts widget v0.2 added:

*Querying webAPI for adverts content;
*Starting browsers new tab with url specified in json file
*Basic QML items and models
*Overview tab layout changes
*Storing images on harddrive (temporary solution)
*Sending queries to webAPI straight from widget (temporary solution)
coming next:
-store whole json file as url/desription need to be stored for further use
-display images of adverts from QImage, not from file stored on harddrive
-hide/show adverts button tweaking
-proper webAPI base class for querying various servers for data (prypto for instance)

*
This commit is contained in:
felixrojauro
2015-10-30 01:51:54 +01:00
parent 1b0545c4e8
commit 5a3b2dbce6
37 changed files with 1947 additions and 43 deletions

View File

@@ -0,0 +1,51 @@
#include "guibannercontrol.h"
#include "guibannerlistview.h"
#include "qmlbannerlistmodel.h"
#include "qmlbannerlistitem.h"
GUIBannerControl::GUIBannerControl( QQuickItem* a_pParent )
: QQuickItem( a_pParent )
, m_pAdvertsView( 0 )
{
}
GUIBannerControl::~GUIBannerControl()
{
if ( m_pAdvertsView )
{
delete m_pAdvertsView;
m_pAdvertsView = 0;
}
}
void GUIBannerControl::InitializeAdvertsView( GUIBannerListView* a_pView )
{
if ( a_pView )
{
if ( m_pAdvertsView )
{
delete m_pAdvertsView;
}
m_pAdvertsView = a_pView;
}
}
void GUIBannerControl::slotPopulateFromWeb( JsonActivePromotionsParser* a_pActivePromotions )
{
if ( m_pAdvertsView )
{
QmlBannerListModel* pAdvertsModel = new QmlBannerListModel( *a_pActivePromotions );
m_pAdvertsView->SetModel( pAdvertsModel );
}
}
void GUIBannerControl::slotPopulateLocally()
{
if ( m_pAdvertsView )
{
QmlBannerListModel* pAdvertsModel = new QmlBannerListModel( 0 );
m_pAdvertsView->SetModel( pAdvertsModel );
qDebug() << "Coming soon";
}
}

View File

@@ -0,0 +1,32 @@
#ifndef GUIBANNERCONTROL_H
#define GUIBANNERCONTROL_H
#include <QQuickItem>
#include "../../CSCPublicAPI/jsonactivepromotionsparser.h"
class GUIBannerListView;
class GUIBannerControl : public QQuickItem
{
Q_OBJECT
public:
GUIBannerControl( QQuickItem* a_pParent = 0 );
virtual ~GUIBannerControl();
Q_INVOKABLE void InitializeAdvertsView( GUIBannerListView* a_pView );
GUIBannerListView* GetAdvertsView() const { return m_pAdvertsView; }
public slots:
void slotPopulateFromWeb( JsonActivePromotionsParser* a_pActivePromotions );
void slotPopulateLocally();
signals:
public slots:
private:
GUIBannerListView* m_pAdvertsView;
};
#endif // GUIBANNERCONTROL_H

View File

@@ -0,0 +1,50 @@
#include "guibannerlistview.h"
#include "qmlbannerlistitem.h"
#include "qmlbannerlistmodel.h"
#include "qmllistmodel.h"
#include <QDesktopServices>
#include <QUrl>
GUIBannerListView::GUIBannerListView( QQuickItem* a_pParent )
: QQuickItem( a_pParent )
, m_pModel( 0 )
{
}
void GUIBannerListView::Clear()
{
if ( m_pModel )
{
m_pModel->clear();
}
}
void GUIBannerListView::OnClicked( int a_iItemIndex )
{
if ( m_pModel )
{
QString strUrl = m_pModel->GetData( a_iItemIndex, QmlBannerListItem::ROLE_DESTINATION_URL ).toString();
QDesktopServices::openUrl( QUrl( strUrl ) );
}
}
GUIBannerListView::~GUIBannerListView()
{
if ( m_pModel )
{
delete m_pModel;
m_pModel = 0;
}
}
void GUIBannerListView::SetModel( QmlBannerListModel* a_pModel )
{
if ( m_pModel )
{
delete m_pModel;
}
m_pModel = a_pModel;
emit signalModelChanged();
}

View File

@@ -0,0 +1,35 @@
#ifndef GUIBANNERLISTVIEW_H
#define GUIBANNERLISTVIEW_H
#include <QQuickItem>
class QmlListModel;
class QmlBannerListModel;
class QmlBannerListItem;
class GUIBannerListView : public QQuickItem
{
Q_OBJECT
Q_PROPERTY( QmlBannerListModel* p_pListModel MEMBER m_pModel NOTIFY signalModelChanged )
public:
explicit GUIBannerListView( QQuickItem *a_pParent = 0 );
virtual ~GUIBannerListView(); /** Destructor **/
void SetModel( QmlBannerListModel* a_pModel );
QmlBannerListModel* GetModel() const { return m_pModel; }
void Clear();
Q_INVOKABLE void OnClicked( int a_iItemIndex );
signals:
void signalModelChanged();
public slots:
private:
QmlBannerListModel* m_pModel;
};
#endif // GUIBANNERLISTVIEW_H

View File

@@ -0,0 +1,88 @@
#include "guibannerwidget.h"
#include <QHBoxLayout>
#include <QCheckBox>
#include <QtQuick/QQuickView>
// qml
#include "guibannercontrol.h"
#include "guibannerlistview.h"
#include "qmlbannerlistmodel.h"
#include "qmlbannerlistitem.h"
#include "qmlimageprovider.h"
// web
#include "../../CSCPublicAPI/casinocoinwebapi.h"
#include "../../CSCPublicAPI/casinocoinwebapiparser.h"
GUIBannerWidget::GUIBannerWidget(QWidget *parent)
: QWidget(parent)
, m_pBannerControl( 0 )
, m_pWebApiParserTemporary( new CasinoCoinWebAPIParser( this ) )
, m_pWebApiTemporary( new CasinoCoinWebAPI( this ) )
{
registerCustomQmlTypes();
connect( m_pWebApiTemporary, SIGNAL( signalResponseReady(const QByteArray&)), m_pWebApiParserTemporary, SLOT( slotParseAnswer(const QByteArray&)), Qt::UniqueConnection );
connect( m_pWebApiTemporary, SIGNAL( signalNetworkError(QNetworkReply::NetworkError)), this, SLOT( slotNetworkError(QNetworkReply::NetworkError)), Qt::UniqueConnection );
}
GUIBannerWidget::~GUIBannerWidget()
{
}
void GUIBannerWidget::registerCustomQmlTypes()
{
qmlRegisterType<GUIBannerControl>("CasinoCoinControls", 1, 0, "GUIBannerControl" );
qmlRegisterType<GUIBannerListView>("CasinoCoinControls", 1, 0, "GUIBannerListView" );
qmlRegisterType<QmlBannerListModel>("CasinoCoinControls", 1, 0, "QmlBannerListModel" );
qmlRegisterType<QmlImageProvider>("CasinoCoinControls", 1, 0, "QmlImageProvider" );
}
QWidget* GUIBannerWidget::dockQmlToWidget()
{
QQuickView* pBannerWindow = new QQuickView;
pBannerWindow->setSource( QUrl( QStringLiteral( "qrc:/qml/qtquick_controls/qml/QmlGUIBannerWindow.qml" ) ) );
QWidget* pPlaceHolder = QWidget::createWindowContainer( pBannerWindow, this );
pPlaceHolder->setMinimumSize( 445, 120 );
pPlaceHolder->setMaximumSize( 445, 120 );
pPlaceHolder->setStyleSheet( "background-color: rgb(242, 241, 240);");
QQuickItem* pRootObject = pBannerWindow->rootObject();
if ( pRootObject )
{
m_pBannerControl = pRootObject->findChild<GUIBannerControl*>();
if ( m_pBannerControl )
{
if ( m_pWebApiParserTemporary )
{
connect( m_pWebApiParserTemporary, SIGNAL( signalActivePromotionsParsed(JsonActivePromotionsParser*)), m_pBannerControl, SLOT( slotPopulateFromWeb(JsonActivePromotionsParser*)), Qt::UniqueConnection );
}
m_pBannerControl->setWidth( ( 115 * 3 ) + ( 4 * 10 ) + 60 );
m_pBannerControl->setHeight( 115 );
}
}
return pPlaceHolder;
}
void GUIBannerWidget::PopulateBannerFromWeb()
{
if ( m_pWebApiTemporary )
{
m_pWebApiTemporary->GetActivePromotions();
}
}
void GUIBannerWidget::PopulateBannerLocally()
{
if ( m_pBannerControl )
{
m_pBannerControl->slotPopulateLocally();
}
}
void GUIBannerWidget::slotNetworkError( QNetworkReply::NetworkError a_eError )
{
PopulateBannerLocally();
}

View File

@@ -0,0 +1,36 @@
#ifndef GUIBANNERWIDGET_H
#define GUIBANNERWIDGET_H
#include <QWidget>
#include <QNetworkReply>
class CasinoCoinWebAPIParser;
class CasinoCoinWebAPI;
class GUIBannerControl;
class GUIBannerWidget : public QWidget
{
Q_OBJECT
public:
GUIBannerWidget(QWidget *parent = 0);
~GUIBannerWidget();
QWidget* dockQmlToWidget();
void PopulateBannerFromWeb();
void PopulateBannerLocally();
private:
void registerCustomQmlTypes();
GUIBannerControl* m_pBannerControl;
CasinoCoinWebAPIParser* m_pWebApiParserTemporary;
CasinoCoinWebAPI* m_pWebApiTemporary;
private slots:
void slotNetworkError( QNetworkReply::NetworkError a_eError );
};
#endif // GUIBANNERWIDGET_H

View File

@@ -0,0 +1,19 @@
#ifndef LISTITEMINTERFACE_H
#define LISTITEMINTERFACE_H
#include <QObject>
class ListItemInterface : public QObject
{
Q_OBJECT
public:
ListItemInterface( QObject* a_pParent = 0 ) : QObject( a_pParent ){} /** Constructor **/
virtual ~ListItemInterface() {} /** Destructor **/
virtual QVariant GetData( int a_iRole ) const = 0; /** Data Accesor **/
virtual bool SetData( int a_iRole, QVariant a_variantData ) = 0; /** Sets given value at specified role **/
virtual QHash<int, QByteArray> RoleNames() const = 0; /** User roles in model **/
};
#endif // LISTITEMINTERFACE_H

View File

@@ -0,0 +1,32 @@
#include "qmlbannerlistitem.h"
#include <QDebug>
QmlBannerListItem::QmlBannerListItem(QString a_strImageSource, QString a_strDestinationUrl, QString a_strDescription, QObject* a_pParent )
: QmlListItem( QVariant( a_strImageSource ), QVariant( a_strDestinationUrl ), QVariant( a_strDescription ), a_pParent )
{
}
QmlBannerListItem::QmlBannerListItem( const JsonSingleActivePromotion& a_rCasinoDescription, QObject* a_pParent )
: QmlListItem( QVariant( a_rCasinoDescription.GetImagePath() ), a_rCasinoDescription.find( "access_url" ).value().toVariant(), a_rCasinoDescription.find( "description" ).value().toVariant(), a_pParent )
{
}
QmlBannerListItem::QmlBannerListItem( QObject* a_pParent )
: QmlListItem( QVariant( "" ), QVariant( "" ), QVariant( "" ), a_pParent )
{
}
QmlBannerListItem::~QmlBannerListItem()
{
}
QHash<int, QByteArray> QmlBannerListItem::RoleNames() const
{
QHash<int, QByteArray> aRoleNames;
aRoleNames[ROLE_IMAGE_SOURCE] = "m_imageSource";
aRoleNames[ROLE_DESTINATION_URL] = "m_destinationUrl";
aRoleNames[ROLE_DESCRIPTION] = "m_description";
return aRoleNames;
}

View File

@@ -0,0 +1,37 @@
#ifndef QMLBANNERLISTITEM_H
#define QMLBANNERLISTITEM_H
#include "qmllistitem.h"
#include "../../CSCPublicAPI/jsonsingleactivepromotion.h"
class QmlBannerListItem : public QmlListItem
{
Q_OBJECT
public:
enum EBannerRoles /** User-specific model roles **/
{ ROLE_IMAGE_SOURCE = ROLE_1
, ROLE_DESTINATION_URL = ROLE_2
, ROLE_DESCRIPTION = ROLE_3
};
explicit QmlBannerListItem
( QString a_strImageSource
, QString a_strDestinationUrl
, QString a_strDescription
, QObject *a_pParent = 0
);
explicit QmlBannerListItem ( const JsonSingleActivePromotion& a_rCasinoDescription
, QObject *a_pParent = 0
);
explicit QmlBannerListItem( QObject *a_pParent = 0 );
virtual ~QmlBannerListItem();
virtual QHash<int, QByteArray> RoleNames() const; /** Define class-specific roles **/
signals:
public slots:
};
#endif // QMLBANNERLISTITEM_H

View File

@@ -0,0 +1,30 @@
#include "qmlbannerlistmodel.h"
#include "qmlbannerlistitem.h"
#include "../../CSCPublicAPI/jsonactivepromotionsparser.h"
#include "../../CSCPublicAPI/jsonsingleactivepromotion.h"
#include <QDebug>
QmlBannerListModel::QmlBannerListModel( QObject* a_pParent )
: QmlListModel( new QmlBannerListItem( 0 ), a_pParent )
{
}
QmlBannerListModel::QmlBannerListModel
( const JsonActivePromotionsParser& a_rActivePromotions
, QObject* a_pParent
)
: QmlListModel( new QmlBannerListItem( 0 ), a_pParent )
{
foreach( const JsonSingleActivePromotion& rPromotion, a_rActivePromotions.GetPromotions() )
{
append( new QmlBannerListItem( rPromotion, this ) );
}
}
QmlBannerListModel::~QmlBannerListModel()
{
}

View File

@@ -0,0 +1,26 @@
#ifndef QMLBANNERLISTMODEL_H
#define QMLBANNERLISTMODEL_H
#include "qmllistmodel.h"
class QmlBannerListItem;
class JsonActivePromotionsParser;
class QmlBannerListModel : public QmlListModel
{
Q_OBJECT
public:
explicit QmlBannerListModel( QObject* a_pParent = 0 );
explicit QmlBannerListModel
( const JsonActivePromotionsParser& a_rActivePromotions
, QObject* a_pParent = 0
);
virtual ~QmlBannerListModel(); /** Destructor **/
signals:
public slots:
};
#endif // QMLBANNERLISTMODEL_H

View File

@@ -0,0 +1,39 @@
#include "qmlimageprovider.h"
QmlImageProvider::QmlImageProvider( QQuickItem* a_pParent )
: QQuickPaintedItem( a_pParent )
{
}
QmlImageProvider::QmlImageProvider( const QImage& a_rImage, QQuickItem* a_pParent )
: QQuickPaintedItem( a_pParent )
, m_oImage( a_rImage )
{
}
void QmlImageProvider::SetImage( const QImage& a_rImage )
{
if ( a_rImage != m_oImage )
{
m_oImage = QImage( a_rImage );
emit signalImageChanged();
}
}
void QmlImageProvider::paint( QPainter* a_pPainter )
{
if ( a_pPainter )
{
a_pPainter->drawImage( QPoint( 0, 0 ), m_oImage );
}
}
void QmlImageProvider::paintImage( const QImage& a_rImage, QPainter* a_pPainter )
{
SetImage( a_rImage );
if ( a_pPainter )
{
a_pPainter->drawImage( QPoint( 0, 0 ), m_oImage );
}
}

View File

@@ -0,0 +1,26 @@
#ifndef QMLIMAGEPROVIDER_H
#define QMLIMAGEPROVIDER_H
#include <QQuickPaintedItem>
#include <QImage>
#include <QPainter>
class QmlImageProvider : public QQuickPaintedItem
{
Q_OBJECT
Q_PROPERTY( QImage p_oImage MEMBER m_oImage NOTIFY signalImageChanged )
public:
explicit QmlImageProvider( QQuickItem* a_pParent = 0 );
explicit QmlImageProvider( const QImage& a_rImage, QQuickItem* a_pParent = 0 );
void SetImage( const QImage& a_rImage );
virtual void paint( QPainter* a_pPainter );
void paintImage( const QImage& a_rImage, QPainter* a_pPainter );
signals:
void signalImageChanged();
private:
QImage m_oImage;
};
#endif // QMLIMAGEPROVIDER_H

View File

@@ -0,0 +1,196 @@
#include "qmllistitem.h"
#include <QDebug>
QmlListItem::QmlListItem( QObject* a_pParent )
: ListItemInterface( a_pParent )
{
// Nothing
}
QmlListItem::QmlListItem( QVariant a_role1
, QObject* a_pParent
)
: ListItemInterface( a_pParent )
{
m_aDataHash[ROLE_1] = a_role1;
}
QmlListItem::QmlListItem( QVariant a_role1
, QVariant a_role2
, QObject* a_pParent
)
: ListItemInterface( a_pParent )
{
m_aDataHash[ROLE_1] = a_role1;
m_aDataHash[ROLE_2] = a_role2;
}
QmlListItem::QmlListItem( QVariant a_role1
, QVariant a_role2
, QVariant a_role3
, QObject* a_pParent
)
: ListItemInterface( a_pParent )
{
m_aDataHash[ROLE_1] = a_role1;
m_aDataHash[ROLE_2] = a_role2;
m_aDataHash[ROLE_3] = a_role3;
}
QmlListItem::QmlListItem( QVariant a_role1
, QVariant a_role2
, QVariant a_role3
, QVariant a_role4
, QObject* a_pParent
)
: ListItemInterface( a_pParent )
{
m_aDataHash[ROLE_1] = a_role1;
m_aDataHash[ROLE_2] = a_role2;
m_aDataHash[ROLE_3] = a_role3;
m_aDataHash[ROLE_4] = a_role4;
}
QmlListItem::QmlListItem( QVariant a_role1
, QVariant a_role2
, QVariant a_role3
, QVariant a_role4
, QVariant a_role5
, QObject* a_pParent
)
: ListItemInterface( a_pParent )
{
m_aDataHash[ROLE_1] = a_role1;
m_aDataHash[ROLE_2] = a_role2;
m_aDataHash[ROLE_3] = a_role3;
m_aDataHash[ROLE_4] = a_role4;
m_aDataHash[ROLE_5] = a_role5;
}
QmlListItem::QmlListItem( QVariant a_role1
, QVariant a_role2
, QVariant a_role3
, QVariant a_role4
, QVariant a_role5
, QVariant a_role6
, QObject* a_pParent
)
: ListItemInterface( a_pParent )
{
m_aDataHash[ROLE_1] = a_role1;
m_aDataHash[ROLE_2] = a_role2;
m_aDataHash[ROLE_3] = a_role3;
m_aDataHash[ROLE_4] = a_role4;
m_aDataHash[ROLE_5] = a_role5;
m_aDataHash[ROLE_6] = a_role6;
}
QmlListItem::QmlListItem( QVariant a_role1
, QVariant a_role2
, QVariant a_role3
, QVariant a_role4
, QVariant a_role5
, QVariant a_role6
, QVariant a_role7
, QObject* a_pParent
)
: ListItemInterface( a_pParent )
{
m_aDataHash[ROLE_1] = a_role1;
m_aDataHash[ROLE_2] = a_role2;
m_aDataHash[ROLE_3] = a_role3;
m_aDataHash[ROLE_4] = a_role4;
m_aDataHash[ROLE_5] = a_role5;
m_aDataHash[ROLE_6] = a_role6;
m_aDataHash[ROLE_7] = a_role7;
}
QmlListItem::QmlListItem( QVariant a_role1
, QVariant a_role2
, QVariant a_role3
, QVariant a_role4
, QVariant a_role5
, QVariant a_role6
, QVariant a_role7
, QVariant a_role8
, QObject* a_pParent
)
: ListItemInterface( a_pParent )
{
m_aDataHash[ROLE_1] = a_role1;
m_aDataHash[ROLE_2] = a_role2;
m_aDataHash[ROLE_3] = a_role3;
m_aDataHash[ROLE_4] = a_role4;
m_aDataHash[ROLE_5] = a_role5;
m_aDataHash[ROLE_6] = a_role6;
m_aDataHash[ROLE_7] = a_role7;
m_aDataHash[ROLE_8] = a_role8;
}
QmlListItem::QmlListItem( QVariant a_role1
, QVariant a_role2
, QVariant a_role3
, QVariant a_role4
, QVariant a_role5
, QVariant a_role6
, QVariant a_role7
, QVariant a_role8
, QVariant a_role9
, QObject* a_pParent
)
: ListItemInterface( a_pParent )
{
m_aDataHash[ROLE_1] = a_role1;
m_aDataHash[ROLE_2] = a_role2;
m_aDataHash[ROLE_3] = a_role3;
m_aDataHash[ROLE_4] = a_role4;
m_aDataHash[ROLE_5] = a_role5;
m_aDataHash[ROLE_6] = a_role6;
m_aDataHash[ROLE_7] = a_role7;
m_aDataHash[ROLE_8] = a_role8;
m_aDataHash[ROLE_9] = a_role9;
}
QmlListItem::QmlListItem( const QmlListItem& a_rOther )
: ListItemInterface( a_rOther.parent() )
, m_aDataHash( a_rOther.m_aDataHash )
{
// Nothing
}
QmlListItem::~QmlListItem()
{
// Nothing
}
QVariant QmlListItem::GetData( int a_iRole ) const
{
return m_aDataHash[static_cast<ERoles>( a_iRole )];
}
bool QmlListItem::SetData( int a_iRole, QVariant a_variantData )
{
if ( m_aDataHash[static_cast<ERoles>( a_iRole )] != a_variantData )
{
m_aDataHash[static_cast<ERoles>( a_iRole )] = a_variantData;
return true;
}
return false;
}
QHash<int, QByteArray> QmlListItem::RoleNames() const
{
QHash<int, QByteArray> aRoleNames;
aRoleNames[ROLE_1] = "m_role1";
aRoleNames[ROLE_2] = "m_role2";
aRoleNames[ROLE_3] = "m_role3";
aRoleNames[ROLE_4] = "m_role4";
aRoleNames[ROLE_5] = "m_role5";
aRoleNames[ROLE_6] = "m_role6";
aRoleNames[ROLE_7] = "m_role7";
aRoleNames[ROLE_8] = "m_role8";
return aRoleNames;
}

View File

@@ -0,0 +1,109 @@
#ifndef QMLLISTITEM_H
#define QMLLISTITEM_H
// Qt
#include <QVariant>
#include <QColor>
#include <QHash>
// ptcommon
#include "listiteminterface.h"
class QmlListItem : public ListItemInterface
{
Q_OBJECT
public:
enum ERoles /** User-specific model roles **/
{ ROLE_1 = Qt::UserRole + 1
, ROLE_2 = Qt::UserRole + 2
, ROLE_3 = Qt::UserRole + 3
, ROLE_4 = Qt::UserRole + 4
, ROLE_5 = Qt::UserRole + 5
, ROLE_6 = Qt::UserRole + 6
, ROLE_7 = Qt::UserRole + 7
, ROLE_8 = Qt::UserRole + 8
, ROLE_9 = Qt::UserRole + 9
, ROLE_10 = Qt::UserRole + 10
, ROLE_LAST = Qt::UserRole + 11
};
explicit QmlListItem( QObject* a_pParent = 0 ); /** Default Constructor **/
explicit QmlListItem( QVariant a_role1
, QObject* a_pParent = 0
); /** Constructor with first role filled **/
explicit QmlListItem( QVariant a_role1
, QVariant a_role2
, QObject* a_pParent = 0
); /** Contstructor with first two roles filled **/
explicit QmlListItem( QVariant a_role1
, QVariant a_role2
, QVariant a_role3
, QObject* a_pParent = 0
); /** Contstructor with first three roles filled **/
explicit QmlListItem( QVariant a_role1
, QVariant a_role2
, QVariant a_role3
, QVariant a_role4
, QObject* a_pParent = 0
); /** Contstructor with first four roles filled **/
explicit QmlListItem( QVariant a_role1
, QVariant a_role2
, QVariant a_role3
, QVariant a_role4
, QVariant a_role5
, QObject* a_pParent = 0
); /** Contstructor with first five roles filled **/
explicit QmlListItem( QVariant a_role1
, QVariant a_role2
, QVariant a_role3
, QVariant a_role4
, QVariant a_role5
, QVariant a_role6
, QObject* a_pParent = 0
); /** Contstructor with first six roles filled **/
explicit QmlListItem( QVariant a_role1
, QVariant a_role2
, QVariant a_role3
, QVariant a_role4
, QVariant a_role5
, QVariant a_role6
, QVariant a_role7
, QObject* a_pParent = 0
); /** Contstructor with first seven roles filled **/
explicit QmlListItem( QVariant a_role1
, QVariant a_role2
, QVariant a_role3
, QVariant a_role4
, QVariant a_role5
, QVariant a_role6
, QVariant a_role7
, QVariant a_role8
, QObject* a_pParent = 0
); /** Contstructor with first eight roles filled **/
explicit QmlListItem( QVariant a_role1
, QVariant a_role2
, QVariant a_role3
, QVariant a_role4
, QVariant a_role5
, QVariant a_role6
, QVariant a_role7
, QVariant a_role8
, QVariant a_role9
, QObject* a_pParent = 0
); /** Contstructor with first nine roles filled **/
QmlListItem( const QmlListItem& a_rOther ); /** Copy constructor **/
virtual ~QmlListItem(); /** Destructor **/
virtual QVariant GetData( int a_iRole ) const; /** Retrieve role-specific data from class **/
virtual bool SetData( int a_iRole, QVariant a_variantData ); /** Sets given value at specified role **/
virtual QHash<int, QByteArray> RoleNames() const; /** Define class-specific roles **/
private:
QHash<ERoles, QVariant> m_aDataHash; /** Container keeping all item-specific roles **/
};
#endif // QMLLISTITEM_H

View File

@@ -0,0 +1,315 @@
#include "qmllistmodel.h"
#include <QDebug>
QmlListModel::QmlListModel( QObject* a_pParent )
: QAbstractListModel( a_pParent )
, m_pPrototype( 0 )
, m_iCurrentElementIndex( -1 )
{
m_aItems = QList<ListItemInterface*>();
}
QmlListModel::QmlListModel( ListItemInterface* a_pPrototype, QObject* a_pParent )
: QAbstractListModel( a_pParent )
, m_pPrototype( a_pPrototype )
, m_iCurrentElementIndex( -1 )
{
m_aItems = QList<ListItemInterface*>();
}
QmlListModel::~QmlListModel()
{
clear();
if ( m_pPrototype )
{
delete m_pPrototype;
m_pPrototype = 0;
}
}
int QmlListModel::rowCount( const QModelIndex& a_rParent ) const
{
Q_UNUSED( a_rParent );
return m_aItems.size();
}
QVariant QmlListModel::data( const QModelIndex& a_rIndex, int a_iRole ) const
{
if ( a_rIndex.row() < 0 )
{
return QVariant();
}
if ( a_rIndex.row() > m_aItems.size() )
{
return QVariant();
}
ListItemInterface* pValidate = m_aItems.at( a_rIndex.row() );
if ( pValidate )
{
return pValidate->GetData( a_iRole );
}
return QVariant();
}
bool QmlListModel::setData( const QModelIndex& a_rIndex, const QVariant& a_rValue, int a_iRole )
{
if ( a_rIndex.isValid() && a_iRole > Qt::UserRole )
{
ListItemInterface* pItem = m_aItems.at( a_rIndex.row() );
if ( pItem )
{
if ( pItem->SetData( a_iRole, a_rValue ) )
{
emit dataChanged( a_rIndex, a_rIndex );
return true;
}
}
}
return false;
}
QHash<int, QByteArray> QmlListModel::roleNames() const //NS
{
if ( m_pPrototype )
{
return m_pPrototype->RoleNames();
}
return ( QHash<int, QByteArray>() );
}
bool QmlListModel::removeRows( int a_iRow, int a_iCount, const QModelIndex& a_rParent ) //NS
{
if ( a_iRow < 0 )
{
return false;
}
if ( a_iCount <= 0 )
{
return false;
}
if ( ( a_iRow + a_iCount ) > m_aItems.size() )
{
return false;
}
beginRemoveRows( a_rParent, a_iRow, a_iRow + a_iCount - 1 );
for ( int i = 0; i < a_iCount; i++ )
{
ListItemInterface* pItem = m_aItems.takeAt( a_iRow );
delete pItem;
pItem = 0;
}
endRemoveRows();
emit signalCountChanged( rowCount() );
return true;
}
void QmlListModel::append( ListItemInterface* a_pItem ) //NS
{
if ( a_pItem != 0 )
{
beginInsertRows( QModelIndex(), rowCount(), rowCount() );
m_aItems.append( a_pItem );
a_pItem->setObjectName( objectName() + "_item_" + QString::number( rowCount() ) );
endInsertRows();
emit signalCountChanged( rowCount() );
}
}
void QmlListModel::insert( int a_iIndex, ListItemInterface* a_pItem ) //NS
{
if ( a_pItem != 0 && a_iIndex >= 0 && a_iIndex <= m_aItems.size() )
{
beginInsertRows( QModelIndex(), a_iIndex, a_iIndex );
m_aItems.insert( a_iIndex, a_pItem );
a_pItem->setObjectName( objectName() + "_item_" + QString::number( a_iIndex ) );
endInsertRows();
emit signalCountChanged( rowCount() );
}
}
QVariant QmlListModel::get( int a_iIndex )
{
if ( a_iIndex < 0 )
{
return QVariant();
}
if ( a_iIndex >= m_aItems.size() )
{
return QVariant();
}
QMap<QString, QVariant> aItemData;
ListItemInterface* pItem = m_aItems.at( a_iIndex );
if ( pItem )
{
QHashIterator<int, QByteArray> aRolesItr( pItem->RoleNames() );
while ( aRolesItr.hasNext() )
{
aRolesItr.next();
aItemData.insert( aRolesItr.value(), QVariant( pItem->GetData( aRolesItr.key() ) ) );
}
return QVariant( aItemData );
}
return QVariant();
}
void QmlListModel::clear()
{
if ( m_aItems.size() > 0 )
{
removeRows( 0, m_aItems.size() );
// signalCountChanged emitted inside removeRows
}
}
void QmlListModel::refresh()
{
if ( m_aItems.size() > 0 )
{
beginResetModel();
endResetModel();
}
}
int QmlListModel::GetItemIndex( int a_iItemRole, QVariant a_variantValue ) const
{
for ( int i = 0; i < m_aItems.size(); i++ )
{
if ( m_aItems[i]->GetData( a_iItemRole ) == a_variantValue )
{
return i;
}
}
return -1;
}
ListItemInterface* QmlListModel::GetCurrentItem() const
{
if ( m_iCurrentElementIndex >= 0 && m_aItems.size() > m_iCurrentElementIndex )
{
return m_aItems[m_iCurrentElementIndex];
}
return 0;
}
void QmlListModel::SetCurrentItemIndex( int a_iIndex )
{
if ( a_iIndex == -1
|| ( a_iIndex >= 0 && a_iIndex < rowCount() )
)
{
if ( m_iCurrentElementIndex != a_iIndex )
{
m_iCurrentElementIndex = a_iIndex;
emit signalCurrentItemIndexChanged();
}
else
{
if ( m_iCurrentElementIndex != -1 )
{
emit signalCurrentItemIndexClicked();
}
}
}
}
int QmlListModel::GetCurrentItemIndex() const
{
return m_iCurrentElementIndex;
}
bool QmlListModel::SetData( int a_iIndex, const QVariant& a_rValue, int a_iRole )
{
QModelIndex modelIndex = createIndex( a_iIndex, 0 );
return setData( modelIndex, a_rValue, a_iRole );
}
QVariant QmlListModel::GetData( int a_iIndex, int a_iRole )
{
QModelIndex modelIndex = createIndex( a_iIndex, 0 );
return data( modelIndex, a_iRole );
}
void QmlListModel::Sort( int a_iRole, Qt::SortOrder a_eOrder )
{
QList<ListItemInterface*> aSortedList;
if ( a_eOrder == Qt::AscendingOrder )
{
foreach ( ListItemInterface* pItem, m_aItems )
{
int iSortedIndex = 0;
const int iSortedListCount = aSortedList.count();
QVariant valueOriginal = pItem->GetData( a_iRole );
bool bIndexFound = false;
for ( iSortedIndex = iSortedListCount - 1 ; iSortedIndex >= 0 ; --iSortedIndex )
{
const ListItemInterface* pSortedItem = aSortedList.at( iSortedIndex );
if ( pSortedItem )
{
QVariant valueSorted = pSortedItem->GetData( a_iRole );
if ( Compare( a_iRole, valueOriginal, valueSorted ) > 0 )
{
aSortedList.insert( iSortedIndex + 1, pItem );
bIndexFound = true;
break;
}
}
}
if ( !bIndexFound )
{
aSortedList.prepend( pItem );
}
}
}
else if ( Qt::DescendingOrder )
{
foreach ( ListItemInterface* pItem, m_aItems )
{
int iSortedIndex = 0;
const int iSortedListCount = aSortedList.count();
QVariant valueOriginal = pItem->GetData( a_iRole );
bool bIndexFound = false;
for ( iSortedIndex = 0 ; iSortedIndex < iSortedListCount ; ++iSortedIndex )
{
const ListItemInterface* pSortedItem = aSortedList.at( iSortedIndex );
if ( pSortedItem )
{
QVariant valueSorted = pSortedItem->GetData( a_iRole );
if ( Compare( a_iRole, valueOriginal, valueSorted ) > 0 )
{
aSortedList.insert( iSortedIndex, pItem );
bIndexFound = true;
break;
}
}
}
if ( !bIndexFound )
{
aSortedList.append( pItem );
}
}
}
m_aItems = aSortedList;
emit dataChanged( createIndex( 0, 0 ), createIndex( rowCount() - 1, 0 ) );
}
int QmlListModel::Compare( int a_iRole, QVariant& a_rValue1, QVariant& a_rValue2 )
{
Q_UNUSED( a_iRole );
int iReturn = 0;
if ( a_rValue1 > a_rValue2 )
{
iReturn = 1;
}
else if ( a_rValue1 < a_rValue2 )
{
iReturn = -1;
}
return iReturn;
}

View File

@@ -0,0 +1,83 @@
#ifndef QMLLISTMODEL_H
#define QMLLISTMODEL_H
#include <QAbstractListModel>
#include <QColor>
#include "qmllistitem.h"
class QmlListModel : public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY( int count READ rowCount NOTIFY signalCountChanged )
Q_PROPERTY( int p_iCurrentElementIndex MEMBER m_iCurrentElementIndex NOTIFY signalCurrentItemIndexChanged )
public:
//////////////////
// Methods
explicit QmlListModel( QObject* a_pParent = 0 ); /** Default Constructor **/
explicit QmlListModel( ListItemInterface* a_pPrototype, QObject* a_pParent = 0 ); /** Constructor with item prototype **/
virtual ~QmlListModel(); /** Destructor **/
int rowCount( const QModelIndex& a_rParent = QModelIndex() ) const; /** size of list getter **/
QVariant data( const QModelIndex& a_rIndex, int a_iRole = Qt::DisplayRole ) const; /** function returning model data at specific index **/
virtual bool setData /** Sets the role data for the item at index to value. **/
( const QModelIndex& a_rIndex
, const QVariant& a_rValue
, int a_iRole = Qt::EditRole
);
QHash<int, QByteArray> roleNames() const; /** returns user defined roles for model **/
bool removeRows( int a_iRow, int a_iCount, const QModelIndex& a_rParent = QModelIndex() ); /** deletes /count/ rows from model starting at /row/ **/
// Methods created to match QML Model style of data manipulation
Q_INVOKABLE void append( ListItemInterface* a_pItem ); /** appends /a_pItem/ element to the list **/
Q_INVOKABLE void insert( int a_iIndex, ListItemInterface* a_pItem ); /** insert /a_pItem/ to the list before the given /a_iIndex/ **/
Q_INVOKABLE QVariant get( int a_iIndex ); /** returns QMap containing value<->role pa at /a_iIndex/ **/
Q_INVOKABLE void clear(); /** clears the list **/
Q_INVOKABLE void refresh(); /** refreshes the list **/
inline const ListItemInterface* GetItem( int a_iIndex ) const; /** Get item at index **/
int GetItemIndex( int a_iItemRole, QVariant a_variantValue ) const; /** Get index of first item having given a_variantValue at given a_iItemRole **/
/** NOTE!!
This three methods have their implementation, however whole
currentItem system must be implemented manually for every derived class. **/
ListItemInterface* GetCurrentItem() const; /** Get current item pointer **/
virtual void SetCurrentItemIndex( int a_iIndex ); /** Set current item index **/
int GetCurrentItemIndex() const; /** Get current item index **/
bool SetData( int a_iIndex, const QVariant& a_rValue, int a_iRole ); /** Sets the role data for the item at index to value. **/
QVariant GetData( int a_iIndex, int a_iRole ); /** Get model data at specific index **/
void Sort( int a_iRole, Qt::SortOrder a_eOrder = Qt::AscendingOrder ); /** Sort items in list model by role in the given order **/
signals:
void signalCountChanged( int a_iNewCount ); /** notify that list contents changed **/
void signalCurrentItemIndexChanged(); /** notify that currently highlighted/focused item changed **/
void signalCurrentItemIndexClicked(); /** notify that currently highlighted/focused item was clicked again **/
void signalTextColorCol1Changed(); /** notify that text color in col1 changed **/
void signalTextSizeChanged(); /** notify that text size changed **/
void signalRowHeightChanged(); /** notify that row height changed **/
protected:
/////////////////
// Methods
virtual int Compare( int a_iRole, QVariant& a_rValue1, QVariant& a_rValue2 ); /** Compare two values with given role **/
//////////////////
// Properties
ListItemInterface* m_pPrototype; /** information on what type is held in a list **/
QList<ListItemInterface*> m_aItems; /** list with model data **/
int m_iCurrentElementIndex; /** current element index **/
};
const ListItemInterface* QmlListModel::GetItem( int a_iIndex ) const
{
if ( m_aItems.size() > a_iIndex && a_iIndex >= 0 )
{
return m_aItems[a_iIndex];
}
return 0;
}
#endif // QMLLISTMODEL_H