Exemple #1
0
void ImageView::paintEvent(QPaintEvent* event) {
  // if the image is scaled and we have a high quality cached image
  if(imageItem_ && scaleFactor_ != 1.0 && !cachedPixmap_.isNull()) {
    // rectangle of the whole image in viewport coordinate
    QRect viewportImageRect = sceneToViewport(imageItem_->rect());
    // the visible part of the image.
    QRect desiredCachedRect = viewportToScene(viewportImageRect.intersected(viewport()->rect()));
    // check if the cached area is what we need and if the cache is out of date
    if(cachedSceneRect_ == desiredCachedRect) {
      // rect of the image area that needs repaint, in viewport coordinate
      QRect repaintImageRect = viewportImageRect.intersected(event->rect());
      // see if the part asking for repaint is contained by our cache.
      if(cachedRect_.contains(repaintImageRect)) {
        QPainter painter(viewport());
        painter.fillRect(event->rect(), backgroundBrush());
        painter.drawPixmap(repaintImageRect, cachedPixmap_);
        return;
      }
    }
  }
  if(!image_.isNull()) { // we don't have a cache yet or it's out of date already, generate one
    queueGenerateCache();
  }
  QGraphicsView::paintEvent(event);
}
Exemple #2
0
void ImageView::setImage(QImage image, bool show) {
  if(show && (gifMovie_ || isSVG)) { // a gif animation or SVG file was shown before
    scene_->clear();
    isSVG = false;
    if(gifMovie_) { // should be deleted explicitly
      delete gifMovie_;
      gifMovie_ = nullptr;
    }
    // recreate the rect item
    imageItem_ = new QGraphicsRectItem();
    imageItem_->hide();
    imageItem_->setPen(QPen(Qt::NoPen));
    scene_->addItem(imageItem_);
  }

  image_ = image;
  if(image.isNull()) {
    imageItem_->hide();
    imageItem_->setBrush(QBrush());
    scene_->setSceneRect(0, 0, 0, 0);
  }
  else {
    if(show) {
      imageItem_->setRect(0, 0, image_.width(), image_.height());
      imageItem_->setBrush(image_);
      imageItem_->show();
    }
    scene_->setSceneRect(0, 0, image_.width(), image_.height());
  }

  if(autoZoomFit_)
    zoomFit();
  queueGenerateCache();
}
Exemple #3
0
void ImageView::setScaleFactor(double factor) {
  if(factor != scaleFactor_) {
    scaleFactor_ = factor;
    resetTransform();
    scale(factor, factor);
    queueGenerateCache();
  }
}
Exemple #4
0
void ImageView::zoomOut() {
  autoZoomFit_ = false;
  if(!image_.isNull()) {
    resetTransform();
    scaleFactor_ /= 1.1;
    scale(scaleFactor_, scaleFactor_);
    queueGenerateCache();
  }
}
Exemple #5
0
void ImageView::zoomFit()
{
  if (!m_image.isNull() && m_image.width() <= width() && m_image.height() <= height()) {
    zoomOriginal();
    return;
  }

  fitInView(m_scene->sceneRect(), Qt::KeepAspectRatio);
  m_scaleFactor = transform().m11();

  queueGenerateCache();
}
Exemple #6
0
void ImageView::zoomFit() {
  if(!image_.isNull()) {
    // if the image is smaller than our view, use its original size
    // instead of scaling it up.
    if(image_.width() <= width() && image_.height() <= height()) {
      zoomOriginal();
      return;
    }
  }
  fitInView(scene_->sceneRect(), Qt::KeepAspectRatio);
  scaleFactor_ = transform().m11();
  queueGenerateCache();
}
Exemple #7
0
void ImageView::zoom(double scale)
{
  if (m_scaleFactor == scale)
    return;

  m_autoZoomFit = false;

  resetTransform();
  m_scaleFactor = scale;
  this->scale(scale, scale);

  queueGenerateCache();
}
Exemple #8
0
void ImageView::setSVG(QString fileName) {
  image_ = QImage(fileName); // for tracking position and dimensions
  if(image_.isNull()) {
    if(imageItem_) {
      imageItem_->hide();
      imageItem_->setBrush(QBrush());
    }
    scene_->setSceneRect(0, 0, 0, 0);
  }
  else {
    scene_->clear();
    imageItem_ = nullptr;
    isSVG = true;
    QGraphicsSvgItem *svgItem = new QGraphicsSvgItem(fileName);
    scene_->addItem(svgItem);
    scene_->setSceneRect(svgItem->boundingRect());
  }

  if(autoZoomFit_)
    zoomFit();
  queueGenerateCache(); // deletes the cache timer in this case
}
Exemple #9
0
void ImageView::setImage(const QImage &image)
{
  reset();

  if (image.format() == QImage::Format_Indexed8)
    m_image = image.convertToFormat(QImage::Format_ARGB32_Premultiplied);
  else
    m_image = image;

  m_item = new QGraphicsRectItem();
  m_item->setRect(0, 0, m_image.width(), m_image.height());
  m_item->setBrush(m_image);
  m_item->setPen(Qt::NoPen);

  m_scene->addItem(m_item);
  m_scene->setSceneRect(0, 0, m_image.width(), m_image.height());

  if (m_autoZoomFit)
    zoomFit();

  queueGenerateCache();
}
Exemple #10
0
void ImageView::paintEvent(QPaintEvent* event)
{
  if (m_item && m_scaleFactor != 1.0 && !m_cachedPixmap.isNull()) {
    const QRect viewportImageRect = sceneToViewport(m_item->rect());
    const QRect desiredCachedRect = viewportToScene(viewportImageRect.intersected(viewport()->rect()));

    if (m_cachedSceneRect == desiredCachedRect) {
      const QRect repaintImageRect = viewportImageRect.intersected(event->rect());
      if (m_cachedRect.contains(repaintImageRect)) {
        QPainter painter(viewport());
        painter.fillRect(event->rect(), backgroundBrush());
        painter.drawPixmap(repaintImageRect, m_cachedPixmap);
        return;
      }
    }
  }

  if (!m_image.isNull())
    queueGenerateCache();

  QGraphicsView::paintEvent(event);
}
Exemple #11
0
void ImageView::setGifAnimation(QString fileName) {
  /* the built-in gif reader gives the first frame, which won't
     be shown but is used for tracking position and dimensions */
  image_ = QImage(fileName);
  if(image_.isNull()) {
    if(imageItem_) {
      imageItem_->hide();
      imageItem_->setBrush(QBrush());
    }
    scene_->setSceneRect(0, 0, 0, 0);
  }
  else {
    scene_->clear();
    imageItem_ = nullptr; // it's deleted by clear();
    if(gifMovie_) {
      delete gifMovie_;
      gifMovie_ = nullptr;
    }
    QPixmap pix(image_.size());
    pix.fill(Qt::transparent);
    QGraphicsItem *gifItem = new QGraphicsPixmapItem(pix);
    QLabel *gifLabel = new QLabel();
    gifMovie_ = new QMovie(fileName);
    QGraphicsProxyWidget* gifWidget = new QGraphicsProxyWidget(gifItem);
    gifLabel->setAttribute(Qt::WA_NoSystemBackground);
    gifLabel->setMovie(gifMovie_);
    gifWidget->setWidget(gifLabel);
    gifMovie_->start();
    scene_->addItem(gifItem);
    scene_->setSceneRect(gifItem->boundingRect());
  }

  if(autoZoomFit_)
    zoomFit();
  queueGenerateCache(); // deletes the cache timer in this case
}
Exemple #12
0
void ImageView::zoomOriginal() {
  resetTransform();
  scaleFactor_ = 1.0;
  autoZoomFit_ = false;
  queueGenerateCache();
}