/* * ComplexInputDataParser.cpp * * Created on: 2012-7-26 * Author: limengzhuo */ #include #include #include "ComplexInputDataParser.h" using namespace pai::graphics2d; ItemData::ItemData(QModelIndex parentIndex, QString destItemIDs, QString destCategorys, QString property, QString value, QString parentID) { m_parentIndex = parentIndex; m_property = property; m_value = value; m_parentID = parentID; if (destItemIDs == "" && destCategorys != "") m_destCategoryList = destCategorys.split(':'); if (destItemIDs != "" && destCategorys == "") m_destItemIDList = destItemIDs.split(':'); } QStringList ItemData::GetDestItemIDList() { return m_destItemIDList; } QStringList ItemData::GetDestCategoryList() { return m_destCategoryList; } QModelIndex ItemData::GetParentIndex() { return m_parentIndex; } QString ItemData::GetParentID() { return m_parentID; } QString ItemData::GetValue() { return m_value; } QString ItemData::GetProperty() { return m_property; } ComplexInputDataParser::ComplexInputDataParser(QModelIndex inputIndex, QString inputData, QString inputValue, std::string inputID) { m_inputValue = inputValue; m_inputIndex = inputIndex; QString strOnClick("onclick="); int nPos0 = inputData.indexOf(strOnClick); int nLengthOfOnClick = strOnClick.length(); QString strScriptContents = inputData.mid(nPos0 + nLengthOfOnClick);//这里截取到onclick=之后的内容 QStringList strScriptSentences = strScriptContents.split(';');//用;进行分割 foreach(QString strScriptFunction, strScriptSentences) { QStringList lstFunction = strScriptFunction.split('=');//用=进行分 if (lstFunction.size() < 2) continue; QString strParentID; size_t nLastPointPos = inputID.rfind('.'); //获得parentID. if (nLastPointPos != std::string::npos) { strParentID = QString::fromStdString(inputID.substr(0, nLastPointPos + 1));//把.也包括进去。 } QStringList lstItemsAndProperties = lstFunction[0].split('.');//用.进行分 QString destItemIDs; QString destCategoryIDs; QModelIndex parentIndex; QString strPropertyName; //解析等于号的左边,获得被控制控件的parentIndex, 控制属性名称strPropertyName,被控制控件的ID列表字符串destItemIDs。 for (int i = 0; i < lstItemsAndProperties.size(); i++) { if (lstItemsAndProperties[i].trimmed() == "this") { parentIndex = m_inputIndex; } else if (lstItemsAndProperties[i].trimmed() == "parentNode") { parentIndex = parentIndex.parent(); } else if (lstItemsAndProperties[i].contains("category[")) { int nIDStartPos = lstItemsAndProperties[i].indexOf("category[") + 9; int nIDLastPos = lstItemsAndProperties[i].indexOf(']'); destCategoryIDs = lstItemsAndProperties[i].mid(nIDStartPos, nIDLastPos - nIDStartPos); } else if (lstItemsAndProperties[i].contains("item[")) { int nIDStartPos = lstItemsAndProperties[i].indexOf("item[") + 5; int nIDLastPos = lstItemsAndProperties[i].indexOf(']'); destItemIDs = lstItemsAndProperties[i].mid(nIDStartPos, nIDLastPos - nIDStartPos); } else if (lstItemsAndProperties[i].trimmed() == "display") { strPropertyName = "Visible"; } else if (lstItemsAndProperties[i].trimmed() == "ability") { strPropertyName = "Ability"; } else { } }//end of for QString strItemValue; QStringList lstFindItemValue = lstFunction[1].split('.'); //解析等于号的右边,获得控制属性的值。 for (int i = 0; i < lstFindItemValue.size(); i++) { if (lstFindItemValue[i].contains("item[")) { int nIDStartPos = lstFindItemValue[i].indexOf("item[") + 5; int nIDLastPos = lstFindItemValue[i].indexOf(']'); strItemValue = lstFindItemValue[i].mid(nIDStartPos, nIDLastPos - nIDStartPos); } else if (lstFindItemValue[i].contains("checked?")) { int nStartPos = lstFindItemValue[i].indexOf("checked?") + 8; QString checkString = lstFindItemValue[i].mid(nStartPos); QStringList lstCheckValue = checkString.split(':'); strItemValue = lstCheckValue[0] == "true" || lstCheckValue[0] == "enable" ? "1" : "0"; } }//end of for //使用上面获得的值,构造ItemData并添加到列表中。 if (strPropertyName.isEmpty() == false) { if (destItemIDs.size() <= 0) { ItemData itemData(parentIndex, "", destCategoryIDs, strPropertyName, strItemValue, strParentID); m_itemDataList.append(itemData); } else { ItemData itemData(parentIndex, destItemIDs, "", strPropertyName, strItemValue, strParentID); m_itemDataList.append(itemData); } } } } QList ComplexInputDataParser::GetItemDataList() { return m_itemDataList; } std::string ComplexInputDataParser::ResetInputDataString(std::string inputdata, QString property, QString itemValue) { if (property == "Visible") { std::string strVisibleInputData = inputdata; std::string strInvisible("Invisible;"); size_t nKeyPos = strVisibleInputData.find(strInvisible); if (itemValue == m_inputValue && nKeyPos != std::string::npos) { strVisibleInputData = strVisibleInputData.replace(nKeyPos, strInvisible.length(), ""); } else if (nKeyPos == std::string::npos) { strVisibleInputData = strInvisible + strVisibleInputData; } return strVisibleInputData; } if (property == "Ability") { std::string strAbilityInputData = inputdata; std::string strDisable("Disable;"); size_t nKeyPos = strAbilityInputData.find(strDisable); if (itemValue == m_inputValue && nKeyPos != std::string::npos) { strAbilityInputData = strAbilityInputData.replace(nKeyPos, strDisable.length(), "Enable;"); } else if (nKeyPos == std::string::npos) { std::string strEnable("Enable;"); size_t nKey = strAbilityInputData.find(strEnable); if (nKey != std::string::npos) { strAbilityInputData = strAbilityInputData.replace(nKey, strEnable.length(), "Disable;"); } } return strAbilityInputData; } return std::string(); }