diff mbox series

kernel-shark: Make the text to be copyable.

Message ID 20230115184412.46720-1-y.karadz@gmail.com (mailing list archive)
State Accepted
Commit 5599c4c111d27da9964c0b2e649e4af4461b5bc4
Headers show
Series kernel-shark: Make the text to be copyable. | expand

Commit Message

Yordan Karadzhov Jan. 15, 2023, 6:44 p.m. UTC
Implement an item delegate class for the Table widget. This will
make possible the text of the records to be selected and copied
so that it can be pasted into another windows.

Signed-off-by: Yordan Karadzhov <y.karadz@gmail.com>
---
 src/KsModels.cpp      | 14 ++++++++++++++
 src/KsModels.hpp      |  2 ++
 src/KsTraceViewer.cpp | 27 ++++++++++++++++++++++++---
 src/KsTraceViewer.hpp | 22 ++++++++++++++++++++++
 4 files changed, 62 insertions(+), 3 deletions(-)
diff mbox series

Patch

diff --git a/src/KsModels.cpp b/src/KsModels.cpp
index df8373e..e10f411 100644
--- a/src/KsModels.cpp
+++ b/src/KsModels.cpp
@@ -482,6 +482,20 @@  size_t KsViewModel::search(int column,
 	return matchList->count();
 }
 
+/** @brief Reimplements handler to return a flags that enables the item
+ *	   (ItemIsEnabled) and allows it to be selecte.
+ *
+ * @param index: The index of the item.
+ *
+ * @returns Returns the item flags for the given index.
+ */
+Qt::ItemFlags KsViewModel::flags(const QModelIndex &index) const {
+	if (!index.isValid())
+	    return Qt::ItemIsEnabled;
+
+	return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
+}
+
 /** Create a default (empty) KsFilterProxyModel object. */
 KsGraphModel::KsGraphModel(QObject *parent)
 : QAbstractTableModel(parent)
diff --git a/src/KsModels.hpp b/src/KsModels.hpp
index 3a6d3f1..2883ce3 100644
--- a/src/KsModels.hpp
+++ b/src/KsModels.hpp
@@ -130,6 +130,8 @@  public:
 		TRACE_VIEW_N_COLUMNS,
 	};
 
+	Qt::ItemFlags flags(const QModelIndex &index) const;
+
 private:
 	void _updateHeader();
 
diff --git a/src/KsTraceViewer.cpp b/src/KsTraceViewer.cpp
index 12a95a7..05f5461 100644
--- a/src/KsTraceViewer.cpp
+++ b/src/KsTraceViewer.cpp
@@ -19,6 +19,26 @@ 
 #include "KsTraceViewer.hpp"
 #include "KsWidgetsLib.hpp"
 
+/**
+ * Reimplemented handler for creating delegate widget.
+ */
+QWidget *KsTableItemDelegate::createEditor(QWidget *parent,
+					   const QStyleOptionViewItem &option,
+					   const QModelIndex &index) const {
+	QTextEdit *edit = new QTextEdit(parent);
+	edit->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
+	edit->setReadOnly(true);
+	return edit;
+}
+
+/**
+ * Reimplemented handler setting the data shown by the delegate widget.
+ */
+void KsTableItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const {
+	QTextEdit *textEditor = qobject_cast<QTextEdit *>(editor);
+	textEditor->setPlainText(_model->getValueStr(index.column(), index.row()));
+}
+
 /**
  * Reimplemented handler for mouse press events. Right mouse click events will
  * be ignored. This is done because we want the Right click is being used to
@@ -47,13 +67,13 @@  void KsTableView::scrollTo(const QModelIndex &index, ScrollHint hint)
 	QTableView::scrollTo(index, hint);
 }
 
-
 /** Create a default (empty) Trace viewer widget. */
 KsTraceViewer::KsTraceViewer(QWidget *parent)
 : KsWidgetsLib::KsDataWidget(parent),
   _view(this),
   _model(this),
   _proxyModel(this),
+  _itemDelegate(&_model, this),
   _toolbar(this),
   _labelSearch("Search: Column", this),
   _labelGrFollows("Graph follows  ", this),
@@ -144,7 +164,7 @@  KsTraceViewer::KsTraceViewer(QWidget *parent)
 	/* Initialize the trace viewer. */
 	_view.horizontalHeader()->setDefaultAlignment(Qt::AlignLeft);
 	_view.verticalHeader()->setVisible(false);
-	_view.setEditTriggers(QAbstractItemView::NoEditTriggers);
+	_view.setEditTriggers(QAbstractItemView::DoubleClicked);
 	_view.setSelectionBehavior(QAbstractItemView::SelectRows);
 	_view.setSelectionMode(QAbstractItemView::SingleSelection);
 	_view.verticalHeader()->setDefaultSectionSize(defaultRowHeight);
@@ -152,7 +172,8 @@  KsTraceViewer::KsTraceViewer(QWidget *parent)
 	_view.horizontalHeader()->setFont(
 		QFontDatabase::systemFont(QFontDatabase::GeneralFont));
 
-	 _proxyModel.setSource(&_model);
+	_view.setItemDelegate(&_itemDelegate);
+	_proxyModel.setSource(&_model);
 	_selectionModel.setModel(&_proxyModel);
 	_view.setModel(&_proxyModel);
 	_view.setSelectionModel(&_selectionModel);
diff --git a/src/KsTraceViewer.hpp b/src/KsTraceViewer.hpp
index ae1929d..99e4d38 100644
--- a/src/KsTraceViewer.hpp
+++ b/src/KsTraceViewer.hpp
@@ -22,6 +22,26 @@ 
 #include "KsDualMarker.hpp"
 #include "KsWidgetsLib.hpp"
 
+/**
+ * Class used to customize the display of the data items from the model.
+ */
+class KsTableItemDelegate : public QItemDelegate
+{
+public:
+	/** Create KsTableItemDelegate. */
+	KsTableItemDelegate(KsViewModel *model, QWidget *parent = nullptr)
+	: QItemDelegate(parent),
+	  _model(model) {};
+
+private:
+	QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
+			      const QModelIndex &index) const override;
+
+	void setEditorData(QWidget *editor, const QModelIndex &index) const override;
+
+	KsViewModel *_model;
+};
+
 /**
  * Table View class, needed in order to reimplemented the handler for mouse
  * press events.
@@ -108,6 +128,8 @@  private:
 
 	QItemSelectionModel 	_selectionModel;
 
+	KsTableItemDelegate	_itemDelegate;
+
 	QToolBar	_toolbar;
 
 	QLabel		_labelSearch, _labelGrFollows;