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 "boardeditor.h"
#include "../dialogs/bomgeneratordialog.h"
#include "../dialogs/projectpropertieseditordialog.h"
#include "../docks/ercmsgdock.h"
#include "../projecteditor.h"
#include "boarddesignrulecheckdialog.h"
#include "boarddesignrulecheckmessagesdock.h"
#include "boardlayersdock.h"
#include "boardlayerstacksetupdialog.h"
#include "boardpickplacegeneratordialog.h"
#include "fabricationoutputdialog.h"
#include "fsm/bes_fsm.h"
#include "ui_boardeditor.h"
#include "unplacedcomponentsdock.h"
#include <librepcb/common/application.h>
#include <librepcb/common/dialogs/aboutdialog.h>
#include <librepcb/common/dialogs/boarddesignrulesdialog.h>
#include <librepcb/common/dialogs/filedialog.h>
#include <librepcb/common/dialogs/gridsettingsdialog.h>
#include <librepcb/common/fileio/fileutils.h>
#include <librepcb/common/graphics/graphicsscene.h>
#include <librepcb/common/graphics/graphicsview.h>
#include <librepcb/common/graphics/primitivepathgraphicsitem.h>
#include <librepcb/common/gridproperties.h>
#include <librepcb/common/undostack.h>
#include <librepcb/common/utils/exclusiveactiongroup.h>
#include <librepcb/common/utils/undostackactiongroup.h>
#include <librepcb/project/boards/board.h>
#include <librepcb/project/boards/boardlayerstack.h>
#include <librepcb/project/boards/cmd/cmdboardadd.h>
#include <librepcb/project/boards/cmd/cmdboarddesignrulesmodify.h>
#include <librepcb/project/boards/cmd/cmdboardremove.h>
#include <librepcb/project/boards/items/bi_device.h>
#include <librepcb/project/boards/items/bi_footprint.h>
#include <librepcb/project/boards/items/bi_plane.h>
#include <librepcb/project/circuit/circuit.h>
#include <librepcb/project/circuit/componentinstance.h>
#include <librepcb/project/metadata/projectmetadata.h>
#include <librepcb/project/project.h>
#include <librepcb/project/settings/projectsettings.h>
#include <librepcb/workspace/library/workspacelibrarydb.h>
#include <librepcb/workspace/settings/workspacesettings.h>
#include <librepcb/workspace/workspace.h>
#include <QSvgGenerator>
#include <QtCore>
#include <QtPrintSupport>
#include <QtWidgets>
/*******************************************************************************
* Namespace
******************************************************************************/
namespace librepcb {
namespace project {
namespace editor {
/*******************************************************************************
* Constructors / Destructor
******************************************************************************/
BoardEditor::BoardEditor(ProjectEditor& projectEditor, Project& project)
: QMainWindow(0),
mProjectEditor(projectEditor),
mProject(project),
mUi(new Ui::BoardEditor),
mGraphicsView(nullptr),
mActiveBoard(nullptr),
mBoardListActionGroup(this),
mErcMsgDock(nullptr),
mUnplacedComponentsDock(nullptr),
mBoardLayersDock(nullptr),
mDrcMessagesDock(),
mFsm(nullptr) {
mUi->setupUi(this);
mUi->lblUnplacedComponentsNote->hide();
mUi->actionProjectSave->setEnabled(mProject.getDirectory().isWritable());
// set window title
QString filenameStr = mProject.getFilepath().getFilename();
if (!mProject.getDirectory().isWritable()) {
filenameStr.append(QStringLiteral(" [Read-Only]"));
}
setWindowTitle(QString("%1 - LibrePCB Board Editor").arg(filenameStr));
// Add Dock Widgets
mUnplacedComponentsDock = new UnplacedComponentsDock(mProjectEditor);
connect(mUnplacedComponentsDock,
&UnplacedComponentsDock::unplacedComponentsCountChanged, this,
&BoardEditor::unplacedComponentsCountChanged);
connect(mUnplacedComponentsDock, &UnplacedComponentsDock::addDeviceTriggered,
[this](ComponentInstance& cmp, const Uuid& dev, const Uuid& fpt) {
mFsm->processEvent(new BEE_StartAddDevice(cmp, dev, fpt), true);
});
addDockWidget(Qt::RightDockWidgetArea, mUnplacedComponentsDock, Qt::Vertical);
mBoardLayersDock = new BoardLayersDock(*this);
addDockWidget(Qt::RightDockWidgetArea, mBoardLayersDock, Qt::Vertical);
tabifyDockWidget(mUnplacedComponentsDock, mBoardLayersDock);
mErcMsgDock = new ErcMsgDock(mProject);
addDockWidget(Qt::RightDockWidgetArea, mErcMsgDock, Qt::Vertical);
tabifyDockWidget(mBoardLayersDock, mErcMsgDock);
mDrcMessagesDock.reset(new BoardDesignRuleCheckMessagesDock(this));
connect(mDrcMessagesDock.data(),
&BoardDesignRuleCheckMessagesDock::messageSelected, this,
&BoardEditor::highlightDrcMessage);
addDockWidget(Qt::RightDockWidgetArea, mDrcMessagesDock.data());
tabifyDockWidget(mErcMsgDock, mDrcMessagesDock.data());
mUnplacedComponentsDock->raise();
// add graphics view as central widget
mGraphicsView = new GraphicsView(nullptr, this);
mGraphicsView->setUseOpenGl(
mProjectEditor.getWorkspace().getSettings().useOpenGl.get());
mGraphicsView->setBackgroundBrush(Qt::black);
mGraphicsView->setForegroundBrush(Qt::white);
// setCentralWidget(mGraphicsView);
mUi->centralwidget->layout()->addWidget(mGraphicsView);
// Add actions to toggle visibility of dock widgets
mUi->menuView->addSeparator();
mUi->menuView->addAction(mUnplacedComponentsDock->toggleViewAction());
mUi->menuView->addAction(mBoardLayersDock->toggleViewAction());
mUi->menuView->addAction(mErcMsgDock->toggleViewAction());
mUi->menuView->addAction(mDrcMessagesDock->toggleViewAction());
// add all boards to the menu and connect to project signals
for (int i = 0; i < mProject.getBoards().count(); i++) boardAdded(i);
connect(&mProject, &Project::boardAdded, this, &BoardEditor::boardAdded);
connect(&mProject, &Project::boardRemoved, this, &BoardEditor::boardRemoved);
connect(&mBoardListActionGroup, &QActionGroup::triggered, this,
&BoardEditor::boardListActionGroupTriggered);
// connect some actions which are created with the Qt Designer
connect(mUi->actionProjectSave, &QAction::triggered, &mProjectEditor,
&ProjectEditor::saveProject);
connect(mUi->actionQuit, &QAction::triggered, this, &BoardEditor::close);
connect(mUi->actionOpenWebsite, &QAction::triggered,
[]() { QDesktopServices::openUrl(QUrl("https://librepcb.org")); });
connect(mUi->actionOnlineDocumentation, &QAction::triggered, []() {
QDesktopServices::openUrl(QUrl("https://docs.librepcb.org"));
});
connect(mUi->actionAbout, &QAction::triggered, qApp, &Application::about);
connect(mUi->actionAboutQt, &QAction::triggered, qApp,
&QApplication::aboutQt);
connect(mUi->actionZoomIn, &QAction::triggered, mGraphicsView,
&GraphicsView::zoomIn);
connect(mUi->actionZoomOut, &QAction::triggered, mGraphicsView,
&GraphicsView::zoomOut);
connect(mUi->actionZoomAll, &QAction::triggered, mGraphicsView,
&GraphicsView::zoomAll);
connect(mUi->actionShowControlPanel, &QAction::triggered, &mProjectEditor,
&ProjectEditor::showControlPanelClicked);
connect(mUi->actionShowSchematicEditor, &QAction::triggered, &mProjectEditor,
&ProjectEditor::showSchematicEditor);
connect(mUi->actionEditNetClasses, &QAction::triggered,
[this]() { mProjectEditor.execNetClassesEditorDialog(this); });
connect(mUi->actionProjectSettings, &QAction::triggered,
[this]() { mProjectEditor.execProjectSettingsDialog(this); });
connect(mUi->actionExportLppz, &QAction::triggered,
[this]() { mProjectEditor.execLppzExportDialog(this); });
// connect the undo/redo actions with the UndoStack of the project
mUndoStackActionGroup.reset(
new UndoStackActionGroup(*mUi->actionUndo, *mUi->actionRedo, nullptr,
&mProjectEditor.getUndoStack(), this));
// build the whole board editor finite state machine with all its substate
// objects
mFsm =
new BES_FSM(*this, *mUi, *mGraphicsView, mProjectEditor.getUndoStack());
// connect the "tools" toolbar with the state machine
mToolsActionGroup.reset(new ExclusiveActionGroup());
mToolsActionGroup->addAction(BES_FSM::State::State_Select,
mUi->actionToolSelect);
mToolsActionGroup->addAction(BES_FSM::State::State_DrawTrace,
mUi->actionToolDrawTrace);
mToolsActionGroup->addAction(BES_FSM::State::State_AddVia,
mUi->actionToolAddVia);
mToolsActionGroup->addAction(BES_FSM::State::State_DrawPolygon,
mUi->actionToolDrawPolygon);
mToolsActionGroup->addAction(BES_FSM::State::State_DrawPlane,
mUi->actionToolAddPlane);
mToolsActionGroup->addAction(BES_FSM::State::State_AddStrokeText,
mUi->actionToolAddText);
mToolsActionGroup->addAction(BES_FSM::State::State_AddHole,
mUi->actionToolAddHole);
mToolsActionGroup->setCurrentAction(mFsm->getCurrentState());
connect(mFsm, &BES_FSM::stateChanged, mToolsActionGroup.data(),
&ExclusiveActionGroup::setCurrentAction);
connect(mToolsActionGroup.data(),
&ExclusiveActionGroup::changeRequestTriggered, this,
&BoardEditor::toolActionGroupChangeTriggered);
// connect the "command" toolbar with the state machine
connect(mUi->actionCommandAbort, &QAction::triggered, [this]() {
mFsm->processEvent(new BEE_Base(BEE_Base::AbortCommand), true);
});
// connect the "edit" toolbar with the state machine
connect(mUi->actionSelectAll, &QAction::triggered, [this]() {
mFsm->processEvent(new BEE_Base(BEE_Base::Edit_SelectAll), true);
});
connect(mUi->actionCopy, &QAction::triggered, [this]() {
mFsm->processEvent(new BEE_Base(BEE_Base::Edit_Copy), true);
});
connect(mUi->actionCut, &QAction::triggered, [this]() {
mFsm->processEvent(new BEE_Base(BEE_Base::Edit_Cut), true);
});
connect(mUi->actionPaste, &QAction::triggered, [this]() {
mFsm->processEvent(new BEE_Base(BEE_Base::Edit_Paste), true);
});
connect(mUi->actionRotate_CW, &QAction::triggered, [this]() {
mFsm->processEvent(new BEE_Base(BEE_Base::Edit_RotateCW), true);
});
connect(mUi->actionRotate_CCW, &QAction::triggered, [this]() {
mFsm->processEvent(new BEE_Base(BEE_Base::Edit_RotateCCW), true);
});
connect(mUi->actionFlipHorizontal, &QAction::triggered, [this]() {
mFsm->processEvent(new BEE_Base(BEE_Base::Edit_FlipHorizontal), true);
});
connect(mUi->actionFlipVertical, &QAction::triggered, [this]() {
mFsm->processEvent(new BEE_Base(BEE_Base::Edit_FlipVertical), true);
});
connect(mUi->actionRemove, &QAction::triggered, [this]() {
mFsm->processEvent(new BEE_Base(BEE_Base::Edit_Remove), true);
});
// setup "search" toolbar
mUi->searchToolbar->setPlaceholderText(tr("Find device..."));
mUi->searchToolbar->setCompleterListFunction(
std::bind(&BoardEditor::getSearchToolBarCompleterList, this));
connect(mUi->searchToolbar, &SearchToolBar::goToTriggered, this,
&BoardEditor::goToDevice);
// setup status bar
mUi->statusbar->setFields(StatusBar::AbsolutePosition |
StatusBar::ProgressBar);
mUi->statusbar->setProgressBarTextFormat(tr("Scanning libraries (%p%)"));
connect(&mProjectEditor.getWorkspace().getLibraryDb(),
&workspace::WorkspaceLibraryDb::scanProgressUpdate, mUi->statusbar,
&StatusBar::setProgressBarPercent, Qt::QueuedConnection);
connect(mGraphicsView, &GraphicsView::cursorScenePositionChanged,
mUi->statusbar, &StatusBar::setAbsoluteCursorPosition);
// Restore Window Geometry
QSettings clientSettings;
restoreGeometry(
clientSettings.value("board_editor/window_geometry").toByteArray());
restoreState(clientSettings.value("board_editor/window_state").toByteArray());
// Load first board
if (mProject.getBoards().count() > 0) setActiveBoardIndex(0);
// Set focus to graphics view (avoid having the focus in some arbitrary
// widget).
mGraphicsView->setFocus();
// mGraphicsView->zoomAll(); does not work properly here, should be executed
// later in the event loop (ugly, but seems to work...)
#if (QT_VERSION >= QT_VERSION_CHECK(5, 4, 0))
QTimer::singleShot(200, mGraphicsView, &GraphicsView::zoomAll);
#else
QTimer::singleShot(200, mGraphicsView, SLOT(zoomAll()));
#endif
}
BoardEditor::~BoardEditor() {
// Save Window Geometry
QSettings clientSettings;
clientSettings.setValue("board_editor/window_geometry", saveGeometry());
clientSettings.setValue("board_editor/window_state", saveState());
delete mFsm;
mFsm = nullptr;
qDeleteAll(mBoardListActions);
mBoardListActions.clear();
delete mBoardLayersDock;
mBoardLayersDock = nullptr;
delete mUnplacedComponentsDock;
mUnplacedComponentsDock = nullptr;
delete mErcMsgDock;
mErcMsgDock = nullptr;
mDrcMessagesDock.reset();
delete mGraphicsView;
mGraphicsView = nullptr;
delete mUi;
mUi = nullptr;
}
/*******************************************************************************
* Setters
******************************************************************************/
void BoardEditor::setActiveBoardIndex(int index) noexcept {
Board* newBoard = mProject.getBoardByIndex(index);
if (newBoard != mActiveBoard) {
if (mActiveBoard) {
// stop airwire rebuild on every project modification (for performance
// reasons)
disconnect(&mProjectEditor.getUndoStack(), &UndoStack::stateModified,
mActiveBoard.data(), &Board::triggerAirWiresRebuild);
// save current view scene rect
mActiveBoard->saveViewSceneRect(mGraphicsView->getVisibleSceneRect());
}
mActiveBoard = newBoard;
if (mActiveBoard) {
// show scene, restore view scene rect, set grid properties
mActiveBoard->showInView(*mGraphicsView);
mGraphicsView->setVisibleSceneRect(mActiveBoard->restoreViewSceneRect());
mGraphicsView->setGridProperties(mActiveBoard->getGridProperties());
mUi->statusbar->setLengthUnit(
mActiveBoard->getGridProperties().getUnit());
// force airwire rebuild immediately and on every project modification
mActiveBoard->triggerAirWiresRebuild();
connect(&mProjectEditor.getUndoStack(), &UndoStack::stateModified,
mActiveBoard.data(), &Board::triggerAirWiresRebuild);
} else {
mGraphicsView->setScene(nullptr);
}
// update dock widgets
mUnplacedComponentsDock->setBoard(mActiveBoard);
mBoardLayersDock->setActiveBoard(mActiveBoard);
mDrcMessagesDock->setMessages(mActiveBoard
? mDrcMessages[mActiveBoard->getUuid()]
: QList<BoardDesignRuleCheckMessage>());
// update toolbars
mUi->actionGrid->setEnabled(mActiveBoard != nullptr);
}
// update GUI
mUi->tabBar->setCurrentIndex(index);
for (int i = 0; i < mBoardListActions.count(); ++i) {
mBoardListActions.at(i)->setChecked(i == index);
}
}
/*******************************************************************************
* General Methods
******************************************************************************/
void BoardEditor::abortAllCommands() noexcept {
// ugly... ;-)
mFsm->processEvent(new BEE_Base(BEE_Base::AbortCommand), true);
mFsm->processEvent(new BEE_Base(BEE_Base::AbortCommand), true);
mFsm->processEvent(new BEE_Base(BEE_Base::AbortCommand), true);
}
/*******************************************************************************
* Inherited Methods
******************************************************************************/
void BoardEditor::closeEvent(QCloseEvent* event) {
if (!mProjectEditor.windowIsAboutToClose(*this))
event->ignore();
else
QMainWindow::closeEvent(event);
}
/*******************************************************************************
* Public Slots
******************************************************************************/
void BoardEditor::boardAdded(int newIndex) {
Board* board = mProject.getBoardByIndex(newIndex);
Q_ASSERT(board);
if (!board) return;
QAction* actionBefore = mBoardListActions.value(newIndex - 1);
QAction* newAction = new QAction(*board->getName(), this);
newAction->setCheckable(true);
mUi->menuBoard->insertAction(actionBefore, newAction);
mBoardListActions.insert(newIndex, newAction);
mBoardListActionGroup.addAction(newAction);
mUi->tabBar->insertTab(newIndex, *board->getName());
}
void BoardEditor::boardRemoved(int oldIndex) {
QAction* action = mBoardListActions.takeAt(oldIndex);
Q_ASSERT(action);
mBoardListActionGroup.removeAction(action);
delete action;
mUi->tabBar->removeTab(oldIndex); // calls setActiveBoardIndex() if needed
}
/*******************************************************************************
* Actions
******************************************************************************/
void BoardEditor::on_actionProjectClose_triggered() {
mProjectEditor.closeAndDestroy(true, this);
}
void BoardEditor::on_actionNewBoard_triggered() {
bool ok = false;
QString name =
QInputDialog::getText(this, tr("Add New Board"), tr("Choose a name:"),
QLineEdit::Normal, tr("new_board"), &ok);
if (!ok) return;
try {
CmdBoardAdd* cmd =
new CmdBoardAdd(mProject, ElementName(name)); // can throw
mProjectEditor.getUndoStack().execCmd(cmd);
setActiveBoardIndex(mProject.getBoardIndex(*cmd->getBoard()));
} catch (Exception& e) {
QMessageBox::critical(this, tr("Error"), e.getMsg());
}
}
void BoardEditor::on_actionCopyBoard_triggered() {
Board* board = getActiveBoard();
if (!board) return;
bool ok = false;
QString name = QInputDialog::getText(
this, tr("Copy Board"), tr("Choose a name:"), QLineEdit::Normal,
QString(tr("copy_of_%1")).arg(*board->getName()), &ok);
if (!ok) return;
try {
CmdBoardAdd* cmd =
new CmdBoardAdd(mProject, *board, ElementName(name)); // can throw
mProjectEditor.getUndoStack().execCmd(cmd);
setActiveBoardIndex(mProject.getBoardIndex(*cmd->getBoard()));
} catch (Exception& e) {
QMessageBox::critical(this, tr("Error"), e.getMsg());
}
}
void BoardEditor::on_actionRemoveBoard_triggered() {
Board* board = getActiveBoard();
if (!board) return;
QMessageBox::StandardButton btn = QMessageBox::question(
this, tr("Remove board"),
QString(tr("Are you really sure to remove the board \"%1\"?"))
.arg(*board->getName()));
if (btn != QMessageBox::Yes) return;
try {
mProjectEditor.getUndoStack().execCmd(new CmdBoardRemove(*board));
} catch (const Exception& e) {
QMessageBox::critical(this, tr("Error"), e.getMsg());
}
}
void BoardEditor::on_actionGrid_triggered() {
if (Board* activeBoard = getActiveBoard()) {
GridSettingsDialog dialog(activeBoard->getGridProperties(), this);
connect(&dialog, &GridSettingsDialog::gridPropertiesChanged,
[this](const GridProperties& grid) {
mGraphicsView->setGridProperties(grid);
mUi->statusbar->setLengthUnit(grid.getUnit());
});
if (dialog.exec()) {
// In contrast to schematics, apply the grid only on the currently active
// board instead of all, so we can use different grids for each board.
activeBoard->setGridProperties(dialog.getGrid());
}
}
}
void BoardEditor::on_actionPrint_triggered() {
try {
Board* board = getActiveBoard();
if (!board) {
throw Exception(__FILE__, __LINE__, tr("No board selected."));
}
QPrinter printer(QPrinter::HighResolution);
printer.setPaperSize(QPrinter::A4);
printer.setOrientation(QPrinter::Landscape);
printer.setCreator(QString("LibrePCB %1").arg(qApp->applicationVersion()));
printer.setDocName(*mProject.getMetadata().getName());
QPrintDialog printDialog(&printer, this);
printDialog.setOption(QAbstractPrintDialog::PrintSelection, false);
printDialog.setMinMax(1, 1);
if (printDialog.exec() == QDialog::Accepted) {
board->print(printer); // can throw
}
} catch (Exception& e) {
QMessageBox::warning(this, tr("Error"), e.getMsg());
}
}
void BoardEditor::on_actionExportAsPdf_triggered() {
try {
Board* board = getActiveBoard();
if (!board) {
throw Exception(__FILE__, __LINE__, tr("No board selected."));
}
QString projectName =
FilePath::cleanFileName(*mProject.getMetadata().getName(),
FilePath::ReplaceSpaces | FilePath::KeepCase);
QString projectVersion =
FilePath::cleanFileName(mProject.getMetadata().getVersion(),
FilePath::ReplaceSpaces | FilePath::KeepCase);
QString relativePath =
QString("output/%1/%2_Board.pdf").arg(projectVersion, projectName);
FilePath defaultFilePath = mProject.getPath().getPathTo(relativePath);
QDir().mkpath(defaultFilePath.getParentDir().toStr());
QString filename = FileDialog::getSaveFileName(
this, tr("PDF Export"), defaultFilePath.toNative(), "*.pdf");
if (filename.isEmpty()) return;
if (!filename.endsWith(".pdf")) filename.append(".pdf");
FilePath filepath(filename);
// Create output directory first because QPrinter silently fails if it
// doesn't exist.
FileUtils::makePath(filepath.getParentDir()); // can throw
// Print (use local block scope to ensure the PDF is fully written & closed
// after leaving the block - without this, opening the PDF could fail)
{
QPrinter printer(QPrinter::HighResolution);
printer.setPaperSize(QPrinter::A4);
printer.setOrientation(QPrinter::Landscape);
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setCreator(
QString("LibrePCB %1").arg(qApp->applicationVersion()));
printer.setOutputFileName(filepath.toStr());
board->print(printer); // can throw
}
// Open PDF
{
namespace ws = ::librepcb::workspace;
const ws::WorkspaceSettings& workspaceSettings =
mProjectEditor.getWorkspace().getSettings();
ws::WorkspaceSettings::PdfOpenBehavior bhv
= workspaceSettings.pdfOpenBehavior.get();
if (bhv == ws::WorkspaceSettings::PdfOpenBehavior::NEVER) {
// do nothing
} else {
// TODO: refractor into FileUtils::askUserAndOpen or something similar
// to avoid duplicated code in SchematicEditor
bool doOpenPdf = true;
if (bhv == ws::WorkspaceSettings::PdfOpenBehavior::ASK) {
int openPdf = QMessageBox::information(
this, tr("PDF Export"),
tr("PDF exported successfully"),
QMessageBox::Ok | QMessageBox::Open);
if (openPdf == QMessageBox::Ok)
doOpenPdf = false;
} else if (bhv != ws::WorkspaceSettings::PdfOpenBehavior::ALWAYS) {
// this is the last possible state, otherwise there is an error
throw LogicError(__FILE__, __LINE__);
}
if (doOpenPdf) {
if (workspaceSettings.useCustomPdfReader.get()) {
QString pdfCmd = workspaceSettings.pdfReaderCommand.get();
// TODO: make it smarter to detect or insert quotes for path
// containing spaces
QProcess::startDetached(pdfCmd.replace(
"{{FILEPATH}}", filepath.toNative()));
} else {
QDesktopServices::openUrl(QUrl::fromLocalFile(filepath.toNative()));
}
}
}
}
} catch (Exception& e) {
QMessageBox::warning(this, tr("Error"), e.getMsg());
}
}
void BoardEditor::on_actionExportAsSvg_triggered() {
try {
Board* board = getActiveBoard();
if (!board) {
throw Exception(__FILE__, __LINE__, tr("No board selected."));
}
QString projectName =
FilePath::cleanFileName(*mProject.getMetadata().getName(),
FilePath::ReplaceSpaces | FilePath::KeepCase);
QString projectVersion =
FilePath::cleanFileName(mProject.getMetadata().getVersion(),
FilePath::ReplaceSpaces | FilePath::KeepCase);
QString relativePath =
QString("output/%1/%2_Board.svg").arg(projectVersion, projectName);
FilePath defaultFilePath = mProject.getPath().getPathTo(relativePath);
QDir().mkpath(defaultFilePath.getParentDir().toStr());
QString filename = FileDialog::getSaveFileName(
this, tr("SVG Export"), defaultFilePath.toNative(), "*.svg");
if (filename.isEmpty()) return;
if (!filename.endsWith(".svg")) filename.append(".svg");
FilePath filepath(filename);
// Create output directory first because QSvgGenerator might not create it.
FileUtils::makePath(filepath.getParentDir()); // can throw
// Export
int dpi = 254;
QRectF rectPx = board->getGraphicsScene().itemsBoundingRect();
QRectF rectSvg(Length::fromPx(rectPx.left()).toInch() * dpi,
Length::fromPx(rectPx.top()).toInch() * dpi,
Length::fromPx(rectPx.width()).toInch() * dpi,
Length::fromPx(rectPx.height()).toInch() * dpi);
rectSvg.moveTo(0, 0); // seems to be required for the SVG viewbox
QSvgGenerator generator;
generator.setTitle(filepath.getFilename());
generator.setDescription(*mProject.getMetadata().getName());
generator.setFileName(filepath.toStr());
generator.setSize(rectSvg.toAlignedRect().size());
generator.setViewBox(rectSvg);
generator.setResolution(dpi);
QPainter painter(&generator);
board->renderToQPainter(painter, dpi);
} catch (Exception& e) {
QMessageBox::warning(this, tr("Error"), e.getMsg());
}
}
void BoardEditor::on_actionGenerateFabricationData_triggered() {
Board* board = getActiveBoard();
if (!board) return;
FabricationOutputDialog dialog(*board, this);
dialog.exec();
}
void BoardEditor::on_actionGenerateBom_triggered() {
BomGeneratorDialog dialog(mProject, getActiveBoard(), this);
dialog.exec();
}
void BoardEditor::on_actionGeneratePickPlace_triggered() {
Board* board = getActiveBoard();
if (!board) return;
BoardPickPlaceGeneratorDialog dialog(*board);
dialog.exec();
}
void BoardEditor::on_actionProjectProperties_triggered() {
ProjectPropertiesEditorDialog dialog(mProject.getMetadata(),
mProjectEditor.getUndoStack(), this);
dialog.exec();
}
void BoardEditor::on_actionUpdateLibrary_triggered() {
// ugly hack until we have a *real* project library updater...
emit mProjectEditor.openProjectLibraryUpdaterClicked(mProject.getFilepath());
}
void BoardEditor::on_actionLayerStackSetup_triggered() {
Board* board = getActiveBoard();
if (!board) return;
try {
BoardLayerStackSetupDialog dialog(board->getLayerStack(),
mProjectEditor.getUndoStack(), this);
dialog.exec();
} catch (Exception& e) {
QMessageBox::warning(this, tr("Error"), e.getMsg());
}
}
void BoardEditor::on_actionModifyDesignRules_triggered() {
Board* board = getActiveBoard();
if (!board) return;
try {
BoardDesignRules originalRules = board->getDesignRules();
BoardDesignRulesDialog dialog(board->getDesignRules(),
mProjectEditor.getDefaultLengthUnit(),
"board_editor/design_rules_dialog", this);
connect(&dialog, &BoardDesignRulesDialog::rulesChanged,
[&](const BoardDesignRules& rules) {
board->getDesignRules() = rules;
emit board->attributesChanged();
});
int result = dialog.exec();
board->getDesignRules() = originalRules; // important hack ;)
if (result == QDialog::Accepted) {
CmdBoardDesignRulesModify* cmd =
new CmdBoardDesignRulesModify(*board, dialog.getDesignRules());
mProjectEditor.getUndoStack().execCmd(cmd);
}
} catch (Exception& e) {
QMessageBox::warning(this, tr("Error"), e.getMsg());
}
}
void BoardEditor::on_actionDesignRuleCheck_triggered() {
Board* board = getActiveBoard();
if (!board) return;
BoardDesignRuleCheckDialog dialog(*board, mDrcOptions,
mProjectEditor.getDefaultLengthUnit(),
"board_editor/drc_dialog", this);
dialog.exec();
mDrcOptions = dialog.getOptions();
if (dialog.getMessages()) {
clearDrcMarker();
mDrcMessages.insert(board->getUuid(), *dialog.getMessages());
mDrcMessagesDock->setMessages(*dialog.getMessages());
if (dialog.getMessages()->count() > 0) {
mDrcMessagesDock->show();
mDrcMessagesDock->raise();
}
}
}
void BoardEditor::on_actionRebuildPlanes_triggered() {
Board* board = getActiveBoard();
if (board) {
board->rebuildAllPlanes();
board->forceAirWiresRebuild();
}
}
void BoardEditor::on_actionShowAllPlanes_triggered() {
if (Board* board = getActiveBoard()) {
foreach (BI_Plane* plane, board->getPlanes()) {
plane->setVisible(true); // No undo command needed since it is not saved
}
}
}
void BoardEditor::on_actionHideAllPlanes_triggered() {
if (Board* board = getActiveBoard()) {
foreach (BI_Plane* plane, board->getPlanes()) {
plane->setVisible(false); // No undo command needed since it is not saved
}
}
}
void BoardEditor::on_tabBar_currentChanged(int index) {
setActiveBoardIndex(index);
}
void BoardEditor::on_lblUnplacedComponentsNote_linkActivated() {
mUnplacedComponentsDock->show();
mUnplacedComponentsDock->raise();
}
void BoardEditor::boardListActionGroupTriggered(QAction* action) {
setActiveBoardIndex(mBoardListActions.indexOf(action));
}
/*******************************************************************************
* Private Methods
******************************************************************************/
bool BoardEditor::graphicsViewEventHandler(QEvent* event) {
// clear DRC location on click
if (event->type() == QEvent::GraphicsSceneMousePress) {
clearDrcMarker();
}
BEE_RedirectedQEvent* e =
new BEE_RedirectedQEvent(BEE_Base::GraphicsViewEvent, event);
return mFsm->processEvent(e, true);
}
void BoardEditor::toolActionGroupChangeTriggered(
const QVariant& newTool) noexcept {
switch (newTool.toInt()) {
case BES_FSM::State::State_Select:
mFsm->processEvent(new BEE_Base(BEE_Base::StartSelect), true);
break;
case BES_FSM::State::State_DrawTrace:
mFsm->processEvent(new BEE_Base(BEE_Base::StartDrawTrace), true);
break;
case BES_FSM::State::State_DrawPolygon:
mFsm->processEvent(new BEE_Base(BEE_Base::StartDrawPolygon), true);
break;
case BES_FSM::State::State_DrawPlane:
mFsm->processEvent(new BEE_Base(BEE_Base::StartDrawPlane), true);
break;
case BES_FSM::State::State_AddVia:
mFsm->processEvent(new BEE_Base(BEE_Base::StartAddVia), true);
break;
case BES_FSM::State::State_AddStrokeText:
mFsm->processEvent(new BEE_Base(BEE_Base::StartAddStrokeText), true);
break;
case BES_FSM::State::State_AddHole:
mFsm->processEvent(new BEE_Base(BEE_Base::StartAddHole), true);
break;
default:
Q_ASSERT(false);
qCritical() << "Unknown tool triggered!";
break;
}
}
void BoardEditor::unplacedComponentsCountChanged(int count) noexcept {
mUi->lblUnplacedComponentsNote->setVisible(count > 0);
}
void BoardEditor::highlightDrcMessage(const BoardDesignRuleCheckMessage& msg,
bool zoomTo) noexcept {
if (QGraphicsScene* scene = mGraphicsView->scene()) {
QPainterPath path = Path::toQPainterPathPx(msg.getLocations(), true);
mDrcLocationGraphicsItem.reset(new QGraphicsPathItem());
mDrcLocationGraphicsItem->setZValue(Board::ZValue_AirWires);
mDrcLocationGraphicsItem->setPen(Qt::NoPen);
mDrcLocationGraphicsItem->setBrush(QColor::fromRgb(255, 127, 0));
mDrcLocationGraphicsItem->setPath(path);
scene->addItem(mDrcLocationGraphicsItem.data());
qreal margin = Length(1000000).toPx();
QRectF rect = path.boundingRect();
rect.adjust(-margin, -margin, margin, margin);
mGraphicsView->setSceneRectMarker(rect);
if (zoomTo) {
mGraphicsView->zoomToRect(rect);
}
}
}
void BoardEditor::clearDrcMarker() noexcept {
mDrcLocationGraphicsItem.reset();
mGraphicsView->setSceneRectMarker(QRectF());
}
QStringList BoardEditor::getSearchToolBarCompleterList() noexcept {
QStringList list;
if (Board* board = getActiveBoard()) {
foreach (const BI_Device* device, board->getDeviceInstances()) {
list.append(*device->getComponentInstance().getName());
}
}
return list;
}
void BoardEditor::goToDevice(const QString& name) noexcept {
if (Board* board = getActiveBoard()) {
foreach (BI_Device* device, board->getDeviceInstances()) {
if (device->getComponentInstance().getName()->toLower() ==
name.toLower()) {
board->clearSelection();
device->setSelected(true);
QRectF rect = device->getFootprint().getBoundingRect();
qreal margin = Length(1000000).toPx();
rect.adjust(-margin, -margin, margin, margin);
mGraphicsView->zoomToRect(rect);
return;
}
}
}
}
/*******************************************************************************
* End of File
******************************************************************************/
} // namespace editor
} // namespace project
} // namespace librepcb
65187b73eThis file is pinned to the selected release. Download the original to open it in your design tool.