-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
165 lines (131 loc) · 3.89 KB
/
mainwindow.cpp
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* homm3-wallpaper, live HOMM3 wallpaper
* Copyright (C) 2024 i.Dark_Templar <[email protected]>
*
* Subject to terms and condition provided in LICENSE.txt
*
*/
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtCore/QTimer>
#include <QtQml/QQmlEngine>
#include <QtQml/QQmlContext>
#include <QtWidgets/QFileDialog>
#include <QtWidgets/QMessageBox>
#include "homm3_image_provider.h"
#include "homm3map.h"
#include "homm3singleton.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, m_settings(QStringLiteral("homm3map-viewer"))
{
ui->setupUi(this);
auto data_archives = m_settings.value(QStringLiteral("Archives"), QStringList()).toStringList();
Homm3MapSingleton::getInstance()->setDataArchives(data_archives);
m_settings_window.setWindowModality(Qt::ApplicationModal);
ui->quickWidget->engine()->addImageProvider(QStringLiteral("homm3"), new Homm3ImageProvider);
ui->quickWidget->setSource(QStringLiteral("qrc:/main.qml"));
auto *map = getMapObject();
if (map != nullptr)
{
QObject::connect(map, &Homm3Map::loadingFinished, this, &MainWindow::setLastMap);
}
{
auto map_name = m_settings.value(QStringLiteral("LastMap"), QString()).toString();
if (!map_name.isEmpty())
{
setMap(map_name, m_settings.value(QStringLiteral("LastLevel"), 0).toInt());
}
}
QObject::connect(ui->actionOpen, &QAction::triggered, this, &MainWindow::openMapDialog);
QObject::connect(ui->actionSettings, &QAction::triggered, this, &MainWindow::openSettingsWindow);
QObject::connect(ui->actionQuit, &QAction::triggered, this, &MainWindow::close);
QObject::connect(ui->actionToggle_level, &QAction::triggered, this, &MainWindow::toggleMapLevel);
QObject::connect(&m_settings_window, &SettingsWindow::accepted, this, &MainWindow::settingsUpdated);
if (data_archives.isEmpty())
{
QTimer::singleShot(0,[](){
QMessageBox::warning(
nullptr,
QObject::tr("Configuration warning"),
QObject::tr("No HOMM3 data archives are set.\nGo to menu 'File' -> 'Settings' and add required data archives.\nAfter that a map may be opened via 'File' -> 'Open' menu."));
});
}
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::openMapDialog()
{
QString map_name = QFileDialog::getOpenFileName(this, QObject::tr("Open map file"), QString(), QObject::tr("Map file (*.h3m)"));
if (map_name.isEmpty())
{
return;
}
setMap(map_name, 0);
}
void MainWindow::openSettingsWindow()
{
if (m_settings_window.isVisible())
{
return;
}
QStringList values = m_settings.value(QStringLiteral("Archives"), QStringList()).toStringList();
m_settings_window.setItems(values);
m_settings_window.show();
}
Homm3Map* MainWindow::getMapObject() const
{
auto root_object = ui->quickWidget->rootObject();
if (root_object == nullptr)
{
return nullptr;
}
auto map = root_object->findChild<Homm3Map*>("map");
return map;
}
void MainWindow::setMap(const QString &name, int level)
{
auto map = getMapObject();
if (map == nullptr)
{
return;
}
map->loadMap(name, level);
}
void MainWindow::toggleMapLevel()
{
auto map = getMapObject();
if (map == nullptr)
{
return;
}
map->toggleLevel();
}
void MainWindow::settingsUpdated()
{
QStringList values = m_settings_window.getItems();
m_settings.setValue(QStringLiteral("Archives"), values);
// reload main view
Homm3MapSingleton::getInstance()->setDataArchives(values);
auto map = getMapObject();
QString map_name = map->currentMapName();
int level = map->mapLevel();
ui->quickWidget->setSource(QStringLiteral("qrc:/main.qml"));
map = getMapObject();
if (map != nullptr)
{
QObject::connect(map, &Homm3Map::loadingFinished, this, &MainWindow::setLastMap);
if (!map_name.isEmpty())
{
map->loadMap(map_name, level);
}
}
}
void MainWindow::setLastMap(QString name, int level)
{
m_settings.setValue(QStringLiteral("LastMap"), name);
m_settings.setValue(QStringLiteral("LastLevel"), level);
}