StanfordCPPLib
gtable.h
1 /*
2  * File: gtable.h
3  * --------------
4  * This file exports the GTable class for a graphical editable 2D table.
5  *
6  * @author Marty Stepp
7  * @version 2018/08/23
8  * - renamed to gtable.h to replace Java version
9  * @version 2018/07/17
10  * - initial version, based on gtable.h
11  * @since 2018/07/17
12  */
13 
14 #include "private/init.h" // ensure that Stanford C++ lib is initialized
15 
16 #ifndef INTERNAL_INCLUDE
17 // signal that GUI system is in use (so it will be initialized)
18 #define SPL_QT_GUI_IN_USE 1
19 #include "private/initstudent.h" // insert necessary included code by student
20 #endif // INTERNAL_INCLUDE
21 
22 #ifndef _gtable_h
23 #define _gtable_h
24 
25 #include <string>
26 #include <QAbstractItemModel>
27 #include <QBrush>
28 #include <QFont>
29 #include <QItemSelection>
30 #include <QStyledItemDelegate>
31 #include <QWidget>
32 #include <QTableWidget>
33 
34 #define INTERNAL_INCLUDE 1
35 #include "grid.h"
36 #define INTERNAL_INCLUDE 1
37 #include "map.h"
38 #define INTERNAL_INCLUDE 1
39 #include "ginteractor.h"
40 #define INTERNAL_INCLUDE 1
41 #include "gobjects.h"
42 #define INTERNAL_INCLUDE 1
43 #include "gtypes.h"
44 #undef INTERNAL_INCLUDE
45 
46 class _Internal_QTableWidget;
47 
64 class GTable : public GInteractor {
65 public:
74  COLUMN_HEADER_NONE, // headers will not show
75  COLUMN_HEADER_EXCEL, // A, B, ..., Z, AA, AB, ...
76  COLUMN_HEADER_NUMERIC // 1, 2, 3, ...
77  };
78 
86  GTable(int rows = 0, int columns = 0, double width = 0, double height = 0,
87  QWidget* parent = nullptr);
88 
89  virtual ~GTable() ;
90 
95  virtual void autofitColumnWidths();
96 
100  virtual void clear();
101 
106  virtual void clearCell(int row, int column);
107 
111  virtual void clearFormatting();
112 
116  virtual void clearCellFormatting(int row, int column);
117 
122  virtual void clearSelection();
123 
127  virtual void fill(const std::string& text);
128 
133  virtual std::string get(int row, int column) const;
134 
139  virtual ColumnHeaderStyle getColumnHeaderStyle() const;
140 
146  virtual double getColumnWidth(int column) const;
147 
148  /* @inherit */
149  virtual _Internal_QWidget* getInternalWidget() const ;
150 
156  virtual double getRowHeight(int row) const;
157 
162  virtual GridLocation getSelectedCell() const;
163 
169  virtual void getSelectedCell(int& row, int& column) const;
170 
175  virtual std::string getSelectedCellValue() const;
176 
181  virtual int getSelectedColumn() const;
182 
187  virtual int getSelectedRow() const;
188 
189  /* @inherit */
190  virtual std::string getType() const ;
191 
192  /* @inherit */
193  virtual QWidget* getWidget() const ;
194 
198  virtual bool hasSelectedCell() const;
199 
204  virtual int height() const;
205 
211  virtual bool inBounds(int row, int column) const;
212 
217  virtual bool isEditable() const;
218 
223  virtual int numCols() const;
224 
229  virtual int numRows() const;
230 
235  virtual void removeTableListener();
236 
237  /* @inherit */
238  virtual void requestFocus() ;
239 
245  virtual void resize(int numRows, int numCols);
246 
251  virtual bool rowColumnHeadersVisible() const;
252 
260  virtual void select(int row, int column);
261 
266  virtual void set(int row, int column, const std::string& text);
267 
272  virtual void setBackground(int rgb) ;
273 
278  virtual void setBackground(const std::string& color) ;
279 
285  virtual void setCellAlignment(int row, int column, HorizontalAlignment alignment);
286 
293  virtual void setCellBackground(int row, int column, int color);
294 
301  virtual void setCellBackground(int row, int column, const std::string& color);
302 
309  virtual void setCellFont(int row, int column, const std::string& font);
310 
317  virtual void setCellForeground(int row, int column, int color);
318 
325  virtual void setCellForeground(int row, int column, const std::string& color);
326 
332  virtual void setColor(int rgb) ;
333 
339  virtual void setColor(const std::string& color) ;
340 
346  virtual void setColumnAlignment(int column, HorizontalAlignment alignment);
347 
354  virtual void setColumnBackground(int column, int color);
355 
362  virtual void setColumnBackground(int column, const std::string& color);
363 
370  virtual void setColumnFont(int column, const std::string& font);
371 
378  virtual void setColumnForeground(int column, int color);
379 
386  virtual void setColumnForeground(int column, const std::string& color);
387 
393  virtual void setColumnWidth(int column, double width);
394 
399  virtual void setColumnHeaderStyle(ColumnHeaderStyle style);
400 
405  virtual void setEditable(bool editable);
406 
413  virtual void setEditorValue(int row, int column, const std::string& text);
414 
418  virtual void setFont(const QFont& font) ;
419 
424  virtual void setFont(const std::string& font) ;
425 
431  virtual void setForeground(int rgb) ;
432 
438  virtual void setForeground(const std::string& color) ;
439 
444  virtual void setHorizontalAlignment(HorizontalAlignment alignment);
445 
451  virtual void setRowAlignment(int row, HorizontalAlignment alignment);
452 
459  virtual void setRowBackground(int row, int rgb);
460 
467  virtual void setRowBackground(int row, const std::string& color);
468 
475  virtual void setRowFont(int row, const std::string& font);
476 
483  virtual void setRowForeground(int row, int rgb);
484 
491  virtual void setRowForeground(int row, const std::string& color);
492 
497  virtual void setRowColumnHeadersVisible(bool visible);
498 
504  virtual void setRowHeight(int row, double width);
505 
510  virtual void setSelectedCellValue(const std::string& text);
511 
516  virtual void setTableListener(GEventListener func);
517 
522  virtual void setTableListener(GEventListenerVoid func);
523 
528  virtual int width() const;
529 
530 private:
531  Q_DISABLE_COPY(GTable)
532 
533  // Represents cascading styles on a cell, row, column, or table.
534  struct TableStyle {
535  int background;
536  int foreground;
537  string font;
538  HorizontalAlignment alignment;
539  // TODO: borders?
540 
541  TableStyle() {
542  background = 0;
543  foreground = 0;
544  font = "";
545  alignment = ALIGN_LEFT;
546  }
547 
548  bool isSet() const {
549  return background >= 0
550  && foreground >= 0
551  && !font.empty()
552  && alignment >= 0;
553  }
554 
555  void mergeWith(const TableStyle& other) {
556  if (other.background >= 0) {
557  background = other.background;
558  }
559  if (other.foreground >= 0) {
560  foreground = other.foreground;
561  }
562  if (!other.font.empty()) {
563  font = other.font;
564  }
565  if (other.alignment >= 0) {
566  alignment = other.alignment;
567  }
568  }
569 
570  TableStyle mergedWith(const TableStyle& other) {
571  TableStyle copy = *this;
572  copy.mergeWith(other);
573  return copy;
574  }
575 
576  static TableStyle unset() {
577  TableStyle style;
578  style.background = -1;
579  style.foreground = -1;
580  style.font = "";
581  style.alignment = (HorizontalAlignment) -1;
582  return style;
583  }
584  };
585 
586  // static variables for default formatting:
587  // background/foreground colors
588  // font
589  // alignment
590  static TableStyle _defaultCellStyle;
591 
592  // member variables
593  _Internal_QTableWidget* _iqtableview;
594  ColumnHeaderStyle _columnHeaderStyle;
595 
596  // styles on table, rows, columns, cells
597  Map<int, TableStyle> _rowStyles;
598  Map<int, TableStyle> _columnStyles;
599  TableStyle _globalCellStyle;
600 
601  void applyStyleToCell(int row, int column, const TableStyle& style);
602 
603  /*
604  * @throw ErrorException if the given row/column values are out of bounds.
605  */
606  void checkColumn(const string& member, int column) const;
607  void checkIndex(const string& member, int row, int column) const;
608  void checkRow(const string& member, int row) const;
609 
610  void ensureColumnStyle(int column);
611  void ensureDefaultFormatting() const; // const hack
612  void ensureGlobalCellStyle();
613  void ensureRowStyle(int row);
614  TableStyle getMergedStyleForCell(int row, int column);
615 
616  // Internal setters for cell formatting.
617  virtual void setCellAlignmentInternal(int row, int column, HorizontalAlignment alignment);
618  virtual void setCellBackgroundInternal(int row, int column, int color);
619  virtual void setCellFontInternal(int row, int column, const string& font);
620  virtual void setCellForegroundInternal(int row, int column, int color);
621 
622  static string toExcelColumnName(int col);
623  // static GridLocation toRowColumn(const string& excelColumnName);
624 
625  void updateColumnHeaders();
626 
627  friend class _Internal_QTableWidget;
628 };
629 
634 class _Internal_QItemDelegate : public QStyledItemDelegate {
635  Q_OBJECT
636 
637 public:
638  _Internal_QItemDelegate(QObject* parent = nullptr);
639  virtual QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const;
640  virtual void destroyEditor(QWidget* editor, const QModelIndex& index) const;
641  virtual QWidget* getEditor() const;
642 
643 private:
644  QWidget* _editor;
645 };
646 
647 
652 class _Internal_QTableWidget : public QTableWidget, public _Internal_QWidget {
653  Q_OBJECT
654 
655 public:
656  _Internal_QTableWidget(GTable* gtable, int rows, int columns, QWidget* parent = nullptr);
657  virtual bool edit(const QModelIndex& index, QAbstractItemView::EditTrigger trigger, QEvent* event) ;
658  virtual QWidget* getEditor() const;
659  virtual _Internal_QItemDelegate* getItemDelegate() const;
660  virtual bool isEditing() const;
661  virtual void closeEditor(QWidget* editor, QAbstractItemDelegate::EndEditHint hint) ;
662  virtual void keyPressEvent(QKeyEvent* event) ;
663  virtual QSize sizeHint() const ;
664 
665 public slots:
666  void handleCellChange(int row, int column);
667  void handleCellDoubleClick(int row, int column);
668  void handleSelectionChange(const QItemSelection& selected, const QItemSelection& deselected);
669 
670 private:
671  GTable* _gtable;
672  _Internal_QItemDelegate* _delegate;
673  int _lastKeyPressed;
674 
675  void fireTableEvent(EventType eventType, const std::string& eventName, int row = -1, int col = -1);
676 
677  friend class GTable;
678 };
679 
680 #endif // _gtable_h
virtual void clearFormatting()
Removes any per-cell/column/row formatting that has been applied to the table.
Definition: gtable.cpp:105
Definition: gtable.h:76
virtual double getRowHeight(int row) const
Returns the height of the given row index in pixels.
Definition: gtable.cpp:216
virtual void setForeground(int rgb)
Sets the color used for the text of each cell.
Definition: gtable.cpp:606
virtual void setEditorValue(int row, int column, string text)
Modifies the value in the cell that is currently being edited to store the given text.
Definition: gtable.cpp:562
virtual void setRowFont(int row, string font)
Sets the text font of the given row to the given font.
Definition: gtable.cpp:698
A GTable represents a graphical editable 2D table, like a mediocre facsimile of an Excel spreadsheet...
Definition: gtable.h:64
virtual int getSelectedColumn() const
Returns the column of the cell that is currently selected, or -1 if no cell is currently selected...
Definition: gtable.cpp:246
virtual void setCellFont(int row, int column, string font)
Sets the text font of the given cell to the given RGB color.
Definition: gtable.cpp:418
Definition: gridlocation.h:41
virtual void fill(string text)
Sets every cell in the table to have the given value.
Definition: gtable.cpp:172
virtual void setHorizontalAlignment(HorizontalAlignment alignment)
Sets the horizontal alignment of the text in all cells in the table.
Definition: gtable.cpp:638
virtual void setColumnWidth(int column, double width)
Sets the given column index to have the given width in pixels.
Definition: gtable.cpp:538
virtual bool hasSelectedCell() const
Returns true if a cell is currently selected.
Definition: gtable.cpp:262
virtual void setColumnAlignment(int column, HorizontalAlignment alignment)
Sets the horizontal alignment of the given column.
Definition: gtable.cpp:454
virtual string get(int row, int column) const
Returns the text stored in the given cell.
Definition: gtable.cpp:184
STL namespace.
virtual void removeTableListener()
Removes the table listener from this button so that it will no longer call it when events occur...
Definition: gtable.cpp:287
virtual void setCellAlignment(int row, int column, HorizontalAlignment alignment)
Sets the horizontal alignment of the given cell.
Definition: gtable.cpp:390
virtual void setCellForeground(int row, int column, int color)
Sets the foreground/text color of the given cell to the given color.
Definition: gtable.cpp:429
virtual void requestFocus()
Transfers keyboard focus to this interactor.
Definition: gtable.cpp:298
virtual int numRows() const
Returns the number of rows in the table.
Definition: gtable.cpp:283
virtual bool rowColumnHeadersVisible() const
Returns whether row and column headers are shown in the table.
Definition: gtable.cpp:337
virtual bool inBounds(int row, int column) const
Returns true if the given 0-based row/column index is within the bounds of the table.
Definition: gtable.cpp:271
virtual ~GTable()
Definition: gtable.cpp:61
virtual string getType() const
Returns a string representing the class name of this interactor, such as "GButton" or "GCheckBox"...
Definition: gtable.cpp:254
virtual int numCols() const
Returns the number of columns in the table.
Definition: gtable.cpp:279
virtual void autofitColumnWidths()
Changes widths of all columns to be perfectly large enough to fit their contents. ...
Definition: gtable.cpp:74
virtual double getColumnWidth(int column) const
Returns the width of the given column index in pixels.
Definition: gtable.cpp:193
virtual void setRowHeight(int row, double width)
Sets the given row index to have the given height in pixels.
Definition: gtable.cpp:740
This abstract class is the superclass for all graphical interactors.
Definition: ginteractor.h:53
virtual void setColumnForeground(int column, int color)
Sets the foreground/text color of the given column to the given color.
Definition: gtable.cpp:503
GTable(int rows=0, int columns=0, double width=0, double height=0, QWidget* parent=nullptr)
Constructs a new table with the given dimensions and (optional) size.
Definition: gtable.cpp:48
virtual void setCellBackground(int row, int column, int color)
Sets the background color of the given cell to the given color.
Definition: gtable.cpp:403
virtual void setFont(const QFont &font)
Sets the font used to display each cell&#39;s text.
Definition: gtable.cpp:575
virtual void setTableListener(GEventListener func)
Sets the given function to be called when events occur in this table.
Definition: gtable.cpp:757
virtual void setSelectedCellValue(string text)
Sets the text in the cell that is currently selected.
Definition: gtable.cpp:750
virtual void setColumnBackground(int column, int color)
Sets the background color of the given column to the given color.
Definition: gtable.cpp:469
virtual void clearCellFormatting(int row, int column)
Removes any formatting that has been applied to the given cell.
Definition: gtable.cpp:121
ColumnHeaderStyle
Styles of column header labels that can be shown.
Definition: gtable.h:73
virtual string getSelectedCellValue() const
Returns the text in the cell that is currently selected.
Definition: gtable.cpp:237
virtual void clearCell(int row, int column)
Sets the given cell to store an empty string value.
Definition: gtable.cpp:100
Definition: gtable.h:75
virtual void clear()
Sets all cells in the table to store an empty string value.
Definition: gtable.cpp:92
virtual int height() const
Returns the number of rows in the table.
Definition: gtable.cpp:267
virtual int getSelectedRow() const
Returns the row of the cell that is currently selected, or -1 if no cell is currently selected...
Definition: gtable.cpp:250
virtual int width() const
Returns the number of columns in the table.
Definition: gtable.cpp:812
virtual void setBackground(int rgb)
Sets the background color that appears behind each cell.
Definition: gtable.cpp:358
virtual void setRowBackground(int row, int rgb)
Sets the background color of the given row to the given RGB color.
Definition: gtable.cpp:679
virtual void resize(int numRows, int numCols)
Modifies the table to have the given number of rows and columns.
Definition: gtable.cpp:309
virtual void setColumnHeaderStyle(ColumnHeaderStyle style)
Sets the column headers to use the given style.
Definition: gtable.cpp:522
virtual ColumnHeaderStyle getColumnHeaderStyle() const
Returns the column headers to use the given style.
Definition: gtable.cpp:189
virtual _Internal_QWidget* getInternalWidget() const
Returns a direct pointer to the internal Qt widget being wrapped by this interactor.
Definition: gtable.cpp:198
virtual bool isEditable() const
Returns whether cells of the table can be edited.
Definition: gtable.cpp:275
virtual QWidget* getWidget() const
Returns a direct pointer to the internal Qt widget being wrapped by this interactor.
Definition: gtable.cpp:258
virtual void setEditable(bool editable)
Sets whether cells of the table can be edited.
Definition: gtable.cpp:548
virtual void setRowAlignment(int row, HorizontalAlignment alignment)
Sets the horizontal alignment of the given row.
Definition: gtable.cpp:664
virtual void setColumnFont(int column, string font)
Sets the text font of the given column to the given RGB color.
Definition: gtable.cpp:488
virtual void setColor(int rgb)
Sets the color used for the text of each cell.
Definition: gtable.cpp:446
virtual void setRowColumnHeadersVisible(bool visible)
Sets whether row and column headers should be shown in the table.
Definition: gtable.cpp:733
virtual void clearSelection()
Deselects any currently selected cell.
Definition: gtable.cpp:141
virtual void setRowForeground(int row, int rgb)
Sets the foreground/text color of the given row to the given color.
Definition: gtable.cpp:713
virtual void set(int row, int column, string text)
Modifies the value in the given cell to store the given text.
Definition: gtable.cpp:350
virtual GridLocation getSelectedCell() const
Returns the row and column of the cell that is currently selected.
Definition: gtable.cpp:221
virtual void select(int row, int column)
Sets the given cell to become currently selected, replacing any previous selection.
Definition: gtable.cpp:342
Definition: gtable.h:74