KiCad PCB EDA Suite
filehistory.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) 2019 Ian McInerney <Ian.S.McInerney@ieee.org>
5  * Copyright (C) 2019 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 #include <filehistory.h>
26 #include <id.h>
27 #include <settings/app_settings.h>
28 #include <tool/action_menu.h>
30 #include <wx/menu.h>
31 
32 #include <functional>
33 using namespace std::placeholders;
34 
35 
36 FILE_HISTORY::FILE_HISTORY( size_t aMaxFiles, int aBaseFileId, int aClearId, wxString aClearText )
37  : wxFileHistory( std::min( aMaxFiles, (size_t) MAX_FILE_HISTORY_SIZE ) ),
38  m_clearId( aClearId ),
39  m_clearText( aClearText )
40 {
41  SetBaseId( aBaseFileId );
42 }
43 
44 
45 void FILE_HISTORY::Load( const APP_SETTINGS_BASE& aSettings )
46 {
47  m_fileHistory.clear();
48 
49  // file_history stores the most recent file first
50  for( auto it = aSettings.m_System.file_history.rbegin();
51  it != aSettings.m_System.file_history.rend(); ++it )
52  AddFileToHistory( *it );
53 }
54 
55 
56 void FILE_HISTORY::Load( const std::vector<wxString>& aList )
57 {
58  m_fileHistory.clear();
59 
60  for( const auto& file : aList )
61  AddFileToHistory( file );
62 }
63 
64 
66 {
67  aSettings.m_System.file_history.clear();
68 
69  for( const auto& file : m_fileHistory )
70  aSettings.m_System.file_history.emplace_back( file.ToStdString() );
71 }
72 
73 
74 void FILE_HISTORY::Save( std::vector<wxString>* aList )
75 {
76  aList->clear();
77 
78  for( const auto& file : m_fileHistory )
79  aList->push_back( file );
80 }
81 
82 
83 void FILE_HISTORY::SetMaxFiles( size_t aMaxFiles )
84 {
85  m_fileMaxFiles = std::min( aMaxFiles, (size_t) MAX_FILE_HISTORY_SIZE );
86 
87  size_t numFiles = m_fileHistory.size();
88 
89  while( numFiles > m_fileMaxFiles )
90  RemoveFileFromHistory( --numFiles );
91 }
92 
93 
94 void FILE_HISTORY::AddFileToHistory( const wxString &aFile )
95 {
96  // Iterate over each menu removing our custom items
97  for( wxList::compatibility_iterator node = m_fileMenus.GetFirst();
98  node; node = node->GetNext() )
99  {
100  wxMenu* menu = static_cast<wxMenu*>( node->GetData() );
101  doRemoveClearitem( menu );
102  }
103 
104  // Let wx add the items in the file history
105  wxFileHistory::AddFileToHistory( aFile );
106 
107  // Add our custom items back
108  for( wxList::compatibility_iterator node = m_fileMenus.GetFirst();
109  node; node = node->GetNext() )
110  {
111  wxMenu* menu = static_cast<wxMenu*>( node->GetData() );
112  doAddClearItem( menu );
113  }
114 }
115 
116 
117 void FILE_HISTORY::AddFilesToMenu( wxMenu* aMenu )
118 {
119  doRemoveClearitem( aMenu );
120  wxFileHistory::AddFilesToMenu( aMenu );
121  doAddClearItem( aMenu );
122 }
123 
124 
125 void FILE_HISTORY::doRemoveClearitem( wxMenu* aMenu )
126 {
127  size_t itemPos;
128  wxMenuItem* clearItem = aMenu->FindChildItem( m_clearId, &itemPos );
129 
130  // Remove the separator if there is one
131  if( clearItem && itemPos > 1 )
132  {
133  wxMenuItem* sepItem = aMenu->FindItemByPosition( itemPos - 1 );
134 
135  if( sepItem )
136  aMenu->Destroy( sepItem );
137  }
138 
139  // Remove the clear and placeholder menu items
140  if( clearItem )
141  aMenu->Destroy( m_clearId );
142 
143  if( aMenu->FindChildItem( ID_FILE_LIST_EMPTY ) )
144  aMenu->Destroy( ID_FILE_LIST_EMPTY );
145 }
146 
147 
148 void FILE_HISTORY::doAddClearItem( wxMenu* aMenu )
149 {
150  if( GetCount() == 0 )
151  {
152  // If the history is empty, we create an item to say there are no files
153  wxMenuItem* item = new wxMenuItem( nullptr, ID_FILE_LIST_EMPTY, _( "No Files" ) );
154 
155  aMenu->Append( item );
156  aMenu->Enable( item->GetId(), false );
157  }
158 
159  wxMenuItem* clearItem = new wxMenuItem( nullptr, m_clearId, m_clearText );
160 
161  aMenu->AppendSeparator();
162  aMenu->Append( clearItem );
163 }
164 
165 
167 {
168  while( GetCount() > 0 )
169  RemoveFileFromHistory( 0 );
170 }
171 
172 
174 {
175  return std::bind( &FILE_HISTORY::isHistoryNotEmpty, _1, std::cref( aHistory ) );
176 }
177 
178 
179 bool FILE_HISTORY::isHistoryNotEmpty( const SELECTION& aSelection, const FILE_HISTORY& aHistory )
180 {
181  return aHistory.GetCount() != 0;
182 }
void AddFilesToMenu() override
Add the files to all registered menus.
Definition: filehistory.h:98
std::vector< wxString > file_history
Definition: app_settings.h:130
static bool isHistoryNotEmpty(const SELECTION &aSelection, const FILE_HISTORY &aHistory)
Test if the file history is empty.
void Save(APP_SETTINGS_BASE &aSettings)
Saves history into a JSON settings object.
Definition: filehistory.cpp:65
Template specialization to enable wxStrings for certain containers (e.g. unordered_map)
Definition: bitmap.cpp:56
void Load(const APP_SETTINGS_BASE &aSettings)
Loads history from a JSON settings object.
Definition: filehistory.cpp:45
This class implements a file history object to store a list of files, that can then be added to a men...
Definition: filehistory.h:42
void ClearFileHistory()
Clear all entries from the file history.
wxString m_clearText
Definition: filehistory.h:168
void AddFileToHistory(const wxString &aFile) override
Adds a file to the history.
Definition: filehistory.cpp:94
FILE_HISTORY(size_t aMaxFiles, int aBaseFileId, int aClearId, wxString aClearText=_("Clear Recent Files"))
Create a file history object to store a list of files and add them to a menu.
Definition: filehistory.cpp:36
APP_SETTINGS_BASE is a settings class that should be derived for each standalone KiCad application.
Definition: app_settings.h:91
void doRemoveClearitem(wxMenu *aMenu)
Remove the clear menu item and the preceding separator from the given menu.
std::function< bool(const SELECTION &)> SELECTION_CONDITION
Functor type that checks a specific condition for selected items.
void SetMaxFiles(size_t aMaxFiles)
Update the number of files that will be contained inside the file history.
Definition: filehistory.cpp:83
static SELECTION_CONDITION FileHistoryNotEmpty(const FILE_HISTORY &aHistory)
Create a SELECTION_CONDITION that can be used to enable a menu item when the file history has items i...
#define _(s)
Definition: 3d_actions.cpp:33
#define MAX_FILE_HISTORY_SIZE
Definition: id.h:70
void doAddClearItem(wxMenu *aMenu)
Add the clear menu item and the preceding separator to the given menu.