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 editor = new EditorWidget(parent);
28 list->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred);
29 // editor->setFocus(); //DISNOWOK
32 for(unsigned int i=0; i<editor->levelStorage.size(); i++)
34 list->addItem(QString(editor->levelStorage[i].name));
35 QListWidgetItem * item = list->item(i);
36 item->setFlags(item->flags() | Qt::ItemIsEditable);
39 QHBoxLayout * buttonBox = new QHBoxLayout;
40 QVBoxLayout * listBox = new QVBoxLayout;
41 QHBoxLayout * mainBox = new QHBoxLayout;
43 buttonBox->addWidget(up);
44 buttonBox->addWidget(down);
45 buttonBox->addWidget(add);
46 buttonBox->addWidget(remove);
48 listBox->addWidget(list);
49 listBox->addLayout(buttonBox);
51 mainBox->addLayout(listBox);
52 mainBox->addWidget(editor);
55 setWindowTitle("Editor");
57 connect(list, SIGNAL(currentRowChanged(int)), this, SLOT(SetEditorLevel(int)));
58 connect(list->itemDelegate(), SIGNAL(closeEditor(QWidget *, QAbstractItemDelegate::EndEditHint)), this, SLOT(SetNewName(QWidget *, QAbstractItemDelegate::EndEditHint)));
60 connect(up, SIGNAL(clicked()), this, SLOT(MoveLevelUp()));
61 connect(down, SIGNAL(clicked()), this, SLOT(MoveLevelDown()));
62 connect(add, SIGNAL(clicked()), this, SLOT(AddLevel()));
63 connect(remove, SIGNAL(clicked()), this, SLOT(DeleteLevel()));
67 EditorWindow::~EditorWindow(void)
79 void EditorWindow::SetEditorLevel(int level)
81 //printf("SetEditorWindow: level=%i\n", level);
82 editor->currentLevel = level;
86 up->setDisabled(true);
87 else if ((unsigned int)level == (editor->levelStorage.size() - 1))
88 down->setDisabled(true);
91 up->setDisabled(false);
92 down->setDisabled(false);
97 void EditorWindow::SetNewName(QWidget * widget, QAbstractItemDelegate::EndEditHint)
99 QString newName = reinterpret_cast<QLineEdit *>(widget)->text();
100 editor->SetNameOnCurrentLevel(newName.toAscii().data());
104 void EditorWindow::MoveLevelUp(void)
106 // Swap in the vector...
107 int n = editor->currentLevel;
108 std::swap(editor->levelStorage[n], editor->levelStorage[n - 1]);
110 // Swap in the QListWidget too...
111 int currentRow = list->currentRow();
112 QListWidgetItem * currentItem = list->takeItem(currentRow);
113 list->insertItem(currentRow - 1, currentItem);
114 list->setCurrentRow(currentRow - 1);
118 void EditorWindow::MoveLevelDown(void)
120 int n = editor->currentLevel;
121 std::swap(editor->levelStorage[n], editor->levelStorage[n + 1]);
123 int currentRow = list->currentRow();
124 QListWidgetItem * currentItem = list->takeItem(currentRow);
125 list->insertItem(currentRow + 1, currentItem);
126 list->setCurrentRow(currentRow + 1);
130 void EditorWindow::AddLevel(void)
132 editor->AddNewLevelAtCurrentPosition();
133 editor->SetNameOnCurrentLevel("New Level");
135 QListWidgetItem * item = new QListWidgetItem("New Level");
136 item->setFlags(item->flags() | Qt::ItemIsEditable);
137 int currentRow = list->currentRow();
138 list->insertItem(currentRow, item);
139 list->setCurrentRow(currentRow);
143 void EditorWindow::DeleteLevel(void)
145 //printf("DeleteLevel: Erasing level from levelStorage...\n");
146 editor->levelStorage.erase(editor->levelStorage.begin() + editor->currentLevel);
148 //printf("DeleteLevel: Erasing level from list...\n");
149 int currentRow = list->currentRow();
150 //printf("DeleteLevel: currentRow = %i\n", currentRow);
151 QListWidgetItem * item = list->takeItem(currentRow);
152 //Does nothing--the same as a click on the item, it's the wrong number (item + 1)...!
153 // list->setCurrentRow(currentRow);
154 // According to the docs, it's now our responsibility to care for this item...
155 //printf("DeleteLevel: Deleting item...\n");
158 // We need to do this because of a bug in QListWidget::takeItem(). For some reason,
159 // it sets the current item to the one following the one taken. Bad!
160 SetEditorLevel(currentRow);