File indexing completed on 2024-06-16 05:24:50

0001 /*
0002     This file is part of the Okteta Kasten module, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2023 Friedrich W. H. Kossebau <kossebau@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007 */
0008 
0009 #include "statisticdisplaymodel.hpp"
0010 
0011 // tool
0012 #include "oktetakastengui_export.hpp"
0013 #include "statistictablemodel.hpp"
0014 // Qt
0015 #include <QWidget>
0016 #include <QLocale>
0017 #include <QMimeData>
0018 
0019 namespace Kasten {
0020 
0021 StatisticDisplayModel::StatisticDisplayModel(QWidget* uiWidget, QObject* parent)
0022     : QIdentityProxyModel(parent)
0023     , m_uiWidget(uiWidget)
0024 {
0025 }
0026 
0027 StatisticDisplayModel::~StatisticDisplayModel() = default;
0028 
0029 
0030 QVariant StatisticDisplayModel::data(const QModelIndex& index, int role) const
0031 {
0032     if (role != Qt::DisplayRole) {
0033         return QIdentityProxyModel::data(index, role);
0034     }
0035 
0036     QVariant result;
0037 
0038     const int column = index.column();
0039     const QVariant data = sourceModel()->data(index, Qt::DisplayRole);
0040 
0041     switch (column)
0042     {
0043     case StatisticTableModel::PercentId: {
0044         result = data.isValid() ?
0045                 QVariant(m_uiWidget->locale().toString(data.toDouble(), 'f', 6)) :
0046                 QVariant(QStringLiteral("-"));
0047         break;
0048     }
0049     default:
0050         result = data;
0051     }
0052 
0053     return result;
0054 }
0055 
0056 Qt::ItemFlags StatisticDisplayModel::flags(const QModelIndex& index) const
0057 {
0058     return QIdentityProxyModel::flags(index) | Qt::ItemIsDragEnabled;
0059 }
0060 
0061 static QString tableAsText(const QModelIndexList& indexes)
0062 {
0063     QString lines;
0064 
0065     // assumes indexes are in row-wise order as seen on screen
0066     // add tab between items in row, but for last linefeed
0067     int lastRow = -1;
0068     for (const QModelIndex& index : indexes) {
0069         const int row = index.row();
0070         if (row != lastRow) {
0071             lastRow = row;
0072             if (!lines.isEmpty()) {
0073                 lines.append(QLatin1Char('\n'));
0074             }
0075         } else {
0076             lines.append(QLatin1Char('\t'));
0077         }
0078 
0079         lines.append(index.data(Qt::DisplayRole).toString());
0080     }
0081 
0082     lines.append(QLatin1Char('\n'));
0083 
0084     return lines;
0085 }
0086 
0087 static QString tableAsHtml(const QModelIndexList& indexes)
0088 {
0089     QString html;
0090 
0091     if (!indexes.isEmpty()) {
0092         // assumes indexes are in row-wise order as seen on screen
0093         // wrap each items in row with <td>,
0094         // but for last add <tr> end & start, matched by global start / end wrapper
0095         int lastRow = indexes.first().row();
0096         for (const QModelIndex &index : indexes) {
0097             const int row = index.row();
0098             if (row != lastRow) {
0099                 lastRow = row;
0100                 if (!html.isEmpty()) {
0101                     html.append(QLatin1String("</tr><tr>"));
0102                 }
0103             }
0104 
0105             html.append(QLatin1String("<td>") + index.data(Qt::DisplayRole).toString() + QLatin1String("</td>"));
0106         }
0107     }
0108 
0109     html = QLatin1String("<html><head><meta name=\"generator\" content=\"Okteta Statistics Tool " OKTETAKASTENGUI_VERSION_STRING "\"></head><table><tr>") +  html + QLatin1String("</tr></table></html>");
0110 
0111     return html;
0112 }
0113 
0114 QMimeData* StatisticDisplayModel::mimeData(const QModelIndexList& indexes) const
0115 {
0116     if (indexes.isEmpty()) {
0117         return nullptr;
0118     }
0119 
0120     auto* mimeData = new QMimeData;
0121 
0122     const QString text = tableAsText(indexes);
0123     mimeData->setText(text);
0124     const QString html = tableAsHtml(indexes);
0125     mimeData->setHtml(html);
0126 
0127     return mimeData;
0128 }
0129 
0130 QStringList StatisticDisplayModel::mimeTypes() const
0131 {
0132     return {
0133         QStringLiteral("text/plain"),
0134         QStringLiteral("text/html"),
0135     };
0136 }
0137 
0138 }
0139 
0140 #include "moc_statisticdisplaymodel.cpp"