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)  

SpreadsheetHeaderView.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  File : SpreadsheetHeaderView.cpp
3  Project : LabPlot
4  --------------------------------------------------------------------
5  Copyright : (C) 2016 Alexander Semke (alexander.semke@web.de)
6  Copyright : (C) 2007 Tilman Benkert (thzs@gmx.net)
7  Description : Horizontal header for SpreadsheetView displaying comments in a second header
8 
9  ***************************************************************************/
10 
11 /***************************************************************************
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * *
18  * This program is distributed in the hope that it will be useful, *
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
21  * GNU General Public License for more details. *
22  * *
23  * You should have received a copy of the GNU General Public License *
24  * along with this program; if not, write to the Free Software *
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
26  * Boston, MA 02110-1301 USA *
27  * *
28  ***************************************************************************/
29 
30 #include "SpreadsheetHeaderView.h"
32 #include "backend/lib/macros.h"
33 
34 /*!
35  \class SpreadsheetCommentsHeaderView
36  \brief Slave header for SpreadsheetDoubleHeaderView
37 
38  This class is only to be used by SpreadsheetDoubleHeaderView.
39  It allows for displaying two horizontal headers in a SpreadsheetView.
40  A SpreadsheetCommentsHeaderView displays the column comments
41  in a second header below the normal header. It is completely
42  controlled by a SpreadsheetDoubleHeaderView object and thus has
43  a master-slave relationship to it. This would be an inner class
44  of SpreadsheetDoubleHeaderView if Qt allowed this.
45 
46  \ingroup commonfrontend
47 */
48 
49 SpreadsheetCommentsHeaderView::SpreadsheetCommentsHeaderView(QWidget* parent) : QHeaderView(Qt::Horizontal, parent) {
50 }
51 
53  delete model();
54 }
55 
56 void SpreadsheetCommentsHeaderView::setModel(QAbstractItemModel* model) {
57  Q_ASSERT(model->inherits("SpreadsheetModel"));
58  delete QHeaderView::model();
59  auto* new_model = new SpreadsheetCommentsHeaderModel(static_cast<SpreadsheetModel*>(model));
60  QHeaderView::setModel(new_model);
61 }
62 
63 /*!
64  \class SpreadsheetDoubleHeaderView
65  \brief Horizontal header for SpreadsheetView displaying comments in a second header
66 
67  This class is only to be used by SpreadsheetView.
68  It allows for displaying two horizontal headers.
69  A \c SpreadsheetDoubleHeaderView displays the column name, plot designation, and
70  type icon in a normal QHeaderView and below that a second header
71  which displays the column comments.
72 
73  \sa SpreadsheetCommentsHeaderView
74  \sa QHeaderView
75 
76  \ingroup commonfrontend
77 */
78 SpreadsheetHeaderView::SpreadsheetHeaderView(QWidget* parent) : QHeaderView(Qt::Horizontal, parent) {
79  setDefaultAlignment(Qt::AlignLeft | Qt::AlignVCenter);
81  m_slave->setDefaultAlignment(Qt::AlignLeft | Qt::AlignVCenter);
82  m_showComments = true;
83 }
84 
86  delete m_slave;
87 }
88 
90  QSize master_size = QHeaderView::sizeHint();
91  if (m_showComments)
92  master_size.setHeight(master_size.height() + m_slave->sizeHint().height());
93  else
94  master_size.setHeight(master_size.height());
95 
96  return master_size;
97 }
98 
99 void SpreadsheetHeaderView::setModel(QAbstractItemModel* model) {
100  Q_ASSERT(model->inherits("SpreadsheetModel"));
101  m_slave->setModel(model);
102  QHeaderView::setModel(model);
103  connect(model, &QAbstractItemModel::headerDataChanged, this, &SpreadsheetHeaderView::headerDataChanged);
104 }
105 
106 void SpreadsheetHeaderView::paintSection(QPainter* painter, const QRect& rect, int logicalIndex) const {
107  QRect master_rect = rect;
108  if (m_showComments)
109  master_rect = rect.adjusted(0, 0, 0, -m_slave->sizeHint().height());
110 
111  QHeaderView::paintSection(painter, master_rect, logicalIndex);
112  if (m_showComments && rect.height() > QHeaderView::sizeHint().height()) {
113  QRect slave_rect = rect.adjusted(0, QHeaderView::sizeHint().height(), 0, 0);
114  m_slave->paintSection(painter, slave_rect, logicalIndex);
115  }
116 }
117 
118 /*!
119  Returns whether comments are shown currently or not.
120 */
122  return m_showComments;
123 }
124 
125 /*!
126  Show or hide (if \c on = \c false) the column comments.
127 */
129  m_showComments = on;
130  refresh();
131 }
132 
133 /*!
134  adjust geometry and repaint header .
135 */
137  //TODO
138  // adjust geometry and repaint header (still looking for a more elegant solution)
139  int width = sectionSize(count()-1);
140  m_slave->setStretchLastSection(true); // ugly hack (flaw in Qt? Does anyone know a better way?)
141  m_slave->updateGeometry();
142  m_slave->setStretchLastSection(false); // ugly hack part 2
143  setStretchLastSection(true); // ugly hack (flaw in Qt? Does anyone know a better way?)
144  updateGeometry();
145  setStretchLastSection(false); // ugly hack part 2
146  resizeSection(count()-1, width);
147 
148  update();
149 }
150 
151 /*!
152  Reacts to a header data change.
153 */
154 void SpreadsheetHeaderView::headerDataChanged(Qt::Orientation orientation, int logicalFirst, int logicalLast) {
155  Q_UNUSED(logicalFirst);
156  Q_UNUSED(logicalLast);
157  if (orientation == Qt::Horizontal)
158  refresh();
159 }
Model class wrapping a SpreadsheetModel to display column comments in a SpreadsheetCommentsHeaderView...
Slave header for SpreadsheetDoubleHeaderView.
void setModel(QAbstractItemModel *) override
SpreadsheetCommentsHeaderView(QWidget *parent=nullptr)
QSize sizeHint() const override
void headerDataChanged(Qt::Orientation, int logicalFirst, int logicalLast)
SpreadsheetHeaderView(QWidget *parent=nullptr)
void setModel(QAbstractItemModel *) override
void paintSection(QPainter *, const QRect &, int logicalIndex) const override
SpreadsheetCommentsHeaderView * m_slave
void showComments(bool on=true)
Model for the access to a Spreadsheet.