Update gettingStartedQml example code to use standard Qt coding style
authorhjk <qthjk@ovi.com>
Fri, 16 Nov 2012 11:15:18 +0000 (12:15 +0100)
committerThe Qt Project <gerrit-noreply@qt-project.org>
Fri, 16 Nov 2012 15:22:16 +0000 (16:22 +0100)
Change-Id: I225ea53a4e06e92367feb368dd6deea6f9acf403
Reviewed-by: Jerome Pasion <jerome.pasion@digia.com>
examples/tutorials/gettingStartedQml/filedialog/dialogPlugin.h
examples/tutorials/gettingStartedQml/filedialog/directory.cpp
examples/tutorials/gettingStartedQml/filedialog/directory.h
examples/tutorials/gettingStartedQml/filedialog/file.cpp
examples/tutorials/gettingStartedQml/filedialog/file.h

index f5fcae0..05e0768 100644 (file)
@@ -48,10 +48,9 @@ class DialogPlugin : public QQmlExtensionPlugin
     Q_OBJECT
     Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QQmlExtensionInterface")
 
-    public:
-        //registerTypes is inherited from QQmlExtensionPlugin
-        void registerTypes(const char *uri);
-
+public:
+    // registerTypes is inherited from QQmlExtensionPlugin
+    void registerTypes(const char *uri);
 };
 
 #endif
index a07ec63..38a47f0 100644 (file)
@@ -48,11 +48,11 @@ Initialize the saves directory and creates the file list
 */
 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);
     }
@@ -72,7 +72,7 @@ int Directory::filesCount() const
 /*
 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);
@@ -82,7 +82,7 @@ void appendFiles(QQmlListProperty<File> * property, File * 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);
 }
@@ -90,7 +90,7 @@ File* fileAt(QQmlListProperty<File> * property, int 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();
 }
@@ -109,7 +109,7 @@ Returns the list of files as a QQmlListProperty.
 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);
 }
 
 /*
@@ -133,7 +133,7 @@ Set the file name of the current file
 */
 void Directory::setFilename(const QString &str)
 {
-    if( str != currentFile.name() ) {
+    if (str != currentFile.name()) {
         currentFile.setName(str);
         emit filenameChanged();
     }
@@ -144,7 +144,7 @@ Set the content of the file as a string
 */
 void Directory::setFileContent(const QString &str)
 {
-    if(str != m_fileContent){
+    if (str != m_fileContent) {
         m_fileContent = str;
         emit fileContentChanged();
     }
@@ -156,16 +156,16 @@ Saving makes sure that the file has a .txt extension.
 */
 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;
     }
@@ -182,19 +182,14 @@ void Directory::loadFile()
 {
     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();
 }
@@ -208,15 +203,14 @@ void Directory::refresh()
     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);
index 7bb41ef..90c427c 100644 (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
index dc91212..b53c569 100644 (file)
@@ -45,12 +45,14 @@ File::File(QObject *parent) : QObject(parent)
 {
 }
 
-QString File::name() const{
+QString File::name() const
+{
     return m_name;
 }
 
-void File::setName(const QString &str){
-    if(str != m_name){
+void File::setName(const QString &str)
+{
+    if (str != m_name) {
         m_name = str;
         emit nameChanged();
     }
index cd12e6d..7a0da05 100644 (file)
 #ifndef FILE_H
 #define FILE_H
 
-
-#include <QString>
 #include <QObject>
+#include <QString>
 
-class File : public QObject{
-
+class File : public QObject
+{
     Q_OBJECT
 
     Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
 
-    public:
-        File(QObject *parent = 0);
+public:
+    File(QObject *parent = 0);
 
-        QString name() const;
-        void setName(const QString &str);
+    QString name() const;
+    void setName(const QString &str);
 
-    signals:
-        void nameChanged();
+signals:
+    void nameChanged();
 
-    private:
-        QString m_name;
+private:
+    QString m_name;
 };
 
-#endif
\ No newline at end of file
+#endif