Skip to content

Commit

Permalink
Progress FollowMyVote#19: Begin QML wrappers
Browse files Browse the repository at this point in the history
Currently working on creating enum classes for the contest limits and
price line items. Everything's OK so far, except QML always says the
enum values are "undefined"
  • Loading branch information
nathanielhourt committed Jan 11, 2016
1 parent 19486dd commit e657ceb
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 7 deletions.
25 changes: 23 additions & 2 deletions VotingApp/BackendWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "BackendWrapper.hpp"
#include "PromiseConverter.hpp"
#include "wrappers/ContestGeneratorWrapper.hpp"
#include "wrappers/ContestCreationRequest.hpp"

#include <Promise.hpp>

Expand Down Expand Up @@ -64,9 +65,29 @@ ContestGeneratorWrapper* BackendWrapper::getContestsByCoin(quint64 coinId)
return new ContestGeneratorWrapper(request.send().getGenerator(), promiseConverter);
}

void BackendWrapper::getContestCreationRequest()
ContestCreationRequest* BackendWrapper::getContestCreationRequest()
{
auto request = backend;
// Fetch a new creator if we don't have one yet
auto creator = [this] {
KJ_IF_MAYBE(creator, contestCreator)
return *creator;
auto creator = backend.getContestCreatorRequest().send().getCreator();
contestCreator = creator;
return creator;
}();

auto pricesPromise = creator.getPriceScheduleRequest().send();
auto pricesAndLimitsPromise = creator.getContestLimitsRequest().send().then(kj::mvCapture(kj::mv(pricesPromise),
[](kj::Promise<capnp::Response<::ContestCreator::GetPriceScheduleResults>> pricesPromise,
capnp::Response<::ContestCreator::GetContestLimitsResults> limits) {
return pricesPromise.then(kj::mvCapture(limits,
[] (decltype(limits) limits,
capnp::Response<::ContestCreator::GetPriceScheduleResults> prices) {
return std::make_pair(kj::mv(prices), kj::mv(limits));
}));
}));

return new ContestCreationRequest(creator.purchaseContestRequest());
}

} // namespace swv
5 changes: 4 additions & 1 deletion VotingApp/BackendWrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@
#include <QObject>

#include <backend.capnp.h>
#include <contestcreation.capnp.h>

class Promise;
class PromiseConverter;
namespace swv {
class ContestGeneratorWrapper;
class ContestCreationRequest;

/**
* @brief The BackendWrapper class provides a QML-friendly wrapper for the RPC backend
Expand All @@ -52,11 +54,12 @@ class BackendWrapper : public QObject
Q_INVOKABLE swv::ContestGeneratorWrapper* getContestsByCreator(QString creator);
Q_INVOKABLE swv::ContestGeneratorWrapper* getContestsByCoin(quint64 coinId);

Q_INVOKABLE void getContestCreationRequest();
Q_INVOKABLE ContestCreationRequest* getContestCreationRequest();

private:
PromiseConverter& promiseConverter;
Backend::Client backend;
kj::Maybe<ContestCreator::Client> contestCreator;
};

} // namespace swv
Expand Down
4 changes: 3 additions & 1 deletion VotingApp/VotingApp.qbs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ QtGuiApplication {
"wrappers/Decision.hpp",
"wrappers/OwningWrapper.cpp",
"wrappers/OwningWrapper.hpp",
"wrappers/Converters.hpp",
"wrappers/Converters.cpp",
"wrappers/Converters.hpp",
"wrappers/ContestCreationRequest.cpp",
"wrappers/ContestCreationRequest.hpp",
"wrappers/README.md",
"qml-promise/src/Promise.cpp",
"qml-promise/src/Promise.hpp",
Expand Down
8 changes: 8 additions & 0 deletions VotingApp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "wrappers/Decision.hpp"
#include "wrappers/Datagram.hpp"
#include "wrappers/ContestGeneratorWrapper.hpp"
#include "wrappers/ContestCreationRequest.hpp"
#include "BackendWrapper.hpp"
#include "VotingSystem.hpp"
#include "ChainAdaptorWrapper.hpp"
Expand Down Expand Up @@ -74,9 +75,16 @@ int main(int argc, char *argv[])
"ContestGenerator",
QStringLiteral("Contest Generator cannot be created from "
"QML."));
qmlRegisterUncreatableType<swv::ContestGeneratorWrapper>("FollowMyVote.StakeWeightedVoting", 1, 0,
"ContestGenerator",
QStringLiteral("Contest Generator cannot be created from "
"QML."));
qmlRegisterType<swv::VotingSystem>("FollowMyVote.StakeWeightedVoting", 1, 0, "VotingSystem");
qmlRegisterType<Promise>("FollowMyVote.StakeWeightedVoting", 1, 0, "Promise");

qmlRegisterUncreatableType<swv::LineItems>("FollowMyVote.StakeWeightedVoting", 1, 0, "LineItems", "");
qmlRegisterUncreatableType<swv::ContestLimits>("FollowMyVote.StakeWeightedVoting", 1, 0, "ContestLimits", "");

QQmlApplicationEngine engine;
engine.addImportPath(QStringLiteral("qrc:/"));
Promise::setEngine(&engine);
Expand Down
8 changes: 8 additions & 0 deletions VotingApp/wrappers/ContestCreationRequest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "ContestCreationRequest.hpp"

namespace swv {
ContestCreationRequest::ContestCreationRequest(PurchaseRequest&& request)
: request(kj::mv(request))
{
}
} // namespace swv
59 changes: 59 additions & 0 deletions VotingApp/wrappers/ContestCreationRequest.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#ifndef CONTESTCREATIONREQUEST_HPP
#define CONTESTCREATIONREQUEST_HPP

#include <QObject>

#include <contestcreation.capnp.h>

#include "vendor/QQmlEnumClassHelper.h"

namespace swv {

class ContestCreationRequest : public QObject
{
Q_OBJECT
public:
using PurchaseRequest = capnp::Request<ContestCreator::PurchaseContestParams,
ContestCreator::PurchaseContestResults>;

ContestCreationRequest(PurchaseRequest&& request);
virtual ~ContestCreationRequest() noexcept {}

private:
PurchaseRequest request;
};

// Create an enum class for the LineItems
#define E(x) uint16_t(::ContestCreator::LineItems::x)
QML_ENUM_CLASS(LineItems,
contestTypeOneOfN = E(CONTEST_TYPE_ONE_OF_N),
pluralityTally = E(PLURALITY_TALLY),
contestant3 = E(CONTESTANT3),
contestant4 = E(CONTESTANT4),
contestant5 = E(CONTESTANT5),
contestant6 = E(CONTESTANT6),
contestant7Plus = E(CONTESTANT7_PLUS),
infiniteDurationContest = E(INFINITE_DURATION_CONTEST)
)
#undef E

// Create an enum class for the ContestLimits
#define E(x) uint16_t(::ContestCreator::ContestLimits::x)
QML_ENUM_CLASS(ContestLimits,
nameLength = E(NAME_LENGTH),
descriptionSoftLength = E(DESCRIPTION_SOFT_LENGTH),
descriptionHardLength = E(DESCRIPTION_HARD_LENGTH),
contestantCount = E(CONTESTANT_COUNT),
contestantName = E(CONTESTANT_NAME_LENGTH),
contestantDescriptionSoftLength = E(CONTESTANT_DESCRIPTION_SOFT_LENGTH),
contestantDescriptionHardLength = E(CONTESTANT_DESCRIPTION_HARD_LENGTH),
maxEndDate = E(MAX_END_DATE)
)
#undef E

#endif // CONTESTCREATIONREQUEST_HPP

} // namespace swv

Q_DECLARE_METATYPE(swv::LineItems)
Q_DECLARE_METATYPE(swv::ContestLimits)
6 changes: 3 additions & 3 deletions shared/capnp/contestcreation.capnp
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ interface ContestCreator {
# Maximum permissible length of the contest description
contestantCount @3;
# Maximum number of contestants
contestnatName @4;
contestantNameLength @4;
# Maximum length of a contestant name
contestantDescriptionHardLength @5;
contestantDescriptionSoftLength @5;
# Maximum length of a contestant description before risking a surcharge
contestantDescriptionSoftLength @6;
contestantDescriptionHardLength @6;
# Maximum permissible length of a contestant description
maxEndDate @7;
# Maximum end date of a finite-duration contest. A value of zero allows infinite contest duration; any other
Expand Down

0 comments on commit e657ceb

Please sign in to comment.