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,
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 if( status & ( GIT_STATUS_INDEX_NEW ) )
104 m_listCtrl->CheckItem( i, true );
105 }
106 else if( status & ( GIT_STATUS_INDEX_MODIFIED | GIT_STATUS_WT_MODIFIED ) )
107 {
108 m_listCtrl->SetItem( i, 1, _( "Modified" ) );
109 m_listCtrl->SetItemImage(
110 i, static_cast<int>( KIGIT_COMMON::GIT_STATUS::GIT_STATUS_MODIFIED ) );
111
112 if( status & ( GIT_STATUS_INDEX_MODIFIED ) )
113 m_listCtrl->CheckItem( i, true );
114 }
115 else if( status & ( GIT_STATUS_INDEX_DELETED | GIT_STATUS_WT_DELETED ) )
116 {
117 m_listCtrl->SetItem( i, 1, _( "Deleted" ) );
118 m_listCtrl->SetItemImage(
119 i, static_cast<int>( KIGIT_COMMON::GIT_STATUS::GIT_STATUS_DELETED ) );
120
121 if( status & ( GIT_STATUS_INDEX_DELETED ) )
122 m_listCtrl->CheckItem( i, true );
123 }
124 else
125 {
126 printf( " Unknown status: %d\n", status );
127 }
128 }
129
130 sizer->Add( m_listCtrl, 1, wxEXPAND | wxALL, 5 );
131
132 // Commit Message Text Control
133 wxStaticText* commitMessageLabel = new wxStaticText( this, wxID_ANY, _( "Commit Message:" ) );
135 new wxTextCtrl( this, wxID_ANY, "", wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE );
136 sizer->Add( commitMessageLabel, 0, wxALL, 5 );
137 sizer->Add( m_commitMessageTextCtrl, 1, wxEXPAND | wxALL, 5 );
138
139 // Author Name and Email Text Control
140 wxStaticText* authorLabel = new wxStaticText( this, wxID_ANY, _( "Author:" ) );
141 wxString defaultAuthor = defaultAuthorName + " <" + defaultAuthorEmail + ">";
143 new wxTextCtrl( this, wxID_ANY, defaultAuthor, wxDefaultPosition, wxDefaultSize, 0 );
144 sizer->Add( authorLabel, 0, wxALL, 5 );
145 sizer->Add( m_authorTextCtrl, 0, wxEXPAND | wxALL, 5 );
146
147 // OK and Cancel Buttons
148
149 wxStdDialogButtonSizer* buttonSizer = new wxStdDialogButtonSizer();
150
151 m_okButton = new wxButton( this, wxID_OK, _( "OK" ) );
152 wxButton* cancelButton = new wxButton( this, wxID_CANCEL, _( "Cancel" ) );
153 buttonSizer->Add( cancelButton, 0, wxALL, 5 );
154 buttonSizer->Add( m_okButton, 0, wxALL, 5 );
155 buttonSizer->Realize();
156
157 sizer->Add( buttonSizer, 0, wxALIGN_RIGHT | wxALL, 5 );
158
159 SetSizerAndFit( sizer );
160
161 SetupStandardButtons( { { wxID_OK, _( "C&ommit" ) } } );
162
163 // Bind events
164 Bind( wxEVT_TEXT, &DIALOG_GIT_COMMIT::OnTextChanged, this, m_commitMessageTextCtrl->GetId() );
165
166 // Set the repository and defaults
167 m_repo = repo;
168 m_defaultAuthorName = defaultAuthorName;
169 m_defaultAuthorEmail = defaultAuthorEmail;
170}
171
172
173void DIALOG_GIT_COMMIT::OnTextChanged( wxCommandEvent& aEvent )
174{
175 if( m_commitMessageTextCtrl->GetValue().IsEmpty() )
176 {
177 m_okButton->Disable();
178 m_okButton->SetToolTip( _( "Commit message cannot be empty" ) );
179 }
180 else
181 {
182 m_okButton->Enable();
183 m_okButton->SetToolTip( wxEmptyString );
184 }
185}
186
187
189{
190 return m_commitMessageTextCtrl->GetValue();
191}
192
193
195{
196 wxString authorText = m_authorTextCtrl->GetValue();
197 size_t pos = authorText.find( '<' );
198
199 if( pos != wxString::npos )
200 return authorText.substr( 0, pos ).Trim();
201
202 return wxEmptyString;
203}
204
205
207{
208 wxString authorText = m_authorTextCtrl->GetValue();
209 size_t startPos = authorText.find( '<' );
210 size_t endPos = authorText.find( '>' );
211
212 if( startPos != wxString::npos && endPos != wxString::npos && startPos < endPos )
213 return authorText.substr( startPos + 1, endPos - startPos - 1 ).Trim();
214
215 return wxEmptyString;
216}
217
218
219std::vector<wxString> DIALOG_GIT_COMMIT::GetSelectedFiles() const
220{
221 std::vector<wxString> selectedFiles;
222
223 long item = -1;
224 while( ( item = m_listCtrl->GetNextItem( item, wxLIST_NEXT_ALL ) )
225 != -1 )
226 {
227 if( m_listCtrl->IsItemChecked( item ) )
228 selectedFiles.push_back( m_listCtrl->GetItemText( item ) );
229 }
230
231 return selectedFiles;
232}
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
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:88
void SetupStandardButtons(std::map< int, wxString > aLabels={})
#define _(s)