File indexing completed on 2024-06-09 05:39:57

0001 /***************************************************************************
0002  *   Copyright (C) 2003-2005 by David Saxton                               *
0003  *   david@bluehaze.org                                                    *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  ***************************************************************************/
0010 
0011 #ifndef PROJECTMANAGER_H
0012 #define PROJECTMANAGER_H
0013 
0014 #include "itemselector.h"
0015 
0016 #include <QList>
0017 #include <QPointer>
0018 #include <QUrl>
0019 
0020 class Document;
0021 class ILVItem;
0022 class KTechlab;
0023 class ProcessOptions;
0024 class ProjectInfo;
0025 class ProjectItem;
0026 class ProjectManager;
0027 class QDomDocument;
0028 class QDomElement;
0029 class QStringList;
0030 namespace KateMDI
0031 {
0032 class ToolView;
0033 }
0034 
0035 typedef QList<ProcessOptions> ProcessOptionsList;
0036 typedef QList<QPointer<ProjectItem>> ProjectItemList;
0037 
0038 class LinkerOptions
0039 {
0040 public:
0041     LinkerOptions();
0042 
0043     class HexFormat
0044     {
0045     public:
0046         enum type { inhx32, inhx8m, inhx8s, inhx16 };
0047     };
0048 
0049     HexFormat::type hexFormat() const
0050     {
0051         return m_hexFormat;
0052     }
0053     void setHexFormat(HexFormat::type hexFormat)
0054     {
0055         m_hexFormat = hexFormat;
0056     }
0057 
0058     bool outputMapFile() const
0059     {
0060         return m_bOutputMapFile;
0061     }
0062     void setOutputMapFile(bool outputMapFile)
0063     {
0064         m_bOutputMapFile = outputMapFile;
0065     }
0066 
0067     QString libraryDir() const
0068     {
0069         return m_libraryDir;
0070     }
0071     void setLibraryDir(const QString &libraryDir)
0072     {
0073         m_libraryDir = libraryDir;
0074     }
0075 
0076     QString linkerScript() const
0077     {
0078         return m_linkerScript;
0079     }
0080     void setLinkerScript(const QString &linkerScript)
0081     {
0082         m_linkerScript = linkerScript;
0083     }
0084 
0085     QString linkerOther() const
0086     {
0087         return m_other;
0088     }
0089     void setLinkerOther(const QString &other)
0090     {
0091         m_other = other;
0092     }
0093 
0094     /**
0095      * Used for linkable ProjectItems. Returns a list of urls of files
0096      * inside the project to link against. Each url is relative to the
0097      * project directory.
0098      */
0099     QStringList linkedInternal() const
0100     {
0101         return m_linkedInternal;
0102     }
0103     void setLinkedInternal(const QStringList &linkedInternal)
0104     {
0105         m_linkedInternal = linkedInternal;
0106     }
0107 
0108     /**
0109      * Used for linkable ProjectItems. Returns a list of urls of files
0110      * outside the project to link against. Each url is absolute.
0111      */
0112     QStringList linkedExternal() const
0113     {
0114         return m_linkedExternal;
0115     }
0116     void setLinkedExternal(const QStringList &linkedExternal)
0117     {
0118         m_linkedExternal = linkedExternal;
0119     }
0120 
0121     /**
0122      * @param baseDirUrl url of the directory of the project
0123      */
0124     QDomElement toDomElement(QDomDocument &doc, const QUrl &baseDirUrl) const;
0125 
0126     static QString hexFormatToString(HexFormat::type format);
0127     static HexFormat::type stringToHexFormat(const QString &hexFormat);
0128 
0129 protected:
0130     /**
0131      * @param baseDirUrl url of the directory of the project
0132      */
0133     void domElementToLinkerOptions(const QDomElement &element, const QUrl &baseDirUrl);
0134 
0135     QStringList m_linkedInternal;
0136     QStringList m_linkedExternal;
0137     HexFormat::type m_hexFormat;
0138     bool m_bOutputMapFile;
0139     QString m_libraryDir;
0140     QString m_linkerScript;
0141     QString m_other;
0142 };
0143 
0144 class ProcessingOptions
0145 {
0146 public:
0147     ProcessingOptions();
0148     virtual ~ProcessingOptions();
0149 
0150     /**
0151      * Sets the output url that this item will be built into (if this is a
0152      * buildable item).
0153      * Only urls of local filesystems supported.
0154      */
0155     void setOutputURL(const QUrl &url)
0156     {
0157         m_outputURL = url;
0158     }
0159     QUrl outputURL() const
0160     {
0161         return m_outputURL;
0162     }
0163 
0164     /**
0165      * Set the microprocessor id that this project item is being built for
0166      * (when applicable).
0167      */
0168     virtual void setMicroID(const QString &id)
0169     {
0170         m_microID = id;
0171     }
0172     virtual QString microID() const
0173     {
0174         return m_microID;
0175     }
0176 
0177     /**
0178      * @param baseDirUrl url of the directory of the project
0179      */
0180     QDomElement toDomElement(QDomDocument &doc, const QUrl &baseDirUrl) const;
0181 
0182     void setUseParentMicroID(bool useParentMicroID)
0183     {
0184         m_bUseParentMicroID = useParentMicroID;
0185     }
0186     bool useParentMicroID() const
0187     {
0188         return m_bUseParentMicroID;
0189     }
0190 
0191 protected:
0192     /**
0193      * @param baseDirUrl url of the directory of the project
0194      */
0195     void domElementToProcessingOptions(const QDomElement &element, const QUrl &baseDirUrl);
0196 
0197     QUrl m_outputURL;
0198     QString m_microID;
0199     bool m_bUseParentMicroID;
0200 };
0201 
0202 /**
0203 @author David Saxton
0204 */
0205 class ProjectItem : public QObject, public LinkerOptions, public ProcessingOptions
0206 {
0207 public:
0208     enum Type { ProjectType = 1 << 0, FileType = 1 << 1, ProgramType = 1 << 2, LibraryType = 1 << 3 };
0209     enum { AllTypes = ProjectType | FileType | ProgramType | LibraryType };
0210 
0211     enum OutputType { ProgramOutput = 1 << 0, ObjectOutput = 1 << 1, LibraryOutput = 1 << 2, UnknownOutput = 1 << 3 };
0212     enum { AllOutputs = ProgramOutput | ObjectOutput | LibraryOutput | UnknownOutput };
0213 
0214     ProjectItem(ProjectItem *parent, Type type, ProjectManager *projectManager);
0215     ~ProjectItem() override;
0216 
0217     Type type() const
0218     {
0219         return m_type;
0220     }
0221     QString typeToString() const;
0222     static Type stringToType(const QString &type);
0223 
0224     void setILVItem(ILVItem *ilvItem);
0225 
0226     /**
0227      * Adds the child to the list of children, and creates an ILVItem for it
0228      * in the project tree view.
0229      */
0230     void addChild(ProjectItem *child);
0231     ProjectItemList children() const
0232     {
0233         return m_children;
0234     }
0235 
0236     void setObjectName(const QString &name);
0237     QString name() const
0238     {
0239         return m_name;
0240     }
0241 
0242     /**
0243      * Sets the (input) url that this project item refers to. If the output
0244      * url has not yet been set, then this project item will set the output
0245      * url based on this (input) url.
0246      */
0247     void setURL(const QUrl &url);
0248     QUrl url() const
0249     {
0250         return m_url;
0251     }
0252 
0253     OutputType outputType() const;
0254 
0255     /**
0256      * Returns a list of output urls of the children and their recursively
0257      * contained children (does not include the url for this project item).
0258      * @param types An OR'ed list of ProjectItem::Type values for the
0259      * children.
0260      * @param outputTypes An OR'ed list of ProjectItem::OutputType values
0261      * for the children.
0262      */
0263     QList<QUrl> childOutputURLs(unsigned types = AllTypes, unsigned outputTypes = AllOutputs) const;
0264 
0265     /**
0266      * Creates a new ProjectItem for the given url and adds it as a child.
0267      */
0268     void addFile(const QUrl &url);
0269     /**
0270      * Queries the user for a list of urls to add, and then calls addFile
0271      * for each url.
0272      */
0273     void addFiles();
0274 
0275     void addCurrentFile();
0276     bool closeOpenFiles();
0277     /**
0278      * @param baseDirUrl url of the directory of the project
0279      */
0280     QDomElement toDomElement(QDomDocument &doc, const QUrl &baseDirUrl) const;
0281 
0282     bool build(ProcessOptionsList *pol);
0283     void upload(ProcessOptionsList *pol);
0284 
0285     void setMicroID(const QString &id) override;
0286     QString microID() const override;
0287 
0288     /**
0289      * Searches this item and the children for an item for the given url,
0290      * return null if no such item could be found.
0291      */
0292     ProjectItem *findItem(const QUrl &url);
0293 
0294 protected:
0295     /**
0296      * @param baseDirUrl url of the directory of the project
0297      */
0298     void domElementToItem(const QDomElement &element, const QUrl &baseDirUrl);
0299     void updateILVItemPixmap();
0300     void updateControlChildMicroIDs();
0301 
0302     QUrl m_url;
0303     QString m_name;
0304     ProjectItemList m_children;
0305     Type m_type;
0306 
0307     QPointer<ILVItem> m_pILVItem;
0308     ProjectManager *m_pProjectManager;
0309     ProjectItem *m_pParent;
0310 };
0311 
0312 /**
0313 @author David Saxton
0314 */
0315 class ProjectInfo : public ProjectItem
0316 {
0317     Q_OBJECT
0318 
0319 public:
0320     ProjectInfo(ProjectManager *projectManager);
0321     ~ProjectInfo() override;
0322 
0323     /**
0324      * Returns the directory that the project is saved in
0325      */
0326     QString directory() const
0327     {
0328         return m_url.adjusted(QUrl::StripTrailingSlash).adjusted(QUrl::RemoveFilename).path();
0329     }
0330 
0331     /**
0332      * Saves the project information to file, and attempts to close all
0333      * open project files.
0334      * @return true iff succesful
0335      */
0336     bool saveAndClose();
0337     bool save();
0338 
0339     bool open(const QUrl &url);
0340 };
0341 
0342 /**
0343 @short Project Management
0344 @author David Saxton
0345 */
0346 class ProjectManager : public ItemSelector
0347 {
0348     Q_OBJECT
0349 public:
0350     ~ProjectManager() override;
0351     static ProjectManager *self(KateMDI::ToolView *parent = nullptr);
0352 
0353     static QString toolViewIdentifier()
0354     {
0355         return "ProjectManager";
0356     }
0357 
0358     /**
0359      * @return the currently open project, or nullptr if no project is open.
0360      */
0361     ProjectInfo *currentProject() const
0362     {
0363         return m_pCurrentProject;
0364     }
0365 
0366     void updateActions();
0367 
0368 signals:
0369     /**
0370      * Emitted when an existing project is opened.
0371      */
0372     void projectOpened();
0373     /**
0374      * Emitted when the current project is closed.
0375      */
0376     void projectClosed();
0377     /**
0378      * Emitted when a new project is created.
0379      */
0380     void projectCreated();
0381     /**
0382      * Emitted when a subproject is created.
0383      */
0384     void subprojectCreated();
0385     /**
0386      * Emitted when file(s) are added to the project or a subproject.
0387      */
0388     void filesAdded();
0389     /**
0390      * Emitted when file(s) are removed from the project or a subproject.
0391      */
0392     void filesRemoved();
0393 
0394 public slots:
0395     void slotNewProject();
0396     void slotOpenProject();
0397     void slotOpenProject(const QUrl &url);
0398     bool slotCloseProject();
0399     void slotCreateSubproject();
0400     void slotAddFile();
0401     void slotAddCurrentFile();
0402     void slotSubprojectAddExistingFile();
0403     void slotSubprojectAddCurrentFile();
0404     void slotItemBuild();
0405     void slotItemUpload();
0406     void slotItemProcessingOptions();
0407     void slotRemoveSelected();
0408     void slotExportToMakefile();
0409     void slotSubprojectLinkerOptions();
0410     /**
0411      * Pops ups a project configuration dialog
0412      */
0413     void slotProjectOptions();
0414 
0415 private slots:
0416     void slotContextMenuRequested(const QPoint &pos) override;
0417     /**
0418      * Called when a user clicks on any item in the project view
0419      */
0420     void slotItemClicked(QTreeWidgetItem *item, int);
0421 
0422 protected:
0423     ProjectInfo *m_pCurrentProject;
0424 
0425 private:
0426     ProjectManager(KateMDI::ToolView *parent);
0427     static ProjectManager *m_pSelf;
0428 };
0429 
0430 #endif