Introduce Utils::DisplayName

The concept of a default display name as used in ProjectConfiguration is
useful, and we'd like to use it elsewhere.

Change-Id: I10d9a5bd3d3e385a99f1122c6b119136662502fa
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Christian Kandeler
2019-08-01 14:13:59 +02:00
parent 1d7f6fe298
commit b85b805092
7 changed files with 153 additions and 31 deletions

View File

@@ -40,6 +40,7 @@ add_qtc_library(Utils
detailsbutton.cpp detailsbutton.h
detailswidget.cpp detailswidget.h
differ.cpp differ.h
displayname.cpp displayname.h
dropsupport.cpp dropsupport.h
elfreader.cpp elfreader.h
elidinglabel.cpp elidinglabel.h

View File

@@ -0,0 +1,76 @@
/****************************************************************************
**
** Copyright (C) 2019 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#include "displayname.h"
namespace Utils {
bool DisplayName::setValue(const QString &name)
{
if (value() == name)
return false;
if (name == m_defaultValue)
m_value.clear();
else
m_value = name;
return true;
}
bool DisplayName::setDefaultValue(const QString &name)
{
if (m_defaultValue == name)
return false;
const QString originalName = value();
m_defaultValue = name;
return originalName != value();
}
QString DisplayName::value() const
{
return m_value.isEmpty() ? m_defaultValue : m_value;
}
bool DisplayName::usesDefaultValue() const
{
return m_value.isEmpty();
}
void DisplayName::toMap(QVariantMap &map, const QString &key) const
{
if (!usesDefaultValue())
map.insert(key, m_value);
}
void DisplayName::fromMap(const QVariantMap &map, const QString &key)
{
m_value = map.value(key).toString();
}
bool operator==(const DisplayName &dn1, const DisplayName &dn2)
{
return dn1.value() == dn2.value() && dn1.defaultValue() == dn2.defaultValue();
}
} // namespace Utils

View File

@@ -0,0 +1,63 @@
/****************************************************************************
**
** Copyright (C) 2019 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#pragma once
#include "utils_global.h"
#include <QString>
#include <QVariantMap>
namespace Utils {
// Can be used for anything with a translatable, user-settable name with a fixed default value
// that gets set by a constructor or factory.
class QTCREATOR_UTILS_EXPORT DisplayName
{
public:
// These return true if and only if the value of displayName() has changed.
bool setValue(const QString &name);
bool setDefaultValue(const QString &name);
QString value() const;
QString defaultValue() const { return m_defaultValue; }
bool usesDefaultValue() const;
void toMap(QVariantMap &map, const QString &key) const;
void fromMap(const QVariantMap &map, const QString &key);
private:
QString m_value;
QString m_defaultValue;
};
bool QTCREATOR_UTILS_EXPORT operator==(const DisplayName &dn1, const DisplayName &dn2);
inline bool QTCREATOR_UTILS_EXPORT operator!=(const DisplayName &dn1, const DisplayName &dn2)
{
return !(dn1 == dn2);
}
} // namespace Utils

View File

@@ -24,6 +24,7 @@ win32: LIBS += -liphlpapi -lws2_32
SOURCES += \
$$PWD/globalfilechangeblocker.cpp \
$$PWD/benchmarker.cpp \
$$PWD/displayname.cpp \
$$PWD/environment.cpp \
$$PWD/environmentmodel.cpp \
$$PWD/environmentdialog.cpp \
@@ -138,6 +139,7 @@ HEADERS += \
$$PWD/genericconstants.h \
$$PWD/globalfilechangeblocker.h \
$$PWD/benchmarker.h \
$$PWD/displayname.h \
$$PWD/environment.h \
$$PWD/environmentmodel.h \
$$PWD/environmentdialog.h \

View File

@@ -80,6 +80,8 @@ Project {
"detailswidget.h",
"differ.cpp",
"differ.h",
"displayname.cpp",
"displayname.h",
"dropsupport.cpp",
"dropsupport.h",
"elfreader.cpp",

View File

@@ -111,31 +111,15 @@ QString ProjectConfiguration::settingsIdKey()
return QString(CONFIGURATION_ID_KEY);
}
QString ProjectConfiguration::displayName() const
{
if (!m_displayName.isEmpty())
return m_displayName;
return m_defaultDisplayName;
}
void ProjectConfiguration::setDisplayName(const QString &name)
{
if (displayName() == name)
return;
if (name == m_defaultDisplayName)
m_displayName.clear();
else
m_displayName = name;
emit displayNameChanged();
if (m_displayName.setValue(name))
emit displayNameChanged();
}
void ProjectConfiguration::setDefaultDisplayName(const QString &name)
{
if (m_defaultDisplayName == name)
return;
const QString originalName = displayName();
m_defaultDisplayName = name;
if (originalName != displayName())
if (m_displayName.setDefaultValue(name))
emit displayNameChanged();
}
@@ -152,17 +136,12 @@ QString ProjectConfiguration::toolTip() const
return m_toolTip;
}
bool ProjectConfiguration::usesDefaultDisplayName() const
{
return m_displayName.isEmpty();
}
QVariantMap ProjectConfiguration::toMap() const
{
QTC_CHECK(m_id.isValid());
QVariantMap map;
map.insert(QLatin1String(CONFIGURATION_ID_KEY), m_id.toSetting());
map.insert(QLatin1String(DISPLAY_NAME_KEY), m_displayName);
m_displayName.toMap(map, DISPLAY_NAME_KEY);
m_aspects.toMap(map);
return map;
}
@@ -180,7 +159,7 @@ bool ProjectConfiguration::fromMap(const QVariantMap &map)
// mangle in their build keys.
QTC_ASSERT(id.toString().startsWith(m_id.toString()), return false);
m_displayName = map.value(QLatin1String(DISPLAY_NAME_KEY), QString()).toString();
m_displayName.fromMap(map, DISPLAY_NAME_KEY);
m_aspects.fromMap(map);
return true;
}

View File

@@ -28,6 +28,7 @@
#include "projectexplorer_export.h"
#include <coreplugin/id.h>
#include <utils/displayname.h>
#include <utils/macroexpander.h>
#include <QObject>
@@ -136,9 +137,8 @@ public:
Core::Id id() const;
QString displayName() const;
bool usesDefaultDisplayName() const;
QString displayName() const { return m_displayName.value(); }
bool usesDefaultDisplayName() const { return m_displayName.usesDefaultValue(); }
void setDisplayName(const QString &name);
void setDefaultDisplayName(const QString &name);
@@ -185,8 +185,7 @@ private:
friend class Target; // FIXME: Remove
Target *m_target = nullptr;
const Core::Id m_id;
QString m_displayName;
QString m_defaultDisplayName;
Utils::DisplayName m_displayName;
QString m_toolTip;
Utils::MacroExpander m_macroExpander;
};