File indexing completed on 2024-05-26 05:10:46
0001 /*************************************************************************** 0002 * SPDX-FileCopyrightText: 2022 S. MANKOWSKI stephane@mankowski.fr 0003 * SPDX-FileCopyrightText: 2022 G. DE BURE support@mankowski.fr 0004 * SPDX-License-Identifier: GPL-3.0-or-later 0005 ***************************************************************************/ 0006 /** @file 0007 * This file is Skrooge plugin for unit management. 0008 * 0009 * @author Stephane MANKOWSKI / Guillaume DE BURE 0010 */ 0011 #include "skgunitboardwidget.h" 0012 0013 #include <qdom.h> 0014 0015 #include <kcolorscheme.h> 0016 0017 #include "skgdocumentbank.h" 0018 #include "skgmainpanel.h" 0019 #include "skgtraces.h" 0020 #include "skgunitobject.h" 0021 0022 SKGUnitBoardWidget::SKGUnitBoardWidget(QWidget* iParent, SKGDocument* iDocument) 0023 : SKGBoardWidget(iParent, iDocument, i18nc("Title of a dashboard widget", "Quotes")) 0024 { 0025 SKGTRACEINFUNC(10) 0026 0027 // Create menu 0028 setContextMenuPolicy(Qt::ActionsContextMenu); 0029 0030 m_menuFavorite = new QAction(SKGServices::fromTheme(QStringLiteral("bookmarks")), i18nc("Display only favorite accounts", "Highlighted only"), this); 0031 m_menuFavorite->setCheckable(true); 0032 m_menuFavorite->setChecked(false); 0033 connect(m_menuFavorite, &QAction::triggered, this, [ = ]() { 0034 this->dataModified(); 0035 }); 0036 addAction(m_menuFavorite); 0037 0038 { 0039 auto sep = new QAction(this); 0040 sep->setSeparator(true); 0041 addAction(sep); 0042 } 0043 0044 m_menuCurrencies = new QAction(i18nc("Noun, a country's currency", "Currencies"), this); 0045 m_menuCurrencies->setCheckable(true); 0046 m_menuCurrencies->setChecked(true); 0047 connect(m_menuCurrencies, &QAction::triggered, this, [ = ]() { 0048 this->dataModified(); 0049 }); 0050 addAction(m_menuCurrencies); 0051 0052 m_menuIndexes = new QAction(i18nc("Financial indexes, used as an indicator of financial places' health. Examples : Dow Jones, CAC40, Nikkei…", "Indexes"), this); 0053 m_menuIndexes->setCheckable(true); 0054 m_menuIndexes->setChecked(true); 0055 connect(m_menuIndexes, &QAction::triggered, this, [ = ]() { 0056 this->dataModified(); 0057 }); 0058 addAction(m_menuIndexes); 0059 0060 m_menuShares = new QAction(i18nc("Shares, as in financial shares: parts of a company that you can buy or sell on financial markets", "Shares"), this); 0061 m_menuShares->setCheckable(true); 0062 m_menuShares->setChecked(true); 0063 connect(m_menuShares, &QAction::triggered, this, [ = ]() { 0064 this->dataModified(); 0065 }); 0066 addAction(m_menuShares); 0067 0068 m_menuObjects = new QAction(i18nc("Noun, a physical object like a house or a car", "Objects"), this); 0069 m_menuObjects->setCheckable(true); 0070 m_menuObjects->setChecked(true); 0071 connect(m_menuObjects, &QAction::triggered, this, [ = ]() { 0072 this->dataModified(); 0073 }); 0074 addAction(m_menuObjects); 0075 0076 m_menuSharesOwnedOnly = new QAction(i18nc("Only show the list of Shares (financial shares) that you own", "Shares owned only"), this); 0077 m_menuSharesOwnedOnly->setCheckable(true); 0078 m_menuSharesOwnedOnly->setChecked(false); 0079 connect(m_menuSharesOwnedOnly, &QAction::triggered, this, [ = ]() { 0080 this->dataModified(); 0081 }); 0082 addAction(m_menuSharesOwnedOnly); 0083 0084 m_label = new QLabel(); 0085 setMainWidget(m_label); 0086 0087 // Refresh 0088 connect(getDocument(), &SKGDocument::tableModified, this, &SKGUnitBoardWidget::dataModified, Qt::QueuedConnection); 0089 connect(m_label, &QLabel::linkActivated, this, [ = ](const QString & val) { 0090 SKGMainPanel::getMainPanel()->openPage(val); 0091 }); 0092 } 0093 0094 SKGUnitBoardWidget::~SKGUnitBoardWidget() 0095 { 0096 SKGTRACEINFUNC(10) 0097 m_menuIndexes = nullptr; 0098 m_menuShares = nullptr; 0099 m_menuSharesOwnedOnly = nullptr; 0100 m_menuObjects = nullptr; 0101 m_menuCurrencies = nullptr; 0102 m_menuFavorite = nullptr; 0103 } 0104 0105 QString SKGUnitBoardWidget::getState() 0106 { 0107 QDomDocument doc(QStringLiteral("SKGML")); 0108 doc.setContent(SKGBoardWidget::getState()); 0109 QDomElement root = doc.documentElement(); 0110 0111 root.setAttribute(QStringLiteral("m_menuCurrencies"), (m_menuCurrencies != nullptr) && m_menuCurrencies->isChecked() ? QStringLiteral("Y") : QStringLiteral("N")); 0112 root.setAttribute(QStringLiteral("m_menuObjects"), (m_menuObjects != nullptr) && m_menuObjects->isChecked() ? QStringLiteral("Y") : QStringLiteral("N")); 0113 root.setAttribute(QStringLiteral("menuIndexes"), (m_menuIndexes != nullptr) && m_menuIndexes->isChecked() ? QStringLiteral("Y") : QStringLiteral("N")); 0114 root.setAttribute(QStringLiteral("menuShares"), (m_menuShares != nullptr) && m_menuShares->isChecked() ? QStringLiteral("Y") : QStringLiteral("N")); 0115 root.setAttribute(QStringLiteral("menuSharesOwnedOnly"), (m_menuSharesOwnedOnly != nullptr) && m_menuSharesOwnedOnly->isChecked() ? QStringLiteral("Y") : QStringLiteral("N")); 0116 root.setAttribute(QStringLiteral("menuFavorite"), (m_menuFavorite != nullptr) && m_menuFavorite->isChecked() ? QStringLiteral("Y") : QStringLiteral("N")); 0117 return doc.toString(); 0118 } 0119 0120 void SKGUnitBoardWidget::setState(const QString& iState) 0121 { 0122 SKGBoardWidget::setState(iState); 0123 0124 QDomDocument doc(QStringLiteral("SKGML")); 0125 doc.setContent(iState); 0126 QDomElement root = doc.documentElement(); 0127 0128 if (m_menuCurrencies != nullptr) { 0129 m_menuCurrencies->setChecked(root.attribute(QStringLiteral("m_menuCurrencies")) == QStringLiteral("Y")); 0130 } 0131 if (m_menuObjects != nullptr) { 0132 m_menuObjects->setChecked(root.attribute(QStringLiteral("m_menuObjects")) == QStringLiteral("Y")); 0133 } 0134 if (m_menuIndexes != nullptr) { 0135 m_menuIndexes->setChecked(root.attribute(QStringLiteral("menuIndexes")) != QStringLiteral("N")); 0136 } 0137 if (m_menuShares != nullptr) { 0138 m_menuShares->setChecked(root.attribute(QStringLiteral("menuShares")) != QStringLiteral("N")); 0139 } 0140 if (m_menuSharesOwnedOnly != nullptr) { 0141 m_menuSharesOwnedOnly->setChecked(root.attribute(QStringLiteral("menuSharesOwnedOnly")) != QStringLiteral("N")); 0142 } 0143 if (m_menuFavorite != nullptr) { 0144 m_menuFavorite->setChecked(root.attribute(QStringLiteral("menuFavorite")) == QStringLiteral("Y")); 0145 } 0146 0147 dataModified(QLatin1String(""), 0); 0148 } 0149 0150 void SKGUnitBoardWidget::dataModified(const QString& iTableName, int iIdTransaction) 0151 { 0152 SKGTRACEINFUNC(10) 0153 Q_UNUSED(iIdTransaction) 0154 0155 if (iTableName == QStringLiteral("v_unit_display") || iTableName.isEmpty()) { 0156 auto* doc = qobject_cast<SKGDocumentBank*>(getDocument()); 0157 if (doc != nullptr) { 0158 SKGServices::SKGUnitInfo primary = doc->getPrimaryUnit(); 0159 0160 // Build where clause 0161 QString wc; 0162 if ((m_menuSharesOwnedOnly != nullptr) && (m_menuShares != nullptr) && (m_menuIndexes != nullptr)) { 0163 m_menuSharesOwnedOnly->setEnabled(m_menuShares->isChecked()); 0164 m_menuShares->setEnabled(!m_menuSharesOwnedOnly->isChecked()); 0165 if (!m_menuShares->isChecked()) { 0166 m_menuSharesOwnedOnly->setChecked(false); 0167 } 0168 0169 if (m_menuIndexes->isChecked()) { 0170 wc = QStringLiteral("t_type='I'"); 0171 } 0172 if (m_menuShares->isChecked()) { 0173 if (!wc.isEmpty()) { 0174 wc += QStringLiteral(" OR "); 0175 } 0176 if (m_menuSharesOwnedOnly->isChecked()) { 0177 wc += QStringLiteral("(t_type='S' AND f_QUANTITYOWNED>0)"); 0178 } else { 0179 wc += QStringLiteral("t_type='S'"); 0180 } 0181 } 0182 if (m_menuCurrencies->isChecked()) { 0183 if (!wc.isEmpty()) { 0184 wc += QStringLiteral(" OR "); 0185 } 0186 wc += QStringLiteral("t_type IN ('1','2','C')"); 0187 } 0188 if (m_menuObjects->isChecked()) { 0189 if (!wc.isEmpty()) { 0190 wc += QStringLiteral(" OR "); 0191 } 0192 wc += QStringLiteral("t_type='O'"); 0193 } 0194 } 0195 0196 if (wc.isEmpty()) { 0197 wc = QStringLiteral("1=0"); 0198 } else if ((m_menuFavorite != nullptr) && m_menuFavorite->isChecked()) { 0199 wc = "t_bookmarked='Y' AND (" % wc % ')'; 0200 } 0201 0202 SKGObjectBase::SKGListSKGObjectBase objs; 0203 SKGError err = getDocument()->getObjects(QStringLiteral("v_unit_display"), wc % " ORDER BY t_type DESC, t_name ASC", objs); 0204 IFOK(err) { 0205 KColorScheme scheme(QPalette::Normal, KColorScheme::Window); 0206 auto color = scheme.foreground(KColorScheme::NormalText).color().name().right(6); 0207 QString html = QStringLiteral("<html><head><style>a {color: #") + color + ";}</style></head><body>"; 0208 int nb = objs.count(); 0209 if (nb != 0) { 0210 html += QStringLiteral("<table>"); 0211 QString lastTitle; 0212 for (int i = 0; i < nb; ++i) { 0213 SKGUnitObject obj(objs.at(i)); 0214 QString type = obj.getAttribute(QStringLiteral("t_TYPENLS")); 0215 if (lastTitle != type) { 0216 lastTitle = type; 0217 html += "<tr><td><b>" % SKGServices::stringToHtml(lastTitle) % "</b></td></tr>"; 0218 } 0219 html += QString("<tr><td><a href=\"skg://Skrooge_operation_plugin/?operationWhereClause=rc_unit_id=" % SKGServices::intToString(obj.getID()) % 0220 "&title=" % SKGServices::encodeForUrl(i18nc("A list of transactions made on the specified unit", "Transactions with unit equal to '%1'", obj.getName())) % 0221 "&title_icon=taxes-finances¤tPage=-1" % "\">") % SKGServices::stringToHtml(obj.getDisplayName()) % "</a></td><td align=\"right\">"; 0222 if (obj.getType() == SKGUnitObject::INDEX) { 0223 primary.Symbol = ' '; 0224 } 0225 html += doc->formatMoney(SKGServices::stringToDouble(obj.getAttribute(QStringLiteral("f_CURRENTAMOUNT"))), primary); 0226 html += QStringLiteral("</td><td>("); 0227 double amountOneYearBefore = obj.getAmount(QDate::currentDate().addYears(-1)); 0228 double val = 100.0 * (obj.getAmount() - amountOneYearBefore) / amountOneYearBefore; 0229 html += doc->formatPercentage(val); 0230 html += QStringLiteral(")</td></tr>"); 0231 } 0232 html += QStringLiteral("</table>"); 0233 } else { 0234 html += i18nc("Message about not having any financial Share or financial index in the document", R"(No share or index defined<br>on the <a href="%1">"Units"</a> page.)", "skg://Skrooge_unit_plugin"); 0235 } 0236 html += QStringLiteral("</body></html>"); 0237 m_label->setText(html); 0238 } 0239 0240 // No widget if no account 0241 bool exist = false; 0242 getDocument()->existObjects(QStringLiteral("account"), QLatin1String(""), exist); 0243 if (parentWidget() != nullptr) { 0244 setVisible(exist); 0245 } 0246 } 0247 } 0248 } 0249 0250