ResInsight/ApplicationLibCode/UserInterface/RiuSummaryVectorSelectionWidgetCreator.cpp
Magne Sjaastad 2814b92055
Qt6: Adjustments (#11804)
* Qt6: Avoid insertWidget, use addWidget
In Qt6, the insertWidget function checks if the index parameter is valid based on current widgets present in the layout. This is error prone, and use addWidget to avoid manual counting of index.

* Disable use of Qt keyword foreach

* Replace use of QRegExp with QRegularExpression
Replace use of QRegExp with QRegularExpression
Remove dependency on qt5compat module
Simplify an expression based on review

* Remove Qt5 ifdefs

* Guard access out of bounds seen in debug build

* Avoid reuse of string variable

* Disconnect all signals from the QOpenGLContext
The call stack when this assert happens indicates that there are more signals to be disconnected from the object. Crash is fixed by disconnecting all signals.

Assert seen in debug build:

ASSERT failure in caf::Viewer: "Called object is not of the correct type (class destructor may have already run)", file C:\Qt\6.6.3\msvc2019_64\include\QtCore/qobjectdefs_impl.h, line 130

* Fix issue related to delete of a linked view
Guard null pointer use in view linker. Remove complicated cleanup in destructor in Rim3dVew.
2024-10-28 13:09:18 +01:00

221 lines
9.1 KiB
C++

/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2017- Statoil ASA
//
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#include "RiuSummaryVectorSelectionWidgetCreator.h"
#include "RiuSummaryCurveDefinitionKeywords.h"
#include "RiuSummaryVectorSelectionUi.h"
#include "cafPdmUiFieldEditorHandle.h"
#include "cafPdmUiGroup.h"
#include "QMinimizePanel.h"
#include <QBoxLayout>
#include <QFrame>
#include <QSplitter>
#include <memory>
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RiuSummaryVectorSelectionWidgetCreator::RiuSummaryVectorSelectionWidgetCreator()
{
m_summaryAddressSelection = std::make_unique<RiuSummaryVectorSelectionUi>();
setPdmObject( m_summaryAddressSelection.get() );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RiuSummaryVectorSelectionWidgetCreator::~RiuSummaryVectorSelectionWidgetCreator()
{
setPdmObject( nullptr );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RiuSummaryVectorSelectionUi* RiuSummaryVectorSelectionWidgetCreator::summaryAddressSelection() const
{
return m_summaryAddressSelection.get();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuSummaryVectorSelectionWidgetCreator::recursivelyConfigureAndUpdateTopLevelUiOrdering( const caf::PdmUiOrdering& topLevelUiOrdering,
const QString& uiConfigName )
{
if ( !m_firstRowLeftLayout || !m_firstRowRightLayout ) return;
const std::vector<caf::PdmUiItem*>& topLevelUiItems = topLevelUiOrdering.uiItems();
for ( size_t i = 0; i < topLevelUiItems.size(); ++i )
{
if ( topLevelUiItems[i]->isUiHidden( uiConfigName ) ) continue;
if ( topLevelUiItems[i]->isUiGroup() )
{
caf::PdmUiGroup* group = static_cast<caf::PdmUiGroup*>( topLevelUiItems[i] );
auto groupBox = createGroupBoxWithContent( group, uiConfigName );
bool isSources = group->keyword() == RiuSummaryCurveDefinitionKeywords::sources();
bool isSummaryTypes = group->keyword() == RiuSummaryCurveDefinitionKeywords::summaryTypes();
bool isSummaries = group->keyword() == RiuSummaryCurveDefinitionKeywords::summaries();
bool isDynamicGroup = !isSources && !isSummaryTypes && !isSummaries;
bool leftColumn = isSources || isSummaryTypes;
if ( isSummaryTypes || isDynamicGroup )
{
groupBox->setFixedWidth( 170 );
}
if ( leftColumn )
m_firstRowLeftLayout->addWidget( groupBox );
else
m_firstRowRightLayout->addWidget( groupBox );
// Add group boxes until summaries are detected
if ( group->keyword() == RiuSummaryCurveDefinitionKeywords::summaries() ) break;
}
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QWidget* RiuSummaryVectorSelectionWidgetCreator::createWidget( QWidget* parent )
{
QWidget* widget = new QWidget( parent );
QVBoxLayout* mainLayout = new QVBoxLayout();
mainLayout->setContentsMargins( 5, 5, 5, 5 );
widget->setLayout( mainLayout );
QFrame* firstRowFrame = new QFrame( widget );
QHBoxLayout* firstRowLayout = new QHBoxLayout;
firstRowLayout->setContentsMargins( 0, 0, 0, 0 );
firstRowFrame->setLayout( firstRowLayout );
QFrame* firstRowLeftFrame = new QFrame( widget );
m_firstRowLeftLayout = new QHBoxLayout;
m_firstRowLeftLayout->setContentsMargins( 0, 0, 0, 0 );
firstRowLeftFrame->setLayout( m_firstRowLeftLayout );
QFrame* firstRowRightFrame = new QFrame( widget );
m_firstRowRightLayout = new QHBoxLayout;
m_firstRowRightLayout->setContentsMargins( 0, 0, 0, 0 );
firstRowRightFrame->setLayout( m_firstRowRightLayout );
QSplitter* rowSplitter = new QSplitter( Qt::Horizontal );
rowSplitter->setContentsMargins( 0, 0, 0, 0 );
rowSplitter->setHandleWidth( 6 );
rowSplitter->setStyleSheet( "QSplitter::handle { image: url(:/SplitterV.png); }" );
rowSplitter->addWidget( firstRowLeftFrame );
rowSplitter->addWidget( firstRowRightFrame );
rowSplitter->setSizes( QList<int>() << 1 << 1 );
firstRowLayout->addWidget( rowSplitter );
mainLayout->addWidget( rowSplitter );
return widget;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuSummaryVectorSelectionWidgetCreator::configureAndUpdateFields( int widgetStartIndex,
QBoxLayout* layout,
const std::vector<caf::PdmUiItem*>& uiItems,
const QString& uiConfigName )
{
int currentWidgetIndex = widgetStartIndex;
for ( size_t i = 0; i < uiItems.size(); ++i )
{
if ( uiItems[i]->isUiHidden( uiConfigName ) ) continue;
if ( uiItems[i]->isUiGroup() ) continue;
{
caf::PdmUiFieldHandle* field = dynamic_cast<caf::PdmUiFieldHandle*>( uiItems[i] );
caf::PdmUiFieldEditorHandle* fieldEditor = findOrCreateFieldEditor( widget(), field, uiConfigName );
if ( fieldEditor )
{
// Place the widget(s) into the correct parent and layout
QWidget* fieldCombinedWidget = fieldEditor->combinedWidget();
if ( fieldCombinedWidget )
{
fieldCombinedWidget->setParent( widget() );
layout->insertWidget( currentWidgetIndex++, fieldCombinedWidget );
}
else
{
caf::PdmUiItemInfo::LabelPosType labelPos = field->uiLabelPosition( uiConfigName );
QWidget* fieldEditorWidget = fieldEditor->editorWidget();
if ( labelPos != caf::PdmUiItemInfo::HIDDEN )
{
QWidget* fieldLabelWidget = fieldEditor->labelWidget();
if ( fieldLabelWidget )
{
fieldLabelWidget->setParent( widget() );
layout->insertWidget( currentWidgetIndex++, fieldLabelWidget );
fieldLabelWidget->show();
}
}
else
{
QWidget* fieldLabelWidget = fieldEditor->labelWidget();
if ( fieldLabelWidget ) fieldLabelWidget->hide();
}
if ( fieldEditorWidget )
{
fieldEditorWidget->setParent( widget() ); // To make sure this widget has the current
// group box as parent.
layout->insertWidget( currentWidgetIndex++, fieldEditorWidget );
}
}
fieldEditor->updateUi( uiConfigName );
}
}
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QMinimizePanel* RiuSummaryVectorSelectionWidgetCreator::createGroupBoxWithContent( caf::PdmUiGroup* group, const QString& uiConfigName )
{
QMinimizePanel* groupBox = findOrCreateGroupBox( widget(), group, uiConfigName );
recursivelyConfigureAndUpdateUiOrderingInGridLayout( *group, groupBox->contentFrame(), uiConfigName );
return groupBox;
}