table_layout.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // This file is part of Desktop App Toolkit,
  2. // a set of libraries for developing nice desktop applications.
  3. //
  4. // For license and copyright information please follow this link:
  5. // https://github.com/desktop-app/legal/blob/master/LEGAL
  6. //
  7. #pragma once
  8. #include "base/object_ptr.h"
  9. #include "ui/rp_widget.h"
  10. namespace style {
  11. struct Table;
  12. } // namespace style
  13. namespace st {
  14. extern const style::Table &defaultTable;
  15. } // namespace st
  16. namespace Ui {
  17. class TableLayout : public RpWidget {
  18. public:
  19. TableLayout(QWidget *parent, const style::Table &st = st::defaultTable);
  20. [[nodiscard]] const style::Table &st() const {
  21. return _st;
  22. }
  23. [[nodiscard]] int rowsCount() const {
  24. return _rows.size();
  25. }
  26. [[nodiscard]] not_null<RpWidget*> labelAt(int index) const {
  27. Expects(index >= 0 && index < rowsCount());
  28. return _rows[index].label.data();
  29. }
  30. [[nodiscard]] not_null<RpWidget*> valueAt(int index) const {
  31. Expects(index >= 0 && index < rowsCount());
  32. return _rows[index].value.data();
  33. }
  34. void insertRow(
  35. int atPosition,
  36. object_ptr<RpWidget> &&label,
  37. object_ptr<RpWidget> &&value,
  38. const style::margins &labelMargin = style::margins(),
  39. const style::margins &valueMargin = style::margins());
  40. void addRow(
  41. object_ptr<RpWidget> &&label,
  42. object_ptr<RpWidget> &&value,
  43. const style::margins &labelMargin = style::margins(),
  44. const style::margins &valueMargin = style::margins()) {
  45. insertRow(
  46. rowsCount(),
  47. std::move(label),
  48. std::move(value),
  49. labelMargin,
  50. valueMargin);
  51. }
  52. void clear();
  53. protected:
  54. void paintEvent(QPaintEvent *e) override;
  55. int resizeGetHeight(int newWidth) override;
  56. void visibleTopBottomUpdated(
  57. int visibleTop,
  58. int visibleBottom) override;
  59. private:
  60. struct Row {
  61. object_ptr<RpWidget> label;
  62. object_ptr<RpWidget> value;
  63. style::margins labelMargin;
  64. style::margins valueMargin;
  65. mutable int top = 0;
  66. };
  67. [[nodiscard]] int rowVerticalSkip(const Row &row) const;
  68. void childHeightUpdated(RpWidget *child);
  69. void removeChild(RpWidget *child);
  70. void updateRowGeometry(const Row &row, int width, int top) const;
  71. void updateRowPosition(const Row &row, int width, int top) const;
  72. const style::Table &_st;
  73. std::vector<Row> _rows;
  74. int _valueLeft = 0;
  75. bool _inResize = false;
  76. rpl::lifetime _rowsLifetime;
  77. };
  78. } // namespace Ui