1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
#include "QFitsViewTableHeader.h"
#include "QFitsGlobal.h"
QFitsViewTableHeader::QFitsViewTableHeader(Qt::Orientation orientation, QWidget* parent)
: QHeaderView(orientation, parent)
{qFitsDebug("QFitsViewTableHeader::QFitsViewTableHeader [consider inheriting as well from QFitsBaseView ?]");
}
QRect QFitsViewTableHeader::sectionRect(int logicalIndex) const
{
return sections.value(logicalIndex);
}
void QFitsViewTableHeader::mouseDoubleClickEvent(QMouseEvent* event)
{
QHeaderView::mouseDoubleClickEvent(event);
const int idx = visualIndexAt(pick(event->pos()));
const QModelIndex persistent = model()->index(idx, 0);
if (event->button() & Qt::LeftButton)
edit(persistent, QAbstractItemView::DoubleClicked, event);
}
void QFitsViewTableHeader::paintSection(QPainter* painter, const QRect& rect, int logicalIndex) const
{
sections.insert(logicalIndex, rect);
QHeaderView::paintSection(painter, rect, logicalIndex);
}
int QFitsViewTableHeader::pick(const QPoint& pos) const
{
return orientation() == Qt::Horizontal ? pos.x() : pos.y();
}
void QFitsViewTableHeader::headerDataChanged(Qt::Orientation orientation, int logicalFirst, int logicalLast) {
emit renamePlotLabels();
}
QFitsHeaderDelegate::QFitsHeaderDelegate(Qt::Orientation orientation, QObject* parent)
: QItemDelegate(parent), orientation(orientation)
{
}
QWidget* QFitsHeaderDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
Q_UNUSED(option);
if (!index.isValid())
return 0;
QWidget* editor = 0;
QFitsViewTableHeader* header = qobject_cast<QFitsViewTableHeader*>(QObject::parent());
if (header)
{
const int logicalIndex = header->logicalIndex(index.row());
const QVariant data = index.model()->headerData(logicalIndex, orientation, Qt::EditRole);
const QVariant::Type type = static_cast<QVariant::Type>(data.userType());
editor = editorFactory()->createEditor(type, parent);
}
return editor;
}
void QFitsHeaderDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
{
QFitsViewTableHeader* header = qobject_cast<QFitsViewTableHeader*>(parent());
if (header)
{
const int logicalIndex = header->logicalIndex(index.row());
const QVariant value = index.model()->headerData(logicalIndex, orientation, Qt::EditRole);
QByteArray name = editor->metaObject()->userProperty().name();
if (name.isEmpty())
name = editorFactory()->valuePropertyName(static_cast<QVariant::Type>(value.userType()));
if (!name.isEmpty())
editor->setProperty(name, value);
}
}
void QFitsHeaderDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
{
QFitsViewTableHeader* header = qobject_cast<QFitsViewTableHeader*>(parent());
if (header)
{
const int logicalIndex = header->logicalIndex(index.row());
const QVariant value = index.model()->headerData(logicalIndex, orientation, Qt::EditRole);
QByteArray name = editor->metaObject()->userProperty().name();
if (name.isEmpty())
name = editorFactory()->valuePropertyName(static_cast<QVariant::Type>(value.userType()));
if (!name.isEmpty())
model->setHeaderData(logicalIndex, orientation, editor->property(name), Qt::EditRole);
}
}
void QFitsHeaderDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
Q_UNUSED(option);
QFitsViewTableHeader* header = qobject_cast<QFitsViewTableHeader*>(parent());
if (header)
{
const int logicalIndex = header->logicalIndex(index.row());
const QRect rect = header->sectionRect(logicalIndex);
editor->setGeometry(rect);
}
}
const QItemEditorFactory* QFitsHeaderDelegate::editorFactory() const
{
const QItemEditorFactory* factory = itemEditorFactory();
if (factory == 0)
factory = QItemEditorFactory::defaultFactory();
return factory;
}
|