Scribus
Open source desktop publishing at your fingertips
pageitem_table.h
1 /*
2 Copyright (C) 2011 Elvis Stansvik <elvstone@gmail.com>
3 
4 For general Scribus (>=1.3.2) copyright and licensing information please refer
5 to the COPYING file provided with the program. Following this notice may exist
6 a copyright and/or license notice that predates the release of Scribus 1.3.2
7 for which a new license (GPL+exception) is in place.
8 */
9 #ifndef PAGEITEM_TABLE_H
10 #define PAGEITEM_TABLE_H
11 
12 #include <QList>
13 #include <QPointF>
14 #include <QRectF>
15 #include <QSet>
16 #include <QString>
17 
18 #include "cellarea.h"
19 #include "pageitem.h"
20 #include "scribusapi.h"
21 #include "styles/tablestyle.h"
22 #include "tablecell.h"
23 #include "tablehandle.h"
24 
25 class ScPainter;
26 class ScribusDoc;
27 class TablePainter;
28 
49 class SCRIBUS_API PageItem_Table : public PageItem
50 {
51  Q_OBJECT
52  Q_ENUMS(ResizeStrategy HitTarget)
53 
54  Q_PROPERTY(int rows READ rows NOTIFY changed)
55  Q_PROPERTY(int columns READ columns NOTIFY changed)
56  Q_PROPERTY(QString fillColor READ fillColor WRITE setFillColor RESET unsetFillColor NOTIFY changed)
57  Q_PROPERTY(TableBorder leftBorder READ leftBorder WRITE setLeftBorder RESET unsetLeftBorder NOTIFY changed)
58  Q_PROPERTY(TableBorder rightBorder READ rightBorder WRITE setRightBorder RESET unsetRightBorder NOTIFY changed)
59  Q_PROPERTY(TableBorder topBorder READ topBorder WRITE setTopBorder RESET unsetTopBorder NOTIFY changed)
60  Q_PROPERTY(TableBorder bottomBorder READ bottomBorder WRITE setBottomBorder RESET unsetBottomBorder NOTIFY changed)
61  Q_PROPERTY(QString style READ style WRITE setStyle RESET unsetStyle NOTIFY changed)
62 
63 public:
72  {
73  MoveFollowing,
74  ResizeFollowing
75  };
76 
78  static const double MinimumRowHeight;
79 
81  static const double MinimumColumnWidth;
82 
83 public:
85  PageItem_Table(ScribusDoc *pa, double x, double y, double w, double h, double w2,
86  QString fill, QString outline, int numRows = 1, int numColumns = 1);
87 
89  ~PageItem_Table();
90 
92  void adjustTable();
93 
95  int rows() const { return m_rows; }
96 
98  int columns() const { return m_columns; }
99 
105  double tableWidth() const { return m_columnPositions.last() + m_columnWidths.last(); }
106 
112  double tableHeight() const { return m_rowPositions.last() + m_rowHeights.last(); }
113 
117  QPointF gridOffset() const { return QPointF(maxLeftBorderWidth()/2, maxTopBorderWidth()/2); }
118 
125  double effectiveWidth() const { return tableWidth() + (maxLeftBorderWidth() + maxRightBorderWidth()) / 2; }
126 
133  double effectiveHeight() const { return tableHeight() + (maxTopBorderWidth() + maxBottomBorderWidth()) / 2; }
134 
143  void resize(double width, double height);
144 
153  void insertRows(int index, int numRows);
154 
161  void removeRows(int index, int numRows);
162 
166  double rowHeight(int row) const;
167 
171  QList<double> rowHeights() const { return m_rowHeights; }
172 
178  void resizeRow(int row, double height, ResizeStrategy strategy = MoveFollowing);
179 
188  void distributeRows(int startRow, int endRow);
189 
193  double rowPosition(int row) const;
194 
198  QList<double> rowPositions() const { return m_rowPositions; }
199 
208  void insertColumns(int index, int numColumns);
209 
216  void removeColumns(int index, int numColumns);
217 
221  double columnWidth(int column) const;
222 
226  QList<double> columnWidths() const { return m_columnWidths; }
227 
233  void resizeColumn(int column, double width, ResizeStrategy strategy = MoveFollowing);
234 
243  void distributeColumns(int startColumn, int endColumn);
244 
248  double columnPosition(int column) const;
249 
253  QList<double> columnPositions() const { return m_columnPositions; }
254 
266  void mergeCells(int row, int column, int numRows, int numCols);
267 
272  void splitCell(int row, int column, int numRows, int numCols);
273 
277  QSet<TableCell> selectedCells() const { return m_selection; }
278 
284  QSet<int> selectedRows() const;
285 
291  QSet<int> selectedColumns() const;
292 
298  void selectCell(int row, int column);
299 
309  void selectCells(int startRow, int startColumn, int endRow, int endColumn);
310 
314  void clearSelection();
315 
323  TableCell cellAt(int row, int column) const;
324 
333  TableCell cellAt(const QPointF& point) const;
334 
341  TableCell activeCell() const { return m_activeCell; }
342 
346  void moveLeft();
347 
351  void moveRight();
352 
356  void moveUp();
357 
361  void moveDown();
362 
366  void moveTo(const TableCell& cell);
367 
374  TableHandle hitTest(const QPointF& point, double threshold) const;
375 
377  void adjustTableToFrame();
378 
380  void adjustFrameToTable();
381 
383  void setFillColor(const QString& color);
384 
386  void unsetFillColor();
387 
389  QString fillColor() const;
390 
392  void setFillShade(const double& shade);
393 
395  void unsetFillShade();
396 
398  double fillShade() const;
399 
401  void setLeftBorder(const TableBorder& border);
402 
404  void unsetLeftBorder();
405 
407  TableBorder leftBorder() const;
408 
410  void setRightBorder(const TableBorder& border);
411 
413  void unsetRightBorder();
414 
416  TableBorder rightBorder() const;
417 
419  void setTopBorder(const TableBorder& border);
420 
422  void unsetTopBorder();
423 
425  TableBorder topBorder() const;
426 
428  void setBottomBorder(const TableBorder& border);
429 
431  void unsetBottomBorder();
432 
434  TableBorder bottomBorder() const;
435 
437  void setStyle(const QString& style);
438 
440  void unsetStyle();
441 
443  void unsetDirectFormatting();
444 
446  QString style() const;
447 
449  void updateCells() { updateCells(0, 0, rows() - 1, columns() - 1); }
450 
452  void updateCells(int startRow, int startColumn, int endRow, int endColumn);
453 
455  bool isOverflowing() const { return effectiveWidth() > width() || effectiveHeight() > height(); }
456 
458  virtual PageItem_Table* asTable() { return this; }
459 
461  virtual bool isTable() const { return true; }
462 
464  virtual ItemType realItemType() const { return PageItem::Table; }
465 
467  virtual void applicableActions(QStringList& actionList);
468 
470  virtual QString infoDescription() { return QString(); }
471 
473  QList<CellArea> cellAreas() const { return m_cellAreas; }
474 
476  QList<QList<TableCell> > cellRows() const { return m_cellRows; }
477 
478 signals:
480  void changed();
482  void selectionChanged();
483 
484 private slots:
486  void handleStyleChanged();
487 
488 protected:
490  virtual void DrawObj_Item(ScPainter *p, QRectF clipRect);
491 
492 private:
494  enum ChangeType
495  {
496  RowsInserted,
497  RowsRemoved,
498  ColumnsInserted,
499  ColumnsRemoved
500  };
501 
507  void initialize(int numRows, int numColumns);
508 
510  void activateCell(const TableCell& cell);
511 
513  bool validRow(int row) const { return row >= 0 && row < m_rows; }
515  bool validColumn(int column) const { return column >= 0 && column < m_columns; }
517  bool validCell(int row, int column) const { return validRow(row) && validColumn(column); }
518 
520  double maxLeftBorderWidth() const;
521 
523  double maxRightBorderWidth() const;
524 
526  double maxTopBorderWidth() const;
527 
529  double maxBottomBorderWidth() const;
530 
533  double resizeRowMoveFollowing(int row, double height);
535  double resizeRowResizeFollowing(int row, double height);
537  double resizeColumnMoveFollowing(int row, double width);
539  double resizeColumnResizeFollowing(int row, double width);
540 
550  void updateSpans(int index, int number, ChangeType changeType);
551 
553  void debug() const;
554 
556  void assertValid() const;
557 
558 private:
559  //<<Data we need to save
561  QList<QList<TableCell> > m_cellRows;
562 
564  int m_rows;
566  int m_columns;
567 
569  QList<double> m_rowPositions;
571  QList<double> m_rowHeights;
572 
574  QList<double> m_columnPositions;
576  QList<double> m_columnWidths;
577 
579  QList<CellArea> m_cellAreas;
580 
582  TableStyle m_style;
583  //>>End of data we need to save
584  //-----------------------------
585  //<<Live working variables/data
587  QSet<TableCell> m_selection;
588 
590  TablePainter* m_tablePainter;
591 
593  TableCell m_activeCell;
594 
595  /*
596  * The two members below describe the active "logical position" on the table grid.
597  * This position may or may not correspond with the position of the active cell.
598  *
599  * As an example, imagine the following scenario:
600  *
601  * +--------+--------+ +--------+--------+
602  * | | | | | | |
603  * +--------+ | --> User presses Tab --> +--------+ |
604  * | | | | | | |
605  * +--------+--------+ +--------+--------+
606  * Cursor in (1, 0) Cursor in (0, 1)
607  *
608  * The active cell is now the spanning cell at (0, 1). If we would use the row and
609  * column of the active cell as the basis for keyboard navigation between cells,
610  * then pressing Shift+Tab after the scenario above would bring the cursor into
611  * cell (0, 0), not back to to (1, 0). Hence, in addition to the active cell, we
612  * keep track of a "logical position". This is the logical position on the table
613  * grid that is active. In the above scenario, the logical position would change
614  * from (1, 0) to (1, 1).
615  */
616 
618  int m_activeRow;
619 
621  int m_activeColumn;
622  //>>End of live working variables/data
623 };
624 
625 #endif // PAGEITEM_TABLE_H
double tableWidth() const
Definition: pageitem_table.h:105
QList< double > columnWidths() const
Definition: pageitem_table.h:226
Definition: tablestyle.h:30
virtual QString infoDescription()
Returns a textual description of this item.
Definition: pageitem_table.h:470
Definition: tablepainter.h:21
QList< double > rowHeights() const
Definition: pageitem_table.h:171
double tableHeight() const
Definition: pageitem_table.h:112
Definition: tablehandle.h:19
void setFillColor(const QString &newColor)
Set the fill color of the object.
Definition: pageitem.cpp:4011
QList< QList< TableCell > > cellRows() const
Returns the rows of the table for writing to SLA.
Definition: pageitem_table.h:476
double fillShade() const
Get the shade of the fill color.
Definition: pageitem.h:813
Definition: tableborder.h:86
QList< double > columnPositions() const
Definition: pageitem_table.h:253
TableCell activeCell() const
Definition: pageitem_table.h:341
void setFillShade(double newShade)
Set the fill color shade.
Definition: pageitem.cpp:4061
virtual bool isTable() const
Returns true.
Definition: pageitem_table.h:461
the Document Class
Definition: scribusdoc.h:90
Definition: scpainter.h:33
virtual ItemType realItemType() const
Returns PageItem::Table.
Definition: pageitem_table.h:464
QList< CellArea > cellAreas() const
Returns the Cell Areas from this table.
Definition: pageitem_table.h:473
double effectiveWidth() const
Definition: pageitem_table.h:125
bool isOverflowing() const
Returns true if the table is overflowing its frame.
Definition: pageitem_table.h:455
int rows() const
Returns the number of rows in the table.
Definition: pageitem_table.h:95
QList< double > rowPositions() const
Definition: pageitem_table.h:198
static const double MinimumRowHeight
The minimum row height.
Definition: pageitem_table.h:78
QString fillColor() const
Get the (name of the) fill color of the object.
Definition: pageitem.h:805
double effectiveHeight() const
Definition: pageitem_table.h:133
ItemType
Item Type.
Definition: pageitem.h:174
virtual PageItem_Table * asTable()
Returns this item as a PageItem_Table.
Definition: pageitem_table.h:458
ResizeStrategy
Definition: pageitem_table.h:71
Definition: tablecell.h:97
Definition: pageitem.h:92
void updateCells()
Updates the position and size of all cell text frames for this table.
Definition: pageitem_table.h:449
QSet< TableCell > selectedCells() const
Definition: pageitem_table.h:277
QPointF gridOffset() const
Definition: pageitem_table.h:117
static const double MinimumColumnWidth
The minimum column width.
Definition: pageitem_table.h:81
Definition: pageitem_table.h:49
int columns() const
Returns the number of columns in the table.
Definition: pageitem_table.h:98