output built-in themes on startup

This commit is contained in:
Le Tan 2018-01-15 19:54:59 +08:00
parent 45526cc0a8
commit 0b885e1639
9 changed files with 82 additions and 1 deletions

View File

@ -6,6 +6,7 @@ qss_file=v_moonlight.qss
mdhl_file=v_moonlight.mdhl mdhl_file=v_moonlight.mdhl
css_file=v_moonlight.css css_file=v_moonlight.css
codeblock_css_file=v_moonlight_codeblock.css codeblock_css_file=v_moonlight_codeblock.css
version=1
; This mapping will be used to translate colors when the content of HTML is copied ; This mapping will be used to translate colors when the content of HTML is copied
; without background. You could just specify the foreground colors mapping here. ; without background. You could just specify the foreground colors mapping here.

View File

@ -6,6 +6,7 @@ qss_file=v_pure.qss
mdhl_file=v_pure.mdhl mdhl_file=v_pure.mdhl
css_file=v_pure.css css_file=v_pure.css
codeblock_css_file=v_pure_codeblock.css codeblock_css_file=v_pure_codeblock.css
version=1
[phony] [phony]
; Abstract color attributes. ; Abstract color attributes.

View File

@ -6,6 +6,7 @@ qss_file=v_white.qss
mdhl_file=v_white.mdhl mdhl_file=v_white.mdhl
css_file=v_white.css css_file=v_white.css
codeblock_css_file=v_white_codeblock.css codeblock_css_file=v_white_codeblock.css
version=1
[phony] [phony]
; Abstract color attributes. ; Abstract color attributes.

View File

@ -878,6 +878,16 @@ bool VUtils::deleteDirectory(const VNotebook *p_notebook,
} }
} }
bool VUtils::deleteDirectory(const QString &p_path)
{
if (p_path.isEmpty()) {
return true;
}
QDir dir(p_path);
return dir.removeRecursively();
}
bool VUtils::emptyDirectory(const VNotebook *p_notebook, bool VUtils::emptyDirectory(const VNotebook *p_notebook,
const QString &p_path, const QString &p_path,
bool p_skipRecycleBin) bool p_skipRecycleBin)

View File

@ -221,6 +221,8 @@ public:
const QString &p_path, const QString &p_path,
bool p_skipRecycleBin = false); bool p_skipRecycleBin = false);
static bool deleteDirectory(const QString &p_path);
// Empty all files in directory recursively specified by @p_path. // Empty all files in directory recursively specified by @p_path.
// Will just move files to the recycle bin of @p_notebook if // Will just move files to the recycle bin of @p_notebook if
// @p_skipRecycleBin is false. // @p_skipRecycleBin is false.

View File

@ -1236,8 +1236,11 @@ void VConfigManager::initThemes()
m_themes.insert(VPalette::themeName(file), file); m_themes.insert(VPalette::themeName(file), file);
*/ */
outputBuiltInThemes();
// User theme folder. // User theme folder.
QDir dir(getThemeConfigFolder()); QDir dir(getThemeConfigFolder());
Q_ASSERT(dir.exists());
if (!dir.exists()) { if (!dir.exists()) {
dir.mkpath(getThemeConfigFolder()); dir.mkpath(getThemeConfigFolder());
return; return;
@ -1257,6 +1260,53 @@ void VConfigManager::initThemes()
} }
} }
void VConfigManager::outputBuiltInThemes()
{
QDir dir(getThemeConfigFolder());
if (!dir.exists()) {
dir.mkpath(getThemeConfigFolder());
}
QStringList suffix({"*.palette"});
for (auto it = m_themes.begin(); it != m_themes.end(); ++it) {
QString file = it.value();
QString srcDir = VUtils::basePathFromPath(file);
QString folder = VUtils::directoryNameFromPath(srcDir);
bool needOutput = false;
if (dir.exists(folder)) {
QString folderPath = dir.filePath(folder);
QDir tmpDir(folderPath);
QStringList files = tmpDir.entryList(suffix);
if (files.size() == 1) {
int newVer = VPalette::getPaletteVersion(file);
int curVer = VPalette::getPaletteVersion(tmpDir.filePath(files[0]));
if (newVer != curVer) {
needOutput = true;
}
} else {
needOutput = true;
}
if (needOutput) {
// Delete the folder.
bool ret = VUtils::deleteDirectory(folderPath);
VUtils::sleepWait(100);
Q_UNUSED(ret);
qDebug() << "delete obsolete theme" << folderPath << ret;
}
} else {
needOutput = true;
}
if (needOutput) {
qDebug() << "output built-in theme" << file << folder;
VUtils::copyDirectory(srcDir, dir.filePath(folder), false);
}
}
}
void VConfigManager::initEditorStyles() void VConfigManager::initEditorStyles()
{ {
Q_ASSERT(!m_themes.isEmpty()); Q_ASSERT(!m_themes.isEmpty());

View File

@ -510,6 +510,10 @@ private:
// Init the themes name-file mappings. // Init the themes name-file mappings.
void initThemes(); void initThemes();
// Output built-in themes to user theme folder if there does not exists folders
// with the same name.
void outputBuiltInThemes();
// Init the editor styles name-file mappings. // Init the editor styles name-file mappings.
void initEditorStyles(); void initEditorStyles();

View File

@ -202,6 +202,8 @@ VPaletteMetaData VPalette::getPaletteMetaData(const QString &p_paletteFile)
QDir dir(VUtils::basePathFromPath(QFileInfo(p_paletteFile).absoluteFilePath())); QDir dir(VUtils::basePathFromPath(QFileInfo(p_paletteFile).absoluteFilePath()));
settings.beginGroup("metadata"); settings.beginGroup("metadata");
data.m_version = settings.value("version").toInt();
QString val = settings.value("qss_file").toString(); QString val = settings.value("qss_file").toString();
if (!val.isEmpty()) { if (!val.isEmpty()) {
data.m_qssFile = dir.filePath(val); data.m_qssFile = dir.filePath(val);
@ -264,6 +266,11 @@ QString VPalette::themeCodeBlockCssStyle(const QString &p_paletteFile)
return themeName(p_paletteFile) + "/" + QFileInfo(data.m_codeBlockCssFile).completeBaseName(); return themeName(p_paletteFile) + "/" + QFileInfo(data.m_codeBlockCssFile).completeBaseName();
} }
int VPalette::getPaletteVersion(const QString &p_paletteFile)
{
return getPaletteMetaData(p_paletteFile).m_version;
}
void VPalette::fillFontFamily(QString &p_text) const void VPalette::fillFontFamily(QString &p_text) const
{ {
QRegExp reg("(\\s|^)font-family:([^;]+);"); QRegExp reg("(\\s|^)font-family:([^;]+);");

View File

@ -9,6 +9,8 @@ class QSettings;
struct VPaletteMetaData struct VPaletteMetaData
{ {
int m_version;
// These are all file PATH, not name. // These are all file PATH, not name.
QString m_qssFile; QString m_qssFile;
QString m_mdhlFile; QString m_mdhlFile;
@ -21,7 +23,8 @@ struct VPaletteMetaData
QString toString() const QString toString() const
{ {
return QString("palette metadata qss=%1 mdhl=%2 css=%3 codeBlockCss=%4 colorMappingSize=%5") return QString("palette metadata version=%1 qss=%2 mdhl=%3 css=%4 codeBlockCss=%5 colorMappingSize=%6")
.arg(m_version)
.arg(m_qssFile) .arg(m_qssFile)
.arg(m_mdhlFile) .arg(m_mdhlFile)
.arg(m_cssFile) .arg(m_cssFile)
@ -59,6 +62,8 @@ public:
// Read themes and return the mappings of css styles. // Read themes and return the mappings of css styles.
static QMap<QString, QString> codeBlockCssStylesFromThemes(const QList<QString> &p_themeFiles); static QMap<QString, QString> codeBlockCssStylesFromThemes(const QList<QString> &p_themeFiles);
static int getPaletteVersion(const QString &p_paletteFile);
static VPaletteMetaData getPaletteMetaData(const QString &p_paletteFile); static VPaletteMetaData getPaletteMetaData(const QString &p_paletteFile);
// Return the name of the theme. // Return the name of the theme.