Qt Editor: Use definition grouping on "def" property delegate enum

qteditor
Sven Eberhardt 2016-06-15 21:57:59 -04:00
parent ad863836b3
commit e5837dc478
5 changed files with 57 additions and 13 deletions

View File

@ -131,6 +131,12 @@ class C4Def *C4ConsoleQtDefinitionListModel::GetDefByModelIndex(const QModelInde
if (node) return node->def; else return NULL;
}
const char *C4ConsoleQtDefinitionListModel::GetNameByModelIndex(const QModelIndex &idx)
{
DefListNode *node = static_cast<DefListNode *>(idx.internalPointer());
if (node) return node->name.getData(); else return nullptr;
}
int C4ConsoleQtDefinitionListModel::rowCount(const QModelIndex & parent) const
{
int result = 0;

View File

@ -60,9 +60,10 @@ public:
void SetSelection(C4Def *new_selection);
class C4Def *GetDefByModelIndex(const QModelIndex &idx);
const char *GetNameByModelIndex(const QModelIndex &idx);
QModelIndex GetModelIndexByItem(class C4Def *def) const;
protected:
public:
int rowCount(const QModelIndex & parent = QModelIndex()) const override;
int columnCount(const QModelIndex & parent = QModelIndex()) const override;
QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const override;

View File

@ -17,6 +17,7 @@
#include "C4Include.h"
#include "script/C4Value.h"
#include "editor/C4ConsoleQtPropListViewer.h"
#include "editor/C4ConsoleQtDefinitionListViewer.h"
#include "editor/C4ConsoleQtState.h"
#include "editor/C4Console.h"
#include "object/C4Object.h"
@ -588,10 +589,11 @@ void C4PropertyDelegateEnum::AddTypeOption(C4String *name, C4V_Type type, const
options.push_back(option);
}
void C4PropertyDelegateEnum::AddConstOption(C4String *name, const C4Value &val)
void C4PropertyDelegateEnum::AddConstOption(C4String *name, const C4Value &val, C4String *group)
{
Option option;
option.name = name;
option.group = group;
option.value = val;
option.storage_type = Option::StorageByValue;
options.push_back(option);
@ -845,17 +847,45 @@ C4PropertyDelegateDef::C4PropertyDelegateDef(const C4PropertyDelegateFactory *fa
: C4PropertyDelegateEnum(factory, props)
{
// Collect sorted definitions
std::vector<C4Def *> defs = ::Definitions.GetAllDefs(props ? props->GetPropertyStr(P_Filter) : NULL);
std::sort(defs.begin(), defs.end(), [](C4Def *a, C4Def *b) -> bool {
return strcmp(a->GetName(), b->GetName()) < 0;
});
// Add them
ReserveOptions(defs.size() + 1);
AddConstOption(::Strings.RegString("nil"), C4VNull); // nil is always an option
for (C4Def *def : defs)
C4String *filter_property = props ? props->GetPropertyStr(P_Filter) : nullptr;
if (filter_property)
{
C4RefCntPointer<C4String> option_name = ::Strings.RegString(FormatString("%s (%s)", def->id.ToString(), def->GetName()));
AddConstOption(option_name, C4Value(def));
// With filter just create a flat list
std::vector<C4Def *> defs = ::Definitions.GetAllDefs(filter_property);
std::sort(defs.begin(), defs.end(), [](C4Def *a, C4Def *b) -> bool {
return strcmp(a->GetName(), b->GetName()) < 0;
});
// Add them
ReserveOptions(defs.size() + 1);
AddConstOption(::Strings.RegString("nil"), C4VNull); // nil is always an option
for (C4Def *def : defs)
{
C4RefCntPointer<C4String> option_name = ::Strings.RegString(FormatString("%s (%s)", def->id.ToString(), def->GetName()));
AddConstOption(option_name, C4Value(def), nullptr);
}
}
else
{
// Without filter copy tree from definition list model
C4ConsoleQtDefinitionListModel *def_list_model = factory->GetDefinitionListModel();
// Recursively add all defs from model
AddDefinitions(def_list_model, QModelIndex(), nullptr);
}
}
void C4PropertyDelegateDef::AddDefinitions(C4ConsoleQtDefinitionListModel *def_list_model, QModelIndex parent, C4String *group)
{
int32_t count = def_list_model->rowCount(parent);
for (int32_t i = 0; i < count; ++i)
{
QModelIndex index = def_list_model->index(i, 0, parent);
C4Def *def = def_list_model->GetDefByModelIndex(index);
C4RefCntPointer<C4String> name = ::Strings.RegString(def_list_model->GetNameByModelIndex(index));
if (def) AddConstOption(name.Get(), C4Value(def), group);
if (def_list_model->rowCount(index))
{
AddDefinitions(def_list_model, index, group ? ::Strings.RegString(FormatString("%s/%s", group->GetCStr(), name->GetCStr()).getData()) : name.Get());
}
}
}

View File

@ -251,7 +251,7 @@ public:
C4PropertyDelegateEnum(const class C4PropertyDelegateFactory *factory, C4PropList *props, const C4ValueArray *poptions=NULL);
void AddTypeOption(C4String *name, C4V_Type type, const C4Value &val, C4PropertyDelegate *adelegate=NULL);
void AddConstOption(C4String *name, const C4Value &val);
void AddConstOption(C4String *name, const C4Value &val, C4String *group=nullptr);
void SetEditorData(QWidget *editor, const C4Value &val, const C4PropertyPath &property_path) const override;
void SetModelData(QObject *editor, const C4PropertyPath &property_path) const override;
@ -274,6 +274,9 @@ class C4PropertyDelegateDef : public C4PropertyDelegateEnum
{
public:
C4PropertyDelegateDef(const C4PropertyDelegateFactory *factory, C4PropList *props);
private:
void AddDefinitions(class C4ConsoleQtDefinitionListModel *def_list_model, QModelIndex parent, C4String *group);
};
// true or false
@ -356,6 +359,7 @@ class C4PropertyDelegateFactory : public QStyledItemDelegate
mutable C4PropertyDelegate *current_editor_delegate;
mutable C4Value last_edited_value;
class C4ConsoleQtPropListModel *property_model;
class C4ConsoleQtDefinitionListModel *def_list_model;
C4PropertyDelegate *CreateDelegateByPropList(C4PropList *props) const;
C4PropertyDelegate *GetDelegateByIndex(const QModelIndex &index) const;
@ -368,6 +372,8 @@ public:
void ClearDelegates();
void SetPropertyData(const C4PropertyDelegate *d, QObject *editor, C4ConsoleQtPropListModelProperty *editor_prop) const;
void SetPropertyModel(class C4ConsoleQtPropListModel *new_property_model) { property_model = new_property_model; }
void SetDefinitionListModel(class C4ConsoleQtDefinitionListModel *new_def_list_model) { def_list_model = new_def_list_model; }
class C4ConsoleQtDefinitionListModel *GetDefinitionListModel() const { return def_list_model; }
class C4ConsoleQtPropListModel *GetPropertyModel() const { return property_model; }
void OnPropListChanged();
bool CheckCurrentEditor(C4PropertyDelegate *d, QWidget *editor) const;

View File

@ -516,6 +516,7 @@ bool C4ConsoleGUIState::CreateConsoleWindow(C4AbstractApp *app)
ui.objectListView->setModel(object_list_model.get());
window->connect(ui.objectListView->selectionModel(), &QItemSelectionModel::selectionChanged, window.get(), &C4ConsoleQtMainWindow::OnObjectListSelectionChanged);
definition_list_model.reset(new C4ConsoleQtDefinitionListModel());
property_delegate_factory->SetDefinitionListModel(definition_list_model.get());
ui.creatorTreeView->setModel(definition_list_model.get());
window->connect(ui.creatorTreeView->selectionModel(), &QItemSelectionModel::selectionChanged, window.get(), &C4ConsoleQtMainWindow::OnCreatorSelectionChanged);
window->connect(ui.creatorTreeView->selectionModel(), &QItemSelectionModel::currentChanged, window.get(), &C4ConsoleQtMainWindow::OnCreatorCurrentChanged);