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#include <wx/scrolwin.h>
28
29#include <variant>
30#include <optional>
31#include <unordered_map>
32
33#include <dialog_shim.h>
35
36class WX_INFOBAR;
37class wxDragImage;
38
50
55{
57 wxString m_nodeName;
60 std::optional<int> m_nodeTypeMap;
61 std::vector<RULE_TREE_NODE> m_childNodes;
62 std::shared_ptr<RULE_EDITOR_DATA_BASE> m_nodeData;
63};
64
65
71class RULE_TREE_ITEM_DATA : public wxTreeItemData
72{
73public:
74 explicit RULE_TREE_ITEM_DATA( int aNodeId, wxTreeItemId aParentTreeItemId,
75 wxTreeItemId aTreeItemId ) :
76 m_nodeId( aNodeId ),
77 m_treeItemId( aTreeItemId ),
78 m_parentTreeItemId( aParentTreeItemId )
79 {
80 }
81
82 int GetNodeId() const { return m_nodeId; }
83
84 wxTreeItemId GetParentTreeItemId() const { return m_parentTreeItemId; }
85
86 void SetParentTreeItemId( wxTreeItemId aParentTreeItemId )
87 {
88 m_parentTreeItemId = aParentTreeItemId;
89 }
90
91 wxTreeItemId GetTreeItemId() const { return m_treeItemId; }
92
93 void SetTreeItemId( wxTreeItemId aTreeItemId ) { m_treeItemId = aTreeItemId; }
94
95private:
97 wxTreeItemId m_treeItemId;
98 wxTreeItemId m_parentTreeItemId;
99};
100
101
103{
105
106public:
107 RULE_EDITOR_DIALOG_BASE( wxWindow* aParent, const wxString& aTitle,
108 const wxSize& aInitialSize = wxDefaultSize );
109
110 ~RULE_EDITOR_DIALOG_BASE() override;
111
123
129 wxTreeCtrl* GetRuleTreeCtrl() { return m_ruleTreeCtrl; }
130
135 void SetModified();
136
141 void ClearModified();
142
146 bool IsModified() const { return m_modified; }
147
155 static RULE_EDITOR_DIALOG_BASE* GetDialog( wxWindow* aWindow );
156
163 virtual std::vector<RULE_TREE_NODE> GetDefaultRuleTreeItems() = 0;
164
171 virtual void AddNewRule( RULE_TREE_ITEM_DATA* aRuleTreeItemData ) = 0;
172
179 virtual void DuplicateRule( RULE_TREE_ITEM_DATA* aRuleTreeItemData ) = 0;
180
187 virtual void RemoveRule( int aNodeId ) = 0;
188
195 virtual void RuleTreeItemSelectionChanged( RULE_TREE_ITEM_DATA* aCurrentRuleTreeItemData ) = 0;
196
203 virtual void UpdateRuleTypeTreeItemData( RULE_TREE_ITEM_DATA* aRuleTreeItemData ) = 0;
204
214 virtual bool isEnabled( RULE_TREE_ITEM_DATA* aRuleTreeItemData,
215 RULE_EDITOR_TREE_CONTEXT_OPT aOption ) = 0;
216
222 void InitRuleTreeItems( const std::vector<RULE_TREE_NODE>& aRuleTreeNodes );
223
229 wxPanel* GetContentPanel() { return m_contentPanel; }
230
236 void SetContentPanel( wxPanel* aContentPanel );
237
243
250 void AppendNewRuleTreeItem( const RULE_TREE_NODE& aRuleTreeNode,
251 wxTreeItemId aParentTreeItemId );
252
259
266
273 void UpdateRuleTreeItemText( wxTreeItemId aItemId, wxString aItemText );
274
280 void SetControlsEnabled( bool aEnable );
281
288 void DeleteRuleTreeItem( wxTreeItemId aItemId, const int& aNodeId );
289
290 virtual void OnSave( wxCommandEvent& aEvent ) = 0;
291
292 virtual void OnCancel( wxCommandEvent& aEvent ) = 0;
293
294 bool Show( bool show ) override;
295
296protected:
298
299 bool TransferDataToWindow() override;
300
301 bool TransferDataFromWindow() override;
302
303 void OnCharHook( wxKeyEvent& aEvt ) override;
304
305private:
313 void populateRuleTreeCtrl( const std::vector<RULE_TREE_NODE>& aRuleTreeNodes,
314 const RULE_TREE_NODE& aRuleTreeNode,
315 wxTreeItemId aParentTreeItemId );
316
322 void onRuleTreeItemRightClick( wxTreeEvent& aEvent );
323
329 void onRuleTreeItemSelectionChanged( wxTreeEvent& aEvent );
330
335 void onRuleTreeItemActivated( wxTreeEvent& aEvent );
336
342 void onNewRuleOptionClick( wxCommandEvent& aEvent );
343
349 void onDuplicateRuleOptionClick( wxCommandEvent& aEvent );
350
356 void onDeleteRuleOptionClick( wxCommandEvent& aEvent );
357
363 void onMoveUpRuleOptionClick( wxCommandEvent& aEvent );
364
370 void onMoveDownRuleOptionClick( wxCommandEvent& aEvent );
371
377 void onRuleTreeItemLeftDown( wxMouseEvent& aEvent );
378
384 void onRuleTreeItemMouseMotion( wxMouseEvent& aEvent );
385
391 void onRuleTreeItemLeftUp( wxMouseEvent& aEvent );
392
398 void onFilterSearch( wxCommandEvent& aEvent );
399
407 bool filterRuleTree( const wxTreeItemId& aItem, const wxString& aFilter );
408
415 void saveRuleTreeState( const wxTreeItemId& aItem, const int& aNodeId = 0 );
416
423 void restoreRuleTree( const wxTreeItemId& aParent, const int& aNodeId );
424
432 wxTreeItemId appendRuleTreeItem( const RULE_TREE_NODE& aRuleTreeNode,
433 wxTreeItemId aParentTreeItemId );
434
442 void getRuleTreeChildNodes( const std::vector<RULE_TREE_NODE>& aNodes, int aParentId,
443 std::vector<RULE_TREE_NODE>& aResult );
444
451 void moveRuleTreeItemChildrensTooOnDrag( wxTreeItemId aSrcTreeItemId,
452 wxTreeItemId aDestTreeItemId );
453
458
462 void updateRuleTreeActionButtonsState( RULE_TREE_ITEM_DATA* aRuleTreeItemData );
463
464 void onResize( wxSizeEvent& event );
465
466 void onClose( wxCloseEvent& aEvt );
467
468protected:
469 wxTreeCtrl* m_ruleTreeCtrl;
472 wxScrolledWindow* m_scrolledContentWin;
473 wxSplitterWindow* m_splitter;
474 wxSearchCtrl* m_filterSearch;
475 wxTextCtrl* m_filterText;
476 wxBoxSizer* m_buttonsSizer;
477 wxBitmapButton* m_addRuleButton;
478 wxBitmapButton* m_copyRuleButton;
479 wxBitmapButton* m_moveTreeItemUpButton;
481 wxBitmapButton* m_deleteRuleButton;
484
485private:
494
495 wxString m_title;
496 std::vector<RULE_TREE_NODE> m_defaultTreeItems;
498 wxTreeItemId m_previousId;
499 wxTreeItemId m_draggedItem;
500 wxDragImage* m_dragImage;
501
502 std::unordered_map<int, std::tuple<wxString, std::vector<int>, wxTreeItemId>> m_treeHistoryData;
503};
504
505#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)
bool Show(bool show) override
void SetModified()
Marks the dialog as modified, indicating unsaved changes.
void RefreshContentScrollArea()
Recalculates the scrolled content area's virtual size based on the current content panel's best size,...
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
void OnCharHook(wxKeyEvent &aEvt) override
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.
wxScrolledWindow * m_scrolledContentWin
wxTreeCtrl * GetRuleTreeCtrl()
Gets the tree control used for displaying and managing rules.
void ClearModified()
Clears the modified flag, typically after saving.
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.
bool IsModified() const
Returns whether the dialog has unsaved changes.
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:77
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