Skip to content

Commit

Permalink
add FileIO interface for QML plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
lasconic committed Jul 15, 2012
1 parent a419129 commit 53f6f8d
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 10 deletions.
61 changes: 60 additions & 1 deletion mscore/plugins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ QDeclarativeEngine* MuseScore::qml()
//-----------some qt bindings
_qml = new QDeclarativeEngine;
qmlRegisterType<MsProcess> ("MuseScore", 1, 0, "QProcess");
qmlRegisterType<MsFile> ("MuseScore", 1, 0, "QFile");
qmlRegisterType<FileIO, 1> ("FileIO", 1, 0, "FileIO");
//-----------mscore bindings
qmlRegisterType<MScore> ("MuseScore", 1, 0, "MScore");
qmlRegisterType<MsScoreView>("MuseScore", 1, 0, "ScoreView");
Expand Down Expand Up @@ -444,6 +444,65 @@ MsScoreView::MsScoreView(QDeclarativeItem* parent)
score = 0;
}

//---------------------------------------------------------
// @@ FileIO
//---------------------------------------------------------

FileIO::FileIO(QObject *parent) :
QObject(parent)
{

}

QString FileIO::read()
{
if (mSource.isEmpty()) {
emit error("source is empty");
return QString();
}

QFile file(mSource);
QString fileContent;
if ( file.open(QIODevice::ReadOnly) ) {
QString line;
QTextStream t( &file );
do {
line = t.readLine();
fileContent += line;
} while (!line.isNull());
file.close();
}
else {
emit error("Unable to open the file");
return QString();
}
return fileContent;
}

bool FileIO::write(const QString& data)
{
if (mSource.isEmpty())
return false;

QFile file(mSource);
if (!file.open(QFile::WriteOnly | QFile::Truncate))
return false;

QTextStream out(&file);
out << data;
file.close();
return true;
}

bool FileIO::remove(const QString& data)
{
if (mSource.isEmpty())
return false;

QFile file(mSource);
return file.remove();
}

//---------------------------------------------------------
// setScore
//---------------------------------------------------------
Expand Down
34 changes: 25 additions & 9 deletions mscore/plugins.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,35 @@
#include "libmscore/score.h"
#include "libmscore/utils.h"

//---------------------------------------------------------
// MsFile
// @@ QFile
//---------------------------------------------------------

class MsFile : public QFile {
class FileIO : public QObject {
Q_OBJECT

public:
MsFile() : QFile() {}
Q_PROPERTY(QString source
READ source
WRITE setSource
NOTIFY sourceChanged)
explicit FileIO(QObject *parent = 0);

Q_INVOKABLE QString read();
Q_INVOKABLE bool write(const QString& data);
Q_INVOKABLE bool remove(const QString& data);
Q_INVOKABLE QString tempPath() {QDir dir; return dir.tempPath();};

QString source() { return mSource; };

public slots:
void setSource(const QString& source) { mSource = source; };

signals:
void sourceChanged(const QString& source);
void error(const QString& msg);

private:
QString mSource;
};


//---------------------------------------------------------
// MsProcess
// @@ QProcess
Expand Down Expand Up @@ -168,7 +185,6 @@ class QmlPlugin : public QDeclarativeItem {
Q_INVOKABLE Element* newElement(int);
Q_INVOKABLE void cmd(const QString&);
Q_INVOKABLE MsProcess* newQProcess() { return new MsProcess(this); }
Q_INVOKABLE MsFile* newQFile() { return new MsFile(); }
Q_INVOKABLE bool writeScore(Score*, const QString& name, const QString& ext);
Q_INVOKABLE Score* readScore(const QString& name);
};
Expand Down

0 comments on commit 53f6f8d

Please sign in to comment.