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     virtual bool saveOasis( KoXmlWriter& xmlwriter , KoGenStyles &mainStyles, int row, int column, int maxCol, int &repeated, GenValidationStyles &valStyle );
00258 
00259     void saveOasisValue (KoXmlWriter &xmlWriter);
00260 
00264     QString saveOasisCellStyle( KoGenStyle &currentCellStyle,KoGenStyles &mainStyles );
00265 
00266     bool load( const QDomElement& cell, int _xshift, int _yshift, Paste::Mode pm = Paste::Normal,
00267                Paste::Operation op = Paste::OverWrite, bool paste = false );
00268 
00274     bool loadOasis( const QDomElement & element, KoOasisLoadingContext &oasisContext , Style* style);
00275 
00276     QTime toTime(const QDomElement &element);
00277     QDate toDate(const QDomElement &element);
00278 
00284     void copyFormat( const int column, const int row );
00290     void copyFormat( const Cell* cell );
00291     void copyContent( const Cell* cell );
00299     void copyAll( Cell *cell);
00300 
00301     enum BorderSides
00302     {
00303       Border_None   =0x00,
00304       Border_Left   =0x01,
00305       Border_Right  =0x02,
00306       Border_Top    =0x04,
00307       Border_Bottom =0x08,
00308       Border_SizeGrip   =0x10   //the size grip is the little square on the bottom right-hand corner of a highlighted range of cells
00309               //which the user can click and drag to resize the range and change which cells are included.
00310               //this is not used with normal borders
00311     };
00312 
00331     void paintCell( const KoRect & rect, QPainter & painter,
00332                     View * view, const KoPoint & coordinate,
00333                     const QPoint & cellRef,
00334             int paintBorder,
00335             QPen & rightPen,
00336                     QPen & bottomPen,
00337                     QPen & leftPen,
00338                     QPen & topPen,
00339             QValueList<QPoint> &mergedCellsPainted,
00340                     bool drawCursor = true );
00341 
00342 
00350     int width( int _col = -1, const Canvas *_canvas = 0L ) const;
00351 
00358     int height( int _row = -1, const Canvas *_canvas = 0L ) const;
00359 
00367     double dblWidth( int _col = -1, const Canvas *_canvas = 0L ) const;
00368 
00375     double dblHeight( int _row = -1, const Canvas *_canvas = 0L ) const;
00376 
00380     QRect cellRect();
00381 
00389     bool needsPrinting() const;
00390 
00398     void incPrecision();
00406     void decPrecision();
00407 
00412     void setCellText( const QString& _text, bool asString = false );
00413 
00419     void setDisplayText( const QString& _text );
00420 
00427     void setLink( const QString& link );
00428 
00433     QString link() const;
00434 
00436     //
00437     // Methods for querying format stuff.
00438     //
00440 
00448     const QPen & effLeftBorderPen( int col, int row ) const;
00453     const QPen & effTopBorderPen( int col, int row ) const;
00458     const QPen & effRightBorderPen( int col, int row ) const;
00463     const QPen & effBottomBorderPen( int col, int row ) const;
00470     const QPen & effGoUpDiagonalPen( int col, int row ) const;
00475     const QPen & effFallDiagonalPen( int col, int row ) const;
00476     const QColor & effTextColor( int col, int row ) const;
00477 
00483     uint effBottomBorderValue( int col, int row ) const;
00489     uint effRightBorderValue( int col, int row ) const;
00495     uint effLeftBorderValue( int col, int row ) const;
00501     uint effTopBorderValue( int col, int row ) const;
00502 
00506     const QPen& leftBorderPen( int col, int row ) const;
00507 
00511     const QPen& topBorderPen( int col, int row ) const;
00512 
00516     const QPen& rightBorderPen( int col, int row ) const;
00517 
00521     const QPen& bottomBorderPen( int col, int row ) const;
00522 
00526     const QColor& bgColor( int col, int row ) const;
00527 
00531     const QBrush& backGroundBrush( int col, int row ) const;
00532 
00534     //
00535     // Methods for setting format stuff.
00536     //
00538 
00542     void setLeftBorderPen( const QPen& p );
00543 
00547     void setTopBorderPen( const QPen& p );
00548 
00552     void setRightBorderPen( const QPen& p );
00553 
00557     void setBottomBorderPen( const QPen& p );
00558 
00560     //
00561     // Other stuff
00562     //
00564 
00571     FormatType formatType() const;
00572 
00574     bool isDate() const;
00576     bool isTime() const;
00577 
00578     void setNumber( double number );
00579 
00581     double getDouble ();
00582 
00584     void convertToDouble ();
00586     void convertToPercent ();
00588     void convertToMoney ();
00590     void convertToTime ();
00592     void convertToDate ();
00593 
00595     double textWidth() const;
00597     double textHeight() const;
00598 
00599 
00606     bool updateChart(bool refresh=true);
00607 
00608     QString testAnchor( int _x, int _y ) const;
00609 
00618     bool calc(bool delay = true);
00619 
00621     void setCalcDirtyFlag();
00623     bool calcDirtyFlag();
00624 
00634     void NotifyDepending( int col, int row, Sheet* sheet, bool isDepending );
00635 
00641     virtual void setLayoutDirtyFlag( bool format = false );
00642     bool layoutDirtyFlag() const;
00643 
00644     void clearDisplayDirtyFlag();
00645     void setDisplayDirtyFlag();
00646 
00655     void obscure( Cell *cell, bool isForcing = false);
00661     void unobscure(Cell* cell);
00665     bool isObscured() const;
00672     bool isPartOfMerged() const;
00673 
00680     Cell *ultimateObscuringCell() const;
00681 
00685     QValueList<Cell*> obscuringCells() const;
00686 
00687     void clearObscuringCells();
00688 
00689 
00701     void mergeCells( int _col, int _row, int _x, int _y );
00702 
00706     bool doesMergeCells() const;
00707 
00712     int mergedXCells() const;
00713 
00718     int mergedYCells() const;
00719 
00723     int extraXCells() const;
00724 
00728     int extraYCells() const;
00729 
00730     double extraWidth() const;
00731     double extraHeight() const;
00732 
00742     QString encodeFormula( bool _era = false, int _col = -1, int _row = -1 ) const;
00743     QString decodeFormula( const QString &_text, int _col = -1, int _row = -1 ) const;
00744 
00754     QString pasteOperation( const QString &new_text, const QString &old_text, Paste::Operation op );
00755 
00760     bool hasError() const;
00761 
00765     void clearAllErrors();
00766 
00771     void makeLayout( QPainter &_painter, int _col, int _row );
00772 
00778     bool makeFormula();
00779 
00780 
00781     void defaultStyle();
00782 
00786     QValueList<Conditional> conditionList() const;
00787 
00791     void setConditionList(const QValueList<Conditional> &newList);
00792 
00793     Validity * getValidity( int newStruct = -1 );
00794 
00795     void removeValidity();
00796 
00801     bool testValidity() const;
00802 
00807     void calculateTextParameters( QPainter &painter, int _col, int _row );
00808 
00812     int defineAlignX();
00813 
00814 
00815 
00816 
00820     bool operator > ( const Cell & ) const;
00821     bool operator < ( const Cell & ) const;
00822 
00823     void freeAllObscuredCells();
00824 
00825     /* descriptions of the flags are just below */
00826     enum CellFlags{
00827     /* this uses the same flags variable as Format.  The least significant
00828        16 bits are reserved for the base class, and the most significant 16
00829        have been left for this subclass to use. */
00830       Flag_LayoutDirty           = 0x00010000,
00831       Flag_CalcDirty             = 0x00020000,
00832       Flag_Progress              = 0x00040000,
00833       Flag_UpdatingDeps          = 0x00080000,
00834       Flag_DisplayDirty          = 0x00100000,
00835       Flag_Merged                = 0x00200000,
00836       Flag_CellTooShortX         = 0x00400000,
00837       Flag_CellTooShortY         = 0x00800000,
00838       Flag_ParseError            = 0x01000000,
00839       Flag_CircularCalculation   = 0x02000000,
00840       Flag_DependancyError       = 0x04000000,
00841       Flag_PaintingCell          = 0x08000000, // On during painting
00842       Flag_TextFormatDirty       = 0x10000000
00843      // Flag_Highlight     = 0x20000000
00844     };
00845 
00846     void clearFlag( CellFlags flag );
00847     void setFlag( CellFlags flag );
00848     bool testFlag( CellFlags flag ) const;
00849 
00850   /* descriptions of the flags are as follows: */
00851 
00852   /*
00853    * Error
00854    * True if the cell is calculated and there was an error during calculation
00855    * In that case the cell usually displays "#####"
00856    *
00857    * LayoutDirty
00858    * Flag showing whether the current layout is OK.
00859    * If you change for example the fonts point size, set this flag. When the
00860    * cell must draw itself on the screen it will first recalculate its layout.
00861    *
00862    * CalcDirty
00863    * Shows whether recalculation is necessary.
00864    * If this cell must be recalculated for some reason, for example the user
00865    * entered a new formula, then this flag is set. If @ref #bFormula is false
00866    * nothing will happen at all.
00867    *
00868    * Progress
00869    * Tells whether this cell it currently under calculation.
00870    * If a cell thats 'progressFlag' is set is told to calculate we
00871    * have detected a circular reference and we must stop calulating.
00872    *
00873    * UpdatingDeps
00874    * Tells whether we've already calculated the reverse dependancies for this
00875    * cell.  Similar to the Progress flag but it's for when we are calculating
00876    * in the reverse direction.
00877    * @see updateDependancies()
00878    *
00879    * DisplayDirty - TODO - is this unused now??
00880    * If this flag is set, then it is known that this cell has to be updated
00881    * on the display. This means that somewhere in the calling stack there is a
00882    * function which will call @ref Sheet::updateCell once it retains
00883    * the control. If a function changes the contents/layout of this cell and this
00884    * flag is not set, then the function must set it at once. After the changes
00885    * are done the function must call <tt>m_pSheet->updateCell(...).
00886    * The flag is cleared by the function format()->sheet()->updateCell.
00887    *
00888    * Merged
00889    * Tells whether the cell is merged with other cells.  Cells may
00890    * occupy other cells space on demand. You may force a cell to
00891    * do so by setting this flag. Merging the cell with 0 in both
00892    * directions, will disable this flag!
00893    *
00894    * CellTooShortX
00895    * When it's True displays ** and/or the red triangle and when the
00896    * mouse is over it, the tooltip displays the full value
00897    * it's true when text size is bigger that cell size
00898    * and when Align is center or left
00899    *
00900    * CellTooShortY
00901    * When it's True when mouseover it, the tooltip displays the full value
00902    * it's true when text size is bigger that cell height
00903    */
00904 
00905 protected:
00909     void applyZoomedFont( QPainter &painter, int _col, int _row );
00910 
00915     void textSize( QPainter &_paint );
00916 
00921     QString textDisplaying( QPainter &painter);
00922 
00928     void clearFormula();
00929 
00938     void checkTextInput();
00939 
00944     void checkNumberFormat();
00945 
00950     void loadOasisCellText( const QDomElement& parent );
00951     void loadOasisObjects( const QDomElement& e, KoOasisLoadingContext& oasisContext );
00952     void loadOasisValidation( const QString& validationName );
00953 
00954     void loadOasisValidationCondition( QString &valExpression );
00955     void saveOasisAnnotation( KoXmlWriter &xmlwriter );
00956     void loadOasisConditional( QDomElement * style );
00957 
00958 
00959 
00960 private:
00961 
00962     class Extra;
00963     class Private;
00964     Private *d;
00965     // static const char* s_dataTypeToString[];
00966 
00967     /* helper functions to the paintCell(...) function */
00968  /*   void paintCellHighlight(QPainter& painter,
00969           const KoRect& cellRect,
00970           const QPoint& cellRef,
00971           const int highlightBorder,
00972           const QPen& rightPen,
00973           const QPen& bottomPen,
00974           const QPen& leftPen,
00975           const QPen& topPen
00976          );*/
00977 
00978 
00979     void paintCellBorders( QPainter& painter, const KoRect &rect,
00980                            const KoRect &cellRect,
00981          const QPoint &cellRef,
00982                            bool paintBorderRight, bool paintBorderBottom,
00983                            bool paintBorderLeft, bool paintBorderTop,
00984                            QPen & rightPen, QPen & bottomPen,
00985                            QPen & leftPen, QPen & topPen );
00986     void paintPageBorders( QPainter& painter, const KoRect &cellRect,
00987                            const QPoint &cellRef,
00988                            bool paintBorderRight, bool paintBorderBottom );
00989     void paintText( QPainter& painter, const KoRect &cellRect,
00990                     const QPoint &cellRef );
00991     void paintMoreTextIndicator( QPainter& painter, const KoRect &cellRect,
00992                                  QColor &backgroundColor );
00993     void paintCommentIndicator( QPainter& painter, const KoRect &cellRect,
00994                                 const QPoint &cellRef, QColor &backgroundColor );
00995     void paintFormulaIndicator( QPainter& painter, const KoRect &cellRect,
00996                                 QColor &backgroundColor );
00997     void paintDefaultBorders( QPainter& painter, const KoRect &rect,
00998                               const KoRect &cellRect, const QPoint &cellRef,
00999                               bool paintBorderRight, bool paintBorderBottom,
01000                               bool paintBorderLeft, bool paintBorderTop,
01001                               QPen const & rightPen, QPen const & bottomPen,
01002                               QPen const & leftPen, QPen const & topPen );
01003     void paintBackground( QPainter& painter, const KoRect &cellRect,
01004                           const QPoint &cellRef, bool selected,
01005                           QColor &backgroundColor );
01006     void paintObscuredCells( const KoRect& rect, QPainter& painter,
01007                              View* view, const KoRect &cellRect,
01008                              const QPoint &cellRef,
01009                              bool paintBorderRight, bool paintBorderBottom,
01010                              bool paintBorderLeft, bool paintBorderTop,
01011                              QPen & rightPen, QPen & bottomPen,
01012                              QPen & leftPen, QPen & topPen,
01013                  QValueList<QPoint> &mergedCellsPainted );
01014     void paintCellDiagonalLines( QPainter& painter, const KoRect &cellRect,
01015                                  const QPoint &cellRef );
01016 
01017 
01018 
01019 
01022   void valueChanged ();
01023 
01024   /* helper functions to the makeLayout(...) function */
01025   /* (more to come) */
01026   void setOutputText();
01027 
01028 
01029   /* helper functions to the load/save routines */
01030   bool loadCellData(const QDomElement &text, Paste::Operation op);
01031   bool saveCellResult( QDomDocument& doc, QDomElement& result,
01032                        QString str );
01033   void update();
01034   int effAlignX();
01035 
01041   void offsetAlign( int _col, int _row );
01042 
01043     void checkForNamedAreas( QString & formula ) const;
01051     QString convertFormulaToOasisFormat( const QString & formula ) const;
01052     void loadOasisValidationValue( const QStringList &listVal );
01053 
01054 };
01055 
01056 } // namespace KSpread
01057 
01058 #endif  // KSPREAD_CELL
KDE Home | KDE Accessibility Home | Description of Access Keys