LibreCAD
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Macros
layermodel.cpp
Go to the documentation of this file.
1 #include "layermodel.h"
2 
3 LayerModel::LayerModel(QObject *parent) :
4  QAbstractTableModel(parent) {
5 
6  _editIcon = QIcon(":/icons/layeredit");
7  _lockedIcon = QIcon(":/icons/locked.svg");
8  _unlockedIcon = QIcon(":/icons/unlocked.svg");
9 }
10 
11 void LayerModel::setLayers(std::vector<lc::Layer_CSPtr> layers) {
12  beginResetModel();
13 
14  _layers = layers;
15 
16  endResetModel();
17 }
18 
19 int LayerModel::rowCount(const QModelIndex&) const {
20  return _layers.size();
21 }
22 
23 int LayerModel::columnCount(const QModelIndex&) const {
24  return LAST;
25 }
26 
27 QVariant LayerModel::data(const QModelIndex& index, int role) const {
28  if (!index.isValid()) {
29  return QVariant();
30  }
31 
32  auto layer = _layers.at(index.row());
33 
34  if(role == Qt::DisplayRole && index.column() == NAME) {
35  return layer->name().c_str();
36  }
37  else if(role == Qt::DecorationRole) {
38  switch (index.column()) {
39  case EDIT:
40  return _editIcon;
41 
42  case LOCKED:
43  if (layer->isFrozen()) {
44  return _lockedIcon;
45  }
46  return _unlockedIcon;
47  }
48  }
49 
50  return QVariant();
51 }
52 
53 Qt::ItemFlags LayerModel::flags(const QModelIndex& index) const {
54  if(index.column() == NAME) {
55  return Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled;
56  }
57 
58  return Qt::ItemIsEnabled;
59 }
60 
61 
62 bool LayerModel::setData(const QModelIndex& index, const QVariant& value, int) {
63  if(!index.isValid()) {
64  return false;
65  }
66 
67  emit nameChanged(_layers.at(index.row()), value.toString().toStdString());
68  return true;
69 }
70 
71 lc::Layer_CSPtr LayerModel::layerAt(int row) {
72  try {
73  return _layers.at(row);
74  }
75  catch(std::out_of_range& e) {
76  return nullptr;
77  }
78 }
79 
80 unsigned int LayerModel::indexOf(lc::Layer_CSPtr layer) {
81  for(unsigned int i = 0; i < _layers.size(); i++) {
82  if(_layers[i] == layer) {
83  return i;
84  }
85  }
86 
87  return 0;
88 }
lc::Layer_CSPtr layerAt(int row)
Get layer at specific row.
Definition: layermodel.cpp:71
QIcon _editIcon
Definition: layermodel.h:67
Qt::ItemFlags flags(const QModelIndex &index) const
Definition: layermodel.cpp:53
void nameChanged(lc::Layer_CSPtr &layer, const std::string &name)
Signal when layer name was changed by double clicking.
LayerModel(QObject *parent=0)
Create widget.
Definition: layermodel.cpp:3
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
Definition: layermodel.cpp:27
unsigned int indexOf(lc::Layer_CSPtr layer)
Give the index of the given layer.
Definition: layermodel.cpp:80
QIcon _lockedIcon
Definition: layermodel.h:68
std::vector< lc::Layer_CSPtr > _layers
Definition: layermodel.h:65
int columnCount(const QModelIndex &parent=QModelIndex()) const
Definition: layermodel.cpp:23
QIcon _unlockedIcon
Definition: layermodel.h:69
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
Definition: layermodel.cpp:62
void setLayers(std::vector< lc::Layer_CSPtr > layers)
Update the list of layers.
Definition: layermodel.cpp:11
int rowCount(const QModelIndex &parent=QModelIndex()) const
Get number of layers.
Definition: layermodel.cpp:19