20 #include "FdModelResults.hpp"
21 #include "FdItemResults.hpp"
22 #include "FdCommandsResults.hpp"
23 #include "FdModelCoa.hpp"
24 #include "FdItemCoa.hpp"
25 #include "../gui/FdSubWindow.hpp"
38 mResults = QList<FdItemResults_p>();
39 QDomElement calculation = results.firstChildElement();
40 while(!calculation.isNull())
43 calculation = calculation.nextSiblingElement();
56 mResults = QList<FdItemResults_p>();
57 for(
int i = 0; i < model->
items().size(); ++i)
65 setSupportedDragActions(Qt::MoveAction);
67 connect(
mStack, SIGNAL(cleanChanged(
bool)),
mSubwindow, SLOT(cleanStateChanged(
bool)));
74 case ResultsId:
return calculation->id();
75 case ResultsLabel:
return calculation->label();
76 case ResultsCalculation:
return calculation->calculation();
78 if(!calculation->isEmpty())
79 return calculation->result();
91 if(role == Qt::TextAlignmentRole && index.column() == ResultsResult)
92 return Qt::AlignRight;
93 if(role == ValidityRole)
95 if(role != Qt::DisplayRole && role != Qt::EditRole)
98 return data(
mResults[index.row()], ResultsColumn(index.column()));
103 int row =
mResults.indexOf(calculation);
107 calculation->setId(value.toString());
110 calculation->setLabel(value.toString());
112 case ResultsCalculation:
113 calculation->setCalculation(value.toString());
117 emit(dataChanged(index(row, column), index(row, column)));
132 if(!index.isValid() || role != Qt::EditRole)
135 if(value ==
data(index, Qt::DisplayRole))
138 int column = index.column();
145 case ResultsCalculation:
149 FdItemResults_p calculation =
mResults.at(index.row());
156 if(role == Qt::DisplayRole)
158 if (orientation == Qt::Horizontal)
162 case ResultsId:
return tr(
"Id",
"Results page column header.");
163 case ResultsLabel:
return tr(
"Label",
"Results page column header.");
164 case ResultsCalculation:
return tr(
"Calculation",
"Results page column header.");
165 case ResultsResult:
return tr(
"Result",
"Results page column header.");
180 flags = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
181 if(index.column() != ResultsResult)
182 flags |= Qt::ItemIsEditable;
190 return Qt::MoveAction;
195 return QStringList() <<
"application/freedebks";
205 QMimeData*
mimeData =
new QMimeData();
206 QByteArray encodedData;
207 QDataStream stream(&encodedData, QIODevice::WriteOnly);
209 for(
int i = 0; i < indexes.size(); ++i)
211 if(indexes[i].column() == 0)
212 stream << indexes[i].row();
215 mimeData->setData(
"application/freedebks", encodedData);
224 if(action != Qt::MoveAction)
227 QByteArray encodedData = data->data(
"application/freedebks");
228 QDataStream stream(&encodedData, QIODevice::ReadOnly);
233 while(!stream.atEnd())
237 for(
int i = 0; i < rows.size(); i++)
250 beginRemoveRows(parent, row, row+count-1);
251 for(
int i = 0; i < count; ++i)
266 return ResultsColumnCount;
276 int count = calculations.size();
277 beginInsertRows(QModelIndex(), row, row+count-1);
278 for(
int i = 0; i < count; ++i)
280 mResults.insert(row+i, calculations[i]);
292 for(
int i = 0; i < calculations.size(); ++i)
293 removeRow(
mResults.indexOf(calculations[i]));
301 return mStack->createRedoAction(
this);
309 return mStack->createUndoAction(
this);
328 for(
int i = 0; i <
mResults.size(); ++i)
334 return FdItemResults_p(0);
352 QModelIndexList results;
355 if(
mResults[i]->label().contains(text, Qt::CaseInsensitive))
356 results << index(i, 0);
366 QRegExp symbols(
"[-+*/()\"]");
371 while(calculation.size())
373 p = calculation.indexOf(symbols);
377 symbol = calculation[p];
379 if(p == 0 && symbol ==
"\"")
381 calculation = calculation.mid(1);
382 p = calculation.indexOf(
"\"");
387 term = calculation.left(p);
389 FdItemResults_p itemResults = this->
item(term);
390 if(itemCoa.isNull() && itemResults.isNull())
394 else if(p && !calculation.left(p).toDouble())
397 calculation = calculation.mid(p+1);
399 else if(!calculation.toDouble())
418 QStack<double> stack;
422 QRegExp operators(
"[-+*/]");
424 while(!postfix.isEmpty())
428 if(e.size() == 1 && e.contains(operators))
457 QString e = element.at(0);
461 i = element.indexOf(
"\"", 1);
466 element = element.mid(1, i-1);
468 FdItemResults_p itemResults = this->
item(element);
469 if(!itemCoa.isNull())
470 return itemCoa->balance();
471 else if(!itemResults.isNull())
472 return itemResults->result();
478 return element.toDouble();
483 QDomElement results = document.createElement(
"Results");
484 for(
int i = 0; i <
mResults.size(); ++i)
488 QDomElement calculation =
mResults[i]->toXml(document);
489 results.appendChild(calculation);
504 if(selected.isValid())
505 row = selected.row()+1;
515 id = base+QString::number(i);
525 return index(row, ResultsId);
544 for(
int i = 0; i <
mResults.size(); ++i)
558 QRegExp operators(
"[-+*/]");
559 QRegExp symbols(
"[-+*/()]");
560 QStack<QString> stack;
561 QStack<QString> postfix;
569 if(prevOp && QString(expr.at(0)) ==
"-")
571 if(QString(expr.at(1)) ==
"(")
573 i = expr.indexOf(symbols,1);
576 i = expr.indexOf(symbols);
580 postfix.push(expr.left(i));
584 if(QString(expr.at(0)) ==
"(")
591 if(e ==
"-" && prevOp)
593 i = expr.indexOf(symbols,1);
594 postfix.push(expr.left(i));
608 while(stack.top() !=
"(")
609 postfix.push(stack.pop());
614 if(e.contains(operators))
621 postfix.push(stack.pop());
631 while(!stack.isEmpty())
632 postfix.push(stack.pop());
640 if(op ==
"+" || op ==
"-")
642 else if(op ==
"*" || op ==
"/")