qtutilities/settingsdialog/optioncategorymodel.cpp

119 lines
3.3 KiB
C++
Raw Normal View History

2015-09-06 20:19:21 +02:00
#include "./optioncategorymodel.h"
#include "./optioncategory.h"
2015-04-22 18:57:44 +02:00
2015-12-20 21:35:07 +01:00
#ifdef GUI_QTWIDGETS
# include <QApplication>
# include <QStyle>
#endif
2015-04-22 18:57:44 +02:00
namespace Dialogs {
/*!
* \class Dialogs::OptionCategoryModel
* \brief The OptionCategoryModel class is used by SettingsDialog to store and display option categories.
*/
/*!
* \brief Constructs an option category model.
*/
OptionCategoryModel::OptionCategoryModel(QObject *parent) :
QAbstractListModel(parent)
{}
/*!
* \brief Constructs an option category model with the specified \a categories.
2016-06-10 23:05:43 +02:00
* \remarks The model takes ownership over the given categories.
2015-04-22 18:57:44 +02:00
*/
2016-06-10 23:05:43 +02:00
OptionCategoryModel::OptionCategoryModel(const QList<OptionCategory *> &categories, QObject *parent) :
2015-04-22 18:57:44 +02:00
QAbstractListModel(parent),
m_categories(categories)
{
for(OptionCategory *category : m_categories) {
2015-04-22 18:57:44 +02:00
category->setParent(this);
}
}
/*!
* \brief Destroys the option category model.
*/
OptionCategoryModel::~OptionCategoryModel()
{}
/*!
* \brief Sets the \a categories for the model.
*
* The model takes ownership over the given \a categories.
*/
void OptionCategoryModel::setCategories(const QList<OptionCategory *> categories)
{
beginResetModel();
qDeleteAll(m_categories);
m_categories = categories;
for(OptionCategory *category : m_categories) {
2015-04-22 18:57:44 +02:00
category->setParent(this);
connect(category, &OptionCategory::displayNameChanged, this, &OptionCategoryModel::categoryChangedName);
connect(category, &OptionCategory::iconChanged, this, &OptionCategoryModel::categoryChangedIcon);
}
endResetModel();
}
int OptionCategoryModel::rowCount(const QModelIndex &parent) const
{
return parent.isValid() ? 0 : m_categories.size();
}
QVariant OptionCategoryModel::data(const QModelIndex &index, int role) const
{
if(index.isValid() && index.row() < m_categories.size()) {
switch (role) {
case Qt::DisplayRole:
return m_categories.at(index.row())->displayName();
case Qt::DecorationRole: {
2015-12-20 21:35:07 +01:00
const QIcon &icon = m_categories.at(index.row())->icon();
2015-04-22 18:57:44 +02:00
if(!icon.isNull()) {
2015-12-20 21:35:07 +01:00
return icon.pixmap(
#ifdef GUI_QTWIDGETS
QApplication::style()->pixelMetric(QStyle::PM_LargeIconSize)
#else
QSize(32, 32)
#endif
);
2015-04-22 18:57:44 +02:00
}
}
}
}
return QVariant();
}
/*!
* \brief Handles the change of name of a category.
*/
void OptionCategoryModel::categoryChangedName()
{
if(OptionCategory *senderCategory = qobject_cast<OptionCategory *>(QObject::sender())) {
for(int i = 0, end = m_categories.size(); i < end; ++i) {
if(senderCategory == m_categories.at(i)) {
QModelIndex index = this->index(i);
emit dataChanged(index, index, QVector<int>() << Qt::DisplayRole);
}
}
}
}
/*!
* \brief Handles the a changed icon of a category.
*/
void OptionCategoryModel::categoryChangedIcon()
{
if(OptionCategory *senderCategory = qobject_cast<OptionCategory *>(QObject::sender())) {
for(int i = 0, end = m_categories.size(); i < end; ++i) {
if(senderCategory == m_categories.at(i)) {
QModelIndex index = this->index(i);
emit dataChanged(index, index, QVector<int>() << Qt::DecorationRole);
}
}
}
}
}