r18922 - gnucash/trunk/src/gnc - Cutecash: Add separate Cmd class for setting the amount/value.

Christian Stimming cstim at code.gnucash.org
Tue Mar 16 15:48:42 EDT 2010


Author: cstim
Date: 2010-03-16 15:48:42 -0400 (Tue, 16 Mar 2010)
New Revision: 18922
Trac: http://svn.gnucash.org/trac/changeset/18922

Modified:
   gnucash/trunk/src/gnc/Cmd.cpp
   gnucash/trunk/src/gnc/Cmd.hpp
   gnucash/trunk/src/gnc/Commodity.hpp
   gnucash/trunk/src/gnc/Numeric.hpp
   gnucash/trunk/src/gnc/SplitListModel.cpp
   gnucash/trunk/src/gnc/Transaction.hpp
Log:
Cutecash: Add separate Cmd class for setting the amount/value.

Setting the amount in the register now works.

Modified: gnucash/trunk/src/gnc/Cmd.cpp
===================================================================
--- gnucash/trunk/src/gnc/Cmd.cpp	2010-03-16 17:16:35 UTC (rev 18921)
+++ gnucash/trunk/src/gnc/Cmd.cpp	2010-03-16 19:48:42 UTC (rev 18922)
@@ -77,6 +77,79 @@
 
 // ////////////////////////////////////////////////////////////
 
+class SplitValueAndAmountCmd : public QUndoCommand
+{
+public:
+    typedef QUndoCommand base_class;
+    typedef Split target_type;
+    typedef Numeric value_type;
+
+    /** Constructor without a getter-function but instead the previous
+     * value given directly.
+     */
+    SplitValueAndAmountCmd(const QString& text,
+                           WeakPointer<target_type::element_type>& targetPtr,
+                           const value_type& previousValue,
+                           const value_type& newValue,
+                           QUndoCommand *parent = 0)
+            : base_class(text, parent)
+            , m_target(targetPtr.get())
+            , m_previousValue(previousValue)
+            , m_newValue(newValue)
+    {
+        Q_ASSERT(m_target);
+    }
+
+    virtual void redo()
+    {
+        set(m_newValue);
+    }
+
+    virtual void undo()
+    {
+        set(m_previousValue);
+    }
+
+
+private:
+    void set(const value_type& value)
+    {
+        Transaction 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;
+
+        trans.beginEdit();
+        m_target.setValue(value);
+        m_target.setAmount(value);
+        Numeric valueNeg = value.neg();
+        other.setAmount(valueNeg);
+        other.setValue(valueNeg);
+        trans.commitEdit();
+    }
+
+protected:
+    target_type m_target;
+    value_type m_previousValue;
+    value_type m_newValue;
+
+};
+
+QUndoCommand* setSplitValueAndAmount(Split& t, const Numeric& newValue)
+{
+    return new SplitValueAndAmountCmd(QObject::tr("Edit Transaction Value"),
+                                      t, t.getValue(), newValue);
+}
+
+// ////////////////////////////////////////////////////////////
+
 QUndoCommand* setTransactionNum(Transaction& t, const QString& newValue)
 {
     return new Cmd<Transaction, QString>(QObject::tr("Edit Transaction Number"),

Modified: gnucash/trunk/src/gnc/Cmd.hpp
===================================================================
--- gnucash/trunk/src/gnc/Cmd.hpp	2010-03-16 17:16:35 UTC (rev 18921)
+++ gnucash/trunk/src/gnc/Cmd.hpp	2010-03-16 19:48:42 UTC (rev 18922)
@@ -69,7 +69,7 @@
     typedef SetterFunc setter_func;
 
     /// Type of the getter function to retrieve the current value from the target object
-    typedef value_type (TargetT::*getter_func)() const;
+    typedef value_type (target_type::*getter_func)() const;
 
     /** Constructor.
      * @param text The QUndoCommand's text which will be displayed in the Undo action.
@@ -80,7 +80,7 @@
      * @param parent The parent QUndoCommand instance, or NULL.
      */
     Cmd(const QString& text,
-        WeakPointer<typename TargetT::element_type>& targetPtr,
+        WeakPointer<typename target_type::element_type>& targetPtr,
         setter_func setter,
         getter_func getter,
         const value_type& newValue,
@@ -91,6 +91,8 @@
             , m_previousValue((m_target.*getter)())
             , m_newValue(newValue)
     {
+        Q_ASSERT(m_target);
+        Q_ASSERT(m_setter);
     }
 
     /** Overloaded constructor without a getter-function but instead
@@ -104,7 +106,7 @@
      * @param parent The parent QUndoCommand instance, or NULL.
      */
     Cmd(const QString& text,
-        WeakPointer<typename TargetT::element_type>& targetPtr,
+        WeakPointer<typename target_type::element_type>& targetPtr,
         setter_func setter,
         const value_type& previousValue,
         const value_type& newValue,
@@ -115,6 +117,8 @@
             , m_previousValue(previousValue)
             , m_newValue(newValue)
     {
+        Q_ASSERT(m_target);
+        Q_ASSERT(m_setter);
     }
 
     virtual void redo()
@@ -137,7 +141,7 @@
     }
 
 protected:
-    TargetT m_target;
+    target_type m_target;
     setter_func m_setter;
     value_type m_previousValue;
     value_type m_newValue;
@@ -161,6 +165,7 @@
 QUndoCommand* setTransactionDescription(Transaction& t, const QString& newValue);
 QUndoCommand* setTransactionNotes(Transaction& t, const QString& newValue);
 QUndoCommand* setTransactionDate(Transaction& t, const QDate& newValue);
+QUndoCommand* setSplitValueAndAmount(Split& t, const Numeric& newValue);
 
 } // END namespace cmd
 

Modified: gnucash/trunk/src/gnc/Commodity.hpp
===================================================================
--- gnucash/trunk/src/gnc/Commodity.hpp	2010-03-16 17:16:35 UTC (rev 18921)
+++ gnucash/trunk/src/gnc/Commodity.hpp	2010-03-16 19:48:42 UTC (rev 18922)
@@ -54,8 +54,20 @@
     bool get_quote_flag() const { return gnc_commodity_get_quote_flag(get()); }
 
     bool is_currency() const { return gnc_commodity_is_currency(get()); }
+
+    bool equal(const Commodity& other) const { return gnc_commodity_equal(get(), other.get()); }
 };
 
+inline bool operator==(const Commodity& a, const Commodity& b)
+{
+    return a.equal(b);
+}
+
+inline bool operator!=(const Commodity& a, const Commodity& b)
+{
+    return !(a == b);
+}
+
 } // END namespace gnc
 
 #endif

Modified: gnucash/trunk/src/gnc/Numeric.hpp
===================================================================
--- gnucash/trunk/src/gnc/Numeric.hpp	2010-03-16 17:16:35 UTC (rev 18921)
+++ gnucash/trunk/src/gnc/Numeric.hpp	2010-03-16 19:48:42 UTC (rev 18922)
@@ -49,6 +49,8 @@
  */
 inline QString gchar_to_QString(gchar* tmp_string)
 {
+    if (!tmp_string)
+        return QString();
     QString result = QString::fromUtf8(tmp_string);
     g_free(tmp_string);
     return result;

Modified: gnucash/trunk/src/gnc/SplitListModel.cpp
===================================================================
--- gnucash/trunk/src/gnc/SplitListModel.cpp	2010-03-16 17:16:35 UTC (rev 18921)
+++ gnucash/trunk/src/gnc/SplitListModel.cpp	2010-03-16 19:48:42 UTC (rev 18922)
@@ -27,7 +27,10 @@
 #include <QDebug>
 #include <QUndoStack>
 #include <QBrush>
+#include <QMessageBox>
 
+#include "app-utils/gnc-ui-util.h" // for gnc_get_reconcile_str
+
 namespace gnc
 {
 
@@ -153,7 +156,8 @@
         switch (role)
         {
         case Qt::DisplayRole:
-            return QChar(split.getReconcile());
+        case Qt::EditRole:
+            return QString::fromUtf8(gnc_get_reconcile_str(split.getReconcile()));
         default:
             return QVariant();
         }
@@ -261,30 +265,69 @@
             cmd = cmd::setTransactionDescription(trans, value.toString());
             break;
         case 4:
-            cmd = cmd::setSplitReconcile(split, value.toChar().toLatin1());
+        {
+            QString str(value.toString());
+            if (str.size() > 0)
+            {
+                char recn = str[0].toLatin1();
+                switch (recn)
+                {
+                case NREC:
+                case CREC:
+                case YREC:
+                case FREC:
+                case VREC:
+                    cmd = cmd::setSplitReconcile(split, recn);
+                    break;
+                default:
+                    qDebug() << "Unknown reconcile string:" << str;
+                }
+            }
             break;
+        }
         case 5:
         case 6:
         {
-            QString str(value.toString());
+            QString str(value.toString().simplified());
             Numeric n;
-//             bool x;
-//             double v = value.toDouble(&x);
             QString errmsg = n.parse(str);
             if (errmsg.isEmpty())
             {
                 qDebug() << "Does setting numeric work? numeric=" << n.to_string();
                 if (index.column() == 6)
                     n = n.neg();
-//                 cmd = cmd::setSplitAmount(split, n);
-//                 m_undoStack->push(cmd);
-                cmd = cmd::setSplitValue(split, n);
+                // Check whether we have the simple case here
+                if (split.getParent().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(split.getParent().countSplits()));
+                }
+                else
+                {
+                    Transaction trans = split.getParent();
+                    Split other = split.getOtherSplit();
+                    Q_ASSERT(other);
+                    Commodity originCommodity =split.getAccount().getCommodity();
+                    Commodity transCommodity = trans.getCurrency();
+                    Commodity otherCommodity = other.getAccount().getCommodity();
+                    if (originCommodity != transCommodity
+                            || transCommodity != otherCommodity)
+                    {
+                        QMessageBox::warning(NULL, tr("Unimplemented"),
+                                             tr("Sorry, but editing a transaction with different accounts is not yet implemented."));
+                    }
+                    else
+                    {
+                        // This is the really simple case which we can
+                        // handle now.
+                        cmd = cmd::setSplitValueAndAmount(split, n);
+                    }
+                }
             }
             else
             {
-                qDebug() << "Cannot convert string to gnc_numeric:" << str;
+                qDebug() << "Cannot convert this string to gnc_numeric:" << str;
             }
-            // Sigh. This doesn't seem to work so far.
             break;
         }
         default:

Modified: gnucash/trunk/src/gnc/Transaction.hpp
===================================================================
--- gnucash/trunk/src/gnc/Transaction.hpp	2010-03-16 17:16:35 UTC (rev 18921)
+++ gnucash/trunk/src/gnc/Transaction.hpp	2010-03-16 19:48:42 UTC (rev 18922)
@@ -58,6 +58,12 @@
             : base_class(ptr)
     { }
 
+    void beginEdit() { xaccTransBeginEdit(get()); }
+    void commitEdit() { xaccTransCommitEdit(get()); }
+    void rollbackEdit() { xaccTransRollbackEdit(get()); }
+    bool isOpen() const { return xaccTransIsOpen(get()); }
+
+
     QString getNum() const { return QString::fromUtf8(xaccTransGetNum(get())); }
     void setNum(const QString& v) { xaccTransSetNum(get(), v.toUtf8()); }
 
@@ -68,6 +74,11 @@
     void setNotes(const QString& v) { xaccTransSetNotes(get(), v.toUtf8()); }
 
     int countSplits() const { return xaccTransCountSplits(get()); }
+    Split findSplitByAccount(const Account& acc) const { return xaccTransFindSplitByAccount(get(), acc.get()); }
+    void appendSplit(Split& split) { xaccSplitSetParent(split.get(), get()); }
+    Split getSplit(int i) const { return xaccTransGetSplit(get(), i); }
+    int getSplitIndex(const Split& split) const { return xaccTransGetSplitIndex(get(), split.get()); }
+    SplitList* getSplitList() const { return xaccTransGetSplitList(get()); }
 
     Commodity getCurrency() const { return xaccTransGetCurrency(get()); }
     void setCurrency(const Commodity& c) { xaccTransSetCurrency(get(), c.get()); }



More information about the gnucash-changes mailing list