tableeditwidget.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #ifndef TABLEEDITWIDGET_H
  2. #define TABLEEDITWIDGET_H
  3. #include "src/gui/dialogues/entryeditdialog.h"
  4. #include <QWidget>
  5. #include <QSqlTableModel>
  6. #include <QHeaderView>
  7. #include <QTableView>
  8. #include <QLineEdit>
  9. #include <QPushButton>
  10. #include <QStackedWidget>
  11. #include <QComboBox>
  12. #include <QLabel>
  13. /*!
  14. * \brief The TableEditWidget class is a base class for widgets which enable
  15. * editing certain tables in the datbase.
  16. * \details The TableEditWidget consists of a QTableView which displays the data
  17. * from a given table. The data is held in a QSqlTableModel. The views edit triggers
  18. * are disabled. Whenever a row is selected in the view, the selected entry is displayed
  19. * for editing in a suitable EntryEditDialog which is responsible for the verification of
  20. * the user input as well as reading and writing to and from the database.
  21. *
  22. * The TableEditWidget has two Orientation options: Horizontal and Vertical
  23. *
  24. * In the Horizontal layout, the table view is split horizontally to make space
  25. * for the TableEditWidget on the right hand side, whereas on the Vertical layout
  26. * it is split vertically with the TableEditWidget occupying the lower half of the screen.
  27. *
  28. * When implementing the TableEditWidget it is important to set up the model and call the
  29. * Base Class implementation of setupUI before performing any specialisations. Before the
  30. * TableEditWidget is shown, the init method must be run.
  31. */
  32. class TableEditWidget : public QWidget
  33. {
  34. Q_OBJECT
  35. public:
  36. /*!
  37. * \brief Determines how the layout is created
  38. * \details <ul>
  39. * <li> Horizontal: The edit widget is shown besides the table </li>
  40. * <li> Vertical: The edit widget is shown below the table </li>
  41. * </ul>
  42. */
  43. enum Orientation {Horizontal, Vertical};
  44. /*!
  45. * \brief Create a new TableEditWidget
  46. */
  47. explicit TableEditWidget(Orientation orientation = Horizontal, QWidget *parent = nullptr);
  48. /*!
  49. * \brief Initialises the dialog by calling its virtual setup functions.
  50. * \attention Call this functian before showing the dialog.
  51. */
  52. void init();
  53. /*!
  54. * \brief Set up the model and view of the widget
  55. * \details Implement this function to initialise the protected members of this class.
  56. * This includes setting the QSqlTableModel and QTableView */
  57. virtual void setupModelAndView() = 0;
  58. /*!
  59. * \brief Set up the UI of the widget
  60. * \details Implement this function to set appropriate labels to the protected members of this
  61. * class. This includes setting appropriate labels on the Push Buttons as well as
  62. * appropriate filter options in the filter Combo Box. Make sure to call the base class
  63. * implementation first when overriding this method.
  64. */
  65. virtual void setupUI();
  66. /*!
  67. * \brief create an error String when deleting a database entry has been unsuccessful
  68. * \param rowId - the row id of the entry to be deleted
  69. * \details When deleting an entry from a database fails, this can have different reasons
  70. * depending on the table. This function returns an implementation-specific error string
  71. * to inform the user about the failure and give hints on how to fix it
  72. */
  73. virtual QString deleteErrorString(int rowId) = 0;
  74. /*!
  75. * \brief return a String asking the user to confirm deletion of a given entry
  76. * \param rowId - the row id of the entry to be deleted
  77. * \brief The message string is displayed in a QMessageBox
  78. */
  79. virtual QString confirmDeleteString(int rowId) = 0;
  80. /*!
  81. * \brief get an apropriate Edit Dialog for the implementation
  82. * \details The Edit Dialogs for different tables differ in the data they display
  83. * and how they verify the user inputs. This method returns an apropriate
  84. * EntryEditDialog for the selected table.
  85. */
  86. virtual EntryEditDialog *getEntryEditDialog(QWidget *parent = nullptr) = 0;
  87. protected:
  88. Orientation _orientation;
  89. QSqlTableModel *model = nullptr;
  90. QTableView *view = new QTableView(this);
  91. QWidget *_filterWidget = nullptr;
  92. QWidget *_buttonWidget = nullptr;
  93. EntryEditDialog *_entryEditDialog = nullptr;
  94. QPushButton *addNewEntryPushButton = new QPushButton(this);
  95. QPushButton *deleteEntryPushButton = new QPushButton(this);
  96. QStackedWidget *stackedWidget = new QStackedWidget(this);
  97. QLineEdit *filterLineEdit = new QLineEdit(this);
  98. QComboBox *filterSelectionComboBox = new QComboBox(this);
  99. private:
  100. void setupHorizontalUI();
  101. void setupVerticalUI();
  102. void setupSignalsAndSlots();
  103. void setupFilterWidget();
  104. void setupButtonWidget();
  105. private slots:
  106. void addEntryRequested();
  107. void editEntryRequested(const QModelIndex &selectedIndex);
  108. void deleteEntryRequested();
  109. void sortColumnChanged(int newSortColumn);
  110. virtual void filterTextChanged(const QString &filterString) = 0;
  111. public slots:
  112. void databaseContentChanged();
  113. };
  114. #endif // TABLEEDITWIDGET_H