PDA

View Full Version : Paint Rect visible on Drag to crop image coordinate



patrik08
16th March 2007, 15:39
PS: my first QPainter not run...

I made a extra QWidget scaled on ratio image... to display a red rect to crop a simple image....


but now this paint is visible only if i resize the widged :-(

http://ppk.ciz.ch/qt_c++/crop_selecter.png

Or must i attach other event on drag???

I wand this rect visible on drag mesure to select crop image ....




void Image_Operator::paintEvent(QPaintEvent *e)
{

if (display.isNull()) {
qDebug() << "### display pixmap broken!!!! " << display.isNull();
return;
}
qDebug() << "### paintEvent go starter...... ";
Load_Actual_Desktop(); /* widget size setting e resolution X11 */
int hi_now = widgetSize.height();
int wi_now = widgetSize.width();
picscaled = display.scaled(wi_now,hi_now,Qt::KeepAspectRatio); /* scaled to widget displayer */
QSize actual_result = picscaled.size(); /* get */
Ratio_On_Work(actual_result);
QPainter painter(this);
painter.drawPixmap(0,0,picscaled);
QString selectionText;
selectionText = QString("Image %1 x %2").arg(actual_result.width()).arg(actual_result.hei ght());

QFontMetrics fm( this->font() );
int stringWidth = fm.width(selectionText);
int stringHeight = fm.ascent();
const int TEXT_MARGIN = 4;

/* color to pen */
QColor textColor = Qt::black;
QColor fillrectcolor = Qt::red;

int textX = 0;
int textY = 0;

/* display rect mesure image crop !!! */
QPoint topLeft( QMIN(mousePRESSPoint.x(), mouseRELEASEPoint.x()),
QMIN(mousePRESSPoint.y(), mouseRELEASEPoint.y()) );
QPoint bottomRight( QMAX(mousePRESSPoint.x(), mouseRELEASEPoint.x()),
QMAX(mousePRESSPoint.y(), mouseRELEASEPoint.y()) );
QRect selectionIMAGE( topLeft, bottomRight );
/* display rect mesure image crop !!! */
/* display rect mesure text crop !!! */
QPoint topLeftT( QMIN(mousePRESSPoint.x(), mouseRELEASEPoint.x()) + TEXT_MARGIN ,
QMIN(mousePRESSPoint.y(), mouseRELEASEPoint.y()) + TEXT_MARGIN );
QPoint bottomRightT( QMAX(mousePRESSPoint.x(), mouseRELEASEPoint.x()) + TEXT_MARGIN ,
QMAX(mousePRESSPoint.y(), mouseRELEASEPoint.y()) + TEXT_MARGIN );
QRect selectionTEXT( topLeftT, bottomRightT );
/* display rect mesure text crop !!! */

QPen pen;
pen.setStyle( Qt::SolidLine );
pen.setCapStyle( Qt::RoundCap );
pen.setColor( fillrectcolor );
pen.setWidth( 2 );
/* display rect to crop image !!! */
painter.setPen( pen);
painter.drawRect(selectionIMAGE);
/* display rect to crop image !!! */
/* display text image ratio text on crop image !!! */
pen.setColor( textColor );
painter.setPen( pen);
/* display text image ratio text on crop image !!! */
painter.drawText(selectionTEXT,selectionText);
//////// Show_Actual_Params(); /* debug display coordinate all */
}

void Image_Operator::mousePressEvent(QMouseEvent *e)
{
qDebug() << "### mousePressEvent -> " << e->x() << "x" << e->y();
mousePRESSPoint = e->pos();
}
void Image_Operator::mouseReleaseEvent(QMouseEvent *e)
{
qDebug() << "### mouseReleaseEvent -> " << e->x() << "x" << e->y();
mouseRELEASEPoint = e->pos();
}

void Image_Operator::mouseMoveEvent(QMouseEvent *e)
{
qDebug() << "### mouseReleaseEvent -> " << e->x() << "x" << e->y();
mouseMOVEPoint = e->pos();
}

wysota
16th March 2007, 16:13
What exactly is the question? Could you prepare a minimal compilable example reproducing the problem?

patrik08
16th March 2007, 16:49
What exactly is the question? Could you prepare a minimal compilable example reproducing the problem?

Here is a example wo reproducing .... the display widged to work on operation crop image...
main.h
main.cpp
image_operator.h
image_operator.cpp
test.png
qt profile

http://ppk.ciz.ch/qt_c++/imagegrop.zip 300kb (one image 280kb inside)

I found so mach sample from paint on qt wiki book or other .... never found a sample to simply crop or cut a part of a image....

I wand to paint the red rectange on the drag process to display live on the selected rect to crop.... (same as gimp)
not only on release mouse.....


The target from this widged is only to find ...
QImage* cropImage( QString filename, QPoint topLeft, QPoint bottomRight )

I take sample base from http://albumshaper.sourceforge.net/

http://ppk.ciz.ch/qt_c++/image_crop/ selectionInterface.cpp

wysota
16th March 2007, 19:07
If you want to cut out part of an image (I assume this is your goal), just create a new image and paint a section of the previous one on the new one.

Take a look at QPainter::drawImage that takes the source rectangle as its argument.

patrik08
16th March 2007, 19:35
If you want to cut out part of an image (I assume this is your goal), just create a new image and paint a section of the previous one on the new one.
Take a look at QPainter::drawImage that takes the source rectangle as its argument.

Yes the final target is Crop the image....:)

..... before i Want to display the -> painter.drawRect(selectionIMAGE); :(

on event Move Mouse pull down rectangle the action to select the Crop ...

Now my script display only the rectangle on resize .... mouse Out.....
mouseReleaseEvent(QMouseEvent *e)

Not on drag move action ... is here other event?




//==============================================
QImage* cropImage( QString filename, QPoint topLeft, QPoint bottomRight )
{
//load original image
QImage origImage( filename );

//convert to 32-bit depth if necessary
if( origImage.depth() < 32 ) { origImage = origImage.convertDepth( 32, Qt::AutoColor ); }

//construct cropped image
QImage* croppedImage = new QImage(bottomRight.x() - topLeft.x() + 1,
bottomRight.y() - topLeft.y() + 1,
origImage.depth());

//iterate over each selected scanline
int xOrig, yOrig;
int xCropped, yCropped;
uchar *origScanLine, *croppedScanLine;

for( yOrig=topLeft.y(),yCropped=0; yOrig<=bottomRight.y(); yOrig++, yCropped++)
{
//iterate over each selected pixel in scanline
origScanLine = origImage.scanLine(yOrig);
croppedScanLine = croppedImage->scanLine(yCropped);

for( xOrig=topLeft.x(),xCropped=0; xOrig<=bottomRight.x(); xOrig++,xCropped++)
{
//copy pixel color from original image to cropped image
*((QRgb*)croppedScanLine+xCropped) = *((QRgb*)origScanLine+xOrig);
}
}

//return pointer to cropped image
return croppedImage;
}

jacek
16th March 2007, 19:36
Maybe it will be easier if you use QRubberBand?

patrik08
16th March 2007, 20:47
Maybe it will be easier if you use QRubberBand?

Crasch app maybe QRubberBand and paintEvent is not able to live together....
&& i wand to display text pixel size to crop.... on top of rectange

i test now to split action & switch paint mode...

&& QWidget::paintEngine ()




typedef enum
{
DRAW_SELECTION,
MOVE_SELECTION,
MOVE_TOP_LEFT_CORNER,
MOVE_TOP_RIGHT_CORNER,
MOVE_BOTTOM_LEFT_CORNER,
MOVE_BOTTOM_RIGHT_CORNER,
MOVE_LEFT_SIDE,
MOVE_RIGHT_SIDE,
MOVE_TOP_SIDE,
MOVE_BOTTOM_SIDE,
SCALE_SELECTION,
DRAW_LINE,
KEYBOARD_CTRL,
NO_EFFECT
} DRAG_MODE;

patrik08
17th March 2007, 00:37
I have it .... :) :) :) the solution is only repaint on mouseMoveEvent && define each DRAG_MODE
on widged....




typedef enum
{
DRAW_SELECTION,
SCALE_SELECTION,
DRAW_LINE,
DRAW_START,
NO_EFFECT
} DRAG_MODE;



void Image_Operator::mouseMoveEvent(QMouseEvent *e)
{
currentDragMode = DRAW_LINE;
mouseRELEASEPoint = e->pos();
if (display.isNull()) {
return;
}

/* remove draw lines */
Load_Actual_Desktop(); /* widget size setting e resolution X11 */
int hi_now = widgetSize.height();
int wi_now = widgetSize.width();

QPoint topLeft( 0 , 0 );
QPoint bottomRight( widgetSize.width() , widgetSize.height() );
QRect areanow( topLeft, bottomRight );
repaint(areanow);
}

if ( currentDragMode == DRAW_SELECTION || currentDragMode == DRAW_LINE ) {
OneWorkImage = selectionIMAGE;
pen.setWidth( bordershade );
pen.setColor( shapepicture );
painter.setPen( pen);
painter.drawRect(selectionOutIMAGE);
/* ................... draw rectangle or only image display on the Zoom Widged && resizable ....*/




source & PrtScn avaiable....

http://www.qtforum.de/forum/viewtopic.php?t=3885

wysota
17th March 2007, 10:04
Please try to express yourself with simple and full sentences, without "...", abreviations, images or code that distract the reader from the topic of your post - especially if you're not sure of your English language skills. Using such constructions makes your post barely readable and hardly understandable. So does including a large amount of code that doesn't add anything to the conversation. If you have to paste some of your code, please remove parts that are not important.

I have to say that after reading this thread a few times I still don't know what the problem was. The thread title suggests problems with cropping an image, but the thread content implies something completely different.