| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. | 
| 4 | ** Contact: https://www.qt.io/licensing/ | 
| 5 | ** | 
| 6 | ** This file is part of the demonstration applications of the Qt Toolkit. | 
| 7 | ** | 
| 8 | ** $QT_BEGIN_LICENSE:BSD$ | 
| 9 | ** Commercial License Usage | 
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | 
| 11 | ** accordance with the commercial license agreement provided with the | 
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | 
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further | 
| 15 | ** information use the contact form at https://www.qt.io/contact-us. | 
| 16 | ** | 
| 17 | ** BSD License Usage | 
| 18 | ** Alternatively, you may use this file under the terms of the BSD license | 
| 19 | ** as follows: | 
| 20 | ** | 
| 21 | ** "Redistribution and use in source and binary forms, with or without | 
| 22 | ** modification, are permitted provided that the following conditions are | 
| 23 | ** met: | 
| 24 | **   * Redistributions of source code must retain the above copyright | 
| 25 | **     notice, this list of conditions and the following disclaimer. | 
| 26 | **   * Redistributions in binary form must reproduce the above copyright | 
| 27 | **     notice, this list of conditions and the following disclaimer in | 
| 28 | **     the documentation and/or other materials provided with the | 
| 29 | **     distribution. | 
| 30 | **   * Neither the name of The Qt Company Ltd nor the names of its | 
| 31 | **     contributors may be used to endorse or promote products derived | 
| 32 | **     from this software without specific prior written permission. | 
| 33 | ** | 
| 34 | ** | 
| 35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 
| 36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 
| 37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 
| 38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 
| 39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 
| 40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 
| 41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 
| 42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 
| 43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 
| 44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 
| 45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." | 
| 46 | ** | 
| 47 | ** $QT_END_LICENSE$ | 
| 48 | ** | 
| 49 | ****************************************************************************/ | 
| 50 |  | 
| 51 | #include "spreadsheetitem.h" | 
| 52 |  | 
| 53 | QTableWidgetItem *SpreadSheetItem::clone() const | 
| 54 | { | 
| 55 |     SpreadSheetItem *item = new SpreadSheetItem; | 
| 56 |     *item = *this; | 
| 57 |     return item; | 
| 58 | } | 
| 59 |  | 
| 60 | QVariant SpreadSheetItem::data(int role) const | 
| 61 | { | 
| 62 |     if (role == Qt::EditRole || role == Qt::StatusTipRole) | 
| 63 |         return formula(); | 
| 64 |  | 
| 65 |     if (role == Qt::DisplayRole) | 
| 66 |         return display(); | 
| 67 |  | 
| 68 |     const QString t = display().toString(); | 
| 69 |  | 
| 70 |     if (role == Qt::ForegroundRole) { | 
| 71 |         bool isNumber = false; | 
| 72 |         const int number = t.toInt(ok: &isNumber); | 
| 73 |         QColor color = Qt::black; | 
| 74 |         if (isNumber) | 
| 75 |             color = (number < 0) ? Qt::red : Qt::blue; | 
| 76 |         return QVariant::fromValue(value: color); | 
| 77 |     } | 
| 78 |  | 
| 79 |     if (role == Qt::TextAlignmentRole) | 
| 80 |         if (!t.isEmpty() && (t.at(i: 0).isNumber() || t.at(i: 0) == '-')) | 
| 81 |             return int(Qt::AlignRight | Qt::AlignVCenter); | 
| 82 |  | 
| 83 |      return QTableWidgetItem::data(role); | 
| 84 |  } | 
| 85 |  | 
| 86 | void SpreadSheetItem::setData(int role, const QVariant &value) | 
| 87 | { | 
| 88 |     QTableWidgetItem::setData(role, value); | 
| 89 |     if (tableWidget()) | 
| 90 |         tableWidget()->viewport()->update(); | 
| 91 | } | 
| 92 |  | 
| 93 | QVariant SpreadSheetItem::display() const | 
| 94 | { | 
| 95 |     // avoid circular dependencies | 
| 96 |     if (isResolving) | 
| 97 |         return QVariant(); | 
| 98 |  | 
| 99 |     isResolving = true; | 
| 100 |     QVariant result = computeFormula(formula: formula(), widget: tableWidget(), self: this); | 
| 101 |     isResolving = false; | 
| 102 |     return result; | 
| 103 | } | 
| 104 |  | 
| 105 | QVariant SpreadSheetItem::computeFormula(const QString &formula, | 
| 106 |                                          const QTableWidget *widget, | 
| 107 |                                          const QTableWidgetItem *self) | 
| 108 | { | 
| 109 |     // check if the string is actually a formula or not | 
| 110 |     QStringList list = formula.split(sep: ' '); | 
| 111 |     if (list.isEmpty() || !widget) | 
| 112 |         return formula; // it is a normal string | 
| 113 |  | 
| 114 |     QString op = list.value(i: 0).toLower(); | 
| 115 |  | 
| 116 |     int firstRow = -1; | 
| 117 |     int firstCol = -1; | 
| 118 |     int secondRow = -1; | 
| 119 |     int secondCol = -1; | 
| 120 |  | 
| 121 |     if (list.count() > 1) | 
| 122 |         decode_pos(pos: list.value(i: 1), row: &firstRow, col: &firstCol); | 
| 123 |  | 
| 124 |     if (list.count() > 2) | 
| 125 |         decode_pos(pos: list.value(i: 2), row: &secondRow, col: &secondCol); | 
| 126 |  | 
| 127 |     const QTableWidgetItem *start = widget->item(row: firstRow, column: firstCol); | 
| 128 |     const QTableWidgetItem *end = widget->item(row: secondRow, column: secondCol); | 
| 129 |  | 
| 130 |     int firstVal = start ? start->text().toInt() : 0; | 
| 131 |     int secondVal = end ? end->text().toInt() : 0; | 
| 132 |  | 
| 133 |     QVariant result; | 
| 134 |     if (op == "sum" ) { | 
| 135 |         int sum = 0; | 
| 136 |         for (int r = firstRow; r <= secondRow; ++r) { | 
| 137 |             for (int c = firstCol; c <= secondCol; ++c) { | 
| 138 |                 const QTableWidgetItem *tableItem = widget->item(row: r, column: c); | 
| 139 |                 if (tableItem && tableItem != self) | 
| 140 |                     sum += tableItem->text().toInt(); | 
| 141 |             } | 
| 142 |         } | 
| 143 |  | 
| 144 |         result = sum; | 
| 145 |     } else if (op == "+" ) { | 
| 146 |         result = (firstVal + secondVal); | 
| 147 |     } else if (op == "-" ) { | 
| 148 |         result = (firstVal - secondVal); | 
| 149 |     } else if (op == "*" ) { | 
| 150 |         result = (firstVal * secondVal); | 
| 151 |     } else if (op == "/" ) { | 
| 152 |         if (secondVal == 0) | 
| 153 |             result = QString("nan" ); | 
| 154 |         else | 
| 155 |             result = (firstVal / secondVal); | 
| 156 |     } else if (op == "=" ) { | 
| 157 |         if (start) | 
| 158 |             result = start->text(); | 
| 159 |     } else { | 
| 160 |         result = formula; | 
| 161 |     } | 
| 162 |  | 
| 163 |     return result; | 
| 164 | } | 
| 165 |  | 
| 166 |  |