196 lines
6.1 KiB
C++
196 lines
6.1 KiB
C++
#include "BarcodeEditor.h"
|
|
#include "ui_BarcodeEditor.h"
|
|
#include "DateFormatEditor.h"
|
|
#include "DateReplaceEditor.h"
|
|
#include <QDebug>
|
|
|
|
BarcodeEditor::BarcodeEditor(QWidget *parent) :
|
|
QDialog(parent),
|
|
ui(new Ui::BarcodeEditor)
|
|
{
|
|
ui->setupUi(this);
|
|
setWindowModality(Qt::NonModal);
|
|
m_dateFormatEditor = new DateFormatEditor();
|
|
m_dateReplaceEditor = new DateReplaceEditor();
|
|
|
|
m_vm = QSharedPointer<BarcodeViewModel>::create();
|
|
m_vm->setDateViewModel(DateItemModel::createDefaultReplaceModel());
|
|
m_dateReplaceEditor->setDateModel(m_vm->dateViewModel());
|
|
}
|
|
|
|
BarcodeEditor::~BarcodeEditor()
|
|
{
|
|
auto index = ui->tabWidget->indexOf(m_dateFormatEditor);
|
|
if(index != -1) {
|
|
ui->tabWidget->removeTab(index);
|
|
}
|
|
|
|
index = ui->tabWidget->indexOf(m_dateReplaceEditor);
|
|
if(index != -1) {
|
|
ui->tabWidget->removeTab(index);
|
|
}
|
|
|
|
delete m_dateFormatEditor;
|
|
delete m_dateReplaceEditor;
|
|
delete ui;
|
|
}
|
|
|
|
void BarcodeEditor::bindViewModel(QSharedPointer<BarcodeViewModel> vm)
|
|
{
|
|
if(m_vm) {
|
|
m_vm->disconnect(this);
|
|
}
|
|
|
|
m_vm = vm;
|
|
if(!m_vm) {
|
|
qDebug() << "view model is empty";
|
|
return;
|
|
}
|
|
|
|
auto dateVm = m_vm->dateViewModel();
|
|
if (dateVm.dataMap.isEmpty() || dateVm.daysMap.isEmpty() || dateVm.monthsMap.isEmpty()
|
|
|| dateVm.yearsMap.isEmpty() || dateVm.weeksMap.isEmpty() || dateVm.hoursMap.isEmpty()) {
|
|
DateItemModel defaultReplaceModel = DateItemModel::createDefaultReplaceModel();
|
|
if (dateVm.dataMap.isEmpty()) {
|
|
dateVm.dataMap = defaultReplaceModel.dataMap;
|
|
}
|
|
if (dateVm.daysMap.isEmpty()) {
|
|
dateVm.daysMap = defaultReplaceModel.daysMap;
|
|
}
|
|
if (dateVm.monthsMap.isEmpty()) {
|
|
dateVm.monthsMap = defaultReplaceModel.monthsMap;
|
|
}
|
|
if (dateVm.yearsMap.isEmpty()) {
|
|
dateVm.yearsMap = defaultReplaceModel.yearsMap;
|
|
}
|
|
if (dateVm.weeksMap.isEmpty()) {
|
|
dateVm.weeksMap = defaultReplaceModel.weeksMap;
|
|
}
|
|
if (dateVm.hoursMap.isEmpty()) {
|
|
dateVm.hoursMap = defaultReplaceModel.hoursMap;
|
|
}
|
|
m_vm->setDateViewModel(dateVm);
|
|
}
|
|
|
|
DateFormatEditorVM dateFormatVM;
|
|
if(m_vm->dateViewModel().daysOffset > 0) {
|
|
dateFormatVM.expirationValue = m_vm->dateViewModel().daysOffset;
|
|
dateFormatVM.expirationType = EXPIRATION_TYPE_DAY;
|
|
}
|
|
else if(m_vm->dateViewModel().monthsOffset > 0) {
|
|
dateFormatVM.expirationValue = m_vm->dateViewModel().monthsOffset;
|
|
dateFormatVM.expirationType = EXPIRATION_TYPE_MONTH;
|
|
}
|
|
|
|
dateFormatVM.pattern = m_vm->dateViewModel().datetimePattern;
|
|
m_dateFormatEditor->setViewModel(dateFormatVM);
|
|
m_dateReplaceEditor->setDateModel(m_vm->dateViewModel());
|
|
|
|
if(vm->contentPattern().contains("{t}")) {
|
|
if(ui->tabWidget->indexOf(m_dateFormatEditor) == -1) {
|
|
ui->tabWidget->addTab(m_dateFormatEditor, tr(u8"日期/时间"));
|
|
}
|
|
m_dateFormatEditor->setFixedSize(ui->tabBarcode->size());
|
|
|
|
if(ui->tabWidget->indexOf(m_dateReplaceEditor) == -1) {
|
|
ui->tabWidget->addTab(m_dateReplaceEditor, tr(u8"代替"));
|
|
}
|
|
m_dateReplaceEditor->setFixedSize(ui->tabBarcode->size());
|
|
}
|
|
else {
|
|
auto index = ui->tabWidget->indexOf(m_dateFormatEditor);
|
|
if(index != -1) {
|
|
ui->tabWidget->removeTab(index);
|
|
}
|
|
|
|
index = ui->tabWidget->indexOf(m_dateReplaceEditor);
|
|
if(index != -1) {
|
|
ui->tabWidget->removeTab(index);
|
|
}
|
|
}
|
|
|
|
if(vm->contentPattern().contains("{c}")) {
|
|
// TODO:: 添加计数器选项卡
|
|
}
|
|
}
|
|
|
|
QSharedPointer<BarcodeViewModel> BarcodeEditor::viewModel() const
|
|
{
|
|
return m_vm;
|
|
}
|
|
|
|
void BarcodeEditor::onBtnFormatClicked()
|
|
{
|
|
if(!m_vm)
|
|
return;
|
|
|
|
// 显示对话框,用于针对于条形码、二维码的格式调整,例如二维码的编码类型、大小、条形码的宽度、等等
|
|
}
|
|
|
|
void BarcodeEditor::onBtnDatetimeClicked()
|
|
{
|
|
if(ui->lineEditContent->text().contains("{t}")) {
|
|
qInfo() << "Cannot add datetime format twice";
|
|
return;
|
|
}
|
|
|
|
ui->lineEditContent->insert("{t}");
|
|
ui->tabWidget->setCurrentIndex(1);
|
|
}
|
|
|
|
void BarcodeEditor::onBtnCounterClicked()
|
|
{
|
|
if(ui->lineEditContent->text().contains("{c}")) {
|
|
qInfo() << "Cannot add counter format twice";
|
|
return;
|
|
}
|
|
|
|
ui->lineEditContent->insert("{c}");
|
|
}
|
|
|
|
void BarcodeEditor::onContentChanged(QString content)
|
|
{
|
|
if(!m_vm)
|
|
return;
|
|
|
|
if(!content.contains("{t}")) {
|
|
auto index = ui->tabWidget->indexOf(m_dateFormatEditor);
|
|
if(index != -1) {
|
|
ui->tabWidget->removeTab(index);
|
|
}
|
|
|
|
index = ui->tabWidget->indexOf(m_dateReplaceEditor);
|
|
if(index != -1) {
|
|
ui->tabWidget->removeTab(index);
|
|
}
|
|
}
|
|
else {
|
|
if(ui->tabWidget->indexOf(m_dateFormatEditor) == -1) {
|
|
ui->tabWidget->addTab(m_dateFormatEditor, tr(u8"日期/时间"));
|
|
}
|
|
m_dateFormatEditor->setFixedSize(ui->tabBarcode->size());
|
|
|
|
if(ui->tabWidget->indexOf(m_dateReplaceEditor) == -1) {
|
|
ui->tabWidget->addTab(m_dateReplaceEditor, tr(u8"代替"));
|
|
}
|
|
m_dateReplaceEditor->setFixedSize(ui->tabBarcode->size());
|
|
|
|
DateFormatEditorVM dateFormatVM;
|
|
if(m_vm->dateViewModel().daysOffset > 0) {
|
|
dateFormatVM.expirationValue = m_vm->dateViewModel().daysOffset;
|
|
dateFormatVM.expirationType = EXPIRATION_TYPE_DAY;
|
|
}
|
|
else if(m_vm->dateViewModel().monthsOffset > 0) {
|
|
dateFormatVM.expirationValue = m_vm->dateViewModel().monthsOffset;
|
|
dateFormatVM.expirationType = EXPIRATION_TYPE_MONTH;
|
|
}
|
|
|
|
dateFormatVM.pattern = m_vm->dateViewModel().datetimePattern;
|
|
m_dateFormatEditor->setViewModel(dateFormatVM);
|
|
|
|
m_dateReplaceEditor->setDateModel(m_vm->dateViewModel());
|
|
}
|
|
|
|
m_vm->setContentPattern(content);
|
|
}
|