| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #ifndef FORMCATEGORYDETAILS_H |
| 4 | #define FORMCATEGORYDETAILS_H |
| 5 | |
| 6 | #include "3rd-party/boolinq/boolinq.h" |
| 7 | #include "definitions/definitions.h" |
| 8 | |
| 9 | #include <QDialog> |
| 10 | |
| 11 | namespace Ui { |
| 12 | class FormCategoryDetails; |
| 13 | } |
| 14 | |
| 15 | class Category; |
| 16 | class ServiceRoot; |
| 17 | class FeedsModel; |
| 18 | class RootItem; |
| 19 | class QMenu; |
| 20 | class QAction; |
| 21 | class MultiFeedEditCheckBox; |
| 22 | |
| 23 | class RSSGUARD_DLLSPEC FormCategoryDetails : public QDialog { |
| 24 | Q_OBJECT |
| 25 | |
| 26 | public: |
| 27 | explicit FormCategoryDetails(ServiceRoot* service_root, |
| 28 | RootItem* parent_to_select = nullptr, |
| 29 | QWidget* parent = nullptr); |
| 30 | virtual ~FormCategoryDetails(); |
| 31 | |
| 32 | template <class T> |
| 33 | QList<T*> addEditCategory(const QList<Category*>& cats_to_edit = {}); |
| 34 | |
| 35 | template <class T> |
| 36 | T* category() const; |
| 37 | |
| 38 | // Returns all cats. |
| 39 | template <class T> |
| 40 | QList<T*> categories() const; |
| 41 | |
| 42 | protected: |
| 43 | bool isChangeAllowed(MultiFeedEditCheckBox* mcb) const; |
| 44 | virtual void loadCategoryData(); |
| 45 | |
| 46 | protected slots: |
| 47 | virtual void apply(); |
| 48 | |
| 49 | private slots: |
| 50 | |
| 51 | // Trigerred when title/description changes. |
| 52 | void onTitleChanged(const QString& new_title); |
| 53 | void onDescriptionChanged(const QString& new_description); |
| 54 | |
| 55 | // Icon selectors. |
| 56 | void onLoadIconFromFile(); |
| 57 | void onUseDefaultIcon(); |
| 58 | |
| 59 | private: |
| 60 | void createConnections(); |
| 61 | void initialize(); |
| 62 | |
| 63 | // Loads categories into the dialog + give root "category" |
| 64 | // and make sure that no childs of input category (including) |
| 65 | // input category are loaded. |
| 66 | void loadCategories(const QList<Category*>& categories, RootItem* root_item, Category* input_category); |
| 67 | |
| 68 | private: |
| 69 | QScopedPointer<Ui::FormCategoryDetails> m_ui; |
| 70 | QList<Category*> m_categories; |
| 71 | ServiceRoot* m_serviceRoot; |
| 72 | QMenu* m_iconMenu{}; |
| 73 | QAction* m_actionLoadIconFromFile{}; |
| 74 | QAction* m_actionUseDefaultIcon{}; |
| 75 | RootItem* m_parentToSelect; |
| 76 | bool m_creatingNew; |
| 77 | bool m_isBatchEdit; |
| 78 | }; |
| 79 | |
| 80 | template <class T> |
| 81 | inline QList<T*> FormCategoryDetails::addEditCategory(const QList<Category*>& cats_to_edit) { |
| 82 | m_creatingNew = cats_to_edit.isEmpty(); |
| 83 | m_isBatchEdit = cats_to_edit.size() > 1; |
| 84 | |
| 85 | if (m_creatingNew) { |
| 86 | m_categories.append(new T()); |
| 87 | } |
| 88 | else { |
| 89 | m_categories.append(cats_to_edit); |
| 90 | } |
| 91 | |
| 92 | loadCategoryData(); |
| 93 | |
| 94 | if (exec() == QDialog::DialogCode::Accepted) { |
| 95 | return categories<T>(); |
| 96 | } |
| 97 | else { |
| 98 | return {}; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | template <class T> |
| 103 | inline T* FormCategoryDetails::category() const { |
| 104 | return qobject_cast<T*>(m_categories.first()); |
| 105 | } |
| 106 | |
| 107 | template <class T> |
| 108 | inline QList<T*> FormCategoryDetails::categories() const { |
| 109 | std::list<T*> std_cats = boolinq::from(m_categories) |
| 110 | .select([](Category* fd) { |
| 111 | return qobject_cast<T*>(fd); |
| 112 | }) |
| 113 | .toStdList(); |
| 114 | |
| 115 | return FROM_STD_LIST(QList<T*>, std_cats); |
| 116 | } |
| 117 | |
| 118 | #endif // FORMCATEGORYDETAILS_H |