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

Refactor workspace libraries handling #400

Merged
merged 6 commits into from Jan 3, 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
45 changes: 27 additions & 18 deletions apps/librepcb/controlpanel/controlpanel.cpp
Expand Up @@ -81,7 +81,7 @@ ControlPanel::ControlPanel(Workspace& workspace)
mUi->statusBar->setProgressBarTextFormat(tr("Scanning libraries (%p%)"));
connect(&mWorkspace.getLibraryDb(), &WorkspaceLibraryDb::scanStarted,
mUi->statusBar, &StatusBar::showProgressBar, Qt::QueuedConnection);
connect(&mWorkspace.getLibraryDb(), &WorkspaceLibraryDb::scanSucceeded,
connect(&mWorkspace.getLibraryDb(), &WorkspaceLibraryDb::scanFinished,
mUi->statusBar, &StatusBar::hideProgressBar, Qt::QueuedConnection);
connect(&mWorkspace.getLibraryDb(), &WorkspaceLibraryDb::scanProgressUpdate,
mUi->statusBar, &StatusBar::setProgressBarPercent,
Expand All @@ -94,17 +94,14 @@ ControlPanel::ControlPanel(Workspace& workspace)
Workspace::getHighestFileFormatVersionOfWorkspace(workspace.getPath());
mUi->lblWarnForNewerAppVersions->setVisible(highestVersion > actualVersion);

// decide if we have to show the warning about missing workspace libraries
if (mWorkspace.getLocalLibraries().isEmpty() &&
mWorkspace.getRemoteLibraries().isEmpty()) {
mUi->lblWarnForNoLibraries->setVisible(true);
connect(mUi->lblWarnForNoLibraries, &QLabel::linkActivated, this,
&ControlPanel::on_actionOpen_Library_Manager_triggered);
connect(&mWorkspace, &Workspace::libraryAdded, mUi->lblWarnForNoLibraries,
&QLabel::hide);
} else {
mUi->lblWarnForNoLibraries->setVisible(false);
}
// hide warning about missing libraries, but update visibility each time the
// workspace library was scanned
mUi->lblWarnForNoLibraries->setVisible(false);
connect(mUi->lblWarnForNoLibraries, &QLabel::linkActivated, this,
&ControlPanel::on_actionOpen_Library_Manager_triggered);
connect(&mWorkspace.getLibraryDb(),
&WorkspaceLibraryDb::scanLibraryListUpdated, this,
&ControlPanel::updateNoLibrariesWarningVisibility);

// connect some actions which are created with the Qt Designer
connect(mUi->actionQuit, &QAction::triggered, this, &ControlPanel::close);
Expand Down Expand Up @@ -248,6 +245,16 @@ void ControlPanel::loadSettings() {
clientSettings.endGroup();
}

void ControlPanel::updateNoLibrariesWarningVisibility() noexcept {
bool showWarning = false;
try {
showWarning = mWorkspace.getLibraryDb().getLibraries().isEmpty();
} catch (const Exception& e) {
qCritical() << "Could not get library list:" << e.getMsg();
}
mUi->lblWarnForNoLibraries->setVisible(showWarning);
}

void ControlPanel::showProjectReadmeInBrowser(
const FilePath& projectFilePath) noexcept {
if (projectFilePath.isValid()) {
Expand Down Expand Up @@ -371,16 +378,18 @@ ProjectEditor* ControlPanel::getOpenProject(const FilePath& filepath) const
* Library Management
******************************************************************************/

void ControlPanel::openLibraryEditor(
QSharedPointer<library::Library> lib) noexcept {
void ControlPanel::openLibraryEditor(const FilePath& libDir) noexcept {
using library::Library;
using library::editor::LibraryEditor;
LibraryEditor* editor = mOpenLibraryEditors.value(lib.data());
LibraryEditor* editor = mOpenLibraryEditors.value(libDir);
if (!editor) {
try {
bool remote = libDir.isLocatedInDir(mWorkspace.getRemoteLibrariesPath());
QSharedPointer<Library> lib(new Library(libDir, remote));
editor = new LibraryEditor(mWorkspace, lib);
connect(editor, &LibraryEditor::destroyed, this,
&ControlPanel::libraryEditorDestroyed);
mOpenLibraryEditors.insert(lib.data(), editor);
mOpenLibraryEditors.insert(libDir, editor);
} catch (const Exception& e) {
QMessageBox::critical(this, tr("Error"), e.getMsg());
}
Expand All @@ -398,8 +407,8 @@ void ControlPanel::libraryEditorDestroyed() noexcept {
// static_cast is used instead ;)
LibraryEditor* editor = static_cast<LibraryEditor*>(QObject::sender());
Q_ASSERT(editor);
library::Library* library = mOpenLibraryEditors.key(editor);
Q_ASSERT(library);
FilePath library = mOpenLibraryEditors.key(editor);
Q_ASSERT(library.isValid());
mOpenLibraryEditors.remove(library);
}

Expand Down
15 changes: 8 additions & 7 deletions apps/librepcb/controlpanel/controlpanel.h
Expand Up @@ -130,6 +130,7 @@ private slots:
// General private methods
void saveSettings();
void loadSettings();
void updateNoLibrariesWarningVisibility() noexcept;
void showProjectReadmeInBrowser(const FilePath& projectFilePath) noexcept;

// Project Management
Expand Down Expand Up @@ -206,7 +207,7 @@ private slots:
noexcept;

// Library Management
void openLibraryEditor(QSharedPointer<library::Library> lib) noexcept;
void openLibraryEditor(const FilePath& libDir) noexcept;
void libraryEditorDestroyed() noexcept;

/**
Expand All @@ -220,12 +221,12 @@ private slots:
bool closeAllLibraryEditors(bool askForSave) noexcept;

// Attributes
workspace::Workspace& mWorkspace;
QScopedPointer<Ui::ControlPanel> mUi;
QScopedPointer<library::manager::LibraryManager> mLibraryManager;
QHash<QString, project::editor::ProjectEditor*> mOpenProjectEditors;
QHash<library::Library*, library::editor::LibraryEditor*> mOpenLibraryEditors;
QScopedPointer<ProjectLibraryUpdater> mProjectLibraryUpdater;
workspace::Workspace& mWorkspace;
QScopedPointer<Ui::ControlPanel> mUi;
QScopedPointer<library::manager::LibraryManager> mLibraryManager;
QHash<QString, project::editor::ProjectEditor*> mOpenProjectEditors;
QHash<FilePath, library::editor::LibraryEditor*> mOpenLibraryEditors;
QScopedPointer<ProjectLibraryUpdater> mProjectLibraryUpdater;
};

/*******************************************************************************
Expand Down
45 changes: 21 additions & 24 deletions libs/librepcb/libraryeditor/lib/librarylisteditorwidget.cpp
Expand Up @@ -25,6 +25,7 @@
#include "ui_librarylisteditorwidget.h"

#include <librepcb/library/library.h>
#include <librepcb/workspace/library/workspacelibrarydb.h>
#include <librepcb/workspace/settings/workspacesettings.h>
#include <librepcb/workspace/workspace.h>

Expand All @@ -46,21 +47,30 @@ LibraryListEditorWidget::LibraryListEditorWidget(const workspace::Workspace& ws,
QWidget* parent) noexcept
: QWidget(parent), mWorkspace(ws), mUi(new Ui::LibraryListEditorWidget) {
mUi->setupUi(this);
mUi->comboBox->addItem(tr("Choose library..."));
connect(mUi->btnAdd, &QPushButton::clicked, this,
&LibraryListEditorWidget::btnAddClicked);
connect(mUi->btnRemove, &QPushButton::clicked, this,
&LibraryListEditorWidget::btnRemoveClicked);

const QStringList& localeOrder =
mWorkspace.getSettings().getLibLocaleOrder().getLocaleOrder();
QList<QSharedPointer<library::Library>> libs;
libs.append(mWorkspace.getLocalLibraries().values());
libs.append(mWorkspace.getRemoteLibraries().values());
mUi->comboBox->addItem(tr("Choose library..."));
foreach (const QSharedPointer<library::Library>& lib, libs) {
mUi->comboBox->addItem(lib->getIconAsPixmap(),
*lib->getNames().value(localeOrder),
lib->getUuid().toStr());
try {
QMultiMap<Version, FilePath> libs =
mWorkspace.getLibraryDb().getLibraries(); // can throw
foreach (const FilePath& fp, libs) {
Uuid uuid = Uuid::createRandom();
mWorkspace.getLibraryDb().getElementMetadata<Library>(
fp, &uuid); // can throw
QString name;
mWorkspace.getLibraryDb().getElementTranslations<Library>(
fp, mWorkspace.getSettings().getLibLocaleOrder().getLocaleOrder(),
&name); // can throw
QPixmap icon;
mWorkspace.getLibraryDb().getLibraryMetadata(fp, &icon); // can throw
mUi->comboBox->addItem(icon, name, uuid.toStr());
mLibNames[uuid] = name;
}
} catch (const Exception& e) {
qCritical() << "Could not load library list.";
}
}

Expand Down Expand Up @@ -116,20 +126,7 @@ void LibraryListEditorWidget::btnRemoveClicked() noexcept {
}

void LibraryListEditorWidget::addItem(const Uuid& library) noexcept {
QString name = library.toStr();

const QStringList& localeOrder =
mWorkspace.getSettings().getLibLocaleOrder().getLocaleOrder();
QList<QSharedPointer<library::Library>> libs;
libs.append(mWorkspace.getLocalLibraries().values());
libs.append(mWorkspace.getRemoteLibraries().values());
foreach (const QSharedPointer<library::Library>& lib, libs) {
if (lib->getUuid() == library) {
name = *lib->getNames().value(localeOrder);
break;
}
}

QString name = mLibNames.value(library, library.toStr());
QListWidgetItem* item = new QListWidgetItem(name, mUi->listWidget);
item->setData(Qt::UserRole, library.toStr());
}
Expand Down
1 change: 1 addition & 0 deletions libs/librepcb/libraryeditor/lib/librarylisteditorwidget.h
Expand Up @@ -89,6 +89,7 @@ class LibraryListEditorWidget final : public QWidget {
const workspace::Workspace& mWorkspace;
QScopedPointer<Ui::LibraryListEditorWidget> mUi;
QSet<Uuid> mUuids;
QHash<Uuid, QString> mLibNames;
};

/*******************************************************************************
Expand Down
2 changes: 1 addition & 1 deletion libs/librepcb/libraryeditor/lib/libraryoverviewwidget.cpp
Expand Up @@ -123,7 +123,7 @@ LibraryOverviewWidget::LibraryOverviewWidget(const Context& context,
// Load all library elements.
updateElementLists();
connect(&mContext.workspace.getLibraryDb(),
&workspace::WorkspaceLibraryDb::scanSucceeded, this,
&workspace::WorkspaceLibraryDb::scanFinished, this,
&LibraryOverviewWidget::updateElementLists);
}

Expand Down
2 changes: 1 addition & 1 deletion libs/librepcb/libraryeditor/libraryeditor.cpp
Expand Up @@ -123,7 +123,7 @@ LibraryEditor::LibraryEditor(workspace::Workspace& ws,
&workspace::WorkspaceLibraryDb::scanStarted, mUi->statusBar,
&StatusBar::showProgressBar, Qt::QueuedConnection);
connect(&mWorkspace.getLibraryDb(),
&workspace::WorkspaceLibraryDb::scanSucceeded, mUi->statusBar,
&workspace::WorkspaceLibraryDb::scanFinished, mUi->statusBar,
&StatusBar::hideProgressBar, Qt::QueuedConnection);
connect(&mWorkspace.getLibraryDb(),
&workspace::WorkspaceLibraryDb::scanProgressUpdate, mUi->statusBar,
Expand Down
30 changes: 3 additions & 27 deletions libs/librepcb/librarymanager/addlibrarywidget.cpp
Expand Up @@ -107,16 +107,6 @@ void AddLibraryWidget::updateRepositoryLibraryList() noexcept {
}
}

void AddLibraryWidget::updateInstalledStatusOfRepositoryLibraries() noexcept {
for (int i = 0; i < mUi->lstRepoLibs->count(); i++) {
QListWidgetItem* item = mUi->lstRepoLibs->item(i);
Q_ASSERT(item);
auto* widget = dynamic_cast<RepositoryLibraryListWidgetItem*>(
mUi->lstRepoLibs->itemWidget(item));
if (widget) widget->updateInstalledStatus();
}
}

/*******************************************************************************
* Private Methods
******************************************************************************/
Expand Down Expand Up @@ -266,9 +256,6 @@ void AddLibraryWidget::createLocalLibraryButtonClicked() noexcept {
qCritical() << "Could not copy the .gitattributes file:" << e.getMsg();
}

// add the new library to the workspace
mWorkspace.addLocalLibrary(directory.getFilename()); // can throw

// library successfully added! reset input fields and emit signal
mUi->edtLocalName->clear();
mUi->edtLocalDescription->clear();
Expand All @@ -277,7 +264,7 @@ void AddLibraryWidget::createLocalLibraryButtonClicked() noexcept {
mUi->edtLocalUrl->clear();
mUi->cbxLocalCc0License->setChecked(false);
mUi->edtLocalDirectory->clear();
emit libraryAdded(directory, true);
emit libraryAdded(directory);
} catch (Exception& e) {
QMessageBox::critical(this, tr("Error"), e.getMsg());
}
Expand Down Expand Up @@ -344,17 +331,8 @@ void AddLibraryWidget::downloadZipFinished(bool success,
Q_ASSERT(mManualLibraryDownload);

if (success) {
try {
// add library to workspace
mWorkspace.addLocalLibrary(
mManualLibraryDownload->getDestinationDir().getFilename());

// finish
mUi->lblDownloadZipStatusMsg->setText("");
emit libraryAdded(mManualLibraryDownload->getDestinationDir(), true);
} catch (const Exception& e) {
mUi->lblDownloadZipStatusMsg->setText(e.getMsg());
}
mUi->lblDownloadZipStatusMsg->setText("");
emit libraryAdded(mManualLibraryDownload->getDestinationDir());
} else {
mUi->lblDownloadZipStatusMsg->setText(errMsg);
}
Expand All @@ -376,8 +354,6 @@ void AddLibraryWidget::repositoryLibraryListReceived(
new RepositoryLibraryListWidgetItem(mWorkspace, libVal.toObject());
connect(widget, &RepositoryLibraryListWidgetItem::checkedChanged, this,
&AddLibraryWidget::repoLibraryDownloadCheckedChanged);
connect(widget, &RepositoryLibraryListWidgetItem::libraryAdded, this,
&AddLibraryWidget::libraryAdded);
QListWidgetItem* item = new QListWidgetItem(mUi->lstRepoLibs);
item->setSizeHint(widget->sizeHint());
mUi->lstRepoLibs->setItemWidget(item, widget);
Expand Down
4 changes: 1 addition & 3 deletions libs/librepcb/librarymanager/addlibrarywidget.h
Expand Up @@ -69,14 +69,12 @@ class AddLibraryWidget final : public QWidget {

// General Methods
void updateRepositoryLibraryList() noexcept;
void updateInstalledStatusOfRepositoryLibraries() noexcept;

// Operator Overloadings
AddLibraryWidget& operator=(const AddLibraryWidget& rhs) = delete;

signals:

void libraryAdded(const FilePath& libDir, bool select);
void libraryAdded(const FilePath& libDir);

private: // Methods
void localLibraryNameLineEditTextChanged(QString name) noexcept;
Expand Down