From 3e6ef7c90818e9684bedc952802c32674c1a8143 Mon Sep 17 00:00:00 2001 From: Pritpal Bedi Date: Tue, 31 Aug 2010 01:57:06 +0000 Subject: [PATCH] 2010-08-30 18:55 UTC-0800 Pritpal Bedi (bedipritpal@hotmail.com) * contrib/hbqt/qtgui/hbqt_hbqgraphicsitem.cpp * contrib/hbqt/qtgui/hbqt_hbqgraphicsscene.cpp * contrib/hbide/hbqreportsmanager.prg ! More graphics stuff on .prg layer. --- harbour/ChangeLog | 6 + harbour/contrib/hbide/hbqreportsmanager.prg | 275 +++++++++++++++++- .../hbqt/qtgui/hbqt_hbqgraphicsitem.cpp | 14 +- .../hbqt/qtgui/hbqt_hbqgraphicsscene.cpp | 19 +- 4 files changed, 297 insertions(+), 17 deletions(-) diff --git a/harbour/ChangeLog b/harbour/ChangeLog index 94c41c5d56..67a04a7df6 100644 --- a/harbour/ChangeLog +++ b/harbour/ChangeLog @@ -16,6 +16,12 @@ The license applies to all entries newer than 2009-04-28. */ +2010-08-30 18:55 UTC-0800 Pritpal Bedi (bedipritpal@hotmail.com) + * contrib/hbqt/qtgui/hbqt_hbqgraphicsitem.cpp + * contrib/hbqt/qtgui/hbqt_hbqgraphicsscene.cpp + * contrib/hbide/hbqreportsmanager.prg + ! More graphics stuff on .prg layer. + 2010-08-30 08:26 UTC-0800 Pritpal Bedi (bedipritpal@hotmail.com) * contrib/hbqt/qtgui/hbqtgui.hbx - Deleted reference to HBQMainWindow. diff --git a/harbour/contrib/hbide/hbqreportsmanager.prg b/harbour/contrib/hbide/hbqreportsmanager.prg index a11b1ab52a..84fb897c6f 100644 --- a/harbour/contrib/hbide/hbqreportsmanager.prg +++ b/harbour/contrib/hbide/hbqreportsmanager.prg @@ -201,6 +201,9 @@ CLASS HbqReportsManager METHOD printPreview( qPrinter ) METHOD paintRequested( pPrinter ) METHOD zoom( nMode ) + METHOD drawObject( pPainter, aInfo ) + METHOD drawBarcode( qPainter, qRect, aObj, qObj ) + METHOD drawPixmap( qPainter, qRect, aObj, qObj ) ENDCLASS @@ -374,7 +377,9 @@ METHOD HbqReportsManager:buildDesignReport() ::loadReport() + ::qScene:setPageSize( QPrinter_Letter ) ::qScene:zoomWYSIWYG() + ::qToolbarAlign:setItemChecked( "Grid", ::qScene:showGrid() ) // ::qScene:setLeftMagnet( .t. ) ::qScene:setTopMagnet( .t. ) @@ -462,6 +467,9 @@ METHOD HbqReportsManager:execEvent( cEvent, p, p1, p2 ) ELSE ::qCurGraphicsItem := NIL ENDIF + CASE p == 21017 // Paint Object + ::drawObject( p1, p2 ) + CASE p == QEvent_GraphicsSceneContextMenu ::contextMenuItem( p1, p2 ) @@ -1291,8 +1299,6 @@ METHOD HbqReportsManager:buildToolbarAlign() ::qToolbarAlign:addToolButton( "Grid" , "Show Grid" , app_image( "grid" ), {|| ::execEvent( "buttonGrid_clicked", 4 ) }, .t., .f. ) ::qToolbarAlign:addSeparator() - ::qToolbarAlign:setItemChecked( "Grid", .t. ) - RETURN Self /*----------------------------------------------------------------------*/ @@ -1417,13 +1423,11 @@ METHOD HbqReportsManager:printReport( qPrinter ) qRectF := QRectF():new( qRectF:x()*10/25.4, qRectF:y()*10/25.4, qRectF:width()*10/25.4, qRectF:height()*10/25.4 ) SWITCH a_[ 4 ] CASE "Image" - //qPainter:drawPixmap_6( qRectF:x(), qRectF:y(), qObj:pixmap() ) - //qPainter:drawPixmap_7( qRectF, qObj:pixmap() ) - qPainter:drawPixmap_8( qRectF:x(), qRectF:y(), qRectF:width(), qRectF:height(), qObj:pixmap() ) + ::drawPixmap( qPainter, qRectF, a_, qObj ) + //qPainter:drawPixmap_8( qRectF:x(), qRectF:y(), qRectF:width(), qRectF:height(), qObj:pixmap() ) EXIT CASE "Barcode" - qPainter:drawRect( qRectF ) - //qPainter:drawRect( qObj:boundingRect() ) + ::drawBarcode( qPainter, qRectF, a_, qObj ) EXIT CASE "Gradient" EXIT @@ -1475,3 +1479,260 @@ METHOD HbqReportsManager:paintRequested( pPrinter ) /*----------------------------------------------------------------------*/ +METHOD HbqReportsManager:drawObject( pPainter, aInfo ) + LOCAL qPainter := QPainter():from( pPainter ) + LOCAL qRect := QRectF():from( aInfo[ 1 ] ) + LOCAL cName := aInfo[ 2 ] + LOCAL qObj, aObj, n + + IF hb_hHasKey( ::hItems, cName ) + IF ( n := ascan( ::aObjects, {|e_| e_[ 3 ] == cName } ) ) > 0 + aObj := ::aObjects[ n ] + qObj := ::hItems[ cName ] + + DO CASE + CASE aObj[ 1 ] == "Object" .AND. aObj[ 4 ] == "Barcode" + ::drawBarcode( qPainter, qRect, aObj, qObj ) + ENDCASE + ENDIF + ENDIF + + RETURN Self + +/*----------------------------------------------------------------------*/ + +METHOD HbqReportsManager:drawBarcode( qPainter, qRect, aObj, qObj ) + LOCAL fl, clr, rc, w, x, i, cCode + + cCode := ::fetchBarString( "Harbour" ) + + rc := QRectF():from( qRect:adjusted( 5, 5, -10, -10 ) ) + fl := QColor():new( Qt_white ) + clr := QColor():new( Qt_black ) + w := rc:width() / len( cCode ) + x := 0.0 + + FOR i := 1 TO len( cCode ) + IF substr( cCode, i, 1 ) == "1" + qPainter:fillRect_6( QRectF():new( rc:x() + x, rc:y(), w, rc:height() ), clr ) + ELSE + qPainter:fillRect_6( QRectF():new( rc:x() + x, rc:y(), w, rc:height() ), fl ) + ENDIF + x += w + NEXT + + RETURN { aObj, qObj } + +/*----------------------------------------------------------------------*/ + +METHOD HbqReportsManager:drawPixmap( qPainter, qRect, aObj, qObj ) + LOCAL qPix, image, rc, img, point + LOCAL drawTextType := HBQT_GRAPHICSITEM_TEXT_DRAW_ABOVE + LOCAL paintType := HBQT_GRAPHICSITEM_RESIZE_PICTURE_TO_ITEM_KEEP_ASPECT_RATIO + LOCAL borderWidth := 0 + LOCAL borderColor := 0, pen, textH, sw, sh, cx, cy, cw, ch, textColor := 0 + LOCAL cText := "Picture" + + rc := QRectF():from( qRect:adjusted( 5, 5, -10, -10 ) ) + + textH := 0 + sw := 0 + sh := 0 + + IF ( drawTextType == HBQT_GRAPHICSITEM_TEXT_DRAW_ABOVE .OR. ::drawTextType == HBQT_GRAPHICSITEM_TEXT_DRAW_BELOW ) + textH = QFontMetricsF():from( qPainter:font() ):height() + ENDIF + + qPix := QPixmap():from( qObj:pixmap() ) + image := QImage():new( "QPixmap", qPix ) + + IF qPix:isNull() + qPainter:drawRect( qRect ) + else + img := QImage():new( 0, 0 ) + point := QPointF():from( qRect:topLeft() ) + cx := 0; cy := 0; cw := qPix:width(); ch := qPix:height() + + SWITCH paintType + CASE HBQT_GRAPHICSITEM_RESIZE_PICTURE_TO_ITEM_KEEP_ASPECT_RATIO + img := QImage():from( image:scaled( rc:width(), rc:height() - textH, Qt_KeepAspectRatio, Qt_SmoothTransformation ) ) + EXIT + + CASE HBQT_GRAPHICSITEM_RESIZE_PICTURE_TO_ITEM_IGNORE_ASPECT_RATIO + img := QImage():from( image:scaled( rc:width(), rc:height() - textH, Qt_IgnoreAspectRatio, Qt_SmoothTransformation ) ) + EXIT + + CASE HBQT_GRAPHICSITEM_CENTER_PICTURE_TO_ITEM + point:setX( point:x() + ( rc:width() - image:width() ) / 2 ) + point:setY( point:y() + ( rc:height() - image:height() - textH ) / 2 ) + IF ( point:x() < 0 ) + cx := abs( point:x() ) + cw -= 2 * cx + point:setX( 0 ) + ENDIF + IF ( point:y() < 0 ) + cy = abs( point:y() ) + ch -= 2 * cy + point:setY( 0 ) + ENDIF + img := QImage():from( image:copy( cx, cy, cw, ch ) ) + EXIT + + CASE HBQT_GRAPHICSITEM_RESIZE_ITEM_TO_PICTURE + img := image + sw := img:width() - qObj:width() + sh := img:height() - ( qObj:height() - textH ) + EXIT + + ENDSWITCH + + IF drawTextType == HBQT_GRAPHICSITEM_TEXT_DRAW_ABOVE + point:setY( point:y() + textH ) + ENDIF + + qPainter:drawImage( point, img ) + ENDIF + + qPainter:setPen( QPen():new( textColor ) ) + + SWITCH drawTextType + + CASE HBQT_GRAPHICSITEM_TEXT_DRAW_TOP + qPainter:drawText( rc, Qt_AlignTop + Qt_AlignHCenter, cText ) + EXIT + + CASE HBQT_GRAPHICSITEM_TEXT_DRAW_BOTTOM + qPainter:drawText( rc, Qt_AlignBottom + Qt_AlignHCenter, cText ) + EXIT + + CASE HBQT_GRAPHICSITEM_TEXT_DRAW_ABOVE + qPainter:drawText( rc, Qt_AlignTop + Qt_AlignHCenter, cText ) + EXIT + + CASE HBQT_GRAPHICSITEM_TEXT_DRAW_BELOW + qPainter:drawText( rc, Qt_AlignBottom + Qt_AlignHCenter, cText ) + EXIT + + ENDSWITCH + + IF !empty( sw ) .OR. !empty( sh ) + qObj:setWidth( qObj:width() + sw ) + qObj:setHeight( qObj:height() + sh ) + ENDIF + + IF borderWidth > 0 + pen := QPen():new() + pen:setWidth( borderWidth ) + pen:setColor( borderColor ) + pen:setJoinStyle( Qt_MiterJoin ) + qPainter:setPen( pen ) + qPainter:setBrush( QBrush():new( Qt_NoBrush ) ) + qPainter:drawRect( rc:x() + borderWidth / 2, rc:y() + borderWidth / 2, ; + rc:width() - borderWidth, rc:height() - borderWidth ) + ENDIF + + RETURN { aObj, qObj } + +/*----------------------------------------------------------------------*/ +// HqrGraphicsItem() Class +/*----------------------------------------------------------------------*/ + +CLASS HqrGraphicsItem + + DATA oWidget + DATA cParent + + DATA nOperation + + DATA cType INIT "" + DATA cName INIT "" + DATA cText INIT "" + + DATA nX INIT 0 + DATA nY INIT 0 + DATA nWidth INIT 200 + DATA nHeight INIT 100 + + DATA qPen + DATA qBrush + DATA qBgBrush + + DATA aPos INIT {} + DATA aGeometry INIT {} + + DATA data INIT NIL + + METHOD new( oRM, cParent, cType, cName, aPos, aGeometry ) + * METHOD create( oRM, cParent, cType, cName ) + + METHOD text( ... ) SETGET + METHOD pen( ... ) SETGET + METHOD brush( ... ) SETGET + METHOD bgBrush( ... ) SETGET + METHOD draw( ... ) + + ENDCLASS + +/*----------------------------------------------------------------------*/ + +METHOD HqrGraphicsItem:new( oRM, cParent, cType, cName, aPos, aGeometry ) + + ::oRM := oRM + ::cParent := cParent + ::cType := cType + ::cName := cName + ::aPos := aPos + ::aGeometry := aGeometry + + + + RETURN Self + +/*----------------------------------------------------------------------*/ + +METHOD HqrGraphicsItem:text( ... ) + LOCAL a_:= hb_aParams() + IF empty( a_ ) + RETURN ::cText + ENDIF + + RETURN Self +/*----------------------------------------------------------------------*/ + +METHOD HqrGraphicsItem:pen( ... ) + LOCAL a_:= hb_aParams() + IF empty( a_ ) + RETURN ::qPen + ENDIF + + RETURN Self +/*----------------------------------------------------------------------*/ + +METHOD HqrGraphicsItem:brush( ... ) + LOCAL a_:= hb_aParams() + IF empty( a_ ) + RETURN ::qBrush + ENDIF + + RETURN Self +/*----------------------------------------------------------------------*/ + +METHOD HqrGraphicsItem:bgBrush( ... ) + LOCAL a_:= hb_aParams() + IF empty( a_ ) + RETURN ::qBgBrush + ENDIF + + RETURN Self +/*----------------------------------------------------------------------*/ + +METHOD HqrGraphicsItem:draw( ... ) + LOCAL a_:= hb_aParams() + IF empty( a_ ) + RETURN Self + ENDIF + + RETURN Self +/*----------------------------------------------------------------------*/ + + diff --git a/harbour/contrib/hbqt/qtgui/hbqt_hbqgraphicsitem.cpp b/harbour/contrib/hbqt/qtgui/hbqt_hbqgraphicsitem.cpp index ef17016b92..ffcdeda6e4 100644 --- a/harbour/contrib/hbqt/qtgui/hbqt_hbqgraphicsitem.cpp +++ b/harbour/contrib/hbqt/qtgui/hbqt_hbqgraphicsitem.cpp @@ -826,7 +826,7 @@ void HBQGraphicsItem::paint( QPainter * painter, const QStyleOptionGraphicsItem drawPicture( painter, option ); break; case HBQT_GRAPHICSITEM_BARCODE : - drawBarcode39( painter, option ); + //drawBarcode39( painter, option ); break; case HBQT_GRAPHICSITEM_TEXT : break; @@ -836,9 +836,17 @@ void HBQGraphicsItem::paint( QPainter * painter, const QStyleOptionGraphicsItem } if( block ){ - PHB_ITEM p1 = hb_itemPutNI( NULL, 11017 ); + QRectF rect = ( option->type == QStyleOption::SO_GraphicsItem ) ? boundingRect() : option->exposedRect; + + PHB_ITEM p1 = hb_itemPutNI( NULL, 21017 ); PHB_ITEM p2 = hb_itemPutPtr( NULL, painter ); - PHB_ITEM p3 = hb_itemPutPtr( NULL, &option ); + PHB_ITEM p3 = hb_itemNew( NULL ); + + hb_arrayNew( p3, 2 ); + + hb_arraySetPtr( p3, 1, &rect ); + hb_arraySetC( p3, 2, objectName().toLatin1().data() ); + hb_vmEvalBlockV( block, 3, p1, p2, p3 ); hb_itemRelease( p1 ); hb_itemRelease( p2 ); diff --git a/harbour/contrib/hbqt/qtgui/hbqt_hbqgraphicsscene.cpp b/harbour/contrib/hbqt/qtgui/hbqt_hbqgraphicsscene.cpp index 675d596ecb..957289bc9d 100644 --- a/harbour/contrib/hbqt/qtgui/hbqt_hbqgraphicsscene.cpp +++ b/harbour/contrib/hbqt/qtgui/hbqt_hbqgraphicsscene.cpp @@ -73,7 +73,7 @@ HBQGraphicsScene::HBQGraphicsScene( QObject * parent ) : QGraphicsScene( parent m_magnetArea = 1; m_paperBorder = 0; m_pageBorder = 0; - m_showGrid = true; + m_showGrid = false; m_pageSize = QPrinter::A4; m_orientation = QPrinter::Portrait; @@ -512,12 +512,12 @@ void HBQGraphicsScene::setShowGrid( bool showGrid ) void HBQGraphicsScene::drawBorder() { QPen p; - p.setStyle( Qt::DashDotDotLine ); delete m_paperBorder; delete m_pageBorder; m_paperBorder = addRect( m_paperRect ); + p.setStyle( Qt::SolidLine ); p.setColor( QColor( 0,0,255 ) ); p.setWidth( 4 ); @@ -526,24 +526,29 @@ void HBQGraphicsScene::drawBorder() if( m_showGrid ) { - QPen p; + QPen p, p1; p.setColor( QColor( 225,225,225 ) ); p.setWidth( 1 ); p.setStyle( Qt::DotLine ); + + p1.setColor( QColor( 210,210,210 ) ); + p1.setWidth( 1 ); + p1.setStyle( Qt::DotLine ); + if( views().size() ) { p.setWidth( 1 + 1 / views()[ 0 ]->transform().m11() ); } - for( int i = 0; i < width(); i += ( 5.0 / UNIT ) ) + for( int i = 0, n = 0; i < width(); i += ( 5.0 / UNIT ), n++ ) { QGraphicsLineItem * line = new QGraphicsLineItem( m_paperBorder ); - line->setPen( p ); + line->setPen( n%2 == 0 ? p : p1 ); line->setLine( i, 0, i, height() ); } - for( int i = 0; i < height(); i += ( 5.0 / UNIT ) ) + for( int i = 0, n = 0; i < height(); i += ( 5.0 / UNIT ), n++ ) { QGraphicsLineItem * line = new QGraphicsLineItem( m_paperBorder ); - line->setPen( p ); + line->setPen( n%2 == 0 ? p : p1 ); line->setLine( 0, i, width(), i ); } }