diff options
author | Slávek Banko <slavek.banko@axis.cz> | 2019-12-16 23:07:15 +0100 |
---|---|---|
committer | Slávek Banko <slavek.banko@axis.cz> | 2019-12-17 17:02:33 +0100 |
commit | 2903268646b878941de5a0bd0cfbc5c74a538879 (patch) | |
tree | adbd590401aab003ed2e905eb316f53f84c86967 /parts | |
parent | 45c6d651358e9a288326a06a0b51c37bca9b1392 (diff) | |
download | tdevelop-2903268646b878941de5a0bd0cfbc5c74a538879.tar.gz tdevelop-2903268646b878941de5a0bd0cfbc5c74a538879.zip |
Fix crash on creating new file if a specific file type is selected
from the popup menu on the New file icon.
This relates to issue #4.
Signed-off-by: Slávek Banko <slavek.banko@axis.cz>
(cherry picked from commit ae5e2adcf39a6c7abbcd874e40e05bbbff428e2c)
Diffstat (limited to 'parts')
-rw-r--r-- | parts/filecreate/filecreate_filetype.h | 10 | ||||
-rw-r--r-- | parts/filecreate/filecreate_part.cpp | 37 | ||||
-rw-r--r-- | parts/filecreate/filecreate_part.h | 11 |
3 files changed, 48 insertions, 10 deletions
diff --git a/parts/filecreate/filecreate_filetype.h b/parts/filecreate/filecreate_filetype.h index 4b050708..e2d2a279 100644 --- a/parts/filecreate/filecreate_filetype.h +++ b/parts/filecreate/filecreate_filetype.h @@ -40,10 +40,13 @@ public: bool enabled() const { return m_enabled; } void setSubtypesEnabled(bool enabled = true); - + void addSubtype(const FileType * subtype) { m_subtypes.append(subtype); } TQPtrList<FileType> subtypes() const { return m_subtypes; } - + + void setId(int id) { m_id = id; } + int id() const { return m_id; } + private: TQString m_name; TQString m_ext; @@ -53,9 +56,10 @@ private: TQString m_descr; bool m_enabled; - + TQPtrList<FileType> m_subtypes; + int m_id; }; } diff --git a/parts/filecreate/filecreate_part.cpp b/parts/filecreate/filecreate_part.cpp index 371e3733..40c8e2c0 100644 --- a/parts/filecreate/filecreate_part.cpp +++ b/parts/filecreate/filecreate_part.cpp @@ -137,7 +137,7 @@ void FileCreatePart::slotAboutToShowNewPopupMenu() TDEIcon::DefaultState, NULL, true); m_newPopupMenu->insertItem(iconPix, filetype->name(), this, TQT_SLOT(slotNewFilePopup(int)), 0, ++id ); - m_newPopupMenu->setItemParameter( id, (long)filetype ); + m_newPopupMenu->setItemParameter( id, filetype->id() ); } else { TDEPopupMenu* subMenu = NULL; @@ -152,7 +152,7 @@ void FileCreatePart::slotAboutToShowNewPopupMenu() TDEIcon::DefaultState, NULL, true); subMenu->insertItem(iconPix, subtype->name(), this, TQT_SLOT(slotNewFilePopup(int)), 0, ++id ); - subMenu->setItemParameter( id, (long)subtype ); + subMenu->setItemParameter( id, subtype->id() ); } } if( subMenu ) @@ -172,9 +172,9 @@ void FileCreatePart::slotAboutToShowNewPopupMenu() } } -void FileCreatePart::slotNewFilePopup( int pFileType ) +void FileCreatePart::slotNewFilePopup( int fileTypeId ) { - const FileType* filetype = (const FileType*) pFileType; + const FileType* filetype = getType(fileTypeId); slotFiletypeSelected( filetype ); } @@ -193,10 +193,13 @@ void FileCreatePart::slotProjectOpened() { void FileCreatePart::addFileType(const TQString & filename) { FileType * filetype = getType(filename); if (!filetype) { + FileType* lastFiletype = m_filetypes.last(); + int lastTypeId = (lastFiletype && lastFiletype->id() < 0 ? lastFiletype->id() : 0); filetype = new FileType; filetype->setName( filename + " files" ); filetype->setExt( filename ); filetype->setCreateMethod("template"); + filetype->setId(--lastTypeId); m_filetypes.append(filetype); } filetype->setEnabled(true); @@ -212,7 +215,9 @@ void FileCreatePart::slotFiletypeSelected(const FileType * filetype) { KDevCreateFile::CreatedFile createdFile = createNewFile(filetype->ext(), TQString(), TQString(), - filetype->subtypeRef()); + filetype + ? filetype->subtypeRef() + : TQString()); openCreatedFile(createdFile); } @@ -228,6 +233,7 @@ void FileCreatePart::openCreatedFile(const KDevCreateFile::CreatedFile & created int FileCreatePart::readTypes(const TQDomDocument & dom, TQPtrList<FileType> &m_filetypes, bool enable) { int numRead = 0; + int typeId = 0; TQDomElement fileTypes = DomUtil::elementByPath(dom,"/kdevfilecreate/filetypes"); if (!fileTypes.isNull()) { for(TQDomNode node = fileTypes.firstChild();!node.isNull();node=node.nextSibling()) { @@ -242,6 +248,7 @@ int FileCreatePart::readTypes(const TQDomDocument & dom, TQPtrList<FileType> &m_ filetype->setIcon( element.attribute("icon") ); filetype->setDescr( (DomUtil::namedChildElement(element, "descr")).text() ); filetype->setEnabled(enable || (filetype->ext()=="")); + filetype->setId(++typeId); m_filetypes.append(filetype); numRead++; @@ -260,6 +267,7 @@ int FileCreatePart::readTypes(const TQDomDocument & dom, TQPtrList<FileType> &m_ subtype->setName( subelement.attribute("name") ); subtype->setDescr( (DomUtil::namedChildElement(subelement, "descr")).text() ); subtype->setEnabled(enable); + subtype->setId(++typeId); filetype->addSubtype(subtype); } } @@ -297,6 +305,25 @@ FileType * FileCreatePart::getType(const TQString & ex, const TQString subtRef) return NULL; } +FileType * FileCreatePart::getType(int id) { + + TQPtrList<FileType> filetypes = getFileTypes(); + for(FileType* filetype = filetypes.first(); + filetype; + filetype = filetypes.next()) + { + if (filetype->id() == id) return filetype; + TQPtrList<FileType> subtypes = filetype->subtypes(); + for(FileType* subtype = subtypes.first(); + subtype; + subtype = subtypes.next()) + { + if (subtype->id() == id) return subtype; + } + } + return NULL; +} + FileType * FileCreatePart::getEnabledType(const TQString & ex, const TQString subtRef) { TQString subtypeRef = subtRef; diff --git a/parts/filecreate/filecreate_part.h b/parts/filecreate/filecreate_part.h index 9d090493..ab870791 100644 --- a/parts/filecreate/filecreate_part.h +++ b/parts/filecreate/filecreate_part.h @@ -68,6 +68,13 @@ public: */ FileType * getType(const TQString & ext, const TQString subtype = TQString()); /** + * Finds the file type object for a given file type or subtype ID. + * IDs for file types are not persistent. ID is a sequence number assigned when + * reading file type definitions from an XML file. Negative numbers are assigned + * for custom file types. + */ + FileType * getType(int id); + /** * Finds the file type object for a given extension and optionally subtype. * You can omit the subtype and specify the extension as ext-subtype if you wish. * Returns only enabled type (i.e. used in the project). @@ -93,9 +100,9 @@ public slots: /** * Called from TDEToolBarPopupMenu to request a new file action - * @param pFileType is acutally a pointer to FileType + * @param fileTypeId is a sequence number that identifies a particular FileType */ - void slotNewFilePopup(int pFileType); + void slotNewFilePopup(int fileTypeId); protected slots: void slotNoteFiletype(const FileType * filetype); |