2013-03-05 06:26:28 -06:00
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2011-2012 Statoil ASA, Ceetron 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 "RiuMultiCaseImportDialog.h"
# include "ui_RiuMultiCaseImportDialog.h"
# include <QFileSystemModel>
# include <QFileDialog>
# include <QStringListModel>
# include <QFileIconProvider>
2013-03-22 10:24:42 -05:00
# include "RiaApplication.h"
2013-03-05 06:26:28 -06:00
class FileListModel : public QStringListModel
{
public :
FileListModel ( QObject * parent = 0 ) : m_isItemsEditable ( false ) , QStringListModel ( parent )
{
}
virtual Qt : : ItemFlags flags ( const QModelIndex & index ) const
{
if ( m_isItemsEditable )
return Qt : : ItemIsSelectable | Qt : : ItemIsEnabled | Qt : : ItemIsEditable ;
else
return Qt : : ItemIsSelectable | Qt : : ItemIsEnabled ;
}
virtual QVariant data ( const QModelIndex & index , int role ) const
{
if ( role = = Qt : : DecorationRole )
{
QFileInfo fileInfo ( stringList ( ) [ index . row ( ) ] ) ;
QFileIconProvider iconProv ;
return QVariant ( iconProv . icon ( fileInfo ) ) ;
}
else
{
return QStringListModel : : data ( index , role ) ;
}
}
2015-07-01 03:05:12 -05:00
void removeFileNames ( const QModelIndexList & indexes )
2015-06-29 08:00:12 -05:00
{
for ( int i = indexes . size ( ) - 1 ; i > = 0 ; i - - )
{
removeRow ( indexes [ i ] . row ( ) , indexes [ i ] . parent ( ) ) ;
}
}
2013-03-05 06:26:28 -06:00
void setItemsEditable ( bool isEditable )
{
m_isItemsEditable = isEditable ;
}
private :
bool m_isItemsEditable ;
} ;
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RiuMultiCaseImportDialog : : RiuMultiCaseImportDialog ( QWidget * parent /*= 0*/ )
: QDialog ( parent )
{
ui = new Ui : : RiuMultiCaseImportDialog ;
ui - > setupUi ( this ) ;
m_searchFolders = new FileListModel ( this ) ;
ui - > m_searchFolderList - > setModel ( m_searchFolders ) ;
m_eclipseGridFiles = new FileListModel ( this ) ;
ui - > m_eclipseCasesList - > setModel ( m_eclipseGridFiles ) ;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RiuMultiCaseImportDialog : : ~ RiuMultiCaseImportDialog ( )
{
delete ui ;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuMultiCaseImportDialog : : on_m_addSearchFolderButton_clicked ( )
{
2016-09-21 02:46:17 -05:00
QString selectedFolder = QFileDialog : : getExistingDirectory ( this , " Select an Eclipse case search folder " , RiaApplication : : instance ( ) - > lastUsedDialogDirectory ( " MULTICASEIMPORT " ) ) ;
2013-03-05 06:26:28 -06:00
QStringList folderNames = m_searchFolders - > stringList ( ) ;
2013-03-05 07:45:29 -06:00
2013-03-05 06:26:28 -06:00
if ( ! folderNames . contains ( selectedFolder ) )
{
folderNames . push_back ( selectedFolder ) ;
m_searchFolders - > setStringList ( folderNames ) ;
updateGridFileList ( ) ;
}
2016-09-21 02:46:17 -05:00
RiaApplication : : instance ( ) - > setLastUsedDialogDirectory ( " MULTICASEIMPORT " , selectedFolder ) ;
2016-07-15 06:56:41 -05:00
QPushButton * okButton = ui - > m_dialogButtons - > button ( QDialogButtonBox : : Ok ) ;
if ( okButton )
{
okButton - > setFocus ( Qt : : OtherFocusReason ) ;
}
2013-03-05 06:26:28 -06:00
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuMultiCaseImportDialog : : on_m_removeSearchFolderButton_clicked ( )
{
QModelIndexList selection = ui - > m_searchFolderList - > selectionModel ( ) - > selectedIndexes ( ) ;
2013-03-05 07:45:29 -06:00
QStringList folderNames = m_searchFolders - > stringList ( ) ;
QStringList searchFoldersToRemove ;
2013-03-05 06:26:28 -06:00
for ( int i = 0 ; i < selection . size ( ) ; + + i )
{
2013-03-05 07:45:29 -06:00
searchFoldersToRemove . push_back ( folderNames [ selection [ i ] . row ( ) ] ) ;
2013-03-05 06:26:28 -06:00
}
2013-03-05 07:45:29 -06:00
for ( int i = 0 ; i < searchFoldersToRemove . size ( ) ; + + i )
{
folderNames . removeOne ( searchFoldersToRemove [ i ] ) ;
}
m_searchFolders - > setStringList ( folderNames ) ;
2013-03-05 06:26:28 -06:00
if ( selection . size ( ) )
{
updateGridFileList ( ) ;
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuMultiCaseImportDialog : : updateGridFileList ( )
{
QStringList folderNames = m_searchFolders - > stringList ( ) ;
QStringList gridFileNames ;
2013-03-05 07:45:29 -06:00
// Filter the search folders to remove subfolders of existing roots'
QStringList searchFoldersToRemove ;
for ( int i = 0 ; i < folderNames . size ( ) ; + + i )
for ( int j = 0 ; j < folderNames . size ( ) ; + + j )
{
if ( i ! = j )
{
if ( folderNames [ i ] . startsWith ( folderNames [ j ] ) )
{
// Remove folderNames[i]
searchFoldersToRemove . push_back ( folderNames [ i ] ) ;
}
}
}
// Remove the subfolders when adding a root
for ( int i = 0 ; i < searchFoldersToRemove . size ( ) ; + + i )
{
folderNames . removeOne ( searchFoldersToRemove [ i ] ) ;
}
2013-03-05 06:26:28 -06:00
for ( int i = 0 ; i < folderNames . size ( ) ; i + + )
{
QString folderName = folderNames [ i ] ;
appendEGRIDFilesRecursively ( folderName , gridFileNames ) ;
}
m_eclipseGridFiles - > setStringList ( gridFileNames ) ;
}
void RiuMultiCaseImportDialog : : appendEGRIDFilesRecursively ( const QString & folderName , QStringList & gridFileNames )
{
{
QDir baseDir ( folderName ) ;
baseDir . setFilter ( QDir : : Files ) ;
QStringList nameFilters ;
nameFilters < < " *.egrid " < < " .EGRID " ;
baseDir . setNameFilters ( nameFilters ) ;
QStringList fileNames = baseDir . entryList ( ) ;
for ( int i = 0 ; i < fileNames . size ( ) ; + + i )
{
QString fileName = fileNames [ i ] ;
QString absoluteFolderName = baseDir . absoluteFilePath ( fileName ) ;
gridFileNames . append ( absoluteFolderName ) ;
}
}
{
QDir baseDir ( folderName ) ;
baseDir . setFilter ( QDir : : Dirs | QDir : : NoDotAndDotDot ) ;
QStringList subFolders = baseDir . entryList ( ) ;
for ( int i = 0 ; i < subFolders . size ( ) ; + + i )
{
QString folderName = subFolders [ i ] ;
QString absoluteFolderName = baseDir . absoluteFilePath ( folderName ) ;
appendEGRIDFilesRecursively ( absoluteFolderName , gridFileNames ) ;
}
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QStringList RiuMultiCaseImportDialog : : eclipseCaseFileNames ( ) const
{
return m_eclipseGridFiles - > stringList ( ) ;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuMultiCaseImportDialog : : on_m_removeEclipseCaseButton_clicked ( )
{
QModelIndexList selection = ui - > m_eclipseCasesList - > selectionModel ( ) - > selectedIndexes ( ) ;
2013-03-05 07:45:29 -06:00
if ( selection . size ( ) )
2013-03-05 06:26:28 -06:00
{
2015-06-29 08:00:12 -05:00
FileListModel * dataModel = static_cast < FileListModel * > ( ui - > m_eclipseCasesList - > model ( ) ) ;
Q_ASSERT ( dataModel ) ;
2013-03-05 07:45:29 -06:00
2015-07-01 03:05:12 -05:00
dataModel - > removeFileNames ( selection ) ;
2013-03-05 06:26:28 -06:00
}
}