]> Shamusworld >> Repos - warehouse-man-deluxe/blob - src/editorwindow.cpp
Finish adding functionality to editor.
[warehouse-man-deluxe] / src / editorwindow.cpp
1 //
2 // editorwindow.cpp: Game editor window
3 //
4 // by James Hammons
5 // (C) 2014 Underground Software
6 //
7 // JLH = James Hammons <jlhamm@acm.org>
8 //
9 // Who  When        What
10 // ---  ----------  ------------------------------------------------------------
11 // JLH  07/03/2014  Created this file
12 //
13
14 #include "editorwindow.h"
15 #include <algorithm>
16 #include "editorwidget.h"
17
18
19 EditorWindow::EditorWindow(QWidget * parent/*= 0*/): QWidget(parent, Qt::Window)
20 {
21         list = new QListWidget;
22         up = new QPushButton("^");
23         down = new QPushButton("v");
24         add = new QPushButton("+");
25         remove = new QPushButton("-");
26         editor = new EditorWidget(parent);
27
28         list->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred);
29 //      editor->setFocus(); //DISNOWOK
30 //      editor->Load();
31
32         for(unsigned int i=0; i<editor->levelStorage.size(); i++)
33         {
34                 list->addItem(QString(editor->levelStorage[i].name));
35                 QListWidgetItem * item = list->item(i);
36                 item->setFlags(item->flags() | Qt::ItemIsEditable);
37         }
38
39         QHBoxLayout * buttonBox = new QHBoxLayout;
40         QVBoxLayout * listBox = new QVBoxLayout;
41         QHBoxLayout * mainBox = new QHBoxLayout;
42
43         buttonBox->addWidget(up);
44         buttonBox->addWidget(down);
45         buttonBox->addWidget(add);
46         buttonBox->addWidget(remove);
47
48         listBox->addWidget(list);
49         listBox->addLayout(buttonBox);
50
51         mainBox->addLayout(listBox);
52         mainBox->addWidget(editor);
53
54         setLayout(mainBox);
55         setWindowTitle("Editor");
56
57         connect(list, SIGNAL(currentRowChanged(int)), this, SLOT(SetEditorLevel(int)));
58         connect(list->itemDelegate(), SIGNAL(closeEditor(QWidget *, QAbstractItemDelegate::EndEditHint)), this, SLOT(SetNewName(QWidget *, QAbstractItemDelegate::EndEditHint)));
59
60         connect(up, SIGNAL(clicked()), this, SLOT(MoveLevelUp()));
61         connect(down, SIGNAL(clicked()), this, SLOT(MoveLevelDown()));
62         connect(add, SIGNAL(clicked()), this, SLOT(AddLevel()));
63         connect(remove, SIGNAL(clicked()), this, SLOT(DeleteLevel()));
64 }
65
66
67 EditorWindow::~EditorWindow(void)
68 {
69 //      editor->Save();
70         delete editor;
71         delete up;
72         delete down;
73         delete add;
74         delete remove;
75         delete list;
76 }
77
78
79 void EditorWindow::SetEditorLevel(int level)
80 {
81 //printf("SetEditorWindow: level=%i\n", level);
82         editor->currentLevel = level;
83         editor->update();
84
85         if (level == 0)
86                 up->setDisabled(true);
87         else if ((unsigned int)level == (editor->levelStorage.size() - 1))
88                 down->setDisabled(true);
89         else
90         {
91                 up->setDisabled(false);
92                 down->setDisabled(false);
93         }
94 }
95
96
97 void EditorWindow::SetNewName(QWidget * widget, QAbstractItemDelegate::EndEditHint)
98 {
99         QString newName = reinterpret_cast<QLineEdit *>(widget)->text();
100         editor->SetNameOnCurrentLevel(newName.toAscii().data());
101 }
102
103
104 void EditorWindow::MoveLevelUp(void)
105 {
106         // Swap in the vector...
107         int n = editor->currentLevel;
108         std::swap(editor->levelStorage[n], editor->levelStorage[n - 1]);
109
110         // Swap in the QListWidget too...
111         int currentRow = list->currentRow();
112         QListWidgetItem * currentItem = list->takeItem(currentRow);
113         list->insertItem(currentRow - 1, currentItem);
114         list->setCurrentRow(currentRow - 1);
115 }
116
117
118 void EditorWindow::MoveLevelDown(void)
119 {
120         int n = editor->currentLevel;
121         std::swap(editor->levelStorage[n], editor->levelStorage[n + 1]);
122
123         int currentRow = list->currentRow();
124         QListWidgetItem * currentItem = list->takeItem(currentRow);
125         list->insertItem(currentRow + 1, currentItem);
126         list->setCurrentRow(currentRow + 1);
127 }
128
129
130 void EditorWindow::AddLevel(void)
131 {
132         editor->AddNewLevelAtCurrentPosition();
133         editor->SetNameOnCurrentLevel("New Level");
134
135         QListWidgetItem * item = new QListWidgetItem("New Level");
136         item->setFlags(item->flags() | Qt::ItemIsEditable);
137         int currentRow = list->currentRow();
138         list->insertItem(currentRow, item);
139         list->setCurrentRow(currentRow);
140 }
141
142
143 void EditorWindow::DeleteLevel(void)
144 {
145 //printf("DeleteLevel: Erasing level from levelStorage...\n");
146         editor->levelStorage.erase(editor->levelStorage.begin() + editor->currentLevel);
147
148 //printf("DeleteLevel: Erasing level from list...\n");
149         int currentRow = list->currentRow();
150 //printf("DeleteLevel: currentRow = %i\n", currentRow);
151         QListWidgetItem * item = list->takeItem(currentRow);
152 //Does nothing--the same as a click on the item, it's the wrong number (item + 1)...!
153 //      list->setCurrentRow(currentRow);
154         // According to the docs, it's now our responsibility to care for this item...
155 //printf("DeleteLevel: Deleting item...\n");
156         delete item;
157
158         // We need to do this because of a bug in QListWidget::takeItem(). For some reason,
159         // it sets the current item to the one following the one taken. Bad!
160         SetEditorLevel(currentRow);
161 }
162