[Kst] branches/work/kst/portto4/kst/src/libkstapp

Mike Fenton mike at staikos.net
Fri Dec 14 20:13:51 CET 2007


SVN commit 748543 by fenton:

Redo StringModel to include strings outputted by DataObjects.


 M  +0 -2      libkstapp.pro  
 M  +85 -64    stringmodel.cpp  
 M  +13 -6     stringmodel.h  
 D             stringtablemodel.cpp  
 D             stringtablemodel.h  
 M  +3 -12     viewprimitivedialog.cpp  


--- branches/work/kst/portto4/kst/src/libkstapp/libkstapp.pro #748542:748543
@@ -86,7 +86,6 @@
     selectionrect.cpp \
     sessionmodel.cpp \
     stringmodel.cpp \
-    stringtablemodel.cpp \
     stroketab.cpp \
     svgitem.cpp \
     tabwidget.cpp \
@@ -170,7 +169,6 @@
     selectionrect.h \
     sessionmodel.h \
     stringmodel.h \
-    stringtablemodel.h \
     svgitem.h \
     stroketab.h \
     tabwidget.h \
--- branches/work/kst/portto4/kst/src/libkstapp/stringmodel.cpp #748542:748543
@@ -12,13 +12,19 @@
 #include "stringmodel.h"
 
 #include <assert.h>
-#include <QFont>
+#include <objectstore.h>
+#include <dataobject.h>
+#include <datavector.h>
+#include <generatedvector.h>
+#include <datamatrix.h>
+#include <generatedmatrix.h>
+#include <string_kst.h>
 
 namespace Kst {
 
-StringModel::StringModel(String *string)
-: QAbstractItemModel(), _string(string) {
-  assert(string);
+StringModel::StringModel(ObjectStore *store)
+: QAbstractItemModel(), _store(store) {
+  generateObjectList();
 }
 
 
@@ -28,98 +34,113 @@
 
 int StringModel::columnCount(const QModelIndex& parent) const {
   Q_UNUSED(parent)
-  return 1;
+  return 2;
 }
 
 
+void StringModel::generateObjectList() {
+  ObjectList<DataObject> dol = _store->getObjects<DataObject>();
+  ObjectList<String> sol = _store->getObjects<String>();
+
+  foreach(DataObject* dataObject, dol) {
+    foreach(StringPtr string, dataObject->outputStrings()) {
+      _objectList.append(string);
+    }
+  }
+
+  foreach(String* string, sol) {
+    if (string->orphan()) {
+      _objectList.append(string);
+    }
+  }
+}
+
 int StringModel::rowCount(const QModelIndex& parent) const {
-  Q_UNUSED(parent)
-  return 1;
+  int rc = 0;
+  if (!parent.isValid()) {
+    rc = _objectList.count();
+  }
+  return rc;
 }
 
 
 QVariant StringModel::data(const QModelIndex& index, int role) const {
+  if (!index.isValid()) {
+    return QVariant();
+  }
+
+  if (role != Qt::DisplayRole) {
+    return QVariant();
+  }
+
+  const int row = index.row();
+  if (row < _objectList.count()) {
+    if (StringPtr p = kst_cast<String>(_objectList.at(row))) {
+      return stringData(p, index);
+    }
+  }
+
+  return QVariant();
+}
+
+
+QVariant StringModel::stringData(StringPtr string, const QModelIndex& index) const {
   QVariant rc;
-  if (index.isValid() && _string) {
-    switch (role) {
-      case Qt::DisplayRole:
-        if (index.column() == 0) {
-          rc = QVariant(_string->tag().displayString());
-        } else {
-          rc = QVariant(_string->value());
-        }
-        break;
-      case Qt::FontRole:
-        {
-          if (_string->editable()) {
-            QFont f;
-            f.setBold(true);
-            rc = f;
-          }
-        }
-        break;
-      default:
-        break;
+
+  if (string) {
+    if (index.column() == Name) {
+      string->readLock();
+      rc.setValue(string->tag().name());
+      string->unlock();
+    } else if (index.column() == Value) {
+      string->readLock();
+      rc = QVariant(string->value());
+      string->unlock();
     }
   }
+
   return rc;
 }
 
 
 QModelIndex StringModel::index(int row, int col, const QModelIndex& parent) const {
-  Q_UNUSED(parent)
-  Q_UNUSED(col)
-  if (_string) {
-    return createIndex(row, 1);
+  if (row < 0 || col < 0 || col > 1) {
+    return QModelIndex();
   }
+
+  const int count = _objectList.count();
+  ObjectPtr object = 0;
+  if (!parent.isValid()) {
+    if (row < count) {
+      return createIndex(row, col);
+    }
+  }
+
   return QModelIndex();
 }
 
 
 QModelIndex StringModel::parent(const QModelIndex& index) const {
-  Q_UNUSED(index)
+  Q_UNUSED(index);
   return QModelIndex();
 }
 
 
 QVariant StringModel::headerData(int section, Qt::Orientation orientation, int role) const {
-  if (!_string || role != Qt::DisplayRole || section != 0) {
+  if (role != Qt::DisplayRole) {
     return QAbstractItemModel::headerData(section, orientation, role);
   }
-  return _string->tag().displayString();
-}
-
-
-Qt::ItemFlags StringModel::flags(const QModelIndex& index) const {
-  Qt::ItemFlags f = QAbstractItemModel::flags(index);
-  if (!_string || !index.isValid()) {
-    return f;
+  switch (section) {
+    case Name:
+      return tr("Name");
+    case Value:
+      return tr("Value");
+    default:
+      break;
   }
-
-  if (_string->editable() && index.row() >= 0) {
-    f |= Qt::ItemIsEditable;
-  }
-
-  return f;
+  return QVariant();
 }
 
-
-bool StringModel::setData(const QModelIndex& index, const QVariant& value, int role) {
-  if (role != Qt::EditRole) {
-    return QAbstractItemModel::setData(index, value, role);
-  }
-
-  if (!_string || !index.isValid() || !_string->editable() || index.row() < 0) {
-    return false;
-  }
-
-  QString stringValue = value.toString();
-  qDebug() << "UGLY!! Add setData API to String!";
-  _string->setValue(stringValue);
-  return true;
 }
 
-
-}
-
 // vim: ts=2 sw=2 et
--- branches/work/kst/portto4/kst/src/libkstapp/stringmodel.h #748542:748543
@@ -13,15 +13,20 @@
 #define STRINGMODEL_H
 
 #include <QAbstractItemModel>
-#include <QPointer>
-#include <string_kst.h>
+#include "dataobject.h"
+#include "object.h"
 
 namespace Kst {
 
+class ObjectStore;
+
 class StringModel : public QAbstractItemModel
 {
+
+  enum ColumnID { Name, Value };
+
 public:
-  StringModel(String *string);
+  StringModel(ObjectStore *store);
   ~StringModel();
 
   int columnCount(const QModelIndex& parent = QModelIndex()) const;
@@ -30,11 +35,13 @@
   QModelIndex index(int row, int col, const QModelIndex& parent = QModelIndex()) const;
   QModelIndex parent(const QModelIndex& index) const;
   QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
-  Qt::ItemFlags flags(const QModelIndex& index) const;
-  bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole);
+  void generateObjectList();
 
 private:
-  QPointer<String> _string;
+  QVariant stringData(StringPtr string, const QModelIndex& index) const;
+
+  ObjectStore *_store;
+  ObjectList<Object> _objectList;
 };
 
 }
--- branches/work/kst/portto4/kst/src/libkstapp/viewprimitivedialog.cpp #748542:748543
@@ -14,7 +14,7 @@
 #include "document.h"
 #include "objectstore.h"
 #include "scalarmodel.h"
-#include "stringtablemodel.h"
+#include "stringmodel.h"
 
 #include <datacollection.h>
 #include <QHeaderView>
@@ -45,18 +45,9 @@
     _tree->header()->setResizeMode(QHeaderView::ResizeToContents);
     _tree->setModel(_model);
   } else if (_type == String) {
-    StringTableModel *stringModel = new StringTableModel;
-    // TODO: Extract this model so the dialog can be reused, and make a new model
-    // or modification to the model so that it tracks all the string creates and
-    // destroys
-    StringList stringList;  // FIXME
-    foreach (StringPtr v, stringList) {
-      StringModel *vm = new StringModel(v);
-      stringModel->strings().append(vm);
-    }
+    _model = (QAbstractItemModel*)new StringModel(_doc->objectStore());
     _tree->header()->setResizeMode(QHeaderView::ResizeToContents);
-    _tree->setModel(stringModel);
-    _model = stringModel;
+    _tree->setModel(_model);
   }
 }
 


More information about the Kst mailing list