Commit 788b294a authored by Tobias Hunger's avatar Tobias Hunger
Browse files

Make project use toMap/fromMap to save state

 * That is what everybody else is using now.

Reviewed-by: dt
parent 4ee223d4
...@@ -490,14 +490,11 @@ QStringList CMakeProject::files(FilesMode fileMode) const ...@@ -490,14 +490,11 @@ QStringList CMakeProject::files(FilesMode fileMode) const
return m_files; return m_files;
} }
void CMakeProject::saveSettingsImpl(ProjectExplorer::PersistentSettingsWriter &writer) bool CMakeProject::fromMap(const QVariantMap &map)
{ {
Project::saveSettingsImpl(writer); if (!Project::fromMap(map))
} return false;
bool CMakeProject::restoreSettingsImpl(ProjectExplorer::PersistentSettingsReader &reader)
{
Project::restoreSettingsImpl(reader);
bool hasUserFile = !buildConfigurations().isEmpty(); bool hasUserFile = !buildConfigurations().isEmpty();
MakeStep *makeStep = 0; MakeStep *makeStep = 0;
if (!hasUserFile) { if (!hasUserFile) {
......
...@@ -105,8 +105,7 @@ signals: ...@@ -105,8 +105,7 @@ signals:
void targetsChanged(); void targetsChanged();
protected: protected:
virtual void saveSettingsImpl(ProjectExplorer::PersistentSettingsWriter &writer); virtual bool fromMap(const QVariantMap &map);
virtual bool restoreSettingsImpl(ProjectExplorer::PersistentSettingsReader &reader);
// called by CMakeBuildSettingsWidget // called by CMakeBuildSettingsWidget
void changeBuildDirectory(CMakeBuildConfiguration *bc, const QString &newBuildDirectory); void changeBuildDirectory(CMakeBuildConfiguration *bc, const QString &newBuildDirectory);
......
...@@ -60,6 +60,7 @@ using namespace GenericProjectManager::Internal; ...@@ -60,6 +60,7 @@ using namespace GenericProjectManager::Internal;
using namespace ProjectExplorer; using namespace ProjectExplorer;
namespace { namespace {
const char * const TOOLCHAIN_KEY("GenericProjectManager.GenericProject.Toolchain");
/** /**
* An editable string list model. New strings can be added by editing the entry * An editable string list model. New strings can be added by editing the entry
...@@ -431,10 +432,19 @@ QStringList GenericProject::targets() const ...@@ -431,10 +432,19 @@ QStringList GenericProject::targets() const
return targets; return targets;
} }
bool GenericProject::restoreSettingsImpl(ProjectExplorer::PersistentSettingsReader &reader) QVariantMap GenericProject::toMap() const
{ {
Project::restoreSettingsImpl(reader); QVariantMap map(Project::toMap());
map.insert(QLatin1String(TOOLCHAIN_KEY), static_cast<int>(m_toolChainType));
return map;
}
bool GenericProject::fromMap(const QVariantMap &map)
{
if (!Project::fromMap(map))
return false;
// Add default BC:
if (buildConfigurations().isEmpty()) { if (buildConfigurations().isEmpty()) {
GenericBuildConfiguration *bc = new GenericBuildConfiguration(this); GenericBuildConfiguration *bc = new GenericBuildConfiguration(this);
bc->setDisplayName("all"); bc->setDisplayName("all");
...@@ -445,34 +455,17 @@ bool GenericProject::restoreSettingsImpl(ProjectExplorer::PersistentSettingsRead ...@@ -445,34 +455,17 @@ bool GenericProject::restoreSettingsImpl(ProjectExplorer::PersistentSettingsRead
makeStep->setBuildTarget("all", /* on = */ true); makeStep->setBuildTarget("all", /* on = */ true);
const QLatin1String buildDirectory("buildDirectory");
const QFileInfo fileInfo(file()->fileName()); const QFileInfo fileInfo(file()->fileName());
bc->setBuildDirectory(fileInfo.absolutePath()); bc->setBuildDirectory(fileInfo.absolutePath());
setActiveBuildConfiguration(bc); setActiveBuildConfiguration(bc);
} }
using namespace ProjectExplorer; ToolChain::ToolChainType type =
QString toolChainName = reader.restoreValue(QLatin1String("toolChain")).toString(); static_cast<ProjectExplorer::ToolChain::ToolChainType>
bool convertible = false; (map.value(QLatin1String(TOOLCHAIN_KEY), 0).toInt());
ToolChain::ToolChainType type = ToolChain::ToolChainType(toolChainName.toInt(&convertible));
if (!convertible) {
// legacy string values
if (toolChainName == QLatin1String("gcc"))
type = ToolChain::GCC;
else if (toolChainName == QLatin1String("mingw"))
type = ToolChain::MinGW;
else if (toolChainName == QLatin1String("msvc"))
type = ToolChain::MSVC;
else if (toolChainName == QLatin1String("wince"))
type = ToolChain::WINCE;
}
setToolChainType(type); // ### move
const QStringList userIncludePaths = setToolChainType(type);
reader.restoreValue(QLatin1String("includePaths")).toStringList();
setIncludePaths(allIncludePaths()); setIncludePaths(allIncludePaths());
...@@ -480,14 +473,6 @@ bool GenericProject::restoreSettingsImpl(ProjectExplorer::PersistentSettingsRead ...@@ -480,14 +473,6 @@ bool GenericProject::restoreSettingsImpl(ProjectExplorer::PersistentSettingsRead
return true; return true;
} }
void GenericProject::saveSettingsImpl(ProjectExplorer::PersistentSettingsWriter &writer)
{
Project::saveSettingsImpl(writer);
writer.saveValue(QLatin1String("toolChain"), m_toolChainType);
writer.saveValue(QLatin1String("includePaths"), m_includePaths);
}
//////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////
// GenericBuildSettingsWidget // GenericBuildSettingsWidget
//////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////
......
...@@ -111,9 +111,10 @@ public: ...@@ -111,9 +111,10 @@ public:
ProjectExplorer::ToolChain::ToolChainType toolChainType() const; ProjectExplorer::ToolChain::ToolChainType toolChainType() const;
void setToolChainType(ProjectExplorer::ToolChain::ToolChainType type); void setToolChainType(ProjectExplorer::ToolChain::ToolChainType type);
QVariantMap toMap() const;
protected: protected:
virtual void saveSettingsImpl(ProjectExplorer::PersistentSettingsWriter &writer); virtual bool fromMap(const QVariantMap &map);
virtual bool restoreSettingsImpl(ProjectExplorer::PersistentSettingsReader &reader);
private: private:
void parseProject(RefreshOptions options); void parseProject(RefreshOptions options);
......
...@@ -50,6 +50,15 @@ using namespace ProjectExplorer::Internal; ...@@ -50,6 +50,15 @@ using namespace ProjectExplorer::Internal;
namespace { namespace {
const char * const PROJECT_FILE_POSTFIX(".user"); const char * const PROJECT_FILE_POSTFIX(".user");
const char * const ACTIVE_BC_KEY("ProjectExplorer.Project.ActiveBuildConfiguration");
const char * const BC_KEY_PREFIX("ProjectExplorer.Project.BuildConfiguration.");
const char * const BC_COUNT_KEY("ProjectExplorer.Project.BuildConfigurationCount");
const char * const ACTIVE_RC_KEY("ProjectExplorer.Project.ActiveRunConfiguration");
const char * const RC_KEY_PREFIX("ProjectExplorer.Project.RunConfiguration.");
const char * const RC_COUNT_KEY("ProjectExplorer.Project.RunConfigurationCount");
const char * const EDITOR_SETTINGS_KEY("ProjectExplorer.Project.EditorSettings"); const char * const EDITOR_SETTINGS_KEY("ProjectExplorer.Project.EditorSettings");
} // namespace } // namespace
...@@ -117,6 +126,21 @@ QList<BuildConfiguration *> Project::buildConfigurations() const ...@@ -117,6 +126,21 @@ QList<BuildConfiguration *> Project::buildConfigurations() const
return m_buildConfigurations; return m_buildConfigurations;
} }
BuildConfiguration *Project::activeBuildConfiguration() const
{
return m_activeBuildConfiguration;
}
void Project::setActiveBuildConfiguration(BuildConfiguration *configuration)
{
if (!configuration ||
m_activeBuildConfiguration == configuration ||
!m_buildConfigurations.contains(configuration))
return;
m_activeBuildConfiguration = configuration;
emit activeBuildConfigurationChanged();
}
bool Project::hasBuildSettings() const bool Project::hasBuildSettings() const
{ {
return true; return true;
...@@ -125,7 +149,10 @@ bool Project::hasBuildSettings() const ...@@ -125,7 +149,10 @@ bool Project::hasBuildSettings() const
void Project::saveSettings() void Project::saveSettings()
{ {
PersistentSettingsWriter writer; PersistentSettingsWriter writer;
saveSettingsImpl(writer); QVariantMap map(toMap());
for (QVariantMap::const_iterator i = map.constBegin(); i != map.constEnd(); ++i)
writer.saveValue(i.key(), i.value());
writer.save(file()->fileName() + QLatin1String(PROJECT_FILE_POSTFIX), "QtCreatorProject"); writer.save(file()->fileName() + QLatin1String(PROJECT_FILE_POSTFIX), "QtCreatorProject");
} }
...@@ -133,15 +160,10 @@ bool Project::restoreSettings() ...@@ -133,15 +160,10 @@ bool Project::restoreSettings()
{ {
PersistentSettingsReader reader; PersistentSettingsReader reader;
reader.load(file()->fileName() + QLatin1String(PROJECT_FILE_POSTFIX)); reader.load(file()->fileName() + QLatin1String(PROJECT_FILE_POSTFIX));
if (!restoreSettingsImpl(reader))
return false;
if (!m_activeBuildConfiguration && !m_buildConfigurations.isEmpty()) QVariantMap map(reader.restoreValues());
setActiveBuildConfiguration(m_buildConfigurations.at(0));
if (!m_activeRunConfiguration && !m_runConfigurations.isEmpty()) return fromMap(map);
setActiveRunConfiguration(m_runConfigurations.at(0));
return true;
} }
QList<BuildConfigWidget*> Project::subConfigWidgets() QList<BuildConfigWidget*> Project::subConfigWidgets()
...@@ -149,97 +171,102 @@ QList<BuildConfigWidget*> Project::subConfigWidgets() ...@@ -149,97 +171,102 @@ QList<BuildConfigWidget*> Project::subConfigWidgets()
return QList<BuildConfigWidget*>(); return QList<BuildConfigWidget*>();
} }
void Project::saveSettingsImpl(PersistentSettingsWriter &writer) QVariantMap Project::toMap() const
{ {
const QList<BuildConfiguration *> bcs = buildConfigurations(); const QList<BuildConfiguration *> bcs = buildConfigurations();
// For compatibility with older versions the "name" is saved as a string instead of a number QVariantMap map;
writer.saveValue("activebuildconfiguration", QString::number(bcs.indexOf(m_activeBuildConfiguration))); map.insert(QLatin1String(ACTIVE_BC_KEY), bcs.indexOf(m_activeBuildConfiguration));
map.insert(QLatin1String(BC_COUNT_KEY), bcs.size());
for (int i = 0; i < bcs.size(); ++i)
map.insert(QString::fromLatin1(BC_KEY_PREFIX) + QString::number(i), bcs.at(i)->toMap());
//save buildsettings const QList<RunConfiguration *> rcs = runConfigurations();
QStringList buildConfigurationNames; map.insert(QLatin1String(ACTIVE_RC_KEY), rcs.indexOf(m_activeRunConfiguration));
for(int i=0; i < bcs.size(); ++i) { map.insert(QLatin1String(RC_COUNT_KEY), rcs.size());
writer.saveValue("buildConfiguration-" + QString::number(i), bcs.at(i)->toMap()); for (int i = 0; i < rcs.size(); ++i)
buildConfigurationNames << QString::number(i); map.insert(QString::fromLatin1(RC_KEY_PREFIX) + QString::number(i), rcs.at(i)->toMap());
}
writer.saveValue("buildconfigurations", buildConfigurationNames);
// Running
int i = 0;
int activeId = 0;
foreach (RunConfiguration* rc, m_runConfigurations) {
writer.saveValue("RunConfiguration" + QString().setNum(i), rc->toMap());
if (rc == m_activeRunConfiguration)
activeId = i;
++i;
}
writer.setPrefix(QString::null);
writer.saveValue("activeRunConfiguration", activeId);
writer.saveValue(QLatin1String(EDITOR_SETTINGS_KEY), m_editorConfiguration->toMap()); map.insert(QLatin1String(EDITOR_SETTINGS_KEY), m_editorConfiguration->toMap());
return map;
} }
bool Project::restoreSettingsImpl(PersistentSettingsReader &reader) bool Project::fromMap(const QVariantMap &map)
{ {
// restoring BuldConfigurations from settings if (map.contains(QLatin1String(EDITOR_SETTINGS_KEY))) {
const QStringList buildConfigurationNames = reader.restoreValue("buildconfigurations").toStringList(); QVariantMap values(map.value(QLatin1String(EDITOR_SETTINGS_KEY)).toMap());
if (!m_editorConfiguration->fromMap(values))
foreach (const QString &buildConfigurationName, buildConfigurationNames) { return false;
QVariantMap temp(reader.restoreValue("buildConfiguration-" + buildConfigurationName).toMap());
BuildConfiguration *bc = buildConfigurationFactory()->restore(this, temp);
addBuildConfiguration(bc);
} }
{ // Try restoring the active configuration bool ok;
QString activeConfigurationName = reader.restoreValue("activebuildconfiguration").toString(); int maxI(map.value(QLatin1String(BC_COUNT_KEY), 0).toInt(&ok));
int index = buildConfigurationNames.indexOf(activeConfigurationName); if (!ok || maxI < 0)
if (index != -1) maxI = 0;
m_activeBuildConfiguration = buildConfigurations().at(index); int activeConfiguration(map.value(QLatin1String(ACTIVE_BC_KEY), 0).toInt(&ok));
else if (!buildConfigurations().isEmpty()) if (!ok || activeConfiguration < 0)
m_activeBuildConfiguration = buildConfigurations().at(0); activeConfiguration = 0;
else if (0 > activeConfiguration || maxI < activeConfiguration)
m_activeBuildConfiguration = 0; activeConfiguration = 0;
for (int i = 0; i < maxI; ++i) {
const QString key(QString::fromLatin1(BC_KEY_PREFIX) + QString::number(i));
if (!map.contains(key))
return false;
if (!buildConfigurationFactory()->canRestore(this, map.value(key).toMap()))
return false;
BuildConfiguration *bc(buildConfigurationFactory()->restore(this, map.value(key).toMap()));
if (!bc)
continue;
addBuildConfiguration(bc);
if (i == activeConfiguration)
setActiveBuildConfiguration(bc);
} }
if (!activeBuildConfiguration() && !m_buildConfigurations.isEmpty())
setActiveBuildConfiguration(m_buildConfigurations.at(0));
// Running maxI = map.value(QLatin1String(RC_COUNT_KEY), 0).toInt(&ok);
const int activeId = reader.restoreValue("activeRunConfiguration").toInt(); if (!ok || maxI < 0)
int i = 0; maxI = 0;
const QList<IRunConfigurationFactory *> factories = activeConfiguration = map.value(QLatin1String(ACTIVE_RC_KEY), 0).toInt(&ok);
ExtensionSystem::PluginManager::instance()->getObjects<IRunConfigurationFactory>(); if (!ok || activeConfiguration < 0)
forever { activeConfiguration = 0;
QVariantMap values(reader.restoreValue("RunConfiguration" + QString().setNum(i)).toMap()); if (0 > activeConfiguration || maxI < activeConfiguration)
if (values.isEmpty()) activeConfiguration = 0;
QList<IRunConfigurationFactory *>
factories(ExtensionSystem::PluginManager::instance()->
getObjects<IRunConfigurationFactory>());
for (int i = 0; i < maxI; ++i) {
const QString key(QString::fromLatin1(RC_KEY_PREFIX) + QString::number(i));
if (!map.contains(key))
return false;
QVariantMap valueMap(map.value(key).toMap());
IRunConfigurationFactory *factory(0);
foreach (IRunConfigurationFactory *f, factories) {
if (!f->canRestore(this, valueMap))
continue;
factory = f;
break; break;
foreach (IRunConfigurationFactory *factory, factories) {
if (factory->canRestore(this, values)) {
RunConfiguration* rc = factory->restore(this, values);
if (!rc)
continue;
addRunConfiguration(rc);
if (i == activeId)
setActiveRunConfiguration(rc);
}
} }
++i; if (!factory)
return false;
RunConfiguration *rc(factory->restore(this, valueMap));
if (!rc)
return false;
addRunConfiguration(rc);
if (i == activeConfiguration)
setActiveRunConfiguration(rc);
} }
if (!activeRunConfiguration() && !m_runConfigurations.isEmpty()) if (!activeRunConfiguration() && !m_runConfigurations.isEmpty())
setActiveRunConfiguration(m_runConfigurations.at(0)); setActiveRunConfiguration(m_runConfigurations.at(0));
QVariantMap tmp = reader.restoreValue(QLatin1String(EDITOR_SETTINGS_KEY)).toMap(); return true;
return m_editorConfiguration->fromMap(tmp);
}
BuildConfiguration *Project::activeBuildConfiguration() const
{
return m_activeBuildConfiguration;
}
void Project::setActiveBuildConfiguration(BuildConfiguration *configuration)
{
if (m_activeBuildConfiguration != configuration && m_buildConfigurations.contains(configuration)) {
m_activeBuildConfiguration = configuration;
emit activeBuildConfigurationChanged();
}
} }
QList<RunConfiguration *> Project::runConfigurations() const QList<RunConfiguration *> Project::runConfigurations() const
......
...@@ -124,12 +124,22 @@ public: ...@@ -124,12 +124,22 @@ public:
virtual QStringList frameworkPaths(const QString &fileName) const; virtual QStringList frameworkPaths(const QString &fileName) const;
static QString makeUnique(const QString &preferedName, const QStringList &usedNames); static QString makeUnique(const QString &preferedName, const QStringList &usedNames);
// Serialize all data into a QVariantMap. This map is then saved
// in the .user file of the project.
//
// Just put all your data into the map.
//
// Note: Do not forget to call your base class' toMap method.
// Note: Do not forget to call setActiveBuildConfiguration when
// creating new BuilConfigurations.
virtual QVariantMap toMap() const;
signals: signals:
void fileListChanged(); void fileListChanged();
// TODO clean up signal names // TODO clean up signal names
// might be better to also have aboutToRemove signals // might be better to also have aboutToRemove signals
// a runconfiguration display name changed is missing
void activeBuildConfigurationChanged(); void activeBuildConfigurationChanged();
void activeRunConfigurationChanged(); void activeRunConfigurationChanged();
void runConfigurationsEnabledStateChanged(); void runConfigurationsEnabledStateChanged();
...@@ -141,23 +151,10 @@ signals: ...@@ -141,23 +151,10 @@ signals:
void addedBuildConfiguration(ProjectExplorer::BuildConfiguration *bc); void addedBuildConfiguration(ProjectExplorer::BuildConfiguration *bc);
protected: protected:
/* This method is called when the project .user file is saved. Simply call // restore all data from the map.
* writer.saveValue() for each value you want to save. Make sure to always //
* call your base class implementation. // Note: Do not forget to call your base class' fromMap method!
* virtual bool fromMap(const QVariantMap &map);
* Note: All the values from the project/buildsteps and buildconfigurations
* are automatically stored.
*/
virtual void saveSettingsImpl(PersistentSettingsWriter &writer);
/* This method is called when the project is opened. You can retrieve all
* the values you saved in saveSettingsImpl() in this method.
*
* Note: This function is also called if there is no .user file. You should
* probably add some default build and run settings to the project so that
* it can be build and run.
*/
virtual bool restoreSettingsImpl(PersistentSettingsReader &reader);
private: private:
QList<BuildConfiguration *> m_buildConfigurations; QList<BuildConfiguration *> m_buildConfigurations;
......
...@@ -281,9 +281,9 @@ QStringList QmlProject::files(FilesMode) const ...@@ -281,9 +281,9 @@ QStringList QmlProject::files(FilesMode) const
return files(); return files();
} }
bool QmlProject::restoreSettingsImpl(ProjectExplorer::PersistentSettingsReader &reader) bool QmlProject::fromMap(const QVariantMap &map)
{ {
Project::restoreSettingsImpl(reader); Project::fromMap(map);
if (runConfigurations().isEmpty()) { if (runConfigurations().isEmpty()) {
QmlRunConfiguration *runConf = new QmlRunConfiguration(this); QmlRunConfiguration *runConf = new QmlRunConfiguration(this);
...@@ -294,11 +294,6 @@ bool QmlProject::restoreSettingsImpl(ProjectExplorer::PersistentSettingsReader & ...@@ -294,11 +294,6 @@ bool QmlProject::restoreSettingsImpl(ProjectExplorer::PersistentSettingsReader &
return true; return true;
} }
void QmlProject::saveSettingsImpl(ProjectExplorer::PersistentSettingsWriter &writer)
{
Project::saveSettingsImpl(writer);
}
//////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////
// QmlProjectFile // QmlProjectFile
//////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////
......
...@@ -50,7 +50,6 @@ class QmlModelManagerInterface; ...@@ -50,7 +50,6 @@ class QmlModelManagerInterface;
} }
namespace QmlProjectManager { namespace QmlProjectManager {
class QmlProject; class QmlProject;
class QmlRunConfiguration; class QmlRunConfiguration;
...@@ -193,8 +192,7 @@ private slots: ...@@ -193,8 +192,7 @@ private slots:
void refreshFiles(); void refreshFiles();
protected: protected:
virtual void saveSettingsImpl(ProjectExplorer::PersistentSettingsWriter &writer); bool fromMap(const QVariantMap &map);
virtual bool restoreSettingsImpl(ProjectExplorer::PersistentSettingsReader &reader);
private: private:
// plain format // plain format
......
...@@ -77,9 +77,7 @@ const int PROGRESS_PACKAGEDEPLOYED = 300; ...@@ -77,9 +77,7 @@ const int PROGRESS_PACKAGEDEPLOYED = 300;
const int PROGRESS_PACKAGEINSTALLED = 400; const int PROGRESS_PACKAGEINSTALLED = 400;
const int PROGRESS_MAX = 400; const int PROGRESS_MAX = 400;
enum { enum { debug = 0 };
debug = false
};
// Format information about a file // Format information about a file
QString lsFile(const QString &f) QString lsFile(const QString &f)
......
...@@ -51,10 +51,7 @@ const char * const TOOLCHAIN_KEY("Qt4ProjectManager.Qt4BuildConfiguration.ToolCh ...@@ -51,10 +51,7 @@ const char * const TOOLCHAIN_KEY("Qt4ProjectManager.Qt4BuildConfiguration.ToolCh
const char * const BUILD_CONFIGURATION_KEY("Qt4ProjectManager.Qt4BuildConfiguration.BuildConfiguration"); const char * const BUILD_CONFIGURATION_KEY("Qt4ProjectManager.Qt4BuildConfiguration.BuildConfiguration");
const char * const QT_VERSION_ID_KEY("Qt4ProjectManager.Qt4BuildConfiguration.QtVersionId"); const char * const QT_VERSION_ID_KEY("Qt4ProjectManager.Qt4BuildConfiguration.QtVersionId");