Skip to content

Commit

Permalink
Allow setting the profile icon
Browse files Browse the repository at this point in the history
  • Loading branch information
YoannQDQ authored and nyalldawson committed Apr 24, 2023
1 parent ed48541 commit 0d5d5c3
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 35 deletions.
45 changes: 45 additions & 0 deletions src/app/options/qgsuserprofileoptions.cpp
Expand Up @@ -33,6 +33,10 @@ QgsUserProfileOptionsWidget::QgsUserProfileOptionsWidget( QWidget *parent )
mDefaultProfileComboBox->setEnabled( false );
connect( mDefaultProfile, &QRadioButton::toggled, mDefaultProfileComboBox, &QComboBox::setEnabled );

// Connect change icon button
connect( mChangeIconButton, &QToolButton::clicked, this, &QgsUserProfileOptionsWidget::onChangeIconClicked );
connect( mResetIconButton, &QToolButton::clicked, this, &QgsUserProfileOptionsWidget::onResetIconClicked );

// Init radio buttons
auto manager = QgisApp::instance()->userProfileManager();
if ( manager->userProfileSelectionPolicy() == QgsUserProfileManager::UserProfileSelectionPolicy::LastProfile )
Expand All @@ -56,6 +60,10 @@ QgsUserProfileOptionsWidget::QgsUserProfileOptionsWidget( QWidget *parent )
mDefaultProfileComboBox->addItem( icon, profile );
}
mDefaultProfileComboBox->setCurrentText( manager->defaultProfileName() );

// Init Active profile name and icon
mChangeIconButton->setIcon( manager->userProfile()->icon() );
mActiveProfileGroupBox->setTitle( tr( "Active Profile (%1)", "Active profile name" ).arg( manager->userProfile()->name() ) );
}

void QgsUserProfileOptionsWidget::apply()
Expand All @@ -76,7 +84,44 @@ void QgsUserProfileOptionsWidget::apply()
}
}

void QgsUserProfileOptionsWidget::onChangeIconClicked()
{
auto activeProfile = QgisApp::instance()->userProfileManager()->userProfile();
const QString profileName = activeProfile->name();
const QString iconPath = QFileDialog::getOpenFileName( this, tr( "Select icon" ), "", tr( "Images (*.png *.jpg *.jpeg *.gif *.bmp *.svg)" ) );
if ( !iconPath.isEmpty() )
{
// Remove existing icon files
QDir dir( activeProfile->folder(), "icon.*", QDir::Name, QDir::Files );
for ( const QString &file : dir.entryList() )
{
dir.remove( file );
}
// Copy the icon file to the profile folder
const QString extension = QFileInfo( iconPath ).suffix();
const QString dstPath = activeProfile->folder() + QDir::separator() + "icon." + extension;
QFile::copy( iconPath, dstPath );

// Update the button icon
mChangeIconButton->setIcon( QIcon( iconPath ) );
mDefaultProfileComboBox->setItemIcon( mDefaultProfileComboBox->findText( activeProfile->name() ), activeProfile->icon() );
}
}


void QgsUserProfileOptionsWidget::onResetIconClicked()
{
auto activeProfile = QgisApp::instance()->userProfileManager()->userProfile();
// Remove existing icon files
QDir dir( activeProfile->folder(), "icon.*", QDir::Name, QDir::Files );
for ( const QString &file : dir.entryList() )
{
dir.remove( file );
}
// Update the button icon
mChangeIconButton->setIcon( activeProfile->icon() );
mDefaultProfileComboBox->setItemIcon( mDefaultProfileComboBox->findText( activeProfile->name() ), activeProfile->icon() );
}


//
Expand Down
12 changes: 9 additions & 3 deletions src/app/options/qgsuserprofileoptions.h
Expand Up @@ -32,13 +32,19 @@ class APP_EXPORT QgsUserProfileOptionsWidget : public QgsOptionsPageWidget, priv

public:

/**
* Constructor for QgsUserProfileOptionsWidget with the specified \a parent widget.
*/
//! Constructor for QgsUserProfileOptionsWidget with the specified \a parent widget.
QgsUserProfileOptionsWidget( QWidget *parent );

void apply() override;

private slots:

//! Change the default profile icon
void onChangeIconClicked();

//! Reset the profile icon to default
void onResetIconClicked();

};


Expand Down
12 changes: 8 additions & 4 deletions src/core/qgsuserprofile.cpp
Expand Up @@ -127,12 +127,16 @@ QgsError QgsUserProfile::setAlias( const QString &alias ) const

const QIcon QgsUserProfile::icon() const
{
const QString path = mProfileFolder + QDir::separator() + "icon.svg";
if ( !QFileInfo::exists( path ) )
const QStringList extensions = {".svg", ".png", ".jpg", ".jpeg", ".gif", ".bmp"};
const QString basename = mProfileFolder + QDir::separator() + "icon";

for ( const QString &extension : extensions )
{
return QgsApplication::getThemeIcon( "user.svg" );
const QString path = basename + extension;
if ( QFileInfo::exists( path ) )
return QIcon( path );
}
return QIcon( path );
return QgsApplication::getThemeIcon( "user.svg" );
}

QString QgsUserProfile::qgisDB() const
Expand Down
133 changes: 105 additions & 28 deletions src/ui/qgsuserprofileoptionswidgetbase.ui
Expand Up @@ -6,31 +6,21 @@
<rect>
<x>0</x>
<y>0</y>
<width>412</width>
<height>226</height>
<width>462</width>
<height>378</height>
</rect>
</property>
<property name="windowTitle">
<string>User Profile Settings</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QGroupBox" name="groupBox">
<widget class="QGroupBox" name="mProfilePolicyGroupBox">
<property name="title">
<string>Which profile should be used when QGIS starts ?</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="2" column="0">
<widget class="QRadioButton" name="mDefaultProfile">
<property name="text">
<string>Always use profile</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QComboBox" name="mDefaultProfileComboBox"/>
</item>
<item row="4" column="0" colspan="2">
<item row="5" column="0" colspan="2">
<widget class="QLabel" name="label">
<property name="font">
<font>
Expand All @@ -49,37 +39,124 @@
</property>
</widget>
</item>
<item row="0" column="0">
<item row="2" column="0" colspan="2">
<widget class="QRadioButton" name="mAskUser">
<property name="text">
<string>Let user choose profile at start up</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QRadioButton" name="mDefaultProfile">
<property name="text">
<string>Always use profile</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QComboBox" name="mDefaultProfileComboBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
<item row="0" column="0" colspan="2">
<widget class="QRadioButton" name="mLastProfile">
<property name="text">
<string>Use last closed profile</string>
</property>
</widget>
</item>
<item row="3" column="0">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="mActiveProfileGroupBox">
<property name="title">
<string>Active Profile</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="mChangeIconLabel">
<property name="text">
<string>Change icon</string>
</property>
<property name="sizeHint" stdset="0">
</widget>
</item>
<item>
<widget class="QToolButton" name="mChangeIconButton">
<property name="toolTip">
<string/>
</property>
<property name="text">
<string>Change profile icon</string>
</property>
<property name="icon">
<iconset resource="../../images/images.qrc">
<normaloff>:/images/themes/default/user.svg</normaloff>:/images/themes/default/user.svg</iconset>
</property>
<property name="iconSize">
<size>
<width>20</width>
<height>40</height>
<width>32</width>
<height>32</height>
</size>
</property>
</spacer>
</widget>
</item>
<item row="1" column="0" colspan="2">
<widget class="QRadioButton" name="mAskUser">
<property name="text">
<string>Let user choose profile at start up</string>
<item>
<widget class="QToolButton" name="mResetIconButton">
<property name="toolTip">
<string>Reset icon</string>
</property>
<property name="icon">
<iconset resource="../../images/images.qrc">
<normaloff>:/images/themes/default/mActionRefresh.svg</normaloff>:/images/themes/default/mActionRefresh.svg</iconset>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
<resources/>
<tabstops>
<tabstop>mLastProfile</tabstop>
<tabstop>mDefaultProfile</tabstop>
<tabstop>mDefaultProfileComboBox</tabstop>
<tabstop>mAskUser</tabstop>
<tabstop>mChangeIconButton</tabstop>
</tabstops>
<resources>
<include location="../../images/images.qrc"/>
</resources>
<connections/>
</ui>

0 comments on commit 0d5d5c3

Please sign in to comment.