QmlDesigner: Improve to_array

Using an initializer list creates an array.

Change-Id: I1fbccc5456760e30d3fd46c787ec1bd49711b6c4
Reviewed-by: Thomas Hartmann <thomas.hartmann@qt.io>
This commit is contained in:
Marco Bubke
2025-02-20 17:24:05 +01:00
parent d1694efc4f
commit db2eb18900
3 changed files with 129 additions and 90 deletions

View File

@@ -7,19 +7,18 @@
namespace Utils { namespace Utils {
namespace Internal { template<typename Type, typename... Arguments>
template<typename Type, std::size_t size, std::size_t... index> constexpr auto to_array(Arguments &&...arguments)
constexpr std::array<std::remove_cv_t<Type>, size> to_array_implementation(
Type (&&array)[size], std::index_sequence<index...>)
{ {
return {{std::move(array[index])...}}; return std::array<Type, sizeof...(Arguments)>{std::forward<Arguments>(arguments)...};
} }
} // namespace Internal
template<typename Type, std::size_t size> template<typename Type, typename... Arguments>
constexpr std::array<std::remove_cv_t<Type>, size> to_array(Type (&&array)[size]) constexpr auto to_sorted_array(Arguments &&...arguments)
{ {
return Internal::to_array_implementation(std::move(array), std::make_index_sequence<size>{}); auto array = to_array<Type>(std::forward<Arguments>(arguments)...);
std::ranges::sort(array);
return array;
} }
} // namespace Utils } // namespace Utils

View File

@@ -70,51 +70,84 @@ bool isSupportedAttachedProperties(const QString &propertyName)
bool isGlobalQtEnums(QStringView value) bool isGlobalQtEnums(QStringView value)
{ {
static constexpr auto list = Utils::to_array<std::u16string_view>( static constexpr auto list = Utils::to_array<std::u16string_view>(u"AlignBaseline",
{u"AlignBaseline", u"AlignBottom", u"AlignHCenter", u"AlignLeft", u"AlignBottom",
u"AlignRight", u"AlignTop", u"AlignVCenter", u"AllButtons", u"AlignHCenter",
u"ArrowCursor", u"BackButton", u"BlankCursor", u"BottomEdge", u"AlignLeft",
u"BottomLeft", u"BusyCursor", u"ClickFocus", u"ClosedHandCursor", u"AlignRight",
u"CrossCursor", u"DragCopyCursor", u"DragLinkCursor", u"DragMoveCursor", u"AlignTop",
u"ForbiddenCursor", u"ForwardButton", u"Horizontal", u"IBeamCursor", u"AlignVCenter",
u"LeftButton", u"LeftEdge", u"LeftToRight", u"MiddleButton", u"AllButtons",
u"NoFocus", u"OpenHandCursor", u"PointingHandCursor", u"RightButton", u"ArrowCursor",
u"RightEdge", u"RightToLeft", u"SizeAllCursor", u"SizeBDiagCursor", u"BackButton",
u"SizeFDiagCursor", u"SizeHorCursor", u"SizeVerCursor", u"SplitHCursor", u"BlankCursor",
u"SplitVCursor", u"StrongFocus", u"TabFocus", u"TopEdge", u"BottomEdge",
u"TopToBottom", u"UpArrowCursor", u"Vertical", u"WaitCursor", u"BottomLeft",
u"WhatsThisCursor", u"WheelFocus"}); u"BusyCursor",
u"ClickFocus",
u"ClosedHandCursor",
u"CrossCursor",
u"DragCopyCursor",
u"DragLinkCursor",
u"DragMoveCursor",
u"ForbiddenCursor",
u"ForwardButton",
u"Horizontal",
u"IBeamCursor",
u"LeftButton",
u"LeftEdge",
u"LeftToRight",
u"MiddleButton",
u"NoFocus",
u"OpenHandCursor",
u"PointingHandCursor",
u"RightButton",
u"RightEdge",
u"RightToLeft",
u"SizeAllCursor",
u"SizeBDiagCursor",
u"SizeFDiagCursor",
u"SizeHorCursor",
u"SizeVerCursor",
u"SplitHCursor",
u"SplitVCursor",
u"StrongFocus",
u"TabFocus",
u"TopEdge",
u"TopToBottom",
u"UpArrowCursor",
u"Vertical",
u"WaitCursor",
u"WhatsThisCursor",
u"WheelFocus");
if (value.startsWith(u"Key_")) if (value.startsWith(u"Key_"))
return true; return true;
return std::binary_search(std::begin(list), return std::ranges::binary_search(list, QmlDesigner::ModelUtils::toStdStringView(value));
std::end(list),
QmlDesigner::ModelUtils::toStdStringView(value));
} }
bool isKnownEnumScopes(QStringView value) bool isKnownEnumScopes(QStringView value)
{ {
static constexpr auto list = Utils::to_array<std::u16string_view>( static constexpr auto list = Utils::to_array<std::u16string_view>(
{u"TextInput", u"TextInput",
u"TextEdit", u"TextEdit",
u"Material", u"Material",
u"Universal", u"Universal",
u"Font", u"Font",
u"Shape", u"Shape",
u"ShapePath", u"ShapePath",
u"AbstractButton", u"AbstractButton",
u"Text", u"Text",
u"ShaderEffectSource", u"ShaderEffectSource",
u"Grid", u"Grid",
u"ItemLayer", u"ItemLayer",
u"ImageLayer", u"ImageLayer",
u"SpriteLayer", u"SpriteLayer",
u"Light", u"Light",
u"ExtendedSceneEnvironment.GlowBlendMode"}); u"ExtendedSceneEnvironment.GlowBlendMode");
return std::find(std::begin(list), std::end(list), QmlDesigner::ModelUtils::toStdStringView(value)) return std::ranges::find(list, QmlDesigner::ModelUtils::toStdStringView(value)) != std::end(list);
!= std::end(list);
} }
QString stripQuotes(const QString &str) QString stripQuotes(const QString &str)

View File

@@ -22,54 +22,53 @@
using namespace QmlJSEditor::Internal; using namespace QmlJSEditor::Internal;
namespace { namespace {
constexpr auto properitesCheckedByDefault = Utils::to_array<std::u16string_view>({
u"x",
u"y",
u"anchors.alignWhenCentered",
u"anchors.baseline",
u"anchors.baselineOffset",
u"anchors.bottom",
u"anchors.bottomMargin",
u"anchors.centerIn",
u"anchors.fill",
u"anchors.horizontalCenter",
u"anchors.horizontalCenterOffset",
u"anchors.left",
u"anchors.leftMargin",
u"anchors.margins",
u"anchors.right",
u"anchors.rightMargin",
u"anchors.top",
u"anchors.topMargin",
u"anchors.verticalCenter",
u"anchors.verticalCenterOffset",
u"Layout.alignment",
u"Layout.bottomMargin",
u"Layout.column",
u"Layout.columnSpan",
u"Layout.fillHeight",
u"Layout.fillWidth",
u"Layout.horizontalStretchFactor",
u"Layout.leftMargin",
u"Layout.margins",
u"Layout.maximumHeight",
u"Layout.maximumWidth",
u"Layout.minimumHeight",
u"Layout.minimumWidth",
u"Layout.preferredHeight",
u"Layout.preferredWidth",
u"Layout.rightMargin",
u"Layout.row",
u"Layout.rowSpan",
u"Layout.topMargin",
u"Layout.useDefaultSizePolicy",
u"Layout.verticalStretchFactor",
});
bool isCheckedByDefault(const QString &property) bool isCheckedByDefault(std::u16string_view property)
{ {
return std::find(properitesCheckedByDefault.begin(), properitesCheckedByDefault.end(), property) static constexpr auto properitesCheckedByDefault = Utils::to_sorted_array<std::u16string_view>(
!= properitesCheckedByDefault.end(); u"x",
u"y",
u"anchors.alignWhenCentered",
u"anchors.baseline",
u"anchors.baselineOffset",
u"anchors.bottom",
u"anchors.bottomMargin",
u"anchors.centerIn",
u"anchors.fill",
u"anchors.horizontalCenter",
u"anchors.horizontalCenterOffset",
u"anchors.left",
u"anchors.leftMargin",
u"anchors.margins",
u"anchors.right",
u"anchors.rightMargin",
u"anchors.top",
u"anchors.topMargin",
u"anchors.verticalCenter",
u"anchors.verticalCenterOffset",
u"Layout.alignment",
u"Layout.bottomMargin",
u"Layout.column",
u"Layout.columnSpan",
u"Layout.fillHeight",
u"Layout.fillWidth",
u"Layout.horizontalStretchFactor",
u"Layout.leftMargin",
u"Layout.margins",
u"Layout.maximumHeight",
u"Layout.maximumWidth",
u"Layout.minimumHeight",
u"Layout.minimumWidth",
u"Layout.preferredHeight",
u"Layout.preferredWidth",
u"Layout.rightMargin",
u"Layout.row",
u"Layout.rowSpan",
u"Layout.topMargin",
u"Layout.useDefaultSizePolicy",
u"Layout.verticalStretchFactor");
return std::ranges::binary_search(properitesCheckedByDefault, property);
} }
} // namespace } // namespace
@@ -167,7 +166,15 @@ void ComponentNameDialog::setProperties(const QStringList &properties)
for (int i = 0; i < m_listWidget->count(); ++i) { for (int i = 0; i < m_listWidget->count(); ++i) {
QListWidgetItem *item = m_listWidget->item(i); QListWidgetItem *item = m_listWidget->item(i);
item->setFlags(Qt::ItemIsUserCheckable | Qt:: ItemIsEnabled); item->setFlags(Qt::ItemIsUserCheckable | Qt:: ItemIsEnabled);
#if QT_VERSION > QT_VERSION_CHECK(6, 7, 0)
if (isCheckedByDefault(item->text())) if (isCheckedByDefault(item->text()))
#else
const QString text = item->text();
const QStringView view = item->text();
if (isCheckedByDefault(
std::u16string_view{view.utf16(), static_cast<std::size_t>(view.size())}))
#endif
m_listWidget->item(i)->setCheckState(Qt::Checked); m_listWidget->item(i)->setCheckState(Qt::Checked);
else else
m_listWidget->item(i)->setCheckState(Qt::Unchecked); m_listWidget->item(i)->setCheckState(Qt::Unchecked);