kspread Library API Documentation

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., 59 Temple Place - Suite 330,
00017    Boston, MA 02111-1307, 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 
00029 
00030 #include <kdebug.h>
00031 
00032 #include <stdio.h>
00033 
00034 /***************************************************
00035  *
00036  * Factory
00037  *
00038  ***************************************************/
00039 
00040 K_EXPORT_COMPONENT_FACTORY( libkspreadcalc, CalcFactory )
00041 
00042 KInstance* CalcFactory::s_global = 0;
00043 
00044 CalcFactory::CalcFactory( QObject* parent, const char* name )
00045     : KLibFactory( parent, name )
00046 {
00047     s_global = new KInstance( "kspreadcalc" );
00048 }
00049 
00050 CalcFactory::~CalcFactory()
00051 {
00052     delete s_global;
00053 }
00054 
00055 QObject* CalcFactory::createObject( QObject* parent, const char* name, const char* /*classname*/, const QStringList & )
00056 {
00057     if ( !parent->inherits("KSpreadView") )
00058     {
00059         kdError() << "CalcFactory: KSpreadView expected. Parent is " << parent->className() << endl;
00060         return 0;
00061     }
00062 
00063     QObject *obj = new Calculator( (KSpreadView*)parent, name );
00064     return obj;
00065 }
00066 
00067 KInstance* CalcFactory::global()
00068 {
00069     return s_global;
00070 }
00071 
00072 /***************************************************
00073  *
00074  * Calculator
00075  *
00076  ***************************************************/
00077 
00078 Calculator::Calculator( KSpreadView* parent, const char* name )
00079     : KParts::Plugin( parent, name )
00080 {
00081     m_calc = 0;
00082     m_view = parent;
00083 
00084     KGlobal::locale()->insertCatalogue("kspreadcalc_calc");
00085     parent->installEventFilter( this );
00086 
00087     (void)new KAction( i18n("Calculator"), SmallIcon("kcalc", CalcFactory::global()),
00088                        0, this, SLOT( showCalculator() ), actionCollection(), "kspreadcalc");
00089 }
00090 
00091 void Calculator::showCalculator()
00092 {
00093     if ( m_calc )
00094     {
00095         m_calc->show();
00096         m_calc->raise();
00097         return;
00098     }
00099 
00100      m_calc = new QtCalculator( this, (KSpreadView*)parent() );
00101      m_calc->setFixedSize( 9 + 100 + 9 + 233 + 9, 239);
00102      m_calc->show();
00103      m_calc->raise();
00104 }
00105 
00106 Calculator::~Calculator()
00107 {
00108 }
00109 
00110 bool Calculator::eventFilter( QObject*, QEvent* ev )
00111 {
00112     if ( !m_calc )
00113         return FALSE;
00114 
00115     if ( KSpreadSelectionChanged::test( ev ) )
00116     {
00117         KSpreadSelectionChanged* event = (KSpreadSelectionChanged*)ev;
00118 
00119         // Selection cleared ?
00120         if ( event->rect().left() == 0 )
00121             return FALSE;
00122 
00123         KSpreadSheet* sheet = m_view->doc()->map()->findSheet( event->sheet() );
00124         if ( !sheet )
00125             return FALSE;
00126 
00127         // A single cell selected ?
00128         if ( event->rect().left() == event->rect().right() &&
00129              event->rect().top() == event->rect().bottom() )
00130         {
00131             KSpreadCell* cell = sheet->cellAt( event->rect().left(), event->rect().top(), false );
00132             if ( !cell )
00133                 return FALSE;
00134 
00135             double d;
00136             if ( cell->isEmpty() )
00137                 d = 0;
00138             else
00139                 d = cell->value().asFloat();
00140             m_calc->setValue( d );
00141 
00142             return FALSE;
00143         }
00144 
00145         // Multiple cells selected ?
00146         m_calc->setData( event->rect(), event->sheet().latin1() );
00147         QString str = util_rangeName( sheet, event->rect() );
00148         m_calc->setLabel( str.latin1() );
00149 
00150         return FALSE;
00151     }
00152 
00153     return FALSE;
00154 }
00155 
00156 /***************************************************
00157  *
00158  * QtCalculator
00159  *
00160  ***************************************************/
00161 
00166 void QtCalculator::useData()
00167 {
00168     stats.clearAll();
00169 
00170     // How many cells ?
00171     int len = ( sheet_range.right() - sheet_range.left() + 1 ) *
00172               ( sheet_range.bottom() - sheet_range.top() + 1 );
00173 
00174     double *v = new double[ len ];
00175     int n = 0;
00176     for( int x = sheet_range.left(); x <= sheet_range.right(); x++ )
00177         for( int y = sheet_range.top(); y <= sheet_range.bottom(); y++ )
00178         {
00179             KSpreadView* view = corba->view();
00180             KSpreadSheet* sheet = view->doc()->map()->findSheet( sheet_name );
00181             if ( !sheet )
00182                 return;
00183             KSpreadCell* cell = sheet->cellAt( x, y, false );
00184             if ( !cell )
00185                 return;
00186 
00187             v[n++] = cell->value().asFloat();
00188         }
00189 
00190     for( int i = 0; i < n; i++ )
00191         stats.enterData( v[i] );
00192 
00193     delete []v;
00194 
00195     sheet_name = QString::null;
00196 }
00197 
00198 #include "main.moc"
KDE Logo
This file is part of the documentation for kspread Library Version 1.4.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Mon Feb 13 09:43:38 2006 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003