KiCad PCB EDA Suite
Loading...
Searching...
No Matches
rule_editor_dialog_base.h
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) 2024 KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#ifndef RULE_EDITOR_DIALOG_BASE_H
21#define RULE_EDITOR_DIALOG_BASE_H
22
23#include <wx/treectrl.h>
24#include <wx/srchctrl.h>
25#include <wx/bmpbuttn.h>
26#include <wx/splitter.h>
27
28#include <variant>
29#include <optional>
30#include <unordered_map>
31
32#include <dialog_shim.h>
34
35class WX_INFOBAR;
36class wxDragImage;
37
49
54{
56 wxString m_nodeName;
59 std::optional<int> m_nodeTypeMap;
60 std::vector<RULE_TREE_NODE> m_childNodes;
61 std::shared_ptr<RULE_EDITOR_DATA_BASE> m_nodeData;
62};
63
64
70class RULE_TREE_ITEM_DATA : public wxTreeItemData
71{
72public:
73 explicit RULE_TREE_ITEM_DATA( int aNodeId, wxTreeItemId aParentTreeItemId,
74 wxTreeItemId aTreeItemId ) :
75 m_nodeId( aNodeId ),
76 m_treeItemId( aTreeItemId ),
77 m_parentTreeItemId( aParentTreeItemId )
78 {
79 }
80
81 int GetNodeId() const { return m_nodeId; }
82
83 wxTreeItemId GetParentTreeItemId() const { return m_parentTreeItemId; }
84
85 void SetParentTreeItemId( wxTreeItemId aParentTreeItemId )
86 {
87 m_parentTreeItemId = aParentTreeItemId;
88 }
89
90 wxTreeItemId GetTreeItemId() const { return m_treeItemId; }
91
92 void SetTreeItemId( wxTreeItemId aTreeItemId ) { m_treeItemId = aTreeItemId; }
93
94private:
96 wxTreeItemId m_treeItemId;
97 wxTreeItemId m_parentTreeItemId;
98};
99
100
102{
104
105public:
106 RULE_EDITOR_DIALOG_BASE( wxWindow* aParent, const wxString& aTitle,
107 const wxSize& aInitialSize = wxDefaultSize );
108
109 ~RULE_EDITOR_DIALOG_BASE() override;
110
122
128 wxTreeCtrl* GetRuleTreeCtrl() { return m_ruleTreeCtrl; }
129
133 void SetModified() { m_modified = true; }
134
142 static RULE_EDITOR_DIALOG_BASE* GetDialog( wxWindow* aWindow );
143
150 virtual std::vector<RULE_TREE_NODE> GetDefaultRuleTreeItems() = 0;
151
158 virtual void AddNewRule( RULE_TREE_ITEM_DATA* aRuleTreeItemData ) = 0;
159
166 virtual void DuplicateRule( RULE_TREE_ITEM_DATA* aRuleTreeItemData ) = 0;
167
174 virtual void RemoveRule( int aNodeId ) = 0;
175
182 virtual void RuleTreeItemSelectionChanged( RULE_TREE_ITEM_DATA* aCurrentRuleTreeItemData ) = 0;
183
190 virtual void UpdateRuleTypeTreeItemData( RULE_TREE_ITEM_DATA* aRuleTreeItemData ) = 0;
191
201 virtual bool isEnabled( RULE_TREE_ITEM_DATA* aRuleTreeItemData,
202 RULE_EDITOR_TREE_CONTEXT_OPT aOption ) = 0;
203
209 void InitRuleTreeItems( const std::vector<RULE_TREE_NODE>& aRuleTreeNodes );
210
216 wxPanel* GetContentPanel() { return m_contentPanel; }
217
223 void SetContentPanel( wxPanel* aContentPanel );
224
231 void AppendNewRuleTreeItem( const RULE_TREE_NODE& aRuleTreeNode,
232 wxTreeItemId aParentTreeItemId );
233
240
247
254 void UpdateRuleTreeItemText( wxTreeItemId aItemId, wxString aItemText );
255
261 void SetControlsEnabled( bool aEnable );
262
269 void DeleteRuleTreeItem( wxTreeItemId aItemId, const int& aNodeId );
270
271 virtual void OnSave( wxCommandEvent& aEvent ) = 0;
272
273 virtual void OnCancel( wxCommandEvent& aEvent ) = 0;
274
275protected:
277
278 bool TransferDataToWindow() override;
279
280 bool TransferDataFromWindow() override;
281
282private:
290 void populateRuleTreeCtrl( const std::vector<RULE_TREE_NODE>& aRuleTreeNodes,
291 const RULE_TREE_NODE& aRuleTreeNode,
292 wxTreeItemId aParentTreeItemId );
293
299 void onRuleTreeItemRightClick( wxTreeEvent& aEvent );
300
306 void onRuleTreeItemSelectionChanged( wxTreeEvent& aEvent );
307
312 void onRuleTreeItemActivated( wxTreeEvent& aEvent );
313
319 void onNewRuleOptionClick( wxCommandEvent& aEvent );
320
326 void onDuplicateRuleOptionClick( wxCommandEvent& aEvent );
327
333 void onDeleteRuleOptionClick( wxCommandEvent& aEvent );
334
340 void onMoveUpRuleOptionClick( wxCommandEvent& aEvent );
341
347 void onMoveDownRuleOptionClick( wxCommandEvent& aEvent );
348
354 void onRuleTreeItemLeftDown( wxMouseEvent& aEvent );
355
361 void onRuleTreeItemMouseMotion( wxMouseEvent& aEvent );
362
368 void onRuleTreeItemLeftUp( wxMouseEvent& aEvent );
369
375 void onFilterSearch( wxCommandEvent& aEvent );
376
384 bool filterRuleTree( const wxTreeItemId& aItem, const wxString& aFilter );
385
392 void saveRuleTreeState( const wxTreeItemId& aItem, const int& aNodeId = 0 );
393
400 void restoreRuleTree( const wxTreeItemId& aParent, const int& aNodeId );
401
409 wxTreeItemId appendRuleTreeItem( const RULE_TREE_NODE& aRuleTreeNode,
410 wxTreeItemId aParentTreeItemId );
411
419 void getRuleTreeChildNodes( const std::vector<RULE_TREE_NODE>& aNodes, int aParentId,
420 std::vector<RULE_TREE_NODE>& aResult );
421
428 void moveRuleTreeItemChildrensTooOnDrag( wxTreeItemId aSrcTreeItemId,
429 wxTreeItemId aDestTreeItemId );
430
435
439 void updateRuleTreeActionButtonsState( RULE_TREE_ITEM_DATA* aRuleTreeItemData );
440
441 void onResize( wxSizeEvent& event );
442
443 void onClose( wxCloseEvent& aEvt );
444
445protected:
446 wxTreeCtrl* m_ruleTreeCtrl;
449 wxSplitterWindow* m_splitter;
450 wxSearchCtrl* m_filterSearch;
451 wxTextCtrl* m_filterText;
452 wxBoxSizer* m_buttonsSizer;
453 wxBitmapButton* m_addRuleButton;
454 wxBitmapButton* m_copyRuleButton;
455 wxBitmapButton* m_moveTreeItemUpButton;
457 wxBitmapButton* m_deleteRuleButton;
460
461private:
469
470 wxString m_title;
471 std::vector<RULE_TREE_NODE> m_defaultTreeItems;
473 wxTreeItemId m_previousId;
474 wxTreeItemId m_draggedItem;
475 wxDragImage* m_dragImage;
476
477 std::unordered_map<int, std::tuple<wxString, std::vector<int>, wxTreeItemId>> m_treeHistoryData;
478};
479
480#endif //RULE_EDITOR_DIALOG_BASE_H
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)
void populateRuleTreeCtrl(const std::vector< RULE_TREE_NODE > &aRuleTreeNodes, const RULE_TREE_NODE &aRuleTreeNode, wxTreeItemId aParentTreeItemId)
Populates the rule tree with nodes and their children.
void onRuleTreeItemSelectionChanged(wxTreeEvent &aEvent)
Updates action buttons based on the selected tree item.
void moveRuleTreeItemChildrensTooOnDrag(wxTreeItemId aSrcTreeItemId, wxTreeItemId aDestTreeItemId)
Recursively moves all child nodes of a source item to a destination during drag.
void DeleteRuleTreeItem(wxTreeItemId aItemId, const int &aNodeId)
Deletes a tree item and removes its corresponding node from history.
void onRuleTreeItemMouseMotion(wxMouseEvent &aEvent)
Handles drag motion to move the item along with the cursor.
void UpdateRuleTreeItemText(wxTreeItemId aItemId, wxString aItemText)
Updates the text of a specified rule tree item.
wxPanel * GetContentPanel()
Retrieves the current content panel.
void onNewRuleOptionClick(wxCommandEvent &aEvent)
Creates a new rule when the "New Rule" option is clicked.
void onRuleTreeItemLeftDown(wxMouseEvent &aEvent)
Initiates drag operation for a tree item on mouse down.
void onMoveUpRuleOptionClick(wxCommandEvent &aEvent)
Moves a rule item up in the tree when "Move Up" is clicked.
virtual void RuleTreeItemSelectionChanged(RULE_TREE_ITEM_DATA *aCurrentRuleTreeItemData)=0
Pure virtual method to handle tree item selection changes.
void onClose(wxCloseEvent &aEvt)
void SetModified()
Marks the dialog as modified, typically used to indicate unsaved changes.
void onMoveDownRuleOptionClick(wxCommandEvent &aEvent)
Moves a rule item down in the tree when "Move Down" is clicked.
void AppendNewRuleTreeItem(const RULE_TREE_NODE &aRuleTreeNode, wxTreeItemId aParentTreeItemId)
Adds a new rule tree item under the specified parent and updates the tree history.
void InitRuleTreeItems(const std::vector< RULE_TREE_NODE > &aRuleTreeNodes)
Initializes the rule tree by adding nodes, setting up the structure, and saving its state.
void saveRuleTreeState(const wxTreeItemId &aItem, const int &aNodeId=0)
Saves the state of a tree item to history.
static RULE_EDITOR_DIALOG_BASE * GetDialog(wxWindow *aWindow)
Static method to retrieve the rule editor dialog instance associated with a given window.
void onRuleTreeItemLeftUp(wxMouseEvent &aEvent)
Completes the drag operation on mouse release.
void onRuleTreeItemActivated(wxTreeEvent &aEvent)
Handles double-click activation of a tree item.
void SetContentPanel(wxPanel *aContentPanel)
Replaces the current content panel with a new one based on the selected constraint type.
virtual void OnSave(wxCommandEvent &aEvent)=0
std::vector< RULE_TREE_NODE > m_defaultTreeItems
virtual void AddNewRule(RULE_TREE_ITEM_DATA *aRuleTreeItemData)=0
Pure virtual method to add a new rule to the tree.
RULE_EDITOR_TREE_CONTEXT_OPT_ID
Enumeration representing the available context menu options for the rule editor tree.
void onDuplicateRuleOptionClick(wxCommandEvent &aEvent)
Duplicates the selected rule when "Duplicate Rule" is clicked.
wxTreeCtrl * GetRuleTreeCtrl()
Gets the tree control used for displaying and managing rules.
RULE_TREE_ITEM_DATA * m_selectedData
void restoreRuleTree(const wxTreeItemId &aParent, const int &aNodeId)
Restores a tree item from history and appends it under a parent.
RULE_EDITOR_DIALOG_BASE(wxWindow *aParent, const wxString &aTitle, const wxSize &aInitialSize=wxDefaultSize)
virtual void RemoveRule(int aNodeId)=0
Pure virtual method to remove a rule from the tree.
wxTreeItemId appendRuleTreeItem(const RULE_TREE_NODE &aRuleTreeNode, wxTreeItemId aParentTreeItemId)
Appends a new rule item to the tree.
std::unordered_map< int, std::tuple< wxString, std::vector< int >, wxTreeItemId > > m_treeHistoryData
void onResize(wxSizeEvent &event)
void onDeleteRuleOptionClick(wxCommandEvent &aEvent)
Deletes the selected rule when "Delete Rule" is clicked.
virtual std::vector< RULE_TREE_NODE > GetDefaultRuleTreeItems()=0
Pure virtual method to get the default rule tree items.
virtual void DuplicateRule(RULE_TREE_ITEM_DATA *aRuleTreeItemData)=0
Pure virtual method to duplicate an existing rule in the tree.
wxTreeItemId GetPreviouslySelectedRuleTreeItemId()
Retrieves the previously selected rule tree item ID.
void onRuleTreeItemRightClick(wxTreeEvent &aEvent)
Handles right-click on a rule tree item to create a context menu.
void getRuleTreeChildNodes(const std::vector< RULE_TREE_NODE > &aNodes, int aParentId, std::vector< RULE_TREE_NODE > &aResult)
Retrieves child nodes of a given parent node.
void onFilterSearch(wxCommandEvent &aEvent)
Applies filter to the rule tree based on the search string.
virtual bool isEnabled(RULE_TREE_ITEM_DATA *aRuleTreeItemData, RULE_EDITOR_TREE_CONTEXT_OPT aOption)=0
Pure virtual method to verify if a context menu option for a rule tree item should be enabled.
virtual void UpdateRuleTypeTreeItemData(RULE_TREE_ITEM_DATA *aRuleTreeItemData)=0
Pure virtual method to update the rule tree item data.
void updateRuleTreeItemMoveOptionState()
Updates the state of move options (up/down) for the selected item.
RULE_TREE_ITEM_DATA * GetCurrentlySelectedRuleTreeItemData()
Retrieves the currently selected rule tree item data.
virtual void OnCancel(wxCommandEvent &aEvent)=0
void updateRuleTreeActionButtonsState(RULE_TREE_ITEM_DATA *aRuleTreeItemData)
Updates the action buttons based on the current selection.
void SetControlsEnabled(bool aEnable)
Enables or disables controls within the rule editor dialog.
bool filterRuleTree(const wxTreeItemId &aItem, const wxString &aFilter)
Recursively filters tree items to show only those matching the filter.
wxBitmapButton * m_moveTreeItemDownButton
A class representing additional data associated with a wxTree item.
void SetTreeItemId(wxTreeItemId aTreeItemId)
wxTreeItemId GetTreeItemId() const
void SetParentTreeItemId(wxTreeItemId aParentTreeItemId)
wxTreeItemId GetParentTreeItemId() const
RULE_TREE_ITEM_DATA(int aNodeId, wxTreeItemId aParentTreeItemId, wxTreeItemId aTreeItemId)
A modified version of the wxInfoBar class that allows us to:
Definition wx_infobar.h:76
RULE_EDITOR_TREE_CONTEXT_OPT
Enumeration representing the available context menu options for the rule editor tree.
Structure representing a node in a rule tree, collection of this used for building the rule tree.
std::shared_ptr< RULE_EDITOR_DATA_BASE > m_nodeData
std::optional< int > m_nodeTypeMap
std::vector< RULE_TREE_NODE > m_childNodes