Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Board editor: Fix empty view after removing a board #431

Merged
merged 1 commit into from Mar 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
90 changes: 35 additions & 55 deletions libs/librepcb/projecteditor/boardeditor/boardeditor.cpp
Expand Up @@ -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),
Expand Down Expand Up @@ -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);
}
}

/*******************************************************************************
Expand Down Expand Up @@ -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);
Expand All @@ -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
}

/*******************************************************************************
Expand Down
13 changes: 4 additions & 9 deletions libs/librepcb/projecteditor/boardeditor/boardeditor.h
Expand Up @@ -25,6 +25,7 @@
******************************************************************************/
#include <librepcb/common/graphics/if_graphicsvieweventhandler.h>
#include <librepcb/common/uuid.h>
#include <librepcb/project/boards/board.h>

#include <QtCore>
#include <QtWidgets>
Expand All @@ -42,7 +43,6 @@ class ExclusiveActionGroup;
namespace project {

class Project;
class Board;
class ComponentInstance;

namespace editor {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -136,7 +131,7 @@ private slots:
QScopedPointer<ExclusiveActionGroup> mToolsActionGroup;

// Misc
int mActiveBoardIndex;
QPointer<Board> mActiveBoard;
QList<QAction*> mBoardListActions;
QActionGroup mBoardListActionGroup;

Expand Down