Loading files
/*
* 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 "cmdaddcomponenttocircuit.h"
#include <librepcb/common/fileio/transactionalfilesystem.h>
#include <librepcb/library/cmp/component.h>
#include <librepcb/library/dev/device.h>
#include <librepcb/library/pkg/package.h>
#include <librepcb/project/circuit/cmd/cmdcomponentinstanceadd.h>
#include <librepcb/project/library/cmd/cmdprojectlibraryaddelement.h>
#include <librepcb/project/library/projectlibrary.h>
#include <librepcb/project/project.h>
#include <librepcb/workspace/library/workspacelibrarydb.h>
#include <librepcb/workspace/workspace.h>
#include <QtCore>
/*******************************************************************************
* Namespace
******************************************************************************/
namespace librepcb {
namespace project {
namespace editor {
/*******************************************************************************
* Constructors / Destructor
******************************************************************************/
CmdAddComponentToCircuit::CmdAddComponentToCircuit(
workspace::Workspace& workspace, Project& project, const Uuid& component,
const Uuid& symbolVariant, const tl::optional<Uuid>& defaultDevice) noexcept
: UndoCommandGroup(tr("Add component")),
mWorkspace(workspace),
mProject(project),
mComponentUuid(component),
mSymbVarUuid(symbolVariant),
mDefaultDeviceUuid(defaultDevice),
mCmdAddToCircuit(nullptr) {
}
CmdAddComponentToCircuit::~CmdAddComponentToCircuit() noexcept {
}
/*******************************************************************************
* Getters
******************************************************************************/
ComponentInstance* CmdAddComponentToCircuit::getComponentInstance() const
noexcept {
Q_ASSERT(mCmdAddToCircuit);
return mCmdAddToCircuit ? mCmdAddToCircuit->getComponentInstance() : nullptr;
}
/*******************************************************************************
* Inherited from UndoCommand
******************************************************************************/
bool CmdAddComponentToCircuit::performExecute() {
// if there is no such component in the project's library, copy it from the
// workspace library to the project's library
if (!mProject.getLibrary().getComponent(mComponentUuid)) {
FilePath cmpFp =
mWorkspace.getLibraryDb().getLatestComponent(mComponentUuid);
if (!cmpFp.isValid()) {
throw RuntimeError(
__FILE__, __LINE__,
QString(tr("The component with the UUID \"%1\" does not exist in the "
"workspace library!"))
.arg(mComponentUuid.toStr()));
}
library::Component* cmp = new library::Component(
std::unique_ptr<TransactionalDirectory>(new TransactionalDirectory(
TransactionalFileSystem::openRO(cmpFp))));
CmdProjectLibraryAddElement<library::Component>* cmdAddToLibrary =
new CmdProjectLibraryAddElement<library::Component>(
mProject.getLibrary(), *cmp);
appendChild(cmdAddToLibrary); // can throw
}
// If the pre-selected device or its package are not available yet in the
// project's library, copy them from the workspace library to the project's
// library too to have them available for adding to boards later.
if (mDefaultDeviceUuid) {
tl::optional<Uuid> pkgUuid;
if (const library::Device* prjDev =
mProject.getLibrary().getDevice(*mDefaultDeviceUuid)) {
pkgUuid = prjDev->getPackageUuid();
} else {
FilePath devFp =
mWorkspace.getLibraryDb().getLatestDevice(*mDefaultDeviceUuid);
if (!devFp.isValid()) {
throw RuntimeError(
__FILE__, __LINE__,
QString(tr("The device with the UUID \"%1\" does not exist in the "
"workspace library!"))
.arg(mDefaultDeviceUuid->toStr()));
}
library::Device* dev = new library::Device(
std::unique_ptr<TransactionalDirectory>(new TransactionalDirectory(
TransactionalFileSystem::openRO(devFp))));
pkgUuid = dev->getPackageUuid();
appendChild(new CmdProjectLibraryAddElement<library::Device>(
mProject.getLibrary(), *dev)); // can throw
}
Q_ASSERT(pkgUuid);
if (!mProject.getLibrary().getPackage(*pkgUuid)) {
FilePath pkgFp = mWorkspace.getLibraryDb().getLatestPackage(*pkgUuid);
if (!pkgFp.isValid()) {
throw RuntimeError(
__FILE__, __LINE__,
QString(tr("The package with the UUID \"%1\" does not exist in the "
"workspace library!"))
.arg(pkgUuid->toStr()));
}
library::Package* pkg = new library::Package(
std::unique_ptr<TransactionalDirectory>(new TransactionalDirectory(
TransactionalFileSystem::openRO(pkgFp))));
appendChild(new CmdProjectLibraryAddElement<library::Package>(
mProject.getLibrary(), *pkg)); // can throw
}
}
// create child command to add a new component instance to the circuit
mCmdAddToCircuit = new CmdComponentInstanceAdd(
mProject.getCircuit(), mComponentUuid, mSymbVarUuid, mDefaultDeviceUuid);
appendChild(mCmdAddToCircuit); // can throw
// execute all child commands
return UndoCommandGroup::performExecute(); // can throw
}
/*******************************************************************************
* End of File
******************************************************************************/
} // namespace editor
} // namespace project
} // namespace librepcb
9b2f48454This file is pinned to the selected release. Download the original to open it in your design tool.