Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to save/load palette #194

Merged
merged 1 commit into from
Aug 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion src/ui-hlp/menu.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#include <cerrno>
#include <cstring>
#include <cstdlib>
#include <QFile>
#include <QTextStream>
#include <QMessageBox>
#include <QSettings>
#include <algorithm>

#include "filter.h"
#include "config.h"
Expand Down Expand Up @@ -285,6 +288,8 @@ void uih_registermenudialogs_i18n(void)

Register(palettepickerdialog);
DIALOGPALPICKER_I("Palette:", 0);
DIALOGIFILE_I(TR("Dialog", "Load Palette File:"), "file*.gpl");
DIALOGOFILE_I(TR("Dialog", "Save Palette File:"), 0);
NULL_I();

Register(uih_cyclingdialog);
Expand Down Expand Up @@ -744,6 +749,38 @@ static void uih_palettegradient(struct uih_context *uih, dialogparam *p)

static void uih_palettepicker(struct uih_context *uih, dialogparam *p)
{
QFile *loadfile = new QFile(p[1].dstring);
QFile *savefile = NULL;
if (strlen(p[2].dstring) > 1)
savefile = new QFile(p[2].dstring);

unsigned char colors[31][3];
memset(colors, 0, sizeof (colors));

if (loadfile->open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream in(loadfile);
QStringList colorvals= in.readAll().split("\n");
for(int i=0; i < std::min(31, (int)colorvals.size()); i++) {
QStringList currcolors = colorvals[i].split(QRegExp("\\s+"));
if(currcolors.size() != 3) {
uih_error(uih, "Corrupted Color File");
return;
}
colors[i][0] = std::min(currcolors[0].toInt(), 255);
colors[i][1] = std::min(currcolors[1].toInt(), 255);
colors[i][2] = std::min(currcolors[2].toInt(), 255);
}
mkcustompalette(uih->palette, colors);
loadfile->close();

} else if(savefile && savefile->open(QIODevice::WriteOnly | QIODevice::Text)) {
getDEFSEGMENTColor(colors);
QTextStream stream(savefile);
for(int i=0; i < 31; i++){
stream << (int)colors[i][0] << " " << (int)colors[i][1] << " " << (int)colors[i][2] << "\n";
}
savefile->close();
}
uih_newimage(uih);
}

Expand Down Expand Up @@ -1208,7 +1245,7 @@ void uih_registermenus_i18n(void)
0, uih_palette, uih_getpalettedialog); //This is a placeholder menu
MENUCDIALOG_I("palettemenu", NULL, TR("Menu", "Custom palette"), "palettegradient",
0, uih_palettegradient, uih_getpalettegradientdialog);
MENUCDIALOG_I("palettemenu", NULL, TR("Menu", "Palette Picker"), "palettepicker",
MENUCDIALOG_I("palettemenu", "x", TR("Menu", "Palette Picker"), "palettepicker",
0, uih_palettepicker, uih_palettepickerdialog);
MENUSEPARATOR_I("palettemenu");
MENUNOPCB_I("palettemenu", "y", TR("Menu", "Color cycling"), "cycling", 0,
Expand Down
11 changes: 9 additions & 2 deletions src/ui/customdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,15 @@ void CustomDialog::chooseInputFile()

QSettings settings;
QString fileLocation = settings.value("MainWindow/lastFileLocation", QDir::homePath()).toString();
QString fileName = QFileDialog::getOpenFileName(
this, sender()->objectName(), fileLocation, "*.xpf *.png *.xaf");
QString fileName;

if(sender()->objectName() == "Load Palette File:") {
fileName = QFileDialog::getOpenFileName(
this, sender()->objectName(), fileLocation, "*.gpl");
} else {
fileName = QFileDialog::getOpenFileName(
this, sender()->objectName(), fileLocation, "*.xpf *.png *.xaf");
}
if (!fileName.isNull()) {
field->setText(fileName);
settings.setValue("MainWindow/lastFileLocation", QFileInfo(fileName).absolutePath());
Expand Down