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

George Staikos staikos at kde.org
Tue May 29 17:19:10 CEST 2007


SVN commit 669513 by staikos:

demonstrate editable spreadsheet mode


 M  +4 -0      mainwindow.cpp  
 M  +37 -0     vectormodel.cpp  
 M  +2 -0      vectormodel.h  
 M  +27 -1     vectortablemodel.cpp  
 M  +2 -0      vectortablemodel.h  


--- branches/work/kst/portto4/kst/src/libkstapp/mainwindow.cpp #669512:669513
@@ -21,6 +21,7 @@
 #include <QtGui>
 
 // Temporaries
+#include "kstavector.h"
 #include "vectortablemodel.h"
 
 namespace Kst {
@@ -106,6 +107,7 @@
   v->resize(999999);
   KstVectorPtr v2 = new KstVector;
   v2->resize(999999);
+  KstVectorPtr v3 = new KstAVector(25, KstObjectTag::fromString("Editable V"));
   double *d = const_cast<double *>(v->value()); // yay :)
   double *d2 = const_cast<double *>(v2->value()); // yay :)
   d[0] = 1;
@@ -116,9 +118,11 @@
   }
   VectorModel *m = new VectorModel(v);
   VectorModel *m2 = new VectorModel(v2);
+  VectorModel *m3 = new VectorModel(v3);
   VectorTableModel *tm = new VectorTableModel;
   tm->vectors().append(m);
   tm->vectors().append(m2);
+  tm->vectors().append(m3);
   view->setModel(tm);
   view->resize(300, 500);
   view->show();
--- branches/work/kst/portto4/kst/src/libkstapp/vectormodel.cpp #669512:669513
@@ -69,6 +69,43 @@
   return _v->tagName();
 }
 
+
+Qt::ItemFlags VectorModel::flags(const QModelIndex& index) const {
+  Qt::ItemFlags f = QAbstractItemModel::flags(index);
+  if (!index.isValid()) {
+    return f;
+  }
+
+  if (_v->editable() && index.row() >= 0 && index.row() < _v->length()) {
+    f |= Qt::ItemIsEditable;
+  }
+
+  return f;
 }
 
+
+bool VectorModel::setData(const QModelIndex& index, const QVariant& value, int role) {
+  if (role != Qt::EditRole) {
+    return QAbstractItemModel::setData(index, value, role);
+  }
+
+  if (!index.isValid() || !_v->editable() || index.row() < 0 || index.row() >= _v->length()) {
+    return false;
+  }
+
+  bool ok = false;
+  double v = value.toDouble(&ok);
+  if (!ok) {
+    return false;
+  }
+
+  qDebug() << "UGLY!! Add setData API to KstVector!";
+  double *d = const_cast<double*>(_v->value());
+  d[index.row()] = v;
+  return true;
+}
+
+
+}
+
 // vim: ts=2 sw=2 et
--- branches/work/kst/portto4/kst/src/libkstapp/vectormodel.h #669512:669513
@@ -29,6 +29,8 @@
   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);
 
 private:
   KstVectorPtr _v;
--- branches/work/kst/portto4/kst/src/libkstapp/vectortablemodel.cpp #669512:669513
@@ -41,7 +41,6 @@
 
 
 QVariant VectorTableModel::data(const QModelIndex& index, int role) const {
-  Q_UNUSED(role)
   if (!index.isValid() || role != Qt::DisplayRole) {
     return QVariant();
   }
@@ -78,6 +77,33 @@
   return _vectors[section]->headerData(0, orientation, role);
 }
 
+
+Qt::ItemFlags VectorTableModel::flags(const QModelIndex& index) const {
+  const int col = index.column();
+  const int row = index.row();
+  if (col < 0 || col >= _vectors.count() || !index.isValid()) {
+    return QAbstractItemModel::flags(index);
+  }
+
+  VectorModel *m = _vectors[col];
+  if (row < 0 || row >= m->rowCount()) {
+    return QAbstractItemModel::flags(index);
+  }
+
+  QModelIndex idx = m->index(row, 0);
+  return m->flags(idx);
 }
 
+
+bool VectorTableModel::setData(const QModelIndex& index, const QVariant& value, int role) {
+  if (!index.isValid() || role != Qt::EditRole) {
+    return false;
+  }
+  VectorModel *m = _vectors[index.column()];
+  QModelIndex idx = m->index(index.row(), 0);
+  return m->setData(idx, value, role);
+}
+
+}
+
 // vim: ts=2 sw=2 et
--- branches/work/kst/portto4/kst/src/libkstapp/vectortablemodel.h #669512:669513
@@ -31,6 +31,8 @@
 
   QVector<VectorModel*>& vectors() { return _vectors; }
   const QVector<VectorModel*>& vectors() const { return _vectors; }
+  Qt::ItemFlags flags(const QModelIndex& index) const;
+  bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole);
 
 private:
   QVector<VectorModel*> _vectors;


More information about the Kst mailing list