#ifndef RECORDPANEL_H #define RECORDPANEL_H #include #include #include class QAbstractItemModel; class QEvent; class QToolButton; /** Shows the values of one row of a model as a scrollable list, one line per column. * Values are edited through the model so the pending change logic of the model is reused. */ class RecordPanel : public QScrollArea { Q_OBJECT public: explicit RecordPanel(QWidget *parent = nullptr); /// Sets the model to show, the columns from firstDataColumn on are shown void setModel(QAbstractItemModel *model); /// Columns before this one are skipped, e.g. the row number column of the CRUD model void setFirstDataColumn(int column); /// Shows the values of the given row of the model, -1 shows nothing void setCurrentRow(int row); protected: bool eventFilter(QObject *watched, QEvent *event) override; private slots: void refresh(); void onModelReset(); private: struct FieldRow { int column = -1; QString original; ///< Text the editor shows, only a different text is written back QWidget *editor = nullptr; QToolButton *expandButton = nullptr; }; void clearFields(); void rebuild(); void showValue(FieldRow &field, const QString &text, bool editable); void commitEditor(QWidget *editor); FieldRow *findField(QWidget *editor); QAbstractItemModel *m_model = nullptr; int m_firstDataColumn = 0; QPersistentModelIndex m_rowIndex; std::vector m_fields; bool m_updating = false; }; #endif // RECORDPANEL_H