From 02eb44be4db68ba9935f4d0108b91fbc59734d99 Mon Sep 17 00:00:00 2001 From: vsr Date: Tue, 21 Jun 2016 16:05:13 +0300 Subject: [PATCH] Show language name in Preferences dialog box. --- src/LightApp/LightApp_Application.cxx | 48 +++++++++---- src/Qtx/QtxComboBox.cxx | 98 ++++++++++++--------------- src/Qtx/QtxComboBox.h | 12 ++-- src/Qtx/QtxPagePrefMgr.cxx | 47 ++++++------- src/Qtx/QtxPagePrefMgr.h | 4 +- 5 files changed, 105 insertions(+), 104 deletions(-) diff --git a/src/LightApp/LightApp_Application.cxx b/src/LightApp/LightApp_Application.cxx index b9ec2df18..47671d41a 100644 --- a/src/LightApp/LightApp_Application.cxx +++ b/src/LightApp/LightApp_Application.cxx @@ -245,21 +245,23 @@ int LightApp_Application::lastStudyId = 0; //since the 'toolbar marker' is not unique, find index of first occurrence of the //'toolbar marker' in the array and check that next string is name of the toolbar -int getToolbarMarkerIndex(QByteArray input, const QStringList& aFlags) { - int aResult = -1,tmp = 0; - int inputLen = input.length(); - QDataStream anInputData(&input, QIODevice::ReadOnly); - while(tmp < inputLen) { - tmp = input.indexOf(QToolBarMarker, tmp + 1); - if(tmp < 0 ) +namespace +{ + int getToolbarMarkerIndex( QByteArray input, const QStringList& aFlags ) { + int aResult = -1,tmp = 0; + int inputLen = input.length(); + QDataStream anInputData( &input, QIODevice::ReadOnly ); + while ( tmp < inputLen ) { + tmp = input.indexOf( QToolBarMarker, tmp + 1 ); + if ( tmp < 0 ) break; - anInputData.device()->seek(tmp); + anInputData.device()->seek( tmp ); uchar mark; - anInputData>>mark; + anInputData >> mark; int lines; anInputData >> lines; - if(lines == 0 && anInputData.atEnd()){ + if ( lines == 0 && anInputData.atEnd() ) { //Case then array doesn't contain information about toolbars, aResult = tmp; break; @@ -270,13 +272,26 @@ int getToolbarMarkerIndex(QByteArray input, const QStringList& aFlags) { int cnt; anInputData >> cnt; QString str; - anInputData>>str; - if(aFlags.contains(str)) { + anInputData >> str; + if ( aFlags.contains( str ) ) { aResult = tmp; break; } } - return aResult; + return aResult; + } + + QString langToName( const QString& lang ) + { + // special processing for English language to avoid such result as "American English" + // as Qt cannot just say "English" + QString result; + if ( lang == "en" ) + result = "English"; + else + result = QLocale( lang ).nativeLanguageName(); + return result; + } } /*! @@ -2219,10 +2234,15 @@ void LightApp_Application::createPreferences( LightApp_Preferences* pref ) LightApp_Preferences::Selector, "language", "language" ); QStringList aLangs = SUIT_Session::session()->resourceMgr()->stringValue( "language", "languages", "en" ).split( "," ); QList aIcons; + QList aNumbers; + QStringList aTitles; foreach ( QString aLang, aLangs ) { aIcons << QPixmap( QString( ":/images/%1" ).arg( aLang ) ); + aNumbers << aLang; + aTitles << langToName( aLang ); } - pref->setItemProperty( "strings", aLangs, curLang ); + pref->setItemProperty( "strings", aTitles, curLang ); + pref->setItemProperty( "ids", aNumbers, curLang ); pref->setItemProperty( "icons", aIcons, curLang ); pref->setItemProperty( "restart", true, curLang ); diff --git a/src/Qtx/QtxComboBox.cxx b/src/Qtx/QtxComboBox.cxx index a0b3b09a8..c9da713ac 100755 --- a/src/Qtx/QtxComboBox.cxx +++ b/src/Qtx/QtxComboBox.cxx @@ -19,10 +19,9 @@ // // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com // - // File: QtxComboBox.cxx // Author: Sergey TELKOV -// + #include "QtxComboBox.h" #include @@ -49,7 +48,7 @@ private: }; /*! - \brief Constructor + \brief Constructor. \internal \param parent parent object */ @@ -60,7 +59,7 @@ QtxComboBox::Model::Model( QObject* parent ) } /*! - \brief Destructor + \brief Destructor. \internal */ QtxComboBox::Model::~Model() @@ -68,7 +67,7 @@ QtxComboBox::Model::~Model() } /*! - \brief Set 'cleared' state + \brief Set 'cleared' state. \param isClear new 'cleared' state \internal */ @@ -84,7 +83,7 @@ void QtxComboBox::Model::setCleared( const bool isClear ) \brief Get model data. \param index model index \param role data role - \return data of role \a role for the \a index + \return data of \a role for given \a index \internal */ QVariant QtxComboBox::Model::data( const QModelIndex& index, int role ) const @@ -96,7 +95,7 @@ QVariant QtxComboBox::Model::data( const QModelIndex& index, int role ) const /*! \class QtxComboBox::ClearEvent \brief Custom event, used to process 'cleared' state of the combo box - in the editable mode. + in editable mode. \internal */ @@ -109,7 +108,7 @@ public: }; /*! - \brief Constructor + \brief Constructor. \internal */ QtxComboBox::ClearEvent::ClearEvent() : QEvent( CLEAR_EVENT ) @@ -140,16 +139,14 @@ QtxComboBox::QtxComboBox( QWidget* parent ) /*! \brief Destructor. - - Does nothing currently. */ QtxComboBox::~QtxComboBox() { } /*! - \brief Check if the combo box is in the "cleared" state. - \return \c true if combobox is in the "cleared" state + \brief Check if combo box is in "cleared" state. + \return \c true if combo box is in "cleared" state or \c false otherwise */ bool QtxComboBox::isCleared() const { @@ -174,31 +171,31 @@ void QtxComboBox::setCleared( const bool isClear ) } /*! - \brief Get current item ID. - \return item id + \brief Get current item's identifier. + \return current item's identifier */ -int QtxComboBox::currentId() const +QVariant QtxComboBox::currentId() const { return id( currentIndex() ); } /*! - \brief Set current item by ID. - \param num item ID + \brief Set current item by identifier. + \param ident item's identifier */ -void QtxComboBox::setCurrentId( int num ) +void QtxComboBox::setCurrentId( const QVariant& ident ) { - setCurrentIndex( index( num ) ); + setCurrentIndex( index( ident ) ); } /*! - \brief Set the identifier to specified item. - \param index - index of the item - \param id - identifier of the item + \brief Assign identifier to specified item. + \param idx item's index + \param ident item's identifier */ -void QtxComboBox::setId( const int index, const int id ) +void QtxComboBox::setId( const int idx, const QVariant& ident ) { - setItemData( index, QVariant( id ), (Qt::ItemDataRole)IdRole ); + setItemData( idx, ident, (Qt::ItemDataRole)IdRole ); } /*! @@ -216,7 +213,7 @@ void QtxComboBox::paintEvent( QPaintEvent* e ) } /*! - \brief Customize child addition/removal event + \brief Customize child addition/removal event. \param e child event */ void QtxComboBox::childEvent( QChildEvent* e ) @@ -236,8 +233,8 @@ void QtxComboBox::customEvent( QEvent* e ) } /*! - \brief Called when current item is chaned (by the user or programmatically). - \param idx item being set current + \brief Called when current item is changed (by user or programmatically). + \param idx index of item being set current */ void QtxComboBox::onCurrentChanged( int idx ) { @@ -249,7 +246,7 @@ void QtxComboBox::onCurrentChanged( int idx ) } /*! - \brief Reset "cleared" state and update the combo box. + \brief Reset "cleared" state and update combo box. */ void QtxComboBox::resetClear() { @@ -261,47 +258,40 @@ void QtxComboBox::resetClear() } /*! - \brief Get item ID by the index. - \param idx item index - \return item ID or -1 if index is invalid. + \brief Get item's identifier by index. + \param idx item's index + \return item's identifier or invalid QVariant if index is out of range + or identifier is not assigned to item */ -int QtxComboBox::id( const int idx ) const +QVariant QtxComboBox::id( const int idx ) const { - int id = -1; - QVariant v = itemData( idx, (Qt::ItemDataRole)IdRole ); - if ( v.canConvert( QVariant::Int ) ) - id = v.toInt(); - return id; + return itemData( idx, (Qt::ItemDataRole)IdRole ); } /*! - \brief Get item index by the ID. - \param id item ID - \return item index or -1 if ID is invalid. + \brief Get item index by identifier. + \param ident item's identifier + \return item's index or -1 if \a ident is invalid of if item is not found */ -int QtxComboBox::index( const int ident ) const +int QtxComboBox::index( const QVariant& ident ) const { - int idx = -1; - for ( int i = 0; i < (int)count() && idx == -1; i++ ) - { - if ( id( i ) == ident ) - idx = i; - } - return idx; + if ( !ident.isValid() ) return -1; + return findData( ident, (Qt::ItemDataRole)IdRole ); } /*! - \brief Returns true if the item with index has ID. - \param idx item index + \brief Check if item has assigned identifier. + \param idx item's index + \return \c true if item with given index has identifier of \c false otherwise */ bool QtxComboBox::hasId( const int idx ) const { QVariant v = itemData( idx, (Qt::ItemDataRole)IdRole ); - return v.canConvert( QVariant::Int ); + return !v.isNull(); } /*! - \fn void QtxComboBox::activatedId( int id ) - \brief Emitted when the item with identificator \a id is activated. - \param id item ID + \fn void QtxComboBox::activatedId( int ident ) + \brief Emitted when item with identificator \a ident is activated. + \param ident item's identifier */ diff --git a/src/Qtx/QtxComboBox.h b/src/Qtx/QtxComboBox.h index 5bffead00..45cad6740 100755 --- a/src/Qtx/QtxComboBox.h +++ b/src/Qtx/QtxComboBox.h @@ -49,17 +49,17 @@ public: bool isCleared() const; void setCleared( const bool ); - int currentId() const; - void setCurrentId( int ); + QVariant currentId() const; + void setCurrentId( const QVariant& ); - int id( const int ) const; - int index( const int ) const; + QVariant id( const int ) const; + int index( const QVariant& ) const; bool hasId( const int ) const; - void setId( const int, const int ); + void setId( const int, const QVariant& ); signals: - void activatedId( int ); + void activatedId( QVariant ); private slots: void onCurrentChanged( int ); diff --git a/src/Qtx/QtxPagePrefMgr.cxx b/src/Qtx/QtxPagePrefMgr.cxx index e4e5138a7..b6eac24c7 100644 --- a/src/Qtx/QtxPagePrefMgr.cxx +++ b/src/Qtx/QtxPagePrefMgr.cxx @@ -2735,9 +2735,9 @@ QStringList QtxPagePrefSelectItem::strings() const \return list of values IDs \sa strings(), icons(), setNumbers() */ -QList QtxPagePrefSelectItem::numbers() const +QList QtxPagePrefSelectItem::numbers() const { - QList res; + QList res; for ( int i = 0; i < mySelector->count(); i++ ) { if ( mySelector->hasId( i ) ) @@ -2775,10 +2775,10 @@ void QtxPagePrefSelectItem::setStrings( const QStringList& lst ) \param ids new list of values IDs \sa numbers(), setStrings(), setIcons() */ -void QtxPagePrefSelectItem::setNumbers( const QList& ids ) +void QtxPagePrefSelectItem::setNumbers( const QList& ids ) { int i = 0; - for ( QList::const_iterator it = ids.begin(); it != ids.end(); ++it, i++ ) { + for ( QList::const_iterator it = ids.begin(); it != ids.end(); ++it, i++ ) { if ( i >= mySelector->count() ) mySelector->addItem(QString("") ); @@ -2812,10 +2812,16 @@ void QtxPagePrefSelectItem::store() int idx = mySelector->currentIndex(); - if ( mySelector->hasId( idx ) ) - setInteger( mySelector->id( idx ) ); - else if ( idx >= 0 ) + if ( mySelector->hasId( idx ) ) { + QVariant id = mySelector->id( idx ); + if ( id.type() == QVariant::Int ) + setInteger( id.toInt() ); + else if ( id.type() == QVariant::String ) + setString( id.toString() ); + } + else if ( idx >= 0 ) { setString( mySelector->itemText( idx ) ); + } } /*! @@ -2825,14 +2831,10 @@ void QtxPagePrefSelectItem::store() void QtxPagePrefSelectItem::retrieve() { QString txt = getString(); - - int idx = -1; - - bool ok = false; - int num = txt.toInt( &ok ); - if ( ok ) - idx = mySelector->index( num ); - else + + // try to find via the id + int idx = mySelector->index( txt ); + if ( idx < 0 ) { for ( int i = 0; i < mySelector->count() && idx == -1; i++ ) { @@ -2872,11 +2874,7 @@ QVariant QtxPagePrefSelectItem::optionValue( const QString& name ) const return strings(); else if ( name == "numbers" || name == "ids" || name == "indexes" ) { - QList lst; - QList nums = numbers(); - for ( QList::const_iterator it = nums.begin(); it != nums.end(); ++it ) - lst.append( *it ); - return lst; + return numbers(); } else if ( name == "icons" || name == "pixmaps" ) { @@ -2935,14 +2933,7 @@ void QtxPagePrefSelectItem::setNumbers( const QVariant& var ) if ( var.type() != QVariant::List ) return; - QList lst; - QList varList = var.toList(); - for ( QList::const_iterator it = varList.begin(); it != varList.end(); ++it ) - { - if ( (*it).canConvert( QVariant::Int ) ) - lst.append( (*it).toInt() ); - } - setNumbers( lst ); + setNumbers( var.toList() ); } /*! diff --git a/src/Qtx/QtxPagePrefMgr.h b/src/Qtx/QtxPagePrefMgr.h index aaa3a56b0..43e6961ac 100644 --- a/src/Qtx/QtxPagePrefMgr.h +++ b/src/Qtx/QtxPagePrefMgr.h @@ -487,11 +487,11 @@ public: void setInputType( const int ); QStringList strings() const; - QList numbers() const; + QList numbers() const; QList icons() const; void setStrings( const QStringList& ); - void setNumbers( const QList& ); + void setNumbers( const QList& ); void setIcons( const QList& ); virtual void store(); -- 2.39.2