Browse Source

update states on contract rename

cl-refactor
arkpar 10 years ago
parent
commit
16a778073c
  1. 7
      mix/CodeModel.cpp
  2. 2
      mix/CodeModel.h
  3. 9
      mix/qml/ProjectList.qml
  4. 39
      mix/qml/StateListModel.qml

7
mix/CodeModel.cpp

@ -258,11 +258,16 @@ void CodeModel::runCompilationJob(int _jobId)
CompiledContract* contract = new CompiledContract(cs, name, source); CompiledContract* contract = new CompiledContract(cs, name, source);
QQmlEngine::setObjectOwnership(contract, QQmlEngine::CppOwnership); QQmlEngine::setObjectOwnership(contract, QQmlEngine::CppOwnership);
result[name] = contract; result[name] = contract;
CompiledContract* prevContract = m_contractMap.value(name); CompiledContract* prevContract = nullptr;
for (ContractMap::const_iterator c = m_contractMap.cbegin(); c != m_contractMap.cend(); ++c)
if (c.value()->documentId() == contract->documentId())
prevContract = c.value();
if (prevContract != nullptr && prevContract->contractInterface() != result[name]->contractInterface()) if (prevContract != nullptr && prevContract->contractInterface() != result[name]->contractInterface())
emit contractInterfaceChanged(name); emit contractInterfaceChanged(name);
if (prevContract == nullptr) if (prevContract == nullptr)
emit newContractCompiled(name); emit newContractCompiled(name);
else if (prevContract->contract()->name() != name)
emit contractRenamed(contract->documentId(), prevContract->contract()->name(), name);
} }
releaseContracts(); releaseContracts();
m_contractMap.swap(result); m_contractMap.swap(result);

2
mix/CodeModel.h

@ -165,6 +165,8 @@ signals:
void contractInterfaceChanged(QString _documentId); void contractInterfaceChanged(QString _documentId);
/// Emitted if there is a new contract compiled for the first time /// Emitted if there is a new contract compiled for the first time
void newContractCompiled(QString _documentId); void newContractCompiled(QString _documentId);
/// Emitted if a contract name has been changed
void contractRenamed(QString _documentId, QString _oldName, QString _newName);
public slots: public slots:
/// Update code model on source code change /// Update code model on source code change

9
mix/qml/ProjectList.qml

@ -111,15 +111,16 @@ Item {
Connections { Connections {
target: codeModel target: codeModel
onCompilationComplete: { onContractRenamed: {
if (modelData === "Contracts") { if (modelData === "Contracts")
{
var ci = 0; var ci = 0;
for (var si = 0; si < projectModel.listModel.count; si++) { for (var si = 0; si < projectModel.listModel.count; si++) {
var document = projectModel.listModel.get(si); var document = projectModel.listModel.get(si);
if (document.isContract) { if (document.isContract) {
var compiledDoc = codeModel.contractByDocumentId(document.documentId); var compiledDoc = codeModel.contractByDocumentId(document.documentId);
if (compiledDoc && compiledDoc.documentId === document.documentId && compiledDoc.contract.name !== document.name) { if (_documentId === document.documentId && _newName !== document.name) {
document.name = compiledDoc.contract.name; document.name = _newName;
projectModel.listModel.set(si, document); projectModel.listModel.set(si, document);
sectionModel.set(ci, document); sectionModel.set(ci, document);
} }

39
mix/qml/StateListModel.qml

@ -128,6 +128,9 @@ Item {
onNewContractCompiled: { onNewContractCompiled: {
stateListModel.addNewContracts(); stateListModel.addNewContracts();
} }
onContractRenamed: {
stateListModel.renameContracts(_oldName, _newName);
}
} }
StateDialog { StateDialog {
@ -206,11 +209,36 @@ Item {
return item; return item;
} }
function renameContracts(oldName, newName) {
var changed = false;
for(var c in codeModel.contracts) {
for (var s = 0; s < stateListModel.count; s++) {
var state = stateList[s];
for (var t = 0; t < state.transactions.length; t++) {
var transaction = state.transactions[t];
if (transaction.contractId === oldName) {
transaction.contractId = newName;
if (transaction.functionId === oldName)
transaction.functionId = newName;
changed = true;
state.transactions[t] = transaction;
}
}
stateListModel.set(s, state);
stateList[s] = state;
}
}
if (changed)
save();
}
function addNewContracts() { function addNewContracts() {
//add new contracts for all states //add new contracts for all states
var changed = false;
for(var c in codeModel.contracts) { for(var c in codeModel.contracts) {
for (var s = 0; s < stateListModel.count; s++) { for (var s = 0; s < stateListModel.count; s++) {
var state = stateList[s];//toPlainStateItem(stateListModel.get(s)); var state = stateList[s];
for (var t = 0; t < state.transactions.length; t++) { for (var t = 0; t < state.transactions.length; t++) {
var transaction = state.transactions[t]; var transaction = state.transactions[t];
if (transaction.functionId === c && transaction.contractId === c) if (transaction.functionId === c && transaction.contractId === c)
@ -223,13 +251,14 @@ Item {
ctorTr.contractId = c; ctorTr.contractId = c;
ctorTr.sender = state.accounts[0].secret; ctorTr.sender = state.accounts[0].secret;
state.transactions.push(ctorTr); state.transactions.push(ctorTr);
var item = state;//fromPlainStateItem(state); changed = true;
stateListModel.set(s, item); stateListModel.set(s, state);
stateList[s] = item; stateList[s] = state;
} }
} }
} }
save(); if (changed)
save();
} }
function addState() { function addState() {

Loading…
Cancel
Save