]> Shamusworld >> Repos - warehouse-man-deluxe/blob - src/editorwindow.cpp
Added sensible icons to the 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         editor = new EditorWidget(parent);
23         up = new QToolButton;
24         down = new QToolButton;
25         add = new QToolButton;
26         remove = new QToolButton;
27         play = new QToolButton;
28
29         up->setIcon(QIcon(":/icon-up.png"));
30         down->setIcon(QIcon(":/icon-down.png"));
31         add->setIcon(QIcon(":/icon-add.png"));
32         remove->setIcon(QIcon(":/icon-delete.png"));
33         play->setIcon(QIcon(":/icon-play.png"));
34
35         list->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred);
36 //      editor->setFocus(); //DISNOWOK
37 //      editor->Load();
38
39         for(unsigned int i=0; i<editor->levelStorage.size(); i++)
40         {
41                 list->addItem(QString(editor->levelStorage[i].name));
42                 QListWidgetItem * item = list->item(i);
43                 item->setFlags(item->flags() | Qt::ItemIsEditable);
44         }
45
46         QHBoxLayout * buttonBox = new QHBoxLayout;
47         QVBoxLayout * listBox = new QVBoxLayout;
48         QHBoxLayout * mainBox = new QHBoxLayout;
49
50         buttonBox->addWidget(up);
51         buttonBox->addWidget(down);
52         buttonBox->addWidget(add);
53         buttonBox->addWidget(remove);
54         buttonBox->addWidget(play);
55
56         listBox->addWidget(list);
57         listBox->addLayout(buttonBox);
58
59         mainBox->addLayout(listBox);
60         mainBox->addWidget(editor);
61
62         setLayout(mainBox);
63         setWindowTitle("Editor");
64
65         connect(list, SIGNAL(currentRowChanged(int)), this, SLOT(SetEditorLevel(int)));
66         connect(list->itemDelegate(), SIGNAL(closeEditor(QWidget *, QAbstractItemDelegate::EndEditHint)), this, SLOT(SetNewName(QWidget *, QAbstractItemDelegate::EndEditHint)));
67
68         connect(up, SIGNAL(clicked()), this, SLOT(MoveLevelUp()));
69         connect(down, SIGNAL(clicked()), this, SLOT(MoveLevelDown()));
70         connect(add, SIGNAL(clicked()), this, SLOT(AddLevel()));
71         connect(remove, SIGNAL(clicked()), this, SLOT(DeleteLevel()));
72         connect(play, SIGNAL(clicked()), this, SLOT(PlayLevel()));
73 }
74
75
76 EditorWindow::~EditorWindow(void)
77 {
78 //      editor->Save();
79         delete editor;
80         delete up;
81         delete down;
82         delete add;
83         delete remove;
84         delete list;
85 }
86
87
88 void EditorWindow::SetEditorLevel(int level)
89 {
90 //printf("SetEditorWindow: level=%i\n", level);
91         editor->currentLevel = level;
92         editor->update();
93
94         if (level == 0)
95                 up->setDisabled(true);
96         else if ((unsigned int)level == (editor->levelStorage.size() - 1))
97                 down->setDisabled(true);
98         else
99         {
100                 up->setDisabled(false);
101                 down->setDisabled(false);
102         }
103 }
104
105
106 void EditorWindow::SetNewName(QWidget * widget, QAbstractItemDelegate::EndEditHint)
107 {
108         QString newName = reinterpret_cast<QLineEdit *>(widget)->text();
109         editor->SetNameOnCurrentLevel(newName.toAscii().data());
110 }
111
112
113 void EditorWindow::MoveLevelUp(void)
114 {
115         // Swap in the vector...
116         int n = editor->currentLevel;
117         std::swap(editor->levelStorage[n], editor->levelStorage[n - 1]);
118
119         // Swap in the QListWidget too...
120         int currentRow = list->currentRow();
121         QListWidgetItem * currentItem = list->takeItem(currentRow);
122         list->insertItem(currentRow - 1, currentItem);
123         list->setCurrentRow(currentRow - 1);
124 }
125
126
127 void EditorWindow::MoveLevelDown(void)
128 {
129         int n = editor->currentLevel;
130         std::swap(editor->levelStorage[n], editor->levelStorage[n + 1]);
131
132         int currentRow = list->currentRow();
133         QListWidgetItem * currentItem = list->takeItem(currentRow);
134         list->insertItem(currentRow + 1, currentItem);
135         list->setCurrentRow(currentRow + 1);
136 }
137
138
139 void EditorWindow::AddLevel(void)
140 {
141         editor->AddNewLevelAtCurrentPosition();
142         editor->SetNameOnCurrentLevel("New Level");
143
144         QListWidgetItem * item = new QListWidgetItem("New Level");
145         item->setFlags(item->flags() | Qt::ItemIsEditable);
146         int currentRow = list->currentRow();
147         list->insertItem(currentRow, item);
148         list->setCurrentRow(currentRow);
149 }
150
151
152 void EditorWindow::DeleteLevel(void)
153 {
154 //printf("DeleteLevel: Erasing level from levelStorage...\n");
155         editor->levelStorage.erase(editor->levelStorage.begin() + editor->currentLevel);
156
157 //printf("DeleteLevel: Erasing level from list...\n");
158         int currentRow = list->currentRow();
159 //printf("DeleteLevel: currentRow = %i\n", currentRow);
160         QListWidgetItem * item = list->takeItem(currentRow);
161 //Does nothing--the same as a click on the item, it's the wrong number (item + 1)...!
162 //      list->setCurrentRow(currentRow);
163         // According to the docs, it's now our responsibility to care for this item...
164 //printf("DeleteLevel: Deleting item...\n");
165         delete item;
166
167         // We need to do this because of a bug in QListWidget::takeItem(). For some reason,
168         // it sets the current item to the one following the one taken. Bad!
169         SetEditorLevel(currentRow);
170 }
171
172
173 void EditorWindow::PlayLevel(void)
174 {
175         emit SetupLevel(&(editor->levelStorage[editor->currentLevel]));
176 }
177