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 editor = new EditorWidget(parent);
24 down = new QToolButton;
25 add = new QToolButton;
26 remove = new QToolButton;
27 play = new QToolButton;
29 up->setIcon(QIcon(":/icon-up.png"));
30 down->setIcon(QIcon(":/icon-down.png"));
31 add->setIcon(QIcon(":/icon-add.png"));
32 remove->setIcon(QIcon(":/icon-delete.png"));
33 play->setIcon(QIcon(":/icon-play.png"));
35 list->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred);
36 // editor->setFocus(); //DISNOWOK
39 for(unsigned int i=0; i<editor->levelStorage.size(); i++)
41 list->addItem(QString(editor->levelStorage[i].name));
42 QListWidgetItem * item = list->item(i);
43 item->setFlags(item->flags() | Qt::ItemIsEditable);
46 QHBoxLayout * buttonBox = new QHBoxLayout;
47 QVBoxLayout * listBox = new QVBoxLayout;
48 QHBoxLayout * mainBox = new QHBoxLayout;
50 buttonBox->addWidget(up);
51 buttonBox->addWidget(down);
52 buttonBox->addWidget(add);
53 buttonBox->addWidget(remove);
54 buttonBox->addWidget(play);
56 listBox->addWidget(list);
57 listBox->addLayout(buttonBox);
59 mainBox->addLayout(listBox);
60 mainBox->addWidget(editor);
63 setWindowTitle("Editor");
65 connect(list, SIGNAL(currentRowChanged(int)), this, SLOT(SetEditorLevel(int)));
66 connect(list->itemDelegate(), SIGNAL(closeEditor(QWidget *, QAbstractItemDelegate::EndEditHint)), this, SLOT(SetNewName(QWidget *, QAbstractItemDelegate::EndEditHint)));
68 connect(up, SIGNAL(clicked()), this, SLOT(MoveLevelUp()));
69 connect(down, SIGNAL(clicked()), this, SLOT(MoveLevelDown()));
70 connect(add, SIGNAL(clicked()), this, SLOT(AddLevel()));
71 connect(remove, SIGNAL(clicked()), this, SLOT(DeleteLevel()));
72 connect(play, SIGNAL(clicked()), this, SLOT(PlayLevel()));
76 EditorWindow::~EditorWindow(void)
88 void EditorWindow::SetEditorLevel(int level)
90 //printf("SetEditorWindow: level=%i\n", level);
91 editor->currentLevel = level;
95 up->setDisabled(true);
96 else if ((unsigned int)level == (editor->levelStorage.size() - 1))
97 down->setDisabled(true);
100 up->setDisabled(false);
101 down->setDisabled(false);
106 void EditorWindow::SetNewName(QWidget * widget, QAbstractItemDelegate::EndEditHint)
108 QString newName = reinterpret_cast<QLineEdit *>(widget)->text();
109 editor->SetNameOnCurrentLevel(newName.toAscii().data());
113 void EditorWindow::MoveLevelUp(void)
115 // Swap in the vector...
116 int n = editor->currentLevel;
117 std::swap(editor->levelStorage[n], editor->levelStorage[n - 1]);
119 // Swap in the QListWidget too...
120 int currentRow = list->currentRow();
121 QListWidgetItem * currentItem = list->takeItem(currentRow);
122 list->insertItem(currentRow - 1, currentItem);
123 list->setCurrentRow(currentRow - 1);
127 void EditorWindow::MoveLevelDown(void)
129 int n = editor->currentLevel;
130 std::swap(editor->levelStorage[n], editor->levelStorage[n + 1]);
132 int currentRow = list->currentRow();
133 QListWidgetItem * currentItem = list->takeItem(currentRow);
134 list->insertItem(currentRow + 1, currentItem);
135 list->setCurrentRow(currentRow + 1);
139 void EditorWindow::AddLevel(void)
141 editor->AddNewLevelAtCurrentPosition();
142 editor->SetNameOnCurrentLevel("New Level");
144 QListWidgetItem * item = new QListWidgetItem("New Level");
145 item->setFlags(item->flags() | Qt::ItemIsEditable);
146 int currentRow = list->currentRow();
147 list->insertItem(currentRow, item);
148 list->setCurrentRow(currentRow);
152 void EditorWindow::DeleteLevel(void)
154 //printf("DeleteLevel: Erasing level from levelStorage...\n");
155 editor->levelStorage.erase(editor->levelStorage.begin() + editor->currentLevel);
157 //printf("DeleteLevel: Erasing level from list...\n");
158 int currentRow = list->currentRow();
159 //printf("DeleteLevel: currentRow = %i\n", currentRow);
160 QListWidgetItem * item = list->takeItem(currentRow);
161 //Does nothing--the same as a click on the item, it's the wrong number (item + 1)...!
162 // list->setCurrentRow(currentRow);
163 // According to the docs, it's now our responsibility to care for this item...
164 //printf("DeleteLevel: Deleting item...\n");
167 // We need to do this because of a bug in QListWidget::takeItem(). For some reason,
168 // it sets the current item to the one following the one taken. Bad!
169 SetEditorLevel(currentRow);
173 void EditorWindow::PlayLevel(void)
175 emit SetupLevel(&(editor->levelStorage[editor->currentLevel]));