// Called before a plot to connect to the terminal window, if needed void qt_connectToServer() { if (!qt) return; ensureOptionsCreated(); // Determine to which server we should connect bool connectToWidget = !qt_option->Widget.isEmpty(); QString server = connectToWidget ? qt_option->Widget : qt->localServerName; if (qt->socket.state() == QLocalSocket::ConnectedState) { // Check if we are already connected to the correct server if (qt->socket.serverName() == server) return; // Otherwise disconnect qt->socket.disconnectFromServer(); while (qt->socket.state() == QLocalSocket::ConnectedState) qt->socket.waitForDisconnected(1000); } // Start the gnuplot_qt helper program if not already started if (!connectToWidget && !qt->gnuplot_qtStarted) { execGnuplotQt(); server = qt->localServerName; } // Connect to the server, or local server if not available. qt_connectToServer(server); }
// Helper function called by qt_connectToServer() void qt_connectToServer(const QString& server, bool retry = true) { bool connectToWidget = (server != qt_localServerName); // The QLocalSocket::waitForConnected does not respect the time out argument when the // gnuplot_qt application is not yet started. To wait for it, we need to implement the timeout ourselves QDateTime timeout = QDateTime::currentDateTime().addMSecs(1000); do { qt_socket.connectToServer(server); qt_socket.waitForConnected(200); // TODO: yield CPU ? } while((qt_socket.state() != QLocalSocket::ConnectedState) && (QDateTime::currentDateTime() < timeout)); // Still not connected... if ((qt_socket.state() != QLocalSocket::ConnectedState) && retry) { // The widget could not be reached: start a gnuplot_qt program which will create a QtGnuplotApplication if (connectToWidget) { qDebug() << "Could not connect to widget" << qt_optionWidget << ". Starting a QtGnuplotApplication"; qt_optionWidget = QString(); qt_connectToServer(qt_localServerName); } // The gnuplot_qt program could not be reached: try to start a new one else { qDebug() << "Could not connect gnuplot_qt" << qt_optionWidget << ". Starting a new one"; execGnuplotQt(); qt_connectToServer(qt_localServerName, false); } } }
// Called before first plot after a set term command. void qt_init() { if (qt_initialized) return; // If we are not connecting to an existing QtGnuplotWidget, start a QtGnuplotApplication if (qt_optionWidget.isEmpty()) execGnuplotQt(); // Create a QApplication without event loop for QObject's that need it, namely font handling // A better strategy would be to transfer the font handling to the QtGnuplotWidget, but it would require // some synchronization between the widget and the gnuplot process. int argc = 0; QApplication* application = new QApplication(argc, (char**)( NULL)); #ifdef Q_WS_MAC // Don't display this application in the MAC OS X dock ProcessSerialNumber psn; if (GetCurrentProcess(&psn) == noErr) TransformProcessType(&psn, kProcessTransformToBackgroundApplication); #endif // The creation of a QApplication mangled our locale settings #ifdef HAVE_LOCALE_H setlocale(LC_NUMERIC, "C"); setlocale(LC_TIME, current_locale); #endif qt_out.setVersion(QDataStream::Qt_4_4); qt_initialized = true; term_interlock = (void *)qt_init; GP_ATEXIT(qt_atexit); }
// Called before first plot after a set term command. void qt_init() { if (qt) return; ensureOptionsCreated(); qt = new QtGnuplotState(); // If we are not connecting to an existing QtGnuplotWidget, start a QtGnuplotApplication if (qt_option->Widget.isEmpty()) execGnuplotQt(); #ifdef Q_WS_MAC // Don't display this application in the MAC OS X dock removeDockIcon(); #endif // The creation of a QApplication mangled our locale settings #ifdef HAVE_LOCALE_H setlocale(LC_NUMERIC, "C"); setlocale(LC_TIME, current_locale); #endif qt->out.setVersion(QDataStream::Qt_4_4); term_interlock = (void *)qt_init; gp_atexit(qt_atexit); }
// Helper function called by qt_connectToServer() void qt_connectToServer(const QString& server, bool retry = true) { ensureOptionsCreated(); bool connectToWidget = (server != qt->localServerName); // The QLocalSocket::waitForConnected does not respect the time out argument when // the gnuplot_qt application is not yet started or has not yet self-initialized. // To wait for it, we need to implement the timeout ourselves QDateTime timeout = QDateTime::currentDateTime().addMSecs(30000); do { qt->socket.connectToServer(server); if (!qt->socket.waitForConnected(-1)) { // qDebug() << qt->socket.errorString(); GP_SLEEP(0.2); // yield CPU for 0.2 seconds } } while((qt->socket.state() != QLocalSocket::ConnectedState) && (QDateTime::currentDateTime() < timeout)); // Still not connected... if ((qt->socket.state() != QLocalSocket::ConnectedState) && retry) { // The widget could not be reached: start a gnuplot_qt program which will create a QtGnuplotApplication if (connectToWidget) { fprintf(stderr, "Could not connect to existing qt widget. Starting a new one.\n"); qt_option->Widget = QString(); qt_connectToServer(qt->localServerName); } // The gnuplot_qt program could not be reached: try to start a new one else { fprintf(stderr, "Could not connect to existing gnuplot_qt. Starting a new one.\n"); execGnuplotQt(); qt_connectToServer(qt->localServerName, false); } } }