File indexing completed on 2024-04-28 05:48:39

0001 /***************************************************************************
0002  *   This file is part of Kate build plugin                                *
0003  *   SPDX-FileCopyrightText: 2014 Kåre Särs <kare.sars@iki.fi>             *
0004  *                                                                         *
0005  *   SPDX-License-Identifier: LGPL-2.0-or-later                            *
0006  ***************************************************************************/
0007 
0008 #pragma once
0009 
0010 #include <QAbstractItemModel>
0011 #include <QByteArray>
0012 #include <limits>
0013 
0014 class TargetModel : public QAbstractItemModel
0015 {
0016     Q_OBJECT
0017 public:
0018     struct Command {
0019         QString name;
0020         QString buildCmd;
0021         QString runCmd;
0022     };
0023 
0024     struct TargetSet {
0025         TargetSet(const QString &_name, const QString &_workDir);
0026         QString name;
0027         QString workDir;
0028         QList<Command> commands;
0029     };
0030 
0031     enum RowType {
0032         RootRow,
0033         TargetSetRow,
0034         CommandRow,
0035     };
0036     Q_ENUM(RowType)
0037 
0038     enum TargetRoles {
0039         CommandRole = Qt::UserRole,
0040         CommandNameRole,
0041         WorkDirRole,
0042         SearchPathsRole,
0043         TargetSetNameRole,
0044         RowTypeRole,
0045         IsProjectTargetRole,
0046     };
0047     Q_ENUM(TargetRoles)
0048 
0049     explicit TargetModel(QObject *parent = nullptr);
0050     ~TargetModel() override;
0051 
0052     /** This function clears all the target-sets */
0053     void clear(bool setSessionFirst);
0054 
0055     /** This function returns the root item for the Session TargetSets */
0056     QModelIndex sessionRootIndex() const;
0057 
0058     /** This function returns the root item for the Project TargetSets */
0059     QModelIndex projectRootIndex() const;
0060 
0061 public Q_SLOTS:
0062 
0063     /** This function insert a target set and returns the model-index of the newly
0064      * inserted target-set */
0065     QModelIndex insertTargetSetAfter(const QModelIndex &beforeIndex, const QString &setName, const QString &workDir);
0066 
0067     /** This function adds a new command to a target-set and returns the model index */
0068     QModelIndex addCommandAfter(const QModelIndex &beforeIndex, const QString &cmdName, const QString &buildCmd, const QString &runCmd);
0069 
0070     /** This function copies the target(-set) the model index points to and returns
0071      * the model index of the copy. */
0072     QModelIndex copyTargetOrSet(const QModelIndex &index);
0073 
0074     /** This function deletes the index */
0075     void deleteItem(const QModelIndex &index);
0076 
0077     /** This function deletes the target-set with the same name */
0078     void deleteProjectTargerts();
0079 
0080     void moveRowUp(const QModelIndex &index);
0081     void moveRowDown(const QModelIndex &index);
0082 
0083     const QList<TargetSet> sessionTargetSets() const;
0084     const QList<TargetSet> projectTargetSets() const;
0085 
0086 Q_SIGNALS:
0087     void projectTargetChanged();
0088 
0089 public:
0090     static constexpr quintptr InvalidIndex = std::numeric_limits<quintptr>::max();
0091     // Model-View model functions
0092     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0093     QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
0094     bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
0095     Qt::ItemFlags flags(const QModelIndex &index) const override;
0096     QModelIndex index(int row, int column = 0, const QModelIndex &parent = QModelIndex()) const override;
0097     QModelIndex parent(const QModelIndex &child) const override;
0098     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0099     int columnCount(const QModelIndex &parent = QModelIndex()) const override;
0100 
0101     struct RootNode {
0102         bool isProject = false;
0103         QList<TargetSet> targetSets;
0104     };
0105 
0106 private:
0107     QList<RootNode> m_rootNodes;
0108 };