QVariant FormularModel::data(const QModelIndex &index, int role) const { QVariant data = QVariant(); if(index.isValid()) { FieldData *field = m_formularData.at(index.row()); if(field != NULL) { switch(role) { case Qt::DisplayRole: { HeaderSection section = (HeaderSection)index.column(); switch(section) { case Size: { data = QVariant(field->getSize()); break; } case Type: { data = QVariant(FieldData::types.at(field->getType())); break; } case Name: { data = QVariant(field->getName()); break; } case Description: { data = QVariant(field->getDescription()); break; } } break; } case Qt::EditRole: { data = QVariant((long long int)field); break; } case Qt::ToolTipRole: { QString toolTip = QString::fromUtf8( "Размер:\t\t%size\n" "Тип:\t\t\t%type\n" "Размерность:\t\t%dimension\n" "Наименование:\t%name\n" "Описание:\t\t%description" "%additional" ); toolTip.replace("%size", QString::number(field->getSize())); toolTip.replace("%type", FieldData::types.at(field->getType())); toolTip.replace("%dimension", FieldData::dimensions.at(field->getDimension())); toolTip.replace("%name", field->getName()); toolTip.replace("%description", field->getDescription()); switch(field->getType()) { case FieldData::Integer: case FieldData::Real: case FieldData::Boolean: case FieldData::String: case FieldData::Unused: { toolTip.replace("%additional", QString()); break; } case FieldData::Scalable: { FieldScalable *fieldScalable = static_cast<FieldScalable*>(field); QString additional = QString::fromUtf8( "\n\nЦена %kind разряда:\t%order\n" "%sign" ); if(fieldScalable->hasAdditionalCode()) { additional.replace("%sign", QString::fromUtf8("Знак в дополнительном коде")); } else if(fieldScalable->hasHighBitSign()) { additional.replace("%sign", QString::fromUtf8("Знак в старшем разряде")); } else { additional.replace("%sign", QString()); } if(fieldScalable->getHighOrderBit() != 0.0) { additional.replace("%kind", QString::fromUtf8("старшего")); additional.replace("%order", QString::number(fieldScalable->getHighOrderBit())); } else if(fieldScalable->getLowerOrderBit() != 0.0) { additional.replace("%kind", QString::fromUtf8("младшего")); additional.replace("%order", QString::number(fieldScalable->getLowerOrderBit())); } else { additional = QString(); } toolTip.replace("%additional", additional); break; } case FieldData::Enumeration: { FieldEnumeration *fieldEnumeration = static_cast<FieldEnumeration*>(field); QString additional = "\n"; for(int i = 0; i < fieldEnumeration->getElements().size(); i++) { additional.append("\n%code - %transcript"); additional.replace("%code", fieldEnumeration->getElements()[i].getCode()); additional.replace("%transcript", fieldEnumeration->getElements()[i].getTranscript()); } toolTip.replace("%additional", additional); break; } case FieldData::Constant: { FieldConstant *fieldConstant = static_cast<FieldConstant*>(field); QString additional = QString::fromUtf8("\n\nКонстанта %constant"); additional.replace("%constant", fieldConstant->getValue()); toolTip.replace("%additional", additional); break; } } data = QVariant(toolTip); break; } // case Qt::SizeHintRole: { // int size = 0; // for(int i = 0; i <= index.row(); i++) { // size += index.model()->index(i, Size).data(Qt::DisplayRole).toInt(); // } // qDebug() << "Size" << size; // if(size == 16) { // data = QVariant(QSize(50,40)); // } else { // data = QVariant(QSize(50, 20)); // } // break; // } } } } return data; }
bool FormularEditor::writeToFile(QString fileName) { QDomDocument domDocument("formular"); QDomNode xmlNode = domDocument.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\""); domDocument.insertBefore(xmlNode, domDocument.firstChildElement()); QDomElement domRoot; domRoot = domDocument.createElement("formular"); domRoot.setAttribute("capacity", Formular::capacities.at(ui->capacityBox->currentIndex())); domRoot.setAttribute("description", ui->descriptionEdit->toPlainText()); domDocument.appendChild(domRoot); for(int i = 0; i < m_formularModel->rowCount(); i++) { QDomElement domField = domDocument.createElement("field"); domRoot.appendChild(domField); FieldData *field = (FieldData*)m_formularModel->data(m_formularModel->index(i), Qt::EditRole).toInt(); domField.setAttribute("name", field->getName()); domField.setAttribute("description", field->getDescription()); domField.setAttribute("type", FieldData::types.at(field->getType())); domField.setAttribute("dimension", FieldData::dimensions.at(field->getDimension())); domField.setAttribute("size", field->getSize()); switch(field->getType()) { case FieldData::Integer: case FieldData::Real: case FieldData::Boolean: case FieldData::String: case FieldData::Unused: { break; } case FieldData::Scalable: { FieldScalable *fieldScalable = static_cast<FieldScalable*>((FieldData*)m_formularModel->data(m_formularModel->index(i), Qt::EditRole).toInt()); domField.setAttribute("highOrderBit", fieldScalable->getHighOrderBit()); domField.setAttribute("lowerOrderBit", fieldScalable->getLowerOrderBit()); domField.setAttribute("additionalCode", fieldScalable->hasAdditionalCode()); domField.setAttribute("highBitSign", fieldScalable->hasHighBitSign()); break; } case FieldData::Enumeration: { FieldEnumeration *fieldEnumeration = static_cast<FieldEnumeration*>((FieldData*)m_formularModel->data(m_formularModel->index(i), Qt::EditRole).toInt()); for (int j = 0; j < fieldEnumeration->getElements().size(); j++) { QDomElement domEnumerationElement = domDocument.createElement("element"); domField.appendChild(domEnumerationElement); domEnumerationElement.setAttribute("code", fieldEnumeration->getElements()[j].getCode()); domEnumerationElement.setAttribute("acronym", fieldEnumeration->getElements()[j].getAcronym()); domEnumerationElement.setAttribute("transcript", fieldEnumeration->getElements()[j].getTranscript()); } break; } case FieldData::Constant: { FieldConstant *fieldConstant = static_cast<FieldConstant*>((FieldData*)m_formularModel->data(m_formularModel->index(i), Qt::EditRole).toInt()); domField.setAttribute("value", fieldConstant->getValue()); break; } } } QFile file(fileName); if(!file.open(QIODevice::WriteOnly)) return false; QTextStream st(&file); domDocument.save(st, 4); file.close(); return true; }