source: trunk/demos/spreadsheet/spreadsheetitem.cpp@ 123

Last change on this file since 123 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 5.3 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the demonstration applications of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "spreadsheetitem.h"
43
44SpreadSheetItem::SpreadSheetItem()
45 : QTableWidgetItem(), isResolving(false)
46{
47}
48
49SpreadSheetItem::SpreadSheetItem(const QString &text)
50 : QTableWidgetItem(text), isResolving(false)
51{
52}
53
54QTableWidgetItem *SpreadSheetItem::clone() const
55{
56 SpreadSheetItem *item = new SpreadSheetItem();
57 *item = *this;
58 return item;
59}
60
61QVariant 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
88void SpreadSheetItem::setData(int role, const QVariant &value)
89{
90 QTableWidgetItem::setData(role, value);
91 if (tableWidget())
92 tableWidget()->viewport()->update();
93}
94
95QVariant 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
107QVariant 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
Note: See TracBrowser for help on using the repository browser.