diff --git a/modules/twainui/hg_settingdialog.cpp b/modules/twainui/hg_settingdialog.cpp index f2f9acc6..6f364c5c 100644 --- a/modules/twainui/hg_settingdialog.cpp +++ b/modules/twainui/hg_settingdialog.cpp @@ -115,7 +115,7 @@ hg_settingdialog::hg_settingdialog(class Manager *mgr, SANE_Handle handle, const m_closeButton = closeButtonNormal; initUi(); - on_current_scheme_changed(); + //on_current_scheme_changed(); HGChar cfgPath[512]; GetConfigPath(cfgPath, 512); @@ -124,7 +124,14 @@ hg_settingdialog::hg_settingdialog(class Manager *mgr, SANE_Handle handle, const QFile::copy(oldFile, newFile); int index = getDefaultCfgValue("current", "index", 0); + if (comb_->count() == 1) + index = 0; comb_->setCurrentIndex(index < 0 ? 0 : index); + + saveOriginDeviceScheme(); + applyDeviceScheme(); + + connect(comb_, SIGNAL(currentTextChanged(const QString)), this, SLOT(on_current_scheme_changed())); } hg_settingdialog::~hg_settingdialog() @@ -286,6 +293,7 @@ void hg_settingdialog::create_scheme_management_ui(QVBoxLayout* layout) cur_cfg_->get_all_schemes(schemes); comb_ = new QComboBox(this); + //disconnect(comb_, SIGNAL(currentTextChanged(const QString)), this, SLOT(on_current_scheme_changed())); comb_->addItem(tr("Default scheme")); layout->addSpacing(30); for(int i = 1; i < (int)schemes.size(); ++i) @@ -344,6 +352,20 @@ void hg_settingdialog::create_scheme_management_ui(QVBoxLayout* layout) layout->addStretch(); + m_deleteCur->setEnabled(true); + m_deleteAll->setEnabled(true); + m_pbtn_Save->setEnabled(true); + int index = getDefaultCfgValue("default", "index", 0); + if (index == 0) + { + m_deleteCur->setEnabled(false); + m_pbtn_Save->setEnabled(false); + } + if (index == 1) + { + m_deleteAll->setEnabled(false); + } + //title = new QLabel(this); //title->setText(tr("confgiuration information:")); //layout->addWidget(title); @@ -353,8 +375,6 @@ void hg_settingdialog::create_scheme_management_ui(QVBoxLayout* layout) // sketch_->setFixedSize(width, 200); // sketch_->setVisible(false); //layout->addWidget(sketch_); - - connect(comb_, SIGNAL(currentTextChanged(const QString)), this, SLOT(on_current_scheme_changed())); //on_current_scheme_changed(); } void hg_settingdialog::createUI() @@ -1915,6 +1935,13 @@ void hg_settingdialog::closeEvent(QCloseEvent* e) QFile::remove(newFile); QFile file(oldFile); file.rename(newFile); + if (0 == index) + { + cancelScheme(); + } + + else + cancel_setting(); } e->accept(); @@ -2199,6 +2226,137 @@ void hg_settingdialog::setIcon() #endif } +void hg_settingdialog::applyDeviceScheme() +{ + 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 ctrls = find_control(id); + + if (ctrls.empty()) + continue; + + const SANE_Option_Descriptor* opt = (const SANE_Option_Descriptor*)m_list_defaultOptions.at(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) + { + SANE_Bool value = false; + SANE_Int method = 0; + + m_saneAPI.sane_control_option_api(m_devHandle, id, SANE_ACTION_GET_VALUE, &value, &method); + disconnect(cb, SIGNAL(stateChanged(int)), this, SLOT(slot_checkedClicked())); + cb->setCheckState(value ? Qt::Checked : Qt::Unchecked); + 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) + { + QSlider* slider = qobject_cast(ctrls[i]); + if (slider) + { + SANE_Int value = 0; + SANE_Int method = 0; + disconnect(slider, SIGNAL(valueChanged(int)), this, SLOT(slot_sliderClicked(int))); + m_saneAPI.sane_control_option_api(m_devHandle, id, SANE_ACTION_GET_VALUE, &value, &method); + slider->setValue(value); + connect(slider, SIGNAL(valueChanged(int)), this, SLOT(slot_sliderClicked(int))); + } + else + { + QSpinBox* spin = qobject_cast(ctrls[i]); + if (spin) + { + SANE_Int value = 0; + SANE_Int method = 0; + disconnect(spin, SIGNAL(valueChanged(int)), this, SLOT(slot_spinBoxClicked(int))); + m_saneAPI.sane_control_option_api(m_devHandle, id, SANE_ACTION_GET_VALUE, &value, &method); + spin->setValue(value); + connect(spin, SIGNAL(valueChanged(int)), this, SLOT(slot_spinBoxClicked(int))); + } + } + } + } + else if (opt->type == SANE_TYPE_FIXED) + { + QSlider* slider = NULL; + QDoubleSpinBox* spin = NULL; + for (size_t i = 0; i < (size_t)ctrls.size(); ++i) + { + 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) + { + SANE_Fixed value = 0; + SANE_Int method = 0; + m_saneAPI.sane_control_option_api(m_devHandle, id, SANE_ACTION_GET_VALUE, &value, &method); + double value2 = SANE_UNFIX(value); + slider->setValue(value2 * 100); + connect(slider, SIGNAL(valueChanged(int)), this, SLOT(slot_sliderClicked(int))); + } + + if (spin) + { + SANE_Fixed value = 0; + SANE_Int method = 0; + m_saneAPI.sane_control_option_api(m_devHandle, id, SANE_ACTION_GET_VALUE, &value, &method); + double value2 = SANE_UNFIX(value); + spin->setValue(value2); + 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) + { + char* value = (char*)malloc(opt->size * 2 + 4); + SANE_Int method = 0; + disconnect(comb, SIGNAL(currentTextChanged(const QString)), this, SLOT(slot_string_list_comboBoxClicked())); + m_saneAPI.sane_control_option_api(m_devHandle, id, SANE_ACTION_GET_VALUE, value, &method); + QString value2 = QString(value); + comb->setCurrentText(QString(value2)); + connect(comb, SIGNAL(currentTextChanged(const QString)), this, SLOT(slot_string_list_comboBoxClicked())); + free(value); + } + 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(id - 1).second.toString()); + connect(edit, SIGNAL(textChanged(const QString&)), this, SLOT(slot_lineEditInput())); + } + } + } + } + } +} + void hg_settingdialog::updateDefaultScheme() { updateOpt(); @@ -2391,6 +2549,93 @@ void hg_settingdialog::updateDefaultScheme() } } +void hg_settingdialog::saveOriginDeviceScheme() +{ + m_list_originDeviceScheme.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_originDeviceScheme.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_originDeviceScheme.append(QPair(opt, QVariant(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_originDeviceScheme.append(QPair(opt, QVariant(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_originDeviceScheme.append(QPair(opt, QVariant(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_originDeviceScheme.append(QPair(opt, QVariant(QString::fromStdString(init)))); + } + else + { + m_list_originDeviceScheme.append(QPair(opt, QVariant(0))); + } + } + } +} + +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 f_ratio = m_list_originDeviceScheme.at(i).second.toDouble(); + auto ff = SANE_FIX(f_ratio); + + m_saneAPI.sane_control_option_api(m_devHandle, i + 1, SANE_ACTION_SET_VALUE, &ff, &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::processIniFile() { HGChar cfgPath[512]; @@ -2442,6 +2687,12 @@ void hg_settingdialog::on_current_scheme_changed() m_deleteAll->setEnabled(false); } + if (comb_->currentIndex() == 0) + { + updateDefaultScheme(); + return; + } + QString text(find_current_scheme_menu()); gb::sane_config_schm *cur = nullptr; @@ -2505,11 +2756,6 @@ void hg_settingdialog::on_current_scheme_changed() if(schm) schm->release(); - if (comb_->currentIndex() == 0) - { - updateDefaultScheme(); - } - // sketch_->setHtml(info); } void hg_settingdialog::slot_pushButton_scheme_management(void) diff --git a/modules/twainui/hg_settingdialog.h b/modules/twainui/hg_settingdialog.h index 0cd73a1c..bde87f75 100644 --- a/modules/twainui/hg_settingdialog.h +++ b/modules/twainui/hg_settingdialog.h @@ -109,12 +109,16 @@ private: std::string getAppVersion(); void apply_current_scheme(void); void setIcon(); + void applyDeviceScheme(); void updateDefaultScheme(); + void saveOriginDeviceScheme(); + void cancelScheme(); void processIniFile(); 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;