KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dialog_restore_local_history.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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 3
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
21
22#include <wx/button.h>
23#include <wx/listctrl.h>
24#include <wx/sizer.h>
25#include <wx/textctrl.h>
26
27
29 wxWindow* aParent, const std::vector<LOCAL_HISTORY_SNAPSHOT_INFO>& aSnapshots ) :
30 DIALOG_SHIM( aParent, wxID_ANY, _( "Restore Project from Local History..." ) ),
31 m_snapshots( aSnapshots )
32{
33 BuildUi();
34 Populate();
35 Centre();
36}
37
38
40{
41 m_list = new wxListCtrl( this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
42 wxLC_HRULES | wxLC_REPORT | wxLC_SINGLE_SEL | wxLC_VRULES );
43 m_list->AppendColumn( _( "Time" ) );
44 m_list->AppendColumn( _( "Action" ) );
45 m_list->AppendColumn( _( "Count" ) );
46
47 m_details = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize,
48 wxTE_MULTILINE | wxTE_READONLY );
49
50 wxStdDialogButtonSizer* buttons = new wxStdDialogButtonSizer();
51 m_restoreButton = new wxButton( this, wxID_OK, _( "Restore" ) );
52 m_restoreButton->Enable( false );
54 buttons->AddButton( m_restoreButton );
55 buttons->AddButton( new wxButton( this, wxID_CANCEL ) );
56 buttons->Realize();
57
58 wxBoxSizer* topSizer = new wxBoxSizer( wxVERTICAL );
59 topSizer->Add( m_list, 1, wxEXPAND | wxALL, 5 );
60 topSizer->Add( m_details, 1, wxEXPAND | wxLEFT | wxRIGHT | wxBOTTOM, 5 );
61 topSizer->Add( buttons, 0, wxEXPAND | wxALL, 5 );
62 SetSizerAndFit( topSizer );
63
64 SetMinSize( FromDIP( wxSize( 700, 500 ) ) );
65
66 m_list->Bind( wxEVT_LIST_ITEM_SELECTED, &DIALOG_RESTORE_LOCAL_HISTORY::OnSelectionChanged, this );
67 m_list->Bind( wxEVT_LIST_ITEM_ACTIVATED, &DIALOG_RESTORE_LOCAL_HISTORY::OnItemActivated, this );
68}
69
70
72{
73 for( size_t i = 0; i < m_snapshots.size(); ++i )
74 {
75 const auto& snapshot = m_snapshots[i];
76 long row = m_list->InsertItem( m_list->GetItemCount(), snapshot.date.FormatISOCombined() );
77 wxString countText =
78 snapshot.filesChanged > 0 ? wxString::Format( "%d", snapshot.filesChanged ) : wxString( "-" );
79
80 m_list->SetItem( row, 1, snapshot.summary );
81
82 m_list->SetItem( row, 2, countText );
83 }
84
85 m_list->SetColumnWidth( 0, FromDIP( 170 ) );
86 m_list->SetColumnWidth( 1, FromDIP( 380 ) );
87 m_list->SetColumnWidth( 2, FromDIP( 70 ) );
88
89 m_details->Clear();
90 m_selectedIndex = wxNOT_FOUND;
91 m_selectedHash.clear();
92
93 if( m_restoreButton )
94 m_restoreButton->Enable( false );
95}
96
97
99{
100 if( aIndex < 0 || aIndex >= static_cast<long>( m_snapshots.size() ) )
101 return;
102
103 const auto& snapshot = m_snapshots[aIndex];
104
105 wxString text;
106 text << snapshot.summary << "\n";
107 text << snapshot.date.FormatISOCombined() << "\n";
108 text << snapshot.hash;
109
110 if( !snapshot.changedFiles.empty() )
111 text << "\n\n";
112
113 for( size_t ii = 0; ii < snapshot.changedFiles.size(); ++ii )
114 {
115 if( ii > 0 )
116 text << "\n";
117
118 text << snapshot.changedFiles[ii];
119 }
120
121 m_details->SetValue( text );
122}
123
124
126{
127 if( m_selectedIndex == wxNOT_FOUND || m_selectedIndex >= static_cast<long>( m_snapshots.size() ) )
128 {
129 return wxEmptyString;
130 }
131
132 return m_snapshots[m_selectedIndex].hash;
133}
134
135
137{
138 long selected = m_list->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
139
140 if( selected == wxNOT_FOUND )
141 {
142 m_selectedIndex = wxNOT_FOUND;
143 m_selectedHash.clear();
144 m_details->Clear();
145
146 if( m_restoreButton )
147 m_restoreButton->Enable( false );
148 }
149 else
150 {
151 m_selectedIndex = selected;
152 m_selectedHash = m_snapshots[selected].hash;
153 UpdateDetails( selected );
154
155 if( m_restoreButton )
156 m_restoreButton->Enable( true );
157 }
158
159 aEvent.Skip();
160}
161
162
164{
165 long index = aEvent.GetIndex();
166
167 if( index >= 0 && index < static_cast<long>( m_snapshots.size() ) )
168 {
172 }
173
174 if( !m_selectedHash.IsEmpty() )
175 EndModal( wxID_OK );
176}
177
178
180{
181 long index = m_list->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
182
183 if( index == wxNOT_FOUND )
184 index = m_list->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_FOCUSED );
185
186 if( index == wxNOT_FOUND || index >= static_cast<long>( m_snapshots.size() ) )
187 return;
188
191
192 aEvent.Skip();
193}
int index
DIALOG_RESTORE_LOCAL_HISTORY(wxWindow *aParent, const std::vector< LOCAL_HISTORY_SNAPSHOT_INFO > &aSnapshots)
void OnRestoreClicked(wxCommandEvent &aEvent)
const std::vector< LOCAL_HISTORY_SNAPSHOT_INFO > & m_snapshots
DIALOG_SHIM(wxWindow *aParent, wxWindowID id, const wxString &title, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=wxDEFAULT_FRAME_STYLE|wxRESIZE_BORDER, const wxString &name=wxDialogNameStr)
#define _(s)