void ImageView::doScale(qreal factor) { qreal currentScale = transform().m11(); qreal newScale = currentScale * factor; qreal actualFactor = factor; // cap to 0.001 - 1000 if (newScale > 1000) actualFactor = 1000./currentScale; else if (newScale < 0.001) actualFactor = 0.001/currentScale; scale(actualFactor, actualFactor); emitScaleFactor(); }
void ImageView::fitToScreen() { fitInView(d->imageItem, Qt::KeepAspectRatio); emitScaleFactor(); }
void ImageView::resetToOriginalSize() { resetTransform(); emitScaleFactor(); }
void ImageView::doScale(qreal factor) { scale(factor, factor); emitScaleFactor(); }
bool ImageView::openFile(QString fileName) { #ifndef QT_NO_SVG bool isSvg = false; #endif QByteArray format = QImageReader::imageFormat(fileName); // if it is impossible to recognize a file format - file will not be open correctly if (format.isEmpty()) return false; #ifndef QT_NO_SVG if (format.startsWith("svg")) isSvg = true; #endif QGraphicsScene *s = scene(); bool drawBackground = (d->backgroundItem ? d->backgroundItem->isVisible() : false); bool drawOutline = (d->outlineItem ? d->outlineItem->isVisible() : true); s->clear(); resetTransform(); // image #ifndef QT_NO_SVG if (isSvg) { d->imageItem = new QGraphicsSvgItem(fileName); } else #endif { QPixmap pixmap(fileName); QGraphicsPixmapItem *pixmapItem = new QGraphicsPixmapItem(pixmap); pixmapItem->setTransformationMode(Qt::SmoothTransformation); d->imageItem = pixmapItem; } d->imageItem->setCacheMode(QGraphicsItem::NoCache); d->imageItem->setZValue(0); // background item d->backgroundItem = new QGraphicsRectItem(d->imageItem->boundingRect()); d->backgroundItem->setBrush(Qt::white); d->backgroundItem->setPen(Qt::NoPen); d->backgroundItem->setVisible(drawBackground); d->backgroundItem->setZValue(-1); // outline d->outlineItem = new QGraphicsRectItem(d->imageItem->boundingRect()); QPen outline(Qt::black, 1, Qt::DashLine); outline.setCosmetic(true); d->outlineItem->setPen(outline); d->outlineItem->setBrush(Qt::NoBrush); d->outlineItem->setVisible(drawOutline); d->outlineItem->setZValue(1); s->addItem(d->backgroundItem); s->addItem(d->imageItem); s->addItem(d->outlineItem); // if image size is 0x0, then it is not loaded if (d->imageItem->boundingRect().height() == 0 && d->imageItem->boundingRect().width() == 0) return false; emitScaleFactor(); return true; }