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::crypto;
using namespace dev::mix; 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) void FileIo::openFileBrowser(QString const& _dir)
{ {
QDesktopServices::openUrl(QUrl(_dir)); QDesktopServices::openUrl(QUrl(_dir));

5
mix/FileIo.h

@ -22,10 +22,11 @@
#pragma once #pragma once
#include <QFileSystemWatcher>
#include <libdevcore/CommonData.h> #include <libdevcore/CommonData.h>
#include <QObject> #include <QObject>
class QFileSystemWatcher;
namespace dev namespace dev
{ {
namespace mix namespace mix
@ -44,7 +45,7 @@ signals:
void fileChanged(QString const& _filePath); void fileChanged(QString const& _filePath);
public: 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. /// Create a directory if it does not exist. Signals on failure.
Q_INVOKABLE void makeDir(QString const& _url); Q_INVOKABLE void makeDir(QString const& _url);
/// Read file contents to a string. Signals on failure. /// 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++) for (var i = 0; i < editorListModel.count; i++)
{ {
var doc = editorListModel.get(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; break;
} }
} }
@ -168,6 +168,7 @@ Item {
property variant doc property variant doc
onYes: { onYes: {
doLoadDocument(item, doc); doLoadDocument(item, doc);
resetEditStatus(doc.documentId);
} }
} }
@ -209,14 +210,28 @@ Item {
onDocumentChanged: { onDocumentChanged: {
if (!item) if (!item)
return; return;
if (currentDocumentId == documentId) var current = editorListModel.get(index);
if (documentId === current.documentId)
{ {
messageDialog.item = loader.item; if (currentDocumentId === current.documentId)
messageDialog.doc = editorListModel.get(index); {
messageDialog.open(); 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 projectLoading(var projectData)
signal projectLoaded() signal projectLoaded()
signal documentSaving(var document) signal documentSaving(var document)
signal documentChanged(var document) signal documentChanged(var documentId)
signal documentOpened(var document) signal documentOpened(var document)
signal documentRemoved(var documentId) signal documentRemoved(var documentId)
signal documentUpdated(var documentId) //renamed signal documentUpdated(var documentId) //renamed
@ -105,8 +105,8 @@ Item {
onFileChanged: onFileChanged:
{ {
fileIo.watchFileChanged(_filePath); fileIo.watchFileChanged(_filePath);
if (_filePath.indexOf(currentDocumentId, _filePath.length - currentDocumentId.length)) var documentId = ProjectModelCode.getDocumentByPath(_filePath);
documentChanged(_filePath); documentChanged(documentId);
} }
} }
@ -120,12 +120,14 @@ Item {
onYes: { onYes: {
projectModel.saveAll(); projectModel.saveAll();
ProjectModelCode.doCloseProject(); ProjectModelCode.doCloseProject();
callBack(); if (callBack)
callBack();
} }
onRejected: {} onRejected: {}
onNo: { onNo: {
ProjectModelCode.doCloseProject(); ProjectModelCode.doCloseProject();
callBack(); if (callBack)
callBack();
} }
} }

8
mix/qml/WebPreview.qml

@ -98,7 +98,13 @@ Item {
} }
onDocumentUpdated: { 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: { onProjectLoading: {

39
mix/qml/js/ProjectModel.js

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

2
mix/qml/main.qml

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

Loading…
Cancel
Save