#include #include #include "qimagewidget.hpp" #include "renderermanager.hpp" static const double ZoomInFactor = 0.8; static const double ZoomOutFactor = 1 / ZoomInFactor; QImageWidget::QImageWidget(QWidget *parent, RendererManager* thread) : QWidget(parent) { curScale = 1.0; autofit = false; qRegisterMetaType("QImage"); connect(thread, SIGNAL(renderedImage(QImage)), this, SLOT(updatePixmap(QImage))); qRegisterMetaType("TZoomTypes"); connect(parent, SIGNAL(zoomfct(TZoomTypes)), this, SLOT(zoomIntercept(TZoomTypes))); #ifndef QT_NO_CURSOR setCursor(Qt::CrossCursor); #endif } void QImageWidget::paintEvent(QPaintEvent * /* event */) { QPainter painter(this); painter.fillRect(rect(), Qt::black); if (pixmap.isNull()) { painter.setPen(Qt::white); painter.drawText(rect(), Qt::AlignCenter, tr("Waiting for data...")); return; } if (curScale == 1.0) { painter.drawPixmap(pixmapOffset, pixmap); } else { int newWidth = int(pixmap.width() * curScale); int newHeight = int(pixmap.height() * curScale); int newX = pixmapOffset.x() + ((pixmap.width() - newWidth) >> 1 /*/ 2*/); int newY = pixmapOffset.y() + ((pixmap.height() - newHeight) >> 1 /*/ 2*/); painter.save(); painter.translate(newX, newY); painter.scale(curScale, curScale); QRectF exposed = painter.matrix().inverted().mapRect(rect()).adjusted(-1, -1, 1, 1); painter.drawPixmap(exposed, pixmap, exposed); painter.restore(); } QString text = tr("Use mouse wheel to zoom. " "Press and hold left mouse button to scroll."); // QString data = QString::number(curScale); QFontMetrics metrics = painter.fontMetrics(); int textWidth = metrics.width(text); // int textHeight = metrics.height(); painter.setPen(Qt::NoPen); painter.setBrush(QColor(0, 0, 0, 127)); painter.drawRect(((width() - textWidth) >> 1 /*/ 2*/) - 5, 0, textWidth + 10, metrics.lineSpacing() + 5); painter.setPen(Qt::white); painter.drawText((width() - textWidth) >> 1 /*/ 2*/, metrics.leading() + metrics.ascent(), text); // painter.drawText(0, height() - textHeight, metrics.width(data), textHeight, 0, data); } void QImageWidget::resizeEvent(QResizeEvent * /* event */) { if(autofit) emit(zoomIntercept(EZoom_fit)); else pixmapOffset = QPoint((width() - pixmap.width()) >> 1, (height() - pixmap.height()) >> 1); update(); } void QImageWidget::keyPressEvent(QKeyEvent *event) { QWidget::keyPressEvent(event); } void QImageWidget::zoomIntercept(const TZoomTypes type) { switch(type) { case EZoom_plus: zoom(ZoomInFactor); break; case EZoom_minus: zoom(ZoomOutFactor); break; case EZoom_fit: { if(pixmap.isNull()) return; const double wsc = width() / double(pixmap.width()); const double hsc = height() / double(pixmap.height()); const bool boow = width() < pixmap.width(); const bool booh = height() < pixmap.height(); if(boow == booh) curScale = std::min(wsc, hsc); else if(boow != booh && boow) curScale = wsc; else curScale = hsc; emit(scaleIsModified(curScale)); pixmapOffset = QPoint((width() - pixmap.width()) >> 1, (height() - pixmap.height()) >> 1); update(); } break; case EZoom_orig: curScale = 1.0; emit(scaleIsModified(curScale)); pixmapOffset = QPoint((width() - pixmap.width()) >> 1, (height() - pixmap.height()) >> 1); update(); break; default: break; } } void QImageWidget::wheelEvent(QWheelEvent *event) { int numDegrees = event->delta() >> 3;// / 8 double numSteps = numDegrees / 15.0; zoom(pow(ZoomInFactor, numSteps)); } void QImageWidget::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) lastDragPos = event->pos(); } void QImageWidget::mouseMoveEvent(QMouseEvent *event) { if (event->buttons() & Qt::LeftButton) { pixmapOffset += event->pos() - lastDragPos; lastDragPos = event->pos(); update(); } } void QImageWidget::mouseReleaseEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { pixmapOffset += event->pos() - lastDragPos; lastDragPos = QPoint(); } } void QImageWidget::updatePixmap(const QImage &image) { if (!lastDragPos.isNull()) return; pixmap = QPixmap::fromImage(image); pixmapOffset = QPoint((width() - pixmap.width()) >> 1, (height() - pixmap.height()) >> 1); lastDragPos = QPoint(); update(); } void QImageWidget::zoom(double zoomFactor) { curScale *= zoomFactor; emit(scaleIsModified(curScale)); update(); } const QImage QImageWidget::getImage() const { return pixmap.toImage(); }