r18941 - gnucash/trunk/src/gnc - Cutecash: Implement account selection in new transaction correctly.

Christian Stimming cstim at code.gnucash.org
Sun Mar 21 05:37:59 EDT 2010


Author: cstim
Date: 2010-03-21 05:37:59 -0400 (Sun, 21 Mar 2010)
New Revision: 18941
Trac: http://svn.gnucash.org/trac/changeset/18941

Modified:
   gnucash/trunk/src/gnc/AccountSelectionDelegate.cpp
   gnucash/trunk/src/gnc/AccountSelectionDelegate.hpp
   gnucash/trunk/src/gnc/Cmd.cpp
   gnucash/trunk/src/gnc/Split.cpp
   gnucash/trunk/src/gnc/Split.hpp
   gnucash/trunk/src/gnc/SplitListModel.cpp
Log:
Cutecash: Implement account selection in new transaction correctly.

Modified: gnucash/trunk/src/gnc/AccountSelectionDelegate.cpp
===================================================================
--- gnucash/trunk/src/gnc/AccountSelectionDelegate.cpp	2010-03-21 00:40:24 UTC (rev 18940)
+++ gnucash/trunk/src/gnc/AccountSelectionDelegate.cpp	2010-03-21 09:37:59 UTC (rev 18941)
@@ -25,6 +25,7 @@
 #include "gnc/AccountItemModel.hpp"
 #include "gnc/Book.hpp"
 #include "gnc/Split.hpp"
+#include "gnc/SplitListModel.hpp"
 
 #include <QDebug>
 #include <QComboBox>
@@ -32,6 +33,10 @@
 namespace gnc
 {
 
+AccountSelectionDelegate::AccountSelectionDelegate(QObject* parent)
+        : base_class(parent)
+{}
+
 QString AccountSelectionDelegate::displayText(const QVariant& value, const QLocale& locale) const
 {
     if (value.canConvert<Account>())
@@ -49,13 +54,15 @@
         const QStyleOptionViewItem &option,
         const QModelIndex &index) const
 {
-    Q_ASSERT(index.isValid());
     QComboBox* comboBox = new QComboBox(parent);
 
-    Split split(static_cast< ::Split*>(index.internalPointer()));
-    if (split)
+    Q_ASSERT(index.isValid());
+    const SplitListModel* smodel = dynamic_cast<const SplitListModel*>(index.model());
+    if (smodel)
     {
-        Book book = split.getBook();
+        Account modelAccount = smodel->getAccount();
+        Q_ASSERT(modelAccount);
+        Book book = modelAccount.getBook();
         Q_ASSERT(book);
         Account rootaccount = book.get_root_account();
         Q_ASSERT(rootaccount);
@@ -69,6 +76,7 @@
 void AccountSelectionDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
 {
     QComboBox* comboBox = dynamic_cast<QComboBox*>(editor);
+    Q_ASSERT(comboBox);
 
     Q_ASSERT(index.isValid());
 
@@ -76,14 +84,16 @@
     if (value.canConvert<Account>())
     {
         Account acc = value.value<Account>();
-        Q_ASSERT(acc);
-        const AccountListModel* amodel = dynamic_cast<const AccountListModel*>(comboBox->model());
-        Q_ASSERT(amodel);
-        comboBox->setCurrentIndex(amodel->indexOf(acc.get()));
+        if (acc)
+        {
+            const AccountListModel* amodel = dynamic_cast<const AccountListModel*>(comboBox->model());
+            Q_ASSERT(amodel);
+            comboBox->setCurrentIndex(amodel->indexOf(acc.get()));
+        }
     }
     else
     {
-        qDebug() << "huh? why no Account?";
+        qDebug() << "AccountSelectionDelegate::setEditorData: oops, item returns no account in the column";
     }
 }
 
@@ -91,14 +101,18 @@
         const QModelIndex &index) const
 {
     QComboBox* comboBox = dynamic_cast<QComboBox*>(editor);
+    Q_ASSERT(comboBox);
+
     int currentIndex = comboBox->currentIndex();
     if (currentIndex == -1)
         return;
     const AccountListModel* amodel = dynamic_cast<const AccountListModel*>(comboBox->model());
     Q_ASSERT(amodel);
     Account acc(amodel->at(currentIndex));
-    Q_ASSERT(acc);
-    model->setData(index, QVariant::fromValue(acc), Qt::EditRole);
+    if (acc)
+    {
+        model->setData(index, QVariant::fromValue(acc), Qt::EditRole);
+    }
 }
 
 

Modified: gnucash/trunk/src/gnc/AccountSelectionDelegate.hpp
===================================================================
--- gnucash/trunk/src/gnc/AccountSelectionDelegate.hpp	2010-03-21 00:40:24 UTC (rev 18940)
+++ gnucash/trunk/src/gnc/AccountSelectionDelegate.hpp	2010-03-21 09:37:59 UTC (rev 18941)
@@ -36,10 +36,9 @@
     Q_OBJECT
 public:
     typedef QStyledItemDelegate base_class;
-    AccountSelectionDelegate(QObject* parent = 0)
-            : base_class(parent)
-    {}
 
+    AccountSelectionDelegate(QObject* parent = 0);
+
     virtual QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
                                   const QModelIndex &index) const;
     virtual QString displayText(const QVariant& value, const QLocale& locale) const;

Modified: gnucash/trunk/src/gnc/Cmd.cpp
===================================================================
--- gnucash/trunk/src/gnc/Cmd.cpp	2010-03-21 00:40:24 UTC (rev 18940)
+++ gnucash/trunk/src/gnc/Cmd.cpp	2010-03-21 09:37:59 UTC (rev 18941)
@@ -372,25 +372,24 @@
 private:
     void set(const value_type& value)
     {
-        const TmpTransaction* p_trans = m_target.getParent();
-//         if (trans.countSplits() != 2)
-//             return;
-//         Split other = m_target.getOtherSplit();
-//         Q_ASSERT(other);
-//         Commodity originCommodity = m_target.getAccount().getCommodity();
-//         Commodity transCommodity = trans.getCurrency();
-//         Commodity otherCommodity = other.getAccount().getCommodity();
-//         if (originCommodity != transCommodity
-//                 || transCommodity != otherCommodity)
-//             return;
+        const TmpTransaction& trans = *m_target.getParent();
+        if (trans.countSplits() != 2)
+            return;
+        TmpSplit* p_other = m_target.getOtherSplit();
+        Q_ASSERT(p_other);
+        TmpSplit& other = *p_other;
+        Commodity originCommodity = Account(m_target.getAccount()).getCommodity();
+        Commodity transCommodity = trans.getCommodity();
+        Commodity otherCommodity = Account(other.getAccount()).getCommodity();
+        if (originCommodity != transCommodity
+                || transCommodity != otherCommodity)
+            return;
 
-//         trans.beginEdit();
         m_target.setValue(value);
         m_target.setAmount(value);
-//         Numeric valueNeg = value.neg();
-//         other.setAmount(valueNeg);
-//         other.setValue(valueNeg);
-//         trans.commitEdit();
+        Numeric valueNeg = value.neg();
+        other.setAmount(valueNeg);
+        other.setValue(valueNeg);
     }
 
 protected:

Modified: gnucash/trunk/src/gnc/Split.cpp
===================================================================
--- gnucash/trunk/src/gnc/Split.cpp	2010-03-21 00:40:24 UTC (rev 18940)
+++ gnucash/trunk/src/gnc/Split.cpp	2010-03-21 09:37:59 UTC (rev 18941)
@@ -53,6 +53,20 @@
     clear(_account);
 }
 
+TmpSplit* TmpSplit::getOtherSplit() const
+{
+    if (!parent)
+        return NULL;
+    const TmpTransaction& p = *parent;
+    if (p.countSplits() != 2)
+        return NULL;
+    TmpTransaction::TmpSplitQList& splits = const_cast<TmpTransaction&>(p).getSplits();
+    if (splits.front().getAccount() != account)
+        return &splits.front();
+    else
+        return &splits.back();
+}
+
 void TmpSplit::clear(::Account* _account)
 {
     account = _account;

Modified: gnucash/trunk/src/gnc/Split.hpp
===================================================================
--- gnucash/trunk/src/gnc/Split.hpp	2010-03-21 00:40:24 UTC (rev 18940)
+++ gnucash/trunk/src/gnc/Split.hpp	2010-03-21 09:37:59 UTC (rev 18941)
@@ -127,6 +127,8 @@
     const TmpTransaction* getParent() const { return parent; }
     void setParent(const TmpTransaction* v) { parent = v; }
 
+    TmpSplit* getOtherSplit() const;
+
     QString getMemo() const { return memo; }
     void setMemo(const QString& v) { memo = v; }
 

Modified: gnucash/trunk/src/gnc/SplitListModel.cpp
===================================================================
--- gnucash/trunk/src/gnc/SplitListModel.cpp	2010-03-21 00:40:24 UTC (rev 18940)
+++ gnucash/trunk/src/gnc/SplitListModel.cpp	2010-03-21 09:37:59 UTC (rev 18941)
@@ -29,6 +29,7 @@
 #include <QUndoStack>
 #include <QBrush>
 #include <QMessageBox>
+#include <QDateTime>
 
 #include "app-utils/gnc-ui-util.h" // for gnc_get_reconcile_str
 
@@ -71,10 +72,14 @@
 void SplitListModel::recreateTmpTrans()
 {
     m_tmpTransaction.clear();
-    m_tmpTransaction.push_back(TmpSplit(m_account.get()));
-//     m_tmpTransaction.push_back(TmpSplit(NULL));
     m_tmpTransaction.setCommodity(m_account.getCommodity());
     m_tmpTransaction.setDatePosted(QDate::currentDate());
+    m_tmpTransaction.push_back(TmpSplit(m_account.get()));
+    m_tmpTransaction.push_back(TmpSplit(NULL));
+
+    Q_ASSERT(m_tmpTransaction.countSplits() == 2);
+    Q_ASSERT(m_tmpTransaction.getSplits().front().getAccount() == m_account.get());
+    Q_ASSERT(m_tmpTransaction.getSplits().back().getAccount() == NULL);
 }
 
 SplitListModel::~SplitListModel()
@@ -167,7 +172,7 @@
         // Special case: We are in the last row which represents the
         // newly entered txn.
 
-        TmpSplit split(m_tmpTransaction.getSplits().front());
+        const TmpSplit& split(m_tmpTransaction.getSplits().front());
         const TmpTransaction& trans = m_tmpTransaction;
         Numeric amount = split.getValue();
         PrintAmountInfo printInfo(m_account, false);
@@ -203,8 +208,12 @@
         case COLUMN_ACCOUNT:
             switch (role)
             {
-//         case Qt::DisplayRole:
-//             return split.getCorrAccountFullName();
+            case Qt::DisplayRole:
+            case Qt::EditRole:
+                if (trans.countSplits() == 2)
+                    return QVariant::fromValue(Account(trans.getSplits().back().getAccount()));
+                else
+                    return QVariant(); // FIXME: Multi-split txn here
             default:
                 return QVariant();
             }
@@ -396,6 +405,9 @@
 
         TmpTransaction& trans = m_tmpTransaction;
         TmpSplit& split = trans.getSplits().front();
+        Q_ASSERT(split.getAccount() == m_account.get());
+        Q_ASSERT(trans.countSplits() == 2);
+        TmpSplit& other = trans.getSplits().back();
 
         // "Editing" is done by creating a Cmd-object and adding it to
         // the undo stack. That's in fact all that was needed to
@@ -418,6 +430,18 @@
         case COLUMN_DESC:
             cmd = cmd::setTransactionDescription(trans, value.toString());
             break;
+        case COLUMN_ACCOUNT:
+            if (value.canConvert<Account>())
+            {
+                if (trans.countSplits() == 2)
+                {
+                    cmd = cmd::setSplitAccount(other, value.value<Account>());
+                }
+                else
+                    QMessageBox::warning(NULL, tr("Unimplemented"),
+                                         tr("Sorry, but editing a transaction with more than two splits (here: %1) is not yet implemented.").arg(trans.countSplits()));
+            }
+            break;
         case COLUMN_RECONCILE:
         {
             QString str(value.toString());
@@ -451,7 +475,7 @@
                 if (index.column() == COLUMN_DECREASE)
                     n = n.neg();
                 // Check whether we have the simple case here
-                if (trans.countSplits() != 1)
+                if (trans.countSplits() != 2)
                 {
                     QMessageBox::warning(NULL, tr("Unimplemented"),
                                          tr("Sorry, but editing a transaction with more than two splits (here: %1) is not yet implemented.").arg(trans.countSplits()));
@@ -460,8 +484,14 @@
                 {
                     Commodity originCommodity = m_account.getCommodity();
                     Commodity transCommodity = trans.getCommodity();
-                    if (originCommodity != transCommodity)
+                    bool sameCommodities = (originCommodity == transCommodity);
+                    if (other.getAccount())
                     {
+                        Commodity otherCommodity = Account(other.getAccount()).getCommodity();
+                        sameCommodities = sameCommodities && (transCommodity == otherCommodity);
+                    }
+                    if (!sameCommodities)
+                    {
                         QMessageBox::warning(NULL, tr("Unimplemented"),
                                              tr("Sorry, but editing a transaction with different accounts is not yet implemented."));
                     }
@@ -523,7 +553,7 @@
                 }
                 else
                     QMessageBox::warning(NULL, tr("Unimplemented"),
-                                         tr("Sorry, but editing a transaction with more than two splits (here: %1) is not yet implemented.").arg(split.getParent().countSplits()));
+                                         tr("Sorry, but editing a transaction with more than two splits (here: %1) is not yet implemented.").arg(trans.countSplits()));
             }
             break;
         case COLUMN_RECONCILE:
@@ -667,6 +697,7 @@
             {
                 // Commit the new transaction
                 qDebug() << "Commit the new transaction as a real one";
+                m_tmpTransaction.setDateEntered(QDateTime::currentDateTime());
                 QUndoCommand* cmd = cmd::commitNewTransaction(m_tmpTransaction);
                 recreateTmpTrans();
                 m_undoStack->push(cmd);



More information about the gnucash-changes mailing list