void ImageViewer::paintEvent(QPaintEvent *e) { QPainter painter(this); if(bgColor.isValid()) painter.fillRect(e->rect(), bgColor); if(noPicture() && !errStr.isEmpty()){ painter.drawText(rect(), Qt::AlignCenter, errStr); return; } switch(antialiasMode){ case Config::AntialiasWhenZoomIn: if(scale <= 1) break; case Config::AlwaysAntialias: painter.setRenderHint(QPainter::SmoothPixmapTransform); break; case Config::NoAntialias: default: break; } painter.save(); painter.translate(topLeft + shift); painter.scale(scale, scale); // painter.setClipRect(e->rect()); painter.drawImage(Config::OriginPoint, image); painter.restore(); /// // draw scroll bar if(velocityTracker->isScrolling()){ const QColor LINE_COLOR(0, 0, 0, 80); painter.setBrush(LINE_COLOR); painter.setPen(Qt::NoPen); const int LINE_WIDTH = 10; const int MARGE = 3; const qreal RADIUS = 6.0; int rectW = rect().width(), rectH = rect().height(); qreal scaleW = image.width() * scale; //! qreal qreal scaleH = image.height() * scale; if(scaleW > rectW){ // draw horizontal scroll bar QRectF rectangle(-(topLeft + shift).x() / scaleW * rectW, rectH - LINE_WIDTH - MARGE, rectW / scaleW * rectW, LINE_WIDTH); painter.drawRoundedRect(rectangle, RADIUS, RADIUS); } if(scaleH > rectH){ // draw vertical scroll bar QRectF rectangle(rectW - LINE_WIDTH - MARGE, -(topLeft + shift).y() / scaleH * rectH, LINE_WIDTH, rectH / scaleH * rectH); painter.drawRoundedRect(rectangle, RADIUS, RADIUS); } } }
void ImageViewer::updateCursor() { if(noPicture() || !scaleLargeThanWidget()) { changeCursor(Qt::ArrowCursor); } else if(leftMousePressed) { changeCursor(Qt::ClosedHandCursor); } else { changeCursor(Qt::OpenHandCursor); } }
void ImageViewer::rotatePixmap(int degree) { if(noPicture()) return; rotate += degree; rotate %= 360; QMatrix matrix = QMatrix().rotate(degree); image = image.transformed(matrix, Qt::SmoothTransformation); layoutImage(); update(); }
void ImageViewer::rotatePixmap(bool isLeft) { if(noPicture()) return; rotate += (isLeft ? -90 : 90); rotate %= 360; QMatrix m = QMatrix().rotate(isLeft ? -90 : 90); image = image.transformed(m, Qt::SmoothTransformation); initToFitWidget(); update(); }
void ImageViewer::resizeEvent(QResizeEvent *e) { if(noPicture()) return; if(hasUserZoom){ updateTopLeft(); updateShift(); }else{ initToFitWidget(); } QWidget::resizeEvent(e); }
void ImageViewer::resizeEvent(QResizeEvent *e) { if(noPicture()) return; if(hasUserZoom){ calcTopLeft(); updateShift(); updateCursor(); }else{ layoutImage(); //! FIXME: if user drag and move image content (when AlignCenter is hasUserZoom and shift != (0,0)) , this will reset the shift if align mode isn't AlignCenterCenter. } QWidget::resizeEvent(e); }
void ImageViewer::zoomIn(double factor, const QPoint &pivot) { if(noPicture() || !rect().contains(pivot)) // pivot must inner widget return; qreal scale_old = scale; zoomIn(factor); QPointF distance(rect().center() - pivot); QPointF change(distance / scale_old * scale - distance); shift += change; // to keep the pivot position, must after scale. updateShift(); }
void ImageViewer::mirrored(bool horizontal, bool vertical) { if(noPicture()) return; if(horizontal) mirrorH = !mirrorH; if(vertical) mirrorV = !mirrorV; image = image.mirrored(horizontal, vertical); initToFitWidget(); update(); }
void ImageViewer::mirrored(MirrorMode mode) { if(noPicture()) return; switch (mode) { case MirrorHorizontal: mirrorH = !mirrorH; break; case MirrorVertical: mirrorV = !mirrorV; break; default: return; } image = image.mirrored(mode == MirrorHorizontal, mode == MirrorVertical); layoutImage(); update(); }
void ImageViewer::initToFitWidget()//no change the value of rotate { //restore the cursor even if the image cannot load. setCursor(QCursor(Qt::ArrowCursor)); if(noPicture()) return; QSize pixSize(image.size()); //if image large than widget, will scale image to fit widget. if(!(rect().size() - pixSize).isValid())//! SIZE_ADJUST !!! pixSize.scale(rect().size() + Config::SizeAdjusted, Qt::KeepAspectRatio); if(image.width() == 0) scale = 1.0; else scale = qreal(pixSize.width()) / image.width(); scaleMin = qMin(Config::ScaleMin, scale); updateTopLeft(); shift = Config::OriginPoint; }
void ImageViewer::zoomIn(double factor) { if(noPicture()) return; qreal scale_old = scale; scale += factor; scale = qMax(scaleMin, qMin(Config::ScaleMax, scale)); if(scale == scale_old)//scale no changed return; /*! topLeft must determined before shift, * otherwise will impact updateShift() */ updateTopLeft(); shift /= scale_old; shift *= scale; updateShift(); hasUserZoom = true; ToolTip::showText(mapToGlobal(rect().center()), QString("<font size='7'><b>%1%</b></font>").arg(scale * 100, 0, 'g', 4), true, 0.7, 800); }
void ImageViewer::wheelEvent(QWheelEvent *e) { if(noPicture() || !rect().contains(e->pos())) //cursor is not in widget return; qreal scale_old = scale; switch(e->modifiers()){ case Qt::ShiftModifier: zoomIn(e->delta() / qreal(2400)); //e->delta() is +120 or -120 break; case Qt::ControlModifier: zoomIn(e->delta() / qreal(600)); break; default: zoomIn(e->delta() / qreal(1200)); break; } QPointF distance(rect().center() - e->pos()); QPointF change(distance / scale_old * scale - distance); shift += change; //to keep the cursor position, must after scale. updateShift(); }