20 #include "FdModelCoa.hpp"
21 #include "FdItemCoa.hpp"
22 #include "FdCommandsCoa.hpp"
23 #include "FdModelJournal.hpp"
24 #include "../gui/FdSubWindow.hpp"
58 connect(
mStack, SIGNAL(cleanChanged(
bool)),
mSubwindow, SLOT(cleanStateChanged(
bool)));
60 QList<FdItemCoa_p> items =
mRootItem->children();
61 for(
int i = 0; i < items.size(); ++i)
64 items << items[i]->children();
77 case CoaId:
return item->id();
78 case CoaLabel:
return item->label();
80 if(item->type() == CategoryType)
82 if(item->signature() == -1)
86 if(item->onBalanceSheet())
89 case CoaInitial:
return item->initialBalance();
90 case CoaBalance:
return item->balance();
100 if(role == Qt::TextAlignmentRole)
102 if(index.column() == CoaBalanceSheet || index.column() == CoaSigns)
103 return Qt::AlignCenter;
104 if(index.column() == CoaInitial || index.column() == CoaBalance)
105 return Qt::AlignRight;
107 if(index.column() != CoaBalanceSheet && role != Qt::DisplayRole && role != Qt::EditRole)
110 FdItemCoa_p item = this->
item(index);
112 if(index.column() == CoaBalanceSheet && (role != Qt::CheckStateRole || item->parent()->type() != RootType))
115 return data(item, CoaColumn(index.column()));
123 item->setId(value.toString());
126 item->setLabel(value.toString());
128 case CoaBalanceSheet:
129 item->setOnBalanceSheet(value.toBool());
132 item->setSignature(AccountSignature(value.toInt()));
135 item->setInitialBalance(value.toDouble());
138 QModelIndex index = this->
index(item, column);
140 emit dataChanged(index, index);
154 if (!index.isValid() || (role != Qt::EditRole && role != Qt::CheckStateRole) || value == QVariant(
""))
157 if(value ==
data(index, Qt::DisplayRole))
160 switch(index.column())
172 FdItemCoa_p item = this->
item(index);
179 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
183 case CoaId:
return tr(
"Id",
"Chart of accounts column header.");
184 case CoaLabel:
return tr(
"Label",
"Chart of accounts column header.");
185 case CoaSigns:
return tr(
"Signs",
"Chart of accounts column header.");
186 case CoaBalanceSheet:
return tr(
"On balance sheet",
"Chart of accounts column header, a column with tick boxes to decide whether or not the category belongs to the balance sheet.");
187 case CoaInitial:
return tr(
"Initial balance",
"Chart of accounts column header.");
188 case CoaBalance:
return tr(
"Balance",
"Chart of accounts column header.");
208 FdItemCoa_p item = this->
item(index);
211 if(!(item->type() == CategoryType && (index.column() == CoaInitial || index.column() == CoaSigns)) && index.column() != CoaBalanceSheet && index.column() != CoaBalance)
212 flags |= Qt::ItemIsEditable;
215 flags |= Qt::ItemIsDropEnabled;
217 if(index.column() == CoaBalanceSheet && item->parent()->type() == RootType)
218 flags |= Qt::ItemIsUserCheckable;
220 flags |= Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled;
226 return Qt::MoveAction;
231 return Qt::MoveAction;
239 QModelIndex
FdModelCoa::index(
int row,
int column,
const QModelIndex &parent)
const
241 if (parent.isValid() && parent.column() != 0)
242 return QModelIndex();
244 FdItemCoa_p parentItem =
item(parent);
245 if(FdItemCoa_p childItem = parentItem->child(row))
246 return createIndex(row, column,
mIndexes.key(childItem));
248 return QModelIndex();
256 return index(item, CoaId);
266 QList<FdItemCoa_p> items;
270 item = item->parent();
273 QModelIndex index = QModelIndex();
274 for(
int i = 0; i < items.size(); ++i)
275 index = this->
index(items[i]->row(),column,
index);
282 if (!index.isValid())
283 return QModelIndex();
285 FdItemCoa_p parentItem =
item(index)->parent();
288 return QModelIndex();
290 return createIndex(parentItem->row(), 0,
mIndexes.key(parentItem));
295 return QStringList() <<
"application/freedebks";
305 QMimeData*
mimeData =
new QMimeData();
306 QByteArray encodedData;
307 QDataStream stream(&encodedData, QIODevice::WriteOnly);
310 for(
int i = 0; i < indexes.size(); ++i)
312 if(indexes[i].column() == 0)
314 index = indexes[i].internalId();
319 mimeData->setData(
"application/freedebks", encodedData);
326 bool FdModelCoa::dropMimeData(
const QMimeData *data, Qt::DropAction action,
int row,
int column,
const QModelIndex &parent)
328 if(action != Qt::MoveAction)
332 QByteArray encodedData = data->data(
"application/freedebks");
333 QDataStream stream(&encodedData, QIODevice::ReadOnly);
334 QList<FdItemCoa_p> items;
336 FdItemCoa_p newParent = this->
item(parent);
343 row = newParent->childrenCount();
346 while(!stream.atEnd())
350 FdItemCoa_p item =
mIndexes.value(index);
360 return item(parent)->childrenCount();
365 return CoaColumnCount;
370 return insertItems(QList<FdItemCoa_p>() << item, row, parent);
378 QModelIndex parentIndex =
index(parent);
379 int count = items.size();
380 beginInsertRows(parentIndex, row, row + count - 1);
382 for(
int i = 0; i < count; ++i)
384 if(!parent->insertChild(items[i],row+i))
386 items[i]->setParent(parent);
400 QModelIndex parentIndex =
index(parent);
401 int row = item->row();
402 beginRemoveRows(parentIndex, row, row);
403 if(!parent->removeChild(row))
406 item->setParent(FdItemCoa_p(0));
428 int count = parent->childrenCount()-1;
430 for(
int i = 0; i < count; ++i)
433 if(parent->child(i+1)->id() < parent->child(i)->id())
437 while(j >= 0 && parent->child(i+1)->id() < parent->child(j)->id())
444 if(parent->child(j)->type() == CategoryType)
448 if(count > 0 && parent->child(count)->type() == CategoryType)
461 if(FdItemCoa_p item =
mIndexes.value(index.internalId()))
472 return mStack->createRedoAction(
this);
480 return mStack->createUndoAction(
this);
513 if(item.isNull() || item->type() != AccountType)
525 if(item.isNull() || item->type() != AccountType)
543 if(item->type() == AccountType)
548 else if(item->type() == CategoryType)
550 QList<FdItemCoa_p> children = item->children();
551 for(
int i = 0; i < children.size(); ++i)
584 return mRootItem->childrenAccountsLabels();
623 QList<FdItemCoa_p> items =
mRootItem->findChildren(text);
624 QModelIndexList results;
625 for(
int i = 0; i < items.size(); ++i)
626 results <<
index(items[i]);
645 FdItemCoa_p parentItem =
item(selected);
649 parentItem = parentItem->parent();
650 row = selected.row()+1;
653 row = parentItem->childrenCount();
660 id = parentItem->id()+QString::number(i);
670 label =
"Empty"+QString::number(i);
675 FdItemCoa_p item = FdItemCoa_p(
new FdItemCoa(parentItem,
id, label));
690 QList<FdItemCoa_p> items;
691 for(
int i = 0; i < selected.size(); ++i)
693 item = this->
item(selected[i]);