1 /****************************************************************************
2 **
3 ** Copyright (C) 2021 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25 
26 #include "itemlibraryassetsdirsmodel.h"
27 #include "itemlibraryassetsmodel.h"
28 
29 #include <QDebug>
30 #include <QMetaProperty>
31 
32 namespace QmlDesigner {
33 
ItemLibraryAssetsDirsModel(QObject * parent)34 ItemLibraryAssetsDirsModel::ItemLibraryAssetsDirsModel(QObject *parent)
35     : QAbstractListModel(parent)
36 {
37     // add roles
38     const QMetaObject meta = ItemLibraryAssetsDir::staticMetaObject;
39     for (int i = meta.propertyOffset(); i < meta.propertyCount(); ++i)
40         m_roleNames.insert(i, meta.property(i).name());
41 }
42 
data(const QModelIndex & index,int role) const43 QVariant ItemLibraryAssetsDirsModel::data(const QModelIndex &index, int role) const
44 {
45     if (!index.isValid()) {
46         qWarning() << Q_FUNC_INFO << "Invalid index requested: " << QString::number(index.row());
47         return {};
48     }
49 
50     if (m_roleNames.contains(role))
51         return m_dirs[index.row()]->property(m_roleNames[role]);
52 
53     qWarning() << Q_FUNC_INFO << "Invalid role requested: " << QString::number(role);
54     return {};
55 }
56 
setData(const QModelIndex & index,const QVariant & value,int role)57 bool ItemLibraryAssetsDirsModel::setData(const QModelIndex &index, const QVariant &value, int role)
58 {
59     // currently only dirExpanded property is updatable
60     if (index.isValid() && m_roleNames.contains(role)) {
61         QVariant currValue = m_dirs.at(index.row())->property(m_roleNames.value(role));
62         if (currValue != value) {
63             m_dirs.at(index.row())->setProperty(m_roleNames.value(role), value);
64             if (m_roleNames.value(role) == "dirExpanded")
65                 ItemLibraryAssetsModel::saveExpandedState(value.toBool(), m_dirs.at(index.row())->dirPath());
66             emit dataChanged(index, index, {role});
67             return true;
68         }
69     }
70     return false;
71 }
72 
rowCount(const QModelIndex & parent) const73 int ItemLibraryAssetsDirsModel::rowCount(const QModelIndex &parent) const
74 {
75     Q_UNUSED(parent)
76 
77     return m_dirs.size();
78 }
79 
roleNames() const80 QHash<int, QByteArray> ItemLibraryAssetsDirsModel::roleNames() const
81 {
82     return m_roleNames;
83 }
84 
addDir(ItemLibraryAssetsDir * assetsDir)85 void ItemLibraryAssetsDirsModel::addDir(ItemLibraryAssetsDir *assetsDir)
86 {
87     m_dirs.append(assetsDir);
88 }
89 
assetsDirs() const90 const QList<ItemLibraryAssetsDir *> ItemLibraryAssetsDirsModel::assetsDirs() const
91 {
92     return m_dirs;
93 }
94 
95 } // namespace QmlDesigner
96