Skip to content

Commit

Permalink
Schematic editor: Allow changing device of components
Browse files Browse the repository at this point in the history
  • Loading branch information
ubruhin committed Oct 26, 2019
1 parent 1b04cba commit 55fe2df
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 55 deletions.
Expand Up @@ -459,8 +459,9 @@ bool SES_Select::removeSelectedItems() noexcept {
}

void SES_Select::openSymbolPropertiesDialog(SI_Symbol& symbol) noexcept {
SymbolInstancePropertiesDialog dialog(mProject, symbol.getComponentInstance(),
symbol, mUndoStack, &mEditor);
SymbolInstancePropertiesDialog dialog(mWorkspace, mProject,
symbol.getComponentInstance(), symbol,
mUndoStack, &mEditor);
dialog.exec();
}

Expand Down
Expand Up @@ -29,13 +29,16 @@
#include <librepcb/common/undocommand.h>
#include <librepcb/common/undostack.h>
#include <librepcb/library/cmp/component.h>
#include <librepcb/library/dev/device.h>
#include <librepcb/library/sym/symbol.h>
#include <librepcb/project/circuit/cmd/cmdcomponentinstanceedit.h>
#include <librepcb/project/circuit/componentinstance.h>
#include <librepcb/project/project.h>
#include <librepcb/project/schematics/cmd/cmdsymbolinstanceedit.h>
#include <librepcb/project/schematics/items/si_symbol.h>
#include <librepcb/project/settings/projectsettings.h>
#include <librepcb/workspace/library/workspacelibrarydb.h>
#include <librepcb/workspace/workspace.h>

#include <QtCore>
#include <QtWidgets>
Expand All @@ -52,9 +55,10 @@ namespace editor {
******************************************************************************/

SymbolInstancePropertiesDialog::SymbolInstancePropertiesDialog(
Project& project, ComponentInstance& cmp, SI_Symbol& symbol,
UndoStack& undoStack, QWidget* parent) noexcept
workspace::Workspace& ws, Project& project, ComponentInstance& cmp,
SI_Symbol& symbol, UndoStack& undoStack, QWidget* parent) noexcept
: QDialog(parent),
mWorkspace(ws),
mProject(project),
mComponentInstance(cmp),
mSymbol(symbol),
Expand All @@ -74,13 +78,19 @@ SymbolInstancePropertiesDialog::SymbolInstancePropertiesDialog(

// Component Library Element Attributes
QString htmlLink("<a href=\"%1\">%2<a>");
mUi->lblCompLibName->setText(htmlLink.arg(
mComponentInstance.getLibComponent()
.getDirectory()
.getAbsPath()
.toQUrl()
.toString(),
*mComponentInstance.getLibComponent().getNames().value(localeOrder)));
mUi->lblCompLibName->setText(
htmlLink.arg(
mComponentInstance.getLibComponent()
.getDirectory()
.getAbsPath()
.toQUrl()
.toString(),
*mComponentInstance.getLibComponent().getNames().value(localeOrder)) +
" (" +
QString(tr("symbol variant \"%1\""))
.arg(*mComponentInstance.getSymbolVariant().getNames().value(
localeOrder)) +
")");
mUi->lblCompLibName->setToolTip(
mComponentInstance.getLibComponent().getDescriptions().value(
localeOrder) +
Expand All @@ -89,11 +99,6 @@ SymbolInstancePropertiesDialog::SymbolInstancePropertiesDialog(
.getDirectory()
.getAbsPath()
.toNative());
mUi->lblSymbVarName->setText(
*mComponentInstance.getSymbolVariant().getNames().value(localeOrder));
mUi->lblSymbVarName->setToolTip(
mComponentInstance.getSymbolVariant().getDescriptions().value(
localeOrder));

// Symbol Instance Attributes
mUi->lblSymbInstName->setText(mSymbol.getName());
Expand All @@ -110,6 +115,41 @@ SymbolInstancePropertiesDialog::SymbolInstancePropertiesDialog(
mSymbol.getLibSymbol().getDescriptions().value(localeOrder) + "<p>" +
mSymbol.getLibSymbol().getDirectory().getAbsPath().toNative());

// List Devices
try {
tl::optional<Uuid> device = mComponentInstance.getDefaultDeviceUuid();
QSet<Uuid> deviceUuids = mWorkspace.getLibraryDb().getDevicesOfComponent(
mComponentInstance.getLibComponent().getUuid()); // can throw
foreach (const Uuid& deviceUuid, deviceUuids) {
FilePath devFp =
mWorkspace.getLibraryDb().getLatestDevice(deviceUuid); // can throw
if (devFp.isValid()) {
QString devName;
mWorkspace.getLibraryDb().getElementTranslations<library::Device>(
devFp, localeOrder, &devName); // can throw
mUi->cbxPreselectedDevice->addItem(devName, deviceUuid.toStr());
}
}
mUi->cbxPreselectedDevice->model()->sort(0, Qt::AscendingOrder);
mUi->cbxPreselectedDevice->insertItem(0, "");
int index = 0;
if (device) {
index = mUi->cbxPreselectedDevice->findData(device->toStr());
if (index < 0) {
// Selected device not found in workspace libraries, show UUID instead.
mUi->cbxPreselectedDevice->insertItem(1, device->toStr(),
device->toStr());
index = 1;
}
}
mUi->cbxPreselectedDevice->setCurrentIndex(index);
mUi->cbxPreselectedDevice->setEnabled(mUi->cbxPreselectedDevice->count() >
1);
} catch (const Exception& e) {
qCritical() << "Could not list devices:" << e.getMsg();
mUi->cbxPreselectedDevice->setEnabled(false);
}

// set focus to component instance name
mUi->edtCompInstName->selectAll();
mUi->edtCompInstName->setFocus();
Expand Down Expand Up @@ -157,6 +197,10 @@ bool SymbolInstancePropertiesDialog::applyChanges() noexcept {
mUi->edtCompInstName->text().trimmed())); // can throw
cmdCmp->setValue(mUi->edtCompInstValue->toPlainText());
cmdCmp->setAttributes(mAttributes);
if (mUi->cbxPreselectedDevice->isEnabled()) {
cmdCmp->setDefaultDeviceUuid(Uuid::tryFromString(
mUi->cbxPreselectedDevice->currentData().toString()));
}
transaction.append(cmdCmp.take());

// Symbol Instance
Expand Down
Expand Up @@ -36,6 +36,10 @@ namespace librepcb {
class UndoStack;
class UndoCommand;

namespace workspace {
class Workspace;
}

namespace project {

class Project;
Expand Down Expand Up @@ -63,9 +67,10 @@ class SymbolInstancePropertiesDialog final : public QDialog {
SymbolInstancePropertiesDialog() = delete;
SymbolInstancePropertiesDialog(const SymbolInstancePropertiesDialog& other) =
delete;
SymbolInstancePropertiesDialog(Project& project, ComponentInstance& cmp,
SI_Symbol& symbol, UndoStack& undoStack,
QWidget* parent) noexcept;
SymbolInstancePropertiesDialog(workspace::Workspace& ws, Project& project,
ComponentInstance& cmp, SI_Symbol& symbol,
UndoStack& undoStack,
QWidget* parent) noexcept;
~SymbolInstancePropertiesDialog() noexcept;

// Operator Overloadings
Expand All @@ -78,6 +83,7 @@ class SymbolInstancePropertiesDialog final : public QDialog {
bool applyChanges() noexcept;

private: // Data
workspace::Workspace& mWorkspace;
Project& mProject;
ComponentInstance& mComponentInstance;
SI_Symbol& mSymbol;
Expand Down
Expand Up @@ -99,6 +99,32 @@
</widget>
</item>
<item row="2" column="0" colspan="2">
<widget class="QGroupBox" name="groupBox_2">
<property name="title">
<string>Boards</string>
</property>
<layout class="QFormLayout" name="formLayout_4">
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Pre-selected device:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="cbxPreselectedDevice">
<property name="currentText">
<string notr="true"/>
</property>
<property name="maxVisibleItems">
<number>20</number>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="3" column="0" colspan="2">
<widget class="QGroupBox" name="gbxCmpLib">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
Expand Down Expand Up @@ -146,41 +172,6 @@
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_18">
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Symbol Variant:</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLabel" name="lblSymbVarName">
<property name="sizePolicy">
<sizepolicy hsizetype="Ignored" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string notr="true">TextLabel</string>
</property>
<property name="textFormat">
<enum>Qt::RichText</enum>
</property>
<property name="openExternalLinks">
<bool>true</bool>
</property>
<property name="textInteractionFlags">
<set>Qt::LinksAccessibleByKeyboard|Qt::LinksAccessibleByMouse</set>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_17">
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Preferred">
Expand All @@ -193,7 +184,7 @@
</property>
</widget>
</item>
<item row="2" column="1">
<item row="1" column="1">
<widget class="QLabel" name="lblSymbLibName">
<property name="sizePolicy">
<sizepolicy hsizetype="Ignored" vsizetype="Preferred">
Expand Down

0 comments on commit 55fe2df

Please sign in to comment.