Ok here's the code:
void FotoEditorFotos
::mousePressEvent(QMouseEvent *event
) {
if (event -> button() != Qt::LeftButton) return;
iniSeleccioX = event -> x();
iniSeleccioY = event -> y();
seleccio = true;
x = iniSeleccioX;
y = iniSeleccioY;
w = 1;
h = 1;
update();
}
void FotoEditorFotos
::mouseMoveEvent(QMouseEvent *event
) {
if (event -> buttons() == Qt::LeftButton)
{
int auxX = event -> x();
int auxY = event -> y();
int posMaxX = width() - 1;
int posMaxY = height() - 1;
if (auxX < iniSeleccioX)
{
if (auxX < 0)
x = 0;
else
x = auxX;
w = iniSeleccioX - x + 1;
}
else
{
if (auxX >= posMaxX)
w = posMaxX - iniSeleccioX + 1;
else
w = auxX - iniSeleccioX + 1;
x = iniSeleccioX;
}
if (auxY < iniSeleccioY)
{
if (auxY < 0)
y = 0;
else
y = auxY;
h = iniSeleccioY - y + 1;
}
else
{
if (auxY >= posMaxY)
h = posMaxY - iniSeleccioY + 1;
else
h = auxY - iniSeleccioY + 1;
y = iniSeleccioY;
}
update();
}
//emit UpdateMouseCoords(event->x(), event->y());*/
event -> ignore();
}
void FotoEditorFotos
::mouseReleaseEvent(QMouseEvent *event
) {
if (event -> button() == Qt::LeftButton)
// Do something with the selection rectangle...
seleccio = false;
}
{
int pixX, pixY;
if (pixmap.width() < width())
pixX = (width() - pixmap.width()) / 2;
else
pixX = 0;
if (pixmap.height() < height())
pixY = (height() - pixmap.height()) / 2;
else
pixY = 0;
painter.begin(this);
QPen pen
(Qt
::blue,
2, Qt
::DashLine);
painter.setPen(pen);
//painter.rotate(5);
//painter.setBrush(Qt::Dense1Pattern);
painter.drawPixmap(pixX, pixY, pixmap);
if (seleccio)
painter.drawRect(x, y, w, h);
painter.end();
}
void FotoEditorFotos::mousePressEvent(QMouseEvent *event)
{
if (event -> button() != Qt::LeftButton) return;
iniSeleccioX = event -> x();
iniSeleccioY = event -> y();
seleccio = true;
x = iniSeleccioX;
y = iniSeleccioY;
w = 1;
h = 1;
update();
}
void FotoEditorFotos::mouseMoveEvent(QMouseEvent *event)
{
if (event -> buttons() == Qt::LeftButton)
{
int auxX = event -> x();
int auxY = event -> y();
int posMaxX = width() - 1;
int posMaxY = height() - 1;
if (auxX < iniSeleccioX)
{
if (auxX < 0)
x = 0;
else
x = auxX;
w = iniSeleccioX - x + 1;
}
else
{
if (auxX >= posMaxX)
w = posMaxX - iniSeleccioX + 1;
else
w = auxX - iniSeleccioX + 1;
x = iniSeleccioX;
}
if (auxY < iniSeleccioY)
{
if (auxY < 0)
y = 0;
else
y = auxY;
h = iniSeleccioY - y + 1;
}
else
{
if (auxY >= posMaxY)
h = posMaxY - iniSeleccioY + 1;
else
h = auxY - iniSeleccioY + 1;
y = iniSeleccioY;
}
update();
}
//emit UpdateMouseCoords(event->x(), event->y());*/
event -> ignore();
}
void FotoEditorFotos::mouseReleaseEvent(QMouseEvent *event)
{
if (event -> button() == Qt::LeftButton)
// Do something with the selection rectangle...
seleccio = false;
}
void FotoEditorFotos::paintEvent(QPaintEvent *event)
{
int pixX, pixY;
if (pixmap.width() < width())
pixX = (width() - pixmap.width()) / 2;
else
pixX = 0;
if (pixmap.height() < height())
pixY = (height() - pixmap.height()) / 2;
else
pixY = 0;
QPainter painter;
painter.begin(this);
QPen pen(Qt::blue, 2, Qt::DashLine);
painter.setPen(pen);
//painter.rotate(5);
//painter.setBrush(Qt::Dense1Pattern);
painter.drawPixmap(pixX, pixY, pixmap);
if (seleccio)
painter.drawRect(x, y, w, h);
painter.end();
}
To copy to clipboard, switch view to plain text mode
That's the code for the widget that I insert in the scroll area and the code for the scroll area is the next:
FotoAreaEditorFotos::FotoAreaEditorFotos(EditorFotos *parent)
{
foto = new FotoEditorFotos;
setWidget(foto);
}
void FotoAreaEditorFotos
::mouseMoveEvent(QMouseEvent *event
) {
if (event ->buttons() != Qt::LeftButton) return;
int auxX = event -> x();
int auxY = event -> y();
int posMaxX = viewport() -> width() - 1;
int posMaxY = viewport() -> height() - 1;
if (auxX <= 0)
{
scrollBar = horizontalScrollBar();
scrollBar -> setValue(scrollBar -> value() - scrollBar -> singleStep());
}
else if (auxX >= posMaxX)
{
scrollBar = horizontalScrollBar();
scrollBar -> setValue(scrollBar -> value() + scrollBar -> singleStep());
}
if (auxY <= 0)
{
scrollBar = verticalScrollBar();
scrollBar -> setValue(scrollBar -> value() - scrollBar -> singleStep());
}
else if (auxY >= posMaxY)
{
scrollBar = verticalScrollBar();
scrollBar -> setValue(scrollBar -> value() + scrollBar -> singleStep());
}
}
void FotoAreaEditorFotos
::paintEvent(QPaintEvent *event
) {
//I put directly the size of the pixmap (256, 259) because it's a test version, in the final version I have to make a method to return the pixmap size ;)
if (viewport() -> width() <= 259)
{
setWidgetResizable(false);
foto -> resize(259, viewport() -> height());
}
else
setWidgetResizable(true);
if (viewport() -> height() <= 256)
{
setWidgetResizable(false);
foto -> resize(qMax(259, viewport() -> width()), 256);
}
else
setWidgetResizable(widgetResizable());
}
FotoAreaEditorFotos::FotoAreaEditorFotos(EditorFotos *parent)
: QScrollArea(parent)
{
foto = new FotoEditorFotos;
setBackgroundRole(QPalette::Dark);
setWidget(foto);
}
void FotoAreaEditorFotos::mouseMoveEvent(QMouseEvent *event)
{
QScrollBar *scrollBar;
if (event ->buttons() != Qt::LeftButton) return;
int auxX = event -> x();
int auxY = event -> y();
int posMaxX = viewport() -> width() - 1;
int posMaxY = viewport() -> height() - 1;
if (auxX <= 0)
{
scrollBar = horizontalScrollBar();
scrollBar -> setValue(scrollBar -> value() - scrollBar -> singleStep());
}
else if (auxX >= posMaxX)
{
scrollBar = horizontalScrollBar();
scrollBar -> setValue(scrollBar -> value() + scrollBar -> singleStep());
}
if (auxY <= 0)
{
scrollBar = verticalScrollBar();
scrollBar -> setValue(scrollBar -> value() - scrollBar -> singleStep());
}
else if (auxY >= posMaxY)
{
scrollBar = verticalScrollBar();
scrollBar -> setValue(scrollBar -> value() + scrollBar -> singleStep());
}
}
void FotoAreaEditorFotos::paintEvent(QPaintEvent *event)
{
//I put directly the size of the pixmap (256, 259) because it's a test version, in the final version I have to make a method to return the pixmap size ;)
if (viewport() -> width() <= 259)
{
setWidgetResizable(false);
foto -> resize(259, viewport() -> height());
}
else
setWidgetResizable(true);
if (viewport() -> height() <= 256)
{
setWidgetResizable(false);
foto -> resize(qMax(259, viewport() -> width()), 256);
}
else
setWidgetResizable(widgetResizable());
}
To copy to clipboard, switch view to plain text mode
All that code paint the image centered in the scroll area and if the image is larger thn the scroll area shows the scroll bars. Moreover if the user press the mouse under the viewport a selection rectangle is shown and if the rectangle is at the limits of the viewport of the scroll area, the scroll bars move automaticaly. Enjoy it.
Bookmarks