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 (C) 2023 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,
48 wxLC_REPORT | wxLC_SINGLE_SEL );
49
50 // Set up columns
51 m_listCtrl->EnableCheckBoxes();
52 m_listCtrl->AppendColumn( _( "Filename" ) );
53 m_listCtrl->AppendColumn( _( "Status" ) );
54
55 // Set column widths
56 m_listCtrl->SetColumnWidth( 0, 200 );
57 m_listCtrl->SetColumnWidth( 1, 200 );
58
59 // Set up image list for icons
60#ifdef __WXMAC__
61 // HiDPI-aware API; will be generally available in wxWidgets 3.4
62 wxVector<wxBitmapBundle> stateImages;
63 stateImages.push_back( wxBitmapBundle() ); // GIT_STATUS_UNTRACKED
64 stateImages.push_back( KiBitmapBundle( BITMAPS::git_good_check ) ); // GIT_STATUS_CURRENT
65 stateImages.push_back( KiBitmapBundle( BITMAPS::git_modified ) ); // GIT_STATUS_MODIFIED
66 stateImages.push_back( KiBitmapBundle( BITMAPS::git_add ) ); // GIT_STATUS_ADDED
67 stateImages.push_back( KiBitmapBundle( BITMAPS::git_delete ) ); // GIT_STATUS_DELETED
68 stateImages.push_back( KiBitmapBundle( BITMAPS::git_out_of_date ) ); // GIT_STATUS_BEHIND
69 stateImages.push_back( KiBitmapBundle( BITMAPS::git_changed_ahead ) ); // GIT_STATUS_AHEAD
70 stateImages.push_back( KiBitmapBundle( BITMAPS::git_conflict ) ); // GIT_STATUS_CONFLICTED
71
72 m_listCtrl->SetNormalImages( stateImages );
73 m_listCtrl->SetSmallImages( stateImages );
74#else
75 wxImageList* imageList = new wxImageList(
76 16, 16, true, static_cast<int>( KIGIT_COMMON::GIT_STATUS::GIT_STATUS_LAST ) );
77
78 imageList->Add( KiBitmap( BITMAPS::git_good_check ) ); // PLACEHOLDER
79 imageList->Add( KiBitmap( BITMAPS::git_good_check ) ); // GIT_STATUS_CURRENT
80 imageList->Add( KiBitmap( BITMAPS::git_modified ) ); // GIT_STATUS_MODIFIED
81 imageList->Add( KiBitmap( BITMAPS::git_add ) ); // GIT_STATUS_ADDED
82 imageList->Add( KiBitmap( BITMAPS::git_delete ) ); // GIT_STATUS_DELETED
83 imageList->Add( KiBitmap( BITMAPS::git_out_of_date ) ); // GIT_STATUS_BEHIND
84 imageList->Add( KiBitmap( BITMAPS::git_changed_ahead ) ); // GIT_STATUS_AHEAD
85 imageList->Add( KiBitmap( BITMAPS::git_conflict ) ); // GIT_STATUS_CONFLICTED
86
87 // Assign the image list to the list control
88 m_listCtrl->SetImageList( imageList, wxIMAGE_LIST_SMALL );
89#endif
90
91 // Populate list control with items
92 for( auto& [filename, status] : filesToCommit )
93 {
94 int i = m_listCtrl->GetItemCount();
95 m_listCtrl->InsertItem( i, filename );
96
97 if( status & ( GIT_STATUS_INDEX_NEW | GIT_STATUS_WT_NEW ) )
98 {
99 m_listCtrl->SetItem( i, 1, _( "New" ) );
100 m_listCtrl->SetItemImage(
101 i, static_cast<int>( KIGIT_COMMON::GIT_STATUS::GIT_STATUS_ADDED ) );
102 }
103 else if( status & ( GIT_STATUS_INDEX_MODIFIED | GIT_STATUS_WT_MODIFIED ) )
104 {
105 m_listCtrl->SetItem( i, 1, _( "Modified" ) );
106 m_listCtrl->SetItemImage(
107 i, static_cast<int>( KIGIT_COMMON::GIT_STATUS::GIT_STATUS_MODIFIED ) );
108 }
109 else if( status & ( GIT_STATUS_INDEX_DELETED | GIT_STATUS_WT_DELETED ) )
110 {
111 m_listCtrl->SetItem( i, 1, _( "Deleted" ) );
112 m_listCtrl->SetItemImage(
113 i, static_cast<int>( KIGIT_COMMON::GIT_STATUS::GIT_STATUS_DELETED ) );
114 }
115 else
116 {
117 printf( " Unknown status: %d\n", status );
118 }
119 }
120
121 sizer->Add( m_listCtrl, 1, wxEXPAND | wxALL, 5 );
122
123 // Commit Message Text Control
124 wxStaticText* commitMessageLabel = new wxStaticText( this, wxID_ANY, _( "Commit Message:" ) );
126 new wxTextCtrl( this, wxID_ANY, "", wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE );
127 sizer->Add( commitMessageLabel, 0, wxALL, 5 );
128 sizer->Add( m_commitMessageTextCtrl, 1, wxEXPAND | wxALL, 5 );
129
130 // Author Name and Email Text Control
131 wxStaticText* authorLabel = new wxStaticText( this, wxID_ANY, _( "Author:" ) );
132 wxString defaultAuthor = defaultAuthorName + " <" + defaultAuthorEmail + ">";
134 new wxTextCtrl( this, wxID_ANY, defaultAuthor, wxDefaultPosition, wxDefaultSize, 0 );
135 sizer->Add( authorLabel, 0, wxALL, 5 );
136 sizer->Add( m_authorTextCtrl, 0, wxEXPAND | wxALL, 5 );
137
138 // OK and Cancel Buttons
139
140 wxStdDialogButtonSizer* buttonSizer = new wxStdDialogButtonSizer();
141
142 m_okButton = new wxButton( this, wxID_OK, _( "OK" ) );
143 wxButton* cancelButton = new wxButton( this, wxID_CANCEL, _( "Cancel" ) );
144 buttonSizer->Add( cancelButton, 0, wxALL, 5 );
145 buttonSizer->Add( m_okButton, 0, wxALL, 5 );
146 buttonSizer->Realize();
147
148 sizer->Add( buttonSizer, 0, wxALIGN_RIGHT | wxALL, 5 );
149
150 SetSizerAndFit( sizer );
151
152 SetupStandardButtons( { { wxID_OK, _( "C&ommit" ) } } );
153
154 // Bind events
155 Bind( wxEVT_TEXT, &DIALOG_GIT_COMMIT::OnTextChanged, this, m_commitMessageTextCtrl->GetId() );
156
157 // Set the repository and defaults
158 m_repo = repo;
159 m_defaultAuthorName = defaultAuthorName;
160 m_defaultAuthorEmail = defaultAuthorEmail;
161}
162
163
164void DIALOG_GIT_COMMIT::OnTextChanged( wxCommandEvent& aEvent )
165{
166 if( m_commitMessageTextCtrl->GetValue().IsEmpty() )
167 {
168 m_okButton->Disable();
169 m_okButton->SetToolTip( _( "Commit message cannot be empty" ) );
170 }
171 else
172 {
173 m_okButton->Enable();
174 m_okButton->SetToolTip( wxEmptyString );
175 }
176}
177
178
180{
181 return m_commitMessageTextCtrl->GetValue();
182}
183
184
186{
187 wxString authorText = m_authorTextCtrl->GetValue();
188 size_t pos = authorText.find( '<' );
189
190 if( pos != wxString::npos )
191 return authorText.substr( 0, pos ).Trim();
192
193 return wxEmptyString;
194}
195
196
198{
199 wxString authorText = m_authorTextCtrl->GetValue();
200 size_t startPos = authorText.find( '<' );
201 size_t endPos = authorText.find( '>' );
202
203 if( startPos != wxString::npos && endPos != wxString::npos && startPos < endPos )
204 return authorText.substr( startPos + 1, endPos - startPos - 1 ).Trim();
205
206 return wxEmptyString;
207}
208
209
210std::vector<wxString> DIALOG_GIT_COMMIT::GetSelectedFiles() const
211{
212 std::vector<wxString> selectedFiles;
213
214 long item = -1;
215 while( ( item = m_listCtrl->GetNextItem( item, wxLIST_NEXT_ALL ) )
216 != -1 )
217 {
218 if( m_listCtrl->IsItemChecked( item ) )
219 selectedFiles.push_back( m_listCtrl->GetItemText( item ) );
220 }
221
222 return selectedFiles;
223}
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap)
Definition: bitmap.cpp:110
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
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)
wxString GetAuthorEmail() const
git_repository * m_repo
std::vector< wxString > GetSelectedFiles() const
wxString m_defaultAuthorEmail
wxString GetAuthorName() const
wxListCtrl * m_listCtrl
wxString m_defaultAuthorName
Dialog helper object to sit in the inheritance tree between wxDialog and any class written by wxFormB...
Definition: dialog_shim.h:84
void SetupStandardButtons(std::map< int, wxString > aLabels={})
#define _(s)