dialog_erc.cpp 10.3 KB
Newer Older
1 2
/////////////////////////////////////////////////////////////////////////////
// Name:        dialog_erc.cpp
3
// Purpose:
4
// Author:      jean-pierre Charras
5
// Modified by:
6
// Created:     02/07/2000
7
// License:     GPL
8 9 10
/////////////////////////////////////////////////////////////////////////////
#include "fctsys.h"
#include "common.h"
11
#include "class_drawpanel.h"
12 13
#include "bitmaps.h"

14 15 16
#include "program.h"
#include "general.h"
#include "netlist.h"
17
#include "class_marker_sch.h"
18
#include "class_pin.h"
19 20 21
#include "protos.h"

#include "dialog_erc.h"
22
#include "dialog_erc_listbox.h"
23
#include "erc.h"
24 25


26
BEGIN_EVENT_TABLE( DIALOG_ERC, DIALOG_ERC_BASE )
27 28 29
    EVT_COMMAND_RANGE( ID_MATRIX_0, ID_MATRIX_0 + ( PIN_NMAX * PIN_NMAX ) - 1,
                       wxEVT_COMMAND_BUTTON_CLICKED,
                       DIALOG_ERC::ChangeErrorLevel )
30 31
END_EVENT_TABLE()

32 33
DIALOG_ERC::DIALOG_ERC( WinEDA_SchematicFrame* parent ) :
    DIALOG_ERC_BASE( parent )
34
{
35 36
    m_Parent = parent;
    Init();
37

38
    GetSizer()->SetSizeHints( this );
39
    Centre();
40 41
}

42

43
void DIALOG_ERC::Init()
44
{
45
    SetFocus();
46

47 48 49 50
    m_Initialized = FALSE;
    for( int ii = 0; ii < PIN_NMAX; ii++ )
        for( int jj = 0; jj < PIN_NMAX; jj++ )
            m_ButtonList[ii][jj] = NULL;
51

52
    m_WriteResultOpt->SetValue( WriteFichierERC );
53

54 55 56
    wxString num;
    num.Printf( wxT( "%d" ), g_EESchemaVar.NbErrorErc );
    m_TotalErrCount->SetLabel( num );
57

58 59 60
    num.Printf( wxT(
                    "%d" ), g_EESchemaVar.NbErrorErc -
                g_EESchemaVar.NbWarningErc );
61
    m_LastErrCount->SetLabel( num );
62

63 64
    num.Printf( wxT( "%d" ), g_EESchemaVar.NbWarningErc );
    m_LastWarningCount->SetLabel( num );
65

66
    DisplayERC_MarkersList();
67

68 69
    // Init Panel Matrix
    ReBuildMatrixPanel();
70 71
}

72

73
/* wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_ERASE_DRC_MARKERS */
74
void DIALOG_ERC::OnEraseDrcMarkersClick( wxCommandEvent& event )
75
{
76 77
/* Delete the old ERC markers, over the whole hierarchy
 */
78
    DeleteAllMarkers( MARK_ERC );
79
    m_MarkersList->ClearList();
80
    m_Parent->DrawPanel->Refresh();
81 82
}

83

84
/* wxEVT_COMMAND_BUTTON_CLICKED event handler for wxID_CANCEL */
85
void DIALOG_ERC::OnCancelClick( wxCommandEvent& event )
86
{
87
    EndModal( 0 );
88 89
}

90

91
/* wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_RESET_MATRIX */
92
void DIALOG_ERC::OnResetMatrixClick( wxCommandEvent& event )
93
{
94
    ResetDefaultERCDiag( event );
95 96
}

97

98
/* wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_ERC_CMP */
99
void DIALOG_ERC::OnErcCmpClick( wxCommandEvent& event )
100
{
101
    wxBusyCursor();
102
    m_MarkersList->Clear();
103
    m_MessagesList->Clear();
104
    wxSafeYield();      // m_MarkersList must be redraw
105 106
    wxArrayString messageList;
    TestErc( &messageList );
107 108
    for( unsigned ii = 0; ii < messageList.GetCount(); ii++ )
        m_MessagesList->AppendText( messageList[ii] );
109 110 111 112 113 114
}


// Double click on a marker info:
void DIALOG_ERC::OnLeftDClickMarkersList( wxCommandEvent& event )
{
115
    int index = m_MarkersList->GetSelection();
116 117 118 119

    if( index < 0 )
        return;

120
    const SCH_MARKER* marker = m_MarkersList->GetItem( (unsigned) index );
121 122 123 124 125

    EndModal( 1 );


    // Search for the selected marker
126 127 128 129
    SCH_SHEET_PATH* sheet;
    bool            NotFound;
    wxPoint         pos = marker->m_Pos;
    wxPoint         curpos, old_cursor_position;
130

131
    SCH_SHEET_LIST  SheetList;
132 133 134

    NotFound = TRUE;
    /* Search for the selected marker */
135 136 137
    for( sheet = SheetList.GetFirst();
        sheet != NULL;
        sheet = SheetList.GetNext() )
138
    {
139
        SCH_ITEM* item = (SCH_ITEM*) sheet->LastDrawList();
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
        while( item && NotFound )
        {
            if( item == marker )
            {
                NotFound = FALSE;
                break;
            }
            item = item->Next();
        }

        if( NotFound == false )
            break;
    }


    if( NotFound ) // Error
    {
        wxMessageBox( wxT( "OnLeftDClickMarkersList() error: Marker Not Found" ) );
        return;
    }

    if( sheet != m_Parent->GetSheet() )
    {
        sheet->LastScreen()->SetZoom( m_Parent->GetScreen()->GetZoom() );
        *m_Parent->m_CurrentSheet = *sheet;
165
        ActiveScreen = m_Parent->m_CurrentSheet->LastScreen();
166 167 168 169 170
        m_Parent->m_CurrentSheet->UpdateAllScreenReferences();
    }

    sheet->LastScreen()->m_Curseur = pos;
    m_Parent->Recadre_Trace( true );
171 172 173
}


174
/* Build or rebuild the panel showing the ERC conflict matrix
175
 */
176 177
void DIALOG_ERC::ReBuildMatrixPanel()
{
178 179
    int           ii, jj, event_id, text_height;
    wxPoint       pos, BoxMatrixPosition;
180

181 182 183 184 185
#define BITMAP_SIZE 19
    int           bitmap_size = BITMAP_SIZE;
    wxStaticText* text;
    int           x, y;
    wxSize        BoxMatrixMinSize;
186

187 188 189 190 191
    if( !DiagErcTableInit )
    {
        memcpy( DiagErc, DefaultDiagErc, sizeof(DefaultDiagErc) );
        DiagErcTableInit = TRUE;
    }
192

193 194
    // Get the current text size: this is a dummy text.
    text = new wxStaticText( m_PanelERCOptions, -1, wxT( "W" ), pos );
195

196 197 198
    text_height = text->GetRect().GetHeight();
    bitmap_size = MAX( bitmap_size, text_height );
    SAFE_DELETE( text );
199

200 201 202 203 204
    // compute the Y pos interval:
    BoxMatrixMinSize.y = ( bitmap_size * (PIN_NMAX + 1) ) + 5;
    GetSizer()->Fit( this );
    GetSizer()->SetSizeHints( this );
    pos = m_MatrixSizer->GetPosition();
205

206 207 208 209
    // Size computation is not made in constructor, in some wxWidgets version,
    // and m_BoxSizerForERC_Opt position is always 0,0. and we can't use it
    pos.x = MAX( pos.x, 5 );
    pos.y = MAX( pos.y, m_ResetOptButton->GetRect().GetHeight() + 30 );
210

211 212 213 214 215 216 217 218 219
    BoxMatrixPosition = pos;

    pos.y += text_height;

    if( m_Initialized == FALSE )
    {
        for( ii = 0; ii < PIN_NMAX; ii++ )
        {
            y    = pos.y + (ii * bitmap_size);
220 221
            text = new wxStaticText( m_PanelERCOptions, -1, CommentERC_H[ii],
                                     wxPoint( 5, y ) );
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241

            x     = text->GetRect().GetRight();
            pos.x = MAX( pos.x, x );
        }

        pos.x += 5;
    }
    else
        pos = m_ButtonList[0][0]->GetPosition();

    for( ii = 0; ii < PIN_NMAX; ii++ )
    {
        y = pos.y + (ii * bitmap_size);
        for( jj = 0; jj <= ii; jj++ )
        {
            int diag = DiagErc[ii][jj];
            x = pos.x + (jj * bitmap_size);
            if( (ii == jj) && !m_Initialized )
            {
                wxPoint txtpos;
242 243 244 245 246 247 248 249 250
                txtpos.x = x + 6;
                txtpos.y = y - bitmap_size;
                text     = new wxStaticText( m_PanelERCOptions,
                                             -1,
                                             CommentERC_V[ii],
                                             txtpos );

                BoxMatrixMinSize.x = MAX( BoxMatrixMinSize.x,
                                          text->GetRect().GetRight() );
251
            }
252
            event_id = ID_MATRIX_0 + ii + ( jj * PIN_NMAX );
253 254 255 256 257 258
            delete m_ButtonList[ii][jj];

            switch( diag )
            {
            case OK:
                m_ButtonList[ii][jj] = new wxBitmapButton( m_PanelERCOptions,
259 260 261
                                                           event_id,
                                                           wxBitmap( erc_green_xpm ),
                                                           wxPoint( x, y ) );
262 263 264 265 266

                break;

            case WAR:
                m_ButtonList[ii][jj] = new wxBitmapButton( m_PanelERCOptions,
267 268 269
                                                           event_id,
                                                           wxBitmap( warning_xpm ),
                                                           wxPoint( x, y ) );
270 271 272 273 274

                break;

            case ERR:
                m_ButtonList[ii][jj] = new wxBitmapButton( m_PanelERCOptions,
275 276 277
                                                           event_id,
                                                           wxBitmap( error_xpm ),
                                                           wxPoint( x, y ) );
278 279 280 281 282 283 284 285 286 287 288 289 290 291

                break;
            }
        }
    }

    if( !m_Initialized )
    {
        BoxMatrixMinSize.x += 5;
        m_MatrixSizer->SetMinSize( BoxMatrixMinSize );
        BoxMatrixMinSize.y += BoxMatrixPosition.y;
        m_PanelMatrixSizer->SetMinSize( BoxMatrixMinSize );
    }
    m_Initialized = TRUE;
292
}
293

294 295 296 297 298 299

/** Function DisplayERC_MarkersList
 * read the schematic and display the list of ERC markers
 */
void DIALOG_ERC::DisplayERC_MarkersList()
{
300
    SCH_SHEET_LIST SheetList;
301

302
    m_MarkersList->ClearList();
303

304
    for( SCH_SHEET_PATH* Sheet = SheetList.GetFirst();
305 306
        Sheet != NULL;
        Sheet = SheetList.GetNext() )
307 308 309 310
    {
        SCH_ITEM* DrawStruct = Sheet->LastDrawList();
        for( ; DrawStruct != NULL; DrawStruct = DrawStruct->Next() )
        {
311
            if( DrawStruct->Type() != TYPE_SCH_MARKER )
312 313
                continue;

314
            SCH_MARKER* Marker = (SCH_MARKER*) DrawStruct;
315 316 317 318
            if( Marker->GetMarkerType() != MARK_ERC )
                continue;

            /* Display diag */
319 320

//            wxString msg;
321 322
//            msg.Printf( _( "<b>sheet %s</b><ul>\n" ),
// Sheet->PathHumanReadable().GetData() );
323
//            msg += Marker->GetReporter().ShowHtml();
324 325
//            m_MarkersList->Append( msg );
            m_MarkersList->AppendToList( Marker );
326 327 328 329 330
        }
    }
}


331
/* Resets the default values of the ERC matrix.
332
 */
333 334
void DIALOG_ERC::ResetDefaultERCDiag( wxCommandEvent& event )
{
335 336 337 338 339 340 341
    memcpy( DiagErc, DefaultDiagErc, sizeof(DiagErc) );
    ReBuildMatrixPanel();
}


/* Change the error level for the pressed button, on the matrix table
 */
342 343
void DIALOG_ERC::ChangeErrorLevel( wxCommandEvent& event )
{
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
    int             id, level, ii, x, y;
    wxBitmapButton* Butt;
    const char**    new_bitmap_xpm = NULL;
    wxPoint         pos;

    id   = event.GetId();
    ii   = id - ID_MATRIX_0;
    Butt = (wxBitmapButton*) event.GetEventObject();
    pos  = Butt->GetPosition();

    x = ii / PIN_NMAX; y = ii % PIN_NMAX;

    level = DiagErc[y][x];

    switch( level )
    {
    case OK:
        level = WAR;
        new_bitmap_xpm = warning_xpm;
        break;

    case WAR:
        level = ERR;
        new_bitmap_xpm = error_xpm;
        break;

    case ERR:
        level = OK;
        new_bitmap_xpm = erc_green_xpm;
        break;
    }

    if( new_bitmap_xpm )
    {
        delete Butt;
        Butt = new wxBitmapButton( m_PanelERCOptions, id,
                                   wxBitmap( new_bitmap_xpm ), pos );

        m_ButtonList[y][x] = Butt;
        DiagErc[y][x] = DiagErc[x][y] = level;
    }
}