kspread Library API Documentation

kspread_view.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 2002-2005 Ariya Hidayat <ariya@kde.org>
00003              (C) 1999-2003 Laurent Montel <montel@kde.org>
00004              (C) 2002-2003 Norbert Andres <nandres@web.de>
00005              (C) 2002-2003 Philipp Mueller <philipp.mueller@gmx.de>
00006              (C) 2002-2003 John Dailey <dailey@vt.edu>
00007              (C) 1999-2003 David Faure <faure@kde.org>
00008              (C) 1999-2001 Simon Hausmann <hausmann@kde.org>
00009              (C) 1998-2000 Torben Weis <weis@kde.org>
00010 
00011    This library is free software; you can redistribute it and/or
00012    modify it under the terms of the GNU Library General Public
00013    License as published by the Free Software Foundation; either
00014    version 2 of the License, or (at your option) any later version.
00015 
00016    This library is distributed in the hope that it will be useful,
00017    but WITHOUT ANY WARRANTY; without even the implied warranty of
00018    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019    Library General Public License for more details.
00020 
00021    You should have received a copy of the GNU Library General Public License
00022    along with this library; see the file COPYING.LIB.  If not, write to
00023    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00024    Boston, MA 02111-1307, USA.
00025 */
00026 
00027 #include <kprinter.h> // has to be first
00028 
00029 // standard C/C++ includes
00030 #include <stdlib.h>
00031 #include <time.h>
00032 #include <assert.h>
00033 
00034 // Qt includes
00035 #include <qcursor.h>
00036 #include <qlayout.h>
00037 #include <qpaintdevicemetrics.h>
00038 #include <qregexp.h>
00039 #include <qtimer.h>
00040 #include <qtoolbutton.h>
00041 
00042 // KDE includes
00043 #include <dcopclient.h>
00044 #include <dcopref.h>
00045 #include <kapplication.h>
00046 #include <kconfig.h>
00047 #include <kdebug.h>
00048 #include <kfind.h>
00049 #include <kfinddialog.h>
00050 #include <kfontdialog.h>
00051 #include <klineeditdlg.h>
00052 #include <kmessagebox.h>
00053 #include <knotifyclient.h>
00054 #include <kpassdlg.h>
00055 #include <kprocio.h>
00056 #include <kreplace.h>
00057 #include <kreplacedialog.h>
00058 #include <kspell.h>
00059 #include <kspelldlg.h>
00060 #include <kstatusbar.h>
00061 #include <kstdaction.h>
00062 #include <kstandarddirs.h>
00063 #include <ktempfile.h>
00064 #include <kparts/partmanager.h>
00065 
00066 // KOffice includes
00067 #include <tkcoloractions.h>
00068 #include <kdatatool.h>
00069 #include <koCharSelectDia.h>
00070 #include <kocommandhistory.h>
00071 #include <koMainWindow.h>
00072 #include <koPartSelectAction.h>
00073 #include <kotabbar.h>
00074 #include <koToolBox.h>
00075 #include <koTemplateCreateDia.h>
00076 #include <kozoomaction.h>
00077 
00078 // KSpread includes
00079 #include "commands.h"
00080 #include "damages.h"
00081 #include "digest.h"
00082 #include "handler.h"
00083 #include "inspector.h"
00084 #include "kspread_canvas.h"
00085 #include "kspread_editors.h"
00086 #include "kspread_events.h"
00087 #include "kspread_global.h"
00088 #include "kspread_handler.h"
00089 #include "kspread_locale.h"
00090 #include "kspread_map.h"
00091 #include "kspread_selection.h"
00092 #include "kspread_sheetprint.h"
00093 #include "kspread_style.h"
00094 #include "kspread_style_manager.h"
00095 #include "kspread_undo.h"
00096 #include "testrunner.h"
00097 #include "valuecalc.h"
00098 #include "valueconverter.h"
00099 
00100 // dialogs
00101 #include "dialogs/kspread_dlg_angle.h"
00102 #include "dialogs/kspread_dlg_area.h"
00103 #include "dialogs/kspread_dlg_comment.h"
00104 #include "dialogs/kspread_dlg_conditional.h"
00105 #include "dialogs/kspread_dlg_cons.h"
00106 #include "dialogs/kspread_dlg_csv.h"
00107 #include "dialogs/kspread_dlg_database.h"
00108 #include "dialogs/kspread_dlg_format.h"
00109 #include "dialogs/kspread_dlg_formula.h"
00110 #include "dialogs/kspread_dlg_goalseek.h"
00111 #include "dialogs/kspread_dlg_goto.h"
00112 #include "dialogs/kspread_dlg_insert.h"
00113 #include "dialogs/kspread_dlg_layout.h"
00114 #include "dialogs/kspread_dlg_list.h"
00115 //#include "dialogs/kspread_dlg_multipleop.h"
00116 #include "dialogs/kspread_dlg_paperlayout.h"
00117 #include "dialogs/kspread_dlg_pasteinsert.h"
00118 #include "dialogs/kspread_dlg_preference.h"
00119 #include "dialogs/kspread_dlg_reference.h"
00120 #include "dialogs/kspread_dlg_resize2.h"
00121 #include "dialogs/kspread_dlg_series.h"
00122 #include "dialogs/kspread_dlg_show.h"
00123 #include "dialogs/kspread_dlg_showColRow.h"
00124 #include "dialogs/kspread_dlg_sort.h"
00125 #include "dialogs/kspread_dlg_special.h"
00126 #include "dialogs/kspread_dlg_styles.h"
00127 #include "dialogs/kspread_dlg_subtotal.h"
00128 #include "dialogs/kspread_dlg_validity.h"
00129 #include "dialogs/link.h"
00130 #include "dialogs/sheet_properties.h"
00131 
00132 // KSpread DCOP
00133 #include "KSpreadViewIface.h"
00134 
00135 using namespace KSpread;
00136 
00137 class ViewActions;
00138 
00139 class ViewPrivate
00140 {
00141 public:
00142     KSpreadView* view;
00143     KSpreadDoc* doc;
00144     DCOPObject* dcop;
00145 
00146     // the active sheet, may be 0
00147     // this is the sheet which has the input focus
00148     KSpreadSheet* activeSheet;
00149 
00150     // GUI elements
00151     QWidget *frame;
00152     QFrame *toolWidget;
00153     KSpreadCanvas *canvas;
00154     KSpreadVBorder *vBorderWidget;
00155     KSpreadHBorder *hBorderWidget;
00156     QScrollBar *horzScrollBar;
00157     QScrollBar *vertScrollBar;
00158     KoTabBar *tabBar;
00159     KStatusBarLabel* calcLabel;
00160 
00161     // formulabar, consists of:
00162     QHBoxLayout* formulaBarLayout;
00163     KSpreadComboboxLocationEditWidget *posWidget;
00164     QButton* formulaButton;
00165     QButton *okButton;
00166     QButton *cancelButton;
00167     KSpreadEditWidget *editWidget;
00168     QGridLayout* viewLayout;
00169     QHBoxLayout* tabScrollBarLayout;
00170 
00171     // all UI actions
00172     ViewActions* actions;
00173 
00174     // If updateEditWidget is called it changes some KToggleActions.
00175     // That causes them to emit a signal. If this lock is TRUE, then these
00176     // signals are ignored.
00177     bool toolbarLock;
00178 
00179     // if true, kspread is still loading the document
00180     // don't try to refresh the view
00181     bool loading;
00182 
00183     // selection/marker
00184     KSpreadSelection* selectionInfo;
00185     QMap<KSpreadSheet*, QPoint> savedAnchors;
00186     QMap<KSpreadSheet*, QPoint> savedMarkers;
00187 
00188     // Find and Replace context. We remember the options and
00189     // the strings used previously.
00190     long findOptions;
00191     QStringList findStrings;
00192     QStringList replaceStrings;
00193 
00194     // Current "find" operation
00195     KFind* find;
00196     KReplace* replace;
00197     int findLeftColumn;
00198     int findRightColumn;
00199     QPoint findPos;
00200     QPoint findEnd;
00201 
00202     KSpreadInsertHandler* insertHandler;
00203 
00204     // Insert special character dialog
00205     KoCharSelectDia* specialCharDlg;
00206 
00207     // Holds a guarded pointer to the transformation toolbox.
00208     QGuardedPtr<KoTransformToolBox> transformToolBox;
00209 
00210     // the last popup menu (may be 0).
00211     // Since only one popup menu can be opened at once, its pointer is stored here.
00212     // Delete the old one before you store a pointer to anotheron here.
00213     QPopupMenu *popupMenu;
00214     int popupMenuFirstToolId;
00215 
00216     QPopupMenu *popupRow;
00217     QPopupMenu *popupColumn;
00218     QPopupMenu* popupChild;       // for embedded children
00219     QPopupMenu* popupListChoose;  // for list of choose
00220 
00221     // the child for which the popup menu has been opened.
00222     KSpreadChild* popupChildObject;
00223 
00224     // spell-check context
00225     struct
00226     {
00227       KSpell *   kspell;
00228       KSpreadSheet *  firstSpellSheet;
00229       KSpreadSheet *  currentSpellSheet;
00230       KSpreadCell  *  currentCell;
00231       KSpreadMacroUndoAction *macroCmdSpellCheck;
00232       unsigned int    spellCurrCellX;
00233       unsigned int    spellCurrCellY;
00234       unsigned int    spellStartCellX;
00235       unsigned int    spellStartCellY;
00236       unsigned int    spellEndCellX;
00237       unsigned int    spellEndCellY;
00238       bool            spellCheckSelection;
00239       QStringList replaceAll;
00240     } spell;
00241 
00242     // the tools
00243     struct ToolEntry
00244     {
00245       QString command;
00246       KDataToolInfo info;
00247     };
00248     QPtrList<ToolEntry> toolList;
00249 
00250     void initActions();
00251     void adjustActions( bool mode );
00252     void adjustActions( KSpreadSheet* sheet, KSpreadCell* cell );
00253     void adjustWorkbookActions( bool mode );
00254     void updateButton( KSpreadCell *cell, int column, int row);
00255     QButton* newIconButton( const char *_file, bool _kbutton = false, QWidget *_parent = 0L );
00256 };
00257 
00258 class ViewActions
00259 {
00260 public:
00261 
00262     // cell formatting
00263     KAction* cellLayout;
00264     KAction* defaultFormat;
00265     KToggleAction* bold;
00266     KToggleAction* italic;
00267     KToggleAction* underline;
00268     KToggleAction* strikeOut;
00269     KFontAction* selectFont;
00270     KFontSizeAction* selectFontSize;
00271     KAction* fontSizeUp;
00272     KAction* fontSizeDown;
00273     TKSelectColorAction* textColor;
00274     KToggleAction* alignLeft;
00275     KToggleAction* alignCenter;
00276     KToggleAction* alignRight;
00277     KToggleAction* alignTop;
00278     KToggleAction* alignMiddle;
00279     KToggleAction* alignBottom;
00280     KToggleAction* wrapText;
00281     KToggleAction* verticalText;
00282     KAction* increaseIndent;
00283     KAction* decreaseIndent;
00284     KAction* changeAngle;
00285     KToggleAction* percent;
00286     KAction* precplus;
00287     KAction* precminus;
00288     KToggleAction* money;
00289     KAction* upper;
00290     KAction* lower;
00291     KAction* firstLetterUpper;
00292     TKSelectColorAction* bgColor;
00293     KAction* borderLeft;
00294     KAction* borderRight;
00295     KAction* borderTop;
00296     KAction* borderBottom;
00297     KAction* borderAll;
00298     KAction* borderOutline;
00299     KAction* borderRemove;
00300     TKSelectColorAction* borderColor;
00301     KSelectAction* selectStyle;
00302     KAction* createStyle;
00303 
00304     // cell operations
00305     KAction* editCell;
00306     KAction* insertCell;
00307     KAction* removeCell;
00308     KAction* deleteCell;
00309     KAction* mergeCell;
00310     KAction* dissociateCell;
00311     KAction* clearText;
00312     KAction* conditional;
00313     KAction* clearConditional;
00314     KAction* validity;
00315     KAction* clearValidity;
00316     KAction* addModifyComment;
00317     KAction* removeComment;
00318     KAction* clearComment;
00319 
00320     // column & row operations
00321     KAction* resizeColumn;
00322     KAction* insertColumn;
00323     KAction* deleteColumn;
00324     KAction* hideColumn;
00325     KAction* showColumn;
00326     KAction* equalizeColumn;
00327     KAction* showSelColumns;
00328     KAction* resizeRow;
00329     KAction* insertRow;
00330     KAction* deleteRow;
00331     KAction* hideRow;
00332     KAction* showRow;
00333     KAction* equalizeRow;
00334     KAction* showSelRows;
00335     KAction* adjust;
00336 
00337     // sheet/workbook operations
00338     KAction* sheetProperties;
00339     KAction* insertSheet;
00340     KAction* menuInsertSheet;
00341     KAction* removeSheet;
00342     KAction* renameSheet;
00343     KAction* hideSheet;
00344     KAction* showSheet;
00345     KAction* autoFormat;
00346     KAction* areaName;
00347     KAction* showArea;
00348     KAction* insertSeries;
00349     KAction* insertFunction;
00350     KAction* insertSpecialChar;
00351     KAction* insertFromDatabase;
00352     KAction* insertFromTextfile;
00353     KAction* insertFromClipboard;
00354     KAction* transform;
00355     KAction* sort;
00356     KAction* sortDec;
00357     KAction* sortInc;
00358     KAction* fillRight;
00359     KAction* fillLeft;
00360     KAction* fillUp;
00361     KAction* fillDown;
00362     KAction* paperLayout;
00363     KAction* definePrintRange;
00364     KAction* resetPrintRange;
00365     KToggleAction* showPageBorders;
00366     KAction* recalcWorksheet;
00367     KAction* recalcWorkbook;
00368     KToggleAction* protectSheet;
00369     KToggleAction* protectDoc;
00370 
00371     // general editing
00372     KAction* cut;
00373     KAction* copy;
00374     KAction* paste;
00375     KAction* specialPaste;
00376     KAction* insertCellCopy;
00377     KAction* find;
00378     KAction* replace;
00379 
00380     // navigation
00381     KAction* gotoCell;
00382     KAction* nextSheet;
00383     KAction* prevSheet;
00384     KAction* firstSheet;
00385     KAction* lastSheet;
00386 
00387     // misc
00388     KAction* styleDialog;
00389     KAction* autoSum;
00390     KSelectAction* formulaSelection;
00391     KAction* insertLink;
00392     KAction* removeLink;
00393     KAction* consolidate;
00394     KAction* goalSeek;
00395     KAction* subTotals;
00396     KAction* textToColumns;
00397     KAction* multipleOperations;
00398     KAction* createTemplate;
00399     KoPartSelectAction *insertPart;
00400     KAction* insertChartFrame;
00401     KAction* customList;
00402     KAction* spellChecking;
00403     KAction* internalTests;
00404     KAction* inspector;
00405 
00406     // settings
00407     KoZoomAction* viewZoom;
00408     KToggleAction* showStatusBar;
00409     KToggleAction* showTabBar;
00410     KToggleAction* showFormulaBar;
00411     KToggleAction* showCommentIndicator;
00412     KAction* preference;
00413 
00414     // running calculation
00415     KToggleAction* calcNone;
00416     KToggleAction* calcMin;
00417     KToggleAction* calcMax;
00418     KToggleAction* calcAverage;
00419     KToggleAction* calcCount;
00420     KToggleAction* calcSum;
00421     KToggleAction* calcCountA;
00422 };
00423 
00424 
00425 void ViewPrivate::initActions()
00426 {
00427   actions = new ViewActions;
00428 
00429   KActionCollection* ac = view->actionCollection();
00430 
00431   // -- cell formatting actions --
00432 
00433   actions->cellLayout = new KAction( i18n("Cell Format..."), "cell_layout",
00434       Qt::CTRL+ Qt::ALT+ Qt::Key_F, view, SLOT( layoutDlg() ), ac, "cellLayout" );
00435   actions->cellLayout->setToolTip( i18n("Set the cell formatting.") );
00436 
00437   actions->defaultFormat = new KAction( i18n("Default"),
00438       0, view, SLOT( defaultSelection() ), ac, "default" );
00439   actions->defaultFormat->setToolTip( i18n("Resets to the default format.") );
00440 
00441   actions->bold = new KToggleAction( i18n("Bold"), "text_bold",
00442       Qt::CTRL+Qt::Key_B, ac, "bold");
00443   QObject::connect( actions->bold, SIGNAL( toggled( bool) ),
00444       view, SLOT( bold( bool ) ) );
00445 
00446   actions->italic = new KToggleAction( i18n("Italic"), "text_italic",
00447       Qt::CTRL+Qt::Key_I, ac, "italic");
00448   QObject::connect( actions->italic, SIGNAL( toggled( bool) ),
00449       view, SLOT( italic( bool ) ) );
00450 
00451   actions->underline = new KToggleAction( i18n("Underline"), "text_under",
00452       Qt::CTRL+Qt::Key_U, ac, "underline");
00453   QObject::connect( actions->underline, SIGNAL( toggled( bool) ),
00454       view, SLOT( underline( bool ) ) );
00455 
00456   actions->strikeOut = new KToggleAction( i18n("Strike Out"), "text_strike",
00457       0, ac, "strikeout");
00458   QObject::connect( actions->strikeOut, SIGNAL( toggled( bool) ),
00459       view, SLOT( strikeOut( bool ) ) );
00460 
00461   actions->selectFont = new KFontAction( i18n("Select Font..."),
00462       0, ac, "selectFont" );
00463   QObject::connect( actions->selectFont, SIGNAL( activated( const QString& ) ),
00464       view, SLOT( fontSelected( const QString& ) ) );
00465 
00466   actions->selectFontSize = new KFontSizeAction( i18n("Select Font Size"),
00467       0, ac, "selectFontSize" );
00468   QObject::connect( actions->selectFontSize, SIGNAL( fontSizeChanged( int ) ),
00469       view, SLOT( fontSizeSelected( int ) ) );
00470 
00471   actions->fontSizeUp = new KAction( i18n("Increase Font Size"), "fontsizeup",
00472       0, view, SLOT( increaseFontSize() ), ac,  "increaseFontSize" );
00473 
00474   actions->fontSizeDown = new KAction( i18n("Decrease Font Size"), "fontsizedown",
00475       0, view, SLOT( decreaseFontSize() ), ac, "decreaseFontSize" );
00476 
00477   actions->textColor = new TKSelectColorAction( i18n("Text Color"),
00478       TKSelectColorAction::TextColor, view, SLOT( changeTextColor() ),
00479       ac, "textColor",true );
00480   actions->textColor->setDefaultColor(QColor());
00481 
00482   actions->alignLeft = new KToggleAction( i18n("Align Left"), "text_left",
00483       0, ac, "left");
00484   QObject::connect( actions->alignLeft, SIGNAL( toggled( bool ) ),
00485       view, SLOT( alignLeft( bool ) ) );
00486   actions->alignLeft->setExclusiveGroup( "Align" );
00487   actions->alignLeft->setToolTip(i18n("Left justify the cell contents."));
00488 
00489   actions->alignCenter = new KToggleAction( i18n("Align Center"), "text_center",
00490       0, ac, "center");
00491   QObject::connect( actions->alignCenter, SIGNAL( toggled( bool ) ),
00492       view, SLOT( alignCenter( bool ) ) );
00493   actions->alignCenter->setExclusiveGroup( "Align" );
00494   actions->alignCenter->setToolTip(i18n("Center the cell contents."));
00495 
00496   actions->alignRight = new KToggleAction( i18n("Align Right"), "text_right",
00497       0, ac, "right");
00498   QObject::connect( actions->alignRight, SIGNAL( toggled( bool ) ),
00499       view, SLOT( alignRight( bool ) ) );
00500   actions->alignRight->setExclusiveGroup( "Align" );
00501   actions->alignRight->setToolTip(i18n("Right justify the cell contents."));
00502 
00503   actions->alignTop = new KToggleAction( i18n("Align Top"), "text_top",
00504       0, ac, "top");
00505   QObject::connect( actions->alignTop, SIGNAL( toggled( bool ) ),
00506       view, SLOT( alignTop( bool ) ) );
00507   actions->alignTop->setExclusiveGroup( "Pos" );
00508   actions->alignTop->setToolTip(i18n("Align cell contents along the top of the cell."));
00509 
00510   actions->alignMiddle = new KToggleAction( i18n("Align Middle"), "middle",
00511       0, ac, "middle");
00512   QObject::connect( actions->alignMiddle, SIGNAL( toggled( bool ) ),
00513       view, SLOT( alignMiddle( bool ) ) );
00514   actions->alignMiddle->setExclusiveGroup( "Pos" );
00515   actions->alignMiddle->setToolTip(i18n("Align cell contents centered in the cell."));
00516 
00517   actions->alignBottom = new KToggleAction( i18n("Align Bottom"), "text_bottom",
00518       0, ac, "bottom");
00519   QObject::connect( actions->alignBottom, SIGNAL( toggled( bool ) ),
00520       view, SLOT( alignBottom( bool ) ) );
00521   actions->alignBottom->setExclusiveGroup( "Pos" );
00522   actions->alignBottom->setToolTip(i18n("Align cell contents along the bottom of the cell."));
00523 
00524   actions->wrapText = new KToggleAction( i18n("Wrap Text"), "multirow",
00525       0, ac, "multiRow" );
00526   QObject::connect( actions->wrapText, SIGNAL( toggled( bool ) ),
00527       view, SLOT( wrapText( bool ) ) );
00528   actions->wrapText->setToolTip(i18n("Make the cell text wrap onto multiple lines."));
00529 
00530   actions->verticalText = new KToggleAction( i18n("Vertical Text"),"vertical_text" ,
00531       0 ,ac, "verticaltext" );
00532   QObject::connect( actions->verticalText, SIGNAL( toggled( bool ) ),
00533       view, SLOT( verticalText( bool ) ) );
00534   actions->verticalText->setToolTip(i18n("Print cell contents vertically."));
00535 
00536   actions->increaseIndent = new KAction( i18n("Increase Indent"),
00537       QApplication::reverseLayout() ? "format_decreaseindent":"format_increaseindent",
00538       0, view, SLOT( increaseIndent() ), ac, "increaseindent" );
00539   actions->increaseIndent->setToolTip(i18n("Increase the indentation."));
00540 
00541   actions->decreaseIndent = new KAction( i18n("Decrease Indent"),
00542       QApplication::reverseLayout() ? "format_increaseindent" : "format_decreaseindent",
00543       0, view, SLOT( decreaseIndent() ), ac, "decreaseindent");
00544   actions->decreaseIndent->setToolTip(i18n("Decrease the indentation."));
00545 
00546   actions->changeAngle = new KAction( i18n("Change Angle..."),
00547       0, view, SLOT( changeAngle() ), ac, "changeangle" );
00548   actions->changeAngle->setToolTip(i18n("Change the angle that cell contents are printed."));
00549 
00550   actions->percent = new KToggleAction( i18n("Percent Format"), "percent",
00551       0, ac, "percent");
00552   QObject::connect( actions->percent, SIGNAL( toggled( bool ) ),
00553       view, SLOT( percent( bool ) ) );
00554   actions->percent->setToolTip(i18n("Set the cell formatting to look like a percentage."));
00555 
00556   actions->precplus = new KAction( i18n("Increase Precision"), "prec_plus",
00557       0, view, SLOT( precisionPlus() ), ac, "precplus");
00558   actions->precplus->setToolTip(i18n("Increase the decimal precision shown onscreen."));
00559 
00560   actions->precminus = new KAction( i18n("Decrease Precision"), "prec_minus",
00561       0, view, SLOT( precisionMinus() ), ac, "precminus");
00562   actions->precminus->setToolTip(i18n("Decrease the decimal precision shown onscreen."));
00563 
00564   actions->money = new KToggleAction( i18n("Money Format"), "money",
00565       0, ac, "money");
00566   QObject::connect( actions->money, SIGNAL( toggled( bool ) ),
00567       view, SLOT( moneyFormat( bool ) ) );
00568   actions->money->setToolTip(i18n("Set the cell formatting to look like your local currency."));
00569 
00570   actions->upper = new KAction( i18n("Upper Case"), "fontsizeup",
00571       0, view, SLOT( upper() ), ac, "upper" );
00572   actions->upper->setToolTip(i18n("Convert all letters to upper case."));
00573 
00574   actions->lower = new KAction( i18n("Lower Case"), "fontsizedown",
00575       0, view, SLOT( lower() ), ac, "lower" );
00576   actions->lower->setToolTip(i18n("Convert all letters to lower case."));
00577 
00578   actions->firstLetterUpper = new KAction( i18n("Convert First Letter to Upper Case"), "first_letter_upper",
00579       0, view, SLOT( firstLetterUpper() ),ac, "firstletterupper" );
00580   actions->firstLetterUpper->setToolTip(i18n("Capitalize the first letter."));
00581 
00582   actions->bgColor = new TKSelectColorAction( i18n("Background Color"),
00583       TKSelectColorAction::FillColor, ac, "backgroundColor", true );
00584   QObject::connect(actions->bgColor, SIGNAL( activated() ),
00585       view, SLOT( changeBackgroundColor() ) );
00586   actions->bgColor->setDefaultColor(QColor());
00587   actions->bgColor->setToolTip(i18n("Set the background color."));
00588 
00589   actions->borderLeft = new KAction( i18n("Border Left"), "border_left",
00590       0, view, SLOT( borderLeft() ), ac, "borderLeft" );
00591   actions->borderLeft->setToolTip(i18n("Set a left border to the selected area."));
00592 
00593   actions->borderRight = new KAction( i18n("Border Right"), "border_right",
00594       0, view, SLOT( borderRight() ), ac, "borderRight" );
00595   actions->borderRight->setToolTip(i18n("Set a right border to the selected area."));
00596 
00597   actions->borderTop = new KAction( i18n("Border Top"), "border_top",
00598       0, view, SLOT( borderTop() ), ac, "borderTop" );
00599   actions->borderTop->setToolTip(i18n("Set a top border to the selected area."));
00600 
00601   actions->borderBottom = new KAction( i18n("Border Bottom"), "border_bottom",
00602       0, view, SLOT( borderBottom() ), ac, "borderBottom" );
00603   actions->borderBottom->setToolTip(i18n("Set a bottom border to the selected area."));
00604 
00605   actions->borderAll = new KAction( i18n("All Borders"), "border_all",
00606       0, view, SLOT( borderAll() ), ac, "borderAll" );
00607   actions->borderAll->setToolTip(i18n("Set a border around all cells in the selected area."));
00608 
00609   actions->borderRemove = new KAction( i18n("Remove Borders"), "border_remove",
00610       0, view, SLOT( borderRemove() ), ac, "borderRemove" );
00611   actions->borderRemove->setToolTip(i18n("Remove all borders in the selected area."));
00612 
00613   actions->borderOutline = new KAction( i18n("Border Outline"), ("border_outline"),
00614       0, view, SLOT( borderOutline() ), ac, "borderOutline" );
00615   actions->borderOutline->setToolTip(i18n("Set a border to the outline of the selected area."));
00616 
00617   actions->borderColor = new TKSelectColorAction( i18n("Border Color"),
00618       TKSelectColorAction::LineColor, ac, "borderColor" );
00619   QObject::connect( actions->borderColor, SIGNAL( activated() ),
00620       view, SLOT( changeBorderColor() ) );
00621   actions->borderColor->setToolTip( i18n( "Select a new border color." ) );
00622 
00623   actions->selectStyle = new KSelectAction( i18n( "St&yle" ),
00624       0, ac, "stylemenu" );
00625   actions->selectStyle->setToolTip( i18n( "Apply a predefined style to the selected cells." ) );
00626   QObject::connect( actions->selectStyle, SIGNAL( activated( const QString & ) ),
00627       view, SLOT( styleSelected( const QString & ) ) );
00628 
00629   actions->createStyle = new KAction( i18n( "Create Style From Cell..." ),
00630       0, view, SLOT( createStyleFromCell()), ac, "createStyle" );
00631   actions->createStyle->setToolTip( i18n( "Create a new style based on the currently selected cell." ) );
00632 
00633   // -- cell operation actions --
00634 
00635   actions->editCell = new KAction( i18n("Modify Cell"),"cell_edit",
00636       Qt::CTRL+Qt::Key_M, view, SLOT( editCell() ), ac, "editCell" );
00637   actions->editCell->setToolTip(i18n("Edit the highlighted cell."));
00638 
00639   actions->insertCell = new KAction( i18n("Insert Cells..."), "insertcell",
00640       0, view, SLOT( slotInsert() ), ac, "insertCell" );
00641   actions->insertCell->setToolTip(i18n("Insert a blank cell into the spreadsheet."));
00642 
00643   actions->removeCell = new KAction( i18n("Remove Cells..."), "removecell",
00644       0, view, SLOT( slotRemove() ), ac, "removeCell" );
00645   actions->removeCell->setToolTip(i18n("Removes the current cell from the spreadsheet."));
00646 
00647   actions->deleteCell = new KAction( i18n("Delete"), "deletecell",
00648       0, view, SLOT( deleteSelection() ), ac, "delete" );
00649   actions->deleteCell->setToolTip(i18n("Delete all contents and formatting of the current cell."));
00650 
00651   actions->mergeCell = new KAction( i18n("Merge Cells"),"mergecell",
00652       0, view, SLOT( mergeCell() ), ac, "mergecell" );
00653   actions->mergeCell->setToolTip(i18n("Merge the selected region into one large cell."));
00654 
00655   actions->dissociateCell = new KAction( i18n("Dissociate Cells"),"dissociatecell",
00656       0, view, SLOT( dissociateCell() ), ac, "dissociatecell" );
00657   actions->dissociateCell->setToolTip(i18n("Unmerge the current cell."));
00658 
00659   actions->clearText = new KAction( i18n("Text"),
00660       0, view, SLOT( clearTextSelection() ), ac, "cleartext" );
00661   actions->clearText->setToolTip(i18n("Remove the contents of the current cell."));
00662 
00663   actions->conditional = new KAction( i18n("Conditional Cell Attributes..."),
00664       0, view, SLOT( conditional() ), ac, "conditional" );
00665   actions->conditional->setToolTip(i18n("Set cell format based on certain conditions."));
00666 
00667 
00668   actions->clearConditional = new KAction( i18n("Conditional Cell Attributes"),
00669       0, view, SLOT( clearConditionalSelection() ), ac, "clearconditional" );
00670   actions->clearConditional->setToolTip(i18n("Remove the conditional cell formatting."));
00671 
00672   actions->validity = new KAction( i18n("Validity..."),
00673       0, view, SLOT( validity() ), ac, "validity" );
00674   actions->validity->setToolTip(i18n("Set tests to confirm cell data is valid."));
00675 
00676   actions->clearValidity = new KAction( i18n("Validity"),
00677       0, view, SLOT( clearValiditySelection() ), ac, "clearvalidity" );
00678   actions->clearValidity->setToolTip(i18n("Remove the validity tests on this cell."));
00679 
00680   actions->addModifyComment = new KAction( i18n("&Add/Modify Comment..."),"comment",
00681       0, view, SLOT( addModifyComment() ), ac, "addmodifycomment" );
00682   actions->addModifyComment->setToolTip(i18n("Edit a comment for this cell."));
00683 
00684   actions->removeComment = new KAction( i18n("&Remove Comment"),"removecomment",
00685       0,  view, SLOT( removeComment() ), ac, "removecomment" );
00686   actions->removeComment->setToolTip(i18n("Remove this cell's comment."));
00687 
00688   actions->clearComment = new KAction( i18n("Comment"),
00689       0, view, SLOT( clearCommentSelection() ), ac, "clearcomment" );
00690   actions->clearComment->setToolTip(i18n("Remove this cell's comment."));
00691 
00692   // -- column & row actions --
00693 
00694   actions->resizeColumn = new KAction( i18n("Resize Column..."), "resizecol",
00695       0, view, SLOT( resizeColumn() ), ac, "resizeCol" );
00696   actions->resizeColumn->setToolTip(i18n("Change the width of a column."));
00697 
00698   actions->insertColumn = new KAction( i18n("Insert Columns"), "insert_table_col",
00699       0, view, SLOT( insertColumn() ), ac, "insertColumn" );
00700   actions->insertColumn->setToolTip(i18n("Inserts a new column into the spreadsheet."));
00701 
00702   actions->deleteColumn = new KAction( i18n("Delete Columns"), "delete_table_col",
00703       0, view, SLOT( deleteColumn() ), ac, "deleteColumn" );
00704   actions->deleteColumn->setToolTip(i18n("Removes a column from the spreadsheet."));
00705 
00706   actions->hideColumn = new KAction( i18n("Hide Columns"), "hide_table_column",
00707       0, view, SLOT( hideColumn() ), ac, "hideColumn" );
00708   actions->hideColumn->setToolTip(i18n("Hide the column from view."));
00709 
00710   actions->showColumn = new KAction( i18n("Show Columns..."), "show_table_column",
00711       0, view, SLOT( showColumn() ), ac, "showColumn" );
00712   actions->showColumn->setToolTip(i18n("Show hidden columns."));
00713 
00714   actions->equalizeColumn = new KAction( i18n("Equalize Column"), "adjustcol",
00715       0, view, SLOT( equalizeColumn() ), ac, "equalizeCol" );
00716   actions->equalizeColumn->setToolTip(i18n("Resizes selected columns to be the same size."));
00717 
00718   actions->showSelColumns = new KAction( i18n("Show Columns"), "show_sheet_column",
00719       0, view, SLOT( showSelColumns() ), ac, "showSelColumns" );
00720   actions->showSelColumns->setToolTip(i18n("Show hidden columns in the selection."));
00721   actions->showSelColumns->setEnabled(false);
00722 
00723   actions->resizeRow = new KAction( i18n("Resize Row..."), "resizerow",
00724       0, view, SLOT( resizeRow() ), ac, "resizeRow" );
00725   actions->resizeRow->setToolTip(i18n("Change the height of a row."));
00726 
00727   actions->insertRow = new KAction( i18n("Insert Rows"), "insert_table_row",
00728       0, view, SLOT( insertRow() ), ac, "insertRow" );
00729   actions->insertRow->setToolTip(i18n("Inserts a new row into the spreadsheet."));
00730 
00731   actions->deleteRow = new KAction( i18n("Delete Rows"), "delete_table_row",
00732       0, view, SLOT( deleteRow() ), ac, "deleteRow" );
00733   actions->deleteRow->setToolTip(i18n("Removes a row from the spreadsheet."));
00734 
00735   actions->hideRow = new KAction( i18n("Hide Rows"), "hide_table_row",
00736       0, view, SLOT( hideRow() ), ac, "hideRow" );
00737   actions->hideRow->setToolTip(i18n("Hide a row from view."));
00738 
00739   actions->showRow = new KAction( i18n("Show Rows..."), "show_table_row",
00740       0, view, SLOT( showRow() ), ac, "showRow" );
00741   actions->showRow->setToolTip(i18n("Show hidden rows."));
00742 
00743   actions->equalizeRow = new KAction( i18n("Equalize Row"), "adjustrow",
00744       0, view, SLOT( equalizeRow() ), ac, "equalizeRow" );
00745   actions->equalizeRow->setToolTip(i18n("Resizes selected rows to be the same size."));
00746 
00747   actions->showSelRows = new KAction( i18n("Show Rows"), "show_table_row",
00748       0, view, SLOT( showSelRows() ), ac, "showSelRows" );
00749   actions->showSelRows->setEnabled(false);
00750   actions->showSelRows->setToolTip(i18n("Show hidden rows in the selection."));
00751 
00752   actions->adjust = new KAction( i18n("Adjust Row && Column"),
00753       0, view, SLOT( adjust() ), ac, "adjust" );
00754   actions->adjust->setToolTip(i18n("Adjusts row/column size so that the contents will fit."));
00755 
00756   // -- sheet/workbook actions --
00757   actions->sheetProperties = new KAction( i18n("Sheet Properties"),
00758       0, view, SLOT( sheetProperties() ), ac, "sheetProperties" );
00759   actions->sheetProperties->setToolTip(i18n("Modify current sheet's properties."));
00760 
00761   actions->insertSheet = new KAction( i18n("Insert Sheet"),"inserttable",
00762       0, view, SLOT( insertSheet() ), ac, "insertSheet" );
00763   actions->insertSheet->setToolTip(i18n("Insert a new sheet."));
00764 
00765   // same action as insertSheet, but without 'insert' in the caption
00766   actions->menuInsertSheet = new KAction( i18n("&Sheet"),"inserttable",
00767       0, view, SLOT( insertSheet() ), ac, "menuInsertSheet" );
00768   actions->menuInsertSheet->setToolTip(i18n("Insert a new sheet."));
00769 
00770   actions->removeSheet = new KAction( i18n("Remove Sheet"), "delete_table",
00771       0, view, SLOT( removeSheet() ), ac, "removeSheet" );
00772   actions->removeSheet->setToolTip(i18n("Remove the active sheet."));
00773 
00774   actions->renameSheet=new KAction( i18n("Rename Sheet..."),
00775       0, view, SLOT( slotRename() ), ac, "renameSheet" );
00776   actions->renameSheet->setToolTip(i18n("Rename the active sheet."));
00777 
00778   actions->showSheet = new KAction(i18n("Show Sheet..."),
00779       0, view, SLOT( showSheet()), ac, "showSheet" );
00780   actions->showSheet->setToolTip(i18n("Show a hidden sheet."));
00781 
00782   actions->hideSheet = new KAction(i18n("Hide Sheet"),
00783       0, view, SLOT( hideSheet() ), ac, "hideSheet" );
00784   actions->hideSheet->setToolTip(i18n("Hide the active sheet."));
00785 
00786   actions->autoFormat = new KAction( i18n("AutoFormat..."),
00787       0, view, SLOT( sheetFormat() ), ac, "sheetFormat" );
00788   actions->autoFormat->setToolTip(i18n("Set the worksheet formatting."));
00789 
00790   actions->areaName = new KAction( i18n("Area Name..."),
00791       0, view, SLOT( setAreaName() ), ac, "areaname" );
00792   actions->areaName->setToolTip(i18n("Set a name for a region of the spreadsheet."));
00793 
00794   actions->showArea = new KAction( i18n("Show Area..."),
00795       0, view, SLOT( showAreaName() ), ac, "showArea" );
00796   actions->showArea->setToolTip(i18n("Display a named area."));
00797 
00798   actions->insertFunction = new KAction( i18n("&Function..."), "funct",
00799       0, view, SLOT( insertMathExpr() ), ac, "insertMathExpr" );
00800   actions->insertFunction->setToolTip(i18n("Insert math expression."));
00801 
00802   actions->insertSeries = new KAction( i18n("&Series..."),"series",
00803       0, view, SLOT( insertSeries() ), ac, "series");
00804   actions->insertSeries ->setToolTip(i18n("Insert a series."));
00805 
00806   actions->insertLink = new KAction( i18n("&Link..."), "insert_link",
00807       0, view, SLOT( insertHyperlink() ), ac, "insertHyperlink" );
00808   actions->insertLink->setToolTip(i18n("Insert an Internet hyperlink."));
00809 
00810   actions->removeLink = new KAction( i18n("&Remove Link"),
00811       0, view, SLOT( removeHyperlink() ), ac, "removeHyperlink" );
00812   actions->removeLink->setToolTip(i18n("Remove a link."));
00813 
00814   actions->insertSpecialChar = new KAction( i18n( "S&pecial Character..." ), "char",
00815       view, SLOT( insertSpecialChar() ), ac, "insertSpecialChar" );
00816   actions->insertSpecialChar->setToolTip( i18n( "Insert one or more symbols or letters not found on the keyboard." ) );
00817 
00818   actions->insertPart = new KoPartSelectAction( i18n("&Object"), "frame_query",
00819       view, SLOT( insertObject() ), ac, "insertPart");
00820   actions->insertPart->setToolTip(i18n("Insert an object from another program."));
00821 
00822   actions->insertChartFrame = new KAction( i18n("&Chart"), "insert_chart",
00823       0, view, SLOT( insertChart() ), ac, "insertChart" );
00824   actions->insertChartFrame->setToolTip(i18n("Insert a chart."));
00825 
00826 #ifndef QT_NO_SQL
00827   actions->insertFromDatabase = new KAction( i18n("From &Database..."),
00828       0, view, SLOT( insertFromDatabase() ),  ac, "insertFromDatabase");
00829   actions->insertFromDatabase->setToolTip(i18n("Insert data from a SQL database."));
00830 #endif
00831 
00832   actions->insertFromTextfile = new KAction( i18n("From &Text File..."),
00833       0, view,  SLOT( insertFromTextfile() ), ac, "insertFromTextfile");
00834   actions->insertFromTextfile->setToolTip(i18n("Insert data from a text file to the current cursor position/selection."));
00835 
00836   actions->insertFromClipboard = new KAction( i18n("From &Clipboard..."),
00837       0, view, SLOT( insertFromClipboard() ), ac, "insertFromClipboard");
00838   actions->insertFromClipboard->setToolTip(i18n("Insert CSV data from the clipboard to the current cursor position/selection."));
00839 
00840   actions->transform = new KAction( i18n("Transform Object..."), "rotate",
00841       0, view, SLOT( transformPart() ), ac, "transform" );
00842   actions->transform->setToolTip(i18n("Rotate the contents of the cell."));
00843   actions->transform->setEnabled( FALSE );
00844 
00845   actions->sort = new KAction( i18n("&Sort..."),
00846       0, view, SLOT( sort() ), ac, "sort" );
00847   actions->sort->setToolTip(i18n("Sort a group of cells."));
00848 
00849   actions->sortDec = new KAction( i18n("Sort &Decreasing"), "sort_decrease",
00850       0, view, SLOT( sortDec() ), ac, "sortDec" );
00851   actions->sortDec->setToolTip(i18n("Sort a group of cells in decreasing (last to first) order."));
00852 
00853   actions->sortInc = new KAction( i18n("Sort &Increasing"), "sort_incr",
00854       0, view, SLOT( sortInc() ), ac, "sortInc" );
00855   actions->sortInc->setToolTip(i18n("Sort a group of cells in ascending (first to last) order."));
00856 
00857   actions->paperLayout = new KAction( i18n("Page Layout..."),
00858       0, view, SLOT( paperLayoutDlg() ), ac, "paperLayout" );
00859   actions->paperLayout->setToolTip(i18n("Specify the layout of the spreadsheet for a printout."));
00860 
00861   actions->definePrintRange = new KAction( i18n("Define Print Range"),
00862       0, view, SLOT( definePrintRange() ), ac, "definePrintRange" );
00863   actions->definePrintRange->setToolTip(i18n("Define the print range in the current sheet."));
00864 
00865   actions->resetPrintRange = new KAction( i18n("Reset Print Range"),
00866       0, view, SLOT( resetPrintRange() ), ac, "resetPrintRange" );
00867   actions->definePrintRange->setToolTip(i18n("Define the print range in the current sheet."));
00868 
00869   actions->showPageBorders = new KToggleAction( i18n("Show Page Borders"),
00870       0, ac, "showPageBorders");
00871   actions->showPageBorders->setCheckedState(i18n("Hide Page Borders"));
00872   QObject::connect( actions->showPageBorders, SIGNAL( toggled( bool ) ),
00873       view, SLOT( togglePageBorders( bool ) ) );
00874   actions->showPageBorders->setToolTip( i18n( "Show on the spreadsheet where the page borders will be." ) );
00875 
00876   actions->recalcWorksheet = new KAction( i18n("Recalculate Sheet"),
00877       Qt::SHIFT + Qt::Key_F9, view, SLOT( recalcWorkSheet() ), ac, "RecalcWorkSheet" );
00878   actions->recalcWorksheet->setToolTip(i18n("Recalculate the value of every cell in the current worksheet."));
00879 
00880   actions->recalcWorkbook = new KAction( i18n("Recalculate Workbook"),
00881       Qt::Key_F9, view, SLOT( recalcWorkBook() ), ac, "RecalcWorkBook" );
00882   actions->recalcWorkbook->setToolTip(i18n("Recalculate the value of every cell in all worksheets."));
00883 
00884   actions->protectSheet = new KToggleAction( i18n( "Protect &Sheet..." ),
00885       0, ac, "protectSheet" );
00886   actions->protectSheet->setToolTip( i18n( "Protect the sheet from being modified." ) );
00887   QObject::connect( actions->protectSheet, SIGNAL( toggled( bool ) ),
00888       view, SLOT( toggleProtectSheet( bool ) ) );
00889 
00890   actions->protectDoc = new KToggleAction( i18n( "Protect &Doc..." ),
00891       0, ac, "protectDoc" );
00892   actions->protectDoc->setToolTip( i18n( "Protect the document from being modified." ) );
00893   QObject::connect( actions->protectDoc, SIGNAL( toggled( bool ) ),
00894       view, SLOT( toggleProtectDoc( bool ) ) );
00895 
00896   // -- editing actions --
00897 
00898   actions->copy = KStdAction::copy( view, SLOT( copySelection() ), ac, "copy" );
00899   actions->copy->setToolTip(i18n("Copy the cell object to the clipboard."));
00900 
00901   actions->paste = KStdAction::paste( view, SLOT( paste() ), ac, "paste" );
00902   actions->paste->setToolTip(i18n("Paste the contents of the clipboard at the cursor."));
00903 
00904   actions->cut = KStdAction::cut( view, SLOT( cutSelection() ), ac, "cut" );
00905   actions->cut->setToolTip(i18n("Move the cell object to the clipboard."));
00906 
00907   actions->specialPaste = new KAction( i18n("Special Paste..."), "special_paste",
00908       0, view, SLOT( specialPaste() ), ac, "specialPaste" );
00909   actions->specialPaste->setToolTip(i18n("Paste the contents of the clipboard with special options."));
00910 
00911   actions->insertCellCopy = new KAction( i18n("Paste with Insertion"), "insertcellcopy",
00912       0, view, SLOT( slotInsertCellCopy() ), ac, "insertCellCopy" );
00913   actions->insertCellCopy->setToolTip(i18n("Inserts a cell from the clipboard into the spreadsheet."));
00914 
00915   actions->find = KStdAction::find( view, SLOT(find()), ac );
00916   /*actions->findNext =*/ KStdAction::findNext( view, SLOT( findNext() ), ac );
00917   /*actions->findPrevious =*/ KStdAction::findPrev( view, SLOT( findPrevious() ), ac );
00918 
00919   actions->replace = KStdAction::replace( view, SLOT(replace()), ac );
00920 
00921   actions->fillRight = new KAction( i18n( "&Right" ), 0,
00922       0, view, SLOT( fillRight() ), ac, "fillRight" );
00923 
00924   actions->fillLeft = new KAction( i18n( "&Left" ), 0,
00925       0, view, SLOT( fillLeft() ), ac, "fillLeft" );
00926 
00927   actions->fillDown = new KAction( i18n( "&Down" ), 0,
00928       0, view, SLOT( fillDown() ), ac, "fillDown" );
00929 
00930   actions->fillUp = new KAction( i18n( "&Up" ), 0,
00931       0, view, SLOT( fillUp() ), ac, "fillUp" );
00932 
00933   // -- misc actions --
00934 
00935   actions->styleDialog = new KAction( i18n( "Style Manager" ),
00936       0, view, SLOT( styleDialog() ), ac, "styles" );
00937   actions->styleDialog->setToolTip( i18n( "Edit and organize cell styles." ) );
00938 
00939   actions->autoSum = new KAction( i18n("Autosum"), "black_sum",
00940       0, view, SLOT( autoSum() ), ac, "autoSum" );
00941   actions->autoSum->setToolTip(i18n("Insert the 'sum' function"));
00942 
00943   actions->spellChecking = KStdAction::spelling( view, SLOT( extraSpelling() ),
00944       ac, "spelling" );
00945   actions->spellChecking->setToolTip(i18n("Check the spelling."));
00946 
00947   actions->formulaSelection = new KSelectAction(i18n("Formula Selection"),
00948       0, ac, "formulaSelection");
00949   actions->formulaSelection->setToolTip(i18n("Insert a function."));
00950   QStringList lst;
00951   lst.append( "SUM");
00952   lst.append( "AVERAGE");
00953   lst.append( "IF");
00954   lst.append( "COUNT");
00955   lst.append( "MIN");
00956   lst.append( "MAX");
00957   lst.append( i18n("Others...") );
00958   ((KSelectAction*) actions->formulaSelection)->setItems( lst );
00959   actions->formulaSelection->setComboWidth( 80 );
00960   actions->formulaSelection->setCurrentItem(0);
00961   QObject::connect( actions->formulaSelection, SIGNAL( activated( const QString& ) ),
00962       view, SLOT( formulaSelection( const QString& ) ) );
00963 
00964   actions->viewZoom = new KoZoomAction( i18n( "Zoom" ), "viewmag", 0, ac, "view_zoom" );
00965   QObject::connect( actions->viewZoom, SIGNAL( zoomChanged( const QString & ) ),
00966       view, SLOT( viewZoom( const QString & ) ) );
00967 
00968   actions->consolidate = new KAction( i18n("&Consolidate..."),
00969       0, view, SLOT( consolidate() ), ac, "consolidate" );
00970   actions->consolidate->setToolTip(i18n("Create a region of summary data from a group of similar regions."));
00971 
00972   actions->goalSeek = new KAction( i18n("&Goal Seek..."),
00973       0, view, SLOT( goalSeek() ), ac, "goalSeek" );
00974   actions->goalSeek->setToolTip( i18n("Repeating calculation to find a specific value.") );
00975 
00976   actions->subTotals = new KAction( i18n("&Subtotals..."),
00977       0, view, SLOT( subtotals() ), ac, "subtotals" );
00978   actions->subTotals->setToolTip( i18n("Create different kind of subtotals to a list or database.") );
00979 
00980   actions->textToColumns = new KAction( i18n("&Text to Columns..."),
00981       0, view, SLOT( textToColumns() ), ac, "textToColumns" );
00982   actions->textToColumns->setToolTip( i18n("Expand the content of cells to multiple columns.") );
00983 
00984   actions->multipleOperations = new KAction( i18n("&Multiple Operations..."),
00985       0, view, SLOT( multipleOperations() ), ac, "multipleOperations" );
00986   actions->multipleOperations->setToolTip( i18n("Apply the same formula to various cells using different values for the parameter.") );
00987 
00988   actions->createTemplate = new KAction( i18n( "&Create Template From Document..." ),
00989       0, view, SLOT( createTemplate() ), ac, "createTemplate" );
00990 
00991   actions->customList = new KAction( i18n("Custom Lists..."),
00992       0, view, SLOT( sortList() ), ac, "sortlist" );
00993   actions->customList->setToolTip(i18n("Create custom lists for sorting or autofill."));
00994 
00995   // -- navigation actions --
00996 
00997   actions->gotoCell = new KAction( i18n("Goto Cell..."),"goto",
00998       0, view, SLOT( gotoCell() ), ac, "gotoCell" );
00999   actions->gotoCell->setToolTip(i18n("Move to a particular cell."));
01000 
01001   actions->nextSheet = new KAction( i18n("Next Sheet"), "forward",
01002       Qt::CTRL+Qt::Key_PageDown, view, SLOT( nextSheet() ), ac, "nextSheet");
01003   actions->nextSheet->setToolTip(i18n("Move to the next sheet."));
01004 
01005   actions->prevSheet = new KAction( i18n("Previous Sheet"), "back",
01006       Qt::CTRL+Qt::Key_PageUp, view, SLOT( previousSheet() ), ac, "previousSheet");
01007   actions->prevSheet->setToolTip(i18n("Move to the previous sheet."));
01008 
01009   actions->firstSheet = new KAction( i18n("First Sheet"), "start",
01010       0, view, SLOT( firstSheet() ), ac, "firstSheet");
01011   actions->firstSheet->setToolTip(i18n("Move to the first sheet."));
01012 
01013   actions->lastSheet = new KAction( i18n("Last Sheet"), "finish",
01014       0, view, SLOT( lastSheet() ), ac, "lastSheet");
01015   actions->lastSheet->setToolTip(i18n("Move to the last sheet."));
01016 
01017   // -- settings actions --
01018 
01019   actions->showStatusBar = new KToggleAction( i18n("Show Status Bar"),
01020       0, ac, "showStatusBar" );
01021   actions->showStatusBar->setCheckedState(i18n("Hide Status Bar"));
01022   QObject::connect( actions->showStatusBar, SIGNAL( toggled( bool ) ),
01023       view, SLOT( showStatusBar( bool ) ) );
01024   actions->showStatusBar->setToolTip(i18n("Show the status bar."));
01025 
01026   actions->showTabBar = new KToggleAction( i18n("Show Tab Bar"),
01027       0, ac, "showTabBar" );
01028   actions->showTabBar->setCheckedState(i18n("Hide Tab Bar"));
01029   QObject::connect( actions->showTabBar, SIGNAL( toggled( bool ) ),
01030       view, SLOT( showTabBar( bool ) ) );
01031   actions->showTabBar->setToolTip(i18n("Show the tab bar."));
01032 
01033   actions->showFormulaBar = new KToggleAction( i18n("Show Formula Bar"),
01034       0, ac, "showFormulaBar" );
01035   actions->showFormulaBar->setCheckedState(i18n("Hide Formula Bar"));
01036   QObject::connect( actions->showFormulaBar, SIGNAL( toggled( bool ) ),
01037       view, SLOT( showFormulaBar( bool ) ) );
01038   actions->showFormulaBar->setToolTip(i18n("Show the formula bar."));
01039 
01040   actions->showCommentIndicator = new KToggleAction( i18n("Show Comment Indicator"),
01041       0, ac, "showCommentIndicator" );
01042   actions->showCommentIndicator->setCheckedState(i18n("Hide Comment Indicator"));
01043   QObject::connect( actions->showCommentIndicator, SIGNAL( toggled( bool ) ),
01044       view, SLOT( showCommentIndicator( bool ) ) );
01045   actions->showCommentIndicator->setToolTip(i18n("Show indicator for cells with comments."));
01046 
01047   actions->preference = new KAction( i18n("Configure KSpread..."),"configure",
01048       0, view, SLOT( preference() ), ac, "preference" );
01049   actions->preference->setToolTip(i18n("Set various KSpread options."));
01050 
01051   // -- running calculation actions --
01052 
01053   actions->calcNone = new KToggleAction( i18n("None"), 0, ac, "menu_none");
01054   QObject::connect( actions->calcNone, SIGNAL( toggled( bool ) ),
01055       view, SLOT( menuCalc( bool ) ) );
01056   actions->calcNone->setExclusiveGroup( "Calc" );
01057   actions->calcNone->setToolTip(i18n("No calculation"));
01058 
01059   actions->calcSum = new KToggleAction( i18n("Sum"), 0, ac, "menu_sum");
01060   QObject::connect( actions->calcSum, SIGNAL( toggled( bool ) ),
01061       view, SLOT( menuCalc( bool ) ) );
01062   actions->calcSum->setExclusiveGroup( "Calc" );
01063   actions->calcSum->setToolTip(i18n("Calculate using sum."));
01064 
01065   actions->calcMin = new KToggleAction( i18n("Min"), 0, ac, "menu_min");
01066   QObject::connect( actions->calcMin, SIGNAL( toggled( bool ) ),
01067       view, SLOT( menuCalc( bool ) ) );
01068   actions->calcMin->setExclusiveGroup( "Calc" );
01069   actions->calcMin->setToolTip(i18n("Calculate using minimum."));
01070 
01071   actions->calcMax = new KToggleAction( i18n("Max"), 0, ac, "menu_max");
01072   QObject::connect( actions->calcMax, SIGNAL( toggled( bool ) ),
01073       view, SLOT( menuCalc( bool ) ) );
01074   actions->calcMax->setExclusiveGroup( "Calc" );
01075   actions->calcMax->setToolTip(i18n("Calculate using maximum."));
01076 
01077   actions->calcAverage = new KToggleAction( i18n("Average"), 0, ac, "menu_average");
01078   QObject::connect( actions->calcAverage, SIGNAL( toggled( bool ) ),
01079       view, SLOT( menuCalc( bool ) ) );
01080   actions->calcAverage->setExclusiveGroup( "Calc" );
01081   actions->calcAverage->setToolTip(i18n("Calculate using average."));
01082 
01083   actions->calcCount = new KToggleAction( i18n("Count"), 0, ac, "menu_count");
01084   QObject::connect( actions->calcCount, SIGNAL( toggled( bool ) ),
01085       view, SLOT( menuCalc( bool ) ) );
01086   actions->calcCount->setExclusiveGroup( "Calc" );
01087   actions->calcCount->setToolTip(i18n("Calculate using the count."));
01088 
01089   actions->calcCountA = new KToggleAction( i18n("CountA"), 0, ac, "menu_counta");
01090   QObject::connect( actions->calcCountA, SIGNAL( toggled( bool ) ),
01091       view, SLOT( menuCalc( bool ) ) );
01092   actions->calcCountA->setExclusiveGroup( "Calc" );
01093   actions->calcCountA->setToolTip(i18n("Calculate using the countA."));
01094 
01095   // -- special action, only for developers --
01096 
01097   actions->internalTests = new KAction( i18n("Run Internal Tests..."), "internalTests",
01098       Qt::CTRL+ Qt::SHIFT + Qt::Key_T, view, SLOT( runInternalTests() ), ac, "internalTests" );
01099   actions->inspector = new KAction( i18n("Run Inspector..."), "inspector",
01100       Qt::CTRL+ Qt::SHIFT + Qt::Key_I, view, SLOT( runInspector() ), ac, "inspector" );
01101 }
01102 
01103 void ViewPrivate::adjustActions( bool mode )
01104 {
01105   actions->replace->setEnabled( mode );
01106   actions->insertSeries->setEnabled( mode );
01107   actions->insertLink->setEnabled( mode );
01108   actions->insertSpecialChar->setEnabled( mode );
01109   actions->insertFunction->setEnabled( mode );
01110   actions->removeComment->setEnabled( mode );
01111   actions->decreaseIndent->setEnabled( mode );
01112   actions->bold->setEnabled( mode );
01113   actions->italic->setEnabled( mode );
01114   actions->underline->setEnabled( mode );
01115   actions->strikeOut->setEnabled( mode );
01116   actions->percent->setEnabled( mode );
01117   actions->precplus->setEnabled( mode );
01118   actions->precminus->setEnabled( mode );
01119   actions->money->setEnabled( mode );
01120   actions->alignLeft->setEnabled( mode );
01121   actions->alignCenter->setEnabled( mode );
01122   actions->alignRight->setEnabled( mode );
01123   actions->alignTop->setEnabled( mode );
01124   actions->alignMiddle->setEnabled( mode );
01125   actions->alignBottom->setEnabled( mode );
01126   actions->paste->setEnabled( mode );
01127   actions->cut->setEnabled( mode );
01128   actions->specialPaste->setEnabled( mode );
01129   actions->deleteCell->setEnabled( mode );
01130   actions->clearText->setEnabled( mode );
01131   actions->clearComment->setEnabled( mode );
01132   actions->clearValidity->setEnabled( mode );
01133   actions->clearConditional->setEnabled( mode );
01134   actions->recalcWorkbook->setEnabled( mode );
01135   actions->recalcWorksheet->setEnabled( mode );
01136   actions->adjust->setEnabled( mode );
01137   actions->editCell->setEnabled( mode );
01138   actions->paperLayout->setEnabled( mode );
01139   actions->styleDialog->setEnabled( mode );
01140   actions->definePrintRange->setEnabled( mode );
01141   actions->resetPrintRange->setEnabled( mode );
01142   actions->insertFromDatabase->setEnabled( mode );
01143   actions->insertFromTextfile->setEnabled( mode );
01144   actions->insertFromClipboard->setEnabled( mode );
01145   actions->conditional->setEnabled( mode );
01146   actions->validity->setEnabled( mode );
01147   actions->goalSeek->setEnabled( mode );
01148   actions->subTotals->setEnabled( mode );
01149   actions->multipleOperations->setEnabled( mode );
01150   actions->textToColumns->setEnabled( mode );
01151   actions->consolidate->setEnabled( mode );
01152   actions->insertCellCopy->setEnabled( mode );
01153   actions->wrapText->setEnabled( mode );
01154   actions->selectFont->setEnabled( mode );
01155   actions->selectFontSize->setEnabled( mode );
01156   actions->deleteColumn->setEnabled( mode );
01157   actions->hideColumn->setEnabled( mode );
01158   actions->showColumn->setEnabled( mode );
01159   actions->showSelColumns->setEnabled( mode );
01160   actions->insertColumn->setEnabled( mode );
01161   actions->deleteRow->setEnabled( mode );
01162   actions->insertRow->setEnabled( mode );
01163   actions->hideRow->setEnabled( mode );
01164   actions->showRow->setEnabled( mode );
01165   actions->showSelRows->setEnabled( mode );
01166   actions->formulaSelection->setEnabled( mode );
01167   actions->textColor->setEnabled( mode );
01168   actions->bgColor->setEnabled( mode );
01169   actions->cellLayout->setEnabled( mode );
01170   actions->borderLeft->setEnabled( mode );
01171   actions->borderRight->setEnabled( mode );
01172   actions->borderTop->setEnabled( mode );
01173   actions->borderBottom->setEnabled( mode );
01174   actions->borderAll->setEnabled( mode );
01175   actions->borderOutline->setEnabled( mode );
01176   actions->borderRemove->setEnabled( mode );
01177   actions->borderColor->setEnabled( mode );
01178   actions->removeSheet->setEnabled( mode );
01179   actions->autoSum->setEnabled( mode );
01180   actions->defaultFormat->setEnabled( mode );
01181   actions->areaName->setEnabled( mode );
01182   actions->resizeRow->setEnabled( mode );
01183   actions->resizeColumn->setEnabled( mode );
01184   actions->fontSizeUp->setEnabled( mode );
01185   actions->fontSizeDown->setEnabled( mode );
01186   actions->upper->setEnabled( mode );
01187   actions->lower->setEnabled( mode );
01188   actions->equalizeRow->setEnabled( mode );
01189   actions->equalizeColumn->setEnabled( mode );
01190   actions->verticalText->setEnabled( mode );
01191   actions->addModifyComment->setEnabled( mode );
01192   actions->removeComment->setEnabled( mode );
01193   actions->insertCell->setEnabled( mode );
01194   actions->removeCell->setEnabled( mode );
01195   actions->changeAngle->setEnabled( mode );
01196   actions->dissociateCell->setEnabled( mode );
01197   actions->increaseIndent->setEnabled( mode );
01198   actions->decreaseIndent->setEnabled( mode );
01199   actions->spellChecking->setEnabled( mode );
01200   actions->calcMin->setEnabled( mode );
01201   actions->calcMax->setEnabled( mode );
01202   actions->calcAverage->setEnabled( mode );
01203   actions->calcCount->setEnabled( mode );
01204   actions->calcCountA->setEnabled( mode );
01205   actions->calcSum->setEnabled( mode );
01206   actions->calcNone->setEnabled( mode );
01207   actions->insertPart->setEnabled( mode );
01208   actions->createStyle->setEnabled( mode );
01209   actions->selectStyle->setEnabled( mode );
01210 
01211   actions->autoFormat->setEnabled( false );
01212   actions->sort->setEnabled( false );
01213   actions->mergeCell->setEnabled( false );
01214   actions->insertChartFrame->setEnabled( false );
01215   actions->sortDec->setEnabled( false );
01216   actions->sortInc->setEnabled( false );
01217   actions->transform->setEnabled( false );
01218 
01219   actions->fillRight->setEnabled( false );
01220   actions->fillLeft->setEnabled( false );
01221   actions->fillUp->setEnabled( false );
01222   actions->fillDown->setEnabled( false );
01223 
01224   if ( mode && !view->doc()->map()->isProtected() )
01225     actions->renameSheet->setEnabled( true );
01226   else
01227     actions->renameSheet->setEnabled( false );
01228 
01229   actions->showStatusBar->setChecked( view->doc()->showStatusBar() );
01230   actions->showTabBar->setChecked( view->doc()->showTabBar() );
01231   actions->showFormulaBar->setChecked( view->doc()->showFormulaBar() );
01232   actions->showCommentIndicator->setChecked( view->doc()->showCommentIndicator() );
01233 
01234   formulaButton->setEnabled( mode );
01235 
01236   if ( activeSheet )
01237     view->canvasWidget()->gotoLocation( selectionInfo->marker(), activeSheet );
01238 }
01239 
01240 void ViewPrivate::adjustActions( KSpreadSheet* sheet, KSpreadCell* cell )
01241 {
01242   QRect selection = selectionInfo->selection();
01243   if ( sheet->isProtected() && !cell->isDefault() && cell->notProtected( cell->column(), cell->row() ) )
01244   {
01245     if ( ( selection.width() > 1 ) || ( selection.height() > 1 ) )
01246     {
01247       if ( actions->bold->isEnabled() )
01248         adjustActions( false );
01249     }
01250     else
01251     {
01252       if ( !actions->bold->isEnabled() )
01253         adjustActions( true );
01254     }
01255   }
01256   else if ( sheet->isProtected() )
01257   {
01258     if ( actions->bold->isEnabled() )
01259       adjustActions( false );
01260   }
01261 }
01262 
01263 void ViewPrivate::adjustWorkbookActions( bool mode )
01264 {
01265   actions->hideSheet->setEnabled( mode );
01266   actions->showSheet->setEnabled( mode );
01267   actions->insertSheet->setEnabled( mode );
01268   actions->menuInsertSheet->setEnabled( mode );
01269   actions->removeSheet->setEnabled( mode );
01270 
01271   if ( mode )
01272   {
01273     if ( activeSheet && !activeSheet->isProtected() )
01274     {
01275       bool state = ( view->doc()->map()->visibleSheets().count() > 1 );
01276       actions->removeSheet->setEnabled( state );
01277       actions->hideSheet->setEnabled( state );
01278     }
01279     actions->showSheet->setEnabled( view->doc()->map()->hiddenSheets().count() > 0 );
01280     actions->renameSheet->setEnabled( activeSheet && !activeSheet->isProtected() );
01281   }
01282 }
01283 
01284 // TODO this should be merged with adjustActions
01285 void ViewPrivate::updateButton( KSpreadCell *cell, int column, int row)
01286 {
01287     toolbarLock = TRUE;
01288 
01289     // workaround for bug #59291 (crash upon starting from template)
01290     // certain Qt and Fontconfig combination fail miserably if can not
01291     // find the font name (e.g. not installed in the system)
01292     QStringList fontList;
01293     KFontChooser::getFontList( fontList, 0 );
01294     QString fontFamily = cell->textFontFamily( column,row );
01295     for ( QStringList::Iterator it = fontList.begin(); it != fontList.end(); ++it )
01296       if ((*it).lower() == fontFamily.lower())
01297       {
01298         actions->selectFont->setFont( fontFamily );
01299         break;
01300       }
01301 
01302     actions->selectFontSize->setFontSize( cell->textFontSize( column, row ) );
01303     actions->bold->setChecked( cell->textFontBold( column, row ) );
01304     actions->italic->setChecked( cell->textFontItalic(  column, row) );
01305     actions->underline->setChecked( cell->textFontUnderline( column, row ) );
01306     actions->strikeOut->setChecked( cell->textFontStrike( column, row ) );
01307 
01308     actions->alignLeft->setChecked( cell->align( column, row ) == KSpreadFormat::Left );
01309     actions->alignCenter->setChecked( cell->align( column, row ) == KSpreadFormat::Center );
01310     actions->alignRight->setChecked( cell->align( column, row ) == KSpreadFormat::Right );
01311 
01312     actions->alignTop->setChecked( cell->alignY( column, row ) == KSpreadFormat::Top );
01313     actions->alignMiddle->setChecked( cell->alignY( column, row ) == KSpreadFormat::Middle );
01314     actions->alignBottom->setChecked( cell->alignY( column, row ) == KSpreadFormat::Bottom );
01315 
01316     actions->verticalText->setChecked( cell->verticalText( column,row ) );
01317 
01318     actions->wrapText->setChecked( cell->multiRow( column,row ) );
01319 
01320     FormatType ft = cell->formatType();
01321     actions->percent->setChecked( ft == Percentage_format );
01322     actions->money->setChecked( ft == Money_format );
01323 
01324     if ( activeSheet && !activeSheet->isProtected() )
01325       actions->removeComment->setEnabled( !cell->comment(column,row).isEmpty() );
01326 
01327     if ( activeSheet && !activeSheet->isProtected() )
01328       actions->decreaseIndent->setEnabled( cell->getIndent( column, row ) > 0.0 );
01329 
01330     toolbarLock = FALSE;
01331     if ( activeSheet )
01332       adjustActions( activeSheet, cell );
01333 }
01334 
01335 
01336 
01337 QButton* ViewPrivate::newIconButton( const char *_file, bool _kbutton, QWidget *_parent )
01338 {
01339   if ( _parent == 0L )
01340     _parent = view;
01341 
01342   QButton *pb;
01343   if ( !_kbutton )
01344     pb = new QPushButton( _parent );
01345   else
01346     pb = new QToolButton( _parent );
01347   pb->setPixmap( QPixmap( KSBarIcon(_file) ) );
01348 
01349   return pb;
01350 }
01351 
01352 /*****************************************************************************
01353  *
01354  * KSpreadView
01355  *
01356  *****************************************************************************/
01357 
01358 KSpreadView::KSpreadView( QWidget *_parent, const char *_name,
01359     KSpreadDoc *_doc )
01360   : KoView( _doc, _parent, _name )
01361 {
01362     ElapsedTime et( "KSpreadView constructor" );
01363     kdDebug(36001) << "sizeof(KSpreadCell)=" << sizeof(KSpreadCell) <<endl;
01364 
01365     d = new ViewPrivate;
01366     d->view = this;
01367     d->doc = _doc;
01368 
01369     d->dcop = 0;
01370 
01371     d->activeSheet = 0;
01372 
01373     d->toolbarLock = false;
01374     d->loading = false;
01375 
01376     d->selectionInfo = new KSpreadSelection( this );
01377 
01378     d->findOptions = 0;
01379     d->findLeftColumn = 0;
01380     d->findRightColumn = 0;
01381     d->find = 0;
01382     d->replace = 0;
01383 
01384     d->popupMenuFirstToolId = 0;
01385     d->popupMenu   = 0;
01386     d->popupColumn = 0;
01387     d->popupRow    = 0;
01388     d->popupChild   = 0;
01389     d->popupListChoose = 0;
01390     d->popupChildObject = 0;
01391 
01392     // spell-check context
01393     d->spell.kspell = 0;
01394     d->spell.macroCmdSpellCheck = 0;
01395     d->spell.firstSpellSheet = 0;
01396     d->spell.currentSpellSheet = 0;
01397     d->spell.currentCell = 0;
01398     d->spell.spellStartCellX = 0;
01399     d->spell.spellStartCellY = 0;
01400     d->spell.spellEndCellX   = 0;
01401     d->spell.spellEndCellY   = 0;
01402     d->spell.spellCheckSelection = false;
01403 
01404     d->insertHandler = 0L;
01405     d->specialCharDlg = 0;
01406 
01407     setInstance( KSpreadFactory::global() );
01408     if ( doc()->isReadWrite() )
01409       setXMLFile( "kspread.rc" );
01410     else
01411       setXMLFile( "kspread_readonly.rc" );
01412 
01413     // build the DCOP object
01414     dcopObject();
01415 
01416     connect( doc()->commandHistory(), SIGNAL( commandExecuted() ),
01417         this, SLOT( commandExecuted() ) );
01418 
01419     // GUI Initializations
01420     initView();
01421 
01422     d->initActions();
01423 
01424 
01425     // Handler for moving and resizing embedded parts
01426     ContainerHandler* h = new ContainerHandler( this, d->canvas );
01427     connect( h, SIGNAL( popupMenu( KoChild*, const QPoint& ) ), this, SLOT( popupChildMenu( KoChild*, const QPoint& ) ) );
01428 
01429 
01430     connect( this, SIGNAL( childSelected( KoDocumentChild* ) ),
01431              this, SLOT( slotChildSelected( KoDocumentChild* ) ) );
01432     connect( this, SIGNAL( childUnselected( KoDocumentChild* ) ),
01433              this, SLOT( slotChildUnselected( KoDocumentChild* ) ) );
01434     // If a selected part becomes active this is like it is deselected
01435     // just before.
01436     connect( this, SIGNAL( childActivated( KoDocumentChild* ) ),
01437              this, SLOT( slotChildUnselected( KoDocumentChild* ) ) );
01438 
01439     QObject::connect( doc()->map(), SIGNAL( sig_addSheet( KSpreadSheet* ) ), SLOT( slotAddSheet( KSpreadSheet* ) ) );
01440 
01441     QObject::connect( doc(), SIGNAL( sig_refreshView(  ) ), this, SLOT( slotRefreshView() ) );
01442 
01443     QObject::connect( doc(), SIGNAL( sig_refreshLocale() ), this, SLOT( refreshLocale()));
01444 
01445     QObject::connect( doc(), SIGNAL( sig_addAreaName( const QString & ) ), d->posWidget, SLOT( slotAddAreaName( const QString & ) ) );
01446 
01447     QObject::connect( doc(), SIGNAL( sig_removeAreaName( const QString & ) ), d->posWidget, SLOT( slotRemoveAreaName( const QString & ) ) );
01448 
01449     QObject::connect( doc(), SIGNAL( damagesFlushed( const QValueList<KSpread::Damage*>& ) ),
01450         this, SLOT( handleDamages( const QValueList<KSpread::Damage*>& ) ) );
01451 
01452     KoView::setZoom( doc()->zoomedResolutionY() /* KoView only supports one zoom */ ); // initial value
01453     //when kspread is embedded into konqueror apply a zoom=100
01454     //in konqueror we can't change zoom -- ### TODO ?
01455     if (!doc()->isReadWrite())
01456     {
01457         setZoom( 100, true );
01458     }
01459 
01460     viewZoom( QString::number( doc()->zoom() ) );
01461 
01462     // ## Might be wrong, if doc isn't loaded yet
01463     d->actions->selectStyle->setItems( d->doc->styleManager()->styleNames() );
01464 
01465     // If doc was already loaded, initialize things
01466     // Otherwise the doc will do it in completeLoading.
01467     if ( !doc()->map()->sheetList().isEmpty() )
01468         initialPosition();
01469 }
01470 
01471 KSpreadView::~KSpreadView()
01472 {
01473     //  ElapsedTime el( "~KSpreadView" );
01474     if ( doc()->isReadWrite() ) // make sure we're not embedded in Konq
01475         deleteEditor( true );
01476     if ( !d->transformToolBox.isNull() )
01477         delete (&*d->transformToolBox);
01478     /*if (d->calcLabel)
01479     {
01480         disconnect(d->calcLabel,SIGNAL(pressed( int )),this,SLOT(statusBarClicked(int)));
01481 
01482         }*/
01483 
01484     delete d->selectionInfo;
01485     delete d->spell.kspell;
01486 
01487     d->canvas->endChoose();
01488     d->activeSheet = 0; // set the active sheet to 0L so that when during destruction
01489     // of embedded child documents possible repaints in KSpreadSheet are not
01490     // performed. The repains can happen if you delete an embedded document,
01491     // which leads to an regionInvalidated() signal emission in KoView, which calls
01492     // repaint, etc.etc. :-) (Simon)
01493 
01494     delete d->popupColumn;
01495     delete d->popupRow;
01496     delete d->popupMenu;
01497     delete d->popupChild;
01498     delete d->popupListChoose;
01499     delete d->calcLabel;
01500     delete d->dcop;
01501 
01502     delete d->insertHandler;
01503     d->insertHandler = 0L;
01504 
01505     delete d->actions;
01506     delete d;
01507 }
01508 
01509 KSpreadDoc* KSpreadView::doc()
01510 {
01511   return d->doc;
01512 }
01513 
01514 // should be called only once, from the constructor
01515     /*
01516      * Top part is the formula bar.
01517      * Central part is the canvas, row header and vertical scrollbar.
01518      * Bottom part is the tab bar and horizontal scrollbar.
01519      *
01520      * Note that canvas must the one to be created, since other
01521      * widgets might depend on it.
01522      */
01523 
01524 void KSpreadView::initView()
01525 {
01526     d->viewLayout = new QGridLayout( this, 3, 4 );
01527 
01528     // Vert. Scroll Bar
01529     d->calcLabel  = 0;
01530     d->vertScrollBar = new QScrollBar( this, "ScrollBar_2" );
01531     d->vertScrollBar->setRange( 0, 4096 );
01532     d->vertScrollBar->setOrientation( QScrollBar::Vertical );
01533     d->vertScrollBar->setLineStep(60);  //just random guess based on what feels okay
01534     d->vertScrollBar->setPageStep(60);  //This should be controlled dynamically, depending on how many rows are shown
01535 
01536     // Edit Bar
01537     d->toolWidget = new QFrame( this );
01538 
01539     d->formulaBarLayout = new QHBoxLayout( d->toolWidget );
01540     d->formulaBarLayout->setMargin( 4 );
01541     d->formulaBarLayout->addSpacing( 2 );
01542 
01543     d->posWidget = new KSpreadComboboxLocationEditWidget( d->toolWidget, this );
01544     d->posWidget->setMinimumWidth( 100 );
01545     d->formulaBarLayout->addWidget( d->posWidget );
01546     d->formulaBarLayout->addSpacing( 6 );
01547 
01548     d->formulaButton = d->newIconButton( "funct", TRUE, d->toolWidget );
01549     d->formulaBarLayout->addWidget( d->formulaButton );
01550     d->formulaBarLayout->addSpacing( 2 );
01551     connect( d->formulaButton, SIGNAL( clicked() ), SLOT( insertMathExpr() ) );
01552 
01553     d->cancelButton = d->newIconButton( "cancel", TRUE, d->toolWidget );
01554     d->formulaBarLayout->addWidget( d->cancelButton );
01555     d->okButton = d->newIconButton( "ok", TRUE, d->toolWidget );
01556     d->formulaBarLayout->addWidget( d->okButton );
01557     d->formulaBarLayout->addSpacing( 6 );
01558 
01559     // The widget on which we display the sheet
01560     d->canvas = new KSpreadCanvas( this );
01561 
01562     // The line-editor that appears above the sheet and allows to
01563     // edit the cells content. It knows about the two buttons.
01564     d->editWidget = new KSpreadEditWidget( d->toolWidget, d->canvas, d->cancelButton, d->okButton );
01565     d->editWidget->setFocusPolicy( QWidget::StrongFocus );
01566     d->formulaBarLayout->addWidget( d->editWidget, 2 );
01567     d->formulaBarLayout->addSpacing( 2 );
01568 
01569     d->canvas->setEditWidget( d->editWidget );
01570 
01571     d->hBorderWidget = new KSpreadHBorder( this, d->canvas,this );
01572     d->vBorderWidget = new KSpreadVBorder( this, d->canvas ,this );
01573 
01574     d->canvas->setFocusPolicy( QWidget::StrongFocus );
01575     QWidget::setFocusPolicy( QWidget::StrongFocus );
01576     setFocusProxy( d->canvas );
01577 
01578     connect( this, SIGNAL( invalidated() ), d->canvas, SLOT( update() ) );
01579 
01580     QWidget* bottomPart = new QWidget( this );
01581     d->tabScrollBarLayout = new QHBoxLayout( bottomPart );
01582     d->tabScrollBarLayout->setAutoAdd( true );
01583     d->tabBar = new KoTabBar( bottomPart );
01584     d->horzScrollBar = new QScrollBar( bottomPart, "ScrollBar_1" );
01585 
01586     d->horzScrollBar->setRange( 0, 4096 );
01587     d->horzScrollBar->setOrientation( QScrollBar::Horizontal );
01588 
01589     d->horzScrollBar->setLineStep(60); //just random guess based on what feels okay
01590     d->horzScrollBar->setPageStep(60);
01591 
01592     QObject::connect( d->tabBar, SIGNAL( tabChanged( const QString& ) ), this, SLOT( changeSheet( const QString& ) ) );
01593     QObject::connect( d->tabBar, SIGNAL( tabMoved( unsigned, unsigned ) ),
01594       this, SLOT( moveSheet( unsigned, unsigned ) ) );
01595     QObject::connect( d->tabBar, SIGNAL( contextMenu( const QPoint& ) ),
01596       this, SLOT( popupTabBarMenu( const QPoint& ) ) );
01597     QObject::connect( d->tabBar, SIGNAL( doubleClicked() ),
01598       this, SLOT( slotRename() ) );
01599 
01600     d->viewLayout->setColStretch( 1, 10 );
01601     d->viewLayout->setRowStretch( 2, 10 );
01602     d->viewLayout->addMultiCellWidget( d->toolWidget, 0, 0, 0, 2 );
01603     d->viewLayout->addMultiCellWidget( d->hBorderWidget, 1, 1, 1, 2 );
01604     d->viewLayout->addWidget( d->vBorderWidget, 2, 0 );
01605     d->viewLayout->addWidget( d->canvas, 2, 1 );
01606     d->viewLayout->addWidget( d->vertScrollBar, 2, 2 );
01607     d->viewLayout->addMultiCellWidget( bottomPart, 3, 3, 0, 2 );
01608 
01609     KStatusBar * sb = statusBar();
01610     Q_ASSERT(sb);
01611     d->calcLabel = sb ? new KStatusBarLabel( QString::null, 0, sb ) : 0;
01612     addStatusBarItem( d->calcLabel, 0 );
01613     if (d->calcLabel)
01614         connect(d->calcLabel ,SIGNAL(itemPressed( int )),this,SLOT(statusBarClicked(int)));
01615 
01616     // signal slot
01617     QObject::connect( d->vertScrollBar, SIGNAL( valueChanged(int) ), d->canvas, SLOT( slotScrollVert(int) ) );
01618     QObject::connect( d->horzScrollBar, SIGNAL( valueChanged(int) ), d->canvas, SLOT( slotScrollHorz(int) ) );
01619 
01620 }
01621 
01622 KSpreadCanvas* KSpreadView::canvasWidget() const
01623 {
01624     return d->canvas;
01625 }
01626 
01627 KSpreadHBorder* KSpreadView::hBorderWidget()const
01628 {
01629     return d->hBorderWidget;
01630 }
01631 
01632 KSpreadVBorder* KSpreadView::vBorderWidget()const
01633 {
01634     return d->vBorderWidget;
01635 }
01636 
01637 QScrollBar* KSpreadView::horzScrollBar()const
01638 {
01639     return d->horzScrollBar;
01640 }
01641 
01642 QScrollBar* KSpreadView::vertScrollBar()const
01643 {
01644     return d->vertScrollBar;
01645 }
01646 
01647 KSpreadEditWidget* KSpreadView::editWidget()const
01648 {
01649     return d->editWidget;
01650 }
01651 
01652 KSpreadComboboxLocationEditWidget* KSpreadView::posWidget()const
01653 {
01654     return d->posWidget;
01655 }
01656 
01657 KoTabBar* KSpreadView::tabBar() const
01658 {
01659     return d->tabBar;
01660 }
01661 
01662 bool KSpreadView::isLoading() const
01663 {
01664     return d->loading;
01665 }
01666 
01667 KSpreadSelection* KSpreadView::selectionInfo() const
01668 {
01669     return d->selectionInfo;
01670 }
01671 
01672 QRect KSpreadView::selection(bool extend) const
01673 {
01674     return selectionInfo()->selection(extend);
01675 }
01676 
01677 QPoint KSpreadView::marker() const
01678 {
01679     return selectionInfo()->marker();
01680 }
01681 
01682 void KSpreadView::resetInsertHandle()
01683 {
01684     d->insertHandler = 0L;
01685 }
01686 
01687 const KSpreadSheet* KSpreadView::activeSheet() const
01688 {
01689     return d->activeSheet;
01690 }
01691 
01692 KSpreadSheet* KSpreadView::activeSheet()
01693 {
01694     return d->activeSheet;
01695 }
01696 
01697 void KSpreadView::initConfig()
01698 {
01699     KConfig *config = KSpreadFactory::global()->config();
01700     if ( config->hasGroup("Parameters" ))
01701     {
01702         config->setGroup( "Parameters" );
01703         if ( !doc()->configLoadFromFile() )
01704             doc()->setShowHorizontalScrollBar(config->readBoolEntry("Horiz ScrollBar",true));
01705         if ( !doc()->configLoadFromFile() )
01706             doc()->setShowVerticalScrollBar(config->readBoolEntry("Vert ScrollBar",true));
01707         doc()->setShowColHeader(config->readBoolEntry("Column Header",true));
01708         doc()->setShowRowHeader(config->readBoolEntry("Row Header",true));
01709         if ( !doc()->configLoadFromFile() )
01710             doc()->setCompletionMode((KGlobalSettings::Completion)config->readNumEntry("Completion Mode",(int)(KGlobalSettings::CompletionAuto)));
01711         doc()->setMoveToValue((KSpread::MoveTo)config->readNumEntry("Move",(int)(KSpread::Bottom)));
01712         doc()->setIndentValue( config->readDoubleNumEntry( "Indent", 10.0 ) );
01713         doc()->setTypeOfCalc((MethodOfCalc)config->readNumEntry("Method of Calc",(int)(SumOfNumber)));
01714         if ( !doc()->configLoadFromFile() )
01715             doc()->setShowTabBar(config->readBoolEntry("Tabbar",true));
01716 
01717     doc()->setShowMessageError(config->readBoolEntry( "Msg error" ,false) );
01718 
01719     doc()->setShowCommentIndicator(config->readBoolEntry("Comment Indicator",true));
01720 
01721     doc()->setShowFormulaBar(config->readBoolEntry("Formula bar",true));
01722         doc()->setShowStatusBar(config->readBoolEntry("Status bar",true));
01723 
01724         changeNbOfRecentFiles(config->readNumEntry("NbRecentFile",10));
01725         //autosave value is stored as a minute.
01726         //but default value is stored as seconde.
01727         doc()->setAutoSave(config->readNumEntry("AutoSave",KoDocument::defaultAutoSave()/60)*60);
01728         doc()->setBackupFile( config->readBoolEntry("BackupFile",true));
01729     }
01730 
01731    if (  config->hasGroup("KSpread Color" ) )
01732    {
01733      config->setGroup( "KSpread Color" );
01734      QColor _col(Qt::lightGray);
01735      _col = config->readColorEntry("GridColor", &_col);
01736      doc()->setGridColor(_col);
01737 
01738      QColor _pbCol(Qt::red);
01739      _pbCol = config->readColorEntry("PageBorderColor", &_pbCol);
01740      doc()->changePageBorderColor(_pbCol);
01741    }
01742 
01743 // Do we need a Page Layout in the congiguration file? Isn't this already in the template? Philipp
01744 /*
01745 if ( config->hasGroup("KSpread Page Layout" ) )
01746  {
01747    config->setGroup( "KSpread Page Layout" );
01748    if ( d->activeSheet->isEmpty())
01749      {
01750     d->activeSheet->setPaperFormat((KoFormat)config->readNumEntry("Default size page",1));
01751 
01752     d->activeSheet->setPaperOrientation((KoOrientation)config->readNumEntry("Default orientation page",0));
01753     d->activeSheet->setPaperUnit((KoUnit::Unit)config->readNumEntry("Default unit page",0));
01754      }
01755  }
01756 */
01757 
01758  initCalcMenu();
01759  resultOfCalc();
01760 }
01761 
01762 void KSpreadView::changeNbOfRecentFiles(int _nb)
01763 {
01764     if (shell())
01765         shell()->setMaxRecentItems( _nb );
01766 }
01767 
01768 void KSpreadView::initCalcMenu()
01769 {
01770     switch( doc()->getTypeOfCalc())
01771     {
01772         case  SumOfNumber:
01773             d->actions->calcSum->setChecked(true);
01774             break;
01775         case  Min:
01776             d->actions->calcMin->setChecked(true);
01777             break;
01778         case  Max:
01779             d->actions->calcMax->setChecked(true);
01780             break;
01781         case  Average:
01782             d->actions->calcAverage->setChecked(true);
01783             break;
01784         case  Count:
01785             d->actions->calcCount->setChecked(true);
01786             break;
01787         case  CountA:
01788             d->actions->calcCountA->setChecked(true);
01789             break;
01790         case  NoneCalc:
01791             d->actions->calcNone->setChecked(true);
01792             break;
01793         default :
01794             d->actions->calcSum->setChecked(true);
01795             break;
01796     }
01797 
01798 }
01799 
01800 
01801 void KSpreadView::recalcWorkBook()
01802 {
01803   KSpreadSheet * tbl;
01804   doc()->emitBeginOperation( true );
01805   for ( tbl = doc()->map()->firstSheet();
01806         tbl != 0L;
01807         tbl = doc()->map()->nextSheet() )
01808   {
01809     bool b = tbl->getAutoCalc();
01810     tbl->setAutoCalc( true );
01811     tbl->recalc();
01812     tbl->setAutoCalc( b );
01813   }
01814 
01815   doc()->emitEndOperation( d->activeSheet->visibleRect( d->canvas ) );
01816 }
01817 
01818 void KSpreadView::refreshLocale()
01819 {
01820   doc()->emitBeginOperation(true);
01821   KSpreadSheet *tbl;
01822   for ( tbl = doc()->map()->firstSheet();
01823         tbl != 0L;
01824         tbl = doc()->map()->nextSheet() )
01825   {
01826     tbl->updateLocale();
01827   }
01828   doc()->emitEndOperation( d->activeSheet->visibleRect( d->canvas ) );
01829 }
01830 
01831 void KSpreadView::recalcWorkSheet()
01832 {
01833   if ( d->activeSheet != 0 )
01834   {
01835     doc()->emitBeginOperation( true );
01836     bool b = d->activeSheet->getAutoCalc();
01837     d->activeSheet->setAutoCalc( true );
01838     d->activeSheet->recalc();
01839     d->activeSheet->setAutoCalc( b );
01840     doc()->emitEndOperation( d->activeSheet->visibleRect( d->canvas ) );
01841   }
01842 }
01843 
01844 
01845 void KSpreadView::extraSpelling()
01846 {
01847   if ( d->spell.kspell )
01848     return; // Already in progress
01849 
01850   if (d->activeSheet == 0L)
01851     return;
01852 
01853   d->spell.macroCmdSpellCheck = 0L;
01854   d->spell.firstSpellSheet    = d->activeSheet;
01855   d->spell.currentSpellSheet  = d->spell.firstSpellSheet;
01856 
01857   QRect selection = d->selectionInfo->selection();
01858 
01859   // if nothing is selected, check every cell
01860   if (d->selectionInfo->singleCellSelection())
01861   {
01862     d->spell.spellStartCellX = 0;
01863     d->spell.spellStartCellY = 0;
01864     d->spell.spellEndCellX   = 0;
01865     d->spell.spellEndCellY   = 0;
01866     d->spell.spellCheckSelection = false;
01867     d->spell.currentCell     = d->activeSheet->firstCell();
01868   }
01869   else
01870   {
01871     d->spell.spellStartCellX = selection.left();
01872     d->spell.spellStartCellY = selection.top();
01873     d->spell.spellEndCellX   = selection.right();
01874     d->spell.spellEndCellY   = selection.bottom();
01875     d->spell.spellCheckSelection = true;
01876     d->spell.currentCell     = 0L;
01877 
01878     // "-1" because X gets increased every time we go into spellCheckReady()
01879     d->spell.spellCurrCellX = d->spell.spellStartCellX - 1;
01880     d->spell.spellCurrCellY = d->spell.spellStartCellY;
01881   }
01882 
01883   startKSpell();
01884 }
01885 
01886 
01887 void KSpreadView::startKSpell()
01888 {
01889     if ( doc()->getKSpellConfig() )
01890     {
01891         doc()->getKSpellConfig()->setIgnoreList( doc()->spellListIgnoreAll() );
01892         doc()->getKSpellConfig()->setReplaceAllList( d->spell.replaceAll );
01893 
01894     }
01895     d->spell.kspell = new KSpell( this, i18n( "Spell Checking" ), this,
01896                                        SLOT( spellCheckerReady() ),
01897                                        doc()->getKSpellConfig() );
01898 
01899   d->spell.kspell->setIgnoreUpperWords( doc()->dontCheckUpperWord() );
01900   d->spell.kspell->setIgnoreTitleCase( doc()->dontCheckTitleCase() );
01901 
01902   QObject::connect( d->spell.kspell, SIGNAL( death() ),
01903                     this, SLOT( spellCheckerFinished() ) );
01904   QObject::connect( d->spell.kspell, SIGNAL( misspelling( const QString &,
01905                                                          const QStringList &,
01906                                                          unsigned int) ),
01907                     this, SLOT( spellCheckerMisspelling( const QString &,
01908                                                          const QStringList &,
01909                                                          unsigned int) ) );
01910   QObject::connect( d->spell.kspell, SIGNAL( corrected( const QString &,
01911                                                        const QString &,
01912                                                        unsigned int) ),
01913                     this, SLOT( spellCheckerCorrected( const QString &,
01914                                                        const QString &,
01915                                                        unsigned int ) ) );
01916   QObject::connect( d->spell.kspell, SIGNAL( done( const QString & ) ),
01917                     this, SLOT( spellCheckerDone( const QString & ) ) );
01918   QObject::connect( d->spell.kspell, SIGNAL( ignoreall (const QString & ) ),
01919                     this, SLOT( spellCheckerIgnoreAll( const QString & ) ) );
01920 
01921   QObject::connect( d->spell.kspell, SIGNAL( replaceall( const QString &  ,  const QString & )), this, SLOT( spellCheckerReplaceAll( const QString &  ,  const QString & )));
01922 
01923 }
01924 
01925 void KSpreadView::spellCheckerReplaceAll( const QString &orig, const QString & replacement)
01926 {
01927     d->spell.replaceAll.append( orig);
01928     d->spell.replaceAll.append( replacement);
01929 }
01930 
01931 
01932 void KSpreadView::spellCheckerIgnoreAll( const QString & word)
01933 {
01934     doc()->addIgnoreWordAll( word );
01935 }
01936 
01937 
01938 void KSpreadView::spellCheckerReady()
01939 {
01940   if (d->canvas)
01941     d->canvas->setCursor( WaitCursor );
01942 
01943   // go on to the next cell
01944   if (!d->spell.spellCheckSelection)
01945   {
01946     // if nothing is selected we have to check every cell
01947     // we use a different way to make it faster
01948     while ( d->spell.currentCell )
01949     {
01950       // check text only
01951       if ( d->spell.currentCell->value().isString() )
01952       {
01953         d->spell.kspell->check( d->spell.currentCell->text(), true );
01954 
01955         return;
01956       }
01957 
01958       d->spell.currentCell = d->spell.currentCell->nextCell();
01959       if ( d->spell.currentCell->isDefault() )
01960         kdDebug() << "checking default cell!!" << endl << endl;
01961     }
01962 
01963     if (spellSwitchToOtherSheet())
01964       spellCheckerReady();
01965     else
01966       spellCleanup();
01967 
01968     return;
01969   }
01970 
01971   // if something is selected:
01972 
01973   ++d->spell.spellCurrCellX;
01974   if (d->spell.spellCurrCellX > d->spell.spellEndCellX)
01975   {
01976     d->spell.spellCurrCellX = d->spell.spellStartCellX;
01977     ++d->spell.spellCurrCellY;
01978   }
01979 
01980   unsigned int y;
01981   unsigned int x;
01982 
01983   for ( y = d->spell.spellCurrCellY; y <= d->spell.spellEndCellY; ++y )
01984   {
01985     for ( x = d->spell.spellCurrCellX; x <= d->spell.spellEndCellX; ++x )
01986     {
01987       KSpreadCell * cell = d->spell.currentSpellSheet->cellAt( x, y );
01988 
01989       // check text only
01990       if (cell->isDefault() || !cell->value().isString())
01991         continue;
01992 
01993       d->spell.spellCurrCellX = x;
01994       d->spell.spellCurrCellY = y;
01995 
01996       d->spell.kspell->check( cell->text(), true );
01997 
01998       return;
01999     }
02000     d->spell.spellCurrCellX = d->spell.spellStartCellX;
02001   }
02002 
02003   // if the user selected something to be checked we are done
02004   // otherwise ask for checking the next sheet if any
02005   if (d->spell.spellCheckSelection)
02006   {
02007     // Done
02008     spellCleanup();
02009   }
02010   else
02011   {
02012     if (spellSwitchToOtherSheet())
02013       spellCheckerReady();
02014     else
02015       spellCleanup();
02016   }
02017 }
02018 
02019 
02020 void KSpreadView::spellCleanup()
02021 {
02022   if ( d->canvas )
02023     d->canvas->setCursor( ArrowCursor );
02024 
02025   d->spell.kspell->cleanUp();
02026   delete d->spell.kspell;
02027   d->spell.kspell            = 0L;
02028   d->spell.firstSpellSheet   = 0L;
02029   d->spell.currentSpellSheet = 0L;
02030   d->spell.currentCell       = 0L;
02031   d->spell.replaceAll.clear();
02032 
02033 
02034   KMessageBox::information( this, i18n( "Spell checking is complete." ) );
02035 
02036   if ( d->spell.macroCmdSpellCheck )
02037     doc()->addCommand( d->spell.macroCmdSpellCheck );
02038   d->spell.macroCmdSpellCheck=0L;
02039 }
02040 
02041 
02042 bool KSpreadView::spellSwitchToOtherSheet()
02043 {
02044   // there is no other sheet
02045   if ( doc()->map()->count() == 1 )
02046     return false;
02047 
02048   // for optimization
02049   QPtrList<KSpreadSheet> sheetList = doc()->map()->sheetList();
02050 
02051   unsigned int curIndex = sheetList.findRef(d->spell.currentSpellSheet);
02052   ++curIndex;
02053 
02054   // last sheet? then start at the beginning
02055   if ( curIndex >= sheetList.count() )
02056     d->spell.currentSpellSheet = sheetList.first();
02057   else
02058     d->spell.currentSpellSheet = sheetList.at(curIndex);
02059 
02060   // if the current sheet is the first one again, we are done.
02061   if ( d->spell.currentSpellSheet == d->spell.firstSpellSheet )
02062   {
02063     setActiveSheet( d->spell.firstSpellSheet );
02064     return false;
02065   }
02066 
02067   if (d->spell.spellCheckSelection)
02068   {
02069     d->spell.spellEndCellX = d->spell.currentSpellSheet->maxColumn();
02070     d->spell.spellEndCellY = d->spell.currentSpellSheet->maxRow();
02071 
02072     d->spell.spellCurrCellX = d->spell.spellStartCellX - 1;
02073     d->spell.spellCurrCellY = d->spell.spellStartCellY;
02074   }
02075   else
02076   {
02077     d->spell.currentCell = d->spell.currentSpellSheet->firstCell();
02078   }
02079 
02080   if ( KMessageBox::questionYesNo( this,
02081                                    i18n( "Do you want to check the spelling in the next sheet?") )
02082        != KMessageBox::Yes )
02083     return false;
02084 
02085   setActiveSheet( d->spell.currentSpellSheet );
02086 
02087   return true;
02088 }
02089 
02090 
02091 void KSpreadView::spellCheckerMisspelling( const QString &,
02092                                            const QStringList &,
02093                                            unsigned int )
02094 {
02095   // scroll to the cell
02096   if ( !d->spell.spellCheckSelection )
02097   {
02098     d->spell.spellCurrCellX = d->spell.currentCell->column();
02099     d->spell.spellCurrCellY = d->spell.currentCell->row();
02100   }
02101 
02102   canvasWidget()->gotoLocation( d->spell.spellCurrCellX, d->spell.spellCurrCellY, activeSheet() );
02103 }
02104 
02105 
02106 void KSpreadView::spellCheckerCorrected( const QString & old, const QString & corr,
02107                                          unsigned int pos )
02108 {
02109   KSpreadCell * cell;
02110 
02111   if (d->spell.spellCheckSelection)
02112   {
02113     cell = d->spell.currentSpellSheet->cellAt( d->spell.spellCurrCellX,
02114                                               d->spell.spellCurrCellY );
02115   }
02116   else
02117   {
02118     cell = d->spell.currentCell;
02119     d->spell.spellCurrCellX = cell->column();
02120     d->spell.spellCurrCellY = cell->row();
02121   }
02122 
02123   Q_ASSERT( cell );
02124   if ( !cell )
02125     return;
02126 
02127   doc()->emitBeginOperation(false);
02128   QString content( cell->text() );
02129 
02130   KSpreadUndoSetText* undo = new KSpreadUndoSetText( doc(), d->activeSheet,
02131                                                      content,
02132                                                      d->spell.spellCurrCellX,
02133                                                      d->spell.spellCurrCellY,
02134                                                      cell->formatType());
02135   content.replace( pos, old.length(), corr );
02136   cell->setCellText( content );
02137   d->editWidget->setText( content );
02138 
02139   if ( !d->spell.macroCmdSpellCheck )
02140       d->spell.macroCmdSpellCheck = new KSpreadMacroUndoAction( doc(), i18n("Correct Misspelled Word") );
02141   d->spell.macroCmdSpellCheck->addCommand( undo );
02142   doc()->emitEndOperation( d->activeSheet->visibleRect( d->canvas ) );
02143 }
02144 
02145 void KSpreadView::spellCheckerDone( const QString & )
02146 {
02147     int result = d->spell.kspell->dlgResult();
02148 
02149     d->spell.kspell->cleanUp();
02150     delete d->spell.kspell;
02151     d->spell.kspell = 0L;
02152 
02153     if ( result != KS_CANCEL && result != KS_STOP )
02154     {
02155         if (d->spell.spellCheckSelection)
02156         {
02157             if ( (d->spell.spellCurrCellY <= d->spell.spellEndCellY)
02158                  && (d->spell.spellCurrCellX <= d->spell.spellEndCellX) )
02159             {
02160                 startKSpell();
02161                 return;
02162             }
02163         }
02164         else
02165         {
02166             if ( d->spell.currentCell )
02167             {
02168                 d->spell.currentCell = d->spell.currentCell->nextCell();
02169 
02170                 startKSpell();
02171 
02172                 return;
02173             }
02174         }
02175     }
02176     d->spell.replaceAll.clear();
02177 
02178     if ( d->spell.macroCmdSpellCheck )
02179     {
02180         doc()->addCommand( d->spell.macroCmdSpellCheck );
02181     }
02182     d->spell.macroCmdSpellCheck=0L;
02183 }
02184 
02185 void KSpreadView::spellCheckerFinished()
02186 {
02187   if (d->canvas)
02188     d->canvas->setCursor( ArrowCursor );
02189 
02190   KSpell::spellStatus status = d->spell.kspell->status();
02191   d->spell.kspell->cleanUp();
02192   delete d->spell.kspell;
02193   d->spell.kspell = 0L;
02194   d->spell.replaceAll.clear();
02195 
02196   bool kspellNotConfigured=false;
02197 
02198   if (status == KSpell::Error)
02199   {
02200     KMessageBox::sorry(this, i18n("ISpell could not be started.\n"
02201                                   "Please make sure you have ISpell properly configured and in your PATH."));
02202     kspellNotConfigured=true;
02203   }
02204   else if (status == KSpell::Crashed)
02205   {
02206     KMessageBox::sorry(this, i18n("ISpell seems to have crashed."));
02207   }
02208 
02209   if (d->spell.macroCmdSpellCheck)
02210   {
02211       doc()->addCommand( d->spell.macroCmdSpellCheck );
02212   }
02213   d->spell.macroCmdSpellCheck=0L;
02214 
02215 
02216   if (kspellNotConfigured)
02217   {
02218     KSpreadpreference configDlg( this, 0 );
02219     configDlg.openPage( KSpreadpreference::KS_SPELLING);
02220     configDlg.exec();
02221   }
02222 }
02223 
02224 void KSpreadView::initialPosition()
02225 {
02226     kdDebug() << "KSpreadView::initialPosition" << endl;
02227 
02228     // Loading completed, pick initial worksheet
02229     QPtrListIterator<KSpreadSheet> it( doc()->map()->sheetList() );
02230     for( ; it.current(); ++it )
02231       addSheet( it.current() );
02232 
02233     KSpreadSheet * tbl = 0L;
02234     if ( doc()->isEmbedded() )
02235     {
02236         tbl = doc()->displaySheet();
02237     }
02238 
02239     if ( !tbl )
02240         tbl = doc()->map()->initialActiveSheet();
02241     if ( tbl )
02242       setActiveSheet( tbl );
02243     else
02244     {
02245       //activate first table which is not hiding
02246       tbl = doc()->map()->findSheet( doc()->map()->visibleSheets().first());
02247       if ( !tbl )
02248       {
02249         tbl = doc()->map()->firstSheet();
02250         if ( tbl )
02251         {
02252           tbl->setHidden( false );
02253           QString tabName = tbl->sheetName();
02254           d->tabBar->addTab( tabName );
02255         }
02256       }
02257       setActiveSheet( tbl );
02258     }
02259 
02260     refreshView();
02261 
02262     // Set the initial position for the marker as store in the XML file,
02263     // (1,1) otherwise
02264     int col = doc()->map()->initialMarkerColumn();
02265     if ( col <= 0 )
02266       col = 1;
02267     int row = doc()->map()->initialMarkerRow();
02268     if ( row <= 0 )
02269       row = 1;
02270 
02271     d->canvas->gotoLocation( col, row );
02272 
02273     updateBorderButton();
02274     updateShowSheetMenu();
02275 
02276     d->actions->autoFormat->setEnabled(false);
02277     d->actions->sort->setEnabled(false);
02278     d->actions->mergeCell->setEnabled(false);
02279     d->actions->createStyle->setEnabled(false);
02280 
02281     d->actions->fillUp->setEnabled( false );
02282     d->actions->fillRight->setEnabled( false );
02283     d->actions->fillDown->setEnabled( false );
02284     d->actions->fillLeft->setEnabled( false );
02285 
02286     // make paint effective:
02287     doc()->decreaseNumOperation();
02288     d->actions->insertChartFrame->setEnabled(false);
02289 
02290     QRect vr( activeSheet()->visibleRect( d->canvas ) );
02291 
02292     doc()->emitBeginOperation( false );
02293     activeSheet()->setRegionPaintDirty( vr );
02294     doc()->emitEndOperation( vr );
02295 
02296     d->loading = true;
02297 
02298     if ( koDocument()->isReadWrite() )
02299       initConfig();
02300 
02301     d->adjustActions( !d->activeSheet->isProtected() );
02302     d->adjustWorkbookActions( !doc()->map()->isProtected() );
02303 }
02304 
02305 
02306 void KSpreadView::updateEditWidgetOnPress()
02307 {
02308     int column = d->canvas->markerColumn();
02309     int row    = d->canvas->markerRow();
02310 
02311     KSpreadCell* cell = d->activeSheet->cellAt( column, row );
02312     if ( !cell )
02313     {
02314         editWidget()->setText( "" );
02315         return;
02316     }
02317     if ( d->activeSheet->isProtected() && cell->isHideFormula( column, row ) )
02318         editWidget()->setText( cell->strOutText() );
02319     else if ( d->activeSheet->isProtected() && cell->isHideAll( column, row ) )
02320         editWidget()->setText( "" );
02321     else
02322         editWidget()->setText( cell->text() );
02323 
02324     d->updateButton(cell, column, row);
02325     d->adjustActions( d->activeSheet, cell );
02326 }
02327 
02328 void KSpreadView::updateEditWidget()
02329 {
02330     int column = d->canvas->markerColumn();
02331     int row    = d->canvas->markerRow();
02332 
02333     KSpreadCell * cell = d->activeSheet->cellAt( column, row );
02334     bool active = activeSheet()->getShowFormula()
02335       && !( d->activeSheet->isProtected() && cell && cell->isHideFormula( column, row ) );
02336 
02337     if ( d->activeSheet && !d->activeSheet->isProtected() )
02338     {
02339       d->actions->alignLeft->setEnabled(!active);
02340       d->actions->alignCenter->setEnabled(!active);
02341       d->actions->alignRight->setEnabled(!active);
02342     }
02343 
02344     if ( !cell )
02345     {
02346         editWidget()->setText( "" );
02347         if ( d->activeSheet->isProtected() )
02348           editWidget()->setEnabled( false );
02349         else
02350           editWidget()->setEnabled( true );
02351         return;
02352     }
02353 
02354     if ( d->activeSheet->isProtected() && cell->isHideFormula( column, row ) )
02355         editWidget()->setText( cell->strOutText() );
02356     else if ( d->activeSheet->isProtected() && cell->isHideAll( column, row ) )
02357         editWidget()->setText( "" );
02358     else
02359         editWidget()->setText( cell->text() );
02360 
02361     if ( d->activeSheet->isProtected() && !cell->notProtected( column, row ) )
02362       editWidget()->setEnabled( false );
02363     else
02364       editWidget()->setEnabled( true );
02365 
02366     if ( d->canvas->editor() )
02367     {
02368       d->canvas->editor()->setEditorFont(cell->textFont(column, row), true);
02369       d->canvas->editor()->setFocus();
02370     }
02371     d->updateButton(cell, column, row);
02372     d->adjustActions( d->activeSheet, cell );
02373 }
02374 
02375 void KSpreadView::activateFormulaEditor()
02376 {
02377 }
02378 
02379 void KSpreadView::updateReadWrite( bool readwrite )
02380 {
02381 #ifdef __GNUC_
02382 #warning TODO
02383 #endif
02384     // d->cancelButton->setEnabled( readwrite );
02385     // d->okButton->setEnabled( readwrite );
02386   d->editWidget->setEnabled( readwrite );
02387 
02388   QValueList<KAction*> actions = actionCollection()->actions();
02389   QValueList<KAction*>::ConstIterator aIt = actions.begin();
02390   QValueList<KAction*>::ConstIterator aEnd = actions.end();
02391   for (; aIt != aEnd; ++aIt )
02392     (*aIt)->setEnabled( readwrite );
02393 
02394   d->actions->transform->setEnabled( false );
02395   if ( !doc() || !doc()->map() || doc()->map()->isProtected() )
02396   {
02397     d->actions->showSheet->setEnabled( false );
02398     d->actions->hideSheet->setEnabled( false );
02399   }
02400   else
02401   {
02402     d->actions->showSheet->setEnabled( true );
02403     d->actions->hideSheet->setEnabled( true );
02404   }
02405   d->actions->gotoCell->setEnabled( true );
02406   d->actions->viewZoom->setEnabled( true );
02407   d->actions->showPageBorders->setEnabled( true );
02408   d->actions->find->setEnabled( true);
02409   d->actions->replace->setEnabled( readwrite );
02410   if ( !doc()->isReadWrite())
02411       d->actions->copy->setEnabled( true );
02412   //  d->actions->newView->setEnabled( true );
02413   //doc()->KXMLGUIClient::action( "newView" )->setEnabled( true ); // obsolete (Werner)
02414 }
02415 
02416 void KSpreadView::createTemplate()
02417 {
02418   int width = 60;
02419   int height = 60;
02420   QPixmap pix = doc()->generatePreview(QSize(width, height));
02421 
02422   KTempFile tempFile( QString::null, ".kst" );
02423   tempFile.setAutoDelete(true);
02424 
02425   doc()->saveNativeFormat( tempFile.name() );
02426 
02427   KoTemplateCreateDia::createTemplate( "kspread_template", KSpreadFactory::global(),
02428                                            tempFile.name(), pix, this );
02429 
02430   KSpreadFactory::global()->dirs()->addResourceType("kspread_template",
02431                                                        KStandardDirs::kde_default( "data" ) +
02432                                                        "kspread/templates/");
02433 }
02434 
02435 void KSpreadView::sheetFormat()
02436 {
02437     KSpreadFormatDlg dlg( this );
02438     dlg.exec();
02439 }
02440 
02441 void KSpreadView::autoSum()
02442 {
02443     // ######## Torben: Make sure that this can not be called
02444     // when canvas has a running editor
02445     if ( d->canvas->editor() )
02446         return;
02447 
02448     d->canvas->createEditor( KSpreadCanvas::CellEditor );
02449     d->canvas->editor()->setText( "=SUM()" );
02450     d->canvas->editor()->setCursorPosition( 5 );
02451 
02452     // Try to find numbers above
02453     if ( d->canvas->markerRow() > 1 )
02454     {
02455         KSpreadCell* cell = 0;
02456         int r = d->canvas->markerRow();
02457         do
02458         {
02459             cell = activeSheet()->cellAt( d->canvas->markerColumn(), --r );
02460         }
02461         while ( cell && cell->value().isNumber() );
02462 
02463         if ( r + 1 < d->canvas->markerRow() )
02464         {
02465             d->canvas->startChoose( QRect( d->canvas->markerColumn(), r + 1, 1, d->canvas->markerRow() - r - 1 ) );
02466             return;
02467         }
02468     }
02469 
02470     // Try to find numbers left
02471     if ( d->canvas->markerColumn() > 1 )
02472     {
02473         KSpreadCell* cell = 0;
02474         int c = d->canvas->markerColumn();
02475         do
02476         {
02477             cell = activeSheet()->cellAt( --c, d->canvas->markerRow() );
02478         }
02479         while ( cell && cell->value().isNumber() );
02480 
02481         if ( c + 1 < d->canvas->markerColumn() )
02482         {
02483             d->canvas->startChoose( QRect( c + 1, d->canvas->markerRow(), d->canvas->markerColumn() - c - 1, 1 ) );
02484             return;
02485         }
02486     }
02487 }
02488 
02489 /*
02490 void KSpreadView::oszilloscope()
02491 {
02492     QDialog* dlg = new KSpreadOsziDlg( this );
02493     dlg->show();
02494 }
02495 */
02496 
02497 void KSpreadView::changeTextColor()
02498 {
02499   if ( d->activeSheet != 0L )
02500   {
02501     doc()->emitBeginOperation(false);
02502     d->activeSheet->setSelectionTextColor( selectionInfo(), d->actions->textColor->color() );
02503     doc()->emitEndOperation( d->activeSheet->visibleRect( d->canvas ) );
02504   }
02505 }
02506 
02507 void KSpreadView::setSelectionTextColor(const QColor &txtColor)
02508 {
02509   if (d->activeSheet != 0L)
02510   {
02511     doc()->emitBeginOperation(false);
02512     d->activeSheet->setSelectionTextColor( selectionInfo(), txtColor );
02513     doc()->emitEndOperation( selectionInfo()->selection() );
02514   }
02515 }
02516 
02517 void KSpreadView::changeBackgroundColor()
02518 {
02519   if ( d->activeSheet != 0L )
02520   {
02521     doc()->emitBeginOperation(false);
02522     d->activeSheet->setSelectionbgColor( selectionInfo(), d->actions->bgColor->color() );
02523     doc()->emitEndOperation( d->activeSheet->visibleRect( d->canvas ) );
02524   }
02525 }
02526 
02527 void KSpreadView::setSelectionBackgroundColor(const QColor &bgColor)
02528 {
02529   if (d->activeSheet != 0L)
02530   {
02531     doc()->emitBeginOperation(false);
02532     d->activeSheet->setSelectionbgColor( selectionInfo(), bgColor );
02533     doc()->emitEndOperation( d->activeSheet->visibleRect( d->canvas ) );
02534   }
02535 }
02536 
02537 void KSpreadView::changeBorderColor()
02538 {
02539   if ( d->activeSheet != 0L )
02540   {
02541     doc()->emitBeginOperation(false);
02542     d->activeSheet->setSelectionBorderColor( selectionInfo(), d->actions->borderColor->color() );
02543     doc()->emitEndOperation( d->activeSheet->visibleRect( d->canvas ) );
02544   }
02545 }
02546 
02547 void KSpreadView::setSelectionBorderColor(const QColor &bdColor)
02548 {
02549   if (d->activeSheet != 0L)
02550   {
02551     doc()->emitBeginOperation(false);
02552     d->activeSheet->setSelectionBorderColor( selectionInfo(), bdColor );
02553     doc()->emitEndOperation( d->activeSheet->visibleRect( d->canvas ) );
02554   }
02555 }
02556 
02557 void KSpreadView::helpUsing()
02558 {
02559   kapp->invokeHelp( );
02560 }
02561 
02562 void KSpreadView::enableUndo( bool _b )
02563 {
02564   KAction* action = actionCollection()->action( "office_undo" );
02565   if( action ) action->setEnabled( _b );
02566 }
02567 
02568 void KSpreadView::enableRedo( bool _b )
02569 {
02570   KAction* action = actionCollection()->action( "office_redo" );
02571   if( action ) action->setEnabled( _b );
02572 }
02573 
02574 void KSpreadView::enableInsertColumn( bool _b )
02575 {
02576   if ( d->activeSheet && !d->activeSheet->isProtected() )
02577     d->actions->insertColumn->setEnabled( _b );
02578 }
02579 
02580 void KSpreadView::enableInsertRow( bool _b )
02581 {
02582   if ( d->activeSheet && !d->activeSheet->isProtected() )
02583     d->actions->insertRow->setEnabled( _b );
02584 }
02585 
02586 void KSpreadView::deleteColumn()
02587 {
02588   if ( !d->activeSheet )
02589     return;
02590 
02591   doc()->emitBeginOperation( false );
02592 
02593   QRect r( d->selectionInfo->selection() );
02594 
02595   d->activeSheet->removeColumn( r.left(), ( r.right()-r.left() ) );
02596 
02597   updateEditWidget();
02598   d->selectionInfo->setSelection( d->selectionInfo->marker(),
02599                                  d->selectionInfo->marker(), d->activeSheet );
02600 
02601   QRect vr( d->activeSheet->visibleRect( d->canvas ) );
02602   vr.setLeft( r.left() );
02603 
02604   doc()->emitEndOperation( vr );
02605 }
02606 
02607 void KSpreadView::deleteRow()
02608 {
02609   if ( !d->activeSheet )
02610     return;
02611 
02612   doc()->emitBeginOperation( false );
02613   QRect r( d->selectionInfo->selection() );
02614   d->activeSheet->removeRow( r.top(),(r.bottom()-r.top()) );
02615 
02616   updateEditWidget();
02617   d->selectionInfo->setSelection( d->selectionInfo->marker(),
02618                                  d->selectionInfo->marker(), d->activeSheet );
02619 
02620   QRect vr( d->activeSheet->visibleRect( d->canvas ) );
02621   vr.setTop( r.top() );
02622 
02623   doc()->emitEndOperation( vr );
02624 }
02625 
02626 void KSpreadView::insertColumn()
02627 {
02628   if ( !d->activeSheet )
02629     return;
02630 
02631   doc()->emitBeginOperation( false );
02632   QRect r( d->selectionInfo->selection() );
02633   d->activeSheet->insertColumn( r.left(), ( r.right()-r.left() ) );
02634 
02635   updateEditWidget();
02636 
02637   QRect vr( d->activeSheet->visibleRect( d->canvas ) );
02638   vr.setLeft( r.left() - 1 );
02639 
02640   doc()->emitEndOperation( vr );
02641 }
02642 
02643 void KSpreadView::hideColumn()
02644 {
02645   if ( !d->activeSheet )
02646     return;
02647   doc()->emitBeginOperation( false );
02648   QRect r( d->selectionInfo->selection() );
02649   d->activeSheet->hideColumn( r.left(), ( r.right()-r.left() ) );
02650 
02651   QRect vr( d->activeSheet->visibleRect( d->canvas ) );
02652   vr.setLeft( r.left() );
02653   doc()->emitEndOperation( vr );
02654 }
02655 
02656 void KSpreadView::showColumn()
02657 {
02658   if ( !d->activeSheet )
02659     return;
02660 
02661   KSpreadShowColRow dlg( this, "showCol", KSpreadShowColRow::Column );
02662   dlg.exec();
02663 }
02664 
02665 void KSpreadView::showSelColumns()
02666 {
02667   if ( !d->activeSheet )
02668     return;
02669 
02670   int i;
02671   QRect rect = d->selectionInfo->selection();
02672   ColumnFormat * col;
02673   QValueList<int>hiddenCols;
02674 
02675   doc()->emitBeginOperation( false );
02676 
02677   for ( i = rect.left(); i <= rect.right(); ++i )
02678   {
02679     if ( i == 2 ) // "B"
02680     {
02681       col = activeSheet()->columnFormat( 1 );
02682       if ( col->isHide() )
02683       {
02684         hiddenCols.append( 1 );
02685       }
02686     }
02687 
02688     col = d->activeSheet->columnFormat( i );
02689     if ( col->isHide() )
02690     {
02691       hiddenCols.append( i );
02692     }
02693   }
02694 
02695   if ( hiddenCols.count() > 0 )
02696     d->activeSheet->showColumn( 0, -1, hiddenCols );
02697 
02698   doc()->emitEndOperation( d->activeSheet->visibleRect( d->canvas ) );
02699 }
02700 
02701 void KSpreadView::insertRow()
02702 {
02703   if ( !d->activeSheet )
02704     return;
02705   doc()->emitBeginOperation( false );
02706   QRect r( d->selectionInfo->selection() );
02707   d->activeSheet->insertRow( r.top(), ( r.bottom() - r.top() ) );
02708 
02709   updateEditWidget();
02710   QRect vr( d->activeSheet->visibleRect( d->canvas ) );
02711   vr.setTop( r.top() - 1 );
02712 
02713   doc()->emitEndOperation( vr );
02714 }
02715 
02716 void KSpreadView::hideRow()
02717 {
02718   if ( !d->activeSheet )
02719     return;
02720 
02721   doc()->emitBeginOperation( false );
02722 
02723   QRect r( d->selectionInfo->selection() );
02724   d->activeSheet->hideRow( r.top(), ( r.bottom() - r.top() ) );
02725 
02726   QRect vr( d->activeSheet->visibleRect( d->canvas ) );
02727   vr.setTop( r.top() );
02728 
02729   doc()->emitEndOperation( vr );
02730 }
02731 
02732 void KSpreadView::showRow()
02733 {
02734   if ( !d->activeSheet )
02735     return;
02736 
02737   KSpreadShowColRow dlg( this, "showRow", KSpreadShowColRow::Row );
02738   dlg.exec();
02739 }
02740 
02741 void KSpreadView::showSelRows()
02742 {
02743   if ( !d->activeSheet )
02744     return;
02745 
02746   int i;
02747   QRect rect( d->selectionInfo->selection() );
02748   RowFormat * row;
02749   QValueList<int>hiddenRows;
02750 
02751   doc()->emitBeginOperation( false );
02752 
02753   for ( i = rect.top(); i <= rect.bottom(); ++i )
02754   {
02755     if ( i == 2 )
02756     {
02757       row = activeSheet()->rowFormat( 1 );
02758       if ( row->isHide() )
02759       {
02760         hiddenRows.append( 1 );
02761       }
02762     }
02763 
02764     row = d->activeSheet->rowFormat( i );
02765     if ( row->isHide() )
02766     {
02767       hiddenRows.append( i );
02768     }
02769   }
02770 
02771   if ( hiddenRows.count() > 0 )
02772     d->activeSheet->showRow( 0, -1, hiddenRows );
02773 
02774   endOperation( rect );
02775 }
02776 
02777 void KSpreadView::endOperation( QRect const & rect )
02778 {
02779   QRect vr( d->activeSheet->visibleRect( d->canvas ) );
02780   if ( rect.left() > vr.left() )
02781     vr.setLeft( rect.left() );
02782   if ( rect.top() > vr.top() )
02783     vr.setTop( rect.top() );
02784   if ( rect.right() < vr.right() )
02785     vr.setRight( rect.right() );
02786   if ( rect.bottom() < vr.bottom() )
02787     vr.setBottom( rect.bottom() );
02788 
02789   doc()->emitEndOperation( vr );
02790 }
02791 
02792 void KSpreadView::fontSelected( const QString & _font )
02793 {
02794   if ( d->toolbarLock )
02795     return;
02796 
02797   doc()->emitBeginOperation(false);
02798   if ( d->activeSheet != 0L )
02799     d->activeSheet->setSelectionFont( d->selectionInfo, _font.latin1() );
02800 
02801   // Dont leave the focus in the toolbars combo box ...
02802   if ( d->canvas->editor() )
02803   {
02804     KSpreadCell * cell = d->activeSheet->cellAt( d->selectionInfo->marker() );
02805     d->canvas->editor()->setEditorFont( cell->textFont( cell->column(), cell->row() ), true );
02806     d->canvas->editor()->setFocus();
02807   }
02808   else
02809     d->canvas->setFocus();
02810 
02811   endOperation( d->selectionInfo->selection() );
02812 }
02813 
02814 void KSpreadView::decreaseFontSize()
02815 {
02816   setSelectionFontSize( -1 );
02817 }
02818 
02819 void KSpreadView::increaseFontSize()
02820 {
02821   setSelectionFontSize( 1 );
02822 }
02823 
02824 void KSpreadView::setSelectionFontSize( int size )
02825 {
02826   if ( d->activeSheet != NULL )
02827   {
02828     doc()->emitBeginOperation( false );
02829     d->activeSheet->setSelectionSize( selectionInfo(), size );
02830     endOperation( d->selectionInfo->selection() );
02831   }
02832 }
02833 
02834 void KSpreadView::lower()
02835 {
02836   if ( !d->activeSheet  )
02837     return;
02838 
02839   doc()->emitBeginOperation( false );
02840 
02841   d->activeSheet->setSelectionUpperLower( selectionInfo(), -1 );
02842   updateEditWidget();
02843 
02844   endOperation( d->selectionInfo->selection() );
02845 }
02846 
02847 void KSpreadView::upper()
02848 {
02849   if ( !d->activeSheet  )
02850     return;
02851 
02852   doc()->emitBeginOperation( false );
02853 
02854   d->activeSheet->setSelectionUpperLower( selectionInfo(), 1 );
02855   updateEditWidget();
02856 
02857   endOperation( d->selectionInfo->selection() );
02858 }
02859 
02860 void KSpreadView::firstLetterUpper()
02861 {
02862   if ( !d->activeSheet  )
02863     return;
02864   doc()->emitBeginOperation( false );
02865   d->activeSheet->setSelectionfirstLetterUpper( selectionInfo() );
02866   updateEditWidget();
02867   endOperation( d->selectionInfo->selection() );
02868 }
02869 
02870 void KSpreadView::verticalText(bool b)
02871 {
02872   if ( !d->activeSheet  )
02873     return;
02874 
02875   doc()->emitBeginOperation( false );
02876   d->activeSheet->setSelectionVerticalText( selectionInfo(), b );
02877   if ( util_isRowSelected( selection() ) == FALSE
02878        && util_isColumnSelected( selection() ) == FALSE )
02879   {
02880     d->canvas->adjustArea( false );
02881     updateEditWidget();
02882     endOperation( d->selectionInfo->selection() );
02883     return;
02884   }
02885 
02886   doc()->emitEndOperation( QRect( d->selectionInfo->marker(), d->selectionInfo->marker() ) );
02887 }
02888 
02889 void KSpreadView::insertSpecialChar()
02890 {
02891   QString f( d->actions->selectFont->font() );
02892   QChar c = ' ';
02893 
02894   if ( d->specialCharDlg == 0 )
02895   {
02896     d->specialCharDlg = new KoCharSelectDia( this, "insert special char", f, c, false );
02897     connect( d->specialCharDlg, SIGNAL( insertChar( QChar, const QString & ) ),
02898              this, SLOT( slotSpecialChar( QChar, const QString & ) ) );
02899     connect( d->specialCharDlg, SIGNAL( finished() ),
02900              this, SLOT( slotSpecialCharDlgClosed() ) );
02901   }
02902   d->specialCharDlg->show();
02903 }
02904 
02905 void KSpreadView::slotSpecialCharDlgClosed()
02906 {
02907   if ( d->specialCharDlg )
02908   {
02909     disconnect( d->specialCharDlg, SIGNAL(insertChar(QChar,const QString &)),
02910                 this, SLOT(slotSpecialChar(QChar,const QString &)));
02911     disconnect( d->specialCharDlg, SIGNAL( finished() ),
02912                 this, SLOT( slotSpecialCharDlgClosed() ) );
02913     d->specialCharDlg->deleteLater();
02914     d->specialCharDlg = 0L;
02915   }
02916 }
02917 
02918 void KSpreadView::slotSpecialChar( QChar c, const QString & _font )
02919 {
02920   if ( d->activeSheet )
02921   {
02922     QPoint marker( selectionInfo()->marker() );
02923     KSpreadCell * cell = d->activeSheet->nonDefaultCell( marker );
02924     if ( cell->textFont( marker.x(), marker.y() ).family() != _font )
02925     {
02926       cell->setTextFontFamily( _font );
02927     }
02928     KSpreadEditWidget * edit = d->canvas->editWidget();
02929     QKeyEvent ev( QEvent::KeyPress, 0, 0, 0, QString( c ) );
02930     QApplication::sendEvent( edit, &ev );
02931   }
02932 }
02933 
02934 void KSpreadView::insertMathExpr()
02935 {
02936   if ( d->activeSheet == 0L )
02937     return;
02938 
02939   KSpreadDlgFormula * dlg = new KSpreadDlgFormula( this, "Function" );
02940   dlg->show();
02941 
02942   /* TODO - because I search on 'TODO's :-) */
02943   // #### Is the dialog deleted when it's closed ? (David)
02944   // Torben thinks that not.
02945 }
02946 
02947 void KSpreadView::formulaSelection( const QString &_math )
02948 {
02949   if ( d->activeSheet == 0 )
02950     return;
02951 
02952   if ( _math == i18n("Others...") )
02953   {
02954     insertMathExpr();
02955     return;
02956   }
02957 
02958   KSpreadDlgFormula *dlg = new KSpreadDlgFormula( this, "Formula Editor", _math );
02959   dlg->exec();
02960 }
02961 
02962 void KSpreadView::fontSizeSelected( int _size )
02963 {
02964   if ( d->toolbarLock )
02965     return;
02966 
02967   doc()->emitBeginOperation( false );
02968 
02969   if ( d->activeSheet != 0L )
02970     d->activeSheet->setSelectionFont( selectionInfo(), 0L, _size );
02971 
02972   // Dont leave the focus in the toolbars combo box ...
02973   if ( d->canvas->editor() )
02974   {
02975     KSpreadCell * cell = d->activeSheet->cellAt( d->selectionInfo->marker() );
02976     d->canvas->editor()->setEditorFont( cell->textFont( d->canvas->markerColumn(),
02977                                                         d->canvas->markerRow() ), true );
02978     d->canvas->editor()->setFocus();
02979   }
02980   else
02981     d->canvas->setFocus();
02982 
02983   endOperation( d->selectionInfo->selection() );
02984 }
02985 
02986 void KSpreadView::bold( bool b )
02987 {
02988   if ( d->toolbarLock )
02989     return;
02990   if ( d->activeSheet == 0 )
02991     return;
02992 
02993   doc()->emitBeginOperation( false );
02994 
02995   int col = d->canvas->markerColumn();
02996   int row = d->canvas->markerRow();
02997   d->activeSheet->setSelectionFont( selectionInfo(), 0L, -1, b );
02998 
02999   if ( d->canvas->editor() )
03000   {
03001     KSpreadCell * cell = d->activeSheet->cellAt( col, row );
03002     d->canvas->editor()->setEditorFont( cell->textFont( col, row ), true );
03003   }
03004 
03005   endOperation( d->selectionInfo->selection() );
03006 }
03007 
03008 void KSpreadView::underline( bool b )
03009 {
03010   if ( d->toolbarLock )
03011     return;
03012   if ( d->activeSheet == 0 )
03013     return;
03014 
03015   doc()->emitBeginOperation( false );
03016 
03017   int col = d->canvas->markerColumn();
03018   int row = d->canvas->markerRow();
03019 
03020   d->activeSheet->setSelectionFont( selectionInfo(), 0L, -1, -1, -1 ,b );
03021   if ( d->canvas->editor() )
03022   {
03023     KSpreadCell * cell = d->activeSheet->cellAt( col, row );
03024     d->canvas->editor()->setEditorFont( cell->textFont( col, row ), true );
03025   }
03026 
03027   endOperation( d->selectionInfo->selection() );
03028 }
03029 
03030 void KSpreadView::strikeOut( bool b )
03031 {
03032   if ( d->toolbarLock )
03033     return;
03034   if ( d->activeSheet == 0 )
03035     return;
03036 
03037   doc()->emitBeginOperation( false );
03038 
03039   int col = d->canvas->markerColumn();
03040   int row = d->canvas->markerRow();
03041 
03042   d->activeSheet->setSelectionFont( selectionInfo(), 0L, -1, -1, -1 ,-1, b );
03043   if ( d->canvas->editor() )
03044   {
03045     KSpreadCell * cell = d->activeSheet->cellAt( col, row );
03046     d->canvas->editor()->setEditorFont( cell->textFont( col, row ), true );
03047   }
03048 
03049   endOperation( d->selectionInfo->selection() );
03050 }
03051 
03052 
03053 void KSpreadView::italic( bool b )
03054 {
03055   if ( d->toolbarLock )
03056     return;
03057   if ( d->activeSheet == 0 )
03058     return;
03059 
03060   doc()->emitBeginOperation( false );
03061 
03062   int col = d->canvas->markerColumn();
03063   int row = d->canvas->markerRow();
03064 
03065   d->activeSheet->setSelectionFont( selectionInfo(), 0L, -1, -1, b );
03066   if ( d->canvas->editor() )
03067   {
03068     KSpreadCell * cell = d->activeSheet->cellAt( col, row );
03069     d->canvas->editor()->setEditorFont( cell->textFont( col, row ), true );
03070   }
03071 
03072   endOperation( d->selectionInfo->selection() );
03073 }
03074 
03075 void KSpreadView::sortInc()
03076 {
03077   QRect r( d->selectionInfo->selection() );
03078   if ( d->selectionInfo->singleCellSelection() )
03079   {
03080     KMessageBox::error( this, i18n( "You must select multiple cells." ) );
03081     return;
03082   }
03083 
03084   doc()->emitBeginOperation( false );
03085 
03086   // Entire row(s) selected ? Or just one row ?
03087   if ( util_isRowSelected( selection() ) || r.top() == r.bottom() )
03088     activeSheet()->sortByRow( selection(), r.top(), KSpreadSheet::Increase );
03089   else
03090     activeSheet()->sortByColumn( selection(), r.left(), KSpreadSheet::Increase );
03091   updateEditWidget();
03092 
03093   endOperation( d->selectionInfo->selection() );
03094 }
03095 
03096 void KSpreadView::sortDec()
03097 {
03098   QRect r( d->selectionInfo->selection() );
03099   if ( d->selectionInfo->singleCellSelection() )
03100   {
03101     KMessageBox::error( this, i18n( "You must select multiple cells." ) );
03102     return;
03103   }
03104 
03105   doc()->emitBeginOperation( false );
03106 
03107     // Entire row(s) selected ? Or just one row ?
03108   if ( util_isRowSelected( selection() ) || r.top() == r.bottom() )
03109     activeSheet()->sortByRow( selection(), r.top(), KSpreadSheet::Decrease );
03110   else
03111     activeSheet()->sortByColumn( selection(), r.left(), KSpreadSheet::Decrease );
03112   updateEditWidget();
03113 
03114   endOperation( d->selectionInfo->selection() );
03115 }
03116 
03117 
03118 void KSpreadView::borderBottom()
03119 {
03120   if ( d->activeSheet != 0L )
03121   {
03122     doc()->emitBeginOperation( false );
03123 
03124     d->activeSheet->borderBottom( d->selectionInfo, d->actions->borderColor->color() );
03125 
03126     endOperation( d->selectionInfo->selection() );
03127   }
03128 }
03129 
03130 void KSpreadView::setSelectionBottomBorderColor( const QColor & color )
03131 {
03132   if ( d->activeSheet != 0L )
03133   {
03134     doc()->emitBeginOperation( false );
03135     d->activeSheet->borderBottom( selectionInfo(), color );
03136     endOperation( d->selectionInfo->selection() );
03137   }
03138 }
03139 
03140 void KSpreadView::borderRight()
03141 {
03142   if ( d->activeSheet != 0L )
03143   {
03144     doc()->emitBeginOperation( false );
03145     if ( d->activeSheet->layoutDirection()==KSpreadSheet::RightToLeft )
03146       d->activeSheet->borderLeft( d->selectionInfo, d->actions->borderColor->color() );
03147     else
03148       d->activeSheet->borderRight( d->selectionInfo, d->actions->borderColor->color() );
03149     endOperation( d->selectionInfo->selection() );
03150   }
03151 }
03152 
03153 void KSpreadView::setSelectionRightBorderColor( const QColor & color )
03154 {
03155   if ( d->activeSheet != 0L )
03156   {
03157     doc()->emitBeginOperation( false );
03158     if ( d->activeSheet->layoutDirection()==KSpreadSheet::RightToLeft )
03159       d->activeSheet->borderLeft( selectionInfo(), color );
03160     else
03161       d->activeSheet->borderRight( selectionInfo(), color );
03162     endOperation( d->selectionInfo->selection() );
03163   }
03164 }
03165 
03166 void KSpreadView::borderLeft()
03167 {
03168   if ( d->activeSheet != 0L )
03169   {
03170     doc()->emitBeginOperation( false );
03171     if ( d->activeSheet->layoutDirection()==KSpreadSheet::RightToLeft )
03172       d->activeSheet->borderRight( d->selectionInfo, d->actions->borderColor->color() );
03173     else
03174       d->activeSheet->borderLeft( d->selectionInfo, d->actions->borderColor->color() );
03175     endOperation( d->selectionInfo->selection() );
03176   }
03177 }
03178 
03179 void KSpreadView::setSelectionLeftBorderColor( const QColor & color )
03180 {
03181   if ( d->activeSheet != 0L )
03182   {
03183     doc()->emitBeginOperation( false );
03184     if ( d->activeSheet->layoutDirection()==KSpreadSheet::RightToLeft )
03185       d->activeSheet->borderRight( selectionInfo(), color );
03186     else
03187       d->activeSheet->borderLeft( selectionInfo(), color );
03188     endOperation( d->selectionInfo->selection() );
03189   }
03190 }
03191 
03192 void KSpreadView::borderTop()
03193 {
03194   if ( d->activeSheet != 0L )
03195   {
03196     doc()->emitBeginOperation( false );
03197     d->activeSheet->borderTop( d->selectionInfo, d->actions->borderColor->color() );
03198     endOperation( d->selectionInfo->selection() );
03199   }
03200 }
03201 
03202 void KSpreadView::setSelectionTopBorderColor( const QColor & color )
03203 {
03204   if ( d->activeSheet != 0L )
03205   {
03206     doc()->emitBeginOperation( false );
03207     d->activeSheet->borderTop( selectionInfo(), color );
03208     endOperation( d->selectionInfo->selection() );
03209   }
03210 }
03211 
03212 void KSpreadView::borderOutline()
03213 {
03214   if ( d->activeSheet != 0L )
03215   {
03216     doc()->emitBeginOperation( false );
03217     d->activeSheet->borderOutline( d->selectionInfo, d->actions->borderColor->color() );
03218     endOperation( d->selectionInfo->selection() );
03219   }
03220 }
03221 
03222 void KSpreadView::setSelectionOutlineBorderColor( const QColor & color )
03223 {
03224   if ( d->activeSheet != 0L )
03225   {
03226     doc()->emitBeginOperation( false );
03227     d->activeSheet->borderOutline( selectionInfo(), color );
03228     endOperation( d->selectionInfo->selection() );
03229   }
03230 }
03231 
03232 void KSpreadView::borderAll()
03233 {
03234   if ( d->activeSheet != 0L )
03235   {
03236     doc()->emitBeginOperation( false );
03237     d->activeSheet->borderAll( d->selectionInfo, d->actions->borderColor->color() );
03238     endOperation( d->selectionInfo->selection() );
03239   }
03240 }
03241 
03242 void KSpreadView::setSelectionAllBorderColor( const QColor & color )
03243 {
03244   if ( d->activeSheet != 0L )
03245   {
03246     doc()->emitBeginOperation( false );
03247     d->activeSheet->borderAll( selectionInfo(), color );
03248     endOperation( d->selectionInfo->selection() );
03249   }
03250 }
03251 
03252 void KSpreadView::borderRemove()
03253 {
03254   if ( d->activeSheet != 0L )
03255   {
03256     doc()->emitBeginOperation(false);
03257     d->activeSheet->borderRemove( d->selectionInfo );
03258     endOperation( d->selectionInfo->selection() );
03259   }
03260 }
03261 
03262 void KSpreadView::addSheet( KSpreadSheet * _t )
03263 {
03264   doc()->emitBeginOperation( false );
03265 
03266   insertSheet( _t );
03267 
03268   // Connect some signals
03269   QObject::connect( _t, SIGNAL( sig_refreshView() ), SLOT( slotRefreshView() ) );
03270   QObject::connect( _t, SIGNAL( sig_updateView( KSpreadSheet* ) ), SLOT( slotUpdateView( KSpreadSheet* ) ) );
03271   QObject::connect( _t->print(), SIGNAL( sig_updateView( KSpreadSheet* ) ), SLOT( slotUpdateView( KSpreadSheet* ) ) );
03272   QObject::connect( _t, SIGNAL( sig_updateView( KSpreadSheet *, const QRect& ) ),
03273                     SLOT( slotUpdateView( KSpreadSheet*, const QRect& ) ) );
03274   QObject::connect( _t, SIGNAL( sig_updateHBorder( KSpreadSheet * ) ),
03275                     SLOT( slotUpdateHBorder( KSpreadSheet * ) ) );
03276   QObject::connect( _t, SIGNAL( sig_updateVBorder( KSpreadSheet * ) ),
03277                     SLOT( slotUpdateVBorder( KSpreadSheet * ) ) );
03278   QObject::connect( _t, SIGNAL( sig_nameChanged( KSpreadSheet*, const QString& ) ),
03279                     this, SLOT( slotSheetRenamed( KSpreadSheet*, const QString& ) ) );
03280   QObject::connect( _t, SIGNAL( sig_SheetHidden( KSpreadSheet* ) ),
03281                     this, SLOT( slotSheetHidden( KSpreadSheet* ) ) );
03282   QObject::connect( _t, SIGNAL( sig_SheetShown( KSpreadSheet* ) ),
03283                     this, SLOT( slotSheetShown( KSpreadSheet* ) ) );
03284   QObject::connect( _t, SIGNAL( sig_SheetRemoved( KSpreadSheet* ) ),
03285                     this, SLOT( slotSheetRemoved( KSpreadSheet* ) ) );
03286   // ########### Why do these signals not send a pointer to the sheet?
03287   // This will lead to bugs.
03288   QObject::connect( _t, SIGNAL( sig_updateChildGeometry( KSpreadChild* ) ),
03289                     SLOT( slotUpdateChildGeometry( KSpreadChild* ) ) );
03290   QObject::connect( _t, SIGNAL( sig_removeChild( KSpreadChild* ) ), SLOT( slotRemoveChild( KSpreadChild* ) ) );
03291   QObject::connect( _t, SIGNAL( sig_maxColumn( int ) ), d->canvas, SLOT( slotMaxColumn( int ) ) );
03292   QObject::connect( _t, SIGNAL( sig_maxRow( int ) ), d->canvas, SLOT( slotMaxRow( int ) ) );
03293 
03294   if ( !d->loading )
03295     updateBorderButton();
03296 
03297   if ( !d->activeSheet )
03298   {
03299     doc()->emitEndOperation();
03300     return;
03301   }
03302   endOperation( d->selectionInfo->selection() );
03303 }
03304 
03305 void KSpreadView::slotSheetRemoved( KSpreadSheet *_t )
03306 {
03307   doc()->emitBeginOperation( false );
03308 
03309   QString m_sheetName=_t->sheetName();
03310   d->tabBar->removeTab( _t->sheetName() );
03311   if (doc()->map()->findSheet( doc()->map()->visibleSheets().first()))
03312     setActiveSheet( doc()->map()->findSheet( doc()->map()->visibleSheets().first() ));
03313   else
03314     d->activeSheet = 0L;
03315 
03316   QValueList<Reference>::Iterator it;
03317   QValueList<Reference> area=doc()->listArea();
03318   for ( it = area.begin(); it != area.end(); ++it )
03319   {
03320     //remove Area Name when sheet target is removed
03321     if ( (*it).sheet_name == m_sheetName )
03322     {
03323       doc()->removeArea( (*it).ref_name );
03324       //now area name is used in formula
03325       //so you must recalc sheets when remove areaname
03326       KSpreadSheet * tbl;
03327 
03328       for ( tbl = doc()->map()->firstSheet(); tbl != 0L; tbl = doc()->map()->nextSheet() )
03329       {
03330         tbl->refreshRemoveAreaName((*it).ref_name);
03331       }
03332     }
03333   }
03334 
03335   endOperation( d->selectionInfo->selection() );
03336 }
03337 
03338 void KSpreadView::removeAllSheets()
03339 {
03340   doc()->emitBeginOperation(false);
03341   d->tabBar->clear();
03342 
03343   setActiveSheet( 0L );
03344 
03345   doc()->emitEndOperation();
03346 }
03347 
03348 void KSpreadView::setActiveSheet( KSpreadSheet * _t, bool updateSheet )
03349 {
03350   if ( _t == d->activeSheet )
03351     return;
03352 
03353   doc()->emitBeginOperation(false);
03354 
03355   saveCurrentSheetSelection();
03356 
03357   KSpreadSheet * oldSheet = d->activeSheet;
03358 
03359   d->activeSheet = _t;
03360 
03361   if ( d->activeSheet == 0L )
03362   {
03363     doc()->emitEndOperation();
03364     return;
03365   }
03366 
03367   if ( oldSheet && oldSheet->layoutDirection()==KSpreadSheet::RightToLeft != d->activeSheet->layoutDirection()==KSpreadSheet::RightToLeft )
03368     refreshView();
03369 
03370   doc()->setDisplaySheet( d->activeSheet );
03371   if ( updateSheet )
03372   {
03373     d->tabBar->setActiveTab( _t->sheetName() );
03374     d->vBorderWidget->repaint();
03375     d->hBorderWidget->repaint();
03376     d->activeSheet->setRegionPaintDirty(QRect(QPoint(0,0), QPoint(KS_colMax, KS_rowMax)));
03377     d->canvas->slotMaxColumn( d->activeSheet->maxColumn() );
03378     d->canvas->slotMaxRow( d->activeSheet->maxRow() );
03379   }
03380 
03381   d->actions->showPageBorders->setChecked( d->activeSheet->isShowPageBorders() );
03382   d->actions->protectSheet->setChecked( d->activeSheet->isProtected() );
03383   d->actions->protectDoc->setChecked( doc()->map()->isProtected() );
03384   d->adjustActions( !d->activeSheet->isProtected() );
03385   d->adjustWorkbookActions( !doc()->map()->isProtected() );
03386 
03387   /* see if there was a previous selection on this other sheet */
03388   QMapIterator<KSpreadSheet*, QPoint> it = d->savedAnchors.find(d->activeSheet);
03389   QMapIterator<KSpreadSheet*, QPoint> it2 = d->savedMarkers.find(d->activeSheet);
03390 
03391   QPoint newAnchor = (it == d->savedAnchors.end()) ? QPoint(1,1) : *it;
03392   QPoint newMarker = (it2 == d->savedMarkers.end()) ? QPoint(1,1) : *it2;
03393   selectionInfo()->setSelection(newMarker, newAnchor, d->activeSheet);
03394   if( d->canvas->chooseMode())
03395   {
03396     selectionInfo()->setChooseSheet( d->activeSheet );
03397     selectionInfo()->setChooseMarker( QPoint(0,0) );
03398   }
03399 
03400   d->canvas->scrollToCell(newMarker);
03401   resultOfCalc();
03402 
03403   doc()->emitEndOperation( d->activeSheet->visibleRect( d->canvas ) );
03404 }
03405 
03406 void KSpreadView::slotSheetRenamed( KSpreadSheet* sheet, const QString& old_name )
03407 {
03408   doc()->emitBeginOperation( false );
03409   d->tabBar->renameTab( old_name, sheet->sheetName() );
03410   doc()->emitEndOperation( d->activeSheet->visibleRect( d->canvas ) );
03411 }
03412 
03413 void KSpreadView::slotSheetHidden( KSpreadSheet* sheet )
03414 {
03415   doc()->emitBeginOperation(false);
03416   updateShowSheetMenu();
03417   doc()->emitEndOperation( d->activeSheet->visibleRect( d->canvas ) );
03418 }
03419 
03420 void KSpreadView::slotSheetShown( KSpreadSheet* sheet )
03421 {
03422   doc()->emitBeginOperation(false);
03423   d->tabBar->setTabs( doc()->map()->visibleSheets() );
03424   updateShowSheetMenu();
03425   doc()->emitEndOperation( d->activeSheet->visibleRect( d->canvas ) );
03426 }
03427 
03428 void KSpreadView::changeSheet( const QString& _name )
03429 {
03430     if ( activeSheet()->sheetName() == _name )
03431         return;
03432 
03433     KSpreadSheet *t = doc()->map()->findSheet( _name );
03434     if ( !t )
03435     {
03436         kdDebug(36001) << "Unknown sheet " << _name << endl;
03437         return;
03438     }
03439     doc()->emitBeginOperation(false);
03440     d->canvas->closeEditor();
03441     setActiveSheet( t, false /* False: Endless loop because of setActiveTab() => do the visual area update manually*/);
03442 
03443     updateEditWidget();
03444     //refresh toggle button
03445     updateBorderButton();
03446 
03447     //update visible area
03448     d->vBorderWidget->repaint();
03449     d->hBorderWidget->repaint();
03450     t->setRegionPaintDirty(QRect(QPoint(0,0), QPoint(KS_colMax, KS_rowMax)));
03451     d->canvas->slotMaxColumn( d->activeSheet->maxColumn() );
03452     d->canvas->slotMaxRow( d->activeSheet->maxRow() );
03453     doc()->emitEndOperation( d->activeSheet->visibleRect( d->canvas ) );
03454 }
03455 
03456 void KSpreadView::moveSheet( unsigned sheet, unsigned target )
03457 {
03458     QStringList vs = doc()->map()->visibleSheets();
03459 
03460     if( target >= vs.count() )
03461         doc()->map()->moveSheet( vs[ sheet ], vs[ vs.count()-1 ], false );
03462     else
03463         doc()->map()->moveSheet( vs[ sheet ], vs[ target ], true );
03464 
03465     d->tabBar->moveTab( sheet, target );
03466 }
03467 
03468 void KSpreadView::sheetProperties()
03469 {
03470     // sanity check, shouldn't happen
03471     if( doc()->map()->isProtected() ) return;
03472     if( d->activeSheet->isProtected() ) return;
03473 
03474     bool directionChanged = false;
03475 
03476     SheetPropertiesDialog* dlg = new SheetPropertiesDialog( this );
03477     dlg->setLayoutDirection( d->activeSheet->layoutDirection() );
03478     dlg->setAutoCalc( d->activeSheet->getAutoCalc() );
03479     dlg->setShowGrid( d->activeSheet->getShowGrid() );
03480     dlg->setShowPageBorders( d->activeSheet->isShowPageBorders() );
03481     dlg->setShowFormula( d->activeSheet->getShowFormula() );
03482     dlg->setHideZero( d->activeSheet->getHideZero() );
03483     dlg->setShowFormulaIndicator( d->activeSheet->getShowFormulaIndicator() );
03484     dlg->setColumnAsNumber( d->activeSheet->getShowColumnNumber() );
03485     dlg->setLcMode( d->activeSheet->getLcMode() );
03486     dlg->setCapitalizeFirstLetter( d->activeSheet->getFirstLetterUpper() );
03487 
03488     if( dlg->exec() )
03489     {
03490         SheetPropertiesCommand* command = new SheetPropertiesCommand( doc(), d->activeSheet );
03491 
03492         if ( d->activeSheet->layoutDirection() != dlg->layoutDirection() )
03493             directionChanged = true;
03494 
03495         command->setLayoutDirection( dlg->layoutDirection() );
03496         command->setAutoCalc( dlg->autoCalc() );
03497         command->setShowGrid( dlg->showGrid() );
03498         command->setShowPageBorders( dlg->showPageBorders() );
03499         command->setShowFormula( dlg->showFormula() );
03500         command->setHideZero( dlg->hideZero() );
03501         command->setShowFormulaIndicator( dlg->showFormulaIndicator() );
03502         command->setColumnAsNumber( dlg->columnAsNumber() );
03503         command->setLcMode( dlg->lcMode() );
03504         command->setCapitalizeFirstLetter( dlg->capitalizeFirstLetter() );
03505         doc()->addCommand( command );
03506         command->execute();
03507     }
03508 
03509     delete dlg;
03510 
03511     if ( directionChanged )
03512     {
03513         // the scrollbar and hborder remain reversed otherwise
03514         d->horzScrollBar->setValue( d->horzScrollBar->maxValue() -
03515                                             d->horzScrollBar->value() );
03516         d->hBorderWidget->update();
03517     }
03518 }
03519 
03520 void KSpreadView::insertSheet()
03521 {
03522   if ( doc()->map()->isProtected() )
03523   {
03524     KMessageBox::error( 0, i18n ( "You cannot change a protected sheet." ) );
03525     return;
03526   }
03527 
03528   doc()->emitBeginOperation( false );
03529   d->canvas->closeEditor();
03530   KSpreadSheet * t = doc()->map()->createSheet();
03531   KCommand* command = new AddSheetCommand( t );
03532   doc()->addCommand( command );
03533   updateEditWidget();
03534   setActiveSheet( t );
03535 
03536   if ( doc()->map()->visibleSheets().count() > 1 )
03537   {
03538     d->actions->removeSheet->setEnabled( true );
03539     d->actions->hideSheet->setEnabled( true );
03540   }
03541 
03542   doc()->emitEndOperation( d->activeSheet->visibleRect( d->canvas ) );
03543 }
03544 
03545 void KSpreadView::hideSheet()
03546 {
03547   if ( !d->activeSheet )
03548     return;
03549 
03550   if ( doc()->map()->visibleSheets().count() ==  1)
03551   {
03552      KMessageBox::error( this, i18n("You cannot hide the last visible sheet.") );
03553      return;
03554   }
03555 
03556   QStringList vs = doc()->map()->visibleSheets();
03557   int i = vs.findIndex( d->activeSheet->tableName() ) - 1;
03558   if( i < 0 ) i = 1;
03559   QString sn = vs[i];
03560 
03561   doc()->emitBeginOperation(false);
03562 
03563   KCommand* command = new HideSheetCommand( activeSheet() );
03564   doc()->addCommand( command );
03565   command->execute();
03566 
03567   doc()->emitEndOperation( d->activeSheet->visibleRect( d->canvas ) );
03568 
03569   d->tabBar->removeTab( d->activeSheet->sheetName() );
03570   d->tabBar->setActiveTab( sn );
03571 }
03572 
03573 void KSpreadView::showSheet()
03574 {
03575   if ( !d->activeSheet )
03576     return;
03577 
03578   KSpreadshow dlg( this, "Sheet show");
03579   dlg.exec();
03580 }
03581 
03582 void KSpreadView::copySelection()
03583 {
03584   if ( !d->activeSheet )
03585     return;
03586   if ( !d->canvas->editor() )
03587   {
03588     d->activeSheet->copySelection( selectionInfo() );
03589 
03590     updateEditWidget();
03591   }
03592   else
03593     d->canvas->editor()->copy();
03594 }
03595 
03596 void KSpreadView::copyAsText()
03597 {
03598   if ( !d->activeSheet )
03599     return;
03600   d->activeSheet->copyAsText( selectionInfo() );
03601 }
03602 
03603 
03604 void KSpreadView::cutSelection()
03605 {
03606   if ( !d->activeSheet )
03607     return;
03608   //don't used this function when we edit a cell.
03609   doc()->emitBeginOperation(false);
03610 
03611   if ( !d->canvas->editor())
03612   {
03613     d->activeSheet->cutSelection( selectionInfo() );
03614     resultOfCalc();
03615     updateEditWidget();
03616     }
03617   else
03618     d->canvas->editor()->cut();
03619 
03620   endOperation( selectionInfo()->selection() );
03621 }
03622 
03623 void KSpreadView::paste()
03624 {
03625   if ( !d->activeSheet )
03626     return;
03627 
03628   if (!koDocument()->isReadWrite()) // don't paste into a read only document
03629     return;
03630 
03631 
03632   doc()->emitBeginOperation( false );
03633   if ( !d->canvas->editor() )
03634   {
03635       //kdDebug(36001) << "Pasting. Rect= " << selection(false) << " bytes" << endl;
03636     d->activeSheet->paste( selection(false), true, Normal, OverWrite, false, 0, true );
03637     resultOfCalc();
03638     updateEditWidget();
03639   }
03640   else
03641   {
03642     d->canvas->editor()->paste();
03643   }
03644   doc()->emitEndOperation( d->activeSheet->visibleRect( d->canvas ) );
03645 }
03646 
03647 void KSpreadView::specialPaste()
03648 {
03649   if ( !d->activeSheet )
03650     return;
03651 
03652   KSpreadspecial dlg( this, "Special Paste" );
03653   if ( dlg.exec() )
03654   {
03655     if ( d->activeSheet->getAutoCalc() )
03656     {
03657       doc()->emitBeginOperation( false );
03658       d->activeSheet->recalc();
03659       doc()->emitEndOperation( d->activeSheet->visibleRect( d->canvas ) );
03660     }
03661     resultOfCalc();
03662     updateEditWidget();
03663   }
03664 }
03665 
03666 void KSpreadView::removeComment()
03667 {
03668   if ( !d->activeSheet )
03669         return;
03670 
03671   doc()->emitBeginOperation(false);
03672   d->activeSheet->setSelectionRemoveComment( selectionInfo() );
03673   updateEditWidget();
03674   endOperation( selectionInfo()->selection() );
03675 }
03676 
03677 
03678 void KSpreadView::changeAngle()
03679 {
03680   if ( !d->activeSheet )
03681     return;
03682 
03683   KSpreadAngle dlg( this, "Angle" ,
03684                     QPoint( d->canvas->markerColumn(), d->canvas->markerRow() ));
03685   if ( dlg.exec() )
03686   {
03687     if ( (util_isRowSelected(selection()) == FALSE) &&
03688         (util_isColumnSelected(selection()) == FALSE) )
03689     {
03690       doc()->emitBeginOperation( false );
03691       d->canvas->adjustArea( false );
03692       endOperation( selectionInfo()->selection() );
03693     }
03694   }
03695 }
03696 
03697 void KSpreadView::setSelectionAngle( int angle )
03698 {
03699   doc()->emitBeginOperation( false );
03700 
03701   if ( d->activeSheet != NULL )
03702   {
03703     d->activeSheet->setSelectionAngle( selectionInfo(), angle );
03704 
03705     if (util_isRowSelected(selection()) == false &&
03706         util_isColumnSelected(selection()) == false)
03707     {
03708       d->canvas->adjustArea(false);
03709     }
03710   }
03711 
03712   endOperation( selectionInfo()->selection() );
03713 }
03714 
03715 void KSpreadView::mergeCell()
03716 {
03717   // sanity check
03718   if( !d->activeSheet )
03719     return;
03720   if( d->activeSheet->isProtected() )
03721     return;
03722   if( doc()->map()->isProtected() )
03723     return;
03724 
03725   if ( ( util_isRowSelected( selection() ) )
03726        || ( util_isColumnSelected( selection() ) ) )
03727   {
03728     KMessageBox::error( this, i18n( "Area is too large." ) );
03729     return;
03730   }
03731 
03732   QPoint topLeft = selection().topLeft();
03733   KSpreadCell *cell = d->activeSheet->nonDefaultCell( topLeft );
03734   KCommand* command = new MergeCellCommand( cell, selection().width() - 1,
03735       selection().height() - 1);
03736   doc()->addCommand( command );
03737   command->execute();
03738 }
03739 
03740 void KSpreadView::dissociateCell()
03741 {
03742   // sanity check
03743   if( !d->activeSheet )
03744     return;
03745   if( d->activeSheet->isProtected() )
03746     return;
03747   if( doc()->map()->isProtected() )
03748     return;
03749 
03750   KSpreadCell* cell = d->activeSheet->nonDefaultCell( QPoint( d->canvas->markerColumn(),
03751       d->canvas->markerRow() ) );
03752   KCommand* command = new DissociateCellCommand( cell );
03753   doc()->addCommand( command );
03754   command->execute();
03755 }
03756 
03757 
03758 void KSpreadView::increaseIndent()
03759 {
03760   if ( !d->activeSheet )
03761     return;
03762 
03763   doc()->emitBeginOperation( false );
03764   d->activeSheet->increaseIndent( d->selectionInfo );
03765   updateEditWidget();
03766   endOperation( d->selectionInfo->selection() );
03767 }
03768 
03769 void KSpreadView::decreaseIndent()
03770 {
03771   if ( !d->activeSheet )
03772     return;
03773 
03774   doc()->emitBeginOperation( false );
03775   int column = d->canvas->markerColumn();
03776   int row = d->canvas->markerRow();
03777 
03778   d->activeSheet->decreaseIndent( d->selectionInfo );
03779   KSpreadCell * cell = d->activeSheet->cellAt( column, row );
03780   if ( cell )
03781     if ( !d->activeSheet->isProtected() )
03782       d->actions->decreaseIndent->setEnabled( cell->getIndent( column, row ) > 0.0 );
03783 
03784   endOperation( d->selectionInfo->selection() );
03785 }
03786 
03787 void KSpreadView::goalSeek()
03788 {
03789   if ( d->canvas->editor() )
03790   {
03791     d->canvas->deleteEditor( true ); // save changes
03792   }
03793 
03794   KSpreadGoalSeekDlg * dlg
03795     = new KSpreadGoalSeekDlg( this, QPoint( d->canvas->markerColumn(),
03796                                             d->canvas->markerRow() ),
03797                               "KSpreadGoalSeekDlg" );
03798   dlg->show();
03799   /* dialog autodeletes itself */
03800 }
03801 
03802 void KSpreadView::subtotals()
03803 {
03804   QRect selection( d->selectionInfo->selection() );
03805   if ( ( selection.width() < 2 )
03806        || ( selection.height() < 2 ) )
03807   {
03808     KMessageBox::error( this, i18n("You must select multiple cells.") );
03809     return;
03810   }
03811 
03812   KSpreadSubtotalDlg dlg(this, selection, "KSpreadSubtotalDlg" );
03813   if ( dlg.exec() )
03814   {
03815     doc()->emitBeginOperation( false );
03816     d->selectionInfo->setSelection( dlg.selection().topLeft(),
03817                                    dlg.selection().bottomRight(),
03818                                    dlg.sheet() );
03819     endOperation( selection );
03820   }
03821 }
03822 
03823 void KSpreadView::multipleOperations()
03824 {
03825   if ( d->canvas->editor() )
03826   {
03827     d->canvas->deleteEditor( true ); // save changes
03828   }
03829   //  KSpreadMultipleOpDlg * dlg = new KSpreadMultipleOpDlg( this, "KSpreadMultipleOpDlg" );
03830   //  dlg->show();
03831 }
03832 
03833 void KSpreadView::textToColumns()
03834 {
03835   d->canvas->closeEditor();
03836   if ( d->selectionInfo->selection().width() > 1 )
03837   {
03838     KMessageBox::error( this, i18n("You must not select an area containing more than one column.") );
03839     return;
03840   }
03841 
03842   KSpreadCSVDialog dialog( this, "KSpreadCSVDialog", d->selectionInfo->selection(), KSpreadCSVDialog::Column );
03843   if( !dialog.cancelled() )
03844     dialog.exec();
03845 }
03846 
03847 void KSpreadView::consolidate()
03848 {
03849   d->canvas->closeEditor();
03850   KSpreadConsolidate * dlg = new KSpreadConsolidate( this, "Consolidate" );
03851   dlg->show();
03852   // dlg destroys itself
03853 }
03854 
03855 void KSpreadView::sortList()
03856 {
03857   KSpreadList dlg( this, "List selection" );
03858   dlg.exec();
03859 }
03860 
03861 void KSpreadView::gotoCell()
03862 {
03863   KSpreadGotoDlg dlg( this, "GotoCell" );
03864   dlg.exec();
03865 }
03866 
03867 void KSpreadView::find()
03868 {
03869     KFindDialog dlg( this, "Find", d->findOptions, d->findStrings );
03870     dlg.setHasSelection( !d->selectionInfo->singleCellSelection() );
03871     dlg.setHasCursor( true );
03872     if ( KFindDialog::Accepted != dlg.exec() )
03873         return;
03874 
03875     // Save for next time
03876     d->findOptions = dlg.options();
03877     d->findStrings = dlg.findHistory();
03878 
03879     // Create the KFind object
03880     delete d->find;
03881     delete d->replace;
03882     d->find = new KFind( dlg.pattern(), dlg.options(), this );
03883     d->replace = 0L;
03884 
03885     initFindReplace();
03886     findNext();
03887 }
03888 
03889 // Initialize a find or replace operation, using d->find or d->replace,
03890 // and d->findOptions.
03891 void KSpreadView::initFindReplace()
03892 {
03893     KFind* findObj = d->find ? d->find : d->replace;
03894     Q_ASSERT( findObj );
03895     connect(findObj, SIGNAL( highlight( const QString &, int, int ) ),
03896             this, SLOT( slotHighlight( const QString &, int, int ) ) );
03897     connect(findObj, SIGNAL( findNext() ),
03898             this, SLOT( findNext() ) );
03899 
03900     bool bck = d->findOptions & KFindDialog::FindBackwards;
03901     KSpreadSheet* currentSheet = activeSheet();
03902 
03903     QRect region = ( d->findOptions & KFindDialog::SelectedText )
03904                    ? d->selectionInfo->selection()
03905                    : QRect( 1, 1, currentSheet->maxColumn(), currentSheet->maxRow() ); // All cells
03906 
03907     int colStart = !bck ? region.left() : region.right();
03908     int colEnd = !bck ? region.right() : region.left();
03909     int rowStart = !bck ? region.top() :region.bottom();
03910     int rowEnd = !bck ? region.bottom() : region.top();
03911     if ( d->findOptions & KFindDialog::FromCursor ) {
03912         QPoint marker( d->selectionInfo->marker() );
03913         colStart = marker.x();
03914         rowStart = marker.y();
03915     }
03916     d->findLeftColumn = region.left();
03917     d->findRightColumn = region.right();
03918     d->findPos = QPoint( colStart, rowStart );
03919     d->findEnd = QPoint( colEnd, rowEnd );
03920     //kdDebug() << k_funcinfo << d->findPos << " to " << d->findEnd << endl;
03921     //kdDebug() << k_funcinfo << "leftcol=" << d->findLeftColumn << " rightcol=" << d->findRightColumn << endl;
03922 }
03923 
03924 void KSpreadView::findNext()
03925 {
03926     KFind* findObj = d->find ? d->find : d->replace;
03927     if ( !findObj )  {
03928         find();
03929         return;
03930     }
03931     KFind::Result res = KFind::NoMatch;
03932     KSpreadCell* cell = findNextCell();
03933     bool forw = ! ( d->findOptions & KFindDialog::FindBackwards );
03934     while ( res == KFind::NoMatch && cell )
03935     {
03936         if ( findObj->needData() )
03937         {
03938             findObj->setData( cell->text() );
03939             d->findPos = QPoint( cell->column(), cell->row() );
03940             //kdDebug() << "setData(cell " << d->findPos << ")" << endl;
03941         }
03942 
03943         // Let KFind inspect the text fragment, and display a dialog if a match is found
03944         if ( d->find )
03945             res = d->find->find();
03946         else
03947             res = d->replace->replace();
03948 
03949         if ( res == KFind::NoMatch )  {
03950             // Go to next cell, skipping unwanted cells
03951             if ( forw )
03952                 ++d->findPos.rx();
03953             else
03954                 --d->findPos.rx();
03955             cell = findNextCell();
03956         }
03957     }
03958 
03959     if ( res == KFind::NoMatch )
03960     {
03961         //emitUndoRedo();
03962         //removeHighlight();
03963         if ( findObj->shouldRestart() ) {
03964             d->findOptions &= ~KFindDialog::FromCursor;
03965             findObj->resetCounts();
03966             findNext();
03967         }
03968         else { // done, close the 'find next' dialog
03969             if ( d->find )
03970                 d->find->closeFindNextDialog();
03971             else
03972                 d->replace->closeReplaceNextDialog();
03973         }
03974     }
03975 }
03976 
03977 KSpreadCell* KSpreadView::findNextCell()
03978 {
03979     // getFirstCellRow / getNextCellRight would be faster at doing that,
03980     // but it doesn't seem to be easy to combine it with 'start a column d->find.x()'...
03981 
03982     KSpreadSheet* sheet = activeSheet();
03983     KSpreadCell* cell = 0L;
03984     bool forw = ! ( d->findOptions & KFindDialog::FindBackwards );
03985     int col = d->findPos.x();
03986     int row = d->findPos.y();
03987     int maxRow = sheet->maxRow();
03988     //kdDebug() << "findNextCell starting at " << col << "," << row << "   forw=" << forw << endl;
03989 
03990     while ( !cell && row != d->findEnd.y() && (forw ? row < maxRow : row >= 0) )
03991     {
03992         while ( !cell && (forw ? col <= d->findRightColumn : col >= d->findLeftColumn) )
03993         {
03994             cell = sheet->cellAt( col, row );
03995             if ( cell->isDefault() || cell->isObscured() || cell->isFormula() )
03996                 cell = 0L;
03997             if ( forw ) ++col;
03998             else --col;
03999         }
04000         if ( cell )
04001             break;
04002         // Prepare looking in the next row
04003         if ( forw )  {
04004             col = d->findLeftColumn;
04005             ++row;
04006         } else {
04007             col = d->findRightColumn;
04008             --row;
04009         }
04010         //kdDebug() << "next row: " << col << "," << row << endl;
04011     }
04012     // if ( !cell )
04013     // No more next cell - TODO go to next sheet (if not looking in a selection)
04014     // (and make d->findEnd (max,max) in that case...)
04015     //kdDebug() << k_funcinfo << " returning " << cell << endl;
04016     return cell;
04017 }
04018 
04019 void KSpreadView::findPrevious()
04020 {
04021     KFind* findObj = d->find ? d->find : d->replace;
04022     if ( !findObj )  {
04023         find();
04024         return;
04025     }
04026     //kdDebug() << "findPrevious" << endl;
04027     int opt = d->findOptions;
04028     bool forw = ! ( opt & KFindDialog::FindBackwards );
04029     if ( forw )
04030         d->findOptions = ( opt | KFindDialog::FindBackwards );
04031     else
04032         d->findOptions = ( opt & ~KFindDialog::FindBackwards );
04033 
04034     findNext();
04035 
04036     d->findOptions = opt; // restore initial options
04037 }
04038 
04039 void KSpreadView::replace()
04040 {
04041     KReplaceDialog dlg( this, "Replace", d->findOptions, d->findStrings, d->replaceStrings );
04042     dlg.setHasSelection( !d->selectionInfo->singleCellSelection() );
04043     dlg.setHasCursor( true );
04044     if ( KReplaceDialog::Accepted != dlg.exec() )
04045       return;
04046 
04047     d->findOptions = dlg.options();
04048     d->findStrings = dlg.findHistory();
04049     d->replaceStrings = dlg.replacementHistory();
04050 
04051     delete d->find;
04052     delete d->replace;
04053     d->find = 0L;
04054     d->replace = new KReplace( dlg.pattern(), dlg.replacement(), dlg.options() );
04055     initFindReplace();
04056     connect(
04057         d->replace, SIGNAL( replace( const QString &, int, int, int ) ),
04058         this, SLOT( slotReplace( const QString &, int, int, int ) ) );
04059 
04060     if ( !doc()->undoLocked() )
04061     {
04062         QRect region( d->findPos, d->findEnd );
04063         KSpreadUndoChangeAreaTextCell *undo = new KSpreadUndoChangeAreaTextCell( doc(), activeSheet(), region );
04064         doc()->addCommand( undo );
04065     }
04066 
04067     findNext();
04068 
04069 #if 0
04070     // Refresh the editWidget
04071     // TODO - after a replacement only?
04072     KSpreadCell *cell = activeSheet()->cellAt( canvasWidget()->markerColumn(),
04073                                                canvasWidget()->markerRow() );
04074     if ( cell->text() != 0L )
04075         editWidget()->setText( cell->text() );
04076     else
04077         editWidget()->setText( "" );
04078 #endif
04079 }
04080 
04081 void KSpreadView::slotHighlight( const QString &/*text*/, int /*matchingIndex*/, int /*matchedLength*/ )
04082 {
04083     d->canvas->gotoLocation( d->findPos, activeSheet() );
04084     KDialogBase *baseDialog=0L;
04085     if ( d->find )
04086         baseDialog = d->find->findNextDialog();
04087     else
04088         baseDialog = d->replace->replaceNextDialog();
04089     kdDebug()<<" baseDialog :"<<baseDialog<<endl;
04090     QRect globalRect( d->findPos, d->findEnd );
04091     globalRect.moveTopLeft( canvasWidget()->mapToGlobal( globalRect.topLeft() ) );
04092     KDialog::avoidArea( baseDialog, QRect( d->findPos, d->findEnd ));
04093 }
04094 
04095 void KSpreadView::slotReplace( const QString &newText, int, int, int )
04096 {
04097     // Which cell was this again?
04098     KSpreadCell *cell = activeSheet()->cellAt( d->findPos );
04099 
04100     // ...now I remember, update it!
04101     cell->setDisplayDirtyFlag();
04102     cell->setCellText( newText );
04103     cell->clearDisplayDirtyFlag();
04104 }
04105 
04106 void KSpreadView::conditional()
04107 {
04108   QRect rect( d->selectionInfo->selection() );
04109 
04110   if ( (util_isRowSelected(selection())) || (util_isColumnSelected(selection())) )
04111   {
04112     KMessageBox::error( this, i18n("Area is too large.") );
04113   }
04114   else
04115   {
04116     KSpreadConditionalDlg dlg( this, "KSpreadConditionalDlg", rect);
04117     dlg.exec();
04118   }
04119 }
04120 
04121 void KSpreadView::validity()
04122 {
04123   QRect rect( d->selectionInfo->selection() );
04124 
04125   if ( (util_isRowSelected(selection())) || (util_isColumnSelected(selection())) )
04126   {
04127     KMessageBox::error( this, i18n("Area is too large."));
04128   }
04129   else
04130   {
04131     KSpreadDlgValidity dlg( this,"validity",rect);
04132     dlg.exec();
04133   }
04134 }
04135 
04136 
04137 void KSpreadView::insertSeries()
04138 {
04139     d->canvas->closeEditor();
04140     KSpreadSeriesDlg dlg( this, "Series", QPoint( d->canvas->markerColumn(), d->canvas->markerRow() ) );
04141     dlg.exec();
04142 }
04143 
04144 void KSpreadView::sort()
04145 {
04146     if ( d->selectionInfo->singleCellSelection() )
04147     {
04148         KMessageBox::error( this, i18n("You must select multiple cells.") );
04149         return;
04150     }
04151 
04152     KSpreadSortDlg dlg( this, "Sort" );
04153     dlg.exec();
04154 }
04155 
04156 void KSpreadView::removeHyperlink()
04157 {
04158     QPoint marker( selectionInfo()->marker() );
04159     KSpreadCell * cell = d->activeSheet->cellAt( marker );
04160     if( !cell ) return;
04161     if( cell->link().isEmpty() ) return;
04162 
04163     LinkCommand* command = new LinkCommand( cell, QString::null, QString::null );
04164     doc()->addCommand( command );
04165     command->execute();
04166 
04167     canvasWidget()->setFocus();
04168     editWidget()->setText( cell->text() );
04169 }
04170 
04171 void KSpreadView::insertHyperlink()
04172 {
04173     d->canvas->closeEditor();
04174 
04175     QPoint marker( selectionInfo()->marker() );
04176     KSpreadCell* cell = d->activeSheet->cellAt( marker );
04177 
04178     LinkDialog* dlg = new LinkDialog( this );
04179     dlg->setCaption( i18n( "Insert Link" ) );
04180     if( cell )
04181     {
04182       dlg->setText( cell->text() );
04183       if( !cell->link().isEmpty() )
04184       {
04185         dlg->setCaption( i18n( "Edit Link" ) );
04186         dlg->setLink( cell->link() );
04187       }
04188     }
04189 
04190     if( dlg->exec() == KDialog::Accepted )
04191     {
04192         cell = d->activeSheet->nonDefaultCell( marker );
04193 
04194         LinkCommand* command = new LinkCommand( cell, dlg->text(), dlg->link() );
04195         doc()->addCommand( command );
04196         command->execute();
04197 
04198         //refresh editWidget
04199         canvasWidget()->setFocus();
04200         editWidget()->setText( cell->text() );
04201     }
04202     delete dlg;
04203 }
04204 
04205 void KSpreadView::insertFromDatabase()
04206 {
04207 #ifndef QT_NO_SQL
04208     d->canvas->closeEditor();
04209 
04210     QRect rect = d->selectionInfo->selection();
04211 
04212     KSpreadDatabaseDlg dlg(this, rect, "KSpreadDatabaseDlg");
04213     dlg.exec();
04214 #endif
04215 }
04216 
04217 void KSpreadView::insertFromTextfile()
04218 {
04219     d->canvas->closeEditor();
04220     //KMessageBox::information( this, "Not implemented yet, work in progress...");
04221 
04222     KSpreadCSVDialog dialog( this, "KSpreadCSVDialog", selection(), KSpreadCSVDialog::File );
04223     if( !dialog.cancelled() )
04224       dialog.exec();
04225 }
04226 
04227 void KSpreadView::insertFromClipboard()
04228 {
04229     d->canvas->closeEditor();
04230 
04231     KSpreadCSVDialog dialog( this, "KSpreadCSVDialog", d->selectionInfo->selection(), KSpreadCSVDialog::Clipboard );
04232     if( !dialog.cancelled() )
04233       dialog.exec();
04234 }
04235 
04236 void KSpreadView::setupPrinter( KPrinter &prt )
04237 {
04238     KSpreadSheetPrint* print = d->activeSheet->print();
04239 
04240     //apply page layout parameters
04241     KoFormat pageFormat = print->paperFormat();
04242 
04243     prt.setPageSize( static_cast<KPrinter::PageSize>( KoPageFormat::printerPageSize( pageFormat ) ) );
04244 
04245     if ( print->orientation() == PG_LANDSCAPE || pageFormat == PG_SCREEN )
04246         prt.setOrientation( KPrinter::Landscape );
04247     else
04248         prt.setOrientation( KPrinter::Portrait );
04249 
04250     prt.setFullPage( TRUE );
04251 }
04252 
04253 void KSpreadView::print( KPrinter &prt )
04254 {
04255     KSpreadSheetPrint* print = d->activeSheet->print();
04256 
04257     if ( d->canvas->editor() )
04258     {
04259       d->canvas->deleteEditor( true ); // save changes
04260     }
04261 
04262     int oldZoom = doc()->zoom();
04263 
04264     //Comment from KWord
04265     //   We don't get valid metrics from the printer - and we want a better resolution
04266     //   anyway (it's the PS driver that takes care of the printer resolution).
04267     //But KSpread uses fixed 300 dpis, so we can use it.
04268 
04269     QPaintDeviceMetrics metrics( &prt );
04270 
04271     int dpiX = metrics.logicalDpiX();
04272     int dpiY = metrics.logicalDpiY();
04273 
04274     doc()->setZoomAndResolution( int( print->zoom() * 100 ), dpiX, dpiY );
04275 
04276     //store the current setting in a temporary variable
04277     KoOrientation _orient = print->orientation();
04278 
04279     QPainter painter;
04280 
04281     painter.begin( &prt );
04282 
04283     //use the current orientation from print dialog
04284     if ( prt.orientation() == KPrinter::Landscape )
04285     {
04286         print->setPaperOrientation( PG_LANDSCAPE );
04287     }
04288     else
04289     {
04290         print->setPaperOrientation( PG_PORTRAIT );
04291     }
04292 
04293     bool result = print->print( painter, &prt );
04294 
04295     //Restore original orientation
04296     print->setPaperOrientation( _orient );
04297 
04298     doc()->setZoomAndResolution( oldZoom, KoGlobal::dpiX(), KoGlobal::dpiY() );
04299     doc()->newZoomAndResolution( true, false );
04300 
04301     // Repaint at correct zoom
04302     doc()->emitBeginOperation( false );
04303     setZoom( oldZoom, false );
04304     doc()->emitEndOperation();
04305 
04306     // Nothing to print
04307     if( !result )
04308     {
04309       if( !prt.previewOnly() )
04310       {
04311         KMessageBox::information( 0, i18n("Nothing to print.") );
04312         prt.abort();
04313       }
04314     }
04315 
04316     painter.end();
04317 }
04318 
04319 void KSpreadView::insertChart( const QRect& _geometry, KoDocumentEntry& _e )
04320 {
04321     if ( !d->activeSheet )
04322       return;
04323 
04324     // Transform the view coordinates to document coordinates
04325     KoRect unzoomedRect = doc()->unzoomRect( _geometry );
04326     unzoomedRect.moveBy( d->canvas->xOffset(), d->canvas->yOffset() );
04327 
04328     //KOfficeCore cannot handle KoRect directly, so switching to QRect
04329     QRect unzoomedGeometry = unzoomedRect.toQRect();
04330 
04331     if ( (util_isRowSelected(selection())) || (util_isColumnSelected(selection())) )
04332     {
04333       KMessageBox::error( this, i18n("Area is too large."));
04334       d->activeSheet->insertChart( unzoomedGeometry,
04335                              _e,
04336                              QRect( d->canvas->markerColumn(),
04337                                     d->canvas->markerRow(),
04338                                     1,
04339                                     1 ) );
04340     }
04341     else
04342     {
04343       // Insert the new child in the active sheet.
04344       d->activeSheet->insertChart( unzoomedGeometry,
04345                              _e,
04346                              d->selectionInfo->selection() );
04347     }
04348 }
04349 
04350 void KSpreadView::insertChild( const QRect& _geometry, KoDocumentEntry& _e )
04351 {
04352   if ( !d->activeSheet )
04353     return;
04354 
04355   // Transform the view coordinates to document coordinates
04356   KoRect unzoomedRect = doc()->unzoomRect( _geometry );
04357   unzoomedRect.moveBy( d->canvas->xOffset(), d->canvas->yOffset() );
04358 
04359   //KOfficeCore cannot handle KoRect directly, so switching to QRect
04360   QRect unzoomedGeometry = unzoomedRect.toQRect();
04361 
04362   // Insert the new child in the active sheet.
04363   d->activeSheet->insertChild( unzoomedGeometry, _e );
04364 }
04365 
04366 void KSpreadView::slotRemoveChild( KSpreadChild *_child )
04367 {
04368   if ( _child->sheet() != d->activeSheet )
04369     return;
04370 
04371   // Make shure that this child has no active embedded view -> activate ourselfs
04372   doc()->emitBeginOperation( false );
04373   partManager()->setActivePart( koDocument(), this );
04374   partManager()->setSelectedPart( 0 );
04375   doc()->emitEndOperation( d->activeSheet->visibleRect( d->canvas ) );
04376 }
04377 
04378 void KSpreadView::slotUpdateChildGeometry( KSpreadChild */*_child*/ )
04379 {
04380     // ##############
04381     // TODO
04382     /*
04383   if ( _child->sheet() != d->activeSheet )
04384     return;
04385 
04386   // Find frame for child
04387   KSpreadChildFrame *f = 0L;
04388   QPtrListIterator<KSpreadChildFrame> it( m_lstFrames );
04389   for ( ; it.current() && !f; ++it )
04390     if ( it.current()->child() == _child )
04391       f = it.current();
04392 
04393   assert( f != 0L );
04394 
04395   // Are we already up to date ?
04396   if ( _child->geometry() == f->partGeometry() )
04397     return;
04398 
04399   // TODO zooming
04400   f->setPartGeometry( _child->geometry() );
04401     */
04402 }
04403 
04404 void KSpreadView::toggleProtectDoc( bool mode )
04405 {
04406    if ( !doc() || !doc()->map() )
04407      return;
04408 
04409    QCString passwd;
04410    if ( mode )
04411    {
04412      int result = KPasswordDialog::getNewPassword( passwd, i18n( "Protect Document" ) );
04413      if ( result != KPasswordDialog::Accepted )
04414      {
04415        d->actions->protectDoc->setChecked( false );
04416        return;
04417      }
04418 
04419      QCString hash( "" );
04420      QString password( passwd );
04421      if ( password.length() > 0 )
04422        SHA1::getHash( password, hash );
04423      doc()->map()->setProtected( hash );
04424    }
04425    else
04426    {
04427      int result = KPasswordDialog::getPassword( passwd, i18n( "Unprotect Document" ) );
04428      if ( result != KPasswordDialog::Accepted )
04429      {
04430        d->actions->protectDoc->setChecked( true );
04431        return;
04432      }
04433 
04434      QCString hash( "" );
04435      QString password( passwd );
04436      if ( password.length() > 0 )
04437        SHA1::getHash( password, hash );
04438      if ( !doc()->map()->checkPassword( hash ) )
04439      {
04440        KMessageBox::error( 0, i18n( "Password is incorrect." ) );
04441        d->actions->protectDoc->setChecked( true );
04442        return;
04443      }
04444 
04445      doc()->map()->setProtected( QCString() );
04446    }
04447 
04448    doc()->setModified( true );
04449    d->adjustWorkbookActions( !mode );
04450 }
04451 
04452 void KSpreadView::toggleProtectSheet( bool mode )
04453 {
04454    if ( !d->activeSheet )
04455        return;
04456 
04457    QCString passwd;
04458    if ( mode )
04459    {
04460      int result = KPasswordDialog::getNewPassword( passwd, i18n( "Protect Sheet" ) );
04461      if ( result != KPasswordDialog::Accepted )
04462      {
04463        d->actions->protectSheet->setChecked( false );
04464        return;
04465      }
04466 
04467      QCString hash( "" );
04468      QString password( passwd );
04469      if ( password.length() > 0 )
04470        SHA1::getHash( password, hash );
04471 
04472      d->activeSheet->setProtected( hash );
04473    }
04474    else
04475    {
04476      int result = KPasswordDialog::getPassword( passwd, i18n( "Unprotect Sheet" ) );
04477      if ( result != KPasswordDialog::Accepted )
04478      {
04479        d->actions->protectSheet->setChecked( true );
04480        return;
04481      }
04482 
04483      QCString hash( "" );
04484      QString password( passwd );
04485      if ( password.length() > 0 )
04486        SHA1::getHash( password, hash );
04487 
04488      if ( !d->activeSheet->checkPassword( hash ) )
04489      {
04490        KMessageBox::error( 0, i18n( "Password is incorrect." ) );
04491        d->actions->protectSheet->setChecked( true );
04492        return;
04493      }
04494 
04495      d->activeSheet->setProtected( QCString() );
04496    }
04497    doc()->setModified( true );
04498    d->adjustActions( !mode );
04499    doc()->emitBeginOperation();
04500    // d->activeSheet->setRegionPaintDirty( QRect(QPoint( 0, 0 ), QPoint( KS_colMax, KS_rowMax ) ) );
04501    refreshView();
04502    updateEditWidget();
04503    doc()->emitEndOperation( d->activeSheet->visibleRect( d->canvas ) );
04504 }
04505 
04506 void KSpreadView::togglePageBorders( bool mode )
04507 {
04508   if ( !d->activeSheet )
04509     return;
04510 
04511   doc()->emitBeginOperation( false );
04512   d->activeSheet->setShowPageBorders( mode );
04513   doc()->emitEndOperation( d->activeSheet->visibleRect( d->canvas ) );
04514 }
04515 
04516 void KSpreadView::viewZoom( const QString & s )
04517 {
04518   int oldZoom = doc()->zoom();
04519 
04520   bool ok = false;
04521   QRegExp regexp("(\\d+)"); // "Captured" non-empty sequence of digits
04522   regexp.search(s);
04523   int newZoom=regexp.cap(1).toInt(&ok);
04524   if ( !ok || newZoom < 10 ) //zoom should be valid and >10
04525     newZoom = oldZoom;
04526   if ( newZoom != oldZoom )
04527   {
04528     d->actions->viewZoom->setZoom( newZoom );
04529 
04530     doc()->emitBeginOperation( false );
04531 
04532     d->canvas->closeEditor();
04533     setZoom( newZoom, true );
04534 
04535     QRect r( d->activeSheet->visibleRect( d->canvas ) );
04536     r.setWidth( r.width() + 2 );
04537     doc()->emitEndOperation( r );
04538   }
04539 }
04540 
04541 void KSpreadView::setZoom( int zoom, bool /*updateViews*/ )
04542 {
04543   kdDebug() << "---------SetZoom: " << zoom << endl;
04544 
04545   // Set the zoom in KoView (for embedded views)
04546   doc()->emitBeginOperation( false );
04547 
04548   doc()->setZoomAndResolution( zoom, KoGlobal::dpiX(), KoGlobal::dpiY());
04549   KoView::setZoom( doc()->zoomedResolutionY() /* KoView only supports one zoom */ );
04550 
04551   Q_ASSERT(d->activeSheet);
04552 
04553   if (d->activeSheet)  //this is 0 when viewing a document in konqueror!? (see Q_ASSERT above)
04554     d->activeSheet->setRegionPaintDirty(QRect(QPoint(0,0), QPoint(KS_colMax, KS_rowMax)));
04555 
04556   doc()->refreshInterface();
04557   doc()->emitEndOperation();
04558 }
04559 
04560 void KSpreadView::showStatusBar( bool b )
04561 {
04562   doc()->setShowStatusBar( b );
04563   refreshView();
04564 }
04565 
04566 void KSpreadView::showTabBar( bool b )
04567 {
04568   doc()->setShowTabBar( b );
04569   refreshView();
04570 }
04571 
04572 void KSpreadView::showFormulaBar( bool b )
04573 {
04574   doc()->setShowFormulaBar( b );
04575   refreshView();
04576 }
04577 
04578 void KSpreadView::showCommentIndicator( bool b )
04579 {
04580   doc()->setShowCommentIndicator( b );
04581 
04582   d->adjustActions( !d->activeSheet->isProtected() );
04583 
04584   doc()->emitBeginOperation( false );
04585   doc()->emitEndOperation( d->activeSheet->visibleRect( d->canvas ) );
04586   refreshView();
04587 }
04588 
04589 void KSpreadView::preference()
04590 {
04591   if ( !d->activeSheet )
04592     return;
04593 
04594   KSpreadpreference dlg( this, "Preference" );
04595   if ( dlg.exec() )
04596   {
04597     doc()->emitBeginOperation( false );
04598     d->activeSheet->refreshPreference();
04599     doc()->emitEndOperation( d->activeSheet->visibleRect( d->canvas ) );
04600   }
04601 }
04602 
04603 void KSpreadView::addModifyComment()
04604 {
04605   if ( !d->activeSheet )
04606     return;
04607 
04608   KSpreadComment dlg( this, "comment",
04609                       QPoint( d->canvas->markerColumn(),
04610                               d->canvas->markerRow() ) );
04611   if ( dlg.exec() )
04612     updateEditWidget();
04613 }
04614 
04615 void KSpreadView::setSelectionComment( QString comment )
04616 {
04617   if ( d->activeSheet != NULL )
04618   {
04619     doc()->emitBeginOperation( false );
04620 
04621     d->activeSheet->setSelectionComment( selectionInfo(), comment.stripWhiteSpace() );
04622     updateEditWidget();
04623 
04624     endOperation( d->selectionInfo->selection() );
04625   }
04626 }
04627 
04628 void KSpreadView::editCell()
04629 {
04630   if ( d->canvas->editor() )
04631     return;
04632 
04633   d->canvas->createEditor();
04634 }
04635 
04636 bool KSpreadView::showSheet(const QString& sheetName) {
04637   KSpreadSheet *t=doc()->map()->findSheet(sheetName);
04638   if ( !t )
04639   {
04640     kdDebug(36001) << "Unknown sheet " <<sheetName<<  endl;
04641     return false;
04642   }
04643   d->canvas->closeEditor();
04644   setActiveSheet( t );
04645 
04646   return true;
04647 }
04648 
04649 void KSpreadView::nextSheet(){
04650 
04651   KSpreadSheet * t = doc()->map()->nextSheet( activeSheet() );
04652   if ( !t )
04653   {
04654     kdDebug(36001) << "Unknown sheet " <<  endl;
04655     return;
04656   }
04657   d->canvas->closeEditor();
04658   setActiveSheet( t );
04659 }
04660 
04661 void KSpreadView::previousSheet()
04662 {
04663   KSpreadSheet * t = doc()->map()->previousSheet( activeSheet() );
04664   if ( !t )
04665   {
04666     kdDebug(36001) << "Unknown sheet "  << endl;
04667     return;
04668   }
04669   d->canvas->closeEditor();
04670   setActiveSheet( t );
04671 }
04672 
04673 void KSpreadView::firstSheet()
04674 {
04675   KSpreadSheet *t = doc()->map()->firstSheet();
04676   if ( !t )
04677   {
04678     kdDebug(36001) << "Unknown sheet " <<  endl;
04679     return;
04680   }
04681   d->canvas->closeEditor();
04682   setActiveSheet( t );
04683 }
04684 
04685 void KSpreadView::lastSheet()
04686 {
04687   KSpreadSheet *t = doc()->map()->lastSheet( );
04688   if ( !t )
04689   {
04690     kdDebug(36001) << "Unknown sheet " <<  endl;
04691     return;
04692   }
04693   d->canvas->closeEditor();
04694   setActiveSheet( t );
04695 }
04696 
04697 void KSpreadView::keyPressEvent ( QKeyEvent* _ev )
04698 {
04699   // Dont eat accelerators
04700   if ( _ev->state() & ( Qt::AltButton | Qt::ControlButton ) )
04701   {
04702     if ( _ev->state() & ( Qt::ControlButton ) )
04703     {
04704       switch( _ev->key() )
04705       {
04706 #ifndef NDEBUG
04707        case Key_V: // Ctrl+Shift+V to show debug (similar to KWord)
04708         if ( _ev->state() & Qt::ShiftButton )
04709           d->activeSheet->printDebug();
04710 #endif
04711        default:
04712         QWidget::keyPressEvent( _ev );
04713         return;
04714       }
04715     }
04716     QWidget::keyPressEvent( _ev );
04717   }
04718   else
04719     QApplication::sendEvent( d->canvas, _ev );
04720 }
04721 
04722 KoDocument * KSpreadView::hitTest( const QPoint &pos )
04723 {
04724     // Code copied from KoView::hitTest
04725     KoViewChild *viewChild;
04726 
04727     QWMatrix m = matrix();
04728     m.translate( d->canvas->xOffset() / doc()->zoomedResolutionX(),
04729                  d->canvas->yOffset() / doc()->zoomedResolutionY() );
04730 
04731     KoDocumentChild *docChild = selectedChild();
04732     if ( docChild )
04733     {
04734         if ( ( viewChild = child( docChild->document() ) ) )
04735         {
04736             if ( viewChild->frameRegion( m ).contains( pos ) )
04737                 return 0;
04738         }
04739         else
04740             if ( docChild->frameRegion( m ).contains( pos ) )
04741                 return 0;
04742     }
04743 
04744     docChild = activeChild();
04745     if ( docChild )
04746     {
04747         if ( ( viewChild = child( docChild->document() ) ) )
04748         {
04749             if ( viewChild->frameRegion( m ).contains( pos ) )
04750                 return 0;
04751         }
04752         else
04753             if ( docChild->frameRegion( m ).contains( pos ) )
04754                 return 0;
04755     }
04756 
04757     QPtrListIterator<KoDocumentChild> it( doc()->children() );
04758     for (; it.current(); ++it )
04759     {
04760         // Is the child document on the visible sheet ?
04761         if ( ((KSpreadChild*)it.current())->sheet() == d->activeSheet )
04762         {
04763             KoDocument *doc = it.current()->hitTest( pos, m );
04764             if ( doc )
04765                 return doc;
04766         }
04767     }
04768 
04769     return doc();
04770 }
04771 
04772 int KSpreadView::leftBorder() const
04773 {
04774   return int( d->canvas->doc()->zoomItX( YBORDER_WIDTH ) );
04775 }
04776 
04777 int KSpreadView::rightBorder() const
04778 {
04779   return d->vertScrollBar->width();
04780 }
04781 
04782 int KSpreadView::topBorder() const
04783 {
04784   return d->toolWidget->height() + int( d->canvas->doc()->zoomItX( KSpreadFormat::globalRowHeight() + 2 ) );
04785 }
04786 
04787 int KSpreadView::bottomBorder() const
04788 {
04789   return d->horzScrollBar->height();
04790 }
04791 
04792 void KSpreadView::refreshView()
04793 {
04794   kdDebug() << "refreshing view" << endl;
04795 
04796   KSpreadSheet * sheet = activeSheet();
04797   if ( !sheet )
04798     return;
04799 
04800   d->adjustActions( !sheet->isProtected() );
04801   d->actions->viewZoom->setZoom( doc()->zoom() );
04802 
04803   bool active = sheet->getShowFormula();
04804   if ( sheet && !sheet->isProtected() )
04805   {
04806     d->actions->alignLeft->setEnabled( !active );
04807     d->actions->alignCenter->setEnabled( !active );
04808     d->actions->alignRight->setEnabled( !active );
04809   }
04810 
04811   d->tabBar->setReadOnly( !doc()->isReadWrite() );
04812 
04813   d->toolWidget->setShown( doc()->showFormulaBar() );
04814   editWidget()->showEditWidget( doc()->showFormulaBar() );
04815   d->hBorderWidget->setShown( doc()->showColumnHeader() );
04816   d->vBorderWidget->setShown( doc()->showRowHeader() );
04817   d->vertScrollBar->setShown( doc()->showVerticalScrollBar() );
04818   d->horzScrollBar->setShown( doc()->showHorizontalScrollBar() );
04819   d->tabBar->setShown( doc()->showTabBar() );
04820   if ( statusBar() ) statusBar()->setShown( doc()->showStatusBar() );
04821 
04822   d->canvas->updatePosWidget();
04823 
04824   d->hBorderWidget->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum );
04825   d->hBorderWidget->setMinimumHeight( doc()->zoomItY( KSpreadFormat::globalRowHeight() + 2 ) );
04826   d->vBorderWidget->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Expanding );
04827   d->vBorderWidget->setMinimumWidth( doc()->zoomItX( YBORDER_WIDTH ) );
04828 
04829   KSpreadSheet::LayoutDirection sheetDir = sheet->layoutDirection();
04830   bool interfaceIsRTL = QApplication::reverseLayout();
04831 
04832   kdDebug()<<" sheetDir == KSpreadSheet::LeftToRight :"<<( sheetDir == KSpreadSheet::LeftToRight )<<endl;
04833   if ((sheetDir == KSpreadSheet::LeftToRight && !interfaceIsRTL) ||
04834       (sheetDir == KSpreadSheet::RightToLeft && interfaceIsRTL))
04835   {
04836     d->formulaBarLayout->setDirection( QBoxLayout::LeftToRight );
04837     d->viewLayout->setOrigin( QGridLayout::TopLeft );
04838     d->tabScrollBarLayout->setDirection( QBoxLayout::LeftToRight );
04839     d->tabBar->setReverseLayout( interfaceIsRTL );
04840   }
04841   else
04842   {
04843     d->formulaBarLayout->setDirection( QBoxLayout::RightToLeft );
04844     d->viewLayout->setOrigin( QGridLayout::TopRight );
04845     d->tabScrollBarLayout->setDirection( QBoxLayout::RightToLeft );
04846     d->tabBar->setReverseLayout( !interfaceIsRTL );
04847   }
04848 
04849 }
04850 
04851 void KSpreadView::resizeEvent( QResizeEvent * )
04852 {
04853   refreshView();
04854 }
04855 
04856 void KSpreadView::popupChildMenu( KoChild* child, const QPoint& global_pos )
04857 {
04858     if ( !child )
04859     return;
04860 
04861     delete d->popupChild;
04862 
04863     d->popupChildObject = static_cast<KSpreadChild*>(child);
04864 
04865     d->popupChild = new QPopupMenu( this );
04866 
04867     d->popupChild->insertItem( i18n("Delete Embedded Document"), this, SLOT( slotPopupDeleteChild() ) );
04868 
04869     d->popupChild->popup( global_pos );
04870 }
04871 
04872 void KSpreadView::slotPopupDeleteChild()
04873 {
04874     if ( !d->popupChildObject || !d->popupChildObject->sheet() )
04875     return;
04876     int ret = KMessageBox::warningContinueCancel(this,i18n("You are about to remove this embedded document.\nDo you want to continue?"),i18n("Delete Embedded Document"),KGuiItem(i18n("&Delete"),"editdelete"));
04877     if ( ret == KMessageBox::Continue )
04878     {
04879       doc()->emitBeginOperation(false);
04880       d->popupChildObject->sheet()->deleteChild( d->popupChildObject );
04881       d->popupChildObject = 0;
04882       doc()->emitEndOperation( d->activeSheet->visibleRect( d->canvas ) );
04883     }
04884 }
04885 
04886 void KSpreadView::popupColumnMenu( const QPoint & _point )
04887 {
04888   assert( d->activeSheet );
04889 
04890   if ( !koDocument()->isReadWrite() )
04891     return;
04892 
04893     delete d->popupColumn ;
04894 
04895     d->popupColumn = new QPopupMenu( this );
04896 
04897     bool isProtected = d->activeSheet->isProtected();
04898 
04899     if ( !isProtected )
04900     {
04901       d->actions->cellLayout->plug( d->popupColumn );
04902       d->popupColumn->insertSeparator();
04903       d->actions->cut->plug( d->popupColumn );
04904     }
04905     d->actions->copy->plug( d->popupColumn );
04906     if ( !isProtected )
04907     {
04908       d->actions->paste->plug( d->popupColumn );
04909       d->actions->specialPaste->plug( d->popupColumn );
04910       d->actions->insertCellCopy->plug( d->popupColumn );
04911       d->popupColumn->insertSeparator();
04912       d->actions->defaultFormat->plug( d->popupColumn );
04913       // If there is no selection
04914       if ((util_isRowSelected(selection()) == FALSE) && (util_isColumnSelected(selection()) == FALSE) )
04915       {
04916         d->actions->areaName->plug( d->popupColumn );
04917       }
04918 
04919       d->actions->resizeColumn->plug( d->popupColumn );
04920       d->popupColumn->insertItem( i18n("Adjust Column"), this, SLOT(slotPopupAdjustColumn() ) );
04921       d->popupColumn->insertSeparator();
04922       d->actions->insertColumn->plug( d->popupColumn );
04923       d->actions->deleteColumn->plug( d->popupColumn );
04924       d->actions->hideColumn->plug( d->popupColumn );
04925 
04926       d->actions->showSelColumns->setEnabled(false);
04927 
04928       int i;
04929       ColumnFormat * col;
04930       QRect rect = d->selectionInfo->selection();
04931       //kdDebug(36001) << "Column: L: " << rect.left() << endl;
04932       for ( i = rect.left(); i <= rect.right(); ++i )
04933       {
04934         if (i == 2) // "B"
04935         {
04936           col = activeSheet()->columnFormat( 1 );
04937           if ( col->isHide() )
04938           {
04939             d->actions->showSelColumns->setEnabled(true);
04940             d->actions->showSelColumns->plug( d->popupColumn );
04941             break;
04942           }
04943         }
04944 
04945         col = activeSheet()->columnFormat( i );
04946 
04947         if ( col->isHide() )
04948         {
04949           d->actions->showSelColumns->setEnabled( true );
04950           d->actions->showSelColumns->plug( d->popupColumn );
04951           break;
04952         }
04953       }
04954     }
04955 
04956     QObject::connect( d->popupColumn, SIGNAL(activated( int ) ), this, SLOT( slotActivateTool( int ) ) );
04957 
04958     d->popupColumn->popup( _point );
04959 }
04960 
04961 void KSpreadView::slotPopupAdjustColumn()
04962 {
04963     if ( !d->activeSheet )
04964        return;
04965 
04966     doc()->emitBeginOperation( false );
04967     canvasWidget()->adjustArea();
04968     doc()->emitEndOperation( d->activeSheet->visibleRect( d->canvas ) );
04969 }
04970 
04971 void KSpreadView::popupRowMenu( const QPoint & _point )
04972 {
04973     assert( d->activeSheet );
04974 
04975     if ( !koDocument()->isReadWrite() )
04976       return;
04977 
04978     delete d->popupRow ;
04979 
04980     d->popupRow= new QPopupMenu();
04981 
04982     bool isProtected = d->activeSheet->isProtected();
04983 
04984     if ( !isProtected )
04985     {
04986         d->actions->cellLayout->plug( d->popupRow );
04987         d->popupRow->insertSeparator();
04988         d->actions->cut->plug( d->popupRow );
04989     }
04990     d->actions->copy->plug( d->popupRow );
04991     if ( !isProtected )
04992     {
04993       d->actions->paste->plug( d->popupRow );
04994       d->actions->specialPaste->plug( d->popupRow );
04995       d->actions->insertCellCopy->plug( d->popupRow );
04996       d->popupRow->insertSeparator();
04997       d->actions->defaultFormat->plug( d->popupRow );
04998       // If there is no selection
04999       if ( (util_isRowSelected(selection()) == FALSE) && (util_isColumnSelected(selection()) == FALSE) )
05000       {
05001     d->actions->areaName->plug( d->popupRow );
05002       }
05003 
05004       d->actions->resizeRow->plug( d->popupRow );
05005       d->popupRow->insertItem( i18n("Adjust Row"), this, SLOT( slotPopupAdjustRow() ) );
05006       d->popupRow->insertSeparator();
05007       d->actions->insertRow->plug( d->popupRow );
05008       d->actions->deleteRow->plug( d->popupRow );
05009       d->actions->hideRow->plug( d->popupRow );
05010 
05011       d->actions->showSelColumns->setEnabled(false);
05012 
05013       int i;
05014       RowFormat * row;
05015       QRect rect = d->selectionInfo->selection();
05016       for ( i = rect.top(); i <= rect.bottom(); ++i )
05017       {
05018         //kdDebug(36001) << "popupRow: " << rect.top() << endl;
05019         if (i == 2)
05020         {
05021           row = activeSheet()->rowFormat( 1 );
05022           if ( row->isHide() )
05023           {
05024             d->actions->showSelRows->setEnabled(true);
05025             d->actions->showSelRows->plug( d->popupRow );
05026             break;
05027           }
05028         }
05029 
05030         row = activeSheet()->rowFormat( i );
05031         if ( row->isHide() )
05032         {
05033           d->actions->showSelRows->setEnabled(true);
05034           d->actions->showSelRows->plug( d->popupRow );
05035           break;
05036         }
05037       }
05038     }
05039 
05040     QObject::connect( d->popupRow, SIGNAL( activated( int ) ), this, SLOT( slotActivateTool( int ) ) );
05041     d->popupRow->popup( _point );
05042 }
05043 
05044 void KSpreadView::slotPopupAdjustRow()
05045 {
05046     if ( !d->activeSheet )
05047        return;
05048 
05049     doc()->emitBeginOperation(false);
05050     canvasWidget()->adjustArea();
05051     doc()->emitEndOperation( d->activeSheet->visibleRect( d->canvas ) );
05052 }
05053 
05054 
05055 void KSpreadView::slotListChoosePopupMenu( )
05056 {
05057   if ( !koDocument()->isReadWrite() )
05058     return;
05059 
05060   assert( d->activeSheet );
05061   delete d->popupListChoose;
05062 
05063   d->popupListChoose = new QPopupMenu();
05064   int id = 0;
05065   QRect selection( d->selectionInfo->selection() );
05066   KSpreadCell * cell = d->activeSheet->cellAt( d->canvas->markerColumn(), d->canvas->markerRow() );
05067   QString tmp = cell->text();
05068   QStringList itemList;
05069 
05070   for ( int col = selection.left(); col <= selection.right(); ++col )
05071   {
05072     KSpreadCell * c = d->activeSheet->getFirstCellColumn( col );
05073     while ( c )
05074     {
05075       if ( !c->isObscuringForced()
05076            && !( col == d->canvas->markerColumn()
05077                  && c->row() == d->canvas->markerRow()) )
05078       {
05079         if ( c->value().isString() && c->text() != tmp && !c->text().isEmpty() )
05080         {
05081           if ( itemList.findIndex( c->text() ) == -1 )
05082             itemList.append(c->text());
05083         }
05084       }
05085 
05086       c = d->activeSheet->getNextCellDown( col, c->row() );
05087     }
05088   }
05089 
05090   /* TODO: remove this later:
05091     for( ;c; c = c->nextCell() )
05092    {
05093      int col = c->column();
05094      if ( selection.left() <= col && selection.right() >= col
05095       &&!c->isObscuringForced()&& !(col==d->canvas->markerColumn()&& c->row()==d->canvas->markerRow()))
05096        {
05097      if (c->isString() && c->text()!=tmp && !c->text().isEmpty())
05098        {
05099          if (itemList.findIndex(c->text())==-1)
05100                  itemList.append(c->text());
05101        }
05102 
05103        }
05104     }
05105  */
05106 
05107   for ( QStringList::Iterator it = itemList.begin(); it != itemList.end();++it )
05108     d->popupListChoose->insertItem( (*it), id++ );
05109 
05110   if ( id == 0 )
05111     return;
05112   RowFormat * rl = d->activeSheet->rowFormat( d->canvas->markerRow());
05113   double tx = d->activeSheet->dblColumnPos( d->canvas->markerColumn(), d->canvas );
05114   double ty = d->activeSheet->dblRowPos(d->canvas->markerRow(), d->canvas );
05115   double h = rl->dblHeight( d->canvas );
05116   if ( cell->extraYCells() )
05117     h = cell->extraHeight();
05118   ty += h;
05119 
05120   if ( d->activeSheet->layoutDirection()==KSpreadSheet::RightToLeft )
05121   {
05122     tx = canvasWidget()->width() - tx;
05123   }
05124 
05125   QPoint p( (int)tx, (int)ty );
05126   QPoint p2 = d->canvas->mapToGlobal( p );
05127 
05128   if ( d->activeSheet->layoutDirection()==KSpreadSheet::RightToLeft )
05129   {
05130     p2.setX( p2.x() - d->popupListChoose->sizeHint().width() + 1 );
05131   }
05132 
05133   d->popupListChoose->popup( p2 );
05134   QObject::connect( d->popupListChoose, SIGNAL( activated( int ) ),
05135                     this, SLOT( slotItemSelected( int ) ) );
05136 }
05137 
05138 
05139 void KSpreadView::slotItemSelected( int id )
05140 {
05141   QString tmp = d->popupListChoose->text( id );
05142   int x = d->canvas->markerColumn();
05143   int y = d->canvas->markerRow();
05144   KSpreadCell * cell = d->activeSheet->nonDefaultCell( x, y );
05145 
05146   if ( tmp == cell->text() )
05147     return;
05148 
05149   doc()->emitBeginOperation( false );
05150 
05151   if ( !doc()->undoLocked() )
05152   {
05153     KSpreadUndoSetText* undo = new KSpreadUndoSetText( doc(), d->activeSheet, cell->text(),
05154                                                        x, y, cell->formatType() );
05155     doc()->addCommand( undo );
05156   }
05157 
05158   cell->setCellText( tmp );
05159   editWidget()->setText( tmp );
05160 
05161   doc()->emitEndOperation( QRect( x, y, 1, 1 ) );
05162 }
05163 
05164 void KSpreadView::openPopupMenu( const QPoint & _point )
05165 {
05166     assert( d->activeSheet );
05167     delete d->popupMenu;
05168 
05169     if ( !koDocument()->isReadWrite() )
05170         return;
05171 
05172     d->popupMenu = new QPopupMenu();
05173     KSpreadCell * cell = d->activeSheet->cellAt( d->canvas->markerColumn(), d->canvas->markerRow() );
05174 
05175     bool isProtected = d->activeSheet->isProtected();
05176     if ( !cell->isDefault() && cell->notProtected( d->canvas->markerColumn(), d->canvas->markerRow() )
05177          && ( selection().width() == 1 ) && ( selection().height() == 1 ) )
05178       isProtected = false;
05179 
05180     if ( !isProtected )
05181     {
05182       d->actions->cellLayout->plug( d->popupMenu );
05183       d->popupMenu->insertSeparator();
05184       d->actions->cut->plug( d->popupMenu );
05185     }
05186     d->actions->copy->plug( d->popupMenu );
05187     if ( !isProtected )
05188       d->actions->paste->plug( d->popupMenu );
05189 
05190     if ( !isProtected )
05191     {
05192       d->actions->specialPaste->plug( d->popupMenu );
05193       d->actions->insertCellCopy->plug( d->popupMenu );
05194       d->popupMenu->insertSeparator();
05195       d->actions->deleteCell->plug( d->popupMenu );
05196       d->actions->adjust->plug( d->popupMenu );
05197       d->actions->defaultFormat->plug( d->popupMenu );
05198 
05199       // If there is no selection
05200       if ( (util_isRowSelected(selection()) == FALSE) && (util_isColumnSelected(selection()) == FALSE) )
05201       {
05202         d->actions->areaName->plug( d->popupMenu );
05203         d->popupMenu->insertSeparator();
05204         d->actions->insertCell->plug( d->popupMenu );
05205         d->actions->removeCell->plug( d->popupMenu );
05206       }
05207 
05208       d->popupMenu->insertSeparator();
05209       d->actions->addModifyComment->plug( d->popupMenu );
05210       if ( !cell->comment(d->canvas->markerColumn(), d->canvas->markerRow()).isEmpty() )
05211       {
05212         d->actions->removeComment->plug( d->popupMenu );
05213       }
05214 
05215       if (activeSheet()->testListChoose(selectionInfo()))
05216       {
05217     d->popupMenu->insertSeparator();
05218     d->popupMenu->insertItem( i18n("Selection List..."), this, SLOT( slotListChoosePopupMenu() ) );
05219       }
05220     }
05221 
05222     // Remove informations about the last tools we offered
05223     d->toolList.clear();
05224     d->toolList.setAutoDelete( true );
05225 
05226     if ( !isProtected && !activeSheet()->getWordSpelling( selectionInfo() ).isEmpty() )
05227     {
05228       d->popupMenuFirstToolId = 10;
05229       int i = 0;
05230       QValueList<KDataToolInfo> tools = KDataToolInfo::query( "QString", "text/plain", doc()->instance() );
05231       if ( tools.count() > 0 )
05232       {
05233         d->popupMenu->insertSeparator();
05234         QValueList<KDataToolInfo>::Iterator entry = tools.begin();
05235         for( ; entry != tools.end(); ++entry )
05236         {
05237           QStringList lst = (*entry).userCommands();
05238           QStringList::ConstIterator it = lst.begin();
05239 
05240           // ### Torben: Insert pixmaps here, too
05241           for (; it != lst.end(); ++it )
05242             d->popupMenu->insertItem( *it, d->popupMenuFirstToolId + i++ );
05243 
05244           lst = (*entry).commands();
05245           it = lst.begin();
05246           for (; it != lst.end(); ++it )
05247           {
05248             ViewPrivate::ToolEntry *t = new ViewPrivate::ToolEntry;
05249             t->command = *it;
05250             t->info = *entry;
05251             d->toolList.append( t );
05252           }
05253         }
05254 
05255         QObject::connect( d->popupMenu, SIGNAL( activated( int ) ), this, SLOT( slotActivateTool( int ) ) );
05256       }
05257     }
05258 
05259     d->popupMenu->popup( _point );
05260 }
05261 
05262 void KSpreadView::slotActivateTool( int _id )
05263 {
05264   Q_ASSERT( d->activeSheet );
05265 
05266   // Is it the id of a tool in the latest popupmenu ?
05267   if ( _id < d->popupMenuFirstToolId )
05268     return;
05269 
05270   ViewPrivate::ToolEntry* entry = d->toolList.at( _id - d->popupMenuFirstToolId );
05271 
05272   KDataTool* tool = entry->info.createTool();
05273   if ( !tool )
05274   {
05275       kdDebug(36001) << "Could not create Tool" << endl;
05276       return;
05277   }
05278 
05279   QString text = activeSheet()->getWordSpelling( selectionInfo() );
05280 
05281   if ( tool->run( entry->command, &text, "QString", "text/plain") )
05282   {
05283       doc()->emitBeginOperation(false);
05284 
05285       activeSheet()->setWordSpelling( selectionInfo(), text);
05286 
05287       KSpreadCell *cell = d->activeSheet->cellAt( d->canvas->markerColumn(), d->canvas->markerRow() );
05288       editWidget()->setText( cell->text() );
05289 
05290       doc()->emitEndOperation( d->activeSheet->visibleRect( d->canvas ) );
05291   }
05292 }
05293 
05294 void KSpreadView::deleteSelection()
05295 {
05296     Q_ASSERT( d->activeSheet );
05297 
05298     doc()->emitBeginOperation( false );
05299     d->activeSheet->deleteSelection( selectionInfo() );
05300     resultOfCalc();
05301     updateEditWidget();
05302     endOperation( selectionInfo()->selection() );
05303 }
05304 
05305 void KSpreadView::adjust()
05306 {
05307     if ( (util_isRowSelected(selection())) || (util_isColumnSelected(selection())) )
05308     {
05309       KMessageBox::error( this, i18n("Area is too large."));
05310     }
05311     else
05312     {
05313       doc()->emitBeginOperation( false );
05314       canvasWidget()->adjustArea();
05315       endOperation( selection() );
05316     }
05317 }
05318 
05319 void KSpreadView::clearTextSelection()
05320 {
05321     Q_ASSERT( d->activeSheet );
05322     doc()->emitBeginOperation( false );
05323     d->activeSheet->clearTextSelection( selectionInfo() );
05324 
05325     updateEditWidget();
05326     doc()->emitEndOperation( selectionInfo()->selection() );
05327 }
05328 
05329 void KSpreadView::clearCommentSelection()
05330 {
05331     Q_ASSERT( d->activeSheet );
05332     doc()->emitBeginOperation( false );
05333     d->activeSheet->setSelectionRemoveComment( selectionInfo() );
05334 
05335     updateEditWidget();
05336     doc()->emitEndOperation( selectionInfo()->selection() );
05337 }
05338 
05339 void KSpreadView::clearValiditySelection()
05340 {
05341     Q_ASSERT( d->activeSheet );
05342     doc()->emitBeginOperation( false );
05343     d->activeSheet->clearValiditySelection( selectionInfo() );
05344 
05345     updateEditWidget();
05346     doc()->emitEndOperation( selectionInfo()->selection() );
05347 }
05348 
05349 void KSpreadView::clearConditionalSelection()
05350 {
05351     Q_ASSERT( d->activeSheet );
05352     doc()->emitBeginOperation( false );
05353     d->activeSheet->clearConditionalSelection( selectionInfo() );
05354 
05355     updateEditWidget();
05356     doc()->emitEndOperation( selectionInfo()->selection() );
05357 }
05358 
05359 void KSpreadView::fillRight()
05360 {
05361   Q_ASSERT( d->activeSheet );
05362   doc()->emitBeginOperation( false );
05363   d->activeSheet->fillSelection( selectionInfo(), KSpreadSheet::Right );
05364   doc()->emitEndOperation( selectionInfo()->selection() );
05365 }
05366 
05367 void KSpreadView::fillLeft()
05368 {
05369   Q_ASSERT( d->activeSheet );
05370   doc()->emitBeginOperation( false );
05371   d->activeSheet->fillSelection( selectionInfo(), KSpreadSheet::Left );
05372   doc()->emitEndOperation( selectionInfo()->selection() );
05373 }
05374 
05375 void KSpreadView::fillUp()
05376 {
05377   Q_ASSERT( d->activeSheet );
05378   doc()->emitBeginOperation( false );
05379   d->activeSheet->fillSelection( selectionInfo(), KSpreadSheet::Up );
05380   doc()->emitEndOperation( selectionInfo()->selection() );
05381 }
05382 
05383 void KSpreadView::fillDown()
05384 {
05385   Q_ASSERT( d->activeSheet );
05386   doc()->emitBeginOperation( false );
05387   d->activeSheet->fillSelection( selectionInfo(), KSpreadSheet::Down );
05388   doc()->emitEndOperation( selectionInfo()->selection() );
05389 }
05390 
05391 void KSpreadView::defaultSelection()
05392 {
05393   Q_ASSERT( d->activeSheet );
05394   doc()->emitBeginOperation( false );
05395   d->activeSheet->defaultSelection( selectionInfo() );
05396 
05397   updateEditWidget();
05398   doc()->emitEndOperation( selectionInfo()->selection() );
05399 }
05400 
05401 void KSpreadView::slotInsert()
05402 {
05403   QRect r( selection() );
05404   KSpreadinsert dlg( this, "Insert", r, KSpreadinsert::Insert );
05405   dlg.exec();
05406 }
05407 
05408 void KSpreadView::slotRemove()
05409 {
05410   QRect r( d->selectionInfo->selection() );
05411   KSpreadinsert dlg( this, "Remove", r, KSpreadinsert::Remove );
05412   dlg.exec();
05413 }
05414 
05415 void KSpreadView::slotInsertCellCopy()
05416 {
05417   if ( !d->activeSheet )
05418     return;
05419 
05420   if ( !d->activeSheet->testAreaPasteInsert() )
05421   {
05422     doc()->emitBeginOperation( false );
05423     d->activeSheet->paste( selection(), true, Normal, OverWrite, true );
05424     doc()->emitEndOperation( d->activeSheet->visibleRect( d->canvas ) );
05425   }
05426   else
05427   {
05428     KSpreadpasteinsert dlg( this, "Remove", selection() );
05429     dlg.exec();
05430   }
05431 
05432   if ( d->activeSheet->getAutoCalc() )
05433   {
05434     doc()->emitBeginOperation( false );
05435     d->activeSheet->recalc();
05436     doc()->emitEndOperation( d->activeSheet->visibleRect( d->canvas ) );
05437   }
05438   updateEditWidget();
05439 }
05440 
05441 void KSpreadView::setAreaName()
05442 {
05443   KSpreadarea dlg( this, "Area Name",QPoint(d->canvas->markerColumn(), d->canvas->markerRow()) );
05444   dlg.exec();
05445 }
05446 
05447 void KSpreadView::showAreaName()
05448 {
05449   KSpreadreference dlg( this, "Show Area" );
05450   dlg.exec();
05451 }
05452 
05453 void KSpreadView::resizeRow()
05454 {
05455   if ( util_isColumnSelected(selection()) )
05456     KMessageBox::error( this, i18n("Area is too large."));
05457   else
05458   {
05459     KSpreadResizeRow dlg( this );
05460     dlg.exec();
05461   }
05462 }
05463 
05464 void KSpreadView::resizeColumn()
05465 {
05466   if ( util_isRowSelected( selection() ) )
05467     KMessageBox::error( this, i18n( "Area is too large." ) );
05468   else
05469   {
05470     KSpreadResizeColumn dlg( this );
05471     dlg.exec();
05472   }
05473 }
05474 
05475 void KSpreadView::equalizeRow()
05476 {
05477   if ( util_isColumnSelected( selection() ) )
05478     KMessageBox::error( this, i18n( "Area is too large." ) );
05479   else
05480   {
05481     doc()->emitBeginOperation( false );
05482     canvasWidget()->equalizeRow();
05483     doc()->emitEndOperation( d->activeSheet->visibleRect( d->canvas ) );
05484   }
05485 }
05486 
05487 void KSpreadView::equalizeColumn()
05488 {
05489   if ( util_isRowSelected( selection() ) )
05490     KMessageBox::error( this, i18n( "Area is too large." ) );
05491   else
05492   {
05493     doc()->emitBeginOperation( false );
05494     canvasWidget()->equalizeColumn();
05495     doc()->emitEndOperation( d->activeSheet->visibleRect( d->canvas ) );
05496   }
05497 }
05498 
05499 
05500 void KSpreadView::layoutDlg()
05501 {
05502   QRect selection( d->selectionInfo->selection() );
05503 
05504   if ( d->selectionInfo->singleCellSelection() )
05505   {
05506     CellFormatDlg dlg( this, d->activeSheet, selection.left(), selection.top(),
05507                        selection.left(), selection.top() );
05508   }
05509   else
05510     CellFormatDlg dlg( this, d->activeSheet, selection.left(), selection.top(),
05511                        selection.right(), selection.bottom() );
05512 }
05513 
05514 void KSpreadView::styleDialog()
05515 {
05516   KSpreadStyleDlg dlg( this, doc()->styleManager() );
05517   dlg.exec();
05518 
05519   d->actions->selectStyle->setItems( doc()->styleManager()->styleNames() );
05520   if ( d->activeSheet )
05521   {
05522     d->activeSheet->setLayoutDirtyFlag();
05523     d->activeSheet->setRegionPaintDirty( d->activeSheet->visibleRect( d->canvas ) );
05524   }
05525   if ( d->canvas )
05526     d->canvas->repaint();
05527 }
05528 
05529 void KSpreadView::paperLayoutDlg()
05530 {
05531   if ( d->canvas->editor() )
05532   {
05533     d->canvas->deleteEditor( true ); // save changes
05534   }
05535   KSpreadSheetPrint* print = d->activeSheet->print();
05536 
05537   KoPageLayout pl;
05538   pl.format = print->paperFormat();
05539   pl.orientation = print->orientation();
05540 
05541   pl.ptWidth =  MM_TO_POINT( print->paperWidth() );
05542   pl.ptHeight = MM_TO_POINT( print->paperHeight() );
05543   pl.ptLeft =   MM_TO_POINT( print->leftBorder() );
05544   pl.ptRight =  MM_TO_POINT( print->rightBorder() );
05545   pl.ptTop =    MM_TO_POINT( print->topBorder() );
05546   pl.ptBottom = MM_TO_POINT( print->bottomBorder() );
05547 
05548   KoHeadFoot hf;
05549   hf.headLeft  = print->localizeHeadFootLine( print->headLeft()  );
05550   hf.headRight = print->localizeHeadFootLine( print->headRight() );
05551   hf.headMid   = print->localizeHeadFootLine( print->headMid()   );
05552   hf.footLeft  = print->localizeHeadFootLine( print->footLeft()  );
05553   hf.footRight = print->localizeHeadFootLine( print->footRight() );
05554   hf.footMid   = print->localizeHeadFootLine( print->footMid()   );
05555 
05556   KoUnit::Unit unit = doc()->getUnit();
05557 
05558   KSpreadPaperLayout * dlg
05559     = new KSpreadPaperLayout( this, "PageLayout", pl, hf,
05560                               FORMAT_AND_BORDERS | HEADER_AND_FOOTER,
05561                               unit, d->activeSheet, this );
05562   dlg->show();
05563   // dlg destroys itself
05564 }
05565 
05566 void KSpreadView::definePrintRange()
05567 {
05568   d->activeSheet->print()->definePrintRange( selectionInfo() );
05569 }
05570 
05571 void KSpreadView::resetPrintRange()
05572 {
05573   d->activeSheet->print()->resetPrintRange();
05574 }
05575 
05576 void KSpreadView::wrapText( bool b )
05577 {
05578   if ( d->toolbarLock )
05579     return;
05580 
05581   if ( d->activeSheet != 0L )
05582   {
05583     doc()->emitBeginOperation( false );
05584     d->activeSheet->setSelectionMultiRow( selectionInfo(), b );
05585     doc()->emitEndOperation( d->activeSheet->visibleRect( d->canvas ) );
05586   }
05587 }
05588 
05589 void KSpreadView::alignLeft( bool b )
05590 {
05591   if ( d->toolbarLock )
05592     return;
05593 
05594   if ( d->activeSheet != 0L )
05595   {
05596     doc()->emitBeginOperation( false );
05597     if ( !b )
05598       d->activeSheet->setSelectionAlign( selectionInfo(),
05599                                    KSpreadFormat::Undefined );
05600     else
05601       d->activeSheet->setSelectionAlign( selectionInfo(),
05602                                    KSpreadFormat::Left );
05603     endOperation( selectionInfo()->selection() );
05604   }
05605 }
05606 
05607 void KSpreadView::alignRight( bool b )
05608 {
05609   if ( d->toolbarLock )
05610     return;
05611 
05612   if ( d->activeSheet != 0L )
05613   {
05614     doc()->emitBeginOperation( false );
05615     if ( !b )
05616       d->activeSheet->setSelectionAlign( selectionInfo(), KSpreadFormat::Undefined );
05617     else
05618       d->activeSheet->setSelectionAlign( selectionInfo(), KSpreadFormat::Right );
05619 
05620     endOperation( selectionInfo()->selection() );
05621   }
05622 }
05623 
05624 void KSpreadView::alignCenter( bool b )
05625 {
05626   if ( d->toolbarLock )
05627     return;
05628 
05629   if ( d->activeSheet != 0L )
05630   {
05631     doc()->emitBeginOperation( false );
05632     if ( !b )
05633       d->activeSheet->setSelectionAlign( selectionInfo(), KSpreadFormat::Undefined );
05634     else
05635       d->activeSheet->setSelectionAlign( selectionInfo(), KSpreadFormat::Center );
05636 
05637     endOperation( selectionInfo()->selection() );
05638   }
05639 }
05640 
05641 void KSpreadView::alignTop( bool b )
05642 {
05643   if ( d->toolbarLock )
05644     return;
05645 
05646   if ( d->activeSheet != 0L )
05647   {
05648     doc()->emitBeginOperation( false );
05649     if ( !b )
05650       d->activeSheet->setSelectionAlignY( selectionInfo(), KSpreadFormat::UndefinedY );
05651     else
05652       d->activeSheet->setSelectionAlignY( selectionInfo(), KSpreadFormat::Top );
05653 
05654     endOperation( selectionInfo()->selection() );
05655   }
05656 }
05657 
05658 void KSpreadView::alignBottom( bool b )
05659 {
05660   if ( d->toolbarLock )
05661     return;
05662 
05663   if ( d->activeSheet != 0L )
05664   {
05665     doc()->emitBeginOperation( false );
05666     if ( !b )
05667       d->activeSheet->setSelectionAlignY( selectionInfo(), KSpreadFormat::UndefinedY );
05668     else
05669       d->activeSheet->setSelectionAlignY( selectionInfo(), KSpreadFormat::Bottom );
05670 
05671     endOperation( selectionInfo()->selection() );
05672   }
05673 }
05674 
05675 void KSpreadView::alignMiddle( bool b )
05676 {
05677   if ( d->toolbarLock )
05678     return;
05679 
05680   if ( d->activeSheet != 0L )
05681   {
05682     doc()->emitBeginOperation( false );
05683     if ( !b )
05684       d->activeSheet->setSelectionAlignY( selectionInfo(), KSpreadFormat::UndefinedY );
05685     else
05686       d->activeSheet->setSelectionAlignY( selectionInfo(), KSpreadFormat::Middle );
05687 
05688     endOperation( selectionInfo()->selection() );
05689   }
05690 }
05691 
05692 void KSpreadView::moneyFormat(bool b)
05693 {
05694   if ( d->toolbarLock )
05695     return;
05696 
05697   doc()->emitBeginOperation( false );
05698   if ( d->activeSheet != 0L )
05699     d->activeSheet->setSelectionMoneyFormat( selectionInfo(), b );
05700   updateEditWidget();
05701   endOperation( selectionInfo()->selection() );
05702 }
05703 
05704 void KSpreadView::createStyleFromCell()
05705 {
05706   if ( !d->activeSheet )
05707     return;
05708 
05709   QPoint p( d->selectionInfo->selection().topLeft() );
05710   KSpreadCell * cell = d->activeSheet->nonDefaultCell( p.x(), p.y() );
05711 
05712   bool ok = false;
05713   QString styleName( "" );
05714 
05715   while( true )
05716   {
05717     styleName = KLineEditDlg::getText( i18n( "Create Style From Cell" ),
05718                                        i18n( "Enter name:" ), styleName, &ok, this );
05719 
05720     if ( !ok ) // User pushed an OK button.
05721       return;
05722 
05723     styleName = styleName.stripWhiteSpace();
05724 
05725     if ( styleName.length() < 1 )
05726     {
05727       KNotifyClient::beep();
05728       KMessageBox::sorry( this, i18n( "The style name cannot be empty." ) );
05729       continue;
05730     }
05731 
05732     if ( doc()->styleManager()->style( styleName ) != 0 )
05733     {
05734       KNotifyClient::beep();
05735       KMessageBox::sorry( this, i18n( "A style with this name already exists." ) );
05736       continue;
05737     }
05738     break;
05739   }
05740 
05741   KSpreadCustomStyle * style = new KSpreadCustomStyle( cell->kspreadStyle(), styleName );
05742 
05743   doc()->styleManager()->m_styles[ styleName ] = style;
05744   cell->setKSpreadStyle( style );
05745   QStringList lst( d->actions->selectStyle->items() );
05746   lst.push_back( styleName );
05747   d->actions->selectStyle->setItems( lst );
05748 }
05749 
05750 void KSpreadView::styleSelected( const QString & style )
05751 {
05752   if (d->activeSheet )
05753   {
05754     KSpreadStyle * s = doc()->styleManager()->style( style );
05755 
05756     if ( s )
05757     {
05758       doc()->emitBeginOperation(false);
05759       d->activeSheet->setSelectionStyle( selectionInfo(), s );
05760       endOperation( selectionInfo()->selection() );
05761     }
05762   }
05763 }
05764 
05765 void KSpreadView::precisionPlus()
05766 {
05767   setSelectionPrecision( 1 );
05768 }
05769 
05770 void KSpreadView::precisionMinus()
05771 {
05772   setSelectionPrecision( -1 );
05773 }
05774 
05775 void KSpreadView::setSelectionPrecision( int delta )
05776 {
05777   if ( d->activeSheet != NULL )
05778   {
05779     doc()->emitBeginOperation( false );
05780     d->activeSheet->setSelectionPrecision( selectionInfo(), delta );
05781     endOperation( selectionInfo()->selection() );
05782   }
05783 }
05784 
05785 void KSpreadView::percent( bool b )
05786 {
05787   if ( d->toolbarLock )
05788     return;
05789 
05790   doc()->emitBeginOperation( false );
05791   if ( d->activeSheet != 0L )
05792     d->activeSheet->setSelectionPercent( selectionInfo() ,b );
05793   updateEditWidget();
05794 
05795   endOperation( selectionInfo()->selection() );
05796 }
05797 
05798 void KSpreadView::insertObject()
05799 {
05800   doc()->emitBeginOperation( false );
05801   KoDocumentEntry e =  d->actions->insertPart->documentEntry();//KoPartSelectDia::selectPart( d->canvas );
05802   if ( e.isEmpty() )
05803   {
05804     doc()->emitEndOperation( d->activeSheet->visibleRect( d->canvas ) );
05805     return;
05806   }
05807 
05808   //Don't start handles more than once
05809   delete d->insertHandler;
05810 
05811   d->insertHandler = new KSpreadInsertHandler( this, d->canvas, e );
05812   doc()->emitEndOperation( d->activeSheet->visibleRect( d->canvas ) );
05813 }
05814 
05815 void KSpreadView::insertChart()
05816 {
05817   if ( util_isColumnSelected(selection()) || util_isRowSelected(selection()) )
05818   {
05819     KMessageBox::error( this, i18n("Area too large."));
05820     return;
05821   }
05822   QValueList<KoDocumentEntry> vec = KoDocumentEntry::query( true, "'KOfficeChart' in ServiceTypes" );
05823   if ( vec.isEmpty() )
05824   {
05825     KMessageBox::error( this, i18n("No charting component registered.") );
05826     return;
05827   }
05828 
05829   //Don't start handles more than once
05830   delete d->insertHandler;
05831 
05832   doc()->emitBeginOperation( false );
05833 
05834   d->insertHandler = new KSpreadInsertHandler( this, d->canvas, vec[0], TRUE );
05835   doc()->emitEndOperation( d->activeSheet->visibleRect( d->canvas ) );
05836 }
05837 
05838 
05839 /*
05840   // TODO Use KoView setScaling/xScaling/yScaling instead
05841 void KSpreadView::zoomMinus()
05842 {
05843   if ( m_fZoom <= 0.25 )
05844     return;
05845 
05846   m_fZoom -= 0.25;
05847 
05848   if ( d->activeSheet != 0L )
05849     d->activeSheet->setLayoutDirtyFlag();
05850 
05851   d->canvas->repaint();
05852   d->vBorderWidget->repaint();
05853   d->hBorderWidget->repaint();
05854 }
05855 
05856 void KSpreadView::zoomPlus()
05857 {
05858   if ( m_fZoom >= 3 )
05859     return;
05860 
05861   m_fZoom += 0.25;
05862 
05863   if ( d->activeSheet != 0L )
05864     d->activeSheet->setLayoutDirtyFlag();
05865 
05866   d->canvas->repaint();
05867   d->vBorderWidget->repaint();
05868   d->hBorderWidget->repaint();
05869 }
05870 */
05871 
05872 void KSpreadView::removeSheet()
05873 {
05874   if ( doc()->map()->count() <= 1 || ( doc()->map()->visibleSheets().count() <= 1 ) )
05875   {
05876     KNotifyClient::beep();
05877     KMessageBox::sorry( this, i18n("You cannot delete the only sheet."), i18n("Remove Sheet") ); // FIXME bad english? no english!
05878     return;
05879   }
05880   KNotifyClient::beep();
05881   int ret = KMessageBox::warningContinueCancel( this, i18n( "You are about to remove the active sheet.\nDo you want to continue?" ),
05882                                        i18n( "Remove Sheet" ),KGuiItem(i18n("&Delete"),"editdelete") );
05883 
05884   if ( ret == KMessageBox::Continue )
05885   {
05886     doc()->emitBeginOperation( false );
05887     if ( d->canvas->editor() )
05888     {
05889       d->canvas->deleteEditor( false );
05890     }
05891     doc()->setModified( true );
05892     KSpreadSheet * tbl = activeSheet();
05893     KCommand* command = new RemoveSheetCommand( tbl );
05894     doc()->addCommand( command );
05895     command->execute();
05896 
05897 
05898 #if 0
05899     KSpreadUndoRemoveSheet * undo = new KSpreadUndoRemoveSheet( doc(), tbl );
05900     doc()->addCommand( undo );
05901     tbl->doc()->map()->takeSheet( tbl );
05902     doc()->takeSheet( tbl );
05903 #endif
05904     doc()->emitEndOperation( d->activeSheet->visibleRect( d->canvas ) );
05905   }
05906 }
05907 
05908 
05909 void KSpreadView::slotRename()
05910 {
05911 
05912   KSpreadSheet * sheet = activeSheet();
05913 
05914   if( sheet->isProtected() )
05915   {
05916       KMessageBox::error( 0, i18n ( "You cannot change a protected sheet." ) );
05917       return;
05918   }
05919 
05920   bool ok;
05921   QString activeName = sheet->sheetName();
05922   QString newName = KLineEditDlg::getText( i18n("Rename Sheet"),i18n("Enter name:"), activeName, &ok, this );
05923 
05924   if( !ok ) return;
05925 
05926   while (!util_validateSheetName(newName))
05927   {
05928     KNotifyClient::beep();
05929     KMessageBox::information( this, i18n("Sheet name contains illegal characters. Only numbers and letters are allowed."),
05930       i18n("Change Sheet Name") );
05931 
05932     newName = newName.simplifyWhiteSpace();
05933     int n = newName.find('-');
05934     if ( n > -1 ) newName[n] = '_';
05935     n = newName.find('!');
05936     if ( n > -1 ) newName[n] = '_';
05937     n = newName.find('$');
05938     if ( n > -1 ) newName[n] = '_';
05939 
05940     newName = KLineEditDlg::getText( i18n("Rename Sheet"),i18n("Enter name:"), newName, &ok, this );
05941 
05942     if ( !ok ) return;
05943   }
05944 
05945   if ( (newName.stripWhiteSpace()).isEmpty() ) // Sheet name is empty.
05946   {
05947     KNotifyClient::beep();
05948     KMessageBox::information( this, i18n("Sheet name cannot be empty."), i18n("Change Sheet Name") );
05949     // Recursion
05950     slotRename();
05951   }
05952   else if ( newName != activeName ) // Sheet name changed.
05953   {
05954     // Is the name already used
05955     if ( doc()->map()->findSheet( newName ) )
05956     {
05957       KNotifyClient::beep();
05958       KMessageBox::information( this, i18n("This name is already used."), i18n("Change Sheet Name") );
05959       // Recursion
05960       slotRename();
05961       return;
05962     }
05963 
05964     KCommand* command = new RenameSheetCommand( sheet, newName );
05965     doc()->addCommand( command );
05966     command->execute();
05967 
05968     //sheet->setSheetName( newName );
05969 
05970     doc()->emitBeginOperation(false);
05971     updateEditWidget();
05972     doc()->setModified( true );
05973     doc()->emitEndOperation( d->activeSheet->visibleRect( d->canvas ) );
05974   }
05975 }
05976 
05977 void KSpreadView::setText( const QString & _text )
05978 {
05979   if ( d->activeSheet == 0L )
05980     return;
05981 
05982   int x = d->canvas->markerColumn();
05983   int y = d->canvas->markerRow();
05984 
05985   doc()->emitBeginOperation( false );
05986   d->activeSheet->setText( y, x, _text );
05987   KSpreadCell * cell = d->activeSheet->cellAt( x, y );
05988 
05989   if ( cell->value().isString() && !_text.isEmpty() && !_text.at(0).isDigit() && !cell->isFormula() )
05990     doc()->addStringCompletion( _text );
05991 
05992   doc()->emitEndOperation( QRect( x, y, 1, 1 ) );
05993 }
05994 
05995 //------------------------------------------------
05996 //
05997 // Document signals
05998 //
05999 //------------------------------------------------
06000 
06001 void KSpreadView::slotAddSheet( KSpreadSheet *_sheet )
06002 {
06003   addSheet( _sheet );
06004 }
06005 
06006 void KSpreadView::slotRefreshView()
06007 {
06008   refreshView();
06009   d->canvas->repaint();
06010   d->vBorderWidget->repaint();
06011   d->hBorderWidget->repaint();
06012 }
06013 
06014 void KSpreadView::slotUpdateView( KSpreadSheet *_sheet )
06015 {
06016   // Do we display this sheet ?
06017   if ( _sheet != d->activeSheet )
06018     return;
06019 
06020   //  doc()->emitBeginOperation( false );
06021 
06022   //  d->activeSheet->setRegionPaintDirty(QRect(QPoint(0,0),
06023   //                                      QPoint(KS_colMax, KS_rowMax)));
06024 
06025   doc()->emitEndOperation( d->activeSheet->visibleRect( d->canvas ) );
06026 }
06027 
06028 void KSpreadView::slotUpdateView( KSpreadSheet * _sheet, const QRect & _rect )
06029 {
06030   // qDebug("void KSpreadView::slotUpdateView( KSpreadSheet *_sheet, const QRect& %i %i|%i %i )\n",_rect.left(),_rect.top(),_rect.right(),_rect.bottom());
06031 
06032   // Do we display this sheet ?
06033   if ( _sheet != d->activeSheet )
06034     return;
06035 
06036   // doc()->emitBeginOperation( false );
06037   d->activeSheet->setRegionPaintDirty( _rect );
06038   endOperation( _rect );
06039 }
06040 
06041 void KSpreadView::slotUpdateHBorder( KSpreadSheet * _sheet )
06042 {
06043   // kdDebug(36001)<<"void KSpreadView::slotUpdateHBorder( KSpreadSheet *_sheet )\n";
06044 
06045   // Do we display this sheet ?
06046   if ( _sheet != d->activeSheet )
06047     return;
06048 
06049   doc()->emitBeginOperation(false);
06050   d->hBorderWidget->update();
06051   doc()->emitEndOperation( d->activeSheet->visibleRect( d->canvas ) );
06052 }
06053 
06054 void KSpreadView::slotUpdateVBorder( KSpreadSheet *_sheet )
06055 {
06056   // kdDebug("void KSpreadView::slotUpdateVBorder( KSpreadSheet *_sheet )\n";
06057 
06058   // Do we display this sheet ?
06059   if ( _sheet != d->activeSheet )
06060     return;
06061 
06062   doc()->emitBeginOperation( false );
06063   d->vBorderWidget->update();
06064   doc()->emitEndOperation( d->activeSheet->visibleRect( d->canvas ) );
06065 }
06066 
06067 void KSpreadView::slotChangeSelection( KSpreadSheet *_sheet,
06068                                        const QRect &oldSelection,
06069                                        const QPoint& /* oldMarker*/ )
06070 {
06071   doc()->emitBeginOperation( false );
06072   QRect newSelection = d->selectionInfo->selection();
06073 
06074   // Emit a signal for internal use
06075   emit sig_selectionChanged( _sheet, newSelection );
06076 
06077   // Empty selection ?
06078   // Activate or deactivate some actions.
06079   bool colSelected = util_isColumnSelected( selection() );
06080   bool rowSelected = util_isRowSelected( selection() );
06081 
06082   if ( d->activeSheet && !d->activeSheet->isProtected() )
06083   {
06084     d->actions->resizeRow->setEnabled( !colSelected );
06085     d->actions->equalizeRow->setEnabled( !colSelected );
06086     d->actions->validity->setEnabled( !colSelected && !rowSelected);
06087     d->actions->conditional->setEnabled( !colSelected && !rowSelected);
06088     d->actions->resizeColumn->setEnabled( !rowSelected );
06089     d->actions->equalizeColumn->setEnabled( !rowSelected );
06090     d->actions->textToColumns->setEnabled( !rowSelected );
06091 
06092     bool simpleSelection = d->selectionInfo->singleCellSelection()
06093       || colSelected || rowSelected;
06094     d->actions->autoFormat->setEnabled( !simpleSelection );
06095     d->actions->sort->setEnabled( !simpleSelection );
06096     d->actions->mergeCell->setEnabled( !simpleSelection );
06097     d->actions->fillRight->setEnabled( !simpleSelection );
06098     d->actions->fillUp->setEnabled( !simpleSelection );
06099     d->actions->fillDown->setEnabled( !simpleSelection );
06100     d->actions->fillLeft->setEnabled( !simpleSelection );
06101     d->actions->insertChartFrame->setEnabled( !simpleSelection );
06102     d->actions->sortDec->setEnabled( !simpleSelection );
06103     d->actions->sortInc->setEnabled( !simpleSelection);
06104     d->actions->createStyle->setEnabled( simpleSelection ); // just from one cell
06105   }
06106   d->actions->selectStyle->setCurrentItem( -1 );
06107   resultOfCalc();
06108   // Send some event around. This is read for example
06109   // by the calculator plugin.
06110   KSpreadSelectionChanged ev( newSelection, activeSheet()->name() );
06111   QApplication::sendEvent( this, &ev );
06112 
06113   // Do we display this sheet ?
06114   if ( _sheet != d->activeSheet )
06115   {
06116     doc()->emitEndOperation( d->activeSheet->visibleRect( d->canvas ) );
06117     return;
06118   }
06119 
06120   d->canvas->setSelectionChangePaintDirty( d->activeSheet, oldSelection, newSelection );
06121 
06122   d->vBorderWidget->update();
06123   d->hBorderWidget->update();
06124   doc()->emitEndOperation( newSelection );
06125 }
06126 
06127 void KSpreadView::resultOfCalc()
06128 {
06129   KSpreadSheet * sheet = activeSheet();
06130   ValueCalc* calc = d->doc->calc();
06131   KSpreadValue val;
06132   QRect tmpRect(d->selectionInfo->selection());
06133   MethodOfCalc tmpMethod = doc()->getTypeOfCalc() ;
06134   if ( tmpMethod != NoneCalc )
06135   {
06136 
06137     KSpreadValue range = sheet->valueRange (tmpRect.left(), tmpRect.top(),
06138         tmpRect.right(), tmpRect.bottom());
06139     switch (tmpMethod)
06140     {
06141       case SumOfNumber:
06142         val = calc->sum (range);
06143       break;
06144       case Average:
06145         val = calc->avg (range);
06146       break;
06147       case Min:
06148         val = calc->min (range);
06149       break;
06150       case Max:
06151         val = calc->max (range);
06152       break;
06153       case CountA:
06154         val = KSpreadValue (calc->countA (range));
06155         break;
06156       case Count:
06157         val = KSpreadValue (calc->count (range));
06158       case NoneCalc:
06159       break;
06160       default:
06161       break;
06162     }
06163 
06164   }
06165 
06166   QString res = d->doc->converter()->asString (val).asString ();
06167   QString tmp;
06168   switch(tmpMethod )
06169   {
06170    case SumOfNumber:
06171     tmp = i18n("Sum: ") + res;
06172     break;
06173    case Average:
06174     tmp = i18n("Average: ") + res;
06175     break;
06176    case Min:
06177     tmp = i18n("Min: ") + res;
06178     break;
06179    case Max:
06180     tmp = i18n("Max: ") + res;
06181     break;
06182    case Count:
06183     tmp = i18n("Count: ") + res;
06184     break;
06185    case CountA:
06186     tmp = i18n("CountA: ") + res;
06187     break;
06188    case NoneCalc:
06189     tmp = "";
06190     break;
06191   }
06192 
06193   //doc()->emitBeginOperation();
06194   if ( d->calcLabel )
06195     d->calcLabel->setText(QString(" ") + tmp + ' ');
06196   //doc()->emitEndOperation();
06197 }
06198 
06199 void KSpreadView::statusBarClicked(int _id)
06200 {
06201   if ( !koDocument()->isReadWrite() || !factory() )
06202     return;
06203   if ( _id == 0 ) //menu calc
06204   {
06205     QPoint mousepos = QCursor::pos();
06206     ((QPopupMenu*)factory()->container( "calc_popup" , this ) )->popup( mousepos );
06207   }
06208 }
06209 
06210 void KSpreadView::menuCalc( bool )
06211 {
06212   doc()->emitBeginOperation(false);
06213   if ( d->actions->calcMin->isChecked() )
06214   {
06215     doc()->setTypeOfCalc( Min );
06216   }
06217   else if ( d->actions->calcMax->isChecked() )
06218   {
06219     doc()->setTypeOfCalc( Max );
06220   }
06221   else if ( d->actions->calcCount->isChecked() )
06222   {
06223     doc()->setTypeOfCalc( Count );
06224   }
06225   else if ( d->actions->calcAverage->isChecked() )
06226   {
06227     doc()->setTypeOfCalc( Average );
06228   }
06229   else if ( d->actions->calcSum->isChecked() )
06230   {
06231     doc()->setTypeOfCalc( SumOfNumber );
06232   }
06233   else if ( d->actions->calcCountA->isChecked() )
06234   {
06235     doc()->setTypeOfCalc( CountA );
06236   }
06237   else if ( d->actions->calcNone->isChecked() )
06238     doc()->setTypeOfCalc( NoneCalc );
06239 
06240   resultOfCalc();
06241 
06242   doc()->emitEndOperation( d->activeSheet->visibleRect( d->canvas ) );
06243 }
06244 
06245 
06246 QWMatrix KSpreadView::matrix() const
06247 {
06248   QWMatrix m;
06249   m.scale( d->doc->zoomedResolutionX(),
06250            d->doc->zoomedResolutionY() );
06251   m.translate( - d->canvas->xOffset(), - d->canvas->yOffset() );
06252   return m;
06253 }
06254 
06255 void KSpreadView::transformPart()
06256 {
06257     Q_ASSERT( selectedChild() );
06258 
06259     if ( d->transformToolBox.isNull() )
06260     {
06261         d->transformToolBox = new KoTransformToolBox( selectedChild(), topLevelWidget() );
06262         d->transformToolBox->show();
06263 
06264         d->transformToolBox->setDocumentChild( selectedChild() );
06265     }
06266     else
06267     {
06268         d->transformToolBox->show();
06269         d->transformToolBox->raise();
06270     }
06271 }
06272 
06273 void KSpreadView::slotChildSelected( KoDocumentChild* ch )
06274 {
06275   if ( d->activeSheet && !d->activeSheet->isProtected() )
06276   {
06277     d->actions->transform->setEnabled( TRUE );
06278 
06279     if ( !d->transformToolBox.isNull() )
06280     {
06281         d->transformToolBox->setEnabled( TRUE );
06282         d->transformToolBox->setDocumentChild( ch );
06283     }
06284   }
06285 
06286   doc()->emitBeginOperation( false );
06287   d->activeSheet->setRegionPaintDirty(QRect(QPoint(0,0), QPoint(KS_colMax, KS_rowMax)));
06288   doc()->emitEndOperation();
06289   paintUpdates();
06290 }
06291 
06292 void KSpreadView::slotChildUnselected( KoDocumentChild* )
06293 {
06294   if ( d->activeSheet && !d->activeSheet->isProtected() )
06295   {
06296     d->actions->transform->setEnabled( FALSE );
06297 
06298     if ( !d->transformToolBox.isNull() )
06299     {
06300         d->transformToolBox->setEnabled( FALSE );
06301     }
06302     deleteEditor( true );
06303   }
06304 
06305   doc()->emitBeginOperation( false );
06306   d->activeSheet->setRegionPaintDirty(QRect(QPoint(0,0), QPoint(KS_colMax, KS_rowMax)));
06307   doc()->emitEndOperation();
06308   paintUpdates();
06309 }
06310 
06311 
06312 void KSpreadView::deleteEditor( bool saveChanges )
06313 {
06314     doc()->emitBeginOperation( false );
06315     d->canvas->deleteEditor( saveChanges );
06316     doc()->emitEndOperation( selectionInfo()->selection() );
06317 }
06318 
06319 DCOPObject * KSpreadView::dcopObject()
06320 {
06321   if ( !d->dcop )
06322     d->dcop = new KSpreadViewIface( this );
06323 
06324   return d->dcop;
06325 }
06326 
06327 QWidget * KSpreadView::canvas()
06328 {
06329   return canvasWidget();
06330 }
06331 
06332 int KSpreadView::canvasXOffset() const
06333 {
06334   return int( canvasWidget()->xOffset() );
06335 }
06336 
06337 int KSpreadView::canvasYOffset() const
06338 {
06339   return int( canvasWidget()->yOffset() );
06340 }
06341 
06342 
06343 void KSpreadView::guiActivateEvent( KParts::GUIActivateEvent *ev )
06344 {
06345   if ( d->activeSheet )
06346   {
06347     doc()->emitEndOperation( d->activeSheet->visibleRect( d->canvas ) );
06348 
06349     if ( ev->activated() )
06350     {
06351       if ( d->calcLabel )
06352         resultOfCalc();
06353     }
06354     else
06355     {
06356       /*if (d->calcLabel)
06357         {
06358         disconnect(d->calcLabel,SIGNAL(pressed( int )),this,SLOT(statusBarClicked(int)));
06359         }*/
06360     }
06361   }
06362 
06363   KoView::guiActivateEvent( ev );
06364 }
06365 
06366 void KSpreadView::popupTabBarMenu( const QPoint & _point )
06367 {
06368   if ( !koDocument()->isReadWrite() || !factory() )
06369     return;
06370   if ( d->tabBar )
06371   {
06372     bool state = ( doc()->map()->visibleSheets().count() > 1 );
06373     if ( d->activeSheet && d->activeSheet->isProtected() )
06374     {
06375       d->actions->removeSheet->setEnabled( false );
06376       d->actions->hideSheet->setEnabled( false );
06377       d->actions->showSheet->setEnabled( false );
06378     }
06379     else
06380     {
06381       d->actions->removeSheet->setEnabled( state);
06382       d->actions->hideSheet->setEnabled( state );
06383       d->actions->showSheet->setEnabled( doc()->map()->hiddenSheets().count()>0 );
06384     }
06385     if ( !doc() || !doc()->map() || doc()->map()->isProtected() )
06386     {
06387       d->actions->insertSheet->setEnabled( false );
06388       d->actions->renameSheet->setEnabled( false );
06389       d->actions->showSheet->setEnabled( false );
06390       d->actions->hideSheet->setEnabled( false );
06391       d->actions->removeSheet->setEnabled( false );
06392     }
06393     static_cast<QPopupMenu*>(factory()->container("menupage_popup",this))->popup(_point);
06394   }
06395 }
06396 
06397 void KSpreadView::updateBorderButton()
06398 {
06399   //  doc()->emitBeginOperation( false );
06400   if ( d->activeSheet )
06401     d->actions->showPageBorders->setChecked( d->activeSheet->isShowPageBorders() );
06402   //  doc()->emitEndOperation();
06403 }
06404 
06405 void KSpreadView::removeSheet( KSpreadSheet *_t )
06406 {
06407   doc()->emitBeginOperation(false);
06408   QString m_tablName=_t->sheetName();
06409   d->tabBar->removeTab( m_tablName );
06410   setActiveSheet( doc()->map()->findSheet( doc()->map()->visibleSheets().first() ));
06411 
06412   bool state = doc()->map()->visibleSheets().count() > 1;
06413   d->actions->removeSheet->setEnabled( state );
06414   d->actions->hideSheet->setEnabled( state );
06415   doc()->emitEndOperation( d->activeSheet->visibleRect( d->canvas ) );
06416 }
06417 
06418 void KSpreadView::insertSheet( KSpreadSheet* sheet )
06419 {
06420   doc()->emitBeginOperation( false );
06421   QString tabName = sheet->sheetName();
06422   if ( !sheet->isHidden() )
06423   {
06424     d->tabBar->addTab( tabName );
06425   }
06426 
06427   bool state = ( doc()->map()->visibleSheets().count() > 1 );
06428   d->actions->removeSheet->setEnabled( state );
06429   d->actions->hideSheet->setEnabled( state );
06430   doc()->emitEndOperation( sheet->visibleRect( d->canvas ) );
06431 }
06432 
06433 QColor KSpreadView::borderColor() const
06434 {
06435   return d->actions->borderColor->color();
06436 }
06437 
06438 void KSpreadView::updateShowSheetMenu()
06439 {
06440   doc()->emitBeginOperation( false );
06441   if ( d->activeSheet->isProtected() )
06442     d->actions->showSheet->setEnabled( false );
06443   else
06444     d->actions->showSheet->setEnabled( doc()->map()->hiddenSheets().count() > 0 );
06445   doc()->emitEndOperation( d->activeSheet->visibleRect( d->canvas ) );
06446 }
06447 
06448 void KSpreadView::closeEditor()
06449 {
06450   if ( d->activeSheet ) { // #45822
06451     doc()->emitBeginOperation( false );
06452     d->canvas->closeEditor();
06453     doc()->emitEndOperation( selectionInfo()->selection() );
06454   }
06455 }
06456 
06457 
06458 void KSpreadView::paintUpdates()
06459 {
06460   /* don't do any begin/end operation here -- this is what is called at an
06461      endOperation
06462   */
06463   d->canvas->paintUpdates();
06464 }
06465 
06466 void KSpreadView::commandExecuted()
06467 {
06468   updateEditWidget();
06469   resultOfCalc();
06470 }
06471 
06472 QPoint KSpreadView::markerFromSheet( KSpreadSheet *_sheet ) const
06473 {
06474     QMapIterator<KSpreadSheet*, QPoint> it2 = d->savedMarkers.find(_sheet);
06475     QPoint newMarker = (it2 == d->savedMarkers.end()) ? QPoint(1,1) : *it2;
06476     return newMarker;
06477 }
06478 
06479 void KSpreadView::saveCurrentSheetSelection()
06480 {
06481     /* save the current selection on this sheet */
06482     if (d->activeSheet != NULL)
06483     {
06484         d->savedAnchors.replace(d->activeSheet, selectionInfo()->selectionAnchor());
06485         kdDebug() << " Current scrollbar vert value: " << d->canvas->vertScrollBar()->value() << endl;
06486         kdDebug() << "Saving marker pos: " << selectionInfo()->marker() << endl;
06487         d->savedMarkers.replace(d->activeSheet, selectionInfo()->marker());
06488     }
06489 }
06490 
06491 void KSpreadView::handleDamages( const QValueList<Damage*>& damages )
06492 {
06493     QValueList<Damage*>::ConstIterator it;
06494     for( it = damages.begin(); it != damages.end(); ++it )
06495     {
06496         Damage* damage = *it;
06497         if( !damage ) continue;
06498 
06499         if( damage->type() == Damage::Cell )
06500         {
06501             CellDamage* cd = static_cast<CellDamage*>( damage );
06502             KSpreadCell* damagedCell = cd->cell();
06503             KSpreadSheet* damagedSheet = damagedCell->sheet();
06504             QRect drect( damagedCell->column(), damagedCell->row(), 1, 1 );
06505             damagedSheet->setRegionPaintDirty( drect );
06506             paintUpdates();
06507         }
06508 
06509         if( damage->type() == Damage::Sheet )
06510         {
06511             SheetDamage* sd = static_cast<SheetDamage*>( damage );
06512             KSpreadSheet* damagedSheet = sd->sheet();
06513 
06514             if( sd->action() == SheetDamage::PropertiesChanged )
06515             {
06516                 CellBinding  * b = 0;
06517                 for ( b = damagedSheet->firstCellBinding(); b != 0;
06518                     b = damagedSheet->nextCellBinding() )
06519                         b->cellChanged( 0 );
06520 
06521                 d->activeSheet->setRegionPaintDirty( QRect(QPoint(0,0),
06522                     QPoint(KS_colMax, KS_rowMax)));
06523 
06524                 paintUpdates();
06525                 refreshView();
06526             }
06527 
06528         }
06529 
06530     }
06531 }
06532 
06533 void KSpreadView::runInternalTests()
06534 {
06535     // run various tests, only for developers
06536     KSpread::TestRunner* runner = new KSpread::TestRunner();
06537     runner->exec();
06538     delete runner;
06539 }
06540 
06541 void KSpreadView::runInspector()
06542 {
06543     // useful to inspect objects
06544     if(!d->activeSheet) return;
06545     KSpreadCell * cell = d->activeSheet->cellAt( d->selectionInfo->marker() );
06546     KSpread::Inspector* ins = new KSpread::Inspector( cell );
06547     ins->exec();
06548     delete ins;
06549 }
06550 
06551 
06552 
06553 #include "kspread_view.moc"
06554 
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:37 2006 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003