kspread

kspread_cell.h

00001 /* This file is part of the KDE project
00002 
00003    Copyright 2004 Tomas Mecir <mecirt@gmail.com>
00004    Copyright 1999-2002,2004 Laurent Montel <montel@kde.org>
00005    Copyright 2002,2004 Ariya Hidayat <ariya@kde.org>
00006    Copyright 2002-2003 Norbert Andres <nandres@web.de>
00007    Copyright 2003 Stefan Hetzl <shetzl@chello.at>
00008    Copyright 2001-2002 Philipp Mueller <philipp.mueller@gmx.de>
00009    Copyright 2002 Harri Porten <porten@kde.org>
00010    Copyright 2002 John Dailey <dailey@vt.edu>
00011    Copyright 1999-2001 David Faure <faure@kde.org>
00012    Copyright 2000-2001 Werner Trobin <trobin@kde.org>
00013    Copyright 2000 Simon Hausmann <hausmann@kde.org
00014    Copyright 1998-1999 Torben Weis <weis@kde.org>
00015    Copyright 1999 Michael Reiher <michael.reiher.gmx.de>
00016    Copyright 1999 Reginald Stadlbauer <reggie@kde.org>
00017 
00018    This library is free software; you can redistribute it and/or
00019    modify it under the terms of the GNU Library General Public
00020    License as published by the Free Software Foundation; either
00021    version 2 of the License, or (at your option) any later version.
00022 
00023    This library is distributed in the hope that it will be useful,
00024    but WITHOUT ANY WARRANTY; without even the implied warranty of
00025    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00026    Library General Public License for more details.
00027 
00028    You should have received a copy of the GNU Library General Public License
00029    along with this library; see the file COPYING.LIB.  If not, write to
00030    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00031  * Boston, MA 02110-1301, USA.
00032 */
00033 
00034 #ifndef KSPREAD_CELL
00035 #define KSPREAD_CELL
00036 
00037 #include <qpainter.h>
00038 #include <qptrlist.h>
00039 #include <qdatetime.h>
00040 
00041 #include "kspread_condition.h"
00042 
00043 class KLocale;
00044 class QDomElement;
00045 class QDomDocument;
00046 class KoXmlWriter;
00047 class KoGenStyles;
00048 class KoGenStyle;
00049 class KSParseNode;
00050 class KoRect;
00051 class KoPoint;
00052 class KoOasisStyles;
00053 class KoOasisLoadingContext;
00054 
00055 namespace KSpread
00056 {
00057 class Canvas;
00058 class Format;
00059 class GenValidationStyles;
00060 class Sheet;
00061 class Value;
00062 class View;
00063 class ConditionalDialog;
00064 
00065 struct Validity
00066 {
00067   Validity()
00068   {
00069       valMin = 0.0;
00070       valMax = 0.0;
00071       m_cond = Conditional::None;
00072       m_action = Action::Stop;
00073       m_restriction = Restriction::None;
00074       displayMessage = true;
00075       allowEmptyCell = false;
00076       displayValidationInformation = false;
00077   };
00078 
00079     QString message;
00080     QString title;
00081     QString titleInfo;
00082     QString messageInfo;
00083     double valMin;
00084     double valMax;
00085     Conditional::Type m_cond;
00086     Action::Type m_action;
00087     Restriction::Type m_restriction;
00088     QTime  timeMin;
00089     QTime  timeMax;
00090     QDate  dateMin;
00091     QDate  dateMax;
00092     bool displayMessage;
00093     bool allowEmptyCell;
00094     bool displayValidationInformation;
00095     QStringList listValidity;
00096 };
00097 
00098 class Formula;
00099 
00110 class KSPREAD_EXPORT Cell
00111 {
00112   friend class Conditions;
00113 public:
00114 
00115   Cell (Sheet *_sheet, int _column, int _row);
00116   Cell (Sheet *_sheet, Style * _style, int _column, int _row);
00117 
00121     ~Cell();
00122 
00126     Sheet* sheet() const;
00127 
00132     bool isDefault() const;
00133 
00137     bool isEmpty() const;
00138 
00142     int column() const;
00143 
00147     int row() const;
00148 
00153     QString name() const;
00154 
00159     QString fullName() const;
00160 
00164     QString columnName() const;
00165 
00170     static QString name( int col, int row );
00171 
00176     static QString fullName( const Sheet *s, int col, int row );
00177 
00182     static QString columnName( uint column );
00183 
00187     KLocale* locale() const;
00191     bool isFormula() const;
00192 
00197     QString text() const;
00198 
00199     QString strOutText() const;
00200 
00201     Formula *formula () const;
00202 
00204     Format* format() const;
00205 
00210     const Value value() const;
00211 
00215     void setValue( const Value& value );
00218     void setCellValue (const Value &v, FormatType fmtType = No_format,
00219   const QString &txt = QString::null);
00220 
00221     Cell* previousCell() const;
00222     Cell* nextCell() const;
00223     void setPreviousCell( Cell* c );
00224     void setNextCell( Cell* c );
00225 
00230     void move( int column, int row );
00231 
00235     // Note:  This used to remove any links from this cell to other cells.  However, this caused a problem
00236     // in other parts of the code which relied upon walking from one cell to the next using
00237     // nextCell().
00238     void sheetDies();
00239 
00255     QDomElement save( QDomDocument& doc, int _x_offset = 0, int _y_offset = 0, bool force = false, bool copy = false, bool era = false );
00256 
00257     bool saveOasis( KoXmlWriter& xmlwriter, KoGenStyles& mainStyles,
00258                     int row, int column, int &repeated,
00259                     GenValidationStyles &valStyle );
00260 
00261     void saveOasisValue (KoXmlWriter &xmlWriter);
00262 
00266     QString saveOasisCellStyle( KoGenStyle &currentCellStyle,KoGenStyles &mainStyles );
00267 
00268     bool load( const QDomElement& cell, int _xshift, int _yshift, Paste::Mode pm = Paste::Normal,
00269                Paste::Operation op = Paste::OverWrite, bool paste = false );
00270 
00276     bool loadOasis( const QDomElement & element, KoOasisLoadingContext &oasisContext , Style* style);
00277 
00278     QTime toTime(const QDomElement &element);
00279     QDate toDate(const QDomElement &element);
00280 
00286     void copyFormat( const int column, const int row );
00292     void copyFormat( const Cell* cell );
00293     void copyContent( const Cell* cell );
00301     void copyAll( Cell *cell);
00302 
00303     enum BorderSides
00304     {
00305       Border_None   =0x00,
00306       Border_Left   =0x01,
00307       Border_Right  =0x02,
00308       Border_Top    =0x04,
00309       Border_Bottom =0x08,
00310       Border_SizeGrip   =0x10   //the size grip is the little square on the bottom right-hand corner of a highlighted range of cells
00311               //which the user can click and drag to resize the range and change which cells are included.
00312               //this is not used with normal borders
00313     };
00314 
00333     void paintCell( const KoRect & rect, QPainter & painter,
00334                     View * view, const KoPoint & coordinate,
00335                     const QPoint & cellRef,
00336             int paintBorder,
00337             QPen & rightPen,
00338                     QPen & bottomPen,
00339                     QPen & leftPen,
00340                     QPen & topPen,
00341             QValueList<QPoint> &mergedCellsPainted,
00342                     bool drawCursor = true );
00343 
00344 
00352     int width( int _col = -1, const Canvas *_canvas = 0L ) const;
00353 
00360     int height( int _row = -1, const Canvas *_canvas = 0L ) const;
00361 
00369     double dblWidth( int _col = -1, const Canvas *_canvas = 0L ) const;
00370 
00377     double dblHeight( int _row = -1, const Canvas *_canvas = 0L ) const;
00378 
00382     QRect cellRect();
00383 
00391     bool needsPrinting() const;
00392 
00400     void incPrecision();
00408     void decPrecision();
00409 
00414     void setCellText( const QString& _text, bool asString = false );
00415 
00421     void setDisplayText( const QString& _text );
00422 
00429     void setLink( const QString& link );
00430 
00435     QString link() const;
00436 
00438     //
00439     // Methods for querying format stuff.
00440     //
00442 
00450     const QPen & effLeftBorderPen( int col, int row ) const;
00455     const QPen & effTopBorderPen( int col, int row ) const;
00460     const QPen & effRightBorderPen( int col, int row ) const;
00465     const QPen & effBottomBorderPen( int col, int row ) const;
00472     const QPen & effGoUpDiagonalPen( int col, int row ) const;
00477     const QPen & effFallDiagonalPen( int col, int row ) const;
00478     const QColor & effTextColor( int col, int row ) const;
00479 
00485     uint effBottomBorderValue( int col, int row ) const;
00491     uint effRightBorderValue( int col, int row ) const;
00497     uint effLeftBorderValue( int col, int row ) const;
00503     uint effTopBorderValue( int col, int row ) const;
00504 
00508     const QPen& leftBorderPen( int col, int row ) const;
00509 
00513     const QPen& topBorderPen( int col, int row ) const;
00514 
00518     const QPen& rightBorderPen( int col, int row ) const;
00519 
00523     const QPen& bottomBorderPen( int col, int row ) const;
00524 
00528     const QColor& bgColor( int col, int row ) const;
00529 
00533     const QBrush& backGroundBrush( int col, int row ) const;
00534 
00536     //
00537     // Methods for setting format stuff.
00538     //
00540 
00544     void setLeftBorderPen( const QPen& p );
00545 
00549     void setTopBorderPen( const QPen& p );
00550 
00554     void setRightBorderPen( const QPen& p );
00555 
00559     void setBottomBorderPen( const QPen& p );
00560 
00562     //
00563     // Other stuff
00564     //
00566 
00573     FormatType formatType() const;
00574 
00576     bool isDate() const;
00578     bool isTime() const;
00579 
00580     void setNumber( double number );
00581 
00583     double getDouble ();
00584 
00586     void convertToDouble ();
00588     void convertToPercent ();
00590     void convertToMoney ();
00592     void convertToTime ();
00594     void convertToDate ();
00595 
00597     double textWidth() const;
00599     double textHeight() const;
00600 
00601 
00608     bool updateChart(bool refresh=true);
00609 
00610     QString testAnchor( int _x, int _y ) const;
00611 
00620     bool calc(bool delay = true);
00621 
00623     void setCalcDirtyFlag();
00625     bool calcDirtyFlag();
00626 
00636     void NotifyDepending( int col, int row, Sheet* sheet, bool isDepending );
00637 
00643     void setLayoutDirtyFlag( bool format = false );
00644     bool layoutDirtyFlag() const;
00645 
00646     void clearDisplayDirtyFlag();
00647     void setDisplayDirtyFlag();
00648 
00657     void obscure( Cell *cell, bool isForcing = false);
00663     void unobscure(Cell* cell);
00667     bool isObscured() const;
00674     bool isPartOfMerged() const;
00675 
00682     Cell *ultimateObscuringCell() const;
00683 
00687     QValueList<Cell*> obscuringCells() const;
00688 
00689     void clearObscuringCells();
00690 
00691 
00703     void mergeCells( int _col, int _row, int _x, int _y );
00704 
00708     bool doesMergeCells() const;
00709 
00714     int mergedXCells() const;
00715 
00720     int mergedYCells() const;
00721 
00725     int extraXCells() const;
00726 
00730     int extraYCells() const;
00731 
00732     double extraWidth() const;
00733     double extraHeight() const;
00734 
00744     QString encodeFormula( bool _era = false, int _col = -1, int _row = -1 ) const;
00745     QString decodeFormula( const QString &_text, int _col = -1, int _row = -1 ) const;
00746 
00756     QString pasteOperation( const QString &new_text, const QString &old_text, Paste::Operation op );
00757 
00762     bool hasError() const;
00763 
00767     void clearAllErrors();
00768 
00773     void makeLayout( QPainter &_painter, int _col, int _row );
00774 
00780     bool makeFormula();
00781 
00782 
00783     void defaultStyle();
00784 
00788     QValueList<Conditional> conditionList() const;
00789 
00793     void setConditionList(const QValueList<Conditional> &newList);
00794 
00795     Validity * getValidity( int newStruct = -1 );
00796 
00797     void removeValidity();
00798 
00803     bool testValidity() const;
00804 
00809     void calculateTextParameters( QPainter &painter, int _col, int _row );
00810 
00814     int defineAlignX();
00815 
00816 
00817 
00818 
00822     bool operator > ( const Cell & ) const;
00823     bool operator < ( const Cell & ) const;
00824 
00825     void freeAllObscuredCells();
00826 
00827     /* descriptions of the flags are just below */
00828     enum CellFlags{
00829     /* this uses the same flags variable as Format.  The least significant
00830        16 bits are reserved for the base class, and the most significant 16
00831        have been left for this subclass to use. */
00832       Flag_LayoutDirty           = 0x00010000,
00833       Flag_CalcDirty             = 0x00020000,
00834       Flag_Progress              = 0x00040000,
00835       Flag_UpdatingDeps          = 0x00080000,
00836       Flag_DisplayDirty          = 0x00100000,
00837       Flag_Merged                = 0x00200000,
00838       Flag_CellTooShortX         = 0x00400000,
00839       Flag_CellTooShortY         = 0x00800000,
00840       Flag_ParseError            = 0x01000000,
00841       Flag_CircularCalculation   = 0x02000000,
00842       Flag_DependancyError       = 0x04000000,
00843       Flag_PaintingCell          = 0x08000000, // On during painting
00844       Flag_TextFormatDirty       = 0x10000000
00845      // Flag_Highlight     = 0x20000000
00846     };
00847 
00848     void clearFlag( CellFlags flag );
00849     void setFlag( CellFlags flag );
00850     bool testFlag( CellFlags flag ) const;
00851 
00852   /* descriptions of the flags are as follows: */
00853 
00854   /*
00855    * Error
00856    * True if the cell is calculated and there was an error during calculation
00857    * In that case the cell usually displays "#####"
00858    *
00859    * LayoutDirty
00860    * Flag showing whether the current layout is OK.
00861    * If you change for example the fonts point size, set this flag. When the
00862    * cell must draw itself on the screen it will first recalculate its layout.
00863    *
00864    * CalcDirty
00865    * Shows whether recalculation is necessary.
00866    * If this cell must be recalculated for some reason, for example the user
00867    * entered a new formula, then this flag is set. If @ref #bFormula is false
00868    * nothing will happen at all.
00869    *
00870    * Progress
00871    * Tells whether this cell it currently under calculation.
00872    * If a cell thats 'progressFlag' is set is told to calculate we
00873    * have detected a circular reference and we must stop calulating.
00874    *
00875    * UpdatingDeps
00876    * Tells whether we've already calculated the reverse dependancies for this
00877    * cell.  Similar to the Progress flag but it's for when we are calculating
00878    * in the reverse direction.
00879    * @see updateDependancies()
00880    *
00881    * DisplayDirty - TODO - is this unused now??
00882    * If this flag is set, then it is known that this cell has to be updated
00883    * on the display. This means that somewhere in the calling stack there is a
00884    * function which will call @ref Sheet::updateCell once it retains
00885    * the control. If a function changes the contents/layout of this cell and this
00886    * flag is not set, then the function must set it at once. After the changes
00887    * are done the function must call <tt>m_pSheet->updateCell(...).
00888    * The flag is cleared by the function format()->sheet()->updateCell.
00889    *
00890    * Merged
00891    * Tells whether the cell is merged with other cells.  Cells may
00892    * occupy other cells space on demand. You may force a cell to
00893    * do so by setting this flag. Merging the cell with 0 in both
00894    * directions, will disable this flag!
00895    *
00896    * CellTooShortX
00897    * When it's True displays ** and/or the red triangle and when the
00898    * mouse is over it, the tooltip displays the full value
00899    * it's true when text size is bigger that cell size
00900    * and when Align is center or left
00901    *
00902    * CellTooShortY
00903    * When it's True when mouseover it, the tooltip displays the full value
00904    * it's true when text size is bigger that cell height
00905    */
00906 
00907 protected:
00911     void applyZoomedFont( QPainter &painter, int _col, int _row );
00912 
00917     void textSize( QPainter &_paint );
00918 
00923     QString textDisplaying( QPainter &painter);
00924 
00930     void clearFormula();
00931 
00940     void checkTextInput();
00941 
00946     void checkNumberFormat();
00947 
00952     void loadOasisCellText( const QDomElement& parent );
00953     void loadOasisObjects( const QDomElement& e, KoOasisLoadingContext& oasisContext );
00954     void loadOasisValidation( const QString& validationName );
00955 
00956     void loadOasisValidationCondition( QString &valExpression );
00957     void saveOasisAnnotation( KoXmlWriter &xmlwriter );
00958     void loadOasisConditional( QDomElement * style );
00959 
00960 
00961 
00962 private:
00963 
00964     class Extra;
00965     class Private;
00966     Private *d;
00967     // static const char* s_dataTypeToString[];
00968 
00969     /* helper functions to the paintCell(...) function */
00970  /*   void paintCellHighlight(QPainter& painter,
00971           const KoRect& cellRect,
00972           const QPoint& cellRef,
00973           const int highlightBorder,
00974           const QPen& rightPen,
00975           const QPen& bottomPen,
00976           const QPen& leftPen,
00977           const QPen& topPen
00978          );*/
00979 
00980 
00981     void paintCellBorders( QPainter& painter, const KoRect &rect,
00982                            const KoRect &cellRect,
00983          const QPoint &cellRef,
00984                            bool paintBorderRight, bool paintBorderBottom,
00985                            bool paintBorderLeft, bool paintBorderTop,
00986                            QPen & rightPen, QPen & bottomPen,
00987                            QPen & leftPen, QPen & topPen );
00988     void paintPageBorders( QPainter& painter, const KoRect &cellRect,
00989                            const QPoint &cellRef,
00990                            bool paintBorderRight, bool paintBorderBottom );
00991     void paintText( QPainter& painter, const KoRect &cellRect,
00992                     const QPoint &cellRef );
00993     void paintMoreTextIndicator( QPainter& painter, const KoRect &cellRect,
00994                                  QColor &backgroundColor );
00995     void paintCommentIndicator( QPainter& painter, const KoRect &cellRect,
00996                                 const QPoint &cellRef, QColor &backgroundColor );
00997     void paintFormulaIndicator( QPainter& painter, const KoRect &cellRect,
00998                                 QColor &backgroundColor );
00999     void paintDefaultBorders( QPainter& painter, const KoRect &rect,
01000                               const KoRect &cellRect, const QPoint &cellRef,
01001                               bool paintBorderRight, bool paintBorderBottom,
01002                               bool paintBorderLeft, bool paintBorderTop,
01003                               QPen const & rightPen, QPen const & bottomPen,
01004                               QPen const & leftPen, QPen const & topPen );
01005     void paintBackground( QPainter& painter, const KoRect &cellRect,
01006                           const QPoint &cellRef, bool selected,
01007                           QColor &backgroundColor );
01008     void paintObscuredCells( const KoRect& rect, QPainter& painter,
01009                              View* view, const KoRect &cellRect,
01010                              const QPoint &cellRef,
01011                              bool paintBorderRight, bool paintBorderBottom,
01012                              bool paintBorderLeft, bool paintBorderTop,
01013                              QPen & rightPen, QPen & bottomPen,
01014                              QPen & leftPen, QPen & topPen,
01015                  QValueList<QPoint> &mergedCellsPainted );
01016     void paintCellDiagonalLines( QPainter& painter, const KoRect &cellRect,
01017                                  const QPoint &cellRef );
01018 
01019 
01020 
01021 
01024   void valueChanged ();
01025 
01026   /* helper functions to the makeLayout(...) function */
01027   /* (more to come) */
01028   void setOutputText();
01029 
01030 
01031   /* helper functions to the load/save routines */
01032   bool loadCellData(const QDomElement &text, Paste::Operation op);
01033   bool saveCellResult( QDomDocument& doc, QDomElement& result,
01034                        QString str );
01035   void update();
01036   int effAlignX();
01037 
01043   void offsetAlign( int _col, int _row );
01044 
01045     void checkForNamedAreas( QString & formula ) const;
01053     QString convertFormulaToOasisFormat( const QString & formula ) const;
01054     void loadOasisValidationValue( const QStringList &listVal );
01055 
01056 };
01057 
01058 } // namespace KSpread
01059 
01060 #endif  // KSPREAD_CELL
KDE Home | KDE Accessibility Home | Description of Access Keys