diff --git a/build2/qt/HGSaneUI/HGSaneUI.pro b/build2/qt/HGSaneUI/HGSaneUI.pro index 9e2b9a56..8a07def8 100644 --- a/build2/qt/HGSaneUI/HGSaneUI.pro +++ b/build2/qt/HGSaneUI/HGSaneUI.pro @@ -63,6 +63,8 @@ unix { ../../../modules/saneui/HGSaneUI.cpp \ ../../../modules/saneui/cfg/cJSON.c \ ../../../modules/saneui/cfg/gb_json.cpp \ + ../../../modules/saneui/app_cfg.cpp \ + ../../../modules/saneui/HGUIGlobal.cpp \ ../../../modules/saneui/cutdialog.cpp \ ../../../modules/saneui/cutpapertool.cpp \ ../../../modules/saneui/device_menu.cpp \ @@ -81,6 +83,8 @@ unix { ../../../modules/saneui/HGSaneUI.h \ ../../../modules/saneui/cfg/cJSON.h \ ../../../modules/saneui/cfg/gb_json.h \ + ../../../modules/saneui/app_cfg.h \ + ../../../modules/saneui/HGUIGlobal.h \ ../../../modules/saneui/cutdialog.h \ ../../../modules/saneui/cutpapertool.h \ ../../../modules/saneui/device_menu.h \ diff --git a/modules/saneui/HGUIGlobal.cpp b/modules/saneui/HGUIGlobal.cpp new file mode 100644 index 00000000..fd3dbbf1 --- /dev/null +++ b/modules/saneui/HGUIGlobal.cpp @@ -0,0 +1,20 @@ +#include "HGUIGlobal.h" +#include "base/HGDef.h" +#include "base/HGInc.h" +#include "base/HGUtility.h" + +QString getStdFileName(const QString &fileName) +{ + char result[512] = {0}; + HGBase_StandardiseFileName(fileName.toStdString().c_str(), result, 512); + return result; +} + +std::string getStdString(const QString &str) +{ +#ifdef HG_CMP_MSC + return str.toLocal8Bit().data(); +#else + return str.toStdString(); +#endif +} diff --git a/modules/saneui/HGUIGlobal.h b/modules/saneui/HGUIGlobal.h new file mode 100644 index 00000000..d1da4d16 --- /dev/null +++ b/modules/saneui/HGUIGlobal.h @@ -0,0 +1,10 @@ +#ifndef __HGUIGLOBAL_H__ +#define __HGUIGLOBAL_H__ + +#include + +QString getStdFileName(const QString &fileName); + +std::string getStdString(const QString &str); + +#endif /* __HGUIGLOBAL_H__ */ diff --git a/modules/saneui/app_cfg.cpp b/modules/saneui/app_cfg.cpp new file mode 100644 index 00000000..e341c9f4 --- /dev/null +++ b/modules/saneui/app_cfg.cpp @@ -0,0 +1,120 @@ +#include "app_cfg.h" +#include "base/HGInc.h" +#include "base/HGUtility.h" +#include "base/HGIni.h" +#include "HGUIGlobal.h" +#include "HGString.h" +#ifdef HG_CMP_MSC +#include +#endif + +QString getCfgValue(const char *appName, const char *key, const QString &def) +{ + HGChar cfgPath[512]; + HGBase_GetConfigPath(cfgPath, 512); + strcat(cfgPath, "config.ini"); + + HGChar value[512] = {0}; + HGBase_GetProfileString(cfgPath, appName, key, getStdString(def).c_str(), value, 512); + return StdStringToUtf8(value).c_str(); +} + +int getCfgValue(const char *appName, const char *key, int def) +{ + HGChar cfgPath[512]; + HGBase_GetConfigPath(cfgPath, 512); + strcat(cfgPath, "config.ini"); + + HGInt value = 0; + HGBase_GetProfileInt(cfgPath, appName, key, def, &value); + return value; +} + +bool getCfgValue(const char *appName, const char *key, bool def) +{ + HGChar cfgPath[512]; + HGBase_GetConfigPath(cfgPath, 512); + strcat(cfgPath, "config.ini"); + + HGInt value = 0; + HGBase_GetProfileInt(cfgPath, appName, key, (HGInt)def, &value); + return (bool)value; +} + +void saveCfgValue(const char *appName, const char *key, const QString &value) +{ + HGChar cfgPath[512]; + HGBase_GetConfigPath(cfgPath, 512); + HGBase_CreateDir(cfgPath); + strcat(cfgPath, "config.ini"); + + HGBase_SetProfileString(cfgPath, appName, key, getStdString(value).c_str()); +} + +void saveCfgValue(const char *appName, const char *key, int value) +{ + HGChar cfgPath[512]; + HGBase_GetConfigPath(cfgPath, 512); + HGBase_CreateDir(cfgPath); + strcat(cfgPath, "config.ini"); + + HGBase_SetProfileInt(cfgPath, appName, key, value); +} + +void saveCfgValue(const char *appName, const char *key, bool value) +{ + HGChar cfgPath[512]; + HGBase_GetConfigPath(cfgPath, 512); + HGBase_CreateDir(cfgPath); + strcat(cfgPath, "config.ini"); + + HGBase_SetProfileInt(cfgPath, appName, key, (HGInt)value); +} + +HGResult GetConfigPath(HGChar* configPath, HGUInt maxLen) +{ + if (NULL == configPath || 0 == maxLen) + { + return HGBASE_ERR_INVALIDARG; + } + + const char* appName = "HuaGoScan"; +#if defined(OEM_HANWANG) + appName = "HanvonScan"; +#elif defined(OEM_LISICHENG) + appName = "LanxumScan"; +#elif defined(OEM_CANGTIAN) + appName = "CumtennScan"; +#elif defined(OEM_ZHONGJING) + appName = "MicrotekScan"; +#elif defined(OEM_ZIGUANG) + appName = "UniScan"; +#endif + +#if defined(HG_CMP_MSC) + CHAR cfgPath[MAX_PATH] = { 0 }; + BOOL ret = SHGetSpecialFolderPathA(NULL, cfgPath, CSIDL_APPDATA, FALSE); + if (!ret) + return HGBASE_ERR_FAIL; + if (cfgPath[strlen(cfgPath) - 1] != '\\') + strcat(cfgPath, "\\"); + + strcat(cfgPath, appName); + strcat(cfgPath, "\\Cfg\\"); +#else + char cfgPath[512] = { 0 }; + struct passwd* pw = getpwuid(getuid()); + strcpy(cfgPath, pw->pw_dir); + if (cfgPath[strlen(cfgPath) - 1] != '/') + strcat(cfgPath, "/"); + + strcat(cfgPath, "."); + strcat(cfgPath, appName); + strcat(cfgPath, "/Cfg/"); +#endif + + if (maxLen < strlen(cfgPath) + 1) + return HGBASE_ERR_FAIL; + strcpy(configPath, cfgPath); + return HGBASE_ERR_OK; +} diff --git a/modules/saneui/app_cfg.h b/modules/saneui/app_cfg.h new file mode 100644 index 00000000..5297970f --- /dev/null +++ b/modules/saneui/app_cfg.h @@ -0,0 +1,16 @@ +#ifndef __APP_CFG_H__ +#define __APP_CFG_H__ + +#include +#include "base/HGDef.h" + +QString getCfgValue(const char *appName, const char *key, const QString &def); +int getCfgValue(const char *appName, const char *key, int def); +bool getCfgValue(const char *appName, const char *key, bool def); + +void saveCfgValue(const char *appName, const char *key, const QString &value); +void saveCfgValue(const char *appName, const char *key, int value); +void saveCfgValue(const char *appName, const char *key, bool value); + +HGResult GetConfigPath(HGChar* configPath, HGUInt maxLen); +#endif /* __APP_CFG_H__ */ diff --git a/modules/saneui/cfg/gb_json.cpp b/modules/saneui/cfg/gb_json.cpp index 6b6e1de0..97422a0b 100644 --- a/modules/saneui/cfg/gb_json.cpp +++ b/modules/saneui/cfg/gb_json.cpp @@ -1,4 +1,4 @@ - + #include "gb_json.h" #include #include @@ -1376,7 +1376,15 @@ namespace gb void sane_config_schm::remove_config(const char* name) { if (jsn_) - jsn_->remove(name); + { + if (name) + jsn_->remove(name); + else + { + jsn_->clear(); + jsn_->create_empty(); + } + } } void sane_config_schm::set_value(const char* name, const char* val, size_t bytes, bool extra) { @@ -1422,6 +1430,33 @@ namespace gb return old.size() > 0; } + bool sane_config_schm::is_equal(sane_config_schm* r) + { + std::map old; + std::string n(""), v(""); + if (r->jsn_->first_child(v, &n)) + { + do + { + old[n] = v; + } while (r->jsn_->next_child(v, &n)); + } + + if (jsn_->first_child(v, &n)) + { + do + { + if (old.count(n) == 0) + return false; + if (old[n] != v) + return false; + + old.erase(n); + } while (jsn_->next_child(v, &n)); + } + + return old.size() == 0; + } void sane_config_schm::end_setting(bool cancel) { if (in_setting_) @@ -1557,6 +1592,9 @@ namespace gb scanner_cfg::scanner_cfg() : path_(""), scanner_name_(""), global_(new json()) { default_setting_name_ = QObject::tr("default_setting").toStdString(); + user_default_ = new gb::sane_config_schm(this); + user_default_->set_scheme_name(scanner_cfg::default_setting_name_.c_str()); + init_version(); init_select(); } @@ -1605,6 +1643,11 @@ namespace gb return true; } + std::string scanner_cfg::user_default_scheme_name(void) + { + return scanner_cfg::default_setting_name_; + } + void scanner_cfg::clear(void) { global_->set_value("ver", ""); @@ -1684,10 +1727,15 @@ namespace gb return ret; } - int scanner_cfg::load_mem(const char* mem) + int scanner_cfg::load_mem(const char* mem, bool in_base64) { - base64 b64; - std::string text(b64.decode(mem, strlen(mem))); + std::string text(mem); + if (in_base64) + { + base64 b64; + + text = b64.decode(mem, strlen(mem)); + } cJSON* root = cJSON_Parse(text.c_str()); if (!root) @@ -1702,6 +1750,23 @@ namespace gb walk_sibling_schemes(root->child); cJSON_Delete(root); + user_default_->release(); + user_default_ = nullptr; + for (int i = 0; i < schemes_.size(); ++i) + { + if (schemes_[i].name == scanner_cfg::default_setting_name_) + { + user_default_ = schemes_[i].schm; + schemes_.erase(schemes_.begin() + i); + break; + } + } + if (!user_default_) + { + user_default_ = new gb::sane_config_schm(this); + } + user_default_->set_scheme_name(scanner_cfg::default_setting_name_.c_str()); + return 0; } int scanner_cfg::save(const char* file) @@ -1709,35 +1774,51 @@ namespace gb if (!file && path_.empty() && scanner_name_.empty()) return EINVAL; - std::string cont("{\"" + scanner_cfg::global_name_ + "\":"), + std::string cont(to_text(true)), f(file ? file : path_ + scanner_name_ + ".cfg"), v(""); + + FILE* dst = fopen(f.c_str(), "wb"); + + if (!dst) + return errno; + + fwrite(cont.c_str(), 1, cont.length(), dst); + fclose(dst); + + return 0; + } + + std::string scanner_cfg::to_text(bool in_base64) + { + std::string cont("{\"" + scanner_cfg::global_name_ + "\":"), + v(""); int sel = -1; + base64 b64; if (!global_->get_value("ver", v) || v.empty()) init_version(); if (!global_->get_value(scanner_cfg::cur_sel_.c_str(), sel) || sel >= schemes_.size()) init_select(); + // global at first ... cont += global_->to_string(false); - for (auto& v: schemes_) + + // user fixed schemes ... + for (auto& v : schemes_) { cont += ",\"" + sane_config_schm::to_hex_letter(v.name.c_str(), v.name.length()) + "\":"; cont += v.schm->to_text_stream(false, false); } + + // default setting at last ... + cont += ",\"" + sane_config_schm::to_hex_letter(scanner_cfg::default_setting_name_.c_str(), scanner_cfg::default_setting_name_.length()) + "\":"; + cont += user_default_->to_text_stream(false, false); + + // final ... cont += "}"; - base64 b64; - FILE* dst = fopen(f.c_str(), "wb"); - - if (!dst) - return errno; - - f = b64.encode(cont.c_str(), cont.length()); - fwrite(f.c_str(), 1, f.length(), dst); - fclose(dst); - - return 0; + return std::move(in_base64 ? b64.encode(cont.c_str(), cont.length()) : cont); } void scanner_cfg::get_all_schemes(std::vector& schemes) @@ -1752,9 +1833,14 @@ namespace gb if (scheme_name && *scheme_name) { - std::vector::iterator it = std::find(schemes_.begin(), schemes_.end(), scheme_name); - if (it != schemes_.end()) - found = it->schm; + if (scanner_cfg::default_setting_name_ == scheme_name) + found = user_default_; + else + { + std::vector::iterator it = std::find(schemes_.begin(), schemes_.end(), scheme_name); + if (it != schemes_.end()) + found = it->schm; + } } else { @@ -1763,6 +1849,8 @@ namespace gb global_->get_value(scanner_cfg::cur_sel_.c_str(), ind); if (ind >= 0 && ind < schemes_.size()) found = schemes_[ind].schm; + else + found = user_default_; } if (found) @@ -1808,6 +1896,7 @@ namespace gb v.schm->release(); schemes_.clear(); + user_default_->remove_config(nullptr); } bool scanner_cfg::select_scheme(const char* scheme_name) { @@ -1825,15 +1914,22 @@ namespace gb { if (!cp_from_name) return nullptr; - else if (scanner_cfg::default_setting_name_ == cp_from_name) - return new sane_config_schm(); else { - std::vector::iterator it = std::find(schemes_.begin(), schemes_.end(), cp_from_name); - if (it == schemes_.end()) + std::string cont(""); + if (scanner_cfg::default_setting_name_ == cp_from_name) + { + cont = std::move(user_default_->to_text_stream()); + } + else + { + std::vector::iterator it = std::find(schemes_.begin(), schemes_.end(), cp_from_name); + if (it != schemes_.end()) + cont = std::move(it->schm->to_text_stream()); + } + if (cont.empty()) return nullptr; - std::string cont(it->schm->to_text_stream()); sane_config_schm* schm = new sane_config_schm(); schm->load_from_mem(cont.c_str()); diff --git a/modules/saneui/cfg/gb_json.h b/modules/saneui/cfg/gb_json.h index ffc79d94..27c1f729 100644 --- a/modules/saneui/cfg/gb_json.h +++ b/modules/saneui/cfg/gb_json.h @@ -1,4 +1,4 @@ -#pragma once +#pragma once #if defined(WIN32) || defined(_WIN64) #include @@ -147,9 +147,10 @@ namespace gb void begin_setting(bool restore = false); void config_changed(const char* name, const char* val, size_t bytes, bool extra = false); void config_changed(int sn, const char* val, size_t bytes, bool extra = false); - void remove_config(const char* name); + void remove_config(const char* name/*nullptr for clear*/); void set_value(const char* name, const char* val, size_t bytes, bool extra = false); bool has_changed(int* items = nullptr); + bool is_equal(sane_config_schm* r); void end_setting(bool cancel); int id_from_name(const char* name); std::string to_text_stream(bool b64 = true, bool with_ver = true); @@ -189,6 +190,7 @@ namespace gb } }CFGSCHM; std::vector schemes_; + sane_config_schm* user_default_; static std::string global_name_; static std::string cur_sel_; @@ -213,11 +215,13 @@ namespace gb void* func_param; }UDF, *LPUDF; static bool update(const char* file, LPUDF func); + static std::string user_default_scheme_name(void); public: int load_file(const char* file); - int load_mem(const char* mem); + int load_mem(const char* mem, bool in_base64 = true); int save(const char* file = nullptr); + std::string to_text(bool in_base64); void get_all_schemes(std::vector& schemes); // return all schemes name queue, the first is always be 'Default settings' sane_config_schm* get_scheme(const char* scheme_name = nullptr/*return current scheme if was null*/); // call sane_config_schm::release() if not use anymore diff --git a/modules/saneui/cutdialog.cpp b/modules/saneui/cutdialog.cpp index ff701baa..be564c48 100644 --- a/modules/saneui/cutdialog.cpp +++ b/modules/saneui/cutdialog.cpp @@ -192,6 +192,7 @@ void cutDialog::setCutRectPixel(QRectF &rect) m_startPoint.setY(scaleRec.height()*rect.y()/(paperHeight*realRate*0.03937*dpi)); m_endPoint.setX(m_startPoint.x()+scaleRec.width()*rect.width()/(paperWidth*realRate*0.03937*dpi)); m_endPoint.setY(m_startPoint.y()+scaleRec.height()*rect.height()/(paperHeight*realRate*0.03937*dpi)); + m_rect = QRectF(m_startPoint,m_endPoint); update(); } @@ -434,7 +435,7 @@ void cutDialog::mousePosition(const QPoint& e) void cutDialog::refreshView() { - if(sizeType == INCH){ + if(MILLIM == sizeType || sizeType == INCH){ emit cutRectX(QString::number(getCutRectStartPos().x(),'f',2).toDouble()); emit cutRectY(QString::number(getCutRectStartPos().y(),'f',2).toDouble()); emit cutRectWidth(QString::number(getCutRectSize().width(),'f',2).toDouble()); diff --git a/modules/saneui/cutpapertool.cpp b/modules/saneui/cutpapertool.cpp index ab2f84e0..4c035795 100644 --- a/modules/saneui/cutpapertool.cpp +++ b/modules/saneui/cutpapertool.cpp @@ -3,7 +3,7 @@ #include #include -CutPaperTool::CutPaperTool(QWidget *parent) : +CutPaperTool::CutPaperTool(int d, const QString& t, int w, int unit, QWidget *parent) : QDialog(parent), ui(new Ui::CutPaperTool) { @@ -14,12 +14,19 @@ CutPaperTool::CutPaperTool(QWidget *parent) : connect(ui->widget,SIGNAL(cutRectWidth(double)),this,SLOT(cutRectWidthSlot(double))); connect(ui->widget,SIGNAL(cutRectHeight(double)),this,SLOT(cutRectHeightSlot(double))); connect(ui->widget,SIGNAL(lineEditEnable(bool)),this,SLOT(lineEditEnableSlot(bool))); - //setDpi(200); - //setPaperType(200,"A4",400); ui->widget->setSizeType(MILLIM); + + dpi = d; + ui->dpiLab->setText(QString::number(dpi)); + ui->widget->setDpiValue(dpi); + paperType = t; + ui->paperLab->setText(paperType); + ui->widget->setPaperSize(paperType, w); setSizeLabel(); - //ui->pbtn_init->setFixedWidth(160); this->setFixedWidth(ui->widget->width()+40); + setSizeInit(); + + ui->comboBox_2->setCurrentIndex(unit); } CutPaperTool::~CutPaperTool() @@ -32,32 +39,27 @@ void CutPaperTool::paintEvent(QPaintEvent *) } -void CutPaperTool::setPaperType(const int d, const QString &t, const int& w) -{ - dpi = d; - ui->dpiLab->setText(QString::number(dpi)); - ui->widget->setDpiValue(dpi); - paperType = t; - ui->paperLab->setText(paperType); - ui->widget->setPaperSize(paperType,w); - //ui->widget->refreshView(); - setSizeLabel(); - this->setFixedWidth(ui->widget->width()+40); - setSizeInit(); -} - QRectF CutPaperTool::getCutRectPixel() { return QRectF(ui->widget->getCutRectPixel()); } -void CutPaperTool::setCutRect(QRectF &rect) +void CutPaperTool::setCutRectPixel(QRectF &rect) { ui->widget->setCutRectPixel(rect); ui->startXEdt->setText(QString::number(rect.x()/dpi/0.03937)); ui->startYEdt->setText(QString::number(rect.y()/dpi/0.03937)); ui->rectWidth->setText(QString::number(rect.width()/dpi/0.03937)); ui->rectHeight->setText(QString::number(rect.height()/dpi/0.03937)); + + update(); + setSizeLabel(); + ui->widget->refreshView(); +} + +int CutPaperTool::getUnit() +{ + return ui->comboBox_2->currentIndex(); } void CutPaperTool::setSizeLabel() @@ -87,12 +89,6 @@ void CutPaperTool::setSizeInit() ui->rectHeight->setText(QString::number(int(ui->widget->getPaperSize().height()))); } -void CutPaperTool::setSizeInit(QRectF& rect){ - setSizeInit(); - if(rect != QRectF(0,0,0,0)) - setCutRect(rect); -} - void CutPaperTool::cutRectXSlot(double x) { ui->startXEdt->setText(QString::number(x)); @@ -174,7 +170,8 @@ void CutPaperTool::on_rectHeight_textEdited(QString arg1) void CutPaperTool::on_comboBox_2_currentIndexChanged(int index) { - switch(index){ + switch(index) + { case 0: ui->xLabel->setText("mm"); ui->yLabel->setText("mm"); @@ -197,6 +194,7 @@ void CutPaperTool::on_comboBox_2_currentIndexChanged(int index) ui->widget->setSizeType(PIXEL); break; } + update(); setSizeLabel(); ui->widget->refreshView(); diff --git a/modules/saneui/cutpapertool.h b/modules/saneui/cutpapertool.h index cf6d40eb..d23bd5d5 100644 --- a/modules/saneui/cutpapertool.h +++ b/modules/saneui/cutpapertool.h @@ -13,18 +13,16 @@ class CutPaperTool : public QDialog Q_OBJECT public: - explicit CutPaperTool(QWidget *parent = nullptr); + explicit CutPaperTool(int d, const QString& t, int w, int unit, QWidget *parent = nullptr); ~CutPaperTool(); - void setPaperType(const int dpi, const QString& t,const int& w=200); QRectF getCutRectPixel(); - void setCutRect(QRectF& rect); - void setSizeInit(); - void setSizeInit(QRectF& rect); - + void setCutRectPixel(QRectF& rect); + int getUnit(); private: void paintEvent(QPaintEvent *); void setSizeLabel(); + void setSizeInit(); private slots: void cutRectXSlot(double x); diff --git a/modules/saneui/hg_settingdialog.cpp b/modules/saneui/hg_settingdialog.cpp index 12034657..c8bab67c 100644 --- a/modules/saneui/hg_settingdialog.cpp +++ b/modules/saneui/hg_settingdialog.cpp @@ -1,2165 +1,2599 @@ -#include "hg_settingdialog.h" -#include -#include -#include "cutpapertool.h" -#include "setpicclrtool.h" -#include "base/HGDef.h" -#include "base/HGUtility.h" -#include "HGString.h" -#include "sane/sane_option_definitions.h" -#include "lang/app_language.h" -#include "dialog_input.h" -#include -#include "device_menu.h" -#include "dialog_device_scan.h" -std::string hg_settingdialog::property_combox_data_type_ = "combox_value_type"; - -hg_settingdialog::hg_settingdialog(const SANEAPI* saneApi, SANE_Handle handle, const char *devName, QWidget *parent) - : QDialog(parent) - , save_(false) - , btn_cut_area_(nullptr), btn_gamma_(nullptr), clicked_gamma_(false) - , custom_area_lable_(nullptr), comb_(nullptr) - , m_devHandle(handle) - , m_devName(devName) -{ - HGChar cfgpath[512] = {0}; - HGBase_GetConfigPath(cfgpath, 512); - - HGBase_CreateDir(cfgpath); - dev_que_.set_root_dir(cfgpath); - QString old = QString::fromStdString(cfgpath) + PATH_SYMBOL + "scanner.schm"; - if(QFile::exists(old)) - dev_que::update_old_cfg(old.toStdString().c_str()); - - int pid = 0; - saneApi->sane_control_option_api(m_devHandle, (SANE_Int)0x8853, SANE_ACTION_GET_VALUE, &pid, NULL); - - char buf[10] = { 0 }; - sprintf(buf, "%x", pid); - std::string deviceName = m_devName; - if (pid != 0) - { - deviceName = deviceName.substr(0, deviceName.find(" ")) + " " + buf; - } - dev_que_.add_scanner(deviceName.c_str()); - dev_que_.open_scanner(saneApi, handle, deviceName.c_str(), false); - - std::string n(dev_que_.opened_scanner_name()); - for(int i = 0; i < dev_que_.scanners(); ++i) - { - SCANNER s = dev_que_.get_at(i); - if(s.name == n) - { - cur_cfg_ = s.cfg; - break; - } - } - - cur_scheme_ = cur_cfg_->get_scheme(); - if(!cur_scheme_) - cur_scheme_ = new gb::sane_config_schm(); - cur_scheme_->begin_setting(); - - m_dpiId = -1; - m_dpiValue = 200; - m_paperSizeId = -1; - m_paperSizeValue.clear(); - m_cutLeftId = -1; - m_cutTopId = -1; - m_cutRightId = -1; - m_cutBottomId = -1; - m_cutWidth = 210; - m_cutHeight = 297; - m_cutLeftValue = 0; - m_cutTopValue = 0; - m_cutRightValue = 210; - m_cutBottomValue = 297; - - m_colorModeId = -1; - m_colorModeValue.clear(); - memset(&m_gammaData, 0, sizeof(m_gammaData)); - for(int i = 0; i < sizeof(m_gammaData.table) / sizeof(m_gammaData.table[0]); ++i) - m_gammaData.table[i] = i & 0x0ff; - - memcpy(&m_saneAPI, saneApi, sizeof(SANEAPI)); - - initUi(); - on_current_scheme_changed(); - //getAppVersion(); -} - -hg_settingdialog::~hg_settingdialog() -{ - QCoreApplication::removeTranslator(&m_translator); - if (20127 != m_langCode) - QCoreApplication::removeTranslator(&m_translator_qt); - - cur_scheme_->release(); - cur_cfg_->release(); -} - -void hg_settingdialog::initUi() -{ - updateOpt(); - createUI(); - -#if defined(OEM_ZHONGJING) - setWindowTitle("Microtek DocWizard EX TWAIN"); -#else - setWindowTitle(QString::fromStdString(dev_que_.opened_scanner_name())); -#endif - - setWindowFlags(Qt::Dialog | Qt::WindowCloseButtonHint); - resize(740, height()); -} - -void hg_settingdialog::updateOpt() -{ - bool first = true; - - m_list_defaultOptions.clear(); - m_list_getOpt.clear(); - - SANE_Int dev_options = 0; - m_saneAPI.sane_control_option_api(m_devHandle, 0, SANE_ACTION_GET_VALUE, &dev_options, nullptr); - for (int i = 1, j= dev_options; i < j; i++) - { - const SANE_Option_Descriptor* opt = m_saneAPI.sane_get_option_descriptor_api(m_devHandle, i); - SANE_Int method = 0; - if (opt == nullptr) - { - m_list_defaultOptions.append(QPair(opt, QVariant(0))); - } - else - { - if(opt->type == SANE_TYPE_INT) - { - SANE_Int init = 0; - - m_saneAPI.sane_control_option_api(m_devHandle, i, SANE_ACTION_GET_VALUE, &init, &method); - m_list_defaultOptions.append(QPair(opt, QVariant(init))); - m_list_getOpt.append(QPair(i, opt)); - - if(first) - { - unsigned int n = i; - dev_que::get_default_value(&m_saneAPI, m_devHandle, n, &init); - cur_scheme_->set_default_value(i, opt->name, (char*)&init, sizeof(init)); - } - } - else if(opt->type == SANE_TYPE_FIXED) - { - SANE_Fixed init = 0; - - m_saneAPI.sane_control_option_api(m_devHandle, i, SANE_ACTION_GET_VALUE, &init, &method); - m_list_defaultOptions.append(QPair(opt, QVariant(init))); - m_list_getOpt.append(QPair(i, opt)); - - if(first) - { - unsigned int n = i; - dev_que::get_default_value(&m_saneAPI, m_devHandle, n, &init); - cur_scheme_->set_default_value(i, opt->name, (char*)&init, sizeof(init)); - } - } - else if(opt->type == SANE_TYPE_BOOL) - { - SANE_Bool init = 0; - - m_saneAPI.sane_control_option_api(m_devHandle, i, SANE_ACTION_GET_VALUE, &init, &method); - m_list_defaultOptions.append(QPair(opt, QVariant(init))); - m_list_getOpt.append(QPair(i, opt)); - - if(first) - { - unsigned int n = i; - dev_que::get_default_value(&m_saneAPI, m_devHandle, n, &init); - cur_scheme_->set_default_value(i, opt->name, (char*)&init, sizeof(init)); - } - } - else if(opt->type == SANE_TYPE_STRING) - { - char *init = (char*)malloc(opt->size * 2 + 4); - - m_saneAPI.sane_control_option_api(m_devHandle, i, SANE_ACTION_GET_VALUE, init, &method); - QString str = QString::fromStdString(init); - m_list_defaultOptions.append(QPair(opt, QVariant(QString::fromStdString(init)))); - m_list_getOpt.append(QPair(i, opt)); - - if(first) - { - unsigned int n = i; - int err = dev_que::get_default_value(&m_saneAPI, m_devHandle, n, init); - (void)err; - std::string langCN(to_default_language(init, nullptr)); - cur_scheme_->set_default_value(i, opt->name, &langCN[0], langCN.length()); - } - free(init); - } - else - { - m_list_defaultOptions.append(QPair(opt, QVariant(0))); - m_list_getOpt.append(QPair(i, opt)); - } - } - } -} - -QString hg_settingdialog::find_current_scheme_menu(int *scheme_id) -{ - QString text(comb_->currentText()); - - if(scheme_id) - { - if(comb_->currentIndex() >= 0 && comb_->currentIndex() < comb_->count()) - *scheme_id = comb_->currentIndex(); - else { - *scheme_id = -1; - } - } - - return text; -} -void hg_settingdialog::create_scheme_management_ui(QVBoxLayout* layout) -{ - QLabel *title = new QLabel(this); - bool enabled = false; - QHBoxLayout *hLayout = new QHBoxLayout(); - int width = 180; - std::vector schemes; - std::string cur_schm(cur_cfg_->get_current_scheme_name()); - - cur_cfg_->get_all_schemes(schemes); - comb_ = new QComboBox(this); - comb_->addItem(tr("Default scheme")); - layout->addSpacing(30); - for(int i = 1; i < (int)schemes.size(); ++i) - { - comb_->addItem(QString::fromStdString(schemes[i])); - if(cur_schm == schemes[i]) - { - enabled = true; - comb_->setCurrentText(QString::fromStdString(schemes[i])); - } - } - - if(!enabled) - comb_->setCurrentIndex(0); - - title->setFixedWidth(width); - comb_->setFixedWidth(width); - - title->setText(tr("existing configuration scheme")); - layout->addWidget(title); - layout->addWidget(comb_); - - layout->addSpacing(10); - - m_pbtn_addNew = new QPushButton(this); - m_pbtn_addNew->setText(tr("Add new")); - m_pbtn_addNew->setFixedWidth(width / 2); - layout->addWidget(m_pbtn_addNew); - connect(m_pbtn_addNew, SIGNAL(clicked(bool)), this, SLOT(slot_pushButton_scheme_management())); - - layout->addSpacing(10); - - del_this_ = new QPushButton(this); - del_this_->setText(tr("Delete")); - del_this_->setEnabled(enabled); - del_this_->setFixedWidth(width / 2); - layout->addWidget(del_this_); - connect(del_this_, SIGNAL(clicked(bool)), this, SLOT(slot_pushButton_scheme_management())); - - layout->addSpacing(10); - - del_all_ = new QPushButton(this); - del_all_->setText(tr("Delete all")); - del_all_->setEnabled(enabled); - del_all_->setFixedWidth(width / 2); - layout->addWidget(del_all_); - connect(del_all_, SIGNAL(clicked(bool)), this, SLOT(slot_pushButton_scheme_management())); - - layout->addStretch(); - - title = new QLabel(this); - title->setText(tr("confgiuration information:")); - layout->addWidget(title); - - sketch_ = new QTextEdit(this); - sketch_->setReadOnly(true); - sketch_->setFixedSize(width, 200); - layout->addWidget(sketch_); - - connect(comb_, SIGNAL(currentTextChanged(const QString)), this, SLOT(on_current_scheme_changed())); - //on_current_scheme_changed(); -} -void hg_settingdialog::createUI() -{ - QTabWidget *tabWidgetCreation = new QTabWidget(this); - - QPushButton *buttonAbout = new QPushButton(this); - buttonAbout->setText(tr("about...")); - QPushButton *buttonOk = new QPushButton(this); - buttonOk->setText(tr("ok")); - QPushButton *buttonCancel = new QPushButton(this); - buttonCancel->setText(tr("cancel")); - QHBoxLayout *hlayoutOkAndCancel = new QHBoxLayout; - hlayoutOkAndCancel->addStretch(); - hlayoutOkAndCancel->addWidget(buttonAbout); - hlayoutOkAndCancel->addWidget(buttonOk); - hlayoutOkAndCancel->addWidget(buttonCancel); - QWidget *widgetOkAndCancel = new QWidget(); - widgetOkAndCancel->setLayout(hlayoutOkAndCancel); - connect(buttonAbout, SIGNAL(clicked(bool)), this, SLOT(slot_buttonAboutClicked())); - connect(buttonOk, SIGNAL(clicked(bool)), this, SLOT(slot_buttonOkClicked())); - connect(buttonCancel, SIGNAL(clicked(bool)), this, SLOT(slot_buttonCancelClicked())); - - QHBoxLayout *h = new QHBoxLayout(); - QVBoxLayout *v1 = new QVBoxLayout(), - *v2 = new QVBoxLayout(); - create_scheme_management_ui(v1); - v2->addWidget(tabWidgetCreation); - - QGroupBox *grp = new QGroupBox(tr("configuration scheme management"), this); - grp->setLayout(v1); - grp->setFixedSize(195, 500); - - h->addWidget(grp); - h->addLayout(v2); - - QVBoxLayout* mainVerticalLayout = new QVBoxLayout(this); - mainVerticalLayout->addLayout(h); -// mainVerticalLayout->addWidget(tabWidgetCreation); - mainVerticalLayout->addWidget(widgetOkAndCancel); - this->setLayout(mainVerticalLayout); - - QScrollArea* scrollArea = new QScrollArea; - scrollArea->setWidgetResizable(true); - scrollArea->setAlignment(Qt::AlignCenter); - QFormLayout* layout = new QFormLayout; - - QWidget* widget = new QWidget; - widget->setLayout(layout); - - bool isBegin = true; - std::string cur_val(""); - for (int i = 0; i < m_list_defaultOptions.size(); i++) - { - const SANE_Option_Descriptor* opt = reinterpret_cast(m_list_defaultOptions.at(i).first); - int ind = -1; - - if(opt == nullptr) continue; - h = nullptr; - cur_scheme_->get_config(opt->name, cur_val); - switch (opt->type) - { - case SANE_TYPE_BOOL: - { - QCheckBox *checkBoxCreation = new QCheckBox; - - if (strcmp(opt->name, SANE_STD_OPT_NAME_CUSTOM_AREA) == 0) - { - QWidget* widget_cbtn_pbtn = new QWidget; - widget_cbtn_pbtn->setMaximumWidth(200); - - QLabel *label = new QLabel; - label->setText(QString::fromStdString(opt->title) + QString(" : ")); - - btn_cut_area_ = new QPushButton; - btn_cut_area_->setText(tr("regional crop")); - btn_cut_area_->setFixedWidth(150); - - QHBoxLayout *hLayout = new QHBoxLayout; - hLayout->addWidget(checkBoxCreation); - hLayout->addWidget(btn_cut_area_); - widget_cbtn_pbtn->setLayout(hLayout); - - custom_area_lable_ = label; - - reinterpret_cast(widget->layout())->addRow(label, widget_cbtn_pbtn); - - connect(btn_cut_area_, SIGNAL(clicked(bool)), this, SLOT(slot_cutButtonClicked())); - } - else if (strcmp(opt->name, SANE_STD_OPT_NAME_IS_CUSTOM_GAMMA) == 0) - { - QWidget* widget_cbtn_pbtn = new QWidget(scrollArea); - widget_cbtn_pbtn->setMaximumWidth(200); - btn_gamma_ = new QPushButton(widget_cbtn_pbtn); - btn_gamma_->setText(tr("custom tone curve")); - btn_gamma_->setFixedWidth(150); - - QHBoxLayout *hLayout = new QHBoxLayout; - hLayout->addWidget(checkBoxCreation); - hLayout->addWidget(btn_gamma_); - widget_cbtn_pbtn->setLayout(hLayout); - - reinterpret_cast(widget->layout())->addRow(opt->title + QString(" : "), widget_cbtn_pbtn); - connect(btn_gamma_, SIGNAL(clicked(bool)), this, SLOT(slot_gammaButtonClicked())); - } - else - reinterpret_cast(widget->layout())->addRow(opt->title + QString(" : "), checkBoxCreation); - - checkBoxCreation->setToolTip(opt->desc); - int id = i + 1; - bool enable = *(bool*)&cur_val[0]; - checkBoxCreation->setProperty("controls_id", id); - checkBoxCreation->setChecked(enable); - if (strcmp(opt->name, SANE_STD_OPT_NAME_CUSTOM_AREA) == 0) - btn_cut_area_->setEnabled(enable); - else if (strcmp(opt->name, SANE_STD_OPT_NAME_IS_CUSTOM_GAMMA) == 0) - btn_gamma_->setEnabled(enable); - connect(checkBoxCreation, SIGNAL(stateChanged(int)), this, SLOT(slot_checkedClicked())); - m_list_widgets.append(checkBoxCreation); - - m_list_getOpt.append(QPair(id, opt)); - break; - } - - case SANE_TYPE_INT: - { - switch(opt->constraint_type) - { - case SANE_CONSTRAINT_NONE: - { - QSpinBox* spinBox = new QSpinBox(scrollArea); - spinBox->setMinimumWidth(150); - spinBox->setToolTip(opt->desc); - spinBox->setRange(1, 1000); - int id = i + 1; - spinBox->setProperty("controls_id", id); - - QHBoxLayout* hLayout = new QHBoxLayout; - hLayout->addWidget(spinBox); - hLayout->addStretch(); - reinterpret_cast(widget->layout())->addRow(opt->title + QString(" : "), spinBox); - - m_list_widgets.append(spinBox); - m_list_getOpt.append(QPair(id, opt)); - - spinBox->setValue(*(int*)&cur_val[0]); - connect(spinBox, SIGNAL(valueChanged(int)), this, SLOT(slot_spinBoxClicked(int))); - break; - } - - case SANE_CONSTRAINT_RANGE: - { - QWidget* widget_slider_spin = new QWidget(scrollArea); - widget_slider_spin->setMinimumWidth(300); - - QSlider* sliderCreation = new QSlider(widget_slider_spin); - - if (0 == strcmp(opt->name, SANE_STD_OPT_NAME_THRESHOLD) || 0 == strcmp(opt->name, SANE_STD_OPT_NAME_ANTI_NOISE_LEVEL) || - 0 == strcmp(opt->name, SANE_STD_OPT_NAME_MARGIN)) - { - sliderCreation->installEventFilter(this); - } - - sliderCreation->setOrientation(Qt::Horizontal); - sliderCreation->setMinimumWidth(120); - sliderCreation->setRange(opt->constraint.range->min, opt->constraint.range->max); - sliderCreation->setToolTip(opt->desc); - sliderCreation->setProperty("controls_id", i+1); - sliderCreation->setValue(m_list_defaultOptions.at(i).second.toInt()); - - QSpinBox* spinBox = new QSpinBox(widget_slider_spin); - spinBox->setMinimumWidth(150); - spinBox->setToolTip(opt->desc); - spinBox->setRange(opt->constraint.range->min, opt->constraint.range->max); - - spinBox->setSingleStep(1); - spinBox->setValue(m_list_defaultOptions.at(i).second.toInt()); - int id = i + 1; - spinBox->setProperty("controls_id", id); - m_list_sliderSpinbox.append(QPair(sliderCreation, spinBox)); - - QHBoxLayout* hLayout = new QHBoxLayout; - hLayout->addWidget(sliderCreation); - hLayout->addWidget(spinBox); -// hLayout->addStretch(); - widget_slider_spin->setLayout(hLayout); - reinterpret_cast(widget->layout())->addRow(opt->title + QString(" : "), widget_slider_spin); - - m_list_widgets.append(sliderCreation); - m_list_widgets.append(spinBox); - m_list_getOpt.append(QPair(id, opt)); - - int cur = *(int*)&cur_val[0]; - spinBox->setValue(cur); - sliderCreation->setValue(cur); - - connect(spinBox, SIGNAL(valueChanged(int)), this, SLOT(slot_spinBoxClicked(int))); - connect(sliderCreation, SIGNAL(valueChanged(int)), this, SLOT(slot_sliderClicked(int))); - - break; - } - - case SANE_CONSTRAINT_WORD_LIST: - { - QComboBox* comboBoxCreation = new QComboBox(scrollArea); - comboBoxCreation->setToolTip(opt->desc); - int id = i + 1; - comboBoxCreation->setProperty("controls_id", id); - reinterpret_cast(widget->layout())->addRow(opt->title + QString(" : "), comboBoxCreation); - - auto p_str = opt->constraint.word_list; - char buf[20]; - for(SANE_Int i = 0; i < p_str[0]; ++i) - { - sprintf(buf, "%d", p_str[i + 1]); - comboBoxCreation->addItem(QString::fromStdString(buf)); - } - sprintf(buf, "%d", m_list_defaultOptions.at(i).second.toInt()); - comboBoxCreation->setProperty(hg_settingdialog::property_combox_data_type_.c_str(), COMBO_VAL_INT); - m_list_widgets.append(comboBoxCreation); - m_list_getOpt.append(QPair(id, opt)); - - char nstr[40] = {0}; - sprintf(nstr, "%d", *(int*)&cur_val[0]); - comboBoxCreation->setCurrentText(QString::fromStdString(nstr)); - connect(comboBoxCreation, SIGNAL(currentTextChanged(const QString)), this, SLOT(slot_string_list_comboBoxClicked())); - break; - } - - case SANE_CONSTRAINT_STRING_LIST: - break; - } - break; - } - - case SANE_TYPE_FIXED: - { - QWidget* widget_slider_spin = new QWidget(scrollArea); - widget_slider_spin->setMinimumWidth(300); - QSlider* sliderCreation = new QSlider(widget_slider_spin); - sliderCreation->setOrientation(Qt::Horizontal); - sliderCreation->setMinimumWidth(120); - sliderCreation->setToolTip(opt->desc); - int id = i + 1; - sliderCreation->setProperty("controls_id", id); - sliderCreation->setRange(SANE_UNFIX(opt->constraint.range->min) * 100, SANE_UNFIX(opt->constraint.range->max) * 100); - sliderCreation->setValue(SANE_UNFIX(m_list_defaultOptions.at(i).second.toDouble()) * 100); - - QDoubleSpinBox* spinBox = new QDoubleSpinBox(widget_slider_spin); - spinBox->setMinimumWidth(150); - spinBox->setToolTip(opt->desc); - spinBox->setDecimals(2); - spinBox->setSingleStep(0.01); - spinBox->setRange(SANE_UNFIX(opt->constraint.range->min), SANE_UNFIX(opt->constraint.range->max)); - spinBox->setValue(sliderCreation->value() * spinBox->singleStep()); - spinBox->setProperty("controls_id", id); - - m_list_sliderSpinbox.append(QPair(sliderCreation, spinBox)); - - QHBoxLayout* hLayout = new QHBoxLayout; - hLayout->addWidget(sliderCreation); - hLayout->addWidget(spinBox); -// hLayout->addStretch(); - widget_slider_spin->setLayout(hLayout); - reinterpret_cast(widget->layout())->addRow(opt->title + QString(" : "), widget_slider_spin); - - m_list_widgets.append(sliderCreation); - m_list_widgets.append(spinBox); - - m_list_getOpt.append(QPair(id, opt)); -// iniRead(md5(opt->title), id, sliderCreation); -// iniRead(md5(opt->title), id, spinBox); - - float v = SANE_UNFIX(*(SANE_Fixed*)&cur_val[0]); - sliderCreation->setValue(v * 100); - spinBox->setValue(sliderCreation->value() * spinBox->singleStep()); - connect(spinBox, SIGNAL(valueChanged(double)), this, SLOT(slot_doubleSpinboxClicked(double))); - connect(sliderCreation, SIGNAL(valueChanged(int)), this, SLOT(slot_sliderClicked(int))); - break; - } - - case SANE_TYPE_STRING: - { - switch(opt->constraint_type) - { - case SANE_CONSTRAINT_NONE: - { - QLineEdit *lineEdit = new QLineEdit(scrollArea); - lineEdit->setToolTip(opt->desc); - int id = i + 1; - lineEdit->setProperty("controls_id", id); - reinterpret_cast(widget->layout())->addRow(opt->title + QString(" : "), lineEdit); - - m_list_widgets.append(lineEdit); - m_list_getOpt.append(QPair(id, opt)); -// iniRead(md5(opt->title), id, lineEdit); - - lineEdit->setText(QString::fromStdString(cur_val)); - connect(lineEdit, SIGNAL(textChanged(const QString&)), this, SLOT(slot_lineEditInput())); - break; - } - - case SANE_CONSTRAINT_RANGE: - break; - case SANE_CONSTRAINT_WORD_LIST: - break; - - case SANE_CONSTRAINT_STRING_LIST: - { - QComboBox* comboBoxCreation = new QComboBox(scrollArea); - comboBoxCreation->setToolTip(opt->desc); - int id = i + 1; - comboBoxCreation->setProperty("controls_id", id); - reinterpret_cast(widget->layout())->addRow(opt->title + QString(" : "), comboBoxCreation); - - auto p_str = opt->constraint.string_list; - QStringList stringList; - while(*p_str) - { - stringList.append(*p_str); - p_str++; - } - - if(stringList.isEmpty() != true) - { - for(int i = 0; i < (stringList.size()); i++) - comboBoxCreation->addItem(stringList.at(i)); - } - comboBoxCreation->setCurrentText(m_list_defaultOptions.at(i).second.toString()); - comboBoxCreation->setProperty(hg_settingdialog::property_combox_data_type_.c_str(), COMBO_VAL_STRING); - //printf("Option %02d default value is: %s\n", i + 1, m_list_defaultOptions.at(i).second.toString().data()); - - m_list_widgets.append(comboBoxCreation); - - m_list_getOpt.append(QPair(id, opt)); -// iniRead(md5(opt->title), id, comboBoxCreation); - - comboBoxCreation->setCurrentText(QString::fromStdString(cur_val)); - connect(comboBoxCreation, SIGNAL(currentTextChanged(const QString)), this, SLOT(slot_string_list_comboBoxClicked())); - break; - } - } - break; - } - - case SANE_TYPE_BUTTON: - { - QPushButton* pushButton = new QPushButton(this); - pushButton->setText(opt->title); - pushButton->setToolTip(opt->desc); - int id = i + 1; - pushButton->setProperty("controls_id", id); - hlayoutOkAndCancel->insertWidget(0, pushButton, 0, Qt::AlignRight); - - connect(pushButton, SIGNAL(clicked(bool)), this, SLOT(slot_pushButtonClicked())); - break; - } - - case SANE_TYPE_GROUP: - { - if (isBegin) - { - scrollArea->setWindowTitle(opt->title); - isBegin = false; - }else{ - scrollArea->setWidget(widget); - tabWidgetCreation->addTab(scrollArea, scrollArea->windowTitle()); - scrollArea = new QScrollArea; - scrollArea->setWidgetResizable(true); - scrollArea->setWindowTitle(opt->title); - layout = new QFormLayout; - widget = new QWidget; - widget->setLayout(layout); - } - - m_list_widgets.append(nullptr); - break; - } - } //switch(opt->type) - -// if (Utf8ToStdString(opt->title) == "分辨率") - if (strcmp(opt->name, SANE_STD_OPT_NAME_RESOLUTION) == 0) - { - m_dpiId = i + 1; - m_dpiValue = m_list_defaultOptions.at(i).second.toInt(); - } -// else if (Utf8ToStdString(opt->title) == "纸张尺寸") - else if (strcmp(opt->name, SANE_STD_OPT_NAME_PAPER) == 0) - { - m_paperSizeId = i + 1; - m_paperSizeValue = m_list_defaultOptions.at(i).second.toString(); - } - - if (0 == strcmp(opt->name, SANE_STD_OPT_NAME_CUSTOM_AREA_LEFT)) - { - m_cutLeftId = i + 1; - m_cutLeftValue = SANE_UNFIX(m_list_defaultOptions.at(i).second.toInt()); - if (opt->constraint_type == SANE_CONSTRAINT_RANGE) - m_cutWidth = SANE_UNFIX(opt->constraint.range->max); - } - else if (0 == strcmp(opt->name, SANE_STD_OPT_NAME_CUSTOM_AREA_TOP)) - { - m_cutTopId = i + 1; - m_cutTopValue = SANE_UNFIX(m_list_defaultOptions.at(i).second.toInt()); - if (opt->constraint_type == SANE_CONSTRAINT_RANGE) - m_cutHeight = SANE_UNFIX(opt->constraint.range->max); - } - else if (0 == strcmp(opt->name, SANE_STD_OPT_NAME_CUSTOM_AREA_RIGHT)) - { - m_cutRightId = i + 1; - m_cutRightValue = SANE_UNFIX(m_list_defaultOptions.at(i).second.toInt()); - } - else if (0 == strcmp(opt->name, SANE_STD_OPT_NAME_CUSTOM_AREA_BOTTOM)) - { - m_cutBottomId = i + 1; - m_cutBottomValue = SANE_UNFIX(m_list_defaultOptions.at(i).second.toInt()); - } - -// else if (Utf8ToStdString(opt->title) == "颜色模式") - if (strcmp(opt->name, SANE_STD_OPT_NAME_COLOR_MODE) == 0) - { - m_colorModeId = i + 1; - m_colorModeValue = m_list_defaultOptions.at(i).second.toString(); - } -// else if (Utf8ToStdString(opt->title) == "伽玛" || Utf8ToStdString(opt->title) == "伽玛值") - - } //for - - hlayoutOkAndCancel->insertWidget(0, buttonAbout, 0, Qt::AlignRight); - updateUIStatus(); - - scrollArea->setWidget(widget); - tabWidgetCreation->addTab(scrollArea, scrollArea->windowTitle()); -} - -void hg_settingdialog::refresh_control_value(int op_id) -{ - QVector ctrls = find_control(op_id); - - if(ctrls.empty()) - return; - - const SANE_Option_Descriptor* opt = (const SANE_Option_Descriptor*)m_list_defaultOptions.at(op_id - 1).first; - if(opt->type == SANE_TYPE_BOOL) - { - for(size_t i = 0; i < (size_t)ctrls.size(); ++i) - { - QCheckBox* cb = qobject_cast(ctrls[i]); - if(cb) - { - disconnect(cb, SIGNAL(stateChanged(int)), this, SLOT(slot_checkedClicked())); - cb->setChecked(m_list_defaultOptions.at(op_id - 1).second.toBool()); - connect(cb, SIGNAL(stateChanged(int)), this, SLOT(slot_checkedClicked())); - break; - } - } - } - else if(opt->type == SANE_TYPE_INT) - { - for(size_t i = 0; i < (size_t)ctrls.size(); ++i) - { - QComboBox* comb = qobject_cast(ctrls[i]); - if(comb) - { - char buf[40] = {0}; - sprintf(buf, "%d", m_list_defaultOptions.at(op_id - 1).second.toInt()); - comb->disconnect(comb, SIGNAL(currentTextChanged(const QString)), this, SLOT(slot_string_list_comboBoxClicked())); - - auto p_str = opt->constraint.word_list; - char buf2[20]; - comb->clear(); - for (SANE_Int i = 0; i < p_str[0]; ++i) - { - sprintf(buf2, "%d", p_str[i + 1]); - comb->addItem(QString::fromStdString(buf2)); - } - - comb->setCurrentText(QString::fromStdString(buf)); - connect(comb, SIGNAL(currentTextChanged(const QString)), this, SLOT(slot_string_list_comboBoxClicked())); - } - else - { - QSlider* slider = qobject_cast(ctrls[i]); - if(slider) - { - disconnect(slider, SIGNAL(valueChanged(int)), this, SLOT(slot_sliderClicked(int))); - slider->setValue(m_list_defaultOptions.at(op_id - 1).second.toInt()); - if (opt->constraint_type == SANE_CONSTRAINT_RANGE) - slider->setRange(opt->constraint.range->min, opt->constraint.range->max); - connect(slider, SIGNAL(valueChanged(int)), this, SLOT(slot_sliderClicked(int))); - } - else - { - QSpinBox* spin = qobject_cast(ctrls[i]); - if(spin) - { - disconnect(spin, SIGNAL(valueChanged(int)), this, SLOT(slot_spinBoxClicked(int))); - spin->setValue(m_list_defaultOptions.at(op_id - 1).second.toInt()); - if (opt->constraint_type == SANE_CONSTRAINT_RANGE) - spin->setRange(opt->constraint.range->min, opt->constraint.range->max); - connect(spin, SIGNAL(valueChanged(int)), this, SLOT(slot_spinBoxClicked(int))); - } - } - } - } - } - else if(opt->type == SANE_TYPE_FIXED) - { - double val = SANE_UNFIX(m_list_defaultOptions.at(op_id - 1).second.toInt()); - QSlider *slider = NULL; - QDoubleSpinBox* spin = NULL; - for(size_t i = 0; i < (size_t)ctrls.size(); ++i) - { - QComboBox* comb = qobject_cast(ctrls[i]); - if(comb) - { - char buf[40] = {0}; - sprintf(buf, "%f", val); - comb->disconnect(comb, SIGNAL(currentTextChanged(const QString)), this, SLOT(slot_string_list_comboBoxClicked())); - comb->setCurrentText(QString::fromStdString(buf)); - connect(comb, SIGNAL(currentTextChanged(const QString)), this, SLOT(slot_string_list_comboBoxClicked())); - } - else if(!slider) - { - slider = qobject_cast(ctrls[i]); - if(slider) - disconnect(slider, SIGNAL(valueChanged(int)), this, SLOT(slot_sliderClicked(int))); - } - else if(!spin) - { - spin = qobject_cast(ctrls[i]); - if(spin) - disconnect(spin, SIGNAL(valueChanged(double)), this, SLOT(slot_spinBoxClicked(double))); - } - } - if (slider) - { - if (opt->constraint_type == SANE_CONSTRAINT_RANGE) - slider->setRange(SANE_UNFIX(opt->constraint.range->min) * 100, SANE_UNFIX(opt->constraint.range->max) * 100); - slider->setValue(val * 100); - connect(slider, SIGNAL(valueChanged(int)), this, SLOT(slot_sliderClicked(int))); - } - - if (spin) - { - if (opt->constraint_type == SANE_CONSTRAINT_RANGE) - spin->setRange(SANE_UNFIX(opt->constraint.range->min), SANE_UNFIX(opt->constraint.range->max)); - spin->setValue(val); - connect(spin, SIGNAL(valueChanged(double)), this, SLOT(slot_spinBoxClicked(double))); - } - } - else if(opt->type == SANE_TYPE_STRING) - { - for(size_t i = 0; i < (size_t)ctrls.size(); ++i) - { - QComboBox* comb = qobject_cast(ctrls[i]); - if(comb) - { - disconnect(comb, SIGNAL(currentTextChanged(const QString)), this, SLOT(slot_string_list_comboBoxClicked())); - - comb->clear(); - auto p_str = opt->constraint.string_list; - QStringList stringList; - while (*p_str) - { - stringList.append(*p_str); - p_str++; - } - - if (stringList.isEmpty() != true) - { - for (int i = 0; i < (stringList.size()); i++) - comb->addItem(stringList.at(i)); - } - - comb->setCurrentText(m_list_defaultOptions.at(op_id - 1).second.toString()); - // comb->setProperty(hg_settingdialog::property_combox_data_type_.c_str(), COMBO_VAL_STRING); - connect(comb, SIGNAL(currentTextChanged(const QString)), this, SLOT(slot_string_list_comboBoxClicked())); - } - else - { - QLineEdit* edit = qobject_cast(ctrls[i]); - if(edit) - { - disconnect(edit, SIGNAL(textChanged(const QString&)), this, SLOT(slot_lineEditInput())); - edit->setText(m_list_defaultOptions.at(op_id - 1).second.toString()); - connect(edit, SIGNAL(textChanged(const QString&)), this, SLOT(slot_lineEditInput())); - } - } - } - } -} -QVector hg_settingdialog::find_control(int opt_num) -{ - QVector list_w; - for(int i = 0; i< m_list_widgets.size(); i++) - { - if (m_list_widgets.at(i) == nullptr) continue; - QWidget* w = m_list_widgets.at(i); - int id = w->property("controls_id").toInt(); - if(opt_num == id) - list_w.append(w); - } - return list_w; -} - -void hg_settingdialog::updateUIStatus() -{ - updateOpt(); - - SANE_Int dev_options = 0; - m_saneAPI.sane_control_option_api(m_devHandle, 0, SANE_ACTION_GET_VALUE, &dev_options, nullptr); - for(int id = 1, optons = dev_options; id < optons; id++) - { - QVector list_widgets = find_control(id); - if (list_widgets.empty()) continue; - QWidget* widget = list_widgets.first(); - if (widget == nullptr) continue; - QWidget* parentWidget = widget->parentWidget(); - - while (parentWidget->layout() && - typeid(*(parentWidget->layout())) != typeid(QFormLayout)) - { - widget = parentWidget; - parentWidget = widget->parentWidget(); - } - - QFormLayout* layout = reinterpret_cast(parentWidget->layout()); - const SANE_Option_Descriptor* opt = reinterpret_cast(m_list_defaultOptions.at(id - 1).first); - bool hide = (opt->cap & SANE_CAP_INACTIVE) == SANE_CAP_INACTIVE; - QWidget* w_label = layout ? layout->labelForField(widget) : nullptr; - - if( strcmp(opt->name, SANE_STD_OPT_NAME_CUSTOM_AREA_LEFT) == 0 || - strcmp(opt->name, SANE_STD_OPT_NAME_CUSTOM_AREA_TOP) == 0 || - strcmp(opt->name, SANE_STD_OPT_NAME_CUSTOM_AREA_RIGHT) == 0 || - strcmp(opt->name, SANE_STD_OPT_NAME_CUSTOM_AREA_BOTTOM) == 0 ) - hide = true; - - refresh_control_value(id); - if(w_label) - hide ? w_label->hide() : w_label->show(); - widget->setVisible(!hide); - if(strcmp(opt->name, SANE_STD_OPT_NAME_CUSTOM_AREA) == 0) - { - if(hide) - { - custom_area_lable_->hide(); - btn_cut_area_->hide(); - } - else - { - custom_area_lable_->show(); - btn_cut_area_->show(); - } - } - } -} - -void hg_settingdialog::slot_checkedClicked() -{ - QCheckBox *checkBox = qobject_cast(sender()); - SANE_Int id = checkBox->property("controls_id").toInt(); - SANE_Bool checkBoxcurrentState = checkBox->isChecked(); - -// m_list_IdValueTitle.append(QPair, QString>(QPair(id, checkBoxcurrentState), md5(opt->title))); - - SANE_Int method = 0; - SANE_Status ret = m_saneAPI.sane_control_option_api(m_devHandle, id, SANE_ACTION_SET_VALUE, &checkBoxcurrentState, &method); - if (ret == SANE_STATUS_UNSUPPORTED) - { - SANE_Bool value = false; - ret = m_saneAPI.sane_control_option_api(m_devHandle, id, SANE_ACTION_GET_VALUE, &value, &method); - disconnect(checkBox, SIGNAL(stateChanged(int)), this, SLOT(slot_checkedClicked())); - checkBox->setCheckState(value ? Qt::Checked : Qt::Unchecked); - connect(checkBox, SIGNAL(stateChanged(int)), this, SLOT(slot_checkedClicked())); - QMessageBox::information(this, tr("Prompt"), tr("The funtion is unsupported")); - return; - } - if((method & SANE_INFO_RELOAD_OPTIONS) == SANE_INFO_RELOAD_OPTIONS) - updateUIStatus(); - else if(method & SANE_INFO_INEXACT) - checkBox->setCheckState(checkBoxcurrentState ? Qt::CheckState::Checked : Qt::CheckState::Unchecked); - - const SANE_Option_Descriptor* opt = nullptr; - for (int i = 0; i < m_list_getOpt.size(); i++) - { - if (m_list_getOpt.at(i).first == id) - { - opt = reinterpret_cast(m_list_getOpt.at(i).second); - break; - } - } - - if(strcmp(opt->name, SANE_STD_OPT_NAME_CUSTOM_AREA) == 0) - btn_cut_area_->setEnabled(checkBoxcurrentState); - else if (strcmp(opt->name, SANE_STD_OPT_NAME_IS_CUSTOM_GAMMA) == 0) - btn_gamma_->setEnabled(checkBoxcurrentState); - cur_scheme_->config_changed(id, (char*)&checkBoxcurrentState, sizeof(checkBoxcurrentState)); - - if (0 == strcmp(opt->name, SANE_STD_OPT_NAME_RID_MORR) || 0 == strcmp(opt->name, SANE_STD_OPT_NAME_RID_GRID)) - { - QMessageBox::information(this, tr("Prompt"), tr("This function may cause a decrease in the speed of drawing.")); - } -} - -void hg_settingdialog::slot_string_list_comboBoxClicked() -{ - QComboBox *comboBox = qobject_cast(sender()); - SANE_Int id = comboBox->property("controls_id").toInt(); - std::string comboBoxcurrentItem(comboBox->currentText().toUtf8()); - int type = comboBox->property(hg_settingdialog::property_combox_data_type_.c_str()).toInt(); - - if (id == m_dpiId) - { - m_dpiValue = atoi(comboBoxcurrentItem.c_str()); - qDebug("dpi=%d", m_dpiValue); - } - else if (id == m_paperSizeId) - { - m_paperSizeValue = comboBoxcurrentItem.c_str(); - qDebug("paperSize=%s", comboBoxcurrentItem.c_str()); - } - else if (id == m_colorModeId) - { - m_colorModeValue = comboBoxcurrentItem.c_str(); - qDebug("colorMode=%s", comboBoxcurrentItem.c_str()); - } - - const SANE_Option_Descriptor* opt = nullptr; - for (int i = 0; i < m_list_getOpt.size(); i++) - { - if (m_list_getOpt.at(i).first == id) - { - opt = reinterpret_cast(m_list_getOpt.at(i).second); - break; - } - } - -// m_list_IdValueTitle.append(QPair, QString>(QPair(id, &comboBoxcurrentItem.at(0)), md5(opt->title))); - - SANE_Int method = 0; - SANE_String buf = (SANE_String)malloc(opt->size * 2 + 4); - if(type == COMBO_VAL_INT) - *((SANE_Int*)buf) = atoi(comboBoxcurrentItem.c_str()); - else if(type == COMBO_VAL_FLOAT) - *((SANE_Fixed*)buf) = SANE_FIX(atof(comboBoxcurrentItem.c_str())); - else - strcpy(buf, comboBoxcurrentItem.c_str()); - SANE_Status ret = m_saneAPI.sane_control_option_api(m_devHandle, id, SANE_ACTION_SET_VALUE, buf, &method); - if (ret == SANE_STATUS_UNSUPPORTED) - { - char* value = (char*)malloc(opt->size * 2 + 4); - ret = m_saneAPI.sane_control_option_api(m_devHandle, id, SANE_ACTION_GET_VALUE, value, &method); - if (value != nullptr) - comboBox->setCurrentText(QString::fromStdString(value)); - free(value); - QMessageBox::information(this, tr("Prompt"), tr("The funtion is unsupported")); - return; - } - if((method & SANE_INFO_RELOAD_OPTIONS) == SANE_INFO_RELOAD_OPTIONS) - updateUIStatus(); - else if(method & SANE_INFO_INEXACT) - comboBox->setCurrentText(QString::fromStdString(buf)); - - if(type == COMBO_VAL_INT) - cur_scheme_->config_changed(id, buf, sizeof(SANE_Int)); - else if(type == COMBO_VAL_FLOAT) - cur_scheme_->config_changed(id, buf, sizeof(SANE_Fixed)); - else - { - std::string langCN(to_default_language(buf, nullptr)); - cur_scheme_->config_changed(id, &langCN[0], langCN.length()); - } - free(buf); - - if (0 == strcmp(opt->name, SANE_STD_OPT_NAME_TEXT_DIRECTION)) - { - if (comboBox->currentText() == OPTION_VALUE_WGFX_ZDWBFXSB) - QMessageBox::information(this, tr("Prompt"), tr("This function may cause a decrease in the speed of drawing.")); - } -} - -void hg_settingdialog::slot_pushButtonClicked() -{ - QPushButton *pushButton = qobject_cast(sender()); - SANE_Int id = pushButton->property("controls_id").toInt(), - after = 0; - - // restore to default setting ? - SANE_Status ret = m_saneAPI.sane_control_option_api(m_devHandle, id, SANE_ACTION_SET_VALUE, NULL, &after); - if (ret == SANE_STATUS_UNSUPPORTED) - { - QMessageBox::information(this, tr("Prompt"), tr("The funtion is unsupported")); - return; - } - if((after & SANE_INFO_RELOAD_OPTIONS) == SANE_INFO_RELOAD_OPTIONS) - updateUIStatus(); - - const SANE_Option_Descriptor* opt = m_saneAPI.sane_get_option_descriptor_api(m_devHandle, id); - if(opt && strcmp(opt->name, SANE_STD_OPT_NAME_RESTORE) == 0) - { - restore_2_default_settings(); - } -} - -void hg_settingdialog::slot_cutButtonClicked() -{ - //int width = 0.03937 * m_cutWidth * m_dpiValue; - //int height = 0.03937 * m_cutHeight * m_dpiValue; - int left = 0.03937 * m_cutLeftValue * m_dpiValue; - int top = 0.03937 * m_cutTopValue * m_dpiValue; - int right = 0.03937 * m_cutRightValue * m_dpiValue; - int bottom = 0.03937 * m_cutBottomValue * m_dpiValue; - - CutPaperTool dlg(this); - dlg.setPaperType(m_dpiValue, m_paperSizeValue, 300); - QRectF rc(left, top, right - left, bottom - top); - dlg.setCutRect(rc); - if (dlg.exec()) - { - QRectF rcRet = dlg.getCutRectPixel(); - - m_cutLeftValue = rcRet.left() / (0.03937 * m_dpiValue); - m_cutTopValue = rcRet.top() / (0.03937 * m_dpiValue); - m_cutRightValue = rcRet.right() / (0.03937 * m_dpiValue); - m_cutBottomValue = rcRet.bottom() / (0.03937 * m_dpiValue); - - SANE_Int info; - SANE_Word value = SANE_FIX(m_cutLeftValue); - m_saneAPI.sane_control_option_api(m_devHandle, m_cutLeftId, SANE_ACTION_SET_VALUE, &value, &info); - cur_scheme_->config_changed(m_cutLeftId, (char*)&value, sizeof(value)); - value = SANE_FIX(m_cutTopValue); - m_saneAPI.sane_control_option_api(m_devHandle, m_cutTopId, SANE_ACTION_SET_VALUE, &value, &info); - cur_scheme_->config_changed(m_cutTopId, (char*)&value, sizeof(value)); - value = SANE_FIX(m_cutRightValue); - m_saneAPI.sane_control_option_api(m_devHandle, m_cutRightId, SANE_ACTION_SET_VALUE, &value, &info); - cur_scheme_->config_changed(m_cutRightId, (char*)&value, sizeof(value)); - value = SANE_FIX(m_cutBottomValue); - m_saneAPI.sane_control_option_api(m_devHandle, m_cutBottomId, SANE_ACTION_SET_VALUE, &value, &info); - cur_scheme_->config_changed(m_cutBottomId, (char*)&value, sizeof(value)); - } -} - -void hg_settingdialog::slot_gammaButtonClicked() -{ - setPicClrTool dlg(this); - - int colorMode; // 0-彩色, 1-灰度 - if (m_colorModeValue.toStdString() == OPTION_VALUE_YSMS_256JHD - || m_colorModeValue.toStdString() == OPTION_VALUE_YSMS_HB) - { - colorMode = 1; - - QList keyTable; - for (int i = 0; i < m_gammaData.pt_count; ++i) - { - QPoint pt(m_gammaData.keypoint[i].x, m_gammaData.keypoint[i].y); - keyTable.append(pt); - } - - if (!keyTable.empty()) - { - dlg.setGrayKeyTable(keyTable); - } - } - else - { - colorMode = 0; - - QList keyTable; - for (int i = 0; i < m_gammaData.pt_count; ++i) - { - QPoint pt(m_gammaData.keypoint[i].x, m_gammaData.keypoint[i].y); - keyTable.append(pt); - } - - QList rKeyTable; - for (int i = 0; i < m_gammaData.pt_count_r; ++i) - { - QPoint pt(m_gammaData.keypoint_r[i].x, m_gammaData.keypoint_r[i].y); - rKeyTable.append(pt); - } - - QList gKeyTable; - for (int i = 0; i < m_gammaData.pt_count_g; ++i) - { - QPoint pt(m_gammaData.keypoint_g[i].x, m_gammaData.keypoint_g[i].y); - gKeyTable.append(pt); - } - - QList bKeyTable; - for (int i = 0; i < m_gammaData.pt_count_b; ++i) - { - QPoint pt(m_gammaData.keypoint_b[i].x, m_gammaData.keypoint_b[i].y); - bKeyTable.append(pt); - } - - QVector> keyTableList; - if (!keyTable.empty() && !rKeyTable.empty() && !gKeyTable.empty() && !bKeyTable.empty()) - { - keyTableList.append(keyTable); - keyTableList.append(rKeyTable); - keyTableList.append(gKeyTable); - keyTableList.append(bKeyTable); - dlg.setRGBKeyTable(keyTableList); - } - } - - dlg.setColorMode(colorMode); - if (dlg.exec()) - { - memset(&m_gammaData, 0, sizeof(m_gammaData)); - clicked_gamma_ = true; - - if (1 == colorMode) - { - QList keyTable = dlg.getGrayKeyTable(); - - m_gammaData.pt_count = HGMIN(4, keyTable.size()); - int i = 0; - for (QPoint pt : keyTable) - { - if (i >= 4) - break; - - m_gammaData.keypoint[i].x = pt.x(); - m_gammaData.keypoint[i].y = pt.y(); - ++i; - } - - uchar data[256]; - dlg.getGrayTable(data, 256); - for (int i = 0; i < 256; ++i) - { - m_gammaData.table[i] = data[i]; - } - } - else - { - QVector> keyTableList = dlg.getRGBKeyTable(); - - m_gammaData.pt_count = HGMIN(4, keyTableList[0].size()); - int i = 0; - for (QPoint pt : keyTableList[0]) - { - if (i >= 4) - break; - - m_gammaData.keypoint[i].x = pt.x(); - m_gammaData.keypoint[i].y = pt.y(); - ++i; - } - - m_gammaData.pt_count_r = HGMIN(4, keyTableList[1].size()); - i = 0; - for (QPoint pt : keyTableList[1]) - { - if (i >= 4) - break; - - m_gammaData.keypoint_r[i].x = pt.x(); - m_gammaData.keypoint_r[i].y = pt.y(); - ++i; - } - - m_gammaData.pt_count_g = HGMIN(4, keyTableList[2].size()); - i = 0; - for (QPoint pt : keyTableList[2]) - { - if (i >= 4) - break; - - m_gammaData.keypoint_g[i].x = pt.x(); - m_gammaData.keypoint_g[i].y = pt.y(); - ++i; - } - - m_gammaData.pt_count_b = HGMIN(4, keyTableList[3].size()); - i = 0; - for (QPoint pt : keyTableList[3]) - { - if (i >= 4) - break; - - m_gammaData.keypoint_b[i].x = pt.x(); - m_gammaData.keypoint_b[i].y = pt.y(); - ++i; - } - - uchar data[256 * 3]; - dlg.getRGBTable(data, 256 * 3); - for (int i = 0; i < 256; ++i) - { - m_gammaData.table[i] = data[i * 3 + 2]; - m_gammaData.table[i + 256] = data[i * 3 + 1]; - m_gammaData.table[i + 512] = data[i * 3 + 0]; - } - } - - dev_que::set_custom_gamma(&m_saneAPI, m_devHandle, &m_gammaData); - cur_scheme_->config_changed(SANE_STD_OPT_NAME_IS_CUSTOM_GAMMA, (char*)&m_gammaData, sizeof(m_gammaData), true); - } -} - -void hg_settingdialog::slot_word_list_comboBoxClicked(int value) -{ - QComboBox *comboBox = qobject_cast(sender()); - SANE_Int id = comboBox->property("controls_id").toInt(); - SANE_Int temp = value; - - const SANE_Option_Descriptor* opt = nullptr; - for (int i = 0; i < m_list_getOpt.size(); i++) - { - if (m_list_getOpt.at(i).first == id) - { - opt = reinterpret_cast(m_list_getOpt.at(i).second); - break; - } - } -// m_list_IdValueTitle.append(QPair, QString>(QPair(id, temp), md5(opt->title))); - - - SANE_Int method = 0; - SANE_Status ret = m_saneAPI.sane_control_option_api(m_devHandle, id, SANE_ACTION_SET_VALUE, &temp, &method); - if (ret == SANE_STATUS_UNSUPPORTED) - { - char* value = (char*)malloc(opt->size * 2 + 4); - ret = m_saneAPI.sane_control_option_api(m_devHandle, id, SANE_ACTION_GET_VALUE, value, &method); - if (value != nullptr) - comboBox->setCurrentText(QString::fromStdString(value)); - free(value); - QMessageBox::information(this, tr("Prompt"), tr("The funtion is unsupported")); - return; - } - if((method & SANE_INFO_RELOAD_OPTIONS) == SANE_INFO_RELOAD_OPTIONS) - updateUIStatus(); - else if(method & SANE_INFO_INEXACT) - { - char buf[20]; - sprintf(buf, "%d", temp); - comboBox->setCurrentText(QString::fromStdString(buf)); - } - cur_scheme_->config_changed(id, (char*)&temp, sizeof(temp)); -} - -void hg_settingdialog::slot_sliderClicked(int value) -{ - QSlider *slider = qobject_cast(sender()); - SANE_Int id = slider->property("controls_id").toInt(); - - QAbstractSpinBox* spin = nullptr; - for(int i = 0; i < m_list_sliderSpinbox.size(); i++) - { - if (m_list_sliderSpinbox.at(i).first == slider) - { - spin = reinterpret_cast(m_list_sliderSpinbox.at(i).second); - break; - } - } - - if (spin != nullptr) - { - SANE_Int val = value, method = 0; - bool db_val = false; - if (typeid(*spin) == typeid(QSpinBox)) - { - QSpinBox* spin_ = reinterpret_cast(spin); - spin_->setValue(value); - -// m_list_IdValueTitle.append(QPair, QString>(QPair(id, val), md5(opt->title))); - } - else - { - QDoubleSpinBox* spin_ = reinterpret_cast(spin); - double temp = value * spin_->singleStep(); - if(temp != spin_->value()) - spin_->setValue(temp); - - val = SANE_FIX(temp); - db_val = true; - -// m_list_IdValueTitle.append(QPair, QString>(QPair(id, temp), md5(opt->title))); - } - SANE_Status ret = m_saneAPI.sane_control_option_api(m_devHandle, id, SANE_ACTION_SET_VALUE, &val, &method); - if (ret == SANE_STATUS_UNSUPPORTED) - { - if (!db_val) - { - SANE_Int value = 0; - ret = m_saneAPI.sane_control_option_api(m_devHandle, id, SANE_ACTION_GET_VALUE, &value, &method); - slider->setValue(value); - } - else - { - SANE_Fixed value = 0; - ret = m_saneAPI.sane_control_option_api(m_devHandle, id, SANE_ACTION_GET_VALUE, &value, &method); - slider->setValue(value); - } - QMessageBox::information(this, tr("Prompt"), tr("The funtion is unsupported")); - return; - } - if((method & SANE_INFO_RELOAD_OPTIONS) == SANE_INFO_RELOAD_OPTIONS) - updateUIStatus(); - else if(method & SANE_INFO_INEXACT) - { - if(db_val) - { - QDoubleSpinBox* spin_ = reinterpret_cast(spin); - double v = SANE_UNFIX(val); - spin_->setValue(v); - slider->setValue(spin_->value() / spin_->singleStep()); - } - else { - QSpinBox* spin_ = reinterpret_cast(spin); - spin_->setValue(val); - slider->setValue(spin_->value() / spin_->singleStep()); - } - } - cur_scheme_->config_changed(id, (char*)&val, sizeof(val)); - } -} - -void hg_settingdialog::slot_doubleSpinboxClicked(double value) -{ - QDoubleSpinBox* spinBox = qobject_cast(sender()); - QAbstractSlider* slider = nullptr; - int id = spinBox->property("controls_id").toInt(); - for (int i = 0; i < m_list_sliderSpinbox.size(); i++) - if (m_list_sliderSpinbox.at(i).second == spinBox) - { - slider = reinterpret_cast(m_list_sliderSpinbox.at(i).first); - break; - } - if(slider != nullptr) - { - int temp = static_cast(value / spinBox->singleStep() + 0.5); - QSlider* slider_ = reinterpret_cast(slider); - if (slider_->value() != temp) - slider_->setValue(temp); - } -} - -void hg_settingdialog::slot_spinBoxClicked(int value) -{ - QSpinBox* spinBox = qobject_cast(sender()); - int id = spinBox->property("controls_id").toInt(); - - QAbstractSlider* slider = nullptr; - for (int i = 0; i < m_list_sliderSpinbox.size(); i++) - if (m_list_sliderSpinbox.at(i).second == spinBox) - { - slider = reinterpret_cast(m_list_sliderSpinbox.at(i).first); - break; - } - if(slider == nullptr) - { - SANE_Int temp = value; - -// m_list_IdValueTitle.append(QPair, QString>(QPair(id, temp), md5(opt->title))); - - SANE_Int method = 0; - SANE_Status ret = m_saneAPI.sane_control_option_api(m_devHandle, id, SANE_ACTION_SET_VALUE, &temp, &method); - if (ret == SANE_STATUS_UNSUPPORTED) - { - QMessageBox::information(this, tr("Prompt"), tr("The funtion is unsupported")); - return; - } - if((method & SANE_INFO_RELOAD_OPTIONS) == SANE_INFO_RELOAD_OPTIONS) - updateUIStatus(); - else if(value != temp) - { - disconnect(spinBox, SIGNAL(valueChanged(int)), this, SLOT(slot_spinBoxClicked(int))); - spinBox->setValue(temp); - connect(spinBox, SIGNAL(valueChanged(int)), this, SLOT(slot_spinBoxClicked(int))); - } - cur_scheme_->config_changed(id, (char*)&temp, sizeof(temp)); - }else - { - QSlider* slider_ = reinterpret_cast(slider); - slider_->setValue(spinBox->value()); - } -} - -void hg_settingdialog::slot_lineEditInput() -{ - QLineEdit* lineEdit = qobject_cast(sender()); - int id = lineEdit->property("controls_id").toInt(); - std::string lineEditCurrentText(lineEdit->text().toUtf8()); - - const SANE_Option_Descriptor* opt = nullptr; - for (int i = 0; i < m_list_getOpt.size(); i++) - { - if (m_list_getOpt.at(i).first == id) - { - opt = reinterpret_cast(m_list_getOpt.at(i).second); - break; - } - } - -// m_list_IdValueTitle.append(QPair, QString>(QPair(id, &lineEditCurrentText.at(0)), md5(opt->title))); - - SANE_Int method = 0; - void *buf = NULL; - SANE_Int nv = 0; - if(opt->type == SANE_TYPE_INT) - { - nv = atoi(lineEditCurrentText.c_str()); - buf = &nv; - } - else if(opt->type == SANE_TYPE_FIXED) - { - nv = SANE_FIX(atof(lineEditCurrentText.c_str())); - buf = &nv; - } - else - { - buf = malloc(opt->size * 2 + 4); - strcpy((char*)buf, lineEditCurrentText.c_str()); - } - SANE_Status ret = m_saneAPI.sane_control_option_api(m_devHandle, id, SANE_ACTION_SET_VALUE, buf, &method); - if (ret == SANE_STATUS_UNSUPPORTED) - { - QMessageBox::information(this, tr("Prompt"), tr("The funtion is unsupported")); - return; - } - if((method & SANE_INFO_RELOAD_OPTIONS) == SANE_INFO_RELOAD_OPTIONS) - updateUIStatus(); - else if(method & SANE_INFO_INEXACT) - { - char mem[20], *v = mem; - if(opt->type == SANE_TYPE_INT) - sprintf(mem, "%d", nv); - else if(opt->type == SANE_TYPE_FIXED) - sprintf(mem, "%f", SANE_UNFIX(nv)); - else - v = (char*)buf; - lineEdit->setText(QString::fromStdString(v)); - } - - const SANE_Option_Descriptor* opt2 = nullptr; - for (int i = 0; i < m_list_getOpt.size(); i++) - { - if (m_list_getOpt.at(i).first == id) - { - opt2 = reinterpret_cast(m_list_getOpt.at(i).second); - break; - } - } - - if(opt2->type == SANE_TYPE_INT || opt2->type == SANE_TYPE_FIXED) - { - cur_scheme_->config_changed(id, (char*)buf, sizeof(SANE_Int)); - } - else - { - std::string langCN(to_default_language((char*)buf, nullptr)); - cur_scheme_->config_changed(id, &langCN[0], langCN.length()); - free(buf); - } -} -void hg_settingdialog::slot_buttonAboutClicked() -{ - char info[256] = { 0 }; - SANE_Int data = 0; - SANE_Status ret = SANE_STATUS_GOOD; - QString content; - QString title = tr("about ") + QString::fromStdString(m_devName); - - ret = m_saneAPI.sane_control_option_api(m_devHandle, (SANE_Int)0x8855, SANE_ACTION_GET_VALUE, info, NULL); - if (ret != SANE_STATUS_GOOD) - { - QString str = tr("Not supported"); - strcpy(info, str.toStdString().c_str()); - } - content += tr("

Device model: %1

").arg(QString(info)); - info[0] = 0; - ret = m_saneAPI.sane_control_option_api(m_devHandle, (SANE_Int)0x884A, SANE_ACTION_GET_VALUE, info, NULL); - if (ret != SANE_STATUS_GOOD) - { - QString str = tr("Not supported"); - strcpy(info, str.toStdString().c_str()); - } - content += tr("

Driver version: %1

").arg(QString(info)); - info[0] = 0; - ret = m_saneAPI.sane_control_option_api(m_devHandle, (SANE_Int)0x8857, SANE_ACTION_GET_VALUE, info, NULL); - if (ret != SANE_STATUS_GOOD) - { - QString str = tr("Not supported"); - strcpy(info, str.toStdString().c_str()); - } - content += tr("

Firmware number: %1

").arg(QString(info)); - info[0] = 0; - ret = m_saneAPI.sane_control_option_api(m_devHandle, (SANE_Int)0x8856, SANE_ACTION_GET_VALUE, info, NULL); - if (ret != SANE_STATUS_GOOD) - { - QString str = tr("Not supported"); - strcpy(info, str.toStdString().c_str()); - } - content += tr("

Serial number: %1

").arg(QString(info)); - info[0] = 0; - - ret = m_saneAPI.sane_control_option_api(m_devHandle, (SANE_Int)0x9902, SANE_ACTION_GET_VALUE, &data, NULL); - if (ret != SANE_STATUS_GOOD) - { - QString str = tr("Not supported"); - strcpy(info, str.toStdString().c_str()); - content += tr("

Roller count: %1

").arg(QString(info)); - info[0] = 0; - } - else - { - content += tr("

Roller count: %1

").arg(QString::number(data)); - } - - ret = m_saneAPI.sane_control_option_api(m_devHandle, (SANE_Int)0x8849, SANE_ACTION_GET_VALUE, &data, NULL); - if (ret != SANE_STATUS_GOOD) - { - QString str = tr("Not supported"); - strcpy(info, str.toStdString().c_str()); - content += tr("

Roller count: %1

").arg(QString(info)); - info[0] = 0; - } - else - { - content += tr("

History count: %1

").arg(QString::number(data)); - } - - QMessageBox msg(QMessageBox::NoIcon, title, - content, QMessageBox::Ok, this); - msg.setStyleSheet("QLabel{""min-width: 250px;""}"); - msg.exec(); -} - -void hg_settingdialog::slot_buttonOkClicked() -{ - save_ = true; - close(); -} - -void hg_settingdialog::slot_buttonCancelClicked() -{ - close(); -} - -void hg_settingdialog::keyPressEvent(QKeyEvent *e) -{ - if (e->key() == Qt::Key_Escape) { - e->ignore(); - } - else { - QDialog::keyPressEvent(e); - } -} - -int hg_settingdialog::get_changed_items(void) -{ - return changed_count_; -} - -gb::sane_config_schm *hg_settingdialog::getCurScheme() -{ - return cur_scheme_; -} - -//生成UTF-8编码的MD5值 -QString hg_settingdialog::md5(QString key) -{ - QCryptographicHash md5(QCryptographicHash::Md5); - md5.addData(key.toUtf8()); - return QString(md5.result().toHex()); -} - -const void* hg_settingdialog::find_option_description(int id) -{ - for(int i = 0; i < m_list_getOpt.size(); i++) - { - if (m_list_getOpt.at(i).first == id) - return reinterpret_cast(m_list_getOpt.at(i).second); - } - - return nullptr; -} - -const void* hg_settingdialog::find_option_description(const std::string& title, int* id) -{ - for(int i = 0; i < m_list_getOpt.size(); i++) - { - std::string t((reinterpret_cast(m_list_getOpt.at(i).second))->name); - - if (title == t) - { - if(id) - *id = m_list_getOpt.at(i).first; - return reinterpret_cast(m_list_getOpt.at(i).second); - } - } - - return nullptr; -} - -void hg_settingdialog::closeEvent(QCloseEvent* e) -{ - if(e->type() == QEvent::Close) // consider as cancel ... - { - if(save_) - save_scheme(); - else - cancel_setting(); - } - - e->accept(); -} - -bool hg_settingdialog::eventFilter(QObject *target, QEvent *event) -{ - if (target->findChild("QSlider")); - { - if (event->type() == QEvent::Wheel) - { - return true; - } - } - - return QDialog::eventFilter(target, event); -} - -bool hg_settingdialog::createMsgBoxUi(bool add, std::string &name) -{ - QString text(tr("Please select to overwrite the original configuration:")); - text += QString::fromStdString(name); - text += tr(",or add a new configuration"); - - QDialog *dlg = new QDialog(this); - dlg->setWindowTitle(tr("save the configuration")); - QLabel *label_question = new QLabel; - label_question->setText(text); - - QRadioButton *radioButtonCover = new QRadioButton; - radioButtonCover->setText(tr("cover original configuration:") + QString::fromStdString(name)); - radioButtonCover->setChecked(true); - add = false; - QRadioButton *radioButtonNew = new QRadioButton; - radioButtonNew->setText(tr("add new configuration")); - - QHBoxLayout *hLayoutName = new QHBoxLayout; - QLabel *label_name = new QLabel; - label_name->setText(tr("rename:")); - m_lineEdit_name = new QLineEdit; - std::string name2; - m_lineEdit_name->setText(QString::fromStdString(getCurUiShemeName(name2))); - - QSpacerItem *spacer1 = new QSpacerItem(20, 20, QSizePolicy::Expanding); - hLayoutName->addWidget(label_name); - hLayoutName->addWidget(m_lineEdit_name); - hLayoutName->addSpacerItem(spacer1); - label_name->setVisible(false); - m_lineEdit_name->setVisible(false); - - bool cover = true; - connect(radioButtonCover, &QRadioButton::clicked, this, [=, &add, &cover](){ - cover = true; - add = false; - label_name->setVisible(false); - m_lineEdit_name->setVisible(false); - }); - connect(radioButtonNew, &QRadioButton::clicked, this, [=, &add, &cover](){ - cover = false; - add = true; - label_name->setVisible(true); - m_lineEdit_name->setVisible(true); - - m_lineEdit_name->setFocus(); - QTimer::singleShot(0, m_lineEdit_name, &QLineEdit::selectAll); - }); - - QSpacerItem *spacer2 = new QSpacerItem(20, 20, QSizePolicy::Expanding); - QPushButton *pbtnOk = new QPushButton; - pbtnOk->setText(tr("ok")); - connect(pbtnOk, &QPushButton::clicked, this, [=, &name, &cover](){ - - QString text = m_lineEdit_name->text(); - static QRegularExpression re("\\s"); - text.remove(re);//Remove space - - name = text.toStdString(); - - if(name.empty()) - { - QMessageBox::information(this, tr("tips"), tr("scheme name cannot be empty")); - m_lineEdit_name->setText(QString::fromStdString(getCurUiShemeName(name))); - return; - } - - if (!cover) - { - std::vector now; - cur_cfg_->get_all_schemes(now); - for(auto& v: now) - { - if(v == name) - { - - QMessageBox::information(this, tr("tips"), tr("scheme name: ") + QString::fromStdString(name) + tr(" already exists")); - m_lineEdit_name->setText(QString::fromStdString(getCurUiShemeName(name))); - return; - } - } - } - - dlg->close(); - }); - - QHBoxLayout *hLayout_pbtnOk = new QHBoxLayout; - hLayout_pbtnOk->addSpacerItem(spacer2); - hLayout_pbtnOk->addWidget(pbtnOk); - - QVBoxLayout *vLayout = new QVBoxLayout; - vLayout->addWidget(label_question); - vLayout->addWidget(radioButtonCover); - vLayout->addWidget(radioButtonNew); - vLayout->addLayout(hLayoutName); - vLayout->addLayout(hLayout_pbtnOk); - dlg->setLayout(vLayout); - - dlg->exec(); - - return add; -} - -std::string hg_settingdialog::getCurUiShemeName(std::string name) -{ - std::string k(""), val(""); - int id = 0; - const SANE_Option_Descriptor* opt = nullptr; - - if (cur_scheme_->first_config(k, val)) - { - int count = 0; - do - { - id = cur_scheme_->id_from_name(k.c_str()); - opt = id == -1 ? nullptr : m_saneAPI.sane_get_option_descriptor_api(m_devHandle, id); - if (opt) - { - if (count++) - name += " + "; - - if (opt->type == SANE_TYPE_STRING) - name += from_default_language(val.c_str(), nullptr); - else - { - name += opt->title; - if (opt->type == SANE_TYPE_BOOL) - { - name += std::string("("); - if (*(SANE_Bool*)&val[0] == SANE_TRUE) - name += "true)"; - else - name += "false)"; - } - else if (opt->type == SANE_TYPE_INT) - { - char buf[128] = { 0 }; - sprintf(buf, "(%d)", *(int*)&val[0]); - name += buf; - } - else if (opt->type == SANE_TYPE_FIXED) - { - char buf[128] = { 0 }; - sprintf(buf, "(%.4f)", SANE_UNFIX(*(SANE_Fixed*)&val[0])); - name += buf; - } - } - } - } while (count < 3 && cur_scheme_->next_config(k, val)); - } - return name; -} - -void hg_settingdialog::save_scheme(void) -{ - std::string name(cur_scheme_->get_scheme_name()); - - bool add = name.empty(); - - if(add) - { - name = getCurUiShemeName(name); - } - else - { - int items = 0; - add = cur_scheme_->has_changed(&items); - if(add) - { - if(items == 0) // while shemes is default - { - cur_cfg_->select_scheme(nullptr); - cur_cfg_->remove_scheme(cur_scheme_->get_scheme_name().c_str()); - cur_cfg_->save(); - return; - } - else - add = false; - } - } - if(add) - { - if(name.empty() && cur_scheme_->get_scheme_name().empty()) - { - cur_scheme_->end_setting(true); - return; - } - - gb::sane_config_schm* cp = cur_scheme_->copy(); - - cur_scheme_->end_setting(true); - cur_scheme_->release(); - cur_scheme_ = cp; - - size_t pos = name.rfind('-'); - int ind = 0; - char append[20] = {0}; - - if(pos != std::string::npos) - { - ind = atoi(name.c_str() + pos + 1); - if(ind > 0) - { - name.erase(pos); - sprintf(append, "-%d", ++ind); - } - } - while (!cur_cfg_->add_scheme(cur_scheme_, (name + append).c_str())) - { - sprintf(append, "-%d", ++ind); - } - } - else - { - cur_scheme_->end_setting(false); - } - - cur_cfg_->select_scheme(cur_scheme_->get_scheme_name().c_str()); - - cur_cfg_->save(); -} -void hg_settingdialog::cancel_setting(void) -{ - // restore changed value ... - cur_scheme_->end_setting(true); - dev_que::apply_scheme(&m_saneAPI, m_devHandle, cur_scheme_); -} - -std::string hg_settingdialog::getAppVersion() -{ - char v[256] = {0}; - m_saneAPI.sane_control_option_api(m_devHandle, (SANE_Int)0x884A, SANE_ACTION_GET_VALUE, v, NULL); - return v; -} -void hg_settingdialog::apply_current_scheme(void) -{ - dev_que::apply_scheme(&m_saneAPI, m_devHandle, cur_scheme_); -} - -std::string sane_val_to_string(const char* val, SANE_Value_Type type) -{ - char buf[128] = {0}; - std::string ret(""); - - switch(type) - { - case SANE_TYPE_BOOL: - ret = *(SANE_Bool*)val == SANE_TRUE ? "true" : "false"; - break; - case SANE_TYPE_INT: - sprintf(buf, "%d", *(int*)val); - ret = buf; - break; - case SANE_TYPE_FIXED: - sprintf(buf, "%.4f", SANE_UNFIX(*(SANE_Fixed*)val)); - ret = buf; - break; - default: - ret = val; - break; - } - - return ret; -} -void hg_settingdialog::on_current_scheme_changed() -{ - del_this_->setEnabled(true); - del_all_->setEnabled(false); - - if (comb_->currentIndex() == 0) - { - del_this_->setEnabled(false); - } - if (comb_->count() > 1) - { - del_all_->setEnabled(true); - } - - QString text(find_current_scheme_menu()); - gb::sane_config_schm *cur = nullptr; - - cur_cfg_->select_scheme(text.toStdString().c_str()); - cur = cur_cfg_->get_scheme(); - if(!cur) - cur = new gb::sane_config_schm(); - cur->copy_default_value(cur_scheme_); - cur_scheme_->end_setting(true); - cur_scheme_->release(); - cur_scheme_ = cur; - cur_scheme_->begin_setting(); - - apply_current_scheme(); - updateUIStatus(); - changed_count_++; - - QString scheme(comb_->currentText()); - bool enabled = false; - gb::sane_config_schm *schm = cur_cfg_->get_scheme(scheme.toStdString().c_str()); - - if(schm) - enabled = true; - - memset(&m_gammaData, 0, sizeof(m_gammaData)); - for(int i = 0; i < sizeof(m_gammaData.table) / sizeof(m_gammaData.table[0]); ++i) - m_gammaData.table[i] = i & 0x0ff; - - QString info(tr("")); - std::string name(""), val(""); - if(schm && schm->first_config(name, val)) - { - do - { - QString title; - SANE_Value_Type type = SANE_TYPE_STRING; - for (int ii = 0; ii < m_list_defaultOptions.size(); ii++) - { - const SANE_Option_Descriptor* opt = reinterpret_cast(m_list_defaultOptions.at(ii).first); - if(strcmp(opt->name, name.c_str()) == 0) - { - title = QString::fromStdString(opt->title); - type = opt->type; - if(type == SANE_TYPE_STRING) - val = from_default_language(val.c_str(), nullptr); - break; - } - } - - if(title.length()) - { - info += tr("
") + title + tr(":
"); - info += tr("

") + QString::fromStdString(sane_val_to_string(val.c_str(), type)) + tr("

"); - } - else { - if(val.length() == sizeof(SANE_Gamma)) - memcpy(&m_gammaData, val.c_str(), sizeof(SANE_Gamma)); - } - }while(schm->next_config(name, val)); - } - if(schm) - schm->release(); - - sketch_->setHtml(info); -} -void hg_settingdialog::slot_pushButton_scheme_management(void) -{ - QPushButton* btn = qobject_cast(sender()); - - if(btn == m_pbtn_addNew) - { - int id = 0; - QString text(find_current_scheme_menu(&id)); - if(!text.isEmpty() && id >= 0) - { - Dialog_Input dlg(this); - - dlg.setEditText(text); - dlg.setWindowTitle(tr("Add new scheme")); - if(dlg.exec()) - { - QString newCfgName = dlg.getText(); - for (int i = 0; i < comb_->count(); ++i) - { - if (newCfgName == comb_->itemText(i)) - { - QMessageBox::information(this, tr("tips"), tr("The configuration scheme already exists")); - return; - } - } - - disconnect(comb_, SIGNAL(currentTextChanged(const QString)), this, SLOT(on_current_scheme_changed())); - comb_->insertItem(id, newCfgName); - connect(comb_, SIGNAL(currentTextChanged(const QString)), this, SLOT(on_current_scheme_changed())); - - gb::sane_config_schm *scheme = cur_scheme_->copy(); - cur_scheme_->end_setting(true); - cur_scheme_->release(); - cur_scheme_ = scheme; - cur_cfg_->add_scheme(cur_scheme_, newCfgName.toStdString().c_str()); - cur_cfg_->select_scheme(cur_scheme_->get_scheme_name().c_str()); - cur_cfg_->save(); - comb_->setCurrentIndex(id); - changed_count_++; - } - } - } - else if(btn == del_this_) - { - int id = -1; - QString text(find_current_scheme_menu(&id)); - - if(text.isEmpty()) - return; - - QMessageBox msg(QMessageBox::Question, tr("be sure to delete the configuration"), - tr("Are you sure you want to delete the configuration \"") + text + tr("\" ?"), QMessageBox::Yes | QMessageBox::No, this); - msg.exec(); - if (msg.clickedButton() != msg.button(QMessageBox::Yes)) - return; - - gb::sane_config_schm *sch = cur_cfg_->get_scheme(text.toStdString().c_str()); - cur_cfg_->remove_scheme(text.toStdString().c_str()); - comb_->removeItem(id); - sch->release(); - if(sch == cur_scheme_) - { - restore_2_default_settings(); - updateUIStatus(); - } - - on_current_scheme_changed(); - changed_count_++; - cur_cfg_->save(); - } - else if(btn == del_all_) - { - QMessageBox msg(QMessageBox::Question, tr("be sure to delete the configuration"), - tr("Are you sure you want to delete the configuration?"), QMessageBox::Yes | QMessageBox::No, this); - msg.exec(); - if (msg.clickedButton() != msg.button(QMessageBox::Yes)) - return; - - restore_2_default_settings(); - updateUIStatus(); - comb_->clear(); - comb_->addItem(tr("Default scheme")); - changed_count_++; - cur_cfg_->remove_all_schemes(); - cur_cfg_->save(); - } -} -void hg_settingdialog::restore_2_default_settings(void) -{ - cur_scheme_->end_setting(true); - cur_cfg_->select_scheme(nullptr); - dev_que_.apply_scheme(&m_saneAPI, nullptr); - cur_cfg_->save(); - - gb::sane_config_schm *s = new gb::sane_config_schm(); - s->copy_default_value(cur_scheme_); - cur_scheme_->release(); - cur_scheme_ = s; - cur_scheme_->begin_setting(); - - // on_current_scheme_changed(); - comb_->setCurrentIndex(0); -} +#include "hg_settingdialog.h" +#include +#include +#include "cutpapertool.h" +#include "setpicclrtool.h" +#include "base/HGDef.h" +#include "base/HGUtility.h" +#include "HGString.h" +#include "sane/sane_option_definitions.h" +#include "lang/app_language.h" +#include "dialog_input.h" +#include +#include "device_menu.h" +#include "app_cfg.h" +#include "dialog_device_scan.h" +std::string hg_settingdialog::property_combox_data_type_ = "combox_value_type"; + +hg_settingdialog::hg_settingdialog(const SANEAPI* saneApi, SANE_Handle handle, const char *devName, QWidget *parent) + : QDialog(parent) + , save_(false) + , btn_cut_area_(nullptr), btn_gamma_(nullptr), clicked_gamma_(false) + , custom_area_lable_(nullptr), comb_(nullptr) + , m_devHandle(handle) + , m_devName(devName) + , m_isRefreshUi(false) + , changed_count_(0) + , cur_cfg_(nullptr) +{ + m_dpiId = -1; + m_dpiValue = 200; + m_paperSizeId = -1; + m_paperSizeValue.clear(); + m_cutLeftId = -1; + m_cutTopId = -1; + m_cutRightId = -1; + m_cutBottomId = -1; + m_cutWidth = 210; + m_cutHeight = 297; + m_cutLeftValue = 0; + m_cutTopValue = 0; + m_cutRightValue = 210; + m_cutBottomValue = 297; + + m_colorModeId = -1; + m_colorModeValue.clear(); + memset(&m_gammaData, 0, sizeof(m_gammaData)); + for(int i = 0; i < sizeof(m_gammaData.table) / sizeof(m_gammaData.table[0]); ++i) + m_gammaData.table[i] = i & 0x0ff; + + memcpy(&m_saneAPI, saneApi, sizeof(SANEAPI)); + m_closeButton = closeButtonNormal; + + setIcon(); + HGChar cfgpath[512] = {0}; + GetConfigPath(cfgpath, 512); + HGBase_CreateDir(cfgpath); + + dev_que devQue; + devQue.set_root_dir(cfgpath); + QString old = QString::fromStdString(cfgpath) + PATH_SYMBOL + "scanner.schm"; + if(QFile::exists(old)) + dev_que::update_old_cfg(old.toStdString().c_str()); + + int pid = 0; + m_saneAPI.sane_control_option_api(m_devHandle, (SANE_Int)0x8853, SANE_ACTION_GET_VALUE, &pid, NULL); + + char buf[10] = { 0 }; + sprintf(buf, "%x", pid); + std::string deviceName = m_devName; + if (pid != 0) + { + deviceName = deviceName.substr(0, deviceName.find(" ")) + " " + buf; + } + + std::string fileName = std::string(cfgpath) + deviceName + ".cfg"; + cur_cfg_ = new gb::scanner_cfg(); + cur_cfg_->load_file(fileName.c_str()); + cur_scheme_ = cur_cfg_->get_scheme(); + hg_settingdialog::apply_scheme(m_devHandle, &m_saneAPI, cur_scheme_); + cur_scheme_->begin_setting(); + + def_value_ = new gb::sane_config_schm(); + initUi(); + + m_list_originDeviceScheme.clear(); + m_list_originDeviceScheme = m_list_defaultOptions; + + connect(comb_, SIGNAL(currentTextChanged(const QString)), this, SLOT(on_current_scheme_changed())); +} + +hg_settingdialog::~hg_settingdialog() +{ + if (cur_scheme_ != nullptr) + cur_scheme_->release(); + //cur_cfg_->release(); +} + +void hg_settingdialog::apply_scheme(SANE_Handle dev, LPSANEAPI api, gb::sane_config_schm* schm) +{ + const SANE_Option_Descriptor* desc = nullptr; + SANE_Int count = 0; + typedef struct _sod + { + int id; + const SANE_Option_Descriptor* desc; + }SOD; + + std::vector sods; + + api->sane_control_option_api(dev, 0, SANE_ACTION_GET_VALUE, &count, nullptr); + for (int i = 1; i < count; ++i) + { + desc = api->sane_get_option_descriptor_api(dev, i); + if (!desc) + continue; + + if (desc->type == SANE_TYPE_BUTTON || desc->type == SANE_TYPE_GROUP) + continue; + + api->sane_control_option_api(dev, i, SANE_ACTION_SET_AUTO, nullptr, nullptr); + + SOD sod; + sod.id = i; + sod.desc = desc; + sods.push_back(sod); + } + + if (schm) + { + std::string n(""), val(""); + char* buf = nullptr; + SANE_Int info = 0; + + if (schm->first_config(n, val)) + { + do + { + if (gb::sane_config_schm::is_option_data(n)) + { + if (n == SANE_STD_OPT_NAME_IS_CUSTOM_GAMMA) + { + api->sane_control_option_api(dev, SANE_OPT_ID_CUSTOM_GAMMA, SANE_ACTION_SET_VALUE, &val[0], &info); + } + } + else + { + for (auto& v : sods) + { + if (n == v.desc->name) + { + if (v.desc->type == SANE_TYPE_STRING) + { + buf = new char[v.desc->size + 4]; + strcpy(buf, val.c_str()); + api->sane_control_option_api(dev, v.id, SANE_ACTION_SET_VALUE, buf, &info); + delete[] buf; + } + else + api->sane_control_option_api(dev, v.id, SANE_ACTION_SET_VALUE, &val[0], &info); + break; + } + } + } + } while (schm->next_config(n, val)); + } + } +} + +void hg_settingdialog::initUi() +{ + update_opt_value_from_driver(); + createUI(); + +#if defined(OEM_ZHONGJING) + setWindowTitle("Microtek DocWizard EX TWAIN"); +#else + //setWindowTitle(QString::fromStdString(dev_que_.opened_scanner_name())); + setWindowTitle(QString::fromStdString(m_devName)); +#endif + + Qt::WindowFlags type = Qt::Dialog | Qt::WindowCloseButtonHint; +#ifdef HG_CMP_MSC + OSVERSIONINFOW info; + info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW); + if (GetVersionExW(&info)) + { + if (info.dwMajorVersion == 6 && info.dwMinorVersion == 1) + { + type = Qt::SubWindow | Qt::Popup | Qt::WindowStaysOnTopHint; + } + } +#endif + setWindowFlags(type); + + resize(740, height()); +} + +void hg_settingdialog::update_opt_value_from_driver() +{ + bool first = true; + + m_list_defaultOptions.clear(); + m_list_getOpt.clear(); + + // custom gamma ... + m_saneAPI.sane_control_option_api(m_devHandle, SANE_OPT_ID_CUSTOM_GAMMA, SANE_ACTION_GET_VALUE, &m_gammaData, nullptr); + + SANE_Int dev_options = 0; + m_saneAPI.sane_control_option_api(m_devHandle, 0, SANE_ACTION_GET_VALUE, &dev_options, nullptr); + for (int i = 1, j= dev_options; i < j; i++) + { + const SANE_Option_Descriptor* opt = m_saneAPI.sane_get_option_descriptor_api(m_devHandle, i); + SANE_Int method = 0; + if (opt == nullptr) + { + m_list_defaultOptions.append(QPair(opt, QVariant(0))); + } + else + { + if(opt->type == SANE_TYPE_INT) + { + SANE_Int init = 0; + + m_saneAPI.sane_control_option_api(m_devHandle, i, SANE_ACTION_GET_VALUE, &init, &method); + m_list_defaultOptions.append(QPair(opt, QVariant(init))); + m_list_getOpt.append(QPair(i, opt)); + + if(first) + { + unsigned int n = i; + dev_que::get_default_value(&m_saneAPI, m_devHandle, n, &init); + def_value_->set_default_value(i, opt->name, (char*)&init, sizeof(init)); + } + } + else if(opt->type == SANE_TYPE_FIXED) + { + SANE_Fixed init = 0; + + m_saneAPI.sane_control_option_api(m_devHandle, i, SANE_ACTION_GET_VALUE, &init, &method); + m_list_defaultOptions.append(QPair(opt, QVariant(init))); + m_list_getOpt.append(QPair(i, opt)); + + if(first) + { + unsigned int n = i; + dev_que::get_default_value(&m_saneAPI, m_devHandle, n, &init); + def_value_->set_default_value(i, opt->name, (char*)&init, sizeof(init)); + } + } + else if(opt->type == SANE_TYPE_BOOL) + { + SANE_Bool init = 0; + + m_saneAPI.sane_control_option_api(m_devHandle, i, SANE_ACTION_GET_VALUE, &init, &method); + m_list_defaultOptions.append(QPair(opt, QVariant(init))); + m_list_getOpt.append(QPair(i, opt)); + + if(first) + { + unsigned int n = i; + dev_que::get_default_value(&m_saneAPI, m_devHandle, n, &init); + def_value_->set_default_value(i, opt->name, (char*)&init, sizeof(init)); + } + } + else if(opt->type == SANE_TYPE_STRING) + { + char *init = (char*)malloc(opt->size * 2 + 4); + + m_saneAPI.sane_control_option_api(m_devHandle, i, SANE_ACTION_GET_VALUE, init, &method); + m_list_defaultOptions.append(QPair(opt, QVariant(QString::fromStdString(init)))); + m_list_getOpt.append(QPair(i, opt)); + + if(first) + { + unsigned int n = i; + int err = dev_que::get_default_value(&m_saneAPI, m_devHandle, n, init); + (void)err; + std::string langCN(to_default_language(init, nullptr)); + def_value_->set_default_value(i, opt->name, &langCN[0], langCN.length()); + } + free(init); + } + else + { + m_list_defaultOptions.append(QPair(opt, QVariant(0))); + m_list_getOpt.append(QPair(i, opt)); + } + } + } +} + +QString hg_settingdialog::find_current_scheme_menu(int *scheme_id) +{ + QString text(comb_->currentText()); + + if(scheme_id) + { + if(comb_->currentIndex() >= 0 && comb_->currentIndex() < comb_->count()) + *scheme_id = comb_->currentIndex(); + else { + *scheme_id = -1; + } + } + + return text; +} +void hg_settingdialog::create_scheme_management_ui(QVBoxLayout* layout) +{ + QLabel *title = new QLabel(this); + m_label_restore = new QLabel(this); + bool enabled = false; + QHBoxLayout *hLayout = new QHBoxLayout(); + int width = 180; + std::vector schemes; + std::string cur_schm(cur_cfg_->get_current_scheme_name()); + + cur_cfg_->get_all_schemes(schemes); + comb_ = new QComboBox(this); + layout->addSpacing(30); + for(int i = 0; i < (int)schemes.size(); ++i) + { + comb_->addItem(QString::fromStdString(schemes[i])); + if(cur_schm == schemes[i]) + { + enabled = true; + comb_->setCurrentText(QString::fromStdString(schemes[i])); + } + } + + if(!enabled) + comb_->setCurrentIndex(0); + + title->setFixedWidth(width); + comb_->setFixedWidth(width); + + title->setText(tr("existing configuration scheme")); + layout->addWidget(title); + layout->addWidget(comb_); + + layout->addSpacing(10); + + m_pbtn_addNew = new QPushButton(this); + m_pbtn_addNew->setText(tr("Add new")); + m_pbtn_addNew->setFixedWidth(width / 2); + layout->addWidget(m_pbtn_addNew); + connect(m_pbtn_addNew, SIGNAL(clicked(bool)), this, SLOT(slot_pushButton_scheme_management())); + + layout->addSpacing(10); + + m_pbtn_Save = new QPushButton(this); + m_pbtn_Save->setText(tr("Save")); + m_pbtn_Save->setFixedWidth(width / 2); + layout->addWidget(m_pbtn_Save); + connect(m_pbtn_Save, SIGNAL(clicked(bool)), this, SLOT(slot_pushButton_scheme_management())); + + layout->addSpacing(10); + + m_deleteCur = new QPushButton(this); + m_deleteCur->setText(tr("Delete")); + m_deleteCur->setEnabled(enabled); + m_deleteCur->setFixedWidth(width / 2); + layout->addWidget(m_deleteCur); + connect(m_deleteCur, SIGNAL(clicked(bool)), this, SLOT(slot_pushButton_scheme_management())); + + layout->addSpacing(10); + + m_deleteAll = new QPushButton(this); + m_deleteAll->setText(tr("Delete all")); + m_deleteAll->setEnabled(enabled); + m_deleteAll->setFixedWidth(width / 2); + layout->addWidget(m_deleteAll); + connect(m_deleteAll, SIGNAL(clicked(bool)), this, SLOT(slot_pushButton_scheme_management())); + + layout->addSpacing(10); + m_label_restore->setWordWrap(true); + m_label_restore->setStyleSheet("color:red;"); + + QString prompt = QString(tr("The current parameter settings are inconsistent with the configuration scheme '%1'. To use the configuration scheme '%1' parameters, please click the restore button")); + m_label_restore->setText(prompt.arg(comb_->currentText())); + layout->addWidget(m_label_restore); + + m_pbtn_restore = new QPushButton(this); + m_pbtn_restore->setText(tr("Restore")); + m_pbtn_restore->setFixedWidth(width / 2); + layout->addWidget(m_pbtn_restore); + connect(m_pbtn_restore, SIGNAL(clicked(bool)), this, SLOT(slot_pushButton_scheme_management())); + + layout->addStretch(); + + updateRestorePushButton(); + + m_deleteCur->setEnabled(true); + m_deleteAll->setEnabled(true); + m_pbtn_Save->setEnabled(true); + int index = comb_->currentIndex(); + int count = comb_->count(); + if (index == 0) + { + m_deleteCur->setEnabled(false); + m_pbtn_Save->setEnabled(false); + } + + if (count == 1) + { + m_deleteAll->setEnabled(false); + } + + //title = new QLabel(this); + //title->setText(tr("confgiuration information:")); + //layout->addWidget(title); + +// sketch_ = new QTextEdit(this); +// sketch_->setReadOnly(true); +// sketch_->setFixedSize(width, 200); +// sketch_->setVisible(false); + //layout->addWidget(sketch_); + //on_current_scheme_changed(); +} +void hg_settingdialog::createUI() +{ + QTabWidget *tabWidgetCreation = new QTabWidget(this); + + QPushButton *buttonAbout = new QPushButton(this); + buttonAbout->setText(tr("about...")); + QPushButton *buttonOk = new QPushButton(this); + buttonOk->setText(tr("ok")); + QPushButton *buttonCancel = new QPushButton(this); + buttonCancel->setText(tr("cancel")); + QHBoxLayout *hlayoutOkAndCancel = new QHBoxLayout; + hlayoutOkAndCancel->addStretch(); + hlayoutOkAndCancel->addWidget(buttonAbout); + hlayoutOkAndCancel->addWidget(buttonOk); + hlayoutOkAndCancel->addWidget(buttonCancel); + QWidget *widgetOkAndCancel = new QWidget(); + widgetOkAndCancel->setLayout(hlayoutOkAndCancel); + connect(buttonAbout, SIGNAL(clicked(bool)), this, SLOT(slot_buttonAboutClicked())); + connect(buttonOk, SIGNAL(clicked(bool)), this, SLOT(slot_buttonOkClicked())); + connect(buttonCancel, SIGNAL(clicked(bool)), this, SLOT(slot_buttonCancelClicked())); + + QHBoxLayout *h = new QHBoxLayout(); + QVBoxLayout *v1 = new QVBoxLayout(), + *v2 = new QVBoxLayout(); + create_scheme_management_ui(v1); + v2->addWidget(tabWidgetCreation); + + QGroupBox *grp = new QGroupBox(tr("configuration scheme management"), this); + grp->setLayout(v1); + grp->setFixedSize(195, 500); + + h->addWidget(grp); + h->addLayout(v2); + + QVBoxLayout* mainVerticalLayout = new QVBoxLayout(this); + mainVerticalLayout->addLayout(h); +// mainVerticalLayout->addWidget(tabWidgetCreation); + mainVerticalLayout->addWidget(widgetOkAndCancel); + this->setLayout(mainVerticalLayout); + + QScrollArea* scrollArea = new QScrollArea; + scrollArea->setWidgetResizable(true); + scrollArea->setAlignment(Qt::AlignCenter); + QFormLayout* layout = new QFormLayout; + + QWidget* widget = new QWidget; + widget->setLayout(layout); + + bool isBegin = true; + + for (int i = 0; i < m_list_defaultOptions.size(); i++) + { + const SANE_Option_Descriptor* opt = reinterpret_cast(m_list_defaultOptions.at(i).first); + int ind = -1; + std::string cur_val(""); + if(opt == nullptr) continue; + h = nullptr; + cur_scheme_->get_config(opt->name, cur_val); + switch (opt->type) + { + case SANE_TYPE_BOOL: + { + QCheckBox *checkBoxCreation = new QCheckBox; + + if (strcmp(opt->name, SANE_STD_OPT_NAME_CUSTOM_AREA) == 0) + { + QWidget* widget_cbtn_pbtn = new QWidget; + widget_cbtn_pbtn->setMaximumWidth(200); + + QLabel *label = new QLabel; + label->setText(QString::fromStdString(opt->title) + QString(" : ")); + + btn_cut_area_ = new QPushButton; + btn_cut_area_->setText(tr("regional crop")); + btn_cut_area_->setFixedWidth(150); + + QHBoxLayout *hLayout = new QHBoxLayout; + hLayout->addWidget(checkBoxCreation); + hLayout->addWidget(btn_cut_area_); + widget_cbtn_pbtn->setLayout(hLayout); + + custom_area_lable_ = label; + + reinterpret_cast(widget->layout())->addRow(label, widget_cbtn_pbtn); + + connect(btn_cut_area_, SIGNAL(clicked(bool)), this, SLOT(slot_cutButtonClicked())); + } + else if (strcmp(opt->name, SANE_STD_OPT_NAME_IS_CUSTOM_GAMMA) == 0) + { + continue; + // QWidget* widget_cbtn_pbtn = new QWidget(scrollArea); + // widget_cbtn_pbtn->setMaximumWidth(200); + // btn_gamma_ = new QPushButton(widget_cbtn_pbtn); + // btn_gamma_->setText(tr("custom tone curve")); + // btn_gamma_->setFixedWidth(150); + + // QHBoxLayout *hLayout = new QHBoxLayout; + // hLayout->addWidget(checkBoxCreation); + // hLayout->addWidget(btn_gamma_); + // widget_cbtn_pbtn->setLayout(hLayout); + + // reinterpret_cast(widget->layout())->addRow(opt->title + QString(" : "), widget_cbtn_pbtn); + // connect(btn_gamma_, SIGNAL(clicked(bool)), this, SLOT(slot_gammaButtonClicked())); + } + else + reinterpret_cast(widget->layout())->addRow(opt->title + QString(" : "), checkBoxCreation); + + checkBoxCreation->setToolTip(opt->desc); + int id = i + 1; + bool enable = *(bool*)&cur_val[0]; + checkBoxCreation->setProperty("controls_id", id); + checkBoxCreation->setChecked(enable); + if (strcmp(opt->name, SANE_STD_OPT_NAME_IS_CUSTOM_GAMMA) == 0 && btn_gamma_ != nullptr) + { + btn_gamma_->setEnabled(enable); + } + if (strcmp(opt->name, SANE_STD_OPT_NAME_CUSTOM_AREA) == 0) + btn_cut_area_->setEnabled(enable); + else if (strcmp(opt->name, SANE_STD_OPT_NAME_IS_CUSTOM_GAMMA) == 0 && btn_gamma_ != nullptr) + btn_gamma_->setEnabled(enable); + connect(checkBoxCreation, SIGNAL(stateChanged(int)), this, SLOT(slot_checkedClicked())); + m_list_widgets.append(checkBoxCreation); + + m_list_getOpt.append(QPair(id, opt)); + break; + } + + case SANE_TYPE_INT: + { + switch(opt->constraint_type) + { + case SANE_CONSTRAINT_NONE: + { + QSpinBox* spinBox = new QSpinBox(scrollArea); + spinBox->setMinimumWidth(150); + spinBox->setToolTip(opt->desc); + spinBox->setRange(1, 1000); + int id = i + 1; + spinBox->setProperty("controls_id", id); + + QHBoxLayout* hLayout = new QHBoxLayout; + hLayout->addWidget(spinBox); + hLayout->addStretch(); + reinterpret_cast(widget->layout())->addRow(opt->title + QString(" : "), spinBox); + + m_list_widgets.append(spinBox); + m_list_getOpt.append(QPair(id, opt)); + + spinBox->setValue(*(int*)&cur_val[0]); + connect(spinBox, SIGNAL(valueChanged(int)), this, SLOT(slot_spinBoxClicked(int))); + break; + } + + case SANE_CONSTRAINT_RANGE: + { + QWidget* widget_slider_spin = new QWidget(scrollArea); + widget_slider_spin->setMinimumWidth(300); + + QSlider* sliderCreation = new QSlider(widget_slider_spin); + + if (0 == strcmp(opt->name, SANE_STD_OPT_NAME_THRESHOLD) || 0 == strcmp(opt->name, SANE_STD_OPT_NAME_ANTI_NOISE_LEVEL) || + 0 == strcmp(opt->name, SANE_STD_OPT_NAME_MARGIN)) + { + sliderCreation->installEventFilter(this); + } + + sliderCreation->setOrientation(Qt::Horizontal); + sliderCreation->setMinimumWidth(120); + sliderCreation->setRange(opt->constraint.range->min, opt->constraint.range->max); + sliderCreation->setToolTip(opt->desc); + sliderCreation->setProperty("controls_id", i+1); + sliderCreation->setValue(m_list_defaultOptions.at(i).second.toInt()); + + QSpinBox* spinBox = new QSpinBox(widget_slider_spin); + spinBox->setMinimumWidth(150); + spinBox->setToolTip(opt->desc); + spinBox->setRange(opt->constraint.range->min, opt->constraint.range->max); + + spinBox->setSingleStep(1); + spinBox->setValue(m_list_defaultOptions.at(i).second.toInt()); + int id = i + 1; + spinBox->setProperty("controls_id", id); + m_list_sliderSpinbox.append(QPair(sliderCreation, spinBox)); + + QHBoxLayout* hLayout = new QHBoxLayout; + hLayout->addWidget(sliderCreation); + hLayout->addWidget(spinBox); +// hLayout->addStretch(); + widget_slider_spin->setLayout(hLayout); + reinterpret_cast(widget->layout())->addRow(opt->title + QString(" : "), widget_slider_spin); + + m_list_widgets.append(sliderCreation); + m_list_widgets.append(spinBox); + m_list_getOpt.append(QPair(id, opt)); + + int cur = *(int*)&cur_val[0]; + spinBox->setValue(cur); + sliderCreation->setValue(cur); + + connect(spinBox, SIGNAL(valueChanged(int)), this, SLOT(slot_spinBoxClicked(int))); + connect(sliderCreation, SIGNAL(valueChanged(int)), this, SLOT(slot_sliderClicked(int))); + + break; + } + + case SANE_CONSTRAINT_WORD_LIST: + { + QComboBox* comboBoxCreation = new QComboBox(scrollArea); + comboBoxCreation->setToolTip(opt->desc); + int id = i + 1; + comboBoxCreation->setProperty("controls_id", id); + reinterpret_cast(widget->layout())->addRow(opt->title + QString(" : "), comboBoxCreation); + + auto p_str = opt->constraint.word_list; + char buf[20]; + for(SANE_Int i = 0; i < p_str[0]; ++i) + { + sprintf(buf, "%d", p_str[i + 1]); + comboBoxCreation->addItem(QString::fromStdString(buf)); + } + sprintf(buf, "%d", m_list_defaultOptions.at(i).second.toInt()); + comboBoxCreation->setProperty(hg_settingdialog::property_combox_data_type_.c_str(), COMBO_VAL_INT); + m_list_widgets.append(comboBoxCreation); + m_list_getOpt.append(QPair(id, opt)); + + char nstr[40] = {0}; + sprintf(nstr, "%d", *(int*)&cur_val[0]); + comboBoxCreation->setCurrentText(QString::fromStdString(nstr)); + connect(comboBoxCreation, SIGNAL(currentTextChanged(const QString)), this, SLOT(slot_string_list_comboBoxClicked())); + break; + } + + case SANE_CONSTRAINT_STRING_LIST: + break; + } + break; + } + + case SANE_TYPE_FIXED: + { + QWidget* widget_slider_spin = new QWidget(scrollArea); + widget_slider_spin->setMinimumWidth(300); + QSlider* sliderCreation = new QSlider(widget_slider_spin); + sliderCreation->setOrientation(Qt::Horizontal); + sliderCreation->setMinimumWidth(120); + sliderCreation->setToolTip(opt->desc); + int id = i + 1; + sliderCreation->setProperty("controls_id", id); + sliderCreation->setRange(SANE_UNFIX(opt->constraint.range->min) * 100, SANE_UNFIX(opt->constraint.range->max) * 100); + sliderCreation->setValue(SANE_UNFIX(m_list_defaultOptions.at(i).second.toDouble()) * 100); + + QDoubleSpinBox* spinBox = new QDoubleSpinBox(widget_slider_spin); + spinBox->setMinimumWidth(150); + spinBox->setToolTip(opt->desc); + spinBox->setDecimals(2); + spinBox->setSingleStep(0.01); + spinBox->setRange(SANE_UNFIX(opt->constraint.range->min), SANE_UNFIX(opt->constraint.range->max)); + spinBox->setValue(sliderCreation->value() * spinBox->singleStep()); + spinBox->setProperty("controls_id", id); + + m_list_sliderSpinbox.append(QPair(sliderCreation, spinBox)); + + QHBoxLayout* hLayout = new QHBoxLayout; + hLayout->addWidget(sliderCreation); + hLayout->addWidget(spinBox); +// hLayout->addStretch(); + widget_slider_spin->setLayout(hLayout); + reinterpret_cast(widget->layout())->addRow(opt->title + QString(" : "), widget_slider_spin); + + m_list_widgets.append(sliderCreation); + m_list_widgets.append(spinBox); + + m_list_getOpt.append(QPair(id, opt)); +// iniRead(md5(opt->title), id, sliderCreation); +// iniRead(md5(opt->title), id, spinBox); + + float v = SANE_UNFIX(*(SANE_Fixed*)&cur_val[0]); + sliderCreation->setValue(v * 100); + spinBox->setValue(sliderCreation->value() * spinBox->singleStep()); + connect(spinBox, SIGNAL(valueChanged(double)), this, SLOT(slot_doubleSpinboxClicked(double))); + connect(sliderCreation, SIGNAL(valueChanged(int)), this, SLOT(slot_sliderClicked(int))); + break; + } + + case SANE_TYPE_STRING: + { + switch(opt->constraint_type) + { + case SANE_CONSTRAINT_NONE: + { + QLineEdit *lineEdit = new QLineEdit(scrollArea); + lineEdit->setToolTip(opt->desc); + int id = i + 1; + lineEdit->setProperty("controls_id", id); + reinterpret_cast(widget->layout())->addRow(opt->title + QString(" : "), lineEdit); + + m_list_widgets.append(lineEdit); + m_list_getOpt.append(QPair(id, opt)); +// iniRead(md5(opt->title), id, lineEdit); + + lineEdit->setText(QString::fromStdString(cur_val)); + connect(lineEdit, SIGNAL(textChanged(const QString&)), this, SLOT(slot_lineEditInput())); + break; + } + + case SANE_CONSTRAINT_RANGE: + break; + case SANE_CONSTRAINT_WORD_LIST: + break; + + case SANE_CONSTRAINT_STRING_LIST: + { + QComboBox* comboBoxCreation = new QComboBox(scrollArea); + comboBoxCreation->setToolTip(opt->desc); + int id = i + 1; + comboBoxCreation->setProperty("controls_id", id); + reinterpret_cast(widget->layout())->addRow(opt->title + QString(" : "), comboBoxCreation); + + if (0 == strcmp(opt->name, SANE_STD_OPT_NAME_TEXT_DIRECTION)) + { + comboBoxCreation->installEventFilter(this); + } + + auto p_str = opt->constraint.string_list; + QStringList stringList; + while(*p_str) + { + stringList.append(*p_str); + p_str++; + } + + if(stringList.isEmpty() != true) + { + for(int i = 0; i < (stringList.size()); i++) + comboBoxCreation->addItem(stringList.at(i)); + } + comboBoxCreation->setCurrentText(m_list_defaultOptions.at(i).second.toString()); + comboBoxCreation->setProperty(hg_settingdialog::property_combox_data_type_.c_str(), COMBO_VAL_STRING); + //printf("Option %02d default value is: %s\n", i + 1, m_list_defaultOptions.at(i).second.toString().data()); + + m_list_widgets.append(comboBoxCreation); + + m_list_getOpt.append(QPair(id, opt)); +// iniRead(md5(opt->title), id, comboBoxCreation); + + comboBoxCreation->setCurrentText(QString::fromStdString(cur_val)); + connect(comboBoxCreation, SIGNAL(currentTextChanged(const QString)), this, SLOT(slot_string_list_comboBoxClicked())); + break; + } + } + break; + } + + case SANE_TYPE_BUTTON: + { + QPushButton* pushButton = new QPushButton(this); + pushButton->setText(opt->title); + pushButton->setToolTip(opt->desc); + int id = i + 1; + pushButton->setProperty("controls_id", id); + hlayoutOkAndCancel->insertWidget(0, pushButton, 0, Qt::AlignRight); + + connect(pushButton, SIGNAL(clicked(bool)), this, SLOT(slot_pushButtonClicked())); + break; + } + + case SANE_TYPE_GROUP: + { + if (isBegin) + { + scrollArea->setWindowTitle(opt->title); + isBegin = false; + }else{ + scrollArea->setWidget(widget); + tabWidgetCreation->addTab(scrollArea, scrollArea->windowTitle()); + scrollArea = new QScrollArea; + scrollArea->setWidgetResizable(true); + scrollArea->setWindowTitle(opt->title); + layout = new QFormLayout; + widget = new QWidget; + widget->setLayout(layout); + } + + m_list_widgets.append(nullptr); + break; + } + } //switch(opt->type) + +// if (Utf8ToStdString(opt->title) == "分辨率") + if (strcmp(opt->name, SANE_STD_OPT_NAME_RESOLUTION) == 0) + { + m_dpiId = i + 1; + m_dpiValue = m_list_defaultOptions.at(i).second.toInt(); + } +// else if (Utf8ToStdString(opt->title) == "纸张尺寸") + else if (strcmp(opt->name, SANE_STD_OPT_NAME_PAPER) == 0) + { + m_paperSizeId = i + 1; + m_paperSizeValue = m_list_defaultOptions.at(i).second.toString(); + } + + if (0 == strcmp(opt->name, SANE_STD_OPT_NAME_CUSTOM_AREA_LEFT)) + { + m_cutLeftId = i + 1; + m_cutLeftValue = SANE_UNFIX(m_list_defaultOptions.at(i).second.toInt()); + if (opt->constraint_type == SANE_CONSTRAINT_RANGE) + m_cutWidth = SANE_UNFIX(opt->constraint.range->max); + } + else if (0 == strcmp(opt->name, SANE_STD_OPT_NAME_CUSTOM_AREA_TOP)) + { + m_cutTopId = i + 1; + m_cutTopValue = SANE_UNFIX(m_list_defaultOptions.at(i).second.toInt()); + if (opt->constraint_type == SANE_CONSTRAINT_RANGE) + m_cutHeight = SANE_UNFIX(opt->constraint.range->max); + } + else if (0 == strcmp(opt->name, SANE_STD_OPT_NAME_CUSTOM_AREA_RIGHT)) + { + m_cutRightId = i + 1; + m_cutRightValue = SANE_UNFIX(m_list_defaultOptions.at(i).second.toInt()); + } + else if (0 == strcmp(opt->name, SANE_STD_OPT_NAME_CUSTOM_AREA_BOTTOM)) + { + m_cutBottomId = i + 1; + m_cutBottomValue = SANE_UNFIX(m_list_defaultOptions.at(i).second.toInt()); + } + +// else if (Utf8ToStdString(opt->title) == "颜色模式") + if (strcmp(opt->name, SANE_STD_OPT_NAME_COLOR_MODE) == 0) + { + m_colorModeId = i + 1; + m_colorModeValue = m_list_defaultOptions.at(i).second.toString(); + } +// else if (Utf8ToStdString(opt->title) == "伽玛" || Utf8ToStdString(opt->title) == "伽玛值") + + } //for + + hlayoutOkAndCancel->insertWidget(0, buttonAbout, 0, Qt::AlignRight); + updateUIStatus(); + + scrollArea->setWidget(widget); + tabWidgetCreation->addTab(scrollArea, scrollArea->windowTitle()); +} + +void hg_settingdialog::refresh_control_value(int op_id) +{ + QVector ctrls = find_control(op_id); + + if(ctrls.empty()) + return; + + const SANE_Option_Descriptor* opt = (const SANE_Option_Descriptor*)m_list_defaultOptions.at(op_id - 1).first; + if(opt->type == SANE_TYPE_BOOL) + { + for(size_t i = 0; i < (size_t)ctrls.size(); ++i) + { + QCheckBox* cb = qobject_cast(ctrls[i]); + if(cb) + { + disconnect(cb, SIGNAL(stateChanged(int)), this, SLOT(slot_checkedClicked())); + cb->setChecked(m_list_defaultOptions.at(op_id - 1).second.toBool()); + if (strcmp(opt->name, SANE_STD_OPT_NAME_IS_CUSTOM_GAMMA) == 0 && btn_gamma_ != nullptr) + { + btn_gamma_->setEnabled(m_list_defaultOptions.at(op_id - 1).second.toBool()); + } + if (strcmp(opt->name, SANE_STD_OPT_NAME_CUSTOM_AREA) == 0 && btn_cut_area_ != nullptr) + { + btn_cut_area_->setEnabled(m_list_defaultOptions.at(op_id - 1).second.toBool()); + } + + connect(cb, SIGNAL(stateChanged(int)), this, SLOT(slot_checkedClicked())); + break; + } + } + } + else if(opt->type == SANE_TYPE_INT) + { + for(size_t i = 0; i < (size_t)ctrls.size(); ++i) + { + QComboBox* comb = qobject_cast(ctrls[i]); + if(comb) + { + char buf[40] = {0}; + sprintf(buf, "%d", m_list_defaultOptions.at(op_id - 1).second.toInt()); + comb->disconnect(comb, SIGNAL(currentTextChanged(const QString)), this, SLOT(slot_string_list_comboBoxClicked())); + + auto p_str = opt->constraint.word_list; + char buf2[20]; + comb->clear(); + for (SANE_Int i = 0; i < p_str[0]; ++i) + { + sprintf(buf2, "%d", p_str[i + 1]); + comb->addItem(QString::fromStdString(buf2)); + } + + comb->setCurrentText(QString::fromStdString(buf)); + connect(comb, SIGNAL(currentTextChanged(const QString)), this, SLOT(slot_string_list_comboBoxClicked())); + } + else + { + QSlider* slider = qobject_cast(ctrls[i]); + if(slider) + { + disconnect(slider, SIGNAL(valueChanged(int)), this, SLOT(slot_sliderClicked(int))); + slider->setValue(m_list_defaultOptions.at(op_id - 1).second.toInt()); + if (opt->constraint_type == SANE_CONSTRAINT_RANGE) + slider->setRange(opt->constraint.range->min, opt->constraint.range->max); + connect(slider, SIGNAL(valueChanged(int)), this, SLOT(slot_sliderClicked(int))); + } + else + { + QSpinBox* spin = qobject_cast(ctrls[i]); + if(spin) + { + disconnect(spin, SIGNAL(valueChanged(int)), this, SLOT(slot_spinBoxClicked(int))); + spin->setValue(m_list_defaultOptions.at(op_id - 1).second.toInt()); + if (opt->constraint_type == SANE_CONSTRAINT_RANGE) + spin->setRange(opt->constraint.range->min, opt->constraint.range->max); + connect(spin, SIGNAL(valueChanged(int)), this, SLOT(slot_spinBoxClicked(int))); + } + } + } + } + } + else if(opt->type == SANE_TYPE_FIXED) + { + double val = SANE_UNFIX(m_list_defaultOptions.at(op_id - 1).second.toInt()); + QSlider *slider = NULL; + QDoubleSpinBox* spin = NULL; + for(size_t i = 0; i < (size_t)ctrls.size(); ++i) + { + QComboBox* comb = qobject_cast(ctrls[i]); + if(comb) + { + char buf[40] = {0}; + sprintf(buf, "%f", val); + comb->disconnect(comb, SIGNAL(currentTextChanged(const QString)), this, SLOT(slot_string_list_comboBoxClicked())); + comb->setCurrentText(QString::fromStdString(buf)); + connect(comb, SIGNAL(currentTextChanged(const QString)), this, SLOT(slot_string_list_comboBoxClicked())); + } + else if(!slider) + { + slider = qobject_cast(ctrls[i]); +// if(slider) +// disconnect(slider, SIGNAL(valueChanged(int)), this, SLOT(slot_sliderClicked(int))); + } + else if(!spin) + { + spin = qobject_cast(ctrls[i]); +// if(spin) +// disconnect(spin, SIGNAL(valueChanged(double)), this, SLOT(slot_spinBoxClicked(double))); + } + } + if (slider) + { + disconnect(slider, SIGNAL(valueChanged(int)), this, SLOT(slot_sliderClicked(int))); + if (opt->constraint_type == SANE_CONSTRAINT_RANGE) + slider->setRange(SANE_UNFIX(opt->constraint.range->min) * 100, SANE_UNFIX(opt->constraint.range->max) * 100); + slider->setValue(val * 100); + connect(slider, SIGNAL(valueChanged(int)), this, SLOT(slot_sliderClicked(int))); + } + + if (spin) + { + disconnect(spin, SIGNAL(valueChanged(double)), this, SLOT(slot_spinBoxClicked(double))); + if (opt->constraint_type == SANE_CONSTRAINT_RANGE) + spin->setRange(SANE_UNFIX(opt->constraint.range->min), SANE_UNFIX(opt->constraint.range->max)); + spin->setValue(val); + connect(spin, SIGNAL(valueChanged(double)), this, SLOT(slot_spinBoxClicked(double))); + } + } + else if(opt->type == SANE_TYPE_STRING) + { + for(size_t i = 0; i < (size_t)ctrls.size(); ++i) + { + QComboBox* comb = qobject_cast(ctrls[i]); + if(comb) + { + disconnect(comb, SIGNAL(currentTextChanged(const QString)), this, SLOT(slot_string_list_comboBoxClicked())); + + comb->clear(); + auto p_str = opt->constraint.string_list; + QStringList stringList; + while (*p_str) + { + stringList.append(*p_str); + p_str++; + } + + if (stringList.isEmpty() != true) + { + for (int i = 0; i < (stringList.size()); i++) + comb->addItem(stringList.at(i)); + } + + comb->setCurrentText(m_list_defaultOptions.at(op_id - 1).second.toString()); + // comb->setProperty(hg_settingdialog::property_combox_data_type_.c_str(), COMBO_VAL_STRING); + connect(comb, SIGNAL(currentTextChanged(const QString)), this, SLOT(slot_string_list_comboBoxClicked())); + } + else + { + QLineEdit* edit = qobject_cast(ctrls[i]); + if(edit) + { + disconnect(edit, SIGNAL(textChanged(const QString&)), this, SLOT(slot_lineEditInput())); + edit->setText(m_list_defaultOptions.at(op_id - 1).second.toString()); + connect(edit, SIGNAL(textChanged(const QString&)), this, SLOT(slot_lineEditInput())); + } + } + } + } +} +QVector hg_settingdialog::find_control(int opt_num) +{ + QVector list_w; + for(int i = 0; i< m_list_widgets.size(); i++) + { + if (m_list_widgets.at(i) == nullptr) continue; + QWidget* w = m_list_widgets.at(i); + int id = w->property("controls_id").toInt(); + if(opt_num == id) + list_w.append(w); + } + return list_w; +} + +void hg_settingdialog::updateUIStatus() +{ + update_opt_value_from_driver(); + + SANE_Int dev_options = 0; + m_saneAPI.sane_control_option_api(m_devHandle, 0, SANE_ACTION_GET_VALUE, &dev_options, nullptr); + for(int id = 1, optons = dev_options; id < optons; id++) + { + QVector list_widgets = find_control(id); + if (list_widgets.empty()) continue; + QWidget* widget = list_widgets.first(); + if (widget == nullptr) continue; + QWidget* parentWidget = widget->parentWidget(); + + while (parentWidget->layout() && + typeid(*(parentWidget->layout())) != typeid(QFormLayout)) + { + widget = parentWidget; + parentWidget = widget->parentWidget(); + } + + QFormLayout* layout = reinterpret_cast(parentWidget->layout()); + const SANE_Option_Descriptor* opt = reinterpret_cast(m_list_defaultOptions.at(id - 1).first); + bool hide = (opt->cap & SANE_CAP_INACTIVE) == SANE_CAP_INACTIVE; + QWidget* w_label = layout ? layout->labelForField(widget) : nullptr; + + if( strcmp(opt->name, SANE_STD_OPT_NAME_CUSTOM_AREA_LEFT) == 0 || + strcmp(opt->name, SANE_STD_OPT_NAME_CUSTOM_AREA_TOP) == 0 || + strcmp(opt->name, SANE_STD_OPT_NAME_CUSTOM_AREA_RIGHT) == 0 || + strcmp(opt->name, SANE_STD_OPT_NAME_CUSTOM_AREA_BOTTOM) == 0 ) + hide = true; + + refresh_control_value(id); + + if (strcmp(opt->name, SANE_STD_OPT_NAME_BRIGHTNESS) == 0 || + strcmp(opt->name, SANE_STD_OPT_NAME_CONTRAST) == 0 || + strcmp(opt->name, SANE_STD_OPT_NAME_GAMMA) == 0 || + strcmp(opt->name, SANE_STD_OPT_NAME_IS_CUSTOM_GAMMA) == 0) + { + if (w_label) + w_label->show(); + widget->setVisible(true); + } + else + { + if(w_label) + hide ? w_label->hide() : w_label->show(); + widget->setVisible(!hide); + } + + if(strcmp(opt->name, SANE_STD_OPT_NAME_CUSTOM_AREA) == 0) + { + if(hide) + { + custom_area_lable_->hide(); + btn_cut_area_->hide(); + } + else + { + custom_area_lable_->show(); + btn_cut_area_->show(); + } + } + } +} + +void hg_settingdialog::slot_checkedClicked() +{ + QCheckBox *checkBox = qobject_cast(sender()); + SANE_Int id = checkBox->property("controls_id").toInt(); + SANE_Bool checkBoxcurrentState = checkBox->isChecked(); + + if (checkBox->underMouse()) + { + m_isRefreshUi = false; + } + + SANE_Int method = 0; + SANE_Status ret = m_saneAPI.sane_control_option_api(m_devHandle, id, SANE_ACTION_SET_VALUE, &checkBoxcurrentState, &method); + if (ret == SANE_STATUS_UNSUPPORTED) + { + SANE_Bool value = false; + ret = m_saneAPI.sane_control_option_api(m_devHandle, id, SANE_ACTION_GET_VALUE, &value, &method); + disconnect(checkBox, SIGNAL(stateChanged(int)), this, SLOT(slot_checkedClicked())); + checkBox->setCheckState(value ? Qt::Checked : Qt::Unchecked); + connect(checkBox, SIGNAL(stateChanged(int)), this, SLOT(slot_checkedClicked())); + QMessageBox::information(this, tr("Prompt"), tr("The funtion is unsupported")); + return; + } + + if((method & SANE_INFO_RELOAD_OPTIONS) == SANE_INFO_RELOAD_OPTIONS) + updateUIStatus(); + else if(method & SANE_INFO_INEXACT) + checkBox->setCheckState(checkBoxcurrentState ? Qt::CheckState::Checked : Qt::CheckState::Unchecked); + + const SANE_Option_Descriptor* opt = nullptr; + for (int i = 0; i < m_list_getOpt.size(); i++) + { + if (m_list_getOpt.at(i).first == id) + { + opt = reinterpret_cast(m_list_getOpt.at(i).second); + break; + } + } + + if(strcmp(opt->name, SANE_STD_OPT_NAME_CUSTOM_AREA) == 0) + btn_cut_area_->setEnabled(checkBoxcurrentState); + else if (strcmp(opt->name, SANE_STD_OPT_NAME_IS_CUSTOM_GAMMA) == 0 && btn_gamma_ != nullptr) + btn_gamma_->setEnabled(checkBoxcurrentState); + //cur_scheme_->config_changed(id, (char*)&checkBoxcurrentState, sizeof(checkBoxcurrentState)); + + if (!m_isRefreshUi && checkBoxcurrentState && (0 == strcmp(opt->name, SANE_STD_OPT_NAME_RID_MORR) || 0 == strcmp(opt->name, SANE_STD_OPT_NAME_RID_GRID))) + { + QMessageBox::information(this, tr("Prompt"), tr("This function may cause a decrease in the speed of drawing.")); + } + + updateRestorePushButton(); +} + +void hg_settingdialog::slot_string_list_comboBoxClicked() +{ + QComboBox *comboBox = qobject_cast(sender()); + SANE_Int id = comboBox->property("controls_id").toInt(); + std::string comboBoxcurrentItem(comboBox->currentText().toUtf8()); + int type = comboBox->property(hg_settingdialog::property_combox_data_type_.c_str()).toInt(); + + if (id == m_dpiId) + { + m_dpiValue = atoi(comboBoxcurrentItem.c_str()); + qDebug("dpi=%d", m_dpiValue); + } + else if (id == m_paperSizeId) + { + m_paperSizeValue = comboBoxcurrentItem.c_str(); + qDebug("paperSize=%s", comboBoxcurrentItem.c_str()); + } + else if (id == m_colorModeId) + { + m_colorModeValue = comboBoxcurrentItem.c_str(); + qDebug("colorMode=%s", comboBoxcurrentItem.c_str()); + } + + const SANE_Option_Descriptor* opt = nullptr; + for (int i = 0; i < m_list_getOpt.size(); i++) + { + if (m_list_getOpt.at(i).first == id) + { + opt = reinterpret_cast(m_list_getOpt.at(i).second); + break; + } + } + + SANE_Int method = 0; + SANE_String buf = (SANE_String)malloc(opt->size * 2 + 4); + if(type == COMBO_VAL_INT) + *((SANE_Int*)buf) = atoi(comboBoxcurrentItem.c_str()); + else if(type == COMBO_VAL_FLOAT) + *((SANE_Fixed*)buf) = SANE_FIX(atof(comboBoxcurrentItem.c_str())); + else + strcpy(buf, comboBoxcurrentItem.c_str()); + SANE_Status ret = m_saneAPI.sane_control_option_api(m_devHandle, id, SANE_ACTION_SET_VALUE, buf, &method); + if (ret == SANE_STATUS_UNSUPPORTED) + { + char* value = (char*)malloc(opt->size * 2 + 4); + ret = m_saneAPI.sane_control_option_api(m_devHandle, id, SANE_ACTION_GET_VALUE, value, &method); + if (value != nullptr) + comboBox->setCurrentText(QString::fromStdString(value)); + free(value); + + QMessageBox::information(this, tr("Prompt"), tr("The funtion is unsupported")); + return; + } + + if((method & SANE_INFO_RELOAD_OPTIONS) == SANE_INFO_RELOAD_OPTIONS) + updateUIStatus(); + else if(method & SANE_INFO_INEXACT) + comboBox->setCurrentText(QString::fromStdString(buf)); + + //if(type == COMBO_VAL_INT) + // cur_scheme_->config_changed(id, buf, sizeof(SANE_Int)); + //else if(type == COMBO_VAL_FLOAT) + // cur_scheme_->config_changed(id, buf, sizeof(SANE_Fixed)); + //else + //{ + // std::string langCN(to_default_language(buf, nullptr)); + // cur_scheme_->config_changed(id, &langCN[0], langCN.length()); + //} + free(buf); + + if (0 == strcmp(opt->name, SANE_STD_OPT_NAME_TEXT_DIRECTION)) + { + if (comboBox->currentText() == OPTION_VALUE_WGFX_ZDWBFXSB) + { + if (!m_isRefreshUi) + QMessageBox::information(this, tr("Prompt"), tr("This function may cause a decrease in the speed of drawing.")); + } + } + + updateRestorePushButton(); +} + +void hg_settingdialog::slot_pushButtonClicked() +{ + QPushButton *pushButton = qobject_cast(sender()); + SANE_Int id = pushButton->property("controls_id").toInt(), + after = 0; + + // restore to default setting ? + SANE_Status ret = m_saneAPI.sane_control_option_api(m_devHandle, id, SANE_ACTION_SET_VALUE, NULL, &after); + if (ret == SANE_STATUS_UNSUPPORTED) + { + QMessageBox::information(this, tr("Prompt"), tr("The funtion is unsupported")); + return; + } + + if((after & SANE_INFO_RELOAD_OPTIONS) == SANE_INFO_RELOAD_OPTIONS) + updateUIStatus(); + + const SANE_Option_Descriptor* opt = m_saneAPI.sane_get_option_descriptor_api(m_devHandle, id); + if(opt && strcmp(opt->name, SANE_STD_OPT_NAME_RESTORE) == 0) + { + restore_2_default_settings(); + } +} + +void hg_settingdialog::slot_cutButtonClicked() +{ + //int width = 0.03937 * m_cutWidth * m_dpiValue; + //int height = 0.03937 * m_cutHeight * m_dpiValue; + qreal left = 0.03937 * m_cutLeftValue * m_dpiValue; + qreal top = 0.03937 * m_cutTopValue * m_dpiValue; + qreal right = 0.03937 * m_cutRightValue * m_dpiValue; + qreal bottom = 0.03937 * m_cutBottomValue * m_dpiValue; + + CutPaperTool dlg(m_dpiValue, m_paperSizeValue, 300, 0, this); + QRectF rc(left, top, right - left, bottom - top); + dlg.setCutRectPixel(rc); + if (dlg.exec()) + { + QRectF rcRet = dlg.getCutRectPixel(); + + m_cutLeftValue = rcRet.left() / (0.03937 * m_dpiValue); + m_cutTopValue = rcRet.top() / (0.03937 * m_dpiValue); + m_cutRightValue = rcRet.right() / (0.03937 * m_dpiValue); + m_cutBottomValue = rcRet.bottom() / (0.03937 * m_dpiValue); + + SANE_Int info; + SANE_Word value = SANE_FIX(m_cutLeftValue); + m_saneAPI.sane_control_option_api(m_devHandle, m_cutLeftId, SANE_ACTION_SET_VALUE, &value, &info); + //cur_scheme_->config_changed(m_cutLeftId, (char*)&value, sizeof(value)); + value = SANE_FIX(m_cutTopValue); + m_saneAPI.sane_control_option_api(m_devHandle, m_cutTopId, SANE_ACTION_SET_VALUE, &value, &info); + //cur_scheme_->config_changed(m_cutTopId, (char*)&value, sizeof(value)); + value = SANE_FIX(m_cutRightValue); + m_saneAPI.sane_control_option_api(m_devHandle, m_cutRightId, SANE_ACTION_SET_VALUE, &value, &info); + //cur_scheme_->config_changed(m_cutRightId, (char*)&value, sizeof(value)); + value = SANE_FIX(m_cutBottomValue); + m_saneAPI.sane_control_option_api(m_devHandle, m_cutBottomId, SANE_ACTION_SET_VALUE, &value, &info); + //cur_scheme_->config_changed(m_cutBottomId, (char*)&value, sizeof(value)); + + updateRestorePushButton(); + } +} + +void hg_settingdialog::slot_gammaButtonClicked() +{ + setPicClrTool dlg(this); + + int colorMode; // 0-彩色, 1-灰度 + if (m_colorModeValue.toStdString() == OPTION_VALUE_YSMS_256JHD + || m_colorModeValue.toStdString() == OPTION_VALUE_YSMS_HB) + { + colorMode = 1; + + QList keyTable; + for (int i = 0; i < m_gammaData.pt_count; ++i) + { + QPoint pt(m_gammaData.keypoint[i].x, m_gammaData.keypoint[i].y); + keyTable.append(pt); + } + + if (!keyTable.empty()) + { + dlg.setGrayKeyTable(keyTable); + } + } + else + { + colorMode = 0; + + QList keyTable; + for (int i = 0; i < m_gammaData.pt_count; ++i) + { + QPoint pt(m_gammaData.keypoint[i].x, m_gammaData.keypoint[i].y); + keyTable.append(pt); + } + + QList rKeyTable; + for (int i = 0; i < m_gammaData.pt_count_r; ++i) + { + QPoint pt(m_gammaData.keypoint_r[i].x, m_gammaData.keypoint_r[i].y); + rKeyTable.append(pt); + } + + QList gKeyTable; + for (int i = 0; i < m_gammaData.pt_count_g; ++i) + { + QPoint pt(m_gammaData.keypoint_g[i].x, m_gammaData.keypoint_g[i].y); + gKeyTable.append(pt); + } + + QList bKeyTable; + for (int i = 0; i < m_gammaData.pt_count_b; ++i) + { + QPoint pt(m_gammaData.keypoint_b[i].x, m_gammaData.keypoint_b[i].y); + bKeyTable.append(pt); + } + + QVector> keyTableList; + if (!keyTable.empty() && !rKeyTable.empty() && !gKeyTable.empty() && !bKeyTable.empty()) + { + keyTableList.append(keyTable); + keyTableList.append(rKeyTable); + keyTableList.append(gKeyTable); + keyTableList.append(bKeyTable); + dlg.setRGBKeyTable(keyTableList); + } + } + + int rgbTypeIndex = 0; + rgbTypeIndex = m_gammaData.app_data & 0x0F; + int colorTypeIndex = 0; + colorTypeIndex = (m_gammaData.app_data & 0xF0) >> 4; + + dlg.setRgbAndColorType(rgbTypeIndex, colorTypeIndex); + + dlg.setColorMode(colorMode); + if (dlg.exec()) + { + memset(&m_gammaData, 0, sizeof(m_gammaData)); + clicked_gamma_ = true; + + if (1 == colorMode) + { + QList keyTable = dlg.getGrayKeyTable(); + + m_gammaData.pt_count = HGMIN(4, keyTable.size()); + int i = 0; + for (QPoint pt : keyTable) + { + if (i >= 4) + break; + + m_gammaData.keypoint[i].x = pt.x(); + m_gammaData.keypoint[i].y = pt.y(); + ++i; + } + + uchar data[256]; + dlg.getGrayTable(data, 256); + for (int i = 0; i < 256; ++i) + { + m_gammaData.table[i] = data[i]; + } + } + else + { + QVector> keyTableList = dlg.getRGBKeyTable(); + + m_gammaData.pt_count = HGMIN(4, keyTableList[0].size()); + int i = 0; + for (QPoint pt : keyTableList[0]) + { + if (i >= 4) + break; + + m_gammaData.keypoint[i].x = pt.x(); + m_gammaData.keypoint[i].y = pt.y(); + ++i; + } + + m_gammaData.pt_count_r = HGMIN(4, keyTableList[1].size()); + i = 0; + for (QPoint pt : keyTableList[1]) + { + if (i >= 4) + break; + + m_gammaData.keypoint_r[i].x = pt.x(); + m_gammaData.keypoint_r[i].y = pt.y(); + ++i; + } + + m_gammaData.pt_count_g = HGMIN(4, keyTableList[2].size()); + i = 0; + for (QPoint pt : keyTableList[2]) + { + if (i >= 4) + break; + + m_gammaData.keypoint_g[i].x = pt.x(); + m_gammaData.keypoint_g[i].y = pt.y(); + ++i; + } + + m_gammaData.pt_count_b = HGMIN(4, keyTableList[3].size()); + i = 0; + for (QPoint pt : keyTableList[3]) + { + if (i >= 4) + break; + + m_gammaData.keypoint_b[i].x = pt.x(); + m_gammaData.keypoint_b[i].y = pt.y(); + ++i; + } + + uchar data[256 * 3]; + dlg.getRGBTable(data, 256 * 3); + for (int i = 0; i < 256; ++i) + { + m_gammaData.table[i] = data[i * 3 + 2]; + m_gammaData.table[i + 256] = data[i * 3 + 1]; + m_gammaData.table[i + 512] = data[i * 3 + 0]; + } + } + + QVector type = dlg.getRgbAndColorType(); + m_gammaData.app_data = type[0] | (type[1] << 4); + + dev_que::set_custom_gamma(&m_saneAPI, m_devHandle, &m_gammaData); + + updateRestorePushButton(); + } +} + +void hg_settingdialog::slot_word_list_comboBoxClicked(int value) +{ + QComboBox *comboBox = qobject_cast(sender()); + SANE_Int id = comboBox->property("controls_id").toInt(); + SANE_Int temp = value; + + const SANE_Option_Descriptor* opt = nullptr; + for (int i = 0; i < m_list_getOpt.size(); i++) + { + if (m_list_getOpt.at(i).first == id) + { + opt = reinterpret_cast(m_list_getOpt.at(i).second); + break; + } + } + +// m_list_IdValueTitle.append(QPair, QString>(QPair(id, temp), md5(opt->title))); + + SANE_Int method = 0; + SANE_Status ret = m_saneAPI.sane_control_option_api(m_devHandle, id, SANE_ACTION_SET_VALUE, &temp, &method); + if (ret == SANE_STATUS_UNSUPPORTED) + { + char* value = (char*)malloc(opt->size * 2 + 4); + ret = m_saneAPI.sane_control_option_api(m_devHandle, id, SANE_ACTION_GET_VALUE, value, &method); + if (value != nullptr) + comboBox->setCurrentText(QString::fromStdString(value)); + free(value); + + QMessageBox::information(this, tr("Prompt"), tr("The funtion is unsupported")); + return; + } + + if((method & SANE_INFO_RELOAD_OPTIONS) == SANE_INFO_RELOAD_OPTIONS) + updateUIStatus(); + else if(method & SANE_INFO_INEXACT) + { + char buf[20]; + sprintf(buf, "%d", temp); + comboBox->setCurrentText(QString::fromStdString(buf)); + } + //cur_scheme_->config_changed(id, (char*)&temp, sizeof(temp)); + updateRestorePushButton(); +} + +void hg_settingdialog::slot_sliderClicked(int value) +{ + QSlider *slider = qobject_cast(sender()); + SANE_Int id = slider->property("controls_id").toInt(); + + if (id == m_dpiId) + { + m_dpiValue = value; + qDebug("dpi=%d", m_dpiValue); + } + + QAbstractSpinBox* spin = nullptr; + for(int i = 0; i < m_list_sliderSpinbox.size(); i++) + if (m_list_sliderSpinbox.at(i).first == slider) + { + spin = reinterpret_cast(m_list_sliderSpinbox.at(i).second); + break; + } + + const SANE_Option_Descriptor* opt = nullptr; + for (int i = 0; i < m_list_getOpt.size(); i++) + { + if (m_list_getOpt.at(i).first == id) + { + opt = reinterpret_cast(m_list_getOpt.at(i).second); + break; + } + } + + if (spin != nullptr) + { + SANE_Int val = value, method = 0; + bool db_val = false; + if (typeid(*spin) == typeid(QSpinBox)) + { + QSpinBox* spin_ = reinterpret_cast(spin); + spin_->setValue(value); + } + else + { + QDoubleSpinBox* spin_ = reinterpret_cast(spin); + double temp = value * spin_->singleStep(); + if(temp != spin_->value()) + spin_->setValue(temp); + + val = SANE_FIX(temp); + db_val = true; + } + + SANE_Status ret = m_saneAPI.sane_control_option_api(m_devHandle, id, SANE_ACTION_SET_VALUE, &val, &method); + if (ret == SANE_STATUS_UNSUPPORTED) + { + if (!db_val) + { + SANE_Int value = 0; + ret = m_saneAPI.sane_control_option_api(m_devHandle, id, SANE_ACTION_GET_VALUE, &value, &method); + slider->setValue(value); + } + else + { + SANE_Fixed value = 0; + ret = m_saneAPI.sane_control_option_api(m_devHandle, id, SANE_ACTION_GET_VALUE, &value, &method); + slider->setValue(value); + } + + QMessageBox::information(this, tr("Prompt"), tr("The funtion is unsupported")); + return; + } + + if((method & SANE_INFO_RELOAD_OPTIONS) == SANE_INFO_RELOAD_OPTIONS) + updateUIStatus(); + else if(method & SANE_INFO_INEXACT) + { + if(db_val) + { + QDoubleSpinBox* spin_ = reinterpret_cast(spin); + double v = SANE_UNFIX(val); + spin_->setValue(v); + slider->setValue(spin_->value() / spin_->singleStep()); + } + else + { + QSpinBox* spin_ = reinterpret_cast(spin); + spin_->setValue(val); + slider->setValue(spin_->value() / spin_->singleStep()); + } + } + //cur_scheme_->config_changed(id, (char*)&val, sizeof(val)); + + updateRestorePushButton(); + } +} + +void hg_settingdialog::slot_doubleSpinboxClicked(double value) +{ + QDoubleSpinBox* spinBox = qobject_cast(sender()); + QAbstractSlider* slider = nullptr; + int id = spinBox->property("controls_id").toInt(); + for (int i = 0; i < m_list_sliderSpinbox.size(); i++) + { + if (m_list_sliderSpinbox.at(i).second == spinBox) + { + slider = reinterpret_cast(m_list_sliderSpinbox.at(i).first); + break; + } + } + + if(slider != nullptr) + { + int temp = static_cast(value / spinBox->singleStep() + 0.5); + QSlider* slider_ = reinterpret_cast(slider); + if (slider_->value() != temp) + slider_->setValue(temp); + } +} + +void hg_settingdialog::slot_spinBoxClicked(int value) +{ + QSpinBox* spinBox = qobject_cast(sender()); + int id = spinBox->property("controls_id").toInt(); + + const SANE_Option_Descriptor* opt = nullptr; + for (int i = 0; i < m_list_getOpt.size(); i++) + { + if (m_list_getOpt.at(i).first == id) + { + opt = reinterpret_cast(m_list_getOpt.at(i).second); + break; + } + } + + QAbstractSlider* slider = nullptr; + for (int i = 0; i < m_list_sliderSpinbox.size(); i++) + { + if (m_list_sliderSpinbox.at(i).second == spinBox) + { + slider = reinterpret_cast(m_list_sliderSpinbox.at(i).first); + break; + } + } + + if(slider == nullptr) + { + SANE_Int temp = value; + + //m_list_IdValueTitle.append(QPair, QString>(QPair(id, temp), md5(opt->title))); + + SANE_Int method = 0; + SANE_Status ret = m_saneAPI.sane_control_option_api(m_devHandle, id, SANE_ACTION_SET_VALUE, &temp, &method); + if (ret == SANE_STATUS_UNSUPPORTED) + { + QMessageBox::information(this, tr("Prompt"), tr("The funtion is unsupported")); + return; + } + if((method & SANE_INFO_RELOAD_OPTIONS) == SANE_INFO_RELOAD_OPTIONS) + updateUIStatus(); + else if(value != temp) + { + disconnect(spinBox, SIGNAL(valueChanged(int)), this, SLOT(slot_spinBoxClicked(int))); + spinBox->setValue(temp); + connect(spinBox, SIGNAL(valueChanged(int)), this, SLOT(slot_spinBoxClicked(int))); + } + //cur_scheme_->config_changed(id, (char*)&temp, sizeof(temp)); + + updateRestorePushButton(); + } + else + { + QSlider* slider_ = reinterpret_cast(slider); + slider_->setValue(spinBox->value()); + } +} + +void hg_settingdialog::slot_lineEditInput() +{ + QLineEdit* lineEdit = qobject_cast(sender()); + int id = lineEdit->property("controls_id").toInt(); + std::string lineEditCurrentText(lineEdit->text().toUtf8()); + + const SANE_Option_Descriptor* opt = nullptr; + for (int i = 0; i < m_list_getOpt.size(); i++) + { + if (m_list_getOpt.at(i).first == id) + { + opt = reinterpret_cast(m_list_getOpt.at(i).second); + break; + } + } + + //m_list_IdValueTitle.append(QPair, QString>(QPair(id, &lineEditCurrentText.at(0)), md5(opt->title))); + + SANE_Int method = 0; + void *buf = NULL; + SANE_Int nv = 0; + if(opt->type == SANE_TYPE_INT) + { + nv = atoi(lineEditCurrentText.c_str()); + buf = &nv; + } + else if(opt->type == SANE_TYPE_FIXED) + { + nv = SANE_FIX(atof(lineEditCurrentText.c_str())); + buf = &nv; + } + else + { + buf = malloc(opt->size * 2 + 4); + strcpy((char*)buf, lineEditCurrentText.c_str()); + } + SANE_Status ret = m_saneAPI.sane_control_option_api(m_devHandle, id, SANE_ACTION_SET_VALUE, buf, &method); + if (ret == SANE_STATUS_UNSUPPORTED) + { + QMessageBox::information(this, tr("Prompt"), tr("The funtion is unsupported")); + return; + } + if((method & SANE_INFO_RELOAD_OPTIONS) == SANE_INFO_RELOAD_OPTIONS) + updateUIStatus(); + else if(method & SANE_INFO_INEXACT) + { + char mem[20], *v = mem; + if(opt->type == SANE_TYPE_INT) + sprintf(mem, "%d", nv); + else if(opt->type == SANE_TYPE_FIXED) + sprintf(mem, "%f", SANE_UNFIX(nv)); + else + v = (char*)buf; + lineEdit->setText(QString::fromStdString(v)); + } + + const SANE_Option_Descriptor* opt2 = nullptr; + for (int i = 0; i < m_list_getOpt.size(); i++) + { + if (m_list_getOpt.at(i).first == id) + { + opt2 = reinterpret_cast(m_list_getOpt.at(i).second); + break; + } + } + + if(opt2->type == SANE_TYPE_INT || opt2->type == SANE_TYPE_FIXED) + { + //cur_scheme_->config_changed(id, (char*)buf, sizeof(SANE_Int)); + + updateRestorePushButton(); + } + else + { + std::string langCN(to_default_language((char*)buf, nullptr)); + //cur_scheme_->config_changed(id, &langCN[0], langCN.length()); + + updateRestorePushButton(); + } + if (buf != nullptr) + free(buf); +} +void hg_settingdialog::slot_buttonAboutClicked() +{ + char info[256] = { 0 }; + SANE_Int data = 0; + SANE_Status ret = SANE_STATUS_GOOD; + QString content; + QString title = tr("about ") + QString::fromStdString(m_devName); + + ret = m_saneAPI.sane_control_option_api(m_devHandle, (SANE_Int)0x8855, SANE_ACTION_GET_VALUE, info, NULL); + if (ret != SANE_STATUS_GOOD) + { + QString str = tr("Not supported"); + strcpy(info, str.toStdString().c_str()); + } + content += tr("

Device model: %1

").arg(QString(info)); + info[0] = 0; + ret = m_saneAPI.sane_control_option_api(m_devHandle, (SANE_Int)0x884A, SANE_ACTION_GET_VALUE, info, NULL); + if (ret != SANE_STATUS_GOOD) + { + QString str = tr("Not supported"); + strcpy(info, str.toStdString().c_str()); + } + content += tr("

Driver version: %1

").arg(QString(info)); + info[0] = 0; + ret = m_saneAPI.sane_control_option_api(m_devHandle, (SANE_Int)0x8857, SANE_ACTION_GET_VALUE, info, NULL); + if (ret != SANE_STATUS_GOOD) + { + QString str = tr("Not supported"); + strcpy(info, str.toStdString().c_str()); + } + content += tr("

Firmware number: %1

").arg(QString(info)); + info[0] = 0; + ret = m_saneAPI.sane_control_option_api(m_devHandle, (SANE_Int)0x8856, SANE_ACTION_GET_VALUE, info, NULL); + if (ret != SANE_STATUS_GOOD) + { + QString str = tr("Not supported"); + strcpy(info, str.toStdString().c_str()); + } + content += tr("

Serial number: %1

").arg(QString(info)); + info[0] = 0; + + ret = m_saneAPI.sane_control_option_api(m_devHandle, (SANE_Int)0x9902, SANE_ACTION_GET_VALUE, &data, NULL); + if (ret != SANE_STATUS_GOOD) + { + QString str = tr("Not supported"); + strcpy(info, str.toStdString().c_str()); + content += tr("

Roller count: %1

").arg(QString(info)); + info[0] = 0; + } + else + { + content += tr("

Roller count: %1

").arg(QString::number(data)); + } + + ret = m_saneAPI.sane_control_option_api(m_devHandle, (SANE_Int)0x8849, SANE_ACTION_GET_VALUE, &data, NULL); + if (ret != SANE_STATUS_GOOD) + { + QString str = tr("Not supported"); + strcpy(info, str.toStdString().c_str()); + content += tr("

Roller count: %1

").arg(QString(info)); + info[0] = 0; + } + else + { + content += tr("

History count: %1

").arg(QString::number(data)); + } + + QMessageBox msg(QMessageBox::NoIcon, title, + content, QMessageBox::Ok, this); + msg.setStyleSheet("QLabel{""min-width: 250px;""}"); + msg.exec(); +} + +void hg_settingdialog::slot_buttonOkClicked() +{ + m_closeButton = closeButtonOk; + close(); +} + +void hg_settingdialog::slot_buttonCancelClicked() +{ + m_closeButton = closeButtonCancel; + close(); +} + +void hg_settingdialog::keyPressEvent(QKeyEvent *e) +{ + if (e->key() == Qt::Key_Escape) { + e->ignore(); + } + else { + QDialog::keyPressEvent(e); + } +} + +int hg_settingdialog::get_changed_items(void) +{ + return changed_count_; +} + +int hg_settingdialog::getCloseButtonCliked() +{ + return m_closeButton; +} + +gb::sane_config_schm *hg_settingdialog::getCurScheme() +{ + return cur_scheme_; +} + +//生成UTF-8编码的MD5值 +QString hg_settingdialog::md5(QString key) +{ + QCryptographicHash md5(QCryptographicHash::Md5); + md5.addData(key.toUtf8()); + return QString(md5.result().toHex()); +} + +const void* hg_settingdialog::find_option_description(int id) +{ + for(int i = 0; i < m_list_getOpt.size(); i++) + { + if (m_list_getOpt.at(i).first == id) + return reinterpret_cast(m_list_getOpt.at(i).second); + } + + return nullptr; +} + +const void* hg_settingdialog::find_option_description(const std::string& title, int* id) +{ + for(int i = 0; i < m_list_getOpt.size(); i++) + { + std::string t((reinterpret_cast(m_list_getOpt.at(i).second))->name); + + if (title == t) + { + if(id) + *id = m_list_getOpt.at(i).first; + return reinterpret_cast(m_list_getOpt.at(i).second); + } + } + + return nullptr; +} + +void hg_settingdialog::closeEvent(QCloseEvent* e) +{ + //if(e->type() == QEvent::Close) // consider as cancel ... + //{ + // //if(save_) + // // save_scheme(); + // //else + // // cancel_setting(); + //} + + // 配置方案策略更改后的业务保存逻辑: + // + // 1、用户模板方案,显示的点击buttonSave时才保存; + // 2、默认方案,点击确定按钮后在此处保存;非默认方案保存用户的选择 + // 3、点击取消按钮时,恢复至该窗口初始化时候的状态 + // + // 代码逻辑: + // 1、属性值改变时,不再同时同步到配置方案中,而是点击确定后再一起保存 + // 2、用户切换方案时,如果当前值与方案不匹配,则将改变同步到默认方案(暂时关闭该功能) + // 3、点击确定按钮后,如果当前方案非用户模板,则保存进默认配置;如果为用户模板,则更新当前方案索引 + // 4、点出取消按钮,恢复驱动值至该对话框初始化状态,不对配置作变更 + // + int index = comb_->currentIndex(); + if (m_closeButton == closeButtonOk) + { + if (cur_scheme_) + cur_scheme_->release(); + cur_scheme_ = cur_cfg_->get_scheme(comb_->currentText().toStdString().c_str()); + + if (comb_->currentIndex() == 0) + { + cur_scheme_ = cur_cfg_->get_scheme(gb::scanner_cfg::user_default_scheme_name().c_str()); + updateSchemeFromUi(cur_scheme_); + } + cur_cfg_->select_scheme(cur_scheme_->get_scheme_name().c_str()); + cur_cfg_->save(); + } + else + { + // 配置方案内容不再随时变动 + cancelScheme(); + } + + e->accept(); +} +void hg_settingdialog::save_to_default_if_ui_not_equal_scheme(gb::sane_config_schm* cur, bool save) +{ + int index = 1; // comb_->currentIndex(); + if (index) + { + gb::sane_config_schm* schm = new gb::sane_config_schm(); + updateSchemeFromUi(schm); + if ((cur && !schm->is_equal(cur)) || + (!cur && schm->has_changed())) + { + index = 0; + } + schm->release(); + } + if (index == 0 || !cur) + { + cur = cur_cfg_->get_scheme(gb::scanner_cfg::user_default_scheme_name().c_str()); + updateSchemeFromUi(cur); + } + if (save) + { + cur_cfg_->select_scheme(cur->get_scheme_name().c_str()); + cur_cfg_->save(); + } +} + +bool hg_settingdialog::eventFilter(QObject *target, QEvent *event) +{ + if (typeid(*target) == typeid(QSlider)) + { + if (event->type() == QEvent::Wheel) + { + return true; + } + } + else if (typeid(*target) == typeid(QComboBox)) + { + if (event->type() == QEvent::MouseButtonPress) + { + m_isRefreshUi = false; + } + } + + return QDialog::eventFilter(target, event); +} + +bool hg_settingdialog::createMsgBoxUi(bool add, std::string &name) +{ + QString text(tr("Please select to overwrite the original configuration:")); + text += QString::fromStdString(name); + text += tr(",or add a new configuration"); + + QDialog *dlg = new QDialog(this); + dlg->setWindowTitle(tr("save the configuration")); + QLabel *label_question = new QLabel; + label_question->setText(text); + + QRadioButton *radioButtonCover = new QRadioButton; + radioButtonCover->setText(tr("cover original configuration:") + QString::fromStdString(name)); + radioButtonCover->setChecked(true); + add = false; + QRadioButton *radioButtonNew = new QRadioButton; + radioButtonNew->setText(tr("add new configuration")); + + QHBoxLayout *hLayoutName = new QHBoxLayout; + QLabel *label_name = new QLabel; + label_name->setText(tr("rename:")); + m_lineEdit_name = new QLineEdit; + std::string name2; + m_lineEdit_name->setText(QString::fromStdString(getCurUiShemeName(name2))); + + QSpacerItem *spacer1 = new QSpacerItem(20, 20, QSizePolicy::Expanding); + hLayoutName->addWidget(label_name); + hLayoutName->addWidget(m_lineEdit_name); + hLayoutName->addSpacerItem(spacer1); + label_name->setVisible(false); + m_lineEdit_name->setVisible(false); + + bool cover = true; + connect(radioButtonCover, &QRadioButton::clicked, this, [=, &add, &cover](){ + cover = true; + add = false; + label_name->setVisible(false); + m_lineEdit_name->setVisible(false); + }); + connect(radioButtonNew, &QRadioButton::clicked, this, [=, &add, &cover](){ + cover = false; + add = true; + label_name->setVisible(true); + m_lineEdit_name->setVisible(true); + + m_lineEdit_name->setFocus(); + QTimer::singleShot(0, m_lineEdit_name, &QLineEdit::selectAll); + }); + + QSpacerItem *spacer2 = new QSpacerItem(20, 20, QSizePolicy::Expanding); + QPushButton *pbtnOk = new QPushButton; + pbtnOk->setText(tr("ok")); + connect(pbtnOk, &QPushButton::clicked, this, [=, &name, &cover](){ + + QString text = m_lineEdit_name->text(); + static QRegularExpression re("\\s"); + text.remove(re);//Remove space + + name = text.toStdString(); + + if(name.empty()) + { + QMessageBox::information(this, tr("tips"), tr("scheme name cannot be empty")); + m_lineEdit_name->setText(QString::fromStdString(getCurUiShemeName(name))); + return; + } + + if (!cover) + { + std::vector now; + cur_cfg_->get_all_schemes(now); + for(auto& v: now) + { + if(v == name) + { + + QMessageBox::information(this, tr("tips"), tr("scheme name: ") + QString::fromStdString(name) + tr(" already exists")); + m_lineEdit_name->setText(QString::fromStdString(getCurUiShemeName(name))); + return; + } + } + } + + dlg->close(); + }); + + QHBoxLayout *hLayout_pbtnOk = new QHBoxLayout; + hLayout_pbtnOk->addSpacerItem(spacer2); + hLayout_pbtnOk->addWidget(pbtnOk); + + QVBoxLayout *vLayout = new QVBoxLayout; + vLayout->addWidget(label_question); + vLayout->addWidget(radioButtonCover); + vLayout->addWidget(radioButtonNew); + vLayout->addLayout(hLayoutName); + vLayout->addLayout(hLayout_pbtnOk); + dlg->setLayout(vLayout); + + dlg->exec(); + + return add; +} + +std::string hg_settingdialog::getCurUiShemeName(std::string name) +{ + std::string k(""), val(""); + int id = 0; + const SANE_Option_Descriptor* opt = nullptr; + + if (cur_scheme_->first_config(k, val)) + { + int count = 0; + do + { + id = cur_scheme_->id_from_name(k.c_str()); + opt = id == -1 ? nullptr : m_saneAPI.sane_get_option_descriptor_api(m_devHandle, id); + if (opt) + { + if (count++) + name += " + "; + + if (opt->type == SANE_TYPE_STRING) + name += from_default_language(val.c_str(), nullptr); + else + { + name += opt->title; + if (opt->type == SANE_TYPE_BOOL) + { + name += std::string("("); + if (*(SANE_Bool*)&val[0] == SANE_TRUE) + name += "true)"; + else + name += "false)"; + } + else if (opt->type == SANE_TYPE_INT) + { + char buf[128] = { 0 }; + sprintf(buf, "(%d)", *(int*)&val[0]); + name += buf; + } + else if (opt->type == SANE_TYPE_FIXED) + { + char buf[128] = { 0 }; + sprintf(buf, "(%.4f)", SANE_UNFIX(*(SANE_Fixed*)&val[0])); + name += buf; + } + } + } + } while (count < 3 && cur_scheme_->next_config(k, val)); + } + return name; +} + +void hg_settingdialog::save_scheme(void) +{ + if (comb_->currentIndex() == 0) + return; + + this->cur_scheme_ = this->cur_cfg_->get_scheme(comb_->currentText().toStdString().c_str()); + cur_scheme_->end_setting(true); + cur_scheme_->begin_setting(); + updateSchemeFromUi(); + cur_scheme_->end_setting(false); + cur_cfg_->save(); + cur_cfg_->select_scheme(cur_scheme_->get_scheme_name().c_str()); +} +void hg_settingdialog::cancel_setting(void) +{ + // restore changed value ... + cur_scheme_->end_setting(true); + //dev_que::apply_scheme(&m_saneAPI, m_devHandle, cur_scheme_); +} + +std::string hg_settingdialog::getAppVersion() +{ + char v[256] = {0}; + m_saneAPI.sane_control_option_api(m_devHandle, (SANE_Int)0x884A, SANE_ACTION_GET_VALUE, v, NULL); + return v; +} +void hg_settingdialog::apply_current_scheme(void) +{ + dev_que::apply_scheme(&m_saneAPI, m_devHandle, cur_scheme_); +} + +void hg_settingdialog::setIcon() +{ + this->setWindowIcon(QIcon(":logo/image_rsc/logo/logo.ico")); +#if defined(OEM_HANWANG) + this->setWindowIcon(QIcon(":logo/image_rsc/logo/Hanvon_logo1.ico")); +#elif defined(OEM_LISICHENG) + this->setWindowIcon(QIcon(":logo/image_rsc/logo/Lanxum_logo.ico")); +#elif defined(OEM_CANGTIAN) + this->setWindowIcon(QIcon(":logo/image_rsc/logo/Cumtenn_logo.ico")); +#elif defined(OEM_ZHONGJING) + this->setWindowIcon(QIcon(":logo/image_rsc/logo/Microtek_logo.ico")); +#elif defined(OEM_ZIGUANG) + this->setWindowIcon(QIcon(":logo/image_rsc/logo/uniscan.ico")); +#endif +} + +void hg_settingdialog::cancelScheme() +{ + SANE_Int none = 0; + for (int i = 0; i < m_list_originDeviceScheme.size(); i++) + { + const SANE_Option_Descriptor* opt = reinterpret_cast(m_list_originDeviceScheme.at(i).first); + if (!opt || opt->type == SANE_TYPE_BUTTON || opt->type == SANE_TYPE_GROUP) + continue; + + if (opt->type == SANE_TYPE_BOOL) + { + SANE_Bool v = m_list_originDeviceScheme.at(i).second.toBool(); + m_saneAPI.sane_control_option_api(m_devHandle, i + 1, SANE_ACTION_SET_VALUE, &v, &none); + } + else if (opt->type == SANE_TYPE_INT) + { + SANE_Int v = m_list_originDeviceScheme.at(i).second.toInt();; + m_saneAPI.sane_control_option_api(m_devHandle, i + 1, SANE_ACTION_SET_VALUE, &v, &none); + } + else if (opt->type == SANE_TYPE_FIXED) + { + double v = SANE_UNFIX(m_list_originDeviceScheme.at(i).second.toDouble()); + SANE_Int v2 = SANE_FIX(v); + + m_saneAPI.sane_control_option_api(m_devHandle, i + 1, SANE_ACTION_SET_VALUE, &v2, &none); + } + else + { + QString v = m_list_originDeviceScheme.at(i).second.toString(); + std::string v2 = v.toStdString(); + + m_saneAPI.sane_control_option_api(m_devHandle, i + 1, SANE_ACTION_SET_VALUE, (void*)v2.c_str(), &none); + } + } +} + +void hg_settingdialog::updateSchemeFromUi(gb::sane_config_schm* schm) +{ + SANE_Int dev_options = 0; + bool custom_area = false, custom_gamma = false; + m_saneAPI.sane_control_option_api(m_devHandle, 0, SANE_ACTION_GET_VALUE, &dev_options, nullptr); + + if (!schm) + schm = cur_scheme_; + schm->copy_default_value(def_value_); + for (int i = 1, j = dev_options; i < j; i++) + { + const SANE_Option_Descriptor* opt = m_saneAPI.sane_get_option_descriptor_api(m_devHandle, i); + SANE_Int method = 0; + if (opt == nullptr) + { + continue; + } + else + { + if (opt->type == SANE_TYPE_INT) + { + SANE_Int init = 0; + + m_saneAPI.sane_control_option_api(m_devHandle, i, SANE_ACTION_GET_VALUE, &init, &method); + schm->config_changed(i, (char*)&init, sizeof(init)); + } + else if (opt->type == SANE_TYPE_FIXED) + { + SANE_Fixed init = 0; + + m_saneAPI.sane_control_option_api(m_devHandle, i, SANE_ACTION_GET_VALUE, &init, &method); + schm->config_changed(i, (char*)&init, sizeof(init)); + } + else if (opt->type == SANE_TYPE_BOOL) + { + SANE_Bool init = 0; + m_saneAPI.sane_control_option_api(m_devHandle, i, SANE_ACTION_GET_VALUE, &init, &method); + schm->config_changed(i, (char*)&init, sizeof(init)); + + + if (std::string(opt->name) == SANE_STD_OPT_NAME_CUSTOM_AREA) + { + custom_area = init == SANE_TRUE; + } + else if (std::string(opt->name) == SANE_STD_OPT_NAME_IS_CUSTOM_GAMMA) + { + custom_gamma = init == SANE_TRUE; + } + } + else if (opt->type == SANE_TYPE_STRING) + { + char* init = (char*)malloc(opt->size * 2 + 4); + + m_saneAPI.sane_control_option_api(m_devHandle, i, SANE_ACTION_GET_VALUE, init, &method); + std::string value = QString(init).toStdString(); + schm->config_changed(i, value.c_str(), value.size()); + free(init); + } + } + } + + if (!custom_area) + { + schm->remove_config(SANE_STD_OPT_NAME_CUSTOM_AREA_LEFT); + schm->remove_config(SANE_STD_OPT_NAME_CUSTOM_AREA_TOP); + schm->remove_config(SANE_STD_OPT_NAME_CUSTOM_AREA_RIGHT); + schm->remove_config(SANE_STD_OPT_NAME_CUSTOM_AREA_BOTTOM); + } + if (custom_gamma) + { + SANE_Gamma gamma; + + m_saneAPI.sane_control_option_api(m_devHandle, SANE_OPT_ID_CUSTOM_GAMMA, SANE_ACTION_GET_VALUE, &gamma, nullptr); + schm->config_changed(SANE_STD_OPT_NAME_IS_CUSTOM_GAMMA, (char*)&gamma, sizeof(gamma), true); + } +} + +void hg_settingdialog::updateSchemeManagerUi() +{ + m_deleteCur->setEnabled(true); + m_deleteAll->setEnabled(true); + m_pbtn_Save->setEnabled(true); + if (comb_->currentIndex() == 0) + { + m_deleteCur->setEnabled(false); + m_pbtn_Save->setEnabled(false); + } + if (comb_->count() == 1) + { + m_deleteAll->setEnabled(false); + } +} + +void hg_settingdialog::updateRestorePushButton() +{ + gb::sane_config_schm* cfgScheme = new gb::sane_config_schm(); + cfgScheme = cur_cfg_->get_scheme(comb_->currentText().toStdString().c_str()); + + gb::sane_config_schm* schm = new gb::sane_config_schm(); + updateSchemeFromUi(schm); + bool restore = false; + if ((cfgScheme && !schm->is_equal(cfgScheme))) + { + restore = true; + } + + QString prompt = QString(tr("The current parameter settings are inconsistent with the configuration scheme '%1'. To use the configuration scheme '%1' parameters, please click the restore button")); + m_label_restore->setText(prompt.arg(comb_->currentText())); + m_label_restore->setVisible(restore); + m_pbtn_restore->setVisible(restore); +} + +std::string sane_val_to_string(const char* val, SANE_Value_Type type) +{ + char buf[128] = {0}; + std::string ret(""); + + switch(type) + { + case SANE_TYPE_BOOL: + ret = *(SANE_Bool*)val == SANE_TRUE ? "true" : "false"; + break; + case SANE_TYPE_INT: + sprintf(buf, "%d", *(int*)val); + ret = buf; + break; + case SANE_TYPE_FIXED: + sprintf(buf, "%.4f", SANE_UNFIX(*(SANE_Fixed*)val)); + ret = buf; + break; + default: + ret = val; + break; + } + + return ret; +} +void hg_settingdialog::on_current_scheme_changed() +{ + updateSchemeManagerUi(); + + //save_to_default_if_ui_not_equal_scheme(cur_scheme_, false); + if (cur_scheme_) + cur_scheme_->release(); + + cur_scheme_ = cur_cfg_->get_scheme(comb_->currentText().toStdString().c_str()); + hg_settingdialog::apply_scheme(m_devHandle, &m_saneAPI, cur_scheme_); + updateUIStatus(); + updateRestorePushButton(); + + //QString text(find_current_scheme_menu()); + //gb::sane_config_schm *cur = nullptr; + // + //cur_cfg_->select_scheme(text.toStdString().c_str()); + //cur = cur_cfg_->get_scheme(); + //if(!cur) + // cur = new gb::sane_config_schm(); + //cur->copy_default_value(cur_scheme_); + //cur_scheme_->end_setting(true); + //cur_scheme_->release(); + //cur_scheme_ = cur; + //cur_scheme_->begin_setting(); + // + //apply_current_scheme(); + //updateUIStatus(); + //changed_count_++; + // + //QString scheme(comb_->currentText()); + //bool enabled = false; + //gb::sane_config_schm *schm = cur_cfg_->get_scheme(scheme.toStdString().c_str()); + // + //if(schm) + // enabled = true; + // + //memset(&m_gammaData, 0, sizeof(m_gammaData)); + //for(int i = 0; i < sizeof(m_gammaData.table) / sizeof(m_gammaData.table[0]); ++i) + // m_gammaData.table[i] = i & 0x0ff; + // + //QString info(tr("")); + //std::string name(""), val(""); + //if(schm && schm->first_config(name, val)) + //{ + // do + // { + // QString title; + // SANE_Value_Type type = SANE_TYPE_STRING; + // for (int ii = 0; ii < m_list_defaultOptions.size(); ii++) + // { + // const SANE_Option_Descriptor* opt = reinterpret_cast(m_list_defaultOptions.at(ii).first); + // if(strcmp(opt->name, name.c_str()) == 0) + // { + // title = QString::fromStdString(opt->title); + // type = opt->type; + // if(type == SANE_TYPE_STRING) + // val = from_default_language(val.c_str(), nullptr); + // break; + // } + // } + // + // if(title.length()) + // { + // info += tr("
") + title + tr(":
"); + // info += tr("

") + QString::fromStdString(sane_val_to_string(val.c_str(), type)) + tr("

"); + // } + // else { + // if(val.length() == sizeof(SANE_Gamma)) + // memcpy(&m_gammaData, val.c_str(), sizeof(SANE_Gamma)); + // } + // }while(schm->next_config(name, val)); + //} + //if(schm) + // schm->release(); + // + //sketch_->setHtml(info); +} +void hg_settingdialog::slot_pushButton_scheme_management(void) +{ + QPushButton* btn = qobject_cast(sender()); + + if(btn == m_pbtn_addNew) + { + int id = 0; + QString text(find_current_scheme_menu(&id)); + int index = comb_->count(); + if(!text.isEmpty() && id >= 0) + { + Dialog_Input dlg(this); + + dlg.setEditText(text); + dlg.setWindowTitle(tr("Add new scheme")); + if(dlg.exec()) + { + QString newCfgName = dlg.getText(); + for (int i = 0; i < comb_->count(); ++i) + { + if (newCfgName == comb_->itemText(i)) + { + QMessageBox::information(this, tr("tips"), tr("The configuration scheme already exists")); + return; + } + } + + disconnect(comb_, SIGNAL(currentTextChanged(const QString)), this, SLOT(on_current_scheme_changed())); + comb_->insertItem(index, newCfgName); + connect(comb_, SIGNAL(currentTextChanged(const QString)), this, SLOT(on_current_scheme_changed())); + + gb::sane_config_schm* scheme = new gb::sane_config_schm(); + if (cur_scheme_ != nullptr) + { + cur_scheme_->end_setting(true); + cur_scheme_->release(); + } + + scheme->set_scheme_name(newCfgName.toStdString().c_str()); + cur_scheme_ = scheme; + + updateSchemeFromUi(); + + cur_cfg_->add_scheme(cur_scheme_, newCfgName.toStdString().c_str()); + // cur_cfg_->select_scheme(cur_scheme_->get_scheme_name().c_str()); + cur_cfg_->save(); + disconnect(comb_, SIGNAL(currentTextChanged(const QString)), this, SLOT(on_current_scheme_changed())); + comb_->setCurrentIndex(index); + connect(comb_, SIGNAL(currentTextChanged(const QString)), this, SLOT(on_current_scheme_changed())); + + updateRestorePushButton(); + changed_count_++; + } + } + } + else if (btn == m_pbtn_Save) + { + gb::sane_config_schm* schm = cur_cfg_->get_scheme(comb_->currentText().toStdString().c_str()); + updateSchemeFromUi(schm); + cur_cfg_->save(); + schm->release(); + updateRestorePushButton(); + //if (comb_->currentIndex() == 0) + // processIniFile(); + //else + // save_scheme(); + } + else if(btn == m_deleteCur) + { + int id = -1; + QString text(find_current_scheme_menu(&id)); + + if(text.isEmpty()) + return; + + QMessageBox msg(QMessageBox::Question, tr("be sure to delete the configuration"), + tr("Are you sure you want to delete the configuration \"") + text + tr("\" ?"), QMessageBox::Yes | QMessageBox::No, this); + msg.exec(); + if (msg.clickedButton() != msg.button(QMessageBox::Yes)) + return; + + gb::sane_config_schm *sch = cur_cfg_->get_scheme(text.toStdString().c_str()); + cur_cfg_->remove_scheme(text.toStdString().c_str()); + comb_->removeItem(id); + sch->release(); + if(sch == cur_scheme_) + { + restore_2_default_settings(); + updateUIStatus(); + } + + on_current_scheme_changed(); + changed_count_++; + cur_cfg_->save(); + } + else if(btn == m_deleteAll) + { + QMessageBox msg(QMessageBox::Question, tr("be sure to delete the configuration"), + tr("Are you sure you want to delete the configuration?"), QMessageBox::Yes | QMessageBox::No, this); + msg.exec(); + if (msg.clickedButton() != msg.button(QMessageBox::Yes)) + return; + + restore_2_default_settings(); + updateUIStatus(); + comb_->clear(); + changed_count_++; + cur_cfg_->remove_all_schemes(); + cur_cfg_->save(); + + std::vector schemes; + cur_cfg_->get_all_schemes(schemes); + for (int i = 0; i < (int)schemes.size(); ++i) + { + comb_->addItem(QString::fromStdString(schemes[i])); + comb_->setCurrentText(QString::fromStdString(schemes[i])); + } + } + else if (btn == m_pbtn_restore) + { + cur_scheme_ = cur_cfg_->get_scheme(comb_->currentText().toStdString().c_str()); + hg_settingdialog::apply_scheme(m_devHandle, &m_saneAPI, cur_scheme_); + updateUIStatus(); + updateRestorePushButton(); + } + + updateSchemeManagerUi(); +} +void hg_settingdialog::restore_2_default_settings(void) +{ + hg_settingdialog::apply_scheme(m_devHandle, &m_saneAPI, nullptr); + updateRestorePushButton(); +} diff --git a/modules/saneui/hg_settingdialog.h b/modules/saneui/hg_settingdialog.h index 0405a6c0..17d0b25b 100644 --- a/modules/saneui/hg_settingdialog.h +++ b/modules/saneui/hg_settingdialog.h @@ -1,146 +1,171 @@ -#ifndef HG_SETTING_DIALOG_H -#define HG_SETTING_DIALOG_H - -#include -#include -#include -#include "cfg/gb_json.h" -#include "device_menu.h" - -class hg_settingdialog : public QDialog -{ - Q_OBJECT - - int changed_count_; - bool save_; - bool clicked_gamma_; - bool quit_ = false; - dev_que dev_que_; - gb::scanner_cfg *cur_cfg_; - gb::sane_config_schm *cur_scheme_; - - void refresh_control_value(int op_id); - void on_select_scheme(bool apply_to_dev = true); - QString gen_gamma_file_path(void); - - QMenu *top_menu_; - QLineEdit *edit_name_; - QPushButton *m_pbtn_addNew; - QPushButton *del_this_; - QPushButton *del_all_; - QLabel *custom_area_lable_; - QPushButton *btn_cut_area_; - QPushButton *btn_gamma_; - QTextEdit *sketch_; - QLineEdit *m_lineEdit_name; - void create_scheme_management_ui(QVBoxLayout* layout); - QString find_current_scheme_menu(int *scheme_id = nullptr); - - static std::string property_combox_data_type_; - enum _cbox_type - { - COMBO_VAL_STRING = 0, - COMBO_VAL_INT, - COMBO_VAL_FLOAT, - }; - -public: - explicit hg_settingdialog(const SANEAPI* saneApi, SANE_Handle handle, const char *devName, QWidget *parent = nullptr); - ~hg_settingdialog(); - -public: - void initUi(); - void updateOpt(); - void createUI(); - void updateUIStatus(); - QVector find_control(int opt_num); - void keyPressEvent(QKeyEvent *e); - int get_changed_items(void); - gb::sane_config_schm *getCurScheme(); - -private: - static hg_settingdialog *hg_setting_ui_; - SANEAPI m_saneAPI; - SANE_Handle m_devHandle; - std::string m_devName; - QTranslator m_translator; - QTranslator m_translator_qt; - int m_langCode; - -private: - QString m_qstrFileName; - QSettings *m_configIniWrite; - QSettings *m_configIniRead; - -private: - QString md5(QString key); - const void* find_option_description(int id); // return const SANE_Option_Descriptor* pointer - const void* find_option_description(const std::string& title, int* id); // return const SANE_Option_Descriptor* pointer - - virtual void closeEvent(QCloseEvent* e); - virtual bool eventFilter(QObject *target, QEvent *event) override; - bool createMsgBoxUi(bool add, std::string &name); - std::string getCurUiShemeName(std::string name); - void save_scheme(void); - void cancel_setting(void); - std::string getAppVersion(); - void apply_current_scheme(void); - -private: - QVector, QString>> m_list_IdValueTitle; - QVector> m_list_defaultOptions; // default values of device - QVector> m_list_sliderSpinbox; - QVector> m_list_getOpt; - QVector m_list_deviceNames; - QVector m_list_widgets; - -private slots: - void slot_checkedClicked(); - void slot_sliderClicked(int value); - void slot_spinBoxClicked(int value); - void slot_doubleSpinboxClicked(double value); - void slot_string_list_comboBoxClicked(); - void slot_pushButtonClicked(); - void slot_cutButtonClicked(); - void slot_gammaButtonClicked(); - void slot_word_list_comboBoxClicked(int value); - void slot_lineEditInput(); - void slot_buttonAboutClicked(); - void slot_buttonOkClicked(); - void slot_buttonCancelClicked(); - void slot_pushButton_scheme_management(void); - void on_current_scheme_changed(void); - void restore_2_default_settings(void); - -private: - int m_dpiId; - int m_dpiValue; - int m_paperSizeId; - QString m_paperSizeValue; - int m_cutLeftId; - int m_cutTopId; - int m_cutRightId; - int m_cutBottomId; - double m_cutWidth; // 单位是毫米 - double m_cutHeight; // 单位是毫米 - double m_cutLeftValue; // 单位是毫米 - double m_cutTopValue; // 单位是毫米 - double m_cutRightValue; // 单位是毫米 - double m_cutBottomValue; // 单位是毫米 - - int m_colorModeId; - QString m_colorModeValue; - SANE_Gamma m_gammaData; - QComboBox *comb_; -}; - -#endif // HG_SETTING_DIALOG_H - - - - - - - - - +#ifndef HG_SETTING_DIALOG_H +#define HG_SETTING_DIALOG_H + +#include +#include +#include +#include "cfg/gb_json.h" +#include "device_menu.h" + +class hg_settingdialog : public QDialog +{ + Q_OBJECT + + int changed_count_; + bool save_; + bool clicked_gamma_; + bool quit_ = false; + // dev_que dev_que_; + gb::scanner_cfg *cur_cfg_; + gb::sane_config_schm *cur_scheme_; + gb::sane_config_schm *def_value_; + + void refresh_control_value(int op_id); + void on_select_scheme(bool apply_to_dev = true); + QString gen_gamma_file_path(void); + + QMenu *top_menu_; + QLineEdit *edit_name_; + QPushButton *m_pbtn_addNew; + QPushButton* m_pbtn_Save; + QPushButton *m_deleteCur; + QPushButton *m_deleteAll; + QLabel * m_label_restore; + QPushButton* m_pbtn_restore; + QLabel *custom_area_lable_; + QPushButton *btn_cut_area_; + QPushButton *btn_gamma_; + QTextEdit *sketch_; + QLineEdit *m_lineEdit_name; + void create_scheme_management_ui(QVBoxLayout* layout); + QString find_current_scheme_menu(int *scheme_id = nullptr); + + static std::string property_combox_data_type_; + enum _cbox_type + { + COMBO_VAL_STRING = 0, + COMBO_VAL_INT, + COMBO_VAL_FLOAT, + }; + +public: + explicit hg_settingdialog(const SANEAPI* saneApi, SANE_Handle handle, const char *devName, QWidget *parent = nullptr); + ~hg_settingdialog(); + + static void apply_scheme(SANE_Handle dev, LPSANEAPI api, gb::sane_config_schm* schm); + +public: + void initUi(); + void update_opt_value_from_driver(); + void createUI(); + void updateUIStatus(); + QVector find_control(int opt_num); + void keyPressEvent(QKeyEvent *e); + int get_changed_items(void); + int getCloseButtonCliked(); + gb::sane_config_schm *getCurScheme(); + +public: + enum closeButtonClicked + { + closeButtonOk = 0, + closeButtonCancel, + closeButtonNormal, + closeButtonScan, + }; + + int m_closeButton; + +private: + static hg_settingdialog *hg_setting_ui_; + SANEAPI m_saneAPI; + SANE_Handle m_devHandle; + std::string m_devName; + +private: + QString m_qstrFileName; + QSettings *m_configIniWrite; + QSettings *m_configIniRead; + +private: + QString md5(QString key); + const void* find_option_description(int id); // return const SANE_Option_Descriptor* pointer + const void* find_option_description(const std::string& title, int* id); // return const SANE_Option_Descriptor* pointer + + virtual void closeEvent(QCloseEvent* e); + virtual bool eventFilter(QObject *target, QEvent *event) override; + + void save_to_default_if_ui_not_equal_scheme(gb::sane_config_schm* cur, bool save); + bool createMsgBoxUi(bool add, std::string &name); + std::string getCurUiShemeName(std::string name); + void save_scheme(void); + void cancel_setting(void); + std::string getAppVersion(); + void apply_current_scheme(void); + void setIcon(); + void cancelScheme(); + void updateSchemeFromUi(gb::sane_config_schm* schm = nullptr); + void updateSchemeManagerUi(); + void updateRestorePushButton(); + +private: + QVector, QString>> m_list_IdValueTitle; + QVector> m_list_defaultOptions; // default values of device + QVector> m_list_originDeviceScheme; + QVector> m_list_sliderSpinbox; + QVector> m_list_getOpt; + QVector m_list_deviceNames; + QVector m_list_widgets; + +private slots: + void slot_checkedClicked(); + void slot_sliderClicked(int value); + void slot_spinBoxClicked(int value); + void slot_doubleSpinboxClicked(double value); + void slot_string_list_comboBoxClicked(); + void slot_pushButtonClicked(); + void slot_cutButtonClicked(); + void slot_gammaButtonClicked(); + void slot_word_list_comboBoxClicked(int value); + void slot_lineEditInput(); + void slot_buttonAboutClicked(); + void slot_buttonOkClicked(); + void slot_buttonCancelClicked(); + void slot_pushButton_scheme_management(void); + void on_current_scheme_changed(void); + void restore_2_default_settings(void); + +private: + int m_dpiId; + int m_dpiValue; + int m_paperSizeId; + QString m_paperSizeValue; + int m_cutLeftId; + int m_cutTopId; + int m_cutRightId; + int m_cutBottomId; + double m_cutWidth; // 单位是毫米 + double m_cutHeight; // 单位是毫米 + double m_cutLeftValue; // 单位是毫米 + double m_cutTopValue; // 单位是毫米 + double m_cutRightValue; // 单位是毫米 + double m_cutBottomValue; // 单位是毫米 + + int m_colorModeId; + QString m_colorModeValue; + SANE_Gamma m_gammaData; + QComboBox *comb_; + + bool m_isRefreshUi; +}; + +#endif // HG_SETTING_DIALOG_H + + + + + + + + + diff --git a/modules/saneui/setpicclrtool.cpp b/modules/saneui/setpicclrtool.cpp index 3ebedd56..3e4d249a 100644 --- a/modules/saneui/setpicclrtool.cpp +++ b/modules/saneui/setpicclrtool.cpp @@ -1,4 +1,4 @@ -#include "setpicclrtool.h" +#include "setpicclrtool.h" #include "ui_setpicclrtool.h" #include "widget.h" #include @@ -126,6 +126,21 @@ void setPicClrTool::setGrayKeyTable(QList &plv) setGrayKeyPoint(plv); } +QVector setPicClrTool::getRgbAndColorType() +{ + QVector info; + info.clear(); + info.push_back(ui->comboBox->currentIndex()); + info.push_back(ui->colorSetCmb->currentIndex()); + return info; +} + +void setPicClrTool::setRgbAndColorType(int rgbTypeIndex, int colorTypeIndex) +{ + ui->comboBox->setCurrentIndex(rgbTypeIndex); + ui->colorSetCmb->setCurrentIndex(colorTypeIndex); +} + QVector setPicClrTool::getRgbALLPoint() { return ui->widget->getRgbALLPoint(); @@ -222,7 +237,8 @@ void setPicClrTool::lineChangeSlot() void setPicClrTool::on_colorSetCmb_currentIndexChanged(int index) { - (void)index; + ui->widget->updateCurLinePnt(index); + //(void)index; /* switch(index){ case RED: ui->widget->updateCurLinePnt(RED); diff --git a/modules/saneui/setpicclrtool.h b/modules/saneui/setpicclrtool.h index 59c4c25b..c92a8d71 100644 --- a/modules/saneui/setpicclrtool.h +++ b/modules/saneui/setpicclrtool.h @@ -1,4 +1,4 @@ -#ifndef SETPICCLRTOOL_H +#ifndef SETPICCLRTOOL_H #define SETPICCLRTOOL_H //#include "colorlinesetdef.h" @@ -31,6 +31,8 @@ public: QList getGrayKeyTable(); void setGrayKeyTable(QList &plv); + QVector getRgbAndColorType(); + void setRgbAndColorType(int rgbTypeIndex, int colorTypeIndex); private: QVector getRgbALLPoint();