KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dialog_git_commit.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, you may find one here:
18 * http://www.gnu.org/licenses/gpl-3.0.html
19 * or you may search the http://www.gnu.org website for the version 3 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#include "dialog_git_commit.h"
25
29
30#include <wx/button.h>
31#include <wx/checkbox.h>
32#include <wx/listctrl.h>
33#include <wx/sizer.h>
34#include <wx/stattext.h>
35#include <wx/textctrl.h>
36
37DIALOG_GIT_COMMIT::DIALOG_GIT_COMMIT( wxWindow* parent, git_repository* repo,
38 const wxString& defaultAuthorName,
39 const wxString& defaultAuthorEmail,
40 const std::map<wxString, int>& filesToCommit ) :
41 DIALOG_SHIM( parent, wxID_ANY, _( "Commit Changes" ) )
42{
43 wxBoxSizer* sizer = new wxBoxSizer( wxVERTICAL );
44
45
46 // List Control for files to commit
47 m_listCtrl = new wxListCtrl( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT );
48
49 // Set up columns
50 m_listCtrl->EnableCheckBoxes();
51 m_listCtrl->AppendColumn( _( "Filename" ) );
52 m_listCtrl->AppendColumn( _( "Status" ) );
53
54 // Set column widths
55 m_listCtrl->SetColumnWidth( 0, 200 );
56 m_listCtrl->SetColumnWidth( 1, 200 );
57
58 // Set up image list for icons
59#ifdef __WXMAC__
60 // HiDPI-aware API; will be generally available in wxWidgets 3.4
61 wxVector<wxBitmapBundle> stateImages;
62 stateImages.push_back( wxBitmapBundle() ); // GIT_STATUS_UNTRACKED
63 stateImages.push_back( KiBitmapBundle( BITMAPS::git_good_check ) ); // GIT_STATUS_CURRENT
64 stateImages.push_back( KiBitmapBundle( BITMAPS::git_modified ) ); // GIT_STATUS_MODIFIED
65 stateImages.push_back( KiBitmapBundle( BITMAPS::git_add ) ); // GIT_STATUS_ADDED
66 stateImages.push_back( KiBitmapBundle( BITMAPS::git_delete ) ); // GIT_STATUS_DELETED
67 stateImages.push_back( KiBitmapBundle( BITMAPS::git_out_of_date ) ); // GIT_STATUS_BEHIND
68 stateImages.push_back( KiBitmapBundle( BITMAPS::git_changed_ahead ) ); // GIT_STATUS_AHEAD
69 stateImages.push_back( KiBitmapBundle( BITMAPS::git_conflict ) ); // GIT_STATUS_CONFLICTED
70
71 m_listCtrl->SetNormalImages( stateImages );
72 m_listCtrl->SetSmallImages( stateImages );
73#else
74 wxImageList* imageList = new wxImageList(
75 16, 16, true, static_cast<int>( KIGIT_COMMON::GIT_STATUS::GIT_STATUS_LAST ) );
76
77 imageList->Add( KiBitmap( BITMAPS::git_good_check ) ); // PLACEHOLDER
78 imageList->Add( KiBitmap( BITMAPS::git_good_check ) ); // GIT_STATUS_CURRENT
79 imageList->Add( KiBitmap( BITMAPS::git_modified ) ); // GIT_STATUS_MODIFIED
80 imageList->Add( KiBitmap( BITMAPS::git_add ) ); // GIT_STATUS_ADDED
81 imageList->Add( KiBitmap( BITMAPS::git_delete ) ); // GIT_STATUS_DELETED
82 imageList->Add( KiBitmap( BITMAPS::git_out_of_date ) ); // GIT_STATUS_BEHIND
83 imageList->Add( KiBitmap( BITMAPS::git_changed_ahead ) ); // GIT_STATUS_AHEAD
84 imageList->Add( KiBitmap( BITMAPS::git_conflict ) ); // GIT_STATUS_CONFLICTED
85
86 // Assign the image list to the list control
87 m_listCtrl->SetImageList( imageList, wxIMAGE_LIST_SMALL );
88#endif
89
90 // Populate list control with items
91 for( auto& [filename, status] : filesToCommit )
92 {
93 int i = m_listCtrl->GetItemCount();
94 m_listCtrl->InsertItem( i, filename );
95
96 if( status & ( GIT_STATUS_INDEX_NEW | GIT_STATUS_WT_NEW ) )
97 {
98 m_listCtrl->SetItem( i, 1, _( "New" ) );
99 m_listCtrl->SetItemImage(
100 i, static_cast<int>( KIGIT_COMMON::GIT_STATUS::GIT_STATUS_ADDED ) );
101
102 if( status & ( GIT_STATUS_INDEX_NEW ) )
103 m_listCtrl->CheckItem( i, true );
104 }
105 else if( status & ( GIT_STATUS_INDEX_MODIFIED | GIT_STATUS_WT_MODIFIED ) )
106 {
107 m_listCtrl->SetItem( i, 1, _( "Modified" ) );
108 m_listCtrl->SetItemImage(
109 i, static_cast<int>( KIGIT_COMMON::GIT_STATUS::GIT_STATUS_MODIFIED ) );
110
111 if( status & ( GIT_STATUS_INDEX_MODIFIED ) )
112 m_listCtrl->CheckItem( i, true );
113 }
114 else if( status & ( GIT_STATUS_INDEX_DELETED | GIT_STATUS_WT_DELETED ) )
115 {
116 m_listCtrl->SetItem( i, 1, _( "Deleted" ) );
117 m_listCtrl->SetItemImage(
118 i, static_cast<int>( KIGIT_COMMON::GIT_STATUS::GIT_STATUS_DELETED ) );
119
120 if( status & ( GIT_STATUS_INDEX_DELETED ) )
121 m_listCtrl->CheckItem( i, true );
122 }
123 else
124 {
125 printf( " Unknown status: %d\n", status );
126 }
127 }
128
129 sizer->Add( m_listCtrl, 1, wxEXPAND | wxALL, 5 );
130
131 // Commit Message Text Control
132 wxStaticText* commitMessageLabel = new wxStaticText( this, wxID_ANY, _( "Commit Message:" ) );
134 new wxTextCtrl( this, wxID_ANY, "", wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE );
135 sizer->Add( commitMessageLabel, 0, wxALL, 5 );
136 sizer->Add( m_commitMessageTextCtrl, 1, wxEXPAND | wxALL, 5 );
137
138 // Author Name and Email Text Control
139 wxStaticText* authorLabel = new wxStaticText( this, wxID_ANY, _( "Author:" ) );
140 wxString defaultAuthor = defaultAuthorName + " <" + defaultAuthorEmail + ">";
142 new wxTextCtrl( this, wxID_ANY, defaultAuthor, wxDefaultPosition, wxDefaultSize, 0 );
143 sizer->Add( authorLabel, 0, wxALL, 5 );
144 sizer->Add( m_authorTextCtrl, 0, wxEXPAND | wxALL, 5 );
145
146 // OK and Cancel Buttons
147
148 wxStdDialogButtonSizer* buttonSizer = new wxStdDialogButtonSizer();
149
150 m_okButton = new wxButton( this, wxID_OK, _( "OK" ) );
151 wxButton* cancelButton = new wxButton( this, wxID_CANCEL, _( "Cancel" ) );
152 buttonSizer->Add( cancelButton, 0, wxALL, 5 );
153 buttonSizer->Add( m_okButton, 0, wxALL, 5 );
154 buttonSizer->Realize();
155
156 sizer->Add( buttonSizer, 0, wxALIGN_RIGHT | wxALL, 5 );
157
158 SetSizerAndFit( sizer );
159
160 SetupStandardButtons( { { wxID_OK, _( "C&ommit" ) } } );
161
162 // Bind events
163 Bind( wxEVT_TEXT, &DIALOG_GIT_COMMIT::OnTextChanged, this, m_commitMessageTextCtrl->GetId() );
164 Bind( wxEVT_LIST_ITEM_CHECKED, &DIALOG_GIT_COMMIT::OnItemChecked, this, m_listCtrl->GetId() );
165 Bind( wxEVT_LIST_ITEM_UNCHECKED, &DIALOG_GIT_COMMIT::OnItemUnchecked, this, m_listCtrl->GetId() );
166
167 // Set the repository and defaults
168 m_repo = repo;
169 m_defaultAuthorName = defaultAuthorName;
170 m_defaultAuthorEmail = defaultAuthorEmail;
171}
172
173
174void DIALOG_GIT_COMMIT::OnTextChanged( wxCommandEvent& aEvent )
175{
176 if( m_commitMessageTextCtrl->GetValue().IsEmpty() )
177 {
178 m_okButton->Disable();
179 m_okButton->SetToolTip( _( "Commit message cannot be empty" ) );
180 }
181 else
182 {
183 m_okButton->Enable();
184 m_okButton->SetToolTip( wxEmptyString );
185 }
186}
187
188
190{
191 return m_commitMessageTextCtrl->GetValue();
192}
193
194
196{
197 wxString authorText = m_authorTextCtrl->GetValue();
198 size_t pos = authorText.find( '<' );
199
200 if( pos != wxString::npos )
201 return authorText.substr( 0, pos ).Trim();
202
203 return wxEmptyString;
204}
205
206
208{
209 wxString authorText = m_authorTextCtrl->GetValue();
210 size_t startPos = authorText.find( '<' );
211 size_t endPos = authorText.find( '>' );
212
213 if( startPos != wxString::npos && endPos != wxString::npos && startPos < endPos )
214 return authorText.substr( startPos + 1, endPos - startPos - 1 ).Trim();
215
216 return wxEmptyString;
217}
218
219
220std::vector<wxString> DIALOG_GIT_COMMIT::GetSelectedFiles() const
221{
222 std::vector<wxString> selectedFiles;
223
224 long item = -1;
225
226 while( ( item = m_listCtrl->GetNextItem( item, wxLIST_NEXT_ALL ) ) != -1 )
227 {
228 if( m_listCtrl->IsItemChecked( item ) )
229 selectedFiles.push_back( m_listCtrl->GetItemText( item ) );
230 }
231
232 return selectedFiles;
233}
234
235
236void DIALOG_GIT_COMMIT::OnItemChecked( wxListEvent& aEvent )
237{
238 long checkedItem = aEvent.GetIndex();
239
240 if( m_listCtrl->GetItemState( checkedItem, wxLIST_STATE_SELECTED ) )
241 {
242 long item = -1;
243
244 while( ( item = m_listCtrl->GetNextItem( item, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED ) ) != -1 )
245 {
246 if( item != checkedItem )
247 m_listCtrl->CheckItem( item, true );
248 }
249 }
250}
251
252
253void DIALOG_GIT_COMMIT::OnItemUnchecked( wxListEvent& aEvent )
254{
255 long uncheckedItem = aEvent.GetIndex();
256
257 if( m_listCtrl->GetItemState( uncheckedItem, wxLIST_STATE_SELECTED ) )
258 {
259 long item = -1;
260
261 while( ( item = m_listCtrl->GetNextItem( item, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED ) ) != -1 )
262 {
263 if( item != uncheckedItem )
264 m_listCtrl->CheckItem( item, false );
265 }
266 }
267}
wxBitmap KiBitmap(BITMAPS aBitmap, int aHeightTag)
Construct a wxBitmap from an image identifier Returns the image from the active theme if the image ha...
Definition bitmap.cpp:104
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition bitmap.cpp:110
@ git_out_of_date
@ git_changed_ahead
wxTextCtrl * m_commitMessageTextCtrl
void OnTextChanged(wxCommandEvent &event)
wxString GetCommitMessage() const
wxTextCtrl * m_authorTextCtrl
DIALOG_GIT_COMMIT(wxWindow *parent, git_repository *repo, const wxString &defaultAuthorName, const wxString &defaultAuthorEmail, const std::map< wxString, int > &filesToCommit)
void OnItemChecked(wxListEvent &event)
void OnItemUnchecked(wxListEvent &event)
wxString GetAuthorEmail() const
git_repository * m_repo
std::vector< wxString > GetSelectedFiles() const
wxString GetAuthorName() const
wxListCtrl * m_listCtrl
void SetupStandardButtons(std::map< int, wxString > aLabels={})
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)