summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAotrix <[email protected]>2026-10-10 16:09:48 +0200
committerAotrix <[email protected]>2026-10-10 16:09:48 +0200
commitc777502a1c22b240f66069831e6f0cb1e7e41b64 (patch)
tree6201b076b88ba6c50c4654b6da7abf8b5cda8267
parent326f6cb61773c82182d91142749072aa3ca4c450 (diff)
Use string_view and delete gameViewHEADmaster
-rw-r--r--battleNetApi/include/apiUrl.hpp31
-rw-r--r--battleNetApi/src/apiUrl.cpp94
-rw-r--r--battleNetApi/src/hearthstoneApi.cpp112
-rw-r--r--battleNetApi/tests/src/hearthstoneApiTests.cpp43
-rw-r--r--gameView/.clangd3
-rw-r--r--gameView/CMakeLists.txt8
-rw-r--r--gameView/main.cpp212
7 files changed, 185 insertions, 318 deletions
diff --git a/battleNetApi/include/apiUrl.hpp b/battleNetApi/include/apiUrl.hpp
index 1360102..bc62d42 100644
--- a/battleNetApi/include/apiUrl.hpp
+++ b/battleNetApi/include/apiUrl.hpp
@@ -3,16 +3,18 @@
#include <string>
#include <unordered_map>
-namespace bnet {
+namespace bnet
+{
-class ApiUrl {
+class ApiUrl
+{
public:
- ApiUrl(const std::string &iContentType, const std::string &iRegion);
+ ApiUrl(const std::string& iContentType,
+ const std::string& iRegion);
- void setRegion(const std::string &iRegion);
- void setLocale(const std::string &iLocale);
- void setGameMode(const std::string &iGameMode);
- void setPage(const std::string &iPage);
+ void setRegion(const std::string& iRegion);
+ void setLocale(const std::string& iLocale);
+ void setGameMode(const std::string& iGameMode);
void setPage(uint64_t iPage);
void setPageSize(uint64_t iPageSize);
@@ -25,15 +27,16 @@ class ApiUrl {
std::string text() const;
private:
- static constexpr std::string kLOCALE_KEY{"locale"};
- static constexpr std::string kGAME_MODE_KEY{"gameMode"};
- static constexpr std::string kPAGE_KEY{"page"};
- static constexpr std::string kPAGE_SIZE_KEY{"pageSize"};
+ std::string_view kLOCALE_KEY{"locale"};
+ std::string_view kGAME_MODE_KEY{"gameMode"};
+ std::string_view kPAGE_KEY{"page"};
+ std::string_view kPAGE_SIZE_KEY{"pageSize"};
std::string mContentType;
std::string mRegion;
- std::unordered_map<std::string, std::string> aParameters;
+ std::unordered_map<std::string_view, std::string> aParameters;
- void setValue(const std::string &iKey, const std::string &iValue);
- bool hasValue(const std::string &iKey) const;
+ void setValue(std::string_view iKey,
+ const std::string& iValue);
+ bool hasValue(std::string_view iKey) const;
};
}; // namespace bnet
diff --git a/battleNetApi/src/apiUrl.cpp b/battleNetApi/src/apiUrl.cpp
index 8775943..bc54330 100644
--- a/battleNetApi/src/apiUrl.cpp
+++ b/battleNetApi/src/apiUrl.cpp
@@ -2,54 +2,96 @@
#include <cstdint>
#include <format>
-namespace bnet {
+namespace bnet
+{
-ApiUrl::ApiUrl(const std::string &iContentType, const std::string &iRegion)
- : mContentType{iContentType}, mRegion{iRegion} {}
-void ApiUrl::setRegion(const std::string &iRegion) {
- if (!iRegion.empty()) {
+ApiUrl::ApiUrl(const std::string& iContentType,
+ const std::string& iRegion)
+ : mContentType{iContentType},
+ mRegion{iRegion}
+{
+}
+
+void ApiUrl::setRegion(const std::string& iRegion)
+{
+ if (!iRegion.empty())
+ {
mRegion = iRegion; //!< The region of the data to receive, e.g. "eu"
}
}
-void ApiUrl::setLocale(const std::string &iLocale) {
- if (!iLocale.empty()) {
+
+void ApiUrl::setLocale(const std::string& iLocale)
+{
+ if (!iLocale.empty())
+ {
//!< The locale to reflect in localized data, e.g. "fr_FR" or "ko_KR"
//!< If you do !supply a value, all translations are returned.
setValue(kLOCALE_KEY, iLocale);
}
}
-void ApiUrl::setGameMode(const std::string &iGameMode) {
- if (!iGameMode.empty()) {
+
+void ApiUrl::setGameMode(const std::string& iGameMode)
+{
+ if (!iGameMode.empty())
+ {
//!< A recognized game mode, e.g. "constructed"
//!< @see
//!< https://community.developer.battle.net/documentation/hearthstone/guides/game-modes
setValue(kGAME_MODE_KEY, iGameMode);
}
}
-void ApiUrl::setPage(const std::string &iPage) {
- if (!iPage.empty()) {
- //!< The page to retrieve, e.g. "9"
- setValue(kPAGE_KEY, iPage);
- }
+
+void ApiUrl::setPage(uint64_t iPage)
+{
+ //!< The page to retrieve, e.g. "9"
+ setValue(kPAGE_KEY, std::to_string(iPage));
}
-void ApiUrl::setPage(uint64_t iPage) { setPage(std::to_string(iPage)); }
-void ApiUrl::setPageSize(uint64_t iPageSize) {
+
+void ApiUrl::setPageSize(uint64_t iPageSize)
+{
setValue(kPAGE_SIZE_KEY, std::to_string(iPageSize));
}
-bool ApiUrl::hasLocale() const { return hasValue(kLOCALE_KEY); }
-bool ApiUrl::hasGameMode() const { return hasValue(kGAME_MODE_KEY); }
-bool ApiUrl::hasPage() const { return hasValue(kPAGE_KEY); }
-bool ApiUrl::hasPageSize() const { return hasValue(kPAGE_SIZE_KEY); }
-void ApiUrl::setValue(const std::string &iKey, const std::string &iValue) {
- aParameters[iKey] = iValue;
+
+bool ApiUrl::hasLocale() const
+{
+ return hasValue(kLOCALE_KEY);
+}
+
+bool ApiUrl::hasGameMode() const
+{
+ return hasValue(kGAME_MODE_KEY);
}
-bool ApiUrl::hasValue(const std::string &iKey) const { return aParameters.count(iKey) > 0; }
-std::string ApiUrl::text() const {
+
+bool ApiUrl::hasPage() const
+{
+ return hasValue(kPAGE_KEY);
+}
+
+bool ApiUrl::hasPageSize() const
+{
+ return hasValue(kPAGE_SIZE_KEY);
+}
+
+void ApiUrl::setValue(std::string_view iKey,
+ const std::string& iValue)
+{
+ aParameters[iKey] = std::move(iValue);
+}
+
+bool ApiUrl::hasValue(std::string_view iKey) const
+{
+ return aParameters.count(iKey) > 0;
+}
+
+std::string ApiUrl::text() const
+{
std::string aFormattedParameters;
- for (const auto &[key, value] : aParameters) {
+ for (const auto& [key, value] : aParameters)
+ {
aFormattedParameters += std::format("&{}={}", key, value);
}
- if (!aFormattedParameters.empty()) {
+ if (!aFormattedParameters.empty())
+ {
aFormattedParameters[0] = '?';
}
aFormattedParameters = std::format("https://{}.api.blizzard.com/hearthstone/{}/{}", mRegion,
diff --git a/battleNetApi/src/hearthstoneApi.cpp b/battleNetApi/src/hearthstoneApi.cpp
index 4dd8fac..c78c4b9 100644
--- a/battleNetApi/src/hearthstoneApi.cpp
+++ b/battleNetApi/src/hearthstoneApi.cpp
@@ -7,33 +7,44 @@
#include <iostream>
#include <stdexcept>
-namespace bnet {
-std::string HearthstoneApi::authorizationHeader(const std::string &iTokenText) {
+namespace bnet
+{
+std::string HearthstoneApi::authorizationHeader(const std::string& iTokenText)
+{
return std::format("Authorization: Bearer {}", iTokenText);
}
-size_t HearthstoneApi::curlCallbackStringOutput(void *iContent, size_t iSize, size_t iNmemb,
- std::string *iS) {
+size_t HearthstoneApi::curlCallbackStringOutput(void* iContent,
+ size_t iSize,
+ size_t iNmemb,
+ std::string* iS)
+{
const size_t kNewLength = iSize * iNmemb;
- try {
- iS->append(static_cast<char *>(iContent), kNewLength);
- } catch (std::bad_alloc &e) {
+ try
+ {
+ iS->append(static_cast<char*>(iContent), kNewLength);
+ }
+ catch (std::bad_alloc& e)
+ {
// handle memory problem
return 0;
}
return kNewLength;
}
-json HearthstoneApi::makeApiRequest(const ApiUrl &iApiUrl, const std::string &iTokenText) {
- CURL *curl = curl_easy_init();
+json HearthstoneApi::makeApiRequest(const ApiUrl& iApiUrl,
+ const std::string& iTokenText)
+{
+ CURL* curl = curl_easy_init();
std::string aRequestOutput;
- if (curl) {
+ if (curl)
+ {
curl_easy_setopt(curl, CURLOPT_URL, iApiUrl.text().c_str());
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlCallbackStringOutput);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &aRequestOutput);
- struct curl_slist *chunk = nullptr;
+ struct curl_slist* chunk = nullptr;
chunk = curl_slist_append(chunk, authorizationHeader(iTokenText).c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
curl_easy_perform(curl);
@@ -43,27 +54,31 @@ json HearthstoneApi::makeApiRequest(const ApiUrl &iApiUrl, const std::string &iT
return aOutputJson;
}
-std::vector<json> HearthstoneApi::makeApiRequest(const std::vector<ApiUrl> &iApiUrls,
- const std::string &iTokenText) {
+std::vector<json> HearthstoneApi::makeApiRequest(const std::vector<ApiUrl>& iApiUrls,
+ const std::string& iTokenText)
+{
std::vector<std::string> aApiUrls(iApiUrls.size());
std::ranges::transform(iApiUrls, aApiUrls.begin(),
- [](const ApiUrl &iApiUrl) { return iApiUrl.text(); });
+ [](const ApiUrl& iApiUrl) { return iApiUrl.text(); });
return makeApiRequest(aApiUrls, iTokenText);
}
-std::vector<json> HearthstoneApi::makeApiRequest(const std::vector<std::string> &iUrls,
- const std::string &iTokenText) {
+std::vector<json> HearthstoneApi::makeApiRequest(const std::vector<std::string>& iUrls,
+ const std::string& iTokenText)
+{
std::vector<std::string> aResponses(iUrls.size());
- CURLM *curlm = curl_multi_init();
- if (curlm) {
- for (std::vector<std::string>::size_type i{0}; i < aResponses.size(); ++i) {
- CURL *curl = curl_easy_init();
+ CURLM* curlm = curl_multi_init();
+ if (curlm)
+ {
+ for (std::vector<std::string>::size_type i{0}; i < aResponses.size(); ++i)
+ {
+ CURL* curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, iUrls[i].c_str());
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlCallbackStringOutput);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &(aResponses[i]));
- struct curl_slist *chunk = nullptr;
+ struct curl_slist* chunk = nullptr;
chunk = curl_slist_append(chunk, authorizationHeader(iTokenText).c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
curl_multi_add_handle(curlm, curl);
@@ -71,32 +86,39 @@ std::vector<json> HearthstoneApi::makeApiRequest(const std::vector<std::string>
const int kMaxWaitMs{5000};
int aStillRunning{0};
curl_multi_perform(curlm, &aStillRunning);
- do {
+ do
+ {
int numfds = 0;
- int res = curl_multi_wait(curlm, NULL, 0, kMaxWaitMs, &numfds);
- if (res != CURLM_OK) {
+ int res = curl_multi_wait(curlm, NULL, 0, kMaxWaitMs, &numfds);
+ if (res != CURLM_OK)
+ {
std::cerr << std::format("error: curl_multi_wait() returned {}\n", res);
}
curl_multi_perform(curlm, &aStillRunning);
} while (aStillRunning);
// clean-up
- CURLMsg *aMessage{nullptr};
- CURL *aHandle{nullptr};
+ CURLMsg* aMessage{nullptr};
+ CURL* aHandle{nullptr};
int aMessagesLeft{0};
CURLcode aReturnCode;
- while ((aMessage = curl_multi_info_read(curlm, &aMessagesLeft))) {
- if (aMessage->msg == CURLMSG_DONE) {
- aHandle = aMessage->easy_handle;
+ while ((aMessage = curl_multi_info_read(curlm, &aMessagesLeft)))
+ {
+ if (aMessage->msg == CURLMSG_DONE)
+ {
+ aHandle = aMessage->easy_handle;
aReturnCode = aMessage->data.result;
- if (aReturnCode != CURLE_OK) {
+ if (aReturnCode != CURLE_OK)
+ {
fprintf(stderr, "CURL error code: %d\n", aMessage->data.result);
continue;
}
curl_multi_remove_handle(curlm, aHandle);
curl_easy_cleanup(aHandle);
aHandle = nullptr;
- } else {
+ }
+ else
+ {
std::cerr << std::format("error: after curl_multi_info_read(), CURLMsg={}\n",
static_cast<int>(aMessage->msg));
}
@@ -104,32 +126,40 @@ std::vector<json> HearthstoneApi::makeApiRequest(const std::vector<std::string>
curl_multi_cleanup(curlm);
}
std::vector<json> aJsonOutputs;
- for (std::string aResponse : aResponses) {
+ for (std::string aResponse : aResponses)
+ {
aJsonOutputs.push_back(json::parse(aResponse));
}
return aJsonOutputs;
}
-json HearthstoneApi::downloadCards(ApiUrl iApiUrl, const Token &iToken) {
- if (!iToken.text().has_value() || iToken.text().value().empty()) {
+json HearthstoneApi::downloadCards(ApiUrl iApiUrl,
+ const Token& iToken)
+{
+ if (!iToken.text().has_value() || iToken.text().value().empty())
+ {
throw std::invalid_argument("Token text does not exist or is empty");
}
- if (iApiUrl.hasPage()) {
+ if (iApiUrl.hasPage())
+ {
return makeApiRequest(iApiUrl, *iToken.text());
}
- iApiUrl.setPage("1");
+ iApiUrl.setPage(1);
std::vector<std::string> aRequests;
json aFirstPageJson = makeApiRequest(iApiUrl, *iToken.text());
int kNumberOfPages{aFirstPageJson["pageCount"]};
- for (int i{1}; i < kNumberOfPages; ++i) {
- iApiUrl.setPage(std::to_string(i + 1));
+ for (int i{1}; i < kNumberOfPages; ++i)
+ {
+ iApiUrl.setPage(i + 1);
aRequests.push_back(iApiUrl.text());
}
- json::reference &aOutputJson = aFirstPageJson["cards"];
+ json::reference& aOutputJson = aFirstPageJson["cards"];
std::vector<json> kApiResponses = makeApiRequest(aRequests, *iToken.text());
- for (json &aPage : kApiResponses) {
- for (json::reference &aCard : aPage["cards"]) {
+ for (json& aPage : kApiResponses)
+ {
+ for (json::reference& aCard : aPage["cards"])
+ {
aOutputJson.push_back(std::move(aCard));
}
}
diff --git a/battleNetApi/tests/src/hearthstoneApiTests.cpp b/battleNetApi/tests/src/hearthstoneApiTests.cpp
index 192f38c..c3dfc59 100644
--- a/battleNetApi/tests/src/hearthstoneApiTests.cpp
+++ b/battleNetApi/tests/src/hearthstoneApiTests.cpp
@@ -15,7 +15,9 @@
#include <string>
#include <tokenBuilder.hpp>
-TEST_CASE("Retrieve number of cards", "[countCards]") {
+TEST_CASE("Retrieve number of cards",
+ "[countCards]")
+{
const std::string aContentType{"cards"};
const std::string aRegion{"eu"};
bnet::ApiUrl aApiUrl(aContentType, aRegion);
@@ -37,7 +39,9 @@ TEST_CASE("Retrieve number of cards", "[countCards]") {
REQUIRE(aNumberOfCards >= aExpectedNumberOfCards);
}
-TEST_CASE("Retrieve page once", "[retrievePageOnce]") {
+TEST_CASE("Retrieve page once",
+ "[retrievePageOnce]")
+{
const std::string aContentType{"cards"};
const std::string aRegion{"eu"};
bnet::ApiUrl aApiUrl(aContentType, aRegion);
@@ -55,14 +59,16 @@ TEST_CASE("Retrieve page once", "[retrievePageOnce]") {
REQUIRE(aApiResponse["cards"].size() == 16);
}
-TEST_CASE("Retrieve pages 10 times without parallelization", "[retrievePagesNoParallel]") {
+TEST_CASE("Retrieve pages 10 times without parallelization",
+ "[retrievePagesNoParallel]")
+{
const std::string aContentType{"cards"};
const std::string aRegion{"eu"};
bnet::ApiUrl aApiUrl(aContentType, aRegion);
aApiUrl.setLocale("fr_FR");
aApiUrl.setGameMode("constructed");
aApiUrl.setPageSize(16);
- aApiUrl.setPage("1");
+ aApiUrl.setPage(1);
bnet::HearthstoneTokenBuilder aHearthstoneTokenBuilder("resources/credentials.json");
const std::string aTokenText{aHearthstoneTokenBuilder.allowBattleNetApiCall()
.allowUpdateSourceFile()
@@ -76,23 +82,26 @@ TEST_CASE("Retrieve pages 10 times without parallelization", "[retrievePagesNoPa
const int aNumberOfRetrievals{10};
const int aNumberOfPages{aApiResponse["pageCount"]};
REQUIRE(aNumberOfPages >= aNumberOfRetrievals);
- for (int i{1}; i < aNumberOfRetrievals; ++i) {
+ for (int i{1}; i < aNumberOfRetrievals; ++i)
+ {
REQUIRE(aApiResponse["cards"].size() == 16);
- aApiUrl.setPage(std::to_string(i + 1));
+ aApiUrl.setPage(i + 1);
aApiResponse = bnet::HearthstoneApi::makeApiRequest(aApiUrl, aTokenText);
}
REQUIRE(aApiResponse["cards"].size() >= 1);
REQUIRE(aApiResponse["cards"].size() <= 16);
}
-TEST_CASE("Retrieve pages 10 times with parallelization", "[retrievePagesParallel]") {
+TEST_CASE("Retrieve pages 10 times with parallelization",
+ "[retrievePagesParallel]")
+{
const std::string aContentType{"cards"};
const std::string aRegion{"eu"};
bnet::ApiUrl aApiUrl(aContentType, aRegion);
aApiUrl.setLocale("fr_FR");
aApiUrl.setGameMode("constructed");
aApiUrl.setPageSize(16);
- aApiUrl.setPage("1");
+ aApiUrl.setPage(1);
std::vector<std::string> aRequests;
bnet::HearthstoneTokenBuilder aHearthstoneTokenBuilder("resources/credentials.json");
const std::string aTokenText{aHearthstoneTokenBuilder.allowBattleNetApiCall()
@@ -106,27 +115,32 @@ TEST_CASE("Retrieve pages 10 times with parallelization", "[retrievePagesParalle
const int aNumberOfPages{aFirstPageJson["pageCount"]};
const int aNumberOfRetrievals{10};
REQUIRE(aNumberOfPages >= aNumberOfRetrievals);
- for (int i{1}; i < aNumberOfRetrievals; ++i) {
- aApiUrl.setPage(std::to_string(i + 1));
+ for (int i{1}; i < aNumberOfRetrievals; ++i)
+ {
+ aApiUrl.setPage(i + 1);
aRequests.emplace_back(aApiUrl.text());
}
std::vector<nlohmann::json> aApiResponses =
bnet::HearthstoneApi::makeApiRequest(aRequests, aTokenText);
aApiResponses.emplace(aApiResponses.begin(), aFirstPageJson);
REQUIRE(aApiResponses.size() == aNumberOfRetrievals);
- for (const nlohmann::json &aApiResponse : aApiResponses) {
+ for (const nlohmann::json& aApiResponse : aApiResponses)
+ {
REQUIRE(aApiResponse["cards"].size() == 16);
}
}
-TEST_CASE("Merge two card lists from two page requests", "[Merge pages]") {
+TEST_CASE("Merge two card lists from two page requests",
+ "[Merge pages]")
+{
std::ifstream aFirstPageFile{"resources/page_merge_00.json"};
nlohmann::json aFirstPageJson = nlohmann::json::parse(aFirstPageFile);
std::ifstream aSecondPageFile{"resources/page_merge_01.json"};
const nlohmann::json aSecondPageJson = nlohmann::json::parse(aSecondPageFile);
nlohmann::json::reference aMergedCards = aFirstPageJson["cards"];
- for (auto &card : aSecondPageJson["cards"]) {
+ for (auto& card : aSecondPageJson["cards"])
+ {
aMergedCards.emplace_back(std::move(card));
}
REQUIRE(aMergedCards.size() == 32);
@@ -135,7 +149,8 @@ TEST_CASE("Merge two card lists from two page requests", "[Merge pages]") {
}
TEST_CASE("Retrieve a single page with HearthstoneApi::downloadCards",
- "[retrievePageOnceHearthstoneApi]") {
+ "[retrievePageOnceHearthstoneApi]")
+{
const std::string aContentType{"cards"};
const std::string aRegion{"eu"};
bnet::ApiUrl aApiUrl(aContentType, aRegion);
diff --git a/gameView/.clangd b/gameView/.clangd
deleted file mode 100644
index dddeaef..0000000
--- a/gameView/.clangd
+++ /dev/null
@@ -1,3 +0,0 @@
-CompileFlags:
- Remove:
- - -mno-direct-extern-access
diff --git a/gameView/CMakeLists.txt b/gameView/CMakeLists.txt
deleted file mode 100644
index 1e923f1..0000000
--- a/gameView/CMakeLists.txt
+++ /dev/null
@@ -1,8 +0,0 @@
-cmake_minimum_required(VERSION 3.20)
-
-project(GameView LANGUAGES CXX)
-
-set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
-add_executable(gameView main.cpp)
-find_package(Qt6 COMPONENTS Widgets REQUIRED)
-target_link_libraries(gameView PRIVATE Qt6::Widgets)
diff --git a/gameView/main.cpp b/gameView/main.cpp
deleted file mode 100644
index 2478735..0000000
--- a/gameView/main.cpp
+++ /dev/null
@@ -1,212 +0,0 @@
-#include <QVBoxLayout>
-#include <QtWidgets>
-#include <format>
-#include <fstream>
-#include <memory>
-#include <nlohmann/json.hpp>
-#include <qapplication.h>
-#include <qboxlayout.h>
-#include <qgridlayout.h>
-#include <qlistwidget.h>
-#include <qmenubar.h>
-#include <qnamespace.h>
-#include <qobject.h>
-#include <qpushbutton.h>
-#include <qtmetamacros.h>
-#include <qtoolbar.h>
-#include <qwidget.h>
-
-class ControlInterface : public QObject
-{
- public:
- ControlInterface& registerListOfCardsWidget(std::shared_ptr<QListWidget>& iListOfCards)
- {
- _listOfCardsWidget = iListOfCards;
- return *this;
- }
-
- ControlInterface& registerMainWindow(std::shared_ptr<QWidget>& iMainWindow)
- {
- _mainWindow = iMainWindow;
- return *this;
- }
-
- ControlInterface& registerMainMenuBar(std::shared_ptr<QMenuBar>& iMenuBar)
- {
- _mainMenuBar = iMenuBar;
- return *this;
- }
-
- ControlInterface& registerRightView(std::shared_ptr<QGraphicsView>& iRightView)
- {
- _rightView = iRightView;
- return *this;
- }
-
- std::shared_ptr<QWidget> listOfCardsWidget()
- {
- return _listOfCardsWidget;
- }
-
- std::shared_ptr<QWidget> mainWindow()
- {
- return _mainWindow;
- }
-
- std::shared_ptr<QMenuBar> mainMenuBar()
- {
- return _mainMenuBar;
- }
-
- std::shared_ptr<QGraphicsView> rightView()
- {
- return _rightView;
- }
-
- ControlInterface& configure()
- {
- configureListOfCardsWidget().configureMainMenuBar().configureRightGraphicsView();
- return *this;
- }
-
- public slots:
-
- void display_card(QListWidgetItem* iItem)
- {
- _popUpWindowSelectCard = std::make_shared<QWidget>(_mainWindow.get());
- _popUpWindowSelectCard->resize(400, 500);
- _popUpWindowSelectCard->setWindowFlag(Qt::WindowType::Dialog);
- _popUpWindowSelectCard->show();
- }
-
- private:
- std::shared_ptr<QListWidget> _listOfCardsWidget;
- std::shared_ptr<QWidget> _mainWindow;
- std::shared_ptr<QMenuBar> _mainMenuBar;
- std::shared_ptr<QGraphicsView> _rightView;
- std::shared_ptr<QGraphicsScene> _rightScene;
-
- std::vector<nlohmann::basic_json<>> _cards;
- std::shared_ptr<QWidget> _popUpWindowSelectCard;
-
- ControlInterface& fillListOfCards(const std::string& iCardsJsonPath)
- {
- std::ifstream aCardsJsonFile{iCardsJsonPath};
- uint64_t aCardIndex{0};
- _cards.clear();
- _listOfCardsWidget->clear();
- for (auto& aCard : nlohmann::json::parse(aCardsJsonFile))
- {
- _cards.push_back(aCard);
- _listOfCardsWidget->insertItem(aCardIndex++,
- std::format("{} ({})", aCard["name"].get<std::string>(),
- aCard["id"].get<uint64_t>())
- .c_str());
- }
- QObject::connect(_listOfCardsWidget.get(), &QListWidget::itemDoubleClicked, this,
- &ControlInterface::display_card);
- _listOfCardsWidget->setVisible(true);
- _listOfCardsWidget->setFixedWidth(_listOfCardsWidget->sizeHintForColumn(0) +
- 2 * _listOfCardsWidget->frameWidth() + 20);
- _mainWindow->resize(1500, 1000);
- return *this;
- }
-
- ControlInterface& configureListOfCardsWidget()
- {
- _listOfCardsWidget->hide();
- return *this;
- }
-
- ControlInterface& configureMainMenuBar()
- {
- QMenu* aOpenMenu = _mainMenuBar->addMenu("File");
- aOpenMenu->addAction("Open JSON file",
- [this]
- {
- QFileDialog aFileDialog;
- aFileDialog.setWindowTitle("Choose JSON file");
- QDir aCurrentDir{QDir::currentPath()};
- aFileDialog.setDirectory(aCurrentDir);
- aFileDialog.setFileMode(QFileDialog::ExistingFile);
- aFileDialog.setOption(QFileDialog::ShowDirsOnly, false);
- aFileDialog.setNameFilter("*.json");
- aFileDialog.exec();
- const QStringList kFilePaths{aFileDialog.selectedFiles()};
- if (kFilePaths.size() > 0)
- {
- fillListOfCards(kFilePaths[0].toStdString());
- _rightView->setVisible(true);
- }
- });
- aOpenMenu->addAction("Quit", [this] { _mainWindow->close(); });
- return *this;
- }
-
- ControlInterface& configureRightGraphicsView()
- {
- _rightScene = std::make_shared<QGraphicsScene>();
- QGraphicsRectItem* rect = _rightScene->addRect(QRectF(0, 0, 100, 100));
- QGraphicsItem* item = _rightScene->itemAt(50, 50, QTransform());
- _rightView->setScene(_rightScene.get());
- _rightView->hide();
- return *this;
- }
-};
-
-// int main(int argc,
-// char* argv[])
-//{
-// QApplication aApplication(argc, argv);
-// auto aWindow = std::make_shared<QWidget>();
-// aWindow->resize(400, 50);
-// // Set Dialog flag for i3 users
-// aWindow->setWindowFlag(Qt::WindowType::Dialog);
-//
-// QGridLayout aLayout(aWindow.get());
-// QHBoxLayout aHLayout(aLayout.widget());
-// auto aMenuBar = std::make_shared<QMenuBar>();
-// aLayout.addWidget(aMenuBar.get(), 0, 0);
-// auto aListOfCards = std::make_shared<QListWidget>();
-// aLayout.addLayout(&aHLayout, 1, 0);
-// aHLayout.addWidget(aListOfCards.get());
-// auto aRightView = std::make_shared<QGraphicsView>();
-// aHLayout.addWidget(aRightView.get());
-//
-// ControlInterface aControlInterface;
-// aControlInterface.registerListOfCardsWidget(aListOfCards)
-// .registerMainWindow(aWindow)
-// .registerMainMenuBar(aMenuBar)
-// .registerRightView(aRightView)
-// .configure();
-// aWindow->show();
-// aWindow->setWindowTitle("Hearthstone simulator");
-// return aApplication.exec();
-// }
-
-class ControlCentre : public QObject
-{
- Q_OBJECT
- public:
- /* view */
- public slots:
- void setView(/* View */);
-
- signals:
- void viewSelected(/* selected view */);
-
- private:
-};
-
-auto main(int argc,
- char* argv[]) -> int
-{
- QApplication aApplication(argc, argv);
- auto aWindow = std::make_shared<QWidget>();
- aWindow->resize(1400, 800);
- // Set Dialog flag for i3 users
- aWindow->setWindowFlag(Qt::WindowType::Dialog);
- aWindow->show();
- aWindow->setWindowTitle("Hearthstone simulator");
- return aApplication.exec();
-}