int qt_set_font(const char* font) { ensureOptionsCreated(); int qt_previousFontSize = qt->currentFontSize; QString qt_previousFontName = qt->currentFontName; if (font && (*font)) { QStringList list = QString(font).split(','); if (list.size() > 0) qt->currentFontName = list[0]; if (list.size() > 1) qt->currentFontSize = list[1].toInt(); } else { qt->currentFontSize = qt_optionFontSize; qt->currentFontName = qt_option->FontName; } if (qt->currentFontName.isEmpty()) qt->currentFontName = qt_option->FontName; if (qt->currentFontSize <= 0) qt->currentFontSize = qt_optionFontSize; /* Optimize by leaving early if there is no change */ if (qt->currentFontSize == qt_previousFontSize && qt->currentFontName == qt->currentFontName) { return 1; } qt_sendFont(); return 1; }
// 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); }
// 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. 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_option->Widget << ". Starting a QtGnuplotApplication"; qt_option->Widget = 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_option->Widget << ". Starting a new one"; 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; }
int qt_set_font (const char* font) { ensureOptionsCreated(); int qt_previousFontSize = qt->currentFontSize; QString qt_previousFontName = qt->currentFontName; if (font && (*font)) { QStringList list = QString(font).split(','); if (list.size() > 0) qt->currentFontName = list[0]; if (list.size() > 1) qt->currentFontSize = list[1].toInt(); } else { qt->currentFontSize = qt_optionFontSize; qt->currentFontName = qt_option->FontName; } if (qt->currentFontName.isEmpty()) qt->currentFontName = qt_option->FontName; if (qt->currentFontSize <= 0) qt->currentFontSize = qt_optionFontSize; /* Optimize by leaving early if there is no change */ if (qt->currentFontSize == qt_previousFontSize && qt->currentFontName == qt->currentFontName) { return 1; } qt->out << GESetFont << qt->currentFontName << qt->currentFontSize; /* Update the font size as seen by the core gnuplot code */ 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.; return 1; }
// 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); } } }