QXRD  0.11.16
qxrdmaskstackmodel.cpp
Go to the documentation of this file.
1 #include "qxrdmaskstackmodel.h"
2 #include "qxrdmaskstack.h"
3 #include "qcepmaskdata.h"
4 
6  m_MaskStack(masks)
7 {
9 }
10 
11 int QxrdMaskStackModel::rowCount (const QModelIndex & parent) const
12 {
13  if (parent.isValid()) {
14  return 0;
15  }
16 
17  return m_MaskStack->count();
18 }
19 
20 int QxrdMaskStackModel::columnCount(const QModelIndex & parent) const
21 {
22  if (parent.isValid()) {
23  return 0;
24  }
25 
26  return NumColumns;
27 }
28 
29 QVariant QxrdMaskStackModel::data (const QModelIndex & index, int role) const
30 {
31  if (index.row() < 0 || index.row() >= m_MaskStack->count()) {
32  return QVariant();
33  }
34 
35  QcepMaskDataPtr p = m_MaskStack->at(index.row());
36 
37  if (p) {
38  int col = index.column();
39 
40  if (columnCount()==1) {
41  if (col == 0) {
42  if (role == Qt::DecorationRole) {
43  return p->thumbnailImage();
44  } else if (role == Qt::CheckStateRole) {
45  return (p->get_Used()?Qt::Checked:Qt::Unchecked);
46  } else if (role == Qt::DisplayRole || role == Qt::EditRole) {
47  // return tr("%1 : %2").arg(m_MaskStack->stackLevelName(index.row())).arg(p->get_Title());
48  return p->get_Title();
49  }
50  }
51  } else {
52  if (col == ThumbnailColumn) {
53  if (role == Qt::DecorationRole) {
54  return p->thumbnailImage();
55  } else if (role == Qt::SizeHintRole) {
56  return p->thumbnailImageSize();
57  }
58  } else if (col == VisibilityColumn) {
59  if (role == Qt::CheckStateRole) {
60  return (p->get_Used()?Qt::Checked:Qt::Unchecked);
61  } else if (role == Qt::SizeHintRole) {
62  return 30;
63  }
64  } else if (col == TitleColumn) {
65  if (role == Qt::DisplayRole || role == Qt::EditRole) {
66  return p->get_Title();
67  } else if (role==Qt::SizeHintRole) {
68  return 120;
69  }
70  }
71  }
72  }
73 
74  return QVariant();
75 }
76 
77 QVariant QxrdMaskStackModel::headerData ( int section, Qt::Orientation orientation, int role) const
78 {
79  if (columnCount() == 1) {
80  return QVariant();
81  } else {
82  if (orientation==Qt::Horizontal && role==Qt::DisplayRole) {
83  switch (section) {
84  case VisibilityColumn:
85  return "Vis";
86 
87  case ThumbnailColumn:
88  return "";
89 
90  case TitleColumn:
91  return "Name";
92  }
93  } else if (orientation==Qt::Vertical && role==Qt::DisplayRole) {
94  return QxrdMaskStack::stackLevelName(section);
95  }
96  }
97 
98  return QVariant();
99 }
100 
101 Qt::ItemFlags QxrdMaskStackModel::flags (const QModelIndex & index) const
102 {
103  if (!index.isValid()) {
104  return QAbstractItemModel::flags(index) | Qt::ItemIsDropEnabled;
105  }
106 
107  return QAbstractItemModel::flags(index) | Qt::ItemIsSelectable |Qt::ItemIsEditable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | Qt::ItemIsUserCheckable;
108 }
109 
110 bool QxrdMaskStackModel::insertRows ( int row, int count, const QModelIndex & /*parent*/)
111 {
112  printf("QxrdMaskStackModel::insertRows(%d,%d,...)\n", row, count);
113 
114  return false;
115 }
116 
117 bool QxrdMaskStackModel::removeRows ( int row, int count, const QModelIndex & /*parent*/)
118 {
119  printf("QxrdMaskStackModel::removeRows(%d,%d,...)\n", row, count);
120 
121  return false;
122 }
123 
124 bool QxrdMaskStackModel::setData ( const QModelIndex & index, const QVariant & value, int role)
125 {
126  if (columnCount() == 1) {
127  if (index.column() == 0) {
128  if ((index.row() >= 0) && (index.row() < m_MaskStack->count())) {
129  QcepMaskDataPtr p = m_MaskStack->at(index.row());
130 
131  if ((role == Qt::EditRole || role == Qt::DisplayRole)) {
132  if (p) {
133  p->set_Title(value.toString());
134  }
135 
136  emit dataChanged(index, index);
137 
138  return true;
139  }
140 
141  if (role == Qt::CheckStateRole) {
142  if (p) {
143  p->set_Used(!(p->get_Used()));
144  }
145 
146  emit dataChanged(index, index);
147 
148  return true;
149  }
150  }
151  }
152  } else {
153  if ((index.row() >= 0) && (index.row() < m_MaskStack->count())) {
154  QcepMaskDataPtr p = m_MaskStack->at(index.row());
155 
156  if (index.column() == TitleColumn && (role == Qt::EditRole || role == Qt::DisplayRole)) {
157  if (p) {
158  p->set_Title(value.toString());
159  }
160 
161  emit dataChanged(index, index);
162 
163  return true;
164  }
165 
166  if (index.column() == VisibilityColumn && (role == Qt::CheckStateRole)) {
167  if (p) {
168  p->set_Used(!(p->get_Used()));
169  }
170 
171  emit dataChanged(index, index);
172 
173  return true;
174  }
175  }
176  }
177 
178  return false;
179 }
180 
181 //void QxrdMaskStackModel::sort ( int column, Qt::SortOrder order)
182 //{
183 //}
184 
186 {
187  return QAbstractItemModel::supportedDropActions() | Qt::MoveAction;
188 }
189 
191 {
192  return m_MaskStack;
193 }
194 
196 {
197  emit beginResetModel();
198 
199  m_MaskStack = masks;
200 
201  emit endResetModel();
202 }
203 
205 {
206  emit beginResetModel();
207 
208  emit endResetModel();
209 }
QxrdMaskStackPtr m_MaskStack
virtual QVariant data(const QModelIndex &index, int role) const
void setMaskStack(QxrdMaskStackPtr masks)
virtual Qt::ItemFlags flags(const QModelIndex &index) const
virtual int columnCount(const QModelIndex &parent=QModelIndex()) const
QxrdMaskStackPtr maskStack()
void maskChanged()
virtual QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
virtual bool insertRows(int row, int count, const QModelIndex &parent=QModelIndex())
virtual bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
QxrdMaskStackModel(QxrdMaskStackPtr masks)
static QString stackLevelName(int n)
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const
virtual bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex())
virtual Qt::DropActions supportedDropActions() const
QSharedPointer< QcepMaskData > QcepMaskDataPtr