Skip to content
Snippets Groups Projects
Commit 1e543bab authored by Pospelov, Gennady's avatar Pospelov, Gennady
Browse files

SessionModelDelegates now is aware that double property can be handled either...

SessionModelDelegates now is aware that double property can be handled either in default or 'scientific' way.
parent 65b2f074
No related branches found
No related tags found
No related merge requests found
......@@ -33,6 +33,23 @@ bool isIntProperty(const QModelIndex& index)
return index.data().type() == QVariant::Int;
}
//! Returns text representation of double value depending on user defined editor type.
//! FIXME Remove this temporary function after getting rid from ScientificDoubleProperty
QString doubleToString(const SessionItem& item)
{
QString result;
Q_ASSERT(item.value().type() == QVariant::Double);
if (item.editorType() == Constants::ScientificEditorType) {
result = QString::number(item.value().toDouble(), 'g');
} else {
auto locale = QLocale::system();
result = locale.toString(item.value().toDouble(), 'f', item.decimals());
}
return result;
}
}
SessionModelDelegate::SessionModelDelegate(QObject* parent)
......@@ -49,10 +66,8 @@ void SessionModelDelegate::paint(QPainter* painter, const QStyleOptionViewItem&
paintCustomLabel(painter, option, index, text);
} else if (isDoubleProperty(index)) {
auto locale = QLocale::system();
auto item = static_cast<SessionItem*>(index.internalPointer());
QString text = locale.toString(item->value().toDouble(), 'f', item->decimals());
paintCustomLabel(painter, option, index, text);
paintCustomLabel(painter, option, index, doubleToString(*item));
} else {
QStyledItemDelegate::paint(painter, option, index);
......@@ -62,29 +77,25 @@ void SessionModelDelegate::paint(QPainter* painter, const QStyleOptionViewItem&
QWidget* SessionModelDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option,
const QModelIndex& index) const
{
if (PropertyEditorFactory::IsCustomVariant(index.data())) {
// Custom variant requires an editor based on CustomEditor.
auto item = static_cast<SessionItem*>(index.internalPointer());
auto editor = PropertyEditorFactory::CreateEditor(*item, parent);
auto customEditor = dynamic_cast<CustomEditor*>(editor);
Q_ASSERT(customEditor);
customEditor->setData(index.data());
connect(customEditor, &CustomEditor::dataChanged,
this, &SessionModelDelegate::onCustomEditorDataChanged);
return customEditor;
} else if (isDoubleProperty(index) || isIntProperty(index)) {
// Int and Double will be handled by standard spin boxes specially tunded for limits
auto item = static_cast<SessionItem*>(index.internalPointer());
auto result = PropertyEditorFactory::CreateEditor(*item, parent);
auto item = static_cast<SessionItem*>(index.internalPointer());
return PropertyEditorFactory::CreateEditor(*item, parent);
if (result) {
if(auto customEditor = dynamic_cast<CustomEditor*>(result)) {
customEditor->setData(index.data());
connect(customEditor, &CustomEditor::dataChanged,
this, &SessionModelDelegate::onCustomEditorDataChanged);
} else {
// Int and Double will be handled by standard spin boxes
// QStyledItemDelegate already knows how to handle it, no special connections are required
}
} else {
// the rest, like editors for QString, bool etc will be processed by parent delegate
return QStyledItemDelegate::createEditor(parent, option, index);
result = QStyledItemDelegate::createEditor(parent, option, index);
}
return result;
}
//! Propagates changed data from the editor to the model.
......
......@@ -131,7 +131,13 @@ QWidget* PropertyEditorFactory::CreateEditor(const SessionItem& item, QWidget* p
QWidget* result(nullptr);
if (isDoubleProperty(item.value())) {
result = createCustomDoubleEditor(item);
if (item.editorType() == Constants::ScientificEditorType) {
auto editor = new ScientificDoublePropertyEditor;
editor->setData(item.value());
result = editor;
} else {
result = createCustomDoubleEditor(item);
}
}
else if(isIntProperty(item.value())) {
......
......@@ -29,7 +29,7 @@
#include <QVBoxLayout>
namespace {
const bool show_test_view = false;
const bool show_test_view = true;
}
SessionModelView::SessionModelView(MainWindow *mainWindow)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment