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

tfry at users.sourceforge.net tfry at users.sourceforge.net
Tue Nov 6 12:52:06 UTC 2012


Revision: 4413
          http://rkward.svn.sourceforge.net/rkward/?rev=4413&view=rev
Author:   tfry
Date:     2012-11-06 12:52:05 +0000 (Tue, 06 Nov 2012)
Log Message:
-----------
Implement cut / copy / paste for the matrix input element

Modified Paths:
--------------
    trunk/rkward/rkward/dataeditor/twintablemember.cpp
    trunk/rkward/rkward/dataeditor/twintablemember.h
    trunk/rkward/rkward/misc/CMakeLists.txt
    trunk/rkward/rkward/plugin/rkmatrixinput.cpp
    trunk/rkward/rkward/plugin/rkmatrixinput.h

Modified: trunk/rkward/rkward/dataeditor/twintablemember.cpp
===================================================================
--- trunk/rkward/rkward/dataeditor/twintablemember.cpp	2012-11-06 09:20:00 UTC (rev 4412)
+++ trunk/rkward/rkward/dataeditor/twintablemember.cpp	2012-11-06 12:52:05 UTC (rev 4413)
@@ -2,7 +2,7 @@
                           twintablemember.cpp  -  description
                              -------------------
     begin                : Tue Oct 29 2002
-    copyright            : (C) 2002, 2006, 2007, 2009, 2010 by Thomas Friedrichsmeier
+    copyright            : (C) 2002, 2006, 2007, 2009, 2010, 2012 by Thomas Friedrichsmeier
     email                : tfry at users.sourceforge.net
  ***************************************************************************/
 
@@ -30,7 +30,7 @@
 
 #include "../debug.h"
 
-TwinTableMember::TwinTableMember (QWidget *parent) : QTableView (parent){
+TwinTableMember::TwinTableMember (QWidget *parent) : RKTableView (parent){
 	RK_TRACE (EDITOR);
 
 	rw = true;
@@ -159,28 +159,6 @@
 	mymodel->setTextMatrix (selrange.topLeft (), pasted, limrange);
 }
 
-QItemSelectionRange TwinTableMember::getSelectionBoundaries () {
-	RK_TRACE (EDITOR);
-
-	RK_ASSERT (selectionModel ());
-	QItemSelection sel = selectionModel ()->selection ();
-	if (sel.isEmpty ()){
-		QModelIndex current = currentIndex ();
-		if ((!current.isValid ()) || isIndexHidden (current)) return (QItemSelectionRange ());
-
-		return (QItemSelectionRange (currentIndex (), currentIndex ()));
-	} else {
-		RK_ASSERT (sel.size () == 1);
-
-		QItemSelectionRange range = sel[0];
-		while (isColumnHidden (range.left ())) {
-			// purge hidden leading columns from the range
-			range = QItemSelectionRange (mymodel->index (range.top (), range.left () + 1, rootIndex ()), range.bottomRight ());
-		}
-		return (range);
-	}
-}
-
 void TwinTableMember::keyPressEvent (QKeyEvent *e) {
 	RK_TRACE (EDITOR);
 

Modified: trunk/rkward/rkward/dataeditor/twintablemember.h
===================================================================
--- trunk/rkward/rkward/dataeditor/twintablemember.h	2012-11-06 09:20:00 UTC (rev 4412)
+++ trunk/rkward/rkward/dataeditor/twintablemember.h	2012-11-06 12:52:05 UTC (rev 4413)
@@ -2,7 +2,7 @@
                           twintablemember.h  -  description
                              -------------------
     begin                : Tue Oct 29 2002
-    copyright            : (C) 2002, 2006, 2007, 2010 by Thomas Friedrichsmeier
+    copyright            : (C) 2002, 2006, 2007, 2010, 2012 by Thomas Friedrichsmeier
     email                : tfry at users.sourceforge.net
  ***************************************************************************/
 
@@ -18,7 +18,6 @@
 #ifndef TWINTABLEMEMBER_H
 #define TWINTABLEMEMBER_H
 
-#include <QTableView>
 #include <QItemSelectionRange>
 #include <QItemDelegate>
 #include <qpoint.h>
@@ -29,6 +28,7 @@
 class CellEditor;
 class RKVarEditModelBase;
 
+#include "../misc/rktableview.h"
 #include "rkeditor.h"
 
 class RKVarEditMetaModel;
@@ -71,7 +71,7 @@
 /** One of the tables used in a TwinTable.
 @author Thomas Friedrichsmeier
 */
-class TwinTableMember : public QTableView {
+class TwinTableMember : public RKTableView {
 	Q_OBJECT
 public: 
 	TwinTableMember (QWidget *parent);
@@ -87,8 +87,6 @@
 
 /** blanks out the currently selected cells (or the currently active cell, if there is no selection) */
 	void blankSelected ();
-/** shortcut to get the boundaries of the current selection */
-	QItemSelectionRange getSelectionBoundaries ();
 
 	void setRKModel (RKVarEditModelBase* model);
 	void seRKItemDelegate (RKItemDelegate* delegate);

Modified: trunk/rkward/rkward/misc/CMakeLists.txt
===================================================================
--- trunk/rkward/rkward/misc/CMakeLists.txt	2012-11-06 09:20:00 UTC (rev 4412)
+++ trunk/rkward/rkward/misc/CMakeLists.txt	2012-11-06 12:52:05 UTC (rev 4413)
@@ -19,6 +19,7 @@
    rkstandardactions.cpp
    rkxmlguisyncer.cpp
    rkcommandhistory.cpp
+   rktableview.cpp
    )
 
 QT4_AUTOMOC(${misc_STAT_SRCS})

Modified: trunk/rkward/rkward/plugin/rkmatrixinput.cpp
===================================================================
--- trunk/rkward/rkward/plugin/rkmatrixinput.cpp	2012-11-06 09:20:00 UTC (rev 4412)
+++ trunk/rkward/rkward/plugin/rkmatrixinput.cpp	2012-11-06 12:52:05 UTC (rev 4413)
@@ -19,9 +19,12 @@
 
 #include <QVBoxLayout>
 #include <QLabel>
-#include <QTableView>
 #include <QHeaderView>
 
+#include "../misc/rktableview.h"
+#include "../dataeditor/rktextmatrix.h"
+#include "kstandardaction.h"
+#include "kaction.h"
 #include "klocale.h"
 
 #include "../misc/xmlhelper.h"
@@ -41,7 +44,7 @@
 	QLabel *label = new QLabel (xml->getStringAttribute (element, "label", i18n ("Enter data:"), DL_INFO), this);
 	vbox->addWidget (label);
 
-	display = new QTableView (this);
+	display = new RKTableView (this);
 	vbox->addWidget (display);
 
 	mode = static_cast<Mode> (xml->getMultiChoiceAttribute (element, "mode", "integer;real;string", 1, DL_WARNING));
@@ -92,6 +95,15 @@
 		int max_row = row_count->intValue () - 1;
 		display->setFixedHeight (display->horizontalHeader ()->height () + display->rowViewportPosition (max_row) + display->rowHeight (max_row));
 	}
+
+	// define standard actions
+	KAction *cut = KStandardAction::cut (this, SLOT (cut()), this);
+	display->addAction (cut);
+	KAction *copy = KStandardAction::copy (this, SLOT (copy()), this);
+	display->addAction (copy);
+	KAction *paste = KStandardAction::paste (this, SLOT (paste()), this);
+	display->addAction (paste);
+	display->setContextMenuPolicy (Qt::ActionsContextMenu);
 }
 
 RKMatrixInput::~RKMatrixInput () {
@@ -116,7 +128,7 @@
 bool RKMatrixInput::expandStorageForColumn (int column) {
 	RK_TRACE (PLUGIN);
 
-	if ((column < 0) || (column >= (column_count->intValue () + trailing_columns))) {
+	if ((column < 0) || (!allow_user_resize_columns && (column >= column_count->intValue ()))) {
 		RK_ASSERT (false);
 		return false;
 	}
@@ -129,10 +141,16 @@
 	return true;
 }
 
+QString RKMatrixInput::cellValue (int row, int column) const {
+	if ((column < 0) || (column >= columns.size ())) return (QString ());
+	return columns[column].storage.value (row);
+}
+
+
 void RKMatrixInput::setCellValue (int row, int column, const QString& value) {
 	RK_TRACE (PLUGIN);
 
-	if ((!expandStorageForColumn (column)) || (row < 0) || (row >= (row_count->intValue () + trailing_rows))) {
+	if ((!expandStorageForColumn (column)) || (row < 0) || (!allow_user_resize_rows && (row >= row_count->intValue ()))) {
 		RK_ASSERT (false);
 		return;
 	}
@@ -281,10 +299,9 @@
 }
 
 void RKMatrixInput::tsvPropertyChanged () {
-	RK_TRACE (PLUGIN);
-
 	if (updating_tsv_data) return;
 	updating_tsv_data = true;
+	RK_TRACE (PLUGIN);
 
 	columns.clear ();
 	QStringList coldata = tsv_data->value ().split ('\n', QString::KeepEmptyParts);
@@ -343,9 +360,72 @@
 	return true;
 }
 
+void RKMatrixInput::clearSelectedCells () {
+	RK_TRACE (PLUGIN);
 
+	QItemSelectionRange range = display->getSelectionBoundaries ();
+	if (!range.isValid ()) return;
 
+	updating_tsv_data = true;
+	for (int col = range.left (); col <= range.right (); ++col) {
+		for (int row = range.top (); row <= range.bottom (); ++row) {
+			setCellValue (row, col, QString ());
+		}
+	}
+	updating_tsv_data = false;
+	updateAll ();
+}
 
+void RKMatrixInput::copy () {
+	RK_TRACE (PLUGIN);
+
+	QItemSelectionRange range = display->getSelectionBoundaries ();
+	if (!range.isValid ()) return;
+
+	RKTextMatrix ret;
+	for (int col = range.left (); col <= range.right (); ++col) {
+		for (int row = range.top (); row <= range.bottom (); ++row) {
+			ret.setText (row - range.top (), col - range.left (), cellValue (row, col));
+		}
+	}
+	ret.copyToClipboard ();
+}
+
+void RKMatrixInput::cut () {
+	RK_TRACE (PLUGIN);
+
+	copy ();
+	clearSelectedCells ();
+}
+
+void RKMatrixInput::paste () {
+	RK_TRACE (PLUGIN);
+
+	int left = 0;
+	int top = 0;
+	QItemSelectionRange range = display->getSelectionBoundaries ();
+	if (range.isValid ()) {
+		left = range.left ();
+		top = range.top ();
+	}
+
+	RKTextMatrix pasted = RKTextMatrix::matrixFromClipboard ();
+	int height = allow_user_resize_rows ? pasted.numRows () : qMin (pasted.numRows (), row_count->intValue () - top);
+	int width = allow_user_resize_columns ? pasted.numColumns () : qMin (pasted.numColumns (), column_count->intValue () - left);
+	
+	updating_tsv_data = true;
+	for (int c = 0; c < width; ++c) {
+		for (int r = 0; r < height; ++r) {
+			setCellValue (r + top, c + left, pasted.getText (r, c));
+		}
+	}
+	updating_tsv_data = false;
+	updateAll ();
+}
+
+
+
+
 ///////////////////////////////////////////////////////////////////////////////////////
 
 RKMatrixInputModel::RKMatrixInputModel (RKMatrixInput* _matrix) : QAbstractTableModel (_matrix) {
@@ -397,10 +477,7 @@
 	}
 
 	// 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);
-	}
+	QString value = matrix->cellValue (row, column);
 	if ((role == Qt::DisplayRole) || (role == Qt::EditRole)) {
 		return QVariant (value);
 	} else if (role == Qt::BackgroundRole) {

Modified: trunk/rkward/rkward/plugin/rkmatrixinput.h
===================================================================
--- trunk/rkward/rkward/plugin/rkmatrixinput.h	2012-11-06 09:20:00 UTC (rev 4412)
+++ trunk/rkward/rkward/plugin/rkmatrixinput.h	2012-11-06 12:52:05 UTC (rev 4413)
@@ -23,9 +23,14 @@
 #include <QDomElement>
 #include <QStringList>
 
-class QTableView;
+class RKTableView;
 class RKMatrixInputModel;
 
+/* TODO:
+ * - key-handling: del, backspace, left / right while editing
+ * - cursor-placement after editing trailing rows / columns
+ */
+
 /** Provides a table for editing one- or two-dimensional data
   *@author Thomas Friedrichsmeier
   */
@@ -37,6 +42,10 @@
 	int type () { return ComponentMatrixInput; };
 	bool isValid () { return is_valid; };
 	QString value (const QString &modifier);
+public slots:
+	void cut ();
+	void copy ();
+	void paste ();
 private slots:
 	void dimensionPropertyChanged (RKComponentPropertyBase *property);
 	void tsvPropertyChanged ();
@@ -65,6 +74,7 @@
 	void updateAll ();
 
 	void setCellValue (int row, int column, const QString& value);
+	QString cellValue (int row, int column) const;
 	void setColumnValue (int column, const QString& value);
 	void updateColumn (int column);
 	bool expandStorageForColumn (int column);
@@ -82,11 +92,13 @@
 	};
 	QList<Column> columns;
 
-	QTableView *display;
+	RKTableView *display;
 	RKMatrixInputModel *model;
 
 	// to avoid recursion:
 	bool updating_tsv_data;
+
+	void clearSelectedCells ();
 };
 
 #include <QAbstractTableModel>

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