19 #include <QMouseEvent> 23 #if QT_VERSION >= 0x040200 24 #define CURSOR_NORMAL QCursor(Qt::OpenHandCursor) 25 #define CURSOR_MOUSE_PRESS QCursor(Qt::ClosedHandCursor) 27 #define CURSOR_NORMAL QCursor(Qt::CrossCursor) 28 #define CURSOR_MOUSE_PRESS QCursor(Qt::SizeAllCursor) 70 QBrush background(QColor(
"#fdfdfd"));
73 p.fillRect(rect(), background);
78 QRect iRect =
_image.rect();
94 double scaleFactor = double(sRect.width()) /
double(
_view.width());
100 if (iRect.bottom() < r.bottom()) {
101 r.setBottom(iRect.bottom());
106 if (iRect.right() < r.right()) {
107 r.setRight(iRect.right());
111 QSize scaleTo(
int(
double(r.width()) * scaleFactor),
112 int(
double(r.height()) * scaleFactor));
125 i = i.copy(r).scaled(scaleTo,
126 Qt::KeepAspectRatioByExpanding,
127 Qt::SmoothTransformation);
129 int extraWidth = int(
double(sRect.width() - i.width()) / 2.0);
130 int extraHeight = int(
double(sRect.height() - i.height()) / 2.0);
137 if (extraWidth > 0) {
138 p.fillRect(0, 0, extraWidth, sRect.height(), background);
139 p.fillRect(sRect.width() - extraWidth, 0,
140 sRect.width(), sRect.height(), background);
143 if (extraHeight > 0) {
144 p.fillRect(0, 0, sRect.width(), extraHeight, background);
145 p.fillRect(0, sRect.height() - extraHeight,
146 sRect.width(), sRect.height(), background);
150 p.drawImage(extraWidth, extraHeight, i);
162 QRect sRect = rect();
163 QRect iRect =
_image.rect();
165 float sw = float(sRect.width());
166 float sh = float(sRect.height());
167 float iw = float(iRect.width());
168 float ih = float(iRect.height());
173 float maxw = float(std::max<int>(sRect.width(), iRect.width())) +
_padding;
174 float maxh = float(std::max<int>(sRect.height(), iRect.height())) +
_padding;
180 float aspect = sw / sh;
183 float newmaxh = maxh;
184 float newmaxw = aspect * newmaxh;
185 if (newmaxw < maxw) {
187 newmaxh = maxw / aspect;
191 float newminh = minh;
192 float newminw = aspect * newminh;
193 if (minw < newminw) {
195 newminh = newminw / aspect;
199 float vw = (1.0f -
_zoom) * (newmaxw - newminw) + newminw;
200 float vh = (1.0f -
_zoom) * (newmaxh - newminh) + newminh;
202 _view.setWidth(
int(vw));
203 _view.setHeight(
int(vh));
208 float vdx = vw * (float(screendx) / sw);
209 float vdy = vh * (float(screendy) / sh);
212 _desiredX = qBound(0.0f, _desiredX + vdx, iw);
216 QPoint viewCenter =
_view.center();
217 float vx = viewCenter.x();
218 float vy = viewCenter.y();
230 vdx = (iw / 2.0f) - vx;
233 float vl = float(
_view.left());
234 float vr = float(
_view.right());
237 }
else if (vr > iw) {
243 vdy = (ih / 2.0f) - vy;
246 float vt = float(
_view.top());
247 float vb = float(
_view.bottom());
250 }
else if (vb > ih) {
255 _view.translate(
int(vdx),
int(vdy));
262 QPoint viewCenter =
_view.center();
290 _zoom = qBound(0.0f, pct, 1.0f);
336 QPoint center = rect().center();
337 int dx = e->x() - center.x();
338 int dy = e->y() - center.y();
342 Qt::MouseButton btn = e->button();
343 if (btn == Qt::LeftButton)
345 else if (btn == Qt::RightButton)
360 if (0.001 <=
_zoom) {
368 if (e->delta() > 0) {
virtual void mouseMoveEvent(QMouseEvent *e)
virtual void paintEvent(QPaintEvent *)
virtual void mouseDoubleClickEvent(QMouseEvent *e)
virtual void paintImage(QPainter *painter)
void setImage(QImage &pixmap)
ZImageView(QWidget *parent=0)
virtual void mouseReleaseEvent(QMouseEvent *e)
#define CURSOR_MOUSE_PRESS
virtual void wheelEvent(QWheelEvent *e)
virtual void mousePressEvent(QMouseEvent *e)
void updateViewport(int screendx=0, int screendy=0)