libodsstream
Library for mass spectrometry
Loading...
Searching...
No Matches
qtablewriter.cpp
Go to the documentation of this file.
1/**
2 * \file odsstream/qtablewriter.cpp
3 * \date 15/03/2019
4 * \author Olivier Langella
5 * \brief write an entire table sheet from a QAbstractModel
6 */
7
8/*******************************************************************************
9 * Copyright (c) 2019 Olivier Langella <Olivier.Langella@u-psud.fr>.
10 *
11 * This file is part of the libodsstream library.
12 *
13 * libodsstream is a library to read and write ODS documents as streams
14 * Copyright (C) 2019 Olivier Langella <Olivier.Langella@u-psud.fr>
15 *
16 * This program is free software: you can redistribute it and/or modify
17 * it under the terms of the GNU Lesser General Public License as published
18 *by the Free Software Foundation, either version 3 of the License, or (at your
19 *option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU Lesser General Public License for more details.
25 *
26 * You should have received a copy of the GNU Lesser General Public License
27 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 *
29 ******************************************************************************/
30
31#include "qtablewriter.h"
32#include <QDebug>
33
35 const QAbstractProxyModel *p_table_model)
36 : mp_writer(p_writer), mp_tableModel(p_table_model)
37{
38 for(int i = 0; i < mp_tableModel->columnCount(); i++)
39 {
40 m_percentColumns.insert(std::pair<int, bool>(i, false));
41 }
42}
43
44void
45QtableWriter::setFormatPercentForColumn(const QString &column_title)
46{
47 int col_number = mp_tableModel->columnCount();
48 for(int j = 0; j < col_number; j++)
49 {
50
51 QVariant head_var =
52 mp_tableModel->headerData(j, Qt::Horizontal, Qt::DisplayRole);
53 if(head_var.toString() == column_title)
54 {
55 m_percentColumns[j] = true;
56 qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__
57 << " col=" << j;
58 }
59 }
60}
61
62void
63QtableWriter::setFormatPercentForColumn(const QModelIndex &column_index)
64{
65 qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__
66 << " col=" << column_index.column();
67 QModelIndex nIndex = mp_tableModel->mapFromSource(column_index);
68
69 qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__
70 << " col=" << nIndex.column();
71 m_percentColumns[nIndex.column()] = true;
72}
73
74void
75QtableWriter::writeSheet(const QString &sheetName)
76{
77 mp_writer->writeSheet(sheetName);
78 OdsTableSettings settings;
79 // settings.setHorizontalWindowSplit(4);
80 settings.setVerticalSplit(1);
82
83 int col_number = mp_tableModel->columnCount();
84
85 int line_number = mp_tableModel->rowCount();
86 bool ok = false;
87 for(int j = 0; j < col_number; j++)
88 {
89
90 QVariant head_var =
91 mp_tableModel->headerData(j, Qt::Horizontal, Qt::ToolTipRole);
92
93 if(head_var.isNull())
94 {
95 }
96 else
97 {
98 mp_writer->setCellAnnotation(head_var.toString());
99 }
100 head_var = mp_tableModel->headerData(j, Qt::Horizontal, Qt::DisplayRole);
101 if(head_var.isNull())
102 {
104 }
105 else
106 {
107 mp_writer->writeCell(head_var.toString());
108 }
109 }
110
111 for(int i = 0; i < line_number; i++)
112 {
114 for(int j = 0; j < col_number; j++)
115 {
116 QModelIndex index = mp_tableModel->index(i, j);
117 QVariant var = mp_tableModel->data(index, Qt::CheckStateRole);
118 if(var.isNull())
119 {
120 var = mp_tableModel->data(index, Qt::DisplayRole);
121
122 ok = false;
123 if(var.isNull())
124 {
126 }
127 else
128 {
129 double var_float = var.toDouble(&ok);
130 if(ok)
131 {
132 if(m_percentColumns[j])
133 {
134 mp_writer->writeCellPercentage(var_float);
135 }
136 else
137 {
138 mp_writer->writeCell(var_float);
139 }
140 }
141 else
142 {
143 mp_writer->writeCell(var.toString());
144 }
145 }
146 }
147 else
148 {
149 if(var == Qt::Checked)
150 {
151 mp_writer->writeCell(true);
152 }
153 else
154 {
155 mp_writer->writeCell(false);
156 }
157 }
158 }
159 }
160}
virtual void writeEmptyCell()=0
write an empty cell
virtual void writeCell(const char *cell_text)=0
write a text cell
virtual void setCellAnnotation(const QString &annotation)=0
set annotation to write in the next cell
virtual void writeCellPercentage(double value)=0
write a double as a percentage
virtual void writeSheet(const QString &sheetName)=0
open a new sheet
virtual void setCurrentOdsTableSettings(const OdsTableSettings &settings)
set ODS table settings of the current sheet (table)
virtual void writeLine()=0
open a new line
void setVerticalSplit(unsigned int position)
void setFormatPercentForColumn(const QString &column_title)
std::map< int, bool > m_percentColumns
void writeSheet(const QString &sheetName)
write the entire table in a sheet
const QAbstractProxyModel * mp_tableModel
QtableWriter(CalcWriterInterface *p_writer, const QAbstractProxyModel *p_table_model)
CalcWriterInterface * mp_writer
write an entire table sheet from a QAbstractModel