Exchanges widget

This commit is contained in:
felixrojauro
2015-11-27 15:43:53 +01:00
parent 18f49ef4e5
commit 59d16a1d12
18 changed files with 1024 additions and 738 deletions

View File

@@ -0,0 +1,41 @@
#include "guiexchangescontrol.h"
#include "guiexchangeslistview.h"
#include "qmlexchangeslistmodel.h"
#include "qmlexchangeslistitem.h"
GUIExchangesControl::GUIExchangesControl( QQuickItem* a_pParent )
: QQuickItem( a_pParent )
, m_pExchangesView( 0 )
{
}
GUIExchangesControl::~GUIExchangesControl()
{
if ( m_pExchangesView )
{
delete m_pExchangesView;
m_pExchangesView = 0;
}
}
void GUIExchangesControl::InitializeExchangesView( GUIExchangesListView* a_pView )
{
if ( a_pView )
{
if ( m_pExchangesView )
{
delete m_pExchangesView;
}
m_pExchangesView = a_pView;
}
}
void GUIExchangesControl::slotPopulateListView( JsonActiveExchangesParser* a_pActiveExchanges )
{
if ( m_pExchangesView )
{
QmlExchangesListModel* pExchangesModel = new QmlExchangesListModel( *a_pActiveExchanges );
m_pExchangesView->SetModel( pExchangesModel );
}
}

View File

@@ -0,0 +1,31 @@
#ifndef GUIEEXCHANGESCONTROL_H
#define GUIEEXCHANGESCONTROL_H
#include <QQuickItem>
#include "../../CSCPublicAPI/jsonactiveexchangesparser.h"
class GUIExchangesListView;
class GUIExchangesControl : public QQuickItem
{
Q_OBJECT
public:
GUIExchangesControl( QQuickItem* a_pParent = 0 );
virtual ~GUIExchangesControl();
Q_INVOKABLE void InitializeExchangesView( GUIExchangesListView* a_pView );
GUIExchangesListView* GetExchangesView() const { return m_pExchangesView; }
public slots:
void slotPopulateListView( JsonActiveExchangesParser* a_pActiveExchanges );
signals:
public slots:
private:
GUIExchangesListView* m_pExchangesView;
};
#endif // GUIEEXCHANGESCONTROL_H

View File

@@ -0,0 +1,50 @@
#include "guiexchangeslistview.h"
#include "qmlexchangeslistitem.h"
#include "qmlexchangeslistmodel.h"
#include "qmllistmodel.h"
#include <QDesktopServices>
#include <QUrl>
GUIExchangesListView::GUIExchangesListView( QQuickItem* a_pParent )
: QQuickItem( a_pParent )
, m_pModel( 0 )
{
}
void GUIExchangesListView::Clear()
{
if ( m_pModel )
{
m_pModel->clear();
}
}
void GUIExchangesListView::OnClicked( int a_iItemIndex )
{
if ( m_pModel )
{
QString strUrl = m_pModel->GetData( a_iItemIndex, QmlExchangesListItem::ROLE_DESTINATION_URL ).toString();
QDesktopServices::openUrl( QUrl( strUrl ) );
}
}
GUIExchangesListView::~GUIExchangesListView()
{
if ( m_pModel )
{
delete m_pModel;
m_pModel = 0;
}
}
void GUIExchangesListView::SetModel( QmlExchangesListModel* a_pModel )
{
if ( m_pModel )
{
delete m_pModel;
}
m_pModel = a_pModel;
emit signalModelChanged();
}

View File

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

View File

@@ -0,0 +1,104 @@
#include "guiexchangeswidget.h"
#include <QHBoxLayout>
#include <QCheckBox>
#include <QQuickView>
#include <QQmlContext>
#include "gui20_skin.h"
// qml
#include "guiexchangescontrol.h"
#include "guiexchangeslistview.h"
#include "qmlexchangeslistmodel.h"
#include "qmlexchangeslistitem.h"
#include "qmlimageprovider.h"
// web
#include "../../CSCPublicAPI/casinocoinwebapi.h"
#include "../../CSCPublicAPI/casinocoinwebapiparser.h"
GUIExchangesWidget::GUIExchangesWidget(QWidget *parent)
: QWidget(parent)
, m_pExchangesControl( 0 )
, m_pWebApiParserTemporary( new CasinoCoinWebAPIParser( this ) )
, m_pWebApiTemporary( new CasinoCoinWebAPI( this ) )
, m_pQmlImageProvider( 0 )
{
registerCustomQmlTypes();
connect( m_pWebApiTemporary, SIGNAL( signalResponseReady(const QByteArray&)), m_pWebApiParserTemporary, SLOT( slotParseAnswer(const QByteArray&)), Qt::UniqueConnection );
connect( m_pWebApiTemporary, SIGNAL( signalNetworkError(QNetworkReply::NetworkError,const QUrl)), m_pWebApiParserTemporary, SLOT( slotNetworkError(QNetworkReply::NetworkError,const QUrl)), Qt::UniqueConnection );
connect( m_pWebApiParserTemporary, SIGNAL( signalActiveExchangesParsed(JsonActiveExchangesParser*)), this, SLOT( slotPopulateFromWeb(JsonActiveExchangesParser*)), Qt::UniqueConnection );
}
GUIExchangesWidget::~GUIExchangesWidget()
{
// member objects are moved to qml engine and it manages their instances
}
void GUIExchangesWidget::registerCustomQmlTypes()
{
qmlRegisterType<GUIExchangesControl>("CasinoCoinControls", 1, 0, "GUIExchangesControl" );
qmlRegisterType<GUIExchangesListView>("CasinoCoinControls", 1, 0, "GUIExchangesListView" );
qmlRegisterType<QmlExchangesListModel>("CasinoCoinControls", 1, 0, "QmlExchangesListModel" );
}
QWidget* GUIExchangesWidget::dockQmlToWidget()
{
QQuickView* pExchangesWindow = new QQuickView;
QWidget* pPlaceHolder = 0;
if ( pExchangesWindow )
{
QQmlContext* pContext = pExchangesWindow->rootContext();
if ( pContext )
{
pContext->setContextProperty( "GUI20Skin", &GUI20Skin::Instance() );
}
QQmlEngine* pEngine = pExchangesWindow->engine();
if ( pEngine )
{
m_pQmlImageProvider = new QmlImageProvider();
pEngine->addImageProvider( "exchangesImages", m_pQmlImageProvider );
}
pExchangesWindow->setSource( QUrl( QStringLiteral( "qrc:/qml/qtquick_controls/qml/QmlGUIExchangesWindow.qml" ) ) );
QQuickItem* pRootObject = pExchangesWindow->rootObject();
if ( pRootObject )
{
m_pExchangesControl = pRootObject->findChild<GUIExchangesControl*>();
if ( m_pExchangesControl )
{
m_pExchangesControl->setWidth( 400 );
m_pExchangesControl->setHeight( 120 );
}
}
pPlaceHolder = QWidget::createWindowContainer( pExchangesWindow, this );
if ( pPlaceHolder )
{
pPlaceHolder->setMinimumSize( 400, 120 );
}
}
return pPlaceHolder;
}
void GUIExchangesWidget::PopulateExchangesFromWeb()
{
if ( m_pWebApiTemporary )
{
m_pWebApiTemporary->GetActiveExchanges();
}
}
void GUIExchangesWidget::slotPopulateFromWeb( JsonActiveExchangesParser* a_pExchangesParser )
{
if ( a_pExchangesParser )
{
a_pExchangesParser->AddImagesToPool( m_pQmlImageProvider );
}
if ( m_pExchangesControl )
{
m_pExchangesControl->slotPopulateListView( a_pExchangesParser );
}
}

View File

@@ -0,0 +1,39 @@
#ifndef GUIEXCHANGESWIDGET_H
#define GUIEXCHANGESWIDGET_H
#include <QWidget>
#include <QNetworkReply>
class CasinoCoinWebAPIParser;
class CasinoCoinWebAPI;
class GUIExchangesControl;
class QmlImageProvider;
class JsonActiveExchangesParser;
class GUIExchangesWidget : public QWidget
{
Q_OBJECT
public:
GUIExchangesWidget( QWidget *parent = 0) ;
~GUIExchangesWidget();
QWidget* dockQmlToWidget();
void PopulateExchangesFromWeb();
void PopulateExchangesLocally();
private:
void registerCustomQmlTypes();
GUIExchangesControl* m_pExchangesControl;
CasinoCoinWebAPIParser* m_pWebApiParserTemporary;
CasinoCoinWebAPI* m_pWebApiTemporary;
QmlImageProvider* m_pQmlImageProvider;
private slots:
void slotPopulateFromWeb( JsonActiveExchangesParser* a_pExchangesParser );
};
#endif // GUIEXCHANGESWIDGET_H

View File

@@ -0,0 +1,57 @@
#include "qmlexchangeslistitem.h"
#include <QDebug>
QmlExchangesListItem::QmlExchangesListItem(QString a_strImageSource, QString a_strDestinationUrl, QString a_strExchangesName, double a_dBidPrice, double a_dAskPrice, double a_dLastPrice, QString a_strDescription, QObject* a_pParent )
: QmlListItem ( QVariant( a_strImageSource )
, QVariant( a_strDestinationUrl )
, QVariant( a_strExchangesName )
, QVariant( GetFormattedPrice( a_dBidPrice ) )
, QVariant( GetFormattedPrice( a_dAskPrice ) )
, QVariant( GetFormattedPrice( a_dLastPrice ) )
, QVariant( a_strDescription )
, a_pParent
)
{
}
QmlExchangesListItem::QmlExchangesListItem( const JsonSingleActiveExchange& a_rExchangeDescription, QObject* a_pParent )
: QmlListItem ( QVariant( a_rExchangeDescription.getImageName() )
, QVariant( a_rExchangeDescription.getAccessUrl() )
, QVariant( a_rExchangeDescription.getExchangeName() )
, QVariant( GetFormattedPrice( a_rExchangeDescription.getLastBidPriceBTC() ) )
, QVariant( GetFormattedPrice( a_rExchangeDescription.getLastAskPriceBTC() ) )
, QVariant( GetFormattedPrice( a_rExchangeDescription.getLastPriceBTC() ) )
, QVariant( a_rExchangeDescription.getDescription() )
, a_pParent
)
{
}
QmlExchangesListItem::QmlExchangesListItem( QObject* a_pParent )
: QmlListItem( QVariant( "" ), QVariant( "" ), QVariant( "" ), QVariant( "" ), QVariant( "" ), QVariant( "" ), QVariant( "" ), a_pParent )
{
}
QmlExchangesListItem::~QmlExchangesListItem()
{
}
QHash<int, QByteArray> QmlExchangesListItem::RoleNames() const
{
QHash<int, QByteArray> aRoleNames;
aRoleNames[ROLE_IMAGE_SOURCE] = "m_imageSource";
aRoleNames[ROLE_DESTINATION_URL] = "m_destinationUrl";
aRoleNames[ROLE_EXCHANGE_NAME] = "m_exchangeName";
aRoleNames[ROLE_BID_PRICE] = "m_bidPrice";
aRoleNames[ROLE_ASK_PRICE] = "m_askPrice";
aRoleNames[ROLE_LAST_PRICE] = "m_lastPrice";
aRoleNames[ROLE_DESCRIPTION] = "m_description";
return aRoleNames;
}
QString QmlExchangesListItem::GetFormattedPrice(double a_dPrice)
{
return QString::number( a_dPrice, 'f', 8 );
}

View File

@@ -0,0 +1,47 @@
#ifndef QMLEXCHANGESLISTITEM_H
#define QMLEXCHANGESLISTITEM_H
#include "qmllistitem.h"
#include "../../CSCPublicAPI/jsonsingleactiveexchange.h"
class QmlExchangesListItem : public QmlListItem
{
Q_OBJECT
public:
enum EExchangesRoles /** User-specific model roles **/
{ ROLE_IMAGE_SOURCE = ROLE_1
, ROLE_DESTINATION_URL = ROLE_2
, ROLE_EXCHANGE_NAME = ROLE_3
, ROLE_BID_PRICE = ROLE_4
, ROLE_ASK_PRICE = ROLE_5
, ROLE_LAST_PRICE = ROLE_6
, ROLE_DESCRIPTION = ROLE_7
};
explicit QmlExchangesListItem
( QString a_strImageSource
, QString a_strDestinationUrl
, QString a_strExchangesName
, double a_dBidPrice
, double a_dAskPrice
, double a_dLastPrice
, QString a_strDescription
, QObject *a_pParent = 0
);
explicit QmlExchangesListItem ( const JsonSingleActiveExchange& a_rExchangeDescription
, QObject *a_pParent = 0
);
explicit QmlExchangesListItem( QObject *a_pParent = 0 );
virtual ~QmlExchangesListItem();
virtual QHash<int, QByteArray> RoleNames() const; /** Define class-specific roles **/
signals:
public slots:
private:
QString GetFormattedPrice( double a_dPrice );
};
#endif // QMLEXCHANGESLISTITEM_H

View File

@@ -0,0 +1,30 @@
#include "qmlexchangeslistmodel.h"
#include "qmlexchangeslistitem.h"
#include "../../CSCPublicAPI/jsonactiveexchangesparser.h"
#include "../../CSCPublicAPI/jsonsingleactiveexchange.h"
#include <QDebug>
QmlExchangesListModel::QmlExchangesListModel( QObject* a_pParent )
: QmlListModel( new QmlExchangesListItem( 0 ), a_pParent )
{
}
QmlExchangesListModel::QmlExchangesListModel
( const JsonActiveExchangesParser& a_rActiveExchanges
, QObject* a_pParent
)
: QmlListModel( new QmlExchangesListItem( 0 ), a_pParent )
{
foreach( const JsonSingleActiveExchange& rExchange, a_rActiveExchanges.GetExchanges() )
{
append( new QmlExchangesListItem( rExchange, this ) );
}
}
QmlExchangesListModel::~QmlExchangesListModel()
{
}

View File

@@ -0,0 +1,26 @@
#ifndef QMLEXCHANGESLISTMODEL_H
#define QMLEXCHANGESLISTMODEL_H
#include "qmllistmodel.h"
class QmlExchangesListItem;
class JsonActiveExchangesParser;
class QmlExchangesListModel : public QmlListModel
{
Q_OBJECT
public:
explicit QmlExchangesListModel( QObject* a_pParent = 0 );
explicit QmlExchangesListModel
( const JsonActiveExchangesParser& a_rActiveExchanges
, QObject* a_pParent = 0
);
virtual ~QmlExchangesListModel(); /** Destructor **/
signals:
public slots:
};
#endif // QMLEXCHANGESLISTMODEL_H