No Description

mainwindow.cpp 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include "project.h"
  4. #include <QDebug>
  5. #include <QFileDialog>
  6. #include <QStandardItemModel>
  7. #include <QShortcut>
  8. #include <QSettings>
  9. MainWindow::MainWindow(QWidget *parent) :
  10. QMainWindow(parent),
  11. ui(new Ui::MainWindow)
  12. {
  13. QCoreApplication::setOrganizationName("pret");
  14. QCoreApplication::setApplicationName("pretmap");
  15. editor = new Editor;
  16. new QShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_Z), this, SLOT(redo()));
  17. ui->setupUi(this);
  18. QSettings settings;
  19. QString key = "recent_projects";
  20. if (settings.contains(key)) {
  21. QString default_dir = settings.value(key).toStringList().last();
  22. openProject(default_dir);
  23. }
  24. }
  25. MainWindow::~MainWindow()
  26. {
  27. delete ui;
  28. }
  29. void MainWindow::openProject(QString dir) {
  30. bool already_open = (editor->project != NULL) && (editor->project->root == dir);
  31. if (!already_open) {
  32. editor->project = new Project;
  33. editor->project->root = dir;
  34. populateMapList();
  35. setMap(getDefaultMap());
  36. } else {
  37. populateMapList();
  38. }
  39. }
  40. QString MainWindow::getDefaultMap() {
  41. QSettings settings;
  42. QString key = "project:" + editor->project->root;
  43. if (settings.contains(key)) {
  44. QMap<QString, QVariant> qmap = settings.value(key).toMap();
  45. if (qmap.contains("recent_map")) {
  46. QString map_name = qmap.value("recent_map").toString();
  47. return map_name;
  48. }
  49. }
  50. // Failing that, just get the first map in the list.
  51. for (int i = 0; i < editor->project->groupedMapNames->length(); i++) {
  52. QStringList *list = editor->project->groupedMapNames->value(i);
  53. if (list->length()) {
  54. return list->value(0);
  55. }
  56. }
  57. return NULL;
  58. }
  59. QString MainWindow::getExistingDirectory(QString dir) {
  60. return QFileDialog::getExistingDirectory(this, "Open Directory", dir, QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
  61. }
  62. void MainWindow::on_action_Open_Project_triggered()
  63. {
  64. QSettings settings;
  65. QString key = "recent_projects";
  66. QString recent = ".";
  67. if (settings.contains(key)) {
  68. recent = settings.value(key).toStringList().last();
  69. }
  70. QString dir = getExistingDirectory(recent);
  71. if (!dir.isEmpty()) {
  72. QStringList recents;
  73. if (settings.contains(key)) {
  74. recents = settings.value(key).toStringList();
  75. }
  76. recents.removeAll(dir);
  77. recents.append(dir);
  78. settings.setValue(key, recents);
  79. openProject(dir);
  80. }
  81. }
  82. void MainWindow::setMap(QString map_name) {
  83. if (map_name.isNull()) {
  84. return;
  85. }
  86. editor->setMap(map_name);
  87. if (ui->tabWidget->currentIndex() == 1) {
  88. editor->setEditingObjects();
  89. } else {
  90. if (ui->tabWidget_2->currentIndex() == 1) {
  91. editor->setEditingCollision();
  92. } else {
  93. editor->setEditingMap();
  94. }
  95. }
  96. ui->graphicsView_Map->setScene(editor->scene);
  97. ui->graphicsView_Map->setSceneRect(editor->scene->sceneRect());
  98. ui->graphicsView_Map->setFixedSize(editor->scene->width() + 2, editor->scene->height() + 2);
  99. ui->graphicsView_Objects_Map->setScene(editor->scene);
  100. ui->graphicsView_Objects_Map->setSceneRect(editor->scene->sceneRect());
  101. ui->graphicsView_Objects_Map->setFixedSize(editor->scene->width() + 2, editor->scene->height() + 2);
  102. ui->graphicsView_Metatiles->setScene(editor->scene_metatiles);
  103. //ui->graphicsView_Metatiles->setSceneRect(editor->scene_metatiles->sceneRect());
  104. ui->graphicsView_Metatiles->setFixedSize(editor->metatiles_item->pixmap().width() + 2, editor->metatiles_item->pixmap().height() + 2);
  105. ui->graphicsView_Collision->setScene(editor->scene_collision_metatiles);
  106. //ui->graphicsView_Collision->setSceneRect(editor->scene_collision_metatiles->sceneRect());
  107. ui->graphicsView_Collision->setFixedSize(editor->collision_metatiles_item->pixmap().width() + 2, editor->collision_metatiles_item->pixmap().height() + 2);
  108. ui->graphicsView_Elevation->setScene(editor->scene_elevation_metatiles);
  109. //ui->graphicsView_Elevation->setSceneRect(editor->scene_elevation_metatiles->sceneRect());
  110. ui->graphicsView_Elevation->setFixedSize(editor->elevation_metatiles_item->pixmap().width() + 2, editor->elevation_metatiles_item->pixmap().height() + 2);
  111. displayMapProperties();
  112. setRecentMap(map_name);
  113. updateMapList();
  114. }
  115. void MainWindow::setRecentMap(QString map_name) {
  116. QSettings settings;
  117. QString key = "project:" + editor->project->root;
  118. QMap<QString, QVariant> qmap;
  119. if (settings.contains(key)) {
  120. qmap = settings.value(key).toMap();
  121. }
  122. qmap.insert("recent_map", map_name);
  123. settings.setValue(key, qmap);
  124. }
  125. void MainWindow::displayMapProperties() {
  126. Map *map = editor->map;
  127. Project *project = editor->project;
  128. QStringList songs = project->getSongNames();
  129. ui->comboBox_Song->clear();
  130. ui->comboBox_Song->addItems(songs);
  131. QString song = map->song;
  132. if (!songs.contains(song)) {
  133. song = project->getSongName(song.toInt());
  134. }
  135. ui->comboBox_Song->setCurrentText(song);
  136. ui->comboBox_Location->clear();
  137. ui->comboBox_Location->addItems(project->getLocations());
  138. ui->comboBox_Location->setCurrentText(map->location);
  139. ui->comboBox_Visibility->clear();
  140. ui->comboBox_Visibility->addItems(project->getVisibilities());
  141. ui->comboBox_Visibility->setCurrentText(map->visibility);
  142. ui->comboBox_Weather->clear();
  143. ui->comboBox_Weather->addItems(project->getWeathers());
  144. ui->comboBox_Weather->setCurrentText(map->weather);
  145. ui->comboBox_Type->clear();
  146. ui->comboBox_Type->addItems(project->getMapTypes());
  147. ui->comboBox_Type->setCurrentText(map->type);
  148. ui->comboBox_BattleScene->clear();
  149. ui->comboBox_BattleScene->addItems(project->getBattleScenes());
  150. ui->comboBox_BattleScene->setCurrentText(map->battle_scene);
  151. ui->checkBox_ShowLocation->setChecked(map->show_location.toInt() > 0 || map->show_location == "TRUE");
  152. }
  153. void MainWindow::on_comboBox_Song_activated(const QString &song)
  154. {
  155. editor->map->song = song;
  156. }
  157. void MainWindow::on_comboBox_Location_activated(const QString &location)
  158. {
  159. editor->map->location = location;
  160. }
  161. void MainWindow::on_comboBox_Visibility_activated(const QString &visibility)
  162. {
  163. editor->map->visibility = visibility;
  164. }
  165. void MainWindow::on_comboBox_Weather_activated(const QString &weather)
  166. {
  167. editor->map->weather = weather;
  168. }
  169. void MainWindow::on_comboBox_Type_activated(const QString &type)
  170. {
  171. editor->map->type = type;
  172. }
  173. void MainWindow::on_comboBox_BattleScene_activated(const QString &battle_scene)
  174. {
  175. editor->map->battle_scene = battle_scene;
  176. }
  177. void MainWindow::on_checkBox_ShowLocation_clicked(bool checked)
  178. {
  179. if (checked) {
  180. editor->map->show_location = "TRUE";
  181. } else {
  182. editor->map->show_location = "FALSE";
  183. }
  184. }
  185. void MainWindow::populateMapList() {
  186. Project *project = editor->project;
  187. QIcon mapFolderIcon;
  188. mapFolderIcon.addFile(QStringLiteral(":/icons/folder_closed_map.ico"), QSize(), QIcon::Normal, QIcon::Off);
  189. mapFolderIcon.addFile(QStringLiteral(":/icons/folder_map.ico"), QSize(), QIcon::Normal, QIcon::On);
  190. QIcon folderIcon;
  191. folderIcon.addFile(QStringLiteral(":/icons/folder_closed.ico"), QSize(), QIcon::Normal, QIcon::Off);
  192. QIcon mapIcon;
  193. mapIcon.addFile(QStringLiteral(":/icons/map.ico"), QSize(), QIcon::Normal, QIcon::Off);
  194. mapIcon.addFile(QStringLiteral(":/icons/image.ico"), QSize(), QIcon::Normal, QIcon::On);
  195. QStandardItemModel *model = new QStandardItemModel;
  196. QStandardItem *entry = new QStandardItem;
  197. entry->setText(project->getProjectTitle());
  198. entry->setIcon(folderIcon);
  199. entry->setEditable(false);
  200. model->appendRow(entry);
  201. QStandardItem *maps = new QStandardItem;
  202. maps->setText("maps");
  203. maps->setIcon(folderIcon);
  204. maps->setEditable(false);
  205. entry->appendRow(maps);
  206. project->readMapGroups();
  207. for (int i = 0; i < project->groupNames->length(); i++) {
  208. QString group_name = project->groupNames->value(i);
  209. QStandardItem *group = new QStandardItem;
  210. group->setText(group_name);
  211. group->setIcon(mapFolderIcon);
  212. group->setEditable(false);
  213. maps->appendRow(group);
  214. QStringList *names = project->groupedMapNames->value(i);
  215. for (int j = 0; j < names->length(); j++) {
  216. QString map_name = names->value(j);
  217. QStandardItem *map = new QStandardItem;
  218. map->setText(QString("[%1.%2] ").arg(i).arg(j, 2, 10, QLatin1Char('0')) + map_name);
  219. map->setIcon(mapIcon);
  220. map->setEditable(false);
  221. map->setData(map_name, Qt::UserRole);
  222. group->appendRow(map);
  223. //ui->mapList->setExpanded(model->indexFromItem(map), false); // redundant
  224. }
  225. }
  226. ui->mapList->setModel(model);
  227. ui->mapList->setUpdatesEnabled(true);
  228. ui->mapList->expandToDepth(2);
  229. ui->mapList->repaint();
  230. }
  231. void MainWindow::on_mapList_activated(const QModelIndex &index)
  232. {
  233. QVariant data = index.data(Qt::UserRole);
  234. if (!data.isNull()) {
  235. setMap(data.toString());
  236. }
  237. updateMapList();
  238. }
  239. void MainWindow::markAllEdited(QAbstractItemModel *model) {
  240. QList<QModelIndex> list;
  241. list.append(QModelIndex());
  242. while (list.length()) {
  243. QModelIndex parent = list.takeFirst();
  244. for (int i = 0; i < model->rowCount(parent); i++) {
  245. QModelIndex index = model->index(i, 0, parent);
  246. if (model->hasChildren(index)) {
  247. list.append(index);
  248. }
  249. markEdited(index);
  250. }
  251. }
  252. }
  253. void MainWindow::markEdited(QModelIndex index) {
  254. QVariant data = index.data(Qt::UserRole);
  255. if (!data.isNull()) {
  256. QString map_name = data.toString();
  257. if (editor->project) {
  258. if (editor->project->map_cache->contains(map_name)) {
  259. // Just mark anything that's been opened for now.
  260. // TODO if (project->getMap()->saved)
  261. ui->mapList->setExpanded(index, true);
  262. }
  263. }
  264. }
  265. }
  266. void MainWindow::updateMapList() {
  267. QAbstractItemModel *model = ui->mapList->model();
  268. markAllEdited(model);
  269. }
  270. void MainWindow::on_action_Save_Project_triggered()
  271. {
  272. editor->saveProject();
  273. updateMapList();
  274. }
  275. void MainWindow::undo() {
  276. editor->undo();
  277. }
  278. void MainWindow::redo() {
  279. editor->redo();
  280. }
  281. void MainWindow::on_action_Save_triggered() {
  282. editor->save();
  283. }
  284. void MainWindow::on_tabWidget_2_currentChanged(int index)
  285. {
  286. if (index == 0) {
  287. editor->setEditingMap();
  288. } else if (index == 1) {
  289. editor->setEditingCollision();
  290. }
  291. }
  292. void MainWindow::on_action_Exit_triggered()
  293. {
  294. QApplication::quit();
  295. }
  296. void MainWindow::on_tabWidget_currentChanged(int index)
  297. {
  298. if (index == 0) {
  299. on_tabWidget_2_currentChanged(ui->tabWidget_2->currentIndex());
  300. } else if (index == 1) {
  301. editor->setEditingObjects();
  302. }
  303. }
  304. void MainWindow::on_actionUndo_triggered()
  305. {
  306. undo();
  307. }
  308. void MainWindow::on_actionRedo_triggered()
  309. {
  310. redo();
  311. }