SMetaDataView.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Copyright 2017-2021 marynate. All Rights Reserved.
  2. #include "SMetaDataView.h"
  3. #include "Widgets/Text/SMultiLineEditableText.h"
  4. #include "Widgets/Views/SListView.h"
  5. #include "Widgets/Views/STableRow.h"
  6. namespace MetaDataViewColumns
  7. {
  8. /** IDs for list columns */
  9. static const FName ColumnID_Tag("Tag");
  10. static const FName ColumnID_Value("Value");
  11. }
  12. struct FMetaDataLine
  13. {
  14. FMetaDataLine(FName InTag, const FString& InValue)
  15. : Tag(InTag)
  16. , Value(InValue)
  17. {
  18. }
  19. FName Tag;
  20. FString Value;
  21. };
  22. /**
  23. * The widget that represents a row in the MetaDataView's list view widget. Generates a widget for each column, on-demand.
  24. */
  25. class SMetaDataViewRow : public SMultiColumnTableRow< TSharedPtr< FMetaDataLine > >
  26. {
  27. public:
  28. SLATE_BEGIN_ARGS(SMetaDataViewRow) {}
  29. SLATE_END_ARGS()
  30. /**
  31. * Construct this widget. Called by the SNew() Slate macro.
  32. *
  33. * @param InArgs Declaration used by the SNew() macro to construct this widget
  34. * @param InMetaData The metadata tag/value to display in the row widget
  35. * @param InOwnerTableView The owner of the row widget
  36. */
  37. void Construct(const FArguments& InArgs, TSharedRef< FMetaDataLine > InMetaData, TSharedRef< STableViewBase > InOwnerTableView);
  38. /**
  39. * Constructs the widget that represents the specified ColumnID for this Row
  40. *
  41. * @param ColumnID A unique ID for a column in this TableView; see SHeaderRow::FColumn for more info.
  42. *
  43. * @return a widget to represent the contents of a cell in this row of a TableView.
  44. */
  45. virtual TSharedRef< SWidget > GenerateWidgetForColumn(const FName& ColumnID) override;
  46. private:
  47. TSharedPtr< FMetaDataLine > MetaDataLine;
  48. };
  49. void SMetaDataViewRow::Construct(const FArguments& InArgs, TSharedRef< FMetaDataLine > InMetadata, TSharedRef< STableViewBase > InOwnerTableView)
  50. {
  51. MetaDataLine = InMetadata;
  52. SMultiColumnTableRow< TSharedPtr< FMetaDataLine > >::Construct(FSuperRowType::FArguments(), InOwnerTableView);
  53. }
  54. TSharedRef< SWidget > SMetaDataViewRow::GenerateWidgetForColumn(const FName& ColumnID)
  55. {
  56. TSharedPtr< SWidget > TableRowContent;
  57. static const FTextBlockStyle MetadataTextStyle = FTextBlockStyle(FCoreStyle::Get().GetWidgetStyle<FTextBlockStyle>("NormalText"))
  58. .SetFontSize(10);
  59. if (ColumnID == MetaDataViewColumns::ColumnID_Tag)
  60. {
  61. SAssignNew(TableRowContent, SHorizontalBox)
  62. + SHorizontalBox::Slot()
  63. .Padding(1.5f)
  64. .FillWidth(100.0f)
  65. [
  66. SNew(SMultiLineEditableText)
  67. .Text(FText::FromName(MetaDataLine->Tag))
  68. .TextStyle(&MetadataTextStyle)
  69. .IsReadOnly(true)
  70. ];
  71. }
  72. else if (ColumnID == MetaDataViewColumns::ColumnID_Value)
  73. {
  74. SAssignNew(TableRowContent, SHorizontalBox)
  75. + SHorizontalBox::Slot()
  76. .Padding(1.5f)
  77. .FillWidth(400.0f)
  78. [
  79. SNew(SMultiLineEditableText)
  80. .Text(FText::FromString(*(MetaDataLine->Value)))
  81. .TextStyle(&MetadataTextStyle)
  82. .IsReadOnly(true)
  83. .AutoWrapText(true)
  84. ];
  85. }
  86. else
  87. {
  88. checkf(false, TEXT("Unknown ColumnID provided to SMetaDataView"));
  89. }
  90. return TableRowContent.ToSharedRef();
  91. }
  92. void SMetaDataView::Construct(const FArguments& InArgs, const TMap<FName, FString>& InMetadata)
  93. {
  94. for (auto It = InMetadata.CreateConstIterator(); It; ++It)
  95. {
  96. MetaDataLines.Add(MakeShared<FMetaDataLine>(FMetaDataLine(It->Key, It->Value)));
  97. }
  98. TSharedPtr< SHeaderRow > HeaderRowWidget =
  99. SNew(SHeaderRow)
  100. // Tag column
  101. + SHeaderRow::Column(MetaDataViewColumns::ColumnID_Tag)
  102. .FillWidth(100.0f)
  103. .DefaultLabel(NSLOCTEXT("MetadataView", "ColumnID_Tag", "Tag"))
  104. .DefaultTooltip(FText())
  105. // Value column
  106. + SHeaderRow::Column(MetaDataViewColumns::ColumnID_Value)
  107. .FillWidth(400.0f)
  108. .DefaultLabel(NSLOCTEXT("MetadataView", "ColumnID_Value", "Value"))
  109. .DefaultTooltip(FText());
  110. ChildSlot
  111. [
  112. SNew(SVerticalBox)
  113. + SVerticalBox::Slot()
  114. [
  115. SNew(SListView< TSharedPtr< FMetaDataLine > >)
  116. .ListItemsSource(&MetaDataLines)
  117. .OnGenerateRow(this, &SMetaDataView::OnGenerateRow)
  118. .HeaderRow(HeaderRowWidget)
  119. ]
  120. ];
  121. }
  122. TSharedRef< ITableRow > SMetaDataView::OnGenerateRow(const TSharedPtr< FMetaDataLine > Item, const TSharedRef< STableViewBase >& OwnerTable)
  123. {
  124. return SNew(SMetaDataViewRow, Item.ToSharedRef(), OwnerTable);
  125. }