Qt Editor: Add AllowEditing flag for enum delegates.

Used for sound delegate
qteditor
Sven Eberhardt 2016-07-28 00:24:28 -04:00
parent b6fb7e884f
commit 37885a2e4f
4 changed files with 32 additions and 7 deletions

View File

@ -621,8 +621,8 @@ void C4StyledItemDelegateWithButton::paint(QPainter *painter, const QStyleOption
/* Enum delegate combo box */
C4DeepQComboBox::C4DeepQComboBox(QWidget *parent, C4StyledItemDelegateWithButton::ButtonType button_type)
: QComboBox(parent), last_popup_height(0), is_next_close_blocked(false)
C4DeepQComboBox::C4DeepQComboBox(QWidget *parent, C4StyledItemDelegateWithButton::ButtonType button_type, bool editable)
: QComboBox(parent), last_popup_height(0), is_next_close_blocked(false), editable(editable)
{
item_delegate.reset(new C4StyledItemDelegateWithButton(button_type));
QTreeView *view = new QTreeView(this);
@ -631,6 +631,7 @@ C4DeepQComboBox::C4DeepQComboBox(QWidget *parent, C4StyledItemDelegateWithButton
view->setAllColumnsShowFocus(true);
view->header()->hide();
view->setItemDelegate(item_delegate.get());
setEditable(editable);
// On expansion, enlarge view if necessery
connect(view, &QTreeView::expanded, this, [this, view](const QModelIndex &index)
{
@ -666,11 +667,18 @@ C4DeepQComboBox::C4DeepQComboBox(QWidget *parent, C4StyledItemDelegateWithButton
if (selected_data.type() == QVariant::Int)
{
// Finish selection
this->setRootModelIndex(current.parent());
this->setCurrentIndex(current.row());
setCurrentModelIndex(current);
emit NewItemSelected(selected_data.toInt());
}
});
// New text typed in
if (editable)
{
connect(lineEdit(), &QLineEdit::returnPressed, [this]()
{
emit TextChanged(this->lineEdit()->text());
});
}
// Connect view to combobox
setView(view);
view->viewport()->installEventFilter(this);
@ -701,6 +709,11 @@ void C4DeepQComboBox::setCurrentModelIndex(QModelIndex new_index)
{
setRootModelIndex(new_index.parent());
setCurrentIndex(new_index.row());
// Adjust text
if (editable)
{
lineEdit()->setText(this->model()->data(new_index, ValueStringRole).toString());
}
}
int32_t C4DeepQComboBox::GetCurrentSelectionIndex()
@ -799,7 +812,7 @@ void C4PropertyDelegateEnumEditor::paintEvent(QPaintEvent *ev)
/* Enumeration (dropdown list) delegate */
C4PropertyDelegateEnum::C4PropertyDelegateEnum(const C4PropertyDelegateFactory *factory, C4PropList *props, const C4ValueArray *poptions)
: C4PropertyDelegate(factory, props)
: C4PropertyDelegate(factory, props), allow_editing(false)
{
// Build enum options from C4Value definitions in script
if (!poptions && props) poptions = props->GetPropertyArray(P_Options);
@ -808,6 +821,7 @@ C4PropertyDelegateEnum::C4PropertyDelegateEnum(const C4PropertyDelegateFactory *
{
default_option_key = props->GetPropertyStr(P_OptionKey);
default_value_key = props->GetPropertyStr(P_ValueKey);
allow_editing = props->GetPropertyBool(P_AllowEditing);
}
if (poptions)
{
@ -880,6 +894,11 @@ QStandardItemModel *C4PropertyDelegateEnum::CreateOptionModel() const
new_item->setData(QString((opt.help ? opt.help : opt.name)->GetCStr()), Qt::ToolTipRole);
if (opt.help) new_item->setData(QIcon(":/editor/res/Help.png"), Qt::DecorationRole);
if (opt.sound_name) new_item->setData(QIcon(":/editor/res/Sound.png"), Qt::DecorationRole);
if (allow_editing)
{
C4String *s = opt.value.getStr();
new_item->setData(QString(s ? s->GetCStr() : ""), C4DeepQComboBox::ValueStringRole);
}
parent->appendRow(new_item);
++idx;
}
@ -1125,7 +1144,7 @@ QWidget *C4PropertyDelegateEnum::CreateEditor(const C4PropertyDelegateFactory *p
editor->layout->setMargin(0);
editor->layout->setSpacing(0);
editor->updating = true;
editor->option_box = new C4DeepQComboBox(editor, GetOptionComboBoxButtonType());
editor->option_box = new C4DeepQComboBox(editor, GetOptionComboBoxButtonType(), allow_editing);
editor->layout->addWidget(editor->option_box);
for (auto &option : options) editor->option_box->addItem(option.name->GetCStr());
connect(editor->option_box, &C4DeepQComboBox::NewItemSelected, editor, [editor, this](int32_t newval) {

View File

@ -232,6 +232,7 @@ class C4DeepQComboBox : public QComboBox
{
Q_OBJECT
bool editable;
bool is_next_close_blocked;
int last_popup_height;
std::unique_ptr<C4StyledItemDelegateWithButton> item_delegate;
@ -242,9 +243,10 @@ public:
{
OptionIndexRole = Qt::UserRole + 1,
ObjectHighlightRole = Qt::UserRole + 2,
ValueStringRole = Qt::UserRole + 3,
};
C4DeepQComboBox(QWidget *parent, C4StyledItemDelegateWithButton::ButtonType button_type);
C4DeepQComboBox(QWidget *parent, C4StyledItemDelegateWithButton::ButtonType button_type, bool editable);
void showPopup() override;
void hidePopup() override;
@ -255,6 +257,7 @@ public:
signals:
void NewItemSelected(int32_t new_item);
void TextChanged(const QString new_text);
protected:
// event filter for view: Catch mouse clicks to descend into children
@ -321,6 +324,7 @@ protected:
private:
std::vector<Option> options;
bool allow_editing;
protected:
void ClearOptions();

View File

@ -287,6 +287,7 @@ C4StringTable::C4StringTable()
P[P_EmptyName] = "EmptyName";
P[P_EditorHelp] = "EditorHelp";
P[P_Description] = "Description";
P[P_AllowEditing] = "AllowEditing";
P[DFA_WALK] = "WALK";
P[DFA_FLIGHT] = "FLIGHT";
P[DFA_KNEEL] = "KNEEL";

View File

@ -511,6 +511,7 @@ enum C4PropertyName
P_EmptyName,
P_EditorHelp,
P_Description,
P_AllowEditing,
// Default Action Procedures
DFA_WALK,
DFA_FLIGHT,