diff --git a/kmymoney/payeeidentifier/ibanandbic/CMakeLists.txt b/kmymoney/payeeidentifier/ibanandbic/CMakeLists.txt index ae3cc4cde..e73b02eb7 100644 --- a/kmymoney/payeeidentifier/ibanandbic/CMakeLists.txt +++ b/kmymoney/payeeidentifier/ibanandbic/CMakeLists.txt @@ -1,68 +1,69 @@ configure_file(ibanbicdata.desktop.in ibanbicdata.desktop) set ( PAYEEIDENTIFIER_IBAN_BIC_SCRS + bankcodemodel.cpp bicmodel.cpp ibanbicdata.cpp ibanbic.cpp ) kde4_add_library( payeeidentifier_iban_bic SHARED ${PAYEEIDENTIFIER_IBAN_BIC_SCRS} ) generate_export_header(payeeidentifier_iban_bic BASE_NAME iban_bic_identifier) target_link_libraries( payeeidentifier_iban_bic PUBLIC kmm_payeeidentifier ${QT_QTCORE_LIBRARY} ${QT_QTSQL_LIBRARY} ${QT_QTXML_LIBRARY} ${KDE4_KDECORE_LIBS} ${GMP_LIBRARIES} ) add_dependencies(payeeidentifier_iban_bic ibanbicdata_de ibanbicdata_ch) set_target_properties(payeeidentifier_iban_bic PROPERTIES VERSION ${GENERIC_LIB_VERSION} SOVERSION ${GENERIC_LIB_SOVERSION}) install(TARGETS payeeidentifier_iban_bic ${INSTALL_TARGETS_DEFAULT_ARGS} ) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/ibanbicdata.desktop DESTINATION ${SERVICETYPES_INSTALL_DIR}) # Storage plugin configure_file(kmymoney-ibanbic-storageplugin.desktop.in kmymoney-ibanbic-storageplugin.desktop) set ( IBAN_BIC_STORAGE_PLUGIN_SCRS ibanbicstorageplugin.cpp ) kde4_add_plugin( payeeidentifier_ibanbic_storageplugin ${IBAN_BIC_STORAGE_PLUGIN_SCRS} ) target_link_libraries( payeeidentifier_ibanbic_storageplugin ${QT_QTCORE_LIBRARY} ${QT_QTSQL_LIBRARY} kmm_mymoney ) install(TARGETS payeeidentifier_ibanbic_storageplugin DESTINATION ${PLUGIN_INSTALL_DIR}) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/kmymoney-ibanbic-storageplugin.desktop DESTINATION ${SERVICES_INSTALL_DIR} ) install( FILES ibanbic.h DESTINATION ${INCLUDE_INSTALL_DIR}/kmymoney/payeeidentifier/ibanandbic COMPONENT Devel) if( KDE4_BUILD_TESTS ) add_subdirectory(tests) endif() add_subdirectory(widgets) diff --git a/kmymoney/payeeidentifier/ibanandbic/tests/CMakeLists.txt b/kmymoney/payeeidentifier/ibanandbic/tests/CMakeLists.txt index c506d41ad..9cba2c33a 100644 --- a/kmymoney/payeeidentifier/ibanandbic/tests/CMakeLists.txt +++ b/kmymoney/payeeidentifier/ibanandbic/tests/CMakeLists.txt @@ -1,14 +1,28 @@ set( payeeidentifierIbanBicTest_SCRS internationalaccountidentifiertest.cpp ) kde4_add_unit_test( payeeidentifier_iban_bic-test TESTNAME kmymoney-payeeidentifier_iban_bic ${payeeidentifierIbanBicTest_SCRS} ) target_link_libraries( payeeidentifier_iban_bic-test ${QT_QTTEST_LIBRARY} payeeidentifier_iban_bic ) + +set( payeeidentifierIbanWidgetTest_SCRS + payeeidentifierwidgettest.cpp +) + +kde4_add_unit_test( payeeidentifier_iban_widget-test + TESTNAME kmymoney-payeeidentifier_iban_widget + ${payeeidentifierIbanWidgetTest_SCRS} +) + +target_link_libraries( payeeidentifier_iban_widget-test + ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY} + payeeidentifier_iban_bic_widgets +) diff --git a/kmymoney/payeeidentifier/ibanandbic/tests/payeeidentifierwidgettest.cpp b/kmymoney/payeeidentifier/ibanandbic/tests/payeeidentifierwidgettest.cpp new file mode 100644 index 000000000..340660f50 --- /dev/null +++ b/kmymoney/payeeidentifier/ibanandbic/tests/payeeidentifierwidgettest.cpp @@ -0,0 +1,32 @@ +#include "payeeidentifierwidgettest.h" + +#include "../widgets/ibanbicitemedit.h" +#include "../widgets/kbankcodeedit.h" +#include "../widgets/kbicedit.h" +#include "../widgets/kibanlineedit.h" + +#include +#include +#include + +PayeeIdentifierWidgetTest::PayeeIdentifierWidgetTest() +{ + QGridLayout *layout = new QGridLayout; + layout->addWidget(new QLabel("KBicEdit"), 0, 0); + layout->addWidget(new KBicEdit(this), 0, 1); + layout->addWidget(new QLabel("KIbanLineEdit"), 1, 0); + layout->addWidget(new KIbanLineEdit(this), 1, 1); + layout->addWidget(new QLabel("KBankCodeEdit"), 2, 0); + layout->addWidget(new KBankCodeEdit(this), 2, 1); + layout->addWidget(new QLabel("ibanbicitemedit"), 3, 0); + layout->addWidget(new ibanBicItemEdit(this), 3, 1); + setLayout(layout); +} + +int main(int argc, char **argv) +{ + QApplication app(argc, argv); + + PayeeIdentifierWidgetTest test; + test.exec(); +} diff --git a/kmymoney/payeeidentifier/ibanandbic/tests/payeeidentifierwidgettest.h b/kmymoney/payeeidentifier/ibanandbic/tests/payeeidentifierwidgettest.h new file mode 100644 index 000000000..65378ad32 --- /dev/null +++ b/kmymoney/payeeidentifier/ibanandbic/tests/payeeidentifierwidgettest.h @@ -0,0 +1,13 @@ +#ifndef PAYEEIDENTIFIERWIDGETTEST_H +#define PAYEEIDENTIFIERWIDGETTEST_H + +#include + +class PayeeIdentifierWidgetTest : public QDialog +{ +Q_OBJECT +public: + PayeeIdentifierWidgetTest(); +}; + +#endif // PAYEEIDENTIFIERWIDGETTEST_H diff --git a/kmymoney/payeeidentifier/ibanandbic/widgets/CMakeLists.txt b/kmymoney/payeeidentifier/ibanandbic/widgets/CMakeLists.txt index 60edadccf..d38759e99 100644 --- a/kmymoney/payeeidentifier/ibanandbic/widgets/CMakeLists.txt +++ b/kmymoney/payeeidentifier/ibanandbic/widgets/CMakeLists.txt @@ -1,72 +1,76 @@ ## Shared library for widgets set ( IBAN_BIC_WIDGETS_SCRS + bankcodevalidator.cpp + kbankcodeedit.cpp kibanlineedit.cpp kbicedit.cpp + ibanbicitemedit.cpp ibanvalidator.cpp bicvalidator.cpp ibanbicitemdelegate.cpp ibanbicitemedit.cpp ) set( IBAN_BIC_WIDGETS_HEADERS kibanlineedit.h kbicedit.h ibanvalidator.h + ibanbicitemedit.h bicvalidator.h ibanbicitemdelegate.h ${CMAKE_CURRENT_BINARY_DIR}/payeeidentifier_iban_bic_widgets_export.h ) kde4_add_ui_files(IBAN_BIC_WIDGETS_SCRS ibanbicitemedit.ui ) kde4_add_library( payeeidentifier_iban_bic_widgets SHARED ${IBAN_BIC_WIDGETS_SCRS} ) set_target_properties(payeeidentifier_iban_bic_widgets PROPERTIES VERSION ${GENERIC_LIB_VERSION} SOVERSION ${GENERIC_LIB_SOVERSION}) target_include_directories(payeeidentifier_iban_bic_widgets PUBLIC ${CMAKE_CURRENT_BINARY_DIR} ) target_link_libraries( payeeidentifier_iban_bic_widgets LINK_PUBLIC payeeidentifier_iban_bic ${QT_QTCORE_LIBRARY} ${KDE4_KDECORE_LIBS} ${KDE4_KDEUI_LIBS} kmm_mymoney kmm_widgets ) generate_export_header( payeeidentifier_iban_bic_widgets ) install(TARGETS payeeidentifier_iban_bic_widgets ${INSTALL_TARGETS_DEFAULT_ARGS}) ## Delegate plugin set (IBAN_BIC_UI_SCRS pluginfactory.cpp ) kde4_add_plugin( payeeidentifier_iban_bic_delegates ${IBAN_BIC_UI_SCRS} ) target_link_libraries( payeeidentifier_iban_bic_delegates payeeidentifier_iban_bic payeeidentifier_iban_bic_widgets kmm_widgets ${QT_QTCORE_LIBRARY} ${KDE4_KDEUI_LIBS} ) install(TARGETS payeeidentifier_iban_bic_delegates DESTINATION ${PLUGIN_INSTALL_DIR}) configure_file(kmymoney-ibanbic-delegate.desktop.in kmymoney-ibanbic-delegate.desktop) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/kmymoney-ibanbic-delegate.desktop DESTINATION ${SERVICES_INSTALL_DIR}) diff --git a/kmymoney/payeeidentifier/ibanandbic/widgets/bankcodevalidator.cpp b/kmymoney/payeeidentifier/ibanandbic/widgets/bankcodevalidator.cpp new file mode 100644 index 000000000..184ecb730 --- /dev/null +++ b/kmymoney/payeeidentifier/ibanandbic/widgets/bankcodevalidator.cpp @@ -0,0 +1,63 @@ +/* + * This file is part of KMyMoney, A Personal Finance Manager for KDE + * Copyright (C) 2014 Christian David + * + * 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, see . + */ + +#include "bankcodevalidator.h" + +#include + +#include "payeeidentifier/ibanandbic/ibanbic.h" + +bankCodeValidator::bankCodeValidator(QObject* parent) + : QValidator(parent) +{ +} + +QValidator::State bankCodeValidator::validate(QString &string, int&) const +{ + for (int i = 0; i < qMin(string.length(), 2); ++i) { + if (!string.at(i).isLetterOrNumber()) + return Invalid; + } + + int start = string.at(0).isLetter() ? 2 : 0; + + for (int i = start; i < qMin(string.length(), 8+start); ++i) { + if (!string.at(i).isNumber()) + return Invalid; + } + + if (string.length() > 10) + return Invalid; + else if (string.length() == 8 || string.length() == 10) { + return Acceptable; + } + return Intermediate; +} + +QPair< KMyMoneyValidationFeedback::MessageType, QString > bankCodeValidator::validateWithMessage(const QString& string) +{ + // Do not show an error message if no BIC is given. + if (string.length() != 8) + return QPair< KMyMoneyValidationFeedback::MessageType, QString >(KMyMoneyValidationFeedback::Error, i18n("A valid bank code is 8 or 10 characters long.")); + +// if (payeeIdentifiers::ibanBic::isBicAllocated(string) == payeeIdentifiers::ibanBic::bicNotAllocated) +// return QPair< KMyMoneyValidationFeedback::MessageType, QString >(KMyMoneyValidationFeedback::Error, i18n("The given bank code is not assigned to any credit institute.")); + + return QPair< KMyMoneyValidationFeedback::MessageType, QString >(KMyMoneyValidationFeedback::None, QString()); + +} diff --git a/kmymoney/payeeidentifier/ibanandbic/widgets/bankcodevalidator.h b/kmymoney/payeeidentifier/ibanandbic/widgets/bankcodevalidator.h new file mode 100644 index 000000000..b1151648b --- /dev/null +++ b/kmymoney/payeeidentifier/ibanandbic/widgets/bankcodevalidator.h @@ -0,0 +1,37 @@ +/* + * This file is part of KMyMoney, A Personal Finance Manager for KDE + * Copyright (C) 2014 Christian David + * + * 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, see . + */ + +#ifndef BANKCODEVALIDATOR_H +#define BANKCODEVALIDATOR_H + +#include +#include "payeeidentifier_iban_bic_widgets_export.h" +#include "kmymoneyvalidationfeedback.h" + +class PAYEEIDENTIFIER_IBAN_BIC_WIDGETS_EXPORT bankCodeValidator : public QValidator +{ + Q_OBJECT + +public: + explicit bankCodeValidator(QObject* parent = 0); + virtual QValidator::State validate(QString& , int&) const; + + static QPair validateWithMessage(const QString&); +}; + +#endif // BICVALIDATOR_H diff --git a/kmymoney/payeeidentifier/ibanandbic/widgets/ibanbicitemedit.h b/kmymoney/payeeidentifier/ibanandbic/widgets/ibanbicitemedit.h index b7b55a16d..bb4769b17 100644 --- a/kmymoney/payeeidentifier/ibanandbic/widgets/ibanbicitemedit.h +++ b/kmymoney/payeeidentifier/ibanandbic/widgets/ibanbicitemedit.h @@ -1,67 +1,69 @@ /* * This file is part of KMyMoney, A Personal Finance Manager for KDE * Copyright (C) 2014 Christian Dávid * * 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, see . */ #ifndef IBANBICITEMEDIT_H #define IBANBICITEMEDIT_H #include + #include +#include "payeeidentifier_iban_bic_widgets_export.h" namespace Ui { class ibanBicItemEdit; } -class ibanBicItemEdit : public QWidget +class PAYEEIDENTIFIER_IBAN_BIC_WIDGETS_EXPORT ibanBicItemEdit : public QWidget { Q_OBJECT - Q_PROPERTY(payeeIdentifier identifier READ identifier WRITE setIdentifier NOTIFY identifierChanged STORED true) - Q_PROPERTY(QString iban READ iban WRITE setIban NOTIFY ibanChanged STORED false DESIGNABLE true) - Q_PROPERTY(QString bic READ bic WRITE setBic NOTIFY bicChanged STORED false DESIGNABLE true) +// Q_PROPERTY(payeeIdentifier identifier READ identifier WRITE setIdentifier NOTIFY identifierChanged STORED true) +// Q_PROPERTY(QString iban READ iban WRITE setIban NOTIFY ibanChanged STORED false DESIGNABLE true) +// Q_PROPERTY(QString bic READ bic WRITE setBic NOTIFY bicChanged STORED false DESIGNABLE true) public: ibanBicItemEdit(QWidget* parent = 0); payeeIdentifier identifier() const; QString iban() const; QString bic() const; public Q_SLOTS: void setIdentifier(const payeeIdentifier&); void setIban(const QString&); void setBic(const QString&); Q_SIGNALS: void commitData(QWidget*); void closeEditor(QWidget* editor); void identifierChanged(payeeIdentifier); void ibanChanged(QString); void bicChanged(QString); private Q_SLOTS: void updateIdentifier(); /** @brief emits commitData(this) and closeEditor(this) */ void editFinished(); private: struct Private; Private* d; }; #endif // IBANBICITEMEDIT_H diff --git a/kmymoney/payeeidentifier/ibanandbic/widgets/kbankcodeedit.cpp b/kmymoney/payeeidentifier/ibanandbic/widgets/kbankcodeedit.cpp new file mode 100644 index 000000000..f7041c9a1 --- /dev/null +++ b/kmymoney/payeeidentifier/ibanandbic/widgets/kbankcodeedit.cpp @@ -0,0 +1,120 @@ +/* + * This file is part of KMyMoney, A Personal Finance Manager for KDE + * Copyright (C) 2014 Christian Dávid + * + * 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, see . + */ + +#include "kbankcodeedit.h" + +#include +#include +#include +#include +#include +#include +#include + +#include "../bankcodemodel.h" +#include "bankcodevalidator.h" + +class bankCodeItemDelegate : public QStyledItemDelegate +{ +public: + explicit bankCodeItemDelegate(QObject* parent = 0) : QStyledItemDelegate(parent) {} + void paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const; + virtual QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const; + +private: + inline QFont getSmallFont(const QStyleOptionViewItem& option) const; +}; + +KBankCodeEdit::KBankCodeEdit(QWidget* parent) + : KLineEdit(parent) +{ + QCompleter* completer = new QCompleter(this); + + bankCodeModel* model = new bankCodeModel(this); + completer->setModel(model); + m_popupDelegate = new bankCodeItemDelegate(this); + completer->popup()->setItemDelegate(m_popupDelegate); + + setCompleter(completer); + + bankCodeValidator *const validator = new bankCodeValidator(this); + setValidator(validator); +} + +KBankCodeEdit::~KBankCodeEdit() +{ + delete m_popupDelegate; +} + +QFont bankCodeItemDelegate::getSmallFont(const QStyleOptionViewItem& option) const +{ + QFont smallFont = option.font; + smallFont.setPointSize(0.9*smallFont.pointSize()); + return smallFont; +} + +QSize bankCodeItemDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const +{ + QStyleOptionViewItemV4 opt = option; + initStyleOption(&opt, index); + + QFontMetrics metrics(option.font); + QFontMetrics smallMetrics(getSmallFont(option)); + const QStyle *style = opt.widget ? opt.widget->style() : QApplication::style(); + const int margin = style->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1; + + // A bic has maximal 11 characters. So we guess, we want to display 11 characters. The name of the institution has to adapt to what is given + return QSize(metrics.width(QLatin1Char('X')) + 2*margin, metrics.lineSpacing() + smallMetrics.lineSpacing() + smallMetrics.leading() + 2*margin); +} + +/** + * @todo enable eliding (use QFontMetrics::elidedText() ) + */ +void bankCodeItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const +{ + QStyleOptionViewItemV4 opt = option; + initStyleOption(&opt, index); + + // Background + QStyle *style = opt.widget ? opt.widget->style() : QApplication::style(); + style->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter, opt.widget); + + const int margin = style->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1; + const QRect textArea = QRect(opt.rect.x() + margin, opt.rect.y() + margin, opt.rect.width() - 2 * margin, opt.rect.height() - 2 * margin); + + // Paint name + painter->save(); + QFont smallFont = getSmallFont(opt); + QFontMetrics metrics(opt.font); + QFontMetrics smallMetrics(smallFont); + QRect nameRect = style->alignedRect(opt.direction, Qt::AlignBottom, QSize(textArea.width(), smallMetrics.lineSpacing()), textArea); + painter->setFont(smallFont); + style->drawItemText(painter, nameRect, Qt::AlignBottom, QApplication::palette(), true, index.model()->data(index, bankCodeModel::InstitutionNameRole).toString(), option.state & QStyle::State_Selected ? QPalette::HighlightedText : QPalette::Mid); + painter->restore(); + + // Paint BIC + painter->save(); + QFont normal = painter->font(); + normal.setBold(true); + painter->setFont(normal); + QRect bankCodeRect = style->alignedRect(opt.direction, Qt::AlignTop, QSize(textArea.width(), metrics.lineSpacing()), textArea); + const QString bankCode = index.model()->data(index, Qt::DisplayRole).toString(); + style->drawItemText(painter, bankCodeRect, Qt::AlignTop, QApplication::palette(), true, bankCode, option.state & QStyle::State_Selected ? QPalette::HighlightedText : QPalette::Text); + + painter->restore(); +} diff --git a/kmymoney/payeeidentifier/ibanandbic/widgets/kbankcodeedit.h b/kmymoney/payeeidentifier/ibanandbic/widgets/kbankcodeedit.h new file mode 100644 index 000000000..f802f7cef --- /dev/null +++ b/kmymoney/payeeidentifier/ibanandbic/widgets/kbankcodeedit.h @@ -0,0 +1,42 @@ +/* + * This file is part of KMyMoney, A Personal Finance Manager for KDE + * Copyright (C) 2014 Christian Dávid + * + * 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, see . + */ + +#ifndef KBANKCODEEDIT_H +#define KBANKCODEEDIT_H + +#include +#include + +#include "payeeidentifier_iban_bic_widgets_export.h" +#include "kmymoneyvalidationfeedback.h" + +class QAbstractItemDelegate; + +class PAYEEIDENTIFIER_IBAN_BIC_WIDGETS_EXPORT KBankCodeEdit : public KLineEdit +{ + Q_OBJECT + +public: + KBankCodeEdit(QWidget* parent = 0); + virtual ~KBankCodeEdit(); + +private: + QAbstractItemDelegate* m_popupDelegate; +}; + +#endif // KBICEDIT_H