2015-08-30 13:30:46 +02: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 14:22:36 +02:00
#include "RicLinkVisibleViewsFeatureUi.h"
2015-09-01 17:14:22 +02:00
#include "RimLinkedViews.h"
2015-08-30 13:30:46 +02:00
#include "RimManagedViewConfig.h"
#include "RimProject.h"
#include "RimView.h"
#include "RiuMainWindow.h"
2015-09-02 14:22:36 +02:00
#include "cafPdmUiPropertyViewDialog.h"
2015-08-30 13:30:46 +02:00
#include "cafPdmUiTreeView.h"
#include <QAction>
#include <QTreeView>
2015-09-02 14:22:36 +02:00
#include <QMessageBox>
2015-08-30 13:30:46 +02: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 14:22:36 +02:00
2015-08-30 13:30:46 +02:00
std :: vector < RimView *> views ;
2015-09-02 14:22:36 +02:00
findNotLinkedVisibleViews ( views );
if ( views . size () < 2 )
{
QMessageBox :: warning ( RiuMainWindow :: instance (), "Link Visible Views" , "Less than two views available for linking. Please open at least two not linked views before creating a new link group." );
return ;
}
2015-09-02 13:44:56 +02:00
RicLinkVisibleViewsFeatureUi featureUi ;
featureUi . setViews ( views );
2015-09-02 14:22:36 +02:00
caf :: PdmUiPropertyViewDialog propertyDialog ( NULL , & featureUi , "Link Visible Views" , "" );
2015-09-02 13:44:56 +02:00
propertyDialog . setWindowIcon ( QIcon ( ":/chain.png" ));
if ( propertyDialog . exec () != QDialog :: Accepted ) return ;
RimView * masterView = featureUi . masterView ();
2015-09-01 17:14:22 +02:00
RimLinkedViews * linkedViews = new RimLinkedViews ;
2015-09-02 13:44:56 +02:00
linkedViews -> setMainView ( masterView );
2015-09-01 17:14:22 +02:00
2015-09-02 13:44:56 +02:00
for ( size_t i = 0 ; i < views . size (); i ++ )
2015-08-30 13:30:46 +02:00
{
RimView * rimView = views [ i ];
2015-09-02 13:44:56 +02:00
if ( rimView == masterView ) continue ;
2015-09-02 08:13:17 +02:00
2015-08-30 13:30:46 +02:00
RimManagedViewConfig * viewConfig = new RimManagedViewConfig ;
2015-09-02 13:44:56 +02:00
viewConfig -> setManagedView ( rimView );
2015-09-02 08:13:17 +02:00
2015-09-01 17:14:22 +02:00
linkedViews -> viewConfigs . push_back ( viewConfig );
2015-08-30 13:30:46 +02:00
viewConfig -> initAfterReadRecursively ();
2015-09-02 08:13:17 +02:00
viewConfig -> updateViewChanged ();
2015-08-30 13:30:46 +02:00
}
2015-09-01 17:14:22 +02:00
proj -> linkedViews . push_back ( linkedViews );
proj -> linkedViews . uiCapability () -> updateConnectedEditors ();
linkedViews -> applyAllOperations ();
linkedViews -> updateConnectedEditors ();
2015-08-30 13:30:46 +02:00
// Set managed view collection to selected and expanded in project tree
caf :: PdmUiTreeView * projTreeView = RiuMainWindow :: instance () -> projectTreeView ();
2015-09-01 17:14:22 +02:00
QModelIndex modIndex = projTreeView -> findModelIndex ( linkedViews );
2015-08-30 13:30:46 +02: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 14:22:36 +02:00
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicLinkVisibleViewsFeature :: allLinkedViews ( std :: vector < RimView *>& views )
{
RimProject * proj = RiaApplication :: instance () -> project ();
for ( size_t i = 0 ; i < proj -> linkedViews (). size (); i ++ )
{
RimLinkedViews * linkedViews = proj -> linkedViews ()[ i ];
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 )
{
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 of visible views." );
}
}