HGGitLab

Commit 497b4541 authored by luoliangyi's avatar luoliangyi

完善缩略图的滚动条功能

parent 81171da7
......@@ -39,6 +39,8 @@ MainWindow::MainWindow(QWidget *parent)
m_thumb->move(450, 100);
m_thumb->resize(200, height() - 150);
connect(m_thumb, SIGNAL(currItemChanged(int)), this, SLOT(on_currItemChanged(int)), Qt::QueuedConnection);
HGImage img = NULL;
HGImgFmt_LoadImage("D:\\1.jpg", 0, NULL, HGBASE_IMGTYPE_RGB, HGBASE_IMGORIGIN_TOP, &img);
m_view->addImage(img);
......@@ -46,9 +48,13 @@ MainWindow::MainWindow(QWidget *parent)
QImage img2("D:\\image.png");
m_thumb->setDefItemImage(&img2);
m_thumb->setType(ThumbType_Vert);
m_thumb->setType(ThumbType_Grid);
//m_thumb->setGapSize(30);
m_thumb->setItemSize(80);
//m_thumb->setItemTextHeight(0);
//m_thumb->setScrollSize(12);
for (int i = 0; i < 1000; ++i)
for (int i = 0; i < 20000; ++i)
{
char fileName[256];
sprintf(fileName, "D:\\Pictures\\%d.jpg", i);
......@@ -120,3 +126,20 @@ void MainWindow::on_act_fullscreen_triggered()
{
m_view->setFullScreen(true);
}
void MainWindow::on_currItemChanged(int index)
{
if (-1 != index)
{
QString fileName;
m_thumb->getItemFileName(index, fileName);
HGImage img = NULL;
HGImgFmt_LoadImage(fileName.toStdString().c_str(), 0, NULL, HGBASE_IMGTYPE_RGB, HGBASE_IMGORIGIN_TOP, &img);
if (NULL != img)
{
m_view->addImage(img);
HGBase_DestroyImage(img);
}
}
}
......@@ -42,6 +42,9 @@ private slots:
void on_act_fullscreen_triggered();
private slots:
void on_currItemChanged(int index);
private:
Ui::MainWindow *ui;
HGImgView *m_view;
......
......@@ -15,102 +15,650 @@ HGImgThumb::HGImgThumb(QWidget* parent)
HGBase_CreateLock(&m_lockBack);
HGBase_CreateLock(&m_lockItemSize);
HGBase_CreateEvent(HGFALSE, HGFALSE, &m_event);
m_type = ThumbType_Grid;
m_gapSize = 10;
m_scrollSize = 10;
m_itemSize = 120;
m_itemTextHeight = 20;
m_defItemImage = NULL;
m_itemImage = NULL;
m_gapWidth = 10;
m_itemSize = 120;
for (int i = 0; i < 3; ++i)
{
m_hScrollLeftImage[i] = NULL;
m_hScrollRightImage[i] = NULL;
m_vScrollTopImage[i] = NULL;
m_vScrollBottomImage[i] = NULL;
m_scrollSliderImage[i] = NULL;
}
m_scrollImage = NULL;
m_type = ThumbType_Grid;
m_curItemIndex = -1;
m_hotItemIndex = -1;
m_signItemIndex = -1;
m_stopThread = false;
HGBase_OpenThread(ThreadFunc, this, &m_thread);
m_hRoll = false;
m_vRoll = false;
m_hScroll = false;
m_vScroll = false;
m_showThumb = false;
memset(&m_showRect, 0, sizeof(HGRect));
m_mousePosX = -1;
m_mousePosY = -1;
m_moveStatus = MoveStatus_Null;
m_beginX = -1;
m_beginY = -1;
m_mouseMoveStatus = MouseStatus_Null;
m_mousePressStatus = MouseStatus_Null;
m_mousePressBeginX = -1;
m_mousePressBeginY = -1;
setFocusPolicy(Qt::ClickFocus);
setMouseTracking(true);
connect(this, SIGNAL(updateItem()), this, SLOT(on_updateItem()), Qt::QueuedConnection);
}
HGImgThumb::~HGImgThumb()
HGImgThumb::~HGImgThumb()
{
removeAllItems(ThumbRemoveFlag::ThumbRemoveFlag_NULL);
m_stopThread = true;
HGBase_SetEvent(m_event);
HGBase_CloseThread(m_thread);
m_thread = NULL;
delete m_scrollImage;
m_scrollImage = NULL;
for (int i = 0; i < 3; ++i)
{
delete m_hScrollLeftImage[i];
m_hScrollLeftImage[i] = NULL;
delete m_hScrollRightImage[i];
m_hScrollRightImage[i] = NULL;
delete m_vScrollTopImage[i];
m_vScrollTopImage[i] = NULL;
delete m_vScrollBottomImage[i];
m_vScrollBottomImage[i] = NULL;
delete m_scrollSliderImage[i];
m_scrollSliderImage[i] = NULL;
}
delete m_itemImage;
m_itemImage = NULL;
delete m_defItemImage;
m_defItemImage = NULL;
HGBase_DestroyEvent(m_event);
m_event = NULL;
HGBase_DestroyLock(m_lockItemSize);
m_lockItemSize = NULL;
HGBase_DestroyLock(m_lockBack);
m_lockBack = NULL;
HGBase_DestroyLock(m_lockFront);
m_lockFront = NULL;
qDebug("~HGImgThumb");
}
HGResult HGImgThumb::setGapSize(int size)
{
if (size < 0 || size > 40)
{
return HGBASE_ERR_INVALIDARG;
}
if (0 != m_frontItems.size())
{
return HGBASE_ERR_FAIL;
}
m_gapSize = size;
return HGBASE_ERR_OK;
}
HGResult HGImgThumb::setScrollSize(int size)
{
if (size < 10 || size > 20)
{
return HGBASE_ERR_INVALIDARG;
}
if (0 != m_frontItems.size())
{
return HGBASE_ERR_FAIL;
}
m_scrollSize = size;
return HGBASE_ERR_OK;
}
HGResult HGImgThumb::setItemSize(int size)
{
if (size < 60 || size > 240)
{
return HGBASE_ERR_INVALIDARG;
}
if (0 != m_frontItems.size())
{
return HGBASE_ERR_FAIL;
}
if (m_itemSize == size)
{
return HGBASE_ERR_OK;
}
HGBase_EnterLock(m_lockItemSize);
m_itemSize = size;
HGBase_LeaveLock(m_lockItemSize);
if (NULL != m_defItemImage)
{
assert(NULL != m_itemImage);
delete m_itemImage;
m_itemImage = createItemImage(m_defItemImage, m_itemSize);
assert(NULL != m_itemImage);
}
return HGBASE_ERR_OK;
}
HGResult HGImgThumb::setItemTextHeight(int height)
{
if (height < 0 || height > 40)
{
return HGBASE_ERR_INVALIDARG;
}
if (0 != m_frontItems.size())
{
return HGBASE_ERR_FAIL;
}
m_itemTextHeight = height;
return HGBASE_ERR_OK;
}
HGResult HGImgThumb::setDefItemImage(const QImage *image)
{
if (0 != m_frontItems.size())
{
return HGBASE_ERR_FAIL;
}
if (NULL == image)
{
delete m_itemImage;
m_itemImage = NULL;
delete m_defItemImage;
m_defItemImage = NULL;
return HGBASE_ERR_OK;
}
QImage *img = new QImage(*image);
if (img->isNull())
{
delete img;
return HGBASE_ERR_FAIL;
}
if (NULL != m_defItemImage)
{
assert(NULL != m_itemImage);
delete m_itemImage;
delete m_defItemImage;
}
else
{
assert(NULL == m_itemImage);
}
m_defItemImage = img;
m_itemImage = createItemImage(m_defItemImage, m_itemSize);
assert(NULL != m_itemImage);
return HGBASE_ERR_OK;
}
HGResult HGImgThumb::setHScrollLeftImage(const QImage *normalImage, const QImage *hotImage, const QImage *pushImage)
{
if (0 != m_frontItems.size())
{
return HGBASE_ERR_FAIL;
}
if (NULL == normalImage)
{
delete m_hScrollLeftImage[0];
m_hScrollLeftImage[0] = NULL;
}
else
{
QImage *img = new QImage(*normalImage);
if (!img->isNull())
{
if (NULL != m_hScrollLeftImage[0])
delete m_hScrollLeftImage[0];
m_hScrollLeftImage[0] = img;
}
else
{
delete img;
}
}
if (NULL == hotImage)
{
delete m_hScrollLeftImage[1];
m_hScrollLeftImage[1] = NULL;
}
else
{
QImage *img = new QImage(*hotImage);
if (!img->isNull())
{
if (NULL != m_hScrollLeftImage[1])
delete m_hScrollLeftImage[1];
m_hScrollLeftImage[1] = img;
}
else
{
delete img;
}
}
if (NULL == pushImage)
{
delete m_hScrollLeftImage[2];
m_hScrollLeftImage[2] = NULL;
}
else
{
QImage *img = new QImage(*pushImage);
if (!img->isNull())
{
if (NULL != m_hScrollLeftImage[2])
delete m_hScrollLeftImage[2];
m_hScrollLeftImage[2] = img;
}
else
{
delete img;
}
}
return HGBASE_ERR_OK;
}
HGResult HGImgThumb::setHScrollRightImage(const QImage *normalImage, const QImage *hotImage, const QImage *pushImage)
{
if (0 != m_frontItems.size())
{
return HGBASE_ERR_FAIL;
}
if (NULL == normalImage)
{
delete m_hScrollRightImage[0];
m_hScrollRightImage[0] = NULL;
}
else
{
QImage *img = new QImage(*normalImage);
if (!img->isNull())
{
if (NULL != m_hScrollRightImage[0])
delete m_hScrollRightImage[0];
m_hScrollRightImage[0] = img;
}
else
{
delete img;
}
}
if (NULL == hotImage)
{
delete m_hScrollRightImage[1];
m_hScrollRightImage[1] = NULL;
}
else
{
QImage *img = new QImage(*hotImage);
if (!img->isNull())
{
if (NULL != m_hScrollRightImage[1])
delete m_hScrollRightImage[1];
m_hScrollRightImage[1] = img;
}
else
{
delete img;
}
}
if (NULL == pushImage)
{
delete m_hScrollRightImage[2];
m_hScrollRightImage[2] = NULL;
}
else
{
QImage *img = new QImage(*pushImage);
if (!img->isNull())
{
if (NULL != m_hScrollRightImage[2])
delete m_hScrollRightImage[2];
m_hScrollRightImage[2] = img;
}
else
{
delete img;
}
}
return HGBASE_ERR_OK;
}
HGResult HGImgThumb::setVScrollTopImage(const QImage *normalImage, const QImage *hotImage, const QImage *pushImage)
{
if (0 != m_frontItems.size())
{
return HGBASE_ERR_FAIL;
}
if (NULL == normalImage)
{
delete m_vScrollTopImage[0];
m_vScrollTopImage[0] = NULL;
}
else
{
QImage *img = new QImage(*normalImage);
if (!img->isNull())
{
if (NULL != m_vScrollTopImage[0])
delete m_vScrollTopImage[0];
m_vScrollTopImage[0] = img;
}
else
{
delete img;
}
}
if (NULL == hotImage)
{
delete m_vScrollTopImage[1];
m_vScrollTopImage[1] = NULL;
}
else
{
QImage *img = new QImage(*hotImage);
if (!img->isNull())
{
if (NULL != m_vScrollTopImage[1])
delete m_vScrollTopImage[1];
m_vScrollTopImage[1] = img;
}
else
{
delete img;
}
}
if (NULL == pushImage)
{
delete m_vScrollTopImage[2];
m_vScrollTopImage[2] = NULL;
}
else
{
QImage *img = new QImage(*pushImage);
if (!img->isNull())
{
if (NULL != m_vScrollTopImage[2])
delete m_vScrollTopImage[2];
m_vScrollTopImage[2] = img;
}
else
{
delete img;
}
}
return HGBASE_ERR_OK;
}
HGResult HGImgThumb::setVScrollBottomImage(const QImage *normalImage, const QImage *hotImage, const QImage *pushImage)
{
if (0 != m_frontItems.size())
{
return HGBASE_ERR_FAIL;
}
if (NULL == normalImage)
{
delete m_vScrollBottomImage[0];
m_vScrollBottomImage[0] = NULL;
}
else
{
QImage *img = new QImage(*normalImage);
if (!img->isNull())
{
if (NULL != m_vScrollBottomImage[0])
delete m_vScrollBottomImage[0];
m_vScrollBottomImage[0] = img;
}
else
{
delete img;
}
}
if (NULL == hotImage)
{
delete m_vScrollBottomImage[1];
m_vScrollBottomImage[1] = NULL;
}
else
{
QImage *img = new QImage(*hotImage);
if (!img->isNull())
{
if (NULL != m_vScrollBottomImage[1])
delete m_vScrollBottomImage[1];
m_vScrollBottomImage[1] = img;
}
else
{
delete img;
}
}
if (NULL == pushImage)
{
delete m_vScrollBottomImage[2];
m_vScrollBottomImage[2] = NULL;
}
else
{
QImage *img = new QImage(*pushImage);
if (!img->isNull())
{
if (NULL != m_vScrollBottomImage[2])
delete m_vScrollBottomImage[2];
m_vScrollBottomImage[2] = img;
}
else
{
delete img;
}
}
return HGBASE_ERR_OK;
}
HGResult HGImgThumb::setScrollImage(const QImage *image, const QRect *stretch)
{
if (0 != m_frontItems.size())
{
return HGBASE_ERR_FAIL;
}
if (NULL == image)
{
delete m_scrollImage;
m_scrollImage = NULL;
}
else
{
QImage *img = new QImage(*image);
if (!img->isNull())
{
if (NULL != m_scrollImage)
delete m_scrollImage;
m_scrollImage = img;
if (NULL == stretch || stretch->left() < 0 || stretch->top() < 0
|| stretch->right() > m_scrollImage->width() || stretch->bottom() > m_scrollImage->height())
{
m_scrollImageStretch.setX(0);
m_scrollImageStretch.setY(0);
m_scrollImageStretch.setWidth(m_scrollImage->width());
m_scrollImageStretch.setHeight(m_scrollImage->height());
}
else
{
m_scrollImageStretch = *stretch;
}
}
else
{
delete img;
}
}
return HGBASE_ERR_OK;
}
HGResult HGImgThumb::setscrollSliderImage(const QImage *normalImage, const QRect *normalStretch, const QImage *hotImage, const QRect *hotStretch,
const QImage *pushImage, const QRect *pushStretch)
{
removeAllItems(ThumbRemoveFlag::ThumbRemoveFlag_NULL);
if (0 != m_frontItems.size())
{
return HGBASE_ERR_FAIL;
}
m_stopThread = true;
HGBase_SetEvent(m_event);
HGBase_CloseThread(m_thread);
m_thread = NULL;
delete m_itemImage;
m_itemImage = NULL;
delete m_defItemImage;
m_defItemImage = NULL;
HGBase_DestroyEvent(m_event);
m_event = NULL;
HGBase_DestroyLock(m_lockItemSize);
m_lockItemSize = NULL;
HGBase_DestroyLock(m_lockBack);
m_lockBack = NULL;
HGBase_DestroyLock(m_lockFront);
m_lockFront = NULL;
if (NULL == normalImage)
{
delete m_scrollSliderImage[0];
m_scrollSliderImage[0] = NULL;
}
else
{
QImage *img = new QImage(*normalImage);
if (!img->isNull())
{
if (NULL != m_scrollSliderImage[0])
delete m_scrollSliderImage[0];
m_scrollSliderImage[0] = img;
qDebug("~HGImgThumb");
}
if (NULL == normalStretch || normalStretch->left() < 0 || normalStretch->top() < 0
|| normalStretch->right() > m_scrollSliderImage[0]->width() || normalStretch->bottom() > m_scrollSliderImage[0]->height())
{
m_scrollSliderImageStretch[0].setX(0);
m_scrollSliderImageStretch[0].setY(0);
m_scrollSliderImageStretch[0].setWidth(m_scrollSliderImage[0]->width());
m_scrollSliderImageStretch[0].setHeight(m_scrollSliderImage[0]->height());
}
else
{
m_scrollSliderImageStretch[0] = *normalStretch;
}
}
else
{
delete img;
}
}
HGResult HGImgThumb::setType(ThumbType type)
{
if (m_type == type)
if (NULL == hotImage)
{
return HGBASE_ERR_OK;
delete m_scrollSliderImage[1];
m_scrollSliderImage[1] = NULL;
}
else
{
QImage *img = new QImage(*hotImage);
if (!img->isNull())
{
if (NULL != m_scrollSliderImage[1])
delete m_scrollSliderImage[1];
m_scrollSliderImage[1] = img;
m_type = type;
if (NULL == hotStretch || hotStretch->left() < 0 || hotStretch->top() < 0
|| hotStretch->right() > m_scrollSliderImage[1]->width() || hotStretch->bottom() > m_scrollSliderImage[1]->height())
{
m_scrollSliderImageStretch[1].setX(0);
m_scrollSliderImageStretch[1].setY(0);
m_scrollSliderImageStretch[1].setWidth(m_scrollSliderImage[1]->width());
m_scrollSliderImageStretch[1].setHeight(m_scrollSliderImage[1]->height());
}
else
{
m_scrollSliderImageStretch[1] = *hotStretch;
}
}
else
{
delete img;
}
}
int showWidth = 0, showHeight = 0;
calcShowSize(this->width(), this->height(), 10, m_type, m_itemSize, m_gapWidth, (int)m_frontItems.size(),
showWidth, showHeight, m_hRoll, m_vRoll);
m_showThumb = false;
memset(&m_showRect, 0, sizeof(HGRect));
if (showWidth > 0 && showHeight > 0)
if (NULL == pushImage)
{
m_showThumb = true;
m_showRect.left = 0;
m_showRect.top = 0;
m_showRect.right = showWidth;
m_showRect.bottom = showHeight;
recalcShowRect(this->width(), this->height(), 10, m_type, m_hRoll, m_vRoll, m_showThumb, m_showRect);
delete m_scrollSliderImage[2];
m_scrollSliderImage[2] = NULL;
}
else
{
QImage *img = new QImage(*pushImage);
if (!img->isNull())
{
if (NULL != m_scrollSliderImage[2])
delete m_scrollSliderImage[2];
m_scrollSliderImage[2] = img;
if (NULL == pushStretch || pushStretch->left() < 0 || pushStretch->top() < 0
|| pushStretch->right() > m_scrollSliderImage[2]->width() || pushStretch->bottom() > m_scrollSliderImage[2]->height())
{
m_scrollSliderImageStretch[2].setX(0);
m_scrollSliderImageStretch[2].setY(0);
m_scrollSliderImageStretch[2].setWidth(m_scrollSliderImage[2]->width());
m_scrollSliderImageStretch[2].setHeight(m_scrollSliderImage[2]->height());
}
else
{
m_scrollSliderImageStretch[2] = *pushStretch;
}
}
else
{
delete img;
}
}
QPoint pt(m_mousePosX, m_mousePosY);
m_hotItemIndex = getItem(pt);
Show();
return HGBASE_ERR_OK;
}
HGResult HGImgThumb::setGap(int width)
HGResult HGImgThumb::setType(ThumbType type)
{
if (width < 0 || width > 100)
{
return HGBASE_ERR_INVALIDARG;
}
if (m_gapWidth == width)
if (m_type == type)
{
return HGBASE_ERR_OK;
}
m_gapWidth = width;
m_type = type;
int showWidth = 0, showHeight = 0;
calcShowSize(this->width(), this->height(), 10, m_type, m_itemSize, m_gapWidth, (int)m_frontItems.size(),
showWidth, showHeight, m_hRoll, m_vRoll);
calcShowSize(this->width(), this->height(), m_gapSize, m_scrollSize, m_itemSize, m_itemTextHeight, m_type, (int)m_frontItems.size(),
showWidth, showHeight, m_hScroll, m_vScroll);
m_showThumb = false;
memset(&m_showRect, 0, sizeof(HGRect));
if (showWidth > 0 && showHeight > 0)
......@@ -120,22 +668,24 @@ HGResult HGImgThumb::setGap(int width)
m_showRect.top = 0;
m_showRect.right = showWidth;
m_showRect.bottom = showHeight;
recalcShowRect(this->width(), this->height(), 10, m_type, m_hRoll, m_vRoll, m_showThumb, m_showRect);
recalcShowRect(this->width(), this->height(), m_scrollSize, m_type, m_hScroll, m_vScroll, m_showThumb, m_showRect);
}
QPoint pt(m_mousePosX, m_mousePosY);
m_hotItemIndex = getItem(pt);
m_hotItemIndex = getItem(pt, m_mouseMoveStatus);
Show();
return HGBASE_ERR_OK;
}
HGResult HGImgThumb::zoomIn()
{
if (!m_showThumb)
if (0 == m_frontItems.size())
{
return HGBASE_ERR_FAIL;
}
assert(m_showThumb);
int itemSize = (int)((float)m_itemSize * 1.2f);
int newItemSize = HGMIN(240, HGMAX(60, itemSize));
if (m_itemSize == newItemSize)
......@@ -155,15 +705,15 @@ HGResult HGImgThumb::zoomIn()
}
int showWidth = 0, showHeight = 0;
calcShowSize(this->width(), this->height(), 10, m_type, m_itemSize, m_gapWidth, (int)m_frontItems.size(),
showWidth, showHeight, m_hRoll, m_vRoll);
calcShowSize(this->width(), this->height(), m_gapSize, m_scrollSize, m_itemSize, m_itemTextHeight, m_type, (int)m_frontItems.size(),
showWidth, showHeight, m_hScroll, m_vScroll);
assert(showWidth > 0 && showHeight > 0);
m_showRect.right = m_showRect.left + showWidth;
m_showRect.bottom = m_showRect.top + showHeight;
recalcShowRect(this->width(), this->height(), m_scrollSize, m_type, m_hScroll, m_vScroll, m_showThumb, m_showRect);
recalcShowRect(this->width(), this->height(), 10, m_type, m_hRoll, m_vRoll, m_showThumb, m_showRect);
QPoint pt(m_mousePosX, m_mousePosY);
m_hotItemIndex = getItem(pt);
m_hotItemIndex = getItem(pt, m_mouseMoveStatus);
Show();
HGBase_EnterLock(m_lockBack);
......@@ -180,11 +730,13 @@ HGResult HGImgThumb::zoomIn()
HGResult HGImgThumb::zoomOut()
{
if (!m_showThumb)
if (0 == m_frontItems.size())
{
return HGBASE_ERR_FAIL;
}
assert(m_showThumb);
int itemSize = (int)((float)m_itemSize * 1.0f / 1.2f);
int newItemSize = HGMIN(240, HGMAX(60, itemSize));
if (m_itemSize == newItemSize)
......@@ -204,15 +756,15 @@ HGResult HGImgThumb::zoomOut()
}
int showWidth = 0, showHeight = 0;
calcShowSize(this->width(), this->height(), 10, m_type, m_itemSize, m_gapWidth, (int)m_frontItems.size(),
showWidth, showHeight, m_hRoll, m_vRoll);
calcShowSize(this->width(), this->height(), m_gapSize, m_scrollSize, m_itemSize, m_itemTextHeight, m_type, (int)m_frontItems.size(),
showWidth, showHeight, m_hScroll, m_vScroll);
assert(showWidth > 0 && showHeight > 0);
m_showRect.right = m_showRect.left + showWidth;
m_showRect.bottom = m_showRect.top + showHeight;
recalcShowRect(this->width(), this->height(), m_scrollSize, m_type, m_hScroll, m_vScroll, m_showThumb, m_showRect);
recalcShowRect(this->width(), this->height(), 10, m_type, m_hRoll, m_vRoll, m_showThumb, m_showRect);
QPoint pt(m_mousePosX, m_mousePosY);
m_hotItemIndex = getItem(pt);
m_hotItemIndex = getItem(pt, m_mouseMoveStatus);
Show();
HGBase_EnterLock(m_lockBack);
......@@ -227,46 +779,6 @@ HGResult HGImgThumb::zoomOut()
return HGBASE_ERR_OK;
}
HGResult HGImgThumb::setDefItemImage(const QImage *image)
{
if (0 != m_frontItems.size())
{
return HGBASE_ERR_FAIL;
}
if (NULL == image)
{
delete m_itemImage;
m_itemImage = NULL;
delete m_defItemImage;
m_defItemImage = NULL;
return HGBASE_ERR_OK;
}
QImage *img = new QImage(*image);
if (img->isNull())
{
delete img;
return HGBASE_ERR_FAIL;
}
if (NULL != m_defItemImage)
{
assert(NULL != m_itemImage);
delete m_itemImage;
delete m_defItemImage;
}
else
{
assert(NULL == m_itemImage);
}
m_defItemImage = img;
m_itemImage = createItemImage(m_defItemImage, m_itemSize);
assert(NULL != m_itemImage);
return HGBASE_ERR_OK;
}
HGResult HGImgThumb::getItemCount(int *count)
{
if (NULL == count)
......@@ -303,16 +815,16 @@ HGResult HGImgThumb::addItem(const QString &fileName)
HGBase_SetEvent(m_event);
int showWidth = 0, showHeight = 0;
calcShowSize(this->width(), this->height(), 10, m_type, m_itemSize, m_gapWidth, (int)m_frontItems.size(),
showWidth, showHeight, m_hRoll, m_vRoll);
calcShowSize(this->width(), this->height(), m_gapSize, m_scrollSize, m_itemSize, m_itemTextHeight, m_type, (int)m_frontItems.size(),
showWidth, showHeight, m_hScroll, m_vScroll);
assert(showWidth > 0 && showHeight > 0);
m_showThumb = true;
m_showRect.right = m_showRect.left + showWidth;
m_showRect.bottom = m_showRect.top + showHeight;
recalcShowRect(this->width(), this->height(), m_scrollSize, m_type, m_hScroll, m_vScroll, m_showThumb, m_showRect);
recalcShowRect(this->width(), this->height(), 10, m_type, m_hRoll, m_vRoll, m_showThumb, m_showRect);
QPoint pt(m_mousePosX, m_mousePosY);
m_hotItemIndex = getItem(pt);
m_hotItemIndex = getItem(pt, m_mouseMoveStatus);
Show();
emit itemCountChanged(m_frontItems.size());
return HGBASE_ERR_OK;
......@@ -346,16 +858,16 @@ HGResult HGImgThumb::insertItem(const QString &fileName, int pos)
HGBase_SetEvent(m_event);
int showWidth = 0, showHeight = 0;
calcShowSize(this->width(), this->height(), 10, m_type, m_itemSize, m_gapWidth, (int)m_frontItems.size(),
showWidth, showHeight, m_hRoll, m_vRoll);
calcShowSize(this->width(), this->height(), m_gapSize, m_scrollSize, m_itemSize, m_itemTextHeight, m_type, (int)m_frontItems.size(),
showWidth, showHeight, m_hScroll, m_vScroll);
assert(showWidth > 0 && showHeight > 0);
m_showThumb = true;
m_showRect.right = m_showRect.left + showWidth;
m_showRect.bottom = m_showRect.top + showHeight;
recalcShowRect(this->width(), this->height(), m_scrollSize, m_type, m_hScroll, m_vScroll, m_showThumb, m_showRect);
recalcShowRect(this->width(), this->height(), 10, m_type, m_hRoll, m_vRoll, m_showThumb, m_showRect);
QPoint pt(m_mousePosX, m_mousePosY);
m_hotItemIndex = getItem(pt);
m_hotItemIndex = getItem(pt, m_mouseMoveStatus);
Show();
emit itemCountChanged(m_frontItems.size());
return HGBASE_ERR_OK;
......@@ -397,8 +909,7 @@ HGResult HGImgThumb::setCurrItem(int index)
m_curItemIndex = index;
Show();
emit currentItemChanged(m_curItemIndex);
emit currItemChanged(m_curItemIndex);
return HGBASE_ERR_OK;
}
......@@ -449,8 +960,8 @@ HGResult HGImgThumb::removeItem(int index, ThumbRemoveFlag flag)
--m_signItemIndex;
int showWidth = 0, showHeight = 0;
calcShowSize(this->width(), this->height(), 10, m_type, m_itemSize, m_gapWidth, (int)m_frontItems.size(),
showWidth, showHeight, m_hRoll, m_vRoll);
calcShowSize(this->width(), this->height(), m_gapSize, m_scrollSize, m_itemSize, m_itemTextHeight, m_type, (int)m_frontItems.size(),
showWidth, showHeight, m_hScroll, m_vScroll);
if (showWidth <= 0 || showHeight <= 0)
{
m_showThumb = false;
......@@ -460,16 +971,16 @@ HGResult HGImgThumb::removeItem(int index, ThumbRemoveFlag flag)
{
m_showRect.right = m_showRect.left + showWidth;
m_showRect.bottom = m_showRect.top + showHeight;
recalcShowRect(this->width(), this->height(), 10, m_type, m_hRoll, m_vRoll, m_showThumb, m_showRect);
recalcShowRect(this->width(), this->height(), m_scrollSize, m_type, m_hScroll, m_vScroll, m_showThumb, m_showRect);
}
QPoint pt(m_mousePosX, m_mousePosY);
m_hotItemIndex = getItem(pt);
m_hotItemIndex = getItem(pt, m_mouseMoveStatus);
Show();
emit itemCountChanged(m_frontItems.size());
if (emitItemChange)
{
emit currentItemChanged(m_curItemIndex);
emit currItemChanged(m_curItemIndex);
}
return HGBASE_ERR_OK;
......@@ -508,15 +1019,17 @@ HGResult HGImgThumb::removeAllItems(ThumbRemoveFlag flag)
m_signItemIndex = -1;
m_showThumb = 0;
m_hScroll = false;
m_vScroll = false;
m_showThumb = false;
memset(&m_showRect, 0, sizeof(HGRect));
QPoint pt(m_mousePosX, m_mousePosY);
m_hotItemIndex = getItem(pt);
m_hotItemIndex = getItem(pt, m_mouseMoveStatus);
Show();
emit itemCountChanged(m_frontItems.size());
if (emitItemChange)
{
emit currentItemChanged(m_curItemIndex);
emit currItemChanged(m_curItemIndex);
}
return HGBASE_ERR_OK;
......@@ -648,81 +1161,25 @@ HGResult HGImgThumb::locateItem(int index)
}
locateItemEx(index);
recalcShowRect(this->width(), this->height(), 10, m_type, m_hRoll, m_vRoll, m_showThumb, m_showRect);
QPoint pt2(m_mousePosX, m_mousePosY);
m_hotItemIndex = getItem(pt2);
recalcShowRect(this->width(), this->height(), m_scrollSize, m_type, m_hScroll, m_vScroll, m_showThumb, m_showRect);
QPoint pt(m_mousePosX, m_mousePosY);
m_hotItemIndex = getItem(pt, m_mouseMoveStatus);
Show();
return HGBASE_ERR_OK;
}
void HGImgThumb::mousePressEvent(QMouseEvent *e)
{
bool emitCurItemChange = false;
bool emitItemSelectingChange = false;
int index = getItem(e->pos());
bool hitHRoll = false, hitHRollSlider = false, hitHRollLeft = false, hitHRollRight = false;
bool hitVRoll = false, hitVRollSlider = false, hitVRollTop = false, hitVRollBottom = false;
bool hitNull = false;
if (m_hRoll && m_vRoll)
{
if (getHRollPos().contains(e->pos()))
hitHRoll = true;
if (getHRollSliderPos().contains(e->pos()))
hitHRollSlider = true;
if (getHRollLeftPos().contains(e->pos()))
hitHRollLeft = true;
if (getHRollRightPos().contains(e->pos()))
hitHRollRight = true;
if (getVRollPos().contains(e->pos()))
hitVRoll = true;
if (getVRollSliderPos().contains(e->pos()))
hitVRollSlider = true;
if (getVRollTopPos().contains(e->pos()))
hitVRollTop = true;
if (getVRollBottomPos().contains(e->pos()))
hitVRollBottom = true;
if (getNullRollPos().contains(e->pos()))
hitNull = true;
}
else if (m_hRoll && !m_vRoll)
{
if (getHRollPos().contains(e->pos()))
hitHRoll = true;
if (getHRollSliderPos().contains(e->pos()))
hitHRollSlider = true;
if (getHRollLeftPos().contains(e->pos()))
hitHRollLeft = true;
if (getHRollRightPos().contains(e->pos()))
hitHRollRight = true;
}
else if (!m_hRoll && m_vRoll)
{
if (getVRollPos().contains(e->pos()))
hitVRoll = true;
if (getVRollSliderPos().contains(e->pos()))
hitVRollSlider = true;
if (getVRollTopPos().contains(e->pos()))
hitVRollTop = true;
if (getVRollBottomPos().contains(e->pos()))
hitVRollBottom = true;
}
MouseStatus mouseMoveStatus = MouseStatus_Null;
int index = getItem(e->pos(), mouseMoveStatus);
if (hitHRoll || hitVRoll || hitNull)
if (-1 == index && MouseStatus_Null != mouseMoveStatus)
{
if (hitHRollSlider)
{
m_moveStatus = MoveStatus_HRoll;
m_beginX = e->pos().x();
m_beginY = e->pos().y();
}
else if (hitVRollSlider)
{
m_moveStatus = MoveStatus_VRoll;
m_beginX = e->pos().x();
m_beginY = e->pos().y();
}
m_mousePressStatus = mouseMoveStatus;
m_mousePressBeginX = e->pos().x();
m_mousePressBeginY = e->pos().y();
Show();
return;
......@@ -904,15 +1361,15 @@ void HGImgThumb::mousePressEvent(QMouseEvent *e)
if (-1 != index)
{
locateItemEx(index);
recalcShowRect(this->width(), this->height(), 10, m_type, m_hRoll, m_vRoll, m_showThumb, m_showRect);
QPoint pt2(m_mousePosX, m_mousePosY);
m_hotItemIndex = getItem(pt2);
recalcShowRect(this->width(), this->height(), m_scrollSize, m_type, m_hScroll, m_vScroll, m_showThumb, m_showRect);
QPoint pt(m_mousePosX, m_mousePosY);
m_hotItemIndex = getItem(pt, m_mouseMoveStatus);
}
Show();
if (emitCurItemChange)
{
emit currentItemChanged(m_curItemIndex);
emit currItemChanged(m_curItemIndex);
qDebug("currentItemChanged m_curItemIndex=%d", m_curItemIndex);
}
if (emitItemSelectingChange)
......@@ -924,41 +1381,74 @@ void HGImgThumb::mousePressEvent(QMouseEvent *e)
void HGImgThumb::mouseMoveEvent(QMouseEvent* e)
{
if (MoveStatus_HRoll == m_moveStatus)
if (MouseStatus_HScrollSlider == m_mousePressStatus)
{
int lXAmount = e->pos().x() - m_beginX;
int lXAmount = e->pos().x() - m_mousePressBeginX;
int offset;
if (!m_vRoll)
offset = lXAmount * (m_showRect.right - m_showRect.left) / (this->width() - 20);
if (!m_vScroll)
{
int rollTotalLen = this->width() - 2 * m_scrollSize;
int rollLeft = (int)((double)rollTotalLen * (-m_showRect.left) / (double)(m_showRect.right - m_showRect.left) + 0.5);
int rollRight = (int)((double)rollTotalLen * (-m_showRect.left + this->width()) / (double)(m_showRect.right - m_showRect.left) + 0.5);
int rollLen = rollRight - rollLeft;
if (rollLen >= 8)
offset = lXAmount * (m_showRect.right - m_showRect.left) / rollTotalLen;
else
offset = lXAmount * (m_showRect.right - m_showRect.left) / (rollTotalLen - 8);
}
else
offset = lXAmount * (m_showRect.right - m_showRect.left) / (this->width() - 30);
{
int rollTotalLen = this->width() - 3 * m_scrollSize;
int rollLeft = (int)((double)rollTotalLen * (-m_showRect.left) / (double)(m_showRect.right - m_showRect.left) + 0.5);
int rollRight = (int)((double)rollTotalLen * (-m_showRect.left + this->width() - m_scrollSize) / (double)(m_showRect.right - m_showRect.left) + 0.5);
int rollLen = rollRight - rollLeft;
if (rollLen >= 8)
offset = lXAmount * (m_showRect.right - m_showRect.left) / rollTotalLen;
else
offset = lXAmount * (m_showRect.right - m_showRect.left) / (rollTotalLen - 8);
}
m_showRect.left -= offset;
m_showRect.right -= offset;
recalcShowRect(this->width(), this->height(), 10, m_type, m_hRoll, m_vRoll, m_showThumb, m_showRect);
recalcShowRect(this->width(), this->height(), m_scrollSize, m_type, m_hScroll, m_vScroll, m_showThumb, m_showRect);
m_beginX = e->pos().x();
m_beginY = e->pos().y();
m_mousePressBeginX = e->pos().x();
m_mousePressBeginY = e->pos().y();
}
else if (MoveStatus_VRoll == m_moveStatus)
else if (MouseStatus_VScrollSlider == m_mousePressStatus)
{
int lYAmount = e->pos().y() - m_beginY;
int lYAmount = e->pos().y() - m_mousePressBeginY;
int offset;
if (!m_hRoll)
offset = lYAmount * (m_showRect.bottom - m_showRect.top) / (this->height() - 20);
if (!m_hScroll)
{
int rollTotalLen = this->height() - 2 * m_scrollSize;
int rollTop = (int)((double)rollTotalLen * (-m_showRect.top) / (double)(m_showRect.bottom - m_showRect.top) + 0.5);
int rollBottom = (int)((double)rollTotalLen * (-m_showRect.top + this->height()) / (double)(m_showRect.bottom - m_showRect.top) + 0.5);
int rollLen = rollBottom - rollTop;
if (rollLen >= 8)
offset = lYAmount * (m_showRect.bottom - m_showRect.top) / rollTotalLen;
else
offset = lYAmount * (m_showRect.bottom - m_showRect.top) / (rollTotalLen - 8);
}
else
offset = lYAmount * (m_showRect.bottom - m_showRect.top) / (this->height() - 30);
{
int rollTotalLen = this->height() - 3 * m_scrollSize;
int rollTop = (int)((double)rollTotalLen * (-m_showRect.top) / (double)(m_showRect.bottom - m_showRect.top) + 0.5);
int rollBottom = (int)((double)rollTotalLen * (-m_showRect.top + this->height() - m_scrollSize) / (double)(m_showRect.bottom - m_showRect.top) + 0.5);
int rollLen = rollBottom - rollTop;
if (rollLen >= 8)
offset = lYAmount * (m_showRect.bottom - m_showRect.top) / rollTotalLen;
else
offset = lYAmount * (m_showRect.bottom - m_showRect.top) / (rollTotalLen - 8);
}
m_showRect.top -= offset;
m_showRect.bottom -= offset;
recalcShowRect(this->width(), this->height(), 10, m_type, m_hRoll, m_vRoll, m_showThumb, m_showRect);
recalcShowRect(this->width(), this->height(), m_scrollSize, m_type, m_hScroll, m_vScroll, m_showThumb, m_showRect);
m_beginX = e->pos().x();
m_beginY = e->pos().y();
}
else
{
m_hotItemIndex = getItem(e->pos());
m_mousePressBeginX = e->pos().x();
m_mousePressBeginY = e->pos().y();
}
m_hotItemIndex = getItem(e->pos(), m_mouseMoveStatus);
Show();
m_mousePosX = e->pos().x();
m_mousePosY = e->pos().y();
......@@ -966,25 +1456,59 @@ void HGImgThumb::mouseMoveEvent(QMouseEvent* e)
void HGImgThumb::mouseReleaseEvent(QMouseEvent *e)
{
m_moveStatus = MoveStatus_Null;
m_beginX = -1;
m_beginY = -1;
Q_UNUSED(e);
MouseStatus oldMousePressStatus = m_mousePressStatus;
m_mousePressStatus = MouseStatus_Null;
m_mousePressBeginX = -1;
m_mousePressBeginY = -1;
MouseStatus mouseMoveStatus = MouseStatus_Null;
getItem(e->pos(), mouseMoveStatus);
if (MouseStatus_HScrollLeft == oldMousePressStatus && MouseStatus_HScrollLeft == mouseMoveStatus)
{
m_showRect.left += m_itemSize / 2;
m_showRect.right += m_itemSize / 2;
recalcShowRect(this->width(), this->height(), m_scrollSize, m_type, m_hScroll, m_vScroll, m_showThumb, m_showRect);
}
else if (MouseStatus_HScrollRight == oldMousePressStatus && MouseStatus_HScrollRight == mouseMoveStatus)
{
m_showRect.left -= m_itemSize / 2;
m_showRect.right -= m_itemSize / 2;
recalcShowRect(this->width(), this->height(), m_scrollSize, m_type, m_hScroll, m_vScroll, m_showThumb, m_showRect);
}
else if (MouseStatus_VScrollTop == oldMousePressStatus && MouseStatus_VScrollTop == mouseMoveStatus)
{
m_showRect.top += m_itemSize / 2;
m_showRect.bottom += m_itemSize / 2;
recalcShowRect(this->width(), this->height(), m_scrollSize, m_type, m_hScroll, m_vScroll, m_showThumb, m_showRect);
}
else if (MouseStatus_VScrollBottom == oldMousePressStatus && MouseStatus_VScrollBottom == mouseMoveStatus)
{
m_showRect.top -= m_itemSize / 2;
m_showRect.bottom -= m_itemSize / 2;
recalcShowRect(this->width(), this->height(), m_scrollSize, m_type, m_hScroll, m_vScroll, m_showThumb, m_showRect);
}
QPoint pt2(m_mousePosX, m_mousePosY);
m_hotItemIndex = getItem(pt2);
QPoint pt(m_mousePosX, m_mousePosY);
m_hotItemIndex = getItem(pt, m_mouseMoveStatus);
Show();
}
void HGImgThumb::enterEvent(QEvent *e)
{
Q_UNUSED(e);
}
void HGImgThumb::leaveEvent(QEvent *e)
{
Q_UNUSED(e);
m_mousePosX = -1;
m_mousePosY = -1;
m_hotItemIndex = -1;
m_mouseMoveStatus = MouseStatus_Null;
Show();
}
......@@ -1022,7 +1546,8 @@ void HGImgThumb::paintEvent(QPaintEvent* e)
Q_UNUSED(e);
QPainter painter(this);
painter.fillRect(0, 0, this->width(), this->height(), qRgb(255, 255, 255));
QRect rcWnd(0, 0, this->width(), this->height());
painter.fillRect(rcWnd, qRgb(255, 255, 255));
QFont font("微软雅黑", 8);
painter.setFont(font);
......@@ -1030,6 +1555,12 @@ void HGImgThumb::paintEvent(QPaintEvent* e)
{
QPoint pt = getItemLoc(i);
QRect rcItem(pt.x(), pt.y(), m_itemSize, m_itemSize + m_itemTextHeight);
if (!rcItem.intersects(rcWnd))
{
continue;
}
// 绘制图像
HGBase_EnterLock(m_lockFront);
QImage *img = m_frontItems[i]->image;
......@@ -1038,8 +1569,8 @@ void HGImgThumb::paintEvent(QPaintEvent* e)
if (NULL != img)
{
HGRect rcShow;
HGRect rcWnd = {pt.x(), pt.y(), pt.x() + m_itemSize, pt.y() + m_itemSize};
GetShowImageRect(&rcWnd, img->width(), img->height(), rcShow);
HGRect rcItemShow = {pt.x(), pt.y(), pt.x() + m_itemSize, pt.y() + m_itemSize};
GetShowImageRect(&rcItemShow, img->width(), img->height(), rcShow);
QRect destRect(rcShow.left, rcShow.top, rcShow.right - rcShow.left, rcShow.bottom - rcShow.top);
painter.drawImage(destRect, *img);
}
......@@ -1048,19 +1579,19 @@ void HGImgThumb::paintEvent(QPaintEvent* e)
// 绘制文件名
HGChar name[256] = {0};
HGBase_GetFileName(m_frontItems[i]->fileName.toStdString().c_str(), name, 256);
QRect textRect(pt.x(), pt.y() + m_itemSize, m_itemSize, 20);
QRect textRect(pt.x(), pt.y() + m_itemSize, m_itemSize, m_itemTextHeight);
painter.drawText(textRect, Qt::AlignHCenter | Qt::AlignVCenter, name);
if (m_frontItems[i]->selected)
{
painter.fillRect(pt.x(), pt.y(), m_itemSize, m_itemSize + 20, QColor(0, 0, 0, 128));
painter.fillRect(pt.x(), pt.y(), m_itemSize, m_itemSize + m_itemTextHeight, QColor(0, 0, 0, 128));
}
}
if (-1 != m_hotItemIndex && !m_frontItems[m_hotItemIndex]->selected)
{
QPoint pt = getItemLoc(m_hotItemIndex);
painter.fillRect(pt.x(), pt.y(), m_itemSize, m_itemSize + 20, QColor(0, 0, 0, 64));
painter.fillRect(pt.x(), pt.y(), m_itemSize, m_itemSize + m_itemTextHeight, QColor(0, 0, 0, 64));
}
if (-1 != m_curItemIndex)
......@@ -1068,36 +1599,74 @@ void HGImgThumb::paintEvent(QPaintEvent* e)
QPoint pt = getItemLoc(m_curItemIndex);
QPen pen(QColor(0, 0, 0, 196));
painter.setPen(pen);
painter.drawRect(pt.x(), pt.y(), m_itemSize, m_itemSize + 20);
painter.drawRect(pt.x(), pt.y(), m_itemSize, m_itemSize + m_itemTextHeight);
}
if (m_hRoll && m_vRoll)
QColor clrScroll = qRgb(220, 220, 220);
QColor clrHScrollSlider = qRgb(180, 180, 180);
if (MouseStatus_HScrollSlider == m_mousePressStatus)
clrHScrollSlider = qRgb(100, 100, 100);
else if (MouseStatus_HScrollSlider == m_mouseMoveStatus)
clrHScrollSlider = qRgb(140, 140, 140);
QColor clrVScrollSlider = qRgb(180, 180, 180);
if (MouseStatus_VScrollSlider == m_mousePressStatus)
clrVScrollSlider = qRgb(100, 100, 100);
else if (MouseStatus_VScrollSlider == m_mouseMoveStatus)
clrVScrollSlider = qRgb(140, 140, 140);
QColor clrHScrollLeft = qRgb(220, 220, 220);
if (MouseStatus_HScrollLeft == m_mousePressStatus)
clrHScrollLeft = qRgb(140, 140, 140);
else if (MouseStatus_HScrollLeft == m_mouseMoveStatus)
clrHScrollLeft = qRgb(180, 180, 180);
QColor clrHScrollRight = qRgb(220, 220, 220);
if (MouseStatus_HScrollRight == m_mousePressStatus)
clrHScrollRight = qRgb(140, 140, 140);
else if (MouseStatus_HScrollRight == m_mouseMoveStatus)
clrHScrollRight = qRgb(180, 180, 180);
QColor clrVScrollTop = qRgb(220, 220, 220);
if (MouseStatus_VScrollTop == m_mousePressStatus)
clrVScrollTop = qRgb(140, 140, 140);
else if (MouseStatus_VScrollTop == m_mouseMoveStatus)
clrVScrollTop = qRgb(180, 180, 180);
QColor clrVScrollBottom = qRgb(220, 220, 220);
if (MouseStatus_VScrollBottom == m_mousePressStatus)
clrVScrollBottom = qRgb(140, 140, 140);
else if (MouseStatus_VScrollBottom == m_mouseMoveStatus)
clrVScrollBottom = qRgb(180, 180, 180);
if (m_hScroll && m_vScroll)
{
painter.fillRect(getHRollPos(), qRgb(196, 196, 196));
painter.fillRect(getHRollLeftPos(), qRgb(150, 150, 150));
painter.fillRect(getHRollRightPos(), qRgb(150, 150, 150));
painter.fillRect(getHRollSliderPos(), qRgb(96, 96, 96));
painter.fillRect(getHScrollLeftPos(), clrHScrollLeft);
painter.fillRect(getHScrollRightPos(), clrHScrollRight);
painter.fillRect(getHScrollPos(), clrScroll);
painter.fillRect(getHScrollSliderPos(), clrHScrollSlider);
painter.fillRect(getVRollPos(), qRgb(196, 196, 196));
painter.fillRect(getVRollTopPos(), qRgb(150, 150, 150));
painter.fillRect(getVRollBottomPos(), qRgb(150, 150, 150));
painter.fillRect(getVRollSliderPos(), qRgb(96, 96, 96));
painter.fillRect(getVScrollTopPos(), clrVScrollTop);
painter.fillRect(getVScrollBottomPos(), clrVScrollBottom);
painter.fillRect(getVScrollPos(), clrScroll);
painter.fillRect(getVScrollSliderPos(), clrVScrollSlider);
painter.fillRect(getNullRollPos(), qRgb(128, 128, 128));
painter.fillRect(getNullScrollPos(), clrScroll);
}
else if (m_hRoll && !m_vRoll)
else if (m_hScroll && !m_vScroll)
{
painter.fillRect(getHRollPos(), qRgb(196, 196, 196));
painter.fillRect(getHRollLeftPos(), qRgb(150, 150, 150));
painter.fillRect(getHRollRightPos(), qRgb(150, 150, 150));
painter.fillRect(getHRollSliderPos(), qRgb(96, 96, 96));
painter.fillRect(getHScrollLeftPos(), clrHScrollLeft);
painter.fillRect(getHScrollRightPos(), clrHScrollRight);
painter.fillRect(getHScrollPos(), clrScroll);
painter.fillRect(getHScrollSliderPos(), clrHScrollSlider);
}
else if (!m_hRoll && m_vRoll)
else if (!m_hScroll && m_vScroll)
{
painter.fillRect(getVRollPos(), qRgb(196, 196, 196));
painter.fillRect(getVRollTopPos(), qRgb(150, 150, 150));
painter.fillRect(getVRollBottomPos(), qRgb(150, 150, 150));
painter.fillRect(getVRollSliderPos(), qRgb(96, 96, 96));
painter.fillRect(getVScrollTopPos(), clrVScrollTop);
painter.fillRect(getVScrollBottomPos(), clrVScrollBottom);
painter.fillRect(getVScrollPos(), clrScroll);
painter.fillRect(getVScrollSliderPos(), clrVScrollSlider);
}
}
......@@ -1144,25 +1713,27 @@ void HGImgThumb::wheelEvent(QWheelEvent* e)
}
}
recalcShowRect(this->width(), this->height(), 10, m_type, m_hRoll, m_vRoll, m_showThumb, m_showRect);
recalcShowRect(this->width(), this->height(), m_scrollSize, m_type, m_hScroll, m_vScroll, m_showThumb, m_showRect);
QPoint pt(m_mousePosX, m_mousePosY);
m_hotItemIndex = getItem(pt);
m_hotItemIndex = getItem(pt, m_mouseMoveStatus);
Show();
}
void HGImgThumb::resizeEvent(QResizeEvent* e)
{
Q_UNUSED(e);
if (m_showThumb)
{
int showWidth = 0, showHeight = 0;
calcShowSize(this->width(), this->height(), 10, m_type, m_itemSize, m_gapWidth, (int)m_frontItems.size(),
showWidth, showHeight, m_hRoll, m_vRoll);
calcShowSize(this->width(), this->height(), m_gapSize, m_scrollSize, m_itemSize, m_itemTextHeight, m_type, (int)m_frontItems.size(),
showWidth, showHeight, m_hScroll, m_vScroll);
m_showRect.right = m_showRect.left + showWidth;
m_showRect.bottom = m_showRect.top + showHeight;
recalcShowRect(this->width(), this->height(), m_scrollSize, m_type, m_hScroll, m_vScroll, m_showThumb, m_showRect);
recalcShowRect(this->width(), this->height(), 10, m_type, m_hRoll, m_vRoll, m_showThumb, m_showRect);
QPoint pt(m_mousePosX, m_mousePosY);
m_hotItemIndex = getItem(pt);
m_hotItemIndex = getItem(pt, m_mouseMoveStatus);
}
Show();
......@@ -1582,15 +2153,15 @@ void HGImgThumb::keyPressEvent(QKeyEvent *e)
if (-1 != m_curItemIndex)
{
locateItemEx(m_curItemIndex);
recalcShowRect(this->width(), this->height(), 10, m_type, m_hRoll, m_vRoll, m_showThumb, m_showRect);
QPoint pt2(m_mousePosX, m_mousePosY);
m_hotItemIndex = getItem(pt2);
recalcShowRect(this->width(), this->height(), m_scrollSize, m_type, m_hScroll, m_vScroll, m_showThumb, m_showRect);
QPoint pt(m_mousePosX, m_mousePosY);
m_hotItemIndex = getItem(pt, m_mouseMoveStatus);
}
Show();
if (emitCurItemChange)
{
emit currentItemChanged(m_curItemIndex);
emit currItemChanged(m_curItemIndex);
qDebug("currentItemChanged m_curItemIndex=%d", m_curItemIndex);
}
if (emitItemSelectingChange)
......@@ -1602,23 +2173,25 @@ void HGImgThumb::keyPressEvent(QKeyEvent *e)
void HGImgThumb::keyReleaseEvent(QKeyEvent *e)
{
Q_UNUSED(e);
}
void HGImgThumb::focusInEvent(QFocusEvent *e)
{
Q_UNUSED(e);
}
void HGImgThumb::focusOutEvent(QFocusEvent *e)
{
Q_UNUSED(e);
}
void HGImgThumb::contextMenuEvent(QContextMenuEvent* e)
{
int index = getItem(e->pos());
emit contextMenuEvent(index);
MouseStatus mouseMoveStatus = MouseStatus_Null;
int index = getItem(e->pos(), mouseMoveStatus);
if (MouseStatus_Null == mouseMoveStatus)
emit contextMenuEvent(index);
}
void HGImgThumb::on_updateItem()
......@@ -1684,13 +2257,13 @@ QImage* HGImgThumb::createItemImage(HGImage srcImage, int itemSize)
return createItemImage(&img, itemSize);
}
void HGImgThumb::calcShowSize(int wndWidth, int wndHeight, int rollWidth, ThumbType type, int itemSize, int gapWidth, int itemCount,
int &showWidth, int &showHeight, bool &hRoll, bool &vRoll)
void HGImgThumb::calcShowSize(int wndWidth, int wndHeight, int gapSize, int scrollSize, int itemSize, int itemTextHeight, ThumbType type, int itemCount,
int &showWidth, int &showHeight, bool &hScroll, bool &vScroll)
{
showWidth = 0;
showHeight = 0;
hRoll = false;
vRoll = false;
hScroll = false;
vScroll = false;
if (itemCount <= 0)
{
......@@ -1699,44 +2272,48 @@ void HGImgThumb::calcShowSize(int wndWidth, int wndHeight, int rollWidth, ThumbT
if (ThumbType_Hori == type)
{
showWidth = gapWidth + itemCount * (itemSize + gapWidth);
showHeight = gapWidth * 2 + itemSize + 20;
showWidth = gapSize + itemCount * (itemSize + gapSize);
showHeight = gapSize * 2 + itemSize + itemTextHeight;
if (showWidth > wndWidth)
hRoll = true;
hScroll = true;
if (showHeight > wndHeight)
vRoll = true;
vScroll = true;
}
else if (ThumbType_Vert == type)
{
showWidth = gapWidth * 2 + itemSize;
showHeight = gapWidth + itemCount * (itemSize + 20 + gapWidth);
showWidth = gapSize * 2 + itemSize;
showHeight = gapSize + itemCount * (itemSize + itemTextHeight + gapSize);
if (showWidth > wndWidth)
hRoll = true;
hScroll = true;
if (showHeight > wndHeight)
vRoll = true;
vScroll = true;
}
else
{
int cols = HGMIN(itemCount, HGMAX(1, (wndWidth - gapWidth) / (itemSize + gapWidth)));
showWidth = gapWidth + cols * (itemSize + gapWidth);
int cols = HGMIN(itemCount, HGMAX(1, (wndWidth - gapSize) / (itemSize + gapSize)));
showWidth = gapSize + cols * (itemSize + gapSize);
int rows = (itemCount - 1) / cols + 1;
showHeight = gapWidth + rows * (itemSize + 20 + gapWidth);
showHeight = gapSize + rows * (itemSize + itemTextHeight + gapSize);
if (showHeight > wndHeight)
{
cols = HGMIN(itemCount, HGMAX(1, (wndWidth - rollWidth - gapWidth) / (itemSize + gapWidth)));
showWidth = gapWidth + cols * (itemSize + gapWidth);
cols = HGMIN(itemCount, HGMAX(1, (wndWidth - scrollSize - gapSize) / (itemSize + gapSize)));
showWidth = gapSize + cols * (itemSize + gapSize);
rows = (itemCount - 1) / cols + 1;
showHeight = gapWidth + rows * (itemSize + 20 + gapWidth);
showHeight = gapSize + rows * (itemSize + itemTextHeight + gapSize);
assert(showHeight > wndHeight);
vRoll = true;
vScroll = true;
if (showWidth > wndWidth - scrollSize)
hScroll = true;
}
else
{
if (showWidth > wndWidth)
hScroll = true;
}
if (showWidth > wndWidth)
hRoll = true;
}
}
void HGImgThumb::recalcShowRect(int wndWidth, int wndHeight, int rollWidth, ThumbType type, bool hRoll, bool vRoll, bool showThumb, HGRect &showRect)
void HGImgThumb::recalcShowRect(int wndWidth, int wndHeight, int scrollSize, ThumbType type, bool hScroll, bool vScroll, bool showThumb, HGRect &showRect)
{
if (!showThumb)
return;
......@@ -1747,21 +2324,21 @@ void HGImgThumb::recalcShowRect(int wndWidth, int wndHeight, int rollWidth, Thum
// 调整
if (ThumbType_Hori == type)
{
if (!vRoll) // 没有垂直滚动条
if (!vScroll) // 没有垂直滚动条
{
if (hRoll) // 有水平滚动条
showRect.top = (wndHeight - rollWidth - height) / 2;
if (hScroll) // 有水平滚动条
showRect.top = (wndHeight - scrollSize - height) / 2;
else // 没有水平滚动条
showRect.top = (wndHeight - height) / 2;
showRect.bottom = showRect.top + height;
}
else // 有垂直滚动条
{
if (hRoll) // 有水平滚动条
if (hScroll) // 有水平滚动条
{
if (showRect.bottom < wndHeight - rollWidth)
if (showRect.bottom < wndHeight - scrollSize)
{
showRect.bottom = wndHeight - rollWidth;
showRect.bottom = wndHeight - scrollSize;
showRect.top = showRect.bottom - height;
}
}
......@@ -1781,11 +2358,11 @@ void HGImgThumb::recalcShowRect(int wndWidth, int wndHeight, int rollWidth, Thum
}
}
if (vRoll) // 有垂直滚动条
if (vScroll) // 有垂直滚动条
{
if (showRect.right < wndWidth - rollWidth)
if (showRect.right < wndWidth - scrollSize)
{
showRect.right = wndWidth - rollWidth;
showRect.right = wndWidth - scrollSize;
showRect.left = showRect.right - width;
}
}
......@@ -1806,21 +2383,21 @@ void HGImgThumb::recalcShowRect(int wndWidth, int wndHeight, int rollWidth, Thum
}
else if (ThumbType_Vert == type)
{
if (!hRoll) // 没有水平滚动条
if (!hScroll) // 没有水平滚动条
{
if (vRoll) // 有垂直滚动条
showRect.left = (wndWidth - rollWidth - width) / 2;
if (vScroll) // 有垂直滚动条
showRect.left = (wndWidth - scrollSize - width) / 2;
else // 没有垂直滚动条
showRect.left = (wndWidth - width) / 2;
showRect.right = showRect.left + width;
}
else // 有水平滚动条
{
if (vRoll) // 有垂直滚动条
if (vScroll) // 有垂直滚动条
{
if (showRect.right < wndWidth - rollWidth)
if (showRect.right < wndWidth - scrollSize)
{
showRect.right = wndWidth - rollWidth;
showRect.right = wndWidth - scrollSize;
showRect.left = showRect.right - width;
}
}
......@@ -1840,11 +2417,11 @@ void HGImgThumb::recalcShowRect(int wndWidth, int wndHeight, int rollWidth, Thum
}
}
if (hRoll) // 有水平滚动条
if (hScroll) // 有水平滚动条
{
if (showRect.bottom < wndHeight - rollWidth)
if (showRect.bottom < wndHeight - scrollSize)
{
showRect.bottom = wndHeight - rollWidth;
showRect.bottom = wndHeight - scrollSize;
showRect.top = showRect.bottom - height;
}
}
......@@ -1865,11 +2442,11 @@ void HGImgThumb::recalcShowRect(int wndWidth, int wndHeight, int rollWidth, Thum
}
else
{
if (vRoll) // 有垂直滚动条
if (vScroll) // 有垂直滚动条
{
if (showRect.right < wndWidth - rollWidth)
if (showRect.right < wndWidth - scrollSize)
{
showRect.right = wndWidth - rollWidth;
showRect.right = wndWidth - scrollSize;
showRect.left = showRect.right - width;
}
}
......@@ -1888,11 +2465,11 @@ void HGImgThumb::recalcShowRect(int wndWidth, int wndHeight, int rollWidth, Thum
showRect.right = showRect.left + width;
}
if (hRoll) // 有水平滚动条
if (hScroll) // 有水平滚动条
{
if (showRect.bottom < wndHeight - rollWidth)
if (showRect.bottom < wndHeight - scrollSize)
{
showRect.bottom = wndHeight - rollWidth;
showRect.bottom = wndHeight - scrollSize;
showRect.top = showRect.bottom - height;
}
}
......@@ -1921,59 +2498,59 @@ void HGImgThumb::locateItemEx(int index)
int left = getItemLoc(index).x();
int top = getItemLoc(index).y();
int right = left + m_itemSize;
int bottom = top + m_itemSize + 20;
int bottom = top + m_itemSize + m_itemTextHeight;
if (m_vRoll)
if (m_vScroll)
{
if (right + m_gapWidth > this->width() - 10 && left > m_gapWidth)
if (left < m_gapSize && right + m_gapSize < this->width() - m_scrollSize)
{
m_showRect.left -= (right + m_gapWidth - this->width() + 10);
m_showRect.right -= (right + m_gapWidth - this->width() + 10);
m_showRect.left += (m_gapSize - left);
m_showRect.right += (m_gapSize - left);
}
else if (left < m_gapWidth)
else if (right + m_gapSize > this->width() - m_scrollSize && left > m_gapSize)
{
m_showRect.left += (m_gapWidth - left);
m_showRect.right += (m_gapWidth - left);
m_showRect.left -= (right + m_gapSize - this->width() + m_scrollSize);
m_showRect.right -= (right + m_gapSize - this->width() + m_scrollSize);
}
}
else
{
if (right + m_gapWidth > this->width() && left > m_gapWidth)
if (left < m_gapSize && right + m_gapSize < this->width())
{
m_showRect.left -= (right + m_gapWidth - this->width());
m_showRect.right -= (right + m_gapWidth - this->width());
m_showRect.left += (m_gapSize - left);
m_showRect.right += (m_gapSize - left);
}
else if (left < m_gapWidth)
else if (right + m_gapSize > this->width() && left > m_gapSize)
{
m_showRect.left += (m_gapWidth - left);
m_showRect.right += (m_gapWidth - left);
m_showRect.left -= (right + m_gapSize - this->width());
m_showRect.right -= (right + m_gapSize - this->width());
}
}
if (m_hRoll)
if (m_hScroll)
{
if (bottom + m_gapWidth > this->height() - 10 && top > m_gapWidth)
if (top < m_gapSize && bottom + m_gapSize < this->height() - m_scrollSize)
{
m_showRect.top -= (bottom + m_gapWidth - this->height() + 10);
m_showRect.bottom -= (bottom + m_gapWidth - this->height() + 10);
m_showRect.top += (m_gapSize - top);
m_showRect.bottom += (m_gapSize - top);
}
else if (top < m_gapWidth)
else if (bottom + m_gapSize > this->height() - m_scrollSize && top > m_gapSize)
{
m_showRect.top += (m_gapWidth - top);
m_showRect.bottom += (m_gapWidth - top);
m_showRect.top -= (bottom + m_gapSize - this->height() + m_scrollSize);
m_showRect.bottom -= (bottom + m_gapSize - this->height() + m_scrollSize);
}
}
else
{
if (bottom + m_gapWidth > this->height() && top > m_gapWidth)
if (top < m_gapSize && bottom + m_gapSize < this->height())
{
m_showRect.top -= (bottom + m_gapWidth - this->height());
m_showRect.bottom -= (bottom + m_gapWidth - this->height());
m_showRect.top += (m_gapSize - top);
m_showRect.bottom += (m_gapSize - top);
}
else if (top < m_gapWidth)
else if (bottom + m_gapSize > this->height() && top > m_gapSize)
{
m_showRect.top += (m_gapWidth - top);
m_showRect.bottom += (m_gapWidth - top);
m_showRect.top -= (bottom + m_gapSize - this->height());
m_showRect.bottom -= (bottom + m_gapSize - this->height());
}
}
}
......@@ -1986,19 +2563,19 @@ QPoint HGImgThumb::getItemLoc(int index)
int x, y;
if (ThumbType_Hori == m_type)
{
x = m_gapWidth + index * (m_itemSize + m_gapWidth);
y = m_gapWidth;
x = m_gapSize + index * (m_itemSize + m_gapSize);
y = m_gapSize;
}
else if (ThumbType_Vert == m_type)
{
x = m_gapWidth;
y = m_gapWidth + index * (m_itemSize + 20 + m_gapWidth); // 20为文件名显示条宽度
x = m_gapSize;
y = m_gapSize + index * (m_itemSize + m_itemTextHeight + m_gapSize);
}
else
{
int cols = (m_showRect.right - m_showRect.left - m_gapWidth) / (m_itemSize + m_gapWidth);
x = m_gapWidth + (index % cols) * (m_itemSize + m_gapWidth);
y = m_gapWidth + (index / cols) * (m_itemSize + 20 + m_gapWidth); // 20为文件名显示条宽度
int cols = (m_showRect.right - m_showRect.left - m_gapSize) / (m_itemSize + m_gapSize);
x = m_gapSize + (index % cols) * (m_itemSize + m_gapSize);
y = m_gapSize + (index / cols) * (m_itemSize + m_itemTextHeight + m_gapSize);
}
x += m_showRect.left;
......@@ -2023,7 +2600,7 @@ int HGImgThumb::getItemRow(int index)
}
else
{
int cols = (m_showRect.right - m_showRect.left - m_gapWidth) / (m_itemSize + m_gapWidth);
int cols = (m_showRect.right - m_showRect.left - m_gapSize) / (m_itemSize + m_gapSize);
row = index / cols;
}
......@@ -2046,7 +2623,7 @@ int HGImgThumb::getItemCol(int index)
}
else
{
int cols = (m_showRect.right - m_showRect.left - m_gapWidth) / (m_itemSize + m_gapWidth);
int cols = (m_showRect.right - m_showRect.left - m_gapSize) / (m_itemSize + m_gapSize);
col = index % cols;
}
......@@ -2072,7 +2649,7 @@ int HGImgThumb::getTotalRows()
}
else
{
rows = (m_showRect.bottom - m_showRect.top - m_gapWidth) / (m_itemSize + 20 + m_gapWidth);
rows = (m_showRect.bottom - m_showRect.top - m_gapSize) / (m_itemSize + m_itemTextHeight + m_gapSize);
}
return rows;
......@@ -2097,45 +2674,71 @@ int HGImgThumb::getTotalCols()
}
else
{
cols = (m_showRect.right - m_showRect.left - m_gapWidth) / (m_itemSize + m_gapWidth);
cols = (m_showRect.right - m_showRect.left - m_gapSize) / (m_itemSize + m_gapSize);
}
return cols;
}
int HGImgThumb::getItem(const QPoint &pt)
int HGImgThumb::getItem(const QPoint &pt, MouseStatus &mouseMoveStatus)
{
mouseMoveStatus = MouseStatus_Null;
if (!m_showThumb)
{
assert(0 == m_frontItems.size());
return -1;
}
bool hitHRoll = false;
bool hitVRoll = false;
bool hitNull = false;
if (m_hRoll && m_vRoll)
{
if (getHRollPos().contains(pt))
hitHRoll = true;
if (getVRollPos().contains(pt))
hitVRoll = true;
if (getNullRollPos().contains(pt))
hitNull = true;
}
else if (m_hRoll && !m_vRoll)
{
if (getHRollPos().contains(pt))
hitHRoll = true;
}
else if (!m_hRoll && m_vRoll)
if (MouseStatus_Null != m_mousePressStatus)
{
if (getVRollPos().contains(pt))
hitVRoll = true;
return -1;
}
if (hitHRoll || hitVRoll || hitNull)
if (m_hScroll && m_vScroll)
{
if (getHScrollSliderPos().contains(pt))
mouseMoveStatus = MouseStatus_HScrollSlider;
else if (getHScrollPos().contains(pt))
mouseMoveStatus = MouseStatus_HScroll;
else if (getHScrollLeftPos().contains(pt))
mouseMoveStatus = MouseStatus_HScrollLeft;
else if (getHScrollRightPos().contains(pt))
mouseMoveStatus = MouseStatus_HScrollRight;
else if (getVScrollSliderPos().contains(pt))
mouseMoveStatus = MouseStatus_VScrollSlider;
else if (getVScrollPos().contains(pt))
mouseMoveStatus = MouseStatus_VScroll;
else if (getVScrollTopPos().contains(pt))
mouseMoveStatus = MouseStatus_VScrollTop;
else if (getVScrollBottomPos().contains(pt))
mouseMoveStatus = MouseStatus_VScrollBottom;
else if (getNullScrollPos().contains(pt))
mouseMoveStatus = MouseStatus_NullScroll;
}
else if (m_hScroll && !m_vScroll)
{
if (getHScrollSliderPos().contains(pt))
mouseMoveStatus = MouseStatus_HScrollSlider;
else if (getHScrollPos().contains(pt))
mouseMoveStatus = MouseStatus_HScroll;
else if (getHScrollLeftPos().contains(pt))
mouseMoveStatus = MouseStatus_HScrollLeft;
else if (getHScrollRightPos().contains(pt))
mouseMoveStatus = MouseStatus_HScrollRight;
}
else if (!m_hScroll && m_vScroll)
{
if (getVScrollSliderPos().contains(pt))
mouseMoveStatus = MouseStatus_VScrollSlider;
else if (getVScrollPos().contains(pt))
mouseMoveStatus = MouseStatus_VScroll;
else if (getVScrollTopPos().contains(pt))
mouseMoveStatus = MouseStatus_VScrollTop;
else if (getVScrollBottomPos().contains(pt))
mouseMoveStatus = MouseStatus_VScrollBottom;
}
if (MouseStatus_Null != mouseMoveStatus)
{
return -1;
}
......@@ -2148,7 +2751,7 @@ int HGImgThumb::getItem(const QPoint &pt)
int left = ptLoc.x();
int top = ptLoc.y();
int right = left + m_itemSize;
int bottom = top + m_itemSize + 20;
int bottom = top + m_itemSize + m_itemTextHeight;
if (pt.x() >= left && pt.y() >= top && pt.x() < right && pt.y() < bottom)
{
index = i;
......@@ -2188,7 +2791,7 @@ int HGImgThumb::getInsertPos(const QPoint &pt)
for (int i = 0; i < (int)m_frontItems.size(); ++i)
{
int top = getItemLoc(i).y();
int bottom = top + m_itemSize + 20;
int bottom = top + m_itemSize + m_itemTextHeight;
if (pt.y() < (top + bottom) / 2)
{
pos = i;
......@@ -2207,14 +2810,14 @@ int HGImgThumb::getInsertPos(const QPoint &pt)
int left = getItemLoc(i).x();
int top = getItemLoc(i).y();
int right = left + m_itemSize;
int bottom = top + m_itemSize + 20;
int bottom = top + m_itemSize + m_itemTextHeight;
int row = getItemRow(i);
int col = getItemCol(i);
if (0 == col && 0 != row)
{
if (pt.y() < top - m_gapWidth)
if (pt.y() < top - m_gapSize)
{
pos = i;
break;
......@@ -2236,7 +2839,7 @@ int HGImgThumb::getInsertPos(const QPoint &pt)
for (int i = 0; i < (int)m_frontItems.size(); ++i)
{
int top = getItemLoc(i).y();
int bottom = top + m_itemSize + 20;
int bottom = top + m_itemSize + m_itemTextHeight;
if (pt.y() < (top + bottom) / 2)
{
pos = i;
......@@ -2265,155 +2868,159 @@ int HGImgThumb::findIndex(const QString &fileName)
return index;
}
QRect HGImgThumb::getHRollPos()
QRect HGImgThumb::getHScrollLeftPos()
{
assert(m_hRoll);
assert(m_hScroll);
if (!m_vRoll)
{
return QRect(0, this->height() - 10, this->width(), 10);
}
return QRect(0, this->height() - 10, this->width() - 10, 10);
return QRect(0, this->height() - m_scrollSize, m_scrollSize, m_scrollSize);
}
QRect HGImgThumb::getHRollLeftPos()
QRect HGImgThumb::getHScrollRightPos()
{
assert(m_hRoll);
assert(m_hScroll);
if (!m_vScroll)
{
return QRect(this->width() - m_scrollSize, this->height() - m_scrollSize, m_scrollSize, m_scrollSize);
}
return QRect(0, this->height() - 10, 10, 10);
return QRect(this->width() - 2 * m_scrollSize, this->height() - m_scrollSize, m_scrollSize, m_scrollSize);
}
QRect HGImgThumb::getHRollRightPos()
QRect HGImgThumb::getHScrollPos()
{
assert(m_hRoll);
assert(m_hScroll);
if (!m_vRoll)
if (!m_vScroll)
{
return QRect(this->width() - 10, this->height() - 10, 10, 10);
return QRect(m_scrollSize, this->height() - m_scrollSize, this->width() - 2 * m_scrollSize, m_scrollSize);
}
return QRect(this->width() - 20, this->height() - 10, 10, 10);
return QRect(m_scrollSize, this->height() - m_scrollSize, this->width() - 3 * m_scrollSize, m_scrollSize);
}
QRect HGImgThumb::getVRollPos()
QRect HGImgThumb::getHScrollSliderPos()
{
assert(m_vRoll);
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 = (int)((double)rollTotalLen * (-m_showRect.left) / (double)(m_showRect.right - m_showRect.left) + 0.5);
int rollRight = (int)((double)rollTotalLen * (-m_showRect.left + this->width()) / (double)(m_showRect.right - m_showRect.left) + 0.5);
int rollLen = rollRight - rollLeft;
if (rollLen < 8)
{
rollLen = 8;
rollLeft = (int)((double)(rollTotalLen - 8) * (-m_showRect.left) / (double)(m_showRect.right - m_showRect.left) + 0.5);
}
if (!m_hRoll)
rect.setX(rollLeft + m_scrollSize);
rect.setWidth(rollLen);
}
else
{
return QRect(this->width() - 10, 0, 10, this->height());
int rollTotalLen = this->width() - 3 * m_scrollSize;
int rollLeft = (int)((double)rollTotalLen * (-m_showRect.left) / (double)(m_showRect.right - m_showRect.left) + 0.5);
int rollRight = (int)((double)rollTotalLen * (-m_showRect.left + this->width() - m_scrollSize) / (double)(m_showRect.right - m_showRect.left) + 0.5);
int rollLen = rollRight - rollLeft;
if (rollLen < 8)
{
rollLen = 8;
rollLeft = (int)((double)(rollTotalLen - 8) * (-m_showRect.left) / (double)(m_showRect.right - m_showRect.left) + 0.5);
}
rect.setX(rollLeft + m_scrollSize);
rect.setWidth(rollLen);
}
return QRect(this->width() - 10, 0, 10, this->height() - 10);
return rect;
}
QRect HGImgThumb::getVRollTopPos()
QRect HGImgThumb::getVScrollTopPos()
{
assert(m_vRoll);
assert(m_vScroll);
return QRect(this->width() - 10, 0, 10, 10);
return QRect(this->width() - m_scrollSize, 0, m_scrollSize, m_scrollSize);
}
QRect HGImgThumb::getVRollBottomPos()
QRect HGImgThumb::getVScrollBottomPos()
{
assert(m_vRoll);
assert(m_vScroll);
if (!m_hRoll)
if (!m_hScroll)
{
return QRect(this->width() - 10, this->height() - 10, 10, 10);
return QRect(this->width() - m_scrollSize, this->height() - m_scrollSize, m_scrollSize, m_scrollSize);
}
return QRect(this->width() - 10, this->height() - 20, 10, 10);
return QRect(this->width() - m_scrollSize, this->height() - 2 * m_scrollSize, m_scrollSize, m_scrollSize);
}
QRect HGImgThumb::getHRollSliderPos()
QRect HGImgThumb::getVScrollPos()
{
assert(m_hRoll);
assert(m_vScroll);
QRect rect;
rect.setY(this->height() - 10);
rect.setHeight(10);
if (!m_vRoll)
{
int rollTotalLen = this->width() - 20;
int rollLeft = rollTotalLen * (-m_showRect.left) / (m_showRect.right - m_showRect.left);
int rollRight = rollTotalLen * (-m_showRect.left + this->width()) / (m_showRect.right - m_showRect.left);
int rollLen = rollRight - rollLeft;
if (rollLen < 4)
rollLen = 4;
if (rollLeft + rollLen > rollTotalLen)
rect.setX(rollTotalLen - rollLen + 10);
else
rect.setX(rollLeft + 10);
rect.setWidth(rollLen);
}
else
if (!m_hScroll)
{
int rollTotalLen = this->width() - 30;
int rollLeft = rollTotalLen * (-m_showRect.left) / (m_showRect.right - m_showRect.left);
int rollRight = rollTotalLen * (-m_showRect.left + this->width() - 10) / (m_showRect.right - m_showRect.left);
int rollLen = rollRight - rollLeft;
if (rollLen < 4)
rollLen = 4;
if (rollLeft + rollLen > rollTotalLen)
rect.setX(rollTotalLen - rollLen + 10);
else
rect.setX(rollLeft + 10);
rect.setWidth(rollLen);
return QRect(this->width() - m_scrollSize, m_scrollSize, m_scrollSize, this->height() - 2 * m_scrollSize);
}
return rect;
return QRect(this->width() - m_scrollSize, m_scrollSize, m_scrollSize, this->height() - 3 * m_scrollSize);
}
QRect HGImgThumb::getVRollSliderPos()
QRect HGImgThumb::getVScrollSliderPos()
{
assert(m_vRoll);
assert(m_vScroll);
QRect rect;
rect.setX(this->width() - 10);
rect.setWidth(10);
if (!m_hRoll)
rect.setX(this->width() - m_scrollSize);
rect.setWidth(m_scrollSize);
if (!m_hScroll)
{
int rollTotalLen = this->height() - 20;
int rollTop = rollTotalLen * (-m_showRect.top) / (m_showRect.bottom - m_showRect.top);
int rollBottom = rollTotalLen * (-m_showRect.top + this->height()) / (m_showRect.bottom - m_showRect.top);
int rollTotalLen = this->height() - 2 * m_scrollSize;
int rollTop = (int)((double)rollTotalLen * (-m_showRect.top) / (double)(m_showRect.bottom - m_showRect.top) + 0.5);
int rollBottom = (int)((double)rollTotalLen * (-m_showRect.top + this->height()) / (double)(m_showRect.bottom - m_showRect.top) + 0.5);
int rollLen = rollBottom - rollTop;
if (rollLen < 4)
rollLen = 4;
if (rollTop + rollLen > rollTotalLen)
rect.setY(rollTotalLen - rollLen + 10);
else
rect.setY(rollTop + 10);
if (rollLen < 8)
{
rollLen = 8;
rollTop = (int)((double)(rollTotalLen - 8) * (-m_showRect.top) / (double)(m_showRect.bottom - m_showRect.top) + 0.5);
}
rect.setY(rollTop + m_scrollSize);
rect.setHeight(rollLen);
}
else
{
int rollTotalLen = this->height() - 30;
int rollTop = rollTotalLen * (-m_showRect.top) / (m_showRect.bottom - m_showRect.top);
int rollBottom = rollTotalLen * (-m_showRect.top + this->height() - 10) / (m_showRect.bottom - m_showRect.top);
int rollTotalLen = this->height() - 3 * m_scrollSize;
int rollTop = (int)((double)rollTotalLen * (-m_showRect.top) / (double)(m_showRect.bottom - m_showRect.top) + 0.5);
int rollBottom = (int)((double)rollTotalLen * (-m_showRect.top + this->height() - m_scrollSize) / (double)(m_showRect.bottom - m_showRect.top) + 0.5);
int rollLen = rollBottom - rollTop;
if (rollLen < 4)
rollLen = 4;
if (rollTop + rollLen > rollTotalLen)
rect.setY(rollTotalLen - rollLen + 10);
else
rect.setY(rollTop + 10);
if (rollLen < 8)
{
rollLen = 8;
rollTop = (int)((double)(rollTotalLen - 8) * (-m_showRect.top) / (double)(m_showRect.bottom - m_showRect.top) + 0.5);
}
rect.setY(rollTop + m_scrollSize);
rect.setHeight(rollLen);
}
return rect;
}
QRect HGImgThumb::getNullRollPos()
QRect HGImgThumb::getNullScrollPos()
{
assert(m_hRoll && m_vRoll);
return QRect(this->width() - 10, this->height() - 10, 10, 10);
assert(m_hScroll && m_vScroll);
return QRect(this->width() - m_scrollSize, this->height() - m_scrollSize, m_scrollSize, m_scrollSize);
}
void HGImgThumb::Show()
{
repaint();
update();
}
void HGImgThumb::ThreadFunc(HGThread thread, HGPointer param)
......
......@@ -23,28 +23,6 @@ enum ThumbRemoveFlag
ThumbRemoveFlag_Delete = 2 // 从列表中删除,并删除文件
};
struct HGImgThumbItem
{
HGImgThumbItem()
{
image = NULL;
selected = false;
}
~HGImgThumbItem()
{
if (NULL != image)
{
delete image;
image = NULL;
}
}
QString fileName;
QImage *image;
bool selected;
};
class HGImgThumb : public QWidget
{
Q_OBJECT
......@@ -52,18 +30,22 @@ public:
HGImgThumb(QWidget* parent = nullptr);
virtual ~HGImgThumb();
enum MoveStauts
{
MoveStatus_Null = 0,
MoveStatus_HRoll = 1,
MoveStatus_VRoll = 2
};
HGResult setGapSize(int size);
HGResult setScrollSize(int size);
HGResult setItemSize(int size);
HGResult setItemTextHeight(int height);
HGResult setDefItemImage(const QImage *image);
HGResult setHScrollLeftImage(const QImage *normalImage, const QImage *hotImage, const QImage *pushImage);
HGResult setHScrollRightImage(const QImage *normalImage, const QImage *hotImage, const QImage *pushImage);
HGResult setVScrollTopImage(const QImage *normalImage, const QImage *hotImage, const QImage *pushImage);
HGResult setVScrollBottomImage(const QImage *normalImage, const QImage *hotImage, const QImage *pushImage);
HGResult setScrollImage(const QImage *image, const QRect *stretch);
HGResult setscrollSliderImage(const QImage *normalImage, const QRect *normalStretch, const QImage *hotImage, const QRect *hotStretch,
const QImage *pushImage, const QRect *pushStretch);
HGResult setType(ThumbType type);
HGResult setGap(int width);
HGResult zoomIn();
HGResult zoomOut();
HGResult setDefItemImage(const QImage *image);
HGResult getItemCount(int *count);
HGResult addItem(const QString &fileName);
......@@ -81,12 +63,50 @@ public:
HGResult locateItem(int index);
signals:
void currentItemChanged(int index);
void currItemChanged(int index);
void itemDoubleClicked(int index);
void itemCountChanged(int count);
void itemSelectingChanged();
void contextMenuEvent(int index);
private:
struct HGImgThumbItem
{
HGImgThumbItem()
{
image = NULL;
selected = false;
}
~HGImgThumbItem()
{
if (NULL != image)
{
delete image;
image = NULL;
}
}
QString fileName;
QImage *image;
bool selected;
};
enum MouseStatus
{
MouseStatus_Null = 0,
MouseStatus_HScroll,
MouseStatus_HScrollSlider,
MouseStatus_HScrollLeft,
MouseStatus_HScrollRight,
MouseStatus_VScroll,
MouseStatus_VScrollSlider,
MouseStatus_VScrollTop,
MouseStatus_VScrollBottom,
MouseStatus_NullScroll
};
protected:
virtual void mousePressEvent(QMouseEvent *e);
virtual void mouseMoveEvent(QMouseEvent* e);
......@@ -110,27 +130,30 @@ private slots:
private:
static QImage* createItemImage(const QImage *srcImage, int itemSize);
static QImage* createItemImage(HGImage srcImage, int itemSize);
static void calcShowSize(int wndWidth, int wndHeight, int rollWidth, ThumbType type, int itemSize, int gapWidth, int itemCount,
int &showWidth, int &showHeight, bool &hRoll, bool &vRoll);
static void recalcShowRect(int wndWidth, int wndHeight, int rollWidth, ThumbType type, bool hRoll, bool vRoll, bool showThumb, HGRect &showRect);
static void calcShowSize(int wndWidth, int wndHeight, int gapSize, int scrollSize, int itemSize, int itemTextHeight, ThumbType type, int itemCount,
int &showWidth, int &showHeight, bool &hScroll, bool &vScroll);
static void recalcShowRect(int wndWidth, int wndHeight, int scrollSize, ThumbType type, bool hScroll, bool vScroll, bool showThumb, HGRect &showRect);
void locateItemEx(int index);
QPoint getItemLoc(int index);
int getItemRow(int index);
int getItemCol(int index);
int getTotalRows();
int getTotalCols();
int getItem(const QPoint &pt);
int getItem(const QPoint &pt, MouseStatus &mouseMoveStatus);
int getInsertPos(const QPoint &pt);
int findIndex(const QString &fileName);
QRect getHRollPos();
QRect getHRollLeftPos();
QRect getHRollRightPos();
QRect getVRollPos();
QRect getVRollTopPos();
QRect getVRollBottomPos();
QRect getHRollSliderPos();
QRect getVRollSliderPos();
QRect getNullRollPos();
QRect getHScrollLeftPos();
QRect getHScrollRightPos();
QRect getHScrollPos();
QRect getHScrollSliderPos();
QRect getVScrollTopPos();
QRect getVScrollBottomPos();
QRect getVScrollPos();
QRect getVScrollSliderPos();
QRect getNullScrollPos();
void Show();
static void ThreadFunc(HGThread thread, HGPointer param);
......@@ -139,11 +162,23 @@ private:
HGLock m_lockBack;
HGLock m_lockItemSize;
HGEvent m_event;
ThumbType m_type;
int m_gapSize;
int m_scrollSize;
int m_itemSize;
int m_itemTextHeight;
QImage *m_defItemImage;
QImage *m_itemImage;
int m_gapWidth;
int m_itemSize;
QImage *m_hScrollLeftImage[3];
QImage *m_hScrollRightImage[3];
QImage *m_vScrollTopImage[3];
QImage *m_vScrollBottomImage[3];
QImage *m_scrollImage;
QRect m_scrollImageStretch;
QImage *m_scrollSliderImage[3];
QRect m_scrollSliderImageStretch[3];
ThumbType m_type;
int m_curItemIndex;
int m_hotItemIndex;
int m_signItemIndex;
......@@ -151,15 +186,16 @@ private:
HGThread m_thread;
std::vector<HGImgThumbItem *> m_frontItems;
std::list<QString> m_backList;
bool m_hRoll;
bool m_vRoll;
bool m_hScroll;
bool m_vScroll;
bool m_showThumb;
HGRect m_showRect;
int m_mousePosX;
int m_mousePosY;
MoveStauts m_moveStatus;
int m_beginX;
int m_beginY;
MouseStatus m_mouseMoveStatus;
MouseStatus m_mousePressStatus;
int m_mousePressBeginX;
int m_mousePressBeginY;
};
#endif /* __HGIMGTHUMB_HPP__ */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment