Commit 9c6436b6 authored by LEFEBVREJP email's avatar LEFEBVREJP email
Browse files

Adding a TableView class that allows for copy functionality.

parent ead0cd0b
......@@ -22,6 +22,7 @@ TRIBITS_SUBPACKAGE(widgets)
# C/C++ headers
SET(HEADERS
tabwidget.hh
tableview.hh
)
#
# Moc the header files
......@@ -48,6 +49,7 @@ ENDIF()
# C/C++ source
SET(SOURCES
tabwidget.cc
tableview.cc
)
##---------------------------------------------------------------------------##
......
......@@ -10,7 +10,7 @@ ENDIF()
IF(USE_QT4)
SET(QT_PACKAGES QT)
ELSE()
SET(QT_PACKAGES Qt5Gui)
SET(QT_PACKAGES Qt5Gui Qt5Widgets Qt5Core)
ENDIF()
TRIBITS_PACKAGE_DEFINE_DEPENDENCIES(
......
#include "radixwidgets/tableview.hh"
#include <QApplication>
#include <QHeaderView>
#include <QClipBoard>
#include <QKeyEvent>
namespace radix
{
TableView::TableView(QWidget *parent)
: QTableView(parent)
{
}
void TableView::copy()
{
// selected items
auto selected = selectionModel()->selectedIndexes();
// selection is empty
if( selected.empty() )
{
return;
}
// data cells, unique rows, unique columns
QHash<int, QHash<int, QString>> cells;
QList<int> rows;
QList<int> cols;
// process selected indices
{
// row/column sets
QSet<int> rs;
QSet<int> cs;
// determine min/max row/columns
for( const auto &index : selected )
{
rs << index.row();
cs << index.column();
cells[index.row()][index.column()] = index.data().toString();
}
rows = rs.toList();
cols = cs.toList();
qSort( rows );
qSort( cols );
}
// row/column header models
auto rhmodel = verticalHeader()->model();
auto chmodel = horizontalHeader()->model();
// table data
QString data;
// add column headers
for( auto col : cols )
{
data.append( '\t').append(chmodel->headerData(col, Qt::Horizontal ).toString());
}
// add cell data
for( auto row : rows )
{
// add row header
data.append('\n').append(rhmodel->headerData(row, Qt::Vertical).toString());
// add row's cells
for( auto col : cols )
{
data.append( '\t' );
// row's data
auto &rd = cells[row];
// current cell is valid
if(rd.contains(col))
{
data.append(rd[col]);
}
}
}
// store in clipboard
QApplication::clipboard()->setText(data);
}
void TableView::keyPressEvent(QKeyEvent *event)
{
if(event->matches(QKeySequence::Copy) )
{
copy();
}
else
{
QTableView::keyPressEvent(event);
}
}
} // namespace radix
#ifndef RADIX_RADIXWIDGETS_TABLEVIEW_HH_
#define RADIX_RADIXWIDGETS_TABLEVIEW_HH_
#include <QTableView>
namespace radix
{
/**
* * @brief The TableWidget class
* * Allows extentions of the QTableWidget
* provides for copy
* */
class TableView : public QTableView
{
Q_OBJECT
public:
TableView(QWidget *parent = 0);
private:
void copy();
public slots:
void keyPressEvent(QKeyEvent *event);
};
} // namespace radix
#endif /** RADIX_RADIXWIDGETS_TABLEVIEW_HH_ */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment