File indexing completed on 2024-06-16 04:46:41

0001 /*
0002     SPDX-FileCopyrightText: Bernd Gonsior <bernd.gonsior@googlemail.com>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "tocitem.h"
0007 
0008 // ----------------------------------------------------------------------------
0009 // QT Includes
0010 
0011 #include <QTreeWidget>
0012 #include <QDebug>
0013 
0014 // ----------------------------------------------------------------------------
0015 // KDE Includes
0016 
0017 // ----------------------------------------------------------------------------
0018 // Project Includes
0019 
0020 TocItem::TocItem(QTreeWidget* parent, QStringList columns) :
0021     QTreeWidgetItem(parent, columns),
0022     type(GROUP)
0023 {}
0024 
0025 TocItem::TocItem(QTreeWidgetItem* parent, QStringList columns) :
0026     QTreeWidgetItem(parent, columns),
0027     type(GROUP)
0028 {}
0029 
0030 bool TocItem::isReport()
0031 {
0032     return (type == REPORT ? true : false);
0033 }
0034 
0035 /**
0036  * @link tocitem.h @endlink
0037  */
0038 bool TocItem::operator<(const QTreeWidgetItem &other)const
0039 {
0040 
0041     // this operator is used by QTreeWidgetItem to sort items
0042 
0043     QString cm = "TocItem::<:";
0044 
0045     // the table of contents for reports (TOC) has 2 columns:
0046     // 1st: name of report or group
0047     // 2nd: comment
0048 
0049     // if user clicks column 'name',
0050     // TOC is sorted alphabetically
0051     // by the sort key provided
0052     // in the items data of column 0, role Qt::UserRole
0053 
0054     // if user clicks column 'comment',
0055     // TOC is sorted alphabetically
0056     // by the content of column 'comment'
0057 
0058     // get the column clicked by user
0059     int column = treeWidget()->sortColumn();
0060 
0061     // preset compare result
0062     bool compareResult = false;
0063 
0064     if (column != 0) {
0065         // user clicked column 'comment',
0066         // so sort alphabetically
0067         // by the content this column
0068         compareResult = this->text(column) < other.text(column);
0069         return compareResult;
0070     }
0071 
0072     // if there is any error condition in the following code,
0073     // we should get an information via stderr
0074     // or an alternative message handler,
0075     // but the program should not be interrupted,
0076     // the only result will be that the sort is wrong - bad luck
0077 
0078     // user clicked column 'name',
0079     // so retrieve the sort key
0080 
0081     // get the data of this item
0082     QVariant thisItemsData = this->data(0, Qt::UserRole);
0083     if (thisItemsData.isNull()) {
0084         qWarning() << cm << "thisItemsData is NULL";
0085         return false;
0086     }
0087 
0088     // get the data of the other item
0089     QVariant otherItemsData = other.data(0, Qt::UserRole);
0090     if (otherItemsData.isNull()) {
0091         qWarning() << cm << "otherItemsData is NULL";
0092         return false;
0093     }
0094 
0095     // get the QStringList of this item
0096     QStringList thisItemsDataList = thisItemsData.toStringList();
0097 
0098     // get the QStringList of the other item
0099     QStringList otherItemsDataList = otherItemsData.toStringList();
0100 
0101     // get the type (report or group) of this item
0102     // this information is only read as a precaution,
0103     // if it is sure, that always the same types are compared,
0104     // this can be removed
0105     QString thisItemsType = thisItemsDataList.at(0);
0106     if (thisItemsType.isNull()) {
0107         qWarning() << cm << "thisItemsType is NULL";
0108         return false;
0109     }
0110 
0111     // get the type (report or group) of the other item
0112     // this information is only read as a precaution,
0113     // if it is sure, that always the same types are compared,
0114     // this can be removed
0115     QString otherItemsType = otherItemsDataList.at(0);
0116     if (otherItemsType.isNull()) {
0117         qWarning() << cm << "otherItemsType is NULL";
0118         return false;
0119     }
0120 
0121     // get the sort key of this item
0122     // (to enable a pseudo-numeric sort,
0123     // in case of reportgroups this key
0124     // is the string-representation of a number
0125     // with leading zeros, e.g. '001')
0126     QString thisItemsSortKey = thisItemsDataList.at(1);
0127     if (thisItemsSortKey.isNull()) {
0128         qWarning() << cm << "thisItemsSortKey is NULL";
0129         return false;
0130     }
0131 
0132     // get the sort key of the other item
0133     QString otherItemsSortKey = otherItemsDataList.at(1);
0134     if (otherItemsSortKey.isNull()) {
0135         qWarning() << cm << "otherItemsSortKey is NULL";
0136         return false;
0137     }
0138 
0139     // this is a safety check only,
0140     // if it is sure, that always the same types are compared,
0141     // this can be removed
0142     if (thisItemsType != otherItemsType) {
0143         qWarning() << cm << "comparing different types: thisItemsType:"
0144                    << thisItemsType << ", otherItemsType:" << otherItemsType;
0145         return false;
0146     }
0147 
0148     // compare both sort keys
0149     compareResult = thisItemsSortKey < otherItemsSortKey;
0150 
0151     return compareResult;
0152 }