Esempio n. 1
0
        public void ProjectItemsEvents_ItemAdded(ProjectItem projectItem)
        {
            Project   project = HelperFunctions.GetSelectedQtProject(VSPackage.dte);
            QtProject qtPro   = QtProject.Create(project);

            if (!HelperFunctions.IsQtProject(project))
            {
                return;
            }
            VCFilter filter = null;
            VCFile   vcFile = GetVCFileFromProject(projectItem.Name, qtPro.VCProject);

            if (vcFile == null)
            {
                return;
            }

            try
            {
                // Try to find the filter, the file is located in
                // If the file is not inside any filter, move it to
                // the according one, used by the Add-in
                filter = (VCFilter)vcFile.Parent;
            }
            catch { }

            try
            {
                FakeFilter ui  = Filters.FormFiles();
                FakeFilter qrc = Filters.ResourceFiles();
                FakeFilter ts  = Filters.TranslationFiles();
                FakeFilter h   = Filters.HeaderFiles();
                FakeFilter src = Filters.SourceFiles();

                VCFilter uiFilter  = qtPro.FindFilterFromGuid(ui.UniqueIdentifier);
                VCFilter tsFilter  = qtPro.FindFilterFromGuid(ts.UniqueIdentifier);
                VCFilter qrcFilter = qtPro.FindFilterFromGuid(qrc.UniqueIdentifier);
                VCFilter hFilter   = qtPro.FindFilterFromGuid(h.UniqueIdentifier);
                VCFilter srcFilter = qtPro.FindFilterFromGuid(src.UniqueIdentifier);

                if (HelperFunctions.HasSourceFileExtension(vcFile.Name))
                {
                    if (vcFile.Name.ToLower().StartsWith("moc_"))
                    {
                        return;
                    }
                    else if (vcFile.Name.ToLower().StartsWith("qrc_"))
                    {
                        // Do not use precompiled headers with these files
                        QtProject.SetPCHOption(vcFile, pchOption.pchNone);
                        return;
                    }
                    string pcHeaderThrough = qtPro.GetPrecompiledHeaderThrough();
                    if (pcHeaderThrough != null)
                    {
                        string pcHeaderCreator = pcHeaderThrough.Remove(pcHeaderThrough.LastIndexOf('.')) + ".cpp";
                        if (vcFile.Name.ToLower().EndsWith(pcHeaderCreator.ToLower()) &&
                            HelperFunctions.CxxFileContainsNotCommented(vcFile, "#include \"" + pcHeaderThrough + "\"", false, false))
                        {
                            //File is used to create precompiled headers
                            QtProject.SetPCHOption(vcFile, pchOption.pchCreateUsingSpecific);
                            return;
                        }
                    }
                    if (filter == null && !HelperFunctions.IsInFilter(vcFile, src))
                    {
                        if (null == srcFilter && qtPro.VCProject.CanAddFilter(src.Name))
                        {
                            srcFilter                  = (VCFilter)qtPro.VCProject.AddFilter(src.Name);
                            srcFilter.Filter           = src.Filter;
                            srcFilter.ParseFiles       = src.ParseFiles;
                            srcFilter.UniqueIdentifier = src.UniqueIdentifier;
                        }
                        qtPro.RemoveItem(projectItem);
                        qtPro.AddFileToProject(vcFile.FullPath, src);
                    }
                    if (HelperFunctions.HasQObjectDeclaration(vcFile))
                    {
//#if (VS2010 || VS2012 || VS2013 || VS2015)
                        HelperFunctions.EnsureCustomBuildToolAvailable(projectItem);
//#endif
                        qtPro.AddMocStep(vcFile);
                    }
                }
                else if (HelperFunctions.HasHeaderFileExtension(vcFile.Name))
                {
                    if (vcFile.Name.ToLower().StartsWith("ui_"))
                    {
                        return;
                    }
                    if (filter == null && !HelperFunctions.IsInFilter(vcFile, h))
                    {
                        if (null == hFilter && qtPro.VCProject.CanAddFilter(h.Name))
                        {
                            hFilter                  = (VCFilter)qtPro.VCProject.AddFilter(h.Name);
                            hFilter.Filter           = h.Filter;
                            hFilter.ParseFiles       = h.ParseFiles;
                            hFilter.UniqueIdentifier = h.UniqueIdentifier;
                        }
                        qtPro.RemoveItem(projectItem);
                        qtPro.AddFileToProject(vcFile.FullPath, h);
                    }
                    if (HelperFunctions.HasQObjectDeclaration(vcFile))
                    {
//#if (VS2010 || VS2012 || VS2013 || VS2015)
                        HelperFunctions.EnsureCustomBuildToolAvailable(projectItem);
//#endif
                        qtPro.AddMocStep(vcFile);
                    }
                }
                else if (vcFile.Name.EndsWith(".ui"))
                {
                    if (filter == null && !HelperFunctions.IsInFilter(vcFile, ui))
                    {
                        if (null == uiFilter && qtPro.VCProject.CanAddFilter(ui.Name))
                        {
                            uiFilter                  = (VCFilter)qtPro.VCProject.AddFilter(ui.Name);
                            uiFilter.Filter           = ui.Filter;
                            uiFilter.ParseFiles       = ui.ParseFiles;
                            uiFilter.UniqueIdentifier = ui.UniqueIdentifier;
                        }
                        qtPro.RemoveItem(projectItem);
                        qtPro.AddFileToProject(vcFile.FullPath, ui);
                    }
//#if (VS2010 || VS2012 || VS2013 || VS2015)
                    HelperFunctions.EnsureCustomBuildToolAvailable(projectItem);
//#endif
                    qtPro.AddUic4BuildStep(vcFile);
                }
                else if (vcFile.Name.EndsWith(".qrc"))
                {
                    if (filter == null && !HelperFunctions.IsInFilter(vcFile, qrc))
                    {
                        if (null == qrcFilter && qtPro.VCProject.CanAddFilter(qrc.Name))
                        {
                            qrcFilter                  = (VCFilter)qtPro.VCProject.AddFilter(qrc.Name);
                            qrcFilter.Filter           = qrc.Filter;
                            qrcFilter.ParseFiles       = qrc.ParseFiles;
                            qrcFilter.UniqueIdentifier = qrc.UniqueIdentifier;
                        }
                        qtPro.RemoveItem(projectItem);
                        qtPro.AddFileToProject(vcFile.FullPath, qrc);
                    }
//#if (VS2010 || VS2012 || VS2013 || VS2015)
                    HelperFunctions.EnsureCustomBuildToolAvailable(projectItem);
//#endif
                    qtPro.UpdateRccStep(vcFile, null);
                }
                else if (HelperFunctions.IsTranslationFile(vcFile))
                {
                    if (filter == null && !HelperFunctions.IsInFilter(vcFile, ts))
                    {
                        if (null == tsFilter && qtPro.VCProject.CanAddFilter(ts.Name))
                        {
                            tsFilter                  = (VCFilter)qtPro.VCProject.AddFilter(ts.Name);
                            tsFilter.Filter           = ts.Filter;
                            tsFilter.ParseFiles       = ts.ParseFiles;
                            tsFilter.UniqueIdentifier = ts.UniqueIdentifier;
                        }
                        qtPro.RemoveItem(projectItem);
                        qtPro.AddFileToProject(vcFile.FullPath, ts);
                    }
                }
            }
            catch { }

            return;
        }
Esempio n. 2
0
        public void DocumentSaved(EnvDTE.Document document)
        {
            QtProject qtPro = QtProject.Create(document.ProjectItem.ContainingProject);

            if (!HelperFunctions.IsQtProject(qtPro.VCProject))
            {
                return;
            }

            VCFile file = (VCFile)((IVCCollection)qtPro.VCProject.Files).Item(document.FullName);

            if (file.Extension == ".ui")
            {
                if (QtVSIPSettings.AutoUpdateUicSteps() && !QtProject.HasUicStep(file))
                {
                    qtPro.AddUic4BuildStep(file);
                }
                return;
            }

            if (!HelperFunctions.HasSourceFileExtension(file.Name) && !HelperFunctions.HasHeaderFileExtension(file.Name))
            {
                return;
            }

            if (HelperFunctions.HasQObjectDeclaration(file))
            {
                if (!qtPro.HasMocStep(file))
                {
                    qtPro.AddMocStep(file);
                }
            }
            else
            {
                qtPro.RemoveMocStep(file);
            }

            if (HelperFunctions.HasSourceFileExtension(file.Name))
            {
                string moccedFileName = "moc_" + file.Name;

                if (qtPro.IsMoccedFileIncluded(file))
                {
                    // exclude moc_foo.cpp from build
//#if (VS2012 || VS2013 || VS2015)
                    // Code copied here from 'GetFilesFromProject'
                    // For some reason error CS1771 was generated from function call
                    List <VCFile> tmpList = new System.Collections.Generic.List <VCFile>();
                    moccedFileName = HelperFunctions.NormalizeRelativeFilePath(moccedFileName);

                    FileInfo fi = new FileInfo(moccedFileName);
                    foreach (VCFile f in (IVCCollection)qtPro.VCProject.Files)
                    {
                        if (f.Name.ToLower() == fi.Name.ToLower())
                        {
                            tmpList.Add(f);
                        }
                    }
                    foreach (VCFile moccedFile in tmpList)
                    {
                        QtProject.ExcludeFromAllBuilds(moccedFile);
                    }
//#else
///                    foreach (VCFile moccedFile in qtPro.GetFilesFromProject(moccedFileName))
//                        QtProject.ExcludeFromAllBuilds(moccedFile);
//#endif
                }
                else
                {
                    // make sure that moc_foo.cpp isn't excluded from build
//#if (VS2012 || VS2013 || VS2015)
                    // Code copied here from 'GetFilesFromProject'
                    // For some reason error CS1771 was generated from function call
                    List <VCFile> moccedFiles = new System.Collections.Generic.List <VCFile>();
                    moccedFileName = HelperFunctions.NormalizeRelativeFilePath(moccedFileName);

                    FileInfo fi = new FileInfo(moccedFileName);
                    foreach (VCFile f in (IVCCollection)qtPro.VCProject.Files)
                    {
                        if (f.Name.ToLower() == fi.Name.ToLower())
                        {
                            moccedFiles.Add(f);
                        }
                    }
//#else
//                    List<VCFile> moccedFiles = qtPro.GetFilesFromProject(moccedFileName);
//#endif
                    if (moccedFiles.Count > 0)
                    {
                        bool     hasDifferentMocFilesPerConfig   = QtVSIPSettings.HasDifferentMocFilePerConfig(qtPro.Project);
                        bool     hasDifferentMocFilesPerPlatform = QtVSIPSettings.HasDifferentMocFilePerPlatform(qtPro.Project);
                        VCFilter generatedFiles = qtPro.FindFilterFromGuid(Filters.GeneratedFiles().UniqueIdentifier);
                        foreach (VCFile fileInFilter in (IVCCollection)generatedFiles.Files)
                        {
                            if (fileInFilter.Name == moccedFileName)
                            {
                                foreach (VCFileConfiguration config in (IVCCollection)fileInFilter.FileConfigurations)
                                {
                                    bool            exclude  = true;
                                    VCConfiguration vcConfig = config.ProjectConfiguration as VCConfiguration;
                                    if (hasDifferentMocFilesPerConfig && hasDifferentMocFilesPerPlatform)
                                    {
                                        VCPlatform platform     = vcConfig.Platform as VCPlatform;
                                        string     platformName = platform.Name;
                                        if (fileInFilter.RelativePath.ToLower().Contains(vcConfig.ConfigurationName.ToLower()) &&
                                            fileInFilter.RelativePath.ToLower().Contains(platform.Name.ToLower()))
                                        {
                                            exclude = false;
                                        }
                                    }
                                    else if (hasDifferentMocFilesPerConfig)
                                    {
                                        if (fileInFilter.RelativePath.ToLower().Contains(vcConfig.ConfigurationName.ToLower()))
                                        {
                                            exclude = false;
                                        }
                                    }
                                    else if (hasDifferentMocFilesPerPlatform)
                                    {
                                        VCPlatform platform     = vcConfig.Platform as VCPlatform;
                                        string     platformName = platform.Name;
                                        if (fileInFilter.RelativePath.ToLower().Contains(platformName.ToLower()))
                                        {
                                            exclude = false;
                                        }
                                    }
                                    else
                                    {
                                        exclude = false;
                                    }
                                    if (config.ExcludedFromBuild != exclude)
                                    {
                                        config.ExcludedFromBuild = exclude;
                                    }
                                }
                            }
                        }
                        foreach (VCFilter filt in (IVCCollection)generatedFiles.Filters)
                        {
                            foreach (VCFile f in (IVCCollection)filt.Files)
                            {
                                if (f.Name == moccedFileName)
                                {
                                    foreach (VCFileConfiguration config in (IVCCollection)f.FileConfigurations)
                                    {
                                        VCConfiguration vcConfig        = config.ProjectConfiguration as VCConfiguration;
                                        string          filterToLookFor = "";
                                        if (hasDifferentMocFilesPerConfig)
                                        {
                                            filterToLookFor = vcConfig.ConfigurationName;
                                        }
                                        if (hasDifferentMocFilesPerPlatform)
                                        {
                                            VCPlatform platform = vcConfig.Platform as VCPlatform;
                                            if (!string.IsNullOrEmpty(filterToLookFor))
                                            {
                                                filterToLookFor += '_';
                                            }
                                            filterToLookFor += platform.Name;
                                        }
                                        if (filt.Name == filterToLookFor)
                                        {
                                            if (config.ExcludedFromBuild)
                                            {
                                                config.ExcludedFromBuild = false;
                                            }
                                        }
                                        else
                                        {
                                            if (!config.ExcludedFromBuild)
                                            {
                                                config.ExcludedFromBuild = true;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }