void TagViewer::paintEvent( QPaintEvent* /*e*/ ) { QPainter p( this ); float scale_f = scale_factor(); QRect drawing_area( 0, 0, size().width(), size().height() ); if( !pix_.isNull() && !size().isNull() ) { p.drawPixmap( drawing_area, pix_ ); } else { QTextOption options( Qt::AlignLeft ); options.setWrapMode( QTextOption::WordWrap ); p.setPen( QPen( Qt::red, 4 ) ); p.drawText( QRectF( drawing_area ), "Image cannot be displayed. Check:\n" " - the same image is selected among the multiple selection (it's ok to select labels)\n" " - the image file is still at the same disk location when imported\n" " - the image format is valid and/or the file is not corrupted ", options ); return; } QFont font; font.setPointSize( 10 ); p.setFont( font ); // draw bounding boxes for( QList<TagDisplayElement>::iterator tag_itr = elts_.begin(); tag_itr != elts_.end(); ++tag_itr ) { const TagDisplayElement& tag = *tag_itr; const QList<QRect>& bbox = tag._bbox; p.setPen( QPen( tag._color, 2 ) ); for( QList<QRect>::const_iterator bbox_itr = bbox.begin(); bbox_itr != bbox.end(); ++bbox_itr ) { const QRect& box_rect = *bbox_itr; QRect scaled_box( scale_f * box_rect.topLeft(), scale_f * box_rect.bottomRight() ); p.drawRect( scaled_box ); p.drawText( scaled_box.x(), scaled_box.y(), tag._label ); } } // draw current box being tagged p.setPen( QPen( current_color_, 2 ) ); if( tagging_ && tag_start_ != tag_end_ ) { QRect current_rect( tag_start_, tag_end_ ); p.drawRect( current_rect ); p.drawText( current_rect.x(), current_rect.y(), current_label_ ); } }
void nxtCanvasWidget::paintEvent( QPaintEvent *event ){ QPainter painter( this ); //Set op some areas QRect drawing_area( QPoint(), size() ); QPoint p_delta( pos_x, pos_y ); //Draw the entire widget gray unsigned int widget_height = this->height(); unsigned int widget_width = this->width(); painter.fillRect( drawing_area, Qt::gray ); //Reserve some width for borders and the like unsigned int left_area_width = 0; unsigned int top_area_height = 0; if( canvas ){ //If size changed if( canvas->size_affected() ) emit visible_area_changed(); //Set up contants QRect canvas_area( -canvas->get_offset_x(),-canvas->get_offset_y(), canvas->get_width(), canvas->get_height() ); qDebug( "nxtCanvasWidget: Redraw started" ); //Find last visible point on the widget //QPoint end_point = //Find start point unsigned int start_x = -pos_x; unsigned int start_y = -pos_y; /* if( pos_x < 0 ) start_x = -pos_x; if( pos_y < 0 ) start_y = -pos_y; */ unsigned int available_width = widget_width - left_area_width; unsigned int available_height = widget_height - top_area_height; //Find end point unsigned int end_x = available_width / current_zoom - pos_x; unsigned int end_y = available_height / current_zoom - pos_y; //Round up instead of down if( available_width / current_zoom ) end_x++; if( available_height / current_zoom ) end_y++; //Find size unsigned int view_width = available_width / current_zoom; unsigned int view_height = available_height / current_zoom; if( available_width / current_zoom ) view_width++; if( available_height / current_zoom ) view_height++; QRect visible_area( start_x, start_y, view_width, view_height ); QRect drawn_area = canvas_area & visible_area; //Transform the view painter.translate( 0, widget_height ); painter.scale( current_zoom, current_zoom ); painter.translate( pos_x, -pos_y ); //Draw the canvas background painter.fillRect( drawn_area.x(), -(drawn_area.y()+drawn_area.height()), drawn_area.width(), drawn_area.height(), Qt::white ); //Draw the active pixels for( int iy=drawn_area.y(); iy < drawn_area.y()+drawn_area.height(); iy++ ){ bool *pixel = canvas->get_scanline( iy + canvas->get_offset_y() ); pixel += drawn_area.x() + canvas->get_offset_x(); for( int ix=drawn_area.x(); ix < drawn_area.x()+drawn_area.width(); ix++ ){ if( *pixel ) painter.fillRect( ix, -iy-1, 1, 1, Qt::black ); pixel++; } } //Draw grid if zoom is large enough if( current_zoom > 2 ){ //Draw base grid painter.setPen( QColor( 0xA0, 0xA0, 0xA4 ) ); for( int ix=drawn_area.x(); ix < drawn_area.x()+drawn_area.width()+1; ix++ ) painter.drawLine( ix,-drawn_area.y(), ix,-(drawn_area.y()+drawn_area.height()) ); for( int iy=drawn_area.y(); iy < drawn_area.y()+drawn_area.height()+1; iy++ ) painter.drawLine( drawn_area.x(),-iy, (drawn_area.x()+drawn_area.width()),-iy ); //Draw larger grid painter.setPen( QColor( 0x58, 0x58, 0xFF ) ); for( int ix=drawn_area.x(); ix < drawn_area.x()+drawn_area.width()+1; ix++ ) if( (ix % 10) == 0 ) painter.drawLine( ix,-drawn_area.y(), ix,-(drawn_area.y()+drawn_area.height()) ); for( int iy=drawn_area.y(); iy < drawn_area.y()+drawn_area.height()+1; iy++ ) if( (iy % 10) == 0 ) painter.drawLine( drawn_area.x(),-iy, (drawn_area.x()+drawn_area.width()),-iy ); } painter.fillRect( selection.x(), -(selection.y()+selection.height()), selection.width(), selection.height(), QColor( 255,0,0, 128 ) ); canvas->reset_affected(); } }