Cutecash: Add gnc::Numeric wrapper for gnc_numeric.

git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@18871 57a11ea4-9604-0410-9ed3-97b8803252fd
This commit is contained in:
Christian Stimming 2010-03-07 21:27:38 +00:00
parent 0bfd892d37
commit 190f7bac1a
11 changed files with 257 additions and 23 deletions

View File

@ -3,6 +3,7 @@
ADD_DEFINITIONS (-DG_LOG_DOMAIN=\"gnc.engine\")
INCLUDE_DIRECTORIES (${GLIB2_INCLUDE_DIRS})
INCLUDE_DIRECTORIES (${GCONF2_INCLUDE_DIRS})
INCLUDE_DIRECTORIES (${LIBINTL_INCLUDE_PATH})
INCLUDE_DIRECTORIES (${REGEX_INCLUDE_PATH})
INCLUDE_DIRECTORIES (${GUILE_INCLUDE_DIRS})
@ -41,11 +42,13 @@ SET (libgncmod_engine_HEADERS
gnc-budget.h
gnc-commodity.h
gnc-engine.h
gnc-euro.h
gnc-event.h
gnc-hooks.h
gnc-pricedb.h
gnc-session.h
gnc-session-scm.h
gnc-session.h
gnc-ui-util.h
gncObject.h
kvp-scm.h
policy.h
@ -98,11 +101,13 @@ SET (libgncmod_engine_SOURCES
gnc-budget.c
gnc-commodity.c
gnc-engine.c
gnc-euro.c
gnc-hooks.c
gnc-lot.c
gnc-pricedb.c
gnc-session.c
gnc-session-scm.c
gnc-session.c
gnc-ui-util.c
gncmod-engine.c
kvp-scm.c
engine-helpers.c

View File

@ -39,6 +39,9 @@ extern "C"
namespace gnc
{
typedef QList< ::Account*> AccountQList;
class Account : public WeakPointer< ::Account >
{
public:
@ -81,7 +84,6 @@ public:
//@}
typedef QList< ::Account*> AccountQList;
static AccountQList fromGList(GList* glist)
{
AccountQList result;

View File

@ -85,7 +85,7 @@ public:
QModelIndex parent(const QModelIndex &index) const { return QModelIndex(); }
private:
Account::AccountQList m_list;
AccountQList m_list;
};

View File

@ -12,7 +12,9 @@ LINK_DIRECTORIES (${GLIB2_LIBRARY_DIRS}
SET (gnc_SOURCES
AccountItemModel.cpp
Book.cpp
Numeric.cpp
Session.cpp
Split.cpp
SplitListModel.cpp
main.cpp
mainwindow.cpp

49
src/gnc/Numeric.cpp Normal file
View File

@ -0,0 +1,49 @@
/*
* Numeric.cpp
* Copyright (C) 2010 Christian Stimming
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, contact:
*
* Free Software Foundation Voice: +1-617-542-5942
* 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652
* Boston, MA 02110-1301, USA gnu@gnu.org
*/
#include "Numeric.hpp"
#include "gnc/Account.hpp"
#include "gnc/Split.hpp"
namespace gnc
{
// These are in the cpp file to avoid circular dependency between the
// headers
PrintAmountInfo::PrintAmountInfo(const Account& account, bool use_symbol)
: base_class(gnc_account_print_info(account.get(), use_symbol))
{}
PrintAmountInfo::PrintAmountInfo(const Split& split, bool use_symbol)
: base_class(gnc_split_amount_print_info(split.get(), use_symbol))
{}
QString Numeric::printAmount(const PrintAmountInfo& info)
{
char buf[256];
if (!xaccSPrintAmount (buf, *this, info))
buf[0] = '\0';
return QString::fromUtf8(buf);
}
} // END namespace gnc

134
src/gnc/Numeric.hpp Normal file
View File

@ -0,0 +1,134 @@
/*
* Numeric.hpp
* Copyright (C) 2010 Christian Stimming
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, contact:
*
* Free Software Foundation Voice: +1-617-542-5942
* 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652
* Boston, MA 02110-1301, USA gnu@gnu.org
*/
#ifndef GNC_NUMERIC_HPP
#define GNC_NUMERIC_HPP
// gnucash includes
#include "config.h"
extern "C"
{
#include "qof.h"
#include "engine/gnc-ui-util.h"
}
#include <QString>
namespace gnc
{
class Account;
class Split;
inline QString gchar_to_QString(gchar* tmp_string)
{
QString result = QString::fromUtf8(tmp_string);
g_free(tmp_string);
return result;
}
class PrintAmountInfo : public ::GNCPrintAmountInfo
{
public:
typedef ::GNCPrintAmountInfo base_class;
PrintAmountInfo(const base_class& other)
: base_class(other)
{}
PrintAmountInfo(bool use_symbol)
: base_class(gnc_default_print_info(use_symbol))
{}
PrintAmountInfo(const Account& account, bool use_symbol);
PrintAmountInfo(const Split& split, bool use_symbol);
static PrintAmountInfo share_places(int decplaces) { return gnc_share_print_info_places(decplaces); }
static PrintAmountInfo default_share() { return gnc_default_share_print_info(); }
static PrintAmountInfo default_price() { return gnc_default_price_print_info(); }
static PrintAmountInfo integral() { return gnc_integral_print_info(); }
};
class Numeric : public ::gnc_numeric
{
public:
typedef ::gnc_numeric base_class;
Numeric(gint64 num, gint64 denom)
{
base_class::num = num;
base_class::denom = denom;
}
Numeric(const base_class& other) : base_class(other) {}
static Numeric zero() { return Numeric(0, 1); }
Numeric(double in, gint64 denom, gint how)
{
*this = double_to_gnc_numeric(in, denom, how);
}
static bool string_to_numeric(const QString& str, Numeric& n)
{
return string_to_gnc_numeric(str.toUtf8(), &n);
}
Numeric(GNCNumericErrorCode error_code)
{
*this = gnc_numeric_error(error_code);
}
gint64 num() const { return base_class::num; }
gint64 denom() const { return base_class::denom; }
gdouble to_double() const { return gnc_numeric_to_double(*this); }
QString to_string() const
{
return gchar_to_QString(gnc_numeric_to_string(*this));
}
GNCNumericErrorCode check() const { return gnc_numeric_check(*this); }
gint compare(const Numeric& b) const { return gnc_numeric_compare(*this, b); }
bool zero_p() const { return gnc_numeric_zero_p(*this); }
bool negative_p() const { return gnc_numeric_negative_p(*this); }
bool positive_p() const { return gnc_numeric_positive_p(*this); }
bool eq(const Numeric& b) const { return gnc_numeric_eq(*this, b); }
bool equal(const Numeric& b) const { return gnc_numeric_equal(*this, b); }
bool same(const Numeric& b, gint64 denom, gint how) const { return gnc_numeric_same(*this, b, denom, how); }
Numeric add(const Numeric& b, gint64 denom, gint how) const { return gnc_numeric_add(*this, b, denom, how); }
Numeric sub(const Numeric& b, gint64 denom, gint how) const { return gnc_numeric_sub(*this, b, denom, how); }
Numeric mul(const Numeric& b, gint64 denom, gint how) const { return gnc_numeric_mul(*this, b, denom, how); }
Numeric div(const Numeric& b, gint64 denom, gint how) const { return gnc_numeric_div(*this, b, denom, how); }
Numeric neg() const { return gnc_numeric_neg(*this); }
Numeric abs() const { return gnc_numeric_abs(*this); }
Numeric add_fixed(const Numeric& b) const { return gnc_numeric_add_fixed(*this, b); }
Numeric sub_fixed(const Numeric& b) const { return gnc_numeric_sub_fixed(*this, b); }
Numeric reduce() const { return gnc_numeric_reduce(*this); }
QString printAmount(const PrintAmountInfo& info);
};
inline bool operator==(const Numeric& a, const Numeric& b)
{
return a.equal(b);
}
} // END namespace gnc
#endif

43
src/gnc/Split.cpp Normal file
View File

@ -0,0 +1,43 @@
/*
* Split.cpp
* Copyright (C) 2010 Christian Stimming
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, contact:
*
* Free Software Foundation Voice: +1-617-542-5942
* 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652
* Boston, MA 02110-1301, USA gnu@gnu.org
*/
#include "Split.hpp"
#include "gnc/Account.hpp"
#include "gnc/Book.hpp"
#include "gnc/Transaction.hpp"
namespace gnc
{
Book Split::getBook() const { return xaccSplitGetBook(get()); }
Account Split::getAccount() const { return xaccSplitGetAccount(get()); }
void Split::setAccount(Account& acc) { xaccSplitSetAccount(get(), acc.get()); }
Transaction Split::getParent() const { return xaccSplitGetParent(get()); }
void Split::setParent(Transaction& trans) { xaccSplitSetParent(get(), trans.get()); }
} // END namespace gnc

View File

@ -34,19 +34,17 @@ extern "C"
#include <QString>
#include <QList>
namespace gnc
{
class Split;
typedef QList< ::Split*> SplitQList;
}
#include "gnc/WeakPointer.hpp"
#include "gnc/Account.hpp"
#include "gnc/Book.hpp"
#include "gnc/Transaction.hpp"
#include "gnc/Numeric.hpp"
namespace gnc
{
class Book;
class Account;
class Transaction;
typedef QList< ::Split*> SplitQList;
class Split : public WeakPointer< ::Split >
{
@ -56,12 +54,12 @@ public:
: base_class(ptr)
{ }
Book getBook() const { return xaccSplitGetBook(get()); }
Account getAccount() const { return xaccSplitGetAccount(get()); }
void setAccount(Account& acc) { xaccSplitSetAccount(get(), acc.get()); }
Book getBook() const;
Account getAccount() const;
void setAccount(Account& acc);
Transaction getParent() const { return xaccSplitGetParent(get()); }
void setParent(Transaction& trans) { xaccSplitSetParent(get(), trans.get()); }
Transaction getParent() const;
void setParent(Transaction& trans);
QString getMemo() const { return QString::fromUtf8(xaccSplitGetMemo(get())); }
void setMemo(const QString& v) { xaccSplitSetMemo(get(), v.toUtf8()); }
@ -76,10 +74,7 @@ public:
QString getCorrAccountFullName() const
{
char * r = xaccSplitGetCorrAccountFullName(get());
QString result = QString::fromUtf8(r);
g_free (r);
return result;
return gchar_to_QString(xaccSplitGetCorrAccountFullName(get()));
}
QString getCorrAccountName() const { return QString::fromUtf8(xaccSplitGetCorrAccountName(get())); }
QString getCorrAccountCode() const { return QString::fromUtf8(xaccSplitGetCorrAccountCode(get())); }

View File

@ -1,5 +1,5 @@
/*
* SplitListModel.hpp
* SplitListModel.cpp
* Copyright (C) 2010 Christian Stimming
*
* This program is free software; you can redistribute it and/or
@ -21,6 +21,7 @@
*/
#include "SplitListModel.hpp"
#include "gnc/Transaction.hpp"
#include <QDebug>
namespace gnc

View File

@ -43,6 +43,7 @@ extern "C"
#include "engine/gnc-session.h"
#include "engine/engine-helpers.h"
#include "engine/gnc-engine.h"
#include "engine/gnc-ui-util.h" // for gnc_ui_util_init()
#include "swig-runtime.h"
#include "backend/xml/gnc-backend-xml.h"
@ -183,6 +184,7 @@ main(int argc, char ** argv)
//gnc_module_init_backend_dbi();
gnc_module_init_business_core_init();
gnc_module_init_business_core_xml_init();
gnc_ui_util_init();
// From here on the new C++ code
QApplication app(argc, argv);

View File

@ -46,6 +46,7 @@ extern "C"
#include "gnc/Account.hpp"
#include "gnc/AccountItemModel.hpp"
#include "gnc/Book.hpp"
#include "gnc/Numeric.hpp"
#include "gnc/Split.hpp"
#include "gnc/SplitListModel.hpp"