Skip to content
Snippets Groups Projects
Commit 96cb7232 authored by Wuttke, Joachim's avatar Wuttke, Joachim
Browse files

Cloned GUI/View/List/InstrumentsQModel -> GUI/Model/Type/ModelForSet

parent 172c8f2b
No related branches found
No related tags found
1 merge request!2397InstrumentsSet and SamplesSet based on SetWithModel, which owns a QListModel
// ************************************************************************************************
//
// BornAgain: simulate and fit reflection and scattering
//
//! @file GUI/Model/Type/ModelForSet.cpp
//! @brief Implements class InstrumentsQModel.
//!
//! @homepage http://www.bornagainproject.org
//! @license GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2018
//! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
// ************************************************************************************************
#include "GUI/View/List/InstrumentsQModel.h"
#include "Base/Util/Assert.h"
#include "GUI/Model/Device/InstrumentItems.h"
#include "GUI/Model/Device/InstrumentsSet.h"
#include "GUI/Model/Project/ProjectDocument.h"
#include <QColor>
#include <QUuid>
InstrumentsQModel::InstrumentsQModel(QObject* parent)
: QAbstractListModel(parent) // parent needed to ensure end of life
{
}
InstrumentsSet* InstrumentsQModel::set()
{
return gDoc->instrumentsModifier();
}
const InstrumentsSet* InstrumentsQModel::set() const
{
return gDoc->instruments();
}
int InstrumentsQModel::rowCount(const QModelIndex&) const
{
return set()->size();
}
QVariant InstrumentsQModel::data(const QModelIndex& index, int role) const
{
if (!index.isValid())
return {};
size_t row = index.row();
if (row >= set()->size())
return {};
const InstrumentItem* t = set()->at(row);
switch (role) {
case Qt::DisplayRole:
return t->name();
case Qt::ToolTipRole:
return t->description();
case Qt::BackgroundRole:
return row == set()->currentIndex() ? QColor(Qt::cyan) : QColor(Qt::white);
default:
return {};
}
}
void InstrumentsQModel::deleteItem()
{
const size_t row = set()->currentIndex();
ASSERT(row != size_t(-1));
beginRemoveRows({}, row, row);
set()->delete_current();
endRemoveRows();
emit set()->setChanged();
}
void InstrumentsQModel::cloneItem()
{
const InstrumentItem* t = set()->currentItem();
ASSERT(t);
InstrumentItem* t2 = t->createItemCopy();
t2->setId(QUuid::createUuid().toString());
pushItem(t2);
}
void InstrumentsQModel::pushItem(InstrumentItem* t)
{
if (!t)
return;
const size_t row = set()->size();
beginInsertRows({}, row, row);
set()->push_back(t);
endInsertRows();
emit set()->setChanged();
}
// ************************************************************************************************
//
// BornAgain: simulate and fit reflection and scattering
//
//! @file GUI/Model/Type/ModelForSet.h
//! @brief Defines class InstrumentsQModel.
//!
//! @homepage http://www.bornagainproject.org
//! @license GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2018
//! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
// ************************************************************************************************
#ifndef BORNAGAIN_GUI_MODEL_TYPE_MODELFORSET_H
#define BORNAGAIN_GUI_MODEL_TYPE_MODELFORSET_H
#include <QAbstractListModel>
class InstrumentItem;
class InstrumentsSet;
//! Data model for InstrumentsQListView.
class InstrumentsQModel : public QAbstractListModel {
public:
InstrumentsQModel(QObject* parent);
int rowCount(const QModelIndex&) const override;
QVariant data(const QModelIndex& index, int role) const override;
void deleteItem();
void cloneItem();
void pushItem(InstrumentItem*);
private:
InstrumentsSet* set();
const InstrumentsSet* set() const;
};
#endif // BORNAGAIN_GUI_MODEL_TYPE_MODELFORSET_H
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