kspread
main.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
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* , 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
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
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
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
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
00160
00161
00162
00167 void QtCalculator::useData()
00168 {
00169 stats.clearAll();
00170
00171
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"
|