Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Local imagelib #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -291,3 +291,4 @@ __pycache__/
*.user
*.vcxproj
*.vcxproj.filters
*.sln
41 changes: 35 additions & 6 deletions ImageItem.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
#include "ImageItem.h"
#include <QFontMetrics>

CImageItem::CImageItem(const QString& path, QWidget* parent /*= 0*/)
CImageItem::CImageItem(const QString& path, bool isLocalFile, QWidget* parent /*= 0*/)
: QDialog(parent)
, m_isLoading(true)
, m_scale(1.0)
{
m_imageLoadThread.setImagePath(path);
ImageLib::stReadParam param(path, isLocalFile);
m_imageLoadThread.setParam(param);
connect(&m_imageLoadThread, &QThread::finished, this, &CImageItem::onImageLoaded);

setWindowFlags(windowFlags() | Qt::WindowMinMaxButtonsHint);
setWindowFlags(windowFlags() | Qt::WindowMinMaxButtonsHint | Qt::FramelessWindowHint);

resize(800, 600);
//resize(1024, 768);
}


Expand Down Expand Up @@ -39,7 +41,7 @@ void CImageItem::paintEvent(QPaintEvent *event)
}
else
{
QImage temp = m_imageLoadThread.image().scaled(size(), Qt::KeepAspectRatio);
QImage temp = m_imageLoadThread.image().scaled(size() * m_scale, Qt::KeepAspectRatio, Qt::SmoothTransformation);
painter.drawImage(QRect(rect().center().x() - temp.width() / 2, rect().center().y() - temp.height() / 2,
temp.width(), temp.height()), m_imageLoadThread.image());
}
Expand All @@ -56,7 +58,34 @@ void CImageItem::showEvent(QShowEvent *event)

void CImageItem::onImageLoaded()
{
m_isLoading = !(m_imageLoadThread.isSuccessed() && !m_imageLoadThread.image().isNull());
m_isLoading = !(m_imageLoadThread.result().isSuccess && !m_imageLoadThread.image().isNull());
update();
}

void CImageItem::mousePressEvent(QMouseEvent *event)
{
close();
}

const int kMinScale = 0.1;
const int kMaxScale = 3.0;
void CImageItem::wheelEvent(QWheelEvent *event)
{
const int i = event->delta() / 120;

m_scale = m_scale - i * 0.05;

if (m_scale < kMinScale)
{
m_scale = kMinScale;
}
else if (m_scale > kMaxScale)
{
m_scale = kMaxScale;
}

qDebug() << "scale -->" << m_scale;

update();
}

9 changes: 6 additions & 3 deletions ImageItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,28 @@

#include <QtCore>
#include <QtWidgets>
#include "ImageLoadThread.h"
#include "ImageReadThread.h"

class CImageItem : public QDialog
{
Q_OBJECT
public:
CImageItem(const QString& path, QWidget* parent = 0);
CImageItem(const QString& path, bool isLocalFile, QWidget* parent = 0);


protected:
void paintEvent(QPaintEvent *event);
void showEvent(QShowEvent *event);
void mousePressEvent(QMouseEvent *event);
void wheelEvent(QWheelEvent *event);

protected Q_SLOTS:
void onImageLoaded();

private:
CImageLoadThread m_imageLoadThread;
ImageLib::CImageReadThread m_imageLoadThread;
bool m_isLoading;
double m_scale;
};

#endif // ImageItem_h__
11 changes: 11 additions & 0 deletions ImageLib/includes/ImageMacros.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef ImageMacros_h__
#define ImageMacros_h__

namespace ImageLib
{
static const char* kVerison = "0.1.0";

static const int kInvalidSize = -1;
}

#endif // ImageMacros_h__
88 changes: 88 additions & 0 deletions ImageLib/includes/ImageReadQueue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#ifndef ImageReadQueue_h__
#define ImageReadQueue_h__

#include <QtCore>
#include <QtGui>
#include "ImageStructs.h"

namespace ImageLib
{
class CImageReadThread;
class CImageReadQueue : public QObject
{
Q_OBJECT
private:
enum ELoadState
{
Ready, //��׼����
Loading, //������
Loaded, //���سɹ�
LoadFailed, //����ʧ��
};

struct stLoader
{
stLoader()
{
state = Ready;
reader = 0;
}
stLoader(const ImageLib::stReadParam& t)
{
state = Ready;
param = t;
reader = 0;
}

ImageLib::CImageReadThread *reader;
ImageLib::stReadParam param;
ELoadState state;
QString id;
};
typedef QList<stLoader> ListLoader;

public:
CImageReadQueue(int poolSize = 10, QObject *parent = 0);
~CImageReadQueue();

public:
//
QString addLoadTask(const ImageLib::stReadParam& param);

//
void releaseTask(const QString& taskID);

//�Ƿ������������
bool isAllFinished() const;

private:
//��������һ����������
ImageLib::CImageReadThread* createImageLoader(const ImageLib::stReadParam& param);

//��ʼ��һ������
void startNextTask();

//�̳߳ظ���
int poolSize() const { return m_poolSize; }

private Q_SLOTS:
void onImageLoadFinished();
void onThreadFinished(CImageReadThread* loader);

signals:
//�������
void loadFinished(const QString& taskID, bool success, const QImage& image);


signals:
//�߳̽����źţ��ڲ�ʹ�ã��ⲿ�������ӣ�
void threadFinished(CImageReadThread* loader);

public:
QMutex m_mutex;
ListLoader m_loaders;
int m_poolSize;
};
}

#endif // ImageReadQueue_h__
44 changes: 44 additions & 0 deletions ImageLib/includes/ImageReadThread.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#ifndef ImageReadThread_h__
#define ImageReadThread_h__

#include <QtCore>
#include <QtGui>
#include "ImageStructs.h"


namespace ImageLib
{
class CImageReader;
class CImageReadThread : public QThread
{
Q_OBJECT
public:
CImageReadThread(QObject *parent = 0);
CImageReadThread(const ImageLib::stReadParam& param, QObject *parent = 0);
~CImageReadThread();

public:
//图片
QImage image() const { return m_image; }

//加载图片
void setParam(const ImageLib::stReadParam& param) { m_param = param; }

//加载结果
const ImageLib::stReadResult& result() const { return m_result; }

//id
QString id() const { return m_id; }

protected:
void run();

private:
const QString m_id;
QImage m_image;
CImageReader* m_reader;
ImageLib::stReadParam m_param;
ImageLib::stReadResult m_result;
};
}
#endif // ImageReadThread_h__
31 changes: 31 additions & 0 deletions ImageLib/includes/ImageReader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef ImageReader_h__
#define ImageReader_h__

#include <QtCore>
#include <QtGui>

#include "ImageMacros.h"
#include "ImageStructs.h"

namespace ImageLib
{
class CImageReader
{
public:
CImageReader(){}
CImageReader(const ImageLib::stReadParam& param);

public:
//加载图片
void loadImage(QImage& image, const ImageLib::stReadParam& param);
void loadImage(QImage& image);

//加载结果
const ImageLib::stReadResult& result() const { return m_result; }

private:
ImageLib::stReadParam m_param;
ImageLib::stReadResult m_result;
};
}
#endif // ImageReader_h__
79 changes: 79 additions & 0 deletions ImageLib/includes/ImageStructs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#ifndef ImageStructs_h__
#define ImageStructs_h__

#include <QtCore>
#include <QtGui>
#include "ImageMacros.h"

namespace ImageLib
{
//��ȡ����
struct stReadParam
{
stReadParam(){}
stReadParam(const QString& path, bool isLocalFile, int limit = ImageLib::kInvalidSize)
: limitSize(ImageLib::kInvalidSize)
{
if (isLocalFile)
{
fileUrl = QUrl::fromLocalFile(path);
}
else
{
fileUrl = QUrl(path);
}

limitSize = limit;
}

bool operator==(const stReadParam& other) const
{
return fileUrl == other.fileUrl;
}

QUrl fileUrl; //�ļ���url
int limitSize; //���ƴ�С
};

//��ȡ���
struct stReadResult
{
stReadResult()
: isSuccess(false)
, error(QImageReader::UnknownError)
{}

bool isSuccess; //�Ƿ�ɹ�
QImageReader::ImageReaderError error; //����
};

//��ȡ����
struct stWriteParam
{
stWriteParam(){}
stWriteParam(const QString& filePath, int limit = ImageLib::kInvalidSize)
: limitSize(ImageLib::kInvalidSize)
{
this->filePath = filePath;
this->limitSize = limit;
}

QString filePath;
int limitSize; //���ƴ�С
};


//дͼƬ���
struct stWriteResult
{
stWriteResult()
: isSuccess(false)
, error(QImageWriter::UnknownError)
{}

bool isSuccess; //�Ƿ�ɹ�
QImageWriter::ImageWriterError error; //����
};
}

#endif // ImageStructs_h__
Loading