From 148a516b3540dd85beeecdf8815140d2503f0d15 Mon Sep 17 00:00:00 2001 From: Ovidiu Mircescu Date: Mon, 20 Feb 2023 14:01:37 +0100 Subject: [PATCH] PyInterp prepare for qt6. Synchronisation with phimeca sources. --- tools/PyConsole/src/PyConsole.h | 2 +- tools/PyConsole/src/PyConsole_Console.cxx | 2 +- tools/PyConsole/src/PyConsole_Editor.cxx | 23 ++++++++++++++------- tools/PyConsole/src/PyConsole_Editor.h | 2 +- tools/PyConsole/src/PyConsole_Interp.cxx | 7 ++++--- tools/PyConsole/src/PyConsole_Request.cxx | 2 +- tools/PyConsole/src/PyConsole_Request.h | 2 +- tools/PyInterp/src/PyInterp.h | 2 +- tools/PyInterp/src/PyInterp_Interp.cxx | 25 ++++++++++++----------- tools/PyInterp/src/PyInterp_Utils.h | 2 +- 10 files changed, 40 insertions(+), 29 deletions(-) diff --git a/tools/PyConsole/src/PyConsole.h b/tools/PyConsole/src/PyConsole.h index d089918d2..e5e769d4f 100644 --- a/tools/PyConsole/src/PyConsole.h +++ b/tools/PyConsole/src/PyConsole.h @@ -27,7 +27,7 @@ // ======================================================== // set dllexport type for Win platform -#ifdef WIN32 +#if !defined(PYCONSOLE_STATIC) && defined(_WIN32) # if defined PYCONSOLE_EXPORTS || defined PyConsole_EXPORTS # define PYCONSOLE_EXPORT __declspec(dllexport) # else diff --git a/tools/PyConsole/src/PyConsole_Console.cxx b/tools/PyConsole/src/PyConsole_Console.cxx index 1615721db..5d1da3cdd 100644 --- a/tools/PyConsole/src/PyConsole_Console.cxx +++ b/tools/PyConsole/src/PyConsole_Console.cxx @@ -87,7 +87,7 @@ void PyConsole_Console::init( PyConsole_Editor* editor ) // create editor console QVBoxLayout* lay = new QVBoxLayout( this ); - lay->setMargin( 0 ); + lay->setContentsMargins( 0, 0, 0 ,0 ); myEditor = editor ? editor : new PyConsole_Editor( this, interp ); myEditor->setContextMenuPolicy( Qt::NoContextMenu ); lay->addWidget( myEditor ); diff --git a/tools/PyConsole/src/PyConsole_Editor.cxx b/tools/PyConsole/src/PyConsole_Editor.cxx index 62e1e6d56..d3ffff9df 100644 --- a/tools/PyConsole/src/PyConsole_Editor.cxx +++ b/tools/PyConsole/src/PyConsole_Editor.cxx @@ -167,7 +167,7 @@ PyConsole_Editor::PyConsole_Editor( QWidget* parent ) Creates python editor window. \param parent parent widget - \param interp python interper + \param interp python interpreter */ PyConsole_Editor::PyConsole_Editor( QWidget* parent, PyConsole_Interp* interp ) @@ -198,7 +198,7 @@ void PyConsole_Editor::init() setWordWrapMode( QTextOption::WrapAnywhere ); setAcceptRichText( false ); - // set callbacks to interpeter + // set callbacks to interpreter myInterp->setvoutcb( PyConsole_CallbackStdout, this ); myInterp->setverrcb( PyConsole_CallbackStderr, this ); // print banner @@ -387,7 +387,7 @@ void PyConsole_Editor::exec( const QString& command ) { if ( isReadOnly() ) { // some interactive command is being executed in this editor... - // shedule the command to the queue + // schedule the command to the queue myQueue.push_back( command ); return; } @@ -633,8 +633,12 @@ void PyConsole_Editor::handleBackTab() void PyConsole_Editor::dropEvent( QDropEvent* event ) { // get the initial drop position +#if QT_VERSION >= 0x060000 + QPoint pos = event->position().toPoint(); +#else QPoint pos = event->pos(); - QTextCursor aCursor = cursorForPosition( event->pos() ); +#endif + QTextCursor aCursor = cursorForPosition( pos ); // if the position is not in the last line move it to the end of the command line if ( aCursor.position() < document()->end().previous().position() + promptSize() ) { @@ -646,8 +650,13 @@ void PyConsole_Editor::dropEvent( QDropEvent* event ) QDropEvent de( pos, event->possibleActions(), event->mimeData(), +#if QT_VERSION >= 0x060000 + event->buttons(), + event->modifiers(), +#else event->mouseButtons(), event->keyboardModifiers(), +#endif event->type() ); QTextEdit::dropEvent( &de ); @@ -684,7 +693,7 @@ void PyConsole_Editor::mouseReleaseEvent( QMouseEvent* event ) if ( event->button() == Qt::LeftButton ) { QTextEdit::mouseReleaseEvent( event ); } - else if ( event->button() == Qt::MidButton ) { + else if ( event->button() == Qt::MiddleButton ) { QTextCursor aCursor = cursorForPosition( event->pos() ); // if the position is not in the last line move it to the end of the command line if ( aCursor.position() < document()->end().previous().position() + promptSize() ) { @@ -1306,7 +1315,7 @@ void PyConsole_Editor::customEvent( QEvent* event ) if ( (int)event->type() == (int)PyInterp_Event::ES_OK && myQueue.count() > 0 ) { - // process the next sheduled command from the queue (if there is any) + // process the next scheduled command from the queue (if there is any) QString nextcmd = myQueue[0]; myQueue.pop_front(); exec( nextcmd ); @@ -1393,7 +1402,7 @@ bool PyConsole_Editor::dump( const QString& fileName ) if ( file.open( QFile::WriteOnly ) ) { QTextStream out( &file ); for ( int i = 0; i < myHistory.count(); i++ ) { -#if QT_VERSION >= QT_VERSION_CHECK(5,15,0) +#if QT_VERSION >= QT_VERSION_CHECK(5,14,0) out << myHistory[i] << Qt::endl; #else out << myHistory[i] << endl; diff --git a/tools/PyConsole/src/PyConsole_Editor.h b/tools/PyConsole/src/PyConsole_Editor.h index 0748cf1e6..451a79f97 100644 --- a/tools/PyConsole/src/PyConsole_Editor.h +++ b/tools/PyConsole/src/PyConsole_Editor.h @@ -38,7 +38,7 @@ class QEventLoop; class PYCONSOLE_EXPORT PyConsole_Editor : public QTextEdit { - Q_OBJECT + Q_OBJECT; public: PyConsole_Editor( QWidget* = 0 ); diff --git a/tools/PyConsole/src/PyConsole_Interp.cxx b/tools/PyConsole/src/PyConsole_Interp.cxx index 7dcff79d8..2a0303e36 100644 --- a/tools/PyConsole/src/PyConsole_Interp.cxx +++ b/tools/PyConsole/src/PyConsole_Interp.cxx @@ -23,6 +23,7 @@ // Author : Nicolas REJNERI (OPEN CASCADE), Adrien BRUNETON (CEA/DEN), Vadim SANDLER (OPEN CASCADE) #include "PyConsole_Interp.h" +#include /*! \class PyConsole_Interp @@ -90,7 +91,7 @@ int PyConsole_Interp::afterRun() \internal \param dirArgument Python expression to pass to the dir command. The parsing of what the user actually started typing is dedicated to the caller - \param startMatch string representing the begining of the patter to be completed. For example, when + \param startMatch string representing the beginning of the pattern to be completed. For example, when the user types "a_string_variable.rsp ", this is "rsp". \param[out] matches resulting list of matches \param[out] docString resulting docstring of single match @@ -159,7 +160,7 @@ bool PyConsole_Interp::runDirAndExtract( const QString& dirArgument, QStringList& result, bool discardSwig ) const { - QRegExp re( "^[A-Z].+_[A-Z]+[a-z]+.+$" ); // REX to discard SWIG static method, e.g. MEDCouplingUMesh_Blabla + QRegularExpression re( "^[A-Z].+_[A-Z]+[a-z]+.+$" ); // REX to discard SWIG static method, e.g. MEDCouplingUMesh_Blabla // Execute dir() command QString command( "dir(" + dirArgument + ")" ); @@ -187,7 +188,7 @@ bool PyConsole_Interp::runDirAndExtract( const QString& dirArgument, // if the method is not from swig, not static (guessed from the reg exp) and matches // what is already there if ( s.startsWith( startMatch ) ) { - if ( !discardSwig || ( !re.exactMatch( s ) && !s.contains( "swig" ) ) ) + if ( !discardSwig || ( !re.match( s ).hasMatch() && !s.contains( "swig" ) ) ) result.append( s ); } Py_DECREF( it ); diff --git a/tools/PyConsole/src/PyConsole_Request.cxx b/tools/PyConsole/src/PyConsole_Request.cxx index b6e4cf3de..10ef5fa94 100644 --- a/tools/PyConsole/src/PyConsole_Request.cxx +++ b/tools/PyConsole/src/PyConsole_Request.cxx @@ -56,7 +56,7 @@ PyConsole_ExecCommand::PyConsole_ExecCommand( PyInterp_Interp* theInterp, void PyConsole_ExecCommand::execute() { if ( myCommand != "" ) { - int ret = getInterp()->run( myCommand.toUtf8().data() ); + int ret = getInterp()->run(myCommand.toUtf8()); if ( ret < 0 ) myState = PyInterp_Event::ES_ERROR; else if ( ret > 0 ) diff --git a/tools/PyConsole/src/PyConsole_Request.h b/tools/PyConsole/src/PyConsole_Request.h index f126f5e7c..642b2fb2c 100644 --- a/tools/PyConsole/src/PyConsole_Request.h +++ b/tools/PyConsole/src/PyConsole_Request.h @@ -55,7 +55,7 @@ protected: private: QString myDirArg; //!< String to be passed to the dir() comman - QString myStartMatch; //!< Begining of the command (as typed by the user) + QString myStartMatch; //!< Beginning of the command (as typed by the user) bool myStatus; //!< Status of completion command execution QStringList myMatches; //!< Matches QString myDoc; //!< Docstring of single match diff --git a/tools/PyInterp/src/PyInterp.h b/tools/PyInterp/src/PyInterp.h index 387cf1849..985770d0f 100644 --- a/tools/PyInterp/src/PyInterp.h +++ b/tools/PyInterp/src/PyInterp.h @@ -27,7 +27,7 @@ // ======================================================== // set dllexport type for Win platform -#ifdef WIN32 +#if !defined(PYINTERP_STATIC) && defined(_WIN32) # if defined PYINTERP_EXPORTS || defined PyInterp_EXPORTS # define PYINTERP_EXPORT __declspec(dllexport) # else diff --git a/tools/PyInterp/src/PyInterp_Interp.cxx b/tools/PyInterp/src/PyInterp_Interp.cxx index 7a8d020cf..287f6400e 100644 --- a/tools/PyInterp/src/PyInterp_Interp.cxx +++ b/tools/PyInterp/src/PyInterp_Interp.cxx @@ -35,7 +35,7 @@ #include #include -#include +#include #include #define TOP_HISTORY_PY "--- top of history ---" @@ -185,7 +185,7 @@ char* PyInterp_Interp::_argv[] = {(char*)""}; \brief Basic constructor. After construction the interpreter instance successor classes - must call virtual method initalize(). + must call virtual method initialize(). */ PyInterp_Interp::PyInterp_Interp(): _vout(0), _verr(0), _global_context(0), _local_context(0), _initialized(false) @@ -203,7 +203,7 @@ PyInterp_Interp::~PyInterp_Interp() /*! \brief Initialize embedded interpreter. - This method shoud be called after construction of the interpreter. + This method should be called after construction of the interpreter. The method initialize() calls virtuals methods - initPython() to initialize global Python interpreter - initContext() to initialize interpreter internal context @@ -341,7 +341,7 @@ void PyInterp_Interp::closeContext() /*! \brief Compile Python command and evaluate it in the python dictionary contexts if possible. This is not thread-safe. - This is the caller's responsability to make this thread-safe. + This is the caller's responsibility to make this thread-safe. \internal \param command Python command string \return -1 on fatal error, 1 if command is incomplete and 0 @@ -479,11 +479,12 @@ static int compile_command(const char *command, PyObject * global_ctxt, PyObject QString singleCommand = command; QString commandArgs = ""; - QRegExp rx("exec\\s*\\(.*open\\s*\\(\\s*(.*)\\s*\\)\\s*\\.\\s*read\\s*\\(\\)(\\s*,\\s*args\\s*=\\s*\\(.*\\))\\s*\\)"); - if (rx.indexIn(command) != -1) { - commandArgs = rx.cap(2).remove(0, rx.cap(2).indexOf("(")); // arguments of command - commandArgs.insert(commandArgs.indexOf('(')+1, rx.cap(1).split(",")[0].trimmed() + ","); // prepend arguments list by the script file itself - singleCommand = singleCommand.remove(rx.pos(2), rx.cap(2).size()); // command for execution without arguments + QRegularExpression rx("exec\\s*\\(.*open\\s*\\(\\s*(.*)\\s*\\)\\s*\\.\\s*read\\s*\\(\\)(\\s*,\\s*args\\s*=\\s*\\(.*\\))\\s*\\)"); + QRegularExpressionMatch match = rx.match(command); + if (match.hasMatch()) { + commandArgs = match.captured(2).remove(0, match.captured(2).indexOf("(")); // arguments of command + commandArgs.insert(commandArgs.indexOf('(')+1, match.captured(1).split(",")[0].trimmed() + ","); // prepend arguments list by the script file itself + singleCommand = singleCommand.remove(match.capturedStart(2), match.captured(2).size()); // command for execution without arguments } if (commandArgs.isEmpty()) { @@ -514,7 +515,7 @@ int PyInterp_Interp::run(const char *command) } /** - * Called before a command is run (when calling run() method). Not thread-safe. Caller's responsability + * Called before a command is run (when calling run() method). Not thread-safe. Caller's responsibility * to acquire GIL if needed. */ int PyInterp_Interp::beforeRun() @@ -523,7 +524,7 @@ int PyInterp_Interp::beforeRun() } /** - * Called after a command is run (when calling run() method). Not thread-safe. Caller's responsability + * Called after a command is run (when calling run() method). Not thread-safe. Caller's responsibility * to acquire GIL if needed. */ int PyInterp_Interp::afterRun() @@ -532,7 +533,7 @@ int PyInterp_Interp::afterRun() } /*! - \brief Run Python command (used internally). Not thread-safe. GIL acquisition is caller's responsability. + \brief Run Python command (used internally). Not thread-safe. GIL acquisition is caller's responsibility. \param command Python command \param addToHistory if \c true (default), the command is added to the commands history \return command status diff --git a/tools/PyInterp/src/PyInterp_Utils.h b/tools/PyInterp/src/PyInterp_Utils.h index 4d3c5b199..2251b27b4 100644 --- a/tools/PyInterp/src/PyInterp_Utils.h +++ b/tools/PyInterp/src/PyInterp_Utils.h @@ -51,7 +51,7 @@ Py_DecodeLocale(const char *arg, size_t *size) * Utility class wrapping the Python GIL acquisition. This makes use of the high level * API (PyGILState_Ensure and PyGILState_Release), and is hence compatible with only * one running Python interpreter (no call to Py_NewInterpreter()). - * When the class is instanciated the lock is acquired. It is released at destruction time. + * When the class is instantiated the lock is acquired. It is released at destruction time. * Copy construction (and hence assignation) is forbidden. */ class PYINTERP_EXPORT PyLockWrapper -- 2.30.2