labplot  2.8.2
About: LabPlot is an application for plotting and analysis of 2D and 3D functions and data. It is a complete rewrite of LabPlot1 and lacks in the first release a lot of features available in the predecessor. On the other hand, the GUI and the usability is more superior.
  Fossies Dox: labplot-2.8.2.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

SpreadsheetItemDelegate.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  File : SpreadsheetItemDelegate.cpp
3  Project : LabPlot
4  --------------------------------------------------------------------
5  Copyright : (C) 2007 by Tilman Benkert (thzs@gmx.net)
6  Copyright : (C) 2010-2020 by Alexander Semke (alexander.semke@web.de)
7 
8  ***************************************************************************/
9 
10 /***************************************************************************
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * *
17  * This program is distributed in the hope that it will be useful, *
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
20  * GNU General Public License for more details. *
21  * *
22  * You should have received a copy of the GNU General Public License *
23  * along with this program; if not, write to the Free Software *
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
25  * Boston, MA 02110-1301 USA *
26  * *
27  ***************************************************************************/
30 
31 #include <QAbstractItemModel>
32 #include <QKeyEvent>
33 #include <QMetaProperty>
34 #include <QPainter>
35 
36 /*!
37 \class SpreadsheetItemDelegate
38 \brief Item delegate for SpreadsheetView.
39 
40 Overides QItemDelegate::paint() and provides shaded representation
41 of masked cells used in SpreadsheetView.
42 
43 \ingroup commonfrontend
44 */
45 
46 SpreadsheetItemDelegate::SpreadsheetItemDelegate(QObject* parent) : QItemDelegate(parent) {
47  installEventFilter(this);
48 }
49 
50 void SpreadsheetItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option,
51  const QModelIndex& index) const {
52 
53  QItemDelegate::paint(painter, option, index);
54  if (!index.data(static_cast<int>(SpreadsheetModel::CustomDataRole::MaskingRole)).toBool())
55  return;
56 
57  painter->save();
58  painter->fillRect(option.rect, QBrush(m_maskingColor, Qt::BDiagPattern));
59  painter->restore();
60 }
61 
62 void SpreadsheetItemDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index ) const {
63  model->setData(index, editor->metaObject()->userProperty().read(editor), Qt::EditRole);
64 }
65 
66 void SpreadsheetItemDelegate::setEditorData(QWidget* editor, const QModelIndex& index ) const {
67  editor->metaObject()->userProperty().write(editor, index.data(Qt::EditRole));
68 }
69 
70 bool SpreadsheetItemDelegate::eventFilter(QObject* editor, QEvent* event) {
71  if (event->type() == QEvent::KeyPress) {
72  auto* keyEvent = static_cast<QKeyEvent*>(event);
73  if (keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter) {
74  emit commitData((QWidget*)editor);
75  emit closeEditor((QWidget*)editor, QAbstractItemDelegate::NoHint);
76  emit returnPressed();
77  return true;
78  }
79  } else if (event->type() == QEvent::InputMethodQuery) {
80  emit editorEntered();
81  return true;
82  }
83 
84  return QItemDelegate::eventFilter(editor, event);
85 }
SpreadsheetItemDelegate(QObject *parent=nullptr)
void setEditorData(QWidget *, const QModelIndex &) const override
bool eventFilter(QObject *, QEvent *) override
void setModelData(QWidget *, QAbstractItemModel *, const QModelIndex &) const override
void paint(QPainter *, const QStyleOptionViewItem &, const QModelIndex &) const override
@ MaskingRole
bool determining whether the cell is masked