-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
186 lines (136 loc) · 4.23 KB
/
mainwindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include "mainwindow.h"
#include <QDebug>
#include <QPixmap>
#include <QLabel>
#include <QImage>
#include <QFileInfo>
#include <QFileDialog>
#include "color.h"
#include "spriteeditor.h"
#include "dialogs/newfile.h"
MainWindow::MainWindow(QWidget * parent)
: QMainWindow(parent)
{
ui.setupUi(this);
// openFile("dagr_2b.png");
connect(ui.action_New, SIGNAL(triggered()), this, SLOT(newFile()));
connect(ui.action_Open, SIGNAL(triggered()), this, SLOT(open()));
connect(ui.action_Save, SIGNAL(triggered()), this, SLOT(save()));
connect(ui.actionSave_as, SIGNAL(triggered()), this, SLOT(saveAs()));
connect(ui.actionQuit, SIGNAL(triggered()), this, SLOT(close()));
// configure color schemes
foreach (QString key, Color().keys())
ui.colorschemes->addItem(key);
palette = ui.colorschemes->itemText(0);
connect(ui.colorschemes, SIGNAL(currentIndexChanged(const QString &)),
this, SLOT(setColorPalette(const QString &)));
}
void MainWindow::open()
{
// QString dir = QDir(tabToolTip(currentIndex())).path();
QString fn = QFileDialog::getOpenFileName(this,
tr("Open File"), QDir::currentPath(), "PNG Files (*.png);;All Files (*)");
if (!fn.isEmpty())
openFile(fn);
}
void MainWindow::save()
{
QString fn = filename;
if (fn.isEmpty())
saveAs();
else
saveFile(fn);
}
void MainWindow::saveAs()
{
QString fn = filename;
if (fn.isEmpty())
fn = tr("Untitled.png");
fn = QFileDialog::getSaveFileName(this,
tr("Save File As..."),
QDir(fn).path(),
tr("PNG files (*.png)"));
if (fn.isEmpty())
return;
saveFile(fn);
}
void MainWindow::emptyFile()
{
closeFile();
SpriteEditor *editor = new SpriteEditor(this);
editor->setAttribute(Qt::WA_DeleteOnClose);
int index = ui.editor->addTab(editor, QString());
ui.editor->setCurrentIndex(index);
}
void MainWindow::newFile()
{
NewFile newfile;
if (!newfile.exec())
return;
emptyFile();
QPixmap pixmap(newfile.width(), newfile.height());
pixmap.fill();
SpriteEditor * editor = (SpriteEditor *) ui.editor->currentWidget();
editor->setPixmap(pixmap);
int index = ui.editor->currentIndex();
ui.editor->setTabIcon(index,QIcon(pixmap));
}
void MainWindow::saveFile(const QString & name)
{
if (!ui.editor->count())
return;
SpriteEditor * editor = (SpriteEditor *) ui.editor->currentWidget();
QImage img = editor->pixmap().toImage();
img.setText("LameGFX:framesize:w",QString::number(323));
img.setText("LameGFX:framesize:h",QString::number(111));
img.save(name);
}
void MainWindow::openFile(const QString & name)
{
if (!QFileInfo(name).exists())
{
qDebug() << "File does not exist";
return;
}
filename = name;
emptyFile();
int index = ui.editor->currentIndex();
SpriteEditor * editor = (SpriteEditor *) ui.editor->currentWidget();
QImage img(filename);
int framew = 0, frameh = 0;
QString fw = img.text("LameGFX:framesize:w");
QString fh = img.text("LameGFX:framesize:h");
if (!fw.isEmpty())
framew = fw.toInt();
if (!fh.isEmpty())
frameh = fh.toInt();
editor->setFrameSize(QSize(framew, frameh));
qDebug() << framew << frameh;
img = img.convertToFormat(QImage::Format_RGB32);
img = img.convertToFormat(QImage::Format_Indexed8, Color()["Plain"]);
img.setColorTable(Color()["Plain"]);
QPixmap pixmap = QPixmap::fromImage(img);
editor->setPixmap(pixmap);
ui.editor->setTabIcon(index,QIcon(pixmap));
}
void MainWindow::closeFile()
{
while (ui.editor->count() > 0)
{
SpriteEditor * editor = (SpriteEditor *) ui.editor->widget(0);
editor->disconnect();
editor->close();
ui.editor->removeTab(0);
}
}
void MainWindow::setColorPalette(const QString & name)
{
SpriteEditor * editor = (SpriteEditor *) ui.editor->currentWidget();
if (!editor) return;
QImage img = editor->pixmap().toImage();
img = img.convertToFormat(QImage::Format_Indexed8, Color()[palette]);
img.setColorTable(Color()[name]);
palette = name;
QPixmap pixmap = QPixmap::fromImage(img);
editor->setPixmap(pixmap);
}