File indexing completed on 2024-05-12 16:35:36

0001 /* This file is part of the KDE project
0002    Copyright 2008 Stefan Nikolaus stefan.nikolaus@kdemail.net
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017    Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include "ReadOnlyTableModel.h"
0021 
0022 // Sheets
0023 #include "Cell.h"
0024 #include "kspread_limits.h"
0025 #include "Map.h"
0026 #include "Sheet.h"
0027 #include "Style.h"
0028 #include "Value.h"
0029 #include "ValueFormatter.h"
0030 
0031 // Qt
0032 #include <QBrush>
0033 #include <QSize>
0034 
0035 using namespace KSpread;
0036 
0037 class ReadOnlyTableModel::Private
0038 {
0039 public:
0040     Sheet* sheet;
0041     QSize size;
0042 };
0043 
0044 ReadOnlyTableModel::ReadOnlyTableModel(Sheet* sheet, int columns, int rows)
0045         : QAbstractTableModel(sheet)
0046         , d(new Private)
0047 {
0048     d->sheet = sheet;
0049     d->size = (columns && rows) ? QSize(columns, rows) : QSize(KS_colMax, KS_rowMax);
0050 }
0051 
0052 ReadOnlyTableModel::~ReadOnlyTableModel()
0053 {
0054     delete d;
0055 }
0056 
0057 int ReadOnlyTableModel::columnCount(const QModelIndex& parent) const
0058 {
0059     Q_UNUSED(parent);
0060     return (d->sheet->usedArea() & QRect(QPoint(1, 1), d->size)).width();
0061 }
0062 
0063 int ReadOnlyTableModel::rowCount(const QModelIndex& parent) const
0064 {
0065     Q_UNUSED(parent);
0066     return (d->sheet->usedArea() & QRect(QPoint(1, 1), d->size)).height();
0067 }
0068 
0069 QVariant ReadOnlyTableModel::data(const QModelIndex& index, int role) const
0070 {
0071     // NOTE Model indices start from 0, while Calligra Sheets column/row indices start from 1.
0072     const Cell cell = Cell(d->sheet, index.column() + 1, index.row() + 1).masterCell();
0073     const Style style = cell.effectiveStyle();
0074     if (role == Qt::DisplayRole) {
0075         // Display a formula if warranted.  If not, simply display the value.
0076         if (cell.isFormula() && d->sheet->getShowFormula() &&
0077                 !(d->sheet->isProtected() && style.hideFormula())) {
0078             return QVariant(cell.userInput());
0079         } else if (d->sheet->getHideZero() && cell.value().isNumber() && cell.value().asFloat() == 0.0) {
0080             // Hide zero.
0081             return QVariant();
0082         } else if (!cell.isEmpty()) {
0083             // Format the value appropriately and set the display text.
0084             // The format of the resulting value is used below to determine the alignment.
0085             Value value = d->sheet->map()->formatter()->formatText(cell.value(), style.formatType(),
0086                           style.precision(), style.floatFormat(),
0087                           style.prefix(), style.postfix(),
0088                           style.currency().symbol());
0089             return value.asString();
0090         }
0091     } else if (role == Qt::EditRole) {
0092         return cell.userInput();
0093     } else if (role == Qt::ToolTipRole) {
0094         return cell.comment();
0095     } else if (role == Qt::SizeHintRole) {
0096         // TODO
0097     } else if (role == Qt::FontRole) {
0098         return style.font();
0099     } else if (role == Qt::TextAlignmentRole) {
0100         // TODO
0101     } else if (role == Qt::BackgroundRole) {
0102         return style.backgroundBrush();
0103     } else if (role == Qt::BackgroundColorRole) {
0104         return style.backgroundColor();
0105     } else if (role == Qt::ForegroundRole) {
0106         return style.fontColor();
0107     }
0108     return QVariant();
0109 }
0110 
0111 QVariant ReadOnlyTableModel::headerData(int section, Qt::Orientation orientation, int role) const
0112 {
0113     // NOTE Model indices start from 0, while Calligra Sheets column/row indices start from 1.
0114     if (role == Qt::DisplayRole) {
0115         if (orientation == Qt::Horizontal) {
0116             return Cell::columnName(section + 1);
0117         } else {
0118             return QString::number(section + 1);
0119         }
0120     }
0121     return QVariant();
0122 }
0123 
0124 QModelIndex ReadOnlyTableModel::index(int row, int column, const QModelIndex &parent) const
0125 {
0126     QModelIndex index;
0127     // A cell in our sheet?
0128     if (!parent.isValid()) {
0129         index = createIndex(row, column, d->sheet);
0130         // Embedded in a MapModel?
0131     } else if (parent.internalPointer() == d->sheet->map()) {
0132         index = createIndex(row, column, d->sheet);
0133         // A sub-table?
0134     } else if (parent.internalPointer() == this) {
0135         // TODO sub-tables
0136     }
0137     return index;
0138 }
0139 
0140 Sheet* ReadOnlyTableModel::sheet() const
0141 {
0142     return d->sheet;
0143 }
0144 
0145 const QSize& ReadOnlyTableModel::size() const
0146 {
0147     return d->size;
0148 }