kspread

main.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 1998, 1999 Torben Weis <weis@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "main.h"
00021 #include "kcalc.h"
00022 #include "kspread_view.h"
00023 #include "kspread_events.h"
00024 #include "kspread_doc.h"
00025 #include "kspread_locale.h"
00026 #include "kspread_util.h"
00027 #include "kspread_map.h"
00028 #include "region.h"
00029 
00030 #include <kdebug.h>
00031 
00032 #include <stdio.h>
00033 
00034 using namespace KSpread;
00035 
00036 /***************************************************
00037  *
00038  * Factory
00039  *
00040  ***************************************************/
00041 
00042 K_EXPORT_COMPONENT_FACTORY( libkspreadcalc, CalcFactory )
00043 
00044 KInstance* CalcFactory::s_global = 0;
00045 
00046 CalcFactory::CalcFactory( QObject* parent, const char* name )
00047     : KLibFactory( parent, name )
00048 {
00049     s_global = new KInstance( "kspreadcalc" );
00050 }
00051 
00052 CalcFactory::~CalcFactory()
00053 {
00054     delete s_global;
00055 }
00056 
00057 QObject* CalcFactory::createObject( QObject* parent, const char* name, const char* /*classname*/, const QStringList & )
00058 {
00059     if ( !parent->inherits("KSpread::View") )
00060     {
00061         kdError() << "CalcFactory: KSpread::View expected. Parent is " << parent->className() << endl;
00062         return 0;
00063     }
00064 
00065     QObject *obj = new Calculator( (View*)parent, name );
00066     return obj;
00067 }
00068 
00069 KInstance* CalcFactory::global()
00070 {
00071     return s_global;
00072 }
00073 
00074 /***************************************************
00075  *
00076  * Calculator
00077  *
00078  ***************************************************/
00079 
00080 Calculator::Calculator( View* parent, const char* name )
00081     : KParts::Plugin( parent, name )
00082 {
00083     m_calc = 0;
00084     m_view = parent;
00085 
00086     KGlobal::locale()->insertCatalogue("kspreadcalc_calc");
00087     parent->installEventFilter( this );
00088 
00089     (void)new KAction( i18n("Calculator"), SmallIcon("kcalc", CalcFactory::global()),
00090                        0, this, SLOT( showCalculator() ), actionCollection(), "kspreadcalc");
00091 }
00092 
00093 void Calculator::showCalculator()
00094 {
00095     if ( m_calc )
00096     {
00097         m_calc->show();
00098         m_calc->raise();
00099         return;
00100     }
00101 
00102      m_calc = new QtCalculator( this, (View*)parent() );
00103      m_calc->setFixedSize( 9 + 100 + 9 + 233 + 9, 239);
00104      m_calc->show();
00105      m_calc->raise();
00106 }
00107 
00108 Calculator::~Calculator()
00109 {
00110 }
00111 
00112 bool Calculator::eventFilter( QObject*, QEvent* ev )
00113 {
00114     if ( !m_calc )
00115         return FALSE;
00116 
00117     if ( SelectionChanged::test( ev ) )
00118     {
00119         SelectionChanged* event = (SelectionChanged*)ev;
00120 
00121         // Selection cleared ?
00122         if (!event->region().isValid())
00123             return FALSE;
00124 
00125         Sheet* sheet = m_view->doc()->map()->findSheet( event->sheet() );
00126         if ( !sheet )
00127             return FALSE;
00128 
00129         // A single cell selected ?
00130         if (event->region().isSingular())
00131         {
00132             Cell* cell = sheet->cellAt( event->rect().left(), event->rect().top(), false );
00133             if ( !cell )
00134                 return FALSE;
00135 
00136             double d;
00137             if ( cell->isEmpty() )
00138                 d = 0;
00139             else
00140                 d = cell->value().asFloat();
00141             m_calc->setValue( d );
00142 
00143             return FALSE;
00144         }
00145 
00146         // Multiple cells selected ?
00147         m_calc->setData( event->rect(), event->sheet().latin1() );
00148         QString str = util_rangeName( sheet, event->rect() );
00149         m_calc->setLabel( str.latin1() );
00150 
00151         return FALSE;
00152     }
00153 
00154     return FALSE;
00155 }
00156 
00157 /***************************************************
00158  *
00159  * QtCalculator
00160  *
00161  ***************************************************/
00162 
00167 void QtCalculator::useData()
00168 {
00169     stats.clearAll();
00170 
00171     // How many cells ?
00172     int len = ( sheet_range.right() - sheet_range.left() + 1 ) *
00173               ( sheet_range.bottom() - sheet_range.top() + 1 );
00174 
00175     double *v = new double[ len ];
00176     int n = 0;
00177     for( int x = sheet_range.left(); x <= sheet_range.right(); x++ )
00178         for( int y = sheet_range.top(); y <= sheet_range.bottom(); y++ )
00179         {
00180             View* view = corba->view();
00181             Sheet* sheet = view->doc()->map()->findSheet( sheet_name );
00182             if ( !sheet )
00183                 return;
00184             Cell* cell = sheet->cellAt( x, y, false );
00185             if ( !cell )
00186                 return;
00187 
00188             v[n++] = cell->value().asFloat();
00189         }
00190 
00191     for( int i = 0; i < n; i++ )
00192         stats.enterData( v[i] );
00193 
00194     delete []v;
00195 
00196     sheet_name = QString::null;
00197 }
00198 
00199 #include "main.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys