Commit a62e50a2 authored by Eric Stanton's avatar Eric Stanton

ENH: Support icons as decorations for Qt list views.

parent 28656fb4
......@@ -48,7 +48,7 @@
#include "vtkTable.h"
#include "vtkViewTheme.h"
vtkCxxRevisionMacro(vtkQtListView, "1.8");
vtkCxxRevisionMacro(vtkQtListView, "1.9");
vtkStandardNewMacro(vtkQtListView);
......@@ -57,13 +57,14 @@ vtkQtListView::vtkQtListView()
{
this->ApplyColors = vtkSmartPointer<vtkApplyColors>::New();
this->DataObjectToTable = vtkSmartPointer<vtkDataObjectToTable>::New();
this->ApplyColors->SetInputConnection(0, this->DataObjectToTable->GetOutputPort(0));
this->DataObjectToTable->SetFieldType(vtkDataObjectToTable::VERTEX_DATA);
this->FieldType = vtkQtListView::VERTEX_DATA;
this->ListView = new QListView();
this->TableAdapter = new vtkQtTableModelAdapter();
this->TableAdapter->SetRowColorStrategy(vtkQtTableModelAdapter::ITEM);
this->TableAdapter->SetDecorationLocation(vtkQtTableModelAdapter::ITEM);
this->TableSorter = new QSortFilterProxyModel();
this->TableSorter->setFilterCaseSensitivity(Qt::CaseInsensitive);
this->TableSorter->setFilterRole(Qt::DisplayRole);
......@@ -71,7 +72,8 @@ vtkQtListView::vtkQtListView()
this->ListView->setModel(this->TableSorter);
this->ListView->setModelColumn(0);
this->TableSorter->setFilterKeyColumn(0);
this->TableAdapter->SetColorColumnName("vtkApplyColors color");
// Set up some default properties
this->ListView->setSelectionMode(QAbstractItemView::ExtendedSelection);
this->ListView->setSelectionBehavior(QAbstractItemView::SelectRows);
......@@ -81,8 +83,10 @@ vtkQtListView::vtkQtListView()
this->LastMTime = 0;
this->ApplyRowColors = false;
this->VisibleColumn = 0;
this->TableAdapter->SetDecorationStrategy(vtkQtTableModelAdapter::NONE);
this->ColorArrayNameInternal = 0;
this->IconIndexArrayNameInternal = 0;
double defCol[3] = {0.827,0.827,0.827};
this->ApplyColors->SetDefaultPointColor(defCol);
this->ApplyColors->SetUseCurrentAnnotationColor(true);
......@@ -132,26 +136,31 @@ void vtkQtListView::SetFieldType(int type)
}
}
void vtkQtListView::SetApplyRowColors(bool value)
void vtkQtListView::SetIconSheet(QImage sheet)
{
if(value != this->ApplyRowColors)
{
if(value)
{
this->DataObjectToTable->SetInputConnection(0, this->ApplyColors->GetOutputPort());
}
else
{
vtkDataRepresentation* rep = this->GetRepresentation();
if (rep)
{
vtkAlgorithmOutput *conn = rep->GetInputConnection();
this->DataObjectToTable->SetInputConnection(0, conn);
}
}
this->ApplyRowColors = value;
this->Modified();
}
this->TableAdapter->SetIconSheet(sheet);
}
void vtkQtListView::SetIconSheetSize(int w, int h)
{
this->TableAdapter->SetIconSheetSize(w,h);
}
void vtkQtListView::SetIconSize(int w, int h)
{
this->TableAdapter->SetIconSize(w,h);
}
void vtkQtListView::SetIconArrayName(const char* name)
{
this->SetIconIndexArrayNameInternal(name);
this->TableAdapter->SetIconIndexColumnName(name);
}
void vtkQtListView::SetDecorationStrategy(int value)
{
this->TableAdapter->SetDecorationStrategy(value);
this->Modified();
}
//----------------------------------------------------------------------------
......@@ -192,27 +201,11 @@ void vtkQtListView::SetVisibleColumn(int col)
void vtkQtListView::AddRepresentationInternal(vtkDataRepresentation* rep)
{
vtkAlgorithmOutput *selConn, *annConn, *conn;
vtkAlgorithmOutput *annConn, *conn;
conn = rep->GetInputConnection();
annConn = rep->GetInternalAnnotationOutputPort();
selConn = rep->GetInternalSelectionOutputPort();
if(!this->ApplyRowColors)
{
this->DataObjectToTable->SetInputConnection(0, conn);
}
this->ApplyColors->SetInputConnection(0, conn);
vtkSmartPointer<vtkSelection> empty =
vtkSmartPointer<vtkSelection>::New();
vtkSmartPointer<vtkSelectionNode> emptyNode =
vtkSmartPointer<vtkSelectionNode>::New();
emptyNode->SetContentType(vtkSelectionNode::INDICES);
vtkSmartPointer<vtkIdTypeArray> arr =
vtkSmartPointer<vtkIdTypeArray>::New();
emptyNode->SetSelectionList(arr);
empty->AddNode(emptyNode);
this->DataObjectToTable->SetInputConnection(0, conn);
if(annConn)
{
......@@ -220,15 +213,13 @@ void vtkQtListView::AddRepresentationInternal(vtkDataRepresentation* rep)
}
}
void vtkQtListView::RemoveRepresentationInternal(vtkDataRepresentation* rep)
{
vtkAlgorithmOutput *selConn, *annConn, *conn;
vtkAlgorithmOutput *annConn, *conn;
conn = rep->GetInputConnection();
selConn = rep->GetInternalSelectionOutputPort();
annConn = rep->GetInternalAnnotationOutputPort();
this->ApplyColors->RemoveInputConnection(0, conn);
this->DataObjectToTable->RemoveInputConnection(0, conn);
this->ApplyColors->RemoveInputConnection(1, annConn);
this->TableAdapter->SetVTKDataObject(0);
}
......@@ -334,21 +325,13 @@ void vtkQtListView::Update()
this->GetMTime() > this->LastMTime ||
atime > this->LastSelectionMTime)
{
this->TableAdapter->SetVTKDataObject(0);
if(this->ApplyRowColors)
{
this->ApplyColors->Update();
}
this->DataObjectToTable->Update();
this->ApplyColors->Update();
this->TableAdapter->SetVTKDataObject(0);
this->TableAdapter->SetVTKDataObject(this->ApplyColors->GetOutput());
this->TableAdapter->SetVTKDataObject(this->DataObjectToTable->GetOutput());
if(this->ApplyRowColors)
{
this->TableAdapter->SetColorColumnName("vtkApplyColors color");
}
this->TableAdapter->SetColorColumnName("vtkApplyColors color");
this->TableAdapter->SetIconIndexColumnName(this->IconIndexArrayNameInternal);
if (atime > this->LastSelectionMTime)
{
......
......@@ -86,10 +86,11 @@ public:
void SetAlternatingRowColors(bool);
// Description:
// Whether or not to add an icon to the row header denoting the color
// of an annotated row.
void SetApplyRowColors(bool value);
vtkGetMacro(ApplyRowColors, bool);
// The strategy for how to decorate rows.
// Should be one of vtkQtTableModelAdapter::COLORS,
// vtkQtTableModelAdapter::ICONS, or
// vtkQtTableModelAdapter::NONE. Default is NONE.
void SetDecorationStrategy(int);
// Description:
// The array to use for coloring items in view. Default is "color".
......@@ -110,6 +111,13 @@ public:
// The column used to filter on
void SetFilterRegExp(const QRegExp& pattern);
// Description:
// Set the icon ivars. Only used if the decoration strategy is set to ICONS.
void SetIconSheet(QImage sheet);
void SetIconSize(int w, int h);
void SetIconSheetSize(int w, int h);
void SetIconArrayName(const char* name);
virtual void ApplyViewTheme(vtkViewTheme* theme);
// Description:
......@@ -135,11 +143,14 @@ private:
vtkSetStringMacro(ColorArrayNameInternal);
vtkGetStringMacro(ColorArrayNameInternal);
vtkSetStringMacro(IconIndexArrayNameInternal);
vtkGetStringMacro(IconIndexArrayNameInternal);
QPointer<QListView> ListView;
vtkQtTableModelAdapter* TableAdapter;
QSortFilterProxyModel* TableSorter;
char* ColorArrayNameInternal;
char* IconIndexArrayNameInternal;
char* VisibleColumnName;
bool SortSelectionToTop;
bool ApplyRowColors;
......
......@@ -25,6 +25,7 @@
#include "vtkTable.h"
#include "vtkIdList.h"
#include "vtkIdTypeArray.h"
#include "vtkIntArray.h"
#include "vtkSelection.h"
#include "vtkSelectionNode.h"
#include "vtkSmartPointer.h"
......@@ -42,6 +43,7 @@
#include <QMimeData>
#include <QPair>
#include <QPixmap>
#include <QImage>
#include <vtkstd/set>
#include <vtksys/ios/sstream>
......@@ -71,8 +73,12 @@ vtkQtTableModelAdapter::vtkQtTableModelAdapter(QObject* p)
this->Internal = new vtkInternal;
this->Table = NULL;
this->SplitMultiComponentColumns = false;
this->RowColorStrategy = vtkQtTableModelAdapter::HEADER;
this->DecorationLocation = vtkQtTableModelAdapter::HEADER;
this->DecorationStrategy = vtkQtTableModelAdapter::NONE;
this->ColorColumn = -1;
this->IconIndexColumn = -1;
this->IconSheetSize[0] = this->IconSheetSize[1] = 0;
this->IconSize[0] = this->IconSize[1] = 0;
}
//----------------------------------------------------------------------------
......@@ -81,8 +87,12 @@ vtkQtTableModelAdapter::vtkQtTableModelAdapter(vtkTable* t, QObject* p)
{
this->Internal = new vtkInternal;
this->SplitMultiComponentColumns = false;
this->RowColorStrategy = vtkQtTableModelAdapter::HEADER;
this->DecorationLocation = vtkQtTableModelAdapter::HEADER;
this->DecorationStrategy = vtkQtTableModelAdapter::NONE;
this->ColorColumn = -1;
this->IconIndexColumn = -1;
this->IconSheetSize[0] = this->IconSheetSize[1] = 0;
this->IconSize[0] = this->IconSize[1] = 0;
if (this->Table != NULL)
{
this->Table->Register(0);
......@@ -139,6 +149,46 @@ void vtkQtTableModelAdapter::SetColorColumnName(const char* name)
}
}
//----------------------------------------------------------------------------
void vtkQtTableModelAdapter::SetIconIndexColumnName(const char* name)
{
int color_column = this->IconIndexColumn;
if (name == 0 || !this->Table)
{
this->IconIndexColumn = -1;
}
else if (this->SplitMultiComponentColumns)
{
this->IconIndexColumn = -1;
int color_index=0;
foreach(QString columnname, this->Internal->ModelColumnNames)
{
if (columnname == name)
{
this->IconIndexColumn = color_index;
break;
}
color_index++;
}
}
else
{
this->IconIndexColumn = -1;
for (int i = 0; i < static_cast<int>(this->Table->GetNumberOfColumns()); i++)
{
if (!strcmp(name, this->Table->GetColumn(i)->GetName()))
{
this->IconIndexColumn = i;
break;
}
}
}
if (this->IconIndexColumn != color_column)
{
emit this->reset();
}
}
//----------------------------------------------------------------------------
void vtkQtTableModelAdapter::SetKeyColumnName(const char* name)
{
......@@ -397,11 +447,20 @@ void vtkQtTableModelAdapter::SetSplitMultiComponentColumns(bool value)
}
//----------------------------------------------------------------------------
void vtkQtTableModelAdapter::SetRowColorStrategy(int s)
void vtkQtTableModelAdapter::SetDecorationStrategy(int s)
{
if (s != this->DecorationStrategy)
{
this->DecorationStrategy = s;
}
}
//----------------------------------------------------------------------------
void vtkQtTableModelAdapter::SetDecorationLocation(int s)
{
if (s != this->RowColorStrategy)
if (s != this->DecorationLocation)
{
this->RowColorStrategy = s;
this->DecorationLocation = s;
}
}
......@@ -440,10 +499,16 @@ QVariant vtkQtTableModelAdapter::data(const QModelIndex &idx, int role) const
// Return a byte array if they ask for a decorate role
if (role == Qt::DecorationRole)
{
if(this->RowColorStrategy == vtkQtTableModelAdapter::ITEM && this->ColorColumn >= 0)
if(this->DecorationStrategy == vtkQtTableModelAdapter::COLORS &&
this->DecorationLocation == vtkQtTableModelAdapter::ITEM && this->ColorColumn >= 0)
{
return this->getColorIcon(idx.row());
}
else if(this->DecorationStrategy == vtkQtTableModelAdapter::ICONS &&
this->DecorationLocation == vtkQtTableModelAdapter::ITEM && this->IconIndexColumn >= 0)
{
return this->getIcon(idx.row());
}
return this->Internal->IndexToDecoration[idx];
}
......@@ -539,9 +604,11 @@ QVariant vtkQtTableModelAdapter::headerData(int section, Qt::Orientation orienta
return QVariant(v.ToString().c_str());
}
}
else if(role == Qt::DecorationRole && this->ColorColumn >= 0 && this->RowColorStrategy == vtkQtTableModelAdapter::HEADER)
else if(role == Qt::DecorationRole &&
this->DecorationStrategy == vtkQtTableModelAdapter::ICONS &&
this->DecorationLocation == vtkQtTableModelAdapter::ITEM && this->IconIndexColumn >= 0)
{
return this->getColorIcon(section);
return this->getIcon(section);
}
}
......@@ -767,3 +834,47 @@ QVariant vtkQtTableModelAdapter::getColorIcon(int row) const
return QVariant();
}
QVariant vtkQtTableModelAdapter::getIcon(int row) const
{
int column;
if (this->GetSplitMultiComponentColumns())
{
column = this->Internal->ModelColumnToTableColumn[this->IconIndexColumn].first;
}
else
{
column = this->ModelColumnToFieldDataColumn(this->IconIndexColumn);
}
vtkIntArray* icon_indices = vtkIntArray::SafeDownCast(this->Table->GetColumn(column));
if (!icon_indices)
{
return QVariant();
}
int icon_idx = icon_indices->GetValue(row);
int x, y;
int dimX = this->IconSheetSize[0]/this->IconSize[0];
int dimY = this->IconSheetSize[1]/this->IconSize[1];
x = (icon_idx >= dimX) ? icon_idx % dimX : icon_idx;
x *= this->IconSize[0];
y = (icon_idx >= dimX) ? static_cast<int>(icon_idx/dimX) : 0;
y *= this->IconSize[1];
return this->IconSheet.copy(x, y, this->IconSize[0], this->IconSize[1]);
}
void vtkQtTableModelAdapter::SetIconSheet(QImage sheet)
{
this->IconSheet = sheet;
}
void vtkQtTableModelAdapter::SetIconSheetSize(int w, int h)
{
this->IconSheetSize[0] = w;
this->IconSheetSize[1] = h;
}
void vtkQtTableModelAdapter::SetIconSize(int w, int h)
{
this->IconSize[0] = w;
this->IconSize[1] = h;
}
......@@ -31,10 +31,12 @@
#include "vtkQtAbstractModelAdapter.h"
#include <QMimeData>
#include <QImage>
class vtkSelection;
class vtkTable;
class vtkVariant;
class QVTK_EXPORT vtkQtTableModelAdapter : public vtkQtAbstractModelAdapter
{
Q_OBJECT
......@@ -58,6 +60,7 @@ public:
virtual void SetKeyColumnName(const char* name);
virtual void SetColorColumnName(const char* name);
void SetIconIndexColumnName(const char* name);
//BTX
enum
......@@ -67,10 +70,24 @@ public:
};
//ETX
//BTX
enum
{
COLORS = 0,
ICONS = 1,
NONE = 2
};
//ETX
// Description:
// Specify how to color rows if colors are provided by SetColorColumnName().
// Default is the vertical header.
void SetDecorationLocation(int s);
// Description:
// Specify how to color rows if colors are provided by SetColorColumnName().
// Default is the vertical header.
void SetRowColorStrategy(int s);
void SetDecorationStrategy(int s);
bool GetSplitMultiComponentColumns() const;
void SetSplitMultiComponentColumns(bool value);
......@@ -95,6 +112,10 @@ public:
virtual QStringList mimeTypes () const ;
Qt::DropActions supportedDropActions() const;
void SetIconSheet(QImage sheet);
void SetIconSize(int w, int h);
void SetIconSheetSize(int w, int h);
signals:
void selectionDropped(vtkSelection*);
......@@ -104,11 +125,16 @@ private:
bool noTableCheck() const;
void updateModelColumnHashTables();
QVariant getColorIcon(int row) const;
QVariant getIcon(int row) const;
bool SplitMultiComponentColumns;
vtkTable* Table;
char* ColorColumnName;
int RowColorStrategy;
int DecorationLocation;
int DecorationStrategy;
QImage IconSheet;
int IconSize[2];
int IconSheetSize[2];
int IconIndexColumn;
class vtkInternal;
vtkInternal* Internal;
......
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