// 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 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) { 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); } } }
// Called just before a plot is going to be displayed. void qt_graphics() { ensureOptionsCreated(); qt->out << GEDesactivate; qt_flushOutBuffer(); qt_connectToServer(); // Set text encoding if (!(qt->codec = qt_encodingToCodec(encoding))) qt->codec = QTextCodec::codecForLocale(); // Set font qt->currentFontSize = qt_optionFontSize; qt->currentFontName = qt_option->FontName; // Set plot size if (qt_setSize) { term->xmax = qt_oversampling*qt_setWidth; term->ymax = qt_oversampling*qt_setHeight; qt_setSize = false; } // Initialize window qt->out << GESetCurrentWindow << qt_optionWindowId; qt->out << GEInitWindow; #ifdef _WIN32 // Let the terminal window know our PID qt->out << GEPID << quint32(GetCurrentProcessId()); #endif qt->out << GEActivate; qt->out << GETitle << qt_option->Title; qt->out << GESetCtrl << qt_optionCtrl; qt->out << GESetWidgetSize << QSize(term->xmax, term->ymax)/qt_oversampling; // Initialize the scene qt->out << GESetSceneSize << QSize(term->xmax, term->ymax)/qt_oversampling; qt->out << GEClear; // Initialize the font qt_sendFont(); term->v_tic = (unsigned int) (term->v_char/2.5); term->h_tic = (unsigned int) (term->v_char/2.5); if (qt_setPosition) { qt->out << GESetPosition << qt_option->position; qt_setPosition = false; } }
// Called just before a plot is going to be displayed. void qt_graphics() { ensureOptionsCreated(); qt->out << GEDesactivate; qt_flushOutBuffer(); qt_connectToServer(); // Set text encoding if (!(qt->codec = qt_encodingToCodec(encoding))) qt->codec = QTextCodec::codecForLocale(); // Set font qt->currentFontSize = qt_optionFontSize; qt->currentFontName = qt_option->FontName; // Set plot metrics QFontMetrics metrics(QFont(qt->currentFontName, qt->currentFontSize)); term->v_char = qt_oversampling * (metrics.ascent() + metrics.descent()); term->h_char = qt_oversampling * metrics.width("0123456789")/10.; term->v_tic = (unsigned int) (term->v_char/2.5); term->h_tic = (unsigned int) (term->v_char/2.5); if (qt_setSize) { term->xmax = qt_oversampling*qt_setWidth; term->ymax = qt_oversampling*qt_setHeight; qt_setSize = false; } // Initialize window qt->out << GESetCurrentWindow << qt_optionWindowId; qt->out << GEInitWindow; qt->out << GEActivate; qt->out << GETitle << qt_option->Title; qt->out << GESetCtrl << qt_optionCtrl; qt->out << GESetWidgetSize << QSize(term->xmax, term->ymax)/qt_oversampling; // Initialize the scene qt->out << GESetSceneSize << QSize(term->xmax, term->ymax)/qt_oversampling; qt->out << GEClear; // Initialize the font qt->out << GESetFont << qt->currentFontName << qt->currentFontSize; }