00001 #include <QSortFilterProxyModel>
00002 #include <QDebug>
00003 #include "addcostpositiondialog.h"
00004 #include "../Data/datadefinitions.h"
00005 #include "../Models/costtypemodel.h"
00006 #include "../Models/costtypeselectionmodel.h"
00007 #include "../Models/addcostpositionmodel.h"
00008 #include <QCheckBox>
00009 #include <QMessageBox>
00010 #include <QHeaderView>
00011
00012 AddCostPositionDialog::AddCostPositionDialog(CostTypeModel *m, CostCentreModel *costcentre, QWidget *parent) : QDialog(parent), costCentreModel(costcentre), costTypeModel(m)
00013 {
00014 setupUi(this);
00015 createActions();
00016 setAttribute(Qt::WA_DeleteOnClose, true);
00017
00018 proxyModel = new CostTypeFilterModel(true, true, true, true, costcentre, this);
00019 proxyModel->setSourceModel(m);
00020 proxyModel->setDynamicSortFilter(true);
00021
00022 tableViewPositions->setModel(proxyModel);
00023 tableViewPositions->setSortingEnabled(true);
00024 tableViewPositions->sortByColumn(0, Qt::AscendingOrder);
00025
00026 tableViewAddPosition->setFocusPolicy(Qt::NoFocus);
00027 tableViewAddPosition->setItemDelegateForColumn(0, new AddCostPositionDelegate());
00028 tableViewAddPosition->horizontalHeader()->setSortIndicatorShown(false);
00029 tableViewAddPosition->horizontalHeader()->setClickable(false);
00030 tableViewAddPosition->verticalHeader()->setClickable(false);
00031
00032 connect(tableViewPositions, SIGNAL(clicked(QModelIndex)), this, SLOT(changeCurrentEditingPosition(QModelIndex)));
00033 }
00034
00035 void AddCostPositionDialog::createActions()
00036 {
00037 connect(buttonAddPosition, SIGNAL(clicked()), this, SLOT(addPosition()));
00038 connect(buttonClose, SIGNAL(clicked()), this, SLOT(close()));
00039 }
00040
00041 void AddCostPositionDialog::addPosition()
00042 {
00043 if(!model->isCorrectSpreaded())
00044 {
00045 QMessageBox::warning(this, tr("Fehler in der Aufteilung!"), tr("Sie haben einen Fehler in der Aufteilung! Bevor die Position hinzugefĆ¼gt werden kann muss dieser korrigiert sein!"));
00046 return;
00047 }
00048
00049 QStringList row = costCentreRow();
00050 costCentreModel->addPosition(row);
00051
00052
00053
00054
00055
00056 int firstRow = affectedRows.value(0);
00057 int found = 0;
00058 for(int i = 0; i < alreadyAddedRows.count(); i++)
00059 {
00060 if(affectedRows.contains(alreadyAddedRows.value(i)))
00061 {
00062
00063 affectedRows.removeAll(alreadyAddedRows.value(i));
00064
00065 found++;
00066 }
00067 }
00068
00069 QModelIndex index;
00070 if(found == affectedRows.count())
00071 {
00072
00073 alreadyAddedRows.clear();
00074 affectedRows.clear();
00075
00076
00077 index = model->index(0, 0, QModelIndex());
00078
00079 qDebug()<<"new row selected : " << index;
00080 }
00081 else
00082 {
00083 alreadyAddedRows << row.value(row.count() - 1).toInt();
00084 searchForAffectedRows(firstRow);
00085
00086
00087 for(int i = 0; i < alreadyAddedRows.count(); i++)
00088 {
00089 affectedRows.removeAll(alreadyAddedRows.value(i));
00090 }
00091
00092 int nextRow = affectedRows.value(0);
00093 index = mapFileIdToAddPositionModel(nextRow);
00094
00095 qDebug()<< "next row selected (fileid): " << nextRow << " " << index;
00096
00097 searchForAffectedRows(firstRow);
00098 }
00099
00100 if(!index.isValid())
00101 {
00102 qDebug()<<"index isn't valid!";
00103
00104 textCosts->setText("");
00105 textCostSum->setText("");
00106 textCostType->setText("");
00107 textPositionName->setText("");
00108 model->clear();
00109 tableViewAddPosition->setEnabled(false);
00110 }
00111 else
00112 {
00113 qDebug()<<"index is valid";
00114 changeCurrentEditingPosition(index);
00115 tableViewAddPosition->setEnabled(true);
00116 }
00117
00118
00119 proxyModel->invalidate();
00120 }
00121
00122 QModelIndex AddCostPositionDialog::mapFileIdToAddPositionModel(int fileId)
00123 {
00124 int row = costTypeModel->rowFromRowId(fileId);
00125
00126 QModelIndex index = costTypeModel->index(row, 0, QModelIndex());
00127 qDebug()<<"fileid: " << fileId;
00128 qDebug()<<"index to map: " << index;
00129 index = proxyModel->mapFromSource(index);
00130 qDebug()<<"mapped index: " << index;
00131
00132 return index;
00133 }
00134
00135 QStringList AddCostPositionDialog::costCentreRow()
00136 {
00137
00138 QStringList currentRow = p_tmp_currentRow;
00139
00140 QStringList costcentrerow;
00141 costcentrerow << currentRow.value(CostTypeColumns::PositionName);
00142 costcentrerow << currentRow.value(CostTypeColumns::Costs);
00143
00144
00145 for(int i = 1; i < model->columnCount(QModelIndex()) - 1; i++)
00146 {
00147 costcentrerow << QString::number(model->calculateCostCentreSum(i));
00148 }
00149
00150
00151 costcentrerow << model->costSpreadingRow();
00152 costcentrerow << model->costTypesRow();
00153 costcentrerow << "row";
00154 costcentrerow << "rowid";
00155 costcentrerow << "associationid";
00156 costcentrerow << currentRow.value(CostTypeColumns::RowID);
00157
00158 return costcentrerow;
00159 }
00160
00161 QStringList AddCostPositionDialog::currentSelectedRow(QModelIndex index)
00162 {
00163 CostTypeModel *model = (CostTypeModel*)proxyModel->sourceModel();
00164 QModelIndex proxyIndex = index;
00165 QModelIndex modelIndex = proxyModel->mapToSource(proxyIndex);
00166 int row = modelIndex.row();
00167
00168 p_tmp_currentRow = model->data(row);
00169
00170 return p_tmp_currentRow;
00171 }
00172
00173 void AddCostPositionDialog::changeCurrentEditingPosition(QModelIndex index)
00174 {
00175 tableViewAddPosition->setEnabled(true);
00176 fillPositionInformation(currentSelectedRow(index));
00177 }
00178
00179 void AddCostPositionDialog::fillPositionInformation(QStringList row)
00180 {
00181 if(row != QStringList())
00182 {
00183 textPositionName->setText(row.value(CostTypeColumns::PositionName));
00184 textCosts->setText(row.value(CostTypeColumns::Costs));
00185 int costtype = row.value(CostTypeColumns::CostType).toInt();
00186 if(costtype == CostTypes::FixedCosts)
00187 {
00188 textCostType->setText("Gemeinkosten");
00189 }
00190 else
00191 {
00192 textCostType->setText("Einzelkosten");
00193 }
00194
00195 model = new AddCostPositionModel(costTypeModel, (CostCentreModel*)costCentreModel, row.value(CostTypeColumns::RowID).toInt(), row.value(CostTypeColumns::Costs).toDouble());
00196 tableViewAddPosition->setModel(model);
00197 connect(model, SIGNAL(currentSumChanged(double)), this, SLOT(currentSumChanged(double)));
00198 connect(model, SIGNAL(updateView()), this, SLOT(update()));
00199
00200 QAbstractItemDelegate *delegate = tableViewAddPosition->itemDelegate();
00201 connect(delegate, SIGNAL(commitData(QWidget*)), this, SLOT(refreshSums()));
00202
00203 QCheckBox *checkbox = new QCheckBox(this);
00204 tableViewAddPosition->setColumnWidth(0, checkbox->sizeHint().width());
00205
00206 searchForAffectedRows(row.value(row.count() - 1).toInt());
00207 }
00208 }
00209
00210 void AddCostPositionDialog::refreshSums()
00211 {
00212 qDebug()<<"refreshSums";
00213 QItemSelectionModel *selection = tableViewAddPosition->selectionModel();
00214 QModelIndex index = selection->selectedIndexes().value(0);
00215 qDebug()<<index;
00216
00217 model->refreshSums(index, index);
00218 }
00219
00220 void AddCostPositionDialog::currentSumChanged(double complete)
00221 {
00222 if(complete == textCosts->text().toDouble())
00223 {
00224 textCostSum->setText(QString::number(complete));
00225 labelCostSumIcon->setPixmap(QIcon(":/misc/Apply").pixmap(22,22));
00226 }
00227 else if(complete == -1)
00228 {
00229 textCostSum->setText("Fehler in der Aufteilung!");
00230 labelCostSumIcon->setPixmap(QIcon(":/misc/Cancel").pixmap(22,22));
00231 }
00232 else
00233 {
00234 textCostSum->setText(QString::number(complete));
00235 labelCostSumIcon->setPixmap(QIcon(":/misc/Cancel").pixmap(22,22));
00236 }
00237 }
00238
00239 void AddCostPositionDialog::searchForAffectedRows(int rowid)
00240 {
00241 QList<QStringList> list = costTypeModel->getList();
00242
00243 int associationid = -1;
00244 for(int i = 0; i < list.count(); i++)
00245 {
00246 if(list.value(i).value(CostTypeColumns::RowID).toInt() == rowid)
00247 {
00248 associationid = list.value(i).value(CostTypeColumns::AssociationID).toInt();
00249 }
00250 }
00251
00252 if(!affectedRows.contains(rowid))
00253 {
00254 affectedRows.append(rowid);
00255 }
00256 else
00257 {
00258 return;
00259 }
00260
00261 for(int i = 0; i < list.count(); i++)
00262 {
00263 QStringList rowdata = list.value(i);
00264 int rowid = rowdata.value(CostTypeColumns::RowID).toInt();
00265 if(rowid == associationid)
00266 {
00267 searchForAffectedRows(rowid);
00268 }
00269 }
00270 }