~sithlord48/hyne/master

2 by myst6re
First commit
1
/****************************************************************************
2
 ** Hyne Final Fantasy VIII Save Editor
124 by myst6re
Merging Qt5 branch in trunk:
3
 ** Copyright (C) 2009-2013 Arzel Jérôme <myst6re@gmail.com>
2 by myst6re
First commit
4
 **
5
 ** This program is free software: you can redistribute it and/or modify
6
 ** it under the terms of the GNU General Public License as published by
7
 ** the Free Software Foundation, either version 3 of the License, or
8
 ** (at your option) any later version.
9
 **
10
 ** This program is distributed in the hope that it will be useful,
11
 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 ** GNU General Public License for more details.
14
 **
15
 ** You should have received a copy of the GNU General Public License
16
 ** along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 ****************************************************************************/
18
19
#include "HeaderDialog.h"
54 by myst6re
HeaderDialog: changing the "Save" button to "OK"
20
#include "Config.h"
2 by myst6re
First commit
21
22
HeaderDialog::HeaderDialog(SaveData *saveData, QWidget *parent, ViewType viewType)
23
	: QDialog(parent, Qt::Dialog | Qt::WindowCloseButtonHint), saveData(saveData), viewType(viewType)
24
{
124 by myst6re
Merging Qt5 branch in trunk:
25
	setWindowTitle(tr("Propriétés"));
2 by myst6re
First commit
26
27
	group1 = new QGroupBox(tr("Memory card header"), this);
28
29
	exists_lbl = new QLabel(tr("Existe :"));
30
	exists_lbl->setTextFormat(Qt::PlainText);
31
	exists = new QCheckBox;
32
124 by myst6re
Merging Qt5 branch in trunk:
33
	QLabel *country_lbl = new QLabel(tr("Région :"));
2 by myst6re
First commit
34
	country_lbl->setTextFormat(Qt::PlainText);
35
	country = new QComboBox;
36
	country->addItem(QIcon(":/images/jp.png"), tr("Japon"), COUNTRY_JP);
124 by myst6re
Merging Qt5 branch in trunk:
37
	country->addItem(QIcon(":/images/us.png"), tr("Amérique"), COUNTRY_US);
2 by myst6re
First commit
38
	country->addItem(QIcon(":/images/eu.png"), tr("Europe"), COUNTRY_EU);
39
	country->addItem(tr("Invalide"), '\x00');
40
102 by myst6re
Using an enumeration to list all config keys.
41
	country->setCurrentIndex(Config::valueVar(Config::LastCountry).toInt());
2 by myst6re
First commit
42
43
	QLabel *code_lbl = new QLabel(tr("Code :"));
44
	code_lbl->setTextFormat(Qt::PlainText);
45
	code = new QComboBox;
46
	code->setEditable(true);
47
	code->lineEdit()->setMaxLength(10);
48
	fillCode(code);
49
102 by myst6re
Using an enumeration to list all config keys.
50
	QString lastGameCode = Config::value(Config::LastGameCode);
2 by myst6re
First commit
51
	if(!lastGameCode.isEmpty()) {
52
		setCode(lastGameCode);
53
	}
54
55
	id_lbl = new QLabel(tr("Identifiant :"));
56
	id_lbl->setTextFormat(Qt::PlainText);
57
	id = new QComboBox;
58
	id->setEditable(true);
59
	id->lineEdit()->setMaxLength(8);
50 by myst6re
Save Header: Using the current save id in the identifier list.
60
	fillId(id, saveData->id());
2 by myst6re
First commit
61
62
	QGridLayout *layout1 = new QGridLayout(group1);
63
	layout1->addWidget(exists_lbl, 0, 0);
64
	layout1->addWidget(exists, 0, 1);
65
	layout1->addWidget(country_lbl, 1, 0);
66
	layout1->addWidget(country, 1, 1);
67
	layout1->addWidget(code_lbl, 2, 0);
68
	layout1->addWidget(code, 2, 1);
69
	layout1->addWidget(id_lbl, 3, 0);
70
	layout1->addWidget(id, 3, 1);
71
72
	group2 = new QGroupBox(tr("Save header"), this);
73
58 by myst6re
Editors:
74
	desc = new QLineEdit();
75
	desc->setMaxLength(64);
76
77
	desc_auto = new QCheckBox(tr("Auto."));
2 by myst6re
First commit
78
124 by myst6re
Merging Qt5 branch in trunk:
79
	QLabel *bloc_lbl = new QLabel(tr("Blocs utilisés :"));
2 by myst6re
First commit
80
	bloc = new QLabel();
81
	bloc->setTextFormat(Qt::PlainText);
82
83
	QLabel *desc_lbl = new QLabel(tr("Description :"));
84
	desc_lbl->setTextFormat(Qt::PlainText);
85
27 by myst6re
Icon editor: can replace an icon by another from ff8.
86
	QList<SaveIconData> saveIcons;
87
	for(int i=0 ; i<16 ; ++i) {
88
		QFile iconFile(QString(":/data/icon%1.psico").arg(i));
89
		if(iconFile.open(QIODevice::ReadOnly)) {
90
			saveIcons.append(SaveIconData(iconFile.readAll()));
91
			iconFile.close();
92
		}
93
	}
94
124 by myst6re
Merging Qt5 branch in trunk:
95
	QLabel *icon1_lbl = new QLabel(tr("Icône :"));
2 by myst6re
First commit
96
	icon1_lbl->setTextFormat(Qt::PlainText);
27 by myst6re
Icon editor: can replace an icon by another from ff8.
97
	icon1 = new QComboBox();
98
	icon1->setIconSize(QSize(16, 16));
99
	foreach(const SaveIconData &saveIcon, saveIcons) {
100
		icon1->addItem(QIcon(saveIcon.icon()), QString(), saveIcon.data().left(160));
101
	}
2 by myst6re
First commit
102
	QPushButton *icon1_saveButton = new QPushButton();
103
	QIcon saveIcon(QApplication::style()->standardIcon(QStyle::SP_DialogSaveButton));
104
	icon1_saveButton->setIcon(saveIcon);
105
	icon1_saveButton->setFlat(true);
124 by myst6re
Merging Qt5 branch in trunk:
106
	icon2_lbl = new QLabel(tr("Icône additionnel :"));
2 by myst6re
First commit
107
	icon2_lbl->setTextFormat(Qt::PlainText);
27 by myst6re
Icon editor: can replace an icon by another from ff8.
108
	icon2 = new QComboBox();
109
	icon2->setIconSize(QSize(32, 32));
110
	foreach(const SaveIconData &saveIcon, saveIcons) {
111
		icon2->addItem(QIcon(saveIcon.icon(0, true)), QString(), saveIcon.data().mid(160));
112
	}
2 by myst6re
First commit
113
	icon2_saveButton = new QPushButton();
114
	icon2_saveButton->setIcon(saveIcon);
115
	icon2_saveButton->setFlat(true);
116
117
	QGridLayout *layout2 = new QGridLayout(group2);
118
	layout2->addWidget(bloc_lbl, 0, 0);
119
	layout2->addWidget(bloc, 0, 1, 1, 2);
120
	layout2->addWidget(desc_lbl, 1, 0);
121
	layout2->addWidget(desc, 1, 1, 1, 2);
58 by myst6re
Editors:
122
	layout2->addWidget(desc_auto, 1, 3);
2 by myst6re
First commit
123
	layout2->addWidget(icon1_lbl, 2, 0);
124
	layout2->addWidget(icon1, 2, 1);
125
	layout2->addWidget(icon1_saveButton, 2, 2, Qt::AlignRight);
126
	layout2->addWidget(icon2_lbl, 3, 0);
127
	layout2->addWidget(icon2, 3, 1);
128
	layout2->addWidget(icon2_saveButton, 3, 2, Qt::AlignRight);
129
54 by myst6re
HeaderDialog: changing the "Save" button to "OK"
130
	QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Close);
25 by myst6re
Use my own function to retrieve the ff8 path from the register (to prevent the creation of the key if it does not exists).
131
132
	QGridLayout *layout = new QGridLayout(this);
133
	layout->addWidget(group1, 0, 0);
134
	layout->addWidget(group2, 1, 0);
135
	layout->setRowStretch(2, 1);
136
	layout->addWidget(buttonBox, 3, 0);
137
138
	connect(buttonBox, SIGNAL(accepted()), SLOT(accept()));
139
	connect(buttonBox, SIGNAL(rejected()), SLOT(reject()));
2 by myst6re
First commit
140
	connect(icon1_saveButton, SIGNAL(released()), SLOT(saveIcon1()));
141
	connect(icon2_saveButton, SIGNAL(released()), SLOT(saveIcon2()));
58 by myst6re
Editors:
142
	connect(desc_auto, SIGNAL(toggled(bool)), desc, SLOT(setDisabled(bool)));
2 by myst6re
First commit
143
144
	fill();
145
}
146
147
void HeaderDialog::fillCode(QComboBox *code)
148
{
149
	QIcon jp(":/images/jp.png");
150
	code->addItem(jp, "SLPSP01880");//japanese
151
	code->addItem(jp, "SCPSP45375");//japanese (???)
152
	code->addItem(jp, "SLPMP87384");//japanese (Ultimate Hits)
153
	code->addItem(QIcon(":/images/us.png"), "SLUSP00892");//american
154
	code->addItem(QIcon(":/images/uk.png"), "SLESP02080");//english
155
	code->addItem(QIcon(":/images/fr.png"), "SLESP02081");//french
156
	code->addItem(QIcon(":/images/de.png"), "SLESP02082");//german
157
	code->addItem(QIcon(":/images/it.png"), "SLESP02083");//italian
158
	code->addItem(QIcon(":/images/sp.png"), "SLESP02084");//spanish
159
}
160
50 by myst6re
Save Header: Using the current save id in the identifier list.
161
void HeaderDialog::fillId(QComboBox *id, int saveId)
2 by myst6re
First commit
162
{
50 by myst6re
Save Header: Using the current save id in the identifier list.
163
	id->addItem(QIcon(":/images/jp.png"), QString("FF08%1").arg(saveId, 2, 10, QChar('0')));
164
	id->addItem(QIcon(":/images/us.png"), QString("0426%1").arg(saveId, 2, 10, QChar('0')));
165
	id->addItem(QIcon(":/images/eu.png"), QString("0520%1").arg(saveId, 2, 10, QChar('0')));
2 by myst6re
First commit
166
}
167
168
void HeaderDialog::fill()
169
{
23 by myst6re
save2PSV function (experimental for now)
170
	setWindowIcon(QIcon(saveData->saveIcon().icon()));
2 by myst6re
First commit
171
172
	if(viewType == NormalView && !saveData->hasMCHeader()) {
173
		group1->hide();
174
	}
175
	else {
176
		// Fill group1 (MCHeader)
177
178
		if(viewType != NormalView) {
179
			exists->setChecked(true);
180
		}
181
		else {
182
			exists->setChecked(!saveData->isDelete());
183
			setId(saveData->MCHeaderId());
184
		}
185
186
		if(viewType != CreateView || saveData->hasMCHeader()) {
187
			setCountry(saveData->MCHeaderCountry());
188
			setCode(saveData->MCHeaderCode());
189
		}
190
	}
191
54 by myst6re
HeaderDialog: changing the "Save" button to "OK"
192
	if(!saveData->hasExistsInfos()) {
2 by myst6re
First commit
193
		exists->hide();
194
		exists_lbl->hide();
195
	}
196
197
	if(viewType == RestoreView || viewType == CreateView) {
198
		group2->hide();
199
200
		exists->hide();
201
		exists_lbl->hide();
202
		id_lbl->hide();
203
		id->hide();
204
		country->setMaxCount(3);
205
	}
206
	else {
207
		// Fill group2 (SC_header)
208
58 by myst6re
Editors:
209
		desc->setText(saveData->shortDescription());
210
		desc_auto->setChecked(saveData->isDescriptionAuto());
211
		desc_auto->setVisible(saveData->isFF8());
54 by myst6re
HeaderDialog: changing the "Save" button to "OK"
212
		bloc->setText(QString::number(saveData->blockCount()));
27 by myst6re
Icon editor: can replace an icon by another from ff8.
213
		QImage pix = saveData->saveIcon().icon().toImage();
214
		int currentIndex = -1;
215
		for(int i=0 ; i<icon1->count() ; ++i) {
216
			if(icon1->itemIcon(i).pixmap(16).toImage() == pix) {
217
				currentIndex = i;
218
				break;
219
			}
220
		}
221
		if(currentIndex == -1) {
222
			icon1->addItem(QIcon(QPixmap::fromImage(pix)), QString(), QByteArray());
223
			currentIndex = icon1->count()-1;
224
		}
225
		icon1->setCurrentIndex(currentIndex);
2 by myst6re
First commit
226
		if(saveData->isFF8()) {
27 by myst6re
Icon editor: can replace an icon by another from ff8.
227
			pix = saveData->saveIcon().icon(0, true).toImage();
228
			currentIndex = -1;
229
			for(int i=0 ; i<icon2->count() ; ++i) {
230
				if(icon2->itemIcon(i).pixmap(32).toImage() == pix) {
231
					currentIndex = i;
232
					break;
233
				}
234
			}
235
			if(currentIndex == -1) {
236
				icon2->addItem(QIcon(QPixmap::fromImage(pix)), QString(), QByteArray());
237
				currentIndex = icon1->count()-1;
238
			}
239
			icon2->setCurrentIndex(currentIndex);
2 by myst6re
First commit
240
		}
241
		else {
242
			icon2_lbl->hide();
243
			icon2->hide();
244
			icon2_saveButton->hide();
245
		}
246
	}
247
}
248
249
void HeaderDialog::setCountry(char c)
250
{
251
	int index;
252
	if((index=country->findData(c)) != -1)
253
	{
254
		country->setCurrentIndex(index);
255
		if(index != 3)		country->setMaxCount(3);
256
	}
257
	else
258
	{
259
		if(viewType == RestoreView) {
260
			country->setCurrentIndex(0);
261
		}
262
		else {
263
			country->setItemData(3, c);
264
			country->setCurrentIndex(3);//Invalid
265
		}
266
	}
267
}
268
269
void HeaderDialog::setCode(const QString &codestr)
270
{
271
	int index;
272
	if(codestr.size()==10 && (index=code->findText(codestr)) != -1)
273
	{
274
		code->setCurrentIndex(index);
275
	}
276
	else if(viewType == NormalView) {
277
		code->insertItem(0, codestr);
278
		code->setCurrentIndex(0);
279
	}
280
}
281
282
void HeaderDialog::setId(const QString &idStr)
283
{
284
	if(idStr.startsWith("FF08")) {
285
		id->setCurrentIndex(0);
286
	} else if(idStr.startsWith("0426")) {
287
		id->setCurrentIndex(1);
288
	} else if(idStr.startsWith("0520")) {
289
		id->setCurrentIndex(2);
290
	} else {
291
		id->addItem(idStr);
292
		id->setCurrentIndex(id->count()-1);
293
	}
294
	id->lineEdit()->setText(idStr);
295
}
296
25 by myst6re
Use my own function to retrieve the ff8 path from the register (to prevent the creation of the key if it does not exists).
297
void HeaderDialog::accept()
2 by myst6re
First commit
298
{
27 by myst6re
Icon editor: can replace an icon by another from ff8.
299
	if(viewType != NormalView || saveData->hasMCHeader()) {
102 by myst6re
Using an enumeration to list all config keys.
300
		Config::setValue(Config::LastCountry, country->currentIndex());
301
		Config::setValue(Config::LastGameCode, code->currentText());
27 by myst6re
Icon editor: can replace an icon by another from ff8.
302
303
		// Edit MC Header
304
		saveData->setMCHeader(exists->isChecked(),
305
							  country->itemData(country->currentIndex()).toInt(),
306
							  code->currentText(),
307
							  id->isVisible() ? id->currentText() : QString());
308
	}
309
58 by myst6re
Editors:
310
	// Edit short description
311
	saveData->setShortDescription(desc->text());
312
	saveData->setDescriptionAuto(desc_auto->isChecked());
313
27 by myst6re
Icon editor: can replace an icon by another from ff8.
314
	// Edit icon 1
315
	SaveIconData saveIcon = saveData->saveIcon();
316
	QByteArray itemData = icon1->itemData(icon1->currentIndex()).toByteArray();
317
	if(!itemData.isEmpty()) {
318
		saveIcon.setData(itemData + saveIcon.data().mid(itemData.size()));
319
		saveData->setSaveIcon(saveIcon);
320
	}
321
322
	if(saveData->isFF8()) {
323
		// Edit icon 2
324
		itemData = icon2->itemData(icon2->currentIndex()).toByteArray();
325
		if(!itemData.isEmpty()) {
326
			saveIcon.setData(saveIcon.data().left(160) + itemData);
327
			saveData->setSaveIcon(saveIcon);
328
		}
329
	}
2 by myst6re
First commit
330
25 by myst6re
Use my own function to retrieve the ff8 path from the register (to prevent the creation of the key if it does not exists).
331
	QDialog::accept();
2 by myst6re
First commit
332
}
333
334
void HeaderDialog::saveIcon(bool chocobo_world_icon)
335
{
102 by myst6re
Using an enumeration to list all config keys.
336
	QString path, savePathIcon = Config::value(Config::SavePathIcon);
337
338
	path = savePathIcon.isEmpty() ? QString() : savePathIcon % "/";
2 by myst6re
First commit
339
	path = QFileDialog::getSaveFileName(this, tr("Enregistrer sous"), path % QString("icon%1%2.png").arg(saveData->id()+1).arg(chocobo_world_icon ? "b" : ""), tr("Image PNG (*.png);;Image JPG (*.jpg *.jpeg);;Image BMP (*.bmp)"));
340
	if(path.isEmpty())		return;
341
342
	int index = path.lastIndexOf('/');
102 by myst6re
Using an enumeration to list all config keys.
343
	Config::setValue(Config::SavePathIcon, index == -1 ? path : path.left(index));
2 by myst6re
First commit
344
102 by myst6re
Using an enumeration to list all config keys.
345
	if(!saveData->saveIcon().icon(0, chocobo_world_icon).save(path)) {
2 by myst6re
First commit
346
		QMessageBox::warning(this, tr("Erreur"), tr("Format incorrect."));
102 by myst6re
Using an enumeration to list all config keys.
347
	}
2 by myst6re
First commit
348
}