[rkward-cvs] SF.net SVN: rkward: [2182] branches/KDE4_port/rkward/dataeditor

tfry at users.sourceforge.net tfry at users.sourceforge.net
Mon Nov 5 23:20:42 UTC 2007


Revision: 2182
          http://rkward.svn.sourceforge.net/rkward/?rev=2182&view=rev
Author:   tfry
Date:     2007-11-05 15:20:42 -0800 (Mon, 05 Nov 2007)

Log Message:
-----------
First class implemented (but entirely untested)

Modified Paths:
--------------
    branches/KDE4_port/rkward/dataeditor/rkvareditmodel.cpp
    branches/KDE4_port/rkward/dataeditor/rkvareditmodel.h

Modified: branches/KDE4_port/rkward/dataeditor/rkvareditmodel.cpp
===================================================================
--- branches/KDE4_port/rkward/dataeditor/rkvareditmodel.cpp	2007-11-05 22:22:33 UTC (rev 2181)
+++ branches/KDE4_port/rkward/dataeditor/rkvareditmodel.cpp	2007-11-05 23:20:42 UTC (rev 2182)
@@ -17,7 +17,8 @@
 
 #include "rkvareditmodel.h"
 
-#include "../core/rkvariable.h"
+#include <klocale.h>
+
 #include "../core/rcontainerobject.h"
 #include "../core/rkmodificationtracker.h"
 #include "../rkglobals.h"
@@ -64,6 +65,11 @@
 	endRemoveColumns ();
 }
 
+void RKVarEditModel::doInsertColumn (int) {
+	RK_TRACE (EDITOR);
+	RK_ASSERT (false);	// should be implemented in a subclass, or never called
+}
+
 RKVarEditMetaModel* RKVarEditModel::getMetaModel () {
 	RK_TRACE (EDITOR);
 
@@ -75,11 +81,12 @@
 bool RKVarEditModel::insertRows (int row, int count, const QModelIndex& parent) {
 	RK_TRACE (EDITOR);
 
-	int lastrow = row+count - 1;
-	if (edit_blocks || parent.isValid () || objects.isEmpty () || (row > objects[0]->getLength ())) {
+	if (edit_blocks || parent.isValid () || objects.isEmpty () || (row > apparentRows ())) {
 		RK_ASSERT (false);
 		return false;
 	}
+	if (row > objects[0]->getLength ()) row = objects[0]->getLength ();
+	int lastrow = row+count - 1;
 	RK_ASSERT (row >= 0);
 	RK_ASSERT (lastrow <= row);
 
@@ -96,10 +103,11 @@
 	RK_TRACE (EDITOR);
 
 	int lastrow = row+count - 1;
-	if (edit_blocks || parent.isValid () || objects.isEmpty () || (lastrow > objects[0]->getLength ())) {
+	if (edit_blocks || parent.isValid () || objects.isEmpty () || (lastrow >= (apparentRows ()))) {
 		RK_ASSERT (false);
 		return false;
 	}
+	if (lastrow >= objects[0]->getLength ()) lastrow = objects[0]->getLength () - 1;
 	RK_ASSERT (row >= 0);
 	RK_ASSERT (lastrow <= row);
 
@@ -120,34 +128,112 @@
 		RK_ASSERT (false);
 		return 0;
 	}
-	return objects[0]->getLength ();
+	return (apparentRows ());
 }
 
 int RKVarEditModel::columnCount (const QModelIndex& parent) const {
 	RK_TRACE (EDITOR);
 
 	if (parent.isValid ()) return 0;
-	return objects.size ();
+	return (apparentCols ());
 }
 
 QVariant RKVarEditModel::data (const QModelIndex& index, int role) const {
 	RK_TRACE (EDITOR);
-#warning implement
+
+	if (!index.isValid ()) return QVariant ();
+	int row = index.row ();
+	int col = index.column ();
+	if ((col >= apparentCols ()) || (row >= apparentRows ())) {
+		RK_ASSERT (false);
+		return QVariant ();
+	}
+
+	// on a trailing row / col
+	if ((col >= objects.size ()) || (row >= objects[0]->getLength ())) {
+		if (role == Qt::BackgroundRole) return (Qt::gray);
+		if (role == Qt::ToolTipRole) {
+			if (col >= objects.size ()) return (i18n ("Type on these fields to add new columns"));
+			else return (i18n ("Type on these fields to add new rows"));
+		}
+		return QVariant ();
+	}
+
+	// a normal cell
+	RKVariable *var = objects[col];
+	RK_ASSERT (var);
+
+	if (role == Qt::DisplayRole) return var->getText (row, true);
+	if (role == Qt::EditRole) return var->getText (row, false);
+
+	RKVariable::Status status = var->cellStatus (row);
+	if ((role == Qt::BackgroundRole) && (status == RKVariable::ValueInvalid)) return (Qt::red);
+	if ((role == Qt::ForegroundRole) && (status == RKVariable::ValueUnknown)) return (Qt::lightGray);
+
+	return QVariant ();
 }
 
 Qt::ItemFlags RKVarEditModel::flags (const QModelIndex& index) const {
 	RK_TRACE (EDITOR);
-#warning implement
+
+	Qt::ItemFlags flags = 0;
+
+	if (!index.isValid ()) return flags;
+	int row = index.row ();
+	int col = index.column ();
+	if ((col >= apparentCols ()) || (row >= apparentRows ())) {
+		RK_ASSERT (false);
+		return flags;
+	}
+
+	if (!edit_blocks) flags |= Qt::ItemIsEditable | Qt::ItemIsEnabled;
+	if ((col < objects.size ()) && (row >= objects[0]->getLength ())) flags |= Qt::ItemIsSelectable;
+
+	return flags;
 }
 
 bool RKVarEditModel::setData (const QModelIndex& index, const QVariant& value, int role) {
 	RK_TRACE (EDITOR);
-#warning implement
+
+	if (!index.isValid ()) return false;
+	int row = index.row ();
+	int col = index.column ();
+	if (edit_blocks || (role != Qt::EditRole) || (col >= apparentCols ()) || (row >= apparentRows ())) {
+		RK_ASSERT (false);
+		return false;
+	}
+
+	if (col >= objects.size ()) {		// trailing col
+		// somebody should add a column for us
+		doInsertColumn (objects.size ());
+
+		if (col >= objects.size ()) {
+			// apparently, no column has been added in the above signal
+			return false;
+		}
+	}
+	if (row >= objects[0]->getLength ()) {		// trailing row
+		insertRows (objects[0]->getLength (), 1);
+	}
+
+	// edit of normal cells
+	RKVariable* var = objects[col];
+	RK_ASSERT (var);
+	var->setText (row, value.toString ());
+	return true;
 }
 
 QVariant RKVarEditModel::headerData (int section, Qt::Orientation orientation, int role) const {
 	RK_TRACE (EDITOR);
-#warning implement
+
+	if (role != Qt::DisplayRole) return QVariant ();
+
+	if (orientation == Qt::Horizontal) {
+		if (section >= objects.size ()) return QVariant ();
+		return objects[section]->getShortName ();
+	}
+
+	return QString::number (section);
 }
 
 

Modified: branches/KDE4_port/rkward/dataeditor/rkvareditmodel.h
===================================================================
--- branches/KDE4_port/rkward/dataeditor/rkvareditmodel.h	2007-11-05 22:22:33 UTC (rev 2181)
+++ branches/KDE4_port/rkward/dataeditor/rkvareditmodel.h	2007-11-05 23:20:42 UTC (rev 2182)
@@ -21,10 +21,9 @@
 #include <QAbstractTableModel>
 #include <QList>
 
+#include "../core/rkvariable.h"
+
 class RKVarEditMetaModel;
-class RKVariable;
-class RObject;
-class RContainerObject;
 
 /** This class represents a collection of RKVariables of uniform length (typically a data.frame) suitable for editing in a model/view editor such as QTableView. Probably it will only ever support editing a single RKVariable, though, as it is not possible to ensure uniform length outside of a data.frame. For a data.frame use RKVarEditDataFrameModel . */
 class RKVarEditModel : public QAbstractTableModel {
@@ -33,7 +32,11 @@
 	RKVarEditModel (QObject *parent);
 	~RKVarEditModel ();
 
+	/** Add an object to the model at the given index. Calls this only once, unless from an RKVarEditDataFrameModel. You should add at least one object, **before** you add this model to any view.
+	@param index position to insert at, or -1 to append the item
+	@param object the objects to insert */
 	void addObject (int index, RKVariable* object);
+	/** Return a pointer to the meta model. The meta model is created the first time this function is called. */
 	RKVarEditMetaModel* getMetaModel ();
 
 	// QAbstractTableModel implementations
@@ -49,11 +52,20 @@
 	// convenience signal to tell the editor to block editing entirely, without having to set all flags to non-editable.
 	void blockEdit (bool block);
 public slots:
+	/** Receives notifications of object removals. Takes care of removing the object from the list. */
 	virtual void objectRemoved (RObject* object);
 protected:
 friend class RKVarEditMetaModel;
 	QList<RKVariable*> objects;
 
+	/** very simple convenience function to return the number of true cols + trailing cols */
+	int apparentCols () const { return objects.size () + trailing_cols; };
+	/** very simple convenience function to return the number of true rows + trailing rows */
+	int apparentRows () const { return (trailing_rows + objects.isEmpty () ? 0 : objects[0]->getLength ()); };
+
+	/** insert a new column at index. Default implementation does nothing. To be implemented in subclasses */
+	virtual void doInsertColumn (int index);
+
 	int trailing_rows;
 	int trailing_cols;
 
@@ -62,11 +74,11 @@
 	RKVarEditMetaModel* meta_model;
 };
 
-/** Represents the meta information portion belonging to an RKVarEditModel. Implemented in a separate class for technical reasons, only (so this info can be displayed in a separate QTableView) */
+/** Represents the meta information portion belonging to an RKVarEditModel. Implemented in a separate class for technical reasons, only (so this info can be displayed in a separate QTableView). This model mostly acts as a slave of an RKVarEditModel. You will not need to call any functions directly except from the RKVarEditModel, or an item view. */
 class RKVarEditMetaModel : public QAbstractTableModel {
 	Q_OBJECT
 public:
-	// QAbstractTableModel implementations. Most of these are simply proxies to the corresponding functions in the datamodel
+	// QAbstractTableModel implementations
 	int rowCount (const QModelIndex& parent = QModelIndex()) const;
 	int columnCount (const QModelIndex& parent = QModelIndex()) const;
 	QVariant data (const QModelIndex& index, int role = Qt::DisplayRole) const;


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the rkward-tracker mailing list