summaryrefslogtreecommitdiff
path: root/noncore/apps/opie-sheet/sheet.h
blob: 89385114c2fa134fe5219464e459357e91f37695 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
               =.            This file is part of the Opie Project
             .=l.            Copyright (C) 2004 Opie Developer Team <opie-devel@handhelds.org>
           .>+-=
 _;:,     .>    :=|.         This program is free software; you can
.> <`_,   >  .   <=          redistribute it and/or  modify it under
:`=1 )Y*s>-.--   :           the terms of the GNU General Public
.="- .-=="i,     .._         License as published by the Free Software
 - .   .-<_>     .<>         Foundation; either version 2 of the License,
     ._= =}       :          or (at your option) any later version.
    .%`+i>       _;_.
    .i_,=:_.      -<s.       This program is distributed in the hope that
     +  .  -:.       =       it will be useful,  but WITHOUT ANY WARRANTY;
    : ..    .:,     . . .    without even the implied warranty of
    =_        +     =;=|`    MERCHANTABILITY or FITNESS FOR A
  _.=:.       :    :=>`:     PARTICULAR PURPOSE. See the GNU
..}^=.=       =       ;      Library General Public License for more
++=   -.     .`     .:       details.
 :     =  ...= . :.=-
 -.   .:....=;==+<;          You should have received a copy of the GNU
  -_. . .   )=.  =           Library General Public License along with
    --        :-=`           this library; see the file COPYING.LIB.
                             If not, write to the Free Software Foundation,
                             Inc., 59 Temple Place - Suite 330,
                             Boston, MA 02111-1307, USA.

*/

/*
 * Opie Sheet (formerly Sheet/Qt)
 * by Serdar Ozler <sozler@sitebest.com>
 */

#ifndef SHEET_H
#define SHEET_H

/* QT */
#include <qtable.h>
#include <qstack.h>

typedef struct typeCellBorders
{
    QPen right, bottom;
};

typedef struct typeCellData
{
    int col, row;
    typeCellBorders borders;
    QBrush background;
    Qt::AlignmentFlags alignment;
    QColor fontColor;
    QFont font;
    QString data;
};


#define NONE_TOKEN 0
#define NUMBER_TOKEN 1
#define VARIABLE_TOKEN 2
#define FUNCTION_TOKEN 3
#define SYMBOL_TOKEN 4
#define STRING_TOKEN 5

class Expression
{
public:
    QString Body;
    QList<QString> CompiledBody;
    QList<int> CompiledBodyType;
    QString SYMBOL;
    QString MATHSYMBOL;
    QArray<int> ArgsOfFunc;
    int FuncDepth;
    bool ErrorFound;
    int n; // holds the current parser position
    QString chunk; // the piece in the parser we are on
    int SymbGroup; // the current type

    QString InExpr;

    QChar chunk0(void); // retunrs the first char of expression;
    Expression(QString expr1);// constructor

    bool isSymbol(QChar ch);
    bool isMathSymbol(QChar ch);
    void GetNext();
    void First();
    void Third();
    void Fourth();
    void Fifth();
    void Sixth();
    void Seventh();
    void Eighth();
    void Ninth();

    bool Parse(); //parses the expression in RPN format;

};



class Sheet: public QTable
{
    Q_OBJECT

    // Variables
    bool clicksLocked;
    int selectionNo;
    typeCellBorders defaultBorders;
    typeCellData defaultCellData;

    // QT objects
    QList<typeCellData> sheetData, clipboardData;
    QString pressedCell, releasedCell, sheetName;
    QStringList listDataParser;

    // Private functions
    bool findRowColumn(const QString &variable, int *row, int *col, bool giveError=FALSE);
    QString findCellName(int row, int col);
    bool findRange(const QString &variable1, const QString &variable2, int *row1, int *col1, int *row2, int *col2);
    QString calculateVariable(const QString &variable);
    QString calculateFunction(const QString &func, const QString &parameters, int NumOfParams);
    QString getParameter(const QString &parameters, int paramNo, bool giveError=FALSE, const QString funcName="");
    QString dataParser(const QString &cell, const QString &data);
    QString dataParserHelper(const QString &data);
    typeCellData *createCellData(int row, int col);
    typeCellData *findCellData(int row, int col);


    //LOGICAL / INFO
    double functionCountIf(const QString &param1, const QString &param2, const QString &param3);
    double functionSumSQ(const QString &param1, const QString &param2); //sum of squares
    QString functionIndex(const QString &param1, const QString &param2, int indx);
    //math functions computations
    double BesselI0(double x);
    double BesselI(int n, double x);
    double BesselK0(double x);
    double BesselI1(double x);
    double BesselK1(double x);
    double BesselK(int n, double x);
    double BesselJ0(double x);
    double BesselY0(double x);
    double BesselJ1(double x);
    double BesselY1(double x);
    double BesselY(int n, double x);
    double BesselJ(int n, double x);
    double GammaLn(double xx);
    double Factorial(double n);
    double GammaP(double a, double x);
    double GammaQ(double a,double x);
    void GammaSeries(double *gamser, double a, double x, double *gln);
    void GammaContinuedFraction(double *gammcf, double a, double x, double *gln);
    double ErrorFunction(double x);
    double ErrorFunctionComplementary(double x);
    double Beta(double z, double w);
    double BetaContinuedFraction(double a, double b, double x);
    double BetaIncomplete(double a, double b, double x);
    double functionVariance(const QString &param1, const QString &param2);
    double functionVariancePopulation(const QString &param1, const QString &param2);
    double functionSkew(const QString &param1, const QString &param2);
    double functionKurt(const QString &param1, const QString &param2);

    // Sheet/Qt parser functions
    double functionSum(const QString &param1, const QString &param2);
    double functionAvg(const QString &param1, const QString &param2);
    double functionMax(const QString &param1, const QString &param2);
    double functionMin(const QString &param1, const QString &param2);
    double functionCount(const QString &param1, const QString &param2);

    // Reimplemented QTable functions
    void paintCell(QPainter *p, int row, int col, const QRect & cr, bool selected);
    void viewportMousePressEvent(QMouseEvent *e);
    void viewportMouseMoveEvent(QMouseEvent *e);
    void viewportMouseReleaseEvent(QMouseEvent *e);

public slots:
    void slotCellSelected(int row, int col);
    void slotCellChanged(int row, int col);

public:
    Sheet(int numRows, int numCols, QWidget *parent);
    ~Sheet();
    void ReCalc(void);
    void setData(const QString &data);
    QString getData();

    void setName(const QString &data);
    QString getName();

    void setPen(int row, int col, int vertical, const QPen &pen);
    QPen getPen(int row, int col, int vertical);

    void setBrush(int row, int col, const QBrush &brush);
    QBrush getBrush(int row, int col);

    void setTextAlign(int row, int col, Qt::AlignmentFlags flags);
    Qt::AlignmentFlags getAlignment(int row, int col);

    void setTextFont(int row, int col, const QFont &font, const QColor &color);
    QFont getFont(int row, int col);
    QColor getFontColor(int row, int col);

    void lockClicks(bool lock=TRUE);
    void copySheetData(QList<typeCellData> *destSheetData);
    void setSheetData(QList<typeCellData> *srcSheetData);
    void getSelection(int *row1, int *col1, int *row2, int *col2);

    void insertRows(int no=1, bool allColumns=TRUE);
    void insertColumns(int no=1, bool allRows=TRUE);

    void dataFindReplace(const QString &find, const QString &replace, bool matchCase=TRUE, bool allCells=TRUE, bool entireCell=FALSE, bool replace=FALSE, bool replaceAll=FALSE);

    // Static functions
    static int getHeaderColumn(const QString &section);
    static QString getHeaderString(int section);

public slots:
    void editCut();
    void editCopy();
    void editPaste(bool onlyContents=FALSE);
    void editClear();
    void swapCells(int row1, int col1, int row2, int col2);

signals:
    void currentDataChanged(const QString &data);
    void cellClicked(const QString &cell);
    void sheetModified();
};

#endif