Crossfire Server, Trunk  R20513
CREQuestItemModel.cpp
Go to the documentation of this file.
1 #include "CREQuestItemModel.h"
2 #include "Quest.h"
3 
4 CREQuestItemModel::CREQuestItemModel(QObject* parent) : QAbstractItemModel(parent)
5 {
6  myQuest = NULL;
7 }
8 
10 }
11 
13 {
14  return myQuest;
15 }
16 
18 {
19  myQuest = quest;
20  reset();
21 }
22 
23 int CREQuestItemModel::columnCount(const QModelIndex& parent) const
24 {
25  if (myQuest == NULL)
26  return 0;
27 
28  if (parent.isValid())
29  return 0;
30 
31  return 4;
32 }
33 
34 QModelIndex CREQuestItemModel::index(int row, int column, const QModelIndex& parent) const
35 {
36  if (parent.isValid())
37  return QModelIndex();
38 
39  return createIndex(row, column);
40 }
41 
42 QModelIndex CREQuestItemModel::parent(const QModelIndex&) const
43 {
44  return QModelIndex();
45 }
46 
47 int CREQuestItemModel::rowCount(const QModelIndex & parent) const
48 {
49  if (parent.isValid())
50  return 0;
51 
52  if (myQuest == NULL)
53  return 0;
54 
55  return myQuest->steps().size();
56 }
57 
58 QVariant CREQuestItemModel::data(const QModelIndex& index, int role) const
59 {
60  if (myQuest == NULL || !index.isValid())
61  return QVariant();
62 
63  if (role != Qt::DisplayRole && role != Qt::EditRole && (role != Qt::CheckStateRole || index.column() != 2))
64  return QVariant();
65 
66  Q_ASSERT(index.row() < myQuest->steps().size());
67  const QuestStep* step = myQuest->steps()[index.row()];
68 
69  switch(index.column())
70  {
71  case 0:
72  return step->step();
73 
74  case 1:
75  return step->description();
76 
77  case 2:
78  if (role == Qt::DisplayRole)
79  return QVariant();
80  return step->isCompletion() ? Qt::Checked : Qt::Unchecked;
81 
82  case 3:
83  /*if (role == Qt::EditRole)
84  return step->setWhen();*/
85  return step->setWhen().join("\n");
86  }
87 
88  return QVariant();;
89 }
90 
91 QVariant CREQuestItemModel::headerData(int section, Qt::Orientation orientation, int role) const
92 {
93  if (orientation != Qt::Horizontal || role != Qt::DisplayRole)
94  return QVariant();
95 
96  switch(section)
97  {
98  case 0:
99  return tr("step");
100  case 1:
101  return tr("description");
102  case 2:
103  return tr("end?");
104  case 3:
105  return tr("set when");
106  }
107 
108  return QVariant();
109 }
110 
111 Qt::ItemFlags CREQuestItemModel::flags(const QModelIndex& index) const
112 {
113  if (!index.isValid())
114  return 0;
115 
116  if (index.column() == 2)
117  return Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
118 
119  return Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled;
120 }
121 
122 bool CREQuestItemModel::setData(const QModelIndex& index, const QVariant& value, int role)
123 {
124  if (!index.isValid() || myQuest == NULL || myQuest->steps().size() <= index.row())
125  return false;
126 
127  QuestStep* step = myQuest->steps()[index.row()];
128 
129  if (role != Qt::EditRole && index.column() != 2)
130  return false;
131 
132  if (index.column() == 0)
133  step->setStep(value.toInt());
134  else if (index.column() == 1)
135  step->setDescription(value.toString());
136  else if (index.column() == 2)
137  step->setCompletion(value == Qt::Checked);
138  else if (index.column() == 3)
139  step->setWhen() = value.toString().split("\n");
140 
141  emit dataChanged(index, index);
142  myQuest->setModified(true);
143 
144  return true;
145 }
146 
148 {
149  if (myQuest == NULL)
150  return;
151 
152  beginInsertRows(QModelIndex(), myQuest->steps().size(), myQuest->steps().size());
153 
154  myQuest->steps().append(new QuestStep());
155  myQuest->setModified(true);
156 
157  endInsertRows();
158 }
159 
160 bool CREQuestItemModel::removeRows(int row, int count, const QModelIndex& parent)
161 {
162  if (myQuest == NULL || parent.isValid() || count != 1)
163  return false;
164 
165  if (row < 0 || row >= myQuest->steps().size())
166  return false;
167 
168  beginRemoveRows(parent, row, row);
169  delete myQuest->steps().takeAt(row);
170  endRemoveRows();
171 
172  return true;
173 }
174 
176 {
177  if (step < 1)
178  return;
179 
180  beginMoveRows(QModelIndex(), step, step, QModelIndex(), step - 1);
181  QuestStep* s = myQuest->steps()[step];
182  myQuest->steps()[step] = myQuest->steps()[step - 1];
183  myQuest->steps()[step - 1] = s;
184  endMoveRows();
185  myQuest->setModified(true);
186 }
187 
189 {
190  if (step >= myQuest->steps().size() - 1)
191  return;
192 
193  beginMoveRows(QModelIndex(), step + 1, step + 1, QModelIndex(), step);
194  QuestStep* s = myQuest->steps()[step];
195  myQuest->steps()[step] = myQuest->steps()[step + 1];
196  myQuest->steps()[step + 1] = s;
197  endMoveRows();
198  myQuest->setModified(true);
199 }
virtual Qt::ItemFlags flags(const QModelIndex &index) const
const QStringList & setWhen() const
Definition: Quest.cpp:39
virtual bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex())
virtual ~CREQuestItemModel()
Definition: Quest.h:32
QList< const QuestStep * > steps() const
Definition: Quest.cpp:138
void setStep(int step)
Definition: Quest.cpp:14
CREQuestItemModel(QObject *parent)
virtual bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
Definition: Quest.h:9
void setCompletion(bool completion)
Definition: Quest.cpp:34
virtual int rowCount(const QModelIndex &parent) const
virtual QModelIndex parent(const QModelIndex &index) const
Quest * quest() const
virtual int columnCount(const QModelIndex &parent) const
virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const
virtual QVariant data(const QModelIndex &index, int role) const
void setDescription(const QString &description)
Definition: Quest.cpp:24
void setQuest(Quest *quest)
void setModified(bool modified)
Definition: Quest.cpp:155
void moveDown(int step)
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const