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 <kicommon.h>
24#include <map>
25#include <wx/string.h>
27#include <lseq.h>
28#include <lset.h>
29#include <title_block.h>
30
31class PROJECT;
32
34{
35public:
36 JOB_PARAM_BASE( const std::string& aJsonPath );
37
38 virtual ~JOB_PARAM_BASE() = default;
39
40 virtual void FromJson( const nlohmann::json& j ) const = 0;
41
42 virtual void ToJson( nlohmann::json& j ) = 0;
43
44protected:
45 std::string m_jsonPath;
46};
47
48template <typename ValueType>
50{
51public:
52
53 JOB_PARAM( const std::string& aJsonPath, ValueType* aPtr,
54 ValueType aDefault ) :
55 JOB_PARAM_BASE( aJsonPath ), m_ptr( aPtr ), m_default( aDefault )
56 {
57 }
58
59 virtual void FromJson( const nlohmann::json& j ) const override
60 {
61 *m_ptr = j.value( m_jsonPath, m_default );
62 }
63
64 virtual void ToJson( nlohmann::json& j ) override { j[m_jsonPath] = *m_ptr; }
65
66protected:
67 ValueType* m_ptr;
68 ValueType m_default;
69};
70
71
72template <typename ListElementType>
74{
75public:
76 JOB_PARAM_LIST( const std::string& aJsonPath, std::vector<ListElementType>* aPtr,
77 std::vector<ListElementType> aDefault ) :
78 JOB_PARAM_BASE( aJsonPath ),
79 m_ptr( aPtr ),
80 m_default( std::move( aDefault ) )
81 { }
82
83 virtual void FromJson( const nlohmann::json& j ) const override
84 {
85 if( j.contains( m_jsonPath ) )
86 {
87 auto js = j.at( m_jsonPath );
88 std::vector<ListElementType> val;
89
90 if( js.is_array() )
91 {
92 for( const auto& el : js.items() )
93 val.push_back( el.value().template get<ListElementType>() );
94 }
95
96 *m_ptr = val;
97 }
98 else
100 }
101
102 void ToJson( nlohmann::json& j ) override
103 {
104 nlohmann::json js = nlohmann::json::array();
105
106 for( const auto& el : *m_ptr )
107 js.push_back( el );
108
109 j[m_jsonPath] = js;
110 }
111
112protected:
113 std::vector<ListElementType>* m_ptr;
114 std::vector<ListElementType> m_default;
115};
116
117
118class JOB_PARAM_LSEQ : public JOB_PARAM<LSEQ>
119{
120public:
121 JOB_PARAM_LSEQ( const std::string& aJsonPath, LSEQ* aPtr, LSEQ aDefault ) :
122 JOB_PARAM<LSEQ>( aJsonPath, aPtr, aDefault )
123 {
124 }
125
126 virtual void FromJson( const nlohmann::json& j ) const override
127 {
128 if( j.contains( m_jsonPath ) )
129 {
130 auto js = j.at( m_jsonPath );
131
132 LSEQ layers;
133 if( js.is_array() )
134 {
135 for( const nlohmann::json& layer : js )
136 {
137 if( layer.is_string() )
138 {
139 wxString name = layer.get<wxString>();
140 int layerId = LSET::NameToLayer( name );
141 if( layerId != UNDEFINED_LAYER )
142 layers.push_back( static_cast<PCB_LAYER_ID>( layerId ) );
143 }
144 else
145 {
146 int layerId = layer.get<int>();
147 if( layerId != UNDEFINED_LAYER )
148 layers.push_back( static_cast<PCB_LAYER_ID>( layerId ) );
149 }
150 }
151 }
152 *m_ptr = layers;
153 }
154 else
155 *m_ptr = m_default;
156 }
157
158 void ToJson( nlohmann::json& j ) override
159 {
160 nlohmann::json js = nlohmann::json::array();
161
162 for( PCB_LAYER_ID layer : ( *m_ptr ) )
163 js.push_back( LSET::Name( layer ) );
164
165 j[m_jsonPath] = js;
166 }
167};
168
170{
172
173 JOB_OUTPUT( wxString outputPath ) { m_outputPath = outputPath; }
174
175 wxString m_outputPath;
176};
177
182{
183public:
184 JOB( const std::string& aType, bool aOutputIsDirectory );
185
186 virtual ~JOB();
187
188 const std::string& GetType() const { return m_type; };
189
190 const std::map<wxString, wxString>& GetVarOverrides() const { return m_varOverrides; }
191
192 void SetVarOverrides( const std::map<wxString, wxString>& aVarOverrides )
193 {
194 m_varOverrides = aVarOverrides;
195 }
196
197 void SetTitleBlock( const TITLE_BLOCK& aTitleBlock ) { m_titleBlock = aTitleBlock; }
198
199 virtual void FromJson( const nlohmann::json& j );
200 virtual void ToJson( nlohmann::json& j ) const;
201
202 virtual wxString GetDefaultDescription() const;
203 virtual wxString GetSettingsDialogTitle() const;
204
205 const std::vector<JOB_PARAM_BASE*>& GetParams() { return m_params; }
206
207 void ClearExistingOutputs() { m_outputs.clear(); }
208 const std::vector<JOB_OUTPUT>& GetOutputs() { return m_outputs; }
209 void AddOutput( wxString aOutputPath ) { m_outputs.emplace_back( aOutputPath ); }
210
216 void SetTempOutputDirectory( const wxString& aBase );
217
221 void SetConfiguredOutputPath( const wxString& aPath );
222
226 wxString GetConfiguredOutputPath() const { return m_outputPath; }
227
232 void SetWorkingOutputPath( const wxString& aPath ) { m_workingOutputPath = aPath; }
233
237 wxString GetWorkingOutputPath() const { return m_workingOutputPath; }
238
245 wxString GetFullOutputPath( PROJECT* aProject ) const;
246
247 bool GetOutputPathIsDirectory() const { return m_outputPathIsDirectory; }
248
249protected:
250 std::string m_type;
251 std::map<wxString, wxString> m_varOverrides;
253
255
256 wxString m_outputPath;
260
261 std::vector<JOB_PARAM_BASE*> m_params;
262
263 std::vector<JOB_OUTPUT> m_outputs;
264};
265
266KICOMMON_API void from_json( const nlohmann::json& j, JOB& f );
267KICOMMON_API void to_json( nlohmann::json& j, const JOB& f );
const char * name
Definition: DXF_plotter.cpp:59
virtual void ToJson(nlohmann::json &j)=0
virtual void FromJson(const nlohmann::json &j) const =0
std::string m_jsonPath
Definition: job.h:45
virtual ~JOB_PARAM_BASE()=default
void ToJson(nlohmann::json &j) override
Definition: job.h:102
std::vector< ListElementType > m_default
Definition: job.h:114
JOB_PARAM_LIST(const std::string &aJsonPath, std::vector< ListElementType > *aPtr, std::vector< ListElementType > aDefault)
Definition: job.h:76
virtual void FromJson(const nlohmann::json &j) const override
Definition: job.h:83
std::vector< ListElementType > * m_ptr
Definition: job.h:113
JOB_PARAM_LSEQ(const std::string &aJsonPath, LSEQ *aPtr, LSEQ aDefault)
Definition: job.h:121
void ToJson(nlohmann::json &j) override
Definition: job.h:158
virtual void FromJson(const nlohmann::json &j) const override
Definition: job.h:126
Definition: job.h:50
JOB_PARAM(const std::string &aJsonPath, ValueType *aPtr, ValueType aDefault)
Definition: job.h:53
virtual void ToJson(nlohmann::json &j) override
Definition: job.h:64
ValueType m_default
Definition: job.h:68
ValueType * m_ptr
Definition: job.h:67
virtual void FromJson(const nlohmann::json &j) const override
Definition: job.h:59
An simple container class that lets us dispatch output jobs to kifaces.
Definition: job.h:182
void SetVarOverrides(const std::map< wxString, wxString > &aVarOverrides)
Definition: job.h:192
std::vector< JOB_PARAM_BASE * > m_params
Definition: job.h:261
TITLE_BLOCK m_titleBlock
Definition: job.h:252
const std::vector< JOB_OUTPUT > & GetOutputs()
Definition: job.h:208
void AddOutput(wxString aOutputPath)
Definition: job.h:209
const std::vector< JOB_PARAM_BASE * > & GetParams()
Definition: job.h:205
wxString GetWorkingOutputPath() const
Returns the working output path for the job, if one has been set.
Definition: job.h:237
wxString m_workingOutputPath
Definition: job.h:259
bool GetOutputPathIsDirectory() const
Definition: job.h:247
std::string m_type
Definition: job.h:250
wxString m_tempOutputDirectory
Definition: job.h:254
wxString m_description
Definition: job.h:258
wxString GetConfiguredOutputPath() const
Returns the configured output path for the job.
Definition: job.h:226
std::vector< JOB_OUTPUT > m_outputs
Definition: job.h:263
bool m_outputPathIsDirectory
Definition: job.h:257
void SetTitleBlock(const TITLE_BLOCK &aTitleBlock)
Definition: job.h:197
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:232
const std::string & GetType() const
Definition: job.h:188
std::map< wxString, wxString > m_varOverrides
Definition: job.h:251
const std::map< wxString, wxString > & GetVarOverrides() const
Definition: job.h:190
void ClearExistingOutputs()
Definition: job.h:207
wxString m_outputPath
Definition: job.h:256
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:64
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:171
wxString m_outputPath
Definition: job.h:175
JOB_OUTPUT(wxString outputPath)
Definition: job.h:173