Commit 65464683 authored by Volker Krause's avatar Volker Krause
Browse files

Add generic enum editing combobox for the schema model

parent af024725
......@@ -42,6 +42,8 @@ set(analyzer_srcs
surveyeditor/surveydialog.cpp
surveyeditor/surveyeditor.cpp
widgets/metaenumcombobox.cpp
)
add_executable(UserFeedbackAnalyzerApplication ${analyzer_srcs})
......
......@@ -104,6 +104,8 @@ QVariant SchemaModel::data(const QModelIndex& index, int role) const
case 2:
if (role == Qt::DisplayRole)
return m_product.schema().at(index.row()).aggregationType();
if (role == Qt::EditRole)
return QVariant::fromValue(m_product.schema().at(index.row()).aggregationType());
}
} else {
const auto entry = m_product.schema().at(index.internalId());
......@@ -116,7 +118,7 @@ QVariant SchemaModel::data(const QModelIndex& index, int role) const
if (role == Qt::DisplayRole)
return elem.type(); // TODO stringify
else if (role == Qt::EditRole)
return elem.type();
return QVariant::fromValue(elem.type());
}
}
......@@ -159,6 +161,9 @@ bool SchemaModel::setData(const QModelIndex &index, const QVariant &value, int r
case 1:
entry.setType(value.value<SchemaEntry::Type>());
break;
case 2:
entry.setAggregationType(value.value<SchemaEntry::AggregationType>());
break;
}
} else {
auto &entry = schema[index.internalId()];
......
......@@ -17,7 +17,9 @@
#include "schemaentryitemeditorfactory.h"
#include <core/schemaentry.h>
#include <core/schemaentryelement.h>
#include "schemaentrytypecombobox.h"
#include <widgets/metaenumcombobox.h>
#include <QDebug>
......@@ -26,4 +28,6 @@ using namespace UserFeedback::Analyzer;
SchemaEntryItemEditorFactory::SchemaEntryItemEditorFactory()
{
registerEditor(qMetaTypeId<SchemaEntry::Type>(), new QStandardItemEditorCreator<SchemaEntryTypeComboBox>());
registerEditor(qMetaTypeId<SchemaEntry::AggregationType>(), new QStandardItemEditorCreator<MetaEnumComboBox>());
registerEditor(qMetaTypeId<SchemaEntryElement::Type>(), new QStandardItemEditorCreator<MetaEnumComboBox>());
}
/*
Copyright (C) 2016 Volker Krause <vkrause@kde.org>
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "metaenumcombobox.h"
#include <QDebug>
#include <QMetaEnum>
#include <QMetaType>
using namespace UserFeedback::Analyzer;
MetaEnumComboBox::MetaEnumComboBox(QWidget* parent) :
QComboBox(parent)
{
}
MetaEnumComboBox::~MetaEnumComboBox()
{
}
QVariant MetaEnumComboBox::value() const
{
auto value = m_value;
*(static_cast<int*>(value.data())) = currentData().toInt();
return value;
}
void MetaEnumComboBox::setValue(const QVariant& value)
{
clear();
m_value = value;
const auto mo = QMetaType::metaObjectForType(value.userType());
if (!mo)
return;
const QByteArray typeName = value.typeName();
const auto idx = typeName.lastIndexOf("::");
if (idx <= 0)
return;
const auto enumName = typeName.mid(idx + 2);
const auto enumIdx = mo->indexOfEnumerator(enumName);
if (enumIdx < 0)
return;
const auto me = mo->enumerator(enumIdx);
for (int i = 0; i < me.keyCount(); ++i) {
addItem(QLatin1String(me.key(i)), me.value(i));
if (me.value(i) == value.toInt())
setCurrentIndex(i);
}
}
/*
Copyright (C) 2016 Volker Krause <vkrause@kde.org>
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef USERFEEDBACK_ANALYZER_METAENUMCOMBOBOX_H
#define USERFEEDBACK_ANALYZER_METAENUMCOMBOBOX_H
#include <QComboBox>
namespace UserFeedback {
namespace Analyzer {
class MetaEnumComboBox : public QComboBox
{
Q_OBJECT
Q_PROPERTY(QVariant value READ value WRITE setValue USER true)
public:
explicit MetaEnumComboBox(QWidget *parent = nullptr);
~MetaEnumComboBox();
QVariant value() const;
void setValue(const QVariant &value);
private:
QVariant m_value;
};
}
}
#endif // USERFEEDBACK_ANALYZER_METAENUMCOMBOBOX_H
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment