File indexing completed on 2024-05-19 04:56:09

0001 /**
0002  * \file texttablemodel.h
0003  * Model to display a text with tabulators in a table.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 12 Aug 2011
0008  *
0009  * Copyright (C) 2011-2024  Urs Fleisch
0010  *
0011  * This file is part of Kid3.
0012  *
0013  * Kid3 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  * Kid3 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, see <http://www.gnu.org/licenses/>.
0025  */
0026 
0027 #pragma once
0028 
0029 #include <QAbstractTableModel>
0030 #include <QList>
0031 #include <QStringList>
0032 #include "kid3api.h"
0033 
0034 /**
0035  * Model to display a text with tabulators in a table.
0036  */
0037 class KID3_CORE_EXPORT TextTableModel : public QAbstractTableModel {
0038 public:
0039   /**
0040    * Constructor.
0041    * @param parent parent widget
0042    */
0043   explicit TextTableModel(QObject* parent = nullptr);
0044 
0045   /**
0046    * Destructor.
0047    */
0048   ~TextTableModel() override = default;
0049 
0050   /**
0051    * Get item flags for index.
0052    * @param index model index
0053    * @return item flags
0054    */
0055   Qt::ItemFlags flags(const QModelIndex& index) const override;
0056 
0057   /**
0058    * Get data for a given role.
0059    * @param index model index
0060    * @param role item data role
0061    * @return data for role
0062    */
0063   QVariant data(const QModelIndex& index,
0064                 int role = Qt::DisplayRole) const override;
0065 
0066   /**
0067    * Get data for header section.
0068    * @param section column or row
0069    * @param orientation horizontal or vertical
0070    * @param role item data role
0071    * @return header data for role
0072    */
0073   QVariant headerData(int section, Qt::Orientation orientation,
0074                       int role = Qt::DisplayRole) const override;
0075 
0076   /**
0077    * Get number of rows.
0078    * @param parent parent model index, invalid for table models
0079    * @return number of rows,
0080    * if parent is valid number of children (0 for table models)
0081    */
0082   int rowCount(const QModelIndex& parent = QModelIndex()) const override;
0083 
0084   /**
0085    * Get number of columns.
0086    * @param parent parent model index, invalid for table models
0087    * @return number of columns,
0088    * if parent is valid number of children (0 for table models)
0089    */
0090   int columnCount(const QModelIndex& parent = QModelIndex()) const override;
0091 
0092   /**
0093    * Set the text to be displayed in the table.
0094    * @param text text with tab-separated columns and newline-separated rows
0095    * @param hasHeaderLine true if the first line is the header
0096    * @return true if the first line of the text contains a tab character.
0097    */
0098   bool setText(const QString& text, bool hasHeaderLine);
0099 
0100 private:
0101   QList<QStringList> m_cells;
0102   bool m_hasHeaderLine;
0103 };