1 #include "resourcetable.hpp"
2 
3 #include <stdexcept>
4 
5 #include "resources.hpp"
6 #include "columnbase.hpp"
7 #include "universalid.hpp"
8 
ResourceTable(const Resources * resources,unsigned int features)9 CSMWorld::ResourceTable::ResourceTable (const Resources *resources, unsigned int features)
10 : IdTableBase (features | Feature_Constant), mResources (resources)
11 {}
12 
~ResourceTable()13 CSMWorld::ResourceTable::~ResourceTable() {}
14 
rowCount(const QModelIndex & parent) const15 int CSMWorld::ResourceTable::rowCount (const QModelIndex & parent) const
16 {
17     if (parent.isValid())
18         return 0;
19 
20     return mResources->getSize();
21 }
22 
columnCount(const QModelIndex & parent) const23 int CSMWorld::ResourceTable::columnCount (const QModelIndex & parent) const
24 {
25     if (parent.isValid())
26         return 0;
27 
28     return 2; // ID, type
29 }
30 
data(const QModelIndex & index,int role) const31 QVariant CSMWorld::ResourceTable::data  (const QModelIndex & index, int role) const
32 {
33     if (role!=Qt::DisplayRole)
34         return QVariant();
35 
36     if (index.column()==0)
37         return QString::fromUtf8 (mResources->getId (index.row()).c_str());
38 
39     if (index.column()==1)
40         return static_cast<int> (mResources->getType());
41 
42     throw std::logic_error ("Invalid column in resource table");
43 }
44 
headerData(int section,Qt::Orientation orientation,int role) const45 QVariant CSMWorld::ResourceTable::headerData (int section, Qt::Orientation orientation,
46     int role ) const
47 {
48     if (orientation==Qt::Vertical)
49         return QVariant();
50 
51     if (role==ColumnBase::Role_Flags)
52         return section==0 ? ColumnBase::Flag_Table : 0;
53 
54     switch (section)
55     {
56         case 0:
57 
58             if (role==Qt::DisplayRole)
59                 return Columns::getName (Columns::ColumnId_Id).c_str();
60 
61             if (role==ColumnBase::Role_Display)
62                 return ColumnBase::Display_Id;
63 
64             break;
65 
66         case 1:
67 
68             if (role==Qt::DisplayRole)
69                 return Columns::getName (Columns::ColumnId_RecordType).c_str();
70 
71             if (role==ColumnBase::Role_Display)
72                 return ColumnBase::Display_Integer;
73 
74             break;
75     }
76 
77     return QVariant();
78 }
79 
setData(const QModelIndex & index,const QVariant & value,int role)80 bool CSMWorld::ResourceTable::setData ( const QModelIndex &index, const QVariant &value,
81     int role)
82 {
83     return false;
84 }
85 
flags(const QModelIndex & index) const86 Qt::ItemFlags CSMWorld::ResourceTable::flags (const QModelIndex & index) const
87 {
88     return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
89 }
90 
index(int row,int column,const QModelIndex & parent) const91 QModelIndex CSMWorld::ResourceTable::index (int row, int column, const QModelIndex& parent)
92     const
93 {
94     if (parent.isValid())
95         return QModelIndex();
96 
97     if (row<0 || row>=mResources->getSize())
98         return QModelIndex();
99 
100     if (column<0 || column>1)
101         return QModelIndex();
102 
103     return createIndex (row, column);
104 }
105 
parent(const QModelIndex & index) const106 QModelIndex CSMWorld::ResourceTable::parent (const QModelIndex& index) const
107 {
108     return QModelIndex();
109 }
110 
getModelIndex(const std::string & id,int column) const111 QModelIndex CSMWorld::ResourceTable::getModelIndex (const std::string& id, int column) const
112 {
113     int row = mResources->searchId(id);
114     if (row != -1)
115         return index (row, column);
116 
117     return QModelIndex();
118 }
119 
searchColumnIndex(Columns::ColumnId id) const120 int CSMWorld::ResourceTable::searchColumnIndex (Columns::ColumnId id) const
121 {
122     if (id==Columns::ColumnId_Id)
123         return 0;
124 
125     if (id==Columns::ColumnId_RecordType)
126         return 1;
127 
128     return -1;
129 }
130 
findColumnIndex(Columns::ColumnId id) const131 int CSMWorld::ResourceTable::findColumnIndex (Columns::ColumnId id) const
132 {
133     int index = searchColumnIndex (id);
134 
135     if (index==-1)
136         throw std::logic_error ("invalid column index");
137 
138     return index;
139 }
140 
view(int row) const141 std::pair<CSMWorld::UniversalId, std::string> CSMWorld::ResourceTable::view (int row) const
142 {
143     return std::make_pair (UniversalId::Type_None, "");
144 }
145 
isDeleted(const std::string & id) const146 bool CSMWorld::ResourceTable::isDeleted (const std::string& id) const
147 {
148     return false;
149 }
150 
getColumnId(int column) const151 int CSMWorld::ResourceTable::getColumnId (int column) const
152 {
153     switch (column)
154     {
155         case 0: return Columns::ColumnId_Id;
156         case 1: return Columns::ColumnId_RecordType;
157     }
158 
159     return -1;
160 }
161 
beginReset()162 void CSMWorld::ResourceTable::beginReset()
163 {
164     beginResetModel();
165 }
166 
endReset()167 void CSMWorld::ResourceTable::endReset()
168 {
169     endResetModel();
170 }
171