File indexing completed on 2024-05-12 15:27:38
0001 /*************************************************************************** 0002 File : SpreadsheetItemDelegate.cpp 0003 Project : LabPlot 0004 -------------------------------------------------------------------- 0005 Copyright : (C) 2007 by Tilman Benkert (thzs@gmx.net) 0006 Copyright : (C) 2010-2020 by Alexander Semke (alexander.semke@web.de) 0007 0008 ***************************************************************************/ 0009 0010 /*************************************************************************** 0011 * * 0012 * This program is free software; you can redistribute it and/or modify * 0013 * it under the terms of the GNU General Public License as published by * 0014 * the Free Software Foundation; either version 2 of the License, or * 0015 * (at your option) any later version. * 0016 * * 0017 * This program is distributed in the hope that it will be useful, * 0018 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0019 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0020 * GNU General Public License for more details. * 0021 * * 0022 * You should have received a copy of the GNU General Public License * 0023 * along with this program; if not, write to the Free Software * 0024 * Foundation, Inc., 51 Franklin Street, Fifth Floor, * 0025 * Boston, MA 02110-1301 USA * 0026 * * 0027 ***************************************************************************/ 0028 #include "SpreadsheetItemDelegate.h" 0029 #include "backend/spreadsheet/SpreadsheetModel.h" 0030 0031 #include <QAbstractItemModel> 0032 #include <QKeyEvent> 0033 #include <QMetaProperty> 0034 #include <QPainter> 0035 0036 /*! 0037 \class SpreadsheetItemDelegate 0038 \brief Item delegate for SpreadsheetView. 0039 0040 Overides QItemDelegate::paint() and provides shaded representation 0041 of masked cells used in SpreadsheetView. 0042 0043 \ingroup commonfrontend 0044 */ 0045 0046 SpreadsheetItemDelegate::SpreadsheetItemDelegate(QObject* parent) : QItemDelegate(parent) { 0047 installEventFilter(this); 0048 } 0049 0050 void SpreadsheetItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, 0051 const QModelIndex& index) const { 0052 0053 QItemDelegate::paint(painter, option, index); 0054 if (!index.data(static_cast<int>(SpreadsheetModel::CustomDataRole::MaskingRole)).toBool()) 0055 return; 0056 0057 painter->save(); 0058 painter->fillRect(option.rect, QBrush(m_maskingColor, Qt::BDiagPattern)); 0059 painter->restore(); 0060 } 0061 0062 void SpreadsheetItemDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index ) const { 0063 model->setData(index, editor->metaObject()->userProperty().read(editor), Qt::EditRole); 0064 } 0065 0066 void SpreadsheetItemDelegate::setEditorData(QWidget* editor, const QModelIndex& index ) const { 0067 editor->metaObject()->userProperty().write(editor, index.data(Qt::EditRole)); 0068 } 0069 0070 bool SpreadsheetItemDelegate::eventFilter(QObject* editor, QEvent* event) { 0071 if (event->type() == QEvent::KeyPress) { 0072 auto* keyEvent = static_cast<QKeyEvent*>(event); 0073 if (keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter) { 0074 emit commitData((QWidget*)editor); 0075 emit closeEditor((QWidget*)editor, QAbstractItemDelegate::NoHint); 0076 emit returnPressed(); 0077 return true; 0078 } 0079 } else if (event->type() == QEvent::InputMethodQuery) { 0080 emit editorEntered(); 0081 return true; 0082 } 0083 0084 return QItemDelegate::eventFilter(editor, event); 0085 }