eeschema: Iterate through plugins when determining file format.

This commit is contained in:
Maciej Suminski 2017-06-26 13:13:43 +02:00
parent c274a21548
commit d6383893a2
5 changed files with 65 additions and 8 deletions

View File

@ -315,17 +315,24 @@ bool SCH_EDIT_FRAME::OpenProjectFiles( const std::vector<wxString>& aFileSet, in
{
delete g_RootSheet; // Delete the current project.
g_RootSheet = NULL; // Force CreateScreens() to build new empty project on load failure.
SCH_PLUGIN::SCH_PLUGIN_RELEASER pi;
SCH_PLUGIN::SCH_PLUGIN_RELEASER pi( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_LEGACY ) );
// cycle through plugins as they are added to Eeschema
if( !pi->CheckHeader( fullFileName ) )
// Iterate through the available plugins to determine the file type
for( auto pluginType : SCH_IO_MGR::SCH_FILE_T_vector )
{
pi.set( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_EAGLE ) );
pi.set( SCH_IO_MGR::FindPlugin( pluginType ) );
if( pi && pi->CheckHeader( fullFileName ) )
break; // got the right plugin
else
pi.set( nullptr ); // do not give a false impression that we have a valid plugin
}
try
{
if( !pi )
THROW_IO_ERROR( _( "File format not recognized" ) );
g_RootSheet = pi->Load( fullFileName, &Kiway() );
m_CurrentSheet->clear();
m_CurrentSheet->push_back( g_RootSheet );

View File

@ -170,3 +170,6 @@ void SCH_IO_MGR::Save( SCH_FILE_T aFileType, const wxString& aFileName,
THROW_IO_ERROR( wxString::Format( FMT_NOTFOUND, ShowType( aFileType ).GetData() ) );
}
DECLARE_ENUM_VECTOR( SCH_IO_MGR, SCH_FILE_T );

View File

@ -26,6 +26,7 @@
#include <richio.h>
#include <import_export.h>
#include <map>
#include <enum_vector.h>
class SCH_SHEET;
@ -50,7 +51,7 @@ public:
* A set of file types that the #SCH_IO_MGR knows about, and for which there
* has been a plugin written.
*/
enum SCH_FILE_T
DEFINE_ENUM_VECTOR( SCH_FILE_T,
{
SCH_LEGACY, ///< Legacy Eeschema file formats prior to s-expression.
SCH_KICAD, ///< The s-expression version of the schematic file formats.
@ -59,7 +60,7 @@ public:
// ALTIUM,
// etc.
};
} );
/**
* Return a #SCH_PLUGIN which the caller can use to import, export, save, or load

View File

@ -124,7 +124,7 @@ public:
const PROPERTIES* aProperties = NULL ) override;
void SaveLibrary( const wxString& aLibraryPath, const PROPERTIES* aProperties = NULL ) override;
bool CheckHeader(const wxString& aFileName) override;
bool CheckHeader( const wxString& aFileName ) override;
private:
void loadHierarchy( SCH_SHEET* aSheet );

46
include/enum_vector.h Normal file
View File

@ -0,0 +1,46 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2017 CERN
* @author Maciej Suminski <maciej.suminski@cern.ch>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef DEFINE_ENUM_VECTOR
/**
* Macro to create const vectors containing enum values to enable easy iteration.
*
* Usage:
* [header]
* class A {
* DEFINE_ENUM_VECTOR( COLORS, { RED, GREEN, BLUE } );
* };
*
* [source]
* for( auto color : A::COLORS_vector ) {
* // do sth with color
* }
*
* DECLARE_ENUM_VECTOR( COLORS );
*/
#define DEFINE_ENUM_VECTOR(enum_name, ...) \
enum enum_name __VA_ARGS__; \
static constexpr enum_name enum_name##_vector[] = __VA_ARGS__;
#define DECLARE_ENUM_VECTOR(class_name, enum_name) \
constexpr class_name::enum_name class_name::enum_name##_vector[];
#endif