1 /*
2 * LibrePCB - Professional EDA for everyone!
3 * Copyright (C) 2013 LibrePCB Developers, see AUTHORS.md for contributors.
4 * https://librepcb.org/
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (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, see <http://www.gnu.org/licenses/>.
18 */
19
20 /*******************************************************************************
21 * Includes
22 ******************************************************************************/
23 #include "componentsymbolvariantitemlisteditorwidget.h"
24
25 #include "../common/symbolchooserdialog.h"
26
27 #include <librepcb/common/widgets/editabletablewidget.h>
28 #include <librepcb/libraryeditor/cmp/componentsymbolvariantitemlistmodel.h>
29
30 #include <QtCore>
31 #include <QtWidgets>
32
33 /*******************************************************************************
34 * Namespace
35 ******************************************************************************/
36 namespace librepcb {
37 namespace library {
38 namespace editor {
39
40 /*******************************************************************************
41 * Constructors / Destructor
42 ******************************************************************************/
43
44 ComponentSymbolVariantItemListEditorWidget::
ComponentSymbolVariantItemListEditorWidget(QWidget * parent)45 ComponentSymbolVariantItemListEditorWidget(QWidget* parent) noexcept
46 : QWidget(parent),
47 mModel(new ComponentSymbolVariantItemListModel(this)),
48 mView(new EditableTableWidget(this)),
49 mWorkspace(nullptr),
50 mLayerProvider(nullptr),
51 mOnItemListEditedSlot(
52 *this, &ComponentSymbolVariantItemListEditorWidget::itemListEdited),
53 mOnItemEditedSlot(*this,
54 &ComponentSymbolVariantItemListEditorWidget::itemEdited) {
55 mView->setShowMoveButtons(true);
56 mView->setBrowseButtonColumn(
57 ComponentSymbolVariantItemListModel::COLUMN_SYMBOL);
58 mView->setModel(mModel.data());
59 mView->horizontalHeader()->setSectionResizeMode(
60 ComponentSymbolVariantItemListModel::COLUMN_NUMBER,
61 QHeaderView::ResizeToContents);
62 mView->horizontalHeader()->setSectionResizeMode(
63 ComponentSymbolVariantItemListModel::COLUMN_SYMBOL, QHeaderView::Stretch);
64 mView->horizontalHeader()->setSectionResizeMode(
65 ComponentSymbolVariantItemListModel::COLUMN_SUFFIX,
66 QHeaderView::ResizeToContents);
67 mView->horizontalHeader()->setSectionResizeMode(
68 ComponentSymbolVariantItemListModel::COLUMN_ISREQUIRED,
69 QHeaderView::ResizeToContents);
70 mView->horizontalHeader()->setSectionResizeMode(
71 ComponentSymbolVariantItemListModel::COLUMN_X,
72 QHeaderView::ResizeToContents);
73 mView->horizontalHeader()->setSectionResizeMode(
74 ComponentSymbolVariantItemListModel::COLUMN_Y,
75 QHeaderView::ResizeToContents);
76 mView->horizontalHeader()->setSectionResizeMode(
77 ComponentSymbolVariantItemListModel::COLUMN_ROTATION,
78 QHeaderView::ResizeToContents);
79 mView->horizontalHeader()->setSectionResizeMode(
80 ComponentSymbolVariantItemListModel::COLUMN_ACTIONS,
81 QHeaderView::ResizeToContents);
82 connect(mView.data(), &EditableTableWidget::btnAddClicked, mModel.data(),
83 &ComponentSymbolVariantItemListModel::addItem);
84 connect(mView.data(), &EditableTableWidget::btnRemoveClicked, mModel.data(),
85 &ComponentSymbolVariantItemListModel::removeItem);
86 connect(mView.data(), &EditableTableWidget::btnMoveUpClicked, mModel.data(),
87 &ComponentSymbolVariantItemListModel::moveItemUp);
88 connect(mView.data(), &EditableTableWidget::btnMoveDownClicked, mModel.data(),
89 &ComponentSymbolVariantItemListModel::moveItemDown);
90 connect(mView.data(), &EditableTableWidget::btnBrowseClicked, this,
91 &ComponentSymbolVariantItemListEditorWidget::btnSymbolBrowseClicked);
92
93 QVBoxLayout* layout = new QVBoxLayout(this);
94 layout->setContentsMargins(0, 0, 0, 0);
95 layout->addWidget(mView.data());
96 }
97
98 ComponentSymbolVariantItemListEditorWidget::
~ComponentSymbolVariantItemListEditorWidget()99 ~ComponentSymbolVariantItemListEditorWidget() noexcept {
100 }
101
102 /*******************************************************************************
103 * Setters
104 ******************************************************************************/
105
setReadOnly(bool readOnly)106 void ComponentSymbolVariantItemListEditorWidget::setReadOnly(
107 bool readOnly) noexcept {
108 mView->setReadOnly(readOnly);
109 }
110
setReferences(const workspace::Workspace & ws,const IF_GraphicsLayerProvider & layerProvider,ComponentSymbolVariantItemList & items,const std::shared_ptr<const LibraryElementCache> & symbolCache,UndoStack * undoStack)111 void ComponentSymbolVariantItemListEditorWidget::setReferences(
112 const workspace::Workspace& ws,
113 const IF_GraphicsLayerProvider& layerProvider,
114 ComponentSymbolVariantItemList& items,
115 const std::shared_ptr<const LibraryElementCache>& symbolCache,
116 UndoStack* undoStack) noexcept {
117 mOnItemListEditedSlot.detachAll();
118 mOnItemEditedSlot.detachAll();
119
120 mWorkspace = &ws;
121 mLayerProvider = &layerProvider;
122 mModel->setSymbolsCache(symbolCache);
123 mModel->setItemList(&items);
124 mModel->setUndoStack(undoStack);
125
126 items.onEdited.attach(mOnItemListEditedSlot);
127 items.onElementEdited.attach(mOnItemEditedSlot);
128 }
129
resetReferences()130 void ComponentSymbolVariantItemListEditorWidget::resetReferences() noexcept {
131 mOnItemListEditedSlot.detachAll();
132 mOnItemEditedSlot.detachAll();
133
134 mModel->setItemList(nullptr);
135 mModel->setUndoStack(nullptr);
136 mModel->setSymbolsCache(nullptr);
137 mLayerProvider = nullptr;
138 mWorkspace = nullptr;
139 }
140
141 /*******************************************************************************
142 * Private Methods
143 ******************************************************************************/
144
itemListEdited(const ComponentSymbolVariantItemList & list,int index,const std::shared_ptr<const ComponentSymbolVariantItem> & item,ComponentSymbolVariantItemList::Event event)145 void ComponentSymbolVariantItemListEditorWidget::itemListEdited(
146 const ComponentSymbolVariantItemList& list, int index,
147 const std::shared_ptr<const ComponentSymbolVariantItem>& item,
148 ComponentSymbolVariantItemList::Event event) noexcept {
149 Q_UNUSED(list);
150 Q_UNUSED(index);
151 Q_UNUSED(item);
152 switch (event) {
153 case ComponentSymbolVariantItemList::Event::ElementAdded:
154 case ComponentSymbolVariantItemList::Event::ElementRemoved:
155 emit edited();
156 emit triggerGraphicsItemsUpdate();
157 break;
158 default:
159 break;
160 }
161 emit edited();
162 }
163
itemEdited(const ComponentSymbolVariantItemList & list,int index,const std::shared_ptr<const ComponentSymbolVariantItem> & item,ComponentSymbolVariantItem::Event event)164 void ComponentSymbolVariantItemListEditorWidget::itemEdited(
165 const ComponentSymbolVariantItemList& list, int index,
166 const std::shared_ptr<const ComponentSymbolVariantItem>& item,
167 ComponentSymbolVariantItem::Event event) noexcept {
168 Q_UNUSED(list);
169 Q_UNUSED(index);
170 Q_UNUSED(item);
171 switch (event) {
172 case ComponentSymbolVariantItem::Event::SymbolPositionChanged:
173 case ComponentSymbolVariantItem::Event::SymbolRotationChanged:
174 case ComponentSymbolVariantItem::Event::SymbolUuidChanged:
175 emit triggerGraphicsItemsUpdate();
176 break;
177 default:
178 break;
179 }
180 }
181
btnSymbolBrowseClicked(const QVariant & data)182 void ComponentSymbolVariantItemListEditorWidget::btnSymbolBrowseClicked(
183 const QVariant& data) noexcept {
184 SymbolChooserDialog dialog(*mWorkspace, *mLayerProvider, this);
185 if ((dialog.exec() == QDialog::Accepted) && dialog.getSelectedSymbolUuid()) {
186 mModel->changeSymbol(data, *dialog.getSelectedSymbolUuid());
187 }
188 }
189
190 /*******************************************************************************
191 * End of File
192 ******************************************************************************/
193
194 } // namespace editor
195 } // namespace library
196 } // namespace librepcb
197