[rkward-cvs] SF.net SVN: rkward:[4398] trunk/rkward/rkward/plugin

tfry at users.sourceforge.net tfry at users.sourceforge.net
Tue Oct 30 13:12:37 UTC 2012


Revision: 4398
          http://rkward.svn.sourceforge.net/rkward/?rev=4398&view=rev
Author:   tfry
Date:     2012-10-30 13:12:37 +0000 (Tue, 30 Oct 2012)
Log Message:
-----------
Make matrix input compile. Still unused / untested.

Modified Paths:
--------------
    trunk/rkward/rkward/plugin/CMakeLists.txt
    trunk/rkward/rkward/plugin/rkcomponent.h
    trunk/rkward/rkward/plugin/rkmatrixinput.cpp
    trunk/rkward/rkward/plugin/rkmatrixinput.h

Modified: trunk/rkward/rkward/plugin/CMakeLists.txt
===================================================================
--- trunk/rkward/rkward/plugin/CMakeLists.txt	2012-10-30 12:27:24 UTC (rev 4397)
+++ trunk/rkward/rkward/plugin/CMakeLists.txt	2012-10-30 13:12:37 UTC (rev 4398)
@@ -26,6 +26,7 @@
    rkabstractoptionselector.cpp
    rkpluginframe.cpp
    rkoptionset.cpp
+   rkmatrixinput.cpp
    )
 
 QT4_AUTOMOC(${plugin_STAT_SRCS})

Modified: trunk/rkward/rkward/plugin/rkcomponent.h
===================================================================
--- trunk/rkward/rkward/plugin/rkcomponent.h	2012-10-30 12:27:24 UTC (rev 4397)
+++ trunk/rkward/rkward/plugin/rkcomponent.h	2012-10-30 13:12:37 UTC (rev 4398)
@@ -61,6 +61,7 @@
 		ComponentSaveObject = 2015,
 		ComponentFrame = 2016,
 		ComponentOptionSet = 2017,
+		ComponentMatrixInput = 2018,
 		ComponentStandard = 2100,
 		ComponentContextHandler = 2900,
 		ComponentUser = 3000	/**< for user expansion */

Modified: trunk/rkward/rkward/plugin/rkmatrixinput.cpp
===================================================================
--- trunk/rkward/rkward/plugin/rkmatrixinput.cpp	2012-10-30 12:27:24 UTC (rev 4397)
+++ trunk/rkward/rkward/plugin/rkmatrixinput.cpp	2012-10-30 13:12:37 UTC (rev 4398)
@@ -18,6 +18,8 @@
 #include "rkmatrixinput.h"
 
 #include <QVBoxLayout>
+#include <QLabel>
+#include <QTableView>
 
 #include "klocale.h"
 
@@ -25,7 +27,7 @@
 
 #include "../debug.h"
 
-RKMatrixInput::RKMatrixInput (const QDomElement& element, RKComponent* parent_component, QWidget* parent_widget) : RKComponent (parent_component, parent_widget), QAbstractTableModel () {
+RKMatrixInput::RKMatrixInput (const QDomElement& element, RKComponent* parent_component, QWidget* parent_widget) : RKComponent (parent_component, parent_widget) {
 	RK_TRACE (PLUGIN);
 
 	// get xml-helper
@@ -61,19 +63,20 @@
 
 	row_count = new RKComponentPropertyInt (this, false, xml->getIntAttribute (element, "rows", 2, DL_INFO));
 	column_count = new RKComponentPropertyInt (this, false, xml->getIntAttribute (element, "columns", 2, DL_INFO));
-	tsv_data = new RKComponentPropertyBase (this);
+	tsv_data = new RKComponentPropertyBase (this, false);
 	row_count->setInternal (true);
-	addChild (row_count);
+	addChild ("rows", row_count);
 	column_count->setInternal (true);
-	addChild (column_count);
-	addChild (tsv_data);
+	addChild ("columns", column_count);
+	addChild ("tsv", tsv_data);
 	connect (row_count, SIGNAL (valueChanged(RKComponentPropertyBase*)), this, SLOT (dimensionPropertyChanged(RKComponentPropertyBase*)));
 	connect (column_count, SIGNAL (valueChanged(RKComponentPropertyBase*)), this, SLOT (dimensionPropertyChanged(RKComponentPropertyBase*)));
 	connect (tsv_data, SIGNAL (valueChanged(RKComponentPropertyBase*)), this, SLOT (tsvPropertyChanged()));
 	updating_tsv_data = false;
 	updating_dimensions = false;
 
-	display->setModel (this);
+	model = new RKMatrixInputModel (this);
+	display->setModel (model);
 }
 
 
@@ -81,69 +84,6 @@
 	RK_TRACE (PLUGIN);
 }
 
-int RKMatrixInput::columnCount (const QModelIndex& parent) const {
-	if (parent.isValid ()) return 0;
-	return column_count->intValue () + trailing_columns;
-}
-
-int RKMatrixInput::rowCount (const QModelIndex& parent) const {
-	if (parent.isValid ()) return 0;
-	return row_count->intValue () + trailing_rows;
-}
-
-QVariant RKMatrixInput::data (const QModelIndex& index, int role) const {
-	if (!index.isValid ()) return QVariant ();
-
-	int row = index.row ();
-	int column = index.column ();
-
-	// handle trailing rows / cols in user expandable tables
-	bool trailing = false;
-	if (row > row_count->intValue ()) {
-		if ((!allow_user_resize_rows) || (row > row_count->intValue () + trailing_rows)) {
-			RK_ASSERT (false);
-			return QVariant ();
-		}
-		trailing = true;
-	}
-	if (column > column_count->intValue ()) {
-		if ((!allow_user_resize_columns) || (column > column_count->intValue () + trailing_columns)) {
-			RK_ASSERT (false);
-			return QVariant ();
-		}
-		trailing = true;
-	};
-	if (trailing) {
-		if (role == Qt::BackgroundRole) return QVariant (QBrush (Qt::lightGray));
-		if (role == Qt::ToolTipRole || role == Qt::StatusTipRole) return QVariant (i18n ("Type on these cells to expand the table"));
-		return QVariant ();
-	}
-
-	// regular cells
-	QString value;
-	if (columns.size () > column) {	// column >= columns.size() can happen, legally. In this case the value is simply missing.
-		value = columns[column].storage.value (row);
-	}
-	if ((role == Qt::DisplayRole) || (role == Qt::EditRole)) {
-		return QVariant (value);
-	} else if (role == Qt::BackgroundRole) {
-		if (!is_valid && (value.isEmpty () && !allow_missings) || !isValueValid (value)) return QVariant (QBrush (Qt::red));
-	} else if ((role == Qt::ToolTipRole) || (role == Qt::StatusTipRole)) {
-		if (!is_valid && (value.isEmpty () && !allow_missings)) return QVariant (i18n ("Empty values are not allowed"));
-		if (!is_valid && !isValueValid (value)) return QVariant (i18n ("This value is not allowed, here"));
-	}
-	return QVariant ();
-}
-
-bool RKMatrixInput::setData (const QModelIndex& index, const QVariant& value, int role) {
-	RK_TRACE (PLUGIN);
-
-	if (!index.isValid ()) return false;
-	if (role != Qt::EditRole) return false;
-	setCellValue (index.row (), index.column (), value.toString ());
-	return true;
-}
-
 bool RKMatrixInput::expandStorageForColumn (int column) {
 	RK_TRACE (PLUGIN);
 
@@ -169,12 +109,13 @@
 		return;
 	}
 
-	Column &col = columns[col];
+	Column &col = columns[column];
 	while (row >= col.storage.size ()) {
 		col.storage.append (QString ());
 	}
 	col.storage[row] = value;
 	updateColumn (row, column);
+	model->dataChanged (model->index (row, column), model->index (row, column));
 }
 
 void RKMatrixInput::setColumnValue (int column, const QString& value) {
@@ -183,6 +124,7 @@
 	if (!expandStorageForColumn (column)) return;
 	columns[column].storage = value.split ('\t', QString::KeepEmptyParts);
 	updateColumn (0, column);
+	model->dataChanged (model->index (0, column), model->index (row_count->intValue () + trailing_rows, column));
 }
 
 void RKMatrixInput::updateColumn (int offset, int column) {
@@ -244,7 +186,9 @@
 		}
 		if (max_row != row_count->intValue () - 1) {
 			updating_dimensions = true;
+			model->layoutAboutToBeChanged ();
 			row_count->setIntValue (max_row);
+			model->layoutChanged ();
 			updating_dimensions = false;
 		}
 	}
@@ -260,7 +204,9 @@
 		}
 		if (max_col != column_count->intValue () - 1) {
 			updating_dimensions = true;
+			model->layoutAboutToBeChanged ();
 			column_count->setIntValue (max_col);
+			model->layoutChanged ();
 			updating_dimensions = false;
 		}
 	}
@@ -273,7 +219,7 @@
 		} else {
 			Column& col = columns[i];
 			if (col.cached_tab_joined_string.isNull ()) {
-				QString cache = col.storage.mid (0, max_row + 1).join ('\t');
+				QString cache = QStringList (col.storage.mid (0, max_row + 1)).join ("\t");
 				if (col.storage.size () < max_row) {
 					QString empty_trail (max_row - col.storage.size (), '\t');
 					cache.append (empty_trail);
@@ -283,7 +229,7 @@
 			tsv.append (col.cached_tab_joined_string);
 		}
 	}
-	tsv_data->setValue (tsv);
+	tsv_data->setValue (tsv.join ("\n"));
 
 	updating_tsv_data = false;
 }
@@ -343,7 +289,81 @@
 	return (val < max);
 }
 
-#error TODO
-#error TODO signal model layout and data changes
 
+
+///////////////////////////////////////////////////////////////////////////////////////
+
+RKMatrixInputModel::RKMatrixInputModel (RKMatrixInput* _matrix) : QAbstractTableModel (_matrix) {
+	RK_TRACE (PLUGIN);
+
+	matrix = _matrix;
+}
+
+RKMatrixInputModel::~RKMatrixInputModel () {
+	RK_TRACE (PLUGIN);
+}
+
+int RKMatrixInputModel::columnCount (const QModelIndex& parent) const {
+	if (parent.isValid ()) return 0;
+	return matrix->column_count->intValue () + matrix->trailing_columns;
+}
+
+int RKMatrixInputModel::rowCount (const QModelIndex& parent) const {
+	if (parent.isValid ()) return 0;
+	return matrix->row_count->intValue () + matrix->trailing_rows;
+}
+
+QVariant RKMatrixInputModel::data (const QModelIndex& index, int role) const {
+	if (!index.isValid ()) return QVariant ();
+
+	int row = index.row ();
+	int column = index.column ();
+
+	// handle trailing rows / cols in user expandable tables
+	bool trailing = false;
+	if (row > matrix->row_count->intValue ()) {
+		if ((!matrix->allow_user_resize_rows) || (row > matrix->row_count->intValue () + matrix->trailing_rows)) {
+			RK_ASSERT (false);
+			return QVariant ();
+		}
+		trailing = true;
+	}
+	if (column > matrix->column_count->intValue ()) {
+		if ((!matrix->allow_user_resize_columns) || (column > matrix->column_count->intValue () + matrix->trailing_columns)) {
+			RK_ASSERT (false);
+			return QVariant ();
+		}
+		trailing = true;
+	};
+	if (trailing) {
+		if (role == Qt::BackgroundRole) return QVariant (QBrush (Qt::lightGray));
+		if (role == Qt::ToolTipRole || role == Qt::StatusTipRole) return QVariant (i18n ("Type on these cells to expand the table"));
+		return QVariant ();
+	}
+
+	// regular cells
+	QString value;
+	if (matrix->columns.size () > column) {	// column >= columns.size() can happen, legally. In this case the value is simply missing.
+		value = matrix->columns[column].storage.value (row);
+	}
+	if ((role == Qt::DisplayRole) || (role == Qt::EditRole)) {
+		return QVariant (value);
+	} else if (role == Qt::BackgroundRole) {
+		if (!matrix->is_valid && !matrix->isValueValid (value)) return QVariant (QBrush (Qt::red));
+	} else if ((role == Qt::ToolTipRole) || (role == Qt::StatusTipRole)) {
+		if (!matrix->is_valid && (value.isEmpty () && !matrix->allow_missings)) return QVariant (i18n ("Empty values are not allowed"));
+		if (!matrix->is_valid && !matrix->isValueValid (value)) return QVariant (i18n ("This value is not allowed, here"));
+	}
+	return QVariant ();
+}
+
+bool RKMatrixInputModel::setData (const QModelIndex& index, const QVariant& value, int role) {
+	RK_TRACE (PLUGIN);
+
+	if (!index.isValid ()) return false;
+	if (role != Qt::EditRole) return false;
+	matrix->setCellValue (index.row (), index.column (), value.toString ());
+	return true;
+}
+
 #include "rkmatrixinput.moc"

Modified: trunk/rkward/rkward/plugin/rkmatrixinput.h
===================================================================
--- trunk/rkward/rkward/plugin/rkmatrixinput.h	2012-10-30 12:27:24 UTC (rev 4397)
+++ trunk/rkward/rkward/plugin/rkmatrixinput.h	2012-10-30 13:12:37 UTC (rev 4398)
@@ -20,16 +20,16 @@
 
 #include <rkcomponent.h>
 
-#include <QAbstractTableModel>
 #include <QDomElement>
 #include <QStringList>
 
 class QTableView;
+class RKMatrixInputModel;
 
 /** Provides a table for editing one- or two-dimensional data
   *@author Thomas Friedrichsmeier
   */
-class RKMatrixInput : public RKComponent, public QAbstractTableModel {
+class RKMatrixInput : public RKComponent {
 	Q_OBJECT
 public:
 	RKMatrixInput (const QDomElement &element, RKComponent *parent_component, QWidget *parent_widget);
@@ -41,15 +41,11 @@
 	void dimensionPropertyChanged (RKComponentPropertyBase *property);
 	void tsvPropertyChanged ();
 private:
+	friend class RKMatrixInputModel;
 	RKComponentPropertyInt *row_count;
 	RKComponentPropertyInt *column_count;
 	RKComponentPropertyBase *tsv_data;
 
-	int rowCount (const QModelIndex &parent = QModelIndex()) const; // implemented for QAbstractTableModel
-	int columnCount (const QModelIndex &parent = QModelIndex()) const; // implemented for QAbstractTableModel
-	QVariant data (const QModelIndex &index, int role = Qt::DisplayRole) const; // re-implemented for QAbstractTableModel
-	bool setData (const QModelIndex &index, const QVariant &value, int role = Qt::EditRole); // re-implemented for QAbstractTableModel
-
 	enum Mode {
 		Integer=0,
 		Real,
@@ -59,11 +55,11 @@
 	double min;
 	double max;
 
-	const bool allow_missings;
-	const bool allow_user_resize_rows;
-	const bool allow_user_resize_columns;
-	const int trailing_rows;
-	const int trailing_columns;
+	bool allow_missings;
+	bool allow_user_resize_rows;
+	bool allow_user_resize_columns;
+	int trailing_rows;
+	int trailing_columns;
 
 	bool isValueValid (const QString &value) const;
 	void updateValidityFlag ();
@@ -87,10 +83,26 @@
 	QList<Column> columns;
 
 	QTableView *display;
+	RKMatrixInputModel *model;
 
 	// these two to avoid recursion:
 	bool updating_dimensions;
 	bool updating_tsv_data;
 };
 
+#include <QAbstractTableModel>
+
+class RKMatrixInputModel : public QAbstractTableModel {
+private:
+friend class RKMatrixInput;
+	RKMatrixInputModel (RKMatrixInput *matrix);
+	virtual ~RKMatrixInputModel ();
+	int rowCount (const QModelIndex &parent = QModelIndex()) const; // implemented for QAbstractTableModel
+	int columnCount (const QModelIndex &parent = QModelIndex()) const; // implemented for QAbstractTableModel
+	QVariant data (const QModelIndex &index, int role = Qt::DisplayRole) const; // re-implemented for QAbstractTableModel
+	bool setData (const QModelIndex &index, const QVariant &value, int role = Qt::EditRole); // re-implemented for QAbstractTableModel
+
+	RKMatrixInput *matrix;
+};
+
 #endif

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