2012-11-22 13:30:02 +01:00
# include "editor.hpp"
2013-08-21 17:14:29 -05:00
# include <QApplication>
2013-09-03 01:44:21 -05:00
# include <QLocalServer>
# include <QLocalSocket>
2013-09-08 14:31:20 +02:00
# include <QMessageBox>
2012-11-22 13:30:02 +01:00
2012-11-26 12:29:22 +01:00
# include "model/doc/document.hpp"
# include "model/world/data.hpp"
2013-09-10 16:45:01 +02:00
2013-09-27 11:36:06 +02:00
CS : : Editor : : Editor ( )
: mDocumentManager ( mCfgMgr . getUserPath ( ) / " projects " ) , mViewManager ( mDocumentManager )
2012-11-22 13:30:02 +01:00
{
2013-09-05 23:02:51 -05:00
mIpcServerName = " org.openmw.OpenCS " ;
2013-09-03 04:12:19 -05:00
2013-09-10 16:45:01 +02:00
setupDataFiles ( ) ;
mNewGame . setLocalData ( mLocal ) ;
2013-09-23 11:26:29 +02:00
mFileDialog . setLocalData ( mLocal ) ;
2013-09-10 16:45:01 +02:00
2013-09-08 12:06:28 +02:00
connect ( & mViewManager , SIGNAL ( newGameRequest ( ) ) , this , SLOT ( createGame ( ) ) ) ;
connect ( & mViewManager , SIGNAL ( newAddonRequest ( ) ) , this , SLOT ( createAddon ( ) ) ) ;
2013-02-02 16:14:58 +01:00
connect ( & mViewManager , SIGNAL ( loadDocumentRequest ( ) ) , this , SLOT ( loadDocument ( ) ) ) ;
2013-09-08 09:33:45 +02:00
connect ( & mViewManager , SIGNAL ( editSettingsRequest ( ) ) , this , SLOT ( showSettings ( ) ) ) ;
2013-02-02 16:14:58 +01:00
2013-09-08 12:06:28 +02:00
connect ( & mStartup , SIGNAL ( createGame ( ) ) , this , SLOT ( createGame ( ) ) ) ;
connect ( & mStartup , SIGNAL ( createAddon ( ) ) , this , SLOT ( createAddon ( ) ) ) ;
2013-02-02 16:14:58 +01:00
connect ( & mStartup , SIGNAL ( loadDocument ( ) ) , this , SLOT ( loadDocument ( ) ) ) ;
2013-09-08 09:26:43 +02:00
connect ( & mStartup , SIGNAL ( editConfig ( ) ) , this , SLOT ( showSettings ( ) ) ) ;
2013-02-07 12:52:01 +01:00
2013-03-07 03:00:59 +01:00
connect ( & mFileDialog , SIGNAL ( openFiles ( ) ) , this , SLOT ( openFiles ( ) ) ) ;
2013-09-23 11:58:11 +02:00
connect ( & mFileDialog , SIGNAL ( createNewFile ( const boost : : filesystem : : path & ) ) ,
this , SLOT ( createNewFile ( const boost : : filesystem : : path & ) ) ) ;
2013-03-07 03:00:59 +01:00
2013-09-10 16:45:01 +02:00
connect ( & mNewGame , SIGNAL ( createRequest ( const boost : : filesystem : : path & ) ) ,
this , SLOT ( createNewGame ( const boost : : filesystem : : path & ) ) ) ;
2012-11-22 13:30:02 +01:00
}
2013-03-07 03:00:59 +01:00
void CS : : Editor : : setupDataFiles ( )
2012-11-22 13:30:02 +01:00
{
2013-03-07 03:00:59 +01:00
boost : : program_options : : variables_map variables ;
boost : : program_options : : options_description desc ;
2013-02-02 16:14:58 +01:00
2013-03-07 03:00:59 +01:00
desc . add_options ( )
( " data " , boost : : program_options : : value < Files : : PathContainer > ( ) - > default_value ( Files : : PathContainer ( ) , " data " ) - > multitoken ( ) )
( " data-local " , boost : : program_options : : value < std : : string > ( ) - > default_value ( " " ) )
( " fs-strict " , boost : : program_options : : value < bool > ( ) - > implicit_value ( true ) - > default_value ( false ) )
( " encoding " , boost : : program_options : : value < std : : string > ( ) - > default_value ( " win1252 " ) ) ;
2013-02-02 16:14:58 +01:00
2013-03-07 03:00:59 +01:00
boost : : program_options : : notify ( variables ) ;
2012-11-23 14:05:49 +01:00
2013-03-07 03:00:59 +01:00
mCfgMgr . readConfiguration ( variables , desc ) ;
2012-11-23 14:05:49 +01:00
2013-09-08 14:31:20 +02:00
Files : : PathContainer dataDirs , dataLocal ;
2013-03-07 03:00:59 +01:00
if ( ! variables [ " data " ] . empty ( ) ) {
2013-09-08 14:31:20 +02:00
dataDirs = Files : : PathContainer ( variables [ " data " ] . as < Files : : PathContainer > ( ) ) ;
2013-03-07 03:00:59 +01:00
}
2013-02-04 13:46:54 +01:00
2013-03-07 03:00:59 +01:00
std : : string local = variables [ " data-local " ] . as < std : : string > ( ) ;
if ( ! local . empty ( ) ) {
2013-09-08 14:31:20 +02:00
dataLocal . push_back ( Files : : PathContainer : : value_type ( local ) ) ;
2013-03-07 03:00:59 +01:00
}
2012-11-26 12:29:22 +01:00
2013-09-08 14:31:20 +02:00
mCfgMgr . processPaths ( dataDirs ) ;
mCfgMgr . processPaths ( dataLocal , true ) ;
if ( ! dataLocal . empty ( ) )
mLocal = dataLocal [ 0 ] ;
else
{
QMessageBox messageBox ;
messageBox . setWindowTitle ( tr ( " No local data path available " ) ) ;
messageBox . setIcon ( QMessageBox : : Critical ) ;
messageBox . setStandardButtons ( QMessageBox : : Ok ) ;
messageBox . setText ( tr ( " <br><b>OpenCS is unable to access the local data directory. This may indicate a faulty configuration or a broken install.</b> " ) ) ;
messageBox . exec ( ) ;
QApplication : : exit ( 1 ) ;
return ;
}
2013-03-07 03:00:59 +01:00
// Set the charset for reading the esm/esp files
2013-09-21 23:06:29 -05:00
// QString encoding = QString::fromStdString(variables["encoding"].as<std::string>());
//mFileDialog.setEncoding(encoding);
2013-03-07 03:00:59 +01:00
2013-09-08 14:31:20 +02:00
dataDirs . insert ( dataDirs . end ( ) , dataLocal . begin ( ) , dataLocal . end ( ) ) ;
2013-03-07 03:00:59 +01:00
for ( Files : : PathContainer : : const_iterator iter = dataDirs . begin ( ) ; iter ! = dataDirs . end ( ) ; + + iter )
{
QString path = QString : : fromStdString ( iter - > string ( ) ) ;
mFileDialog . addFiles ( path ) ;
}
2013-06-12 12:36:35 +02:00
2013-06-20 18:06:25 -05:00
//load the settings into the userSettings instance.
const QString settingFileName = " opencs.cfg " ;
CSMSettings : : UserSettings : : instance ( ) . loadSettings ( settingFileName ) ;
2013-03-07 03:00:59 +01:00
}
2013-09-08 12:06:28 +02:00
void CS : : Editor : : createGame ( )
{
mStartup . hide ( ) ;
if ( mNewGame . isHidden ( ) )
mNewGame . show ( ) ;
mNewGame . raise ( ) ;
mNewGame . activateWindow ( ) ;
}
void CS : : Editor : : createAddon ( )
2013-03-07 03:00:59 +01:00
{
mStartup . hide ( ) ;
mFileDialog . newFile ( ) ;
2013-02-02 16:14:58 +01:00
}
void CS : : Editor : : loadDocument ( )
{
2013-02-07 13:11:41 +01:00
mStartup . hide ( ) ;
2013-03-07 03:00:59 +01:00
mFileDialog . openFile ( ) ;
2013-02-05 22:06:36 +01:00
}
2013-02-02 16:14:58 +01:00
2013-02-05 22:06:36 +01:00
void CS : : Editor : : openFiles ( )
{
2013-03-07 03:00:59 +01:00
std : : vector < boost : : filesystem : : path > files ;
QStringList paths = mFileDialog . checkedItemsPaths ( ) ;
foreach ( const QString & path , paths ) {
files . push_back ( path . toStdString ( ) ) ;
}
2013-09-10 16:45:01 +02:00
/// \todo Get the save path from the file dialogue
CSMDoc : : Document * document = mDocumentManager . addDocument ( files , * files . rbegin ( ) , false ) ;
2013-02-07 12:52:01 +01:00
2012-11-22 13:30:02 +01:00
mViewManager . addView ( document ) ;
2013-03-07 03:00:59 +01:00
mFileDialog . hide ( ) ;
}
2013-09-23 11:58:11 +02:00
void CS : : Editor : : createNewFile ( const boost : : filesystem : : path & savePath )
2013-03-07 03:00:59 +01:00
{
std : : vector < boost : : filesystem : : path > files ;
QStringList paths = mFileDialog . checkedItemsPaths ( ) ;
foreach ( const QString & path , paths ) {
files . push_back ( path . toStdString ( ) ) ;
}
files . push_back ( mFileDialog . fileName ( ) . toStdString ( ) ) ;
2013-09-23 11:58:11 +02:00
CSMDoc : : Document * document = mDocumentManager . addDocument ( files , savePath , true ) ;
2013-02-07 12:52:01 +01:00
2012-11-22 13:30:02 +01:00
mViewManager . addView ( document ) ;
2013-03-07 03:00:59 +01:00
mFileDialog . hide ( ) ;
2012-11-22 13:30:02 +01:00
}
2013-09-10 16:45:01 +02:00
void CS : : Editor : : createNewGame ( const boost : : filesystem : : path & file )
2013-09-08 14:31:20 +02:00
{
std : : vector < boost : : filesystem : : path > files ;
2013-09-10 16:45:01 +02:00
files . push_back ( file ) ;
2013-09-08 14:31:20 +02:00
2013-09-10 16:45:01 +02:00
CSMDoc : : Document * document = mDocumentManager . addDocument ( files , file , true ) ;
2013-09-08 14:31:20 +02:00
mViewManager . addView ( document ) ;
mNewGame . hide ( ) ;
}
2013-09-03 04:12:19 -05:00
void CS : : Editor : : showStartup ( )
{
2013-09-05 23:02:51 -05:00
if ( mStartup . isHidden ( ) )
mStartup . show ( ) ;
mStartup . raise ( ) ;
mStartup . activateWindow ( ) ;
2013-09-03 04:12:19 -05:00
}
2013-09-08 09:26:43 +02:00
void CS : : Editor : : showSettings ( )
{
if ( mSettings . isHidden ( ) )
mSettings . show ( ) ;
mSettings . raise ( ) ;
mSettings . activateWindow ( ) ;
}
2013-09-03 01:44:21 -05:00
bool CS : : Editor : : makeIPCServer ( )
{
2013-09-05 23:02:51 -05:00
mServer = new QLocalServer ( this ) ;
2013-09-03 04:12:19 -05:00
2013-09-05 23:02:51 -05:00
if ( mServer - > listen ( mIpcServerName ) )
{
connect ( mServer , SIGNAL ( newConnection ( ) ) , this , SLOT ( showStartup ( ) ) ) ;
return true ;
}
2013-09-03 04:12:19 -05:00
2013-09-05 23:02:51 -05:00
mServer - > close ( ) ;
return false ;
2013-09-03 01:44:21 -05:00
}
2013-09-03 04:12:19 -05:00
void CS : : Editor : : connectToIPCServer ( )
{
2013-09-05 23:02:51 -05:00
mClientSocket = new QLocalSocket ( this ) ;
mClientSocket - > connectToServer ( mIpcServerName ) ;
mClientSocket - > close ( ) ;
2013-09-03 04:12:19 -05:00
}
2012-11-22 13:30:02 +01:00
int CS : : Editor : : run ( )
{
2013-09-08 14:31:20 +02:00
if ( mLocal . empty ( ) )
return 1 ;
2013-02-02 16:14:58 +01:00
mStartup . show ( ) ;
2012-11-22 13:30:02 +01:00
2013-03-03 15:58:26 -06:00
QApplication : : setQuitOnLastWindowClosed ( true ) ;
2012-11-22 13:30:02 +01:00
return QApplication : : exec ( ) ;
2013-03-03 15:58:26 -06:00
}