1 /***************************************************************************
2  * SPDX-FileCopyrightText: 2021 S. MANKOWSKI stephane@mankowski.fr
3  * SPDX-FileCopyrightText: 2021 G. DE BURE support@mankowski.fr
4  * SPDX-License-Identifier: GPL-3.0-or-later
5  ***************************************************************************/
6 /** @file
7  * This file is Skrooge plugin for operation management.
8  *
9  * @author Stephane MANKOWSKI / Guillaume DE BURE
10  */
11 #include "skgsplittabledelegate.h"
12 
13 #include <kformat.h>
14 #include <ksqueezedtextlabel.h>
15 
16 #include <qheaderview.h>
17 #include <qpainter.h>
18 
19 #include <utility>
20 
21 #include "skgbasegui_settings.h"
22 #include "skgcalculatoredit.h"
23 #include "skgcombobox.h"
24 #include "skgdateedit.h"
25 #include "skgmainpanel.h"
26 #include "skgtablewidget.h"
27 
SKGSplitTableDelegate(QObject * iParent,SKGDocument * iDoc,QStringList iListAttribut)28 SKGSplitTableDelegate::SKGSplitTableDelegate(QObject* iParent, SKGDocument* iDoc, QStringList  iListAttribut) : QItemDelegate(iParent),
29     m_document(iDoc), m_listAttributes(std::move(iListAttribut)), m_table(qobject_cast<SKGTableWidget * >(iParent))
30 {
31 }
32 
~SKGSplitTableDelegate()33 SKGSplitTableDelegate::~SKGSplitTableDelegate()
34 {
35     m_document = nullptr;
36 }
37 
createEditor(QWidget * iParent,const QStyleOptionViewItem & option,const QModelIndex & index) const38 QWidget* SKGSplitTableDelegate::createEditor(QWidget* iParent,
39         const QStyleOptionViewItem& option,
40         const QModelIndex& index) const
41 {
42     QSizePolicy newSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
43     newSizePolicy.setHorizontalStretch(0);
44     newSizePolicy.setVerticalStretch(0);
45 
46     if (index.column() == m_listAttributes.indexOf(QStringLiteral("t_category"))) {
47         auto editor = new SKGComboBox(iParent);
48         editor->setEditable(true);
49         if (m_document != nullptr) {
50             SKGMainPanel::fillWithDistinctValue(QList<QWidget*>() << editor, m_document, QStringLiteral("category"), QStringLiteral("t_fullname"), QStringLiteral("t_close='N'"));
51         }
52         editor->setSizePolicy(newSizePolicy);
53         editor->setSizeAdjustPolicy(QComboBox::AdjustToContents);
54         m_table->setColumnWidth(index.column(), editor->sizeHint().width());
55 
56         return editor;
57     }
58     if (index.column() == m_listAttributes.indexOf(QStringLiteral("t_comment"))) {
59         auto editor = new SKGComboBox(iParent);
60         editor->setEditable(true);
61         if (m_document != nullptr) {
62             SKGMainPanel::fillWithDistinctValue(QList<QWidget*>() << editor, m_document, QStringLiteral("v_operation_all_comment"), QStringLiteral("t_comment"), QLatin1String(""));
63         }
64         editor->setSizePolicy(newSizePolicy);
65         editor->setSizeAdjustPolicy(QComboBox::AdjustToContents);
66         m_table->setColumnWidth(index.column(), editor->sizeHint().width());
67 
68         return editor;
69     }
70     if (index.column() == m_listAttributes.indexOf(QStringLiteral("f_value"))) {
71         auto editor = new SKGCalculatorEdit(iParent);
72         editor->setMode(SKGCalculatorEdit::EXPRESSION);
73         QMapIterator<QString, double> i(m_parameters);
74         while (i.hasNext()) {
75             i.next();
76             editor->addParameterValue(i.key(), i.value());
77         }
78         editor->setSizePolicy(newSizePolicy);
79         m_table->setColumnWidth(index.column(), editor->sizeHint().width());
80 
81         return editor;
82     }
83     if (index.column() == m_listAttributes.indexOf(QStringLiteral("t_refund"))) {
84         auto editor = new SKGComboBox(iParent);
85         editor->setEditable(true);
86         if (m_document != nullptr) {
87             SKGMainPanel::fillWithDistinctValue(QList<QWidget*>() << editor, m_document, QStringLiteral("refund"), QStringLiteral("t_name"), QStringLiteral("t_close='N'"));
88         }
89         editor->setSizePolicy(newSizePolicy);
90         editor->setSizeAdjustPolicy(QComboBox::AdjustToContents);
91         m_table->setColumnWidth(index.column(), editor->sizeHint().width());
92 
93         return editor;
94     }  if (index.column() == m_listAttributes.indexOf(QStringLiteral("d_date"))) {
95         auto editor = new SKGDateEdit(iParent);
96         editor->setSizePolicy(newSizePolicy);
97         editor->setSizeAdjustPolicy(QComboBox::AdjustToContents);
98         m_table->setColumnWidth(index.column(), editor->sizeHint().width());
99 
100         return editor;
101     }
102 
103     return QItemDelegate::createEditor(iParent, option, index);
104 }
105 
setEditorData(QWidget * editor,const QModelIndex & index) const106 void SKGSplitTableDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
107 {
108     if (index.column() == m_listAttributes.indexOf(QStringLiteral("f_value"))) {
109         auto* calculator = qobject_cast<SKGCalculatorEdit*>(editor);
110         if (calculator != nullptr) {
111             calculator->setText(index.model()->data(index, Qt::ToolTipRole).toString());
112         }
113     } else if (index.column() == m_listAttributes.indexOf(QStringLiteral("d_date"))) {
114         auto* date = qobject_cast<SKGDateEdit*>(editor);
115         if (date != nullptr) {
116             date->setDate(SKGServices::stringToTime(index.model()->data(index, Qt::ToolTipRole).toString()).date());
117         }
118     } else {
119         QItemDelegate::setEditorData(editor, index);
120     }
121 }
122 
setModelData(QWidget * editor,QAbstractItemModel * model,const QModelIndex & index) const123 void SKGSplitTableDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
124 {
125     if (index.column() == m_listAttributes.indexOf(QStringLiteral("f_value"))) {
126         auto* calculator = qobject_cast<SKGCalculatorEdit*>(editor);
127         if ((calculator != nullptr) && (model != nullptr)) {
128             QString f = calculator->formula();
129             QString t = SKGServices::doubleToString(calculator->value());
130             if (f.isEmpty()) {
131                 f = t;
132             }
133             bool previous = model->blockSignals(true);
134             model->setData(index, f, Qt::ToolTipRole);
135             model->setData(index, calculator->value(), 101);
136             model->blockSignals(previous);
137             model->setData(index, t, Qt::DisplayRole);
138         }
139     } else if (index.column() == m_listAttributes.indexOf(QStringLiteral("d_date"))) {
140         auto* date = qobject_cast<SKGDateEdit*>(editor);
141         if ((date != nullptr) && (model != nullptr)) {
142             QString dateDisplay = SKGMainPanel::dateToString(date->date());
143             QString dateInternal = SKGServices::dateToSqlString(date->date());
144 
145             model->setData(index, dateInternal, Qt::ToolTipRole);
146             model->setData(index, dateDisplay, Qt::DisplayRole);
147         }
148     } else {
149         QItemDelegate::setModelData(editor, model, index);
150     }
151     m_table->resizeColumnsToContents();
152     m_table->horizontalHeader()->setStretchLastSection(true);
153 }
154 
addParameterValue(const QString & iParameter,double iValue)155 void SKGSplitTableDelegate::addParameterValue(const QString& iParameter, double iValue)
156 {
157     m_parameters.insert(iParameter, iValue);
158 }
159 
160