1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-16 12:49:56 +00:00
openmw-tes3mp/apps/opencs/model/settings/usersettings.cpp

280 lines
9.9 KiB
C++
Raw Normal View History

2013-05-08 01:33:42 +00:00
#include "usersettings.hpp"
#include <QTextStream>
#include <QDir>
#include <QString>
#include <QRegExp>
#include <QMap>
#include <QMessageBox>
#include <QTextCodec>
#include <QFile>
#include <QSortFilterProxyModel>
2013-05-08 01:33:42 +00:00
#include <components/files/configurationmanager.hpp>
#include <boost/version.hpp>
#include "setting.hpp"
#include "support.hpp"
2013-05-08 01:33:42 +00:00
/**
* Workaround for problems with whitespaces in paths in older versions of Boost library
*/
#if (BOOST_VERSION <= 104600)
namespace boost
{
template<>
inline boost::filesystem::path lexical_cast<boost::filesystem::path, std::string>(const std::string& arg)
{
return boost::filesystem::path(arg);
}
} /* namespace boost */
#endif /* (BOOST_VERSION <= 104600) */
2013-06-12 10:36:35 +00:00
CSMSettings::UserSettings *CSMSettings::UserSettings::mUserSettingsInstance = 0;
2013-05-08 01:33:42 +00:00
CSMSettings::UserSettings::UserSettings()
2013-05-08 01:33:42 +00:00
{
2013-06-12 10:36:35 +00:00
assert(!mUserSettingsInstance);
mUserSettingsInstance = this;
buildSettingModelDefaults();
}
void CSMSettings::UserSettings::buildSettingModelDefaults()
{
QString section = "Window Size";
{
Setting *width = createSetting (Type_LineEdit, section, "Width");
Setting *height = createSetting (Type_LineEdit, section, "Height");
width->setWidgetWidth (5);
height->setWidgetWidth (5);
width->setDefaultValues (QStringList() << "1024");
height->setDefaultValues (QStringList() << "768");
width->setEditorSetting (true);
height->setEditorSetting (true);
height->setViewLocation (2,2);
width->setViewLocation (2,1);
/*
*Create the proxy setting for predefined values
*/
Setting *preDefined = createSetting (Type_ComboBox, section,
"Pre-Defined",
QStringList()
<< "640 x 480"
<< "800 x 600"
<< "1024 x 768"
<< "1440 x 900"
);
preDefined->setViewLocation (1, 1);
preDefined->setWidgetWidth (10);
preDefined->setColumnSpan (2);
preDefined->addProxy (width,
QStringList() << "640" << "800" << "1024" << "1440"
);
preDefined->addProxy (height,
QStringList() << "480" << "600" << "768" << "900"
);
}
section = "Display Format";
2013-05-08 01:33:42 +00:00
{
QString defaultValue = "Icon and Text";
2013-05-08 01:33:42 +00:00
QStringList values = QStringList()
<< defaultValue << "Icon Only" << "Text Only";
2013-05-08 01:33:42 +00:00
Setting *rsd = createSetting (Type_RadioButton,
section, "Record Status Display",
values);
2013-05-08 01:33:42 +00:00
Setting *ritd = createSetting (Type_RadioButton,
section, "Referenceable ID Type Display",
values);
2013-05-08 01:33:42 +00:00
rsd->setEditorSetting (true);
ritd->setEditorSetting (true);
}
section = "Proxy Selection Test";
{
//create three setting objects, specifying the basic widget type,
//the setting view name, the page name, and the default value
Setting *masterBoolean = createSetting (Type_RadioButton, section,
"Master Proxy",
QStringList()
<< "Profile One" << "Profile Two"
<< "Profile Three" << "Profile Four"
);
Setting *slaveBoolean = createSetting (Type_CheckBox, section,
"Proxy Checkboxes",
QStringList() << "One" << "Two"
<< "Three" << "Four" << "Five"
);
Setting *slaveSingleText = createSetting (Type_LineEdit, section,
"Proxy TextBox 1"
);
Setting *slaveMultiText = createSetting (Type_LineEdit, section,
"ProxyTextBox 2"
);
// There are three types of values:
//
// Declared values - Pre-determined values, typically for
// combobox drop downs and boolean (radiobutton / checkbox) labels.
// These values represent the total possible list of values that may
// define a setting. No other values are allowed.
//
// Defined values - Values which represent the atual, current value of
// a setting. For settings with declared values, this must be one or
// several declared values, as appropriate.
//
// Proxy values - values the proxy master updates the proxy slave when
// it's own definition is set / changed. These are definitions for
// proxy slave settings, but must match any declared values the proxy
// slave has, if any.
masterBoolean->addProxy (slaveBoolean, QList <QStringList>()
<< (QStringList() << "One" << "Three")
<< (QStringList() << "One" << "Three")
<< (QStringList() << "One" << "Three" << "Five")
<< (QStringList() << "Two" << "Four")
);
masterBoolean->addProxy (slaveSingleText, QList <QStringList>()
<< (QStringList() << "Text A")
<< (QStringList() << "Text B")
<< (QStringList() << "Text A")
<< (QStringList() << "Text C")
);
masterBoolean->addProxy (slaveMultiText, QList <QStringList>()
<< (QStringList() << "One" << "Three")
<< (QStringList() << "One" << "Three")
<< (QStringList() << "One" << "Three" << "Five")
<< (QStringList() << "Two" << "Four")
);
//settings with proxies are not serialized by default
//other settings non-serialized for demo purposes
slaveBoolean->setSerializable (false);
slaveSingleText->setSerializable (false);
slaveMultiText->setSerializable (false);
slaveBoolean->setDefaultValues (QStringList()
<< "One" << "Three" << "Five");
slaveSingleText->setDefaultValue ("Text A");
slaveMultiText->setDefaultValues (QStringList()
<< "One" << "Three" << "Five");
slaveSingleText->setWidgetWidth (24);
slaveMultiText->setWidgetWidth (24);
}
2013-05-08 01:33:42 +00:00
}
CSMSettings::UserSettings::~UserSettings()
2013-05-08 01:33:42 +00:00
{
mUserSettingsInstance = 0;
}
2013-05-08 01:33:42 +00:00
void CSMSettings::UserSettings::loadSettings (const QString &fileName)
{
mUserFilePath = QString::fromUtf8
(mCfgMgr.getUserConfigPath().c_str()) + fileName.toUtf8();
QString global = QString::fromUtf8
(mCfgMgr.getGlobalPath().c_str()) + fileName.toUtf8();
2013-05-08 01:33:42 +00:00
QString local = QString::fromUtf8
(mCfgMgr.getLocalPath().c_str()) + fileName.toUtf8();
2013-05-08 01:33:42 +00:00
//open user and global streams
QTextStream *userStream = openFilestream (mUserFilePath, true);
QTextStream *otherStream = openFilestream (global, true);
2013-05-08 01:33:42 +00:00
//failed stream, try for local
if (!otherStream)
otherStream = openFilestream (local, true);
//error condition - notify and return
if (!otherStream || !userStream)
2013-05-08 01:33:42 +00:00
{
QString message = QObject::tr("<br><b>An error was encountered loading \
user settings files.</b><br><br> One or several files could not \
be read. This may be caused by a missing configuration file, \
incorrect file permissions or a corrupted installation of \
OpenCS.<br>");
message += QObject::tr("<br>Global filepath: ") + global;
message += QObject::tr("<br>Local filepath: ") + local;
message += QObject::tr("<br>User filepath: ") + mUserFilePath;
displayFileErrorMessage ( message, true);
return;
}
//success condition - merge the two streams into a single map and save
DefinitionPageMap totalMap = readFilestream (userStream);
DefinitionPageMap otherMap = readFilestream(otherStream);
//merging other settings file in and ignore duplicate settings to
//avoid overwriting user-level settings
mergeSettings (totalMap, otherMap);
if (!totalMap.isEmpty())
addDefinitions (totalMap);
}
void CSMSettings::UserSettings::saveSettings
(const QMap <QString, QStringList> &settingMap)
{
for (int i = 0; i < settings().size(); i++)
{
Setting* setting = settings().at(i);
QString key = setting->page() + '.' + setting->name();
if (!settingMap.keys().contains(key))
continue;
setting->setDefinedValues (settingMap.value(key));
}
writeFilestream (openFilestream (mUserFilePath, false), settingMap);
}
QString CSMSettings::UserSettings::settingValue (const QString &settingKey)
{
QStringList names = settingKey.split('.');
Setting *setting = findSetting(names.at(0), names.at(1));
if (setting)
{
if (!setting->definedValues().isEmpty())
return setting->definedValues().at(0);
2013-06-08 22:34:27 +00:00
}
return "";
2013-06-08 22:34:27 +00:00
}
CSMSettings::UserSettings& CSMSettings::UserSettings::instance()
2013-06-12 10:36:35 +00:00
{
assert(mUserSettingsInstance);
return *mUserSettingsInstance;
2013-06-12 10:36:35 +00:00
}