plugins/calculator/main.cpp
1/* This file is part of the KDE project
2 Copyright (C) 1998, 1999 Torben Weis <weis@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include "main.h"
21#include "kcalc.h"
22#include "kspread_view.h"
23#include "kspread_events.h"
24#include "kspread_doc.h"
25#include "kspread_locale.h"
26#include "kspread_util.h"
27#include "kspread_map.h"
28#include "region.h"
29
30#include <kdebug.h>
31
32#include <stdio.h>
33
34using namespace KSpread;
35
36/***************************************************
37 *
38 * Factory
39 *
40 ***************************************************/
41
42K_EXPORT_COMPONENT_FACTORY( libkspreadcalc, CalcFactory )
43
44TDEInstance* CalcFactory::s_global = 0;
45
46CalcFactory::CalcFactory( TQObject* parent, const char* name )
47 : KLibFactory( parent, name )
48{
49 s_global = new TDEInstance( "kspreadcalc" );
50}
51
52CalcFactory::~CalcFactory()
53{
54 delete s_global;
55}
56
57TQObject* CalcFactory::createObject( TQObject* parent, const char* name, const char* /*classname*/, const TQStringList & )
58{
59 if ( !parent->inherits("KSpread::View") )
60 {
61 kdError() << "CalcFactory: KSpread::View expected. Parent is " << parent->className() << endl;
62 return 0;
63 }
64
65 TQObject *obj = new Calculator( (View*)parent, name );
66 return obj;
67}
68
69TDEInstance* CalcFactory::global()
70{
71 return s_global;
72}
73
74/***************************************************
75 *
76 * Calculator
77 *
78 ***************************************************/
79
80Calculator::Calculator( View* parent, const char* name )
81 : KParts::Plugin( parent, name )
82{
83 m_calc = 0;
84 m_view = parent;
85
86 TDEGlobal::locale()->insertCatalogue("kspreadcalc_calc");
87 parent->installEventFilter( this );
88
89 (void)new TDEAction( i18n("Calculator"), SmallIcon("kcalc", CalcFactory::global()),
90 0, this, TQ_SLOT( showCalculator() ), actionCollection(), "kspreadcalc");
91}
92
93void Calculator::showCalculator()
94{
95 if ( m_calc )
96 {
97 m_calc->show();
98 m_calc->raise();
99 return;
100 }
101
102 m_calc = new TQtCalculator( this, (View*)parent() );
103 m_calc->setFixedSize( 9 + 100 + 9 + 233 + 9, 239);
104 m_calc->show();
105 m_calc->raise();
106}
107
108Calculator::~Calculator()
109{
110}
111
112bool Calculator::eventFilter( TQObject*, TQEvent* ev )
113{
114 if ( !m_calc )
115 return false;
116
117 if ( SelectionChanged::test( ev ) )
118 {
119 SelectionChanged* event = (SelectionChanged*)ev;
120
121 // Selection cleared ?
122 if (!event->region().isValid())
123 return false;
124
125 Sheet* sheet = m_view->doc()->map()->findSheet( event->sheet() );
126 if ( !sheet )
127 return false;
128
129 // A single cell selected ?
130 if (event->region().isSingular())
131 {
132 Cell* cell = sheet->cellAt( event->rect().left(), event->rect().top(), false );
133 if ( !cell )
134 return false;
135
136 double d;
137 if ( cell->isEmpty() )
138 d = 0;
139 else
140 d = cell->value().asFloat();
141 m_calc->setValue( d );
142
143 return false;
144 }
145
146 // Multiple cells selected ?
147 m_calc->setData( event->rect(), event->sheet().latin1() );
148 TQString str = util_rangeName( sheet, event->rect() );
149 m_calc->setLabel( str.latin1() );
150
151 return false;
152 }
153
154 return false;
155}
156
157/***************************************************
158 *
159 * TQtCalculator
160 *
161 ***************************************************/
162
167void TQtCalculator::useData()
168{
169 stats.clearAll();
170
171 // How many cells ?
172 int len = ( sheet_range.right() - sheet_range.left() + 1 ) *
173 ( sheet_range.bottom() - sheet_range.top() + 1 );
174
175 double *v = new double[ len ];
176 int n = 0;
177 for( int x = sheet_range.left(); x <= sheet_range.right(); x++ )
178 for( int y = sheet_range.top(); y <= sheet_range.bottom(); y++ )
179 {
180 View* view = corba->view();
181 Sheet* sheet = view->doc()->map()->findSheet( sheet_name );
182 if ( !sheet )
183 return;
184 Cell* cell = sheet->cellAt( x, y, false );
185 if ( !cell )
186 return;
187
188 v[n++] = cell->value().asFloat();
189 }
190
191 for( int i = 0; i < n; i++ )
192 stats.enterData( v[i] );
193
194 delete []v;
195
196 sheet_name = TQString();
197}
198
199#include "main.moc"
For every cell in the spread sheet there is a Cell object.
const Value value() const
Returns the value that this cell holds.
bool isEmpty() const
Returns true if this cell has no content, i.e no text and no formula.
Sheet * findSheet(const TQString &_name)
searches for a sheet named _name
double asFloat() const
Returns the floating-point value of this value.
The View class displays a KSpread document.
The KSpread namespace.
Definition commands.h:39