Skip to content

Commit

Permalink
Add User Selection Dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
YoannQDQ authored and nyalldawson committed Apr 24, 2023
1 parent 7b5f745 commit aa4701e
Show file tree
Hide file tree
Showing 6 changed files with 443 additions and 15 deletions.
1 change: 1 addition & 0 deletions src/app/CMakeLists.txt
Expand Up @@ -261,6 +261,7 @@ set(QGIS_APP_SRCS
options/qgsrenderingoptions.cpp
options/qgsvectorrenderingoptions.cpp
options/qgsuserprofilesettingsdialog.cpp
options/qgsuserprofileselectiondialog.cpp

gps/qgsappgpsconnection.cpp
gps/qgsappgpsdigitizing.cpp
Expand Down
58 changes: 47 additions & 11 deletions src/app/main.cpp
Expand Up @@ -105,6 +105,7 @@ typedef SInt32 SRefCon;
#include "qgsuserprofilemanager.h"
#include "qgsuserprofile.h"
#include "layers/qgsapplayerhandling.h"
#include "options/qgsuserprofileselectiondialog.h"

#ifdef HAVE_OPENCL
#include "qgsopenclutils.h"
Expand Down Expand Up @@ -1083,18 +1084,53 @@ int main( int argc, char *argv[] )
// If profile name was not explicitly set, use the policy to determine which profile to use
if ( profileName.isEmpty() )
{
switch ( manager.userProfileSelectionPolicy() )

// If no profiles exist, use the default profile
if ( manager.allProfiles().isEmpty() )
{
case QgsUserProfileManager::UserProfileSelectionPolicy::LastProfile:
profileName = manager.lastProfileName();
break;
// case QgsUserProfileManager::UserProfileSelectionPolicy::AskUser:
// profileName = manager.askUserToChooseProfile();
// break;
case QgsUserProfileManager::UserProfileSelectionPolicy::DefaultProfile:
default:
profileName = manager.defaultProfileName();
break;
profileName = manager.defaultProfileName();
}
else
{
switch ( manager.userProfileSelectionPolicy() )
{
// Use the last closed profile (default behavior prior to QGIS 3.32)
case QgsUserProfileManager::UserProfileSelectionPolicy::LastProfile:
profileName = manager.lastProfileName();
// If last used profile no longer exists, use the default profile
if ( !manager.profileExists( profileName ) )
{
profileName = manager.defaultProfileName();
}
break;

// Ask the user to select a profile (if more than one exists)
case QgsUserProfileManager::UserProfileSelectionPolicy::AskUser:
{
if ( manager.allProfiles().size() == 1 )
{
profileName = manager.allProfiles()[0];
break;
}
QgsUserProfileSelectionDialog dlg( &manager );
if ( dlg.exec() == QDialog::Accepted )
{
profileName = dlg.selectedProfileName();
}
else
{
// Exit QGIS if the user cancels the profile selection dialog
return 0;
}
break;
}

// Use the default profile
case QgsUserProfileManager::UserProfileSelectionPolicy::DefaultProfile:
default:
profileName = manager.defaultProfileName();
break;
}
}
}

Expand Down
103 changes: 103 additions & 0 deletions src/app/options/qgsuserprofileselectiondialog.cpp
@@ -0,0 +1,103 @@
/***************************************************************************
qgsuserprofileselectiondialog.cpp
--------------------------------------
Date : February 2023
Copyright : (C) 2023 by Yoann Quenach de Quivillic
Email : yoann dot quenach at gmail dot com
***************************************************************************
* *
* 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 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include <QListWidgetItem>
#include <QMessageBox>

#include "qgisapp.h"
#include "qgsapplication.h"
#include "qgsuserprofilemanager.h"
#include "qgsnewnamedialog.h"

#include "qgsuserprofileselectiondialog.h"

QgsUserProfileSelectionDialog::QgsUserProfileSelectionDialog( QgsUserProfileManager *manager, QWidget *parent )
: QDialog( parent ), mManager( manager )

{
setupUi( this );
setWindowIcon( QIcon( QgsApplication::appIconPath() ) );

// Select user profile on double click
connect( mProfileListWidget, &QListWidget::itemDoubleClicked, this, &QgsUserProfileSelectionDialog::accept );

// Add a new profile on button click
connect( mAddProfileButton, &QPushButton::clicked, this, &QgsUserProfileSelectionDialog::onAddProfile );

// Fill the list of profiles
mProfileListWidget->clear(); // Clear bogus profiles in the Ui form
for ( auto profile : mManager->allProfiles() )
{
auto item = new QListWidgetItem( mManager->profileForName( profile )->icon(), profile );
mProfileListWidget->addItem( item );

// If the profile is the last used one, select it
if ( profile == mManager->lastProfileName() )
{
mProfileListWidget->setCurrentItem( item );
item->setSelected( true );
}
}
}

QString QgsUserProfileSelectionDialog::selectedProfileName() const
{
return mProfileListWidget->currentItem()->text();
}

void QgsUserProfileSelectionDialog::accept()
{
// Accept only if an item is selected
if ( mProfileListWidget->currentItem() != nullptr && mProfileListWidget->currentItem()->isSelected() )
{
QDialog::accept();
}
}

void QgsUserProfileSelectionDialog::onAddProfile()
{
// Ask for a new profile name
QgsNewNameDialog dlg( QString(), QString(), QStringList(), mManager->allProfiles(), Qt::CaseInsensitive, this );
dlg.setConflictingNameWarning( tr( "A profile with this name already exists" ) );
dlg.setOverwriteEnabled( false );
dlg.setHintString( tr( "New profile name" ) );
dlg.setWindowTitle( tr( "New profile name" ) );

// Prevent from entering slashes and backslashes
dlg.setRegularExpression( "[^/\\\\]+" );

if ( dlg.exec() != QDialog::Accepted )
return;

// Try to create the profile folder
QString profileName = dlg.name();
QgsError error = mManager->createUserProfile( profileName );
if ( error.isEmpty() )
{
auto item = new QListWidgetItem( QgsApplication::getThemeIcon( "user.svg" ), profileName );
mProfileListWidget->addItem( item );
mProfileListWidget->setCurrentItem( item );
item->setSelected( true );
// Automatically accept the dialog
accept();
}
else
{
QMessageBox::warning( this, tr( "New Profile" ), tr( "Cannot create folder '%1'" ).arg( profileName ) );
return;
}
}

QgsUserProfileSelectionDialog::~QgsUserProfileSelectionDialog() {}
63 changes: 63 additions & 0 deletions src/app/options/qgsuserprofileselectiondialog.h
@@ -0,0 +1,63 @@
/***************************************************************************
qgsuserprofileselectiondialog.h
--------------------------------------
Date : February 2023
Copyright : (C) 2023 by Yoann Quenach de Quivillic
Email : yoann dot quenach at gmail dot com
***************************************************************************
* *
* 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 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSUSERPROFILESELECTIONDIALOG_H
#define QGSUSERPROFILESELECTIONDIALOG_H

#include <QDialog>

#include "ui_qgsuserprofileselectiondialog.h"


// Forward declarations
class QgsUserProfileManager;
class QEvent;

/**
* \class QgsUserProfileSelectionDialog
* \brief A dialog shown at startup to select the user profile
* \since QGIS 3.32
*/
class GUI_EXPORT QgsUserProfileSelectionDialog : public QDialog, private Ui::QgsUserProfileSelectionDialog
{
Q_OBJECT

public:

/**
* Constructor for QgsUserProfileSelectionDialog.
* \param manager QgsUserProfileManager manager that will be used to fill the list of profiles
* \param parent parent widget
*/
explicit QgsUserProfileSelectionDialog( QgsUserProfileManager *manager, QWidget *parent = nullptr );
virtual ~QgsUserProfileSelectionDialog();


/* Return the selected profile name */
QString selectedProfileName() const;

public slots:
void accept() override;

private slots:
void onAddProfile();

private:
QgsUserProfileManager *mManager = nullptr;


};

#endif // QGSUSERPROFILESELECTIONDIALOG_H
8 changes: 4 additions & 4 deletions src/app/options/qgsuserprofilesettingsdialog.cpp
Expand Up @@ -18,15 +18,17 @@

#include "qgsuserprofilesettingsdialog.h"


QgsUserProfileSettingsDialog::QgsUserProfileSettingsDialog( QWidget *parent )
: QDialog( parent )

{
setupUi( this );
connect( mDefaultProfile, &QRadioButton::toggled, mDefaultProfileComboBox, &QComboBox::setEnabled );
connect( mButtonBox, &QDialogButtonBox::accepted, this, &QgsUserProfileSettingsDialog::apply );

// Disable combobox if default profile is not selected
mDefaultProfileComboBox->setEnabled( false );
connect( mDefaultProfile, &QRadioButton::toggled, mDefaultProfileComboBox, &QComboBox::setEnabled );

// Init radio buttons
auto manager = QgisApp::instance()->userProfileManager();
if ( manager->userProfileSelectionPolicy() == QgsUserProfileManager::UserProfileSelectionPolicy::LastProfile )
Expand All @@ -46,7 +48,6 @@ QgsUserProfileSettingsDialog::QgsUserProfileSettingsDialog( QWidget *parent )
mDefaultProfileComboBox->clear();
mDefaultProfileComboBox->addItems( manager->allProfiles() );
mDefaultProfileComboBox->setCurrentText( manager->defaultProfileName() );

}

void QgsUserProfileSettingsDialog::apply()
Expand All @@ -66,4 +67,3 @@ void QgsUserProfileSettingsDialog::apply()
manager->setDefaultProfileName( mDefaultProfileComboBox->currentText() );
}
}

0 comments on commit aa4701e

Please sign in to comment.