Skip to content

Commit

Permalink
Refactor workspace settings software architecture
Browse files Browse the repository at this point in the history
- Decoupling data from GUI
- Reducing lines of code
- General code cleanup
  • Loading branch information
ubruhin committed Jan 2, 2020
1 parent ca59407 commit 3695664
Show file tree
Hide file tree
Showing 54 changed files with 957 additions and 2,482 deletions.
12 changes: 8 additions & 4 deletions apps/librepcb/controlpanel/controlpanel.cpp
Expand Up @@ -42,6 +42,7 @@
#include <librepcb/workspace/projecttreemodel.h>
#include <librepcb/workspace/recentprojectsmodel.h>
#include <librepcb/workspace/settings/workspacesettings.h>
#include <librepcb/workspace/settings/workspacesettingsdialog.h>
#include <librepcb/workspace/workspace.h>

#include <QtCore>
Expand Down Expand Up @@ -110,8 +111,6 @@ ControlPanel::ControlPanel(Workspace& workspace)
connect(mUi->actionAbout_Qt, &QAction::triggered, qApp,
&QApplication::aboutQt);
connect(mUi->actionAbout, &QAction::triggered, qApp, &Application::about);
connect(mUi->actionWorkspace_Settings, &QAction::triggered,
&mWorkspace.getSettings(), &WorkspaceSettings::showSettingsDialog);
connect(mLibraryManager.data(), &LibraryManager::openLibraryEditorTriggered,
this, &ControlPanel::openLibraryEditor);

Expand Down Expand Up @@ -451,8 +450,8 @@ void ControlPanel::projectEditorClosed() noexcept {
if (!editor) return;

Project* project = &editor->getProject();
Q_ASSERT(mOpenProjectEditors.contains(
project->getFilepath().toUnique().toStr()));
Q_ASSERT(
mOpenProjectEditors.contains(project->getFilepath().toUnique().toStr()));
mOpenProjectEditors.remove(project->getFilepath().toUnique().toStr());
delete project;
}
Expand Down Expand Up @@ -504,6 +503,11 @@ void ControlPanel::on_actionSwitch_Workspace_triggered() {
"restarting the application."));
}

void ControlPanel::on_actionWorkspace_Settings_triggered() {
WorkspaceSettingsDialog dialog(mWorkspace.getSettings(), this);
dialog.exec();
}

void ControlPanel::on_projectTreeView_clicked(const QModelIndex& index) {
FilePath fp(mWorkspace.getProjectTreeModel().filePath(index));
if ((fp.getSuffix() == "lpp") || (fp.getFilename() == "README.md")) {
Expand Down
1 change: 1 addition & 0 deletions apps/librepcb/controlpanel/controlpanel.h
Expand Up @@ -106,6 +106,7 @@ private slots:
void on_actionOpen_Library_Manager_triggered();
void on_actionClose_all_open_projects_triggered();
void on_actionSwitch_Workspace_triggered();
void on_actionWorkspace_Settings_triggered();
void on_projectTreeView_clicked(const QModelIndex& index);
void on_projectTreeView_doubleClicked(const QModelIndex& index);
void on_projectTreeView_customContextMenuRequested(const QPoint& pos);
Expand Down
8 changes: 4 additions & 4 deletions apps/librepcb/main.cpp
Expand Up @@ -212,8 +212,8 @@ static FilePath determineWorkspacePath() noexcept {

// open workspace and apply settings
Workspace ws(wsPath);
ws.getSettings().getUser().setName(wizard.getNewWorkspaceUserName());
ws.getSettings().applyAll(); // can throw
ws.getSettings().userName.set(wizard.getNewWorkspaceUserName());
ws.getSettings().saveToFile(); // can throw
} catch (const Exception& e) {
QMessageBox::critical(0, Application::translate("Workspace", "Error"),
e.getMsg());
Expand All @@ -239,8 +239,8 @@ static int openWorkspace(const FilePath& path) noexcept {

// Now since workspace settings are loaded, switch to the locale defined
// there (until now, the system locale was used).
if (!ws.getSettings().getAppLocale().getAppLocaleName().isEmpty()) {
QLocale locale(ws.getSettings().getAppLocale().getAppLocaleName());
if (!ws.getSettings().applicationLocale.get().isEmpty()) {
QLocale locale(ws.getSettings().applicationLocale.get());
QLocale::setDefault(locale);
qApp->setTranslationLocale(locale);
}
Expand Down
11 changes: 11 additions & 0 deletions libs/librepcb/common/application.cpp
Expand Up @@ -182,6 +182,17 @@ FilePath Application::getResourcesFilePath(const QString& filepath) const
return mResourcesDir.getPathTo(filepath);
}

QStringList Application::getAvailableTranslationLocales() const noexcept {
QStringList locales;
QDir dir(getResourcesFilePath("i18n").toStr());
foreach (QString filename, dir.entryList({"*.qm"}, QDir::Files, QDir::Name)) {
filename.remove("librepcb_");
filename.remove(".qm");
locales.append(filename);
}
return locales;
}

const StrokeFont& Application::getDefaultStrokeFont() const noexcept {
try {
return mStrokeFontPool->getFont(getDefaultStrokeFontName());
Expand Down
1 change: 1 addition & 0 deletions libs/librepcb/common/application.h
Expand Up @@ -79,6 +79,7 @@ class Application final : public QApplication {
bool isFileFormatStable() const noexcept { return mIsFileFormatStable; }
const FilePath& getResourcesDir() const noexcept { return mResourcesDir; }
FilePath getResourcesFilePath(const QString& filepath) const noexcept;
QStringList getAvailableTranslationLocales() const noexcept;
const QFont& getDefaultSansSerifFont() const noexcept {
return mSansSerifFont;
}
Expand Down
3 changes: 1 addition & 2 deletions libs/librepcb/common/dialogs/gridsettingsdialog.cpp
Expand Up @@ -163,8 +163,7 @@ void GridSettingsDialog::buttonBoxClicked(QAbstractButton* button) {
******************************************************************************/

void GridSettingsDialog::updateInternalRepresentation() noexcept {
QLocale locale; // this loads the application's default locale (defined in
// WSI_AppLocale)
QLocale locale; // this loads the application's default locale
mUi->lblIntervalNm->setText(QString("%1 nm").arg(
locale.toString(mCurrentGrid.getInterval()->toNm())));
}
Expand Down
Expand Up @@ -50,7 +50,7 @@ CategoryChooserDialog<ElementType>::CategoryChooserDialog(
&CategoryChooserDialog<ElementType>::accept);

mModel.reset(new workspace::CategoryTreeModel<ElementType>(
ws.getLibraryDb(), ws.getSettings().getLibLocaleOrder().getLocaleOrder(),
ws.getLibraryDb(), ws.getSettings().libraryLocaleOrder.get(),
workspace::CategoryTreeFilter::ALL));
mUi->treeView->setModel(mModel.data());
mUi->treeView->setRootIndex(QModelIndex());
Expand Down
Expand Up @@ -207,7 +207,7 @@ QString CategoryListEditorWidget<ElementType>::getCategoryName(
const FilePath& fp) const {
QString name;
mWorkspace.getLibraryDb().template getElementTranslations<ElementType>(
fp, mWorkspace.getSettings().getLibLocaleOrder().getLocaleOrder(),
fp, mWorkspace.getSettings().libraryLocaleOrder.get(),
&name); // can throw
return name;
}
Expand Down
Expand Up @@ -254,7 +254,7 @@ void ComponentChooserDialog::accept() noexcept {
}

const QStringList& ComponentChooserDialog::localeOrder() const noexcept {
return mWorkspace.getSettings().getLibLocaleOrder().getLocaleOrder();
return mWorkspace.getSettings().libraryLocaleOrder.get();
}

/*******************************************************************************
Expand Down
4 changes: 2 additions & 2 deletions libs/librepcb/libraryeditor/common/editorwidgetbase.cpp
Expand Up @@ -178,11 +178,11 @@ void EditorWidgetBase::undoStackStateModified() noexcept {
}

const QStringList& EditorWidgetBase::getLibLocaleOrder() const noexcept {
return mContext.workspace.getSettings().getLibLocaleOrder().getLocaleOrder();
return mContext.workspace.getSettings().libraryLocaleOrder.get();
}

QString EditorWidgetBase::getWorkspaceSettingsUserName() noexcept {
QString u = mContext.workspace.getSettings().getUser().getName().trimmed();
QString u = mContext.workspace.getSettings().userName.get();
if (u.isEmpty()) {
QMessageBox::warning(
this, tr("User name not set"),
Expand Down
Expand Up @@ -234,7 +234,7 @@ void PackageChooserDialog::accept() noexcept {
}

const QStringList& PackageChooserDialog::localeOrder() const noexcept {
return mWorkspace.getSettings().getLibLocaleOrder().getLocaleOrder();
return mWorkspace.getSettings().libraryLocaleOrder.get();
}

/*******************************************************************************
Expand Down
2 changes: 1 addition & 1 deletion libs/librepcb/libraryeditor/common/symbolchooserdialog.cpp
Expand Up @@ -233,7 +233,7 @@ void SymbolChooserDialog::accept() noexcept {
}

const QStringList& SymbolChooserDialog::localeOrder() const noexcept {
return mWorkspace.getSettings().getLibLocaleOrder().getLocaleOrder();
return mWorkspace.getSettings().libraryLocaleOrder.get();
}

/*******************************************************************************
Expand Down
2 changes: 1 addition & 1 deletion libs/librepcb/libraryeditor/libraryeditor.cpp
Expand Up @@ -129,7 +129,7 @@ LibraryEditor::LibraryEditor(workspace::Workspace& ws, const FilePath& libFp,

// set window title and icon
const QStringList localeOrder =
mWorkspace.getSettings().getLibLocaleOrder().getLocaleOrder();
mWorkspace.getSettings().libraryLocaleOrder.get();
QString libName = *mLibrary->getNames().value(localeOrder);
if (readOnly) libName.append(tr(" [Read-Only]"));
setWindowTitle(QString(tr("%1 - LibrePCB Library Editor")).arg(libName));
Expand Down
Expand Up @@ -58,7 +58,7 @@ NewElementWizardContext::~NewElementWizardContext() noexcept {
******************************************************************************/

const QStringList& NewElementWizardContext::getLibLocaleOrder() const noexcept {
return mWorkspace.getSettings().getLibLocaleOrder().getLocaleOrder();
return mWorkspace.getSettings().libraryLocaleOrder.get();
}

/*******************************************************************************
Expand All @@ -71,7 +71,7 @@ void NewElementWizardContext::reset(ElementType newType) noexcept {
mElementName = tl::nullopt;
mElementDescription.clear();
mElementKeywords.clear();
mElementAuthor = mWorkspace.getSettings().getUser().getName();
mElementAuthor = mWorkspace.getSettings().userName.get();
mElementVersion = Version::fromString("0.1");
mElementCategoryUuid = tl::nullopt;

Expand Down
Expand Up @@ -169,10 +169,7 @@ void NewElementWizardPage_EnterMetadata::updateCategoryTreeLabel() noexcept {
case NewElementWizardContext::ElementType::Device: {
ComponentCategoryTreeLabelTextBuilder builder(
mContext.getWorkspace().getLibraryDb(),
mContext.getWorkspace()
.getSettings()
.getLibLocaleOrder()
.getLocaleOrder(),
mContext.getWorkspace().getSettings().libraryLocaleOrder.get(),
*mUi->lblCategoryTree);
builder.setHighlightLastLine(true);
builder.setOneLine(true);
Expand All @@ -183,10 +180,7 @@ void NewElementWizardPage_EnterMetadata::updateCategoryTreeLabel() noexcept {
case NewElementWizardContext::ElementType::Package: {
PackageCategoryTreeLabelTextBuilder builder(
mContext.getWorkspace().getLibraryDb(),
mContext.getWorkspace()
.getSettings()
.getLibLocaleOrder()
.getLocaleOrder(),
mContext.getWorkspace().getSettings().libraryLocaleOrder.get(),
*mUi->lblCategoryTree);
builder.setHighlightLastLine(true);
builder.setOneLine(true);
Expand Down
2 changes: 1 addition & 1 deletion libs/librepcb/libraryeditor/pkg/packageeditorwidget.cpp
Expand Up @@ -65,7 +65,7 @@ PackageEditorWidget::PackageEditorWidget(const Context& context,
mUi->lstMessages->setHandler(this);
setupErrorNotificationWidget(*mUi->errorNotificationWidget);
mUi->graphicsView->setUseOpenGl(
mContext.workspace.getSettings().getAppearance().getUseOpenGl());
mContext.workspace.getSettings().useOpenGl.get());
mUi->graphicsView->setScene(mGraphicsScene.data());
mUi->graphicsView->setBackgroundBrush(Qt::black);
mUi->graphicsView->setForegroundBrush(Qt::white);
Expand Down
2 changes: 1 addition & 1 deletion libs/librepcb/libraryeditor/sym/symboleditorwidget.cpp
Expand Up @@ -70,7 +70,7 @@ SymbolEditorWidget::SymbolEditorWidget(const Context& context,
mUi->lstMessages->setHandler(this);
setupErrorNotificationWidget(*mUi->errorNotificationWidget);
mUi->graphicsView->setUseOpenGl(
mContext.workspace.getSettings().getAppearance().getUseOpenGl());
mContext.workspace.getSettings().useOpenGl.get());
mUi->graphicsView->setScene(mGraphicsScene.data());
connect(mUi->graphicsView, &GraphicsView::cursorScenePositionChanged, this,
&SymbolEditorWidget::cursorPositionChanged);
Expand Down
5 changes: 2 additions & 3 deletions libs/librepcb/librarymanager/addlibrarywidget.cpp
Expand Up @@ -65,7 +65,7 @@ AddLibraryWidget::AddLibraryWidget(workspace::Workspace& ws) noexcept
// tab "create local library": set placeholder texts
mUi->edtLocalName->setPlaceholderText("My Library");
mUi->edtLocalAuthor->setPlaceholderText(
mWorkspace.getSettings().getUser().getName());
mWorkspace.getSettings().userName.get());
mUi->edtLocalVersion->setPlaceholderText("0.1");
mUi->edtLocalUrl->setPlaceholderText(
tr("e.g. the URL to the Git repository (optional)"));
Expand Down Expand Up @@ -94,8 +94,7 @@ AddLibraryWidget::~AddLibraryWidget() noexcept {

void AddLibraryWidget::updateRepositoryLibraryList() noexcept {
clearRepositoryLibraryList();
foreach (const QUrl& url,
mWorkspace.getSettings().getRepositories().getUrls()) {
foreach (const QUrl& url, mWorkspace.getSettings().repositoryUrls.get()) {
std::shared_ptr<Repository> repo = std::make_shared<Repository>(url);
connect(repo.get(), &Repository::libraryListReceived, this,
&AddLibraryWidget::repositoryLibraryListReceived);
Expand Down
3 changes: 1 addition & 2 deletions libs/librepcb/librarymanager/libraryinfowidget.cpp
Expand Up @@ -62,8 +62,7 @@ LibraryInfoWidget::LibraryInfoWidget(workspace::Workspace& ws,
std::unique_ptr<TransactionalDirectory>(new TransactionalDirectory(
TransactionalFileSystem::openRO(mLibDir)))); // can throw

const QStringList& localeOrder =
ws.getSettings().getLibLocaleOrder().getLocaleOrder();
const QStringList& localeOrder = ws.getSettings().libraryLocaleOrder.get();

// image
if (!lib.getIconAsPixmap().isNull()) {
Expand Down
4 changes: 2 additions & 2 deletions libs/librepcb/librarymanager/librarymanager.cpp
Expand Up @@ -134,8 +134,8 @@ void LibraryManager::updateLibraryList() noexcept {
foreach (const FilePath& libDir, libraries) {
QString name, description, keywords;
mWorkspace.getLibraryDb().getElementTranslations<Library>(
libDir, mWorkspace.getSettings().getLibLocaleOrder().getLocaleOrder(),
&name, &description, &keywords); // can throw
libDir, mWorkspace.getSettings().libraryLocaleOrder.get(), &name,
&description, &keywords); // can throw
QPixmap icon;
mWorkspace.getLibraryDb().getLibraryMetadata(libDir, &icon); // can throw

Expand Down
6 changes: 2 additions & 4 deletions libs/librepcb/projecteditor/boardeditor/boardeditor.cpp
Expand Up @@ -127,10 +127,8 @@ BoardEditor::BoardEditor(ProjectEditor& projectEditor, Project& project)

// add graphics view as central widget
mGraphicsView = new GraphicsView(nullptr, this);
mGraphicsView->setUseOpenGl(mProjectEditor.getWorkspace()
.getSettings()
.getAppearance()
.getUseOpenGl());
mGraphicsView->setUseOpenGl(
mProjectEditor.getWorkspace().getSettings().useOpenGl.get());
mGraphicsView->setBackgroundBrush(Qt::black);
mGraphicsView->setForegroundBrush(Qt::white);
// setCentralWidget(mGraphicsView);
Expand Down
Expand Up @@ -92,10 +92,8 @@ Project* NewProjectWizard::createProject() const {

// set project settings (copy from workspace settings)
ProjectSettings& settings = project->getSettings();
settings.setLocaleOrder(
mWorkspace.getSettings().getLibLocaleOrder().getLocaleOrder());
settings.setNormOrder(
mWorkspace.getSettings().getLibNormOrder().getNormOrder());
settings.setLocaleOrder(mWorkspace.getSettings().libraryLocaleOrder.get());
settings.setNormOrder(mWorkspace.getSettings().libraryNormOrder.get());

// add schematic
if (mPageInitialization->getCreateSchematic()) {
Expand Down
Expand Up @@ -59,7 +59,7 @@ NewProjectWizardPage_Metadata::NewProjectWizardPage_Metadata(
&NewProjectWizardPage_Metadata::chooseLocationClicked);

// insert values
mUi->edtAuthor->setText(ws.getSettings().getUser().getName());
mUi->edtAuthor->setText(ws.getSettings().userName.get());
mUi->cbxLicense->addItem(QString("No License (not recommended)"), QString());
mUi->cbxLicense->addItem(
tr("CC0-1.0 (no restrictions, recommended for open hardware projects)"),
Expand Down
2 changes: 1 addition & 1 deletion libs/librepcb/projecteditor/projecteditor.cpp
Expand Up @@ -73,7 +73,7 @@ ProjectEditor::ProjectEditor(workspace::Workspace& workspace, Project& project)

// setup the timer for automatic backups, if enabled in the settings
int intervalSecs =
mWorkspace.getSettings().getProjectAutosaveInterval().getInterval();
mWorkspace.getSettings().projectAutosaveIntervalSeconds.get();
if ((intervalSecs > 0) && project.getDirectory().isWritable()) {
// autosaving is enabled --> start the timer
connect(&mAutoSaveTimer, &QTimer::timeout, this,
Expand Down
Expand Up @@ -108,10 +108,8 @@ SchematicEditor::SchematicEditor(ProjectEditor& projectEditor, Project& project)

// add graphics view as central widget
mGraphicsView = new GraphicsView(nullptr, this);
mGraphicsView->setUseOpenGl(mProjectEditor.getWorkspace()
.getSettings()
.getAppearance()
.getUseOpenGl());
mGraphicsView->setUseOpenGl(
mProjectEditor.getWorkspace().getSettings().useOpenGl.get());
mGraphicsView->setGridProperties(*mGridProperties);
setCentralWidget(mGraphicsView);

Expand Down

0 comments on commit 3695664

Please sign in to comment.