2015-08-30 06:30:46 -05:00
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2015- Statoil ASA
// Copyright (C) 2015- Ceetron Solutions AS
//
// 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 "RicLinkVisibleViewsFeature.h"
# include "RiaApplication.h"
2015-09-02 07:22:36 -05:00
# include "RicLinkVisibleViewsFeatureUi.h"
2015-09-11 06:46:37 -05:00
# include "RimViewLink.h"
2015-08-30 06:30:46 -05:00
# include "RimProject.h"
# include "RimView.h"
2015-09-07 07:29:46 -05:00
# include "RimViewLinker.h"
2015-09-08 03:17:35 -05:00
# include "RimViewLinkerCollection.h"
2015-08-30 06:30:46 -05:00
# include "RiuMainWindow.h"
2015-09-02 07:22:36 -05:00
# include "cafPdmUiPropertyViewDialog.h"
2015-08-30 06:30:46 -05:00
# include "cafPdmUiTreeView.h"
# include <QAction>
# include <QTreeView>
2015-09-02 07:22:36 -05:00
# include <QMessageBox>
2015-08-30 06:30:46 -05:00
CAF_CMD_SOURCE_INIT ( RicLinkVisibleViewsFeature , " RicLinkVisibleViewsFeature " ) ;
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RicLinkVisibleViewsFeature : : isCommandEnabled ( )
{
RimProject * proj = RiaApplication : : instance ( ) - > project ( ) ;
std : : vector < RimView * > views ;
proj - > allVisibleViews ( views ) ;
if ( views . size ( ) > 1 ) return true ;
return false ;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicLinkVisibleViewsFeature : : onActionTriggered ( bool isChecked )
{
RimProject * proj = RiaApplication : : instance ( ) - > project ( ) ;
2015-09-02 07:22:36 -05:00
2015-08-30 06:30:46 -05:00
std : : vector < RimView * > views ;
2015-09-02 07:22:36 -05:00
findNotLinkedVisibleViews ( views ) ;
if ( views . size ( ) < 2 )
{
2015-09-07 05:27:00 -05:00
QMessageBox : : warning ( RiuMainWindow : : instance ( ) , " Link Visible Views " , " Less than two views are available for linking. Please open at least two not linked views before creating a new linked views group. " ) ;
2015-09-02 07:22:36 -05:00
return ;
}
2015-09-02 06:44:56 -05:00
RicLinkVisibleViewsFeatureUi featureUi ;
featureUi . setViews ( views ) ;
2015-09-07 05:14:50 -05:00
caf : : PdmUiPropertyViewDialog propertyDialog ( NULL , & featureUi , " Select Master View " , " " ) ;
2015-09-02 06:44:56 -05:00
propertyDialog . setWindowIcon ( QIcon ( " :/chain.png " ) ) ;
if ( propertyDialog . exec ( ) ! = QDialog : : Accepted ) return ;
RimView * masterView = featureUi . masterView ( ) ;
2015-09-07 07:29:46 -05:00
RimViewLinker * linkedViews = new RimViewLinker ;
2015-09-02 06:44:56 -05:00
linkedViews - > setMainView ( masterView ) ;
2015-09-01 10:14:22 -05:00
2015-09-02 06:44:56 -05:00
for ( size_t i = 0 ; i < views . size ( ) ; i + + )
2015-08-30 06:30:46 -05:00
{
RimView * rimView = views [ i ] ;
2015-09-02 06:44:56 -05:00
if ( rimView = = masterView ) continue ;
2015-09-02 01:13:17 -05:00
2015-09-15 02:47:59 -05:00
RimViewLink * viewLink = new RimViewLink ;
viewLink - > setManagedView ( rimView ) ;
2015-09-02 01:13:17 -05:00
2015-09-15 02:47:59 -05:00
linkedViews - > viewLinks . push_back ( viewLink ) ;
2015-08-30 06:30:46 -05:00
2015-09-15 02:47:59 -05:00
viewLink - > initAfterReadRecursively ( ) ;
viewLink - > updateOptionSensitivity ( ) ;
2015-09-15 03:45:39 -05:00
viewLink - > updateUiIconFromActiveState ( ) ;
2015-08-30 06:30:46 -05:00
}
2015-09-08 03:17:35 -05:00
proj - > viewLinkerCollection ( ) - > viewLinkers ( ) . push_back ( linkedViews ) ;
linkedViews - > updateUiIcon ( ) ;
proj - > viewLinkerCollection . uiCapability ( ) - > updateConnectedEditors ( ) ;
2015-09-01 10:14:22 -05:00
linkedViews - > applyAllOperations ( ) ;
2015-09-07 07:01:19 -05:00
proj - > updateConnectedEditors ( ) ;
2015-08-30 06:30:46 -05:00
// Set managed view collection to selected and expanded in project tree
caf : : PdmUiTreeView * projTreeView = RiuMainWindow : : instance ( ) - > projectTreeView ( ) ;
2015-09-01 10:14:22 -05:00
QModelIndex modIndex = projTreeView - > findModelIndex ( linkedViews ) ;
2015-08-30 06:30:46 -05:00
projTreeView - > treeView ( ) - > setCurrentIndex ( modIndex ) ;
projTreeView - > treeView ( ) - > setExpanded ( modIndex , true ) ;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicLinkVisibleViewsFeature : : setupActionLook ( QAction * actionToSetup )
{
actionToSetup - > setText ( " Link Visible Views " ) ;
actionToSetup - > setIcon ( QIcon ( " :/chain.png " ) ) ;
}
2015-09-02 07:22:36 -05:00
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicLinkVisibleViewsFeature : : allLinkedViews ( std : : vector < RimView * > & views )
{
RimProject * proj = RiaApplication : : instance ( ) - > project ( ) ;
2015-09-08 03:17:35 -05:00
for ( size_t i = 0 ; i < proj - > viewLinkerCollection ( ) - > viewLinkers ( ) . size ( ) ; i + + )
2015-09-02 07:22:36 -05:00
{
2015-09-08 03:17:35 -05:00
RimViewLinker * linkedViews = proj - > viewLinkerCollection ( ) - > viewLinkers ( ) [ i ] ;
2015-09-02 07:22:36 -05:00
linkedViews - > allViews ( views ) ;
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicLinkVisibleViewsFeature : : findNotLinkedVisibleViews ( std : : vector < RimView * > & views )
{
RimProject * proj = RiaApplication : : instance ( ) - > project ( ) ;
std : : vector < RimView * > alreadyLinkedViews ;
allLinkedViews ( alreadyLinkedViews ) ;
std : : vector < RimView * > visibleViews ;
proj - > allVisibleViews ( visibleViews ) ;
bool anyAlreadyLinkedViews = false ;
for ( size_t i = 0 ; i < visibleViews . size ( ) ; i + + )
{
bool isLinked = false ;
for ( size_t j = 0 ; j < alreadyLinkedViews . size ( ) ; j + + )
{
if ( visibleViews [ i ] = = alreadyLinkedViews [ j ] )
{
anyAlreadyLinkedViews = true ;
isLinked = true ;
}
}
if ( ! isLinked )
{
views . push_back ( visibleViews [ i ] ) ;
}
}
if ( anyAlreadyLinkedViews )
{
2015-09-07 05:27:00 -05:00
QMessageBox : : warning ( RiuMainWindow : : instance ( ) , " Link Visible Views " ,
" Detected one or more visible view(s) already part of a Linked View Group. \n These views were removed from the list views to be linked. " ) ;
2015-09-02 07:22:36 -05:00
}
}