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