Salome HOME
Merge branch 'V9_2_2_BR'
[modules/gui.git] / tools / PyEditor / src / PyEditor.cxx
1 // Copyright (C) 2015-2019  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_Widget.h"
24 #include "PyEditor_Window.h"
25 #include "PyEditor_StdSettings.h"
26
27 #include <QApplication>
28 #include <QCommandLineParser>
29 #include <QDir>
30 #include <QLibraryInfo>
31 #include <QLocale>
32 #include <QStringList>
33 #include <QTranslator>
34
35 QString qtTrDir()
36 {
37   const char* vars[] = { "QT5_ROOT_DIR", "QT_ROOT_DIR", "QTDIR" };
38   QString qtPath;
39   for ( uint i = 0; i < sizeof( vars ) / sizeof( vars[0] ) && qtPath.isEmpty(); i++ )
40     qtPath = qgetenv( vars[i] );
41   if ( !qtPath.isEmpty() )
42     qtPath = QDir( qtPath ).absoluteFilePath( "translations" );
43   return qtPath;
44 }
45
46 QString resourceDir()
47 {
48   // Try standalone application's dir.
49   QDir appDir = QApplication::applicationDirPath();
50   appDir.cdUp();
51   QFileInfo resFile( appDir.filePath( "share/resources/PyEditor_msg_en.qm" ) );
52   if ( resFile.exists() )
53     return resFile.absolutePath();
54
55   // Try salome dir.
56   appDir = QApplication::applicationDirPath();
57   appDir.cdUp(); appDir.cdUp();
58   resFile.setFile( appDir.filePath( "share/salome/resources/gui/PyEditor_msg_en.qm" ) );
59   if ( resFile.exists() )
60     return resFile.absolutePath();
61
62   return QString();
63 }
64
65 int main( int argc, char *argv[] )
66 {
67   QApplication app( argc, argv );
68   app.setOrganizationName( "salome" );
69   app.setOrganizationDomain( "www.salome-platform.org" );
70   app.setApplicationName( "pyeditor" );
71   
72   QLocale locale;
73
74   PyEditor_StdSettings* settings = new PyEditor_StdSettings();
75   PyEditor_Settings::setSettings( settings );
76   
77   // Load Qt translations.
78   QString qtDirTrSet = QLibraryInfo::location( QLibraryInfo::TranslationsPath );
79   QString qtDirTrEnv = qtTrDir();
80   
81   QStringList qtTrFiles;
82   qtTrFiles << "qt" << "qtbase";
83   QStringList qtTrDirs;
84   qtTrDirs << QLibraryInfo::location( QLibraryInfo::TranslationsPath );
85   qtTrDirs << qtTrDir();
86   
87   foreach( QString qtTrFile, qtTrFiles ) {
88     foreach ( QString qtTrDir, qtTrDirs ) {
89       QTranslator* translator = new QTranslator;
90       if ( translator->load( locale, QString("%1").arg( qtTrFile ), "_", qtTrDir ) ) {
91         app.installTranslator( translator );
92         break;
93       }
94       else if ( translator->load( QString("%1_en").arg( qtTrFile ), qtTrDir ) ) {
95         app.installTranslator( translator );
96         break;
97       }
98       else {
99         delete translator;
100       }
101     }
102   }
103   
104   // Load application's translations.
105   QTranslator translator;
106   if ( translator.load( locale, QString( "PyEditor_msg" ), "_", resourceDir() ) )
107     app.installTranslator( &translator );
108   else if ( translator.load( QString( "PyEditor_msg_en" ), resourceDir() ) )
109     app.installTranslator( &translator );
110
111   QCommandLineParser parser;
112   parser.setApplicationDescription( QApplication::translate( "PyEditor", "PROGRAM_DESCRIPTION" ) );
113   parser.addHelpOption();
114   QCommandLineOption gotoOption( QStringList() << "l" << "line",
115                                  QApplication::translate( "PyEditor", "Set initial line number." ),
116                                  "line" );
117   parser.addOption( gotoOption );
118   parser.addPositionalArgument( QApplication::translate( "PyEditor", "FILE_PARAM_NAME" ),
119                                 QApplication::translate( "PyEditor", "FILE_PARAM_DESCRIPTION" ) );
120
121   parser.process( app );
122   const QStringList args = parser.positionalArguments();
123
124   PyEditor_Window window;
125   window.setWindowIcon( QIcon( ":/images/py_editor.png" ) );
126   window.resize( 650, 700 );
127   window.show();
128
129   if ( args.count() > 0 ) {
130     window.loadFile( args[0], false );
131
132     if ( parser.isSet( gotoOption ) ) {
133       bool ok;
134       int line = parser.value( gotoOption ).toInt( &ok );
135       if ( ok )
136         window.editor()->setCurrentLine( line );
137     }
138   }
139   
140   return app.exec();
141 }