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 (C) 2024 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 <nlohmann/json.hpp>
22
23#include <settings/parameters.h>
25
26#include <jobs/jobset.h>
27#include <jobs/job_registry.h>
30#include <kiid.h>
31
32#include <algorithm>
33
35
37 {
38 { JOBSET_OUTPUT_TYPE::FOLDER, "folder" },
39 { JOBSET_OUTPUT_TYPE::ARCHIVE, "archive" }
40 } )
41
42KICOMMON_API void to_json( nlohmann::json& j, const JOBSET_JOB& f )
43{
44 j = nlohmann::json{ { "id", f.m_id },
45 { "type", f.m_type },
46 { "settings", nlohmann::json::object( {} ) }
47 };
48
49 f.m_job->ToJson( j.at( "settings" ) );
50}
51
52
53KICOMMON_API void from_json( const nlohmann::json& j, JOBSET_JOB& f )
54{
55 j.at( "type" ).get_to( f.m_type );
56 j.at( "id" ).get_to( f.m_id );
57
58 nlohmann::json settings_obj = j.at( "settings" );
59
60 f.m_job = JOB_REGISTRY::CreateInstance<JOB>( f.m_type );
61
62 if( f.m_job != nullptr )
63 {
64 f.m_job->FromJson( settings_obj );
65 }
66}
67
68
69KICOMMON_API void to_json( nlohmann::json& j, const JOBSET_OUTPUT& f )
70{
71 j = nlohmann::json{ { "type", f.m_type }, { "settings", nlohmann::json::object( {} ) } };
72
73 f.m_outputHandler->ToJson( j.at( "settings" ) );
74}
75
76
77KICOMMON_API void from_json( const nlohmann::json& j, JOBSET_OUTPUT& f )
78{
79 j.at( "type" ).get_to( f.m_type );
80 f.m_only = j.value( "only", std::vector<wxString>() );
81
82 nlohmann::json settings_obj = j.at( "settings" );
83
85 {
87 }
89 {
91 }
92
93 if( f.m_outputHandler != nullptr )
94 {
95 f.m_outputHandler->FromJson( settings_obj );
96 }
97}
98
99
100bool JOBSET_JOB::operator==( const JOBSET_JOB & rhs ) const
101{
102 return rhs.m_type == m_type;
103}
104
105
107{
108 return rhs.m_type == m_type;
109}
110
111
112JOBSET::JOBSET( const wxString& aFilename ) :
114 m_dirty( false )
115{
116 m_params.emplace_back( new PARAM_LIST<JOBSET_JOB>( "jobs", &m_jobs, {} ) );
117 m_params.emplace_back( new PARAM_LIST<JOBSET_OUTPUT>( "outputs", &m_outputs, {} ) );
118
119 m_fileNameWithoutPath = wxFileName( aFilename ).GetFullName();
120}
121
122
123wxString JOBSET::getFileExt() const
124{
126}
127
128
129void JOBSET::AddNewJob( wxString aType, JOB* aJob )
130{
131 m_jobs.emplace_back( KIID().AsString(), aType, aJob );
132 SetDirty();
133}
134
135
137 JOBS_OUTPUT_HANDLER* aJobOutput )
138{
139 m_outputs.emplace_back( KIID().AsString(), aType, aJobOutput );
140 SetDirty();
141
142 return m_outputs.back();
143}
144
145
147{
148 std::erase_if( m_outputs,
149 [&]( JOBSET_OUTPUT const& output )
150 {
151 return output.m_id == aOutput->m_id;
152 } );
153}
154
155
156void JOBSET::MoveJobUp( size_t aJobIdx )
157{
158 if( aJobIdx > 0 )
159 {
160 std::swap( m_jobs[aJobIdx], m_jobs[aJobIdx - 1] );
161 SetDirty();
162 }
163}
164
165
166void JOBSET::MoveJobDown( size_t aJobIdx )
167{
168 if( aJobIdx < m_jobs.size() - 1 )
169 {
170 std::swap( m_jobs[aJobIdx], m_jobs[aJobIdx + 1] );
171 SetDirty();
172 }
173}
174
175
176void JOBSET::RemoveJob( size_t aJobIdx )
177{
178 m_jobs.erase( m_jobs.begin() + aJobIdx );
179 SetDirty();
180}
181
182
183bool JOBSET::SaveToFile( const wxString& aDirectory, bool aForce )
184{
185 bool success = JSON_SETTINGS::SaveToFile( aDirectory, aForce );
186 if( success )
187 {
188 m_dirty = false;
189 }
190
191 return success;
192}
193
194
195JOBSET_OUTPUT* JOBSET::GetOutput( wxString& aOutput )
196{
197 auto it = std::find_if( m_outputs.begin(), m_outputs.end(),
198 [&]( const JOBSET_OUTPUT& output )
199 {
200 if( output.m_id == aOutput )
201 return true;
202
203 return false;
204 } );
205
206 if( it != m_outputs.end() )
207 return &(*it);
208
209 return nullptr;
210}
211
212
213std::vector<JOBSET_JOB> JOBSET::GetJobsForOutput( JOBSET_OUTPUT* aOutput )
214{
215 wxASSERT( aOutput != nullptr );
216
217 if( aOutput->m_only.size() == 0 )
218 {
219 return m_jobs;
220 }
221
222 std::vector<JOBSET_JOB> result;
223 for( wxString& onlyId : aOutput->m_only )
224 {
225 auto it = std::find_if( m_jobs.begin(), m_jobs.end(),
226 [&]( const JOBSET_JOB& job )
227 {
228 if( job.m_id == onlyId )
229 return true;
230
231 return false;
232 } );
233
234 if( it != m_jobs.end() )
235 result.push_back( *it );
236 }
237
238 return result;
239}
240
241
242#if !defined( __MINGW32__ )
245#endif
wxString m_fileNameWithoutPath
Definition: jobset.h:110
std::vector< JOBSET_JOB > GetJobsForOutput(JOBSET_OUTPUT *aOutput)
Definition: jobset.cpp:213
JOBSET_OUTPUT AddNewJobOutput(JOBSET_OUTPUT_TYPE aType, JOBS_OUTPUT_HANDLER *aJobOutput)
Definition: jobset.cpp:136
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:183
void SetDirty()
Definition: jobset.h:88
void RemoveOutput(JOBSET_OUTPUT *aOutput)
Definition: jobset.cpp:146
void MoveJobUp(size_t aJobIdx)
Definition: jobset.cpp:156
JOBSET_OUTPUT * GetOutput(wxString &aOutput)
Definition: jobset.cpp:195
std::vector< JOBSET_JOB > m_jobs
Definition: jobset.h:106
void RemoveJob(size_t aJobIdx)
Definition: jobset.cpp:176
std::vector< JOBSET_OUTPUT > m_outputs
Definition: jobset.h:107
void AddNewJob(wxString aType, JOB *aJob)
Definition: jobset.cpp:129
void MoveJobDown(size_t aJobIdx)
Definition: jobset.cpp:166
wxString getFileExt() const override
Definition: jobset.cpp:123
bool m_dirty
Definition: jobset.h:109
JOBSET(const wxString &aFilename)
Definition: jobset.cpp:112
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:79
virtual void FromJson(const nlohmann::json &j)
Definition: job.cpp:54
virtual void ToJson(nlohmann::json &j) const
Definition: job.cpp:61
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
static const std::string KiCadJobSetFileExtension
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:36
KICOMMON_API void from_json(const nlohmann::json &j, JOBSET_JOB &f)
Definition: jobset.cpp:53
KICOMMON_API void to_json(nlohmann::json &j, const JOBSET_OUTPUT &f)
Definition: jobset.cpp:69
const int jobsFileSchemaVersion
Definition: jobset.cpp:34
JOBSET_OUTPUT_TYPE
Definition: jobset.h:44
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:36
JOB * m_job
Definition: jobset.h:38
wxString m_type
Definition: jobset.h:37
bool operator==(const JOBSET_JOB &rhs) const
Definition: jobset.cpp:100
std::vector< wxString > m_only
Definition: jobset.h:63
wxString m_id
Definition: jobset.h:60
JOBS_OUTPUT_HANDLER * m_outputHandler
Definition: jobset.h:62
bool operator==(const JOBSET_OUTPUT &rhs) const
Definition: jobset.cpp:106
JOBSET_OUTPUT_TYPE m_type
Definition: jobset.h:61
Definition of file extensions used in Kicad.