00001 #include <QStringList>
00002 #include <QDebug>
00003 #include <QIcon>
00004 #include "documentchoosermodel.h"
00005 #include "../Data/datadefinitions.h"
00006 #include "abstractmodel.h"
00007 #include "../Models/costtypemodel.h"
00008
00009 DocumentChooserModel::DocumentChooserModel(QObject *parent) : QAbstractItemModel(parent)
00010 {
00011 p_rootItem = new DocumentItem(new AbstractModel(-1, -1, "Dokumente"));
00012
00013 addDocumentType(WindowType::CostType);
00014 addDocumentType(WindowType::CostCentre);
00015 addDocumentType(WindowType::CostUnit);
00016 addDocumentType(WindowType::PriceComparison);
00017 }
00018
00019 DocumentChooserModel::~DocumentChooserModel()
00020 {
00021 delete p_rootItem;
00022 }
00023
00024 int DocumentChooserModel::columnCount(const QModelIndex &parent) const
00025 {
00026 if(parent.isValid())
00027 {
00028 return static_cast<DocumentItem*>(parent.internalPointer())->columnCount();
00029 }
00030 else
00031 {
00032 return p_rootItem->columnCount();
00033 }
00034 }
00035
00036 QVariant DocumentChooserModel::data(const QModelIndex &index, int role) const
00037 {
00038 if(!index.isValid())
00039 {
00040 return QVariant();
00041 }
00042
00043 if(role == 32)
00044 {
00045 DocumentItem *item = documentItem(index.parent().data(Qt::DisplayRole).toString(), index.data(Qt::DisplayRole).toString());
00046
00047 QVariant var;
00048 var.setValue(*item);
00049
00050 return var;
00051 }
00052
00053 if(role == Qt::DecorationRole)
00054 {
00055 DocumentItem *item = static_cast<DocumentItem*>(index.internalPointer());
00056 switch(item->type())
00057 {
00058 case WindowType::CostType:
00059 {
00060 return QIcon(":/childwindows/CostType");
00061 }
00062 case WindowType::CostCentre:
00063 {
00064 return QIcon(":/childwindows/CostCentre");
00065 }
00066 case WindowType::CostUnit:
00067 {
00068 return QIcon(":/childwindows/CostUnit");
00069 }
00070 case WindowType::PriceComparison:
00071 {
00072 return QIcon(":/childwindows/PriceComparison");
00073 }
00074 }
00075 }
00076
00077 if(role == Qt::DisplayRole)
00078 {
00079 DocumentItem *item = static_cast<DocumentItem*>(index.internalPointer());
00080 return item->data(index.column());
00081 }
00082 return QVariant();
00083 }
00084
00085 DocumentItem* DocumentChooserModel::documentItem(int fileid, int documentType) const
00086 {
00087 DocumentItem* root = p_rootItem;
00088 for(int i = 0; i != root->childCount(); i++)
00089 {
00090 DocumentItem *category = root->child(i);
00091
00092 if(category->model()->type() == documentType)
00093 {
00094 for(int j = 0; j != category->childCount(); j++)
00095 {
00096 DocumentItem *item = category->child(j);
00097 if(item->model()->fileId() == fileid)
00098 {
00099 return item;
00100 }
00101 }
00102 }
00103 }
00104 return new DocumentItem(new AbstractModel(-1, -1, ""));
00105 }
00106
00107 DocumentItem* DocumentChooserModel::documentItem(QString documentType, QString filename) const
00108 {
00109 DocumentItem* root = p_rootItem;
00110 for(int i = 0; i != root->childCount(); i++)
00111 {
00112 DocumentItem *category = root->child(i);
00113 WindowType *type = new WindowType();
00114 if(type->type(category->model()->type()) == documentType)
00115 {
00116 for(int j = 0; j != category->childCount(); j++)
00117 {
00118 DocumentItem *item = category->child(j);
00119 if(item->model()->fileName() == filename)
00120 {
00121 return item;
00122 }
00123 }
00124 }
00125 }
00126 return new DocumentItem(new AbstractModel(-1, -1, ""));
00127 }
00128
00129 AbstractModel* DocumentChooserModel::abstractModel(int fileid, int documentType) const
00130 {
00131 DocumentItem* root = p_rootItem;
00132 for(int i = 0; i != root->childCount(); i++)
00133 {
00134 DocumentItem *category = root->child(i);
00135 WindowType *type = new WindowType();
00136 if(category->model()->type() == documentType)
00137 {
00138 for(int j = 0; j != category->childCount(); j++)
00139 {
00140 DocumentItem *item = category->child(j);
00141 if(item->model()->fileId() == fileid)
00142 {
00143 return item->model();
00144 }
00145 }
00146 }
00147 }
00148 return new AbstractModel(-1, -1, "");
00149 }
00150
00151 Qt::ItemFlags DocumentChooserModel::flags(const QModelIndex &index) const
00152 {
00153 if(!index.isValid())
00154 {
00155 return 0;
00156 }
00157
00158 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
00159 }
00160
00161 QVariant DocumentChooserModel::headerData(int section, Qt::Orientation orientation, int role) const
00162 {
00163 if(orientation == Qt::Horizontal && role == Qt::DisplayRole)
00164 {
00165 return p_rootItem->model()->fileName();
00166 }
00167
00168 return QVariant();
00169 }
00170
00171 QModelIndex DocumentChooserModel::index(int row, int column, const QModelIndex &parent) const
00172 {
00173 if(!hasIndex(row, column, parent))
00174 {
00175 return QModelIndex();
00176 }
00177
00178 DocumentItem *parentItem;
00179
00180 if(!parent.isValid())
00181 {
00182 parentItem = p_rootItem;
00183 }
00184 else
00185 {
00186 parentItem = static_cast<DocumentItem*>(parent.internalPointer());
00187 }
00188
00189 DocumentItem *childItem = parentItem->child(row);
00190 if(childItem)
00191 {
00192 return createIndex(row, column, childItem);
00193 }
00194 else
00195 {
00196 return QModelIndex();
00197 }
00198 }
00199
00200 QModelIndex DocumentChooserModel::parent(const QModelIndex &index) const
00201 {
00202 if(!index.isValid())
00203 {
00204 return QModelIndex();
00205 }
00206
00207 DocumentItem *childItem = static_cast<DocumentItem*>(index.internalPointer());
00208 DocumentItem *parentItem = childItem->parent();
00209
00210 if(parentItem == p_rootItem)
00211 {
00212 return QModelIndex();
00213 }
00214
00215 return createIndex(parentItem->row(), 0, parentItem);
00216 }
00217
00218 int DocumentChooserModel::rowCount(const QModelIndex &parent) const
00219 {
00220 DocumentItem *parentItem;
00221 if(parent.column() > 0)
00222 return 0;
00223
00224 if(!parent.isValid())
00225 {
00226 parentItem = p_rootItem;
00227 }
00228 else
00229 {
00230 parentItem = static_cast<DocumentItem*>(parent.internalPointer());
00231 }
00232 return parentItem->childCount();
00233 }
00234
00235 void DocumentChooserModel::addDocumentType(int documentType)
00236 {
00237 WindowType *window = new WindowType();
00238
00239 p_rootItem->appendChild(new DocumentItem(new AbstractModel(-1, documentType, window->type(documentType)), p_rootItem));
00240
00241 reset();
00242 }
00243
00244 void DocumentChooserModel::addDocument(AbstractModel *model)
00245 {
00246 for(int i=0; i < p_rootItem->childCount();i++)
00247 {
00248 DocumentItem *type = p_rootItem->child(i);
00249 if(type->model()->type() == model->type())
00250 {
00251 p_rootItem->childItems.value(i)->appendChild(new DocumentItem(model, p_rootItem->childItems.value(i)));
00252 reset();
00253 }
00254 }
00255 }
00256
00257 bool DocumentChooserModel::documentExists(int fileid, int documentType)
00258 {
00259 bool exists = false;
00260 for(int i = 0; i != p_rootItem->childCount(); i++)
00261 {
00262 DocumentItem *cate = p_rootItem->child(i);
00263 if(cate->type() == documentType)
00264 {
00265 for(int j = 0; j != cate->childCount(); j++)
00266 {
00267 DocumentItem *pos = cate->child(j);
00268 if(pos->fileid() == fileid)
00269 {
00270 exists = true;
00271 }
00272 }
00273 }
00274 }
00275 return exists;
00276 }
00277
00278 void DocumentChooserModel::deleteDocument(int fileid, int documentType)
00279 {
00280 for(int i = 0; i != p_rootItem->childCount(); i++)
00281 {
00282 DocumentItem *cat = p_rootItem->child(i);
00283 if(cat->type() == documentType)
00284 {
00285 for(int j = 0; j != cat->childCount(); j++)
00286 {
00287 DocumentItem *item = cat->child(j);
00288 if(item->fileid() == fileid)
00289 {
00290 cat->removeChild(item);
00291
00292 reset();
00293 emit documentRemoved(fileid);
00294
00295 return;
00296 }
00297 }
00298 }
00299 }
00300 }
00301
00302 void DocumentChooserModel::clear()
00303 {
00304 for(int i = 0; i < p_rootItem->childCount(); i++)
00305 {
00306 DocumentItem *cat = p_rootItem->child(i);
00307 cat->removeChilds();
00308
00309 reset();
00310 }
00311 }
00312
00313 bool DocumentChooserModel::fileExists(const QString &name) const
00314 {
00315 for(int i = 0; i < p_rootItem->childCount(); i++)
00316 {
00317 DocumentItem *cat = p_rootItem->child(i);
00318 for(int j = 0; j < cat->childCount(); j++)
00319 {
00320 DocumentItem *doc = cat->child(j);
00321 if(QString::compare(doc->fileName(), name, Qt::CaseInsensitive) == 0)
00322 {
00323 return true;
00324 }
00325 }
00326 }
00327 return false;
00328 }
00329
00330 CostTypeModel* DocumentChooserModel::costTypeModelById(int fileId)
00331 {
00332 return (CostTypeModel*)abstractModel(fileId, WindowType::CostType);
00333 }
00334
00335 DocumentItem::DocumentItem(AbstractModel *model, DocumentItem *parent)
00336 {
00337 p_model = model;
00338
00339 parentItem = parent;
00340 }
00341
00342 DocumentItem::~DocumentItem()
00343 {
00344 qDeleteAll(childItems);
00345 }
00346
00347 void DocumentItem::appendChild(DocumentItem *item)
00348 {
00349 childItems.append(item);
00350 }
00351
00352 void DocumentItem::removeChild(DocumentItem *item)
00353 {
00354 childItems.removeAll(item);
00355 }
00356
00357 void DocumentItem::removeChilds()
00358 {
00359 childItems.clear();
00360 }
00361
00362 DocumentItem *DocumentItem::child(int row)
00363 {
00364 return childItems.value(row);
00365 }
00366
00367 int DocumentItem::childCount() const
00368 {
00369 return childItems.count();
00370 }
00371
00372 int DocumentItem::columnCount() const
00373 {
00374 return 1;
00375 }
00376
00377 QVariant DocumentItem::data(int column) const
00378 {
00379 return p_model->fileName();
00380 }
00381
00382 AbstractModel* DocumentItem::model() const
00383 {
00384 return p_model;
00385 }
00386
00387 int DocumentItem::fileid() const
00388 {
00389 return p_model->fileId();
00390 }
00391
00392 QString DocumentItem::fileName() const
00393 {
00394 return p_model->fileName();
00395 }
00396
00397 int DocumentItem::type() const
00398 {
00399 return p_model->type();
00400 }
00401
00402 DocumentItem *DocumentItem::parent()
00403 {
00404 return parentItem;
00405 }
00406
00407 int DocumentItem::row() const
00408 {
00409 if(parentItem)
00410 {
00411 return parentItem->childItems.indexOf(const_cast<DocumentItem*>(this));
00412 }
00413
00414 return 0;
00415 }