From dadc34655c3ab961b0b0b94a10eaaba710f0b5e8 Mon Sep 17 00:00:00 2001 From: tpearson Date: Mon, 4 Jul 2011 22:38:03 +0000 Subject: Added kmymoney git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/applications/kmymoney@1239792 283d02a7-25f6-0310-bc7c-ecb5cbfe19da --- kmymoney2/widgets/kmymoneyaccountcombo.cpp | 218 +++++++++++++++++++++++++++++ 1 file changed, 218 insertions(+) create mode 100644 kmymoney2/widgets/kmymoneyaccountcombo.cpp (limited to 'kmymoney2/widgets/kmymoneyaccountcombo.cpp') diff --git a/kmymoney2/widgets/kmymoneyaccountcombo.cpp b/kmymoney2/widgets/kmymoneyaccountcombo.cpp new file mode 100644 index 0000000..8a44786 --- /dev/null +++ b/kmymoney2/widgets/kmymoneyaccountcombo.cpp @@ -0,0 +1,218 @@ +/*************************************************************************** + kmymoneyaccountbutton - description + ------------------- + begin : Mon May 31 2004 + copyright : (C) 2000-2004 by Michael Edwardes + email : mte@users.sourceforge.net + Javier Campos Morales + Felix Rodriguez + John C + Thomas Baumgart + Kevin Tambascio +***************************************************************************/ + +/*************************************************************************** + * * + * 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. * + * * + ***************************************************************************/ + +// ---------------------------------------------------------------------------- +// QT Includes + +#include +#include +#include +#include + +// ---------------------------------------------------------------------------- +// KDE Includes + +// ---------------------------------------------------------------------------- +// Project Includes + +#include +#include +#include "kmymoneyaccountcompletion.h" + +KMyMoneyAccountCombo::KMyMoneyAccountCombo( QWidget* parent, const char* name ) : + KComboBox( parent, name ), + m_completion(0), + m_mlbDown(false) +{ +#ifndef KMM_DESIGNER + m_completion = new kMyMoneyAccountCompletion(this); + + connect(this, SIGNAL(clicked()), this, SLOT(slotButtonPressed())); + connect(m_completion, SIGNAL(itemSelected(const QString&)), this, SLOT(slotSelected(const QString&))); +#endif + + // make sure that we can display a minimum of characters + QFontMetrics fm(font()); + setMinimumWidth(fm.maxWidth()*15); + setMaximumHeight(height()); + + // we only use this one item and replace the text as we have our own dropdown box + insertItem(QString("")); +} + +KMyMoneyAccountCombo::~KMyMoneyAccountCombo() +{ +} + +void KMyMoneyAccountCombo::slotButtonPressed(void) +{ + m_completion->show(); +} + +void KMyMoneyAccountCombo::slotSelected(const QString& id) +{ + try { + MyMoneyAccount acc = MyMoneyFile::instance()->account(id); + setText(acc.name()); + emit accountSelected(id); + } catch(MyMoneyException *e) { + delete e; + } +} + +void KMyMoneyAccountCombo::setSelected(const QString& id) +{ + if(!id.isEmpty()) { + try { + MyMoneyAccount acc = MyMoneyFile::instance()->account(id); + setSelected(acc); + } catch(MyMoneyException *e) { + qDebug("Account '%s' not found in %s(%d)", id.data(), __FILE__, __LINE__); + delete e; + } + } else { + setText(QString()); + m_completion->setSelected(id); + } +} + +void KMyMoneyAccountCombo::setSelected(const MyMoneyAccount& acc) +{ + m_completion->setSelected(acc.id()); + setText(acc.name()); +} + +void KMyMoneyAccountCombo::setText(const QString& txt) +{ + changeItem(txt, currentItem()); +} + +int KMyMoneyAccountCombo::loadList(const QString& baseName, const QValueList& accountIdList, const bool clear) +{ + AccountSet set; + + return set.load(m_completion->selector(), baseName, accountIdList, clear); +} + +int KMyMoneyAccountCombo::loadList(KMyMoneyUtils::categoryTypeE typeMask) +{ + AccountSet set; + QValueList typeList; + + if(typeMask & KMyMoneyUtils::asset) { + set.addAccountGroup(MyMoneyAccount::Asset); + } + if(typeMask & KMyMoneyUtils::liability) { + set.addAccountGroup(MyMoneyAccount::Liability); + } + if(typeMask & KMyMoneyUtils::income) { + set.addAccountGroup(MyMoneyAccount::Income); + } + if(typeMask & KMyMoneyUtils::expense) { + set.addAccountGroup(MyMoneyAccount::Expense); + } + + return set.load(m_completion->selector()); +} + +int KMyMoneyAccountCombo::loadList(MyMoneyAccount::accountTypeE type) +{ + AccountSet set; + + set.addAccountType(type); + + return set.load(m_completion->selector()); +} + +void KMyMoneyAccountCombo::keyPressEvent(QKeyEvent* k) +{ + switch(k->key()) { + case Qt::Key_Tab: + break; + + case Qt::Key_Space: + emit clicked(); + break; + + default: + break; + } + return; +} + +void KMyMoneyAccountCombo::mousePressEvent(QMouseEvent *e) +{ + if ( e->button() != LeftButton ) { + e->ignore(); + return; + } + bool hit = rect().contains( e->pos() ); + if ( hit ) { // mouse press on button + m_mlbDown = TRUE; // left mouse button down + emit pressed(); + } +} + +void KMyMoneyAccountCombo::mouseReleaseEvent(QMouseEvent *e) +{ + if ( e->button() != LeftButton ) { + e->ignore(); + return; + } + if ( !m_mlbDown ) + return; + m_mlbDown = FALSE; // left mouse button up + emit released(); + if ( rect().contains( e->pos() ) ) { // mouse release on button + emit clicked(); + } +} + +int KMyMoneyAccountCombo::count(void) const +{ + return m_completion->selector()->accountList().count(); +} + +QStringList KMyMoneyAccountCombo::accountList(const QValueList& list) const +{ + return m_completion->selector()->accountList(list); +}; + +int KMyMoneyAccountCombo::loadList(const QValueList& list) +{ + // FIXME make the caller construct the AccountSet directly + AccountSet set; + QValueList::const_iterator it; + for(it = list.begin(); it != list.end(); ++it) { + set.addAccountType(static_cast(*it)); + } + return set.load(m_completion->selector()); +}; + +QStringList KMyMoneyAccountCombo::selectedAccounts(void) const +{ + QStringList list; + m_completion->selector()->selectedItems(list); + return list; +}; + +#include "kmymoneyaccountcombo.moc" -- cgit v1.2.1