MWAWCell.hxx
Go to the documentation of this file.
00001 /* -*- Mode: C++; c-default-style: "k&r"; indent-tabs-mode: nil; tab-width: 2; c-basic-offset: 2 -*- */
00002 
00003 /* libmwaw
00004 * Version: MPL 2.0 / LGPLv2+
00005 *
00006 * The contents of this file are subject to the Mozilla Public License Version
00007 * 2.0 (the "License"); you may not use this file except in compliance with
00008 * the License or as specified alternatively below. You may obtain a copy of
00009 * the License at http://www.mozilla.org/MPL/
00010 *
00011 * Software distributed under the License is distributed on an "AS IS" basis,
00012 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013 * for the specific language governing rights and limitations under the
00014 * License.
00015 *
00016 * Major Contributor(s):
00017 * Copyright (C) 2002 William Lachance (wrlach@gmail.com)
00018 * Copyright (C) 2002,2004 Marc Maurer (uwog@uwog.net)
00019 * Copyright (C) 2004-2006 Fridrich Strba (fridrich.strba@bluewin.ch)
00020 * Copyright (C) 2006, 2007 Andrew Ziem
00021 * Copyright (C) 2011, 2012 Alonso Laurent (alonso@loria.fr)
00022 *
00023 *
00024 * All Rights Reserved.
00025 *
00026 * For minor contributions see the git repository.
00027 *
00028 * Alternatively, the contents of this file may be used under the terms of
00029 * the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"),
00030 * in which case the provisions of the LGPLv2+ are applicable
00031 * instead of those above.
00032 */
00033 
00038 #ifndef MWAW_CELL_H
00039 #  define MWAW_CELL_H
00040 
00041 #include <string>
00042 #include <vector>
00043 
00044 #include "libmwaw_internal.hxx"
00045 
00046 #include "MWAWEntry.hxx"
00047 #include "MWAWFont.hxx"
00048 
00049 class MWAWTable;
00050 
00052 class MWAWCell
00053 {
00054 public:
00056   enum FormatType { F_TEXT, F_BOOLEAN, F_NUMBER, F_DATE, F_TIME, F_UNKNOWN };
00058   enum NumberType { F_NUMBER_CURRENCY, F_NUMBER_DECIMAL, F_NUMBER_FRACTION, F_NUMBER_GENERIC, F_NUMBER_SCIENTIFIC, F_NUMBER_PERCENT, F_NUMBER_UNKNOWN };
00060   struct Format {
00062     Format() : m_format(F_UNKNOWN), m_numberFormat(F_NUMBER_UNKNOWN), m_digits(-1), m_integerDigits(-1), m_numeratorDigits(-1), m_denominatorDigits(-1),
00063       m_thousandHasSeparator(false), m_parenthesesForNegative(false), m_currencySymbol("$"), m_DTFormat("")
00064     {
00065     }
00067     virtual ~Format() {}
00069     bool hasBasicFormat() const
00070     {
00071       return m_format==F_TEXT || m_format==F_UNKNOWN;
00072     }
00074     std::string getValueType() const;
00076     bool getNumberingProperties(librevenge::RVNGPropertyList &propList) const;
00078     static bool convertDTFormat(std::string const &dtFormat, librevenge::RVNGPropertyListVector &propListVector);
00080     friend std::ostream &operator<<(std::ostream &o, Format const &format);
00082     int compare(Format const &format) const;
00083 
00085     FormatType m_format;
00087     NumberType m_numberFormat;
00089     int m_digits;
00091     int m_integerDigits;
00093     int m_numeratorDigits;
00095     int m_denominatorDigits;
00097     bool m_thousandHasSeparator;
00099     bool m_parenthesesForNegative;
00101     std::string m_currencySymbol;
00103     std::string m_DTFormat;
00104   };
00106   struct CompareFormat {
00108     CompareFormat() {}
00110     bool operator()(Format const &c1, Format const &c2) const
00111     {
00112       return c1.compare(c2) < 0;
00113     }
00114   };
00118   enum HorizontalAlignment { HALIGN_LEFT, HALIGN_RIGHT, HALIGN_CENTER,
00119                              HALIGN_FULL, HALIGN_DEFAULT
00120                            };
00121 
00124   enum VerticalAlignment { VALIGN_TOP, VALIGN_CENTER, VALIGN_BOTTOM, VALIGN_DEFAULT };
00125 
00127   enum ExtraLine { E_None, E_Line1, E_Line2, E_Cross };
00128 
00130   MWAWCell() : m_position(0,0), m_numberCellSpanned(1,1), m_bdBox(),  m_bdSize(),
00131     m_format(), m_font(3,12), m_fontSet(false), m_hAlign(HALIGN_DEFAULT), m_vAlign(VALIGN_DEFAULT),
00132     m_backgroundColor(MWAWColor::white()), m_protected(false),
00133     m_bordersList(), m_extraLine(E_None), m_extraLineType() { }
00134 
00136   virtual ~MWAWCell() {}
00137 
00139   void addTo(librevenge::RVNGPropertyList &propList, shared_ptr<MWAWFontConverter> fontConverter) const;
00140 
00142   friend std::ostream &operator<<(std::ostream &o, MWAWCell const &cell);
00143 
00144   // interface with MWAWTable:
00145 
00150   virtual bool send(MWAWListenerPtr listener, MWAWTable &table);
00155   virtual bool sendContent(MWAWListenerPtr listener, MWAWTable &table);
00156 
00157   // position
00158 
00160   MWAWVec2i const &position() const
00161   {
00162     return m_position;
00163   }
00165   void setPosition(MWAWVec2i posi)
00166   {
00167     m_position = posi;
00168   }
00169 
00171   MWAWVec2i const &numSpannedCells() const
00172   {
00173     return m_numberCellSpanned;
00174   }
00176   void setNumSpannedCells(MWAWVec2i numSpanned)
00177   {
00178     m_numberCellSpanned=numSpanned;
00179   }
00180 
00182   MWAWBox2f const &bdBox() const
00183   {
00184     return m_bdBox;
00185   }
00187   void setBdBox(MWAWBox2f box)
00188   {
00189     m_bdBox = box;
00190   }
00191 
00193   MWAWVec2f const &bdSize() const
00194   {
00195     return m_bdSize;
00196   }
00198   void setBdSize(MWAWVec2f sz)
00199   {
00200     m_bdSize = sz;
00201   }
00202 
00204   static std::string getCellName(MWAWVec2i const &pos, MWAWVec2b const &absolute);
00205 
00207   static std::string getColumnName(int col);
00208 
00209   // format
00210 
00212   Format const &getFormat() const
00213   {
00214     return m_format;
00215   }
00217   void setFormat(Format const &format)
00218   {
00219     m_format=format;
00220   }
00221 
00223   bool isFontSet() const
00224   {
00225     return m_fontSet;
00226   }
00228   MWAWFont getFont() const
00229   {
00230     return m_font;
00231   }
00233   void setFont(MWAWFont const &font, bool isDefault=false)
00234   {
00235     m_font=font;
00236     m_fontSet=!isDefault;
00237   }
00238 
00240   bool isProtected() const
00241   {
00242     return m_protected;
00243   }
00245   void setProtected(bool fl)
00246   {
00247     m_protected = fl;
00248   }
00249 
00251   HorizontalAlignment hAlignment() const
00252   {
00253     return m_hAlign;
00254   }
00256   void setHAlignment(HorizontalAlignment align)
00257   {
00258     m_hAlign = align;
00259   }
00260 
00262   VerticalAlignment vAlignment() const
00263   {
00264     return m_vAlign;
00265   }
00267   void setVAlignment(VerticalAlignment align)
00268   {
00269     m_vAlign = align;
00270   }
00271 
00273   bool hasBorders() const
00274   {
00275     return m_bordersList.size() != 0;
00276   }
00278   std::vector<MWAWBorder> const &borders() const
00279   {
00280     return m_bordersList;
00281   }
00282 
00284   void resetBorders()
00285   {
00286     m_bordersList.resize(0);
00287   }
00289   void setBorders(int wh, MWAWBorder const &border);
00290 
00292   MWAWColor backgroundColor() const
00293   {
00294     return m_backgroundColor;
00295   }
00297   void setBackgroundColor(MWAWColor color)
00298   {
00299     m_backgroundColor = color;
00300   }
00302   bool hasExtraLine() const
00303   {
00304     return m_extraLine!=E_None && !m_extraLineType.isEmpty();
00305   }
00307   ExtraLine extraLine() const
00308   {
00309     return m_extraLine;
00310   }
00312   MWAWBorder const &extraLineType() const
00313   {
00314     return m_extraLineType;
00315   }
00317   void setExtraLine(ExtraLine extrLine, MWAWBorder const &type=MWAWBorder())
00318   {
00319     m_extraLine = extrLine;
00320     m_extraLineType=type;
00321   }
00322 protected:
00324   MWAWVec2i m_position;
00326   MWAWVec2i m_numberCellSpanned;
00328   MWAWBox2f m_bdBox;
00330   MWAWVec2f m_bdSize;
00331 
00333   Format m_format;
00335   MWAWFont m_font;
00337   bool m_fontSet;
00339   HorizontalAlignment m_hAlign;
00341   VerticalAlignment m_vAlign;
00343   MWAWColor m_backgroundColor;
00345   bool m_protected;
00346 
00348   std::vector<MWAWBorder> m_bordersList;
00350   ExtraLine m_extraLine;
00352   MWAWBorder m_extraLineType;
00353 };
00354 
00356 class MWAWCellContent
00357 {
00358 public:
00360   struct FormulaInstruction {
00361     enum Type { F_Operator, F_Function, F_Cell, F_CellList, F_Long, F_Double, F_Text };
00363     FormulaInstruction() : m_type(F_Text), m_content(""), m_longValue(0), m_doubleValue(0), m_sheet("")
00364     {
00365       for (int i=0; i<2; ++i) {
00366         m_position[i]=MWAWVec2i(0,0);
00367         m_positionRelative[i]=MWAWVec2b(false,false);
00368       }
00369     }
00371     librevenge::RVNGPropertyList getPropertyList(MWAWFontConverter &fontConverter, int fontId) const;
00373     friend std::ostream &operator<<(std::ostream &o, FormulaInstruction const &inst);
00375     Type m_type;
00377     std::string m_content;
00379     double m_longValue;
00381     double m_doubleValue;
00383     MWAWVec2i m_position[2];
00385     MWAWVec2b m_positionRelative[2];
00387     std::string m_sheet;
00388   };
00389 
00391   enum Type { C_NONE, C_TEXT, C_NUMBER, C_FORMULA, C_UNKNOWN };
00393   MWAWCellContent() : m_contentType(C_UNKNOWN), m_value(0.0), m_valueSet(false), m_textEntry(), m_formula() { }
00395   ~MWAWCellContent() {}
00397   friend std::ostream &operator<<(std::ostream &o, MWAWCellContent const &cell);
00398 
00400   bool empty() const
00401   {
00402     if (m_contentType == C_NUMBER) return false;
00403     if (m_contentType == C_TEXT && m_textEntry.valid()) return false;
00404     if (m_contentType == C_FORMULA && (m_formula.size() || isValueSet())) return false;
00405     return true;
00406   }
00408   void setValue(double value)
00409   {
00410     m_value = value;
00411     m_valueSet = true;
00412   }
00414   bool isValueSet() const
00415   {
00416     return m_valueSet;
00417   }
00419   bool hasText() const
00420   {
00421     return m_textEntry.valid();
00422   }
00425   static bool double2Date(double val, int &Y, int &M, int &D);
00427   static bool double2Time(double val, int &H, int &M, int &S);
00429   static bool double2String(double val, MWAWCell::Format const &format, std::string &str);
00431   static bool date2Double(int Y, int M, int D, double &val);
00433   Type m_contentType;
00435   double m_value;
00437   bool m_valueSet;
00439   MWAWEntry m_textEntry;
00441   std::vector<FormulaInstruction> m_formula;
00442 };
00443 
00444 #endif
00445 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: