DockManager: Introduce read/writeAttribute() helpers

They are going to be reused in reading / writing
display name and mcus enabled.

Change-Id: I679de8858c37fb8629774d0166a7dbc56e5dab76
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
This commit is contained in:
Jarek Kobus
2024-02-17 12:29:00 +01:00
parent a38b1db252
commit 353ddc87a4
2 changed files with 46 additions and 1 deletions

View File

@@ -1627,6 +1627,48 @@ bool DockManager::writeMcusEnabled(const FilePath &filePath, const QString &mcus
return true;
}
QString DockManager::readAttribute(const FilePath &filePath, QStringView key)
{
auto data = loadFile(filePath);
if (data.isEmpty())
return {};
auto tmp = data.startsWith("<?xml") ? data : qUncompress(data);
DockingStateReader reader(tmp);
if (!reader.readNextStartElement())
return {};
if (reader.name() != QLatin1String("QtAdvancedDockingSystem"))
return {};
return reader.attributes().value(key.toString()).toString();
}
bool DockManager::writeAttribute(const FilePath &filePath, QStringView key, const QString &value)
{
const expected_str<QByteArray> content = filePath.fileContents();
QTC_ASSERT_EXPECTED(content, return false);
QDomDocument doc;
QString error_msg;
int error_line, error_col;
if (!doc.setContent(*content, &error_msg, &error_line, &error_col)) {
qWarning() << QString("XML error on line %1, col %2: %3")
.arg(error_line).arg(error_col).arg(error_msg);
return false;
}
QDomElement docElem = doc.documentElement();
docElem.setAttribute(key.toString(), value);
const expected_str<void> result = write(filePath, doc.toByteArray(workspaceXmlFormattingIndent));
if (result)
return true;
qWarning() << "Could not write" << key << value << "to" << filePath << ":" << result.error();
return false;
}
expected_str<void> DockManager::write(const FilePath &filePath, const QByteArray &data)
{
qCInfo(adsLog) << "Write" << filePath;

View File

@@ -458,7 +458,7 @@ public:
/**
* This function sets the tool button style for the given dock widget state. It is possible to
* switch the tool button style depending on the state. If a dock widget is floating, then here
* switch the tool button style depending on the state. If a dock widget is floating, then here
* are more space and it is possible to select a style that requires more space like
* Qt::ToolButtonTextUnderIcon. For the docked state Qt::ToolButtonIconOnly might be better.
*/
@@ -783,6 +783,9 @@ signals:
void lockWorkspaceChanged();
private:
static QString readAttribute(const Utils::FilePath &filePath, QStringView key);
static bool writeAttribute(const Utils::FilePath &filePath, QStringView key,
const QString &value);
static Utils::expected_str<void> write(const Utils::FilePath &filePath, const QByteArray &data);
Utils::expected_str<QByteArray> loadWorkspace(const Workspace &workspace) const;