2014-05-21 12:34:36 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
|
|
|
|
|
** Contact: http://www.qt-project.org/legal
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator.
|
|
|
|
|
**
|
|
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and Digia. For licensing terms and
|
|
|
|
|
** conditions see http://qt.digia.com/licensing. For further information
|
|
|
|
|
** use the contact form at http://qt.digia.com/contact-us.
|
|
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
|
|
|
** General Public License version 2.1 as published by the Free Software
|
|
|
|
|
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
|
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
|
|
|
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
|
|
|
**
|
|
|
|
|
** In addition, as a special exception, Digia gives you certain additional
|
|
|
|
|
** rights. These rights are described in the Digia Qt LGPL Exception
|
|
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "itemlibrarysectionmodel.h"
|
|
|
|
|
|
2014-05-21 15:35:03 +02:00
|
|
|
#include "itemlibraryitem.h"
|
2014-05-21 12:35:27 +02:00
|
|
|
|
2014-05-21 12:34:36 +02:00
|
|
|
namespace QmlDesigner {
|
|
|
|
|
|
2014-05-21 18:24:30 +02:00
|
|
|
ItemLibrarySectionModel::ItemLibrarySectionModel(QObject *parent) :
|
2014-05-19 16:29:38 +02:00
|
|
|
QAbstractListModel(parent)
|
|
|
|
|
{
|
2014-05-22 17:16:50 +02:00
|
|
|
addRoleNames();
|
2014-05-19 16:29:38 +02:00
|
|
|
}
|
|
|
|
|
|
2014-05-21 18:24:30 +02:00
|
|
|
ItemLibrarySectionModel::~ItemLibrarySectionModel()
|
2014-05-19 16:29:38 +02:00
|
|
|
{
|
2014-05-21 14:49:34 +02:00
|
|
|
clearItems();
|
2014-05-19 16:29:38 +02:00
|
|
|
}
|
|
|
|
|
|
2014-05-21 18:24:30 +02:00
|
|
|
int ItemLibrarySectionModel::rowCount(const QModelIndex &) const
|
2014-05-19 16:29:38 +02:00
|
|
|
{
|
2014-06-23 16:01:48 +02:00
|
|
|
return m_itemList.count();
|
2014-05-19 16:29:38 +02:00
|
|
|
}
|
|
|
|
|
|
2014-05-21 18:24:30 +02:00
|
|
|
QVariant ItemLibrarySectionModel::data(const QModelIndex &index, int role) const
|
2014-05-19 16:29:38 +02:00
|
|
|
{
|
2014-06-23 16:01:48 +02:00
|
|
|
if (!index.isValid() || index.row() + 1 > m_itemList.count()) {
|
2014-05-19 16:29:38 +02:00
|
|
|
qDebug() << Q_FUNC_INFO << "invalid index requested";
|
|
|
|
|
return QVariant();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (m_roleNames.contains(role)) {
|
2014-06-23 16:01:48 +02:00
|
|
|
QVariant value = m_itemList.at(index.row())->property(m_roleNames.value(role));
|
2014-05-19 16:29:38 +02:00
|
|
|
|
2014-05-21 18:24:30 +02:00
|
|
|
if (ItemLibrarySectionModel* model = qobject_cast<ItemLibrarySectionModel *>(value.value<QObject*>()))
|
2014-05-19 16:29:38 +02:00
|
|
|
return QVariant::fromValue(model);
|
|
|
|
|
|
2014-06-23 16:01:48 +02:00
|
|
|
return m_itemList.at(index.row())->property(m_roleNames.value(role));
|
2014-05-19 16:29:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
qWarning() << Q_FUNC_INFO << "invalid role requested";
|
|
|
|
|
|
|
|
|
|
return QVariant();
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-21 18:24:30 +02:00
|
|
|
void ItemLibrarySectionModel::clearItems()
|
2014-05-19 16:29:38 +02:00
|
|
|
{
|
|
|
|
|
beginResetModel();
|
|
|
|
|
endResetModel();
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-26 14:06:28 +02:00
|
|
|
void ItemLibrarySectionModel::addItem(ItemLibraryItem *element)
|
2014-05-19 16:29:38 +02:00
|
|
|
{
|
2014-05-26 14:05:12 +02:00
|
|
|
m_itemList.append(element);
|
2014-05-22 17:16:50 +02:00
|
|
|
|
|
|
|
|
element->setVisible(true);
|
2014-05-19 16:29:38 +02:00
|
|
|
}
|
|
|
|
|
|
2014-05-26 14:05:12 +02:00
|
|
|
const QList<ItemLibraryItem *> &ItemLibrarySectionModel::items() const
|
2014-05-19 16:29:38 +02:00
|
|
|
{
|
2014-05-26 14:05:12 +02:00
|
|
|
return m_itemList;
|
2014-05-19 16:29:38 +02:00
|
|
|
}
|
|
|
|
|
|
2014-05-21 18:24:30 +02:00
|
|
|
void ItemLibrarySectionModel::resetModel()
|
2014-05-19 16:29:38 +02:00
|
|
|
{
|
2014-06-23 17:48:15 +02:00
|
|
|
beginResetModel();
|
|
|
|
|
endResetModel();
|
2014-05-19 16:29:38 +02:00
|
|
|
}
|
|
|
|
|
|
2014-05-22 17:16:50 +02:00
|
|
|
void ItemLibrarySectionModel::addRoleNames()
|
2014-05-19 16:29:38 +02:00
|
|
|
{
|
2014-05-22 17:16:50 +02:00
|
|
|
int role = 0;
|
|
|
|
|
for (int propertyIndex = 0; propertyIndex < ItemLibraryItem::staticMetaObject.propertyCount(); ++propertyIndex) {
|
|
|
|
|
QMetaProperty property = ItemLibraryItem::staticMetaObject.property(propertyIndex);
|
|
|
|
|
m_roleNames.insert(role, property.name());
|
|
|
|
|
++role;
|
|
|
|
|
}
|
2014-05-19 16:29:38 +02:00
|
|
|
|
|
|
|
|
setRoleNames(m_roleNames);
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-21 12:34:36 +02:00
|
|
|
} // namespace QmlDesigner
|