Skip to content

Commit

Permalink
Read color schemes from additional user config path.
Browse files Browse the repository at this point in the history
  • Loading branch information
t-paul committed Nov 1, 2014
1 parent 42fab36 commit 7c3077b
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 10 deletions.
5 changes: 5 additions & 0 deletions src/PlatformUtils-mac.mm
Expand Up @@ -11,5 +11,10 @@
return std::string([[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] UTF8String]);
}

std::string PlatformUtils::userConfigPath()
{
return "";
}

void PlatformUtils::ensureStdIO(void) {}

22 changes: 22 additions & 0 deletions src/PlatformUtils-posix.cc
Expand Up @@ -19,5 +19,27 @@ std::string PlatformUtils::documentsPath()
}
}

// see http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
std::string PlatformUtils::userConfigPath()
{
fs::path config_path;

const char *xdg_env = getenv("XDG_CONFIG_HOME");
if (xdg_env && fs::exists(fs::path(xdg_env))) {
config_path = fs::path(xdg_env) / "OpenSCAD";
} else {
const char *home = getenv("HOME");
if (home) {
config_path = fs::path(home) / ".config" / "OpenSCAD";
}
}

if (fs::is_directory(config_path)) {
return boosty::stringy(boosty::absolute(config_path));
}

return "";
}

void PlatformUtils::ensureStdIO(void) {}

5 changes: 5 additions & 0 deletions src/PlatformUtils-win.cc
Expand Up @@ -78,6 +78,11 @@ std::string PlatformUtils::documentsPath()
return retval;
}

std::string PlatformUtils::userConfigPath()
{
return "";
}

#include <io.h>
#include <stdio.h>
#include <fstream>
Expand Down
10 changes: 10 additions & 0 deletions src/PlatformUtils.h
Expand Up @@ -10,6 +10,16 @@ namespace PlatformUtils {
std::string documentsPath();
std::string resourcesPath();
std::string userLibraryPath();

/**
* Base path where user configuration can be read and written to. On
* Linux this is the $XDG_CONFIG_HOME.
*
* @return absolute path to the writable configuration folder or
* an empty string if the config path does not exist.
*/
std::string userConfigPath();

bool createUserLibraryPath();
std::string backupPath();
bool createBackupPath();
Expand Down
18 changes: 12 additions & 6 deletions src/colormap.cc
Expand Up @@ -140,12 +140,10 @@ Color4f ColorMap::getColor(const ColorScheme &cs, const RenderColor rc)
return Color4f(0, 0, 0, 127);
}

ColorMap::colorscheme_set_t ColorMap::enumerateColorSchemes()
void ColorMap::enumerateColorSchemesInPath(colorscheme_set_t &result_set, const fs::path path)
{
const fs::path resources = PlatformUtils::resourcesPath();
const fs::path color_schemes = resources / "color-schemes" / "render";

colorscheme_set_t result_set;
const fs::path color_schemes = path / "color-schemes" / "render";

fs::directory_iterator end_iter;

if (fs::exists(color_schemes) && fs::is_directory(color_schemes)) {
Expand All @@ -160,13 +158,21 @@ ColorMap::colorscheme_set_t ColorMap::enumerateColorSchemes()
}

RenderColorScheme *colorScheme = new RenderColorScheme(path);
if (colorScheme->valid()) {
if (colorScheme->valid() && (findColorScheme(colorScheme->name()) == 0)) {
result_set.insert(colorscheme_set_t::value_type(colorScheme->index(), boost::shared_ptr<RenderColorScheme>(colorScheme)));
} else {
delete colorScheme;
}
}
}
}

ColorMap::colorscheme_set_t ColorMap::enumerateColorSchemes()
{
colorscheme_set_t result_set;

enumerateColorSchemesInPath(result_set, PlatformUtils::resourcesPath());
enumerateColorSchemesInPath(result_set, PlatformUtils::userConfigPath());

return result_set;
}
1 change: 1 addition & 0 deletions src/colormap.h
Expand Up @@ -71,5 +71,6 @@ class ColorMap
ColorMap();
virtual ~ColorMap();
colorscheme_set_t enumerateColorSchemes();
void enumerateColorSchemesInPath(colorscheme_set_t &result_set, const fs::path path);
colorscheme_set_t colorSchemeSet;
};
14 changes: 10 additions & 4 deletions src/scintillaeditor.cpp
Expand Up @@ -223,12 +223,10 @@ void ScintillaEditor::noColor()
qsci->setEdgeColor(Qt::black);
}

ScintillaEditor::colorscheme_set_t ScintillaEditor::enumerateColorSchemes()
void ScintillaEditor::enumerateColorSchemesInPath(ScintillaEditor::colorscheme_set_t &result_set, const fs::path path)
{
const fs::path resources = PlatformUtils::resourcesPath();
const fs::path color_schemes = resources / "color-schemes" / "editor";
const fs::path color_schemes = path / "color-schemes" / "editor";

colorscheme_set_t result_set;
fs::directory_iterator end_iter;

if (fs::exists(color_schemes) && fs::is_directory(color_schemes)) {
Expand All @@ -250,6 +248,14 @@ ScintillaEditor::colorscheme_set_t ScintillaEditor::enumerateColorSchemes()
}
}
}
}

ScintillaEditor::colorscheme_set_t ScintillaEditor::enumerateColorSchemes()
{
colorscheme_set_t result_set;

enumerateColorSchemesInPath(result_set, PlatformUtils::resourcesPath());
enumerateColorSchemesInPath(result_set, PlatformUtils::userConfigPath());

return result_set;
}
Expand Down
1 change: 1 addition & 0 deletions src/scintillaeditor.h
Expand Up @@ -59,6 +59,7 @@ class ScintillaEditor : public EditorInterface
void setColormap(const EditorColorScheme *colorScheme);
int readInt(const boost::property_tree::ptree &pt, const std::string name, const int defaultValue);
QColor readColor(const boost::property_tree::ptree &pt, const std::string name, const QColor defaultColor);
void enumerateColorSchemesInPath(colorscheme_set_t &result_set, const fs::path path);
colorscheme_set_t enumerateColorSchemes();

public slots:
Expand Down

0 comments on commit 7c3077b

Please sign in to comment.