]> Shamusworld >> Repos - warehouse-man-deluxe/blobdiff - src/editorwindow.cpp
Finish adding functionality to editor.
[warehouse-man-deluxe] / src / editorwindow.cpp
index 1ccc14c43adde23c10a8d2769c039a288e887dcf..9c95ba17accfafabfa3e5c1d9875301d0eed83ad 100644 (file)
@@ -12,6 +12,7 @@
 //
 
 #include "editorwindow.h"
+#include <algorithm>
 #include "editorwidget.h"
 
 
@@ -55,6 +56,11 @@ EditorWindow::EditorWindow(QWidget * parent/*= 0*/): QWidget(parent, Qt::Window)
 
        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()));
 }
 
 
@@ -72,8 +78,19 @@ EditorWindow::~EditorWindow(void)
 
 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);
+       }
 }
 
 
@@ -83,3 +100,63 @@ void EditorWindow::SetNewName(QWidget * widget, QAbstractItemDelegate::EndEditHi
        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);
+}
+