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/kmymoneylineedit.cpp | 152 +++++++++++++++++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 kmymoney2/widgets/kmymoneylineedit.cpp (limited to 'kmymoney2/widgets/kmymoneylineedit.cpp') diff --git a/kmymoney2/widgets/kmymoneylineedit.cpp b/kmymoney2/widgets/kmymoneylineedit.cpp new file mode 100644 index 0000000..18e2e31 --- /dev/null +++ b/kmymoney2/widgets/kmymoneylineedit.cpp @@ -0,0 +1,152 @@ +/*************************************************************************** + kmymoneylineedit.cpp - description + ------------------- + begin : Wed May 9 2001 + copyright : (C) 2001 by Michael Edwardes + email : mte@users.sourceforge.net + Javier Campos Morales + Felix Rodriguez + ***************************************************************************/ + +/*************************************************************************** + * * + * 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 + +// ---------------------------------------------------------------------------- +// KDE Includes + +#include +#include + +// ---------------------------------------------------------------------------- +// Project Includes + +#include "kmymoneylineedit.h" + +kMyMoneyLineEdit::kMyMoneyLineEdit(QWidget *w, const char* name, bool forceMonetaryDecimalSymbol, int alignment) : + KLineEdit(w, name), + m_forceMonetaryDecimalSymbol(forceMonetaryDecimalSymbol) +{ + setAlignment(alignment); +} + +kMyMoneyLineEdit::~kMyMoneyLineEdit() +{ +} + +void kMyMoneyLineEdit::resetText(void) +{ + setText(m_text); +} + +void kMyMoneyLineEdit::loadText(const QString& text) +{ + m_text = text; + setText(text); +} + +void kMyMoneyLineEdit::focusOutEvent(QFocusEvent *ev) +{ + // if the current text is not in the list of + // possible completions, we have a new payee + // and signal that to the outside world. + if(text() != m_text) { + emit lineChanged(text()); + } + KLineEdit::focusOutEvent(ev); + + // force update of hint + if(text().isEmpty()) + repaint(); +} + +void kMyMoneyLineEdit::keyReleaseEvent(QKeyEvent* k) +{ + if(m_forceMonetaryDecimalSymbol) { + if(k->state() & Qt::Keypad) { + if(k->key() == Qt::Key_Comma + || k->key() == Qt::Key_Period) { + if(KGlobal::locale()->monetaryDecimalSymbol() == ",") { + QKeyEvent newk(k->type(), Qt::Key_Comma, ',', k->state(), ",", k->isAutoRepeat(), k->count()); + KLineEdit::keyReleaseEvent(&newk); + k->ignore(); + return; + } + + if(KGlobal::locale()->monetaryDecimalSymbol() == ".") { + QKeyEvent newk(k->type(), Qt::Key_Comma, ',', k->state(), ".", k->isAutoRepeat(), k->count()); + KLineEdit::keyReleaseEvent(&newk); + k->ignore(); + return; + } + } + } + } + KLineEdit::keyReleaseEvent(k); +} + +void kMyMoneyLineEdit::keyPressEvent(QKeyEvent* k) +{ + if(m_forceMonetaryDecimalSymbol) { + if(k->state() & Qt::Keypad) { + if(k->key() == Qt::Key_Comma + || k->key() == Qt::Key_Period) { + if(KGlobal::locale()->monetaryDecimalSymbol() == ",") { + QKeyEvent newk(k->type(), Qt::Key_Comma, ',', k->state(), ",", k->isAutoRepeat(), k->count()); + KLineEdit::keyPressEvent(&newk); + k->ignore(); + return; + } + + if(KGlobal::locale()->monetaryDecimalSymbol() == ".") { + QKeyEvent newk(k->type(), Qt::Key_Period, '.', k->state(), ".", k->isAutoRepeat(), k->count()); + KLineEdit::keyPressEvent(&newk); + k->ignore(); + return; + } + } + } + } + KLineEdit::keyPressEvent(k); +} + +void kMyMoneyLineEdit::drawContents( QPainter *p) +{ + KLineEdit::drawContents(p); + + if(text().isEmpty() && !m_hint.isEmpty() && !hasFocus()) { + const int innerMargin = 1; + + // the following 5 lines are taken from QLineEdit::drawContents() + QRect cr = contentsRect(); + QFontMetrics fm = fontMetrics(); + QRect lineRect( cr.x() + innerMargin, cr.y() + (cr.height() - fm.height() + 1) / 2, + cr.width() - 2*innerMargin, fm.height() ); + QPoint topLeft = lineRect.topLeft() - QPoint(0, -fm.ascent()); + + p->save(); + QFont f = p->font(); + f.setItalic(true); + f.setWeight(QFont::Light); + p->setFont(f); + p->setPen(palette().disabled().text()); + + p->drawText(topLeft, m_hint); + + p->restore(); + } +} + +#include "kmymoneylineedit.moc" -- cgit v1.2.1