- using .gpl file format config filewilder/unmaintained
parent
f791e02ed1
commit
f3d406beb7
22 changed files with 307 additions and 280 deletions
@ -1,34 +0,0 @@ |
||||
#include "CustomizeableColorList.h" |
||||
|
||||
#include <utility> |
||||
|
||||
#include <config.h> |
||||
|
||||
#include "i18n.h" |
||||
|
||||
CustomizeableColorList::CustomizeableColorList() { |
||||
this->addPredefinedColor(0x000000U, _("Black")); |
||||
this->addPredefinedColor(0x008000U, _("Green")); |
||||
this->addPredefinedColor(0x00c0ffU, _("Light Blue")); |
||||
this->addPredefinedColor(0x00ff00U, _("Light Green")); |
||||
this->addPredefinedColor(0x3333ccU, _("Blue")); |
||||
this->addPredefinedColor(0x808080U, _("Gray")); |
||||
this->addPredefinedColor(0xff0000U, _("Red")); |
||||
this->addPredefinedColor(0xff00ffU, _("Magenta")); |
||||
this->addPredefinedColor(0xff8000U, _("Orange")); |
||||
this->addPredefinedColor(0xffff00U, _("Yellow")); |
||||
this->addPredefinedColor(0xffffffU, _("White")); |
||||
} |
||||
|
||||
CustomizeableColorList::~CustomizeableColorList() { |
||||
for (XojColor* c: this->colors) { |
||||
delete c; |
||||
} |
||||
this->colors.clear(); |
||||
} |
||||
|
||||
auto CustomizeableColorList::getPredefinedColors() -> vector<XojColor*>* { return &this->colors; } |
||||
|
||||
void CustomizeableColorList::addPredefinedColor(Color color, string name) { |
||||
this->colors.push_back(new XojColor(color, std::move(name))); |
||||
} |
||||
@ -1,34 +0,0 @@ |
||||
/*
|
||||
* Xournal++ |
||||
* |
||||
* List of unused colors for toolbar customisation |
||||
* |
||||
* @author Xournal++ Team |
||||
* https://github.com/xournalpp/xournalpp
|
||||
* |
||||
* @license GNU GPLv2 or later |
||||
*/ |
||||
|
||||
#pragma once |
||||
|
||||
#include <string> |
||||
#include <vector> |
||||
|
||||
#include "gui/XojColor.h" |
||||
|
||||
#include "XournalType.h" |
||||
|
||||
class CustomizeableColorList { |
||||
public: |
||||
CustomizeableColorList(); |
||||
virtual ~CustomizeableColorList(); |
||||
|
||||
public: |
||||
vector<XojColor*>* getPredefinedColors(); |
||||
|
||||
private: |
||||
void addPredefinedColor(Color color, string name); |
||||
|
||||
private: |
||||
vector<XojColor*> colors; |
||||
}; |
||||
@ -0,0 +1,132 @@ |
||||
#include "ColorPalette.h" |
||||
|
||||
#include <fstream> |
||||
#include <limits> |
||||
#include <sstream> |
||||
|
||||
#include <gtk/gtk.h> |
||||
#include <util/StringUtils.h> |
||||
|
||||
Palette::Palette(fs::path path): filepath{std::move(path)}, header{}, colors{}, next{0} {} |
||||
|
||||
void Palette::load() { |
||||
std::ifstream myfile{filepath}; |
||||
std::string line; |
||||
Header h; |
||||
|
||||
if (myfile.is_open()) { |
||||
getline(myfile, line); |
||||
line = StringUtils::trim(line); |
||||
if (line != "GIMP Palette") { |
||||
g_error(".gpt file needs to start with \"GIMP Palette\" in the " |
||||
"first line"); |
||||
} |
||||
while (getline(myfile, line)) { |
||||
std::istringstream iss{line}; |
||||
if (iss >> h) { |
||||
header[h.attribute] = h.value; |
||||
} else { |
||||
NamedColor color; |
||||
std::istringstream iss2{line}; |
||||
if (iss2 >> color) { |
||||
colors.push_back(color); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
NamedColor Palette::getNext(int inc) { |
||||
next += inc; |
||||
if (next >= colors.size()) { |
||||
next = 0; |
||||
g_warning("There are more Coloritems in the Toolbar then your Palette defines.\n" |
||||
"Hence, cycling through palette from the beginning."); |
||||
} |
||||
return colors.at(next); |
||||
} |
||||
|
||||
void Palette::reset() { next = 0; }; |
||||
|
||||
void Palette::create_default(fs::path filepath) { |
||||
std::ofstream myfile{filepath}; |
||||
myfile << "GIMP Palette" |
||||
<< "\n"; |
||||
myfile << "Name: Xournal Default Palette" |
||||
<< "\n"; |
||||
myfile << "#" |
||||
<< "\n"; |
||||
myfile << 0 << " " << 0 << " " << 0 << " " |
||||
<< "Black" |
||||
<< "\n"; |
||||
myfile << 0 << " " << 128 << " " << 0 << " " |
||||
<< "Green" |
||||
<< "\n"; |
||||
myfile << 0 << " " << 192 << " " << 255 << " " |
||||
<< "Light Blue" |
||||
<< "\n"; |
||||
myfile << 0 << " " << 255 << " " << 0 << " " |
||||
<< "Light Green" |
||||
<< "\n"; |
||||
myfile << 51 << " " << 51 << " " << 204 << " " |
||||
<< "Blue" |
||||
<< "\n"; |
||||
myfile << 128 << " " << 128 << " " << 128 << " " |
||||
<< "Gray" |
||||
<< "\n"; |
||||
myfile << 255 << " " << 0 << " " << 0 << " " |
||||
<< "Red" |
||||
<< "\n"; |
||||
myfile << 255 << " " << 0 << " " << 255 << " " |
||||
<< "Magenta" |
||||
<< "\n"; |
||||
myfile << 255 << " " << 128 << " " << 0 << " " |
||||
<< "Orange" |
||||
<< "\n"; |
||||
myfile << 255 << " " << 255 << " " << 0 << " " |
||||
<< "Yellow" |
||||
<< "\n"; |
||||
myfile << 255 << " " << 255 << " " << 255 << " " |
||||
<< "White" |
||||
<< "\n"; |
||||
} |
||||
|
||||
std::istream& operator>>(std::istream& str, Header& data) { |
||||
std::string line; |
||||
Header tmp; |
||||
if (std::getline(str, line)) { |
||||
std::istringstream iss{line}; |
||||
if (getline(iss, tmp.attribute, ':') && getline(iss, tmp.value, '\n')) { |
||||
tmp.attribute = StringUtils::trim(tmp.attribute); |
||||
tmp.value = StringUtils::trim(tmp.value); |
||||
|
||||
/* OK: All read operations worked */ |
||||
data.swap(tmp); // C++03 as this answer was written a long time ago.
|
||||
} else { |
||||
// One operation failed.
|
||||
// So set the state on the main stream
|
||||
// to indicate failure.
|
||||
str.setstate(std::ios::failbit); |
||||
} |
||||
} |
||||
return str; |
||||
} |
||||
|
||||
void Header::swap(Header& other) // C++03 as this answer was written a long time ago.
|
||||
{ |
||||
std::swap(attribute, other.attribute); |
||||
std::swap(value, other.value); |
||||
} |
||||
|
||||
// Default xournal
|
||||
// 0 0 0 Black
|
||||
// 0 128 0 Green
|
||||
// 0 192 255 Light Blue
|
||||
// 0 255 0 Light Green
|
||||
// 51 51 204 Blue
|
||||
// 128 128 128 Gray
|
||||
// 255 0 0 Red
|
||||
// 255 0 255 Magenta
|
||||
// 255 128 0 Orange
|
||||
// 255 255 0 Yellow
|
||||
// 255 255 255 White
|
||||
@ -0,0 +1,35 @@ |
||||
#pragma once |
||||
|
||||
#include <map> |
||||
#include <string> |
||||
#include <vector> |
||||
|
||||
#include <filesystem.h> |
||||
#include <util/NamedColor.h> |
||||
|
||||
struct Header { |
||||
// inspired by
|
||||
// https://codereview.stackexchange.com/questions/38879/parsing-text-file-in-c
|
||||
std::string attribute; |
||||
std::string value; |
||||
|
||||
friend std::istream& operator>>(std::istream& str, Header& data); |
||||
|
||||
void swap(Header& other); |
||||
}; |
||||
|
||||
struct Palette { |
||||
fs::path filepath; |
||||
int next; |
||||
|
||||
std::map<std::string, std::string> header; |
||||
std::vector<NamedColor> colors; |
||||
|
||||
Palette(fs::path path); |
||||
|
||||
NamedColor getNext(int inc = 1); |
||||
void load(); |
||||
void reset(); |
||||
|
||||
static void create_default(fs::path path); |
||||
}; |
||||
@ -1,105 +0,0 @@ |
||||
#include "ToolbarColorNames.h" |
||||
|
||||
#include <fstream> |
||||
|
||||
#include <glib/gstdio.h> |
||||
|
||||
#include "i18n.h" |
||||
|
||||
ToolbarColorNames::ToolbarColorNames() { |
||||
this->config = g_key_file_new(); |
||||
g_key_file_set_string(this->config, "info", "about", "Xournalpp custom color names"); |
||||
initPredefinedColors(); |
||||
} |
||||
|
||||
ToolbarColorNames::~ToolbarColorNames() { |
||||
g_key_file_free(this->config); |
||||
} |
||||
|
||||
static ToolbarColorNames* instance = nullptr; |
||||
|
||||
auto ToolbarColorNames::getInstance() -> ToolbarColorNames& { |
||||
if (instance == nullptr) { |
||||
instance = new ToolbarColorNames(); |
||||
} |
||||
|
||||
return *instance; |
||||
} |
||||
|
||||
void ToolbarColorNames::freeInstance() { |
||||
delete instance; |
||||
instance = nullptr; |
||||
} |
||||
|
||||
void ToolbarColorNames::loadFile(fs::path const& file) { |
||||
GError* error = nullptr; |
||||
if (!g_key_file_load_from_file(config, file.u8string().c_str(), G_KEY_FILE_NONE, &error)) { |
||||
g_warning("Failed to load \"colornames.ini\" (%s): %s\n", file.string().c_str(), error->message); |
||||
g_error_free(error); |
||||
return; |
||||
} |
||||
|
||||
g_key_file_set_string(this->config, "info", "about", "Xournalpp custom color names"); |
||||
} |
||||
|
||||
void ToolbarColorNames::saveFile(fs::path const& file) { |
||||
gsize len = 0; |
||||
char* data = g_key_file_to_data(this->config, &len, nullptr); |
||||
try { |
||||
std::ofstream ofs{file, std::ios::binary}; |
||||
ofs.exceptions(std::ios::badbit | std::ios::failbit); |
||||
ofs.write(data, len); |
||||
} catch (std::ios_base::failure const& e) { |
||||
g_warning("Could not save color file «%s».\n%s with error code %d", file.u8string().c_str(), e.what(), |
||||
e.code().value()); |
||||
} |
||||
g_free(data); |
||||
} |
||||
|
||||
void ToolbarColorNames::addColor(Color color, const std::string& name, bool predefined) { |
||||
if (predefined) { |
||||
this->predefinedColorNames[color] = name; |
||||
} else { |
||||
char colorHex[16]; |
||||
sprintf(colorHex, "%06x", uint32_t{color}); |
||||
g_key_file_set_string(this->config, "custom", colorHex, name.c_str()); |
||||
} |
||||
} |
||||
|
||||
auto ToolbarColorNames::getColorName(Color color) -> std::string { |
||||
char colorHex[16]; |
||||
sprintf(colorHex, "%06x", uint32_t{color}); |
||||
|
||||
std::string colorName; |
||||
char* name = g_key_file_get_string(this->config, "custom", colorHex, nullptr); |
||||
if (name != nullptr) { |
||||
colorName = name; |
||||
g_free(name); |
||||
} |
||||
|
||||
if (!colorName.empty()) { |
||||
return colorName; |
||||
} |
||||
|
||||
if (auto iter = this->predefinedColorNames.find(color); iter != end(this->predefinedColorNames)) { |
||||
return iter->second; |
||||
} |
||||
|
||||
return colorHex; |
||||
} |
||||
|
||||
void ToolbarColorNames::initPredefinedColors() { |
||||
// Here you can add predefined color names
|
||||
// this ordering fixes #2
|
||||
addColor(0x000000U, _("Black"), true); |
||||
addColor(0x008000U, _("Green"), true); |
||||
addColor(0x00c0ffU, _("Light Blue"), true); |
||||
addColor(0x00ff00U, _("Light Green"), true); |
||||
addColor(0x3333ccU, _("Blue"), true); |
||||
addColor(0x808080U, _("Gray"), true); |
||||
addColor(0xff0000U, _("Red"), true); |
||||
addColor(0xff00ffU, _("Magenta"), true); |
||||
addColor(0xff8000U, _("Orange"), true); |
||||
addColor(0xffff00U, _("Yellow"), true); |
||||
addColor(0xffffffU, _("White"), true); |
||||
} |
||||
@ -1,45 +0,0 @@ |
||||
/*
|
||||
* Xournal++ |
||||
* |
||||
* Names for the toolbar color items (e.g. 0xff000 is called red) |
||||
* Singleton |
||||
* |
||||
* @author Xournal++ Team |
||||
* https://github.com/xournalpp/xournalpp
|
||||
* |
||||
* @license GNU GPLv2 or later |
||||
*/ |
||||
|
||||
#pragma once |
||||
|
||||
#include <string> |
||||
#include <unordered_map> |
||||
|
||||
#include "util/Color.h" |
||||
|
||||
#include "filesystem.h" |
||||
|
||||
class ToolbarColorNames { |
||||
private: |
||||
ToolbarColorNames(); |
||||
virtual ~ToolbarColorNames(); |
||||
|
||||
public: |
||||
static ToolbarColorNames& getInstance(); |
||||
static void freeInstance(); |
||||
|
||||
public: |
||||
void loadFile(fs::path const& file); |
||||
void saveFile(fs::path const& file); |
||||
|
||||
void addColor(Color color, const std::string& name, bool predefined); |
||||
|
||||
std::string getColorName(Color color); |
||||
|
||||
private: |
||||
void initPredefinedColors(); |
||||
|
||||
private: |
||||
GKeyFile* config; |
||||
std::unordered_map<Color, std::string> predefinedColorNames{}; |
||||
}; |
||||
@ -0,0 +1,33 @@ |
||||
#include "NamedColor.h" |
||||
|
||||
#include <sstream> |
||||
|
||||
#include <util/StringUtils.h> |
||||
|
||||
std::istream& operator>>(std::istream& str, NamedColor& data) { |
||||
std::string line; |
||||
NamedColor tmp; |
||||
if (std::getline(str, line)) { |
||||
std::istringstream iss{line}; |
||||
if (iss >> tmp.color.red >> tmp.color.blue >> tmp.color.green && std::getline(iss, tmp.name)) { |
||||
tmp.name = StringUtils::trim(tmp.name); |
||||
|
||||
/* OK: All read operations worked */ |
||||
data.swap(tmp); // C++03 as this answer was written a long time ago.
|
||||
} else { |
||||
// One operation failed.
|
||||
// So set the state on the main stream
|
||||
// to indicate failure.
|
||||
str.setstate(std::ios::failbit); |
||||
} |
||||
} |
||||
return str; |
||||
} |
||||
void NamedColor::swap(NamedColor& other) // C++03 as this answer was written a long time ago.
|
||||
{ |
||||
std::swap(color.red, other.color.red); |
||||
std::swap(color.green, other.color.green); |
||||
std::swap(color.blue, other.color.blue); |
||||
std::swap(color.alpha, other.color.alpha); |
||||
std::swap(name, other.name); |
||||
} |
||||
@ -0,0 +1,11 @@ |
||||
#include <string> |
||||
|
||||
#include <util/Color.h> |
||||
|
||||
struct NamedColor { |
||||
std::string name; |
||||
ColorU16 color; |
||||
|
||||
friend std::istream& operator>>(std::istream& str, NamedColor& data); |
||||
void swap(NamedColor& other); |
||||
}; |
||||
Loading…
Reference in new issue