HGGitLab

Commit 8256a0f4 authored by luoliangyi's avatar luoliangyi

处理拖入文件自动滚动功能

parent 68251015
...@@ -2615,8 +2615,12 @@ void HGImgThumb::dragMoveEvent(QDragMoveEvent *e) ...@@ -2615,8 +2615,12 @@ void HGImgThumb::dragMoveEvent(QDragMoveEvent *e)
qDebug("dragMoveEvent"); qDebug("dragMoveEvent");
m_dragInsertPos = getInsertPos(e->pos()); m_dragInsertPos = getInsertPos(e->pos());
if (-1 != m_dragInsertPos)
{
locateInsert(m_dragInsertPos); locateInsert(m_dragInsertPos);
recalcShowRect(this->width(), this->height(), m_scrollSize, m_type, m_hScroll, m_vScroll, m_showThumb, m_thumbRect); recalcShowRect(this->width(), this->height(), m_scrollSize, m_type, m_hScroll, m_vScroll, m_showThumb, m_thumbRect);
}
Show(); Show();
} }
...@@ -2635,6 +2639,8 @@ void HGImgThumb::dropEvent(QDropEvent *e) ...@@ -2635,6 +2639,8 @@ void HGImgThumb::dropEvent(QDropEvent *e)
m_dragInsertPos = -1; m_dragInsertPos = -1;
m_draging = false; m_draging = false;
bool emitItemCount = false;
bool emitItemChange = false;
int insertPos = getInsertPos(e->pos()); int insertPos = getInsertPos(e->pos());
QList<QUrl> urls = e->mimeData()->urls(); QList<QUrl> urls = e->mimeData()->urls();
...@@ -2667,16 +2673,15 @@ void HGImgThumb::dropEvent(QDropEvent *e) ...@@ -2667,16 +2673,15 @@ void HGImgThumb::dropEvent(QDropEvent *e)
if (pos > insertPos) if (pos > insertPos)
{ {
bool emitItemChange = false;
if (-1 != m_curItemIndex && m_curItemIndex >= insertPos) if (-1 != m_curItemIndex && m_curItemIndex >= insertPos)
{ {
m_curItemIndex = pos; m_curItemIndex += (pos - insertPos);
emitItemChange = true; emitItemChange = true;
} }
if (-1 != m_signItemIndex && m_signItemIndex >= insertPos) if (-1 != m_signItemIndex && m_signItemIndex >= insertPos)
{ {
m_signItemIndex = pos; m_signItemIndex += (pos - insertPos);
} }
int showWidth = 0, showHeight = 0; int showWidth = 0, showHeight = 0;
...@@ -2689,15 +2694,20 @@ void HGImgThumb::dropEvent(QDropEvent *e) ...@@ -2689,15 +2694,20 @@ void HGImgThumb::dropEvent(QDropEvent *e)
locateItem(pos - 1); locateItem(pos - 1);
recalcShowRect(this->width(), this->height(), m_scrollSize, m_type, m_hScroll, m_vScroll, m_showThumb, m_thumbRect); recalcShowRect(this->width(), this->height(), m_scrollSize, m_type, m_hScroll, m_vScroll, m_showThumb, m_thumbRect);
emitItemCount = true;
}
}
m_hotItemIndex = getItem(mapFromGlobal(QCursor::pos()), m_mouseMoveStatus); m_hotItemIndex = getItem(mapFromGlobal(QCursor::pos()), m_mouseMoveStatus);
Show(); Show();
if (emitItemCount)
{
emit itemCountChanged(m_frontItems.size()); emit itemCountChanged(m_frontItems.size());
}
if (emitItemChange) if (emitItemChange)
{ {
emit currItemChanged(m_curItemIndex); emit currItemChanged(m_curItemIndex);
} }
}
}
} }
void HGImgThumb::on_updateItem() void HGImgThumb::on_updateItem()
...@@ -3105,59 +3115,63 @@ void HGImgThumb::locateItem(int index) ...@@ -3105,59 +3115,63 @@ void HGImgThumb::locateItem(int index)
if (m_vScroll) if (m_vScroll)
{ {
if (left < m_gapSize && right + (m_gapSize - left) <= (this->width() - m_scrollSize)) int gap = m_gapSize;
if (left < gap && right + (gap - left) <= (this->width() - m_scrollSize))
{ {
m_thumbRect.left += (m_gapSize - left); m_thumbRect.left += (gap - left);
m_thumbRect.right += (m_gapSize - left); m_thumbRect.right += (gap - left);
} }
else if (right + m_gapSize > (this->width() - m_scrollSize) else if (right > (this->width() - m_scrollSize - gap)
&& left - (right + m_gapSize - this->width() + m_scrollSize) >= m_gapSize) && left - (right - this->width() + m_scrollSize + gap) >= 0)
{ {
m_thumbRect.left -= (right + m_gapSize - this->width() + m_scrollSize); m_thumbRect.left -= (right - this->width() + m_scrollSize + gap);
m_thumbRect.right -= (right + m_gapSize - this->width() + m_scrollSize); m_thumbRect.right -= (right - this->width() + m_scrollSize + gap);
} }
} }
else else
{ {
if (left < m_gapSize && right + (m_gapSize - left) <= this->width()) int gap = m_gapSize;
if (left < gap && right + (gap - left) <= this->width())
{ {
m_thumbRect.left += (m_gapSize - left); m_thumbRect.left += (gap - left);
m_thumbRect.right += (m_gapSize - left); m_thumbRect.right += (gap - left);
} }
else if (right + m_gapSize > this->width() else if (right > (this->width() - gap)
&& left - (right + m_gapSize - this->width()) >= m_gapSize) && left - (right - this->width() + gap) >= 0)
{ {
m_thumbRect.left -= (right + m_gapSize - this->width()); m_thumbRect.left -= (right - this->width() + gap);
m_thumbRect.right -= (right + m_gapSize - this->width()); m_thumbRect.right -= (right - this->width() + gap);
} }
} }
if (m_hScroll) if (m_hScroll)
{ {
if (top < m_gapSize && bottom + (m_gapSize - top) <= (this->height() - m_scrollSize)) int gap = m_gapSize;
if (top < gap && bottom + (gap - top) <= (this->height() - m_scrollSize))
{ {
m_thumbRect.top += (m_gapSize - top); m_thumbRect.top += (gap - top);
m_thumbRect.bottom += (m_gapSize - top); m_thumbRect.bottom += (gap - top);
} }
else if (bottom + m_gapSize > (this->height() - m_scrollSize) else if (bottom > (this->height() - m_scrollSize - gap)
&& top - (bottom + m_gapSize - this->height() + m_scrollSize) >= m_gapSize) && top - (bottom - this->height() + m_scrollSize + gap) >= 0)
{ {
m_thumbRect.top -= (bottom + m_gapSize - this->height() + m_scrollSize); m_thumbRect.top -= (bottom - this->height() + m_scrollSize + gap);
m_thumbRect.bottom -= (bottom + m_gapSize - this->height() + m_scrollSize); m_thumbRect.bottom -= (bottom - this->height() + m_scrollSize + gap);
} }
} }
else else
{ {
if (top < m_gapSize && bottom + (m_gapSize - top) <= this->height()) int gap = m_gapSize;
if (top < gap && bottom + (gap - top) <= this->height())
{ {
m_thumbRect.top += (m_gapSize - top); m_thumbRect.top += (gap - top);
m_thumbRect.bottom += (m_gapSize - top); m_thumbRect.bottom += (gap - top);
} }
else if (bottom + m_gapSize > this->height() else if (bottom > (this->height() - gap)
&& top - (bottom + m_gapSize - this->height()) >= m_gapSize) && top - (bottom - this->height() + gap) >= 0)
{ {
m_thumbRect.top -= (bottom + m_gapSize - this->height()); m_thumbRect.top -= (bottom - this->height() + gap);
m_thumbRect.bottom -= (bottom + m_gapSize - this->height()); m_thumbRect.bottom -= (bottom - this->height() + gap);
} }
} }
} }
...@@ -3206,59 +3220,63 @@ void HGImgThumb::locateInsert(int pos) ...@@ -3206,59 +3220,63 @@ void HGImgThumb::locateInsert(int pos)
if (m_vScroll) if (m_vScroll)
{ {
if (left < m_gapSize && right + (m_gapSize - left) <= (this->width() - m_scrollSize)) int gap = m_gapSize + m_itemSize;
if (left < gap && right + (gap - left) <= (this->width() - m_scrollSize))
{ {
m_thumbRect.left += (m_gapSize - left); m_thumbRect.left += (gap - left);
m_thumbRect.right += (m_gapSize - left); m_thumbRect.right += (gap - left);
} }
else if (right + m_gapSize > (this->width() - m_scrollSize) else if (right > (this->width() - m_scrollSize - gap)
&& left - (right + m_gapSize - this->width() + m_scrollSize) >= m_gapSize) && left - (right - this->width() + m_scrollSize + gap) >= 0)
{ {
m_thumbRect.left -= (right + m_gapSize - this->width() + m_scrollSize); m_thumbRect.left -= (right - this->width() + m_scrollSize + gap);
m_thumbRect.right -= (right + m_gapSize - this->width() + m_scrollSize); m_thumbRect.right -= (right - this->width() + m_scrollSize + gap);
} }
} }
else else
{ {
if (left < m_gapSize && right + (m_gapSize - left) <= this->width()) int gap = m_gapSize + m_itemSize;
if (left < gap && right + (gap - left) <= this->width())
{ {
m_thumbRect.left += (m_gapSize - left); m_thumbRect.left += (gap - left);
m_thumbRect.right += (m_gapSize - left); m_thumbRect.right += (gap - left);
} }
else if (right + m_gapSize > this->width() else if (right > (this->width() - gap)
&& left - (right + m_gapSize - this->width()) >= m_gapSize) && left - (right - this->width() + gap) >= 0)
{ {
m_thumbRect.left -= (right + m_gapSize - this->width()); m_thumbRect.left -= (right - this->width() + gap);
m_thumbRect.right -= (right + m_gapSize - this->width()); m_thumbRect.right -= (right - this->width() + gap);
} }
} }
if (m_hScroll) if (m_hScroll)
{ {
if (top < m_gapSize && bottom + (m_gapSize - top) <= (this->height() - m_scrollSize)) int gap = m_gapSize + (m_itemSize + m_itemTextHeight);
if (top < gap && bottom + (gap - top) <= (this->height() - m_scrollSize))
{ {
m_thumbRect.top += (m_gapSize - top); m_thumbRect.top += (gap - top);
m_thumbRect.bottom += (m_gapSize - top); m_thumbRect.bottom += (gap - top);
} }
else if (bottom + m_gapSize > (this->height() - m_scrollSize) else if (bottom > (this->height() - m_scrollSize - gap)
&& top - (bottom + m_gapSize - this->height() + m_scrollSize) >= m_gapSize) && top - (bottom - this->height() + m_scrollSize + gap) >= 0)
{ {
m_thumbRect.top -= (bottom + m_gapSize - this->height() + m_scrollSize); m_thumbRect.top -= (bottom - this->height() + m_scrollSize + gap);
m_thumbRect.bottom -= (bottom + m_gapSize - this->height() + m_scrollSize); m_thumbRect.bottom -= (bottom - this->height() + m_scrollSize + gap);
} }
} }
else else
{ {
if (top < m_gapSize && bottom + (m_gapSize - top) <= this->height()) int gap = m_gapSize + (m_itemSize + m_itemTextHeight);
if (top < gap && bottom + (gap - top) <= this->height())
{ {
m_thumbRect.top += (m_gapSize - top); m_thumbRect.top += (gap - top);
m_thumbRect.bottom += (m_gapSize - top); m_thumbRect.bottom += (gap - top);
} }
else if (bottom + m_gapSize > this->height() else if (bottom > (this->height() - gap)
&& top - (bottom + m_gapSize - this->height()) >= m_gapSize) && top - (bottom - this->height() + gap) >= 0)
{ {
m_thumbRect.top -= (bottom + m_gapSize - this->height()); m_thumbRect.top -= (bottom - this->height() + gap);
m_thumbRect.bottom -= (bottom + m_gapSize - this->height()); m_thumbRect.bottom -= (bottom - this->height() + gap);
} }
} }
} }
...@@ -3318,23 +3336,11 @@ void HGImgThumb::getInsertLoc(int pos, QPoint &pt1, QPoint &pt2) ...@@ -3318,23 +3336,11 @@ void HGImgThumb::getInsertLoc(int pos, QPoint &pt1, QPoint &pt2)
x2 = x1 + m_itemSize; x2 = x1 + m_itemSize;
} }
else else
{
int cols = getTotalCols();
if (cols > 1)
{ {
x1 = x2 = pt.x() - m_gapSize / 2; x1 = x2 = pt.x() - m_gapSize / 2;
y1 = pt.y(); y1 = pt.y();
y2 = y1 + m_itemSize + m_itemTextHeight; y2 = y1 + m_itemSize + m_itemTextHeight;
} }
else
{
assert(1 == cols);
y1 = y2 = pt.y() - m_gapSize / 2;
x1 = pt.x();
x2 = x1 + m_itemSize;
}
}
} }
else else
{ {
...@@ -3354,23 +3360,11 @@ void HGImgThumb::getInsertLoc(int pos, QPoint &pt1, QPoint &pt2) ...@@ -3354,23 +3360,11 @@ void HGImgThumb::getInsertLoc(int pos, QPoint &pt1, QPoint &pt2)
x2 = x1 + m_itemSize; x2 = x1 + m_itemSize;
} }
else else
{
int cols = getTotalCols();
if (cols > 1)
{ {
x1 = x2 = pt.x() + m_itemSize + m_gapSize / 2; x1 = x2 = pt.x() + m_itemSize + m_gapSize / 2;
y1 = pt.y(); y1 = pt.y();
y2 = y1 + m_itemSize + m_itemTextHeight; y2 = y1 + m_itemSize + m_itemTextHeight;
} }
else
{
assert(1 == cols);
y1 = y2 = pt.y() + m_itemSize + m_itemTextHeight + m_gapSize / 2;
x1 = pt.x();
x2 = x1 + m_itemSize;
}
}
} }
pt1.setX(x1); pt1.setX(x1);
...@@ -3475,6 +3469,35 @@ int HGImgThumb::getTotalCols() ...@@ -3475,6 +3469,35 @@ int HGImgThumb::getTotalCols()
return cols; return cols;
} }
int HGImgThumb::getRowCount(int row)
{
assert(row >= 0 && row < getTotalRows());
int count;
if (ThumbType_Hori == m_type)
{
count = m_frontItems.size();
}
else if (ThumbType_Vert == m_type)
{
count = 1;
}
else
{
int cols = round((m_thumbRect.right - m_thumbRect.left - m_gapSize) / (m_itemSize + m_gapSize));
if ((row + 1) * cols <= (int)m_frontItems.size())
{
count = cols;
}
else
{
count = (int)m_frontItems.size() - row * cols;
}
}
return count;
}
int HGImgThumb::getItem(const QPoint &pt, MouseStatus &mouseStatus) int HGImgThumb::getItem(const QPoint &pt, MouseStatus &mouseStatus)
{ {
mouseStatus = MouseStatus_Null; mouseStatus = MouseStatus_Null;
...@@ -3559,6 +3582,56 @@ int HGImgThumb::getItem(const QPoint &pt, MouseStatus &mouseStatus) ...@@ -3559,6 +3582,56 @@ int HGImgThumb::getItem(const QPoint &pt, MouseStatus &mouseStatus)
int HGImgThumb::getInsertPos(const QPoint &pt) int HGImgThumb::getInsertPos(const QPoint &pt)
{ {
MouseStatus mouseStatus = MouseStatus_Null;
if (m_hScroll && m_vScroll)
{
if (getHScrollSliderPos().contains(pt))
mouseStatus = MouseStatus_HScrollSlider;
else if (getHScrollPos().contains(pt))
mouseStatus = MouseStatus_HScroll;
else if (getHScrollLeftPos().contains(pt))
mouseStatus = MouseStatus_HScrollLeft;
else if (getHScrollRightPos().contains(pt))
mouseStatus = MouseStatus_HScrollRight;
else if (getVScrollSliderPos().contains(pt))
mouseStatus = MouseStatus_VScrollSlider;
else if (getVScrollPos().contains(pt))
mouseStatus = MouseStatus_VScroll;
else if (getVScrollTopPos().contains(pt))
mouseStatus = MouseStatus_VScrollTop;
else if (getVScrollBottomPos().contains(pt))
mouseStatus = MouseStatus_VScrollBottom;
else if (getNullScrollPos().contains(pt))
mouseStatus = MouseStatus_NullScroll;
}
else if (m_hScroll && !m_vScroll)
{
if (getHScrollSliderPos().contains(pt))
mouseStatus = MouseStatus_HScrollSlider;
else if (getHScrollPos().contains(pt))
mouseStatus = MouseStatus_HScroll;
else if (getHScrollLeftPos().contains(pt))
mouseStatus = MouseStatus_HScrollLeft;
else if (getHScrollRightPos().contains(pt))
mouseStatus = MouseStatus_HScrollRight;
}
else if (!m_hScroll && m_vScroll)
{
if (getVScrollSliderPos().contains(pt))
mouseStatus = MouseStatus_VScrollSlider;
else if (getVScrollPos().contains(pt))
mouseStatus = MouseStatus_VScroll;
else if (getVScrollTopPos().contains(pt))
mouseStatus = MouseStatus_VScrollTop;
else if (getVScrollBottomPos().contains(pt))
mouseStatus = MouseStatus_VScrollBottom;
}
if (MouseStatus_Null != mouseStatus)
{
return -1;
}
if (!m_showThumb) if (!m_showThumb)
{ {
assert(0 == m_frontItems.size()); assert(0 == m_frontItems.size());
...@@ -3600,52 +3673,39 @@ int HGImgThumb::getInsertPos(const QPoint &pt) ...@@ -3600,52 +3673,39 @@ int HGImgThumb::getInsertPos(const QPoint &pt)
} }
else else
{ {
int rows = getTotalRows();
int cols = getTotalCols(); int cols = getTotalCols();
if (cols > 1)
{ int row = rows;
pos = (int)m_frontItems.size(); for (int i = 0; i < rows; ++i)
for (int i = 0; i < (int)m_frontItems.size(); ++i)
{ {
QPoint pt2 = getItemLoc(i); QPoint pt2 = getItemLoc(cols * i);
int left = pt2.x();
int top = pt2.y(); int top = pt2.y();
int right = left + m_itemSize;
int bottom = top + m_itemSize + m_itemTextHeight; int bottom = top + m_itemSize + m_itemTextHeight;
if (pt.y() < bottom + m_gapSize / 2)
int row = getItemRow(i);
int col = getItemCol(i);
if (0 == col && 0 != row)
{ {
if (pt.y() < top - m_gapSize) row = i;
{
pos = i;
break; break;
} }
} }
if (pt.x() < right && pt.y() < bottom) if (row == rows)
{ {
pos = i; pos = (int)m_frontItems.size();
break;
}
}
} }
else else
{ {
assert(1 == cols); pos = row * cols + getRowCount(row);
for (int i = 0; i < getRowCount(row); ++i)
pos = (int)m_frontItems.size();
for (int i = 0; i < (int)m_frontItems.size(); ++i)
{ {
QPoint pt2 = getItemLoc(i); QPoint pt2 = getItemLoc(row * cols + i);
int top = pt2.y(); int left = pt2.x();
int bottom = top + m_itemSize + m_itemTextHeight; int right = left + m_itemSize;
if (pt.y() < (top + bottom) / 2) if (pt.x() < (left + right) / 2)
{ {
pos = i; pos = row * cols + i;
break; break;
} }
} }
......
...@@ -161,6 +161,7 @@ private: ...@@ -161,6 +161,7 @@ private:
int getItemCol(int index); int getItemCol(int index);
int getTotalRows(); int getTotalRows();
int getTotalCols(); int getTotalCols();
int getRowCount(int row); // 获取每行的个数
int getItem(const QPoint &pt, MouseStatus &mouseStatus); int getItem(const QPoint &pt, MouseStatus &mouseStatus);
int getInsertPos(const QPoint &pt); int getInsertPos(const QPoint &pt);
int findIndex(const QString &fileName); int findIndex(const QString &fileName);
......
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