| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
/*************************************************************************** |
| 3 |
* Copyright (c) 2004 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 4 |
* * |
| 5 |
* This file is part of the FreeCAD CAx development system. * |
| 6 |
* * |
| 7 |
* This library is free software; you can redistribute it and/or * |
| 8 |
* modify it under the terms of the GNU Library General Public * |
| 9 |
* License as published by the Free Software Foundation; either * |
| 10 |
* version 2 of the License, or (at your option) any later version. * |
| 11 |
* * |
| 12 |
* This library is distributed in the hope that it will be useful, * |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 15 |
* GNU Library General Public License for more details. * |
| 16 |
* * |
| 17 |
* You should have received a copy of the GNU Library General Public * |
| 18 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 19 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 20 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 21 |
* * |
| 22 |
***************************************************************************/ |
| 23 |
|
| 24 |
|
| 25 |
#ifndef PROPERTYMODEL_H |
| 26 |
#define PROPERTYMODEL_H |
| 27 |
|
| 28 |
#include <QAbstractItemModel> |
| 29 |
#include <QStringList> |
| 30 |
#include <map> |
| 31 |
#include <unordered_map> |
| 32 |
#include <vector> |
| 33 |
#include "PropertyItem.h" |
| 34 |
|
| 35 |
|
| 36 |
namespace App |
| 37 |
{ |
| 38 |
class Property; |
| 39 |
} |
| 40 |
namespace Gui |
| 41 |
{ |
| 42 |
namespace PropertyEditor |
| 43 |
{ |
| 44 |
|
| 45 |
class GuiExport PropertyModel: public QAbstractItemModel |
| 46 |
{ |
| 47 |
Q_OBJECT |
| 48 |
|
| 49 |
public: |
| 50 |
using PropertyList = std::vector<std::pair<std::string, std::vector<App::Property*>>>; |
| 51 |
|
| 52 |
PropertyModel(QObject* parent); |
| 53 |
~PropertyModel() override; |
| 54 |
|
| 55 |
QModelIndex buddy(const QModelIndex& index) const override; |
| 56 |
int columnCount(const QModelIndex& parent = QModelIndex()) const override; |
| 57 |
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override; |
| 58 |
bool setData(const QModelIndex& idx, const QVariant& value, int role) override; |
| 59 |
Qt::ItemFlags flags(const QModelIndex& index) const override; |
| 60 |
QModelIndex index(int row, int column, const QModelIndex& parent = QModelIndex()) const override; |
| 61 |
QModelIndex parent(const QModelIndex& index) const override; |
| 62 |
int rowCount(const QModelIndex& parent = QModelIndex()) const override; |
| 63 |
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; |
| 64 |
bool setHeaderData( |
| 65 |
int section, |
| 66 |
Qt::Orientation orientation, |
| 67 |
const QVariant& value, |
| 68 |
int role = Qt::EditRole |
| 69 |
) override; |
| 70 |
void buildUp(const PropertyList& props); |
| 71 |
|
| 72 |
bool removeRows(int row, int count, const QModelIndex& parent = QModelIndex()) override; |
| 73 |
|
| 74 |
void updateProperty(const App::Property&); |
| 75 |
void appendProperty(const App::Property&); |
| 76 |
void removeProperty(const App::Property&); |
| 77 |
void renameProperty(const App::Property&); |
| 78 |
|
| 79 |
QStringList propertyPathFromIndex(const QModelIndex&) const; |
| 80 |
QModelIndex propertyIndexFromPath(const QStringList&) const; |
| 81 |
|
| 82 |
private: |
| 83 |
void resetGroups(); |
| 84 |
void initGroups(); |
| 85 |
void updateChildren(PropertyItem* item, int column, const QModelIndex& parent); |
| 86 |
void findOrCreateChildren(const PropertyList& props); |
| 87 |
void insertOrMoveChildren(); |
| 88 |
void removeChildren(); |
| 89 |
|
| 90 |
struct GroupInfo |
| 91 |
{ |
| 92 |
PropertySeparatorItem* groupItem = nullptr; |
| 93 |
std::vector<PropertyItem*> children; |
| 94 |
}; |
| 95 |
GroupInfo& getGroupInfo(App::Property*); |
| 96 |
void getRange(const GroupInfo&, int& first, int& last) const; |
| 97 |
|
| 98 |
private: |
| 99 |
PropertyItem* rootItem; |
| 100 |
|
| 101 |
std::unordered_map<App::Property*, QPointer<PropertyItem>> itemMap; |
| 102 |
|
| 103 |
std::map<QString, GroupInfo> groupItems; |
| 104 |
}; |
| 105 |
|
| 106 |
} // namespace PropertyEditor |
| 107 |
} // namespace Gui |
| 108 |
|
| 109 |
|
| 110 |
#endif // PROPERTYMODEL_H |