Salome HOME
Merge modifications for HYDRO project (origin/hydro/imps_2017_salome_83 branch)
[modules/gui.git] / tools / PyEditor / src / PyEditor.cxx
1 // Copyright (C) 2015-2016  OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // File   : PyEditor.cxx
20 // Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
21 //
22
23 #include "PyEditor_Window.h"
24 #include "PyEditor_StdSettings.h"
25
26 #include <QApplication>
27 #include <QCommandLineParser>
28 #include <QDir>
29 #include <QLibraryInfo>
30 #include <QLocale>
31 #include <QStringList>
32 #include <QTranslator>
33
34 QString qtTrDir()
35 {
36   const char* vars[] = { "QT5_ROOT_DIR", "QT_ROOT_DIR", "QTDIR" };
37   QString qtPath;
38   for ( uint i = 0; i < sizeof( vars ) / sizeof( vars[0] ) && qtPath.isEmpty(); i++ )
39     qtPath = qgetenv( vars[i] );
40   if ( !qtPath.isEmpty() )
41     qtPath = QDir( qtPath ).absoluteFilePath( "translations" );
42   return qtPath;
43 }
44
45 QString resourceDir()
46 {
47   // Try standalone application's dir.
48   QDir appDir = QApplication::applicationDirPath();
49   appDir.cdUp();
50   QFileInfo resFile( appDir.filePath( "share/resources/PyEditor_msg_en.qm" ) );
51   if ( resFile.exists() )
52     return resFile.absolutePath();
53
54   // Try salome dir.
55   appDir = QApplication::applicationDirPath();
56   appDir.cdUp(); appDir.cdUp();
57   resFile.setFile( appDir.filePath( "share/salome/resources/gui/PyEditor_msg_en.qm" ) );
58   if ( resFile.exists() )
59     return resFile.absolutePath();
60
61   return QString();
62 }
63
64 int main( int argc, char *argv[] )
65 {
66   QApplication app( argc, argv );
67   app.setOrganizationName( "salome" );
68   app.setOrganizationDomain( "www.salome-platform.org" );
69   app.setApplicationName( "pyeditor" );
70   
71   QLocale locale;
72
73   PyEditor_StdSettings* settings = new PyEditor_StdSettings();
74   PyEditor_Settings::setSettings( settings );
75   
76   // Load Qt translations.
77   QString qtDirTrSet = QLibraryInfo::location( QLibraryInfo::TranslationsPath );
78   QString qtDirTrEnv = qtTrDir();
79   
80   QStringList qtTrFiles;
81   qtTrFiles << "qt" << "qtbase";
82   QStringList qtTrDirs;
83   qtTrDirs << QLibraryInfo::location( QLibraryInfo::TranslationsPath );
84   qtTrDirs << qtTrDir();
85   
86   foreach( QString qtTrFile, qtTrFiles ) {
87     foreach ( QString qtTrDir, qtTrDirs ) {
88       QTranslator* translator = new QTranslator;
89       if ( translator->load( locale, QString("%1").arg( qtTrFile ), "_", qtTrDir ) ) {
90         app.installTranslator( translator );
91         break;
92       }
93       else if ( translator->load( QString("%1_en").arg( qtTrFile ), qtTrDir ) ) {
94         app.installTranslator( translator );
95         break;
96       }
97       else {
98         delete translator;
99       }
100     }
101   }
102   
103   // Load application's translations.
104   QTranslator translator;
105   if ( translator.load( locale, QString( "PyEditor_msg" ), "_", resourceDir() ) )
106     app.installTranslator( &translator );
107   else if ( translator.load( QString( "PyEditor_msg_en" ), resourceDir() ) )
108     app.installTranslator( &translator );
109
110   QCommandLineParser parser;
111   parser.setApplicationDescription( QApplication::translate( "PyEditor", "PROGRAM_DESCRIPTION" ) );
112   parser.addHelpOption();
113   parser.addPositionalArgument( QApplication::translate( "PyEditor", "FILE_PARAM_NAME" ),
114                                 QApplication::translate( "PyEditor", "FILE_PARAM_DESCRIPTION" ) );
115
116   parser.process( app );
117   const QStringList args = parser.positionalArguments();
118
119   PyEditor_Window window;
120   window.setWindowIcon( QIcon( ":/images/py_editor.png" ) );
121   window.resize( 650, 700 );
122   window.show();
123
124   if ( args.count() > 0 )
125     window.loadFile( args[0], false );
126   
127   return app.exec();
128 }