kspread

kspread_dlg_series.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 2002-2004 Ariya Hidayat <ariya@kde.org>
00003              (C) 2002-2003 Norbert Andres <nandres@web.de>
00004              (C) 2002 John Dailey <dailey@vt.edu>
00005              (C) 1999-2002 Laurent Montel <montel@kde.org>
00006              (C) 2001-2002 Philipp Mueller <philipp.mueller@gmx.de>
00007              (C) 2000-2001 Werner Trobin <trobin@kde.org>
00008              (C) 1998-2000 Torben Weis <weis@kde.org>
00009    
00010    This library is free software; you can redistribute it and/or
00011    modify it under the terms of the GNU Library General Public
00012    License as published by the Free Software Foundation; either
00013    version 2 of the License, or (at your option) any later version.
00014 
00015    This library is distributed in the hope that it will be useful,
00016    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018    Library General Public License for more details.
00019 
00020    You should have received a copy of the GNU Library General Public License
00021    along with this library; see the file COPYING.LIB.  If not, write to
00022    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00023  * Boston, MA 02110-1301, USA.
00024 */
00025 
00026 
00027 #include "kspread_dlg_series.h"
00028 #include "kspread_doc.h"
00029 #include "kspread_editors.h"
00030 #include "kspread_sheet.h"
00031 #include "kspread_view.h"
00032 
00033 #include <qlayout.h>
00034 #include <klocale.h>
00035 #include <qlabel.h>
00036 
00037 #include <qbuttongroup.h>
00038 #include <qgroupbox.h>
00039 #include <kmessagebox.h>
00040 #include <knumvalidator.h>
00041 
00042 #include <qradiobutton.h>
00043 #include <qcheckbox.h>
00044 #include <qlineedit.h>
00045 #include <qwhatsthis.h>
00046 #include <knuminput.h>
00047 
00048 using namespace KSpread;
00049 
00050 SeriesDlg::SeriesDlg( View* parent, const char* name,const QPoint &_marker)
00051   : KDialogBase( parent, name,TRUE,i18n("Series"),Ok|Cancel )
00052 {
00053   m_pView = parent;
00054   marker=_marker;
00055   QWidget *page = new QWidget( this );
00056   setMainWidget(page);
00057 
00058   QBoxLayout *grid1 = new QHBoxLayout(page);
00059   grid1->setSpacing( spacingHint() );
00060 
00061   QButtonGroup* gb1 = new QButtonGroup( 2, Qt::Vertical,
00062     i18n("Insert Values"), page );
00063   column = new QRadioButton( i18n("Vertical"), gb1 );
00064   QWhatsThis::add(column, i18n("Insert the series vertically, one below the other") );
00065   row = new QRadioButton( i18n("Horizontal"), gb1 );
00066   QWhatsThis::add(row, i18n("Insert the series horizontally, from left to right") );
00067 
00068   column->setChecked(true);
00069 
00070   QButtonGroup* gb2 = new QButtonGroup( 2, Qt::Vertical,
00071     i18n("Type"), page );
00072   linear = new QRadioButton( i18n("Linear (2,4,6,...)"), gb2 );
00073   QWhatsThis::add(linear, i18n("Generate a series from 'start' to 'end' and for each step add "
00074      "the value provided in step. This creates a series where each value "
00075      "is 'step' larger than the value before it.") );
00076   geometric = new QRadioButton( i18n("Geometric (2,4,8,...)"), gb2 );
00077   QWhatsThis::add(geometric, i18n("Generate a series from 'start' to 'end' and for each step multiply "
00078      "the value with the value provided in step. Using a step of 5 produces a list like: "
00079       "5, 25, 125, 625 since 5 multiplied by 5 (step) equals 25, and that multiplied by 5 equals 125, "
00080       "which multiplied by the same step-value of 5 equals 625.") );
00081 
00082   linear->setChecked(true);
00083 
00084   QGroupBox* gb = new QGroupBox( 1, Qt::Vertical, i18n("Parameters"), page );
00085   QWidget *params = new QWidget( gb );
00086   QGridLayout *params_layout = new QGridLayout( params, 3, 2 );
00087   params_layout->setSpacing( spacingHint() );
00088   params_layout->setAutoAdd( true );
00089 
00090   new QLabel( i18n( "Start value:" ), params );
00091   start=new KDoubleNumInput(-999999.999, 999999.99, 0.0, 1.0, 3, params);
00092 
00093   new QLabel( i18n( "Stop value:" ), params );
00094   end=new KDoubleNumInput(-999999.999, 999999.99, 0.0, 1.0, 3, params);
00095 
00096   new QLabel( i18n( "Step value:" ), params );
00097   step=new KDoubleNumInput(-999999.999, 999999.99, 0.0, 1.0, 3, params);
00098  
00099   grid1->addWidget(gb);
00100 
00101   grid1->addWidget(gb1);
00102   grid1->addWidget(gb2);
00103 
00104   start->setFocus();
00105 
00106   connect( this, SIGNAL( okClicked() ), this, SLOT( slotOk() ) );
00107 }
00108 
00109 
00110 void SeriesDlg::slotOk()
00111 {
00112 
00113   Series mode=Column;  //same as Vertical
00114   Series type=Linear;  // same as Horizontal
00115   QString tmp;
00116   double dstep, dend, dstart;
00117   Sheet * m_pSheet;
00118   m_pSheet = m_pView->activeSheet();
00119 
00120   if(column->isChecked())
00121     mode = Column;
00122   else if(row->isChecked())
00123     mode = Row;
00124 
00125   if (linear->isChecked())
00126     type = Linear;
00127   else if (geometric->isChecked())
00128     type = Geometric;
00129 
00130   dstart = start->value();
00131   dend= end->value();
00132   dstep = step->value();
00133   if ( type == Geometric )
00134   {
00135       if  ( dstart < 0 || dend < 0 )
00136       {
00137           KMessageBox::error( this, i18n("End and start value must be positive.") );
00138           return;
00139       }
00140       if ( dstart > dend && dstep >= 1)
00141       {
00142         KMessageBox::error( this, i18n("End value must be greater than the start value or the step must be less than '1'.") );
00143         return;
00144       }
00145       if ( dstart == 0 || dend == 0 || dstep == 0)
00146       {
00147         KMessageBox::error( this, i18n("None of the Start, Stop or Step values may be equal to zero.") );
00148         return;
00149       }
00150       if ( dstep == 1)
00151       {
00152         KMessageBox::error( this, i18n("Step value must be different from 1") );
00153         return;
00154       }
00155   }
00156 
00157   if (dstep >= 0)
00158   {
00159       if (linear->isChecked() && dstep == 0)
00160       {
00161           KMessageBox::error( this, i18n("The step value must be greater than zero; "
00162                                          "otherwise, the linear series is infinite.") );
00163           step->setFocus();
00164           return;
00165       }
00166       /*      else if (geometric->isChecked() && dstep <= 1)
00167               {
00168               KMessageBox::error( this, i18n("The step value must be greater than one; "
00169                                        "otherwise, the geometric series is infinite.") );
00170                                        step->setFocus();
00171                                        return;
00172                                        }
00173       */
00174       else if ( type == Linear && dend < dstart )
00175       {
00176           KMessageBox::error( this,
00177                               i18n("If the start value is greater than the end value the step must be less than zero.") );
00178           return;
00179       }
00180   }
00181   else if (type != Linear)
00182   {
00183       KMessageBox::error( this, i18n("Step is negative.") );
00184       return;
00185   }
00186   else
00187   {
00188       if (dstart <= dend)
00189       {
00190         KMessageBox::error( this,
00191                             i18n("If the step is negative, the start value must be greater then the end value.") );
00192         return;
00193       }
00194   }
00195 
00196   //        double val_end = QMAX(dend, dstart);
00197   //        double val_start = QMIN(dend, dstart);
00198   m_pView->doc()->emitBeginOperation( false );
00199 
00200   m_pSheet->setSeries( marker, dstart, dend, dstep, mode, type );
00201 
00202   Cell * cell = m_pSheet->cellAt( marker.x(), marker.y() );
00203   if ( cell->text() != 0L )
00204     m_pView->editWidget()->setText( cell->text() );
00205   else
00206     m_pView->editWidget()->setText( "" );
00207 
00208   m_pView->slotUpdateView( m_pView->activeSheet() );
00209   accept();
00210 }
00211 
00212 
00213 #include "kspread_dlg_series.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys