#include "HGImgView.h" #include #include #include #include #include #include #include "base/HGInc.h" #include "imgfmt/HGImgFmt.h" #include "HGUIGlobal.h" #include HGImgView::HGImgView(QWidget* parent) : QWidget(parent) { m_scrollSize = 10; m_minScrollSliderSize = 8; for (int i = 0; i < 3; ++i) { m_hScrollLeftImage[i] = nullptr; m_hScrollRightImage[i] = nullptr; m_vScrollTopImage[i] = nullptr; m_vScrollBottomImage[i] = nullptr; m_hScrollSliderImage[i] = nullptr; m_vScrollSliderImage[i] = nullptr; } m_hScrollImage = nullptr; m_vScrollImage = nullptr; m_nullScrollImage = nullptr; m_enableScroll = false; m_mouseOn = false; m_hScroll = false; m_vScroll = false; m_image = nullptr; m_qImage = nullptr; m_showImage = false; memset(&m_showRect, 0, sizeof(HGRectF)); m_mouseMoveStatus = MouseStatus_Null; m_mousePressStatus = MouseStatus_Null; m_mousePressBeginX = -1; m_mousePressBeginY = -1; m_showColorInfo = false; m_operate = 0; m_beginX = -1; m_beginY = -1; m_draging = false; setFocusPolicy(Qt::ClickFocus); setMouseTracking(true); setAcceptDrops(true); } HGImgView::~HGImgView() { clearImage(); delete m_hScrollImage; m_hScrollImage = nullptr; delete m_vScrollImage; m_vScrollImage = nullptr; delete m_nullScrollImage; m_nullScrollImage = nullptr; for (int i = 0; i < 3; ++i) { delete m_hScrollLeftImage[i]; m_hScrollLeftImage[i] = nullptr; delete m_hScrollRightImage[i]; m_hScrollRightImage[i] = nullptr; delete m_vScrollTopImage[i]; m_vScrollTopImage[i] = nullptr; delete m_vScrollBottomImage[i]; m_vScrollBottomImage[i] = nullptr; delete m_hScrollSliderImage[i]; m_hScrollSliderImage[i] = nullptr; delete m_vScrollSliderImage[i]; m_vScrollSliderImage[i] = nullptr; } qDebug("~HGImgView"); } HGResult HGImgView::setScrollSize(int size) { if (MouseStatus_Null != m_mousePressStatus || 0 != m_operate || m_draging) { return HGBASE_ERR_FAIL; } if (size < 10 || size > 20) { return HGBASE_ERR_INVALIDARG; } if (nullptr != m_image) { return HGBASE_ERR_FAIL; } m_scrollSize = size; return HGBASE_ERR_OK; } HGResult HGImgView::setMinScrollSliderSize(int size) { if (MouseStatus_Null != m_mousePressStatus || 0 != m_operate || m_draging) { return HGBASE_ERR_FAIL; } if (size < 8 || size > 64) { return HGBASE_ERR_INVALIDARG; } if (nullptr != m_image) { return HGBASE_ERR_FAIL; } m_minScrollSliderSize = size; return HGBASE_ERR_OK; } HGResult HGImgView::setHScrollLeftImage(const QImage *normalImage, const QImage *hotImage, const QImage *pushImage) { if (MouseStatus_Null != m_mousePressStatus || 0 != m_operate || m_draging) { return HGBASE_ERR_FAIL; } if (nullptr != m_image) { return HGBASE_ERR_FAIL; } if (nullptr == normalImage) { delete m_hScrollLeftImage[0]; m_hScrollLeftImage[0] = nullptr; } else { QImage *img = new QImage(*normalImage); if (!img->isNull()) { if (nullptr != m_hScrollLeftImage[0]) delete m_hScrollLeftImage[0]; m_hScrollLeftImage[0] = img; } else { delete img; } } if (nullptr == hotImage) { delete m_hScrollLeftImage[1]; m_hScrollLeftImage[1] = nullptr; } else { QImage *img = new QImage(*hotImage); if (!img->isNull()) { if (nullptr != m_hScrollLeftImage[1]) delete m_hScrollLeftImage[1]; m_hScrollLeftImage[1] = img; } else { delete img; } } if (nullptr == pushImage) { delete m_hScrollLeftImage[2]; m_hScrollLeftImage[2] = nullptr; } else { QImage *img = new QImage(*pushImage); if (!img->isNull()) { if (nullptr != m_hScrollLeftImage[2]) delete m_hScrollLeftImage[2]; m_hScrollLeftImage[2] = img; } else { delete img; } } return HGBASE_ERR_OK; } HGResult HGImgView::setHScrollRightImage(const QImage *normalImage, const QImage *hotImage, const QImage *pushImage) { if (MouseStatus_Null != m_mousePressStatus || 0 != m_operate || m_draging) { return HGBASE_ERR_FAIL; } if (nullptr != m_image) { return HGBASE_ERR_FAIL; } if (nullptr == normalImage) { delete m_hScrollRightImage[0]; m_hScrollRightImage[0] = nullptr; } else { QImage *img = new QImage(*normalImage); if (!img->isNull()) { if (nullptr != m_hScrollRightImage[0]) delete m_hScrollRightImage[0]; m_hScrollRightImage[0] = img; } else { delete img; } } if (nullptr == hotImage) { delete m_hScrollRightImage[1]; m_hScrollRightImage[1] = nullptr; } else { QImage *img = new QImage(*hotImage); if (!img->isNull()) { if (nullptr != m_hScrollRightImage[1]) delete m_hScrollRightImage[1]; m_hScrollRightImage[1] = img; } else { delete img; } } if (nullptr == pushImage) { delete m_hScrollRightImage[2]; m_hScrollRightImage[2] = nullptr; } else { QImage *img = new QImage(*pushImage); if (!img->isNull()) { if (nullptr != m_hScrollRightImage[2]) delete m_hScrollRightImage[2]; m_hScrollRightImage[2] = img; } else { delete img; } } return HGBASE_ERR_OK; } HGResult HGImgView::setVScrollTopImage(const QImage *normalImage, const QImage *hotImage, const QImage *pushImage) { if (MouseStatus_Null != m_mousePressStatus || 0 != m_operate || m_draging) { return HGBASE_ERR_FAIL; } if (nullptr != m_image) { return HGBASE_ERR_FAIL; } if (nullptr == normalImage) { delete m_vScrollTopImage[0]; m_vScrollTopImage[0] = nullptr; } else { QImage *img = new QImage(*normalImage); if (!img->isNull()) { if (nullptr != m_vScrollTopImage[0]) delete m_vScrollTopImage[0]; m_vScrollTopImage[0] = img; } else { delete img; } } if (nullptr == hotImage) { delete m_vScrollTopImage[1]; m_vScrollTopImage[1] = nullptr; } else { QImage *img = new QImage(*hotImage); if (!img->isNull()) { if (nullptr != m_vScrollTopImage[1]) delete m_vScrollTopImage[1]; m_vScrollTopImage[1] = img; } else { delete img; } } if (nullptr == pushImage) { delete m_vScrollTopImage[2]; m_vScrollTopImage[2] = nullptr; } else { QImage *img = new QImage(*pushImage); if (!img->isNull()) { if (nullptr != m_vScrollTopImage[2]) delete m_vScrollTopImage[2]; m_vScrollTopImage[2] = img; } else { delete img; } } return HGBASE_ERR_OK; } HGResult HGImgView::setVScrollBottomImage(const QImage *normalImage, const QImage *hotImage, const QImage *pushImage) { if (MouseStatus_Null != m_mousePressStatus || 0 != m_operate || m_draging) { return HGBASE_ERR_FAIL; } if (nullptr != m_image) { return HGBASE_ERR_FAIL; } if (nullptr == normalImage) { delete m_vScrollBottomImage[0]; m_vScrollBottomImage[0] = nullptr; } else { QImage *img = new QImage(*normalImage); if (!img->isNull()) { if (nullptr != m_vScrollBottomImage[0]) delete m_vScrollBottomImage[0]; m_vScrollBottomImage[0] = img; } else { delete img; } } if (nullptr == hotImage) { delete m_vScrollBottomImage[1]; m_vScrollBottomImage[1] = nullptr; } else { QImage *img = new QImage(*hotImage); if (!img->isNull()) { if (nullptr != m_vScrollBottomImage[1]) delete m_vScrollBottomImage[1]; m_vScrollBottomImage[1] = img; } else { delete img; } } if (nullptr == pushImage) { delete m_vScrollBottomImage[2]; m_vScrollBottomImage[2] = nullptr; } else { QImage *img = new QImage(*pushImage); if (!img->isNull()) { if (nullptr != m_vScrollBottomImage[2]) delete m_vScrollBottomImage[2]; m_vScrollBottomImage[2] = img; } else { delete img; } } return HGBASE_ERR_OK; } HGResult HGImgView::setHScrollImage(const QImage *image, const HGRect *stretch) { if (MouseStatus_Null != m_mousePressStatus || 0 != m_operate || m_draging) { return HGBASE_ERR_FAIL; } if (nullptr != m_image) { return HGBASE_ERR_FAIL; } if (nullptr == image) { delete m_hScrollImage; m_hScrollImage = nullptr; } else { QImage *img = new QImage(*image); if (!img->isNull()) { if (nullptr != m_hScrollImage) delete m_hScrollImage; m_hScrollImage = img; if (nullptr == stretch || stretch->left < 0 || stretch->top < 0 || stretch->right > m_hScrollImage->width() || stretch->bottom > m_hScrollImage->height() || stretch->left >= stretch->right || stretch->top >= stretch->bottom) { m_hScrollImageStretch.setX(0); m_hScrollImageStretch.setY(0); m_hScrollImageStretch.setWidth(m_hScrollImage->width()); m_hScrollImageStretch.setHeight(m_hScrollImage->height()); } else { m_hScrollImageStretch.setX(stretch->left); m_hScrollImageStretch.setY(stretch->top); m_hScrollImageStretch.setWidth(stretch->right - stretch->left); m_hScrollImageStretch.setHeight(stretch->bottom - stretch->top); } } else { delete img; } } return HGBASE_ERR_OK; } HGResult HGImgView::setHScrollSliderImage(const QImage *normalImage, const HGRect *normalStretch, const QImage *hotImage, const HGRect *hotStretch, const QImage *pushImage, const HGRect *pushStretch) { if (MouseStatus_Null != m_mousePressStatus || 0 != m_operate || m_draging) { return HGBASE_ERR_FAIL; } if (nullptr != m_image) { return HGBASE_ERR_FAIL; } if (nullptr == normalImage) { delete m_hScrollSliderImage[0]; m_hScrollSliderImage[0] = nullptr; } else { QImage *img = new QImage(*normalImage); if (!img->isNull()) { if (nullptr != m_hScrollSliderImage[0]) delete m_hScrollSliderImage[0]; m_hScrollSliderImage[0] = img; if (nullptr == normalStretch || normalStretch->left < 0 || normalStretch->top < 0 || normalStretch->right > m_hScrollSliderImage[0]->width() || normalStretch->bottom > m_hScrollSliderImage[0]->height() || normalStretch->left >= normalStretch->right || normalStretch->top >= normalStretch->bottom) { m_hScrollSliderImageStretch[0].setX(0); m_hScrollSliderImageStretch[0].setY(0); m_hScrollSliderImageStretch[0].setWidth(m_hScrollSliderImage[0]->width()); m_hScrollSliderImageStretch[0].setHeight(m_hScrollSliderImage[0]->height()); } else { m_hScrollSliderImageStretch[0].setX(normalStretch->left); m_hScrollSliderImageStretch[0].setY(normalStretch->top); m_hScrollSliderImageStretch[0].setWidth(normalStretch->right - normalStretch->left); m_hScrollSliderImageStretch[0].setHeight(normalStretch->bottom - normalStretch->top); } } else { delete img; } } if (nullptr == hotImage) { delete m_hScrollSliderImage[1]; m_hScrollSliderImage[1] = nullptr; } else { QImage *img = new QImage(*hotImage); if (!img->isNull()) { if (nullptr != m_hScrollSliderImage[1]) delete m_hScrollSliderImage[1]; m_hScrollSliderImage[1] = img; if (nullptr == hotStretch || hotStretch->left < 0 || hotStretch->top < 0 || hotStretch->right > m_hScrollSliderImage[1]->width() || hotStretch->bottom > m_hScrollSliderImage[1]->height() || hotStretch->left >= hotStretch->right || hotStretch->top >= hotStretch->bottom) { m_hScrollSliderImageStretch[1].setX(0); m_hScrollSliderImageStretch[1].setY(0); m_hScrollSliderImageStretch[1].setWidth(m_hScrollSliderImage[1]->width()); m_hScrollSliderImageStretch[1].setHeight(m_hScrollSliderImage[1]->height()); } else { m_hScrollSliderImageStretch[1].setX(hotStretch->left); m_hScrollSliderImageStretch[1].setY(hotStretch->top); m_hScrollSliderImageStretch[1].setWidth(hotStretch->right - hotStretch->left); m_hScrollSliderImageStretch[1].setHeight(hotStretch->bottom - hotStretch->top); } } else { delete img; } } if (nullptr == pushImage) { delete m_hScrollSliderImage[2]; m_hScrollSliderImage[2] = nullptr; } else { QImage *img = new QImage(*pushImage); if (!img->isNull()) { if (nullptr != m_hScrollSliderImage[2]) delete m_hScrollSliderImage[2]; m_hScrollSliderImage[2] = img; if (nullptr == pushStretch || pushStretch->left < 0 || pushStretch->top < 0 || pushStretch->right > m_hScrollSliderImage[2]->width() || pushStretch->bottom > m_hScrollSliderImage[2]->height() || pushStretch->left >= pushStretch->right || pushStretch->top >= hotStretch->bottom) { m_hScrollSliderImageStretch[2].setX(0); m_hScrollSliderImageStretch[2].setY(0); m_hScrollSliderImageStretch[2].setWidth(m_hScrollSliderImage[2]->width()); m_hScrollSliderImageStretch[2].setHeight(m_hScrollSliderImage[2]->height()); } else { m_hScrollSliderImageStretch[2].setX(pushStretch->left); m_hScrollSliderImageStretch[2].setY(pushStretch->top); m_hScrollSliderImageStretch[2].setWidth(pushStretch->right - pushStretch->left); m_hScrollSliderImageStretch[2].setHeight(pushStretch->bottom - pushStretch->top); } } else { delete img; } } return HGBASE_ERR_OK; } HGResult HGImgView::setVScrollImage(const QImage *image, const HGRect *stretch) { if (MouseStatus_Null != m_mousePressStatus || 0 != m_operate || m_draging) { return HGBASE_ERR_FAIL; } if (nullptr != m_image) { return HGBASE_ERR_FAIL; } if (nullptr == image) { delete m_vScrollImage; m_vScrollImage = nullptr; } else { QImage *img = new QImage(*image); if (!img->isNull()) { if (nullptr != m_vScrollImage) delete m_vScrollImage; m_vScrollImage = img; if (nullptr == stretch || stretch->left < 0 || stretch->top < 0 || stretch->right > m_vScrollImage->width() || stretch->bottom > m_vScrollImage->height() || stretch->left >= stretch->right || stretch->top >= stretch->bottom) { m_vScrollImageStretch.setX(0); m_vScrollImageStretch.setY(0); m_vScrollImageStretch.setWidth(m_vScrollImage->width()); m_vScrollImageStretch.setHeight(m_vScrollImage->height()); } else { m_vScrollImageStretch.setX(stretch->left); m_vScrollImageStretch.setY(stretch->top); m_vScrollImageStretch.setWidth(stretch->right - stretch->left); m_vScrollImageStretch.setHeight(stretch->bottom - stretch->top); } } else { delete img; } } return HGBASE_ERR_OK; } HGResult HGImgView::setVScrollSliderImage(const QImage *normalImage, const HGRect *normalStretch, const QImage *hotImage, const HGRect *hotStretch, const QImage *pushImage, const HGRect *pushStretch) { if (MouseStatus_Null != m_mousePressStatus || 0 != m_operate || m_draging) { return HGBASE_ERR_FAIL; } if (nullptr != m_image) { return HGBASE_ERR_FAIL; } if (nullptr == normalImage) { delete m_vScrollSliderImage[0]; m_vScrollSliderImage[0] = nullptr; } else { QImage *img = new QImage(*normalImage); if (!img->isNull()) { if (nullptr != m_vScrollSliderImage[0]) delete m_vScrollSliderImage[0]; m_vScrollSliderImage[0] = img; if (nullptr == normalStretch || normalStretch->left < 0 || normalStretch->top < 0 || normalStretch->right > m_vScrollSliderImage[0]->width() || normalStretch->bottom > m_vScrollSliderImage[0]->height() || normalStretch->left >= normalStretch->right || normalStretch->top >= normalStretch->bottom) { m_vScrollSliderImageStretch[0].setX(0); m_vScrollSliderImageStretch[0].setY(0); m_vScrollSliderImageStretch[0].setWidth(m_vScrollSliderImage[0]->width()); m_vScrollSliderImageStretch[0].setHeight(m_vScrollSliderImage[0]->height()); } else { m_vScrollSliderImageStretch[0].setX(normalStretch->left); m_vScrollSliderImageStretch[0].setY(normalStretch->top); m_vScrollSliderImageStretch[0].setWidth(normalStretch->right - normalStretch->left); m_vScrollSliderImageStretch[0].setHeight(normalStretch->bottom - normalStretch->top); } } else { delete img; } } if (nullptr == hotImage) { delete m_vScrollSliderImage[1]; m_vScrollSliderImage[1] = nullptr; } else { QImage *img = new QImage(*hotImage); if (!img->isNull()) { if (nullptr != m_vScrollSliderImage[1]) delete m_vScrollSliderImage[1]; m_vScrollSliderImage[1] = img; if (nullptr == hotStretch || hotStretch->left < 0 || hotStretch->top < 0 || hotStretch->right > m_vScrollSliderImage[1]->width() || hotStretch->bottom > m_vScrollSliderImage[1]->height() || hotStretch->left >= hotStretch->right || hotStretch->top >= hotStretch->bottom) { m_vScrollSliderImageStretch[1].setX(0); m_vScrollSliderImageStretch[1].setY(0); m_vScrollSliderImageStretch[1].setWidth(m_vScrollSliderImage[1]->width()); m_vScrollSliderImageStretch[1].setHeight(m_vScrollSliderImage[1]->height()); } else { m_vScrollSliderImageStretch[1].setX(hotStretch->left); m_vScrollSliderImageStretch[1].setY(hotStretch->top); m_vScrollSliderImageStretch[1].setWidth(hotStretch->right - hotStretch->left); m_vScrollSliderImageStretch[1].setHeight(hotStretch->bottom - hotStretch->top); } } else { delete img; } } if (nullptr == pushImage) { delete m_vScrollSliderImage[2]; m_vScrollSliderImage[2] = nullptr; } else { QImage *img = new QImage(*pushImage); if (!img->isNull()) { if (nullptr != m_vScrollSliderImage[2]) delete m_vScrollSliderImage[2]; m_vScrollSliderImage[2] = img; if (nullptr == pushStretch || pushStretch->left < 0 || pushStretch->top < 0 || pushStretch->right > m_vScrollSliderImage[2]->width() || pushStretch->bottom > m_vScrollSliderImage[2]->height() || pushStretch->left >= pushStretch->right || pushStretch->top >= hotStretch->bottom) { m_vScrollSliderImageStretch[2].setX(0); m_vScrollSliderImageStretch[2].setY(0); m_vScrollSliderImageStretch[2].setWidth(m_vScrollSliderImage[2]->width()); m_vScrollSliderImageStretch[2].setHeight(m_vScrollSliderImage[2]->height()); } else { m_vScrollSliderImageStretch[2].setX(pushStretch->left); m_vScrollSliderImageStretch[2].setY(pushStretch->top); m_vScrollSliderImageStretch[2].setWidth(pushStretch->right - pushStretch->left); m_vScrollSliderImageStretch[2].setHeight(pushStretch->bottom - pushStretch->top); } } else { delete img; } } return HGBASE_ERR_OK; } HGResult HGImgView::setNullScrollImage(const QImage *image) { if (MouseStatus_Null != m_mousePressStatus || 0 != m_operate || m_draging) { return HGBASE_ERR_FAIL; } if (nullptr != m_image) { return HGBASE_ERR_FAIL; } if (nullptr == image) { delete m_nullScrollImage; m_nullScrollImage = nullptr; } else { QImage *img = new QImage(*image); if (!img->isNull()) { if (nullptr != m_nullScrollImage) delete m_nullScrollImage; m_nullScrollImage = img; } else { delete img; } } return HGBASE_ERR_OK; } HGResult HGImgView::enableScroll(bool enable) { if (MouseStatus_Null != m_mousePressStatus || 0 != m_operate || m_draging) { return HGBASE_ERR_FAIL; } if (nullptr != m_image) { return HGBASE_ERR_FAIL; } m_enableScroll = enable; return HGBASE_ERR_OK; } HGResult HGImgView::addImage(HGImage image) { HGImage img = nullptr; HGResult ret = HGBase_CloneImage(image, 0, HGBASE_IMGORIGIN_TOP, &img); if (ret != HGBASE_ERR_OK) { return ret; } reset(); if (nullptr != m_image) { delete m_qImage; HGBase_DestroyImage(m_image); } m_image = img; m_qImage = createQImage(); HGImageInfo info; HGBase_GetImageInfo(m_image, &info); HGRect rcWnd = {0, 0, this->width(), this->height()}; m_hScroll = false; m_vScroll = false; GetMinShowImageRect(&rcWnd, info.width, info.height, m_showImage, m_showRect); double scale = (double)(m_showRect.right - m_showRect.left) / (double)info.width; updateMoveStatusAndCursor(); Show(); emit scaleChanged(scale); return HGBASE_ERR_OK; } HGResult HGImgView::clearImage() { reset(); if (nullptr != m_image) { delete m_qImage; m_qImage = nullptr; HGBase_DestroyImage(m_image); m_image = nullptr; } m_hScroll = false; m_vScroll = false; m_showImage = false; memset(&m_showRect, 0, sizeof(HGRectF)); double scale = 0.0; updateMoveStatusAndCursor(); Show(); emit scaleChanged(scale); return HGBASE_ERR_OK; } HGResult HGImgView::getImage(HGImage *image) { if (nullptr == image) { return HGBASE_ERR_INVALIDARG; } *image = m_image; return HGBASE_ERR_OK; } HGResult HGImgView::rotateLeft() { if (MouseStatus_Null != m_mousePressStatus || 0 != m_operate || m_draging || nullptr == m_image) { return HGBASE_ERR_FAIL; } HGImageInfo info; HGBase_GetImageInfo(m_image, &info); HGImage img = nullptr; HGResult ret = HGBase_CreateImage(info.height, info.width, info.type, HGBASE_IMGORIGIN_TOP, &img); if (ret != HGBASE_ERR_OK) { return ret; } HGUInt xDpi, yDpi; HGBase_GetImageDpi(m_image, &xDpi, &yDpi); HGBase_SetImageDpi(img, xDpi, yDpi); HGBase_ImageRotateLeft(m_image, img); delete m_qImage; HGBase_DestroyImage(m_image); m_image = img; m_qImage = createQImage(); HGBase_GetImageInfo(m_image, &info); HGRect rcWnd = {0, 0, this->width(), this->height()}; m_hScroll = false; m_vScroll = false; GetMinShowImageRect(&rcWnd, info.width, info.height, m_showImage, m_showRect); double scale = (double)(m_showRect.right - m_showRect.left) / (double)info.width; updateMoveStatusAndCursor(); Show(); emit scaleChanged(scale); return HGBASE_ERR_OK; } HGResult HGImgView::rotateRight() { if (MouseStatus_Null != m_mousePressStatus || 0 != m_operate || m_draging || nullptr == m_image) { return HGBASE_ERR_FAIL; } HGImageInfo info; HGBase_GetImageInfo(m_image, &info); HGImage img = nullptr; HGResult ret = HGBase_CreateImage(info.height, info.width, info.type, HGBASE_IMGORIGIN_TOP, &img); if (ret != HGBASE_ERR_OK) { return ret; } HGUInt xDpi, yDpi; HGBase_GetImageDpi(m_image, &xDpi, &yDpi); HGBase_SetImageDpi(img, xDpi, yDpi); HGBase_ImageRotateRight(m_image, img); delete m_qImage; HGBase_DestroyImage(m_image); m_image = img; m_qImage = createQImage(); HGBase_GetImageInfo(m_image, &info); HGRect rcWnd = {0, 0, this->width(), this->height()}; m_hScroll = false; m_vScroll = false; GetMinShowImageRect(&rcWnd, info.width, info.height, m_showImage, m_showRect); double scale = (double)(m_showRect.right - m_showRect.left) / (double)info.width; updateMoveStatusAndCursor(); Show(); emit scaleChanged(scale); return HGBASE_ERR_OK; } HGResult HGImgView::rotate180() { if (MouseStatus_Null != m_mousePressStatus || 0 != m_operate || m_draging || nullptr == m_image) { return HGBASE_ERR_FAIL; } HGBase_ImageRotate180(m_image, m_image); delete m_qImage; m_qImage = createQImage(); Show(); return HGBASE_ERR_OK; } HGResult HGImgView::zoomIn(const HGPoint *pCenter) { if (MouseStatus_Null != m_mousePressStatus || 0 != m_operate || m_draging || nullptr == m_image) { return HGBASE_ERR_FAIL; } HGImageInfo info; HGBase_GetImageInfo(m_image, &info); // 获取最大值 float fMaxWidth = info.width * 20.0f; float fMaxHeight = info.height * 20.0f; // 获取当前值 float fCurWidth = m_showRect.right - m_showRect.left; float fCurHeight = m_showRect.bottom - m_showRect.top; // 新值 float fNewWidth = fCurWidth * 1.2f; float fNewHeight = fCurHeight * 1.2f; // support 1% zoom-in when 'ctrl' pressed (added on 2022-05-24) if(QApplication::queryKeyboardModifiers() == Qt::CTRL) { // 1% fNewWidth = info.width * .01f; if(fNewWidth < 1.0f) fNewWidth = 1.0f; fNewWidth += fCurWidth; fNewHeight = info.height * .01f; if(fNewHeight < 1.0f) fNewHeight = 1.0f; fNewHeight += fCurHeight; } if (fabs(fMaxWidth - fCurWidth) > 1e-3 || fabs(fMaxHeight - fCurHeight) > 1e-3) { if (fNewWidth - fMaxWidth > -1e-3 || fNewHeight - fMaxHeight > -1e-3 || round(100.0f * fNewWidth / (float)info.width) == round(100.0f * fMaxWidth / (float)info.width)) { fNewWidth = fMaxWidth; fNewHeight = fMaxHeight; } m_hScroll = false; m_vScroll = false; if (m_enableScroll) { if (fNewWidth > this->width()) { m_hScroll = true; if (fNewHeight > this->height() - m_scrollSize) m_vScroll = true; } else { if (fNewHeight > this->height()) { m_vScroll = true; if (fNewWidth > this->width() - m_scrollSize) m_hScroll = true; } } } ResizeShowImageRect(m_showImage, m_showRect, fNewWidth, fNewHeight, pCenter); recalcShowRect(this->width(), this->height(), m_scrollSize, m_hScroll, m_vScroll, m_showImage, m_showRect); double scale = (double)(m_showRect.right - m_showRect.left) / (double)info.width; updateMoveStatusAndCursor(); Show(); emit scaleChanged(scale); } return HGBASE_ERR_OK; } HGResult HGImgView::zoomOut(const HGPoint *pCenter) { if (MouseStatus_Null != m_mousePressStatus || 0 != m_operate || m_draging || nullptr == m_image) { return HGBASE_ERR_FAIL; } HGImageInfo info; HGBase_GetImageInfo(m_image, &info); HGRect rcWnd = {0, 0, this->width(), this->height()}; // 获取最小值 bool bMinShow; HGRectF rcMinShow; GetMinShowImageRect(&rcWnd, info.width, info.height, bMinShow, rcMinShow); float fMinWidth = rcMinShow.right - rcMinShow.left; float fMinHeight = rcMinShow.bottom - rcMinShow.top; // 获取当前值 float fCurWidth = m_showRect.right - m_showRect.left; float fCurHeight = m_showRect.bottom - m_showRect.top; // 新值 float fNewWidth = fCurWidth * 1.0f / 1.2f; float fNewHeight = fCurHeight * 1.0f / 1.2f; // support 1% zoom-out when 'ctrl' pressed (added on 2022-05-24) if(QApplication::queryKeyboardModifiers() == Qt::CTRL) { // 1% fNewWidth = info.width * .01f; if(fNewWidth < 1.0f) fNewWidth = 1.0f; fNewWidth = fCurWidth - fNewWidth; fNewHeight = info.height * .01f; if(fNewHeight < 1.0f) fNewHeight = 1.0f; fNewHeight = fCurHeight - fNewHeight; } if (fabs(fMinWidth - fCurWidth) > 1e-3 || fabs(fMinHeight - fCurHeight) > 1e-3) { if (fNewWidth - fMinWidth < 1e-3 || fNewHeight - fMinHeight < 1e-3 || round(100.0f * fNewWidth / (float)info.width) == round(100.0f * fMinWidth / (float)info.width)) { fNewWidth = fMinWidth; fNewHeight = fMinHeight; } m_hScroll = false; m_vScroll = false; if (m_enableScroll) { if (fNewWidth > this->width()) { m_hScroll = true; if (fNewHeight > this->height() - m_scrollSize) m_vScroll = true; } else { if (fNewHeight > this->height()) { m_vScroll = true; if (fNewWidth > this->width() - m_scrollSize) m_hScroll = true; } } } ResizeShowImageRect(m_showImage, m_showRect, fNewWidth, fNewHeight, pCenter); recalcShowRect(this->width(), this->height(), m_scrollSize, m_hScroll, m_vScroll, m_showImage, m_showRect); double scale = (double)(m_showRect.right - m_showRect.left) / (double)info.width; updateMoveStatusAndCursor(); Show(); emit scaleChanged(scale); } return HGBASE_ERR_OK; } HGResult HGImgView::realSize() { if (MouseStatus_Null != m_mousePressStatus || 0 != m_operate || m_draging || nullptr == m_image) { return HGBASE_ERR_FAIL; } HGImageInfo info; HGBase_GetImageInfo(m_image, &info); m_hScroll = false; m_vScroll = false; if (m_enableScroll) { if ((int)info.width > this->width()) { m_hScroll = true; if ((int)info.height > this->height() - m_scrollSize) m_vScroll = true; } else { if ((int)info.height > this->height()) { m_vScroll = true; if ((int)info.width > this->width() - m_scrollSize) m_hScroll = true; } } } ResizeShowImageRect(m_showImage, m_showRect, (float)info.width, (float)info.height, nullptr); recalcShowRect(this->width(), this->height(), m_scrollSize, m_hScroll, m_vScroll, m_showImage, m_showRect); double scale = 1.0; updateMoveStatusAndCursor(); Show(); emit scaleChanged(scale); return HGBASE_ERR_OK; } HGResult HGImgView::fitWndSize() { if (MouseStatus_Null != m_mousePressStatus || 0 != m_operate || m_draging || nullptr == m_image) { return HGBASE_ERR_FAIL; } HGImageInfo info; HGBase_GetImageInfo(m_image, &info); HGRect rcWnd = {0, 0, this->width(), this->height()}; m_hScroll = false; m_vScroll = false; GetMinShowImageRect(&rcWnd, info.width, info.height, m_showImage, m_showRect); double scale = (double)(m_showRect.right - m_showRect.left) / (double)info.width; updateMoveStatusAndCursor(); Show(); emit scaleChanged(scale); return HGBASE_ERR_OK; } HGResult HGImgView::fitWndWidth() { if (MouseStatus_Null != m_mousePressStatus || 0 != m_operate || m_draging || nullptr == m_image) { return HGBASE_ERR_FAIL; } HGImageInfo info; HGBase_GetImageInfo(m_image, &info); HGRect rcWnd = {0, 0, this->width(), this->height()}; // 获取最小值 bool bMinShow; HGRectF rcMinShow; GetMinShowImageRect(&rcWnd, info.width, info.height, bMinShow, rcMinShow); float fMinWidth = rcMinShow.right - rcMinShow.left; float fMinHeight = rcMinShow.bottom - rcMinShow.top; // 获取最大值 float fMaxWidth = info.width * 20.0f; float fMaxHeight = info.height * 20.0f; // 新值 float fNewWidth = (float)this->width(); float fNewHeight = fNewWidth * (float)info.height / (float)info.width; fNewWidth = HGMIN(fMaxWidth, HGMAX(fMinWidth, fNewWidth)); fNewHeight = HGMIN(fMaxHeight, HGMAX(fMinHeight, fNewHeight)); m_hScroll = false; m_vScroll = false; if (m_enableScroll) { if (fNewHeight > this->height()) { m_vScroll = true; fNewWidth = this->width() - m_scrollSize; fNewHeight = fNewWidth * (float)info.height / (float)info.width; fNewWidth = HGMIN(fMaxWidth, HGMAX(fMinWidth, fNewWidth)); fNewHeight = HGMIN(fMaxHeight, HGMAX(fMinHeight, fNewHeight)); if (fNewHeight <= this->height()) { m_vScroll = false; fNewWidth = (float)this->width(); fNewHeight = fNewWidth * (float)info.height / (float)info.width; fNewWidth = HGMIN(fMaxWidth, HGMAX(fMinWidth, fNewWidth)); fNewHeight = HGMIN(fMaxHeight, HGMAX(fMinHeight, fNewHeight)); } } } ResizeShowImageRect(m_showImage, m_showRect, fNewWidth, fNewHeight, nullptr); recalcShowRect(this->width(), this->height(), m_scrollSize, m_hScroll, m_vScroll, m_showImage, m_showRect); double scale = (double)(m_showRect.right - m_showRect.left) / (double)info.width; updateMoveStatusAndCursor(); Show(); emit scaleChanged(scale); return HGBASE_ERR_OK; } HGResult HGImgView::showColorInfo(bool show) { if (MouseStatus_Null != m_mousePressStatus || 0 != m_operate || m_draging) { return HGBASE_ERR_FAIL; } m_showColorInfo = show; QToolTip::hideText(); updateMoveStatusAndCursor(); return HGBASE_ERR_OK; } void HGImgView::mousePressEvent(QMouseEvent* e) { if (e->button() != Qt::LeftButton) { return; } MouseStatus mouseStatus = getMouseStatus(e->pos()); if (MouseStatus_Null != mouseStatus) { // 如果鼠标点到滚动条区域 m_mousePressStatus = mouseStatus; m_mousePressBeginX = e->pos().x(); m_mousePressBeginY = e->pos().y(); updateMoveStatusAndCursor(); Show(); return; } assert(0 == m_operate); if (nullptr == m_image || !m_showImage) { return; } m_operate = 1; m_beginX = e->pos().x(); m_beginY = e->pos().y(); updateMoveStatusAndCursor(); } void HGImgView::mouseMoveEvent(QMouseEvent* e) { // 处理滚动条 if (MouseStatus_HScrollSlider == m_mousePressStatus) { int lXAmount = e->pos().x() - m_mousePressBeginX; double offset; if (!m_vScroll) { int rollTotalLen = this->width() - 2 * m_scrollSize; int rollLeft = round((double)rollTotalLen * (-m_showRect.left) / (double)(m_showRect.right - m_showRect.left)); int rollRight = round((double)rollTotalLen * (-m_showRect.left + this->width()) / (double)(m_showRect.right - m_showRect.left)); int rollLen = rollRight - rollLeft; if (rollLen >= m_minScrollSliderSize) offset = lXAmount * (m_showRect.right - m_showRect.left) / rollTotalLen; else offset = lXAmount * (m_showRect.right - m_showRect.left - this->width()) / (rollTotalLen - m_minScrollSliderSize); } else { int rollTotalLen = this->width() - 3 * m_scrollSize; int rollLeft = round((double)rollTotalLen * (-m_showRect.left) / (double)(m_showRect.right - m_showRect.left)); int rollRight = round((double)rollTotalLen * (-m_showRect.left + this->width() - m_scrollSize) / (double)(m_showRect.right - m_showRect.left)); int rollLen = rollRight - rollLeft; if (rollLen >= m_minScrollSliderSize) offset = lXAmount * (m_showRect.right - m_showRect.left) / rollTotalLen; else offset = lXAmount * (m_showRect.right - m_showRect.left - this->width() + m_scrollSize) / (rollTotalLen - m_minScrollSliderSize); } m_showRect.left -= offset; m_showRect.right -= offset; recalcShowRect(this->width(), this->height(), m_scrollSize, m_hScroll, m_vScroll, m_showImage, m_showRect); Show(); m_mousePressBeginX = e->pos().x(); m_mousePressBeginY = e->pos().y(); } else if (MouseStatus_VScrollSlider == m_mousePressStatus) { int lYAmount = e->pos().y() - m_mousePressBeginY; double offset; if (!m_hScroll) { int rollTotalLen = this->height() - 2 * m_scrollSize; int rollTop = round((double)rollTotalLen * (-m_showRect.top) / (double)(m_showRect.bottom - m_showRect.top)); int rollBottom = round((double)rollTotalLen * (-m_showRect.top + this->height()) / (double)(m_showRect.bottom - m_showRect.top)); int rollLen = rollBottom - rollTop; if (rollLen >= m_minScrollSliderSize) offset = lYAmount * (m_showRect.bottom - m_showRect.top) / rollTotalLen; else offset = lYAmount * (m_showRect.bottom - m_showRect.top - this->height()) / (rollTotalLen - m_minScrollSliderSize); } else { int rollTotalLen = this->height() - 3 * m_scrollSize; int rollTop = round((double)rollTotalLen * (-m_showRect.top) / (double)(m_showRect.bottom - m_showRect.top)); int rollBottom = round((double)rollTotalLen * (-m_showRect.top + this->height() - m_scrollSize) / (double)(m_showRect.bottom - m_showRect.top)); int rollLen = rollBottom - rollTop; if (rollLen >= m_minScrollSliderSize) offset = lYAmount * (m_showRect.bottom - m_showRect.top) / rollTotalLen; else offset = lYAmount * (m_showRect.bottom - m_showRect.top - this->height() + m_scrollSize) / (rollTotalLen - m_minScrollSliderSize); } m_showRect.top -= offset; m_showRect.bottom -= offset; recalcShowRect(this->width(), this->height(), m_scrollSize, m_hScroll, m_vScroll, m_showImage, m_showRect); Show(); m_mousePressBeginX = e->pos().x(); m_mousePressBeginY = e->pos().y(); } if (1 == m_operate) { assert(nullptr != m_image && m_showImage); int lXAmount = e->pos().x() - m_beginX; int lYAmount = e->pos().y() - m_beginY; m_showRect.left += lXAmount; m_showRect.right += lXAmount; m_showRect.top += lYAmount; m_showRect.bottom += lYAmount; recalcShowRect(this->width(), this->height(), m_scrollSize, m_hScroll, m_vScroll, m_showImage, m_showRect); Show(); m_beginX = e->pos().x(); m_beginY = e->pos().y(); } else { assert(0 == m_operate); MouseStatus mouseStatus = getMouseStatus(e->pos()); if (nullptr != m_image && m_showImage && !m_draging && MouseStatus_Null == m_mousePressStatus && MouseStatus_Null == mouseStatus) { HGImageInfo info; HGBase_GetImageInfo(m_image, &info); int xShow = e->pos().x() - m_showRect.left; int yShow = e->pos().y() - m_showRect.top; int xImg = xShow * (int)info.width / (m_showRect.right - m_showRect.left); int yImg = yShow * (int)info.height / (m_showRect.bottom - m_showRect.top); if (xImg >= 0 && xImg < (int)info.width && yImg >= 0 && yImg < (int)info.height) { //qDebug("mousePos, x=%d, y=%d", xImg, yImg); emit mousePos(xImg, yImg); if (m_showColorInfo) { HGByte *data = nullptr; HGBase_GetImageData(m_image, &data); HGByte *pixel = data + yImg * info.widthStep + xImg * 3; HGByte r = pixel[0]; HGByte g = pixel[1]; HGByte b = pixel[2]; //qDebug("colorInfo, r=%d, g=%d, b=%d", r, g, b); char colorInfo[1024]; sprintf(colorInfo, "X: %d, Y: %d\nRGB(%d, %d, %d)\nHTML(#%02X%02X%02X)", xImg, yImg, r, g, b, r, g, b); QToolTip::showText(e->globalPos(), colorInfo, this); } } else { emit mousePos(0, 0); QToolTip::hideText(); } } else { emit mousePos(0, 0); QToolTip::hideText(); } updateMoveStatusAndCursor(); Show(); } } void HGImgView::mouseReleaseEvent(QMouseEvent* e) { Q_UNUSED(e); m_draging = false; m_beginX = -1; m_beginY = -1; m_operate = 0; MouseStatus oldMousePressStatus = m_mousePressStatus; m_mousePressStatus = MouseStatus_Null; m_mousePressBeginX = -1; m_mousePressBeginY = -1; // 处理按滚动条区域 MouseStatus mouseMoveStatus = getMouseStatus(e->pos()); if (MouseStatus_HScrollLeft == oldMousePressStatus && MouseStatus_HScrollLeft == mouseMoveStatus) { double width = m_showRect.right - m_showRect.left; m_showRect.left += width / 10.0; m_showRect.right += width / 10.0; recalcShowRect(this->width(), this->height(), m_scrollSize, m_hScroll, m_vScroll, m_showImage, m_showRect); } else if (MouseStatus_HScrollRight == oldMousePressStatus && MouseStatus_HScrollRight == mouseMoveStatus) { double width = m_showRect.right - m_showRect.left; m_showRect.left -= width / 10.0; m_showRect.right -= width / 10.0; recalcShowRect(this->width(), this->height(), m_scrollSize, m_hScroll, m_vScroll, m_showImage, m_showRect); } else if (MouseStatus_VScrollTop == oldMousePressStatus && MouseStatus_VScrollTop == mouseMoveStatus) { double height = m_showRect.bottom - m_showRect.top; m_showRect.top += height / 10.0; m_showRect.bottom += height / 10.0; recalcShowRect(this->width(), this->height(), m_scrollSize, m_hScroll, m_vScroll, m_showImage, m_showRect); } else if (MouseStatus_VScrollBottom == oldMousePressStatus && MouseStatus_VScrollBottom == mouseMoveStatus) { double height = m_showRect.bottom - m_showRect.top; m_showRect.top -= height / 10.0; m_showRect.bottom -= height / 10.0; recalcShowRect(this->width(), this->height(), m_scrollSize, m_hScroll, m_vScroll, m_showImage, m_showRect); } updateMoveStatusAndCursor(); Show(); } void HGImgView::enterEvent(QEvent *e) { Q_UNUSED(e); m_mouseOn = true; } void HGImgView::leaveEvent(QEvent *e) { Q_UNUSED(e); // 拖动状态不能重置 //m_draging = false; m_operate = 0; m_beginX = -1; m_beginY = -1; m_mouseOn = false; m_mouseMoveStatus = MouseStatus_Null; Show(); emit mousePos(0, 0); } void HGImgView::paintEvent(QPaintEvent* e) { Q_UNUSED(e); QPainter painter(this); painter.fillRect(0, 0, this->width(), this->height(), qRgb(250, 250, 250)); if (nullptr == m_qImage || !m_showImage) { return; } #if 0 HGRect rcWnd = {0, 0, this->width(), this->height()}; int nImgWidth = info.width; int nImgHeight = info.height; // 绘制Image int xDest, yDest, wDest, hDest; int xSrc, ySrc, wSrc, hSrc; HGRect rcShowImage; // 绘制的图像区域 rcShowImage.left = round(m_showRect.left); rcShowImage.top = round(m_showRect.top); rcShowImage.right = round(m_showRect.right); rcShowImage.bottom = round(m_showRect.bottom); int nShowWidth = rcShowImage.right - rcShowImage.left; int nShowHeight = rcShowImage.bottom - rcShowImage.top; if (rcShowImage.left <= rcWnd.left) { float fPixel = (float)nImgWidth * (rcWnd.left - rcShowImage.left) / nShowWidth; float fPixelR = (float)nImgWidth * (rcShowImage.right - rcWnd.right) / nShowWidth; xDest = rcShowImage.left + round((int)fPixel * (float)nShowWidth / nImgWidth); int xDestR = rcShowImage.right - round((int)fPixelR * (float)nShowWidth / nImgWidth); wDest = xDestR - xDest; wSrc = round((float)nImgWidth * wDest / nShowWidth); xSrc = round((float)(xDest - rcShowImage.left) * nImgWidth / nShowWidth); } else { xDest = rcShowImage.left; wDest = nShowWidth; xSrc = 0; wSrc = nImgWidth; } if (rcShowImage.top <= rcWnd.top) { float fPixel = (float)nImgHeight * (rcWnd.top - rcShowImage.top) / nShowHeight; float fPixelB = (float)nImgHeight * (rcShowImage.bottom - rcWnd.bottom) / nShowHeight; yDest = rcShowImage.top + round((int)fPixel * (float)nShowHeight / nImgHeight); int yDestB = rcShowImage.bottom - round((int)fPixelB * (float)nShowHeight / nImgHeight); hDest = yDestB - yDest; hSrc = round((float)nImgHeight * hDest / nShowHeight); ySrc = round((float)(yDest - rcShowImage.top) * nImgHeight / nShowHeight); } else { yDest = rcShowImage.top; hDest = nShowHeight; ySrc = 0; hSrc = nImgHeight; } QRect srcRect(xSrc, ySrc, wSrc, hSrc); QRect destRect(xDest, yDest, wDest, hDest); painter.setRenderHint(QPainter::SmoothPixmapTransform); painter.drawImage(destRect, *m_qImage, srcRect); #else QRectF destRect(m_showRect.left, m_showRect.top, m_showRect.right - m_showRect.left, m_showRect.bottom - m_showRect.top); painter.setRenderHint(QPainter::SmoothPixmapTransform); painter.drawImage(destRect, *m_qImage); #endif DrawScroll(painter); } void HGImgView::wheelEvent(QWheelEvent* e) { if (MouseStatus_Null != m_mousePressStatus || 0 != m_operate || m_draging) { return; } MouseStatus mouseStatus = getMouseStatus(e->pos()); if (MouseStatus_Null != mouseStatus) { if (MouseStatus_HScroll == mouseStatus || MouseStatus_HScrollSlider == mouseStatus || MouseStatus_HScrollLeft == mouseStatus || MouseStatus_HScrollRight == mouseStatus) { double width = m_showRect.right - m_showRect.left; if (e->delta() > 0) { m_showRect.left += width / 10.0; m_showRect.right += width / 10.0; } else { m_showRect.left -= width / 10.0; m_showRect.right -= width / 10.0; } recalcShowRect(this->width(), this->height(), m_scrollSize, m_hScroll, m_vScroll, m_showImage, m_showRect); } else if (MouseStatus_VScroll == mouseStatus || MouseStatus_VScrollSlider == mouseStatus || MouseStatus_VScrollTop == mouseStatus || MouseStatus_VScrollBottom == mouseStatus) { double height = m_showRect.bottom - m_showRect.top; if (e->delta() > 0) { m_showRect.top += height / 10.0; m_showRect.bottom += height / 10.0; } else { m_showRect.top -= height / 10.0; m_showRect.bottom -= height / 10.0; } recalcShowRect(this->width(), this->height(), m_scrollSize, m_hScroll, m_vScroll, m_showImage, m_showRect); } Show(); return; } HGPoint pt = {e->pos().x(), e->pos().y()}; if (e->delta() > 0) zoomIn(&pt); else zoomOut(&pt); } void HGImgView::resizeEvent(QResizeEvent* e) { Q_UNUSED(e); reset(); if (nullptr != m_image) { HGImageInfo info; HGBase_GetImageInfo(m_image, &info); HGRect rcWnd = {0, 0, this->width(), this->height()}; m_hScroll = false; m_vScroll = false; GetMinShowImageRect(&rcWnd, info.width, info.height, m_showImage, m_showRect); double scale = (double)(m_showRect.right - m_showRect.left) / (double)info.width; emit scaleChanged(scale); } updateMoveStatusAndCursor(); Show(); } void HGImgView::keyPressEvent(QKeyEvent *e) { Q_UNUSED(e); if (MouseStatus_Null != m_mousePressStatus || 0 != m_operate || m_draging) { return; } if (e->key() == Qt::Key_Escape) { emit escape(); } } void HGImgView::dragEnterEvent(QDragEnterEvent *e) { qDebug("dragEnterEvent"); if (e->mimeData()->hasUrls()) { m_draging = true; e->accept(); } } void HGImgView::dragMoveEvent(QDragMoveEvent *e) { //qDebug("dragMoveEvent"); Q_UNUSED(e); //Show(); } void HGImgView::dragLeaveEvent(QDragLeaveEvent *e) { qDebug("dragLeaveEvent"); Q_UNUSED(e); m_draging = false; //Show(); } void HGImgView::dropEvent(QDropEvent *e) { Q_UNUSED(e); QStringList fileNames; QList urls = e->mimeData()->urls(); for (QUrl url : urls) { fileNames.append(url.toLocalFile()); } m_draging = false; emit drop(e->source(), fileNames); } void HGImgView::mouseDoubleClickEvent(QMouseEvent* e) { if (MouseStatus_Null != m_mousePressStatus || 0 != m_operate || m_draging) { return; } MouseStatus mouseMoveStatus = getMouseStatus(e->pos()); if (MouseStatus_Null == mouseMoveStatus) { emit doubleClicked(); } else { QWidget::mouseDoubleClickEvent(e); } } void HGImgView::GetMinShowImageRect(const HGRect *pWnd, int nImgWidth, int nImgHeight, bool &bShowImage, HGRectF &rcShowImage) { bShowImage = false; memset(&rcShowImage, 0, sizeof(HGRectF)); assert(nullptr != pWnd); int nWndWidth = pWnd->right - pWnd->left; int nWndHeight = pWnd->bottom - pWnd->top; if (nWndWidth <= 0 || nWndHeight <= 0) { return; } if (nImgWidth <= 0 || nImgHeight <= 0) { return; } float fLeft, fTop, fRight, fBottom; if (nImgWidth > nWndWidth || nImgHeight > nWndHeight) { if (nImgWidth * nWndHeight < nImgHeight * nWndWidth) { float fWidth = (float)nWndHeight * nImgWidth / nImgHeight; fLeft = (pWnd->left + pWnd->right - fWidth) / 2.0f; fRight = (pWnd->left + pWnd->right + fWidth) / 2.0f; fTop = (float)pWnd->top; fBottom = (float)pWnd->bottom; } else { float fHeight = (float)nWndWidth * nImgHeight / nImgWidth; fLeft = (float)pWnd->left; fRight = (float)pWnd->right; fTop = (pWnd->top + pWnd->bottom - fHeight) / 2.0f; fBottom = (pWnd->top + pWnd->bottom + fHeight) / 2.0f; } } else { fLeft = (pWnd->left + pWnd->right - nImgWidth) / 2.0f; fRight = (pWnd->left + pWnd->right + nImgWidth) / 2.0f; fTop = (pWnd->top + pWnd->bottom - nImgHeight) / 2.0f; fBottom = (pWnd->top + pWnd->bottom + nImgHeight) / 2.0f; } if (fRight - fLeft < 1e-3 || fBottom - fTop < 1e-3) { return; } bShowImage = true; rcShowImage.left = fLeft; rcShowImage.top = fTop; rcShowImage.right = fRight; rcShowImage.bottom = fBottom; } void HGImgView::ResizeShowImageRect(bool &bShowImage, HGRectF &rcShowImage, float fNewWidth, float fNewHeight, const HGPoint *pCenter) { if (!bShowImage) { return; } if (fNewWidth <= 0 || fNewHeight <= 0) { bShowImage = false; memset(&rcShowImage, 0, sizeof(HGRectF)); return; } float fCurWidth = rcShowImage.right - rcShowImage.left; float fCurHeight = rcShowImage.bottom - rcShowImage.top; if (nullptr != pCenter) { rcShowImage.left = (float)pCenter->x - ((float)pCenter->x - rcShowImage.left) * fNewWidth / fCurWidth; rcShowImage.top = (float)pCenter->y - ((float)pCenter->y - rcShowImage.top) * fNewHeight / fCurHeight; } else { rcShowImage.left = rcShowImage.left - (fNewWidth - fCurWidth) / 2.0f; rcShowImage.top = rcShowImage.top - (fNewHeight - fCurHeight) / 2.0f; } rcShowImage.right = rcShowImage.left + fNewWidth; rcShowImage.bottom = rcShowImage.top + fNewHeight; if (rcShowImage.right - rcShowImage.left < 1e-3 || rcShowImage.bottom - rcShowImage.top < 1e-3) { bShowImage = false; memset(&rcShowImage, 0, sizeof(HGRectF)); } } void HGImgView::recalcShowRect(int wndWidth, int wndHeight, int scrollSize, bool hScroll, bool vScroll, bool showImage, HGRectF &imageRect) { if (!showImage) return; double width = imageRect.right - imageRect.left; double height = imageRect.bottom - imageRect.top; int vScorllSize = vScroll ? scrollSize : 0; if (width <= (double)(wndWidth - vScorllSize)) { imageRect.left = (wndWidth - vScorllSize - width) / 2.0; imageRect.right = imageRect.left + width; } else { if (imageRect.right < (double)(wndWidth - vScorllSize)) { imageRect.right = wndWidth - vScorllSize; imageRect.left = imageRect.right - width; } if (imageRect.left > 0) { imageRect.left = 0; imageRect.right = imageRect.left + width; } } int hScorllSize = hScroll ? scrollSize : 0; if (height <= (double)(wndHeight - hScorllSize)) { imageRect.top = (wndHeight - hScorllSize - height) / 2.0; imageRect.bottom = imageRect.top + height; } else { if (imageRect.bottom < (double)(wndHeight - hScorllSize)) { imageRect.bottom = wndHeight - hScorllSize; imageRect.top = imageRect.bottom - height; } if (imageRect.top > 0) { imageRect.top = 0; imageRect.bottom = imageRect.top + height; } } } void HGImgView::DrawImage(QPainter &painter, const QRect &destRect, const QImage *image, const QRect &stretchRect) { if (destRect.width() < image->width() - stretchRect.width() || destRect.height() < image->height() - stretchRect.height()) { painter.drawImage(destRect, *image); return; } for (int i = 0; i < 9; ++i) { int DestX = destRect.left(), DestY = destRect.top(), DestWidth = destRect.width(), DestHeight = destRect.height(); int SrcX = 0, SrcY = 0, SrcWidth = image->width(), SrcHeight = image->height(); if (0 == i % 3) { DestX = destRect.left(); DestWidth = stretchRect.left(); SrcX = 0; SrcWidth = stretchRect.left(); } else if (1 == i % 3) { DestX = destRect.left() + stretchRect.left(); DestWidth = destRect.width() + stretchRect.width() - image->width(); SrcX = stretchRect.left(); SrcWidth = stretchRect.width(); } else if (2 == i % 3) { DestX = destRect.left() + destRect.width() + stretchRect.left() + stretchRect.width() - image->width(); DestWidth = image->width() - (stretchRect.left() + stretchRect.width()); SrcX = stretchRect.left() + stretchRect.width(); SrcWidth = image->width() - (stretchRect.left() + stretchRect.width()); } if (0 == i / 3) { DestY = destRect.top(); DestHeight = stretchRect.top(); SrcY = 0; SrcHeight = stretchRect.top(); } else if (1 == i / 3) { DestY = destRect.top() + stretchRect.top(); DestHeight = destRect.height() + stretchRect.height() - image->height(); SrcY = stretchRect.top(); SrcHeight = stretchRect.height(); } else if (2 == i / 3) { DestY = destRect.top() + destRect.height() + stretchRect.top() + stretchRect.height() - image->height(); DestHeight = image->height() - (stretchRect.top() + stretchRect.height()); SrcY = stretchRect.top() + stretchRect.height(); SrcHeight = image->height() - (stretchRect.top() + stretchRect.height()); } QRect dest(DestX, DestY, DestWidth, DestHeight); QRect src(SrcX, SrcY, SrcWidth, SrcHeight); painter.drawImage(dest, *image, src); } } QImage* HGImgView::createQImage() { if (nullptr == m_image) return nullptr; HGImageInfo info; HGBase_GetImageInfo(m_image, &info); QImage::Format fmt = QImage::Format_Invalid; if (info.type == HGBASE_IMGTYPE_BINARY) fmt = QImage::Format_Mono; else if (info.type == HGBASE_IMGTYPE_GRAY) fmt = QImage::Format_Grayscale8; else if (info.type == HGBASE_IMGTYPE_RGB) fmt = QImage::Format_RGB888; else if (info.type == HGBASE_IMGTYPE_RGBA) fmt = QImage::Format_RGBA8888; if (QImage::Format_Invalid == fmt) { return nullptr; } HGByte* data = nullptr; HGBase_GetImageData(m_image, &data); QImage* img = new QImage(data, info.width, info.height, info.widthStep, fmt); if (img->isNull()) { delete img; return nullptr; } if (img->format() != QImage::Format_Mono) { return img; } QImage *newImg = new QImage(img->convertToFormat(QImage::Format_Grayscale8)); delete img; return newImg; } void HGImgView::DrawScroll(QPainter &painter) { // 滚动条背景 QColor clrHScroll = qRgb(220, 220, 220); QImage *imgHScroll = m_hScrollImage; QColor clrVScroll = qRgb(220, 220, 220); QImage *imgVScroll = m_vScrollImage; QColor clrNullScroll = qRgb(255, 255, 255); QImage *nullScrollImage = m_nullScrollImage; // 水平滑块 QColor clrHScrollSlider = qRgb(180, 180, 180); QImage *imgHScrollSlider = m_hScrollSliderImage[0]; QRect hScrollSliderStretch = m_hScrollSliderImageStretch[0]; if (MouseStatus_HScrollSlider == m_mousePressStatus) { clrHScrollSlider = qRgb(100, 100, 100); imgHScrollSlider = m_hScrollSliderImage[2]; hScrollSliderStretch = m_hScrollSliderImageStretch[2]; } else if (MouseStatus_HScrollLeft == m_mousePressStatus || MouseStatus_HScrollRight == m_mousePressStatus || MouseStatus_HScroll == m_mousePressStatus || MouseStatus_HScrollSlider == m_mouseMoveStatus || MouseStatus_HScroll == m_mouseMoveStatus || MouseStatus_HScrollLeft == m_mouseMoveStatus || MouseStatus_HScrollRight == m_mouseMoveStatus) { clrHScrollSlider = qRgb(140, 140, 140); imgHScrollSlider = m_hScrollSliderImage[1]; hScrollSliderStretch = m_hScrollSliderImageStretch[1]; } // 垂直滑块 QColor clrVScrollSlider = qRgb(180, 180, 180); QImage *imgVScrollSlider = m_vScrollSliderImage[0]; QRect vScrollSliderStretch = m_vScrollSliderImageStretch[0]; if (MouseStatus_VScrollSlider == m_mousePressStatus) { clrVScrollSlider = qRgb(100, 100, 100); imgVScrollSlider = m_vScrollSliderImage[2]; vScrollSliderStretch = m_vScrollSliderImageStretch[2]; } else if (MouseStatus_VScrollTop == m_mousePressStatus || MouseStatus_VScrollBottom == m_mousePressStatus || MouseStatus_VScroll == m_mousePressStatus || MouseStatus_VScrollSlider == m_mouseMoveStatus || MouseStatus_VScroll == m_mouseMoveStatus || MouseStatus_VScrollTop == m_mouseMoveStatus || MouseStatus_VScrollBottom == m_mouseMoveStatus) { clrVScrollSlider = qRgb(140, 140, 140); imgVScrollSlider = m_vScrollSliderImage[1]; vScrollSliderStretch = m_vScrollSliderImageStretch[1]; } // 水平左键 QColor clrHScrollLeft = qRgb(220, 220, 220); QImage *imgHScrollLeft = m_hScrollLeftImage[0]; if (MouseStatus_HScrollLeft == m_mousePressStatus) { clrHScrollLeft = qRgb(140, 140, 140); imgHScrollLeft = m_hScrollLeftImage[2]; } else if (MouseStatus_HScrollRight == m_mousePressStatus || MouseStatus_HScrollSlider == m_mousePressStatus || MouseStatus_HScroll == m_mousePressStatus || MouseStatus_HScrollSlider == m_mouseMoveStatus || MouseStatus_HScroll == m_mouseMoveStatus || MouseStatus_HScrollLeft == m_mouseMoveStatus || MouseStatus_HScrollRight == m_mouseMoveStatus) { clrHScrollLeft = qRgb(180, 180, 180); imgHScrollLeft = m_hScrollLeftImage[1]; } // 水平右键 QColor clrHScrollRight = qRgb(220, 220, 220); QImage *imgHScrollRight = m_hScrollRightImage[0]; if (MouseStatus_HScrollRight == m_mousePressStatus) { clrHScrollRight = qRgb(140, 140, 140); imgHScrollRight = m_hScrollRightImage[2]; } else if (MouseStatus_HScrollLeft == m_mousePressStatus || MouseStatus_HScrollSlider == m_mousePressStatus || MouseStatus_HScroll == m_mousePressStatus || MouseStatus_HScrollSlider == m_mouseMoveStatus || MouseStatus_HScroll == m_mouseMoveStatus || MouseStatus_HScrollLeft == m_mouseMoveStatus || MouseStatus_HScrollRight == m_mouseMoveStatus) { clrHScrollRight = qRgb(180, 180, 180); imgHScrollRight = m_hScrollRightImage[1]; } // 垂直上键 QColor clrVScrollTop = qRgb(220, 220, 220); QImage *imgVScrollTop = m_vScrollTopImage[0]; if (MouseStatus_VScrollTop == m_mousePressStatus) { clrVScrollTop = qRgb(140, 140, 140); imgVScrollTop = m_vScrollTopImage[2]; } else if (MouseStatus_VScrollSlider == m_mousePressStatus || MouseStatus_VScrollBottom == m_mousePressStatus || MouseStatus_VScroll == m_mousePressStatus || MouseStatus_VScrollSlider == m_mouseMoveStatus || MouseStatus_VScroll == m_mouseMoveStatus || MouseStatus_VScrollTop == m_mouseMoveStatus || MouseStatus_VScrollBottom == m_mouseMoveStatus) { clrVScrollTop = qRgb(180, 180, 180); imgVScrollTop = m_vScrollTopImage[1]; } // 垂直下键 QColor clrVScrollBottom = qRgb(220, 220, 220); QImage *imgVScrollBottom = m_vScrollBottomImage[0]; if (MouseStatus_VScrollBottom == m_mousePressStatus) { clrVScrollBottom = qRgb(140, 140, 140); imgVScrollBottom = m_vScrollBottomImage[2]; } else if (MouseStatus_VScrollSlider == m_mousePressStatus || MouseStatus_VScrollTop == m_mousePressStatus || MouseStatus_VScroll == m_mousePressStatus || MouseStatus_VScrollSlider == m_mouseMoveStatus || MouseStatus_VScroll == m_mouseMoveStatus || MouseStatus_VScrollTop == m_mouseMoveStatus || MouseStatus_VScrollBottom == m_mouseMoveStatus) { clrVScrollBottom = qRgb(180, 180, 180); imgVScrollBottom = m_vScrollBottomImage[1]; } if (m_hScroll) { if (nullptr == imgHScroll) painter.fillRect(getHScrollPos(), clrHScroll); else DrawImage(painter, getHScrollPos(), imgHScroll, m_hScrollImageStretch); if (nullptr == imgHScrollLeft) painter.fillRect(getHScrollLeftPos(), clrHScrollLeft); else painter.drawImage(getHScrollLeftPos(), *imgHScrollLeft); if (nullptr == imgHScrollRight) painter.fillRect(getHScrollRightPos(), clrHScrollRight); else painter.drawImage(getHScrollRightPos(), *imgHScrollRight); if (nullptr == imgHScrollSlider) painter.fillRect(getHScrollSliderPos(), clrHScrollSlider); else DrawImage(painter, getHScrollSliderPos(), imgHScrollSlider, hScrollSliderStretch); } if (m_vScroll) { if (nullptr == imgVScroll) painter.fillRect(getVScrollPos(), clrVScroll); else DrawImage(painter, getVScrollPos(), imgVScroll, m_vScrollImageStretch); if (nullptr == imgVScrollTop) painter.fillRect(getVScrollTopPos(), clrVScrollTop); else painter.drawImage(getVScrollTopPos(), *imgVScrollTop); if (nullptr == imgVScrollBottom) painter.fillRect(getVScrollBottomPos(), clrVScrollBottom); else painter.drawImage(getVScrollBottomPos(), *imgVScrollBottom); if (nullptr == imgVScrollSlider) painter.fillRect(getVScrollSliderPos(), clrVScrollSlider); else DrawImage(painter, getVScrollSliderPos(), imgVScrollSlider, vScrollSliderStretch); } if (m_hScroll && m_vScroll) { if (nullptr == nullScrollImage) painter.fillRect(getNullScrollPos(), clrNullScroll); else painter.drawImage(getNullScrollPos(), *nullScrollImage); } } void HGImgView::reset() { // 复位 m_draging = false; m_operate = 0; m_beginX = -1; m_beginY = -1; } void HGImgView::updateMoveStatusAndCursor() { m_mouseMoveStatus = getMouseStatus(mapFromGlobal(QCursor::pos())); if (0 != m_operate) { setCursor(Qt::ClosedHandCursor); } else if (MouseStatus_Null != m_mousePressStatus || m_draging) { setCursor(Qt::ArrowCursor); } else { QRect rcWnd(0, 0, this->width(), this->height()); if (nullptr == m_image || !m_showImage || !m_showColorInfo || MouseStatus_Null != m_mouseMoveStatus || !rcWnd.contains(mapFromGlobal(QCursor::pos()))) { setCursor(Qt::ArrowCursor); } else { HGImageInfo info; HGBase_GetImageInfo(m_image, &info); int xShow = mapFromGlobal(QCursor::pos()).x() - m_showRect.left; int yShow = mapFromGlobal(QCursor::pos()).y() - m_showRect.top; int xImg = xShow * (int)info.width / (m_showRect.right - m_showRect.left); int yImg = yShow * (int)info.height / (m_showRect.bottom - m_showRect.top); if (xImg >= 0 && xImg < (int)info.width && yImg >= 0 && yImg < (int)info.height) { setCursor(Qt::CrossCursor); } else { setCursor(Qt::ArrowCursor); } } } } HGImgView::MouseStatus HGImgView::getMouseStatus(const QPoint &pt) { MouseStatus mouseStatus = MouseStatus_Null; if (MouseStatus_Null != m_mousePressStatus || 0 != m_operate || m_draging) { return mouseStatus; } if (!m_mouseOn) { return mouseStatus; } if (m_hScroll && m_vScroll) { if (getHScrollSliderPos().contains(pt)) mouseStatus = MouseStatus_HScrollSlider; else if (getHScrollLeftPos().contains(pt)) mouseStatus = MouseStatus_HScrollLeft; else if (getHScrollRightPos().contains(pt)) mouseStatus = MouseStatus_HScrollRight; else if (getHScrollPos().contains(pt)) mouseStatus = MouseStatus_HScroll; else if (getVScrollSliderPos().contains(pt)) mouseStatus = MouseStatus_VScrollSlider; else if (getVScrollTopPos().contains(pt)) mouseStatus = MouseStatus_VScrollTop; else if (getVScrollBottomPos().contains(pt)) mouseStatus = MouseStatus_VScrollBottom; else if (getVScrollPos().contains(pt)) mouseStatus = MouseStatus_VScroll; else if (getNullScrollPos().contains(pt)) mouseStatus = MouseStatus_NullScroll; } else if (m_hScroll && !m_vScroll) { if (getHScrollSliderPos().contains(pt)) mouseStatus = MouseStatus_HScrollSlider; else if (getHScrollLeftPos().contains(pt)) mouseStatus = MouseStatus_HScrollLeft; else if (getHScrollRightPos().contains(pt)) mouseStatus = MouseStatus_HScrollRight; else if (getHScrollPos().contains(pt)) mouseStatus = MouseStatus_HScroll; } else if (!m_hScroll && m_vScroll) { if (getVScrollSliderPos().contains(pt)) mouseStatus = MouseStatus_VScrollSlider; else if (getVScrollTopPos().contains(pt)) mouseStatus = MouseStatus_VScrollTop; else if (getVScrollBottomPos().contains(pt)) mouseStatus = MouseStatus_VScrollBottom; else if (getVScrollPos().contains(pt)) mouseStatus = MouseStatus_VScroll; } return mouseStatus; } QRect HGImgView::getHScrollLeftPos() { assert(m_hScroll); return QRect(0, this->height() - m_scrollSize, m_scrollSize, m_scrollSize); } QRect HGImgView::getHScrollRightPos() { assert(m_hScroll); if (!m_vScroll) { return QRect(this->width() - m_scrollSize, this->height() - m_scrollSize, m_scrollSize, m_scrollSize); } return QRect(this->width() - 2 * m_scrollSize, this->height() - m_scrollSize, m_scrollSize, m_scrollSize); } QRect HGImgView::getHScrollPos() { assert(m_hScroll); if (!m_vScroll) { return QRect(0, this->height() - m_scrollSize, this->width(), m_scrollSize); } return QRect(0, this->height() - m_scrollSize, this->width() - m_scrollSize, m_scrollSize); } QRect HGImgView::getHScrollSliderPos() { assert(m_hScroll); QRect rect; rect.setY(this->height() - m_scrollSize); rect.setHeight(m_scrollSize); if (!m_vScroll) { int rollTotalLen = this->width() - 2 * m_scrollSize; int rollLeft = round((double)rollTotalLen * (-m_showRect.left) / (double)(m_showRect.right - m_showRect.left)); int rollRight = round((double)rollTotalLen * (-m_showRect.left + this->width()) / (double)(m_showRect.right - m_showRect.left)); int rollLen = rollRight - rollLeft; if (rollLen < m_minScrollSliderSize) { rollLen = m_minScrollSliderSize; rollLeft = round((double)(rollTotalLen - m_minScrollSliderSize) * (-m_showRect.left) / (double)(m_showRect.right - m_showRect.left - this->width())); } rect.setX(rollLeft + m_scrollSize); rect.setWidth(rollLen); } else { int rollTotalLen = this->width() - 3 * m_scrollSize; int rollLeft = round((double)rollTotalLen * (-m_showRect.left) / (double)(m_showRect.right - m_showRect.left)); int rollRight = round((double)rollTotalLen * (-m_showRect.left + this->width() - m_scrollSize) / (double)(m_showRect.right - m_showRect.left)); int rollLen = rollRight - rollLeft; if (rollLen < m_minScrollSliderSize) { rollLen = m_minScrollSliderSize; rollLeft = round((double)(rollTotalLen - m_minScrollSliderSize) * (-m_showRect.left) / (double)(m_showRect.right - m_showRect.left - this->width() + m_scrollSize)); } rect.setX(rollLeft + m_scrollSize); rect.setWidth(rollLen); } return rect; } QRect HGImgView::getVScrollTopPos() { assert(m_vScroll); return QRect(this->width() - m_scrollSize, 0, m_scrollSize, m_scrollSize); } QRect HGImgView::getVScrollBottomPos() { assert(m_vScroll); if (!m_hScroll) { return QRect(this->width() - m_scrollSize, this->height() - m_scrollSize, m_scrollSize, m_scrollSize); } return QRect(this->width() - m_scrollSize, this->height() - 2 * m_scrollSize, m_scrollSize, m_scrollSize); } QRect HGImgView::getVScrollPos() { assert(m_vScroll); if (!m_hScroll) { return QRect(this->width() - m_scrollSize, 0, m_scrollSize, this->height()); } return QRect(this->width() - m_scrollSize, 0, m_scrollSize, this->height() - m_scrollSize); } QRect HGImgView::getVScrollSliderPos() { assert(m_vScroll); QRect rect; rect.setX(this->width() - m_scrollSize); rect.setWidth(m_scrollSize); if (!m_hScroll) { int rollTotalLen = this->height() - 2 * m_scrollSize; int rollTop = round((double)rollTotalLen * (-m_showRect.top) / (double)(m_showRect.bottom - m_showRect.top)); int rollBottom = round((double)rollTotalLen * (-m_showRect.top + this->height()) / (double)(m_showRect.bottom - m_showRect.top)); int rollLen = rollBottom - rollTop; if (rollLen < m_minScrollSliderSize) { rollLen = m_minScrollSliderSize; rollTop = round((double)(rollTotalLen - m_minScrollSliderSize) * (-m_showRect.top) / (double)(m_showRect.bottom - m_showRect.top - this->height())); } rect.setY(rollTop + m_scrollSize); rect.setHeight(rollLen); } else { int rollTotalLen = this->height() - 3 * m_scrollSize; int rollTop = round((double)rollTotalLen * (-m_showRect.top) / (double)(m_showRect.bottom - m_showRect.top)); int rollBottom = round((double)rollTotalLen * (-m_showRect.top + this->height() - m_scrollSize) / (double)(m_showRect.bottom - m_showRect.top)); int rollLen = rollBottom - rollTop; if (rollLen < m_minScrollSliderSize) { rollLen = m_minScrollSliderSize; rollTop = round((double)(rollTotalLen - m_minScrollSliderSize) * (-m_showRect.top) / (double)(m_showRect.bottom - m_showRect.top - this->height() + m_scrollSize)); } rect.setY(rollTop + m_scrollSize); rect.setHeight(rollLen); } return rect; } QRect HGImgView::getNullScrollPos() { assert(m_hScroll && m_vScroll); return QRect(this->width() - m_scrollSize, this->height() - m_scrollSize, m_scrollSize, m_scrollSize); } void HGImgView::Show() { repaint(); }