|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the demonstration applications of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include "spreadsheetitem.h" |
|
43 |
|
44 SpreadSheetItem::SpreadSheetItem() |
|
45 : QTableWidgetItem(), isResolving(false) |
|
46 { |
|
47 } |
|
48 |
|
49 SpreadSheetItem::SpreadSheetItem(const QString &text) |
|
50 : QTableWidgetItem(text), isResolving(false) |
|
51 { |
|
52 } |
|
53 |
|
54 QTableWidgetItem *SpreadSheetItem::clone() const |
|
55 { |
|
56 SpreadSheetItem *item = new SpreadSheetItem(); |
|
57 *item = *this; |
|
58 return item; |
|
59 } |
|
60 |
|
61 QVariant SpreadSheetItem::data(int role) const |
|
62 { |
|
63 if (role == Qt::EditRole || role == Qt::StatusTipRole) |
|
64 return formula(); |
|
65 |
|
66 if (role == Qt::DisplayRole) |
|
67 return display(); |
|
68 |
|
69 QString t = display().toString(); |
|
70 bool isNumber = false; |
|
71 int number = t.toInt(&isNumber); |
|
72 |
|
73 if (role == Qt::TextColorRole) { |
|
74 if (!isNumber) |
|
75 return qVariantFromValue(QColor(Qt::black)); |
|
76 else if (number < 0) |
|
77 return qVariantFromValue(QColor(Qt::red)); |
|
78 return qVariantFromValue(QColor(Qt::blue)); |
|
79 } |
|
80 |
|
81 if (role == Qt::TextAlignmentRole) |
|
82 if (!t.isEmpty() && (t.at(0).isNumber() || t.at(0) == '-')) |
|
83 return (int)(Qt::AlignRight | Qt::AlignVCenter); |
|
84 |
|
85 return QTableWidgetItem::data(role); |
|
86 } |
|
87 |
|
88 void SpreadSheetItem::setData(int role, const QVariant &value) |
|
89 { |
|
90 QTableWidgetItem::setData(role, value); |
|
91 if (tableWidget()) |
|
92 tableWidget()->viewport()->update(); |
|
93 } |
|
94 |
|
95 QVariant SpreadSheetItem::display() const |
|
96 { |
|
97 // avoid circular dependencies |
|
98 if (isResolving) |
|
99 return QVariant(); |
|
100 |
|
101 isResolving = true; |
|
102 QVariant result = computeFormula(formula(), tableWidget(), this); |
|
103 isResolving = false; |
|
104 return result; |
|
105 } |
|
106 |
|
107 QVariant SpreadSheetItem::computeFormula(const QString &formula, |
|
108 const QTableWidget *widget, |
|
109 const QTableWidgetItem *self) |
|
110 { |
|
111 // check if the s tring is actually a formula or not |
|
112 QStringList list = formula.split(' '); |
|
113 if (list.isEmpty() || !widget) |
|
114 return formula; // it is a normal string |
|
115 |
|
116 QString op = list.value(0).toLower(); |
|
117 |
|
118 int firstRow = -1; |
|
119 int firstCol = -1; |
|
120 int secondRow = -1; |
|
121 int secondCol = -1; |
|
122 |
|
123 if (list.count() > 1) |
|
124 decode_pos(list.value(1), &firstRow, &firstCol); |
|
125 |
|
126 if (list.count() > 2) |
|
127 decode_pos(list.value(2), &secondRow, &secondCol); |
|
128 |
|
129 const QTableWidgetItem *start = widget->item(firstRow, firstCol); |
|
130 const QTableWidgetItem *end = widget->item(secondRow, secondCol); |
|
131 |
|
132 int firstVal = start ? start->text().toInt() : 0; |
|
133 int secondVal = end ? end->text().toInt() : 0; |
|
134 |
|
135 QVariant result; |
|
136 if (op == "sum") { |
|
137 int sum = 0; |
|
138 for (int r = firstRow; r <= secondRow; ++r) { |
|
139 for (int c = firstCol; c <= secondCol; ++c) { |
|
140 const QTableWidgetItem *tableItem = widget->item(r, c); |
|
141 if (tableItem && tableItem != self) |
|
142 sum += tableItem->text().toInt(); |
|
143 } |
|
144 } |
|
145 |
|
146 result = sum; |
|
147 } else if (op == "+") { |
|
148 result = (firstVal + secondVal); |
|
149 } else if (op == "-") { |
|
150 result = (firstVal - secondVal); |
|
151 } else if (op == "*") { |
|
152 result = (firstVal * secondVal); |
|
153 } else if (op == "/") { |
|
154 if (secondVal == 0) |
|
155 result = QString("nan"); |
|
156 else |
|
157 result = (firstVal / secondVal); |
|
158 } else if (op == "=") { |
|
159 if (start) |
|
160 result = start->text(); |
|
161 } else { |
|
162 result = formula; |
|
163 } |
|
164 |
|
165 return result; |
|
166 } |
|
167 |