From: <and...@us...> - 2010-05-10 21:53:08
|
Revision: 10985 http://plplot.svn.sourceforge.net/plplot/?rev=10985&view=rev Author: andrewross Date: 2010-05-10 21:53:01 +0000 (Mon, 10 May 2010) Log Message: ----------- Update qt driver to support PLESC_GETC escape command. This means for example that plGetCursor now works for the qtwidget device. The -locate option of example 1 demonstrates this. Also support the "Q" keystroke to abort and the "L" keystroke to enter locate mode (consistent with xwin driver). Finally advance page on mouse button down rather than button release (again, consistent with xwin driver). Modified Paths: -------------- trunk/bindings/qt_gui/plqt.cpp trunk/bindings/qt_gui/pyqt4/plplot_pyqt4.sip trunk/drivers/qt.cpp trunk/include/qt.h Modified: trunk/bindings/qt_gui/plqt.cpp =================================================================== --- trunk/bindings/qt_gui/plqt.cpp 2010-05-10 21:34:56 UTC (rev 10984) +++ trunk/bindings/qt_gui/plqt.cpp 2010-05-10 21:53:01 UTC (rev 10985) @@ -85,6 +85,11 @@ { } +void QtPLDriver::setPLStream( PLStream *p ) +{ + pls = p; +} + void QtPLDriver::drawLine( short x1, short y1, short x2, short y2 ) { if ( !m_painterP->isActive() ) return; @@ -314,7 +319,7 @@ return res; } -void QtPLDriver::drawText( PLStream* pls, EscText* txt ) +void QtPLDriver::drawText( EscText* txt ) { if ( !m_painterP->isActive() ) return; @@ -599,6 +604,8 @@ NoPen = QPen( Qt::NoPen ); NoPen.setWidthF( 0. ); + + locate_mode = 0; } QtPLWidget::~QtPLWidget() @@ -848,7 +855,7 @@ // redrawFromLastFlush=true; } -void QtPLWidget::drawText( PLStream* pls, EscText* txt ) +void QtPLWidget::drawText( EscText* txt ) { BufferElement el; el.Element = TEXT; @@ -908,20 +915,188 @@ SolidBrush = QBrush( Qt::SolidPattern ); } -void QtPLWidget::mouseReleaseEvent( QMouseEvent * event ) +void QtPLWidget::lookupButtonEvent( QMouseEvent * event ) { - if ( event->button() == Qt::RightButton ) + Qt::MouseButtons buttons = event->buttons(); + Qt::KeyboardModifiers modifiers = event->modifiers(); + gin.pX = event->x(); + gin.pY = height() - event->y(); + gin.dX = (PLFLT) event->x() / width(); + gin.dY = (PLFLT) ( height() - event->y() ) / height(); + + switch ( event->button() ) { - handler.DeviceChangedPage( this ); + case Qt::LeftButton: + gin.button = 1; + break; + case Qt::MidButton: + gin.button = 2; + break; + case Qt::RightButton: + gin.button = 3; + break; } + + // Map Qt button and key states to the (X windows) values used + // by plplot. + gin.state = 0; + if ( buttons & Qt::LeftButton ) + gin.state |= 1 << 8; + if ( buttons & Qt::MidButton ) + gin.state |= 1 << 9; + if ( buttons & Qt::RightButton ) + gin.state |= 1 << 10; + if ( modifiers & Qt::ShiftModifier ) + gin.state |= 1 << 0; + if ( modifiers & Qt::ControlModifier ) + gin.state |= 1 << 2; + if ( modifiers & Qt::AltModifier ) + gin.state |= 1 << 3; + if ( modifiers & Qt::MetaModifier ) + gin.state |= 1 << 3; + + gin.keysym = 0x20; } +void QtPLWidget::locate() +{ + if ( plTranslateCursor( &gin ) ) + { + if ( locate_mode == 2 ) + { + pltext(); + if ( gin.keysym < 0xFF && isprint( gin.keysym ) ) + std::cout << gin.wX << " " << gin.wY << " " << (char) gin.keysym << std::endl; + else + std::cout << gin.wX << " " << gin.wY << " " << std::hex << gin.keysym << std::endl; + + plgra(); + } + } + else + { + locate_mode = 0; + QApplication::restoreOverrideCursor(); + } +} + +void QtPLWidget::mousePressEvent( QMouseEvent * event ) +{ + lookupButtonEvent( event ); + + if ( locate_mode ) + { + if ( event->button() == Qt::LeftButton ) + { + locate(); + } + } + else + { + if ( event->button() == Qt::RightButton ) + { + handler.DeviceChangedPage( this ); + } + } +} + void QtPLWidget::keyPressEvent( QKeyEvent* event ) { - if ( event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return ) + if ( locate_mode ) { - handler.DeviceChangedPage( this ); + Qt::KeyboardModifiers modifiers = event->modifiers(); + QPoint p = QCursor::pos(); + gin.pX = p.x(); + gin.pY = height() - p.y(); + gin.dX = (PLFLT) p.x() / width(); + gin.dY = (PLFLT) ( height() - p.y() ) / height(); + + switch ( event->key() ) + { + case Qt::Key_Escape: + locate_mode = 0; + QApplication::restoreOverrideCursor(); + plGinInit( &gin ); + break; + case Qt::Key_Shift: + case Qt::Key_Control: + case Qt::Key_Alt: + case Qt::Key_Meta: + case Qt::Key_AltGr: + plGinInit( &gin ); + case Qt::Key_Left: + case Qt::Key_Right: + case Qt::Key_Up: + case Qt::Key_Down: + { + int x1, y1, dx = 0, dy = 0; + int xmin = 0, xmax = width() - 1, ymin = 0, ymax = height() - 1; + switch ( event->key() ) + { + case Qt::Key_Left: + dx = -1; + break; + case Qt::Key_Right: + dx = 1; + break; + case Qt::Key_Up: + dy = -1; + break; + case Qt::Key_Down: + dy = 1; + break; + } + if ( event->modifiers() & Qt::ShiftModifier ) + { + dx *= 5; + dy *= 5; + } + if ( event->modifiers() & Qt::ControlModifier ) + { + dx *= 5; + dy *= 5; + } + if ( event->modifiers() & Qt::AltModifier ) + { + dx *= 5; + dy *= 5; + } + x1 = gin.pX + dx; + y1 = gin.pY + dy; + + if ( x1 < xmin ) dx = xmin - gin.pX; + if ( y1 < ymin ) dy = ymin - gin.pY; + if ( x1 > xmax ) dx = xmax - gin.pX; + if ( y1 > ymax ) dy = ymax - gin.pY; + + QCursor::setPos( p.x() + dx, p.y() + dy ); + plGinInit( &gin ); + break; + } + default: + locate(); + break; + } } + else + { + if ( event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return ) + { + handler.DeviceChangedPage( this ); + } + if ( event->text() == "Q" ) + { + // Terminate on a 'Q' (not 'q', since it's too easy to hit by mistake) + pls->nopause = TRUE; + plexit( "" ); + } + else if ( event->text() == "L" ) + // Begin locate mode + { + locate_mode = 2; + QApplication::setOverrideCursor( Qt::CrossCursor ); + } + } } void QtPLWidget::closeEvent( QCloseEvent* event ) @@ -1147,6 +1322,20 @@ } } +void QtPLWidget::getCursorCmd( PLGraphicsIn *ptr ) +{ + plGinInit( &gin ); + + locate_mode = 1; + QApplication::setOverrideCursor( Qt::CrossCursor ); + + while ( gin.pX < 0 && locate_mode ) + QCoreApplication::processEvents( QEventLoop::AllEvents, 10 ); + + QApplication::restoreOverrideCursor(); + *ptr = gin; +} + #endif #if defined ( PLD_extqt ) @@ -1272,12 +1461,14 @@ void plsetqtdev( QtExtWidget* widget ) { plsc->dev = (void*) widget; + widget->setPLStream( plsc ); } void plsetqtdev( QtExtWidget* widget, int argc, char** argv ) { plparseopts( &argc, (const char**) argv, PL_PARSE_FULL ); plsc->dev = (void*) widget; + widget->setPLStream( plsc ); } void plfreeqtdev() Modified: trunk/bindings/qt_gui/pyqt4/plplot_pyqt4.sip =================================================================== --- trunk/bindings/qt_gui/pyqt4/plplot_pyqt4.sip 2010-05-10 21:34:56 UTC (rev 10984) +++ trunk/bindings/qt_gui/pyqt4/plplot_pyqt4.sip 2010-05-10 21:53:01 UTC (rev 10985) @@ -51,7 +51,7 @@ void clearWidget(); protected: - void mouseReleaseEvent(QMouseEvent *event); + void mousePressEvent(QMouseEvent *event); void keyPressEvent(QKeyEvent *event); void closeEvent(QCloseEvent *event); void nextPage(); Modified: trunk/drivers/qt.cpp =================================================================== --- trunk/drivers/qt.cpp 2010-05-10 21:34:56 UTC (rev 10984) +++ trunk/drivers/qt.cpp 2010-05-10 21:53:01 UTC (rev 10985) @@ -276,6 +276,7 @@ { pls->dev = new QtRasterDevice( pls->xlength, pls->ylength ); } + ( (QtRasterDevice *) pls->dev )->setPLStream( pls ); if ( isMaster ) handler.setMasterDevice( (QtRasterDevice*) ( pls->dev ) ); @@ -406,7 +407,7 @@ /*$$ call the generic ProcessString function * ProcessString( pls, (EscText *)ptr ); */ widget->QtPLDriver::setColor( pls->curcolor.r, pls->curcolor.g, pls->curcolor.b, pls->curcolor.a ); - widget->drawText( pls, (EscText *) ptr ); + widget->drawText( (EscText *) ptr ); break; default: break; @@ -687,6 +688,7 @@ { pls->dev = new QtSVGDevice( pls->xlength, pls->ylength ); } + ( (QtSVGDevice *) pls->dev )->setPLStream( pls ); if ( isMaster ) handler.setMasterDevice( (QtSVGDevice*) ( pls->dev ) ); @@ -735,6 +737,7 @@ pls->dev = new QtSVGDevice( s.width(), s.height() ); ( (QtSVGDevice *) pls->dev )->downscale = downscale; + ( (QtSVGDevice *) pls->dev )->setPLStream( pls ); if ( isMaster ) handler.setMasterDevice( (QtSVGDevice *) pls->dev ); handler.DeviceChangedPage( (QtSVGDevice *) pls->dev ); @@ -833,7 +836,7 @@ /*$$ call the generic ProcessString function * ProcessString( pls, (EscText *)ptr ); */ widget->setColor( pls->curcolor.r, pls->curcolor.g, pls->curcolor.b, pls->curcolor.a ); - widget->drawText( pls, (EscText *) ptr ); + widget->drawText( (EscText *) ptr ); break; default: @@ -962,6 +965,7 @@ { pls->dev = new QtEPSDevice( pls->xlength, pls->ylength ); } + ( (QtEPSDevice *) pls->dev )->setPLStream( pls ); if ( isMaster ) handler.setMasterDevice( (QtEPSDevice*) ( pls->dev ) ); @@ -1112,7 +1116,7 @@ /*$$ call the generic ProcessString function * ProcessString( pls, (EscText *)ptr ); */ widget->setColor( pls->curcolor.r, pls->curcolor.g, pls->curcolor.b, pls->curcolor.a ); - widget->drawText( pls, (EscText *) ptr ); + widget->drawText( (EscText *) ptr ); break; default: break; @@ -1216,6 +1220,7 @@ widget = new QtPLWidget( pls->xlength, pls->ylength ); pls->dev = (void*) widget; } + widget->setPLStream( pls ); if ( isMaster ) handler.setMasterDevice( widget ); @@ -1356,15 +1361,15 @@ case PLESC_HAS_TEXT: widget->setColor( pls->curcolor.r, pls->curcolor.g, pls->curcolor.b, pls->curcolor.a ); - widget->drawText( pls, (EscText *) ptr ); + widget->drawText( (EscText *) ptr ); break; case PLESC_FLUSH: widget->flush(); break; - - - + case PLESC_GETC: + widget->getCursorCmd( (PLGraphicsIn *) ptr ); + break; default: break; } } @@ -1557,7 +1562,7 @@ /*$$ call the generic ProcessString function * ProcessString( pls, (EscText *)ptr ); */ widget->setColor( pls->curcolor.r, pls->curcolor.g, pls->curcolor.b, pls->curcolor.a ); - widget->drawText( pls, (EscText *) ptr ); + widget->drawText( (EscText *) ptr ); break; default: break; Modified: trunk/include/qt.h =================================================================== --- trunk/include/qt.h 2010-05-10 21:34:56 UTC (rev 10984) +++ trunk/include/qt.h 2010-05-10 21:53:01 UTC (rev 10985) @@ -120,13 +120,15 @@ // Constructor, taking the device size as arguments QtPLDriver( PLINT i_iWidth = QT_DEFAULT_X, PLINT i_iHeight = QT_DEFAULT_Y ); - virtual ~QtPLDriver(); // does not delete emitter! + virtual ~QtPLDriver(); // does not delete emitter! + void setPLStream( PLStream *pls ); // store the related stream + // Draws a line from (x1, y1) to (x2, y2) in internal plplot coordinates virtual void drawLine( short x1, short y1, short x2, short y2 ); virtual void drawPolyline( short * x, short * y, PLINT npts ); virtual void drawPolygon( short * x, short * y, PLINT npts ); - virtual void drawText( PLStream* pls, EscText* txt ); + virtual void drawText( EscText* txt ); virtual void setColor( int r, int g, int b, double alpha ); virtual void setBackgroundColor( int r, int g, int b, double alpha ){} virtual void setGradient( int x1, int x2, int y1, int y2, @@ -149,14 +151,16 @@ QPicture getTextPicture( PLUNICODE fci, PLUNICODE* text, int len, PLFLT chrht ); // Text-related variables - bool underlined; - bool overlined; - double currentFontScale; - double currentFontSize; - double yOffset; - double xOffset; + bool underlined; + bool overlined; + double currentFontScale; + double currentFontSize; + double yOffset; + double xOffset; - QPainter* m_painterP; + PLStream *pls; + + QPainter * m_painterP; }; #if defined ( PLD_bmpqt ) || defined ( PLD_jpgqt ) || defined ( PLD_pngqt ) || defined ( PLD_ppmqt ) || defined ( PLD_tiffqt ) @@ -310,8 +314,9 @@ unsigned char *r, unsigned char *g, unsigned char *b, PLFLT *alpha, PLINT ncol1 ); void setWidth( PLINT r ); - void drawText( PLStream* pls, EscText* txt ); + void drawText( EscText* txt ); void flush(); + void getCursorCmd( PLGraphicsIn *ptr ); protected: @@ -321,6 +326,8 @@ void getPlotParameters( double & io_dXFact, double & io_dYFact, double & io_dXOffset, double & io_dYOffset ); // gives the parameters to scale and center the plot on the page void doPlot( QPainter* p, double x_fact, double y_fact, double x_offset, double y_offset ); // Actually draws the plot. Deported in a function for readability void renderText( QPainter* p, struct TextStruct_* s, double x_fact, double x_offset, double y_fact, double y_offset ); + void lookupButtonEvent( QMouseEvent * event ); + void locate(); void resetPensAndBrushes( QPainter* ); @@ -356,10 +363,13 @@ int g; int b; double alpha; - } bgColour; + } bgColour; + PLGraphicsIn gin; // Graphics Input Structure + int locate_mode; // Set while in locate mode + protected slots: - void mouseReleaseEvent( QMouseEvent * event ); + void mousePressEvent( QMouseEvent * event ); void keyPressEvent( QKeyEvent* event ); void closeEvent( QCloseEvent* event ); void nextPage(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |