432 lines
12 KiB
Plaintext
432 lines
12 KiB
Plaintext
/*
|
|
* Public.cpp
|
|
*
|
|
* Created on: Jul 16, 2013
|
|
* Author: yiyu
|
|
* QQ:1036184468
|
|
* Descriptoin:
|
|
|
|
*/
|
|
|
|
#include "Public.h"
|
|
#include <QFont>
|
|
#include <QFontMetrics>
|
|
#include <stdio.h>
|
|
|
|
#include <QFile>
|
|
#include <QFont>
|
|
#include <QImage>
|
|
#include <QTextEdit>
|
|
#include <QPainter>
|
|
#include <QDebug>
|
|
|
|
static QString FontName;
|
|
|
|
Public::Public(){
|
|
|
|
}
|
|
// "Zfull-GB";break;/
|
|
Public::~Public(){
|
|
FontName="";
|
|
}
|
|
// 20160303 : #define FONTV "WenQuanYiCN" ; FONTV1 FONTV2 "WenQuanYiNX"
|
|
//#define FONTV "DejaVuSansnx"//"DejaVu Sans"
|
|
#define FONTV "WenQuanYiCN"
|
|
//#define FONTV "WenQuanYiNX"
|
|
#define FONTV1 "WenQuanYiNX"
|
|
//#define FONTV1 "DejaVu Sans"
|
|
//#define FONTV2 "DejaVu Sans"
|
|
#define FONTV2 "WenQuanYiNX"//"WenQuanYi Micro Hei"
|
|
//"Zfull-GB"
|
|
#define FONTC "wenquanyi"
|
|
|
|
//#define FONTC "WenQuanYi"
|
|
int Public::getMinFontPixelSize(){
|
|
return 5;
|
|
}
|
|
QString Public::getFontFamily(int pixelSize){
|
|
switch(pixelSize){
|
|
|
|
#ifdef VNLG
|
|
case 5:
|
|
case 6:
|
|
case 7:
|
|
case 9:
|
|
return "biyang";break;
|
|
case 10:
|
|
case 11:
|
|
case 12:
|
|
return (FontName=="Chinese" || FontName=="English")?FONTC:FONTV;break;//return "UnDotum";break;//
|
|
//return "UnDotum";break;//Zfull-GB
|
|
case 14:case 13:case 15:
|
|
case 16:
|
|
case 24:
|
|
case 32:
|
|
return (FontName=="Chinese" || FontName=="English")?FONTC:FONTV;break;//return "UnDotum";break;//return (FontName=="Chinese")?FONTC:"Zfull-GB";break;
|
|
case 28:
|
|
//case 32:
|
|
case 36:
|
|
return //(FontName=="Chinese")?FONTC:
|
|
//FONTV1;//
|
|
(FontName=="Chinese" || FontName=="English")?FONTV2:FONTV1;break;
|
|
|
|
// "Zfull-GB";break;//return "UnDotum";break;//return (FontName=="Chinese")?FONTC:"Zfull-GB";break;
|
|
// 16点阵以上直接使用方正字体
|
|
default:
|
|
return
|
|
(FontName=="Chinese" || FontName=="English")?FONTV2:FONTV1;break;
|
|
//FONTC; //
|
|
//"WenQuanYi Micro Hei";
|
|
//"WenQuanYiMH";
|
|
#else
|
|
case 5://return "SerifL";break;
|
|
case 6:
|
|
case 7:
|
|
case 9:return "biyang";break;
|
|
default:return
|
|
FONTC;
|
|
#endif
|
|
|
|
break;//return "WenQuanYi Zen Hei"; break;
|
|
}
|
|
return FONTV2;
|
|
}
|
|
|
|
QString Public::getFontFamily(int pixelSize, int vartype){
|
|
switch(pixelSize){
|
|
case 5:
|
|
case 6:
|
|
case 7:
|
|
case 9:
|
|
{
|
|
if(vartype>19&& vartype<30)
|
|
{
|
|
return "SerifL";
|
|
}
|
|
else if(vartype>39&& vartype<50)
|
|
{
|
|
return "SerifR";
|
|
}
|
|
else if((vartype>29&& vartype<40) || vartype == 90 || vartype==91)
|
|
{
|
|
return "bangla";
|
|
}
|
|
else if((vartype>49&& vartype<60) || vartype == 100 || vartype==101)
|
|
{
|
|
return "biyangF";
|
|
}
|
|
else if((vartype>59&& vartype<70) || vartype == 110 || vartype==111)
|
|
{
|
|
return "Roman";
|
|
}
|
|
else
|
|
{
|
|
return "biyang";
|
|
}
|
|
// (vartype>19&& vartype<30)?"SerifL":
|
|
// (vartype>39&& vartype<50)?"SerifR":
|
|
// "biyang";
|
|
}
|
|
break;
|
|
default:return
|
|
FONTC;
|
|
break;//return "WenQuanYi Zen Hei"; break;
|
|
}
|
|
|
|
return FONTV2;
|
|
}
|
|
|
|
static int pixel_size[] = {5,7,9,12,16,21,24};//,28,32};
|
|
#define NUM 6
|
|
int Public::derivePixelSizeFromHeight(int value){
|
|
static int init_done;
|
|
static int height[NUM*2];
|
|
//static int heightB[NUM];
|
|
|
|
// 12 -12 16--15 24 --19 28 -22 32 --25
|
|
/*
|
|
HEIL 0 6
|
|
HEIL 1 8
|
|
HEIL 2 10
|
|
HEIL 3 13
|
|
HEIL 4 17
|
|
HEIL 5 32
|
|
HEIL 6 37
|
|
HEIL 7 42
|
|
*/
|
|
if(!init_done){
|
|
for(int i = 0; i < NUM; i++){
|
|
QFont font;
|
|
font.setFamily(Public::getFontFamily(pixel_size[i]));
|
|
font.setPixelSize(pixel_size[i]);
|
|
font.setBold(0);
|
|
height[2*i] = getNumberHeight(font);
|
|
font.setBold(1);
|
|
height[2*i+1] = getNumberHeight(font);
|
|
//qDebug()<<"HEIL"<<i<<height[2*i]<<height[2*i+1]<<font;
|
|
}
|
|
init_done = 1;
|
|
}
|
|
/*
|
|
HEIL 0 5
|
|
HEIL 1 7
|
|
HEIL 2 9
|
|
HEIL 3 12
|
|
HEIL 4 16
|
|
HEIL 5 26
|
|
HEIL 6 30
|
|
HEIL 7 34
|
|
HEIL 8 39
|
|
HEIL 9 32
|
|
* */
|
|
for(int i = 0; i < (NUM) ; i ++){
|
|
if(value <= height[2*i]){
|
|
//qDebug()<<"pixel_size"<<pixel_size[i];
|
|
return pixel_size[i];
|
|
}
|
|
if(value <= height[2*i+1]){
|
|
//qDebug()<<"pixel_size"<<pixel_size[i];
|
|
return pixel_size[i];
|
|
}
|
|
}
|
|
return pixel_size[1];
|
|
}
|
|
// 12 -12 16--15 24 --19 28 -22 32 --25
|
|
int Public::derivePixelSizeFromHeightNx(int value){
|
|
static int init_done;
|
|
static int height[sizeof(pixel_size)];
|
|
//qDebug()<<"pixel_size"<<pixel_size;
|
|
return pixel_size[1];
|
|
}
|
|
|
|
int Public::getAdjustedPixelSize(int value){
|
|
for(int i = 0; i < sizeof(pixel_size); i++){
|
|
if(value <= pixel_size[i])
|
|
return pixel_size[i];
|
|
}
|
|
return value;
|
|
}
|
|
|
|
void Public::resetPara()
|
|
{
|
|
char cmd[500];
|
|
//snprintf(cmd,sizeof(cmd),"cp /home/plg/r_conf/*.bin /home/plg/r_conf/UP.dpn /home/plg/conf/");
|
|
//system(cmd);
|
|
//snprintf(cmd,sizeof(cmd),"cp /home/plg/r_conf/UP.png /home/plg/r_conf/UP.h* /home/plg/doc/ ");
|
|
//system(cmd);
|
|
//snprintf(cmd,sizeof(cmd),"cp /home/plg/r_conf/UP1.png /home/plg/r_conf/UP1.h* /home/plg/doc/ ");
|
|
//system(cmd);
|
|
}
|
|
|
|
void Public::backupPara()
|
|
{
|
|
char cmd[500];
|
|
return ;
|
|
QFile file("/home/plg/conf/sysPara.bin");
|
|
// if(!file.exists())return ;
|
|
snprintf(cmd,sizeof(cmd),"cp /home/plg/conf/*.bin /home/plg/conf/UP.dpn /home/plg/r_conf/");
|
|
system(cmd);
|
|
snprintf(cmd,sizeof(cmd),"cp /home/plg/doc/UP.png /home/plg/doc/UP.h* /home/plg/r_conf/ ");
|
|
system(cmd);
|
|
snprintf(cmd,sizeof(cmd),"cp /home/plg/doc/UP1.png /home/plg/doc/UP1.h* /home/plg/r_conf/ ");
|
|
system(cmd);
|
|
system("mkdir /opt/bak");
|
|
system("mkdir /opt/bak/conf");
|
|
snprintf(cmd,sizeof(cmd),"cp /home/plg/QtUI /opt/bak/");
|
|
system(cmd);
|
|
system("cp /home/plg/conf/sysPara.bin /opt/bak/");
|
|
if(file.exists("/ect/init.d/rcS")){
|
|
snprintf(cmd,sizeof(cmd),"cp /ect/init.d/rcS /opt/bak/ ");
|
|
system(cmd);
|
|
}
|
|
if(file.exists("/home/plg/qt4")){
|
|
snprintf(cmd,sizeof(cmd),"cp /home/plg/qt4 /opt/bak/");
|
|
system(cmd);
|
|
}
|
|
|
|
}
|
|
|
|
QString Public::readLanguageType(){
|
|
QFile file1("/home/plg/UseEn");
|
|
QString InputTxtFileName;
|
|
char buf[256];
|
|
InputTxtFileName = "WINPY_utf8.txt";
|
|
if(file1.exists())
|
|
{
|
|
file1.open(QFile::ReadWrite);
|
|
int len=file1.readLine((char*)buf,256);
|
|
buf[len]='\0';
|
|
//qDebug()<<"language:"<<buf<<len<<language.length();
|
|
if(len>2) {
|
|
len=file1.readLine((char*)buf,256);
|
|
if(len){
|
|
buf[len]='\0';
|
|
QString s1 = buf;
|
|
int n=s1.indexOf("\r\n");
|
|
if(n)
|
|
s1 = s1.mid(0,n);
|
|
if(s1.endsWith("utf8.txt")) InputTxtFileName = s1;
|
|
else InputTxtFileName = "WINPY_utf8.txt";
|
|
}
|
|
}
|
|
if(file1.isOpen()) file1.close();
|
|
}
|
|
return InputTxtFileName;
|
|
}
|
|
|
|
QString Public::ReadLanguageName()
|
|
{
|
|
QFile file1("/home/plg/UseEn");
|
|
QString language="Chinese";
|
|
char buf[256];
|
|
|
|
if(file1.exists())
|
|
{
|
|
file1.open(QFile::ReadWrite);
|
|
int len=file1.readLine((char*)buf,256);
|
|
buf[len]='\0';
|
|
//qDebug()<<"language:"<<buf<<len<<language;
|
|
if(len>2) {
|
|
language = buf;
|
|
int n=language.indexOf("\r\n");
|
|
if(n>0)
|
|
language = language.mid(0,n);
|
|
}
|
|
if(file1.isOpen())file1.close();
|
|
}
|
|
//qDebug()<<"language2:"<<buf<<language;
|
|
// language="Chinese"; // 仅仅中文
|
|
|
|
FontName = language;
|
|
return language;
|
|
}
|
|
|
|
int Public::getNumberHeight(QFont font){
|
|
QFontMetrics fontMetrics(font);
|
|
//int width = (fontMetrics.width("0123456789"))/10 ;
|
|
int height = fontMetrics.ascent()+ fontMetrics.descent();
|
|
int width1 = (fontMetrics.width("ABH89"));
|
|
QTextEdit text1;
|
|
text1.setFont(font);
|
|
//qDebug()<<font;
|
|
text1.setText("F08H");
|
|
text1.show();
|
|
QImage image(width1,height+16,QImage::Format_ARGB32);
|
|
image.fill(0x00ffffff);
|
|
QPainter painter(&image);
|
|
text1.document()->drawContents (&painter, image.rect()); /*将text写到pic*/
|
|
text1.hide();
|
|
#if 0
|
|
QString s;
|
|
s.sprintf("/home/ss%d-%d.jpg",font.pixelSize(),font.bold()?75:50);
|
|
image.save(s);
|
|
#endif
|
|
|
|
int imgHeight = image.height();
|
|
int imgWidth = image.width();
|
|
int FirstValidRow = 0;
|
|
bool lastFlag=0;
|
|
int k,j;
|
|
for(k = 0; k <imgHeight - 1 ; k++){
|
|
for( j = 0; j < imgWidth; j ++){
|
|
QRgb rgb = image.pixel(j,k);
|
|
if((rgb&0x00ffffff) < 0x50){
|
|
FirstValidRow = k;
|
|
lastFlag=1;
|
|
break;
|
|
}
|
|
}
|
|
if(lastFlag)
|
|
break;
|
|
}
|
|
int lastValidRow = 0;
|
|
lastFlag=0;
|
|
for( k = imgHeight - 1; k > 0 ; k--){
|
|
for( j = 0; j < imgWidth; j ++){
|
|
QRgb rgb = image.pixel(j,k);
|
|
//qDebug()<<k<<QString().sprintf("%06X",rgb);
|
|
if((rgb&0x00ffffff) < 0x50){
|
|
lastValidRow = k +1;
|
|
lastFlag=1;
|
|
break;
|
|
}
|
|
}
|
|
if(lastFlag)
|
|
break;
|
|
}
|
|
height = lastValidRow-FirstValidRow;
|
|
//qDebug()<<"Var:"<<FirstValidRow<<lastValidRow<<height;
|
|
return height;
|
|
}
|
|
|
|
|
|
time_t Public::getDateFromMacro(char const *time) {
|
|
char s_month[5];
|
|
int month, day, year;
|
|
struct tm t = {0};
|
|
static const char month_names[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
|
|
|
|
sscanf(time, "%s %d %d", s_month, &day, &year);
|
|
|
|
month = (strstr(month_names, s_month)-month_names)/3;
|
|
|
|
t.tm_mon = month;
|
|
t.tm_mday = day;
|
|
t.tm_year = year - 1900;
|
|
t.tm_isdst = -1;
|
|
|
|
return mktime(&t);
|
|
}
|
|
|
|
QString Public::DateToString(char const *time){
|
|
char s_month[5];
|
|
int month, day, year;
|
|
//struct tm t = {0};
|
|
static const char month_names[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
|
|
//qDebug()<<time;
|
|
sscanf(time, "%s %d %d", s_month, &day, &year);
|
|
//qDebug()<<time<<s_month<<day<<year;
|
|
|
|
month = ((strstr(month_names, s_month)-month_names)/3)+1;
|
|
//qDebug()<<month<<s_month;
|
|
QString s;
|
|
s.sprintf("%04d-%02d-%02d",year,month,day);
|
|
return s;
|
|
}
|
|
|
|
int Public::getType(int vartype){
|
|
|
|
int tmp = vartype ;
|
|
if(tmp >= 60) tmp = vartype-60;
|
|
else if(tmp >= 50) tmp = vartype-50;
|
|
else if(tmp >= 40) tmp = vartype-40;
|
|
else if(tmp >= 30) tmp = vartype-30;
|
|
else if(tmp >= 20) tmp = vartype-20;
|
|
return tmp;
|
|
|
|
|
|
}
|
|
|
|
/*
|
|
*
|
|
#ifdef MMVAR11
|
|
// 同一行先排序: ,不同行分开排序,如何实现。
|
|
int i,j;
|
|
for(i=0;i<sysPara->status.variNum-1;i++){
|
|
bool ch_flag=0;
|
|
for(j=i+1;j<sysPara->status.variNum;j++){
|
|
if(sysPara->status.vars[j].startColPos < sysPara->status.vars[j-1].startColPos ){
|
|
int n=sysPara->status.vars[j].startRowPos;
|
|
sysPara->status.vars[j].startRowPos = sysPara->status.vars[j-1].startRowPos;
|
|
sysPara->status.vars[j-1].startRowPos = n;
|
|
n=sysPara->status.vars[j].startColPos;
|
|
sysPara->status.vars[j].startColPos = sysPara->status.vars[j-1].startColPos;
|
|
sysPara->status.vars[j-1].startColPos = n;
|
|
ch_flag =1;
|
|
}
|
|
}
|
|
if(ch_flag==0) break;
|
|
}
|
|
#endif
|
|
*/
|