/
libraryoverviewwidget.cpp
463 lines (416 loc) · 16.9 KB
/
libraryoverviewwidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*
* LibrePCB - Professional EDA for everyone!
* Copyright (C) 2013 LibrePCB Developers, see AUTHORS.md for contributors.
* https://librepcb.org/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*******************************************************************************
* Includes
******************************************************************************/
#include "libraryoverviewwidget.h"
#include "librarylisteditorwidget.h"
#include "ui_libraryoverviewwidget.h"
#include <librepcb/common/dialogs/filedialog.h>
#include <librepcb/common/fileio/fileutils.h>
#include <librepcb/library/cmd/cmdlibraryedit.h>
#include <librepcb/library/elements.h>
#include <librepcb/library/msg/msgmissingauthor.h>
#include <librepcb/library/msg/msgnamenottitlecase.h>
#include <librepcb/workspace/library/workspacelibrarydb.h>
#include <librepcb/workspace/settings/workspacesettings.h>
#include <librepcb/workspace/workspace.h>
#include <QtCore>
#include <QtWidgets>
/*******************************************************************************
* Namespace
******************************************************************************/
namespace librepcb {
namespace library {
namespace editor {
/*******************************************************************************
* Constructors / Destructor
******************************************************************************/
LibraryOverviewWidget::LibraryOverviewWidget(const Context& context,
QSharedPointer<Library> lib,
QWidget* parent) noexcept
: EditorWidgetBase(context, lib->getFilePath(), parent),
mLibrary(lib),
mUi(new Ui::LibraryOverviewWidget) {
mUi->setupUi(this);
mUi->lstMessages->setHandler(this);
connect(mUi->btnIcon, &QPushButton::clicked, this,
&LibraryOverviewWidget::btnIconClicked);
connect(mUi->lstCmpCat, &QListWidget::doubleClicked, this,
&LibraryOverviewWidget::lstCmpCatDoubleClicked);
connect(mUi->lstPkgCat, &QListWidget::doubleClicked, this,
&LibraryOverviewWidget::lstPkgCatDoubleClicked);
connect(mUi->lstSym, &QListWidget::doubleClicked, this,
&LibraryOverviewWidget::lstSymDoubleClicked);
connect(mUi->lstPkg, &QListWidget::doubleClicked, this,
&LibraryOverviewWidget::lstPkgDoubleClicked);
connect(mUi->lstCmp, &QListWidget::doubleClicked, this,
&LibraryOverviewWidget::lstCmpDoubleClicked);
connect(mUi->lstDev, &QListWidget::doubleClicked, this,
&LibraryOverviewWidget::lstDevDoubleClicked);
// Insert dependencies editor widget.
mDependenciesEditorWidget.reset(
new LibraryListEditorWidget(mContext.workspace, this));
int row;
QFormLayout::ItemRole role;
mUi->formLayout->getWidgetPosition(mUi->lblDependencies, &row, &role);
mUi->formLayout->setWidget(row, QFormLayout::FieldRole,
mDependenciesEditorWidget.data());
// Load metadata.
updateMetadata();
// Reload metadata on undo stack state changes.
connect(mUndoStack.data(), &UndoStack::stateModified, this,
&LibraryOverviewWidget::updateMetadata);
// Handle changes of metadata.
connect(mUi->edtName, &QLineEdit::editingFinished, this,
&LibraryOverviewWidget::commitMetadata);
connect(mUi->edtDescription, &PlainTextEdit::editingFinished, this,
&LibraryOverviewWidget::commitMetadata);
connect(mUi->edtKeywords, &QLineEdit::editingFinished, this,
&LibraryOverviewWidget::commitMetadata);
connect(mUi->edtAuthor, &QLineEdit::editingFinished, this,
&LibraryOverviewWidget::commitMetadata);
connect(mUi->edtVersion, &QLineEdit::editingFinished, this,
&LibraryOverviewWidget::commitMetadata);
connect(mUi->cbxDeprecated, &QCheckBox::clicked, this,
&LibraryOverviewWidget::commitMetadata);
connect(mUi->edtUrl, &QLineEdit::editingFinished, this,
&LibraryOverviewWidget::commitMetadata);
connect(mDependenciesEditorWidget.data(), &LibraryListEditorWidget::edited,
this, &LibraryOverviewWidget::commitMetadata);
// Set up context menu triggers
connect(mUi->lstCmpCat, &QListWidget::customContextMenuRequested, this,
&LibraryOverviewWidget::openContextMenuAtPos);
connect(mUi->lstSym, &QListWidget::customContextMenuRequested, this,
&LibraryOverviewWidget::openContextMenuAtPos);
connect(mUi->lstCmp, &QListWidget::customContextMenuRequested, this,
&LibraryOverviewWidget::openContextMenuAtPos);
connect(mUi->lstPkgCat, &QListWidget::customContextMenuRequested, this,
&LibraryOverviewWidget::openContextMenuAtPos);
connect(mUi->lstPkg, &QListWidget::customContextMenuRequested, this,
&LibraryOverviewWidget::openContextMenuAtPos);
connect(mUi->lstDev, &QListWidget::customContextMenuRequested, this,
&LibraryOverviewWidget::openContextMenuAtPos);
// Load all library elements.
updateElementLists();
connect(&mContext.workspace.getLibraryDb(),
&workspace::WorkspaceLibraryDb::scanFinished, this,
&LibraryOverviewWidget::updateElementLists);
}
LibraryOverviewWidget::~LibraryOverviewWidget() noexcept {
}
/*******************************************************************************
* Public Slots
******************************************************************************/
bool LibraryOverviewWidget::save() noexcept {
// Commit metadata.
QString errorMsg = commitMetadata();
if (!errorMsg.isEmpty()) {
QMessageBox::critical(this, tr("Invalid metadata"), errorMsg);
return false;
}
// Save element.
try {
mLibrary->save(); // can throw
return EditorWidgetBase::save();
} catch (const Exception& e) {
QMessageBox::critical(this, tr("Save failed"), e.getMsg());
return false;
}
}
/*******************************************************************************
* Private Methods
******************************************************************************/
void LibraryOverviewWidget::updateMetadata() noexcept {
setWindowTitle(*mLibrary->getNames().getDefaultValue());
setWindowIcon(mLibrary->getIconAsPixmap());
mUi->btnIcon->setIcon(mLibrary->getIconAsPixmap());
if (mLibrary->getIconAsPixmap().isNull()) {
mUi->btnIcon->setText(mUi->btnIcon->toolTip());
} else {
mUi->btnIcon->setText(QString());
}
mUi->edtName->setText(*mLibrary->getNames().getDefaultValue());
mUi->edtDescription->setPlainText(
mLibrary->getDescriptions().getDefaultValue());
mUi->edtKeywords->setText(mLibrary->getKeywords().getDefaultValue());
mUi->edtAuthor->setText(mLibrary->getAuthor());
mUi->edtVersion->setText(mLibrary->getVersion().toStr());
mUi->cbxDeprecated->setChecked(mLibrary->isDeprecated());
mUi->edtUrl->setText(mLibrary->getUrl().toString());
mDependenciesEditorWidget->setUuids(mLibrary->getDependencies());
mIcon = mLibrary->getIcon();
}
QString LibraryOverviewWidget::commitMetadata() noexcept {
try {
QScopedPointer<CmdLibraryEdit> cmd(new CmdLibraryEdit(*mLibrary));
try {
// throws on invalid name
cmd->setName("", ElementName(mUi->edtName->text().trimmed()));
} catch (const Exception& e) {
}
cmd->setDescription("", mUi->edtDescription->toPlainText().trimmed());
cmd->setKeywords("", mUi->edtKeywords->text().trimmed());
try {
// throws on invalid version
cmd->setVersion(Version::fromString(mUi->edtVersion->text().trimmed()));
} catch (const Exception& e) {
}
cmd->setAuthor(mUi->edtAuthor->text().trimmed());
cmd->setDeprecated(mUi->cbxDeprecated->isChecked());
cmd->setUrl(QUrl::fromUserInput(mUi->edtUrl->text().trimmed()));
cmd->setDependencies(mDependenciesEditorWidget->getUuids());
cmd->setIcon(mIcon);
// Commit all changes.
mUndoStack->execCmd(cmd.take()); // can throw
// Reload metadata into widgets to discard invalid input.
updateMetadata();
} catch (const Exception& e) {
return e.getMsg();
}
return QString();
}
bool LibraryOverviewWidget::runChecks(
LibraryElementCheckMessageList& msgs) const {
msgs = mLibrary->runChecks(); // can throw
mUi->lstMessages->setMessages(msgs);
return true;
}
template <>
void LibraryOverviewWidget::fixMsg(const MsgNameNotTitleCase& msg) {
mUi->edtName->setText(*msg.getFixedName());
commitMetadata();
}
template <>
void LibraryOverviewWidget::fixMsg(const MsgMissingAuthor& msg) {
Q_UNUSED(msg);
mUi->edtAuthor->setText(getWorkspaceSettingsUserName());
commitMetadata();
}
template <typename MessageType>
bool LibraryOverviewWidget::fixMsgHelper(
std::shared_ptr<const LibraryElementCheckMessage> msg, bool applyFix) {
if (msg) {
if (auto m = msg->as<MessageType>()) {
if (applyFix) fixMsg(*m); // can throw
return true;
}
}
return false;
}
bool LibraryOverviewWidget::processCheckMessage(
std::shared_ptr<const LibraryElementCheckMessage> msg, bool applyFix) {
if (fixMsgHelper<MsgNameNotTitleCase>(msg, applyFix)) return true;
if (fixMsgHelper<MsgMissingAuthor>(msg, applyFix)) return true;
return false;
}
void LibraryOverviewWidget::updateElementLists() noexcept {
updateElementList<ComponentCategory>(*mUi->lstCmpCat,
QIcon(":/img/places/folder.png"));
updateElementList<PackageCategory>(*mUi->lstPkgCat,
QIcon(":/img/places/folder_green.png"));
updateElementList<Symbol>(*mUi->lstSym, QIcon(":/img/library/symbol.png"));
updateElementList<Package>(*mUi->lstPkg, QIcon(":/img/library/package.png"));
updateElementList<Component>(*mUi->lstCmp,
QIcon(":/img/library/component.png"));
updateElementList<Device>(*mUi->lstDev, QIcon(":/img/library/device.png"));
}
template <typename ElementType>
void LibraryOverviewWidget::updateElementList(QListWidget& listWidget,
const QIcon& icon) noexcept {
QHash<FilePath, QString> elementNames;
try {
// get all library element names
QList<FilePath> elements =
mContext.workspace.getLibraryDb().getLibraryElements<ElementType>(
mLibrary->getFilePath()); // can throw
foreach (const FilePath& filepath, elements) {
QString name;
mContext.workspace.getLibraryDb().getElementTranslations<ElementType>(
filepath, getLibLocaleOrder(), &name); // can throw
elementNames.insert(filepath, name);
}
} catch (const Exception& e) {
listWidget.clear();
QListWidgetItem* item = new QListWidgetItem(&listWidget);
item->setText(e.getMsg());
item->setToolTip(e.getMsg());
item->setIcon(QIcon(":/img/status/dialog_error.png"));
item->setBackground(Qt::red);
item->setForeground(Qt::white);
return;
}
// update/remove existing list widget items
for (int i = listWidget.count() - 1; i >= 0; --i) {
QListWidgetItem* item = listWidget.item(i);
Q_ASSERT(item);
FilePath filePath(item->data(Qt::UserRole).toString());
if (elementNames.contains(filePath)) {
item->setText(elementNames.take(filePath));
} else {
delete item;
}
}
// add new list widget items
foreach (const FilePath& fp, elementNames.keys()) {
QString name = elementNames.value(fp);
QListWidgetItem* item = new QListWidgetItem(&listWidget);
item->setText(name);
item->setToolTip(name);
item->setData(Qt::UserRole, fp.toStr());
item->setIcon(icon);
}
}
void LibraryOverviewWidget::openContextMenuAtPos(const QPoint& pos) noexcept {
// Get selected item (may be null)
QListWidget* list = dynamic_cast<QListWidget*>(sender());
Q_ASSERT(list);
QListWidgetItem* selectedItem = list->itemAt(pos);
// Get item text and validated file path
QString itemName = selectedItem ? selectedItem->text() : QString();
tl::optional<FilePath> itemPath = tl::nullopt;
if (selectedItem) {
FilePath fp = FilePath(selectedItem->data(Qt::UserRole).toString());
if (fp.isValid()) {
itemPath = tl::make_optional(fp);
} else {
qWarning() << "File path for selected item is not valid";
}
}
// Build the context menu
QMenu menu;
QAction* aRemove =
menu.addAction(QIcon(":/img/actions/delete.png"), tr("Remove"));
aRemove->setEnabled(selectedItem && itemPath.has_value());
// Show context menu, handle action
QAction* action = menu.exec(QCursor::pos());
if (action == aRemove) {
Q_ASSERT(selectedItem);
Q_ASSERT(itemPath.has_value());
if (removeSelectedItem(itemName, *itemPath)) {
delete selectedItem;
};
}
}
bool LibraryOverviewWidget::removeSelectedItem(
const QString& itemName, const FilePath& itemPath) noexcept {
int ret = QMessageBox::warning(
this, tr("Remove %1").arg(itemName),
QString(tr("WARNING: Library elements must normally NOT be removed "
"because this will break "
"other elements which depend on this one! They should be just "
"marked as "
"deprecated instead.\n\nAre you still sure to delete the "
"whole library element "
"\"%1\"?\n\nThis cannot be undone!"))
.arg(itemName),
QMessageBox::Yes, QMessageBox::Cancel);
if (ret == QMessageBox::Yes) {
try {
// Emit signal so that the library editor can close any tabs that have
// opened this item
emit removeElementTriggered(itemPath);
FileUtils::removeDirRecursively(itemPath);
} catch (const Exception& e) {
QMessageBox::critical(this, tr("Error"), e.getMsg());
mContext.workspace.getLibraryDb().startLibraryRescan();
return false;
}
mContext.workspace.getLibraryDb().startLibraryRescan();
return true;
} else {
return false;
}
}
/*******************************************************************************
* Event Handlers
******************************************************************************/
void LibraryOverviewWidget::btnIconClicked() noexcept {
QString fp = FileDialog::getOpenFileName(
this, tr("Choose library icon"), mLibrary->getIconFilePath().toNative(),
tr("Portable Network Graphics (*.png)"));
if (!fp.isEmpty()) {
try {
mIcon = FileUtils::readFile(FilePath(fp)); // can throw
commitMetadata();
} catch (const Exception& e) {
QMessageBox::critical(this, tr("Could not open file"), e.getMsg());
}
}
}
void LibraryOverviewWidget::lstCmpCatDoubleClicked(
const QModelIndex& index) noexcept {
QListWidgetItem* item = mUi->lstCmpCat->item(index.row());
FilePath fp =
item ? FilePath(item->data(Qt::UserRole).toString()) : FilePath();
if (fp.isValid()) {
emit editComponentCategoryTriggered(fp);
}
}
void LibraryOverviewWidget::lstPkgCatDoubleClicked(
const QModelIndex& index) noexcept {
QListWidgetItem* item = mUi->lstPkgCat->item(index.row());
FilePath fp =
item ? FilePath(item->data(Qt::UserRole).toString()) : FilePath();
if (fp.isValid()) {
emit editPackageCategoryTriggered(fp);
}
}
void LibraryOverviewWidget::lstSymDoubleClicked(
const QModelIndex& index) noexcept {
QListWidgetItem* item = mUi->lstSym->item(index.row());
FilePath fp =
item ? FilePath(item->data(Qt::UserRole).toString()) : FilePath();
if (fp.isValid()) {
emit editSymbolTriggered(fp);
}
}
void LibraryOverviewWidget::lstPkgDoubleClicked(
const QModelIndex& index) noexcept {
QListWidgetItem* item = mUi->lstPkg->item(index.row());
FilePath fp =
item ? FilePath(item->data(Qt::UserRole).toString()) : FilePath();
if (fp.isValid()) {
emit editPackageTriggered(fp);
}
}
void LibraryOverviewWidget::lstCmpDoubleClicked(
const QModelIndex& index) noexcept {
QListWidgetItem* item = mUi->lstCmp->item(index.row());
FilePath fp =
item ? FilePath(item->data(Qt::UserRole).toString()) : FilePath();
if (fp.isValid()) {
emit editComponentTriggered(fp);
}
}
void LibraryOverviewWidget::lstDevDoubleClicked(
const QModelIndex& index) noexcept {
QListWidgetItem* item = mUi->lstDev->item(index.row());
FilePath fp =
item ? FilePath(item->data(Qt::UserRole).toString()) : FilePath();
if (fp.isValid()) {
emit editDeviceTriggered(fp);
}
}
/*******************************************************************************
* End of File
******************************************************************************/
} // namespace editor
} // namespace library
} // namespace librepcb