00001 #include "costtypeselectionmodel.h" 00002 #include <QDebug> 00003 #include <QStringList> 00004 #include "costtypemodel.h" 00005 00006 CostTypeSelectionModel::CostTypeSelectionModel(CostTypeFilterModel *t) : QItemSelectionModel(t) 00007 { 00008 } 00009 00010 void CostTypeSelectionModel::select(const QModelIndex &index, QItemSelectionModel::SelectionFlags flags) 00011 { 00012 //get a list of all affected rows 00013 QModelIndexList *selectedRows = new QModelIndexList(); 00014 affectedRowIndexes(selectedRows, index); 00015 00016 clearSelection(); 00017 00018 for(int i = 0; i < selectedRows->count(); i++) 00019 { 00020 CostTypeFilterModel *m = (CostTypeFilterModel*)model(); 00021 QModelIndex in = m->mapFromSource(selectedRows->value(i)); 00022 00023 QItemSelectionModel::select(in, QItemSelectionModel::Select | QItemSelectionModel::Rows); 00024 } 00025 00026 // disable/enable actions according to the type of the row 00027 QStringList row = ((CostTypeModel*)((CostTypeFilterModel*)model())->sourceModel())->data(selectedRows->value(0).row()); 00028 if(row.value(CostTypeColumns::Type).startsWith("sum")) 00029 { 00030 emit enableEditRemoveActions(false); 00031 } 00032 else 00033 { 00034 emit enableEditRemoveActions(true); 00035 } 00036 } 00037 00038 void CostTypeSelectionModel::affectedRowIndexes(QModelIndexList *selectedIndexes, QModelIndex index) 00039 { 00040 //need to get my model from it -- otherwise the data functions won't do the right thing(tm) 00041 CostTypeFilterModel *m = (CostTypeFilterModel*)model(); 00042 00043 // map index to datasource (costtypemodel) 00044 index = m->mapToSource(index); 00045 00046 int r = index.row(); 00047 int associationid = ((CostTypeModel*)m->sourceModel())->data(r).value(CostTypeColumns::AssociationID).toInt(); 00048 00049 if(!selectedIndexes->contains(index)) 00050 { 00051 selectedIndexes->append(index); 00052 } 00053 else 00054 { 00055 return; 00056 } 00057 00058 for(int i = 0; i < m->rowCount(QModelIndex()); i++) 00059 { 00060 QModelIndex index = m->index(i, 0, QModelIndex()); 00061 00062 QStringList rowdata = ((CostTypeModel*)m->sourceModel())->data(index.row()); 00063 if(rowdata.value(CostTypeColumns::RowID).toInt() == associationid) 00064 { 00065 // map to costtypefiltermodel 00066 index = m->mapFromSource(index); 00067 affectedRowIndexes(selectedIndexes, index); //get more associated indexes 00068 } 00069 } 00070 }