Themes: Fix issues with restoring themes.

Themes from the user config where not restored correctly.
Improve error handling when no themes are found
in case of broken installations.
Cleanly differentiate between theme "id" (currently complete basename of
theme file) and theme "displayName" (as specified as a property in the
theme file).
Remove convoluted broken code that tried to allow using an absolute file
path for a theme on the command line and require themes to be installed
either in Qt Creator globally or in the user settings path.
In general stream line the code.

Task-number: QTCREATORBUG-15113
Task-number: QTCREATORBUG-15233
Change-Id: I014a4314e8bea27422ed4c42462cf16f4220698b
Reviewed-by: Alessandro Portale <alessandro.portale@theqtcompany.com>
This commit is contained in:
Eike Ziller
2015-10-27 08:46:02 +01:00
parent 1bf1f58a9c
commit 11f6162739
9 changed files with 168 additions and 149 deletions

View File

@@ -31,14 +31,55 @@
#include "themesettings.h"
#include "themesettingswidget.h"
#include "coreconstants.h"
#include "icore.h"
#include <utils/algorithm.h>
#include <QCoreApplication>
#include <QDebug>
#include <QDir>
#include <QSettings>
static const char themeNameKey[] = "ThemeName";
namespace Core {
namespace Internal {
ThemeSettings::ThemeSettings() :
m_widget(0)
ThemeEntry::ThemeEntry(Id id, const QString &filePath, bool readOnly)
: m_id(id),
m_filePath(filePath),
m_readOnly(readOnly)
{
}
Id ThemeEntry::id() const
{
return m_id;
}
QString ThemeEntry::displayName() const
{
if (m_displayName.isEmpty() && !m_filePath.isEmpty()) {
QSettings settings(m_filePath, QSettings::IniFormat);
m_displayName = settings.value(QLatin1String(themeNameKey),
QCoreApplication::tr("unnamed")).toString();
if (false) // TODO: Revert to m_readOnly
m_displayName = QCoreApplication::tr("%1 (built-in)").arg(m_displayName);
}
return m_displayName;
}
QString ThemeEntry::filePath() const
{
return m_filePath;
}
bool ThemeEntry::readOnly() const
{
return m_readOnly;
}
ThemeSettings::ThemeSettings()
{
setId(Constants::SETTINGS_ID_INTERFACE);
setDisplayName(tr("Theme"));
@@ -71,5 +112,38 @@ void ThemeSettings::finish()
m_widget = 0;
}
static void addThemesFromPath(const QString &path, bool readOnly, QList<ThemeEntry> *themes)
{
static const QLatin1String extension(".creatortheme");
QDir themeDir(path);
themeDir.setNameFilters(QStringList() << QLatin1String("*.creatortheme"));
themeDir.setFilter(QDir::Files);
const QStringList themeList = themeDir.entryList();
foreach (const QString &fileName, themeList) {
QString id = QFileInfo(fileName).completeBaseName();
themes->append(ThemeEntry(Id::fromString(id), themeDir.absoluteFilePath(fileName), readOnly));
}
}
QList<ThemeEntry> ThemeSettings::availableThemes()
{
QList<ThemeEntry> themes;
static const QString installThemeDir = ICore::resourcePath() + QLatin1String("/themes");
static const QString userThemeDir = ICore::userResourcePath() + QLatin1String("/themes");
addThemesFromPath(installThemeDir, /*readOnly=*/true, &themes);
if (themes.isEmpty())
qWarning() << "Warning: No themes found in installation: "
<< QDir::toNativeSeparators(installThemeDir);
// move default theme to front
int defaultIndex = Utils::indexOf(themes, Utils::equal(&ThemeEntry::id, Id("default")));
if (defaultIndex > 0) { // == exists and not at front
ThemeEntry defaultEntry = themes.takeAt(defaultIndex);
themes.prepend(defaultEntry);
}
addThemesFromPath(userThemeDir, /*readOnly=*/false, &themes);
return themes;
}
} // namespace Internal
} // namespace Core