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 Internal {
template<typename Type, std::size_t size, std::size_t... index>
constexpr std::array<std::remove_cv_t<Type>, size> to_array_implementation(
Type (&&array)[size], std::index_sequence<index...>)
template<typename Type, typename... Arguments>
constexpr auto to_array(Arguments &&...arguments)
{
return {{std::move(array[index])...}};
return std::array<Type, sizeof...(Arguments)>{std::forward<Arguments>(arguments)...};
}
} // namespace Internal
template<typename Type, std::size_t size>
constexpr std::array<std::remove_cv_t<Type>, size> to_array(Type (&&array)[size])
template<typename Type, typename... Arguments>
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

View File

@@ -70,51 +70,84 @@ bool isSupportedAttachedProperties(const QString &propertyName)
bool isGlobalQtEnums(QStringView value)
{
static constexpr auto list = Utils::to_array<std::u16string_view>(
{u"AlignBaseline", u"AlignBottom", u"AlignHCenter", u"AlignLeft",
u"AlignRight", u"AlignTop", u"AlignVCenter", u"AllButtons",
u"ArrowCursor", u"BackButton", u"BlankCursor", u"BottomEdge",
u"BottomLeft", 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"});
static constexpr auto list = Utils::to_array<std::u16string_view>(u"AlignBaseline",
u"AlignBottom",
u"AlignHCenter",
u"AlignLeft",
u"AlignRight",
u"AlignTop",
u"AlignVCenter",
u"AllButtons",
u"ArrowCursor",
u"BackButton",
u"BlankCursor",
u"BottomEdge",
u"BottomLeft",
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_"))
return true;
return std::binary_search(std::begin(list),
std::end(list),
QmlDesigner::ModelUtils::toStdStringView(value));
return std::ranges::binary_search(list, QmlDesigner::ModelUtils::toStdStringView(value));
}
bool isKnownEnumScopes(QStringView value)
{
static constexpr auto list = Utils::to_array<std::u16string_view>(
{u"TextInput",
u"TextEdit",
u"Material",
u"Universal",
u"Font",
u"Shape",
u"ShapePath",
u"AbstractButton",
u"Text",
u"ShaderEffectSource",
u"Grid",
u"ItemLayer",
u"ImageLayer",
u"SpriteLayer",
u"Light",
u"ExtendedSceneEnvironment.GlowBlendMode"});
u"TextInput",
u"TextEdit",
u"Material",
u"Universal",
u"Font",
u"Shape",
u"ShapePath",
u"AbstractButton",
u"Text",
u"ShaderEffectSource",
u"Grid",
u"ItemLayer",
u"ImageLayer",
u"SpriteLayer",
u"Light",
u"ExtendedSceneEnvironment.GlowBlendMode");
return std::find(std::begin(list), std::end(list), QmlDesigner::ModelUtils::toStdStringView(value))
!= std::end(list);
return std::ranges::find(list, QmlDesigner::ModelUtils::toStdStringView(value)) != std::end(list);
}
QString stripQuotes(const QString &str)

View File

@@ -22,54 +22,53 @@
using namespace QmlJSEditor::Internal;
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)
!= properitesCheckedByDefault.end();
static constexpr auto properitesCheckedByDefault = Utils::to_sorted_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");
return std::ranges::binary_search(properitesCheckedByDefault, property);
}
} // namespace
@@ -167,7 +166,15 @@ void ComponentNameDialog::setProperties(const QStringList &properties)
for (int i = 0; i < m_listWidget->count(); ++i) {
QListWidgetItem *item = m_listWidget->item(i);
item->setFlags(Qt::ItemIsUserCheckable | Qt:: ItemIsEnabled);
#if QT_VERSION > QT_VERSION_CHECK(6, 7, 0)
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);
else
m_listWidget->item(i)->setCheckState(Qt::Unchecked);