2015-08-06 10:52:10 +00:00
# include "merge.hpp"
# include <QVBoxLayout>
# include <QDialogButtonBox>
# include <QSplitter>
# include <QPushButton>
# include <QListWidget>
# include <QLabel>
2015-08-13 10:53:00 +00:00
# include <QKeyEvent>
2015-08-06 10:52:10 +00:00
# include "../../model/doc/document.hpp"
2015-08-16 13:24:48 +00:00
# include "../../model/doc/documentmanager.hpp"
2022-08-19 17:19:42 +00:00
# include "../../model/doc/state.hpp"
2015-08-06 10:52:10 +00:00
# include "../doc/filewidget.hpp"
# include "../doc/adjusterwidget.hpp"
2015-08-13 10:53:00 +00:00
void CSVTools : : Merge : : keyPressEvent ( QKeyEvent * event )
{
if ( event - > key ( ) = = Qt : : Key_Escape )
{
event - > accept ( ) ;
cancel ( ) ;
}
else
QWidget : : keyPressEvent ( event ) ;
}
2015-08-16 13:24:48 +00:00
CSVTools : : Merge : : Merge ( CSMDoc : : DocumentManager & documentManager , QWidget * parent )
2020-11-13 07:39:47 +00:00
: QWidget ( parent ) , mDocument ( nullptr ) , mDocumentManager ( documentManager )
2015-08-06 10:52:10 +00:00
{
setWindowTitle ( " Merge Content Files into a new Game File " ) ;
QVBoxLayout * mainLayout = new QVBoxLayout ;
setLayout ( mainLayout ) ;
QSplitter * splitter = new QSplitter ( Qt : : Horizontal , this ) ;
mainLayout - > addWidget ( splitter , 1 ) ;
// left panel (files to be merged)
QWidget * left = new QWidget ( this ) ;
left - > setContentsMargins ( 0 , 0 , 0 , 0 ) ;
splitter - > addWidget ( left ) ;
QVBoxLayout * leftLayout = new QVBoxLayout ;
left - > setLayout ( leftLayout ) ;
leftLayout - > addWidget ( new QLabel ( " Files to be merged " , this ) ) ;
mFiles = new QListWidget ( this ) ;
leftLayout - > addWidget ( mFiles , 1 ) ;
// right panel (new game file)
QWidget * right = new QWidget ( this ) ;
right - > setContentsMargins ( 0 , 0 , 0 , 0 ) ;
splitter - > addWidget ( right ) ;
QVBoxLayout * rightLayout = new QVBoxLayout ;
rightLayout - > setAlignment ( Qt : : AlignTop ) ;
right - > setLayout ( rightLayout ) ;
rightLayout - > addWidget ( new QLabel ( " New game file " , this ) ) ;
mNewFile = new CSVDoc : : FileWidget ( this ) ;
mNewFile - > setType ( false ) ;
mNewFile - > extensionLabelIsVisible ( true ) ;
rightLayout - > addWidget ( mNewFile ) ;
mAdjuster = new CSVDoc : : AdjusterWidget ( this ) ;
rightLayout - > addWidget ( mAdjuster ) ;
2022-08-23 02:28:58 +00:00
connect ( mNewFile , & CSVDoc : : FileWidget : : nameChanged , mAdjuster , & CSVDoc : : AdjusterWidget : : setName ) ;
connect ( mAdjuster , & CSVDoc : : AdjusterWidget : : stateChanged , this , & Merge : : stateChanged ) ;
2015-08-06 10:52:10 +00:00
// buttons
QDialogButtonBox * buttons = new QDialogButtonBox ( QDialogButtonBox : : Cancel , Qt : : Horizontal , this ) ;
2022-08-23 02:28:58 +00:00
connect ( buttons - > button ( QDialogButtonBox : : Cancel ) , & QPushButton : : clicked , this , & Merge : : cancel ) ;
2015-08-13 10:03:20 +00:00
2015-08-06 10:52:10 +00:00
mOkay = new QPushButton ( " Merge " , this ) ;
2022-08-23 02:28:58 +00:00
connect ( mOkay , & QPushButton : : clicked , this , & Merge : : accept ) ;
2015-08-06 10:52:10 +00:00
mOkay - > setDefault ( true ) ;
buttons - > addButton ( mOkay , QDialogButtonBox : : AcceptRole ) ;
mainLayout - > addWidget ( buttons ) ;
}
void CSVTools : : Merge : : configure ( CSMDoc : : Document * document )
{
mDocument = document ;
mNewFile - > setName ( " " ) ;
// content files
while ( mFiles - > count ( ) )
delete mFiles - > takeItem ( 0 ) ;
2022-06-08 21:25:50 +00:00
std : : vector < std : : filesystem : : path > files = document - > getContentFiles ( ) ;
2015-08-06 10:52:10 +00:00
2022-06-08 21:25:50 +00:00
for ( std : : vector < std : : filesystem : : path > : : const_iterator iter ( files . begin ( ) ) ;
2015-08-06 10:52:10 +00:00
iter ! = files . end ( ) ; + + iter )
2022-06-19 11:28:33 +00:00
mFiles - > addItem ( QString : : fromUtf8 ( iter - > filename ( ) . string ( ) . c_str ( ) ) ) ; //TODO(Project579): let's hope unicode characters are never used in these filenames on windows or this will break
2015-08-06 10:52:10 +00:00
}
2022-06-08 21:25:50 +00:00
void CSVTools : : Merge : : setLocalData ( const std : : filesystem : : path & localData )
2015-08-06 10:52:10 +00:00
{
mAdjuster - > setLocalData ( localData ) ;
}
CSMDoc : : Document * CSVTools : : Merge : : getDocument ( ) const
{
return mDocument ;
}
void CSVTools : : Merge : : cancel ( )
{
2020-11-13 07:39:47 +00:00
mDocument = nullptr ;
2015-08-06 10:52:10 +00:00
hide ( ) ;
}
void CSVTools : : Merge : : accept ( )
{
2015-08-13 10:03:20 +00:00
if ( ( mDocument - > getState ( ) & CSMDoc : : State_Merging ) = = 0 )
{
2022-06-08 21:25:50 +00:00
std : : vector < std : : filesystem : : path > files ( 1 , mAdjuster - > getPath ( ) ) ;
2015-08-16 13:24:48 +00:00
2017-04-28 15:30:26 +00:00
std : : unique_ptr < CSMDoc : : Document > target (
2015-08-16 13:24:48 +00:00
mDocumentManager . makeDocument ( files , files [ 0 ] , true ) ) ;
2017-04-28 15:30:26 +00:00
mDocument - > runMerge ( std : : move ( target ) ) ;
2015-08-16 13:24:48 +00:00
2015-08-13 10:03:20 +00:00
hide ( ) ;
}
2015-08-06 10:52:10 +00:00
}
void CSVTools : : Merge : : stateChanged ( bool valid )
{
mOkay - > setEnabled ( valid ) ;
}