1 ///////////////////////////////////////////////////////////////////////////////
2 // Copyright (C) 2004-2010 by The Allacrost Project
3 // All Rights Reserved
4 //
5 // This code is licensed under the GNU GPL version 2. It is free software
6 // and you may modify it and/or redistribute it under the terms of this license.
7 // See http://www.gnu.org/copyleft/gpl.html for details.
8 ///////////////////////////////////////////////////////////////////////////////
9
10 /*!****************************************************************************
11 * \file dialog_boxes.cpp
12 * \author Philip Vorsilak, gorzuate@allacrost.org
13 * \brief Source file for all of editor's dialog boxes.
14 *****************************************************************************/
15
16 #include "dialog_boxes.h"
17
18 using namespace hoa_editor;
19
20 /************************
21 MapPropertiesDialog class functions follow
22 ************************/
23
MapPropertiesDialog(QWidget * parent,const QString & name,bool prop)24 MapPropertiesDialog::MapPropertiesDialog(QWidget* parent, const QString& name,
25 bool prop)
26 : QDialog(parent, (const char*) name)
27 {
28 setCaption("Map Properties...");
29
30 // Set up the height spinbox
31 _height_label = new QLabel("Height (in tiles):", this);
32 _height_sbox = new QSpinBox(this);
33 _height_sbox->setMinimum(24);
34 _height_sbox->setMaximum(1000);
35
36 // Set up the width spinbox
37 _width_label = new QLabel(" Width (in tiles):", this);
38 _width_sbox = new QSpinBox(this);
39 _width_sbox->setMinimum(32);
40 _width_sbox->setMaximum(1000);
41
42 // Set up the cancel and okay push buttons
43 _cancel_pbut = new QPushButton("Cancel", this);
44 _ok_pbut = new QPushButton("OK", this);
45 _cancel_pbut->setDefault(true);
46 // at construction no tilesets are checked, disable ok button
47 _ok_pbut->setEnabled(false);
48 connect(_ok_pbut, SIGNAL(released()), this, SLOT(accept()));
49 connect(_cancel_pbut, SIGNAL(released()), this, SLOT(reject()));
50
51 // Set up the list of selectable tilesets
52 QDir tileset_dir("img/tilesets");
53 _tileset_tree = new QTreeWidget(this);
54 _tileset_tree->setColumnCount(1);
55 _tileset_tree->setHeaderLabels(QStringList("Tilesets"));
56 connect(_tileset_tree, SIGNAL(itemChanged(QTreeWidgetItem*, int)), this,
57 SLOT(_EnableOKButton()));
58 QList<QTreeWidgetItem*> tilesets;
59
60 // Start the loop at 2 to skip over . (present working directory)
61 // and .. (parent directory).
62 for (uint32 i = 2; i < tileset_dir.count(); i++)
63 {
64 tilesets.append(new QTreeWidgetItem((QTreeWidget*)0,
65 QStringList(tileset_dir[i].remove(".png"))));
66 tilesets.back()->setCheckState(0, Qt::Unchecked); // enables checkboxes
67
68 if (prop)
69 {
70 // get reference to the Editor
71 Editor* editor = static_cast<Editor*> (parent);
72
73 // Iterate through the names of the tabs to see which ones in the
74 // list are already present and set their checkbox appropriately.
75 for (int j = 0; j < editor->_ed_tabs->count(); j++)
76 if (tilesets.back()->text(0) == editor->_ed_tabs->tabText(j))
77 {
78 tilesets.back()->setCheckState(0, Qt::Checked);
79 _ok_pbut->setEnabled(true);
80 break;
81 } // the two tileset names must match in order to set the checkbox
82 } // user wants to edit the map's properties
83 } // loop through all files in the tileset directory
84 _tileset_tree->insertTopLevelItems(0, tilesets);
85
86 if (prop)
87 {
88 // get reference to the Editor
89 Editor* editor = static_cast<Editor*> (parent);
90
91 _height_sbox->setValue(editor->_ed_scrollview->_map->GetHeight());
92 _width_sbox->setValue(editor->_ed_scrollview->_map->GetWidth());
93 } // user wants to edit the map's properties
94
95 // Add all of the aforementioned widgets into a nice-looking grid layout
96 _dia_layout = new QGridLayout(this);
97 _dia_layout->addWidget(_height_label, 0, 0);
98 _dia_layout->addWidget(_height_sbox, 1, 0);
99 _dia_layout->addWidget(_width_label, 2, 0);
100 _dia_layout->addWidget(_width_sbox, 3, 0);
101 _dia_layout->addWidget(_tileset_tree, 0, 1, 5, -1);
102 _dia_layout->addWidget(_cancel_pbut, 6, 0);
103 _dia_layout->addWidget(_ok_pbut, 6, 1);
104 } // MapPropertiesDialog constructor
105
~MapPropertiesDialog()106 MapPropertiesDialog::~MapPropertiesDialog()
107 {
108 delete _height_label;
109 delete _height_sbox;
110 delete _width_label;
111 delete _width_sbox;
112 delete _cancel_pbut;
113 delete _ok_pbut;
114 delete _tileset_tree;
115 delete _dia_layout;
116 } // MapPropertiesDialog destructor
117
118
119
120 // ********** Private slot **********
121
_EnableOKButton()122 void MapPropertiesDialog::_EnableOKButton()
123 {
124 // Disable the ok button if no tilesets are checked, otherwise enable it.
125 int num_items = _tileset_tree->topLevelItemCount();
126 for (int i = 0; i < num_items; i++)
127 {
128 if (_tileset_tree->topLevelItem(i)->checkState(0) == Qt::Checked)
129 {
130 _ok_pbut->setEnabled(true);
131 return;
132 } // at least one tileset must be checked in order to enable push button
133 } // iterate through all items in the _tileset_tree
134
135 // If this point is reached, no tilesets are checked.
136 _ok_pbut->setEnabled(false);
137 } // _EnableOKButton()
138
139
140
141 /************************
142 MusicDialog class functions follow
143 ************************/
144
MusicDialog(QWidget * parent,const QString & name)145 MusicDialog::MusicDialog(QWidget* parent, const QString& name)
146 : QDialog(parent, name)
147 {
148 setCaption("Map Music Picker");
149 _dia_layout = new QGridLayout(this);
150
151
152 // Set up the push buttons
153 _add_pbut = new QPushButton("Add to map", this);
154 _remove_pbut = new QPushButton("Remove from map", this);
155 _ok_pbut = new QPushButton("OK", this);
156 _ok_pbut->setDefault(true);
157 connect(_ok_pbut, SIGNAL(released()), this, SLOT(accept()));
158 connect(_add_pbut, SIGNAL(released()), this, SLOT(_AddMusic()));
159 connect(_remove_pbut, SIGNAL(released()), this, SLOT(_RemoveMusic()));
160
161 _available_label = new QLabel("Available Music",this);
162 _used_label = new QLabel("Used by Map",this);
163
164
165 // Set up the list of already used music files
166 _used_music_list = new QListWidget(this);
167 _used_music_list->setSelectionMode(QAbstractItemView::ExtendedSelection);
168
169 // Get reference to the Editor
170 Editor* editor = static_cast<Editor*> (parent);
171
172 QStringList used_music_files = editor->_ed_scrollview->_map->music_files;
173 for (QStringList::Iterator qit = used_music_files.begin();
174 qit != used_music_files.end(); ++qit)
175 {
176 _used_music_list->addItem(*qit);
177 } // add all files currently used by the map
178
179
180 // Set up the list of available, remaining music files
181 _available_music_list = new QListWidget(this);
182 _available_music_list->setSortingEnabled(true);
183 _available_music_list->setSelectionMode(QAbstractItemView::ExtendedSelection);
184 QDir music_dir("mus");
185
186 // Add music files
187 for (unsigned int i = 0; i < music_dir.count(); i++)
188 {
189 // Only look for *.ogg files that are not already used by the map
190 if (music_dir[i].contains(".ogg") &&
191 _used_music_list->findItems(music_dir[i], Qt::MatchContains).empty())
192 _available_music_list->addItem(music_dir[i]);
193 } // iterate through all files in the music directory
194
195
196 // Add all of the aforementioned widgets into a nice-looking grid layout
197 _dia_layout->addWidget(_available_label, 0, 0);
198 _dia_layout->addWidget(_available_music_list, 1, 0);
199 _dia_layout->addWidget(_used_label, 0, 1);
200 _dia_layout->addWidget(_used_music_list, 1, 1);
201 _dia_layout->addWidget(_add_pbut, 2, 0);
202 _dia_layout->addWidget(_remove_pbut, 2, 1);
203 _dia_layout->addWidget(_ok_pbut, 3, 1);
204 } // MusicDialog constructor
205
~MusicDialog()206 MusicDialog::~MusicDialog()
207 {
208 delete _add_pbut;
209 delete _remove_pbut;
210 delete _ok_pbut;
211 delete _available_label;
212 delete _used_label;
213 delete _available_music_list;
214 delete _used_music_list;
215 delete _dia_layout;
216 } // MusicDialog destructor
217
218
219
220 // ********** Private slots **********
221
_AddMusic()222 void MusicDialog::_AddMusic()
223 {
224 QList<QListWidgetItem*> files = _available_music_list->selectedItems();
225 for (QList<QListWidgetItem*>::Iterator qit = files.begin();
226 qit != files.end(); ++qit)
227 {
228 _used_music_list->addItem((*qit)->text());
229 _available_music_list->takeItem(_available_music_list->row(*qit));
230 } // add all selected files
231 } // AddMusic()
232
_RemoveMusic()233 void MusicDialog::_RemoveMusic()
234 {
235 QList<QListWidgetItem*> files = _used_music_list->selectedItems();
236 for (QList<QListWidgetItem*>::Iterator qit = files.begin(); qit != files.end(); ++qit)
237 {
238 _available_music_list->addItem((*qit)->text());
239 _used_music_list->takeItem(_used_music_list->row(*qit));
240 } // remove all selected files
241 } // RemoveMusic()
242
243
244
245 /************************
246 ContextPropertiesDialog class functions follow
247 ************************/
248
ContextPropertiesDialog(QWidget * parent,const QString & name)249 ContextPropertiesDialog::ContextPropertiesDialog(QWidget* parent, const QString& name)
250 : QDialog(parent, (const char*) name)
251 {
252 setCaption("Context Properties...");
253 _name_label = new QLabel("Context name", this);
254 _name_ledit = new QLineEdit(this);
255 connect(_name_ledit, SIGNAL(textEdited(const QString&)), this,
256 SLOT(_EnableOKButton()));
257
258
259 // Set up the cancel and okay push buttons
260 _cancel_pbut = new QPushButton("Cancel", this);
261 _ok_pbut = new QPushButton("OK", this);
262 _cancel_pbut->setDefault(true);
263 // At construction nothing has been entered, disable ok button
264 _ok_pbut->setEnabled(false);
265 connect(_ok_pbut, SIGNAL(released()), this, SLOT(accept()));
266 connect(_cancel_pbut, SIGNAL(released()), this, SLOT(reject()));
267
268
269 // Set up the list of inheritable contexts
270 _context_tree = new QTreeWidget(this);
271 _context_tree->setColumnCount(1);
272 _context_tree->setHeaderLabels(QStringList("Inherit from context:"));
273 QList<QTreeWidgetItem*> contexts;
274
275 // Get reference to the Editor
276 Editor* editor = static_cast<Editor*> (parent);
277
278 QStringList context_names = editor->_ed_scrollview->_map->context_names;
279 for (QStringList::Iterator qit = context_names.begin();
280 qit != context_names.end(); ++qit)
281 {
282 contexts.append(new QTreeWidgetItem((QTreeWidget*)0,
283 QStringList(*qit)));
284 } // loop through all files in the tileset directory
285 _context_tree->insertTopLevelItems(0, contexts);
286
287
288 // Add all of the aforementioned widgets into a nice-looking grid layout
289 _dia_layout = new QGridLayout(this);
290 _dia_layout->addWidget(_name_label, 0, 0);
291 _dia_layout->addWidget(_name_ledit, 0, 1);
292 _dia_layout->addWidget(_context_tree, 1, 1, 5, -1);
293 _dia_layout->addWidget(_cancel_pbut, 6, 0);
294 _dia_layout->addWidget(_ok_pbut, 6, 1);
295 } // ContextPropertiesDialog constructor
296
~ContextPropertiesDialog()297 ContextPropertiesDialog::~ContextPropertiesDialog()
298 {
299 delete _name_label;
300 delete _name_ledit;
301 delete _cancel_pbut;
302 delete _ok_pbut;
303 delete _context_tree;
304 delete _dia_layout;
305 } // ContextPropertiesDialog destructor
306
307
308
309 // ********** Private slot **********
310
_EnableOKButton()311 void ContextPropertiesDialog::_EnableOKButton()
312 {
313 // Disable the ok button if the line edit is empty.
314 // The default inheritable context is the base context.
315 if (_name_ledit->text() == "")
316 _ok_pbut->setEnabled(false);
317 else
318 _ok_pbut->setEnabled(true);
319 } // _EnableOKButton()
320
321