File indexing completed on 2024-05-12 04:37:48

0001 /*
0002     SPDX-FileCopyrightText: 2007 Alexander Dymo <adymo@kdevelop.org>
0003     SPDX-FileCopyrightText: 2012 Miha Čančula <miha@noughmad.eu>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #ifndef KDEVPLATFORM_TEMPLATESMODEL_H
0009 #define KDEVPLATFORM_TEMPLATESMODEL_H
0010 
0011 #include <QStandardItemModel>
0012 
0013 #include <language/languageexport.h>
0014 
0015 namespace KDevelop {
0016 class TemplatesModelPrivate;
0017 
0018 /**
0019  * @brief A convenience class for loading templates using .kdevtemplate files
0020  *
0021  * It loads template archives, extracts and stores their description files, and
0022  * displays them as a three-level tree structure.
0023  *
0024  * The locations for loading and storing files are determined by the typePrefix.
0025  * We use QStandardPaths with the GenericData type and create a filter string as such:
0026  * \li templates: typePrefix "/templates/"
0027  * \li descriptions: typePrefix "/template_descriptions/"
0028  *
0029  * Preview images are directly read from the archives on demand (or for legacy
0030  * template installations from typePrefix "/template_previews/").
0031  *
0032  * @sa ITemplateProvider::templatesModel()
0033  **/
0034 class KDEVPLATFORMLANGUAGE_EXPORT TemplatesModel
0035     : public QStandardItemModel
0036 {
0037     Q_OBJECT
0038 
0039 public:
0040 
0041     /**
0042      * Extra roles for template-specific properties
0043      * @sa Qt::ItemDataRole
0044      **/
0045     enum TemplateRole
0046     {
0047         DescriptionFileRole = Qt::UserRole + 1, ///< Template description file name
0048         PreviewIconRole = Qt::UserRole + 2, ///< Template preview icon, provides a TemplatePreviewIcon
0049         CommentRole = Qt::UserRole + 3, ///< Template comment
0050         ArchiveFileRole = Qt::UserRole + 4 ///< Template archive file name
0051     };
0052 
0053     /**
0054      * Creates a new templates model
0055      *
0056      * @param typePrefix the type prefix used to determine resource locations.
0057      * @param parent parent object, defaults to 0.
0058      **/
0059     explicit TemplatesModel(const QString& typePrefix, QObject* parent = nullptr);
0060 
0061     /**
0062      * Destructor
0063      *
0064      **/
0065     ~TemplatesModel() override;
0066 
0067     /**
0068      * Reloads all found templates
0069      **/
0070     virtual void refresh();
0071 
0072     /**
0073      * Loads template @p fileName and save it to the template storage directory.
0074      *
0075      * If the file is an archive, the whole archive will be copied.
0076      * If the file has a .desktop or .kdevtemplate suffix, the contents
0077      * of its containing directory will be archived and saved.
0078      **/
0079     QString loadTemplateFile(const QString& fileName);
0080 
0081     /**
0082      * Finds the model index of the template file @p fileName.
0083      *
0084      * For convenience, this function returns the found template index, as well as all of its ancestors.
0085      * The indexes are ordered from the top-level ancestor to the actual index of the template.
0086      * This is useful for managing selection when multiple views display different level of the model.
0087      *
0088      * @param fileName the template file name
0089      **/
0090     QModelIndexList templateIndexes(const QString& fileName) const;
0091 
0092     /**
0093      * Returns the type prefix used to find a template resource.
0094      **/
0095     QString typePrefix() const;
0096 
0097     /**
0098      * The model will include @p path during the search for template archives
0099      *
0100      * @param path Path to a directory that contains normal user data. The template model will search for a kdevappwizard/templates (or your model name prefix) directory
0101      * inside @p path and will use them. Please note that the path has to end with a '/'.
0102      */
0103     void addDataPath(const QString& path);
0104 
0105 private:
0106     const QScopedPointer<class TemplatesModelPrivate> d_ptr;
0107     Q_DECLARE_PRIVATE(TemplatesModel)
0108 };
0109 }
0110 
0111 #endif // KDEVPLATFORM_TEMPLATESMODEL_H