Browse Source

Additional commands for project model

cl-refactor
arkpar 10 years ago
parent
commit
2ae6a42e25
  1. 2
      mix/ContractCallDataEncoder.cpp
  2. 15
      mix/FileIo.cpp
  3. 6
      mix/FileIo.h
  4. 3
      mix/qml/NewProjectDialog.qml
  5. 65
      mix/qml/ProjectList.qml
  6. 9
      mix/qml/ProjectModel.qml
  7. 88
      mix/qml/js/ProjectModel.js
  8. 13
      mix/qml/main.qml

2
mix/ContractCallDataEncoder.cpp

@ -92,7 +92,7 @@ int ContractCallDataEncoder::padding(QString type)
else if (type.indexOf("bool") != -1)
return 1;
else if ((type.indexOf("address") != -1))
return 20;
return 32;
else
return 0;
}

15
mix/FileIo.cpp

@ -78,3 +78,18 @@ QString FileIo::getHomePath() const
{
return QDir::homePath();
}
void FileIo::moveFile(QString const& _sourceUrl, QString const& _destUrl)
{
QUrl sourceUrl(_sourceUrl);
QUrl destUrl(_destUrl);
if (!QFile::rename(sourceUrl.path(), destUrl.path()))
error(tr("Error moving file %1 to %2").arg(_sourceUrl).arg(_destUrl));
}
bool FileIo::fileExists(QString const& _url)
{
QUrl url(_url);
QFile file(url.path());
return file.exists();
}

6
mix/FileIo.h

@ -33,7 +33,7 @@ namespace mix
class FileIo : public QObject
{
Q_OBJECT
Q_PROPERTY(QString homePath READ getHomePath CONSTANT);
Q_PROPERTY(QString homePath READ getHomePath CONSTANT)
signals:
/// Signalled in case of IO error
@ -48,6 +48,10 @@ public:
Q_INVOKABLE void writeFile(QString const& _url, QString const& _data);
/// Copy a file from _sourcePath to _destPath. Signals on failure.
Q_INVOKABLE void copyFile(QString const& _sourceUrl, QString const& _destUrl);
/// Move (rename) a file from _sourcePath to _destPath. Signals on failure.
Q_INVOKABLE void moveFile(QString const& _sourceUrl, QString const& _destUrl);
/// Check if file exists
Q_INVOKABLE bool fileExists(QString const& _url);
private:
QString getHomePath() const;

3
mix/qml/NewProjectDialog.qml

@ -9,7 +9,7 @@ Window {
modality: Qt.WindowModal
width: 640
height: 280
height: 120
visible: false
@ -89,4 +89,5 @@ Window {
pathField.text = u;
}
}
Component.onCompleted: pathField.text = fileIo.homePath
}

65
mix/qml/ProjectList.qml

@ -5,7 +5,7 @@ import QtQuick.Controls 1.0
import org.ethereum.qml.ProjectModel 1.0
Item {
property bool renameMode: false;
ColumnLayout {
anchors.fill: parent
Text {
@ -16,9 +16,10 @@ Item {
visible: !ProjectModel.isEmpty;
}
ListView {
id: projectList
Layout.fillWidth: true
Layout.fillHeight: true
id: projectList
model: ProjectModel.listModel
delegate: renderDelegate
@ -34,6 +35,21 @@ Item {
ProjectModel.openDocument(ProjectModel.listModel.get(currentIndex).documentId);
}
}
Menu {
id: contextMenu
MenuItem {
text: qsTr("Rename")
onTriggered: {
renameMode = true;
}
}
MenuItem {
text: qsTr("Delete")
onTriggered: {
ProjectModel.removeDocument(projectList.model.get(projectList.currentIndex).documentId);
}
}
}
}
Component {
id: renderDelegate
@ -43,7 +59,9 @@ Item {
width: parent.width
RowLayout {
anchors.fill: parent
visible: !(index === projectList.currentIndex) || !renameMode
Text {
id: nameText
Layout.fillWidth: true
Layout.fillHeight: true
text: name
@ -51,17 +69,56 @@ Item {
verticalAlignment: Text.AlignBottom
}
}
TextInput {
id: textInput
text: nameText.text
visible: (index === projectList.currentIndex) && renameMode
MouseArea {
id: textMouseArea
anchors.fill: parent
hoverEnabled: true
z:2
onClicked: {
console.log("clicked");
textInput.forceActiveFocus();
}
}
onVisibleChanged: {
if (visible) {
selectAll();
forceActiveFocus();
}
}
onAccepted: close(true);
onCursorVisibleChanged: {
if (!cursorVisible)
close(false);
}
onFocusChanged: {
if (!focus)
close(false);
}
function close(accept) {
renameMode = false;
if (accept)
ProjectModel.renameDocument(projectList.model.get(projectList.currentIndex).documentId, textInput.text);
}
}
MouseArea {
id: mouseArea
z: 1
hoverEnabled: false
anchors.fill: parent
acceptedButtons: Qt.LeftButton | Qt.RightButton
onClicked:{
projectList.currentIndex = index;
if (mouse.button === Qt.RightButton && !projectList.model.get(index).isContract)
contextMenu.popup();
}
}
Connections {
target: ProjectModel
onProjectLoaded: {

9
mix/qml/ProjectModel.qml

@ -15,6 +15,8 @@ Item {
signal projectClosed
signal projectLoaded
signal documentOpened(var document)
signal documentRemoved(var documentId)
signal documentUpdated(var documentId) //renamed
signal projectSaving(var projectData)
property bool isEmpty: (projectData === null)
@ -33,7 +35,12 @@ Item {
function saveProject() { ProjectModelCode.saveProject(); }
function loadProject(path) { ProjectModelCode.loadProject(path); }
function addExistingFile() { ProjectModelCode.addExistingFile(); }
function newHtmlFile() { ProjectModelCode.newHtmlFile(); }
function newJsFile() { ProjectModelCode.newJsFile(); }
//function newContract() { ProjectModelCode.newContract(); }
function openDocument(documentId) { ProjectModelCode.openDocument(documentId); }
function renameDocument(documentId, newName) { ProjectModelCode.renameDocument(documentId, newName); }
function removeDocument(documentId) { ProjectModelCode.removeDocument(documentId); }
Connections {
target: appContext
@ -95,7 +102,7 @@ Item {
title: qsTr("Add a file")
selectFolder: false
onAccepted: {
var paths = openProjectFileDialog.fileUrls;
var paths = addExistingFileDialog.fileUrls;
ProjectModelCode.doAddExistingFiles(paths);
}
}

88
mix/qml/js/ProjectModel.js

@ -72,7 +72,7 @@ function loadProject(path) {
}
function addExistingFile() {
addExistingFileDialog().open();
addExistingFileDialog.open();
}
function addProjectFiles(files) {
@ -82,22 +82,31 @@ function addProjectFiles(files) {
function addFile(fileName) {
var p = projectPath + fileName;
var extension = fileName.substring(fileName.length - 4, fileName.length);
var isContract = extension === ".sol";
var fileData = {
contract: false,
path: p,
name: fileName,
name: isContract ? "Contract" : fileName,
documentId: fileName,
isText: true,
isContract: fileName.substring(fileName.length - 4, fileName.length) === ".sol",
isText: isContract || extension === ".html" || extension === ".js",
isContract: fileData,
};
projectListModel.append(fileData);
}
function openDocument(documentId) {
function findDocument(documentId)
{
for (var i = 0; i < projectListModel.count; i++)
if (projectListModel.get(i).documentId === documentId)
documentOpened(projectListModel.get(i));
return i;
console.error("Cant find document " + documentId);
return -1;
}
function openDocument(documentId) {
documentOpened(projectListModel.get(findDocument(documentId)));
}
function doCloseProject() {
@ -117,15 +126,15 @@ function doCreateProject(title, path) {
fileIo.makeDir(dirPath);
var projectFile = dirPath + projectFileName;
var indexFile = dirPath + "index.html";
var contractsFile = dirPath + "contracts.sol";
var indexFile = "index.html";
var contractsFile = "contracts.sol";
var projectData = {
title: title,
files: [ "contracts.sol", "index.html" ]
files: [ indexFile, contractsFile ]
};
fileIo.writeFile(indexFile, "<html></html>");
fileIo.writeFile(contractsFile, "contract MyContract {}");
//TODO: copy from template
fileIo.writeFile(dirPath + indexFile, "<html></html>");
fileIo.writeFile(dirPath + contractsFile, "contract MyContract {\n }\n");
var json = JSON.stringify(projectData);
fileIo.writeFile(projectFile, json);
loadProject(dirPath);
@ -134,9 +143,62 @@ function doCreateProject(title, path) {
function doAddExistingFiles(files) {
for(var i = 0; i < files.length; i++) {
var sourcePath = files[i];
console.log(sourcePath);
var sourceFileName = sourcePath.substring(sourcePath.lastIndexOf("/") + 1, sourcePath.length);
console.log(sourceFileName);
var destPath = projectPath + sourceFileName;
fileIo.copyFile(sourcePath, destPath);
console.log(destPath);
if (sourcePath !== destPath)
fileIo.copyFile(sourcePath, destPath);
addFile(sourceFileName);
}
}
function renameDocument(documentId, newName) {
var i = findDocument(documentId);
var document = projectListModel.get(i);
if (!document.isContract) {
var sourcePath = document.path;
var destPath = projectPath + newName;
fileIo.moveFile(sourcePath, destPath);
document.path = destPath;
document.name = newName;
projectListModel.set(i, document);
documentUpdated(documentId);
}
}
function removeDocument(documentId) {
var i = findDocument(documentId);
var document = projectListModel.get(i);
if (!document.isContract) {
projectListModel.remove(i);
documentUpdated(documentId);
}
}
function newHtmlFile() {
createAndAddFile("page", "html", "<html>\n</html>");
}
function newJsFile() {
createAndAddFile("script", "js", "function foo() {\n}\n");
}
function createAndAddFile(name, extension, content) {
var fileName = generateFileName(name, extension);
var filePath = projectPath + fileName;
fileIo.writeFile(filePath, content);
addFile(fileName);
}
function generateFileName(name, extension) {
var i = 1;
do {
var fileName = name + i + "." + extension;
var filePath = projectPath + fileName;
i++;
} while (fileIo.fileExists(filePath));
return fileName
}

13
mix/qml/main.qml

@ -21,11 +21,16 @@ ApplicationWindow {
title: qsTr("File")
MenuItem { action: createProjectAction }
MenuItem { action: openProjectAction }
MenuSeparator {}
MenuItem { action: saveAllFilesAction }
MenuSeparator {}
MenuItem { action: addExistingFileAction }
MenuItem { action: addNewJsFileAction }
MenuItem { action: addNewHtmlFileAction }
MenuItem { action: addNewContractAction }
MenuSeparator {}
//MenuItem { action: addNewContractAction }
MenuItem { action: closeProjectAction }
MenuSeparator {}
MenuItem { action: exitAppAction }
}
Menu {
@ -95,7 +100,7 @@ ApplicationWindow {
text: qsTr("New JavaScript file")
shortcut: "Ctrl+Alt+J"
enabled: !ProjectModel.isEmpty
onTriggered: ProjectModel.addJsFile();
onTriggered: ProjectModel.newJsFile();
}
Action {
@ -103,7 +108,7 @@ ApplicationWindow {
text: qsTr("New HTML file")
shortcut: "Ctrl+Alt+H"
enabled: !ProjectModel.isEmpty
onTriggered: ProjectModel.addHtmlFile();
onTriggered: ProjectModel.newHtmlFile();
}
Action {
@ -111,7 +116,7 @@ ApplicationWindow {
text: qsTr("New contract")
shortcut: "Ctrl+Alt+C"
enabled: !ProjectModel.isEmpty
onTriggered: ProjectModel.addContract();
onTriggered: ProjectModel.newContract();
}
Action {

Loading…
Cancel
Save