qlistobject.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. Copyright 2017 Herik Lima de Castro and Marcelo Medeiros Eler
  3. Distributed under MIT license, or public domain if desired and
  4. recognized in your jurisdiction.
  5. See file LICENSE for detail.
  6. */
  7. #ifndef QLISTOBJECT_H
  8. #define QLISTOBJECT_H
  9. #include <QObject>
  10. #include "cppwebframework_global.h"
  11. CWF_BEGIN_NAMESPACE
  12. /**
  13. * @brief The QListObject class is used to pass a list of object to a view page.
  14. * NOTE: Always when you need to pass a list of object to a view page you will need to use this class,
  15. * your class need to inherit from the QObject class and all the methods needs to be in the public slots session.
  16. */
  17. class CPPWEBFRAMEWORKSHARED_EXPORT QListObject : public QObject
  18. {
  19. Q_OBJECT
  20. private:
  21. bool autoDelete = false;
  22. public:
  23. /**
  24. * @brief This constructor can receive a parent.
  25. * @param QObject *parent : Parent.
  26. */
  27. explicit QListObject(QObject *parent = nullptr) : QObject(parent) {}
  28. /**
  29. * @brief Contructs a QListObject with N elements.
  30. */
  31. explicit QListObject(QObject *parent, const std::initializer_list<QObject *> &objects) : QObject(parent) { add(objects); }
  32. /**
  33. * @brief Contructs a QListObject with N elements.
  34. */
  35. explicit QListObject(const std::initializer_list<QObject *> &objects) : QObject(nullptr) { add(objects); }
  36. /**
  37. * @brief Destructor.
  38. */
  39. ~QListObject();
  40. /**
  41. * @brief This is an operator overload and returns a QObject given an specific index.
  42. * @param index : This is an integer value.
  43. * @return QObject *
  44. */
  45. inline QObject *operator [](int index) const { return children()[index]; }
  46. /**
  47. * @brief This method returns the number of elements in this QListObject.
  48. * @return int
  49. */
  50. inline int size() const { return children().count(); }
  51. /**
  52. * @brief This method add a new QObject to the list.
  53. * @param QObject *object : Object.
  54. */
  55. inline void add(QObject *object) { object->setParent(this); }
  56. /**
  57. * @brief This method add a N new QObjects to the list.
  58. */
  59. inline void add(const std::initializer_list<QObject *> &objects)
  60. {
  61. std::for_each(objects.begin(), objects.end(), [&](QObject *o){ o->setParent(this); });
  62. }
  63. /**
  64. * @brief This method remove and object from the list.
  65. * @param o
  66. */
  67. inline void remove(QObject *object) { object->setParent(nullptr); }
  68. /**
  69. * @brief getAutoDelete
  70. * @return
  71. */
  72. inline bool getAutoDelete() const { return autoDelete; }
  73. /**
  74. * @brief setAutoDelete
  75. * @param value
  76. */
  77. inline void setAutoDelete(bool value) { autoDelete = value; }
  78. };
  79. CWF_END_NAMESPACE
  80. #endif // QLISTOBJECT_H