kspread

kspread_cell.h

00001 /* This file is part of the KDE project
00002 
00003    Copyright 2006 Stefan Nikolaus <stefan.nikolaus@kdemail.net>
00004    Copyright 2004 Tomas Mecir <mecirt@gmail.com>
00005    Copyright 1999-2002,2004 Laurent Montel <montel@kde.org>
00006    Copyright 2002,2004 Ariya Hidayat <ariya@kde.org>
00007    Copyright 2002-2003 Norbert Andres <nandres@web.de>
00008    Copyright 2003 Stefan Hetzl <shetzl@chello.at>
00009    Copyright 2001-2002 Philipp Mueller <philipp.mueller@gmx.de>
00010    Copyright 2002 Harri Porten <porten@kde.org>
00011    Copyright 2002 John Dailey <dailey@vt.edu>
00012    Copyright 1999-2001 David Faure <faure@kde.org>
00013    Copyright 2000-2001 Werner Trobin <trobin@kde.org>
00014    Copyright 2000 Simon Hausmann <hausmann@kde.org
00015    Copyright 1998-1999 Torben Weis <weis@kde.org>
00016    Copyright 1999 Michael Reiher <michael.reiher.gmx.de>
00017    Copyright 1999 Reginald Stadlbauer <reggie@kde.org>
00018 
00019    This library is free software; you can redistribute it and/or
00020    modify it under the terms of the GNU Library General Public
00021    License as published by the Free Software Foundation; either
00022    version 2 of the License, or (at your option) any later version.
00023 
00024    This library is distributed in the hope that it will be useful,
00025    but WITHOUT ANY WARRANTY; without even the implied warranty of
00026    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00027    Library General Public License for more details.
00028 
00029    You should have received a copy of the GNU Library General Public License
00030    along with this library; see the file COPYING.LIB.  If not, write to
00031    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00032  * Boston, MA 02110-1301, USA.
00033 */
00034 
00035 #ifndef KSPREAD_CELL
00036 #define KSPREAD_CELL
00037 
00038 #include <qpainter.h>
00039 #include <qptrlist.h>
00040 #include <qdatetime.h>
00041 
00042 #include "kspread_condition.h"
00043 
00044 class KLocale;
00045 class QDomElement;
00046 class QDomDocument;
00047 class KoXmlWriter;
00048 class KoGenStyles;
00049 class KoGenStyle;
00050 class KSParseNode;
00051 class KoRect;
00052 class KoPoint;
00053 class KoOasisStyles;
00054 class KoOasisLoadingContext;
00055 
00056 namespace KSpread
00057 {
00058 class Canvas;
00059 class Format;
00060 class GenValidationStyles;
00061 class Sheet;
00062 class Value;
00063 class View;
00064 class ConditionalDialog;
00065 
00066 struct Validity
00067 {
00068   Validity()
00069   {
00070       valMin = 0.0;
00071       valMax = 0.0;
00072       m_cond = Conditional::None;
00073       m_action = Action::Stop;
00074       m_restriction = Restriction::None;
00075       displayMessage = true;
00076       allowEmptyCell = false;
00077       displayValidationInformation = false;
00078   };
00079 
00080   bool operator==( const Validity& other ) const
00081   {
00082     if ( message == other.message &&
00083          title == other.title &&
00084          titleInfo == other.titleInfo &&
00085          messageInfo == other.messageInfo &&
00086          valMin == other.valMin &&
00087          valMax == other.valMax &&
00088          m_cond == other.m_cond &&
00089          m_action == other.m_action &&
00090          m_restriction == other.m_restriction &&
00091          timeMin == other.timeMin &&
00092          timeMax == other.timeMax &&
00093          dateMin == other.dateMin &&
00094          dateMax == other.dateMax &&
00095          displayMessage == other.displayMessage &&
00096          allowEmptyCell == other.allowEmptyCell &&
00097          displayValidationInformation == other.displayValidationInformation &&
00098          listValidity == other.listValidity )
00099     {
00100       return true;
00101     }
00102     return false;
00103   }
00104   inline bool operator!=( const Validity& other ) const { return !operator==( other ); }
00105 
00106     QString message;
00107     QString title;
00108     QString titleInfo;
00109     QString messageInfo;
00110     double valMin;
00111     double valMax;
00112     Conditional::Type m_cond;
00113     Action::Type m_action;
00114     Restriction::Type m_restriction;
00115     QTime  timeMin;
00116     QTime  timeMax;
00117     QDate  dateMin;
00118     QDate  dateMax;
00119     bool displayMessage;
00120     bool allowEmptyCell;
00121     bool displayValidationInformation;
00122     QStringList listValidity;
00123 };
00124 
00125 class Formula;
00126 
00137 class KSPREAD_EXPORT Cell
00138 {
00139   friend class Conditions;
00140 public:
00141 
00142   Cell (Sheet *_sheet, int _column, int _row);
00143   Cell (Sheet *_sheet, Style * _style, int _column, int _row);
00144 
00148     ~Cell();
00149 
00153     Sheet* sheet() const;
00154 
00159     bool isDefault() const;
00160 
00164     bool isEmpty() const;
00165 
00169     int column() const;
00170 
00174     int row() const;
00175 
00180     QString name() const;
00181 
00186     QString fullName() const;
00187 
00191     QString columnName() const;
00192 
00197     static QString name( int col, int row );
00198 
00203     static QString fullName( const Sheet *s, int col, int row );
00204 
00209     static QString columnName( uint column );
00210 
00214     KLocale* locale() const;
00218     bool isFormula() const;
00219 
00224     QString text() const;
00225 
00226     QString strOutText() const;
00227 
00228     Formula *formula () const;
00229 
00231     Format* format() const;
00232 
00237     const Value value() const;
00238 
00242     void setValue( const Value& value );
00243 
00258     void setCellValue (const Value& value, FormatType fmtType = No_format,
00259                        const QString& inputText = QString::null);
00260 
00261     Cell* previousCell() const;
00262     Cell* nextCell() const;
00263     void setPreviousCell( Cell* c );
00264     void setNextCell( Cell* c );
00265 
00270     void move( int column, int row );
00271 
00275     // Note:  This used to remove any links from this cell to other cells.  However, this caused a problem
00276     // in other parts of the code which relied upon walking from one cell to the next using
00277     // nextCell().
00278     void sheetDies();
00279 
00295     QDomElement save( QDomDocument& doc, int _x_offset = 0, int _y_offset = 0, bool force = false, bool copy = false, bool era = false );
00296 
00297     bool saveOasis( KoXmlWriter& xmlwriter, KoGenStyles& mainStyles,
00298                     int row, int column, int &repeated,
00299                     GenValidationStyles &valStyle );
00300 
00301     void saveOasisValue (KoXmlWriter &xmlWriter);
00302 
00306     QString saveOasisCellStyle( KoGenStyle &currentCellStyle,KoGenStyles &mainStyles );
00307 
00308     bool load( const QDomElement& cell, int _xshift, int _yshift, Paste::Mode pm = Paste::Normal,
00309                Paste::Operation op = Paste::OverWrite, bool paste = false );
00310 
00316     bool loadOasis( const QDomElement & element, KoOasisLoadingContext &oasisContext , Style* style);
00317 
00318     QTime toTime(const QDomElement &element);
00319     QDate toDate(const QDomElement &element);
00320 
00326     void copyFormat( const int column, const int row );
00332     void copyFormat( const Cell* cell );
00333     void copyContent( const Cell* cell );
00341     void copyAll( Cell *cell);
00342 
00343     enum BorderSides
00344     {
00345       Border_None   =0x00,
00346       Border_Left   =0x01,
00347       Border_Right  =0x02,
00348       Border_Top    =0x04,
00349       Border_Bottom =0x08,
00350       Border_SizeGrip   =0x10   //the size grip is the little square on the bottom right-hand corner of a highlighted range of cells
00351               //which the user can click and drag to resize the range and change which cells are included.
00352               //this is not used with normal borders
00353     };
00354 
00373     void paintCell( const KoRect & rect, QPainter & painter,
00374                     View * view, const KoPoint & coordinate,
00375                     const QPoint & cellRef,
00376             int paintBorder,
00377             QPen & rightPen,
00378                     QPen & bottomPen,
00379                     QPen & leftPen,
00380                     QPen & topPen,
00381             QValueList<QPoint> &mergedCellsPainted,
00382                     bool drawCursor = true );
00383 
00384 
00392     int width( int _col = -1, const Canvas *_canvas = 0L ) const;
00393 
00400     int height( int _row = -1, const Canvas *_canvas = 0L ) const;
00401 
00409     double dblWidth( int _col = -1, const Canvas *_canvas = 0L ) const;
00410 
00417     double dblHeight( int _row = -1, const Canvas *_canvas = 0L ) const;
00418 
00422     QRect cellRect();
00423 
00431     bool needsPrinting() const;
00432 
00440     void incPrecision();
00448     void decPrecision();
00449 
00454     void setCellText( const QString& _text, bool asString = false );
00455 
00461     void setDisplayText( const QString& _text );
00462 
00469     void setLink( const QString& link );
00470 
00475     QString link() const;
00476 
00478     //
00479     // Methods for querying format stuff.
00480     //
00482 
00490     const QPen & effLeftBorderPen( int col, int row ) const;
00495     const QPen & effTopBorderPen( int col, int row ) const;
00500     const QPen & effRightBorderPen( int col, int row ) const;
00505     const QPen & effBottomBorderPen( int col, int row ) const;
00512     const QPen & effGoUpDiagonalPen( int col, int row ) const;
00517     const QPen & effFallDiagonalPen( int col, int row ) const;
00518     const QColor & effTextColor( int col, int row ) const;
00519 
00525     uint effBottomBorderValue( int col, int row ) const;
00531     uint effRightBorderValue( int col, int row ) const;
00537     uint effLeftBorderValue( int col, int row ) const;
00543     uint effTopBorderValue( int col, int row ) const;
00544 
00548     const QPen& leftBorderPen( int col, int row ) const;
00549 
00553     const QPen& topBorderPen( int col, int row ) const;
00554 
00558     const QPen& rightBorderPen( int col, int row ) const;
00559 
00563     const QPen& bottomBorderPen( int col, int row ) const;
00564 
00568     const QColor& bgColor( int col, int row ) const;
00569 
00573     const QBrush& backGroundBrush( int col, int row ) const;
00574 
00576     //
00577     // Methods for setting format stuff.
00578     //
00580 
00584     void setLeftBorderPen( const QPen& p );
00585 
00589     void setTopBorderPen( const QPen& p );
00590 
00594     void setRightBorderPen( const QPen& p );
00595 
00599     void setBottomBorderPen( const QPen& p );
00600 
00602     //
00603     // Other stuff
00604     //
00606 
00613     FormatType formatType() const;
00614 
00616     bool isDate() const;
00618     bool isTime() const;
00619 
00620     void setNumber( double number );
00621 
00623     double getDouble ();
00624 
00626     void convertToDouble ();
00628     void convertToPercent ();
00630     void convertToMoney ();
00632     void convertToTime ();
00634     void convertToDate ();
00635 
00637     double textWidth() const;
00639     double textHeight() const;
00640 
00641 
00648     bool updateChart(bool refresh=true);
00649 
00650     QString testAnchor( int _x, int _y ) const;
00651 
00660     bool calc(bool delay = true);
00661 
00663     void setCalcDirtyFlag();
00665     bool calcDirtyFlag();
00666 
00676     void NotifyDepending( int col, int row, Sheet* sheet, bool isDepending );
00677 
00683     void setLayoutDirtyFlag( bool format = false );
00684     bool layoutDirtyFlag() const;
00685 
00686     void clearDisplayDirtyFlag();
00687     void setDisplayDirtyFlag();
00688 
00697     void obscure( Cell *cell, bool isForcing = false);
00703     void unobscure(Cell* cell);
00707     bool isObscured() const;
00714     bool isPartOfMerged() const;
00715 
00722     Cell *ultimateObscuringCell() const;
00723 
00727     QValueList<Cell*> obscuringCells() const;
00728 
00729     void clearObscuringCells();
00730 
00731 
00743     void mergeCells( int _col, int _row, int _x, int _y );
00744 
00748     bool doesMergeCells() const;
00749 
00754     int mergedXCells() const;
00755 
00760     int mergedYCells() const;
00761 
00765     int extraXCells() const;
00766 
00770     int extraYCells() const;
00771 
00772     double extraWidth() const;
00773     double extraHeight() const;
00774 
00784     QString encodeFormula( bool _era = false, int _col = -1, int _row = -1 ) const;
00785     QString decodeFormula( const QString &_text, int _col = -1, int _row = -1 ) const;
00786 
00796     QString pasteOperation( const QString &new_text, const QString &old_text, Paste::Operation op );
00797 
00802     bool hasError() const;
00803 
00807     void clearAllErrors();
00808 
00813     void makeLayout( QPainter &_painter, int _col, int _row );
00814 
00820     bool makeFormula();
00821 
00822 
00823     void defaultStyle();
00824 
00828     QValueList<Conditional> conditionList() const;
00829 
00833     void setConditionList(const QValueList<Conditional> &newList);
00834 
00835     Validity * getValidity( int newStruct = -1 );
00836 
00837     void removeValidity();
00838 
00843     bool testValidity() const;
00844 
00849     void calculateTextParameters( QPainter &painter, int _col, int _row );
00850 
00854     int defineAlignX();
00855 
00856 
00857 
00858 
00862     bool operator > ( const Cell & ) const;
00863     bool operator < ( const Cell & ) const;
00864 
00865     bool operator==( const Cell& other ) const;
00866     inline bool operator!=( const Cell& other ) const { return !operator==( other ); }
00867 
00868     void freeAllObscuredCells();
00869 
00870     /* descriptions of the flags are just below */
00871     enum CellFlags{
00872     /* this uses the same flags variable as Format.  The least significant
00873        16 bits are reserved for the base class, and the most significant 16
00874        have been left for this subclass to use. */
00875       Flag_LayoutDirty           = 0x00010000,
00876       Flag_CalcDirty             = 0x00020000,
00877       Flag_Progress              = 0x00040000,
00878       Flag_UpdatingDeps          = 0x00080000,
00879       Flag_DisplayDirty          = 0x00100000,
00880       Flag_Merged                = 0x00200000,
00881       Flag_CellTooShortX         = 0x00400000,
00882       Flag_CellTooShortY         = 0x00800000,
00883       Flag_ParseError            = 0x01000000,
00884       Flag_CircularCalculation   = 0x02000000,
00885       Flag_DependancyError       = 0x04000000,
00886       Flag_PaintingCell          = 0x08000000, // On during painting
00887       Flag_TextFormatDirty       = 0x10000000
00888      // Flag_Highlight     = 0x20000000
00889     };
00890 
00891     void clearFlag( CellFlags flag );
00892     void setFlag( CellFlags flag );
00893     bool testFlag( CellFlags flag ) const;
00894 
00895   /* descriptions of the flags are as follows: */
00896 
00897   /*
00898    * Error
00899    * True if the cell is calculated and there was an error during calculation
00900    * In that case the cell usually displays "#####"
00901    *
00902    * LayoutDirty
00903    * Flag showing whether the current layout is OK.
00904    * If you change for example the fonts point size, set this flag. When the
00905    * cell must draw itself on the screen it will first recalculate its layout.
00906    *
00907    * CalcDirty
00908    * Shows whether recalculation is necessary.
00909    * If this cell must be recalculated for some reason, for example the user
00910    * entered a new formula, then this flag is set. If @ref #bFormula is false
00911    * nothing will happen at all.
00912    *
00913    * Progress
00914    * Tells whether this cell it currently under calculation.
00915    * If a cell thats 'progressFlag' is set is told to calculate we
00916    * have detected a circular reference and we must stop calulating.
00917    *
00918    * UpdatingDeps
00919    * Tells whether we've already calculated the reverse dependancies for this
00920    * cell.  Similar to the Progress flag but it's for when we are calculating
00921    * in the reverse direction.
00922    * @see updateDependancies()
00923    *
00924    * DisplayDirty - TODO - is this unused now??
00925    * If this flag is set, then it is known that this cell has to be updated
00926    * on the display. This means that somewhere in the calling stack there is a
00927    * function which will call @ref Sheet::updateCell once it retains
00928    * the control. If a function changes the contents/layout of this cell and this
00929    * flag is not set, then the function must set it at once. After the changes
00930    * are done the function must call <tt>m_pSheet->updateCell(...).
00931    * The flag is cleared by the function format()->sheet()->updateCell.
00932    *
00933    * Merged
00934    * Tells whether the cell is merged with other cells.  Cells may
00935    * occupy other cells space on demand. You may force a cell to
00936    * do so by setting this flag. Merging the cell with 0 in both
00937    * directions, will disable this flag!
00938    *
00939    * CellTooShortX
00940    * When it's True displays ** and/or the red triangle and when the
00941    * mouse is over it, the tooltip displays the full value
00942    * it's true when text size is bigger that cell size
00943    * and when Align is center or left
00944    *
00945    * CellTooShortY
00946    * When it's True when mouseover it, the tooltip displays the full value
00947    * it's true when text size is bigger that cell height
00948    */
00949 
00950 protected:
00954     void applyZoomedFont( QPainter &painter, int _col, int _row );
00955 
00960     void textSize( QPainter &_paint );
00961 
00966     QString textDisplaying( QPainter &painter);
00967 
00973     void clearFormula();
00974 
00983     void checkTextInput();
00984 
00989     void checkNumberFormat();
00990 
00995     void loadOasisCellText( const QDomElement& parent );
00996     void loadOasisObjects( const QDomElement& e, KoOasisLoadingContext& oasisContext );
00997     void loadOasisValidation( const QString& validationName );
00998 
00999     void loadOasisValidationCondition( QString &valExpression );
01000     void saveOasisAnnotation( KoXmlWriter &xmlwriter );
01001     void loadOasisConditional( QDomElement * style );
01002 
01003 
01004 
01005 private:
01006 
01007     class Extra;
01008     class Private;
01009     Private *d;
01010     // static const char* s_dataTypeToString[];
01011 
01012     /* helper functions to the paintCell(...) function */
01013  /*   void paintCellHighlight(QPainter& painter,
01014           const KoRect& cellRect,
01015           const QPoint& cellRef,
01016           const int highlightBorder,
01017           const QPen& rightPen,
01018           const QPen& bottomPen,
01019           const QPen& leftPen,
01020           const QPen& topPen
01021          );*/
01022 
01023 
01024     void paintCellBorders( QPainter& painter, const KoRect &rect,
01025                            const KoRect &cellRect,
01026          const QPoint &cellRef,
01027                            bool paintBorderRight, bool paintBorderBottom,
01028                            bool paintBorderLeft, bool paintBorderTop,
01029                            QPen & rightPen, QPen & bottomPen,
01030                            QPen & leftPen, QPen & topPen );
01031     void paintPageBorders( QPainter& painter, const KoRect &cellRect,
01032                            const QPoint &cellRef,
01033                            bool paintBorderRight, bool paintBorderBottom );
01034     void paintText( QPainter& painter, const KoRect &cellRect,
01035                     const QPoint &cellRef );
01036     void paintMoreTextIndicator( QPainter& painter, const KoRect &cellRect,
01037                                  QColor &backgroundColor );
01038     void paintCommentIndicator( QPainter& painter, const KoRect &cellRect,
01039                                 const QPoint &cellRef, QColor &backgroundColor );
01040     void paintFormulaIndicator( QPainter& painter, const KoRect &cellRect,
01041                                 QColor &backgroundColor );
01042     void paintDefaultBorders( QPainter& painter, const KoRect &rect,
01043                               const KoRect &cellRect, const QPoint &cellRef,
01044                               bool paintBorderRight, bool paintBorderBottom,
01045                               bool paintBorderLeft, bool paintBorderTop,
01046                               QPen const & rightPen, QPen const & bottomPen,
01047                               QPen const & leftPen, QPen const & topPen );
01048     void paintBackground( QPainter& painter, const KoRect &cellRect,
01049                           const QPoint &cellRef, bool selected,
01050                           QColor &backgroundColor );
01051     void paintObscuredCells( const KoRect& rect, QPainter& painter,
01052                              View* view, const KoRect &cellRect,
01053                              const QPoint &cellRef,
01054                              bool paintBorderRight, bool paintBorderBottom,
01055                              bool paintBorderLeft, bool paintBorderTop,
01056                              QPen & rightPen, QPen & bottomPen,
01057                              QPen & leftPen, QPen & topPen,
01058                  QValueList<QPoint> &mergedCellsPainted );
01059     void paintCellDiagonalLines( QPainter& painter, const KoRect &cellRect,
01060                                  const QPoint &cellRef );
01061 
01062 
01063 
01064 
01067   void valueChanged ();
01068 
01069   /* helper functions to the makeLayout(...) function */
01070   /* (more to come) */
01071   void setOutputText();
01072 
01073 
01074   /* helper functions to the load/save routines */
01075   bool loadCellData(const QDomElement &text, Paste::Operation op);
01076   bool saveCellResult( QDomDocument& doc, QDomElement& result,
01077                        QString str );
01078   void update();
01079   int effAlignX();
01080 
01086   void offsetAlign( int _col, int _row );
01087 
01088     void checkForNamedAreas( QString & formula ) const;
01096     QString convertFormulaToOasisFormat( const QString & formula ) const;
01097     void loadOasisValidationValue( const QStringList &listVal );
01098 
01099 };
01100 
01101 } // namespace KSpread
01102 
01103 #endif  // KSPREAD_CELL
KDE Home | KDE Accessibility Home | Description of Access Keys