KiCad PCB EDA Suite
Loading...
Searching...
No Matches
jobset.cpp
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) 2024 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#include <i18n_utility.h>
22#include <nlohmann/json.hpp>
23
24#include <settings/parameters.h>
26
27#include <jobs/jobset.h>
28#include <jobs/job_registry.h>
31#include <kiid.h>
32#include <reporter.h>
33
34#include <algorithm>
35
37
38
39KICOMMON_API std::map<JOBSET_OUTPUT_TYPE, JOBSET_OUTPUT_TYPE_INFO> JobsetOutputTypeInfos = {
40 { JOBSET_OUTPUT_TYPE::FOLDER,
41 { _HKI( "Folder" ), BITMAPS::small_folder, true, "" } },
42 { JOBSET_OUTPUT_TYPE::ARCHIVE,
43 { _HKI( "Archive" ), BITMAPS::zip, false, FILEEXT::ZipFileWildcard() } },
44};
45
46
48 {
49 { JOBSET_OUTPUT_TYPE::FOLDER, "folder" },
50 { JOBSET_OUTPUT_TYPE::ARCHIVE, "archive" }
51 } )
52
53KICOMMON_API void to_json( nlohmann::json& j, const JOBSET_JOB& f )
54{
55 j = nlohmann::json{ { "id", f.m_id },
56 { "type", f.m_type },
57 { "description", f.m_description },
58 { "settings", nlohmann::json::object( {} ) }
59 };
60
61 f.m_job->ToJson( j.at( "settings" ) );
62}
63
64
65KICOMMON_API void from_json( const nlohmann::json& j, JOBSET_JOB& f )
66{
67 j.at( "type" ).get_to( f.m_type );
68 j.at( "id" ).get_to( f.m_id );
69 f.m_description = j.value( "description", "" );
70
71 nlohmann::json settings_obj = j.at( "settings" );
72
73 f.m_job.reset( JOB_REGISTRY::CreateInstance<JOB>( f.m_type ) );
74
75 if( f.m_job != nullptr )
76 {
77 f.m_job->FromJson( settings_obj );
78 }
79}
80
81
82KICOMMON_API void to_json( nlohmann::json& j, const JOBSET_OUTPUT& f )
83{
84 j = nlohmann::json{ { "id", f.m_id },
85 { "type", f.m_type },
86 { "only", f.m_only },
87 { "description", f.m_description },
88 { "settings", nlohmann::json::object( {} ) }
89 };
90
91 f.m_outputHandler->ToJson( j.at( "settings" ) );
92}
93
94
95KICOMMON_API void from_json( const nlohmann::json& j, JOBSET_OUTPUT& f )
96{
97 // During 9.0 development outputs didn't get ids.
98 if( j.contains( "id" ) )
99 j.at( "id" ).get_to( f.m_id );
100 else
101 f.m_id = KIID().AsString();
102
103 j.at( "type" ).get_to( f.m_type );
104 f.m_only = j.value( "only", std::vector<wxString>() );
105 f.m_description = j.value( "description", "" );
106
107 const nlohmann::json& settings_obj = j.at( "settings" );
108
110
111 if( f.m_outputHandler != nullptr )
112 {
113 f.m_outputHandler->FromJson( settings_obj );
114 }
115}
116
117
119 m_type( JOBSET_OUTPUT_TYPE::FOLDER ),
120 m_outputHandler( nullptr ),
121 m_lastRunSuccess(),
122 m_lastRunReporters()
123{
124}
125
126
128 m_id( id ),
129 m_type( type ),
130 m_outputHandler( nullptr ),
131 m_lastRunSuccess(),
132 m_lastRunReporters()
133{
135}
136
137
139{
140 for( auto& [name, reporter] : m_lastRunReporters )
141 delete reporter;
142
143 m_lastRunReporters.clear();
144}
145
146
148{
149 if( m_type == JOBSET_OUTPUT_TYPE::FOLDER )
150 {
152 }
153 else if( m_type == JOBSET_OUTPUT_TYPE::ARCHIVE )
154 {
156 }
157}
158
159
161{
163}
164
165
166void JOBSET_OUTPUT::SetDescription( const wxString& aDescription )
167{
168 if( aDescription == m_outputHandler->GetDefaultDescription() )
169 m_description = wxEmptyString;
170 else
171 m_description = aDescription;
172}
173
174
175bool JOBSET_JOB::operator==( const JOBSET_JOB & rhs ) const
176{
177 return rhs.m_type == m_type;
178}
179
180
182{
183 return m_description.IsEmpty() ? m_job->GetDefaultDescription() : m_description;
184}
185
186
187void JOBSET_JOB::SetDescription( const wxString& aDescription )
188{
189 if( aDescription == m_job->GetDefaultDescription() )
190 m_description = wxEmptyString;
191 else
192 m_description = aDescription;
193}
194
195
197{
198 return rhs.m_type == m_type;
199}
200
201
202JOBSET::JOBSET( const wxString& aFilename ) :
204 m_dirty( false )
205{
206 m_params.emplace_back( new PARAM_LIST<JOBSET_JOB>( "jobs", &m_jobs, {} ) );
207 m_params.emplace_back( new PARAM_LIST<JOBSET_OUTPUT>( "outputs", &m_outputs, {} ) );
208
209 m_fileNameWithoutPath = wxFileName( aFilename ).GetFullName();
210}
211
212
213wxString JOBSET::getFileExt() const
214{
216}
217
218
219void JOBSET::AddNewJob( wxString aType, JOB* aJob )
220{
221 m_jobs.emplace_back( KIID().AsString(), aType, aJob );
222 SetDirty();
223}
224
225
227{
228 m_outputs.emplace_back( KIID().AsString(), aType);
229 SetDirty();
230
231 return &m_outputs.back();
232}
233
234
236{
237 std::erase_if( m_outputs,
238 [&]( JOBSET_OUTPUT const& output )
239 {
240 return output.m_id == aOutput->m_id;
241 } );
242}
243
244
245void JOBSET::MoveJobUp( size_t aJobIdx )
246{
247 if( aJobIdx > 0 )
248 {
249 std::swap( m_jobs[aJobIdx], m_jobs[aJobIdx - 1] );
250 SetDirty();
251 }
252}
253
254
255void JOBSET::MoveJobDown( size_t aJobIdx )
256{
257 if( aJobIdx < m_jobs.size() - 1 )
258 {
259 std::swap( m_jobs[aJobIdx], m_jobs[aJobIdx + 1] );
260 SetDirty();
261 }
262}
263
264
265void JOBSET::RemoveJob( size_t aJobIdx )
266{
267 m_jobs.erase( m_jobs.begin() + aJobIdx );
268 SetDirty();
269}
270
271
272bool JOBSET::SaveToFile( const wxString& aDirectory, bool aForce )
273{
274 bool success = JSON_SETTINGS::SaveToFile( aDirectory, aForce );
275 if( success )
276 {
277 m_dirty = false;
278 }
279
280 return success;
281}
282
283
284JOBSET_OUTPUT* JOBSET::GetOutput( wxString& aOutput )
285{
286 auto it = std::find_if( m_outputs.begin(), m_outputs.end(),
287 [&]( const JOBSET_OUTPUT& output )
288 {
289 if( output.m_id == aOutput )
290 return true;
291
292 return false;
293 } );
294
295 if( it != m_outputs.end() )
296 return &(*it);
297
298 return nullptr;
299}
300
301
302std::vector<JOBSET_JOB> JOBSET::GetJobsForOutput( JOBSET_OUTPUT* aOutput )
303{
304 wxASSERT( aOutput != nullptr );
305
306 if( aOutput->m_only.size() == 0 )
307 {
308 return m_jobs;
309 }
310
311 std::vector<JOBSET_JOB> result;
312 for( wxString& onlyId : aOutput->m_only )
313 {
314 auto it = std::find_if( m_jobs.begin(), m_jobs.end(),
315 [&]( const JOBSET_JOB& job )
316 {
317 if( job.m_id == onlyId )
318 return true;
319
320 return false;
321 } );
322
323 if( it != m_jobs.end() )
324 result.push_back( *it );
325 }
326
327 return result;
328}
329
330
331#if !defined( __MINGW32__ )
334#endif
const char * name
Definition: DXF_plotter.cpp:59
@ small_folder
wxString m_fileNameWithoutPath
Definition: jobset.h:145
std::vector< JOBSET_JOB > GetJobsForOutput(JOBSET_OUTPUT *aOutput)
Definition: jobset.cpp:302
bool SaveToFile(const wxString &aDirectory="", bool aForce=false) override
Calls Store() and then writes the contents of the JSON document to a file.
Definition: jobset.cpp:272
void SetDirty(bool aFlag=true)
Definition: jobset.h:124
void RemoveOutput(JOBSET_OUTPUT *aOutput)
Definition: jobset.cpp:235
void MoveJobUp(size_t aJobIdx)
Definition: jobset.cpp:245
JOBSET_OUTPUT * GetOutput(wxString &aOutput)
Definition: jobset.cpp:284
std::vector< JOBSET_JOB > m_jobs
Definition: jobset.h:141
void RemoveJob(size_t aJobIdx)
Definition: jobset.cpp:265
std::vector< JOBSET_OUTPUT > m_outputs
Definition: jobset.h:142
void AddNewJob(wxString aType, JOB *aJob)
Definition: jobset.cpp:219
void MoveJobDown(size_t aJobIdx)
Definition: jobset.cpp:255
wxString getFileExt() const override
Definition: jobset.cpp:213
bool m_dirty
Definition: jobset.h:144
JOBSET(const wxString &aFilename)
Definition: jobset.cpp:202
JOBSET_OUTPUT * AddNewJobOutput(JOBSET_OUTPUT_TYPE aType)
Definition: jobset.cpp:226
virtual wxString GetDefaultDescription() const
Definition: jobs_output.h:56
virtual void ToJson(nlohmann::json &j) const =0
virtual void FromJson(const nlohmann::json &j)=0
An simple container class that lets us dispatch output jobs to kifaces.
Definition: job.h:182
std::vector< PARAM_BASE * > m_params
The list of parameters (owned by this object)
virtual bool SaveToFile(const wxString &aDirectory="", bool aForce=false)
Calls Store() and then writes the contents of the JSON document to a file.
Definition: kiid.h:49
wxString AsString() const
Definition: kiid.cpp:246
#define _HKI(x)
static const std::string KiCadJobSetFileExtension
static wxString ZipFileWildcard()
Some functions to handle hotkeys in KiCad.
NLOHMANN_JSON_SERIALIZE_ENUM(JOBSET_OUTPUT_TYPE, { { JOBSET_OUTPUT_TYPE::FOLDER, "folder" }, { JOBSET_OUTPUT_TYPE::ARCHIVE, "archive" } }) KICOMMON_API void to_json(nlohmann
Definition: jobset.cpp:47
KICOMMON_API std::map< JOBSET_OUTPUT_TYPE, JOBSET_OUTPUT_TYPE_INFO > JobsetOutputTypeInfos
Definition: jobset.cpp:39
KICOMMON_API void from_json(const nlohmann::json &j, JOBSET_JOB &f)
Definition: jobset.cpp:65
KICOMMON_API void to_json(nlohmann::json &j, const JOBSET_OUTPUT &f)
Definition: jobset.cpp:82
const int jobsFileSchemaVersion
Definition: jobset.cpp:36
FOLDER
Definition: jobset.h:60
enum KICOMMON_API JOBSET_OUTPUT_TYPE
Definition: jobset.h:59
SETTINGS_LOC
Definition: json_settings.h:54
#define KICOMMON_API
Definition: kicommon.h:28
@ NONE
No connection to this item.
wxString m_id
Definition: jobset.h:46
std::shared_ptr< JOB > m_job
Definition: jobset.h:49
wxString m_description
Definition: jobset.h:48
wxString m_type
Definition: jobset.h:47
bool operator==(const JOBSET_JOB &rhs) const
Definition: jobset.cpp:175
wxString GetDescription() const
Definition: jobset.cpp:181
void SetDescription(const wxString &aDescription)
Definition: jobset.cpp:187
wxString GetDescription() const
Definition: jobset.cpp:160
std::vector< wxString > m_only
Definition: jobset.h:89
void SetDescription(const wxString &aDescription)
Transient property, not stored for now.
Definition: jobset.cpp:166
void InitOutputHandler()
Definition: jobset.cpp:147
wxString m_id
Definition: jobset.h:85
wxString m_description
Definition: jobset.h:87
JOBS_OUTPUT_HANDLER * m_outputHandler
Definition: jobset.h:88
std::unordered_map< wxString, REPORTER * > m_lastRunReporters
Definition: jobset.h:97
bool operator==(const JOBSET_OUTPUT &rhs) const
Definition: jobset.cpp:196
JOBSET_OUTPUT_TYPE m_type
Definition: jobset.h:86
Definition of file extensions used in Kicad.