This commit is contained in:
luoliangyi 2024-05-05 11:29:13 +08:00
parent c4412b68cc
commit a48250bdf8
2 changed files with 104 additions and 237 deletions

View File

@ -17,6 +17,7 @@
Form_DeviceConfig::Form_DeviceConfig(SANE_Handle devHandle, const std::vector<DeviceConfig>& deviceConfigs, QWidget *parent)
: QWidget(parent)
, m_devHandle(devHandle)
, m_defButtonId(-1)
{
Init(deviceConfigs);
}
@ -36,29 +37,27 @@ std::vector<DeviceConfig> Form_DeviceConfig::GetDeviceConfigs()
{
const DeviceConfigEx &deviceConfigEx = m_deviceConfigsGroups[i].deviceConfigs[j];
bool out = true;
for (int k = 0; k < (int)m_defDeviceConfigs.size(); ++k)
{
if (deviceConfigEx.name == m_defDeviceConfigs[k].name
&& deviceConfigEx.valueType == m_defDeviceConfigs[k].valueType
&& deviceConfigEx.stringValue == m_defDeviceConfigs[k].stringValue
&& deviceConfigEx.intValue == m_defDeviceConfigs[k].intValue
&& deviceConfigEx.doubleValue == m_defDeviceConfigs[k].doubleValue
&& deviceConfigEx.boolValue == m_defDeviceConfigs[k].boolValue)
{
out = false;
break;
}
}
if (out)
{
DeviceConfig deviceConfig;
deviceConfig.name = deviceConfigEx.name;
deviceConfig.valueType = deviceConfigEx.valueType;
if (1 == deviceConfigEx.valueType && deviceConfigEx.stringValue != deviceConfigEx.defStringValue)
{
deviceConfig.stringValue = deviceConfigEx.stringValue;
deviceConfigs.push_back(deviceConfig);
}
else if (2 == deviceConfigEx.valueType && deviceConfigEx.intValue != deviceConfigEx.defIntValue)
{
deviceConfig.intValue = deviceConfigEx.intValue;
deviceConfigs.push_back(deviceConfig);
}
else if (3 == deviceConfigEx.valueType && deviceConfigEx.doubleValue != deviceConfigEx.defDoubleValue)
{
deviceConfig.doubleValue = deviceConfigEx.doubleValue;
deviceConfigs.push_back(deviceConfig);
}
else if (4 == deviceConfigEx.valueType && deviceConfigEx.boolValue != deviceConfigEx.defBoolValue)
{
deviceConfig.boolValue = deviceConfigEx.boolValue;
deviceConfigs.push_back(deviceConfig);
}
@ -70,7 +69,8 @@ std::vector<DeviceConfig> Form_DeviceConfig::GetDeviceConfigs()
void Form_DeviceConfig::Init(const std::vector<DeviceConfig>& deviceConfigs)
{
// 1.恢复默认
// 1.获取默认ID
m_defButtonId = -1;
SANE_Int num_dev_options = 0;
sane_control_option(m_devHandle, 0, SANE_ACTION_GET_VALUE, &num_dev_options, NULL);
for (int i = 1; i < num_dev_options; ++i)
@ -85,113 +85,16 @@ void Form_DeviceConfig::Init(const std::vector<DeviceConfig>& deviceConfigs)
if (0 == strcmp(SANE_STD_OPT_NAME_RESTORE, name) && SANE_TYPE_BUTTON == desp->type)
{
sane_control_option(m_devHandle, i, SANE_ACTION_SET_VALUE, NULL, NULL);
m_defButtonId = i;
break;
}
}
// 2.保存默认配置值
m_defDeviceConfigs.clear();
for (int i = 1; i < num_dev_options; ++i)
{
const SANE_Option_Descriptor* desp = sane_get_option_descriptor(m_devHandle, i);
if (NULL == desp)
continue;
// 2.恢复默认
assert(-1 != m_defButtonId);
sane_control_option(m_devHandle, m_defButtonId, SANE_ACTION_SET_VALUE, NULL, NULL);
const char* name = desp->name;
while (' ' == *name)
++name;
if (0 == strcmp(SANE_STD_OPT_NAME_IS_CUSTOM_GAMMA, name))
continue;
if (SANE_TYPE_STRING == desp->type)
{
char value[256] = { 0 };
sane_control_option(m_devHandle, i, SANE_ACTION_GET_VALUE, value, NULL);
DeviceConfig deviceConfig;
deviceConfig.name = name;
deviceConfig.valueType = 1;
deviceConfig.stringValue = value;
m_defDeviceConfigs.push_back(deviceConfig);
}
else if (SANE_TYPE_INT == desp->type)
{
SANE_Int value = 0;
sane_control_option(m_devHandle, i, SANE_ACTION_GET_VALUE, &value, NULL);
DeviceConfig deviceConfig;
deviceConfig.name = name;
deviceConfig.valueType = 2;
deviceConfig.intValue = (int)value;
m_defDeviceConfigs.push_back(deviceConfig);
}
else if (SANE_TYPE_FIXED == desp->type)
{
SANE_Word value = 0;
sane_control_option(m_devHandle, i, SANE_ACTION_GET_VALUE, &value, NULL);
DeviceConfig deviceConfig;
deviceConfig.name = name;
deviceConfig.valueType = 3;
deviceConfig.doubleValue = SANE_UNFIX(value);
m_defDeviceConfigs.push_back(deviceConfig);
}
else if (SANE_TYPE_BOOL == desp->type)
{
SANE_Bool value = 0;
sane_control_option(m_devHandle, i, SANE_ACTION_GET_VALUE, &value, NULL);
DeviceConfig deviceConfig;
deviceConfig.name = name;
deviceConfig.valueType = 4;
deviceConfig.boolValue = (bool)value;
m_defDeviceConfigs.push_back(deviceConfig);
}
}
// 3.设置新配置
for (int i = 0; i < (int)deviceConfigs.size(); ++i)
{
for (int j = 1; j < num_dev_options; ++j)
{
const SANE_Option_Descriptor* desp = sane_get_option_descriptor(m_devHandle, j);
if (NULL == desp)
continue;
const char* name = desp->name;
while (' ' == *name)
++name;
if (0 == strcmp(deviceConfigs[i].name.c_str(), name))
{
if (SANE_TYPE_STRING == desp->type)
{
sane_control_option(m_devHandle, j, SANE_ACTION_SET_VALUE, (void*)deviceConfigs[i].stringValue.c_str(), NULL);
}
else if (SANE_TYPE_INT == desp->type)
{
SANE_Int value = deviceConfigs[i].intValue;
sane_control_option(m_devHandle, j, SANE_ACTION_SET_VALUE, &value, NULL);
}
else if (SANE_TYPE_FIXED == desp->type)
{
SANE_Fixed value = SANE_FIX(deviceConfigs[i].doubleValue);
sane_control_option(m_devHandle, j, SANE_ACTION_SET_VALUE, &value, NULL);
}
else if (SANE_TYPE_BOOL == desp->type)
{
SANE_Bool value = (SANE_Bool)deviceConfigs[i].boolValue;
sane_control_option(m_devHandle, j, SANE_ACTION_SET_VALUE, &value, NULL);
}
break;
}
}
}
// 4.更新当前配置信息
// 3.更新当前配置信息
m_deviceConfigsGroups.clear();
for (int i = 1; i < num_dev_options; ++i)
{
@ -227,6 +130,7 @@ void Form_DeviceConfig::Init(const std::vector<DeviceConfig>& deviceConfigs)
devConfigEx.title = title;
devConfigEx.valueType = 1;
devConfigEx.stringValue = value;
devConfigEx.defStringValue = value;
devConfigEx.hide = ((desp->cap & SANE_CAP_INACTIVE) == SANE_CAP_INACTIVE);
devConfigEx.readOnly = IS_CAP_READONLY(desp->cap);
@ -255,6 +159,7 @@ void Form_DeviceConfig::Init(const std::vector<DeviceConfig>& deviceConfigs)
devConfigEx.title = title;
devConfigEx.valueType = 2;
devConfigEx.intValue = (int)value;
devConfigEx.defIntValue = (int)value;
devConfigEx.hide = ((desp->cap & SANE_CAP_INACTIVE) == SANE_CAP_INACTIVE);
devConfigEx.readOnly = IS_CAP_READONLY(desp->cap);
@ -288,6 +193,7 @@ void Form_DeviceConfig::Init(const std::vector<DeviceConfig>& deviceConfigs)
devConfigEx.title = title;
devConfigEx.valueType = 3;
devConfigEx.doubleValue = SANE_UNFIX(value);
devConfigEx.defDoubleValue = SANE_UNFIX(value);
devConfigEx.hide = ((desp->cap & SANE_CAP_INACTIVE) == SANE_CAP_INACTIVE);
devConfigEx.readOnly = IS_CAP_READONLY(desp->cap);
@ -321,6 +227,7 @@ void Form_DeviceConfig::Init(const std::vector<DeviceConfig>& deviceConfigs)
devConfigEx.title = title;
devConfigEx.valueType = 4;
devConfigEx.boolValue = (bool)value;
devConfigEx.defBoolValue = (bool)value;
devConfigEx.hide = ((desp->cap & SANE_CAP_INACTIVE) == SANE_CAP_INACTIVE);
devConfigEx.readOnly = IS_CAP_READONLY(desp->cap);
@ -329,7 +236,7 @@ void Form_DeviceConfig::Init(const std::vector<DeviceConfig>& deviceConfigs)
}
}
// 5.创建UI
// 4.创建UI
QPushButton *defaultBtn = new QPushButton(tr("Default"));
connect(defaultBtn, &QPushButton::clicked, this, &Form_DeviceConfig::on_defaultBtn_clicked);
QHBoxLayout *hLayout = new QHBoxLayout;
@ -509,6 +416,7 @@ void Form_DeviceConfig::Init(const std::vector<DeviceConfig>& deviceConfigs)
}
ctrl->setProperty("config_id", deviceConfigEx.id);
ctrl->setProperty("configex_ptr", QVariant::fromValue(&deviceConfigEx));
deviceConfigEx.label = label;
deviceConfigEx.ctrl = ctrl;
deviceConfigEx.ctrlWidget = ctrlWidget;
@ -521,6 +429,49 @@ void Form_DeviceConfig::Init(const std::vector<DeviceConfig>& deviceConfigs)
tabWidget->addTab(scrollArea, QString::fromStdString(m_deviceConfigsGroups[i].groupTitle));
}
// 5.设置新配置
for (int i = 0; i < (int)deviceConfigs.size(); ++i)
{
for (int j = 1; j < num_dev_options; ++j)
{
const SANE_Option_Descriptor* desp = sane_get_option_descriptor(m_devHandle, j);
if (NULL == desp)
continue;
const char* name = desp->name;
while (' ' == *name)
++name;
if (0 == strcmp(deviceConfigs[i].name.c_str(), name))
{
if (SANE_TYPE_STRING == desp->type)
{
sane_control_option(m_devHandle, j, SANE_ACTION_SET_VALUE, (void*)deviceConfigs[i].stringValue.c_str(), NULL);
}
else if (SANE_TYPE_INT == desp->type)
{
SANE_Int value = deviceConfigs[i].intValue;
sane_control_option(m_devHandle, j, SANE_ACTION_SET_VALUE, &value, NULL);
}
else if (SANE_TYPE_FIXED == desp->type)
{
SANE_Fixed value = SANE_FIX(deviceConfigs[i].doubleValue);
sane_control_option(m_devHandle, j, SANE_ACTION_SET_VALUE, &value, NULL);
}
else if (SANE_TYPE_BOOL == desp->type)
{
SANE_Bool value = (SANE_Bool)deviceConfigs[i].boolValue;
sane_control_option(m_devHandle, j, SANE_ACTION_SET_VALUE, &value, NULL);
}
break;
}
}
}
// 6.更新UI
Update(-1);
}
void Form_DeviceConfig::Update(int ignoreId)
@ -793,24 +744,8 @@ bool Form_DeviceConfig::eventFilter(QObject *target, QEvent *event)
void Form_DeviceConfig::on_defaultBtn_clicked()
{
// 1.恢复默认
SANE_Int num_dev_options = 0;
sane_control_option(m_devHandle, 0, SANE_ACTION_GET_VALUE, &num_dev_options, NULL);
for (int i = 1; i < num_dev_options; ++i)
{
const SANE_Option_Descriptor* desp = sane_get_option_descriptor(m_devHandle, i);
if (NULL == desp)
continue;
const char* name = desp->name;
while (' ' == *name)
++name;
if (0 == strcmp(SANE_STD_OPT_NAME_RESTORE, name) && SANE_TYPE_BUTTON == desp->type)
{
sane_control_option(m_devHandle, i, SANE_ACTION_SET_VALUE, NULL, NULL);
break;
}
}
assert(-1 != m_defButtonId);
sane_control_option(m_devHandle, m_defButtonId, SANE_ACTION_SET_VALUE, NULL, NULL);
// 2.更新UI
Update(-1);
@ -821,6 +756,7 @@ void Form_DeviceConfig::on_string_list_comboBoxClicked()
QComboBox *comboBox = qobject_cast<QComboBox*>(sender());
std::string currentText = comboBox->currentText().toUtf8();
SANE_Int id = comboBox->property("config_id").toInt();
DeviceConfigEx *deviceConfigEx = qvariant_cast<DeviceConfigEx*>(comboBox->property("configex_ptr"));
SANE_Int method = 0;
SANE_Status ret = sane_control_option(m_devHandle, id, SANE_ACTION_SET_VALUE, (void *)currentText.c_str(), &method);
@ -835,18 +771,7 @@ void Form_DeviceConfig::on_string_list_comboBoxClicked()
return;
}
for (int i = 0; i < (int)m_deviceConfigsGroups.size(); ++i)
{
for (int j = 0; j < (int)m_deviceConfigsGroups[i].deviceConfigs.size(); ++j)
{
if (m_deviceConfigsGroups[i].deviceConfigs[j].id == id)
{
m_deviceConfigsGroups[i].deviceConfigs[j].stringValue = currentText;
break;
}
}
}
deviceConfigEx->stringValue = currentText;
if ((method & SANE_INFO_RELOAD_OPTIONS) == SANE_INFO_RELOAD_OPTIONS)
{
Update(id);
@ -858,6 +783,7 @@ void Form_DeviceConfig::on_int_list_comboBoxClicked()
QComboBox *comboBox = qobject_cast<QComboBox*>(sender());
SANE_Int currentValue = comboBox->currentText().toInt();
SANE_Int id = comboBox->property("config_id").toInt();
DeviceConfigEx *deviceConfigEx = qvariant_cast<DeviceConfigEx*>(comboBox->property("configex_ptr"));
SANE_Int method = 0;
SANE_Status ret = sane_control_option(m_devHandle, id, SANE_ACTION_SET_VALUE, &currentValue, &method);
@ -872,18 +798,7 @@ void Form_DeviceConfig::on_int_list_comboBoxClicked()
return;
}
for (int i = 0; i < (int)m_deviceConfigsGroups.size(); ++i)
{
for (int j = 0; j < (int)m_deviceConfigsGroups[i].deviceConfigs.size(); ++j)
{
if (m_deviceConfigsGroups[i].deviceConfigs[j].id == id)
{
m_deviceConfigsGroups[i].deviceConfigs[j].intValue = currentValue;
break;
}
}
}
deviceConfigEx->intValue = currentValue;
if ((method & SANE_INFO_RELOAD_OPTIONS) == SANE_INFO_RELOAD_OPTIONS)
{
Update(id);
@ -895,6 +810,7 @@ void Form_DeviceConfig::on_double_list_comboBoxClicked()
QComboBox *comboBox = qobject_cast<QComboBox*>(sender());
SANE_Word currentValue = SANE_FIX(comboBox->currentText().toDouble());
SANE_Int id = comboBox->property("config_id").toInt();
DeviceConfigEx *deviceConfigEx = qvariant_cast<DeviceConfigEx*>(comboBox->property("configex_ptr"));
SANE_Int method = 0;
SANE_Status ret = sane_control_option(m_devHandle, id, SANE_ACTION_SET_VALUE, &currentValue, &method);
@ -909,18 +825,7 @@ void Form_DeviceConfig::on_double_list_comboBoxClicked()
return;
}
for (int i = 0; i < (int)m_deviceConfigsGroups.size(); ++i)
{
for (int j = 0; j < (int)m_deviceConfigsGroups[i].deviceConfigs.size(); ++j)
{
if (m_deviceConfigsGroups[i].deviceConfigs[j].id == id)
{
m_deviceConfigsGroups[i].deviceConfigs[j].doubleValue = comboBox->currentText().toDouble();
break;
}
}
}
deviceConfigEx->doubleValue = comboBox->currentText().toDouble();
if ((method & SANE_INFO_RELOAD_OPTIONS) == SANE_INFO_RELOAD_OPTIONS)
{
Update(id);
@ -932,6 +837,8 @@ void Form_DeviceConfig::on_int_sliderClicked(int value)
QSlider *slider = qobject_cast<QSlider*>(sender());
SANE_Int currentValue = value;
SANE_Int id = slider->property("config_id").toInt();
DeviceConfigEx *deviceConfigEx = qvariant_cast<DeviceConfigEx*>(slider->property("configex_ptr"));
QSpinBox* spinBox = qvariant_cast<QSpinBox*>(slider->property("relate_ctrl"));
disconnect(spinBox, SIGNAL(valueChanged(int)), this, SLOT(on_relate_spinBoxClicked(int)));
spinBox->setValue(value);
@ -953,18 +860,7 @@ void Form_DeviceConfig::on_int_sliderClicked(int value)
return;
}
for (int i = 0; i < (int)m_deviceConfigsGroups.size(); ++i)
{
for (int j = 0; j < (int)m_deviceConfigsGroups[i].deviceConfigs.size(); ++j)
{
if (m_deviceConfigsGroups[i].deviceConfigs[j].id == id)
{
m_deviceConfigsGroups[i].deviceConfigs[j].intValue = currentValue;
break;
}
}
}
deviceConfigEx->intValue = currentValue;
if ((method & SANE_INFO_RELOAD_OPTIONS) == SANE_INFO_RELOAD_OPTIONS)
{
Update(id);
@ -976,6 +872,8 @@ void Form_DeviceConfig::on_double_sliderClicked(int value)
QSlider *slider = qobject_cast<QSlider*>(sender());
SANE_Word currentValue = SANE_FIX(value / 100.0);
SANE_Int id = slider->property("config_id").toInt();
DeviceConfigEx *deviceConfigEx = qvariant_cast<DeviceConfigEx*>(slider->property("configex_ptr"));
QDoubleSpinBox* doubleSpinBox = qvariant_cast<QDoubleSpinBox*>(slider->property("relate_ctrl"));
disconnect(doubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(on_relate_doubleSpinboxClicked(double)));
doubleSpinBox->setValue(value / 100.0);
@ -997,18 +895,7 @@ void Form_DeviceConfig::on_double_sliderClicked(int value)
return;
}
for (int i = 0; i < (int)m_deviceConfigsGroups.size(); ++i)
{
for (int j = 0; j < (int)m_deviceConfigsGroups[i].deviceConfigs.size(); ++j)
{
if (m_deviceConfigsGroups[i].deviceConfigs[j].id == id)
{
m_deviceConfigsGroups[i].deviceConfigs[j].doubleValue = value / 100.0;
break;
}
}
}
deviceConfigEx->doubleValue = value / 100.0;
if ((method & SANE_INFO_RELOAD_OPTIONS) == SANE_INFO_RELOAD_OPTIONS)
{
Update(id);
@ -1034,6 +921,7 @@ void Form_DeviceConfig::on_string_comboBoxClicked()
QComboBox *comboBox = qobject_cast<QComboBox*>(sender());
std::string currentText = comboBox->currentText().toUtf8();
SANE_Int id = comboBox->property("config_id").toInt();
DeviceConfigEx *deviceConfigEx = qvariant_cast<DeviceConfigEx*>(comboBox->property("configex_ptr"));
SANE_Int method = 0;
SANE_Status ret = sane_control_option(m_devHandle, id, SANE_ACTION_SET_VALUE, (void *)currentText.c_str(), &method);
@ -1048,18 +936,7 @@ void Form_DeviceConfig::on_string_comboBoxClicked()
return;
}
for (int i = 0; i < (int)m_deviceConfigsGroups.size(); ++i)
{
for (int j = 0; j < (int)m_deviceConfigsGroups[i].deviceConfigs.size(); ++j)
{
if (m_deviceConfigsGroups[i].deviceConfigs[j].id == id)
{
m_deviceConfigsGroups[i].deviceConfigs[j].stringValue = currentText;
break;
}
}
}
deviceConfigEx->stringValue = currentText;
if ((method & SANE_INFO_RELOAD_OPTIONS) == SANE_INFO_RELOAD_OPTIONS)
{
Update(id);
@ -1071,6 +948,7 @@ void Form_DeviceConfig::on_spinBoxClicked(int value)
QSpinBox* spinBox = qobject_cast<QSpinBox*>(sender());
SANE_Int currentValue = value;
SANE_Int id = spinBox->property("config_id").toInt();
DeviceConfigEx *deviceConfigEx = qvariant_cast<DeviceConfigEx*>(spinBox->property("configex_ptr"));
SANE_Int method = 0;
SANE_Status ret = sane_control_option(m_devHandle, id, SANE_ACTION_SET_VALUE, &currentValue, &method);
@ -1085,18 +963,7 @@ void Form_DeviceConfig::on_spinBoxClicked(int value)
return;
}
for (int i = 0; i < (int)m_deviceConfigsGroups.size(); ++i)
{
for (int j = 0; j < (int)m_deviceConfigsGroups[i].deviceConfigs.size(); ++j)
{
if (m_deviceConfigsGroups[i].deviceConfigs[j].id == id)
{
m_deviceConfigsGroups[i].deviceConfigs[j].intValue = currentValue;
break;
}
}
}
deviceConfigEx->intValue = currentValue;
if ((method & SANE_INFO_RELOAD_OPTIONS) == SANE_INFO_RELOAD_OPTIONS)
{
Update(id);
@ -1108,6 +975,7 @@ void Form_DeviceConfig::on_checkedClicked()
QCheckBox *checkBox = qobject_cast<QCheckBox*>(sender());
SANE_Bool currentState = checkBox->isChecked();
int id = checkBox->property("config_id").toInt();
DeviceConfigEx *deviceConfigEx = qvariant_cast<DeviceConfigEx*>(checkBox->property("configex_ptr"));
SANE_Int method = 0;
SANE_Status ret = sane_control_option(m_devHandle, id, SANE_ACTION_SET_VALUE, &currentState, &method);
@ -1122,18 +990,7 @@ void Form_DeviceConfig::on_checkedClicked()
return;
}
for (int i = 0; i < (int)m_deviceConfigsGroups.size(); ++i)
{
for (int j = 0; j < (int)m_deviceConfigsGroups[i].deviceConfigs.size(); ++j)
{
if (m_deviceConfigsGroups[i].deviceConfigs[j].id == id)
{
m_deviceConfigsGroups[i].deviceConfigs[j].boolValue = currentState;
break;
}
}
}
deviceConfigEx->boolValue = currentState;
if ((method & SANE_INFO_RELOAD_OPTIONS) == SANE_INFO_RELOAD_OPTIONS)
{
Update(id);

View File

@ -41,9 +41,13 @@ struct DeviceConfigEx
valueType = 0;
stringValue.clear();
defStringValue.clear();
intValue = 0;
defIntValue = 0;
doubleValue = 0;
defDoubleValue = 0;
boolValue = false;
defBoolValue = false;
rangeType = 0;
stringValueList.clear();
@ -69,9 +73,13 @@ struct DeviceConfigEx
// 配置值
int valueType; // 0-无1-字符串2-整型3-浮点4-布尔
std::string stringValue;
std::string defStringValue;
int intValue;
int defIntValue;
double doubleValue;
double defDoubleValue;
bool boolValue;
bool defBoolValue;
// 配置取值范围
int rangeType; // 0-无1-字符串列表2-整型列表3-浮点数列表4-整型范围5-浮点数范围
@ -84,6 +92,8 @@ struct DeviceConfigEx
double doubleValueMax;
};
Q_DECLARE_METATYPE(DeviceConfigEx*)
struct DeviceConfigsGroup
{
std::string groupTitle;
@ -122,7 +132,7 @@ private slots:
private:
SANE_Handle m_devHandle;
std::vector<DeviceConfig> m_defDeviceConfigs; // 默认配置值
int m_defButtonId;
std::vector<DeviceConfigsGroup> m_deviceConfigsGroups; // 当前配置信息
};