1 /*
2 	Copyright 2006-2019 The QElectroTech Team
3 	This file is part of QElectroTech.
4 
5 	QElectroTech is free software: you can redistribute it and/or modify
6 	it under the terms of the GNU General Public License as published by
7 	the Free Software Foundation, either version 2 of the License, or
8 	(at your option) any later version.
9 
10 	QElectroTech is distributed in the hope that it will be useful,
11 	but WITHOUT ANY WARRANTY; without even the implied warranty of
12 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 	GNU General Public License for more details.
14 
15 	You should have received a copy of the GNU General Public License
16 	along with QElectroTech.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 #ifndef LINKSINGLEELEMENTWIDGET_H
19 #define LINKSINGLEELEMENTWIDGET_H
20 
21 #include "element.h"
22 #include "abstractelementpropertieseditorwidget.h"
23 
24 class QTreeWidgetItem;
25 
26 namespace Ui {
27 	class LinkSingleElementWidget;
28 }
29 
30 /**
31 * @brief The LinkSingleElementWidget class
32  * this class provide a widget to select an element to be linked
33  * to the element given in the constructor.
34  * The element given in constructor must be linked with only one other element (like report or slave element).
35  * This widget detect automaticaly the kind of element given in the constructor and
36  * search all element that can be linked with it.
37  * If the element is already linked, the widget ask user to unlink.
38  * This widget embedded the diagram command for undo/redo the action
39  */
40 class LinkSingleElementWidget : public AbstractElementPropertiesEditorWidget
41 {
42 	Q_OBJECT
43 
44 		///Methods
45 	public:
46 		explicit LinkSingleElementWidget(Element *elmt, QWidget *parent = nullptr);
47 		~LinkSingleElementWidget() override;
48 
49 		void setElement (Element *element) override;
50 		void apply() override;
51 		QUndoCommand *associatedUndo() const override;
52 		QString title() const override;
53 
54 	public slots:
55 		void updateUi() override;
56 		void buildTree();
57 
58 	public:
59 		bool setLiveEdit(bool live_edit) override;
60 
61 	private :
62 		QList <Element *> availableElements();
63 		void setUpCompleter();
64 		void clearTreeWidget();
65 		void setUpHeaderLabels();
66 
67 	private slots:
68 		void diagramWasRemovedFromProject();
69 		void showedElementWasDeleted();
70 		void linkTriggered();
71 		void hideButtons();
72 		void showButtons();
73 		void headerCustomContextMenuRequested(const QPoint &pos);
74 
75 		void on_m_unlink_pb_clicked();
76 		void on_m_tree_widget_itemDoubleClicked(QTreeWidgetItem *item, int column);
77 		void on_m_tree_widget_customContextMenuRequested(const QPoint &pos);
78 		void on_m_show_linked_pb_clicked();
79 		void on_m_show_this_pb_clicked();
80 
81 		void on_m_search_field_textEdited(const QString &arg1);
82 
83 	private:
84 	Ui::LinkSingleElementWidget *ui;
85 
86 	bool m_unlink = false;
87 	Element::kind m_filter;
88 
89 	QHash <QTreeWidgetItem*, Element*> m_qtwi_elmt_hash;
90 	QHash <QTreeWidgetItem*, QStringList> m_qtwi_strl_hash;
91 
92 	QTreeWidgetItem *m_qtwi_at_context_menu = nullptr,
93 					*m_pending_qtwi = nullptr;
94 
95 	Element *m_showed_element = nullptr,
96 			*m_element_to_link = nullptr;
97 
98 	QMenu *m_context_menu;
99 	QAction *m_link_action,
100 			*m_show_qtwi,
101 			*m_show_element,
102 			*m_save_header_state;
103 };
104 
105 #endif // LINKSINGLEELEMENTWIDGET_H
106