Salome HOME
[bos #35159][EDF] (2023-T1) Following commands in Python console. Fixed intermediate...
[modules/gui.git] / src / SUIT / SUIT_Tools.cxx
1 // Copyright (C) 2007-2023  CEA, EDF, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "SUIT_Tools.h"
24
25 #include <QDir>
26
27 #include <stdio.h>
28 #include <stdarg.h>
29 #include <iostream>
30
31 /*!
32   Traces output to log-file.
33   If log is NULL, 'Salome_trace' file is created in temp directory.
34   Log file is written in 'append' mode.
35 */
36 void SUIT_Tools::trace( const char* lpszLog, const char* lpszFormat, ... )
37 {
38   QString tmpPath = tmpDir();
39   if ( !tmpPath.isEmpty() )
40     tmpPath += QDir::separator();
41
42   tmpPath += QString( "Salome_trace" );
43
44   FILE* pStream;
45   pStream = fopen( lpszLog ? lpszLog : (const char*)tmpPath.toUtf8(), "a" );
46   if ( pStream ) 
47   {     
48     va_list argptr;
49     va_start( argptr, lpszFormat );
50     fprintf( pStream, "- Trace %s [%d] : %s", __FILE__, __LINE__, lpszFormat );
51     va_end( argptr );
52
53     fclose( pStream );
54   }
55 }
56
57 /*! 
58     Creates a rect with TopLeft = ( min(x1,x2), min(y1,y2) )
59     and BottomRight = ( TopLeft + (x2-x1)(y2-y1) )    
60 */      
61 QRect SUIT_Tools::makeRect( const int x1, const int y1, const int x2, const int y2 )
62 {  
63   return QRect( qMin( x1, x2 ), qMin( y1, y2 ), qAbs( x2 - x1 ), qAbs( y2 - y1 ) );
64 }
65
66 /*!
67   Creates font's string description
68 */
69 QString SUIT_Tools::fontToString( const QFont& font )
70 {
71   return font.toString();
72 }
73
74 /*!
75   Center widget 'src' relative to widget 'ref'.
76 */
77 void SUIT_Tools::centerWidget( QWidget* src, const QWidget* ref )
78 {
79   SUIT_Tools::alignWidget( src, ref, Qt::AlignCenter );
80 }
81
82 /*!
83   Add tracing code to given python command if it was activated by PYCONSOLE_TRACE env variable.
84   Immediately return if PYCONSOLE_TRACE wasn't set.
85 */
86 void SUIT_Tools::addTraceToPythonCommand(const QString& fileName, QString& command)
87 {
88   auto isPythonTraceEnabled = []() -> bool
89   {
90     const char* envVar = std::getenv("PYCONSOLE_TRACE");
91
92     if (envVar && (envVar[0] != '\0'))
93     {
94       try
95       {
96         const long long numValue = std::stoll(envVar);
97         return numValue > 0;
98       }
99       catch(const std::exception& e)
100       {
101         std::cerr << e.what() << '\n';
102       }
103     }
104
105     return false;
106   };
107
108   static const bool isActivated = isPythonTraceEnabled();
109   if (!isActivated)
110   {
111     return;
112   }
113
114   // Using sys.setprofile() instead of sys.settrace() because we don't need any other events except of 'call' and 'return'.
115   // Another reason: the trace function for sys.settrace() must return itself, so we can't use it properly with lambda.
116   command = QString("sys.setprofile(lambda frame, event, arg: "
117     "print('>>', frame.f_lineno, ': ', frame.f_code.co_name) if event == 'call' and frame.f_code.co_filename == '%1' and frame.f_code.co_name != '<module>' else "
118     "print('<<', frame.f_lineno, ': ', frame.f_code.co_name) if event == 'return' and frame.f_code.co_filename == '%1' and frame.f_code.co_name != '<module>' else "
119     "None); "
120     "%2; "
121     "sys.setprofile(None); ").
122     arg(fileName).
123     arg(command);
124 }