Qt editor: Auto-dropdown child enums on creation

qteditor
Sven Eberhardt 2016-07-30 02:37:33 -04:00
parent 898aff0cc8
commit 8840105f6c
2 changed files with 34 additions and 22 deletions

View File

@ -259,7 +259,7 @@ void C4PropertyDelegateInt::SetModelData(QObject *editor, const C4PropertyPath &
property_path.SetProperty(C4VInt(spinBox->value()));
}
QWidget *C4PropertyDelegateInt::CreateEditor(const C4PropertyDelegateFactory *parent_delegate, QWidget *parent, const QStyleOptionViewItem &option, bool by_selection) const
QWidget *C4PropertyDelegateInt::CreateEditor(const C4PropertyDelegateFactory *parent_delegate, QWidget *parent, const QStyleOptionViewItem &option, bool by_selection, bool is_child) const
{
QSpinBox *editor = new QSpinBox(parent);
editor->setMinimum(min);
@ -300,7 +300,7 @@ void C4PropertyDelegateString::SetModelData(QObject *editor, const C4PropertyPat
}
}
QWidget *C4PropertyDelegateString::CreateEditor(const C4PropertyDelegateFactory *parent_delegate, QWidget *parent, const QStyleOptionViewItem &option, bool by_selection) const
QWidget *C4PropertyDelegateString::CreateEditor(const C4PropertyDelegateFactory *parent_delegate, QWidget *parent, const QStyleOptionViewItem &option, bool by_selection, bool is_child) const
{
Editor *editor = new Editor(parent);
// EditingDone only on Return; not just when leaving edit field
@ -364,7 +364,7 @@ void C4PropertyDelegateDescendPath::SetEditorData(QWidget *aeditor, const C4Valu
if (editor->button_pending) emit editor->button->pressed();
}
QWidget *C4PropertyDelegateDescendPath::CreateEditor(const class C4PropertyDelegateFactory *parent_delegate, QWidget *parent, const QStyleOptionViewItem &option, bool by_selection) const
QWidget *C4PropertyDelegateDescendPath::CreateEditor(const class C4PropertyDelegateFactory *parent_delegate, QWidget *parent, const QStyleOptionViewItem &option, bool by_selection, bool is_child) const
{
// Otherwise create display and button to descend path
Editor *editor;
@ -542,7 +542,7 @@ void C4PropertyDelegateColor::SetModelData(QObject *aeditor, const C4PropertyPat
property_path.SetProperty(editor->last_value);
}
QWidget *C4PropertyDelegateColor::CreateEditor(const class C4PropertyDelegateFactory *parent_delegate, QWidget *parent, const QStyleOptionViewItem &option, bool by_selection) const
QWidget *C4PropertyDelegateColor::CreateEditor(const class C4PropertyDelegateFactory *parent_delegate, QWidget *parent, const QStyleOptionViewItem &option, bool by_selection, bool is_child) const
{
Editor *editor;
std::unique_ptr<Editor> peditor((editor = new Editor(parent)));
@ -1016,7 +1016,7 @@ void C4PropertyDelegateEnum::UpdateEditorParameter(C4PropertyDelegateEnum::Edito
if (props) props->GetPropertyByS(option.value_key.Get(), &parameter_val);
}
// Show it
editor->parameter_widget = option.adelegate->CreateEditor(factory, editor, QStyleOptionViewItem(), by_selection);
editor->parameter_widget = option.adelegate->CreateEditor(factory, editor, QStyleOptionViewItem(), by_selection, true);
if (editor->parameter_widget)
{
editor->layout->addWidget(editor->parameter_widget);
@ -1085,6 +1085,13 @@ void C4PropertyDelegateEnum::SetEditorData(QWidget *aeditor, const C4Value &val,
// Update parameter
UpdateEditorParameter(editor, false);
editor->updating = false;
// Execute pending dropdowns from creation as child enums
if (editor->dropdown_pending)
{
editor->dropdown_pending = false;
QMetaObject::invokeMethod(editor->option_box, "doShowPopup", Qt::QueuedConnection);
editor->option_box->showPopup();
}
}
void C4PropertyDelegateEnum::SetModelData(QObject *aeditor, const C4PropertyPath &property_path, C4ConsoleQtShape *prop_shape) const
@ -1136,7 +1143,7 @@ void C4PropertyDelegateEnum::SetOptionValue(const C4PropertyPath &use_path, cons
}
}
QWidget *C4PropertyDelegateEnum::CreateEditor(const C4PropertyDelegateFactory *parent_delegate, QWidget *parent, const QStyleOptionViewItem &option, bool by_selection) const
QWidget *C4PropertyDelegateEnum::CreateEditor(const C4PropertyDelegateFactory *parent_delegate, QWidget *parent, const QStyleOptionViewItem &option, bool by_selection, bool is_child) const
{
Editor *editor = new Editor(parent);
editor->layout = new QHBoxLayout(editor);
@ -1152,6 +1159,8 @@ QWidget *C4PropertyDelegateEnum::CreateEditor(const C4PropertyDelegateFactory *p
editor->updating = false;
editor->option_box->setModel(CreateOptionModel());
editor->option_box->model()->setParent(editor->option_box);
// If created by a selection from a parent enum, show drop down immediately after value has been set
editor->dropdown_pending = by_selection && is_child;
return editor;
}
@ -1369,12 +1378,12 @@ void C4PropertyDelegateObject::UpdateObjectList()
}
}
QWidget *C4PropertyDelegateObject::CreateEditor(const class C4PropertyDelegateFactory *parent_delegate, QWidget *parent, const QStyleOptionViewItem &option, bool by_selection) const
QWidget *C4PropertyDelegateObject::CreateEditor(const class C4PropertyDelegateFactory *parent_delegate, QWidget *parent, const QStyleOptionViewItem &option, bool by_selection, bool is_child) const
{
// Update object list for created editor
// (This should be safe since the object delegate cannot contain nested delegates)
const_cast<C4PropertyDelegateObject *>(this)->UpdateObjectList();
return C4PropertyDelegateEnum::CreateEditor(parent_delegate, parent, option, by_selection);
return C4PropertyDelegateEnum::CreateEditor(parent_delegate, parent, option, by_selection, is_child);
}
QString C4PropertyDelegateObject::GetDisplayString(const C4Value &v, class C4Object *obj) const
@ -1537,7 +1546,7 @@ void C4PropertyDelegateC4ValueInput::SetModelData(QObject *aeditor, const C4Prop
}
}
QWidget *C4PropertyDelegateC4ValueInput::CreateEditor(const class C4PropertyDelegateFactory *parent_delegate, QWidget *parent, const QStyleOptionViewItem &option, bool by_selection) const
QWidget *C4PropertyDelegateC4ValueInput::CreateEditor(const class C4PropertyDelegateFactory *parent_delegate, QWidget *parent, const QStyleOptionViewItem &option, bool by_selection, bool is_child) const
{
// Editor is just an edit box plus a "..." button for array/proplist types
Editor *editor = new Editor(parent);
@ -1720,7 +1729,7 @@ QWidget *C4PropertyDelegateFactory::createEditor(QWidget *parent, const QStyleOp
if (!d) return NULL;
C4ConsoleQtPropListModel::Property *prop = property_model->GetPropByIndex(index);
prop->about_to_edit = true;
QWidget *editor = d->CreateEditor(this, parent, option, true);
QWidget *editor = d->CreateEditor(this, parent, option, true, false);
// Connect value change signals (if editing is possible for this property)
// For some reason, commitData needs a non-const pointer
if (editor)

View File

@ -85,7 +85,7 @@ public:
virtual void SetEditorData(QWidget *editor, const C4Value &val, const C4PropertyPath &property_path) const {};
virtual void SetModelData(QObject *editor, const C4PropertyPath &property_path, class C4ConsoleQtShape *prop_shape) const {};
virtual QWidget *CreateEditor(const class C4PropertyDelegateFactory *parent_delegate, QWidget *parent, const QStyleOptionViewItem &option, bool by_selection) const = 0;
virtual QWidget *CreateEditor(const class C4PropertyDelegateFactory *parent_delegate, QWidget *parent, const QStyleOptionViewItem &option, bool by_selection, bool is_child) const = 0;
virtual void UpdateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option) const;
virtual bool GetPropertyValue(const C4Value &container, C4String *key, int32_t index, C4Value *out_val) const;
virtual bool GetPropertyValueBase(const C4Value &container, C4String *key, int32_t index, C4Value *out_val) const;
@ -116,7 +116,7 @@ public:
void SetEditorData(QWidget *editor, const C4Value &val, const C4PropertyPath &property_path) const override;
void SetModelData(QObject *editor, const C4PropertyPath &property_path, class C4ConsoleQtShape *prop_shape) const override;
QWidget *CreateEditor(const class C4PropertyDelegateFactory *parent_delegate, QWidget *parent, const QStyleOptionViewItem &option, bool by_selection) const override;
QWidget *CreateEditor(const class C4PropertyDelegateFactory *parent_delegate, QWidget *parent, const QStyleOptionViewItem &option, bool by_selection, bool is_child) const override;
};
class C4PropertyDelegateStringEditor : public QLineEdit
@ -135,7 +135,7 @@ public:
void SetEditorData(QWidget *editor, const C4Value &val, const C4PropertyPath &property_path) const override;
void SetModelData(QObject *editor, const C4PropertyPath &property_path, class C4ConsoleQtShape *prop_shape) const override;
QWidget *CreateEditor(const class C4PropertyDelegateFactory *parent_delegate, QWidget *parent, const QStyleOptionViewItem &option, bool by_selection) const override;
QWidget *CreateEditor(const class C4PropertyDelegateFactory *parent_delegate, QWidget *parent, const QStyleOptionViewItem &option, bool by_selection, bool is_child) const override;
QString GetDisplayString(const C4Value &v, C4Object *obj) const override;
};
@ -167,7 +167,7 @@ public:
C4PropertyDelegateDescendPath(const class C4PropertyDelegateFactory *factory, C4PropList *props);
void SetEditorData(QWidget *aeditor, const C4Value &val, const C4PropertyPath &property_path) const override;
QWidget *CreateEditor(const class C4PropertyDelegateFactory *parent_delegate, QWidget *parent, const QStyleOptionViewItem &option, bool by_selection) const override;
QWidget *CreateEditor(const class C4PropertyDelegateFactory *parent_delegate, QWidget *parent, const QStyleOptionViewItem &option, bool by_selection, bool is_child) const override;
};
class C4PropertyDelegateArray : public C4PropertyDelegateDescendPath
@ -200,7 +200,7 @@ public:
void SetEditorData(QWidget *editor, const C4Value &val, const C4PropertyPath &property_path) const override;
void SetModelData(QObject *editor, const C4PropertyPath &property_path, class C4ConsoleQtShape *prop_shape) const override;
QWidget *CreateEditor(const class C4PropertyDelegateFactory *parent_delegate, QWidget *parent, const QStyleOptionViewItem &option, bool by_selection) const override;
QWidget *CreateEditor(const class C4PropertyDelegateFactory *parent_delegate, QWidget *parent, const QStyleOptionViewItem &option, bool by_selection, bool is_child) const override;
QString GetDisplayString(const C4Value &v, C4Object *obj) const override;
QColor GetDisplayTextColor(const C4Value &val, class C4Object *obj) const override;
QColor GetDisplayBackgroundColor(const C4Value &val, class C4Object *obj) const override;
@ -255,6 +255,9 @@ public:
int32_t GetCurrentSelectionIndex();
void BlockNextCloseEvent() { is_next_close_blocked = true; }; // after item selection on a "play" button, the combo dropdown should stay open
public slots:
void doShowPopup() { showPopup(); }
signals:
void NewItemSelected(int32_t new_item);
void TextChanged(const QString new_text);
@ -277,12 +280,12 @@ public:
C4DeepQComboBox *option_box;
QHBoxLayout *layout;
QWidget *parameter_widget;
bool updating, option_changed;
bool updating, option_changed, dropdown_pending;
const C4PropertyDelegate *paint_parameter_delegate; // Delegate to draw over the parameter_widget if it's an empty transparent QWidget (for shape delegates)
C4PropertyDelegateEnumEditor(QWidget *parent)
: QWidget(parent), last_selection_index(-1), option_box(NULL), layout(NULL), parameter_widget(NULL),
updating(false), option_changed(false), paint_parameter_delegate(nullptr) { }
: QWidget(parent), last_selection_index(-1), option_box(nullptr), layout(nullptr), parameter_widget(nullptr),
updating(false), option_changed(false), dropdown_pending(false), paint_parameter_delegate(nullptr) { }
void paintEvent(QPaintEvent *) override;
};
@ -338,7 +341,7 @@ public:
void SetEditorData(QWidget *editor, const C4Value &val, const C4PropertyPath &property_path) const override;
void SetModelData(QObject *editor, const C4PropertyPath &property_path, class C4ConsoleQtShape *prop_shape) const override;
QWidget *CreateEditor(const class C4PropertyDelegateFactory *parent_delegate, QWidget *parent, const QStyleOptionViewItem &option, bool by_selection) const override;
QWidget *CreateEditor(const class C4PropertyDelegateFactory *parent_delegate, QWidget *parent, const QStyleOptionViewItem &option, bool by_selection, bool is_child) const override;
QString GetDisplayString(const C4Value &val, class C4Object *obj) const override;
const class C4PropertyDelegateShape *GetShapeDelegate(C4Value &val, C4PropertyPath *shape_path) const override; // Forward to parameter of selected option
bool HasCustomPaint() const override { return true; }
@ -377,7 +380,7 @@ private:
public:
C4PropertyDelegateObject(const C4PropertyDelegateFactory *factory, C4PropList *props);
QWidget *CreateEditor(const class C4PropertyDelegateFactory *parent_delegate, QWidget *parent, const QStyleOptionViewItem &option, bool by_selection) const override;
QWidget *CreateEditor(const class C4PropertyDelegateFactory *parent_delegate, QWidget *parent, const QStyleOptionViewItem &option, bool by_selection, bool is_child) const override;
QString GetDisplayString(const C4Value &v, class C4Object *obj) const override;
};
@ -441,7 +444,7 @@ public:
void SetEditorData(QWidget *editor, const C4Value &val, const C4PropertyPath &property_path) const override;
void SetModelData(QObject *editor, const C4PropertyPath &property_path, class C4ConsoleQtShape *prop_shape) const override;
QWidget *CreateEditor(const class C4PropertyDelegateFactory *parent_delegate, QWidget *parent, const QStyleOptionViewItem &option, bool by_selection) const override;
QWidget *CreateEditor(const class C4PropertyDelegateFactory *parent_delegate, QWidget *parent, const QStyleOptionViewItem &option, bool by_selection, bool is_child) const override;
};
// areas shown in viewport
@ -455,7 +458,7 @@ public:
void SetEditorData(QWidget *editor, const C4Value &val, const C4PropertyPath &property_path) const override { } // TODO maybe implement update?
void SetModelData(QObject *editor, const C4PropertyPath &property_path, class C4ConsoleQtShape *prop_shape) const override;
QWidget *CreateEditor(const class C4PropertyDelegateFactory *parent_delegate, QWidget *parent, const QStyleOptionViewItem &option, bool by_selection) const override { return nullptr; }
QWidget *CreateEditor(const class C4PropertyDelegateFactory *parent_delegate, QWidget *parent, const QStyleOptionViewItem &option, bool by_selection, bool is_child) const override { return nullptr; }
const C4PropertyDelegateShape *GetShapeDelegate(C4Value &val, C4PropertyPath *shape_path) const override { return this; }
const C4PropertyDelegateShape *GetDirectShapeDelegate() const override { return this; }
bool HasCustomPaint() const override { return true; }