Qt Editor: Add EditorHelp for enum options

qteditor
Sven Eberhardt 2016-07-25 01:05:15 -04:00
parent ee88a36f73
commit 4246a72485
2 changed files with 67 additions and 55 deletions

View File

@ -552,14 +552,52 @@ QColor C4PropertyDelegateColor::GetDisplayBackgroundColor(const C4Value &val, cl
return static_cast<uint32_t>(val.getInt()) & 0xffffff;
}
/* Enum delegate combo box item delegate */
bool C4StyledItemDelegateWithHelpButton::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
{
// Mouse move over a cell: Display tooltip if over help button
if (event->type() == QEvent::MouseMove)
{
QVariant help_btn = model->data(index, Qt::DecorationRole);
if (!help_btn.isNull())
{
QMouseEvent *mevent = static_cast<QMouseEvent *>(event);
if (option.rect.contains(mevent->localPos().toPoint()))
{
if (mevent->localPos().x() >= option.rect.x() + option.rect.width() - option.rect.height())
{
QString tooltip_text = model->data(index, Qt::ToolTipRole).toString();
QToolTip::showText(mevent->globalPos(), tooltip_text);
}
}
}
}
return QStyledItemDelegate::editorEvent(event, model, option, index);
}
void C4StyledItemDelegateWithHelpButton::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
// Paint icon on the right
QStyleOptionViewItem override_option = option;
override_option.decorationPosition = QStyleOptionViewItem::Right;
QStyledItemDelegate::paint(painter, override_option, index);
}
/* Enum delegate combo box */
C4DeepQComboBox::C4DeepQComboBox(QWidget *parent)
: QComboBox(parent), descending(false), item_clicked(false), last_popup_height(0)
: QComboBox(parent), descending(false), item_clicked(false), last_popup_height(0), item_delegate(new C4StyledItemDelegateWithHelpButton())
{
QTreeView *view = new QTreeView(this);
view->setFrameShape(QFrame::NoFrame);
view->setSelectionBehavior(QTreeView::SelectRows);
view->setAllColumnsShowFocus(true);
view->header()->hide();
view->setItemDelegate(item_delegate.get());
// On expansion, enlarge view if necessery
connect(view, &QTreeView::expanded, this, [this, view](const QModelIndex &index)
{
@ -589,12 +627,16 @@ C4DeepQComboBox::C4DeepQComboBox(QWidget *parent)
// Connect view to combobox
setView(view);
view->viewport()->installEventFilter(this);
// No help icons in main box, unless dropped down
default_icon_size = iconSize();
setIconSize(QSize(0, 0));
}
void C4DeepQComboBox::showPopup()
{
// New selection: Reset to root of model
setRootModelIndex(QModelIndex());
setIconSize(default_icon_size);
QComboBox::showPopup();
view()->setMinimumWidth(200); // prevent element list from becoming too small in nested dialogues
if (last_popup_height && view()->parentWidget()) view()->parentWidget()->resize(view()->parentWidget()->width(), last_popup_height);
@ -626,6 +668,7 @@ void C4DeepQComboBox::hidePopup()
setRootModelIndex(current.parent());
setCurrentIndex(current.row());
::Console.EditCursor.SetHighlightedObject(nullptr);
setIconSize(QSize(0, 0));
QComboBox::hidePopup();
if (selected_data.type() == QVariant::Int)
{
@ -707,6 +750,7 @@ C4PropertyDelegateEnum::C4PropertyDelegateEnum(const C4PropertyDelegateFactory *
Option option;
option.name = props->GetPropertyStr(P_Name);
if (!option.name.Get()) option.name = ::Strings.RegString("???");
option.help = props->GetPropertyStr(P_EditorHelp);
option.group = props->GetPropertyStr(P_Group);
option.value_key = props->GetPropertyStr(P_ValueKey);
if (!option.value_key) option.value_key = default_value_key;
@ -763,6 +807,8 @@ QStandardItemModel *C4PropertyDelegateEnum::CreateOptionModel() const
new_item->setData(QVariant(idx), C4DeepQComboBox::OptionIndexRole);
C4Object *item_obj_data = opt.value.getObj();
if (item_obj_data) new_item->setData(QVariant(item_obj_data->Number), C4DeepQComboBox::ObjectHighlightRole);
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);
parent->appendRow(new_item);
++idx;
}
@ -1588,40 +1634,6 @@ bool C4PropertyDelegateFactory::CheckCurrentEditor(C4PropertyDelegate *d, QWidge
}
/* Property name view */
void C4PropertyNameDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
// Paint icon on the right
QStyleOptionViewItem override_option = option;
override_option.decorationPosition = QStyleOptionViewItem::Right;
QStyledItemDelegate::paint(painter, override_option, index);
}
bool C4PropertyNameDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
{
// Mouse move over a cell: Display tooltip if over help button
if (event->type() == QEvent::MouseMove)
{
QVariant help_btn = property_model->data(index, Qt::DecorationRole);
if (!help_btn.isNull())
{
QMouseEvent *mevent = static_cast<QMouseEvent *>(event);
if (option.rect.contains(mevent->localPos().toPoint()))
{
if (mevent->localPos().x() >= option.rect.x() + option.rect.width() - option.rect.height())
{
QString tooltip_text = property_model->data(index, Qt::ToolTipRole).toString();
QToolTip::showText(mevent->globalPos(), tooltip_text);
}
}
}
}
return QStyledItemDelegate::editorEvent(event, model, option, index);
}
/* Proplist table view */
C4ConsoleQtPropListModel::C4ConsoleQtPropListModel(C4PropertyDelegateFactory *delegate_factory)

View File

@ -206,6 +206,20 @@ public:
QColor GetDisplayBackgroundColor(const C4Value &val, class C4Object *obj) const override;
};
// Display delegate for deep combo box. Handles the help tooltip showing.
class C4StyledItemDelegateWithHelpButton : public QStyledItemDelegate
{
Q_OBJECT
public:
C4StyledItemDelegateWithHelpButton() { }
protected:
// Model callbacks forwarded to actual delegates
bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) override;
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
};
// A combo box that can select from a model with nested elements
// On click, descend into child elements
class C4DeepQComboBox : public QComboBox
@ -214,6 +228,8 @@ class C4DeepQComboBox : public QComboBox
bool descending, item_clicked;
int last_popup_height;
std::unique_ptr<C4StyledItemDelegateWithHelpButton> item_delegate;
QSize default_icon_size;
public:
enum
@ -261,20 +277,6 @@ public:
void paintEvent(QPaintEvent *) override;
};
// widget shown if a shape delegate is a child of an enum delegate
/*class C4PropertyDelegateEnumShapeParameterDisplayWidget : QWidget
{
Q_OBJECT
const C4PropertyDelegateShape *shape_delegate;
public:
C4PropertyDelegateEnumShapeParameterDisplayWidget(QWidget *parent, const C4PropertyDelegateShape *shape_delegate)
: shape_delegate(shape_delegate);
virtual void paintEvent(QPaintEvent *);
};*/
class C4PropertyDelegateEnum : public C4PropertyDelegate
{
Q_OBJECT
@ -286,6 +288,7 @@ public:
{
public:
C4RefCntPointer<C4String> name; // Display name in Editor enum dropdown box
C4RefCntPointer<C4String> help; // Tooltip text
C4RefCntPointer<C4String> group; // Grouping in enum dropdown box; nested groups separated by '/'
C4RefCntPointer<C4String> option_key;
C4RefCntPointer<C4String> value_key;
@ -479,7 +482,9 @@ protected:
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
};
class C4PropertyNameDelegate : public QStyledItemDelegate
// Delegate for the name column of the property window
// For now, just use the default + help button
class C4PropertyNameDelegate : public C4StyledItemDelegateWithHelpButton
{
Q_OBJECT
@ -489,11 +494,6 @@ public:
C4PropertyNameDelegate() : property_model(nullptr) { }
void SetPropertyModel(class C4ConsoleQtPropListModel *new_property_model) { property_model = new_property_model; }
protected:
// Model callbacks forwarded to actual delegates
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override { return nullptr; } // no editing
bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) override;
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
};
// One property in the prop list model view