2012-11-22 12:30:02 +00:00
# include "editor.hpp"
2013-08-21 22:14:29 +00:00
# include <QApplication>
2013-09-03 06:44:21 +00:00
# include <QLocalServer>
# include <QLocalSocket>
2013-09-08 12:31:20 +00:00
# include <QMessageBox>
2012-11-22 12:30:02 +00:00
2012-11-26 11:29:22 +00:00
# include "model/doc/document.hpp"
# include "model/world/data.hpp"
2013-09-10 14:45:01 +00:00
2013-03-07 02:00:59 +00:00
CS : : Editor : : Editor ( ) : mViewManager ( mDocumentManager )
2012-11-22 12:30:02 +00:00
{
2013-09-06 04:02:51 +00:00
mIpcServerName = " org.openmw.OpenCS " ;
2013-09-03 09:12:19 +00:00
2013-09-10 14:45:01 +00:00
setupDataFiles ( ) ;
mNewGame . setLocalData ( mLocal ) ;
2013-09-08 10:06:28 +00:00
connect ( & mViewManager , SIGNAL ( newGameRequest ( ) ) , this , SLOT ( createGame ( ) ) ) ;
connect ( & mViewManager , SIGNAL ( newAddonRequest ( ) ) , this , SLOT ( createAddon ( ) ) ) ;
2013-02-02 15:14:58 +00:00
connect ( & mViewManager , SIGNAL ( loadDocumentRequest ( ) ) , this , SLOT ( loadDocument ( ) ) ) ;
2013-09-08 07:33:45 +00:00
connect ( & mViewManager , SIGNAL ( editSettingsRequest ( ) ) , this , SLOT ( showSettings ( ) ) ) ;
2013-02-02 15:14:58 +00:00
2013-09-08 10:06:28 +00:00
connect ( & mStartup , SIGNAL ( createGame ( ) ) , this , SLOT ( createGame ( ) ) ) ;
connect ( & mStartup , SIGNAL ( createAddon ( ) ) , this , SLOT ( createAddon ( ) ) ) ;
2013-02-02 15:14:58 +00:00
connect ( & mStartup , SIGNAL ( loadDocument ( ) ) , this , SLOT ( loadDocument ( ) ) ) ;
2013-09-08 07:26:43 +00:00
connect ( & mStartup , SIGNAL ( editConfig ( ) ) , this , SLOT ( showSettings ( ) ) ) ;
2013-02-07 11:52:01 +00:00
2013-03-07 02:00:59 +00:00
connect ( & mFileDialog , SIGNAL ( openFiles ( ) ) , this , SLOT ( openFiles ( ) ) ) ;
connect ( & mFileDialog , SIGNAL ( createNewFile ( ) ) , this , SLOT ( createNewFile ( ) ) ) ;
2013-09-10 14:45:01 +00:00
connect ( & mNewGame , SIGNAL ( createRequest ( const boost : : filesystem : : path & ) ) ,
this , SLOT ( createNewGame ( const boost : : filesystem : : path & ) ) ) ;
2012-11-22 12:30:02 +00:00
}
2013-03-07 02:00:59 +00:00
void CS : : Editor : : setupDataFiles ( )
2012-11-22 12:30:02 +00:00
{
2013-03-07 02:00:59 +00:00
boost : : program_options : : variables_map variables ;
boost : : program_options : : options_description desc ;
2013-02-02 15:14:58 +00:00
2013-03-07 02:00:59 +00: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 15:14:58 +00:00
2013-03-07 02:00:59 +00:00
boost : : program_options : : notify ( variables ) ;
2012-11-23 13:05:49 +00:00
2013-03-07 02:00:59 +00:00
mCfgMgr . readConfiguration ( variables , desc ) ;
2012-11-23 13:05:49 +00:00
2013-09-08 12:31:20 +00:00
Files : : PathContainer dataDirs , dataLocal ;
2013-03-07 02:00:59 +00:00
if ( ! variables [ " data " ] . empty ( ) ) {
2013-09-08 12:31:20 +00:00
dataDirs = Files : : PathContainer ( variables [ " data " ] . as < Files : : PathContainer > ( ) ) ;
2013-03-07 02:00:59 +00:00
}
2013-02-04 12:46:54 +00:00
2013-03-07 02:00:59 +00:00
std : : string local = variables [ " data-local " ] . as < std : : string > ( ) ;
if ( ! local . empty ( ) ) {
2013-09-08 12:31:20 +00:00
dataLocal . push_back ( Files : : PathContainer : : value_type ( local ) ) ;
2013-03-07 02:00:59 +00:00
}
2012-11-26 11:29:22 +00:00
2013-09-08 12:31:20 +00: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 02:00:59 +00:00
// Set the charset for reading the esm/esp files
2013-09-22 04:06:29 +00:00
// QString encoding = QString::fromStdString(variables["encoding"].as<std::string>());
//mFileDialog.setEncoding(encoding);
2013-03-07 02:00:59 +00:00
2013-09-08 12:31:20 +00:00
dataDirs . insert ( dataDirs . end ( ) , dataLocal . begin ( ) , dataLocal . end ( ) ) ;
2013-03-07 02:00:59 +00: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 10:36:35 +00:00
2013-06-20 23:06:25 +00:00
//load the settings into the userSettings instance.
const QString settingFileName = " opencs.cfg " ;
CSMSettings : : UserSettings : : instance ( ) . loadSettings ( settingFileName ) ;
2013-03-07 02:00:59 +00:00
}
2013-09-08 10:06:28 +00:00
void CS : : Editor : : createGame ( )
{
mStartup . hide ( ) ;
if ( mNewGame . isHidden ( ) )
mNewGame . show ( ) ;
mNewGame . raise ( ) ;
mNewGame . activateWindow ( ) ;
}
void CS : : Editor : : createAddon ( )
2013-03-07 02:00:59 +00:00
{
mStartup . hide ( ) ;
2013-10-23 02:52:35 +00:00
mFileDialog . showDialog ( CSVDoc : : FileDialog : : DialogType_New ) ;
2013-02-02 15:14:58 +00:00
}
void CS : : Editor : : loadDocument ( )
{
2013-02-07 12:11:41 +00:00
mStartup . hide ( ) ;
2013-10-23 02:52:35 +00:00
mFileDialog . showDialog ( CSVDoc : : FileDialog : : DialogType_Open ) ;
2013-02-05 21:06:36 +00:00
}
2013-02-02 15:14:58 +00:00
2013-02-05 21:06:36 +00:00
void CS : : Editor : : openFiles ( )
{
2013-03-07 02:00:59 +00:00
std : : vector < boost : : filesystem : : path > files ;
2013-10-02 02:29:45 +00:00
foreach ( const QString & path , mFileDialog . selectedFilePaths ( ) ) {
2013-03-07 02:00:59 +00:00
files . push_back ( path . toStdString ( ) ) ;
}
2013-10-07 03:10:38 +00:00
foreach ( const boost : : filesystem : : path fp , files )
qDebug ( ) < < " loading files: " < < fp . c_str ( ) ;
2013-09-10 14:45:01 +00:00
/// \todo Get the save path from the file dialogue
CSMDoc : : Document * document = mDocumentManager . addDocument ( files , * files . rbegin ( ) , false ) ;
2013-02-07 11:52:01 +00:00
2012-11-22 12:30:02 +00:00
mViewManager . addView ( document ) ;
2013-09-29 17:19:07 +00:00
mFileDialog . close ( ) ;
2013-03-07 02:00:59 +00:00
}
void CS : : Editor : : createNewFile ( )
{
std : : vector < boost : : filesystem : : path > files ;
2013-10-02 02:29:45 +00:00
foreach ( const QString & path , mFileDialog . selectedFilePaths ( ) ) {
2013-03-07 02:00:59 +00:00
files . push_back ( path . toStdString ( ) ) ;
}
2013-09-29 17:19:07 +00:00
files . push_back ( mFileDialog . filename ( ) . toStdString ( ) ) ;
2013-03-07 02:00:59 +00:00
2013-09-10 14:45:01 +00:00
/// \todo Get the save path from the file dialogue.
CSMDoc : : Document * document = mDocumentManager . addDocument ( files , * files . rbegin ( ) , true ) ;
2013-02-07 11:52:01 +00:00
2012-11-22 12:30:02 +00:00
mViewManager . addView ( document ) ;
2013-09-29 17:19:07 +00:00
mFileDialog . close ( ) ;
2012-11-22 12:30:02 +00:00
}
2013-09-10 14:45:01 +00:00
void CS : : Editor : : createNewGame ( const boost : : filesystem : : path & file )
2013-09-08 12:31:20 +00:00
{
std : : vector < boost : : filesystem : : path > files ;
2013-09-10 14:45:01 +00:00
files . push_back ( file ) ;
2013-09-08 12:31:20 +00:00
2013-09-10 14:45:01 +00:00
CSMDoc : : Document * document = mDocumentManager . addDocument ( files , file , true ) ;
2013-09-08 12:31:20 +00:00
mViewManager . addView ( document ) ;
mNewGame . hide ( ) ;
}
2013-09-03 09:12:19 +00:00
void CS : : Editor : : showStartup ( )
{
2013-09-06 04:02:51 +00:00
if ( mStartup . isHidden ( ) )
mStartup . show ( ) ;
mStartup . raise ( ) ;
mStartup . activateWindow ( ) ;
2013-09-03 09:12:19 +00:00
}
2013-09-08 07:26:43 +00:00
void CS : : Editor : : showSettings ( )
{
if ( mSettings . isHidden ( ) )
mSettings . show ( ) ;
mSettings . raise ( ) ;
mSettings . activateWindow ( ) ;
}
2013-09-03 06:44:21 +00:00
bool CS : : Editor : : makeIPCServer ( )
{
2013-09-06 04:02:51 +00:00
mServer = new QLocalServer ( this ) ;
2013-09-03 09:12:19 +00:00
2013-09-06 04:02:51 +00:00
if ( mServer - > listen ( mIpcServerName ) )
{
connect ( mServer , SIGNAL ( newConnection ( ) ) , this , SLOT ( showStartup ( ) ) ) ;
return true ;
}
2013-09-03 09:12:19 +00:00
2013-09-06 04:02:51 +00:00
mServer - > close ( ) ;
return false ;
2013-09-03 06:44:21 +00:00
}
2013-09-03 09:12:19 +00:00
void CS : : Editor : : connectToIPCServer ( )
{
2013-09-06 04:02:51 +00:00
mClientSocket = new QLocalSocket ( this ) ;
mClientSocket - > connectToServer ( mIpcServerName ) ;
mClientSocket - > close ( ) ;
2013-09-03 09:12:19 +00:00
}
2012-11-22 12:30:02 +00:00
int CS : : Editor : : run ( )
{
2013-09-08 12:31:20 +00:00
if ( mLocal . empty ( ) )
return 1 ;
2013-02-02 15:14:58 +00:00
mStartup . show ( ) ;
2012-11-22 12:30:02 +00:00
2013-03-03 21:58:26 +00:00
QApplication : : setQuitOnLastWindowClosed ( true ) ;
2012-11-22 12:30:02 +00:00
return QApplication : : exec ( ) ;
2013-03-03 21:58:26 +00:00
}