register.h

Go to the documentation of this file.
00001 /***************************************************************************
00002                              register.h
00003                              ----------
00004     begin                : Fri Mar 10 2006
00005     copyright            : (C) 2006 by Thomas Baumgart
00006     email                : Thomas Baumgart <ipwizard@users.sourceforge.net>
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 #ifndef REGISTER_H
00019 #define REGISTER_H
00020 
00021 // Some STL headers in GCC4.3 contain operator new. Memory checker mangles these
00022 #ifdef _CHECK_MEMORY
00023   #undef new
00024 #endif
00025 
00026 #include <algorithm>
00027 
00028 // ----------------------------------------------------------------------------
00029 // QT Includes
00030 
00031 #include <qtable.h>
00032 #include <qvaluelist.h>
00033 #include <qvaluevector.h>
00034 #include <qwidgetlist.h>
00035 #include <qmap.h>
00036 #include <qpair.h>
00037 #include <qevent.h>
00038 
00039 // ----------------------------------------------------------------------------
00040 // KDE Includes
00041 
00042 // ----------------------------------------------------------------------------
00043 // Project Includes
00044 #ifdef _CHECK_MEMORY
00045   #include <kmymoney/mymoneyutils.h>
00046 #endif
00047 
00048 #include <kmymoney/mymoneyaccount.h>
00049 #include <kmymoney/registeritem.h>
00050 #include <kmymoney/transaction.h>
00051 #include <kmymoney/transactioneditorcontainer.h>
00052 #include <kmymoney/selectedtransaction.h>
00053 #include <kmymoney/transactionsortoption.h>
00054 
00055 class RegisterToolTip;
00056 
00057 namespace KMyMoneyRegister {
00058 
00059 typedef enum {
00060   UnknownSort = 0,      //< unknown sort criteria
00061   PostDateSort = 1,     //< sort by post date
00062   EntryDateSort,        //< sort by entry date
00063   PayeeSort,            //< sort by payee name
00064   ValueSort,            //< sort by value
00065   NoSort,               //< sort by number field
00066   EntryOrderSort,       //< sort by entry order
00067   TypeSort,             //< sort by CashFlowDirection
00068   CategorySort,         //< sort by Category
00069   ReconcileStateSort,   //< sort by reconciliation state
00070   SecuritySort,         //< sort by security (only useful for investment accounts)
00071   // insert new values in front of this line
00072   MaxSortFields
00073 } TransactionSortField;
00074 
00075 typedef enum {
00076   Ascending = 0,        //< sort in ascending order
00077   Descending            //< sort in descending order
00078 } SortDirection;
00079 
00080 class Register;
00081 class RegisterItem;
00082 class ItemPtrVector;
00083 
00084 const QString sortOrderToText(TransactionSortField idx);
00085 TransactionSortField textToSortOrder(const QString& text);
00086 
00087 
00088 class QWidgetContainer : public QMap<QString, QWidget*>
00089 {
00090 public:
00091   QWidgetContainer() {}
00092 
00093   QWidget* haveWidget(const QString& name) const {
00094     QWidgetContainer::const_iterator it_w;
00095     it_w = find(name);
00096     if(it_w != end())
00097       return *it_w;
00098     return 0;
00099   }
00100 
00101   void removeOrphans(void) {
00102     QWidgetContainer::iterator it_w;
00103     for(it_w = begin(); it_w != end(); ) {
00104       if((*it_w) && (*it_w)->parent())
00105         ++it_w;
00106       else {
00107         delete (*it_w);
00108         remove(it_w);
00109         it_w = begin();
00110       }
00111     }
00112   }
00113 
00114 };
00115 
00116 class GroupMarker : public RegisterItem
00117 {
00118 public:
00119   GroupMarker(Register* parent, const QString& txt = QString());
00120   ~GroupMarker();
00121   void setText(const QString& txt) { m_txt = txt; }
00122   const QString& text(void) const { return m_txt; }
00123   bool isSelectable(void) const { return false; }
00124   bool canHaveFocus(void) const { return false; }
00125   int numRows(void) const { return 1; }
00126 
00127   virtual const char* className(void) { return "GroupMarker"; }
00128 
00129   bool isErronous(void) const { return false; }
00130 
00131   void paintRegisterCell(QPainter* painter, int row, int col, const QRect& r, bool selected, const QColorGroup& cg);
00132   void paintFormCell(QPainter* /* painter */, int /* row */, int /* col */, const QRect& /* r */, bool /* selected */, const QColorGroup& /* cg */) {}
00133 
00134   int rowHeightHint(void) const;
00135 
00136   bool matches(const QString&) const { return true; }
00137   virtual int sortSamePostDate(void) const { return 0; }
00138 
00139 protected:
00140   void setupColors(QColorGroup& cg);
00141 
00142 protected:
00143   QString                  m_txt;
00144   unsigned int             m_drawCounter;
00145   bool                     m_showDate;
00146 
00147   static QPixmap*          m_bg;
00148   static int               m_bgRefCnt;
00149 };
00150 
00151 
00152 class FancyDateGroupMarker : public GroupMarker
00153 {
00154 public:
00155   FancyDateGroupMarker(Register* parent, const QDate& date, const QString& txt);
00156 
00157   virtual const QDate& sortPostDate(void) const { return m_date; }
00158   virtual const QDate& sortEntryDate(void) const { return m_date; }
00159   virtual const char* className(void) { return "FancyDateGroupMarker"; }
00160 private:
00161   QDate                    m_date;
00162 };
00163 
00164 class StatementGroupMarker : public FancyDateGroupMarker
00165 {
00166 public:
00167   StatementGroupMarker(Register* parent, CashFlowDirection dir, const QDate& date, const QString& txt );
00168   CashFlowDirection sortType(void) const { return m_dir; }
00169   virtual int sortSamePostDate(void) const { return 3; }
00170 private:
00171   CashFlowDirection        m_dir;
00172 };
00173 
00174 class SimpleDateGroupMarker : public FancyDateGroupMarker
00175 {
00176 public:
00177   SimpleDateGroupMarker(Register* parent, const QDate& date, const QString& txt);
00178   void paintRegisterCell(QPainter* painter, int row, int col, const QRect& r, bool selected, const QColorGroup& cg);
00179   int rowHeightHint(void) const;
00180   virtual const char* className(void) { return "SimpleDateGroupMarker"; }
00181 };
00182 
00183 class TypeGroupMarker : public GroupMarker
00184 {
00185 public:
00186   TypeGroupMarker(Register* parent, CashFlowDirection dir, MyMoneyAccount::accountTypeE accType);
00187   CashFlowDirection sortType(void) const { return m_dir; }
00188 private:
00189   CashFlowDirection        m_dir;
00190 };
00191 
00192 class FiscalYearGroupMarker : public FancyDateGroupMarker
00193 {
00194 public:
00195   FiscalYearGroupMarker(Register* parent, const QDate& date, const QString& txt);
00196   virtual const char* className(void) { return "FiscalYearGroupMarker"; }
00197   virtual int sortSamePostDate(void) const { return 1; }
00198 
00199 protected:
00200   void setupColors(QColorGroup& cg);
00201 };
00202 
00203 class PayeeGroupMarker : public GroupMarker
00204 {
00205 public:
00206   PayeeGroupMarker(Register* parent, const QString& name);
00207   const QString& sortPayee(void) const { return m_txt; }
00208 };
00209 
00210 class CategoryGroupMarker : public GroupMarker
00211 {
00212 public:
00213   CategoryGroupMarker(Register* parent, const QString& category);
00214   const QString& sortCategory(void) const { return m_txt; }
00215   const QString& sortSecurity(void) const { return m_txt; }
00216 
00217   virtual const char* className(void) { return "CategoryGroupMarker"; }
00218 };
00219 
00220 class ReconcileGroupMarker : public GroupMarker
00221 {
00222 public:
00223   ReconcileGroupMarker(Register* parent, MyMoneySplit::reconcileFlagE state);
00224   virtual MyMoneySplit::reconcileFlagE sortReconcileState(void) const { return m_state; }
00225 private:
00226   MyMoneySplit::reconcileFlagE  m_state;
00227 };
00228 
00229 
00230 class ItemPtrVector : public QValueVector<RegisterItem *>
00231 {
00232 public:
00233   ItemPtrVector() {}
00234 
00235   void sort(void);
00236 
00237 protected:
00241   static bool item_cmp(RegisterItem* i1, RegisterItem* i2);
00242 };
00243 
00244 
00245 class Register : public TransactionEditorContainer
00246 {
00247   Q_OBJECT
00248 
00249   // friend class QHeader;
00250   // friend class QTableHeader;
00251   // friend class RegisterItem;
00252   friend class Transaction;
00253   friend class StdTransaction;
00254   friend class InvestTransaction;
00255 
00256 public:
00257   Register(QWidget *parent = 0, const char *name = 0);
00258   virtual ~Register();
00259 
00263   void addItem(RegisterItem* p);
00264 
00268   void insertItemAfter(RegisterItem* p, RegisterItem* q);
00269 
00273   void removeItem(RegisterItem* p);
00274 
00281   QValueList<RegisterItem*> selectedItems(void) const;
00282 
00290   void selectedTransactions(SelectedTransactions&  list) const;
00291 
00292   QString text(int row, int col) const;
00293   QWidget* createEditor(int row, int col, bool initFromCell) const;
00294   void setCellContentFromEditor(int row, int col);
00295   QWidget* cellWidget(int row, int col) const;
00296   void endEdit(int row, int col, bool accept, bool replace);
00297   void paintCell(QPainter* painter, int row, int col, const QRect& r, bool selected, const QColorGroup& cg);
00298 
00299   void resizeData(int) {}
00300   QTableItem* item(int, int) { return 0; }
00301   void setItem(int, int, QTableItem*) {}
00302   void clearCell(int, int) {}
00303   void clearCellWidget(int, int);
00304 
00308   void paintFocus(QPainter*, const QRect& ) {}
00309 
00313   void updateCell(int /* row */, int /* col */) {}
00314 
00315   RegisterItem* focusItem(void) const { return m_focusItem; }
00316   RegisterItem* anchorItem(void) const { return m_selectAnchor; }
00317 
00322   bool setFocusItem(RegisterItem* focusItem);
00323 
00324   void setAnchorItem(RegisterItem* anchorItem);
00325 
00330   bool setFocusToTop(void);
00331 
00338   void selectItem(RegisterItem* item, bool dontChangeSelections = false);
00339 
00344   void clear(void);
00345 
00346   void updateRegister(bool forceUpdateRowHeight = false);
00347 
00351   void updateAlternate(void) const;
00352 
00357   void suppressAdjacentMarkers(void);
00358 
00362   void adjustColumn(int col);
00363 
00371   void setupRegister(const MyMoneyAccount& account, bool showAccountColumn = false);
00372 
00377   void setupRegister(const MyMoneyAccount& account, const QValueList<Column>& cols);
00378 
00379   void setSortOrder(const QString& order);
00380   const QValueList<TransactionSortField>& sortOrder(void) const { return m_sortOrder; }
00381   TransactionSortField primarySortKey(void) const;
00382   void sortItems(void);
00383 
00390   Column lastCol(void) const { return m_lastCol; }
00391 
00392   RegisterItem* firstItem(void) const;
00393   RegisterItem* firstVisibleItem(void) const;
00394   RegisterItem* nextItem(RegisterItem*) const;
00395   RegisterItem* lastItem(void) const;
00396   RegisterItem* lastVisibleItem(void) const;
00397   RegisterItem* prevItem(RegisterItem*) const;
00398   RegisterItem* itemAtRow(int row) const;
00399 
00400   void resize(int col);
00401 
00402   void forceUpdateLists(void) { m_listsDirty = true; }
00403 
00404   void ensureItemVisible(RegisterItem* item);
00405 
00406   void arrangeEditWidgets(QMap<QString, QWidget*>& editWidgets, Transaction* t);
00407   void removeEditWidgets(QMap<QString, QWidget*>& editWidgets);
00408   void tabOrder(QWidgetList& tabOrderWidgets, KMyMoneyRegister::Transaction* t) const;
00409 
00410   int rowHeightHint(void) const;
00411 
00412   void clearSelection(void);
00413 
00414   bool markErronousTransactions(void) const { return (m_markErronousTransactions & 0x01) != 0; }
00415 
00427   static Transaction* transactionFactory(Register *parent, const MyMoneyTransaction& transaction, const MyMoneySplit& split, int uniqueId);
00428 
00429   const MyMoneyAccount& account(void) const { return m_account; }
00430 
00431   void repaintItems(RegisterItem* first = 0, RegisterItem* last = 0);
00432 
00433   unsigned int drawCounter(void) const { return m_drawCounter; }
00434 
00438   void addGroupMarkers(void);
00439 
00445   void removeUnwantedGroupMarkers(void);
00446 
00447   void setLedgerLensForced(bool forced=true) { m_ledgerLensForced = forced; }
00448 
00453   void setSelectionMode(SelectionMode mode) { m_selectionMode = mode; }
00454 
00455 protected:
00456 
00457   void drawContents(QPainter *p, int cx, int cy, int cw, int ch);
00458 
00459   void contentsMouseReleaseEvent( QMouseEvent *e );
00460 
00461   void unselectItems(int from = -1, int to = -1) { doSelectItems(from, to, false); }
00462   void selectItems(int from, int to) { doSelectItems(from, to, true); }
00463   void doSelectItems(int from, int to, bool selected);
00464   int selectedItemsCount(void) const;
00465 
00466   void focusOutEvent(QFocusEvent*);
00467   void focusInEvent(QFocusEvent*);
00468   void keyPressEvent(QKeyEvent*);
00469 
00470   int rowToIndex(int row) const;
00471   void setupItemIndex(int rowCount);
00472 
00482   void scrollPage(int key, ButtonState state);
00483 
00492   RegisterItem* itemById(const QString& id) const;
00493 
00494   void insertWidget(int row, int col, QWidget* w);
00495 
00499   bool focusNextPrevChild(bool next);
00500 
00501   bool eventFilter(QObject* o, QEvent* e);
00502 
00503   void handleItemChange(RegisterItem* old, bool shift, bool control);
00504 
00505   void selectRange(RegisterItem* from, RegisterItem* to, bool invert, bool includeFirst, bool clearSel);
00506 
00507   // DND
00508   void dragMoveEvent(QDragMoveEvent* event);
00509   void dropEvent(QDropEvent* event);
00510   Transaction* dropTransaction(QPoint cPos) const;
00511 
00512 protected slots:
00513   void resize(void);
00514 
00515   void selectItem(int row, int col, int button, const QPoint & mousePos );
00516   void slotEnsureItemVisible(void);
00517   void slotDoubleClicked(int, int, int, const QPoint&);
00518 
00519   void slotToggleErronousTransactions(void);
00520   void slotAutoColumnSizing(int section);
00521 
00522 signals:
00523   void selectionChanged(void);
00524   void selectionChanged(const KMyMoneyRegister::SelectedTransactions& list);
00530   void focusChanged(KMyMoneyRegister::Transaction* item);
00531 
00536   void focusChanged(void);
00537 
00544   void aboutToSelectItem(KMyMoneyRegister::RegisterItem* item, bool& okToSelect);
00545 
00546   void editTransaction(void);
00547   void headerClicked(void);
00548 
00553   void reconcileStateColumnClicked(KMyMoneyRegister::Transaction* item);
00554 
00558   void emptyItemSelected(void);
00559 
00563   void openContextMenu(void);
00564 
00568   void itemAdded(RegisterItem* item);
00569 
00570 protected:
00571   ItemPtrVector                m_items;
00572   QValueVector<RegisterItem*>  m_itemIndex;
00573   RegisterItem*                m_selectAnchor;
00574   RegisterItem*                m_focusItem;
00575   RegisterItem*                m_ensureVisibleItem;
00576   RegisterItem*                m_firstItem;
00577   RegisterItem*                m_lastItem;
00578   RegisterItem*                m_firstErronous;
00579   RegisterItem*                m_lastErronous;
00580 
00581   int                          m_markErronousTransactions;
00582   int                          m_rowHeightHint;
00583 
00584   MyMoneyAccount               m_account;
00585 
00586   bool                         m_ledgerLensForced;
00587   SelectionMode                m_selectionMode;
00588 
00589 private:
00590   bool                         m_listsDirty;
00591   bool                         m_ignoreNextButtonRelease;
00592   bool                         m_needInitialColumnResize;
00593   Qt::ButtonState              m_buttonState;
00594   Column                       m_lastCol;
00595   QValueList<TransactionSortField> m_sortOrder;
00596   QMap<QPair<int, int>, QWidget*> m_cellWidgets;
00597   RegisterToolTip*             m_tooltip;
00598   QRect                        m_lastRepaintRect;
00599   unsigned int                 m_drawCounter;
00600 };
00601 
00602 } // namespace
00603 
00604 #endif
00605 // vim:cin:si:ai:et:ts=2:sw=2:

Generated on Wed Jan 26 13:03:18 2011 for KMyMoney by  doxygen 1.5.6