Add string list model
authorKevin Krammer <kevin.krammer@kdab.com>
Wed, 13 Mar 2013 14:18:17 +0000 (15:18 +0100)
committerKevin Krammer <kevin.krammer@kdab.com>
Wed, 13 Mar 2013 14:18:17 +0000 (15:18 +0100)
lib/declarativestringlistmodelextension.cpp [new file with mode: 0644]
lib/declarativestringlistmodelextension.h [new file with mode: 0644]
lib/declarativewidgetsdocument.cpp
lib/lib.pro

diff --git a/lib/declarativestringlistmodelextension.cpp b/lib/declarativestringlistmodelextension.cpp
new file mode 100644 (file)
index 0000000..e5b34e8
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+  Copyright (C) 2012-2013 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
+  Author: Kevin Krammer, kevin.krammer@kdab.com
+  Author: Tobias Koenig, tobias.koenig@kdab.com
+
+  This program is free software; you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation; either version 2 of the License, or
+  (at your option) any later version.
+
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License along
+  with this program; if not, write to the Free Software Foundation, Inc.,
+  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+*/
+
+#include "declarativestringlistmodelextension.h"
+
+#include <QStringListModel>
+
+DeclarativeStringListModelExtension::DeclarativeStringListModelExtension(QObject *parent)
+  : DeclarativeObjectExtension(parent)
+{
+}
+
+QStringListModel *DeclarativeStringListModelExtension::extendedModel() const
+{
+  QStringListModel *model = qobject_cast<QStringListModel*>(extendedObject());
+  Q_ASSERT(model);
+
+  return model;
+}
+
+void DeclarativeStringListModelExtension::setStringList(const QStringList &list)
+{
+  QStringListModel *model = extendedModel();
+  if (model->stringList() == list)
+    return;
+
+  model->setStringList(list);
+
+  emit stringListChanged(list);
+}
+
+QStringList DeclarativeStringListModelExtension::stringList() const
+{
+  return extendedModel()->stringList();
+}
diff --git a/lib/declarativestringlistmodelextension.h b/lib/declarativestringlistmodelextension.h
new file mode 100644 (file)
index 0000000..4ec0a0c
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+  Copyright (C) 2012-2013 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
+  Author: Kevin Krammer, kevin.krammer@kdab.com
+  Author: Tobias Koenig, tobias.koenig@kdab.com
+
+  This program is free software; you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation; either version 2 of the License, or
+  (at your option) any later version.
+
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License along
+  with this program; if not, write to the Free Software Foundation, Inc.,
+  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+*/
+
+#ifndef DECLARATIVESTRINGLISTMODELEXTENSION_H
+#define DECLARATIVESTRINGLISTMODELEXTENSION_H
+
+#include "declarativeobjectextension.h"
+
+#include <QStringList>
+
+class QStringListModel;
+
+class DeclarativeStringListModelExtension : public DeclarativeObjectExtension
+{
+  Q_OBJECT
+
+  // repeat property declarations, qmlRegisterExtendedType doesn't see the ones from base class
+  Q_PROPERTY(QDeclarativeListProperty<QObject> data READ data DESIGNABLE false)
+  Q_PROPERTY(QStringList stringList READ stringList WRITE setStringList NOTIFY stringListChanged)
+
+  Q_CLASSINFO("DefaultProperty", "data")
+
+  public:
+    explicit DeclarativeStringListModelExtension(QObject *parent = 0);
+
+    QStringListModel *extendedModel() const;
+
+    void setStringList(const QStringList &list);
+
+    QStringList stringList() const;
+
+  Q_SIGNALS:
+    void stringListChanged(const QStringList &stringList);
+};
+
+#endif // DECLARATIVESSTRINGLISTMODELEXTENSION_H
index 85b7d51..7529028 100644 (file)
@@ -44,6 +44,7 @@
 #include "declarativestackedlayout_p.h"
 #include "declarativestackedwidgetextension_p.h"
 #include "declarativestatusbar_p.h"
+#include "declarativestringlistmodelextension.h"
 #include "declarativetabwidget_p.h"
 #include "declarativetexteditextension_p.h"
 #include "declarativetoolbarextension_p.h"
@@ -77,6 +78,7 @@
 #include <QRadioButton>
 #include <QScrollBar>
 #include <QStackedWidget>
+#include <QStringListModel>
 #include <QTableView>
 #include <QTextBrowser>
 #include <QTimer>
@@ -117,6 +119,7 @@ DeclarativeWidgetsDocument::DeclarativeWidgetsDocument(const QUrl &url, QObject
   qmlRegisterExtendedType<QFileSystemModel, DeclarativeFileSystemModelExtension>("QtGui", 1, 0, "FileSystemModel");
   qmlRegisterType<QItemSelectionModel>();
   qmlRegisterExtendedType<DeclarativeSeparator, DeclarativeObjectExtension>("QtGui", 1, 0, "Separator");
+  qmlRegisterExtendedType<QStringListModel, DeclarativeStringListModelExtension>("QtCore", 1, 0, "StringListModel");
   qmlRegisterType<QTextDocument>();
   qmlRegisterType<QTimer>("QtCore", 1, 0, "Timer");
 
index 2978878..6a12502 100644 (file)
@@ -41,7 +41,8 @@ HEADERS = \
   objectadaptors_p.h \
   qmetaobjectbuilder_p.h \
   staticdialogmethodattached_p.h \
-    declarativefilesystemmodelextension.h
+  declarativefilesystemmodelextension.h \
+  declarativestringlistmodelextension.h
 
 SOURCES = \
   abstractdeclarativeobject.cpp \
@@ -79,4 +80,5 @@ SOURCES = \
   objectadaptors.cpp \
   qmetaobjectbuilder.cpp \
   staticdialogmethodattached.cpp \
-    declarativefilesystemmodelextension.cpp
+  declarativefilesystemmodelextension.cpp \
+  declarativestringlistmodelextension.cpp