From e74896ea7e784f92964ccaba97e663f8deed643d Mon Sep 17 00:00:00 2001 From: "U. Bruhin" Date: Sat, 16 Mar 2019 22:26:35 +0100 Subject: [PATCH] Board editor: Fix empty view after removing a board --- .../projecteditor/boardeditor/boardeditor.cpp | 90 ++++++++----------- .../projecteditor/boardeditor/boardeditor.h | 13 +-- 2 files changed, 39 insertions(+), 64 deletions(-) diff --git a/libs/librepcb/projecteditor/boardeditor/boardeditor.cpp b/libs/librepcb/projecteditor/boardeditor/boardeditor.cpp index 7470d85c16..8cf156db6f 100644 --- a/libs/librepcb/projecteditor/boardeditor/boardeditor.cpp +++ b/libs/librepcb/projecteditor/boardeditor/boardeditor.cpp @@ -74,7 +74,7 @@ BoardEditor::BoardEditor(ProjectEditor& projectEditor, Project& project) mProject(project), mUi(new Ui::BoardEditor), mGraphicsView(nullptr), - mActiveBoardIndex(-1), + mActiveBoard(nullptr), mBoardListActionGroup(this), mErcMsgDock(nullptr), mUnplacedComponentsDock(nullptr), @@ -274,60 +274,46 @@ BoardEditor::~BoardEditor() { mUi = nullptr; } -/******************************************************************************* - * Getters - ******************************************************************************/ - -Board* BoardEditor::getActiveBoard() const noexcept { - return mProject.getBoardByIndex(mActiveBoardIndex); -} - /******************************************************************************* * Setters ******************************************************************************/ -bool BoardEditor::setActiveBoardIndex(int index) noexcept { - if (index == mActiveBoardIndex) return true; +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()); + // force airwire rebuild immediately and on every project modification + mActiveBoard->triggerAirWiresRebuild(); + connect(&mProjectEditor.getUndoStack(), &UndoStack::stateModified, + mActiveBoard.data(), &Board::triggerAirWiresRebuild); + } else { + mGraphicsView->setScene(nullptr); + } - Board* board = getActiveBoard(); - if (board) { - // stop airwire rebuild on every project modification (for performance - // reasons) - disconnect(&mProjectEditor.getUndoStack(), &UndoStack::stateModified, board, - &Board::triggerAirWiresRebuild); - // save current view scene rect - board->saveViewSceneRect(mGraphicsView->getVisibleSceneRect()); - // uncheck QAction - QAction* action = mBoardListActions.value(mActiveBoardIndex); - Q_ASSERT(action); - if (action) action->setChecked(false); - } - board = mProject.getBoardByIndex(index); - if (board) { - // show scene, restore view scene rect, set grid properties - board->showInView(*mGraphicsView); - mGraphicsView->setVisibleSceneRect(board->restoreViewSceneRect()); - mGraphicsView->setGridProperties(board->getGridProperties()); - // force airwire rebuild immediately and on every project modification - board->triggerAirWiresRebuild(); - connect(&mProjectEditor.getUndoStack(), &UndoStack::stateModified, board, - &Board::triggerAirWiresRebuild); - // check QAction - QAction* action = mBoardListActions.value(index); - Q_ASSERT(action); - if (action) action->setChecked(true); - } else { - mGraphicsView->setScene(nullptr); + // update dock widgets + mUnplacedComponentsDock->setBoard(mActiveBoard); + mBoardLayersDock->setActiveBoard(mActiveBoard); } - // active board has changed! - int oldIndex = mActiveBoardIndex; - mActiveBoardIndex = index; - mUnplacedComponentsDock->setBoard(board); - mBoardLayersDock->setActiveBoard(board); + // update GUI mUi->tabBar->setCurrentIndex(index); - emit activeBoardChanged(oldIndex, index); - return true; + for (int i = 0; i < mBoardListActions.count(); ++i) { + mBoardListActions.at(i)->setChecked(i == index); + } } /******************************************************************************* @@ -362,8 +348,7 @@ void BoardEditor::boardAdded(int newIndex) { if (!board) return; QAction* actionBefore = mBoardListActions.value(newIndex - 1); - // if (!actionBefore) actionBefore = TODO - QAction* newAction = new QAction(*board->getName(), this); + QAction* newAction = new QAction(*board->getName(), this); newAction->setCheckable(true); mUi->menuBoard->insertAction(actionBefore, newAction); mBoardListActions.insert(newIndex, newAction); @@ -373,17 +358,12 @@ void BoardEditor::boardAdded(int newIndex) { } void BoardEditor::boardRemoved(int oldIndex) { - mUi->tabBar->removeTab(oldIndex); QAction* action = mBoardListActions.takeAt(oldIndex); Q_ASSERT(action); mBoardListActionGroup.removeAction(action); delete action; - if (oldIndex == mActiveBoardIndex) { - setActiveBoardIndex(0); - } else if (oldIndex < mActiveBoardIndex) { - mActiveBoardIndex--; - } + mUi->tabBar->removeTab(oldIndex); // calls setActiveBoardIndex() if needed } /******************************************************************************* diff --git a/libs/librepcb/projecteditor/boardeditor/boardeditor.h b/libs/librepcb/projecteditor/boardeditor/boardeditor.h index e6b5d858a3..545d8f4237 100644 --- a/libs/librepcb/projecteditor/boardeditor/boardeditor.h +++ b/libs/librepcb/projecteditor/boardeditor/boardeditor.h @@ -25,6 +25,7 @@ ******************************************************************************/ #include #include +#include #include #include @@ -42,7 +43,6 @@ class ExclusiveActionGroup; namespace project { class Project; -class Board; class ComponentInstance; namespace editor { @@ -76,11 +76,10 @@ class BoardEditor final : public QMainWindow, // Getters ProjectEditor& getProjectEditor() const noexcept { return mProjectEditor; } Project& getProject() const noexcept { return mProject; } - int getActiveBoardIndex() const noexcept { return mActiveBoardIndex; } - Board* getActiveBoard() const noexcept; + Board* getActiveBoard() const noexcept { return mActiveBoard.data(); } // Setters - bool setActiveBoardIndex(int index) noexcept; + void setActiveBoardIndex(int index) noexcept; // General Methods void abortAllCommands() noexcept; @@ -112,10 +111,6 @@ private slots: void on_lblUnplacedComponentsNote_linkActivated(); void boardListActionGroupTriggered(QAction* action); -signals: - - void activeBoardChanged(int oldIndex, int newIndex); - private: // make some methods inaccessible... BoardEditor() = delete; @@ -136,7 +131,7 @@ private slots: QScopedPointer mToolsActionGroup; // Misc - int mActiveBoardIndex; + QPointer mActiveBoard; QList mBoardListActions; QActionGroup mBoardListActionGroup;