[prev in list] [next in list] [prev in thread] [next in thread] 

List:       koffice-devel
Subject:    [kspread] - Internal Layout (was: Cell simplification)
From:       Norbert Andres <nandres () web ! de>
Date:       2002-12-13 20:37:03
[Download RAW message or body]

On Friday 13 December 2002 16:08, Ariya Hidayat wrote:
> Sharing the class can be achived using private reference-counted object,
> which is shared when doing copy or assignment. KSpreadValue is an example
> of this. Also, the QDom* classes show the concept quite clearly.
Hmm, but in the end you share just the default value, right?

OpenCalc does it the following way:
it has an object ("KSpreadStyleManager") that acts like a registration for all 
the styles, e.g. numeric formats, borders,...

If you change the background it checks, if a style with the same attribute as 
the current one already exists in the StyleManager. If it does it assigns a 
reference to the style otherwise a new style gets created in the StyleManager 
and a reference is stored in the cell.

The great advantage ist: if you create a new cells (or thousands of it), you 
don't need to create any internal objects (like Fonts, Pens,...), just a few 
Pointers.

The disadvantage is, looking up if a style exists is slower if many styles of 
that kind exist (but normally you don't get more than 4 or 5. Most cells use 
a default style.

I started to implement this for KSpread, but I didn't succed because I had 
problems understanding the fallback layouts and stuff like that. They should 
go into the StyleManager as well but in a different category.

See the pseudo code examples below.
Here I called a style a set of options belonging together. A MetaStyle is a 
set of styles, so that you can define CellStyles, like Default, Header1, 
Header2 (see OpenCalc, Excel). Internal created styles are "AutoStyles" 
(autmatically created). I have some more code examples for this...

StyleManager:
 
public: 
 
save(..) 
load(..) 
 
registerBackgroundStyle(BackgroundStyle *) 
registerBorderStyle() 
registerFormatStyle() 
registerFontStyle() 
registerPositionStyle() 
 
registerMetaStyle(MetaStyle *) 
deregisterMetaStyle(MetaStyle *) 
 
deregister(BackgroundStyle *) 
deregister(...) 
 
KSpreadMetaStyle       * getMetaStyle( QString const & id ); 
KSpreadMetaStyle       * getDefaultStyle() const { return 
m_metaStyles.first(); } 
 
BackgroundStyle * getBackgroundStyle( QString const & id ); 
... 
 
private: 
BackgroundStyleList 
... 
MetaStyleList 
 
createDefaultStyle(); 
createHeader1Style(); 
...


Style:

Style 
 
  KSpreadStyle( KSpreadStyleManager * manager, bool dynamic ); 
  virtual ~KSpreadStyle(); 
 
  virtual bool load( QDomElement const & styles ) = 0; 
  virtual QDomElement save( QDomDocument & doc, QString const & name )  = 0; 
 
  void setModifyFlag(); 
  void removeModifyFlag(); 
 
  void setUserStyle(); 
  bool isUser() const { return ( m_state & sUser ); } 
   
  bool getsModified() const { return ( (m_state & sModify) && !(m_state & 
sDynamic) ); } 
 
  QString const & getName() const { return m_name; } 
   
  void addRef() { ++m_count; } 
  void delRef() { --m_count; } 
 
  KSpreadStyleManager * manager() const { return m_manager; } 
 
 protected: 
  int                   m_count; 
  uint                  m_state; 
  KSpreadStyleManager * m_manager; 
  QString               m_name; 




class KSpreadBackgroundStyle : public KSpreadStyle 
{ 
 public: 
  KSpreadBackgroundStyle( KSpreadStyleManager * manager, bool dynamic = true 
); 
  KSpreadBackgroundStyle( KSpreadStyleManager * manager, QColor const & color,  
			     QBrush const & brush, bool dynamic = true ); 
  KSpreadBackgroundStyle( KSpreadBackgroundStyle const & style ); 
  ~KSpreadBackgroundStyle(); 
 
  KSpreadBackgroundStyle * setBackgroundColor( QColor const & color ); 
  KSpreadBackgroundStyle * setBackgroundBrush( QBrush const & brush ); 
 
  KSpreadBackgroundStyle * setBackgroundBrushStyle( Qt::BrushStyle s ); 
  KSpreadBackgroundStyle * setBackgroundBrushColor( QColor const & color ); 
 
  QColor const & backgroundColor() const       { return m_bgColor;         } 
  QBrush const & backgroundBrush() const       { return m_bgBrush;         } 
  Qt::BrushStyle backgroundBrushStyle( ) const { return m_bgBrush.style(); } 
  QColor const & backgroundBrushColor( ) const { return m_bgBrush.color(); } 
 
  bool load( QDomElement const & backgroundStyle ); 
  QDomElement save( QDomDocument & doc, QString const & name ); 
 
  bool operator==( KSpreadBackgroundStyle const & style ) const; 
 
 private: 
  QColor m_bgColor; 
  QBrush m_bgBrush; 
}; 
 
 
... similar things for the other Styles.




MetaStyle 
 
  KSpreadMetaStyle( KSpreadStyleManager * manager, 
                    KSpreadBackgroundStyle * backStyle, 
                    KSpreadBorderStyle * borderStyle, 
                    KSpreadFontStyle * fontStyle, 
                    KSpreadFormatStyle * formatStyle, 
                    KSpreadPositionStyle * positionStyle ); 
 
  ~KSpreadMetaStyle(); 
 
  KSpreadBackgroundStyle * backgroundStyle() const { return m_backStyle;     } 
  KSpreadBorderStyle     * borderStyle() const     { return m_borderStyle;   } 
  KSpreadFontStyle       * fontStyle() const       { return m_fontStyle;     } 
  KSpreadFormatStyle     * formatStyle() const     { return m_formatStyle;   } 
  KSpreadPositionStyle   * positionStyle() const   { return m_positionStyle; } 
 
  bool load( QDomElement const & metaStyle ); 
  QDomElement save( QDomDocument & doc, QString const & name ); 
 
  void registerCell( KSpreadCell * cell ); 
  void deregisterCell( KSpreadCell * cell ); 
 
  void update() const; // updates all registered cells after the meta style 
got changed 
 
  void clean(); // prepares for removal 
 
  QString getName()        const { return m_name; } // ID used for storing on 
disk 
  QString getDisplayName() const { return m_displayName; } //visible in Dialog 
 
  void setDisplayName( QString const & name ) { m_displayName = name; } 
 
 private: 
  QString                  m_displayName; // name displayed in dialog 
  QString                  m_name;       // name saved in file as ID 
  QPtrList<KSpreadCell>    m_cellList;  // cells using this style 



resulting XML code:
 
<cell metaStyle="ms1"> 
 	<text>mytext</text> 
</cell> 
 
<cell bgStyle="bg1" fontStyle="fs5">         // the rest takes default 
         <text>my text</text>			    // style values 
</cell> 
 
 
 
 
<styles> 
  <metaStyles> 
     <style name="ms1"> 
         <backgroundStyle bgColor="..." bgBrush="..."/> 
         <fontStyle...> 
     </style> 
   </metaStyles> 
   <autoStyles> 
      <bgStyle name="bg1" bgColor="..."/> 
      <bgStyle name="bg2" bgBrush="..."/> 
      <borderStyle name="bo1".../> 
   </autoStyles>
> _______________________________________________
> koffice-devel mailing list
> koffice-devel@mail.kde.org
> http://mail.kde.org/mailman/listinfo/koffice-devel

_______________________________________________
koffice-devel mailing list
koffice-devel@mail.kde.org
http://mail.kde.org/mailman/listinfo/koffice-devel
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic