File indexing completed on 2024-05-12 15:27:38
0001 /*************************************************************************** 0002 File : SpreadsheetHeaderView.cpp 0003 Project : LabPlot 0004 -------------------------------------------------------------------- 0005 Copyright : (C) 2016 Alexander Semke (alexander.semke@web.de) 0006 Copyright : (C) 2007 Tilman Benkert (thzs@gmx.net) 0007 Description : Horizontal header for SpreadsheetView displaying comments in a second header 0008 0009 ***************************************************************************/ 0010 0011 /*************************************************************************** 0012 * * 0013 * This program is free software; you can redistribute it and/or modify * 0014 * it under the terms of the GNU General Public License as published by * 0015 * the Free Software Foundation; either version 2 of the License, or * 0016 * (at your option) any later version. * 0017 * * 0018 * This program is distributed in the hope that it will be useful, * 0019 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0020 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0021 * GNU General Public License for more details. * 0022 * * 0023 * You should have received a copy of the GNU General Public License * 0024 * along with this program; if not, write to the Free Software * 0025 * Foundation, Inc., 51 Franklin Street, Fifth Floor, * 0026 * Boston, MA 02110-1301 USA * 0027 * * 0028 ***************************************************************************/ 0029 0030 #include "SpreadsheetHeaderView.h" 0031 #include "SpreadsheetCommentsHeaderModel.h" 0032 #include "backend/lib/macros.h" 0033 0034 /*! 0035 \class SpreadsheetCommentsHeaderView 0036 \brief Slave header for SpreadsheetDoubleHeaderView 0037 0038 This class is only to be used by SpreadsheetDoubleHeaderView. 0039 It allows for displaying two horizontal headers in a SpreadsheetView. 0040 A SpreadsheetCommentsHeaderView displays the column comments 0041 in a second header below the normal header. It is completely 0042 controlled by a SpreadsheetDoubleHeaderView object and thus has 0043 a master-slave relationship to it. This would be an inner class 0044 of SpreadsheetDoubleHeaderView if Qt allowed this. 0045 0046 \ingroup commonfrontend 0047 */ 0048 0049 SpreadsheetCommentsHeaderView::SpreadsheetCommentsHeaderView(QWidget* parent) : QHeaderView(Qt::Horizontal, parent) { 0050 } 0051 0052 SpreadsheetCommentsHeaderView::~SpreadsheetCommentsHeaderView() { 0053 delete model(); 0054 } 0055 0056 void SpreadsheetCommentsHeaderView::setModel(QAbstractItemModel* model) { 0057 Q_ASSERT(model->inherits("SpreadsheetModel")); 0058 delete QHeaderView::model(); 0059 auto* new_model = new SpreadsheetCommentsHeaderModel(static_cast<SpreadsheetModel*>(model)); 0060 QHeaderView::setModel(new_model); 0061 } 0062 0063 /*! 0064 \class SpreadsheetDoubleHeaderView 0065 \brief Horizontal header for SpreadsheetView displaying comments in a second header 0066 0067 This class is only to be used by SpreadsheetView. 0068 It allows for displaying two horizontal headers. 0069 A \c SpreadsheetDoubleHeaderView displays the column name, plot designation, and 0070 type icon in a normal QHeaderView and below that a second header 0071 which displays the column comments. 0072 0073 \sa SpreadsheetCommentsHeaderView 0074 \sa QHeaderView 0075 0076 \ingroup commonfrontend 0077 */ 0078 SpreadsheetHeaderView::SpreadsheetHeaderView(QWidget* parent) : QHeaderView(Qt::Horizontal, parent) { 0079 setDefaultAlignment(Qt::AlignLeft | Qt::AlignVCenter); 0080 m_slave = new SpreadsheetCommentsHeaderView(); 0081 m_slave->setDefaultAlignment(Qt::AlignLeft | Qt::AlignVCenter); 0082 m_showComments = true; 0083 } 0084 0085 SpreadsheetHeaderView::~SpreadsheetHeaderView() { 0086 delete m_slave; 0087 } 0088 0089 QSize SpreadsheetHeaderView::sizeHint() const { 0090 QSize master_size = QHeaderView::sizeHint(); 0091 if (m_showComments) 0092 master_size.setHeight(master_size.height() + m_slave->sizeHint().height()); 0093 else 0094 master_size.setHeight(master_size.height()); 0095 0096 return master_size; 0097 } 0098 0099 void SpreadsheetHeaderView::setModel(QAbstractItemModel* model) { 0100 Q_ASSERT(model->inherits("SpreadsheetModel")); 0101 m_slave->setModel(model); 0102 QHeaderView::setModel(model); 0103 connect(model, &QAbstractItemModel::headerDataChanged, this, &SpreadsheetHeaderView::headerDataChanged); 0104 } 0105 0106 void SpreadsheetHeaderView::paintSection(QPainter* painter, const QRect& rect, int logicalIndex) const { 0107 QRect master_rect = rect; 0108 if (m_showComments) 0109 master_rect = rect.adjusted(0, 0, 0, -m_slave->sizeHint().height()); 0110 0111 QHeaderView::paintSection(painter, master_rect, logicalIndex); 0112 if (m_showComments && rect.height() > QHeaderView::sizeHint().height()) { 0113 QRect slave_rect = rect.adjusted(0, QHeaderView::sizeHint().height(), 0, 0); 0114 m_slave->paintSection(painter, slave_rect, logicalIndex); 0115 } 0116 } 0117 0118 /*! 0119 Returns whether comments are shown currently or not. 0120 */ 0121 bool SpreadsheetHeaderView::areCommentsShown() const { 0122 return m_showComments; 0123 } 0124 0125 /*! 0126 Show or hide (if \c on = \c false) the column comments. 0127 */ 0128 void SpreadsheetHeaderView::showComments(bool on) { 0129 m_showComments = on; 0130 refresh(); 0131 } 0132 0133 /*! 0134 adjust geometry and repaint header . 0135 */ 0136 void SpreadsheetHeaderView::refresh() { 0137 //TODO 0138 // adjust geometry and repaint header (still looking for a more elegant solution) 0139 int width = sectionSize(count()-1); 0140 m_slave->setStretchLastSection(true); // ugly hack (flaw in Qt? Does anyone know a better way?) 0141 m_slave->updateGeometry(); 0142 m_slave->setStretchLastSection(false); // ugly hack part 2 0143 setStretchLastSection(true); // ugly hack (flaw in Qt? Does anyone know a better way?) 0144 updateGeometry(); 0145 setStretchLastSection(false); // ugly hack part 2 0146 resizeSection(count()-1, width); 0147 0148 update(); 0149 } 0150 0151 /*! 0152 Reacts to a header data change. 0153 */ 0154 void SpreadsheetHeaderView::headerDataChanged(Qt::Orientation orientation, int logicalFirst, int logicalLast) { 0155 Q_UNUSED(logicalFirst); 0156 Q_UNUSED(logicalLast); 0157 if (orientation == Qt::Horizontal) 0158 refresh(); 0159 }