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
@ -68,12 +68,14 @@ void PdmUiCheckBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem
|
||||
QStyleOptionViewItemV4 viewItemOption(option);
|
||||
|
||||
const int textMargin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1;
|
||||
QRect newRect = QStyle::alignedRect(option.direction, Qt::AlignCenter,
|
||||
QSize(option.decorationSize.width() + 5,option.decorationSize.height()),
|
||||
QRect(option.rect.x() + textMargin, option.rect.y(),
|
||||
option.rect.width() - (2 * textMargin), option.rect.height()));
|
||||
QRect newRect = QStyle::alignedRect(option.direction,
|
||||
Qt::AlignCenter,
|
||||
QSize(option.decorationSize.width() + 5, option.rect.height()),
|
||||
QRect(option.rect.x(),
|
||||
option.rect.y(),
|
||||
option.rect.width() - (2 * textMargin),
|
||||
option.rect.height()));
|
||||
viewItemOption.rect = newRect;
|
||||
|
||||
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
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
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(model);
|
||||
@ -100,9 +104,11 @@ bool PdmUiCheckBoxDelegate::editorEvent(QEvent *event, QAbstractItemModel *model
|
||||
if (event->type() == QEvent::MouseButtonRelease)
|
||||
{
|
||||
const int textMargin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1;
|
||||
QRect checkRect = QStyle::alignedRect(option.direction, Qt::AlignCenter,
|
||||
QRect checkRect = QStyle::alignedRect(option.direction,
|
||||
Qt::AlignCenter,
|
||||
option.decorationSize,
|
||||
QRect(option.rect.x() + (2 * textMargin), option.rect.y(),
|
||||
QRect(option.rect.x() + (2 * textMargin),
|
||||
option.rect.y(),
|
||||
option.rect.width() - (2 * textMargin),
|
||||
option.rect.height()));
|
||||
|
||||
@ -116,7 +122,7 @@ bool PdmUiCheckBoxDelegate::editorEvent(QEvent *event, QAbstractItemModel *model
|
||||
}
|
||||
else
|
||||
{
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
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_selectionRole = SelectionManager::CURRENT;
|
||||
m_isBlockingSelectionManagerChanged = false;
|
||||
}
|
||||
|
||||
|
||||
@ -172,6 +173,15 @@ void PdmUiTableViewEditor::configureAndUpdateUi(const QString& uiConfigName)
|
||||
if (!m_tableModelPdm) return;
|
||||
|
||||
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);
|
||||
|
||||
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)
|
||||
{
|
||||
if (isSelectionRoleDefined())
|
||||
{
|
||||
std::vector<PdmUiItem*> items;
|
||||
QModelIndexList list;
|
||||
list.append(current);
|
||||
selectedUiItems(list, items);
|
||||
|
||||
SelectionManager::instance()->setSelectedItems(items, m_selectionRole);
|
||||
}
|
||||
//if (isSelectionRoleDefined())
|
||||
//{
|
||||
// std::vector<PdmUiItem*> items;
|
||||
// QModelIndexList list;
|
||||
// list.append(current);
|
||||
// selectedUiItems(list, items);
|
||||
//
|
||||
// SelectionManager::instance()->setSelectedItems(items, m_selectionRole);
|
||||
//}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
@ -271,7 +291,16 @@ void PdmUiTableViewEditor::handleModelSelectionChange()
|
||||
{
|
||||
std::vector<PdmUiItem*> items;
|
||||
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
|
||||
if (items.size() == 1)
|
||||
{
|
||||
@ -282,6 +311,7 @@ void PdmUiTableViewEditor::handleModelSelectionChange()
|
||||
m_tableView->selectionModel()->select(itemSelection, QItemSelectionModel::SelectCurrent);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
@ -343,7 +373,7 @@ void PdmUiTableViewEditor::tableViewWidgetFocusChanged(QEvent* focusEvent)
|
||||
{
|
||||
if (focusEvent->type() == QEvent::FocusIn)
|
||||
{
|
||||
updateSelectionManagerFromTableSelection();
|
||||
//updateSelectionManagerFromTableSelection();
|
||||
}
|
||||
else if (focusEvent->type() == QEvent::FocusOut)
|
||||
{
|
||||
@ -361,6 +391,7 @@ void PdmUiTableViewEditor::updateSelectionManagerFromTableSelection()
|
||||
{
|
||||
if (isSelectionRoleDefined())
|
||||
{
|
||||
#if 0
|
||||
std::vector<PdmUiItem*> items;
|
||||
|
||||
QModelIndexList modelIndexList = m_tableView->selectionModel()->selectedIndexes();
|
||||
@ -380,6 +411,26 @@ void PdmUiTableViewEditor::updateSelectionManagerFromTableSelection()
|
||||
// Multiple selection of fields is handled here
|
||||
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;
|
||||
};
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
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
|
||||
CAF_PDM_UI_FIELD_EDITOR_HEADER_INIT;
|
||||
@ -105,6 +121,7 @@ public:
|
||||
|
||||
protected:
|
||||
virtual void configureAndUpdateUi(const QString& uiConfigName) override;
|
||||
virtual void onSelectionManagerSelectionChanged() override;
|
||||
|
||||
private:
|
||||
void selectedUiItems(const QModelIndexList& modelIndexList, std::vector<PdmUiItem*>& objects);
|
||||
@ -132,6 +149,7 @@ private:
|
||||
|
||||
bool m_useDefaultContextMenu;
|
||||
SelectionManager::SelectionRole m_selectionRole;
|
||||
bool m_isBlockingSelectionManagerChanged;
|
||||
};
|
||||
|
||||
|
||||
|
@ -152,8 +152,6 @@ bool PdmUiTableViewQModel::setData(const QModelIndex &index, const QVariant &val
|
||||
{
|
||||
if (isRepresentingBoolean(index))
|
||||
{
|
||||
// Clear current selection, UI does not behave well for multiple selection
|
||||
SelectionManager::instance()->clear(SelectionManager::CURRENT);
|
||||
|
||||
bool toggleOn = (value == Qt::Checked);
|
||||
|
||||
@ -657,8 +655,8 @@ QItemSelection PdmUiTableViewQModel::modelIndexFromPdmObject(PdmObjectHandle* pd
|
||||
PdmObjectHandle* obj = this->pdmObjectForRow(i);
|
||||
if (obj == pdmObject)
|
||||
{
|
||||
// Currently selection only on model index, can be extended to select whole row
|
||||
itemSelection.select(this->createIndex(i, 0), this->createIndex(i, 0));
|
||||
// Select whole row
|
||||
itemSelection.select(this->createIndex(i, 0), this->createIndex(i, this->columnCount()));
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user