enhance studio palette searching

This commit is contained in:
shun-iwasawa 2020-10-22 15:47:52 +09:00 committed by manongjohn
parent 52f65ab7b5
commit 0b63743b26
3 changed files with 79 additions and 7 deletions

View file

@ -108,6 +108,9 @@ public:
void save(const TFilePath &path, TPalette *palette); void save(const TFilePath &path, TPalette *palette);
void removeEntry(const std::wstring paletteId);
void addEntry(const std::wstring paletteId, const TFilePath &path);
private: private:
StudioPalette(); StudioPalette();
TFilePath m_root; TFilePath m_root;

View file

@ -19,6 +19,8 @@
#include <sys/types.h> #include <sys/types.h>
#include <string.h> #include <string.h>
#include <QSettings>
//=================================================================== //===================================================================
//------------------------------------------------------------------- //-------------------------------------------------------------------
@ -142,6 +144,10 @@ TFilePath searchPalette(TFilePath path, std::wstring paletteId) {
bool studioPaletteHasBeenReferred = false; bool studioPaletteHasBeenReferred = false;
static std::map<std::wstring, TFilePath> table; static std::map<std::wstring, TFilePath> table;
// table loaded from the cache. verify once before storing in the table
static std::map<std::wstring, TFilePath> table_cached;
const std::string pathTableFileName = "palette_paths.ini";
//------------------------------------------------------------------- //-------------------------------------------------------------------
} // namespace } // namespace
@ -173,6 +179,19 @@ StudioPalette::StudioPalette() {
} catch (...) { } catch (...) {
} }
} }
// load [global id] - [path] table file
TFilePath rootFps[2] = {m_root, getProjectPalettesRoot()};
for (auto rootFp : rootFps) {
if (rootFp.isEmpty()) continue;
TFilePath tablePath = rootFp + pathTableFileName;
if (!TFileStatus(tablePath).doesExist()) continue;
QSettings tableSettings(QString::fromStdWString(tablePath.getWideString()),
QSettings::IniFormat);
for (auto key : tableSettings.allKeys())
table_cached[key.toStdWString()] =
rootFp + TFilePath(tableSettings.value(key, "").toString());
}
} }
//------------------------------------------------------------------- //-------------------------------------------------------------------
@ -270,6 +289,7 @@ void StudioPalette::movePalette(const TFilePath &dstPath,
} }
std::wstring id = readPaletteGlobalName(dstPath); std::wstring id = readPaletteGlobalName(dstPath);
table.erase(id); table.erase(id);
removeEntry(id);
FolderListenerManager::instance()->notifyFolderChanged( FolderListenerManager::instance()->notifyFolderChanged(
dstPath.getParentDir()); dstPath.getParentDir());
notifyMove(dstPath, srcPath); notifyMove(dstPath, srcPath);
@ -496,12 +516,27 @@ static void foobar(std::wstring paletteId) { table.erase(paletteId); }
TFilePath StudioPalette::getPalettePath(std::wstring paletteId) { TFilePath StudioPalette::getPalettePath(std::wstring paletteId) {
std::map<std::wstring, TFilePath>::iterator it = table.find(paletteId); std::map<std::wstring, TFilePath>::iterator it = table.find(paletteId);
if (it != table.end()) return it->second; if (it != table.end()) return it->second;
TFilePath fp = searchPalette(m_root, paletteId); TFilePath fp;
if (fp == TFilePath()) { // not found in the verified table, then check for the cached table
fp = searchPalette(getProjectPalettesRoot(), paletteId); it = table_cached.find(paletteId);
// found in the cached table
if (it != table_cached.end()) {
fp = it->second;
// verify if cached path is correct
if (fp.getType() != "tpl" ||
readPaletteGlobalName(it->second) != paletteId) {
fp = TFilePath();
// erase the entry
it = table_cached.erase(it);
removeEntry(paletteId);
}
} }
if (fp == TFilePath()) { if (fp.isEmpty()) {
fp = searchPalette(getPersonalPalettesRoot(), paletteId); fp = searchPalette(m_root, paletteId);
if (fp.isEmpty()) fp = searchPalette(getProjectPalettesRoot(), paletteId);
if (fp.isEmpty()) fp = searchPalette(getPersonalPalettesRoot(), paletteId);
addEntry(paletteId, fp);
} }
table[paletteId] = fp; table[paletteId] = fp;
return fp; return fp;
@ -554,7 +589,7 @@ bool StudioPalette::updateLinkedColors(TPalette *palette) {
it = table.find(paletteId); it = table.find(paletteId);
TPalette *spPalette = 0; TPalette *spPalette = 0;
if (it == table.end()) { if (it == table.end()) {
spPalette = StudioPalette::instance()->getPalette(paletteId); spPalette = getPalette(paletteId);
if (!spPalette) continue; if (!spPalette) continue;
table[paletteId] = spPalette; table[paletteId] = spPalette;
// spPalette->release(); // spPalette->release();
@ -680,3 +715,37 @@ void StudioPalette::notifyPaletteChange(const TFilePath &palette) {
it != m_listeners.end(); ++it) it != m_listeners.end(); ++it)
(*it)->onStudioPaletteChange(palette); (*it)->onStudioPaletteChange(palette);
} }
//-------------------------------------------------------------------
void StudioPalette::removeEntry(const std::wstring paletteId) {
TFilePath rootFps[2] = {m_root, getProjectPalettesRoot()};
for (auto rootFp : rootFps) {
if (rootFp.isEmpty()) continue;
TFilePath tablePath = rootFp + pathTableFileName;
if (!TFileStatus(tablePath).doesExist()) continue;
QSettings tableSettings(QString::fromStdWString(tablePath.getWideString()),
QSettings::IniFormat);
if (tableSettings.contains(QString::fromStdWString(paletteId))) {
tableSettings.remove(QString::fromStdWString(paletteId));
break;
}
}
}
//-------------------------------------------------------------------
void StudioPalette::addEntry(const std::wstring paletteId,
const TFilePath &path) {
TFilePath rootFps[2] = {m_root, getProjectPalettesRoot()};
for (auto rootFp : rootFps) {
if (rootFp.isEmpty()) continue;
if (!rootFp.isAncestorOf(path)) continue;
TFilePath tablePath = rootFp + pathTableFileName;
QSettings tableSettings(QString::fromStdWString(tablePath.getWideString()),
QSettings::IniFormat);
QString pathValue = (path - rootFp).getQString();
tableSettings.setValue(QString::fromStdWString(paletteId), pathValue);
}
}

View file

@ -1807,7 +1807,7 @@ void TStyleSelection::getBackOriginalStyle() {
} else } else
spPalette = palIt->second.getPointer(); spPalette = palIt->second.getPointer();
// j is StudioPaletteID // j is StyleID
int j = std::stoi(gname.substr(k + 1)); int j = std::stoi(gname.substr(k + 1));
if (spPalette && 0 <= j && j < spPalette->getStyleCount()) { if (spPalette && 0 <= j && j < spPalette->getStyleCount()) {