*/
Directory::Directory(QObject *parent) : QObject(parent)
{
- m_dir.cd( QDir::currentPath() );
+ m_dir.cd(QDir::currentPath());
- //go to the saved directory. if not found, create save directory
+ // go to the saved directory. if not found, create save directory
m_saveDir = "saves";
- if ( m_dir.cd(m_saveDir) == 0 ) {
+ if (m_dir.cd(m_saveDir) == 0) {
m_dir.mkdir(m_saveDir);
m_dir.cd(m_saveDir);
}
/*
Function called to append data onto list property
*/
-void appendFiles(QQmlListProperty<File> * property, File * file)
+void appendFiles(QQmlListProperty<File> *property, File *file)
{
Q_UNUSED(property);
Q_UNUSED(file);
/*
Function called to retrieve file in the list using an index
*/
-File* fileAt(QQmlListProperty<File> * property, int index)
+File *fileAt(QQmlListProperty<File> *property, int index)
{
return static_cast< QList<File *> *>(property->data)->at(index);
}
/*
Returns the number of files in the list
*/
-int filesSize(QQmlListProperty<File> * property)
+int filesSize(QQmlListProperty<File> *property)
{
return static_cast< QList<File *> *>(property->data)->size();
}
QQmlListProperty<File> Directory::files()
{
refresh();
- return QQmlListProperty<File>( this, &m_fileList, &appendFiles, &filesSize, &fileAt, &clearFilesPtr );
+ return QQmlListProperty<File>(this, &m_fileList, &appendFiles, &filesSize, &fileAt, &clearFilesPtr);
}
/*
*/
void Directory::setFilename(const QString &str)
{
- if( str != currentFile.name() ) {
+ if (str != currentFile.name()) {
currentFile.setName(str);
emit filenameChanged();
}
*/
void Directory::setFileContent(const QString &str)
{
- if(str != m_fileContent){
+ if (str != m_fileContent) {
m_fileContent = str;
emit fileContentChanged();
}
*/
void Directory::saveFile()
{
- if(currentFile.name().size() == 0){
+ if (currentFile.name().isEmpty()) {
qWarning()<< "Empty filename. no save";
return;
}
QString extendedName = currentFile.name();
- if(!currentFile.name().endsWith(".txt")){
+ if (!currentFile.name().endsWith(".txt")) {
extendedName.append(".txt");
}
- QFile file( m_dir.filePath(extendedName) );
- if ( file.open(QFile::WriteOnly | QFile::Truncate) ) {
+ QFile file(m_dir.filePath(extendedName));
+ if (file.open(QFile::WriteOnly | QFile::Truncate)) {
QTextStream outStream(&file);
outStream << m_fileContent;
}
{
m_fileContent.clear();
QString extendedName = currentFile.name();
- if( !currentFile.name().endsWith(".txt") ) {
+ if (!currentFile.name().endsWith(".txt")) {
extendedName.append(".txt");
}
- QFile file( m_dir.filePath(extendedName) );
- if ( file.open(QFile::ReadOnly ) ) {
+ QFile file(m_dir.filePath(extendedName));
+ if (file.open(QFile::ReadOnly)) {
QTextStream inStream(&file);
-
- QString line;
- do {
- line = inStream.read(75);
- m_fileContent.append(line);
- } while ( !line.isNull() ) ;
+ m_fileContent = inStream.readAll();
}
file.close();
}
m_dirFiles = m_dir.entryList(m_filterList,QDir::Files,QDir::Name);
m_fileList.clear();
- File * file;
- for(int i = 0; i < m_dirFiles.size() ; i ++) {
+ File *file;
+ for (int i = 0; i < m_dirFiles.size(); ++i) {
file = new File();
- if( m_dirFiles.at(i).endsWith(".txt") ) {
+ if (m_dirFiles.at(i).endsWith(".txt")) {
QString name = m_dirFiles.at(i);
- file->setName( name.remove(".txt",Qt::CaseSensitive) );
- }
- else {
+ file->setName(name.remove(".txt",Qt::CaseSensitive));
+ } else {
file->setName(m_dirFiles.at(i));
}
m_fileList.append(file);
#include "file.h"
#include <QDir>
+#include <QObject>
+#include <QQmlListProperty>
#include <QStringList>
#include <QTextStream>
-#include <QQmlListProperty>
-#include <QObject>
-
-class Directory : public QObject {
+class Directory : public QObject
+{
Q_OBJECT
- //number of files in the directory
+ // number of files in the directory
Q_PROPERTY(int filesCount READ filesCount)
- //list property containing file names as QString
- Q_PROPERTY(QQmlListProperty<File> files READ files CONSTANT )
+ // list property containing file names as QString
+ Q_PROPERTY(QQmlListProperty<File> files READ files CONSTANT)
- //file name of the text file to read/write
+ // file name of the text file to read/write
Q_PROPERTY(QString filename READ filename WRITE setFilename NOTIFY filenameChanged)
- //text content of the file
+ // text content of the file
Q_PROPERTY(QString fileContent READ fileContent WRITE setFileContent NOTIFY fileContentChanged)
- public:
- Directory(QObject *parent = 0);
+public:
+ Directory(QObject *parent = 0);
- //properties' read functions
- int filesCount() const;
- QString filename() const;
- QString fileContent() const;
- QQmlListProperty<File> files();
+ // properties' read functions
+ int filesCount() const;
+ QString filename() const;
+ QString fileContent() const;
+ QQmlListProperty<File> files();
- //properties' write functions
- void setFilename(const QString &str);
- void setFileContent(const QString &str);
+ // properties' write functions
+ void setFilename(const QString &str);
+ void setFileContent(const QString &str);
- //accessible from QML
- Q_INVOKABLE void saveFile();
- Q_INVOKABLE void loadFile();
+ // accessible from QML
+ Q_INVOKABLE void saveFile();
+ Q_INVOKABLE void loadFile();
- signals:
- void directoryChanged();
- void filenameChanged();
- void fileContentChanged();
+signals:
+ void directoryChanged();
+ void filenameChanged();
+ void fileContentChanged();
- private:
- QDir m_dir;
- QStringList m_dirFiles;
- File currentFile;
- QString m_saveDir;
- QStringList m_filterList;
+private:
+ QDir m_dir;
+ QStringList m_dirFiles;
+ File currentFile;
+ QString m_saveDir;
+ QStringList m_filterList;
- //contains the file data in QString format
- QString m_fileContent;
+ //contains the file data in QString format
+ QString m_fileContent;
- //Registered to QML in a plugin. Accessible from QML as a property of Directory
- QList<File *> m_fileList;
+ //Registered to QML in a plugin. Accessible from QML as a property of Directory
+ QList<File *> m_fileList;
- //refresh content of the directory
- void refresh();
+ //refresh content of the directory
+ void refresh();
};
#endif