Skip to content

Commit

Permalink
src:improve some code
Browse files Browse the repository at this point in the history
Signed-off-by: xiaoming <[email protected]>
  • Loading branch information
QQxiaoming committed Dec 28, 2022
1 parent 3eae2e6 commit 25e1bc9
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 61 deletions.
32 changes: 16 additions & 16 deletions src/ImgViewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include "TTFdecoder.h"
#include "ui_UI_ImgViewer.h"

TTFDecodeThread::TTFDecodeThread(QWidget *parent,QString ttffilename,QString TTFFormat,
TTFDecodeThread::TTFDecodeThread(QWidget *parent,const QString &ttffilename,QString TTFFormat,
int W, int H, int codepoint) :
QThread(parent) {
this->window = parent;
Expand All @@ -30,11 +30,11 @@ TTFDecodeThread::TTFDecodeThread(QWidget *parent,QString ttffilename,QString TTF
this->H = H;
this->codepoint = codepoint;
// 获取该格式的解码函数
this->decoder = TTF2RGB::ttfdecoder_map.find(TTFFormat).value();
this->decoder = ImageDecoder::ttfdecoder_map.find(TTFFormat).value();
}

void TTFDecodeThread::run() {
QList<SvgInfo> frame_RGB_list;
QList<ImageDecoder::SvgInfo> frame_RGB_list;
if(this->decoder == nullptr) {
// 未能成功获取则返回无法解码
emit finsh_signal(frame_RGB_list,nullptr);
Expand All @@ -54,7 +54,7 @@ ImgViewer::ImgViewer(QWidget *parent,QWidget *parentWindow) :
QWidget(parent),
ui(new Ui::ImgViewerWindow) {
ui->setupUi(this);
qRegisterMetaType<QList<SvgInfo>>("QList<SvgInfo>");
qRegisterMetaType<QList<ImageDecoder::SvgInfo>>("QList<SvgInfo>");
this->parentWindow = parentWindow;
setWindowTitle("loading file, please wait ....");
ui->left_PushButton->setFlat(true);
Expand All @@ -70,7 +70,7 @@ ImgViewer::~ImgViewer() {

bool ImgViewer::setFileList(QStringList filenamelist,QString TTFFormat, int W, int H, int codepoint) {
// 获取该格式的解码函数
ttfdecoder_t decoder = TTF2RGB::ttfdecoder_map.find(TTFFormat).value();
ImageDecoder::ttfdecoder_t decoder = ImageDecoder::ttfdecoder_map.find(TTFFormat).value();
if(decoder == nullptr) {
// 未能成功获取则返回无法解码
return false;
Expand All @@ -79,7 +79,7 @@ bool ImgViewer::setFileList(QStringList filenamelist,QString TTFFormat, int W, i
ui->imgViewer->setText("");
// 遍历文件列表
foreach( QString filename, filenamelist) {
QList<SvgInfo> frame_RGB_list;
QList<ImageDecoder::SvgInfo> frame_RGB_list;

// 使用获取的解码函数进行解码得到RGB的原始帧列表
frame_RGB_list = decoder(filename, W, H, codepoint);
Expand All @@ -103,7 +103,7 @@ bool ImgViewer::setFileList(QStringList filenamelist,QString TTFFormat, int W, i
}
}

void ImgViewer::reciveimgdata(QList<SvgInfo> img_RGB_list,QString filename) {
void ImgViewer::reciveimgdata(QList<ImageDecoder::SvgInfo> img_RGB_list,QString filename) {
if (!img_RGB_list.empty()) {
// img_RGB_list以及文件名存入列表
this->img_list.insert(this->img_list.end(),img_RGB_list);
Expand Down Expand Up @@ -134,7 +134,7 @@ void ImgViewer::reciveimgdata(QList<SvgInfo> img_RGB_list,QString filename) {

bool ImgViewer::setFileList_multithreading(QStringList filenamelist,QString TTFFormat, int W, int H, int codepoint) {
// 获取该格式的解码函数
ttfdecoder_t decoder = TTF2RGB::ttfdecoder_map.find(TTFFormat).value();
ImageDecoder::ttfdecoder_t decoder = ImageDecoder::ttfdecoder_map.find(TTFFormat).value();
if(decoder == nullptr) {
// 未能成功获取则返回无法解码
return false;
Expand All @@ -153,9 +153,9 @@ void ImgViewer::closeEvent(QCloseEvent *event) {
this->parentWindow->show();
event->accept();
if(!this->img_list.empty()) {
foreach(QList<SvgInfo> list,this->img_list) {
foreach(QList<ImageDecoder::SvgInfo> list,this->img_list) {
if(!list.empty()) {
foreach(SvgInfo img,list) {
foreach(ImageDecoder::SvgInfo img,list) {
delete img.src;
}
}
Expand All @@ -167,7 +167,7 @@ void ImgViewer::draw_img(QPainter *painter) {
painter->drawPixmap(this->point, this->scaled_img);
}

void ImgViewer::currentImg2scaledImg(SvgInfo &currentImg,QPixmap &scaledImg, const QSize &size) {
void ImgViewer::currentImg2scaledImg(const ImageDecoder::SvgInfo &currentImg,QPixmap &scaledImg, const QSize &size) {
QString *svgsrc = currentImg.src;
QXmlStreamReader svgXmlStreamReader(*svgsrc);
QSvgRenderer svgRender;
Expand All @@ -186,7 +186,7 @@ void ImgViewer::paintEvent(QPaintEvent *event) {
draw_img(&painter);
painter.end();
}
(void)event;
Q_UNUSED(event);
}

void ImgViewer::mouseMoveEvent(QMouseEvent *event) {
Expand All @@ -198,7 +198,7 @@ void ImgViewer::mouseMoveEvent(QMouseEvent *event) {
this->repaint();
}
}
(void)event;
Q_UNUSED(event);
}

void ImgViewer::mousePressEvent(QMouseEvent *event) {
Expand Down Expand Up @@ -230,7 +230,7 @@ void ImgViewer::mouseDoubleClickEvent(QMouseEvent *event) {
if (!this->img_list.empty()) {
if( event->button() == Qt::LeftButton) {
int list_index = this->img_list.indexOf(this->currentImg_RGB_list);
QList<SvgInfo> img_RGB_list = this->img_list[list_index];
QList<ImageDecoder::SvgInfo> img_RGB_list = this->img_list[list_index];
int img_index = img_RGB_list.indexOf(this->currentImg);
QString savefile_name = QFileDialog::getSaveFileName( this, "保存文件",
this->filelist[list_index].replace(".ttf","-") +
Expand Down Expand Up @@ -305,7 +305,7 @@ void ImgViewer::previousImg() {
if (!this->img_list.empty()) {
//得到当前显示的文件序号
int list_index = this->img_list.indexOf(this->currentImg_RGB_list);
QList<SvgInfo> img_RGB_list = this->img_list[list_index];
QList<ImageDecoder::SvgInfo> img_RGB_list = this->img_list[list_index];
//得到当前显示的图像是文件的帧序号
int img_index = img_RGB_list.indexOf(this->currentImg);

Expand Down Expand Up @@ -340,7 +340,7 @@ void ImgViewer::nextImg() {
if (!this->img_list.empty()) {
//得到当前显示的文件序号
int list_index = this->img_list.indexOf(this->currentImg_RGB_list);
QList<SvgInfo> img_RGB_list = this->img_list[list_index];
QList<ImageDecoder::SvgInfo> img_RGB_list = this->img_list[list_index];
//得到当前显示的图像是文件的帧序号
int img_index = img_RGB_list.indexOf(this->currentImg);

Expand Down
16 changes: 8 additions & 8 deletions src/ImgViewer.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,23 @@ class TTFDecodeThread : public QThread {

public:
explicit TTFDecodeThread(QWidget *parent = nullptr,
QString ttffilename = nullptr,
const QString &ttffilename = QString(),
QString TTFFormat = nullptr,
int W = 0, int H = 0, int codepoint = 0);

protected:
void run();

signals:
void finsh_signal(QList<SvgInfo> frame_RGB_list,QString str);
void finsh_signal(QList<ImageDecoder::SvgInfo> frame_RGB_list,QString str);

private:
QWidget *window;
QString ttffilename;
int W;
int H;
int codepoint;
ttfdecoder_t decoder;
ImageDecoder::ttfdecoder_t decoder;
};

class ImgViewer : public QWidget {
Expand All @@ -57,7 +57,7 @@ class ImgViewer : public QWidget {
bool setFileList_multithreading(QStringList filenamelist, QString TTFFormat, int W, int H, int codepoint);

private slots:
void reciveimgdata(QList<SvgInfo> img_RGB_list, QString filename);
void reciveimgdata(QList<ImageDecoder::SvgInfo> img_RGB_list, QString filename);
void previousImg();
void nextImg();

Expand All @@ -72,20 +72,20 @@ private slots:
void resizeEvent(QResizeEvent *event);

private:
void currentImg2scaledImg(SvgInfo &currentImg,QPixmap &QPixmap,const QSize &size);
void currentImg2scaledImg(const ImageDecoder::SvgInfo &currentImg,QPixmap &QPixmap,const QSize &size);
void draw_img(QPainter *painter);
Ui::ImgViewerWindow *ui;
QWidget *parentWindow;
bool left_click;

QList<QList<SvgInfo>> img_list;
QList<QList<ImageDecoder::SvgInfo>> img_list;
QStringList filelist;

QList<TTFDecodeThread*> decode_thread;
QList<TTFDecodeThread*> decode_thread_finsh;

QList<SvgInfo> currentImg_RGB_list;
SvgInfo currentImg;
QList<ImageDecoder::SvgInfo> currentImg_RGB_list;
ImageDecoder::SvgInfo currentImg;
QPixmap scaled_img;
QPoint point;
QPoint startPos;
Expand Down
14 changes: 7 additions & 7 deletions src/TTFdecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@
#include "TTFdecoder.h"
#include "font_to_svg.hpp"

QMap<QString, ttfdecoder_t> TTF2RGB::ttfdecoder_map = {
{"ttf", TTF2RGB::ttf},
{"ttf_verbose", TTF2RGB::ttf_verbose},
QMap<QString, ImageDecoder::ttfdecoder_t> ImageDecoder::ttfdecoder_map = {
{"ttf", ImageDecoder::ttf},
{"ttf_verbose", ImageDecoder::ttf_verbose},
};

QList<SvgInfo> TTF2RGB::ttf(QString ttffilename,int W, int H, int codepoint) {
QList<ImageDecoder::SvgInfo> ImageDecoder::ttf(const QString &ttffilename,int W, int H, int codepoint) {
return ttf_decode(ttffilename,W,H,codepoint,false);
}

QList<SvgInfo> TTF2RGB::ttf_verbose(QString ttffilename,int W, int H, int codepoint) {
QList<ImageDecoder::SvgInfo> ImageDecoder::ttf_verbose(const QString &ttffilename,int W, int H, int codepoint) {
return ttf_decode(ttffilename,W,H,codepoint,true);
}

std::string TTF2RGB::ttf_glyph_out(void *glyph, bool verbose) {
std::string ImageDecoder::ttf_glyph_out(void *glyph, bool verbose) {
font2svg::glyph *g = static_cast<font2svg::glyph *>(glyph);
if(verbose)
return g->svgheader() +
Expand All @@ -50,7 +50,7 @@ std::string TTF2RGB::ttf_glyph_out(void *glyph, bool verbose) {
g->svgfooter();
}

QList<SvgInfo> TTF2RGB::ttf_decode(QString ttffilename,int W, int H, int codepoint,bool verbose) {
QList<ImageDecoder::SvgInfo> ImageDecoder::ttf_decode(QString ttffilename,int W, int H, int codepoint,bool verbose) {
QList<SvgInfo> rgbImglist;

if(codepoint == -1) {
Expand Down
32 changes: 15 additions & 17 deletions src/TTFdecoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,23 @@
#include <QList>
#include <QMap>

class SvgInfo {
public:
bool operator==(const SvgInfo &l) const
{
if((l.src == this->src)&&(l.W == this->W)&&(l.H == this->H)&&(l.codepoint == this->codepoint)) return true;
else return false;
}
QString *src;
int W;
int H;
int codepoint;
};

typedef QList<SvgInfo> (* ttfdecoder_t)(QString ttffilename,int W, int H, int codepoint);

class TTF2RGB {
class ImageDecoder {
public:
static QList<SvgInfo> ttf(QString ttffilename,int W, int H, int codepoint);
static QList<SvgInfo> ttf_verbose(QString ttffilename,int W, int H, int codepoint);
class SvgInfo {
public:
bool operator==(const SvgInfo &l) const {
if((l.src == this->src)&&(l.W == this->W)&&(l.H == this->H)&&(l.codepoint == this->codepoint)) return true;
else return false;
}
QString *src;
int W;
int H;
int codepoint;
};
typedef QList<SvgInfo> (* ttfdecoder_t)(const QString &ttffilename,int W, int H, int codepoint);
static QList<SvgInfo> ttf(const QString &ttffilename,int W, int H, int codepoint);
static QList<SvgInfo> ttf_verbose(const QString &ttffilename,int W, int H, int codepoint);
static QMap<QString, ttfdecoder_t> ttfdecoder_map;
private:
static std::string ttf_glyph_out(void *glyph, bool verbose);
Expand Down
5 changes: 2 additions & 3 deletions src/configFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@
#include <QXmlStreamWriter>
#include "configFile.h"

ConfigFile::ConfigFile(QString path) {
ConfigFile::ConfigFile(const QString &path) :
configFilePath(path) {
config_dict.lastPath = "";
config_dict.frameSizeType = "Other";
config_dict.TTFFormat = "ttf";
config_dict.frameSize_Width = "2560";
config_dict.frameSize_Height = "1920";
config_dict.frameCodePiont = "*";

configFilePath = path;

QFileInfo fileInfo(configFilePath);
if(!fileInfo.isFile()) {
QFile file(configFilePath);
Expand Down
20 changes: 10 additions & 10 deletions src/configFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@

#include <QString>

typedef struct {
QString lastPath;
QString frameSizeType;
QString TTFFormat;
QString frameSize_Width;
QString frameSize_Height;
QString frameCodePiont;
}config_dict_t;

class ConfigFile {
public:
ConfigFile(QString path);
ConfigFile(const QString &path = QString());
~ConfigFile();

typedef struct {
QString lastPath;
QString frameSizeType;
QString TTFFormat;
QString frameSize_Width;
QString frameSize_Height;
QString frameCodePiont;
}config_dict_t;
config_dict_t config_dict;

private:
Expand Down

0 comments on commit 25e1bc9

Please sign in to comment.