File indexing completed on 2024-05-12 03:48:26
0001 /* 0002 File : SpreadsheetItemDelegate.cpp 0003 Project : LabPlot 0004 -------------------------------------------------------------------- 0005 SPDX-FileCopyrightText: 2007 Tilman Benkert <thzs@gmx.net> 0006 SPDX-FileCopyrightText: 2010-2020 Alexander Semke <alexander.semke@web.de> 0007 SPDX-License-Identifier: GPL-2.0-or-later 0008 */ 0009 0010 #include "SpreadsheetItemDelegate.h" 0011 #include "backend/spreadsheet/SpreadsheetModel.h" 0012 0013 #include <QAbstractItemModel> 0014 #include <QKeyEvent> 0015 #include <QMetaProperty> 0016 #include <QPainter> 0017 0018 /*! 0019 \class SpreadsheetItemDelegate 0020 \brief Item delegate for SpreadsheetView. 0021 0022 Overides QItemDelegate::paint() and provides shaded representation 0023 of masked cells used in SpreadsheetView. 0024 0025 \ingroup commonfrontend 0026 */ 0027 0028 SpreadsheetItemDelegate::SpreadsheetItemDelegate(QObject* parent) 0029 : QItemDelegate(parent) { 0030 installEventFilter(this); 0031 } 0032 0033 void SpreadsheetItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const { 0034 QItemDelegate::paint(painter, option, index); 0035 if (!index.data(static_cast<int>(SpreadsheetModel::CustomDataRole::MaskingRole)).toBool()) 0036 return; 0037 0038 painter->save(); 0039 painter->fillRect(option.rect, QBrush(m_maskingColor, Qt::BDiagPattern)); 0040 painter->restore(); 0041 } 0042 0043 void SpreadsheetItemDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const { 0044 model->setData(index, editor->metaObject()->userProperty().read(editor), Qt::EditRole); 0045 } 0046 0047 void SpreadsheetItemDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const { 0048 editor->metaObject()->userProperty().write(editor, index.data(Qt::EditRole)); 0049 } 0050 0051 bool SpreadsheetItemDelegate::eventFilter(QObject* editor, QEvent* event) { 0052 if (event->type() == QEvent::KeyPress) { 0053 auto* keyEvent = static_cast<QKeyEvent*>(event); 0054 if (keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter) { 0055 Q_EMIT commitData(static_cast<QWidget*>(editor)); 0056 Q_EMIT closeEditor(static_cast<QWidget*>(editor), QAbstractItemDelegate::NoHint); 0057 Q_EMIT returnPressed(); 0058 return true; 0059 } 0060 } else if (event->type() == QEvent::InputMethodQuery) { 0061 Q_EMIT editorEntered(); 0062 return true; 0063 } 0064 0065 return QItemDelegate::eventFilter(editor, event); 0066 }