HGGitLab

Commit b54a7a51 authored by luoliangyi's avatar luoliangyi

实现缩略图的简单功能

parent 0e00a507
......@@ -6,6 +6,7 @@ MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, m_view(new HGImgView(this))
, m_thumb(new HGImgThumb(this))
{
ui->setupUi(this);
// 隐藏centralwidget
......@@ -33,13 +34,26 @@ MainWindow::MainWindow(QWidget *parent)
ui->toolBar->addSeparator();
ui->toolBar->addAction(ui->act_help);
m_view->resize(600, 500);
m_view->move(100, 100);
//m_view->move(0, 100);
//m_view->resize(400, height() - 150);
m_thumb->move(00, 100);
m_thumb->resize(800, height() - 150);
HGImage img = NULL;
HGImgFmt_LoadImage("D:\\1.jpg", 0, NULL, HGBASE_IMGTYPE_RGB, HGBASE_IMGORIGIN_TOP, &img);
m_view->addImage(img);
HGBase_DestroyImage(img);
QImage img2("D:\\image.png");
m_thumb->setDefItemImage(&img2);
m_thumb->setType(ThumbType_Grid);
for (int i = 0; i < 1000; ++i)
{
char fileName[256];
sprintf(fileName, "D:\\Pictures\\%d.jpg", i);
m_thumb->addItem(fileName);
}
}
MainWindow::~MainWindow()
......
......@@ -45,5 +45,6 @@ private slots:
private:
Ui::MainWindow *ui;
HGImgView *m_view;
HGImgThumb *m_thumb;
};
#endif // MAINWINDOW_H
This diff is collapsed.
......@@ -7,7 +7,14 @@
#include "../base/HGEvent.h"
#include "../base/HGThread.h"
#include "../base/HGImage.h"
#include <QListWidget>
#include <QWidget>
enum ThumbType
{
ThumbType_Hori = 0, // 水平, item垂直居中
ThumbType_Vert = 1, // 垂直, item水平居中
ThumbType_Grid = 2 // 网格, 可以放大缩小
};
enum ThumbRemoveFlag
{
......@@ -18,18 +25,36 @@ enum ThumbRemoveFlag
struct HGImgThumbItem
{
HGImgThumbItem()
{
image = NULL;
selected = false;
}
~HGImgThumbItem()
{
delete image;
image = NULL;
}
QString fileName;
QListWidgetItem *item;
QImage *image;
bool selected;
};
class HGImgThumb : public QListWidget
class HGImgThumb : public QWidget
{
Q_OBJECT
public:
HGImgThumb(QWidget* parent = nullptr);
virtual ~HGImgThumb();
HGResult setDefItemImage(HGImage image);
HGResult setType(ThumbType type);
HGResult setGap(int width);
HGResult zoomIn(const HGPoint *pCenter);
HGResult zoomOut(const HGPoint *pCenter);
HGResult setDefItemImage(const QImage *image);
HGResult getItemCount(int *count);
HGResult addItem(const QString &fileName);
HGResult insertItem(const QString &fileName, int pos);
......@@ -44,21 +69,68 @@ public:
HGResult refreshItem(int index);
HGResult refreshAllItems();
signals:
void currentItemChanged(int index);
void itemClicked(int index);
void itemDoubleClicked(int index);
void itemCountChanged(int count);
void itemSelectingChanged();
void contextMenuEvent(int index);
protected:
virtual void mousePressEvent(QMouseEvent *e);
virtual void mouseMoveEvent(QMouseEvent* e);
virtual void mouseReleaseEvent(QMouseEvent *e);
virtual void enterEvent(QEvent *e);
virtual void leaveEvent(QEvent *e);
virtual void paintEvent(QPaintEvent* e);
virtual void wheelEvent(QWheelEvent* e);
virtual void resizeEvent(QResizeEvent* e);
virtual void keyPressEvent(QKeyEvent *e);
virtual void keyReleaseEvent(QKeyEvent *e);
virtual void focusInEvent(QFocusEvent *e);
virtual void focusOutEvent(QFocusEvent *e);
virtual void contextMenuEvent(QContextMenuEvent* e);
signals:
void updateItem();
private slots:
void on_updateItem();
private:
static QIcon createIcon(HGImage image);
static QImage* createItemImage(const QImage *srcImage, int itemSize);
static QImage* createItemImage(HGImage srcImage, int itemSize);
void updateShowRect();
bool wnd2ShowRect(QPoint &pt);
bool showRect2Wnd(QPoint &pt);
bool getItemLoc(int index, QPoint &pt, int &row, int &col);
int getItem(const QPoint &pt);
int getInsertPos(const QPoint &pt);
int findIndex(const QString &fileName);
int findIndex(const QListWidgetItem *item);
void Show();
static void ThreadFunc(HGThread thread, HGPointer param);
private:
HGLock m_lockFront;
HGLock m_lockBack;
HGLock m_lockItemSize;
HGEvent m_event;
ThumbType m_type;
QImage *m_defItemImage;
QImage *m_itemImage;
int m_gapWidth;
int m_itemSize;
int m_curItemIndex;
int m_hotItemIndex;
int m_signItemIndex;
bool m_stopThread;
HGThread m_thread;
QIcon m_defItemIcon;
std::vector<HGImgThumbItem> m_frontItems;
std::list<QString> m_backList;
bool m_showThumb;
HGRect m_showRect;
int m_mousePosX;
int m_mousePosY;
};
#endif /* __HGIMGTHUMB_HPP__ */
......@@ -374,6 +374,25 @@ HGResult HGImgView::showColorInfo(bool show)
return HGBASE_ERR_OK;
}
void HGImgView::mousePressEvent(QMouseEvent* e)
{
assert(0 == m_operate);
if (NULL == m_image || !m_showImage)
{
return;
}
HGRect rcWnd = {0, 0, this->width(), this->height()};
if ((m_showRect.left - (float)rcWnd.left < -1e-3) || (m_showRect.right - (float)rcWnd.right > 1e-3)
|| (m_showRect.top - (float)rcWnd.top < -1e-3) || (m_showRect.bottom - (float)rcWnd.bottom > 1e-3))
{
m_operate = 1;
m_beginX = e->pos().x();
m_beginY = e->pos().y();
updateCursor();
}
}
void HGImgView::mouseMoveEvent(QMouseEvent* e)
{
if (1 == m_operate)
......@@ -458,25 +477,6 @@ void HGImgView::mouseMoveEvent(QMouseEvent* e)
updateCursor();
}
void HGImgView::mousePressEvent(QMouseEvent* e)
{
assert(0 == m_operate);
if (NULL == m_image || !m_showImage)
{
return;
}
HGRect rcWnd = {0, 0, this->width(), this->height()};
if ((m_showRect.left - (float)rcWnd.left < -1e-3) || (m_showRect.right - (float)rcWnd.right > 1e-3)
|| (m_showRect.top - (float)rcWnd.top < -1e-3) || (m_showRect.bottom - (float)rcWnd.bottom > 1e-3))
{
m_operate = 1;
m_beginX = e->pos().x();
m_beginY = e->pos().y();
updateCursor();
}
}
void HGImgView::mouseReleaseEvent(QMouseEvent* e)
{
Q_UNUSED(e);
......@@ -487,6 +487,17 @@ void HGImgView::mouseReleaseEvent(QMouseEvent* e)
updateCursor();
}
void HGImgView::enterEvent(QEvent *e)
{
}
void HGImgView::leaveEvent(QEvent *e)
{
m_mousePosX = -1;
m_mousePosY = -1;
}
void HGImgView::paintEvent(QPaintEvent* e)
{
Q_UNUSED(e);
......
......@@ -35,9 +35,11 @@ signals:
void scaleChanged(double scale);
protected:
virtual void mouseMoveEvent(QMouseEvent* e);
virtual void mousePressEvent(QMouseEvent *e);
virtual void mouseMoveEvent(QMouseEvent* e);
virtual void mouseReleaseEvent(QMouseEvent *e);
virtual void enterEvent(QEvent *e);
virtual void leaveEvent(QEvent *e);
virtual void paintEvent(QPaintEvent* e);
virtual void wheelEvent(QWheelEvent* e);
virtual void resizeEvent(QResizeEvent* e);
......
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