qtutilities/settingsdialog/optioncategorymodel.cpp

126 lines
3.4 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
#ifdef QT_UTILITIES_GUI_QTWIDGETS
2017-05-01 03:16:25 +02:00
#include <QApplication>
#include <QStyle>
2015-12-20 21:35:07 +01:00
#endif
namespace QtUtilities {
2015-04-22 18:57:44 +02:00
/*!
* \class OptionCategoryModel
2017-05-04 22:46:37 +02:00
* \brief The OptionCategoryModel class is used by SettingsDialog to store and
* display option categories.
2015-04-22 18:57:44 +02:00
*/
/*!
* \brief Constructs an option category model.
*/
2017-05-01 03:16:25 +02:00
OptionCategoryModel::OptionCategoryModel(QObject *parent)
: QAbstractListModel(parent)
{
}
2015-04-22 18:57:44 +02:00
/*!
* \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
*/
2017-05-01 03:16:25 +02:00
OptionCategoryModel::OptionCategoryModel(const QList<OptionCategory *> &categories, QObject *parent)
: QAbstractListModel(parent)
, m_categories(categories)
2015-04-22 18:57:44 +02:00
{
2017-05-01 03:16:25 +02:00
for (OptionCategory *category : m_categories) {
2015-04-22 18:57:44 +02:00
category->setParent(this);
}
}
/*!
* \brief Destroys the option category model.
*/
OptionCategoryModel::~OptionCategoryModel()
2017-05-01 03:16:25 +02:00
{
}
2015-04-22 18:57:44 +02:00
/*!
* \brief Sets the \a categories for the model.
*
* The model takes ownership over the given \a categories.
*/
void OptionCategoryModel::setCategories(const QList<OptionCategory *> &categories)
2015-04-22 18:57:44 +02:00
{
beginResetModel();
qDeleteAll(m_categories);
m_categories = categories;
for (OptionCategory *const 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()) {
return QVariant();
}
switch (role) {
case Qt::DisplayRole:
return m_categories.at(index.row())->displayName();
case Qt::DecorationRole: {
const QIcon &icon = m_categories.at(index.row())->icon();
if (!icon.isNull()) {
return icon.pixmap(
#ifdef QT_UTILITIES_GUI_QTWIDGETS
QApplication::style()->pixelMetric(QStyle::PM_LargeIconSize)
2015-12-20 21:35:07 +01:00
#else
QSize(32, 32)
2015-12-20 21:35:07 +01:00
#endif
);
2017-05-01 03:16:25 +02:00
}
2015-04-22 18:57:44 +02:00
}
}
2015-04-22 18:57:44 +02:00
return QVariant();
}
/*!
* \brief Handles the change of name of a category.
*/
void OptionCategoryModel::categoryChangedName()
{
const auto *const senderCategory = qobject_cast<const OptionCategory *>(QObject::sender());
if (!senderCategory) {
return;
}
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 }));
2015-04-22 18:57:44 +02:00
}
}
}
/*!
* \brief Handles the a changed icon of a category.
*/
void OptionCategoryModel::categoryChangedIcon()
{
const auto *const senderCategory = qobject_cast<const OptionCategory *>(QObject::sender());
if (!senderCategory) {
return;
}
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 }));
2015-04-22 18:57:44 +02:00
}
}
}
} // namespace QtUtilities