105 lines
3.1 KiB
C++
105 lines
3.1 KiB
C++
|
#include "gtl_gui_chart_single_markers.h"
|
||
|
|
||
|
namespace gtl
|
||
|
{
|
||
|
namespace gui
|
||
|
{
|
||
|
chart_single_markers::chart_single_markers(QObject *parent)
|
||
|
: chart_markers_model{parent}
|
||
|
{
|
||
|
|
||
|
}
|
||
|
|
||
|
QVariant chart_single_markers::headerData(int section, Qt::Orientation orientation, int role) const
|
||
|
{
|
||
|
if(role == Qt::DisplayRole)
|
||
|
{
|
||
|
if(orientation == Qt::Horizontal)
|
||
|
{
|
||
|
if(section == 0)
|
||
|
return tr("ID");
|
||
|
else if(section == 1)
|
||
|
return tr("pos");
|
||
|
else if(section == 2)
|
||
|
return tr("color");
|
||
|
else if(section == 3)
|
||
|
return tr("kill");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return QVariant();
|
||
|
}
|
||
|
|
||
|
int chart_single_markers::columnCount(const QModelIndex &parent) const
|
||
|
{
|
||
|
return 4;
|
||
|
}
|
||
|
|
||
|
QVariant chart_single_markers::data(const QModelIndex &index, int role) const
|
||
|
{
|
||
|
if (!index.isValid())
|
||
|
return QVariant();
|
||
|
|
||
|
if(role == Qt::DisplayRole)
|
||
|
{
|
||
|
if(index.column() == 0)
|
||
|
return _markers[index.row()]->idx() + 1;
|
||
|
else if(index.column() == 1)
|
||
|
return _markers[index.row()]->pos();
|
||
|
else if(index.column() == 2)
|
||
|
return _markers[index.row()]->color();
|
||
|
}
|
||
|
|
||
|
return QVariant();
|
||
|
}
|
||
|
|
||
|
bool chart_single_markers::setData(const QModelIndex &index, const QVariant &value, int role)
|
||
|
{
|
||
|
if(index.column() == 1)
|
||
|
{
|
||
|
bool is_ok;
|
||
|
qreal double_value = value.toDouble(&is_ok);
|
||
|
|
||
|
if (is_ok)
|
||
|
{
|
||
|
|
||
|
_markers[index.row()]->set_pos(double_value);
|
||
|
|
||
|
emit dataChanged(chart_markers_model::index(index.row(), 1), chart_markers_model::index(index.row(), 3), {Qt::DisplayRole});
|
||
|
return true;
|
||
|
}
|
||
|
}
|
||
|
else if(index.column() == 2)
|
||
|
{
|
||
|
_markers[index.row()]->set_color(value.value<QColor>());
|
||
|
|
||
|
emit dataChanged(chart_markers_model::index(index.row(), 2), chart_markers_model::index(index.row(), 2), {Qt::DisplayRole});
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
Qt::ItemFlags chart_single_markers::flags(const QModelIndex &index) const
|
||
|
{
|
||
|
if (!index.isValid())
|
||
|
return Qt::NoItemFlags;
|
||
|
|
||
|
Qt::ItemFlags flags = QAbstractItemModel::flags(index);
|
||
|
|
||
|
if(index.column() >= 1 && index.column() <= 2)
|
||
|
flags |= Qt::ItemIsEditable;
|
||
|
|
||
|
return flags;
|
||
|
}
|
||
|
|
||
|
void chart_single_markers::marker_changed()
|
||
|
{
|
||
|
auto it = std::find(_markers.begin(), _markers.end(), sender());
|
||
|
int idx = std::distance(_markers.begin(), it);
|
||
|
if(idx < _markers.size())
|
||
|
emit dataChanged(index(idx, 1), index(idx, 1));
|
||
|
}
|
||
|
}
|
||
|
}
|