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 wxImageList* imageList = new wxImageList(
61 16, 16, true, static_cast<int>( KIGIT_COMMON::GIT_STATUS::GIT_STATUS_LAST ) );
62
63 imageList->Add( KiBitmap( BITMAPS::git_good_check ) ); // PLACEHOLDER
64 imageList->Add( KiBitmap( BITMAPS::git_good_check ) ); // GIT_STATUS_CURRENT
65 imageList->Add( KiBitmap( BITMAPS::git_modified ) ); // GIT_STATUS_MODIFIED
66 imageList->Add( KiBitmap( BITMAPS::git_add ) ); // GIT_STATUS_ADDED
67 imageList->Add( KiBitmap( BITMAPS::git_delete ) ); // GIT_STATUS_DELETED
68 imageList->Add( KiBitmap( BITMAPS::git_out_of_date ) ); // GIT_STATUS_BEHIND
69 imageList->Add( KiBitmap( BITMAPS::git_changed_ahead ) ); // GIT_STATUS_AHEAD
70 imageList->Add( KiBitmap( BITMAPS::git_conflict ) ); // GIT_STATUS_CONFLICTED
71
72 // Assign the image list to the list control
73 m_listCtrl->SetImageList( imageList, wxIMAGE_LIST_SMALL );
74
75 // Populate list control with items
76 for( auto& [filename, status] : filesToCommit )
77 {
78 int i = m_listCtrl->GetItemCount();
79 m_listCtrl->InsertItem( i, filename );
80
81 if( status & ( GIT_STATUS_INDEX_NEW | GIT_STATUS_WT_NEW ) )
82 {
83 m_listCtrl->SetItem( i, 1, _( "New" ) );
84 m_listCtrl->SetItemImage(
85 i, static_cast<int>( KIGIT_COMMON::GIT_STATUS::GIT_STATUS_ADDED ) );
86 }
87 else if( status & ( GIT_STATUS_INDEX_MODIFIED | GIT_STATUS_WT_MODIFIED ) )
88 {
89 m_listCtrl->SetItem( i, 1, _( "Modified" ) );
90 m_listCtrl->SetItemImage(
91 i, static_cast<int>( KIGIT_COMMON::GIT_STATUS::GIT_STATUS_MODIFIED ) );
92 }
93 else if( status & ( GIT_STATUS_INDEX_DELETED | GIT_STATUS_WT_DELETED ) )
94 {
95 m_listCtrl->SetItem( i, 1, _( "Deleted" ) );
96 m_listCtrl->SetItemImage(
97 i, static_cast<int>( KIGIT_COMMON::GIT_STATUS::GIT_STATUS_DELETED ) );
98 }
99 else
100 {
101 printf( " Unknown status: %d\n", status );
102 }
103 }
104
105 sizer->Add( m_listCtrl, 1, wxEXPAND | wxALL, 5 );
106
107 // Commit Message Text Control
108 wxStaticText* commitMessageLabel = new wxStaticText( this, wxID_ANY, _( "Commit Message:" ) );
110 new wxTextCtrl( this, wxID_ANY, "", wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE );
111 sizer->Add( commitMessageLabel, 0, wxALL, 5 );
112 sizer->Add( m_commitMessageTextCtrl, 1, wxEXPAND | wxALL, 5 );
113
114 // Author Name and Email Text Control
115 wxStaticText* authorLabel = new wxStaticText( this, wxID_ANY, _( "Author:" ) );
116 wxString defaultAuthor = defaultAuthorName + " <" + defaultAuthorEmail + ">";
118 new wxTextCtrl( this, wxID_ANY, defaultAuthor, wxDefaultPosition, wxDefaultSize, 0 );
119 sizer->Add( authorLabel, 0, wxALL, 5 );
120 sizer->Add( m_authorTextCtrl, 0, wxEXPAND | wxALL, 5 );
121
122 // OK and Cancel Buttons
123
124 wxStdDialogButtonSizer* buttonSizer = new wxStdDialogButtonSizer();
125
126 m_okButton = new wxButton( this, wxID_OK, _( "OK" ) );
127 wxButton* cancelButton = new wxButton( this, wxID_CANCEL, _( "Cancel" ) );
128 buttonSizer->Add( cancelButton, 0, wxALL, 5 );
129 buttonSizer->Add( m_okButton, 0, wxALL, 5 );
130 buttonSizer->Realize();
131
132 sizer->Add( buttonSizer, 0, wxALIGN_RIGHT | wxALL, 5 );
133
134 SetSizerAndFit( sizer );
135
136 SetupStandardButtons( { { wxID_OK, _( "C&ommit" ) } } );
137
138 // Bind events
139 Bind( wxEVT_TEXT, &DIALOG_GIT_COMMIT::OnTextChanged, this, m_commitMessageTextCtrl->GetId() );
140
141 // Set the repository and defaults
142 m_repo = repo;
143 m_defaultAuthorName = defaultAuthorName;
144 m_defaultAuthorEmail = defaultAuthorEmail;
145}
146
147
148void DIALOG_GIT_COMMIT::OnTextChanged( wxCommandEvent& aEvent )
149{
150 if( m_commitMessageTextCtrl->GetValue().IsEmpty() )
151 {
152 m_okButton->Disable();
153 m_okButton->SetToolTip( _( "Commit message cannot be empty" ) );
154 }
155 else
156 {
157 m_okButton->Enable();
158 m_okButton->SetToolTip( wxEmptyString );
159 }
160}
161
162
164{
165 return m_commitMessageTextCtrl->GetValue();
166}
167
168
170{
171 wxString authorText = m_authorTextCtrl->GetValue();
172 size_t pos = authorText.find( '<' );
173
174 if( pos != wxString::npos )
175 return authorText.substr( 0, pos ).Trim();
176
177 return wxEmptyString;
178}
179
180
182{
183 wxString authorText = m_authorTextCtrl->GetValue();
184 size_t startPos = authorText.find( '<' );
185 size_t endPos = authorText.find( '>' );
186
187 if( startPos != wxString::npos && endPos != wxString::npos && startPos < endPos )
188 return authorText.substr( startPos + 1, endPos - startPos - 1 ).Trim();
189
190 return wxEmptyString;
191}
192
193
194std::vector<wxString> DIALOG_GIT_COMMIT::GetSelectedFiles() const
195{
196 std::vector<wxString> selectedFiles;
197
198 long item = -1;
199 while( ( item = m_listCtrl->GetNextItem( item, wxLIST_NEXT_ALL ) )
200 != -1 )
201 {
202 if( m_listCtrl->IsItemChecked( item ) )
203 selectedFiles.push_back( m_listCtrl->GetItemText( item ) );
204 }
205
206 return selectedFiles;
207}
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:83
void SetupStandardButtons(std::map< int, wxString > aLabels={})
#define _(s)