mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
AppFwk: Add editor attribute. Object-only SelectionManager use. Use SelectionChangedReceiver. Improve tableview-checkbox selection behaviour and look.
This commit is contained in:
parent
08c78471c3
commit
88297bdb9f
@ -66,14 +66,16 @@ PdmUiCheckBoxDelegate::~PdmUiCheckBoxDelegate()
|
|||||||
void PdmUiCheckBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
|
void PdmUiCheckBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
|
||||||
{
|
{
|
||||||
QStyleOptionViewItemV4 viewItemOption(option);
|
QStyleOptionViewItemV4 viewItemOption(option);
|
||||||
|
|
||||||
const int textMargin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1;
|
const int textMargin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1;
|
||||||
QRect newRect = QStyle::alignedRect(option.direction, Qt::AlignCenter,
|
QRect newRect = QStyle::alignedRect(option.direction,
|
||||||
QSize(option.decorationSize.width() + 5,option.decorationSize.height()),
|
Qt::AlignCenter,
|
||||||
QRect(option.rect.x() + textMargin, option.rect.y(),
|
QSize(option.decorationSize.width() + 5, option.rect.height()),
|
||||||
option.rect.width() - (2 * textMargin), option.rect.height()));
|
QRect(option.rect.x(),
|
||||||
|
option.rect.y(),
|
||||||
|
option.rect.width() - (2 * textMargin),
|
||||||
|
option.rect.height()));
|
||||||
viewItemOption.rect = newRect;
|
viewItemOption.rect = newRect;
|
||||||
|
|
||||||
QStyledItemDelegate::paint(painter, viewItemOption, index);
|
QStyledItemDelegate::paint(painter, viewItemOption, index);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -81,7 +83,9 @@ void PdmUiCheckBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem
|
|||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
/// Returns true to avoid other factories to produce editors for a check box
|
/// Returns true to avoid other factories to produce editors for a check box
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
bool PdmUiCheckBoxDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
|
bool PdmUiCheckBoxDelegate::editorEvent(QEvent *event, QAbstractItemModel *model,
|
||||||
|
const QStyleOptionViewItem &option,
|
||||||
|
const QModelIndex &index)
|
||||||
{
|
{
|
||||||
Q_ASSERT(event);
|
Q_ASSERT(event);
|
||||||
Q_ASSERT(model);
|
Q_ASSERT(model);
|
||||||
@ -100,11 +104,13 @@ bool PdmUiCheckBoxDelegate::editorEvent(QEvent *event, QAbstractItemModel *model
|
|||||||
if (event->type() == QEvent::MouseButtonRelease)
|
if (event->type() == QEvent::MouseButtonRelease)
|
||||||
{
|
{
|
||||||
const int textMargin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1;
|
const int textMargin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1;
|
||||||
QRect checkRect = QStyle::alignedRect(option.direction, Qt::AlignCenter,
|
QRect checkRect = QStyle::alignedRect(option.direction,
|
||||||
option.decorationSize,
|
Qt::AlignCenter,
|
||||||
QRect(option.rect.x() + (2 * textMargin), option.rect.y(),
|
option.decorationSize,
|
||||||
option.rect.width() - (2 * textMargin),
|
QRect(option.rect.x() + (2 * textMargin),
|
||||||
option.rect.height()));
|
option.rect.y(),
|
||||||
|
option.rect.width() - (2 * textMargin),
|
||||||
|
option.rect.height()));
|
||||||
|
|
||||||
if (!checkRect.contains(static_cast<QMouseEvent*>(event)->pos()))
|
if (!checkRect.contains(static_cast<QMouseEvent*>(event)->pos()))
|
||||||
return true;
|
return true;
|
||||||
@ -116,7 +122,7 @@ bool PdmUiCheckBoxDelegate::editorEvent(QEvent *event, QAbstractItemModel *model
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return true;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Qt::CheckState state = (static_cast<Qt::CheckState>(value.toInt()) == Qt::Checked ? Qt::Unchecked : Qt::Checked);
|
Qt::CheckState state = (static_cast<Qt::CheckState>(value.toInt()) == Qt::Checked ? Qt::Unchecked : Qt::Checked);
|
||||||
|
@ -105,6 +105,7 @@ PdmUiTableViewEditor::PdmUiTableViewEditor()
|
|||||||
m_checkboxDelegate = new PdmUiCheckBoxDelegate(this);
|
m_checkboxDelegate = new PdmUiCheckBoxDelegate(this);
|
||||||
|
|
||||||
m_selectionRole = SelectionManager::CURRENT;
|
m_selectionRole = SelectionManager::CURRENT;
|
||||||
|
m_isBlockingSelectionManagerChanged = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -172,6 +173,15 @@ void PdmUiTableViewEditor::configureAndUpdateUi(const QString& uiConfigName)
|
|||||||
if (!m_tableModelPdm) return;
|
if (!m_tableModelPdm) return;
|
||||||
|
|
||||||
auto childArrayFH = childArrayFieldHandle();
|
auto childArrayFH = childArrayFieldHandle();
|
||||||
|
|
||||||
|
if ( childArrayFH && childArrayFH->ownerObject() && childArrayFH->ownerObject()->uiCapability() )
|
||||||
|
{
|
||||||
|
PdmUiTableViewEditorAttribute editorAttrib;
|
||||||
|
childArrayFH->ownerObject()->uiCapability()->editorAttribute(childArrayFH, uiConfigName, &editorAttrib);
|
||||||
|
this->setSelectionRole(editorAttrib.selectionRole);
|
||||||
|
this->enableHeaderText(editorAttrib.enableHeaderText);
|
||||||
|
}
|
||||||
|
|
||||||
m_tableModelPdm->setPdmData(childArrayFH, uiConfigName);
|
m_tableModelPdm->setPdmData(childArrayFH, uiConfigName);
|
||||||
|
|
||||||
if (m_tableModelPdm->rowCount() > 0)
|
if (m_tableModelPdm->rowCount() > 0)
|
||||||
@ -204,6 +214,16 @@ void PdmUiTableViewEditor::configureAndUpdateUi(const QString& uiConfigName)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
///
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
void PdmUiTableViewEditor::onSelectionManagerSelectionChanged()
|
||||||
|
{
|
||||||
|
if (m_isBlockingSelectionManagerChanged) return;
|
||||||
|
|
||||||
|
handleModelSelectionChange();
|
||||||
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
///
|
///
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
@ -251,15 +271,15 @@ void PdmUiTableViewEditor::setSelectionRole(SelectionManager::SelectionRole role
|
|||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
void PdmUiTableViewEditor::slotCurrentChanged(const QModelIndex & current, const QModelIndex & previous)
|
void PdmUiTableViewEditor::slotCurrentChanged(const QModelIndex & current, const QModelIndex & previous)
|
||||||
{
|
{
|
||||||
if (isSelectionRoleDefined())
|
//if (isSelectionRoleDefined())
|
||||||
{
|
//{
|
||||||
std::vector<PdmUiItem*> items;
|
// std::vector<PdmUiItem*> items;
|
||||||
QModelIndexList list;
|
// QModelIndexList list;
|
||||||
list.append(current);
|
// list.append(current);
|
||||||
selectedUiItems(list, items);
|
// selectedUiItems(list, items);
|
||||||
|
//
|
||||||
SelectionManager::instance()->setSelectedItems(items, m_selectionRole);
|
// SelectionManager::instance()->setSelectedItems(items, m_selectionRole);
|
||||||
}
|
//}
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
@ -271,7 +291,16 @@ void PdmUiTableViewEditor::handleModelSelectionChange()
|
|||||||
{
|
{
|
||||||
std::vector<PdmUiItem*> items;
|
std::vector<PdmUiItem*> items;
|
||||||
SelectionManager::instance()->selectedItems(items, m_selectionRole);
|
SelectionManager::instance()->selectedItems(items, m_selectionRole);
|
||||||
|
#if 1
|
||||||
|
QItemSelection totalSelection;
|
||||||
|
for (auto item: items)
|
||||||
|
{
|
||||||
|
PdmObject* pdmObj = dynamic_cast<PdmObject*>(item);
|
||||||
|
QItemSelection itemSelection = m_tableModelPdm->modelIndexFromPdmObject(pdmObj);
|
||||||
|
totalSelection.merge(itemSelection, QItemSelectionModel::Select);
|
||||||
|
}
|
||||||
|
m_tableView->selectionModel()->select(totalSelection, QItemSelectionModel::SelectCurrent);
|
||||||
|
#else
|
||||||
// TODO: Handle multiple selection
|
// TODO: Handle multiple selection
|
||||||
if (items.size() == 1)
|
if (items.size() == 1)
|
||||||
{
|
{
|
||||||
@ -282,6 +311,7 @@ void PdmUiTableViewEditor::handleModelSelectionChange()
|
|||||||
m_tableView->selectionModel()->select(itemSelection, QItemSelectionModel::SelectCurrent);
|
m_tableView->selectionModel()->select(itemSelection, QItemSelectionModel::SelectCurrent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -343,7 +373,7 @@ void PdmUiTableViewEditor::tableViewWidgetFocusChanged(QEvent* focusEvent)
|
|||||||
{
|
{
|
||||||
if (focusEvent->type() == QEvent::FocusIn)
|
if (focusEvent->type() == QEvent::FocusIn)
|
||||||
{
|
{
|
||||||
updateSelectionManagerFromTableSelection();
|
//updateSelectionManagerFromTableSelection();
|
||||||
}
|
}
|
||||||
else if (focusEvent->type() == QEvent::FocusOut)
|
else if (focusEvent->type() == QEvent::FocusOut)
|
||||||
{
|
{
|
||||||
@ -361,6 +391,7 @@ void PdmUiTableViewEditor::updateSelectionManagerFromTableSelection()
|
|||||||
{
|
{
|
||||||
if (isSelectionRoleDefined())
|
if (isSelectionRoleDefined())
|
||||||
{
|
{
|
||||||
|
#if 0
|
||||||
std::vector<PdmUiItem*> items;
|
std::vector<PdmUiItem*> items;
|
||||||
|
|
||||||
QModelIndexList modelIndexList = m_tableView->selectionModel()->selectedIndexes();
|
QModelIndexList modelIndexList = m_tableView->selectionModel()->selectedIndexes();
|
||||||
@ -380,6 +411,26 @@ void PdmUiTableViewEditor::updateSelectionManagerFromTableSelection()
|
|||||||
// Multiple selection of fields is handled here
|
// Multiple selection of fields is handled here
|
||||||
SelectionManager::instance()->setSelectedItems(items, m_selectionRole);
|
SelectionManager::instance()->setSelectedItems(items, m_selectionRole);
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
|
std::set<PdmUiItem*> selectedRowObjects;
|
||||||
|
QModelIndexList modelIndexList = m_tableView->selectionModel()->selectedIndexes();
|
||||||
|
for (const QModelIndex& mi : modelIndexList)
|
||||||
|
{
|
||||||
|
PdmObjectHandle* obj = m_tableModelPdm->pdmObjectForRow(mi.row());
|
||||||
|
|
||||||
|
if (obj && obj->uiCapability())
|
||||||
|
{
|
||||||
|
selectedRowObjects.insert(obj->uiCapability());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<PdmUiItem*> items{selectedRowObjects.begin(), selectedRowObjects.end()};
|
||||||
|
m_isBlockingSelectionManagerChanged = true;
|
||||||
|
SelectionManager::instance()->setSelectedItems(items, m_selectionRole);
|
||||||
|
m_isBlockingSelectionManagerChanged = false;
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -75,12 +75,28 @@ private:
|
|||||||
std::map<QString, QString> m_fieldKeywordAndPushButtonText;
|
std::map<QString, QString> m_fieldKeywordAndPushButtonText;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
///
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
class PdmUiTableViewEditorAttribute : public PdmUiEditorAttribute
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
PdmUiTableViewEditorAttribute()
|
||||||
|
: selectionRole(SelectionManager::CURRENT)
|
||||||
|
, enableHeaderText(true)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
SelectionManager::SelectionRole selectionRole;
|
||||||
|
bool enableHeaderText;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
///
|
///
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
class PdmUiTableViewEditor : public PdmUiFieldEditorHandle
|
class PdmUiTableViewEditor : public PdmUiFieldEditorHandle, public SelectionChangedReceiver
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
CAF_PDM_UI_FIELD_EDITOR_HEADER_INIT;
|
CAF_PDM_UI_FIELD_EDITOR_HEADER_INIT;
|
||||||
@ -105,6 +121,7 @@ public:
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void configureAndUpdateUi(const QString& uiConfigName) override;
|
virtual void configureAndUpdateUi(const QString& uiConfigName) override;
|
||||||
|
virtual void onSelectionManagerSelectionChanged() override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void selectedUiItems(const QModelIndexList& modelIndexList, std::vector<PdmUiItem*>& objects);
|
void selectedUiItems(const QModelIndexList& modelIndexList, std::vector<PdmUiItem*>& objects);
|
||||||
@ -132,6 +149,7 @@ private:
|
|||||||
|
|
||||||
bool m_useDefaultContextMenu;
|
bool m_useDefaultContextMenu;
|
||||||
SelectionManager::SelectionRole m_selectionRole;
|
SelectionManager::SelectionRole m_selectionRole;
|
||||||
|
bool m_isBlockingSelectionManagerChanged;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -152,8 +152,6 @@ bool PdmUiTableViewQModel::setData(const QModelIndex &index, const QVariant &val
|
|||||||
{
|
{
|
||||||
if (isRepresentingBoolean(index))
|
if (isRepresentingBoolean(index))
|
||||||
{
|
{
|
||||||
// Clear current selection, UI does not behave well for multiple selection
|
|
||||||
SelectionManager::instance()->clear(SelectionManager::CURRENT);
|
|
||||||
|
|
||||||
bool toggleOn = (value == Qt::Checked);
|
bool toggleOn = (value == Qt::Checked);
|
||||||
|
|
||||||
@ -657,8 +655,8 @@ QItemSelection PdmUiTableViewQModel::modelIndexFromPdmObject(PdmObjectHandle* pd
|
|||||||
PdmObjectHandle* obj = this->pdmObjectForRow(i);
|
PdmObjectHandle* obj = this->pdmObjectForRow(i);
|
||||||
if (obj == pdmObject)
|
if (obj == pdmObject)
|
||||||
{
|
{
|
||||||
// Currently selection only on model index, can be extended to select whole row
|
// Select whole row
|
||||||
itemSelection.select(this->createIndex(i, 0), this->createIndex(i, 0));
|
itemSelection.select(this->createIndex(i, 0), this->createIndex(i, this->columnCount()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user