KiCad PCB EDA Suite
Loading...
Searching...
No Matches
job.h
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2022 Mark Roszko <[email protected]>
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#pragma once
22
23#include <wx/string.h>
24
25#include <kicommon.h>
26#include <map>
28#include <lseq.h>
29#include <lset.h>
30#include <title_block.h>
31
32class PROJECT;
33
35{
36public:
37 JOB_PARAM_BASE( const std::string& aJsonPath );
38
39 virtual ~JOB_PARAM_BASE() = default;
40
41 virtual void FromJson( const nlohmann::json& j ) const = 0;
42
43 virtual void ToJson( nlohmann::json& j ) = 0;
44
45protected:
46 std::string m_jsonPath;
47};
48
49template <typename ValueType>
51{
52public:
53
54 JOB_PARAM( const std::string& aJsonPath, ValueType* aPtr,
55 ValueType aDefault ) :
56 JOB_PARAM_BASE( aJsonPath ), m_ptr( aPtr ), m_default( aDefault )
57 {
58 }
59
60 virtual void FromJson( const nlohmann::json& j ) const override
61 {
62 *m_ptr = j.value( m_jsonPath, m_default );
63 }
64
65 virtual void ToJson( nlohmann::json& j ) override { j[m_jsonPath] = *m_ptr; }
66
67protected:
68 ValueType* m_ptr;
69 ValueType m_default;
70};
71
72
73template <typename ListElementType>
75{
76public:
77 JOB_PARAM_LIST( const std::string& aJsonPath, std::vector<ListElementType>* aPtr,
78 std::vector<ListElementType> aDefault ) :
79 JOB_PARAM_BASE( aJsonPath ),
80 m_ptr( aPtr ),
81 m_default( std::move( aDefault ) )
82 { }
83
84 virtual void FromJson( const nlohmann::json& j ) const override
85 {
86 if( j.contains( m_jsonPath ) )
87 {
88 auto js = j.at( m_jsonPath );
89 std::vector<ListElementType> val;
90
91 if( js.is_array() )
92 {
93 for( const auto& el : js.items() )
94 val.push_back( el.value().template get<ListElementType>() );
95 }
96
97 *m_ptr = val;
98 }
99 else
100 *m_ptr = m_default;
101 }
102
103 void ToJson( nlohmann::json& j ) override
104 {
105 nlohmann::json js = nlohmann::json::array();
106
107 for( const auto& el : *m_ptr )
108 js.push_back( el );
109
110 j[m_jsonPath] = js;
111 }
112
113protected:
114 std::vector<ListElementType>* m_ptr;
115 std::vector<ListElementType> m_default;
116};
117
118
119class JOB_PARAM_LSEQ : public JOB_PARAM<LSEQ>
120{
121public:
122 JOB_PARAM_LSEQ( const std::string& aJsonPath, LSEQ* aPtr, LSEQ aDefault ) :
123 JOB_PARAM<LSEQ>( aJsonPath, aPtr, aDefault )
124 {
125 }
126
127 virtual void FromJson( const nlohmann::json& j ) const override
128 {
129 if( j.contains( m_jsonPath ) )
130 {
131 auto js = j.at( m_jsonPath );
132
133 LSEQ layers;
134 if( js.is_array() )
135 {
136 for( const nlohmann::json& layer : js )
137 {
138 if( layer.is_string() )
139 {
140 wxString name = layer.get<wxString>();
141 int layerId = LSET::NameToLayer( name );
142 if( layerId != UNDEFINED_LAYER )
143 layers.push_back( static_cast<PCB_LAYER_ID>( layerId ) );
144 }
145 else
146 {
147 int layerId = layer.get<int>();
148 if( layerId != UNDEFINED_LAYER )
149 layers.push_back( static_cast<PCB_LAYER_ID>( layerId ) );
150 }
151 }
152 }
153 *m_ptr = layers;
154 }
155 else
156 *m_ptr = m_default;
157 }
158
159 void ToJson( nlohmann::json& j ) override
160 {
161 nlohmann::json js = nlohmann::json::array();
162
163 for( PCB_LAYER_ID layer : ( *m_ptr ) )
164 js.push_back( LSET::Name( layer ) );
165
166 j[m_jsonPath] = js;
167 }
168};
169
171{
173
174 JOB_OUTPUT( wxString outputPath ) { m_outputPath = outputPath; }
175
176 wxString m_outputPath;
177};
178
183{
184public:
185 JOB( const std::string& aType, bool aOutputIsDirectory );
186
187 virtual ~JOB();
188
189 const std::string& GetType() const { return m_type; };
190
191 const std::map<wxString, wxString>& GetVarOverrides() const { return m_varOverrides; }
192
193 void SetVarOverrides( const std::map<wxString, wxString>& aVarOverrides )
194 {
195 m_varOverrides = aVarOverrides;
196 }
197
198 void SetTitleBlock( const TITLE_BLOCK& aTitleBlock ) { m_titleBlock = aTitleBlock; }
199
200 virtual void FromJson( const nlohmann::json& j );
201 virtual void ToJson( nlohmann::json& j ) const;
202
203 virtual wxString GetDefaultDescription() const;
204 virtual wxString GetSettingsDialogTitle() const;
205
206 const std::vector<JOB_PARAM_BASE*>& GetParams() { return m_params; }
207
208 void ClearExistingOutputs() { m_outputs.clear(); }
209 const std::vector<JOB_OUTPUT>& GetOutputs() { return m_outputs; }
210 void AddOutput( wxString aOutputPath ) { m_outputs.emplace_back( aOutputPath ); }
211
217 void SetTempOutputDirectory( const wxString& aBase );
218
222 void SetConfiguredOutputPath( const wxString& aPath );
223
227 wxString GetConfiguredOutputPath() const { return m_outputPath; }
228
233 void SetWorkingOutputPath( const wxString& aPath ) { m_workingOutputPath = aPath; }
234
238 wxString GetWorkingOutputPath() const { return m_workingOutputPath; }
239
246 wxString GetFullOutputPath( PROJECT* aProject ) const;
247
248 bool GetOutputPathIsDirectory() const { return m_outputPathIsDirectory; }
249
250protected:
251 std::string m_type;
252 std::map<wxString, wxString> m_varOverrides;
254
256
257 wxString m_outputPath;
261
262 std::vector<JOB_PARAM_BASE*> m_params;
263
264 std::vector<JOB_OUTPUT> m_outputs;
265};
266
267KICOMMON_API void from_json( const nlohmann::json& j, JOB& f );
268KICOMMON_API void to_json( nlohmann::json& j, const JOB& f );
const char * name
Definition: DXF_plotter.cpp:62
virtual void ToJson(nlohmann::json &j)=0
virtual void FromJson(const nlohmann::json &j) const =0
std::string m_jsonPath
Definition: job.h:46
virtual ~JOB_PARAM_BASE()=default
void ToJson(nlohmann::json &j) override
Definition: job.h:103
std::vector< ListElementType > m_default
Definition: job.h:115
JOB_PARAM_LIST(const std::string &aJsonPath, std::vector< ListElementType > *aPtr, std::vector< ListElementType > aDefault)
Definition: job.h:77
virtual void FromJson(const nlohmann::json &j) const override
Definition: job.h:84
std::vector< ListElementType > * m_ptr
Definition: job.h:114
JOB_PARAM_LSEQ(const std::string &aJsonPath, LSEQ *aPtr, LSEQ aDefault)
Definition: job.h:122
void ToJson(nlohmann::json &j) override
Definition: job.h:159
virtual void FromJson(const nlohmann::json &j) const override
Definition: job.h:127
Definition: job.h:51
JOB_PARAM(const std::string &aJsonPath, ValueType *aPtr, ValueType aDefault)
Definition: job.h:54
virtual void ToJson(nlohmann::json &j) override
Definition: job.h:65
ValueType m_default
Definition: job.h:69
ValueType * m_ptr
Definition: job.h:68
virtual void FromJson(const nlohmann::json &j) const override
Definition: job.h:60
An simple container class that lets us dispatch output jobs to kifaces.
Definition: job.h:183
void SetVarOverrides(const std::map< wxString, wxString > &aVarOverrides)
Definition: job.h:193
std::vector< JOB_PARAM_BASE * > m_params
Definition: job.h:262
TITLE_BLOCK m_titleBlock
Definition: job.h:253
const std::vector< JOB_OUTPUT > & GetOutputs()
Definition: job.h:209
void AddOutput(wxString aOutputPath)
Definition: job.h:210
const std::vector< JOB_PARAM_BASE * > & GetParams()
Definition: job.h:206
wxString GetWorkingOutputPath() const
Returns the working output path for the job, if one has been set.
Definition: job.h:238
wxString m_workingOutputPath
Definition: job.h:260
bool GetOutputPathIsDirectory() const
Definition: job.h:248
std::string m_type
Definition: job.h:251
wxString m_tempOutputDirectory
Definition: job.h:255
wxString m_description
Definition: job.h:259
wxString GetConfiguredOutputPath() const
Returns the configured output path for the job.
Definition: job.h:227
std::vector< JOB_OUTPUT > m_outputs
Definition: job.h:264
bool m_outputPathIsDirectory
Definition: job.h:258
void SetTitleBlock(const TITLE_BLOCK &aTitleBlock)
Definition: job.h:198
void SetWorkingOutputPath(const wxString &aPath)
Sets a transient output path for the job, it takes priority over the configured output path when GetF...
Definition: job.h:233
const std::string & GetType() const
Definition: job.h:189
std::map< wxString, wxString > m_varOverrides
Definition: job.h:252
const std::map< wxString, wxString > & GetVarOverrides() const
Definition: job.h:191
void ClearExistingOutputs()
Definition: job.h:208
wxString m_outputPath
Definition: job.h:257
LSEQ is a sequence (and therefore also a set) of PCB_LAYER_IDs.
Definition: lseq.h:47
static int NameToLayer(wxString &aName)
Return the layer number from a layer name.
Definition: lset.cpp:117
static wxString Name(PCB_LAYER_ID aLayerId)
Return the fixed name association with aLayerId.
Definition: lset.cpp:188
Container for project specific data.
Definition: project.h:65
Hold the information shown in the lower right corner of a plot, printout, or editing view.
Definition: title_block.h:41
KICOMMON_API void to_json(nlohmann::json &j, const JOB &f)
Definition: job.cpp:159
KICOMMON_API void from_json(const nlohmann::json &j, JOB &f)
Definition: job.cpp:165
#define KICOMMON_API
Definition: kicommon.h:28
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:60
@ UNDEFINED_LAYER
Definition: layer_ids.h:61
STL namespace.
JOB_OUTPUT()
Definition: job.h:172
wxString m_outputPath
Definition: job.h:176
JOB_OUTPUT(wxString outputPath)
Definition: job.h:174