//
#include "editorwindow.h"
+#include <algorithm>
#include "editorwidget.h"
EditorWindow::EditorWindow(QWidget * parent/*= 0*/): QWidget(parent, Qt::Window)
{
list = new QListWidget;
- up = new QPushButton("^");
- down = new QPushButton("v");
- add = new QPushButton("+");
- remove = new QPushButton("-");
editor = new EditorWidget(parent);
+ up = new QToolButton;
+ down = new QToolButton;
+ add = new QToolButton;
+ remove = new QToolButton;
+ play = new QToolButton;
+
+ up->setIcon(QIcon(":/icon-up.png"));
+ down->setIcon(QIcon(":/icon-down.png"));
+ add->setIcon(QIcon(":/icon-add.png"));
+ remove->setIcon(QIcon(":/icon-delete.png"));
+ play->setIcon(QIcon(":/icon-play.png"));
list->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred);
// editor->setFocus(); //DISNOWOK
// editor->Load();
- for(unsigned int i=0; i<editor->levelStorage.size(); i++)
+ for(unsigned int i=0; i<levelStorage.size(); i++)
{
- list->addItem(QString(editor->levelStorage[i].name));
+ list->addItem(QString(levelStorage[i].name));
QListWidgetItem * item = list->item(i);
item->setFlags(item->flags() | Qt::ItemIsEditable);
}
buttonBox->addWidget(down);
buttonBox->addWidget(add);
buttonBox->addWidget(remove);
+ buttonBox->addWidget(play);
listBox->addWidget(list);
listBox->addLayout(buttonBox);
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()));
+ connect(play, SIGNAL(clicked()), this, SLOT(PlayLevel()));
}
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 == (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(levelStorage[n], 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(levelStorage[n], 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");
+ levelStorage.erase(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);
+}
+
+
+void EditorWindow::PlayLevel(void)
+{
+ emit SetupLevel(&(levelStorage[editor->currentLevel]));
+}
+