1795 lines
66 KiB
C++
1795 lines
66 KiB
C++
#include "TextEditOption.h"
|
|
#include "ui_texteditoption.h"
|
|
#include "PicWidget.h"
|
|
#include "scene/picscene.h"
|
|
#include "SectionalCounting/sectionalmanager.h"
|
|
#include <QFileDialog>
|
|
#include <QDebug>
|
|
#include <QInputDialog>
|
|
#include <QDir>
|
|
#include <QDesktopServices>
|
|
#include <QUrl>
|
|
#include <QFile>
|
|
#include <QIODevice>
|
|
#include <QTextDocumentWriter>
|
|
#include <QFontDatabase>
|
|
#include <QSize>
|
|
#include <QPainter>
|
|
//#include <QPaintEngine>
|
|
#include <stdlib.h>
|
|
#include <QFontMetrics>
|
|
#include <QChar>
|
|
#include <QFileInfo>
|
|
#include <QTextCodec>
|
|
#include <QTextBlockFormat>
|
|
#include <QTextBlock>
|
|
#include <QDateTime>
|
|
#include "Public.h"
|
|
#include "myinclude.h"
|
|
#include <unistd.h>
|
|
#include "QBarCode.h"
|
|
#include "dlgQrCode.h"
|
|
#include<string>
|
|
#include<iostream>
|
|
TextEditOption::TextEditOption(QWidget *parent) :
|
|
QWidget(parent),
|
|
ui(new Ui::TextEditOption)
|
|
{
|
|
ui->setupUi(this);
|
|
fileDialog = 0;
|
|
fileName = "";
|
|
textEdit = 0;
|
|
newFileAppending = false;
|
|
dialogFileOperation = new DialogFileOperation(0);
|
|
dialogInsertVari = new DialogInsertVari(0);
|
|
dialogFontSize = new DialogFontSize(0);
|
|
dialogLineSpace = new DialogLineSpace(0);
|
|
editMode=0;
|
|
#ifdef _2DCODE_
|
|
qrcode = new dlgQrCode(this);
|
|
barcode= new QBarCode();
|
|
connect(barcode,SIGNAL(onBarCodeClose()),this,SLOT(onBarCodeClose()));
|
|
connect(qrcode,SIGNAL(onQRCodeCloseNow()),this,SLOT(onQRCodeClose()));
|
|
|
|
//专门处理网络发送的二维码
|
|
connect(qrcode,SIGNAL(sig_updateNetworkQR()),this,SLOT(slt_updateNetworkQR()));
|
|
#endif
|
|
//textEdit->setFontFamily(Public::getFontFamily(12));
|
|
//textEdit->clear();
|
|
#ifdef MYSAVEFILEDLG
|
|
dialogSaveFile =new DialogSaveFile(0);
|
|
#endif
|
|
|
|
sceneEditItem = false;
|
|
}
|
|
|
|
TextEditOption::~TextEditOption()
|
|
{
|
|
delete ui;
|
|
if(fileDialog)
|
|
delete fileDialog;
|
|
if(dialogInsertVari)
|
|
delete dialogInsertVari;
|
|
if(dialogFontSize)
|
|
delete dialogFontSize;
|
|
if(dialogLineSpace)
|
|
delete dialogLineSpace;
|
|
#if _2DCODE_
|
|
if(qrcode)
|
|
delete qrcode;
|
|
#endif
|
|
//free(dlgQrCode);
|
|
#ifdef MYSAVEFILEDLG
|
|
if(dialogSaveFile) delete dialogSaveFile;
|
|
if(dialogFileOperation) delete dialogFileOperation;
|
|
#endif
|
|
}
|
|
|
|
void TextEditOption::enableButtonSave(){
|
|
if(!ui->bottonSave->isEnabled())
|
|
ui->bottonSave->setEnabled(true);
|
|
}
|
|
|
|
void TextEditOption::bindText(QTextEdit *textEdit){
|
|
this->textEdit = textEdit;
|
|
|
|
connect(textEdit,SIGNAL(textChanged ()),this,SLOT(enableButtonSave()));
|
|
|
|
ui->bottonSave->setEnabled(false);
|
|
connect(dialogFileOperation,SIGNAL(openFile(QString,bool)),this,SLOT(onFileOpen(QString,bool)));
|
|
#ifdef MYSAVEFILEDLG
|
|
connect(dialogSaveFile,SIGNAL(saveFile(QString,bool)),this,SLOT(onSaveASFile(QString,bool)));
|
|
#endif
|
|
|
|
}
|
|
void TextEditOption::bindSysPara(SysPara *sysPara){
|
|
this->sysPara = sysPara;
|
|
#ifdef _2DCODE_
|
|
if(sysPara)
|
|
{
|
|
barcode->bindSysPara(sysPara);
|
|
//qDebug()<<"TextEditOption bind by TextEdit.";
|
|
}
|
|
#endif
|
|
//else qDebug()<<"TextEditOption bind by TextEdit failure";
|
|
}
|
|
|
|
void TextEditOption::bindPicWidget(PicWidget *picwidget){
|
|
this->picwidget = picwidget;// stackedWidgetOption
|
|
|
|
// connect(textEdit,SIGNAL(textChanged ()),this,SLOT(enableButtonSave()));
|
|
// ui->bottonSave->setEnabled(false);
|
|
// connect(dialogFileOperation,SIGNAL(openFile(QString,bool)),this,SLOT(onFileOpen(QString,bool)));
|
|
|
|
|
|
}
|
|
void TextEditOption::bindStackWidget(QStackedWidget *stackedWidgetOption,QStackedWidget *stackedWidgetText){
|
|
this->stackedWidgetOption = stackedWidgetOption;// stackedWidgetOption
|
|
this->stackedWidgetText = stackedWidgetText;
|
|
// connect(textEdit,SIGNAL(textChanged ()),this,SLOT(enableButtonSave()));
|
|
// ui->bottonSave->setEnabled(false);
|
|
// connect(dialogFileOperation,SIGNAL(openFile(QString,bool)),this,SLOT(onFileOpen(QString,bool)));
|
|
|
|
|
|
|
|
}
|
|
void TextEditOption::showDlg(){
|
|
|
|
if(editMode==PICFILE){
|
|
|
|
disconnect(textEdit,SIGNAL(textChanged()),0,0);
|
|
|
|
connect(textEdit,SIGNAL(textChanged ()),this,SLOT(enableButtonSave()));
|
|
ui->bottonSave->setEnabled(false);
|
|
disconnect(dialogFileOperation,SIGNAL(openFile(QString,bool)),0,0);
|
|
connect(dialogFileOperation,SIGNAL(openFile(QString,bool)),this,SLOT(onFileOpen(QString,bool)));
|
|
on_pushButtonFile_pressed();
|
|
|
|
// dialogFileOperation->show();
|
|
}
|
|
}
|
|
|
|
void TextEditOption::slt_updateNetworkQR()
|
|
{
|
|
#ifdef _2DCODE_
|
|
if (qrcode->flagInsert) {
|
|
qrcode->flagInsert = 0;
|
|
qrcode->GetOption((qrOption_t*)&sysPara->para.qrOption);
|
|
if(sysPara->para.EditMode==PICFILE) return;
|
|
|
|
// 多行打印需要用到场景模式
|
|
updateNetQRToEdit();
|
|
}
|
|
sysPara->saveParaToFile();
|
|
#endif
|
|
}
|
|
|
|
/*
|
|
* 和DialogFileOperation对接
|
|
* */
|
|
void TextEditOption::onFileOpen(QString filePath,bool insertFlag)
|
|
{
|
|
if(editMode==PICFILE){
|
|
if(filePath.endsWith(".png") )//||filePath.endsWith(".bmp") )
|
|
{
|
|
if(insertFlag){
|
|
emit PicfileNameOpen(filePath,insertFlag);
|
|
}
|
|
else {
|
|
//
|
|
emit PicfileNameOpen(filePath,0);
|
|
}
|
|
return ;
|
|
}
|
|
else{
|
|
if(stackedWidgetOption) {
|
|
stackedWidgetOption->setCurrentIndex(1);
|
|
stackedWidgetText->setCurrentIndex(1);
|
|
}
|
|
}
|
|
}
|
|
else if(editMode==TXTFILE) {
|
|
if(filePath.endsWith(".gif") || filePath.endsWith(".jpeg") || filePath.endsWith(".jpg")
|
|
|| filePath.endsWith(".bmp") || filePath.endsWith(".BMP") || filePath.endsWith(".png"))
|
|
{
|
|
// QString html = "<img src='" + filePath + "'/>";
|
|
QString html = "<img src='" + filePath + "' style=\"vertical-align: bottom;\" />";
|
|
QTextCharFormat fmt = textEdit->currentCharFormat ();
|
|
|
|
if(insertFlag){
|
|
textEdit->insertHtml (html);
|
|
}
|
|
else {
|
|
textEdit->setHtml (html);
|
|
textEdit->moveCursor(QTextCursor::EndOfLine); //在插入图片的情况下,将图片至于末会导致格式还原
|
|
}
|
|
textEdit->mergeCurrentCharFormat(fmt);
|
|
}
|
|
else if(filePath.endsWith(".xml"))
|
|
{
|
|
memset(sysPara->para.lastOpenedFileName,0,sizeof(sysPara->para.lastOpenedFileName));
|
|
return;
|
|
}
|
|
else {
|
|
QFile file(filePath);
|
|
if(!file.open(QIODevice::ReadOnly)){
|
|
QMessageBox::warning(this,tr("警告"),tr("无法打开文件") + filePath);
|
|
}
|
|
else {
|
|
QByteArray data = file.readAll();
|
|
QTextCodec *codec = Qt::codecForHtml(data);
|
|
QString str = codec->toUnicode(data);
|
|
|
|
// if (Qt::mightBeRichText(str)) {
|
|
// textEdit->setHtml(str);
|
|
// } else {
|
|
// str = QString::fromLocal8Bit(data);
|
|
// textEdit->setPlainText(str);
|
|
// }
|
|
// QTextStream in (&file);in.readAll()
|
|
if(filePath.startsWith(UDISK_DOC_ROOT_DIR)){
|
|
str.replace(DOC_ROOT_DIR,UDISK_DOC_ROOT_DIR); //做相应处理
|
|
}
|
|
|
|
if(insertFlag){
|
|
QMessageBox::StandardButton ret;
|
|
ret = QMessageBox::warning(this, tr("Application"), tr("是否确认将文件内容追加到打开的文件?"),
|
|
QMessageBox::Ok | QMessageBox::Cancel);
|
|
if (ret == QMessageBox::Ok){
|
|
textEdit->insertHtml(str);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// updateFileVariWidth(str);
|
|
updateFontFamily(str); //兼容Vx4保存的文件的字体名称大小
|
|
updateImageStyle(str); //兼容Vx4文件中保存的变量
|
|
updateFileVariWidth(str); //适应变量宽度
|
|
textEdit->setHtml(str);
|
|
fileName = filePath;
|
|
|
|
if(sysPara){
|
|
if(sysPara->sysFile != ""){
|
|
sysPara->comparePrintStruct();
|
|
sysPara->updatePrintInitToPara();
|
|
}
|
|
sysPara->sysFile =fileName;
|
|
sysPara->loadParaFromFile();
|
|
//qDebug()<<"textedit txt mode onFileOpen" << sysPara->sysFile;
|
|
snprintf(sysPara->para.lastOpenedFileName,sizeof(sysPara->para.lastOpenedFileName),"%s",fileName.toLatin1().constData());
|
|
}
|
|
emit fileNameChanged(QFileInfo(fileName).fileName());
|
|
textEdit->moveCursor(QTextCursor::EndOfLine);
|
|
onDialogLineSpaceAccepted();
|
|
}
|
|
}
|
|
fsync(file.handle());
|
|
file.close();
|
|
}
|
|
if(insertFlag) //在打开文件的时候不设置焦点
|
|
textEdit->setFocus();
|
|
if(stackedWidgetOption) {
|
|
stackedWidgetOption->setCurrentIndex(0);
|
|
stackedWidgetText->setCurrentIndex(0);
|
|
// textEdit->setFocus();
|
|
}
|
|
}
|
|
else if(editMode==SCENE) {
|
|
//开机会调用到此,然后打开文件
|
|
if(filePath.endsWith(".xml"))
|
|
{
|
|
memset(sysPara->para.lastOpenedFileName,0,sizeof(sysPara->para.lastOpenedFileName));
|
|
emit scenefileOpen(filePath,false);//sceneeditoption 接收这个信号然后打开文件
|
|
emit fileNameChanged(QFileInfo(filePath).fileName());
|
|
}
|
|
else
|
|
{
|
|
if(sceneEditItem == true)
|
|
{
|
|
if(filePath.endsWith(".gif") || filePath.endsWith(".jpeg") || filePath.endsWith(".jpg")
|
|
|| filePath.endsWith(".bmp") || filePath.endsWith(".BMP") || filePath.endsWith(".png"))
|
|
{
|
|
QString html = "<img src='" + filePath + "'/>";
|
|
QTextCharFormat fmt = textEdit->currentCharFormat ();
|
|
if(insertFlag){
|
|
textEdit->insertHtml (html);
|
|
}
|
|
else {
|
|
textEdit->setHtml(html);
|
|
textEdit->moveCursor(QTextCursor::EndOfLine); //在插入图片的情况下,将图片至于末会导致格式还原
|
|
}
|
|
textEdit->mergeCurrentCharFormat(fmt);
|
|
}
|
|
else {
|
|
QFile file(filePath);
|
|
if(!file.open(QIODevice::ReadOnly)){
|
|
QMessageBox::warning(this,tr("警告"),tr("无法打开文件") + filePath);
|
|
}
|
|
else {
|
|
QByteArray data = file.readAll();
|
|
QTextCodec *codec = Qt::codecForHtml(data);
|
|
QString str = codec->toUnicode(data);
|
|
if(filePath.startsWith(UDISK_DOC_ROOT_DIR)){
|
|
str.replace(DOC_ROOT_DIR,UDISK_DOC_ROOT_DIR); //做相应处理
|
|
}
|
|
|
|
if(insertFlag){
|
|
QMessageBox::StandardButton ret;
|
|
ret = QMessageBox::warning(this, tr("Application"), tr("是否确认将文件内容追加到打开的文件?"),
|
|
QMessageBox::Ok | QMessageBox::Cancel);
|
|
if (ret == QMessageBox::Ok){
|
|
textEdit->insertHtml(str);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
textEdit->setHtml(str);
|
|
fileName = filePath;
|
|
|
|
if(sysPara){
|
|
if(sysPara->sysFile != ""){
|
|
sysPara->comparePrintStruct();
|
|
sysPara->updatePrintInitToPara();
|
|
}
|
|
sysPara->sysFile =fileName;
|
|
sysPara->loadParaFromFile();
|
|
//qDebug()<<"onFileOpen"<<sysPara->sysFile;
|
|
snprintf(sysPara->para.lastOpenedFileName,sizeof(sysPara->para.lastOpenedFileName),"%s",fileName.toLatin1().constData());
|
|
}
|
|
emit fileNameChanged(QFileInfo(fileName).fileName());
|
|
textEdit->moveCursor(QTextCursor::EndOfLine);
|
|
}
|
|
}
|
|
fsync(file.handle());
|
|
file.close();
|
|
}
|
|
}
|
|
/*
|
|
if(stackedWidgetOption) {
|
|
stackedWidgetOption->setCurrentIndex(0);
|
|
stackedWidgetText->setCurrentIndex(0);
|
|
}
|
|
*/
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
void TextEditOption::onbottonInsertImgclicked()
|
|
{
|
|
if(fileDialog)
|
|
delete fileDialog;
|
|
fileDialog = new MyFileDialog(0);
|
|
disconnect(fileDialog,SIGNAL(accepted()),0,0);
|
|
connect(fileDialog,SIGNAL(accepted()),this,SLOT(onInsertImgFIle_select()));
|
|
fileDialog->showFileDialog(tr("插入图片"),QString("/"),QString("img (*.bmp *.jpg *.png *.gif)"));
|
|
}
|
|
|
|
void TextEditOption::onInsertImgFIle_select(){
|
|
QStringList files = fileDialog->selectedFiles();
|
|
|
|
for(int i = 0;i < files.size(); i++){
|
|
QString html = "<img src='" + files.value(i) + "'/>";
|
|
textEdit->insertHtml (html);
|
|
|
|
}
|
|
}
|
|
|
|
|
|
void TextEditOption::onbottonInsertTextclicked()
|
|
{
|
|
if(fileDialog)
|
|
delete fileDialog;
|
|
fileDialog = new MyFileDialog(0);
|
|
disconnect(fileDialog,SIGNAL(accepted()),0,0);
|
|
connect(fileDialog,SIGNAL(accepted()),this,SLOT(onInsertTextFIle_select()));
|
|
fileDialog->showFileDialog(tr("插入文本"),QString(DOC_ROOT_DIR),QString("text (*.html *.txt)"));
|
|
}
|
|
|
|
void TextEditOption::onInsertTextFIle_select(){
|
|
QStringList files = fileDialog->selectedFiles();
|
|
for(int i = 0;i < files.size(); i++){
|
|
QFile file(files.value(i));
|
|
if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){
|
|
QMessageBox::warning(this,tr("警告"),tr("无法打开文件") + files.value(i));
|
|
}
|
|
else{
|
|
QTextStream in (&file);
|
|
textEdit->insertHtml(in.readAll());
|
|
}
|
|
fsync(file.handle());
|
|
file.close();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void TextEditOption::onbuttonOpenclicked()
|
|
{
|
|
if(!textEdit){
|
|
QMessageBox::critical(this,tr("错误"),tr("TextEditOption 构造函数参数textEdit 不应当为空")); //
|
|
}
|
|
if(fileDialog)
|
|
delete fileDialog;
|
|
fileDialog = new MyFileDialog(0);
|
|
disconnect(fileDialog,SIGNAL(accepted()),0,0);
|
|
connect(fileDialog,SIGNAL(accepted()),this,SLOT(onOpenFIle_select()));
|
|
fileDialog->DontUseNativeDialog;
|
|
|
|
fileDialog->showFileDialog(tr("打开文件"),QString(DOC_ROOT_DIR),QString("Text files (*.txt *.html)"));
|
|
}
|
|
|
|
|
|
void TextEditOption::openFIle(QString openFileName){
|
|
QFile file(openFileName);
|
|
if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){
|
|
QMessageBox::warning(this,tr("警告"),tr("无法打开文件") + openFileName);
|
|
}
|
|
else{
|
|
QTextStream in (&file);
|
|
textEdit->setText(in.readAll());
|
|
fileName = openFileName;
|
|
if(sysPara){
|
|
sysPara->sysFile =fileName;
|
|
sysPara->loadParaFromFile();
|
|
//qDebug()<<"openFIle"<<sysPara->sysFile;
|
|
snprintf(sysPara->para.lastOpenedFileName,sizeof(sysPara->para.lastOpenedFileName),"%s",fileName.toLatin1().constData());
|
|
}
|
|
emit fileNameChanged(QFileInfo(fileName).fileName());
|
|
}
|
|
file.close();
|
|
}
|
|
|
|
|
|
void TextEditOption::onOpenFIle_select(){
|
|
|
|
QStringList files = fileDialog->selectedFiles();
|
|
for(int i = 0;i < files.size(); i++){
|
|
openFIle(files.value(i));
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void TextEditOption::on_buttonSaveAS_clicked()
|
|
{
|
|
#ifdef MYSAVEFILEDLG
|
|
disconnect(dialogSaveFile,SIGNAL(saveFile(QString,bool)),0,0);
|
|
connect(dialogSaveFile,SIGNAL(saveFile(QString,bool)),this,SLOT(onSaveASFile(QString,bool)));
|
|
dialogSaveFile->refreshListView(editMode);
|
|
dialogSaveFile->show();
|
|
dialogSaveFile->setEnterFileName("");
|
|
#else
|
|
if(fileDialog)
|
|
delete fileDialog;
|
|
fileDialog = new MyFileDialog(0);
|
|
disconnect(fileDialog,SIGNAL(accepted()),0,0);
|
|
connect(fileDialog,SIGNAL(accepted()),this,SLOT(onSaveAS_FIle_select()));
|
|
fileDialog->showFileDialog(tr("另存为"),QString(DOC_ROOT_DIR),QString("files (*.html *.txt)"));
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
void TextEditOption::saveFile(){
|
|
if(fileName.endsWith(".txt")){
|
|
QMessageBox::StandardButton ret;
|
|
ret = QMessageBox::warning(this, tr("警告"),
|
|
tr("以.txt保存将丢失所有格式,你是否愿意保存为.html格式?"),
|
|
QMessageBox::Ok | QMessageBox::Cancel);
|
|
if (ret == QMessageBox::Ok){
|
|
fileName += ".html";
|
|
}
|
|
}
|
|
QString html = textEdit->toHtml();
|
|
QString pattern = "<img src=\""UDISK_DOC_ROOT_DIR"([\\w.]*)";
|
|
QRegExp rx(pattern);
|
|
bool picFromUdisk = false;
|
|
int from = 0;
|
|
while(from < html.size() && from != -1){ //检查是否有图片来自U盘
|
|
from = html.indexOf(rx,from);
|
|
if(from != -1){
|
|
from ++;
|
|
picFromUdisk = true;
|
|
QMessageBox::StandardButton ret;
|
|
ret = QMessageBox::information(0,tr("提醒"),tr("检测到有图片 %1 来自U盘,是否拷贝图片到本地?").arg(rx.cap(1)),
|
|
QMessageBox::Ok | QMessageBox::Cancel);
|
|
if(ret == QMessageBox::Ok){
|
|
system((char *)((QString("cp "UDISK_DOC_ROOT_DIR"%1 "DOC_ROOT_DIR).arg(rx.cap(1))).toUtf8().data()));
|
|
}
|
|
}
|
|
}
|
|
if(picFromUdisk){
|
|
textEdit->setHtml(html.replace(UDISK_DOC_ROOT_DIR,DOC_ROOT_DIR));
|
|
}
|
|
QTextDocumentWriter writer(fileName);
|
|
|
|
//writer.setFormat("html");
|
|
|
|
// textEdit->document()->setDocumentMargin(0);
|
|
// bool success = writer.write(textEdit->document());
|
|
html.replace(QRegularExpression("<img([^>]*)/>"), "<img \\1 style=\"vertical-align: bottom;\"/>");
|
|
// html.replace(QRegularExpression("<span([^>]*)>"), "<span \\1 style=\"margin-bottom:0px;\">");
|
|
|
|
QFile file(fileName);
|
|
bool success = true;
|
|
if(file.open(QIODevice::WriteOnly)){
|
|
//补上 UTF-8 声明
|
|
int headStart = html.indexOf("<head>");
|
|
int headEnd = html.indexOf("</head>");
|
|
if (headStart != -1 && headEnd != -1) {
|
|
QStringRef head = html.midRef(headStart, headEnd - headStart);
|
|
if (!head.contains("charset=")) {
|
|
int pos = html.indexOf("<style");
|
|
if (pos != -1)
|
|
html.insert(pos,
|
|
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n");
|
|
}
|
|
}
|
|
QTextStream out(&file);
|
|
out.setCodec("UTF-8");
|
|
out<<html;
|
|
}else
|
|
success = false;
|
|
|
|
file.close();
|
|
|
|
if (success){
|
|
textEdit->document()->setModified(false);
|
|
ui->bottonSave->setEnabled(false);
|
|
if(sysPara){
|
|
sysPara->sysFile =fileName;
|
|
snprintf(sysPara->para.lastOpenedFileName,sizeof(sysPara->para.lastOpenedFileName),"%s",fileName.toLatin1().constData());
|
|
}
|
|
}
|
|
else {
|
|
QMessageBox::warning(this,tr("警告"),tr("无法保存文件") + fileName);
|
|
}
|
|
emit fileNameChanged(QFileInfo(fileName).fileName());
|
|
checkNewFileAppending();
|
|
}
|
|
|
|
|
|
void TextEditOption::onSaveAS_FIle_select(){
|
|
QStringList files = fileDialog->selectedFiles();
|
|
for(int i = 0;i < files.size(); i++){
|
|
fileName = files[i];
|
|
|
|
if(!fileName.endsWith(".html",Qt::CaseInsensitive) && !fileName.endsWith(".txt",Qt::CaseInsensitive))
|
|
fileName += ".html";
|
|
saveFile();
|
|
}
|
|
}
|
|
|
|
|
|
void TextEditOption::onSaveASFile(QString filename,bool Flag){// use for dialogSaveFile class
|
|
fileName = filename;
|
|
if(!fileName.endsWith(".html",Qt::CaseInsensitive) && !fileName.endsWith(".txt",Qt::CaseInsensitive))
|
|
fileName += ".html";
|
|
|
|
saveFile();
|
|
//emit fileNameChanged(QFileInfo(fileName).fileName());
|
|
//qDebug()<<fileName;
|
|
}
|
|
|
|
void TextEditOption::checkNewFileAppending(){
|
|
if(newFileAppending){
|
|
textEdit->clear();
|
|
fileName = "";
|
|
sysPara->sysFile = "";
|
|
emit fileNameChanged(QFileInfo(fileName).fileName());
|
|
setFontFormat();
|
|
onDialogLineSpaceAccepted();
|
|
}
|
|
newFileAppending = false;
|
|
}
|
|
|
|
void TextEditOption::onSave_File_reject(){
|
|
checkNewFileAppending();
|
|
}
|
|
|
|
void TextEditOption::on_bottonSave_clicked()
|
|
{
|
|
if(sysPara->para.EditMode==SCENE){
|
|
//在场景模式下,这个是确定按钮,切换到场景模式
|
|
if(sceneEditItem)
|
|
{
|
|
editItemModeDone();
|
|
emit editItemDone();
|
|
}
|
|
|
|
}
|
|
else
|
|
{
|
|
if(fileName.isEmpty()){
|
|
#ifdef MYSAVEFILEDLG
|
|
disconnect(dialogSaveFile,SIGNAL(saveFile(QString,bool)),0,0);
|
|
connect(dialogSaveFile,SIGNAL(saveFile(QString,bool)),this,SLOT(onSaveASFile(QString,bool)));
|
|
dialogSaveFile->refreshListView(editMode);
|
|
dialogSaveFile->show();
|
|
dialogSaveFile->setEnterFileName("");
|
|
#else
|
|
if(fileDialog)
|
|
delete fileDialog;
|
|
fileDialog = new MyFileDialog(0);
|
|
disconnect(fileDialog,SIGNAL(accepted()),0,0);
|
|
connect(fileDialog,SIGNAL(accepted()),this,SLOT(onSaveAS_FIle_select()));
|
|
connect(fileDialog,SIGNAL(rejected()),this,SLOT(onSave_File_reject()));
|
|
fileDialog->showFileDialog(tr("保存..."),QString(DOC_ROOT_DIR),QString("HTML files (*.html)"));
|
|
#endif
|
|
}
|
|
else {
|
|
saveFile();
|
|
newFileAppending = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
bool TextEditOption::maybeSave(){
|
|
if (!textEdit->document()->isModified())
|
|
return true;
|
|
if (fileName.startsWith(QLatin1String(":/")))
|
|
return true;
|
|
|
|
QMessageBox msgBox(QMessageBox::Information,tr("提示"),tr("文档已经改变,是否保存?"));
|
|
QPushButton *saveButton = msgBox.addButton(tr("保存"), QMessageBox::ActionRole);
|
|
QPushButton *cancleSaveButton = msgBox.addButton(tr("放弃保存"), QMessageBox::ActionRole);
|
|
QPushButton *abortButton = msgBox.addButton(QMessageBox::Cancel);
|
|
|
|
msgBox.exec();
|
|
|
|
if (msgBox.clickedButton() == saveButton) {
|
|
newFileAppending = true;
|
|
on_bottonSave_clicked();
|
|
return false;
|
|
} else if (msgBox.clickedButton() == cancleSaveButton) {
|
|
SectionalManager::instance()->emitCancelSection();
|
|
return true;
|
|
}
|
|
else
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
void TextEditOption::on_bottonNew_pressed()
|
|
{
|
|
if (maybeSave()) {
|
|
//setFontFormat();
|
|
if(sysPara->sysFile != ""){
|
|
sysPara->comparePrintStruct();
|
|
sysPara->updatePrintInitToPara();
|
|
}
|
|
textEdit->clear();
|
|
fileName = "";
|
|
sysPara->sysFile = "";
|
|
QFile file(fileName);
|
|
emit fileNameChanged(QFileInfo(fileName).fileName());
|
|
}
|
|
#ifdef VARCOLOR
|
|
textEdit->insertHtml(QString("<span style='background:#ffffff;color=#000000;'> <span />"));
|
|
#endif
|
|
|
|
}
|
|
|
|
void TextEditOption::onOpenNewFile()
|
|
{
|
|
//if (maybeSave())
|
|
{
|
|
//setFontFormat();
|
|
textEdit->clear();
|
|
fileName = "";
|
|
QFile file(fileName);
|
|
emit fileNameChanged(QFileInfo(fileName).fileName());
|
|
}
|
|
#ifdef VARCOLOR
|
|
textEdit->insertHtml(QString("<span style='background:#ffffff;color=#000000;'> <span />"));
|
|
#endif
|
|
|
|
}
|
|
|
|
void TextEditOption::on_buttonInsertVari_pressed()
|
|
{
|
|
QString s,fontname="";
|
|
if(sysPara)
|
|
{
|
|
//s.sprintf("%s(%02dX%02d)",sysPara->para.qrOption.text,sysPara->para.qrOption.width,sysPara->para.qrOption.height);
|
|
s= sysPara->para.qrOption.text;
|
|
if(s.length()>120)s="string to long.";
|
|
dialogInsertVari->setValue("");
|
|
}
|
|
|
|
if(dialogInsertVari->exec() == QDialog::Accepted && sysPara) {
|
|
int pixelSize = ui->labelFontSize->text().toInt();
|
|
QFont font;
|
|
|
|
// 直接返回字体名会导致字模生成时,字体名不对应,因此现在改成根据pixelSize来获取字体名 byons@2026年5月15日
|
|
// fontname = dialogFontSize->getFontFamily();
|
|
fontname = Public::getFontFamily(dialogFontSize->getFont().pixelSize(), 0); // 最后一个参数用不到
|
|
|
|
if(ui->checkBoxBold->isChecked() && (pixelSize<=16))
|
|
fontname = getFamilyName(pixelSize);
|
|
|
|
// font.setPixelSize(pixelSize);
|
|
font.setFamily(fontname);
|
|
font.setPixelSize(dialogFontSize->getFont().pixelSize());
|
|
pixelSize = dialogFontSize->getFont().pixelSize();
|
|
// font.setWeight(ui->checkBoxBold->isChecked() && (pixelSize<=16) ? QFont::Bold:QFont::Normal);
|
|
|
|
// font.setLetterSpacing(QFont::AbsoluteSpacing,sysPara->para.textEditCharSpace);
|
|
QFontMetrics fontMetrics(font);
|
|
int width = (fontMetrics.horizontalAdvance("0123456789"))/10;
|
|
if(sysPara->para.flagOfModuleDecimal || sysPara->para.flagOfModuleHide){
|
|
width = (fontMetrics.horizontalAdvance("0123456789.m "))/13;
|
|
}
|
|
|
|
int height = fontMetrics.ascent()+ fontMetrics.descent();
|
|
int digitalNum;
|
|
int varType;
|
|
|
|
height = font.pixelSize() + 1;
|
|
unsigned char splitMode=sysPara->para.ExpirySplitMode;
|
|
dialogInsertVari->getValue(&varType,&digitalNum,&splitMode);
|
|
|
|
// 对于24、16、12点WenQuanYi字体,需要调整高度。我也不知道为什么,但是不调整会导致字模与普通文字不对齐 byons@2026年5月16日
|
|
// 日后如果替换了这些高度的字体,需要根据实际情况调整高度
|
|
if((height == 25 || height == 17 || height == 13) && !ui->checkBoxBold->isChecked()) {
|
|
height -= 1;
|
|
}
|
|
|
|
if((varType<9 || varType==13 || varType==14 || varType == 15) // 普通变量;varType=13 is CNT3 var
|
|
// ||(varType>=20 && varType<29) // 横左变量
|
|
// ||(varType>=40 && varType<49) // 右横变量
|
|
)
|
|
{
|
|
width += sysPara->para.textEditCharSpace;
|
|
qDebug() << "on_buttonInsertVari_pressed fontname:" << font.family();
|
|
qDebug() << "on_buttonInsertVari_pressed pixelSize:" << height;
|
|
qDebug() << "on_buttonInsertVari_pressed letter space:" << sysPara->para.textEditCharSpace;
|
|
qDebug() << "on_buttonInsertVari_pressed width:" << width;
|
|
|
|
unsigned char splitMode = sysPara->para.ExpirySplitMode;
|
|
dialogInsertVari->getValue(&varType,&digitalNum,&splitMode);
|
|
|
|
fmt = textEdit->currentCharFormat();
|
|
|
|
QString html = QString("<img src=':/%1/%5_%6/../image/digitalWidth%2.bmp' height='%3' width='%4' style=\"vertical-align: bottom;\" />")
|
|
.arg(varType).arg(ui->checkBoxBold->isChecked() && (pixelSize<=16)?(digitalNum*100 + 75):digitalNum)
|
|
.arg(height).arg(width*digitalNum).arg(varType).arg(font.family());
|
|
textEdit->insertHtml(html); //插入索引后
|
|
qDebug() << textEdit->toHtml();
|
|
|
|
} // 普通变量 VarType<=8
|
|
else if(varType >=9 && varType <=11)
|
|
{ //
|
|
#ifdef _2DCODE_
|
|
// 让二维码界面位于底部,键盘不会遮挡输入位置
|
|
int x = (this->width() - qrcode->width()) / 2;
|
|
int y = 480-qrcode->height();
|
|
qrcode->move(this->mapToGlobal(QPoint(x, y)));
|
|
|
|
s= sysPara->para.qrOption.text;
|
|
if(varType==9){
|
|
qrcode->flagInsert=1;
|
|
sysPara->para.qrOption.AutoCount=1;
|
|
sysPara->para.qrOption.AutoRefresh=1;
|
|
sysPara->para.qrOption.type=0;
|
|
sysPara->para.qrOption.cntNum=digitalNum; //digitalNum
|
|
qrcode->setWindowTitle(tr("二维码变量设置"));
|
|
qrcode->SetOption(&sysPara->para.qrOption);
|
|
qrcode->setCntCheckBoxHide(0);
|
|
qrcode->show();
|
|
}
|
|
|
|
else if(varType==10){
|
|
qrcode->flagInsert=1;
|
|
sysPara->para.qrOption.AutoCount=1;
|
|
sysPara->para.qrOption.AutoRefresh=1;
|
|
sysPara->para.qrOption.type=1;
|
|
sysPara->para.qrOption.cntNum=digitalNum;
|
|
qrcode->setCntCheckBoxHide(0);
|
|
qrcode->setWindowTitle(tr("二维码变量设置"));
|
|
qrcode->SetOption(&sysPara->para.qrOption);
|
|
qrcode->show();
|
|
}
|
|
|
|
else if(varType==11){
|
|
barcode->move(0,0);
|
|
barcode->flagInsert=1;
|
|
sysPara->para.qrOption.AutoCount=1;
|
|
sysPara->para.qrOption.AutoRefresh=1;
|
|
sysPara->para.qrOption.type=1;
|
|
sysPara->para.qrOption.cntNum=digitalNum;
|
|
barVarFont = font;
|
|
barVarHeight=height;
|
|
barVarWidth =width;
|
|
//barcode->setCntCheckBoxHide(0);
|
|
barcode->setWindowTitle(tr("条形码变量设置"));
|
|
barcode->SetOption(&sysPara->para.bcOption);
|
|
barcode->show();
|
|
|
|
}
|
|
textEdit->mergeCurrentCharFormat(fmt);
|
|
#endif
|
|
}
|
|
else if(varType ==RANDOMNUM){ //12
|
|
//digitalNum=13;
|
|
//qDebug()<<"digitalNum"<<digitalNum;
|
|
textEdit->insertHtml(QString("<img src=':/%1/image/digitalWidth%2.bmp' height='%3' width='%4'/>")
|
|
.arg(varType).arg(ui->checkBoxBold->isChecked() && (pixelSize<=16)?(digitalNum*100 + 75):digitalNum)
|
|
.arg(height+1).arg(width*digitalNum)); //插入索引后
|
|
|
|
textEdit->mergeCurrentCharFormat(fmt);
|
|
}
|
|
else if(varType ==EXPIRYDATE){
|
|
int varType1 = varType;
|
|
if(fontname.contains("bangla") )
|
|
{
|
|
varType1= 10+varType;
|
|
}
|
|
else if(fontname.contains("biyangF") )
|
|
{
|
|
varType1= 20+varType;
|
|
}
|
|
else if(fontname.contains("Roman") )
|
|
{
|
|
varType1= 30+varType;
|
|
}
|
|
|
|
sysPara->print.ExpiryVar.splitMode=splitMode;
|
|
font.setLetterSpacing(QFont::AbsoluteSpacing,sysPara->para.textEditCharSpace);
|
|
QFontMetrics fontMetrics(font);
|
|
QHash<int, QString> dateFormat;
|
|
dateFormat.insert(0,"yyyy-MM-dd");
|
|
dateFormat.insert(1,"yyyy/MM/dd");
|
|
dateFormat.insert(2,"yyyyMMdd");
|
|
dateFormat.insert(3,"yyyy年MM月dd日");
|
|
dateFormat.insert(4,"yyyy-MM-dd hh:mm");
|
|
dateFormat.insert(5,"yyyy/MM/dd hh:mm");
|
|
dateFormat.insert(6,"yyyyMMddhhmm");
|
|
dateFormat.insert(7,"yyyy年MM月dd日 hh:mm");
|
|
dateFormat.insert(8,"hh:mm");
|
|
dateFormat.insert(9,"ddhhmm");
|
|
dateFormat.insert(10,"dd MM yy");
|
|
dateFormat.insert(11,"dd/MM/yy");
|
|
dateFormat.insert(12,"dd/MM/yyyy");
|
|
dateFormat.insert(13,"dd-MM-yy");
|
|
dateFormat.insert(14,"dd-MM-yyyy");
|
|
//dateFormat.insert(10,"x");
|
|
QString DateTime = QDateTime::currentDateTime().toString(dateFormat.value(splitMode));
|
|
//qDebug()<<"DateTime "<<DateTime;
|
|
int dateTimeWidth = fontMetrics.width(DateTime);
|
|
// qDebug()<<" ExpiryVar height"<<height;
|
|
// qDebug()<<"splitMode="<<splitMode;
|
|
// qDebug()<<" dateTimeWidth"<<dateTimeWidth;
|
|
if(DateTime == "x")
|
|
{
|
|
qDebug()<<" x handle";
|
|
textEdit->insertHtml(QString("<img src=':/%1/%5/../image/digitalWidth%2.bmp' height='%3' width='%4'/>")
|
|
.arg(varType).arg(ui->checkBoxBold->checkState() && (pixelSize<=16)?(175):1)
|
|
.arg(height).arg(dateTimeWidth+4).arg(sysPara->para.textEditCharSpace)); //插入索引后
|
|
sysPara->print.ExpiryVar.bold = ui->checkBoxBold->checkState();
|
|
textEdit->mergeCurrentCharFormat(fmt);
|
|
}else
|
|
{
|
|
if(splitMode == 3 || splitMode ==7 )
|
|
{
|
|
height = fontMetrics.ascent();
|
|
}
|
|
textEdit->insertHtml(QString("<img src=':/%1/%5/../image/expiryDate%2.bmp' height='%3' width='%4' style=\"vertical-align: bottom;\" />")
|
|
.arg(varType1).arg(ui->checkBoxBold->checkState() && (pixelSize<=16)?(875):8)
|
|
.arg(height).arg(dateTimeWidth+4).arg(sysPara->para.textEditCharSpace)); //插入索引后
|
|
sysPara->print.ExpiryVar.bold = ui->checkBoxBold->checkState();
|
|
textEdit->mergeCurrentCharFormat(fmt);
|
|
}
|
|
}
|
|
else if(varType ==CURDATE ){ //81 :/80/image/expiryDate8.bmp qrc:/80/image/curDate8.bmp
|
|
int varType1 = varType;
|
|
if(fontname.contains("bangla") )
|
|
{
|
|
varType1= 10+varType;
|
|
}
|
|
else if(fontname.contains("biyangF") )
|
|
{
|
|
varType1= 20+varType;
|
|
}
|
|
else if(fontname.contains("biyangF") )
|
|
{
|
|
varType1= 30+varType;
|
|
}
|
|
|
|
// 0xff00fc
|
|
//qDebug()<<"CurDateVar height"<<height;
|
|
sysPara->print.CurDateVar.splitMode = splitMode;
|
|
font.setLetterSpacing(QFont::AbsoluteSpacing, sysPara->para.textEditCharSpace);
|
|
QFontMetrics fontMetrics(font);
|
|
QHash<int, QString> dateFormat;
|
|
dateFormat.insert(0,"yyyy-MM-dd");
|
|
dateFormat.insert(1,"yyyy/MM/dd");
|
|
dateFormat.insert(2,"yyyyMMdd");
|
|
dateFormat.insert(3,"yyyy年MM月dd日");
|
|
dateFormat.insert(4,"yyyy-MM-dd hh:mm");
|
|
dateFormat.insert(5,"yyyy/MM/dd hh:mm");
|
|
dateFormat.insert(6,"yyyyMMddhhmm");
|
|
dateFormat.insert(7,"yyyy年MM月dd日 hh:mm");
|
|
dateFormat.insert(8,"hh:mm");
|
|
dateFormat.insert(9,"ddhhmm");
|
|
dateFormat.insert(10,"dd MM yy");
|
|
dateFormat.insert(11,"dd/MM/yy");
|
|
dateFormat.insert(12,"dd/MM/yyyy");
|
|
dateFormat.insert(13,"dd-MM-yy");
|
|
dateFormat.insert(14,"dd-MM-yyyy");
|
|
QString DateTime = QDateTime::currentDateTime().toString(dateFormat.value(splitMode));
|
|
int dateTimeWidth = fontMetrics.width(DateTime) ;
|
|
qDebug()<<"insert splitMode="<<splitMode;
|
|
|
|
if(DateTime == "x")
|
|
{
|
|
qDebug()<<" x handle";
|
|
textEdit->insertHtml(QString("<img src=':/%1/%5/../image/curDate%2.bmp' height='%3' width='%4'/>")
|
|
.arg(varType).arg(ui->checkBoxBold->checkState() && (pixelSize<=16)?(875):1)
|
|
.arg(height).arg(dateTimeWidth+4).arg(sysPara->para.textEditCharSpace)); //插入索引后
|
|
textEdit->mergeCurrentCharFormat(fmt);
|
|
}else
|
|
{
|
|
if(splitMode == 3 || splitMode ==7 )
|
|
{
|
|
height = fontMetrics.ascent();//fontMetrics.ascent()+ fontMetrics.descent();
|
|
}
|
|
textEdit->insertHtml(QString("<img src=':/%1/%5/../image/curDate%2.bmp' height='%3' width='%4' style=\"vertical-align: bottom;\"/>")
|
|
.arg(varType1).arg(ui->checkBoxBold->checkState() && (pixelSize<=16)?(875):8)
|
|
.arg(height).arg(dateTimeWidth+4).arg(sysPara->para.textEditCharSpace)); //插入索引后
|
|
qDebug() << "textEdit->insertHtml:"<<textEdit->toHtml();
|
|
textEdit->mergeCurrentCharFormat(fmt);
|
|
}
|
|
}
|
|
else if(varType == CURCLASS) { //82 :/82/image/curDate1.bmp
|
|
font.setLetterSpacing(QFont::AbsoluteSpacing,sysPara->para.textEditCharSpace);
|
|
QFontMetrics fontMetrics(font);
|
|
int dateTimeWidth = fontMetrics.width("A") ;
|
|
int pixBmpSize = 1;
|
|
if (ui->checkBoxBold->checkState() && (pixelSize<=16)) {
|
|
pixBmpSize = 875;
|
|
}
|
|
else {
|
|
if(pixelSize >= 9) {
|
|
pixBmpSize = 91;
|
|
}
|
|
else {
|
|
pixBmpSize = 1;
|
|
}
|
|
}
|
|
qDebug()<<"height:"<<height<<"pixBmpSize:"<<pixBmpSize;
|
|
textEdit->insertHtml(QString("<img src=':/%1/%5/../image/class%2.bmp' height='%3' width='%4'/>")
|
|
.arg(varType).arg(pixBmpSize)
|
|
.arg(height+1).arg(dateTimeWidth+2).arg(sysPara->para.textEditCharSpace)); //插入索引后
|
|
textEdit->mergeCurrentCharFormat(fmt);
|
|
}
|
|
textEdit->setFocus();
|
|
}
|
|
}
|
|
|
|
|
|
void TextEditOption::mergeFormatOnWordOrSelection(const QTextCharFormat &format)
|
|
{
|
|
|
|
QTextCursor cursor = textEdit->textCursor();
|
|
//if (!cursor.hasSelection())
|
|
// cursor.select(QTextCursor::WordUnderCursor);
|
|
//cursor.mergeCharFormat(format);
|
|
//textEdit->setTextCursor(cursor);
|
|
//qDebug()<<"mergeFormatOnWordOrSelection"<<format;
|
|
//qDebug()<<cursor;
|
|
textEdit->mergeCurrentCharFormat(format);
|
|
// auto font = format.font();
|
|
// auto family = font.family();
|
|
// auto size = font.pointSize();
|
|
|
|
// qDebug() << "mergeFormatOnWordOrSelection: " << textEdit->fontFamily();
|
|
}
|
|
|
|
void TextEditOption::updateSysParaToUI() //重载
|
|
{
|
|
if(sysPara){
|
|
/*务必先设置行距*/
|
|
dialogLineSpace->setValue(sysPara->para.textEditLineSpace,sysPara->para.textEditCharSpace);
|
|
onDialogLineSpaceAccepted();
|
|
/*将文件打开*/
|
|
QString file = QString(sysPara->para.lastOpenedFileName);
|
|
if(QFile::exists(file)){
|
|
sysPara->sysFile =file;
|
|
onFileOpen(file,false);
|
|
}
|
|
dialogFontSize->setValue(sysPara->para.fontPixelSize);
|
|
}
|
|
}
|
|
|
|
void TextEditOption::onMbSetLineAndChar(int line_space, int char_space)
|
|
{
|
|
sysPara->para.textEditLineSpace = line_space;
|
|
sysPara->para.textEditCharSpace = char_space;
|
|
dialogLineSpace->setValue(sysPara->para.textEditLineSpace,sysPara->para.textEditCharSpace);
|
|
onDialogLineSpaceAccepted();
|
|
}
|
|
|
|
QLabel *TextEditOption::getLabelFontSize(){
|
|
return ui->labelFontSize;
|
|
}
|
|
|
|
QLabel *TextEditOption::getLabelFontFamily(){
|
|
return ui->labFontFamily;
|
|
}
|
|
|
|
void TextEditOption::setFontFormat(){
|
|
if(sysPara){
|
|
|
|
qreal pixelSize = ui->labelFontSize->text().toFloat();
|
|
if(pixelSize < Public::getMinFontPixelSize())
|
|
pixelSize = sysPara->para.fontPixelSize;
|
|
|
|
if(pixelSize < Public::getMinFontPixelSize()){
|
|
pixelSize = Public::getMinFontPixelSize();
|
|
}
|
|
|
|
if (pixelSize >= 0) {
|
|
QTextCharFormat fmt;
|
|
int DPI = textEdit->logicalDpiX();
|
|
DPI = DPI?DPI:1;
|
|
qreal pointSize = pixelSize*72/DPI; //Pixel = DPI * Point / 72
|
|
fmt.setFontPointSize(pointSize);
|
|
|
|
#ifdef VNLGXX
|
|
QString fontFamily = dialogFontSize->getFontFamily();
|
|
|
|
#else
|
|
QString fontFamily = ui->labFontFamily->text();
|
|
fontFamily = dialogFontSize->getFontFamily();
|
|
|
|
if(ui->checkBoxBold->isChecked())
|
|
fontFamily = getFamilyName(pixelSize);
|
|
ui->labFontFamily->setText(fontFamily);
|
|
ui->labelFontSize->setText(QString::number(pixelSize));
|
|
#endif
|
|
|
|
fmt.setFontFamily(fontFamily);
|
|
fmt.setFontWeight(ui->checkBoxBold->isChecked() && (pixelSize<=16)? QFont::Bold:QFont::Normal);
|
|
mergeFormatOnWordOrSelection(fmt);
|
|
}
|
|
}
|
|
}
|
|
|
|
void TextEditOption::currentCharFormatChanged(const QTextCharFormat &format){
|
|
QFont font = format.font();
|
|
//qDebug()<<"pixelSize:"<<font.pixelSize();
|
|
//qDebug()<<"pixelSize:"<<font.pointSize();
|
|
// sysPara->para.fontPixelSize = font.pointSize();// 以当前选择字体为准,保证选中界面勾选一致
|
|
dialogFontSize->setValue(sysPara->para.fontPixelSize);
|
|
ui->checkBoxBold->setChecked(font.bold());
|
|
}
|
|
|
|
QPushButton * TextEditOption::getPushButtonPreview(){
|
|
return ui->pushButtonPreview;
|
|
}
|
|
QCheckBox *TextEditOption::getCheckBoxBold(){
|
|
return ui->checkBoxBold;
|
|
}
|
|
|
|
void TextEditOption::on_pushButtonFile_pressed()
|
|
{
|
|
if(sceneEditItem)
|
|
{
|
|
dialogFileOperation->refreshListView(TXTFILE);
|
|
dialogFileOperation->exec();
|
|
}
|
|
else
|
|
{
|
|
dialogFileOperation->refreshListView(editMode);
|
|
dialogFileOperation->exec();
|
|
}
|
|
}
|
|
|
|
QString TextEditOption::getFamilyName(int fontsize){
|
|
// return "YaHei";
|
|
return "YaHei"+QString::number(fontsize);
|
|
}
|
|
|
|
void TextEditOption::on_buttonSelectFont_pressed()
|
|
{
|
|
// loadApplicationFonts();
|
|
// checkFonts();
|
|
if(dialogFontSize->exec() == QDialog::Accepted){
|
|
int fontSize;
|
|
QString fontFamily;
|
|
dialogFontSize->getValue(&fontSize);
|
|
fontFamily = dialogFontSize->getFontFamily();
|
|
if(ui->checkBoxBold->isChecked() && fontSize <= 16)
|
|
fontFamily = getFamilyName(fontSize);
|
|
|
|
ui->labFontFamily->setText(fontFamily);
|
|
#ifdef EnableArabic
|
|
bool switchArabicFlag=false;
|
|
bool arabicSelect=fontFamily.contains("arabic");
|
|
if((fontFamily=="biyang" || fontFamily=="wenquanyi")&&(fontSize==7 || fontSize==9)){
|
|
switchArabicFlag=true;
|
|
emit EnableSwitchArabicFlag(switchArabicFlag);
|
|
}else {
|
|
emit EnableSwitchArabicFlag(switchArabicFlag);
|
|
}
|
|
dialogSaveFile->setFileNameEnglishOrArabic(false);
|
|
emit fontSizeArabicSelect(arabicSelect);
|
|
#endif
|
|
if(sysPara){
|
|
sysPara->para.fontPixelSize = fontSize;
|
|
}
|
|
|
|
bool isDefultBold = fontFamily != "WenQuanYi32" && fontFamily != "WenQuanYi24";
|
|
ui->checkBoxBold->setEnabled(isDefultBold);
|
|
// ui->checkBoxBold->setChecked(!isDefultBold);
|
|
|
|
ui->labelFontSize->setText(QString::number(fontSize));
|
|
setFontFormat();
|
|
textEdit->setFocus();
|
|
emit fontSizeChange(fontSize);
|
|
bool flagBangal=false;
|
|
if(dialogFontSize->getFontFamily() == "bangla")
|
|
flagBangal = true;
|
|
emit switchBengalLanguage(flagBangal);
|
|
|
|
toggleVirtualKeyboard();
|
|
}
|
|
|
|
}
|
|
|
|
void TextEditOption::on_checkBoxBold_released()
|
|
{
|
|
setFontFormat();
|
|
textEdit->setFocus();
|
|
}
|
|
|
|
|
|
void TextEditOption::onDialogLineSpaceAccepted(){
|
|
dialogLineSpace->getValue(&sysPara->para.textEditLineSpace,&sysPara->para.textEditCharSpace);
|
|
sysPara->para.textEditLineSpace=sysPara->para.textEditLineSpace+1;//新系统行间距需要加1
|
|
for(QTextBlock textBlock = textEdit->document()->begin();textBlock != textEdit->document()->end(); textBlock = textBlock.next()){
|
|
QTextBlockFormat textBlockFormat = textBlock.blockFormat();
|
|
textBlockFormat.setAlignment(Qt::AlignBottom);
|
|
textBlockFormat.setTopMargin(-1);
|
|
textBlockFormat.setBottomMargin(sysPara->para.textEditLineSpace-1);
|
|
QTextCursor textCursor = textEdit->textCursor();
|
|
textCursor.setPosition(textBlock.position());
|
|
textCursor.setBlockFormat(textBlockFormat);
|
|
textEdit->setTextCursor(textCursor);
|
|
}
|
|
|
|
QFont font = textEdit->font();
|
|
font.setLetterSpacing(QFont::AbsoluteSpacing,sysPara->para.textEditCharSpace);
|
|
textEdit->setFont(font);
|
|
}
|
|
|
|
void TextEditOption::updateNetQRToEdit()
|
|
{
|
|
#ifdef _2DCODE_
|
|
QTextDocument* doc = textEdit->document();
|
|
QTextCursor cursor(doc);
|
|
|
|
// 查找并删除旧的网络二维码图片
|
|
cursor.movePosition(QTextCursor::Start);
|
|
while (!cursor.atEnd()) {
|
|
// 先移动光标到下一个字符开头
|
|
cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor);
|
|
QTextCharFormat fmt = cursor.charFormat();
|
|
|
|
if (fmt.isImageFormat()) {
|
|
QString imgName = fmt.toImageFormat().name();
|
|
// 匹配网络二维码图片(真实文件路径或资源占位符)
|
|
if (imgName.contains("networkQR.bmp") || imgName.contains(":/10/image/QR")) {
|
|
// 删除这个字符(图片)
|
|
cursor.deleteChar();
|
|
break;
|
|
}
|
|
}
|
|
cursor.clearSelection();
|
|
}
|
|
|
|
// 在原来位置(或当前位置)插入新的二维码图片
|
|
// 使用资源占位符
|
|
QString imgHtml = QString("<img src=':/%1/image/QRV%2.bmp' height='%2' width='%3' />")
|
|
.arg(10).arg(sysPara->para.qrOption.height).arg(sysPara->para.qrOption.width); //插入索引后
|
|
|
|
cursor.insertHtml(imgHtml);
|
|
cursor.clearSelection();
|
|
#endif
|
|
}
|
|
|
|
void TextEditOption::on_buttonLineSpace_clicked()
|
|
{
|
|
if(dialogLineSpace->exec() == QDialog::Accepted){
|
|
onDialogLineSpaceAccepted();
|
|
sysPara->saveParaToFile();
|
|
textEdit->setFocus();
|
|
sysPara->para.alterPrint.lineSpace = sysPara->para.textEditLineSpace;
|
|
}
|
|
}
|
|
|
|
void TextEditOption::InsertQRCodeToEdit(QString barCodeName,int width,int height ,int enSave){
|
|
#ifdef _2DCODE_
|
|
textEdit->setText("");
|
|
InsertQRCodeToEdit(barCodeName, width, height);
|
|
fileName = DOC_ROOT_DIR +QFileInfo(barCodeName).baseName() +".html";
|
|
if(enSave) saveFile();
|
|
#endif
|
|
}
|
|
void TextEditOption::InsertBarCodeToEdit(QString barCodeName,int width,int height ,int enSave){
|
|
#ifdef _2DCODE_
|
|
textEdit->setText("");
|
|
InsertBarCodeToEdit(barCodeName, width, height);
|
|
fileName = DOC_ROOT_DIR +QFileInfo(barCodeName).baseName() +".html";
|
|
if(enSave) saveFile();
|
|
#endif
|
|
}
|
|
void TextEditOption::InsertBarCodeToEdit(QString barCodeName,int width,int height )
|
|
{
|
|
#ifdef _2DCODE_
|
|
//dialogInsertVari->getValue(&varType,&digitalNum);
|
|
QTextCharFormat fmt = textEdit->currentCharFormat ();
|
|
textEdit->insertHtml(QString("<img src='%1' height='%2' width='%3'/>")
|
|
.arg(barCodeName)
|
|
.arg(height).arg(width)); //插入索引后
|
|
textEdit->mergeCurrentCharFormat(fmt);
|
|
//textEdit->setFocus();
|
|
//qDebug()<<"TEOption:"<<barCodeName<<width<<height;
|
|
#endif
|
|
}
|
|
|
|
void TextEditOption::InsertQRCodeToEdit(QString barCodeName,int width,int height )
|
|
{
|
|
#ifdef _2DCODE_
|
|
//dialogInsertVari->getValue(&varType,&digitalNum);
|
|
QTextCharFormat fmt = textEdit->currentCharFormat ();
|
|
textEdit->insertHtml(QString("<img src='%1' height='%2' width='%3'/>")
|
|
.arg(barCodeName)//QFileInfo(barCodeName).
|
|
.arg(width).arg(height)); //插入索引后
|
|
textEdit->mergeCurrentCharFormat(fmt);
|
|
//textEdit->setFocus();
|
|
//qDebug()<<"TEOption:"<<barCodeName<<width<<height;
|
|
#endif
|
|
}
|
|
|
|
void TextEditOption::onQRCodeClose(){
|
|
#ifdef _2DCODE_
|
|
if(qrcode->flagInsert ) {
|
|
qrcode->GetOption((qrOption_t*)&sysPara->para.qrOption);
|
|
qrcode->flagInsert=0;
|
|
if(sysPara->para.EditMode==PICFILE){
|
|
}
|
|
else {
|
|
if(sysPara->para.qrOption.type==0){
|
|
textEdit->insertHtml(QString("<img src=':/%1/image/DMTV%2.bmp' height='%2' width='%3' />")
|
|
.arg(9).arg(sysPara->para.qrOption.height).arg(sysPara->para.qrOption.width)); //插入索引后
|
|
}
|
|
else if(sysPara->para.qrOption.type==1){
|
|
textEdit->insertHtml(QString("<img src=':/%1/image/QRV%2.bmp' height='%2' width='%3' />")
|
|
.arg(10).arg(sysPara->para.qrOption.height).arg(sysPara->para.qrOption.width)); //插入索引后
|
|
}
|
|
else if(sysPara->para.qrOption.type==2){
|
|
textEdit->insertHtml(QString("<img src=':/%1/image/BARV%2.bmp' height='%2' width='%3' />")
|
|
.arg(11).arg(sysPara->para.qrOption.height).arg(sysPara->para.qrOption.width)); //插入索引后
|
|
}
|
|
}
|
|
}
|
|
sysPara->saveParaToFile();
|
|
#endif
|
|
}
|
|
void TextEditOption::onBarCodeClose(){
|
|
#ifdef _2DCODE_
|
|
bcPara_t bcoption;
|
|
if(barcode->flagInsert ) {
|
|
barcode->GetOption((bcOption_t*)&sysPara->para.bcOption);
|
|
//qDebug()<<"onBarCodeClose"<<sysPara->para.bcOption.barType<<sysPara->para.bcOption.height;
|
|
barcode->GetOption2((bcPara_t*)&bcoption);
|
|
//qDebug()<<"onBarCodeClose"<<bcoption.type;
|
|
sysPara->para.bcOption.barType = bcoption.type;
|
|
barcode->flagInsert=0;
|
|
if(sysPara->para.EditMode==PICFILE){
|
|
|
|
}
|
|
else
|
|
{
|
|
fmt = textEdit->currentCharFormat ();
|
|
textEdit->insertHtml(QString("<img src=':/%1/image/BARV%4.bmp' height='%2' width='%3' />")
|
|
.arg(11).arg(bcoption.height).arg(bcoption.width).arg(bcoption.type*100+bcoption.height));
|
|
|
|
//插入索引后
|
|
//qDebug()<<QString("<img src=':/%1/image/BARV%4.bmp' height='%2' width='%3' />")
|
|
// .arg(11).arg(bcoption.height).arg(bcoption.width).arg(bcoption.type*100+bcoption.height);
|
|
|
|
int varType1= 7;//CNT1:7, CNT2:8
|
|
int digitalNum;
|
|
if(sysPara->para.bcOption.var1En){
|
|
digitalNum=sysPara->para.bcOption.var1Num;
|
|
textEdit->insertHtml(QString("%1%2").arg(sysPara->para.bcOption.TxtModule).arg(sysPara->para.bcOption.TxtLine));
|
|
textEdit->insertHtml(QString("<img src=':/%1/image/digitalWidth%2.bmp' height='%3' width='%4'/>")
|
|
.arg(varType1).arg(ui->checkBoxBold->isChecked() && (barVarHeight<=16)?(digitalNum*100 + 75):digitalNum)
|
|
.arg(barVarHeight+1).arg( barVarWidth*digitalNum)); //插入索引后
|
|
}
|
|
if(sysPara->para.bcOption.var2En)
|
|
{
|
|
digitalNum=sysPara->para.bcOption.var2Num;
|
|
textEdit->insertHtml(QString("<img src=':/%1/image/digitalWidth%2.bmp' height='%3' width='%4'/>")
|
|
.arg(varType1+1).arg(ui->checkBoxBold->isChecked() && (barVarHeight<=16)?(digitalNum*100 + 75):digitalNum)
|
|
.arg(barVarHeight+1).arg( barVarWidth*digitalNum)); //插入索引后
|
|
}
|
|
|
|
textEdit->mergeCurrentCharFormat(fmt);
|
|
}
|
|
}
|
|
sysPara->saveParaToFile();
|
|
#endif
|
|
}
|
|
|
|
void TextEditOption::onEditItemMode()
|
|
{
|
|
ui->bottonNew->setDisabled(true);
|
|
ui->buttonSaveAS->setDisabled(true);
|
|
ui->pushButtonPreview->setDisabled(true);
|
|
ui->bottonSave->setText(tr("完成"));
|
|
sceneEditItem = true;
|
|
}
|
|
void TextEditOption::editItemModeDone()
|
|
{
|
|
sceneEditItem = false;
|
|
ui->bottonNew->setDisabled(false);
|
|
ui->buttonSaveAS->setDisabled(false);
|
|
ui->pushButtonPreview->setDisabled(false);
|
|
ui->bottonSave->setText(tr("保存"));
|
|
}
|
|
|
|
void TextEditOption::modbus_insert_pic(QString filename)
|
|
{
|
|
// on_pushButtonFile_pressed();
|
|
dialogFileOperation->modbus_openfile(filename);
|
|
|
|
}
|
|
|
|
void TextEditOption::updateSysParaToZero() //重载
|
|
{
|
|
if(sysPara){
|
|
dialogLineSpace->setValue(sysPara->para.textEditLineSpace,sysPara->para.textEditCharSpace);
|
|
onDialogLineSpaceAccepted();
|
|
}
|
|
}
|
|
|
|
void TextEditOption::SetHiddenValue(bool select)
|
|
{
|
|
dialogInsertVari->EnableHiddenValue(select);
|
|
}
|
|
|
|
void TextEditOption::updateFileVariWidth(QString &str)
|
|
{
|
|
QString patternNormalVar = "<img\\s+src=\":/(\\d*)/(\\d*)/../image/digitalWidth(\\d*).bmp\" width=\"(\\d*)\" height=\"(\\d*)\"\\s+style=\"vertical-align: bottom;\"/>";
|
|
updateNormalVarWidth(str,patternNormalVar);
|
|
|
|
QString patternCurDateVar = "<img\\s+src=\":/(\\d*)/image/curDate(\\d*).bmp\" width=\"(\\d*)\" height=\"(\\d*)\"\\s+style=\"vertical-align: bottom;\"/>";
|
|
updateProduceDateVarWidth(str,patternCurDateVar,2);
|
|
|
|
QString patternExpiryDateVar = "<img\\s+src=\":/(\\d*)/image/expiryDate(\\d*).bmp\" width=\"(\\d*)\" height=\"(\\d*)\"\\s+style=\"vertical-align: bottom;\"/>";
|
|
updateProduceDateVarWidth(str,patternExpiryDateVar,1);
|
|
|
|
}
|
|
|
|
void TextEditOption::updateNormalVarWidth(QString& str,const QString& pattern)
|
|
{
|
|
QRegExp rx(pattern);
|
|
int from = 0;
|
|
// 正常变量
|
|
while(from < str.size() && from != -1) {
|
|
from = str.indexOf(rx,from);//返回匹配到到起始地址
|
|
bool ok;
|
|
if(from != -1){//找到匹配的
|
|
int width = rx.cap(4).toInt(&ok)*(sysPara->status.addSpace?2:1);
|
|
int height = rx.cap(5).toInt(&ok);
|
|
int height2 = getVariableHight(height);
|
|
int digitalNum = rx.cap(3).toInt(&ok);//第二个匹配,数字到个数
|
|
if(digitalNum>100){ //加粗
|
|
digitalNum/=100;
|
|
ui->checkBoxBold->setChecked(true);
|
|
}
|
|
// 获取匹配的<img>标签
|
|
QString matchedTag = rx.cap(0);
|
|
// 构建新的<img>标签,替换width属性值
|
|
QString newTag = matchedTag;
|
|
int singlewidth = getSingleDigitWidth(height2);
|
|
int newWidth= (singlewidth + sysPara->para.textEditCharSpace) * digitalNum;
|
|
if(newWidth == width){
|
|
from ++;
|
|
from+=newTag.length();
|
|
continue;
|
|
}
|
|
newTag.replace(QString("width=\"%1\"").arg(rx.cap(4)), QString("width=\"%1\"").arg(newWidth));
|
|
// 替换原始文本中的<img>标签
|
|
str.replace(from, rx.matchedLength(), newTag);
|
|
from ++;
|
|
from+=newTag.length();
|
|
}
|
|
}
|
|
}
|
|
|
|
void TextEditOption::updateProduceDateVarWidth(QString &str,const QString pattern,int mode){
|
|
QRegExp rx(pattern);
|
|
int from = 0;
|
|
// 正常变量
|
|
while(from < str.size() && from != -1) {
|
|
from = str.indexOf(rx,from);//返回匹配到到起始地址
|
|
bool ok;
|
|
if(from != -1){//找到匹配的
|
|
int width = rx.cap(3).toInt(&ok)*(sysPara->status.addSpace?2:1);
|
|
int height = rx.cap(4).toInt(&ok);
|
|
int digitalNum = rx.cap(2).toInt(&ok);//第二个匹配,数字到个数
|
|
if(digitalNum>100){ //加粗
|
|
digitalNum/=100;
|
|
ui->checkBoxBold->setChecked(true);
|
|
}
|
|
// 获取匹配的<img>标签
|
|
QString matchedTag = rx.cap(0);
|
|
// 构建新的<img>标签,替换width属性值
|
|
QString newTag = matchedTag;
|
|
int newWidth= getDateVarWidth(height,mode);
|
|
if(newWidth == width){
|
|
return;
|
|
}
|
|
newTag.replace(QString("width=\"%1\"").arg(rx.cap(3)), QString("width=\"%1\"").arg(newWidth));
|
|
// 替换原始文本中的<img>标签
|
|
str.replace(from, rx.matchedLength(), newTag);
|
|
from ++;
|
|
from+=newTag.length();
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
int TextEditOption::getDateVarWidth(int height,int mode)
|
|
{
|
|
int pixelSize = getVariableHight(height);
|
|
QFont font;
|
|
QString fontname = getVariableFontFamily(pixelSize);
|
|
font.setFamily(
|
|
fontname // 20160812
|
|
);
|
|
font.setPixelSize(pixelSize);
|
|
font.setWeight(ui->checkBoxBold->isChecked() && (pixelSize<=16) ?QFont::Bold:QFont::Normal);
|
|
unsigned char splitMode=sysPara->para.ExpirySplitMode;
|
|
if(mode==1){
|
|
sysPara->status.flagOfAdjustWidth_1 = 1;
|
|
sysPara->status.splitMode_Adjust_1= splitMode;
|
|
}
|
|
else if(mode ==2 ){
|
|
sysPara->status.flagOfAdjustWidth_2 = 1;
|
|
sysPara->status.splitMode_Adjust_2= splitMode;
|
|
}
|
|
font.setLetterSpacing(QFont::AbsoluteSpacing, sysPara->para.textEditCharSpace);
|
|
QFontMetrics fontMetrics(font);
|
|
QHash<int, QString> dateFormat;
|
|
dateFormat.insert(0,"yyyy-MM-dd");
|
|
dateFormat.insert(1,"yyyy/MM/dd");
|
|
dateFormat.insert(2,"yyyyMMdd");
|
|
dateFormat.insert(3,"yyyy年MM月dd日");
|
|
dateFormat.insert(4,"yyyy-MM-dd hh:mm");
|
|
dateFormat.insert(5,"yyyy/MM/dd hh:mm");
|
|
dateFormat.insert(6,"yyyyMMddhhmm");
|
|
dateFormat.insert(7,"yyyy年MM月dd日 hh:mm");
|
|
dateFormat.insert(8,"hh:mm");
|
|
dateFormat.insert(9,"ddhhmm");
|
|
dateFormat.insert(10,"dd MM yy");
|
|
dateFormat.insert(11,"dd/MM/yy");
|
|
dateFormat.insert(12,"dd/MM/yyyy");
|
|
dateFormat.insert(13,"dd-MM-yy");
|
|
dateFormat.insert(14,"dd-MM-yyyy");
|
|
QString DateTime = QDateTime::currentDateTime().toString(dateFormat.value(splitMode));
|
|
int dateTimeWidth = fontMetrics.width(DateTime) ;
|
|
return dateTimeWidth+4;
|
|
}
|
|
|
|
|
|
int TextEditOption::getSingleDigitWidth(int height)
|
|
{
|
|
// int pixelSize = ui->labelFontSize->text().toInt();
|
|
int pixelSize = height;
|
|
QFont font;
|
|
QString fontname= getVariableFontFamily(height);
|
|
font.setFamily(
|
|
fontname // 20160812
|
|
);
|
|
font.setPixelSize(pixelSize);
|
|
font.setWeight(ui->checkBoxBold->isChecked() && (pixelSize<=16) ?QFont::Bold:QFont::Normal);
|
|
font.setLetterSpacing(QFont::AbsoluteSpacing,0);
|
|
QFontMetrics fontMetrics(font);
|
|
int width = (fontMetrics.width("0"));
|
|
// int width = (fontMetrics.width("0123456789"))/10;
|
|
// if(sysPara->para.flagOfModuleDecimal || sysPara->para.flagOfModuleHide){
|
|
// width = (fontMetrics.width("0123456789.m "))/13;
|
|
// }
|
|
return width;
|
|
}
|
|
|
|
int TextEditOption::getVariableHight(int height){
|
|
|
|
if(height<7){
|
|
return 5;
|
|
}else if(height<9){
|
|
return 7;
|
|
}else if(height <12){
|
|
return 9;
|
|
}else if(height<16){
|
|
return 12;
|
|
}else if(height<22){ //24点阵变量高23
|
|
return 16;
|
|
}else if(height<30){
|
|
return 24;
|
|
}else if(height>=30){
|
|
return 32;
|
|
}
|
|
}
|
|
|
|
QString TextEditOption::getVariableFontFamily(int height){
|
|
QString fontFamily = "WenQuanYi12";
|
|
if(height<7){
|
|
fontFamily = "WenQuanYi5";
|
|
}else if(height<9){
|
|
fontFamily = "WenQuanYi7";
|
|
}else if(height <12){
|
|
fontFamily = "Dinkie9";
|
|
}else if(height<16){
|
|
fontFamily = "WenQuanYi12";
|
|
}else if(height<22){ //24点阵变量高23
|
|
fontFamily = "WenQuanYi16";
|
|
}else if(height<30){
|
|
fontFamily = "WenQuanYi24";
|
|
}else if(height>=30){
|
|
fontFamily = "WenQuanYi32";
|
|
}
|
|
return fontFamily;
|
|
}
|
|
|
|
void TextEditOption::showEvent(QShowEvent * e)
|
|
{
|
|
ui->checkBoxBold->setEnabled(dialogFontSize->getFontFamily() != "WenQuanYi32" && dialogFontSize->getFontFamily() != "WenQuanYi24");
|
|
// ui->checkBoxBold->setChecked(!ui->checkBoxBold->isEnabled());
|
|
|
|
|
|
QWidget::showEvent(e);
|
|
}
|
|
|
|
void TextEditOption::loadApplicationFonts() {
|
|
// 只需调用一次
|
|
static bool fontsLoaded = false;
|
|
if (fontsLoaded) return true;
|
|
|
|
QStringList fontPaths = {
|
|
"/usr/lib/fonts/Arial_5pt.bdf",
|
|
"/usr/lib/fonts/Arial_7pt.bdf",
|
|
"/usr/lib/fonts/Arial_9pt.bdf",
|
|
"/usr/lib/fonts/wq_wm_9_n.bdf",
|
|
"/usr/lib/fonts/wq_wm_12_n.bdf",
|
|
"/usr/lib/fonts/wq_wm_16_n.bdf",
|
|
"/usr/lib/fonts/wq_wm_24_n.bdf"
|
|
};
|
|
|
|
foreach (const QString &path, fontPaths) {
|
|
if (QFile::exists(path)) {
|
|
int fontId = QFontDatabase::addApplicationFont(path);
|
|
if (fontId == -1) {
|
|
qDebug()<< "Failed to load font:" << path;
|
|
} else {
|
|
qDebug()<< "Loaded font:" << path<< "ID:"<< fontId;
|
|
}
|
|
}
|
|
}
|
|
fontsLoaded = true;
|
|
|
|
}
|
|
|
|
void TextEditOption::checkFonts()
|
|
{
|
|
// 检查所有可用字体家族
|
|
QFontDatabase FontDB;
|
|
QStringList families = FontDB.families();
|
|
foreach (const QString &family, families) {
|
|
qDebug()<< " " << family;
|
|
}
|
|
}
|
|
|
|
void TextEditOption::toggleVirtualKeyboard()
|
|
{
|
|
QInputMethod *inputMethod = QGuiApplication::inputMethod();
|
|
if (inputMethod->isVisible()) {
|
|
inputMethod->hide();
|
|
} else {
|
|
inputMethod->show();
|
|
}
|
|
}
|
|
|
|
#ifdef EnableArabic
|
|
void TextEditOption::changeArabicPage(int page){
|
|
int fontSize;
|
|
dialogFontSize->getValue(&fontSize);
|
|
switch (page) {
|
|
case 1:
|
|
ui->labFontFamily->setText("biyangarabic");
|
|
break;
|
|
// case 2:
|
|
// ui->labFontFamily->setText("biyangarabicb");
|
|
// break;
|
|
case 3:
|
|
ui->labFontFamily->setText("biyangarabicc");
|
|
break;
|
|
default:
|
|
ui->labFontFamily->setText(dialogFontSize->getFontFamily());
|
|
break;
|
|
}
|
|
//ui->labFontFamily->setText(dialogFontSize->getFontFamily());
|
|
if(sysPara){
|
|
sysPara->para.fontPixelSize = fontSize;
|
|
}
|
|
ui->labelFontSize->setText(QString::number(fontSize));
|
|
setFontFormat();
|
|
textEdit->setFocus();
|
|
emit fontSizeChange(fontSize);
|
|
}
|
|
|
|
void TextEditOption::swithchEnglisOrArabic(bool flag){
|
|
// qDebug()<<"englistOrArabicFlag:"<<flag;
|
|
int fontSize;
|
|
dialogFontSize->getValue(&fontSize);
|
|
dialogSaveFile->setFileNameEnglishOrArabic(flag);
|
|
if(flag){
|
|
// if(fontSize==7 || fontSize==9 || fontSize==12){
|
|
if(fontSize==9 || fontSize==7){
|
|
ui->labFontFamily->setText("biyangarabic");
|
|
}else{
|
|
//fontSize=9;
|
|
//ui->labFontFamily->setText("biyangarabic");
|
|
}
|
|
}else{
|
|
if(fontSize==7 || fontSize==9){
|
|
ui->labFontFamily->setText("biyang");
|
|
}else{
|
|
ui->labFontFamily->setText("wenquanyi");
|
|
}
|
|
}
|
|
if(sysPara){
|
|
sysPara->para.fontPixelSize = fontSize;
|
|
}
|
|
ui->labelFontSize->setText(QString::number(fontSize));
|
|
setFontFormat();
|
|
textEdit->setFocus();
|
|
emit fontSizeChange(fontSize);
|
|
}
|
|
#endif
|
|
|
|
|
|
void TextEditOption::updateFontFamily(QString &result){
|
|
// 定义正则表达式来匹配font-family属性
|
|
QRegExp regex("font-family\\s*:\\s*'([^']+)'", Qt::CaseInsensitive);
|
|
regex.setMinimal(true); // 设置为最小匹配模式
|
|
// 定义正则表达式来匹配font-size属性
|
|
QRegExp regex2("font-size:(\\d+)pt");
|
|
regex2.setMinimal(true); // 设置为最小匹配模式
|
|
|
|
int pos = 0;
|
|
pos = result.indexOf("<p style=");
|
|
if(pos==-1)
|
|
return;
|
|
while ((pos = regex.indexIn(result, pos)) != -1) {
|
|
// 提取匹配到的font-family值
|
|
QString fontfamilyStr = regex.cap(1);
|
|
if(fontfamilyStr != "biyang" && fontfamilyStr != "wenquanyi"){
|
|
pos += fontfamilyStr.length();
|
|
continue;
|
|
}
|
|
|
|
QString newFontFamilyStr = "";
|
|
// 查找最近的font-size的位置
|
|
int pos2 = pos;
|
|
while (pos2 > 0) {
|
|
// 找到最近的font-size,进行替换
|
|
int matchPos = regex2.indexIn(result, pos2);
|
|
if (matchPos != -1) {
|
|
QString fontSizeStr = regex2.cap(1);
|
|
int fontSize = fontSizeStr.toInt();
|
|
QString newFontSize = getNewFontSize(fontSize);
|
|
QString newFontSizeStr = QString("font-size:%1pt").arg(newFontSize);
|
|
QString newFontFamily = getNewFontFamily(fontSize);
|
|
newFontFamilyStr = QString("font-family:'%1'").arg(newFontFamily);
|
|
result.replace(matchPos, regex2.matchedLength(), newFontSizeStr);
|
|
result.replace(pos, regex.matchedLength(), newFontFamilyStr);
|
|
break;
|
|
}
|
|
}
|
|
pos += newFontFamilyStr.length();
|
|
}
|
|
}
|
|
|
|
void TextEditOption::updateImageStyle(QString &html){
|
|
QRegExp imgRx("<img\\s+([^>]*)>", Qt::CaseInsensitive);
|
|
imgRx.setMinimal(true);
|
|
int pos = 0;
|
|
while ((pos = imgRx.indexIn(html, pos)) != -1) {
|
|
QString attr = imgRx.cap(1); // 原有属性
|
|
if (attr.indexOf("vertical-align") != -1) {
|
|
// 已有 vertical-align: bottom
|
|
pos += attr.length();
|
|
continue;
|
|
} else {
|
|
attr = attr.trimmed();
|
|
if (attr.endsWith('/'))
|
|
attr.chop(1);
|
|
// 没有 style,直接插入
|
|
attr += "style=\"vertical-align: bottom;\"";
|
|
}
|
|
// 3. 拼回 <img ...>
|
|
QString newTag = QString("<img %1/>").arg(attr);
|
|
html.replace(pos, imgRx.matchedLength(), newTag);
|
|
pos += newTag.length();
|
|
}
|
|
}
|
|
|
|
QString TextEditOption::getNewFontSize(int fontsize){
|
|
|
|
switch (fontsize) {
|
|
case 5:
|
|
return "3.75";
|
|
break;
|
|
case 7:
|
|
return "5.25";
|
|
break;
|
|
case 9:
|
|
return "6.75";
|
|
break;
|
|
case 12:
|
|
return "9";
|
|
break;
|
|
case 16:
|
|
return "12";
|
|
break;
|
|
case 24:
|
|
return "18";
|
|
break;
|
|
case 32:
|
|
return "24";
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
|
|
QString TextEditOption::getNewFontFamily(int fontsize){
|
|
switch (fontsize) {
|
|
case 5:
|
|
return "WenQuanYi5";
|
|
break;
|
|
case 7:
|
|
return "WenQuanYi7";
|
|
break;
|
|
case 9:
|
|
return "Dinkie9";
|
|
break;
|
|
case 12:
|
|
return "WenQuanYi12";
|
|
break;
|
|
case 16:
|
|
return "WenQuanYi16";
|
|
break;
|
|
case 24:
|
|
return "WenQuanYi24";
|
|
break;
|
|
case 32:
|
|
return "WenQuanYi32";
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
}
|