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
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();
MakeStep *makeStep = 0;
if (!hasUserFile) {
......
......@@ -105,8 +105,7 @@ signals:
void targetsChanged();
protected:
virtual void saveSettingsImpl(ProjectExplorer::PersistentSettingsWriter &writer);
virtual bool restoreSettingsImpl(ProjectExplorer::PersistentSettingsReader &reader);
virtual bool fromMap(const QVariantMap &map);
// called by CMakeBuildSettingsWidget
void changeBuildDirectory(CMakeBuildConfiguration *bc, const QString &newBuildDirectory);
......
......@@ -60,6 +60,7 @@ using namespace GenericProjectManager::Internal;
using namespace ProjectExplorer;
namespace {
const char * const TOOLCHAIN_KEY("GenericProjectManager.GenericProject.Toolchain");
/**
* An editable string list model. New strings can be added by editing the entry
......@@ -431,10 +432,19 @@ QStringList GenericProject::targets() const
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()) {
GenericBuildConfiguration *bc = new GenericBuildConfiguration(this);
bc->setDisplayName("all");
......@@ -445,34 +455,17 @@ bool GenericProject::restoreSettingsImpl(ProjectExplorer::PersistentSettingsRead
makeStep->setBuildTarget("all", /* on = */ true);
const QLatin1String buildDirectory("buildDirectory");
const QFileInfo fileInfo(file()->fileName());
bc->setBuildDirectory(fileInfo.absolutePath());
setActiveBuildConfiguration(bc);
}
using namespace ProjectExplorer;
QString toolChainName = reader.restoreValue(QLatin1String("toolChain")).toString();
bool convertible = false;
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
ToolChain::ToolChainType type =
static_cast<ProjectExplorer::ToolChain::ToolChainType>
(map.value(QLatin1String(TOOLCHAIN_KEY), 0).toInt());
const QStringList userIncludePaths =
reader.restoreValue(QLatin1String("includePaths")).toStringList();
setToolChainType(type);
setIncludePaths(allIncludePaths());
......@@ -480,14 +473,6 @@ bool GenericProject::restoreSettingsImpl(ProjectExplorer::PersistentSettingsRead
return true;
}
void GenericProject::saveSettingsImpl(ProjectExplorer::PersistentSettingsWriter &writer)
{
Project::saveSettingsImpl(writer);
writer.saveValue(QLatin1String("toolChain"), m_toolChainType);
writer.saveValue(QLatin1String("includePaths"), m_includePaths);
}
////////////////////////////////////////////////////////////////////////////////////
// GenericBuildSettingsWidget
////////////////////////////////////////////////////////////////////////////////////
......
......@@ -111,9 +111,10 @@ public:
ProjectExplorer::ToolChain::ToolChainType toolChainType() const;
void setToolChainType(ProjectExplorer::ToolChain::ToolChainType type);
QVariantMap toMap() const;
protected:
virtual void saveSettingsImpl(ProjectExplorer::PersistentSettingsWriter &writer);
virtual bool restoreSettingsImpl(ProjectExplorer::PersistentSettingsReader &reader);
virtual bool fromMap(const QVariantMap &map);
private:
void parseProject(RefreshOptions options);
......
......@@ -50,6 +50,15 @@ using namespace ProjectExplorer::Internal;
namespace {
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");
} // namespace
......@@ -117,6 +126,21 @@ QList<BuildConfiguration *> Project::buildConfigurations() const
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
{
return true;
......@@ -125,7 +149,10 @@ bool Project::hasBuildSettings() const
void Project::saveSettings()
{
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");
}
......@@ -133,15 +160,10 @@ bool Project::restoreSettings()
{
PersistentSettingsReader reader;
reader.load(file()->fileName() + QLatin1String(PROJECT_FILE_POSTFIX));
if (!restoreSettingsImpl(reader))
return false;
if (!m_activeBuildConfiguration && !m_buildConfigurations.isEmpty())
setActiveBuildConfiguration(m_buildConfigurations.at(0));
QVariantMap map(reader.restoreValues());
if (!m_activeRunConfiguration && !m_runConfigurations.isEmpty())
setActiveRunConfiguration(m_runConfigurations.at(0));
return true;
return fromMap(map);
}
QList<BuildConfigWidget*> Project::subConfigWidgets()
......@@ -149,97 +171,102 @@ QList<BuildConfigWidget*> Project::subConfigWidgets()
return QList<BuildConfigWidget*>();
}
void Project::saveSettingsImpl(PersistentSettingsWriter &writer)
QVariantMap Project::toMap() const
{
const QList<BuildConfiguration *> bcs = buildConfigurations();
// For compatibility with older versions the "name" is saved as a string instead of a number
writer.saveValue("activebuildconfiguration", QString::number(bcs.indexOf(m_activeBuildConfiguration)));
QVariantMap map;
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
QStringList buildConfigurationNames;
for(int i=0; i < bcs.size(); ++i) {
writer.saveValue("buildConfiguration-" + QString::number(i), bcs.at(i)->toMap());
buildConfigurationNames << QString::number(i);
}
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);
const QList<RunConfiguration *> rcs = runConfigurations();
map.insert(QLatin1String(ACTIVE_RC_KEY), rcs.indexOf(m_activeRunConfiguration));
map.insert(QLatin1String(RC_COUNT_KEY), rcs.size());
for (int i = 0; i < rcs.size(); ++i)
map.insert(QString::fromLatin1(RC_KEY_PREFIX) + QString::number(i), rcs.at(i)->toMap());
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
const QStringList buildConfigurationNames = reader.restoreValue("buildconfigurations").toStringList();
foreach (const QString &buildConfigurationName, buildConfigurationNames) {
QVariantMap temp(reader.restoreValue("buildConfiguration-" + buildConfigurationName).toMap());
BuildConfiguration *bc = buildConfigurationFactory()->restore(this, temp);
addBuildConfiguration(bc);
if (map.contains(QLatin1String(EDITOR_SETTINGS_KEY))) {
QVariantMap values(map.value(QLatin1String(EDITOR_SETTINGS_KEY)).toMap());
if (!m_editorConfiguration->fromMap(values))
return false;
}
{ // Try restoring the active configuration
QString activeConfigurationName = reader.restoreValue("activebuildconfiguration").toString();
int index = buildConfigurationNames.indexOf(activeConfigurationName);
if (index != -1)
m_activeBuildConfiguration = buildConfigurations().at(index);
else if (!buildConfigurations().isEmpty())
m_activeBuildConfiguration = buildConfigurations().at(0);
else
m_activeBuildConfiguration = 0;
bool ok;
int maxI(map.value(QLatin1String(BC_COUNT_KEY), 0).toInt(&ok));
if (!ok || maxI < 0)
maxI = 0;
int activeConfiguration(map.value(QLatin1String(ACTIVE_BC_KEY), 0).toInt(&ok));
if (!ok || activeConfiguration < 0)
activeConfiguration = 0;
if (0 > activeConfiguration || maxI < activeConfiguration)
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
const int activeId = reader.restoreValue("activeRunConfiguration").toInt();
int i = 0;
const QList<IRunConfigurationFactory *> factories =
ExtensionSystem::PluginManager::instance()->getObjects<IRunConfigurationFactory>();
forever {
QVariantMap values(reader.restoreValue("RunConfiguration" + QString().setNum(i)).toMap());
if (values.isEmpty())
maxI = map.value(QLatin1String(RC_COUNT_KEY), 0).toInt(&ok);
if (!ok || maxI < 0)
maxI = 0;
activeConfiguration = map.value(QLatin1String(ACTIVE_RC_KEY), 0).toInt(&ok);
if (!ok || activeConfiguration < 0)
activeConfiguration = 0;
if (0 > activeConfiguration || maxI < activeConfiguration)
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;
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())
setActiveRunConfiguration(m_runConfigurations.at(0));
QVariantMap tmp = reader.restoreValue(QLatin1String(EDITOR_SETTINGS_KEY)).toMap();
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();
}
return true;
}
QList<RunConfiguration *> Project::runConfigurations() const
......
......@@ -124,12 +124,22 @@ public:
virtual QStringList frameworkPaths(const QString &fileName) const;
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:
void fileListChanged();
// TODO clean up signal names
// might be better to also have aboutToRemove signals
// a runconfiguration display name changed is missing
void activeBuildConfigurationChanged();
void activeRunConfigurationChanged();
void runConfigurationsEnabledStateChanged();
......@@ -141,23 +151,10 @@ signals:
void addedBuildConfiguration(ProjectExplorer::BuildConfiguration *bc);
protected:
/* This method is called when the project .user file is saved. Simply call
* writer.saveValue() for each value you want to save. Make sure to always
* call your base class implementation.
*
* 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);
// restore all data from the map.
//
// Note: Do not forget to call your base class' fromMap method!
virtual bool fromMap(const QVariantMap &map);
private:
QList<BuildConfiguration *> m_buildConfigurations;
......
......@@ -281,9 +281,9 @@ QStringList QmlProject::files(FilesMode) const
return files();
}
bool QmlProject::restoreSettingsImpl(ProjectExplorer::PersistentSettingsReader &reader)
bool QmlProject::fromMap(const QVariantMap &map)
{
Project::restoreSettingsImpl(reader);
Project::fromMap(map);
if (runConfigurations().isEmpty()) {
QmlRunConfiguration *runConf = new QmlRunConfiguration(this);
......@@ -294,11 +294,6 @@ bool QmlProject::restoreSettingsImpl(ProjectExplorer::PersistentSettingsReader &
return true;
}
void QmlProject::saveSettingsImpl(ProjectExplorer::PersistentSettingsWriter &writer)
{
Project::saveSettingsImpl(writer);
}
////////////////////////////////////////////////////////////////////////////////////
// QmlProjectFile
////////////////////////////////////////////////////////////////////////////////////
......
......@@ -50,7 +50,6 @@ class QmlModelManagerInterface;
}
namespace QmlProjectManager {
class QmlProject;
class QmlRunConfiguration;
......@@ -193,8 +192,7 @@ private slots:
void refreshFiles();
protected:
virtual void saveSettingsImpl(ProjectExplorer::PersistentSettingsWriter &writer);
virtual bool restoreSettingsImpl(ProjectExplorer::PersistentSettingsReader &reader);
bool fromMap(const QVariantMap &map);
private:
// plain format
......
......@@ -77,9 +77,7 @@ const int PROGRESS_PACKAGEDEPLOYED = 300;
const int PROGRESS_PACKAGEINSTALLED = 400;
const int PROGRESS_MAX = 400;
enum {
debug = false
};
enum { debug = 0 };
// Format information about a file
QString lsFile(const QString &f)
......
......@@ -51,10 +51,7 @@ const char * const TOOLCHAIN_KEY("Qt4ProjectManager.Qt4BuildConfiguration.ToolCh
const char * const BUILD_CONFIGURATION_KEY("Qt4ProjectManager.Qt4BuildConfiguration.BuildConfiguration");
const char * const QT_VERSION_ID_KEY("Qt4ProjectManager.Qt4BuildConfiguration.QtVersionId");
enum {
debug = false
};
enum { debug = 0 };
}
Qt4BuildConfiguration::Qt4BuildConfiguration(Qt4Project *pro) :
......
......@@ -271,9 +271,10 @@ void Qt4Project::updateFileList()
}
}
bool Qt4Project::restoreSettingsImpl(PersistentSettingsReader &settingsReader)
bool Qt4Project::fromMap(const QVariantMap &map)
{
Project::restoreSettingsImpl(settingsReader);
if (!Project::fromMap(map))
return false;
if (buildConfigurations().isEmpty())
addDefaultBuild();
......@@ -374,11 +375,6 @@ void Qt4Project::slotActiveBuildConfigurationChanged()
emit targetInformationChanged();
}
void Qt4Project::saveSettingsImpl(ProjectExplorer::PersistentSettingsWriter &writer)
{
Project::saveSettingsImpl(writer);
}
ProjectExplorer::IBuildConfigurationFactory *Qt4Project::buildConfigurationFactory() const
{
return m_buildConfigurationFactory;
......
......@@ -215,8 +215,7 @@ private slots:
const Qt4ProjectManager::Internal::Qt4ProjectType newType);
protected:
virtual bool restoreSettingsImpl(ProjectExplorer::PersistentSettingsReader &settingsReader);
virtual void saveSettingsImpl(ProjectExplorer::PersistentSettingsWriter &writer);
virtual bool fromMap(const QVariantMap &map);
private:
static void collectApplicationProFiles(QList<Internal::Qt4ProFileNode *> &list, Internal::Qt4ProFileNode *node);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment