//
#include "editorwindow.h"
+#include <algorithm>
#include "editorwidget.h"
connect(list, SIGNAL(currentRowChanged(int)), this, SLOT(SetEditorLevel(int)));
connect(list->itemDelegate(), SIGNAL(closeEditor(QWidget *, QAbstractItemDelegate::EndEditHint)), this, SLOT(SetNewName(QWidget *, QAbstractItemDelegate::EndEditHint)));
+
+ connect(up, SIGNAL(clicked()), this, SLOT(MoveLevelUp()));
+ connect(down, SIGNAL(clicked()), this, SLOT(MoveLevelDown()));
+ connect(add, SIGNAL(clicked()), this, SLOT(AddLevel()));
+ connect(remove, SIGNAL(clicked()), this, SLOT(DeleteLevel()));
}
void EditorWindow::SetEditorLevel(int level)
{
+//printf("SetEditorWindow: level=%i\n", level);
editor->currentLevel = level;
editor->update();
+
+ if (level == 0)
+ up->setDisabled(true);
+ else if ((unsigned int)level == (editor->levelStorage.size() - 1))
+ down->setDisabled(true);
+ else
+ {
+ up->setDisabled(false);
+ down->setDisabled(false);
+ }
}
editor->SetNameOnCurrentLevel(newName.toAscii().data());
}
+
+void EditorWindow::MoveLevelUp(void)
+{
+ // Swap in the vector...
+ int n = editor->currentLevel;
+ std::swap(editor->levelStorage[n], editor->levelStorage[n - 1]);
+
+ // Swap in the QListWidget too...
+ int currentRow = list->currentRow();
+ QListWidgetItem * currentItem = list->takeItem(currentRow);
+ list->insertItem(currentRow - 1, currentItem);
+ list->setCurrentRow(currentRow - 1);
+}
+
+
+void EditorWindow::MoveLevelDown(void)
+{
+ int n = editor->currentLevel;
+ std::swap(editor->levelStorage[n], editor->levelStorage[n + 1]);
+
+ int currentRow = list->currentRow();
+ QListWidgetItem * currentItem = list->takeItem(currentRow);
+ list->insertItem(currentRow + 1, currentItem);
+ list->setCurrentRow(currentRow + 1);
+}
+
+
+void EditorWindow::AddLevel(void)
+{
+ editor->AddNewLevelAtCurrentPosition();
+ editor->SetNameOnCurrentLevel("New Level");
+
+ QListWidgetItem * item = new QListWidgetItem("New Level");
+ item->setFlags(item->flags() | Qt::ItemIsEditable);
+ int currentRow = list->currentRow();
+ list->insertItem(currentRow, item);
+ list->setCurrentRow(currentRow);
+}
+
+
+void EditorWindow::DeleteLevel(void)
+{
+//printf("DeleteLevel: Erasing level from levelStorage...\n");
+ editor->levelStorage.erase(editor->levelStorage.begin() + editor->currentLevel);
+
+//printf("DeleteLevel: Erasing level from list...\n");
+ int currentRow = list->currentRow();
+//printf("DeleteLevel: currentRow = %i\n", currentRow);
+ QListWidgetItem * item = list->takeItem(currentRow);
+//Does nothing--the same as a click on the item, it's the wrong number (item + 1)...!
+// list->setCurrentRow(currentRow);
+ // According to the docs, it's now our responsibility to care for this item...
+//printf("DeleteLevel: Deleting item...\n");
+ delete item;
+
+ // We need to do this because of a bug in QListWidget::takeItem(). For some reason,
+ // it sets the current item to the one following the one taken. Bad!
+ SetEditorLevel(currentRow);
+}
+