Browse Source

- bug fix: sync issue when renaming a file.

- small changes
cl-refactor
yann300 10 years ago
parent
commit
6ce17df79a
  1. 5
      mix/FileIo.cpp
  2. 5
      mix/FileIo.h
  3. 31
      mix/qml/CodeEditorView.qml
  4. 12
      mix/qml/ProjectModel.qml
  5. 8
      mix/qml/WebPreview.qml
  6. 39
      mix/qml/js/ProjectModel.js
  7. 2
      mix/qml/main.qml

5
mix/FileIo.cpp

@ -41,6 +41,11 @@ using namespace dev;
using namespace dev::crypto;
using namespace dev::mix;
FileIo::FileIo(): m_watcher(new QFileSystemWatcher(this))
{
connect(m_watcher, &QFileSystemWatcher::fileChanged, this, &FileIo::fileChanged);
}
void FileIo::openFileBrowser(QString const& _dir)
{
QDesktopServices::openUrl(QUrl(_dir));

5
mix/FileIo.h

@ -22,10 +22,11 @@
#pragma once
#include <QFileSystemWatcher>
#include <libdevcore/CommonData.h>
#include <QObject>
class QFileSystemWatcher;
namespace dev
{
namespace mix
@ -44,7 +45,7 @@ signals:
void fileChanged(QString const& _filePath);
public:
FileIo(): m_watcher(new QFileSystemWatcher(this)) { connect(m_watcher, &QFileSystemWatcher::fileChanged, this, &FileIo::fileChanged); }
FileIo();
/// Create a directory if it does not exist. Signals on failure.
Q_INVOKABLE void makeDir(QString const& _url);
/// Read file contents to a string. Signals on failure.

31
mix/qml/CodeEditorView.qml

@ -149,9 +149,9 @@ Item {
for (var i = 0; i < editorListModel.count; i++)
{
var doc = editorListModel.get(i);
if (doc.path === document)
if (doc.path === document.path)
{
fileIo.writeFile(document, editors.itemAt(i).item.getText());
fileIo.writeFile(document.path, editors.itemAt(i).item.getText());
break;
}
}
@ -168,6 +168,7 @@ Item {
property variant doc
onYes: {
doLoadDocument(item, doc);
resetEditStatus(doc.documentId);
}
}
@ -209,14 +210,28 @@ Item {
onDocumentChanged: {
if (!item)
return;
if (currentDocumentId == documentId)
var current = editorListModel.get(index);
if (documentId === current.documentId)
{
messageDialog.item = loader.item;
messageDialog.doc = editorListModel.get(index);
messageDialog.open();
if (currentDocumentId === current.documentId)
{
messageDialog.item = loader.item;
messageDialog.doc = editorListModel.get(index);
messageDialog.open();
}
else
changed = true
}
else
changed = true;
}
onDocumentUpdated: {
var document = projectModel.getDocument(documentId);
for (var i = 0; i < editorListModel.count; i++)
if (editorListModel.get(i).documentId === documentId)
{
editorListModel.set(i, document);
break;
}
}
}

12
mix/qml/ProjectModel.qml

@ -13,7 +13,7 @@ Item {
signal projectLoading(var projectData)
signal projectLoaded()
signal documentSaving(var document)
signal documentChanged(var document)
signal documentChanged(var documentId)
signal documentOpened(var document)
signal documentRemoved(var documentId)
signal documentUpdated(var documentId) //renamed
@ -105,8 +105,8 @@ Item {
onFileChanged:
{
fileIo.watchFileChanged(_filePath);
if (_filePath.indexOf(currentDocumentId, _filePath.length - currentDocumentId.length))
documentChanged(_filePath);
var documentId = ProjectModelCode.getDocumentByPath(_filePath);
documentChanged(documentId);
}
}
@ -120,12 +120,14 @@ Item {
onYes: {
projectModel.saveAll();
ProjectModelCode.doCloseProject();
callBack();
if (callBack)
callBack();
}
onRejected: {}
onNo: {
ProjectModelCode.doCloseProject();
callBack();
if (callBack)
callBack();
}
}

8
mix/qml/WebPreview.qml

@ -98,7 +98,13 @@ Item {
}
onDocumentUpdated: {
updateDocument(documentId, function(i) { pageListModel.set(i, projectModel.getDocument(documentId)) } )
var document = projectModel.getDocument(documentId);
for (var i = 0; i < pageListModel.count; i++)
if (pageListModel.get(i).documentId === documentId)
{
pageListModel.set(i, document);
break;
}
}
onProjectLoading: {

39
mix/qml/js/ProjectModel.js

@ -27,8 +27,8 @@ var contractTemplate = "contract Contract {\n}\n";
function saveCurrentDocument()
{
documentSaving(projectPath + currentDocumentId);
var doc = projectListModel.get(getDocumentIndex(currentDocumentId));
documentSaving(doc);
if (doc.isContract)
contractSaved(currentDocumentId);
else
@ -53,12 +53,25 @@ function closeProject(callBack) {
else
{
doCloseProject();
callBack();
if (callBack)
callBack();
}
}
}
function saveProject() {
if (!isEmpty) {
var projectData = saveProjectFile();
if (projectData !== null)
{
projectSaving(projectData);
projectSaved();
}
}
}
function saveProjectFile()
{
if (!isEmpty) {
var projectData = {
files: [],
@ -72,12 +85,13 @@ function saveProject() {
};
for (var i = 0; i < projectListModel.count; i++)
projectData.files.push(projectListModel.get(i).fileName);
projectSaving(projectData);
var json = JSON.stringify(projectData, null, "\t");
var projectFile = projectPath + projectFileName;
fileIo.writeFile(projectFile, json);
projectSaved();
return projectData;
}
return null;
}
function loadProject(path) {
@ -147,6 +161,7 @@ function addFile(fileName) {
};
projectListModel.append(docData);
saveProjectFile();
fileIo.watchFileChanged(p);
return docData.documentId;
}
@ -160,6 +175,17 @@ function getDocumentIndex(documentId)
return -1;
}
function getDocumentByPath(_path)
{
for (var i = 0; i < projectListModel.count; i++)
{
var doc = projectListModel.get(i);
if (doc.path.indexOf(_path) !== -1)
return doc.documentId;
}
return null;
}
function openDocument(documentId) {
if (documentId !== currentDocumentId) {
documentOpened(projectListModel.get(getDocumentIndex(documentId)));
@ -243,12 +269,16 @@ function renameDocument(documentId, newName) {
var i = getDocumentIndex(documentId);
var document = projectListModel.get(i);
if (!document.isContract) {
fileIo.stopWatching(document.path);
var sourcePath = document.path;
var destPath = projectPath + newName;
fileIo.moveFile(sourcePath, destPath);
document.path = destPath;
document.name = newName;
document.fileName = newName;
projectListModel.set(i, document);
fileIo.watchFileChanged(destPath);
saveProjectFile();
documentUpdated(documentId);
}
}
@ -263,7 +293,6 @@ function removeDocument(documentId) {
var document = projectListModel.get(i);
if (!document.isContract) {
projectListModel.remove(i);
fileIo.stopWatching(projectPath+ documentId);
documentRemoved(documentId);
}
}

2
mix/qml/main.qml

@ -302,7 +302,7 @@ ApplicationWindow {
Action {
id: saveAllFilesAction
text: qsTr("Save All")
shortcut: "Ctrl+A"
shortcut: "Ctrl+Shift+A"
enabled: !projectModel.isEmpty
onTriggered: projectModel.saveAll();
}

Loading…
Cancel
Save