2 // editorwindow.cpp: Game editor window
5 // (C) 2014 Underground Software
7 // JLH = James Hammons <jlhamm@acm.org>
10 // --- ---------- ------------------------------------------------------------
11 // JLH 07/03/2014 Created this file
14 #include "editorwindow.h"
16 #include "editorwidget.h"
19 EditorWindow::EditorWindow(QWidget * parent/*= 0*/): QWidget(parent, Qt::Window)
21 list = new QListWidget;
22 up = new QPushButton("^");
23 down = new QPushButton("v");
24 add = new QPushButton("+");
25 remove = new QPushButton("-");
26 play = new QPushButton(">");
27 editor = new EditorWidget(parent);
29 list->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred);
30 // editor->setFocus(); //DISNOWOK
33 for(unsigned int i=0; i<editor->levelStorage.size(); i++)
35 list->addItem(QString(editor->levelStorage[i].name));
36 QListWidgetItem * item = list->item(i);
37 item->setFlags(item->flags() | Qt::ItemIsEditable);
40 QHBoxLayout * buttonBox = new QHBoxLayout;
41 QVBoxLayout * listBox = new QVBoxLayout;
42 QHBoxLayout * mainBox = new QHBoxLayout;
44 buttonBox->addWidget(up);
45 buttonBox->addWidget(down);
46 buttonBox->addWidget(add);
47 buttonBox->addWidget(remove);
48 buttonBox->addWidget(play);
50 listBox->addWidget(list);
51 listBox->addLayout(buttonBox);
53 mainBox->addLayout(listBox);
54 mainBox->addWidget(editor);
57 setWindowTitle("Editor");
59 connect(list, SIGNAL(currentRowChanged(int)), this, SLOT(SetEditorLevel(int)));
60 connect(list->itemDelegate(), SIGNAL(closeEditor(QWidget *, QAbstractItemDelegate::EndEditHint)), this, SLOT(SetNewName(QWidget *, QAbstractItemDelegate::EndEditHint)));
62 connect(up, SIGNAL(clicked()), this, SLOT(MoveLevelUp()));
63 connect(down, SIGNAL(clicked()), this, SLOT(MoveLevelDown()));
64 connect(add, SIGNAL(clicked()), this, SLOT(AddLevel()));
65 connect(remove, SIGNAL(clicked()), this, SLOT(DeleteLevel()));
66 connect(play, SIGNAL(clicked()), this, SLOT(PlayLevel()));
70 EditorWindow::~EditorWindow(void)
82 void EditorWindow::SetEditorLevel(int level)
84 //printf("SetEditorWindow: level=%i\n", level);
85 editor->currentLevel = level;
89 up->setDisabled(true);
90 else if ((unsigned int)level == (editor->levelStorage.size() - 1))
91 down->setDisabled(true);
94 up->setDisabled(false);
95 down->setDisabled(false);
100 void EditorWindow::SetNewName(QWidget * widget, QAbstractItemDelegate::EndEditHint)
102 QString newName = reinterpret_cast<QLineEdit *>(widget)->text();
103 editor->SetNameOnCurrentLevel(newName.toAscii().data());
107 void EditorWindow::MoveLevelUp(void)
109 // Swap in the vector...
110 int n = editor->currentLevel;
111 std::swap(editor->levelStorage[n], editor->levelStorage[n - 1]);
113 // Swap in the QListWidget too...
114 int currentRow = list->currentRow();
115 QListWidgetItem * currentItem = list->takeItem(currentRow);
116 list->insertItem(currentRow - 1, currentItem);
117 list->setCurrentRow(currentRow - 1);
121 void EditorWindow::MoveLevelDown(void)
123 int n = editor->currentLevel;
124 std::swap(editor->levelStorage[n], editor->levelStorage[n + 1]);
126 int currentRow = list->currentRow();
127 QListWidgetItem * currentItem = list->takeItem(currentRow);
128 list->insertItem(currentRow + 1, currentItem);
129 list->setCurrentRow(currentRow + 1);
133 void EditorWindow::AddLevel(void)
135 editor->AddNewLevelAtCurrentPosition();
136 editor->SetNameOnCurrentLevel("New Level");
138 QListWidgetItem * item = new QListWidgetItem("New Level");
139 item->setFlags(item->flags() | Qt::ItemIsEditable);
140 int currentRow = list->currentRow();
141 list->insertItem(currentRow, item);
142 list->setCurrentRow(currentRow);
146 void EditorWindow::DeleteLevel(void)
148 //printf("DeleteLevel: Erasing level from levelStorage...\n");
149 editor->levelStorage.erase(editor->levelStorage.begin() + editor->currentLevel);
151 //printf("DeleteLevel: Erasing level from list...\n");
152 int currentRow = list->currentRow();
153 //printf("DeleteLevel: currentRow = %i\n", currentRow);
154 QListWidgetItem * item = list->takeItem(currentRow);
155 //Does nothing--the same as a click on the item, it's the wrong number (item + 1)...!
156 // list->setCurrentRow(currentRow);
157 // According to the docs, it's now our responsibility to care for this item...
158 //printf("DeleteLevel: Deleting item...\n");
161 // We need to do this because of a bug in QListWidget::takeItem(). For some reason,
162 // it sets the current item to the one following the one taken. Bad!
163 SetEditorLevel(currentRow);
167 void EditorWindow::PlayLevel(void)
169 emit SetupLevel(&(editor->levelStorage[editor->currentLevel]));