test_sdk/gui/gtl_gui_chart_markers_model...

96 lines
2.8 KiB
C++
Raw Permalink Normal View History

#include "gtl_gui_chart_markers_model.h"
namespace gtl
{
namespace gui
{
chart_markers_model::chart_markers_model(QObject *parent)
: QAbstractItemModel{parent}
{
}
chart_marker *chart_markers_model::at(int idx) const
{
return _markers[idx];
}
void chart_markers_model::add(chart_marker *marker)
{
beginInsertRows(QModelIndex(), (int)_markers.size(), (int)_markers.size());
_markers.push_back(marker);
connect(marker, &chart_marker::position_changed, this, &chart_markers_model::marker_changed);
connect(marker, &chart_marker::idx_changed, this, &chart_markers_model::marker_index_changed);
connect(marker, &chart_marker::deleting, this, &chart_markers_model::marker_deleting);
endInsertRows();
// for openPersistentEditor
emit dataChanged(index(-1, -1), index(-1, -1));
}
void chart_markers_model::remove(chart_marker *marker)
{
auto it = std::find(_markers.begin(), _markers.end(), marker);
if(it == _markers.end())
return;
int idx = std::distance(_markers.begin(), it);
beginRemoveRows(QModelIndex(), idx, idx);
_markers.erase(it);
endRemoveRows();
}
int chart_markers_model::rowCount(const QModelIndex &parent) const
{
if(parent.isValid())
return 0;
return _markers.size();
}
QModelIndex chart_markers_model::index(int row, int column, const QModelIndex &parent) const
{
return createIndex(row, column);
}
QModelIndex chart_markers_model::parent(const QModelIndex &index) const
{
return QModelIndex();
}
void chart_markers_model::connect_color_changed_signal(color_box *sender, const QModelIndex &index)
{
connect(sender, &color_box::color_changed, _markers[index.row()], &chart_marker::set_color);
}
void chart_markers_model::connect_kill_signal(QAbstractButton *sender, const QModelIndex &index)
{
connect(sender, &QAbstractButton::clicked, _markers[index.row()], &chart_marker::deleteLater);
}
void chart_markers_model::marker_index_changed()
{
beginResetModel();
std::sort(_markers.begin(), _markers.end(), [](chart_marker* marker0, chart_marker* marker1){return marker0->idx() < marker1->idx();});
endResetModel();
emit changed();
// emit dataChanged(index(0, 0), index(rowCount() - 1, columnCount() - 1));
}
void chart_markers_model::marker_deleting()
{
remove(static_cast<chart_marker*>(sender()));
}
}
}