File: gui_sketch_object_creator.c

package info (click to toggle)
crystal-facet-uml 1.63.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 21,640 kB
  • sloc: ansic: 109,610; xml: 3,085; makefile: 138; sh: 113
file content (524 lines) | stat: -rw-r--r-- 24,005 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/* File: gui_sketch_object_creator.c; Copyright and License: see below */

#include "sketch/gui_sketch_object_creator.h"
#include "geometry/geometry_rectangle.h"
#include "ctrl_multi_step_changer.h"
#include "entity/data_table.h"
#include "entity/data_id.h"
#include "u8/u8_trace.h"
#include "u8/u8_log.h"
#include <stdint.h>
#include <stdbool.h>

void gui_sketch_object_creator_init ( gui_sketch_object_creator_t *this_,
                                      ctrl_controller_t *controller,
                                      data_database_reader_t *db_reader,
                                      gui_simple_message_to_user_t *message_to_user )
{
    U8_TRACE_BEGIN();

    (*this_).db_reader = db_reader;
    (*this_).controller = controller;
    gui_sketch_defaults_init ( &((*this_).defaults ) );
    data_rules_init ( &((*this_).data_rules ) );
    (*this_).message_to_user = message_to_user;

    U8_TRACE_END();
}

void gui_sketch_object_creator_destroy ( gui_sketch_object_creator_t *this_ )
{
    U8_TRACE_BEGIN();

    (*this_).message_to_user = NULL;
    data_rules_destroy ( &((*this_).data_rules) );
    gui_sketch_defaults_destroy ( &((*this_).defaults) );
    (*this_).db_reader = NULL;
    (*this_).controller = NULL;

    U8_TRACE_END();
}

u8_error_t gui_sketch_object_creator_create_classifier ( gui_sketch_object_creator_t *this_,
                                                         data_row_t diagram_id,
                                                         int32_t x_order,
                                                         int32_t y_order,
                                                         data_row_t *out_diagramelement_id,
                                                         data_row_t *out_classifier_id )
{
    U8_TRACE_BEGIN();
    assert ( NULL != out_classifier_id );
    assert ( NULL != out_diagramelement_id );
    assert ( DATA_ROW_VOID != diagram_id );

    u8_error_t c_result;

    /* get type of diagram */
    data_diagram_type_t diag_type = DATA_DIAGRAM_TYPE_LIST;
    {
        const u8_error_t diag_err
            = data_database_reader_get_diagram_by_id( (*this_).db_reader,
                                                      diagram_id,
                                                      &((*this_).private_temp_diagram)
                                                    );
        if ( U8_ERROR_NONE == diag_err )
        {
            diag_type = data_diagram_get_diagram_type( &((*this_).private_temp_diagram) );
            data_diagram_destroy ( &((*this_).private_temp_diagram) );
        }
        else
        {
            U8_LOG_ERROR_INT( "gui_sketch_object_creator_create_classifier cannot find diagram:", diagram_id );
        }
    }

    /* determine type of new classifier */
    data_classifier_type_t type_of_new_classifier;
    type_of_new_classifier = gui_sketch_defaults_get_classifier_type( &((*this_).defaults), diag_type );

    /* define classifier */
    const u8_error_t d_err
        = data_classifier_init_new ( &((*this_).private_temp_classifier),
                                     type_of_new_classifier,
                                     "",  /* stereotype */
                                     "New Element",  /* name */
                                     "",  /* description */
                                     x_order,
                                     y_order,
                                     y_order  /* y_order used also as list_order */
                                   );
    if ( d_err != U8_ERROR_NONE )
    {
        U8_LOG_ERROR_HEX("data_classifier_init_new failed in gui_sketch_object_creator_create_classifier:",d_err);
    }

    /* create classifier, adapt name if necessary: */
    {
        ctrl_multi_step_changer_t multi_stepper;
        ctrl_multi_step_changer_init( &multi_stepper, (*this_).controller, (*this_).db_reader );
        u8_error_t out_info;
        c_result = ctrl_multi_step_changer_create_classifier( &multi_stepper,
                                                              &((*this_).private_temp_classifier),
                                                              &out_info
                                                            );
        *out_classifier_id = data_classifier_get_row_id( &((*this_).private_temp_classifier) );
        ctrl_multi_step_changer_destroy( &multi_stepper );
    }

    if ( U8_ERROR_NONE == c_result )
    {
        /* get diagram controller */
        ctrl_diagram_controller_t *const diagram_control = ctrl_controller_get_diagram_control_ptr( (*this_).controller );

        data_diagramelement_t new_diagele;
        data_diagramelement_init_new ( &new_diagele,
                                       diagram_id,
                                       *out_classifier_id,
                                       DATA_DIAGRAMELEMENT_FLAG_NONE,
                                       DATA_ROW_VOID
                                     );

        c_result = ctrl_diagram_controller_create_diagramelement ( diagram_control,
                                                                   &new_diagele,
                                                                   CTRL_UNDO_REDO_ACTION_BOUNDARY_APPEND,
                                                                   out_diagramelement_id
                                                                 );

        /* cleanup */
        data_diagramelement_destroy ( &new_diagele );
    }
    else if ( U8_ERROR_READ_ONLY_DB == c_result )
    {
        /* notify read-only warning to user */
        gui_simple_message_to_user_show_message( (*this_).message_to_user,
                                                 GUI_SIMPLE_MESSAGE_TYPE_WARNING,
                                                 GUI_SIMPLE_MESSAGE_CONTENT_DB_IS_READ_ONLY
                                               );
    }

    /* cleanup */
    data_classifier_destroy ( &((*this_).private_temp_classifier) );

    U8_TRACE_END_ERR( c_result );
    return c_result;
}

u8_error_t gui_sketch_object_creator_create_classifier_as_child ( gui_sketch_object_creator_t *this_,
                                                                  data_row_t diagram_id,
                                                                  data_row_t parent_classifier_id,
                                                                  int32_t x_order,
                                                                  int32_t y_order,
                                                                  data_row_t *out_diagramelement_id,
                                                                  data_row_t *out_classifier_id,
                                                                  data_row_t *out_relationship_id )
{
    U8_TRACE_BEGIN();
    assert ( NULL != out_classifier_id );
    assert ( NULL != out_diagramelement_id );
    assert ( NULL != out_relationship_id );
    assert ( DATA_ROW_VOID != parent_classifier_id );
    assert ( DATA_ROW_VOID != diagram_id );

    u8_error_t c_result;

    c_result = gui_sketch_object_creator_create_classifier( this_,
                                                            diagram_id,
                                                            x_order,
                                                            y_order,
                                                            out_diagramelement_id,
                                                            out_classifier_id
                                                          );

    if ( U8_ERROR_NONE == c_result )
    {
        /* get classifier controller */
        ctrl_classifier_controller_t *classifier_control;
        classifier_control = ctrl_controller_get_classifier_control_ptr ( (*this_).controller );

        /* define relationship */
        const u8_error_t d_err
            = data_relationship_init_new( &((*this_).private_temp_relationship),
                                          parent_classifier_id,
                                          DATA_ROW_VOID,
                                          *out_classifier_id,
                                          DATA_ROW_VOID,
                                          DATA_RELATIONSHIP_TYPE_UML_CONTAINMENT,
                                          "", /* =stereotype */
                                          "", /* =name */
                                          "", /* =description */
                                          y_order  /* =list_order */
                                        );
        if ( d_err != U8_ERROR_NONE )
        {
            U8_LOG_ERROR_HEX("data_relationship_init failed in gui_sketch_object_creator_create_classifier_as_child:",d_err);
        }

        /* create relationship */
        c_result = ctrl_classifier_controller_create_relationship( classifier_control,
                                                                   &((*this_).private_temp_relationship),
                                                                   CTRL_UNDO_REDO_ACTION_BOUNDARY_APPEND,
                                                                   out_relationship_id
                                                                 );

        /* cleanup */
        data_relationship_destroy( &((*this_).private_temp_relationship) );
    }

    U8_TRACE_END_ERR( c_result );
    return c_result;
}

u8_error_t gui_sketch_object_creator_create_diagram ( gui_sketch_object_creator_t *this_,
                                                      data_row_t parent_diagram_id,
                                                      int32_t list_order,
                                                      data_row_t *out_diagram_id )
{
    U8_TRACE_BEGIN();
    assert ( NULL != out_diagram_id );

    u8_error_t c_result;

    ctrl_diagram_controller_t *diag_control;
    diag_control = ctrl_controller_get_diagram_control_ptr ( (*this_).controller );

    /* create the diagram */
    const u8_error_t d_err
        = data_diagram_init_new( &((*this_).private_temp_diagram),
                                 parent_diagram_id,
                                 DATA_DIAGRAM_TYPE_UML_COMPONENT_DIAGRAM,
                                 "",
                                 "New Diagram",
                                 "",
                                 list_order,
                                 DATA_DIAGRAM_FLAG_NONE
                               );
    if ( d_err != U8_ERROR_NONE )
    {
        U8_LOG_ERROR_HEX("data_diagram_init_new failed in gui_sketch_object_creator_create_diagram:",d_err);
    }

    c_result = ctrl_diagram_controller_create_diagram( diag_control,
                                                       &((*this_).private_temp_diagram),
                                                       CTRL_UNDO_REDO_ACTION_BOUNDARY_START_NEW,
                                                       out_diagram_id
                                                     );
    if ( U8_ERROR_READ_ONLY_DB == c_result )
    {
        /* notify read-only warning to user */
        gui_simple_message_to_user_show_message( (*this_).message_to_user,
                                                 GUI_SIMPLE_MESSAGE_TYPE_WARNING,
                                                 GUI_SIMPLE_MESSAGE_CONTENT_DB_IS_READ_ONLY
                                               );
    }

    /* cleanup */
    if ( d_err == U8_ERROR_NONE )
    {
        data_diagram_destroy ( &((*this_).private_temp_diagram) );
    }

    U8_TRACE_END_ERR( c_result );
    return c_result;
}

u8_error_t gui_sketch_object_creator_create_relationship ( gui_sketch_object_creator_t *this_,
                                                           data_diagram_type_t diag_type,
                                                           data_row_t from_classifier_id,
                                                           data_row_t from_feature_id,
                                                           data_row_t to_classifier_id,
                                                           data_row_t to_feature_id,
                                                           int32_t list_order,
                                                           data_row_t *out_relationship_id )
{
    U8_TRACE_BEGIN();
    assert ( NULL != out_relationship_id );
    assert ( DATA_ROW_VOID != from_classifier_id );
    assert ( DATA_ROW_VOID != to_classifier_id );

    u8_error_t c_result;

    /* get classifier controller */
    ctrl_classifier_controller_t *classifier_control;
    classifier_control = ctrl_controller_get_classifier_control_ptr( (*this_).controller );

    /* propose a type for the relationship */
    data_relationship_type_t new_rel_type = DATA_RELATIONSHIP_TYPE_UML_DEPENDENCY;
    {
        /* get type of from_classifier */
        data_classifier_type_t from_class_type = DATA_CLASSIFIER_TYPE_CLASS;
        {
            const u8_error_t clsfy_err
                = data_database_reader_get_classifier_by_id( (*this_).db_reader,
                                                             from_classifier_id,
                                                             &((*this_).private_temp_classifier)
                                                           );
            if ( U8_ERROR_NONE == clsfy_err )
            {
                from_class_type = data_classifier_get_main_type( &((*this_).private_temp_classifier) );
                data_classifier_destroy ( &((*this_).private_temp_classifier) );
            }
            else
            {
                U8_LOG_ERROR_INT( "gui_sketch_object_creator_create_relationship cannot find classifier:", from_classifier_id );
            }
        }

        /* get type of from_feature */
        data_feature_type_t from_feature_type = DATA_FEATURE_TYPE_VOID;
        if ( from_feature_id != DATA_ROW_VOID )
        {
            const u8_error_t feat_err
                = data_database_reader_get_feature_by_id( (*this_).db_reader,
                                                          from_feature_id,
                                                          &((*this_).private_temp_feature)
                                                        );
            if ( U8_ERROR_NONE == feat_err )
            {
                from_feature_type = data_feature_get_main_type( &((*this_).private_temp_feature) );
                data_feature_destroy ( &((*this_).private_temp_feature) );
            }
            else
            {
                U8_LOG_ERROR_INT( "gui_sketch_object_creator_create_relationship cannot find feature:", from_feature_id );
            }
        }
        new_rel_type = gui_sketch_defaults_get_relationship_type( &((*this_).defaults), from_class_type, from_feature_type );
    }

    /* define relationship struct */
    const u8_error_t d_err
        = data_relationship_init_new( &((*this_).private_temp_relationship),
                                      from_classifier_id,
                                      from_feature_id,
                                      to_classifier_id,
                                      to_feature_id,
                                      new_rel_type,
                                      "", /* =stereotype */
                                      "", /* =name */
                                      "", /* =description */
                                      list_order
                                    );
    if ( d_err != U8_ERROR_NONE )
    {
        U8_LOG_ERROR_HEX("data_relationship_init failed in gui_sketch_object_creator_create_relationship:",d_err);
    }

    /* check preconditions */
    const bool is_scenario = data_rules_diagram_is_scenario ( &((*this_).data_rules), diag_type )
                             && (( from_feature_id != DATA_ROW_VOID )||( to_feature_id != DATA_ROW_VOID ));
    const bool diagram_ok = is_scenario
                            ? data_rules_diagram_shows_scenario_relationships ( &((*this_).data_rules), diag_type )
                            : data_rules_diagram_shows_uncond_relationships ( &((*this_).data_rules), diag_type );

    if ( diagram_ok ) {
        /* create relationship */
        c_result = ctrl_classifier_controller_create_relationship( classifier_control,
                                                                   &((*this_).private_temp_relationship),
                                                                   CTRL_UNDO_REDO_ACTION_BOUNDARY_START_NEW,
                                                                   out_relationship_id
                                                                 );
        if ( U8_ERROR_READ_ONLY_DB == c_result )
        {
            /* notify read-only warning to user */
            gui_simple_message_to_user_show_message( (*this_).message_to_user,
                                                     GUI_SIMPLE_MESSAGE_TYPE_WARNING,
                                                     GUI_SIMPLE_MESSAGE_CONTENT_DB_IS_READ_ONLY
                                                   );
        }
    }
    else
    {
        /* notify error to user */
        gui_simple_message_to_user_show_message( (*this_).message_to_user,
                                                 GUI_SIMPLE_MESSAGE_TYPE_ERROR,
                                                 GUI_SIMPLE_MESSAGE_CONTENT_NO_RELATIONSHIPS
                                               );
        c_result = U8_ERROR_DIAGRAM_HIDES_RELATIONSHIPS;
    }

    /* cleanup */
    data_relationship_destroy( &((*this_).private_temp_relationship) );

    U8_TRACE_END_ERR( c_result );
    return c_result;
}

u8_error_t gui_sketch_object_creator_create_feature ( gui_sketch_object_creator_t *this_,
                                                      data_diagram_type_t diag_type,
                                                      data_row_t parent_classifier_id,
                                                      int32_t std_list_order,
                                                      int32_t port_list_order,
                                                      data_row_t *out_feature_id )
{
    U8_TRACE_BEGIN();
    assert ( NULL != out_feature_id );
    assert ( DATA_ROW_VOID != parent_classifier_id );

    u8_error_t c_result;

    /* get classifier controller */
    ctrl_classifier_controller_t *classifier_control;
    classifier_control = ctrl_controller_get_classifier_control_ptr( (*this_).controller );

    /* get type of parent classifier */
    data_classifier_type_t parent_class_type = DATA_CLASSIFIER_TYPE_CLASS;
    {
        const u8_error_t clsfy_err
            = data_database_reader_get_classifier_by_id( (*this_).db_reader,
                                                         parent_classifier_id,
                                                         &((*this_).private_temp_classifier)
                                                       );
        if ( U8_ERROR_NONE == clsfy_err )
        {
            parent_class_type = data_classifier_get_main_type( &((*this_).private_temp_classifier) );
            data_classifier_destroy( &((*this_).private_temp_classifier) );
        }
        else
        {
            U8_LOG_ERROR_INT( "gui_sketch_object_creator_create_feature cannot find classifier:", parent_classifier_id );
        }
    }

    /* propose a type for the feature */
    data_feature_type_t new_feature_type;
    new_feature_type = gui_sketch_defaults_get_feature_type( &((*this_).defaults), parent_class_type );

    /* select the right list_order */
    int32_t list_order;
    if ( ( DATA_FEATURE_TYPE_PROVIDED_INTERFACE == new_feature_type )
        || ( DATA_FEATURE_TYPE_REQUIRED_INTERFACE == new_feature_type )
        || ( DATA_FEATURE_TYPE_PORT == new_feature_type )
        || ( DATA_FEATURE_TYPE_IN_PORT_PIN == new_feature_type )
        || ( DATA_FEATURE_TYPE_OUT_PORT_PIN == new_feature_type )
        || ( DATA_FEATURE_TYPE_ENTRY == new_feature_type )
        || ( DATA_FEATURE_TYPE_EXIT == new_feature_type ) )
    {
        list_order = port_list_order;
    }
    else  /* DATA_FEATURE_TYPE_PROPERTY or DATA_FEATURE_TYPE_OPERATION or DATA_FEATURE_TYPE_TAGGED_VALUE */
    {
        list_order = std_list_order;
    }

    /* define feature struct */
    const u8_error_t data_err
        = data_feature_init_new( &((*this_).private_temp_feature),
                                 new_feature_type,
                                 parent_classifier_id,
                                 "New Feature",  /* name */
                                 "",  /* type/value */
                                 "",
                                 list_order
                               );
    if ( data_err != U8_ERROR_NONE )
    {
        U8_LOG_ERROR_HEX("data_feature_init failed in gui_sketch_object_creator_create_feature:",data_err);
    }

    /* check preconditions */
    const bool classifier_ok = data_rules_classifier_has_uncond_features ( &((*this_).data_rules), parent_class_type );
    const bool is_scenario = data_rules_feature_is_scenario_cond ( &((*this_).data_rules), new_feature_type );
    assert ( ! is_scenario );  /* lifelines should not be created by this function */
    const bool diagram_ok = is_scenario
                            ? data_rules_diagram_shows_scenario_features ( &((*this_).data_rules), diag_type )
                            : data_rules_diagram_shows_uncond_features ( &((*this_).data_rules), diag_type );

    if ( diagram_ok && classifier_ok )
    {
        /* create feature */
        c_result = ctrl_classifier_controller_create_feature( classifier_control,
                                                              &((*this_).private_temp_feature),
                                                              CTRL_UNDO_REDO_ACTION_BOUNDARY_START_NEW,
                                                              out_feature_id
                                                            );
        if ( U8_ERROR_READ_ONLY_DB == c_result )
        {
            /* notify read-only warning to user */
            gui_simple_message_to_user_show_message( (*this_).message_to_user,
                                                     GUI_SIMPLE_MESSAGE_TYPE_WARNING,
                                                     GUI_SIMPLE_MESSAGE_CONTENT_DB_IS_READ_ONLY
                                                   );
        }
    }
    else if ( ! classifier_ok )
    {
        /* notify error to user */
        gui_simple_message_to_user_show_message( (*this_).message_to_user,
                                                 GUI_SIMPLE_MESSAGE_TYPE_ERROR,
                                                 GUI_SIMPLE_MESSAGE_CONTENT_FEATURELESS_CLASSIFIER
                                               );
        c_result = U8_ERROR_CLASSIFIER_REFUSES_FEATURE;
    }
    else
    {
        /* notify error to user */
        gui_simple_message_to_user_show_message( (*this_).message_to_user,
                                                 GUI_SIMPLE_MESSAGE_TYPE_ERROR,
                                                 GUI_SIMPLE_MESSAGE_CONTENT_NO_FEATURES
                                               );
        c_result = U8_ERROR_DIAGRAM_HIDES_FEATURES;
    }

    /* cleanup */
    data_feature_destroy( &((*this_).private_temp_feature) );

    U8_TRACE_END_ERR( c_result );
    return c_result;
}


/*
Copyright 2017-2025 Andreas Warnke

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/