summaryrefslogtreecommitdiff
path: root/gameView/main.cpp
blob: 0a212d4aae991412c6fede8a5af1863eb7df10d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <QVBoxLayout>
#include <QtWidgets>
#include <format>
#include <fstream>
#include <iostream>
#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 *) { std::cout << "coucou\n"; }

  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;

    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();
}