From 9b2bbc310db2d764566e96e3fae013688188f43f Mon Sep 17 00:00:00 2001 From: vsr Date: Mon, 14 May 2007 06:22:36 +0000 Subject: [PATCH] Porting to Qt4 --- src/Qtx/Makefile.am | 1 + src/Qtx/Qtx.cxx | 69 +- src/Qtx/Qtx.h | 12 +- src/Qtx/Qtx.pro | 1 + src/Qtx/QtxAction.cxx | 7 +- src/Qtx/QtxAction.h | 6 +- src/Qtx/QtxActionMenuMgr.cxx | 31 +- src/Qtx/QtxActionMenuMgr.h | 4 +- src/Qtx/QtxActionMgr.cxx | 17 +- src/Qtx/QtxActionMgr.h | 6 +- src/Qtx/QtxActionToolMgr.cxx | 2 +- src/Qtx/QtxActionToolMgr.h | 10 +- src/Qtx/QtxColorScale.cxx | 15 +- src/Qtx/QtxColorScale.h | 4 +- src/Qtx/QtxComboBox.cxx | 16 +- src/Qtx/QtxComboBox.h | 4 +- src/Qtx/QtxDialog.cxx | 53 +- src/Qtx/QtxDialog.h | 6 +- src/Qtx/QtxDockAction.cxx | 8 +- src/Qtx/QtxDockAction.h | 2 +- src/Qtx/QtxDockWidget.cxx | 440 ++++++++++ src/Qtx/QtxDockWidget.h | 58 ++ src/Qtx/QtxMainWindow.cxx | 11 +- src/Qtx/QtxMainWindow.h | 2 +- src/Qtx/QtxMap.h | 210 +++++ src/Qtx/QtxResourceMgr.cxx | 1460 +++++++++++++++++++++++----------- src/Qtx/QtxResourceMgr.h | 324 +------- 27 files changed, 1886 insertions(+), 893 deletions(-) create mode 100644 src/Qtx/QtxDockWidget.cxx create mode 100644 src/Qtx/QtxDockWidget.h create mode 100644 src/Qtx/QtxMap.h diff --git a/src/Qtx/Makefile.am b/src/Qtx/Makefile.am index 40e5cb324..4ea4b66da 100755 --- a/src/Qtx/Makefile.am +++ b/src/Qtx/Makefile.am @@ -38,6 +38,7 @@ salomeinclude_HEADERS= \ QtxDockAction.h \ QtxDockWidget.h \ QtxMainWindow.h \ + QtxMap.h \ QtxResourceMgr.h \ QtxToolBar.h \ QtxWorkstack.h diff --git a/src/Qtx/Qtx.cxx b/src/Qtx/Qtx.cxx index 3505b0cfc..05971b643 100755 --- a/src/Qtx/Qtx.cxx +++ b/src/Qtx/Qtx.cxx @@ -21,18 +21,15 @@ #include "Qtx.h" -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #include #include @@ -41,6 +38,17 @@ /*! \class Qtx \brief Set of helpful utility functions. + + The class implements set of static functions which can be used for different purpuses: + - define tab order for set of widgets: setTabOrder() + - align one widget to the other widget: alignWidget() + - remove extra separators from menu or toolbar: simplifySeparators() + - retrieve directory, file name and extension parts of the path: dir(), file(), extension() + - get temporary directory name: tmpDir() + - create and remove directory (recursively): mkDir(), rmDir() + - convert text file from DOS to UNIX native format: dos2unix() + - convert picture to the gray scale: grayscale() + - and other */ /*! @@ -224,43 +232,6 @@ void Qtx::alignWidget( QWidget* src, const QWidget* ref, const int alignFlags ) src->move( x, y ); } -/* VSR: obsolete -void Qtx::simplifySeparators( QToolBar* toolbar ) -{ - if ( !toolbar ) - return; - - const QObjectList& objList = toolbar->children(); - - QObjectList delList; - - bool isPrevSep = true; - QObject* lastVis = 0; // last visible - for ( QObjectList::const_iterator it = objList.begin(); it != objList.end(); ++it ) - { - QObject* obj = *it; - if ( !obj || !obj->isWidgetType() ) - continue; - bool isSep = obj->inherits( "QToolBarSeparator" ); - if ( !isSep && !((QWidget*)obj)->isVisibleTo( toolbar ) ) - continue; - if ( isPrevSep && isSep ) - delList.append( obj ); - else - { - isPrevSep = isSep; - lastVis = obj; - } - } - // remove last visible separator - if ( lastVis && lastVis->inherits( "QToolBarSeparator" ) ) - delList.append( lastVis ); - - for ( QObjectList::iterator itr = delList.begin(); itr != delList.end(); ++itr ) - delete *itr; -} -*/ - /*! \brief Remove (recursively) unnecessary separators from the menu or toolbar. \param wid widget, should be of QMenu* or QToolBar* class diff --git a/src/Qtx/Qtx.h b/src/Qtx/Qtx.h index 7d629aea8..cd43bd5a6 100755 --- a/src/Qtx/Qtx.h +++ b/src/Qtx/Qtx.h @@ -42,15 +42,14 @@ #define true 1 #endif -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include class QObject; class QWidget; -//class QToolBar; typedef QList QIntList; //!< list of int values typedef QList QShortList; //!< list of short int values @@ -94,7 +93,6 @@ public: static void setTabOrder( const QWidgetList& ); static void alignWidget( QWidget*, const QWidget*, const int ); -// static void simplifySeparators( QToolBar* ); static void simplifySeparators( QWidget*, const bool = true ); static bool isParent( QObject*, QObject* ); diff --git a/src/Qtx/Qtx.pro b/src/Qtx/Qtx.pro index fe5e44661..173f57909 100644 --- a/src/Qtx/Qtx.pro +++ b/src/Qtx/Qtx.pro @@ -22,6 +22,7 @@ HEADERS += QtxDialog.h HEADERS += QtxDockAction.h HEADERS += QtxDockWidget.h HEADERS += QtxMainWindow.h +HEADERS += QtxMap.h HEADERS += QtxResourceMgr.h HEADERS += QtxToolBar.h HEADERS += QtxWorkstack.h diff --git a/src/Qtx/QtxAction.cxx b/src/Qtx/QtxAction.cxx index e716103c8..99dc21fa3 100755 --- a/src/Qtx/QtxAction.cxx +++ b/src/Qtx/QtxAction.cxx @@ -21,10 +21,9 @@ #include "QtxAction.h" -#include -#include -#include -#include +#include +#include +#include /*! \class QtxAction diff --git a/src/Qtx/QtxAction.h b/src/Qtx/QtxAction.h index f00b6f625..7540446ad 100755 --- a/src/Qtx/QtxAction.h +++ b/src/Qtx/QtxAction.h @@ -24,9 +24,9 @@ #include "Qtx.h" -#include -#include -#include +#include + +class QIcon; #ifdef WIN32 #pragma warning ( disable:4251 ) diff --git a/src/Qtx/QtxActionMenuMgr.cxx b/src/Qtx/QtxActionMenuMgr.cxx index d1159e580..5e9dc0188 100644 --- a/src/Qtx/QtxActionMenuMgr.cxx +++ b/src/Qtx/QtxActionMenuMgr.cxx @@ -23,22 +23,16 @@ #include "QtxAction.h" -#include -#include - -#include -#include -#include -#include - -#include +#include +#include +#include +#include // VSR: Uncomment this #define in order to allow dynamic menus support // (emit signals when popup menu is pre-activated) // Currently this support is disabled. //#define ENABLE_DYNAMIC_MENU - /*! \class QtxActionMenuMgr::MenuNode \internal @@ -1171,3 +1165,20 @@ int QtxActionMenuMgr::MenuCreator::append( const QString& tag, const bool subMen return res; } + +/*! + \fn void QtxActionMenuMgr::menuAboutToShow( QMenu* m ) + \brief Emitted when the menu is about to be shown. + \param m menu being shown +*/ + +/*! + \fn void QtxActionMenuMgr::menuAboutToHide( QMenu* m ) + \brief Emitted when the menu is about to be hidden. + \param m menu being hidden +*/ + +/*! + \fn void QtxActionMenuMgr::menuHighlighted( int, int ) + \brief Emitted when the menu is hightlighted [obsolete]. +*/ diff --git a/src/Qtx/QtxActionMenuMgr.h b/src/Qtx/QtxActionMenuMgr.h index 8ce33fff9..4ba7214f2 100644 --- a/src/Qtx/QtxActionMenuMgr.h +++ b/src/Qtx/QtxActionMenuMgr.h @@ -25,11 +25,11 @@ #include "Qtx.h" #include "QtxActionMgr.h" -#include -#include +#include class QMenu; class QMainWindow; +class QStringList; #ifdef WIN32 #pragma warning( disable:4251 ) diff --git a/src/Qtx/QtxActionMgr.cxx b/src/Qtx/QtxActionMgr.cxx index dc8ce8cc8..2ae9ebf02 100644 --- a/src/Qtx/QtxActionMgr.cxx +++ b/src/Qtx/QtxActionMgr.cxx @@ -22,16 +22,13 @@ #include "Qtx.h" #include "QtxActionMgr.h" #include "QtxAction.h" - -#include -#include - -#include -#include -#include -#include - -#include +#include +#include +#ifndef QT_NO_DOM +#include +#include +#include +#endif typedef QList< QPointer > qtx_actionlist; static qtx_actionlist qtx_separator_actions; diff --git a/src/Qtx/QtxActionMgr.h b/src/Qtx/QtxActionMgr.h index eef1607ca..0c710386d 100644 --- a/src/Qtx/QtxActionMgr.h +++ b/src/Qtx/QtxActionMgr.h @@ -24,9 +24,9 @@ #include "Qtx.h" -#include -#include -#include +#include +#include +#include class QTimer; class QAction; diff --git a/src/Qtx/QtxActionToolMgr.cxx b/src/Qtx/QtxActionToolMgr.cxx index 1da344416..b4eb75794 100644 --- a/src/Qtx/QtxActionToolMgr.cxx +++ b/src/Qtx/QtxActionToolMgr.cxx @@ -24,7 +24,7 @@ #include "QtxAction.h" #include "QtxToolBar.h" -#include +#include /*! \class QtxActionToolMgr::ToolNode diff --git a/src/Qtx/QtxActionToolMgr.h b/src/Qtx/QtxActionToolMgr.h index 7589037ef..56adc04d2 100644 --- a/src/Qtx/QtxActionToolMgr.h +++ b/src/Qtx/QtxActionToolMgr.h @@ -23,16 +23,14 @@ #define QTXACTIONTOOLMGR_H #include "Qtx.h" - -#include -#include - -#include - #include "QtxActionMgr.h" +#include +#include + class QToolBar; class QMainWindow; +class QAction; #ifdef WIN32 #pragma warning( disable:4251 ) diff --git a/src/Qtx/QtxColorScale.cxx b/src/Qtx/QtxColorScale.cxx index b4e5e235f..97b61a2b3 100755 --- a/src/Qtx/QtxColorScale.cxx +++ b/src/Qtx/QtxColorScale.cxx @@ -21,15 +21,12 @@ #include "QtxColorScale.h" -#include -#include -#include - -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include #include diff --git a/src/Qtx/QtxColorScale.h b/src/Qtx/QtxColorScale.h index 9458670f5..ae9e985f2 100755 --- a/src/Qtx/QtxColorScale.h +++ b/src/Qtx/QtxColorScale.h @@ -24,8 +24,8 @@ #include "Qtx.h" -#include -#include +#include +#include class QTextDocument; diff --git a/src/Qtx/QtxComboBox.cxx b/src/Qtx/QtxComboBox.cxx index ac57d6752..eecfbb405 100755 --- a/src/Qtx/QtxComboBox.cxx +++ b/src/Qtx/QtxComboBox.cxx @@ -21,9 +21,7 @@ #include "QtxComboBox.h" -#include -#include -#include +#include /*! \class QtxComboBox @@ -218,3 +216,15 @@ int QtxComboBox::index( const int id ) const } return idx; } + +/*! + \fn void QtxComboBox::activatedId( int id ) + \brief Emitted when the item with identificator \a id is activated. + \param id item ID +*/ + +/*! + \fn void QtxComboBox::highlightedId( int id ) + \brief Emitted when the item with identificator \a id is highlighted. + \param id item ID +*/ diff --git a/src/Qtx/QtxComboBox.h b/src/Qtx/QtxComboBox.h index 765095551..8038e5aa9 100755 --- a/src/Qtx/QtxComboBox.h +++ b/src/Qtx/QtxComboBox.h @@ -24,8 +24,8 @@ #include "Qtx.h" -#include -#include +#include +#include #ifdef WIN32 #pragma warning( disable:4251 ) diff --git a/src/Qtx/QtxDialog.cxx b/src/Qtx/QtxDialog.cxx index 859dcb226..3ea88f0d5 100755 --- a/src/Qtx/QtxDialog.cxx +++ b/src/Qtx/QtxDialog.cxx @@ -21,13 +21,13 @@ #include "QtxDialog.h" -#include -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include +#include /*! \class QtxDialog::Area @@ -1547,3 +1547,42 @@ void QtxDialog::adjustButtons() (*bItr)->setMinimumWidth( minWidth ); } } + +/*! + \fn void QtxDialog::dlgButton( int id ) + \brief Emitted when the user button is clicked. + \param id user button identificator +*/ +/*! + \fn void QtxDialog::dlgParamChanged() + \brief This signal can be used in successor classes to signalize about + some dialog parameter changing. +*/ +/*! + \fn void QtxDialog::dlgHelp() + \brief Emitted when the "Help" button is clicked. +*/ +/*! + \fn void QtxDialog::dlgApply() + \brief Emitted when the "Apply" button is clicked. +*/ +/*! + \fn void QtxDialog::dlgOk() + \brief Emitted when the "OK" button is clicked. +*/ +/*! + \fn void QtxDialog::dlgNo() + \brief Emitted when the "No" button is clicked. +*/ +/*! + \fn void QtxDialog::dlgYes() + \brief Emitted when the "Yes" button is clicked. +*/ +/*! + \fn void QtxDialog::dlgClose() + \brief Emitted when the "Close" button is clicked. +*/ +/*! + \fn void QtxDialog::dlgCancel() + \brief Emitted when the "Cancel" button is clicked. +*/ diff --git a/src/Qtx/QtxDialog.h b/src/Qtx/QtxDialog.h index 094a092fb..c77d0fa08 100755 --- a/src/Qtx/QtxDialog.h +++ b/src/Qtx/QtxDialog.h @@ -24,10 +24,8 @@ #include "Qtx.h" -#include -#include - -#include +#include +#include class QFrame; class QLabel; diff --git a/src/Qtx/QtxDockAction.cxx b/src/Qtx/QtxDockAction.cxx index d2fdb1616..8f40092d3 100755 --- a/src/Qtx/QtxDockAction.cxx +++ b/src/Qtx/QtxDockAction.cxx @@ -21,10 +21,10 @@ #include "QtxDockAction.h" -#include -#include -#include -#include +#include +#include +#include +#include /*! \class QtxDockAction diff --git a/src/Qtx/QtxDockAction.h b/src/Qtx/QtxDockAction.h index 23b72f71b..f6ee3313f 100755 --- a/src/Qtx/QtxDockAction.h +++ b/src/Qtx/QtxDockAction.h @@ -24,7 +24,7 @@ #include "QtxAction.h" -#include +#include class QIcon; class QString; diff --git a/src/Qtx/QtxDockWidget.cxx b/src/Qtx/QtxDockWidget.cxx new file mode 100644 index 000000000..fd6f00371 --- /dev/null +++ b/src/Qtx/QtxDockWidget.cxx @@ -0,0 +1,440 @@ +// Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License. +// +// This library is distributed in the hope that it will be useful +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// +// File: QtxDockWidget.cxx +// Author: Sergey TELKOV + +#include "QtxDockWidget.h" + +#include +#include +#include +#include +#include + +/*! + \class QtxDockWidget::Watcher + \internal + \brief Internal class which goal is to watch parent dockable widget state changing. +*/ + +class QtxDockWidget::Watcher : public QObject +{ +public: + Watcher( QtxDockWidget* ); + + void shown( QtxDockWidget* ); + void hidden( QtxDockWidget* ); + + virtual bool eventFilter( QObject*, QEvent* ); + +protected: + virtual void customEvent( QEvent* ); + +private: + void installFilters(); + + void showContainer(); + void hideContainer(); + + void updateIcon(); + void updateCaption(); + void updateVisibility(); + +private: + QtxDockWidget* myCont; + bool myState; + bool myEmpty; + bool myVisible; +}; + +/*! + \brief Constructor. + \param cont dockable widget to be watched +*/ +QtxDockWidget::Watcher::Watcher( QtxDockWidget* cont ) +: QObject( cont ), myCont( cont ), + myState( true ), + myEmpty( false ) +{ + myCont->installEventFilter( this ); + myVisible = myCont->isVisibleTo( myCont->parentWidget() ); + + installFilters(); +} + +/*! + \brief Custom event filter. + \param o event receiver object + \param e event sent to object + \return \c true if further event processing should be stopped +*/ +bool QtxDockWidget::Watcher::eventFilter( QObject* o, QEvent* e ) +{ + if ( o == myCont && ( e->type() == QEvent::Show || e->type() == QEvent::ShowToParent || + e->type() == QEvent::Hide || e->type() == QEvent::HideToParent ) ) + { + installFilters(); + QApplication::postEvent( this, new QEvent( QEvent::User ) ); + } + + if ( o == myCont && e->type() == QEvent::ChildAdded ) + { + QChildEvent* ce = (QChildEvent*)e; + if ( ce->child()->isWidgetType() ) + ce->child()->installEventFilter( this ); + + QApplication::postEvent( this, new QEvent( QEvent::User ) ); + } + + if ( o != myCont && e->type() == QEvent::WindowIconChange ) + updateIcon(); + + if ( o != myCont && e->type() == QEvent::WindowTitleChange ) + updateCaption(); + + if ( ( o != myCont && ( e->type() == QEvent::Hide || e->type() == QEvent::HideToParent ) ) || + ( o == myCont && ( e->type() == QEvent::ChildRemoved ) ) || + ( e->type() == QEvent::Show || e->type() == QEvent::ShowToParent ) ) + updateVisibility(); + + return false; +} + +/*! + \brief Set internal status to "shown" + \param dw dockable widget +*/ +void QtxDockWidget::Watcher::shown( QtxDockWidget* dw ) +{ + if ( dw != myCont ) + return; + + myVisible = true; +} + +/*! + \brief Set internal status to "hidden" + \param dw dockable widget +*/ +void QtxDockWidget::Watcher::hidden( QtxDockWidget* dw ) +{ + if ( dw != myCont ) + return; + + myVisible = false; +} + +/*! + \brief Show the dock window being watched +*/ +void QtxDockWidget::Watcher::showContainer() +{ + if ( !myCont ) + return; + + QtxDockWidget* cont = myCont; + myCont = 0; + cont->show(); + myCont = cont; +} + +/*! + \brief Hide the dock window being watched +*/ +void QtxDockWidget::Watcher::hideContainer() +{ + if ( !myCont ) + return; + + QtxDockWidget* cont = myCont; + myCont = 0; + cont->hide(); + myCont = cont; +} + +/*! + \brief Proces custom events. + \param e custom event (not used) +*/ +void QtxDockWidget::Watcher::customEvent( QEvent* /*e*/ ) +{ + updateIcon(); + updateCaption(); + updateVisibility(); +} + +/*! + \brief Install this object as event filter to all children widgets + of the dockable widget being watched. +*/ +void QtxDockWidget::Watcher::installFilters() +{ + if ( !myCont ) + return; + + QLayout* l = myCont->layout(); + if ( !l ) + return; + + for ( int i = 0; i < (int)l->count(); i++ ) + { + if ( l->itemAt( i ) && l->itemAt( i )->widget() ) + l->itemAt( i )->widget()->installEventFilter( this ); + } +} + +/*! + \brief Update visibility state of all children widgets of the dockable widget + being watched. +*/ +void QtxDockWidget::Watcher::updateVisibility() +{ + if ( !myCont ) + return; + + QLayout* l = myCont->layout(); + if ( !l ) + return; + + bool vis = false; + for ( int i = 0; i < (int)l->count() && !vis; i++ ) + vis = l->itemAt( i ) && l->itemAt( i )->widget() && l->itemAt( i )->widget()->isVisibleTo( myCont ); + + if ( myEmpty == vis ) + { + myEmpty = !vis; + if ( !myEmpty ) + myCont->toggleViewAction()->setVisible( myState ); + else + { + myState = myCont->toggleViewAction()->isVisible(); + myCont->toggleViewAction()->setVisible( false ); + } + } + + vis = !myEmpty && myVisible; + if ( vis != myCont->isVisibleTo( myCont->parentWidget() ) ) + vis ? showContainer() : hideContainer(); +} + +/*! + \brief Update the icon of dockable window being watched +*/ +void QtxDockWidget::Watcher::updateIcon() +{ + if ( !myCont || !myCont->widget() ) + return; + + myCont->setWindowIcon( myCont->widget()->windowIcon() ); +} + +/*! + \brief Update the title of dockable window being watched +*/ +void QtxDockWidget::Watcher::updateCaption() +{ + if ( myCont && myCont->widget() && !myCont->widget()->windowTitle().isNull() ) + myCont->setWindowTitle( myCont->widget()->windowTitle() ); +} + +/*! + \class QtxDockWidget + \brief Enhanced dockable widget class. +*/ + +/*! + \brief Constructor. + \param title dockable widget title + \param parent parent widget + \param f widget flags +*/ +QtxDockWidget::QtxDockWidget( const QString& title, QWidget* parent, Qt::WindowFlags f ) +: QDockWidget( title, parent, f ), + myWatcher( 0 ), + myOrientation( (Qt::Orientation)-1 ) +{ + updateState(); +} + +/*! + \brief Constructor. + \param watch if \c true the event filter is installed to watch wigdet state changes + to update it properly + \param parent parent widget + \param f widget flags +*/ +QtxDockWidget::QtxDockWidget( const bool watch, QWidget* parent, Qt::WindowFlags f ) +: QDockWidget( parent, f ), + myWatcher( 0 ), + myOrientation( (Qt::Orientation)-1 ) +{ + if ( watch ) + myWatcher = new Watcher( this ); + + updateState(); +} + +/*! + \brief Constructor. + \param parent parent widget + \param f widget flags +*/ +QtxDockWidget::QtxDockWidget( QWidget* parent, Qt::WindowFlags f ) +: QDockWidget( parent, f ), +myWatcher( 0 ) +{ +} + +/*! + \brief Destructor. +*/ +QtxDockWidget::~QtxDockWidget() +{ +} + +/*! + \brief Get the recommended size for the widget. + \return recommended dockable widget size +*/ +QSize QtxDockWidget::sizeHint() const +{ + QSize sz = QDockWidget::sizeHint(); +/* + if ( place() == InDock && isStretchable() && area() ) + { + if ( orientation() == Horizontal ) + sz.setWidth( area()->width() ); + else + sz.setHeight( area()->height() ); + } +*/ + return sz; +} + +/*! + \brief Get the recommended minimum size for the widget. + \return recommended dockable widget minimum size +*/ +QSize QtxDockWidget::minimumSizeHint() const +{ + QSize sz = QDockWidget::minimumSizeHint(); +/* + if ( orientation() == Horizontal ) + sz = QSize( 0, QDockWidget::minimumSizeHint().height() ); + else + sz = QSize( QDockWidget::minimumSizeHint().width(), 0 ); + + if ( place() == InDock && isStretchable() && area() ) + { + if ( orientation() == Horizontal ) + sz.setWidth( area()->width() ); + else + sz.setHeight( area()->height() ); + } +*/ + return sz; +} + +/*! + \brief Show/hide the dockable window. + \param on new visibility state +*/ +void QtxDockWidget::setVisible( bool on ) +{ + if ( myWatcher ) + { + if ( on ) + myWatcher->shown( this ); + else + myWatcher->hidden( this ); + } + + updateGeometry(); + if ( widget() ) + widget()->updateGeometry(); + + QDockWidget::setVisible( on ); +} + +/*! + \brief Process resize event + \param e event +*/ +void QtxDockWidget::resizeEvent( QResizeEvent* e ) +{ + QDockWidget::resizeEvent( e ); + updateState(); +} + +/*! + \brief Get dockable window orientation. + \return orientation type +*/ +Qt::Orientation QtxDockWidget::orientation() const +{ + QMainWindow* mw = 0; + QWidget* wid = parentWidget(); + while ( wid && !mw ) + { + mw = ::qobject_cast( wid ); + wid = wid->parentWidget(); + } + + Qt::Orientation res = (Qt::Orientation)-1; + + if ( !mw ) + return res; + + Qt::DockWidgetArea area = mw->dockWidgetArea( (QtxDockWidget*)this ); + switch ( area ) + { + case Qt::LeftDockWidgetArea: + case Qt::RightDockWidgetArea: + res = Qt::Vertical; + break; + case Qt::TopDockWidgetArea: + case Qt::BottomDockWidgetArea: + res = Qt::Horizontal; + break; + default: + break; + } + + return res; +} + +/*! + \brief Update dockable window state. +*/ +void QtxDockWidget::updateState() +{ + Qt::Orientation o = orientation(); + if ( myOrientation == o ) + return; + + myOrientation = o; + + emit orientationChanged( myOrientation ); +} + +/*! + \fn QtxDockWidget::orientationChanged(Qt::Orientation o) + \brief Emitted when the dockable window orientation is changed. + \param o new window orientation +*/ diff --git a/src/Qtx/QtxDockWidget.h b/src/Qtx/QtxDockWidget.h new file mode 100644 index 000000000..ed7cb25e9 --- /dev/null +++ b/src/Qtx/QtxDockWidget.h @@ -0,0 +1,58 @@ +// Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License. +// +// This library is distributed in the hope that it will be useful +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// +// File: QtxDockWidget.h +// Author: Sergey TELKOV + +#include "Qtx.h" + +#include + +class QTX_EXPORT QtxDockWidget : public QDockWidget +{ + Q_OBJECT + + class Watcher; + +public: + QtxDockWidget( const QString&, QWidget* = 0, Qt::WindowFlags = 0 ); + QtxDockWidget( const bool, QWidget* = 0, Qt::WindowFlags = 0 ); + QtxDockWidget( QWidget*, Qt::WindowFlags = 0 ); + virtual ~QtxDockWidget(); + + virtual QSize sizeHint() const; + virtual QSize minimumSizeHint() const; + + Qt::Orientation orientation() const; + +signals: + void orientationChanged( Qt::Orientation ); + +public slots: + virtual void setVisible( bool ); + +protected: + virtual void resizeEvent( QResizeEvent* ); + +private: + void updateState(); + +private: + Watcher* myWatcher; //!< watcher object + Qt::Orientation myOrientation; //!< dockable window orientation +}; diff --git a/src/Qtx/QtxMainWindow.cxx b/src/Qtx/QtxMainWindow.cxx index 741a2ef25..a0d5f67f8 100644 --- a/src/Qtx/QtxMainWindow.cxx +++ b/src/Qtx/QtxMainWindow.cxx @@ -24,12 +24,11 @@ #include "QtxToolBar.h" #include "QtxResourceMgr.h" -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include /*! \class QtxMainWindow::Filter diff --git a/src/Qtx/QtxMainWindow.h b/src/Qtx/QtxMainWindow.h index c67ab2688..10accc407 100644 --- a/src/Qtx/QtxMainWindow.h +++ b/src/Qtx/QtxMainWindow.h @@ -24,7 +24,7 @@ #include "Qtx.h" -#include +#include class QtxResourceMgr; diff --git a/src/Qtx/QtxMap.h b/src/Qtx/QtxMap.h new file mode 100644 index 000000000..2030b9e98 --- /dev/null +++ b/src/Qtx/QtxMap.h @@ -0,0 +1,210 @@ +// Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License. +// +// This library is distributed in the hope that it will be useful +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// +// File: QtxMap.h +// Author: Vadim SANDLER + +#ifndef QTX_MAP_H +#define QTX_MAP_H + +template class IMap; +template class IMapIterator; +template class IMapConstIterator; + +/*! + \brief Indexed map template class. +*/ +template class IMap +{ +public: + typedef IMapIterator Iterator; + typedef IMapConstIterator ConstIterator; + +public: + IMap() {} + IMap( const IMap& m ) : myKeys( m.myKeys ), myData( m.myData ) {} + IMap& operator=( const IMap& m ) { myKeys = m.myKeys; myData = m.myData; return *this; } + + int count() const { return myData.count(); } + int size() const { return myData.count(); } + bool empty() const { return myData.empty(); } + bool isEmpty() const { return myData.empty(); } + + void clear() { myKeys.clear(); myData.clear(); } + + QList keys() const { return myKeys; } + QList values() const { QList l; for ( int i = 0; i < count(); i++ ) l.append( value( i ) ); return l; } + bool contains ( const Key& key ) const { return myData.contains( key ); } + + Iterator begin() { return Iterator( this ); } + Iterator end() { return Iterator( this, count() ); } + ConstIterator begin() const { return ConstIterator( this ); } + ConstIterator end() const { return ConstIterator( this, count() ); } + + Iterator insert( const Key& key, const Value& value, bool overwrite = true ) + { + if ( myData.find( key ) == myData.end() || overwrite ) + { + if ( myData.find( key ) != myData.end() && overwrite ) + myKeys.removeAt( myKeys.indexOf( key ) ); + myKeys.append( key ); + myData[key] = value; + } + return Iterator( this, index( key ) ); + } + + Iterator replace( const Key& key, const Value& value ) + { + if ( myData.find( key ) == myData.end() ) + myKeys.append( key ); + myData[ key ] = value; + return Iterator( this, index( key ) ); + } + + int index( const Key& key ) const { return myKeys.indexOf( key ); } + Iterator at( const int index ) { return Iterator( this, index ); } + ConstIterator at( const int index ) const { return ConstIterator( this, index ); } + + Key& key( const int index ) + { + if ( index < 0 || index >= (int)myKeys.count() ) + return dummyKey; + return myKeys[index]; + } + + Value value( const int index ) + { + if ( index < 0 || index >= (int)myKeys.count() ) + return dummyValue; + return myData[ myKeys[index] ]; + } + + Value operator[]( const Key& key ) + { + if ( myData.find( key ) == myData.end() ) + insert( key, Value() ); + return myData[ key ]; + } + + const Value operator[]( const Key& key ) const + { + if ( myData.find( key ) == myData.end() ) + return dummyValue; + return myData[key]; + } + + void erase( Iterator it ) { remove( it ); } + void erase( const Key& key ) { remove( key ); } + void erase( const int index ) { remove( index ); } + void remove( Iterator it ) { if ( it.myMap != this ) return; remove( it.myIndex ); } + void remove( const Key& key ) { remove( index( key ) ); } + void remove( const int index ) + { + if ( index >= 0 && index < (int)myKeys.count() ) + { + myData.remove( myKeys[index] ); + myKeys.removeAt( index ); + } + } + +private: + QList myKeys; + QMap myData; + Key dummyKey; + Value dummyValue; + + friend class IMapIterator; + friend class IMapConstIterator; +}; + +/*! + \brief Indexed map iterator template class. +*/ +template class IMapIterator +{ +public: + IMapIterator() : myMap( 0 ), myIndex( 0 ) { init(); } + IMapIterator( const IMap* m ) : myMap( const_cast< IMap* >( m ) ), myIndex( 0 ) { init(); } + IMapIterator( const IMapIterator& i ) : myMap( i.myMap ), myIndex( i.myIndex ) { init(); } + + bool operator==( const IMapIterator& i ) { return !operator!=( i ); } + bool operator!=( const IMapIterator& i ) { return !myMap || myMap != i.myMap || myIndex != i.myIndex; } + + operator bool() const { return myIndex >= 0; } + + const Key& key() const { return myMap->key( myIndex ); } + Value& value() { return myMap->value( myIndex ); } + const Value& value() const { return myMap->value( myIndex ); } + + Value& operator*() { return value(); } + + IMapIterator& operator++() { myIndex++; init(); return *this; } + IMapIterator operator++( int ) { IMapIterator i = *this; myIndex++; init(); return i; } + IMapIterator& operator--() { myIndex--; init(); return *this; } + IMapIterator operator--( int ) { IMapIterator i = *this; myIndex--; init(); return i; } + +private: + IMapIterator( const IMap* m, const int index ) : myMap( const_cast< IMap* >( m ) ), myIndex( index ) { init(); } + void init() { if ( !myMap || myIndex >= myMap->count() ) myIndex = -1; } + +private: + IMap* myMap; + int myIndex; + + friend class IMap; + friend class IMapConstIterator; +}; + +/*! + \brief Indexed map const iterator template class. +*/ +template class IMapConstIterator +{ +public: + IMapConstIterator() : myMap( 0 ), myIndex( 0 ) { init(); } + IMapConstIterator( const IMap* m ) : myMap( const_cast< IMap* >( m ) ), myIndex( 0 ) { init(); } + IMapConstIterator( const IMapConstIterator& i ) : myMap( i.myMap ), myIndex( i.myIndex ) { init(); } + IMapConstIterator( const IMapIterator& i ) : myMap( i.myMap ), myIndex( i.myIndex ) { init(); } + + bool operator==( const IMapConstIterator& i ) { return !operator!=( i ); } + bool operator!=( const IMapConstIterator& i ) { return !myMap || myMap != i.myMap || myIndex != i.myIndex; } + + operator bool() const { return myIndex >= 0; } + + const Key& key() const { return myMap->key( myIndex ); } + const Value value() const { return myMap->value( myIndex ); } + + const Value operator*() const { return value(); } + + IMapConstIterator& operator++() { myIndex++; init(); return *this; } + IMapConstIterator operator++( int ) { IMapConstIterator i = *this; myIndex++; init(); return i; } + IMapConstIterator& operator--() { myIndex--; init(); return *this; } + IMapConstIterator operator--( int ) { IMapConstIterator i = *this; myIndex--; init(); return i; } + +private: + IMapConstIterator( const IMap* m, const int index ): myMap( const_cast< IMap* >( m ) ), myIndex( index ) { init(); } + void init() { if ( !myMap || myIndex >= myMap->count() ) myIndex = -1; } + +private: + IMap* myMap; + int myIndex; + + friend class IMap; +}; + +#endif // QTX_MAP_H diff --git a/src/Qtx/QtxResourceMgr.cxx b/src/Qtx/QtxResourceMgr.cxx index 02b3a1c65..c61a96bda 100644 --- a/src/Qtx/QtxResourceMgr.cxx +++ b/src/Qtx/QtxResourceMgr.cxx @@ -18,40 +18,105 @@ // #include "QtxResourceMgr.h" -#include -#include -#include -#include -#include -#include - +#include +#include +#include +#include +#include +#include #ifndef QT_NO_DOM -#include +#include +#include +#include #endif +#define EMULATE_GLOBAL_CONTEXT + #include /*! - Class: QtxResourceMgr::Resources - Level: Internal + \class QtxResourceMgr::Resources + \internal + \brief Represents container for settings read from the resource file. */ -QtxResourceMgr::Resources::Resources( const QtxResourceMgr* mgr, const QString& fileName ) -: myFileName( fileName ), - myMgr( const_cast( mgr ) ) +class QtxResourceMgr::Resources +{ +public: + Resources( QtxResourceMgr*, const QString& ); + virtual ~Resources(); + + QString file() const; + void setFile( const QString& ); + + QString value( const QString&, const QString&, const bool ) const; + void setValue( const QString&, const QString&, const QString& ); + + bool hasSection( const QString& ) const; + bool hasValue( const QString&, const QString& ) const; + + void removeSection( const QString& ); + void removeValue( const QString&, const QString& ); + + QPixmap loadPixmap( const QString&, const QString&, const QString& ) const; + QTranslator* loadTranslator( const QString&, const QString&, const QString& ) const; + + QString environmentVariable( const QString&, int&, int& ) const; + QString makeSubstitution( const QString&, const QString&, const QString& ) const; + + void clear(); + + QStringList sections() const; + QStringList parameters( const QString& ) const; + + QString path( const QString&, const QString&, const QString& ) const; + +protected: + QtxResourceMgr* resMgr() const; + +private: + Section section( const QString& ); + const Section section( const QString& ) const; + + QString fileName( const QString&, const QString&, const QString& ) const; + +private: + typedef QMap SectionMap; + +private: + QtxResourceMgr* myMgr; //!< resources manager + SectionMap mySections; //!< sections map + QString myFileName; //!< resources file name + QMap myPixmapCache; //!< pixmaps cache + + friend class QtxResourceMgr::Format; +}; + +/*! + \brief Constructor. + \param mgr parent resources manager + \param fileName resources file name +*/ +QtxResourceMgr::Resources::Resources( QtxResourceMgr* mgr, const QString& fileName ) +: myMgr( mgr ), + myFileName( fileName ) { } /*! - Destructor + \brief Destructor. */ QtxResourceMgr::Resources::~Resources() { } /*! - Returns name of resource file - This file is used to load/save operations + \brief Get resources file name. + + This file is used to load/save operations. + + \return file name + \sa setFile() */ QString QtxResourceMgr::Resources::file() const { @@ -59,8 +124,9 @@ QString QtxResourceMgr::Resources::file() const } /*! - Sets name of resource file - \param fn - name of file + \brief Set resources file name. + \param fn file name + \sa file() */ void QtxResourceMgr::Resources::setFile( const QString& fn ) { @@ -68,14 +134,12 @@ void QtxResourceMgr::Resources::setFile( const QString& fn ) } /*! - Returns string representation of parameter value - Returns QString::null if there is no such parameter - - \param sect - name of section - \param name - name of parameter - \param subst - if it is true, then the substitution of variables - will be done with help of makeSubstitution method - \sa makeSubstitution() + \brief Get string representation of parameter value. + \param sect section name + \param name parameter name + \param subst if \c true, perform variables substitution + \return parameter value or null QString if there is no such parameter + \sa setValue(), makeSubstitution() */ QString QtxResourceMgr::Resources::value( const QString& sect, const QString& name, const bool subst ) const { @@ -91,21 +155,24 @@ QString QtxResourceMgr::Resources::value( const QString& sect, const QString& na } /*! - Sets value by it's string representation - - \param sect - name of section - \param name - name of parameter - \param val - string value + \brief Set parameter value. + \param sect section name + \param name parameter name + \param val parameter value + \sa value(), makeSubstitution() */ void QtxResourceMgr::Resources::setValue( const QString& sect, const QString& name, const QString& val ) { - Section& s = section( sect ); - s.insert( name, val ); + if ( !mySections.contains( sect ) ) + mySections.insert( sect, Section() ); + + mySections[sect].insert( name, val ); } /*! - \return true if section exists - \param sect - name of section + \brief Check section existence. + \param sect section name + \return \c true if section exists */ bool QtxResourceMgr::Resources::hasSection( const QString& sect ) const { @@ -113,9 +180,10 @@ bool QtxResourceMgr::Resources::hasSection( const QString& sect ) const } /*! - \return true if parameter exists in section - \param sect - name of section - \param name - name of parameter + \brief Check parameter existence. + \param sect section name + \param name parameter name + \return \c true if parameter exists in specified section */ bool QtxResourceMgr::Resources::hasValue( const QString& sect, const QString& name ) const { @@ -123,8 +191,8 @@ bool QtxResourceMgr::Resources::hasValue( const QString& sect, const QString& na } /*! - Removes section from resources - \param sect - name of section + \brief Remove resourcs section. + \param sect secton name */ void QtxResourceMgr::Resources::removeSection( const QString& sect ) { @@ -132,24 +200,23 @@ void QtxResourceMgr::Resources::removeSection( const QString& sect ) } /*! - Removes parameter from section - \param sect - name of section - \param name - name of parameter + \brief Remove parameter from the section. + \param sect section name + \param name parameter name */ void QtxResourceMgr::Resources::removeValue( const QString& sect, const QString& name ) { - if ( !hasSection( sect ) ) + if ( !mySections.contains( sect ) ) return; - Section& s = section( sect ); - s.remove( name ); + mySections[sect].remove( name ); - if ( s.isEmpty() ) + if ( mySections[sect].isEmpty() ) mySections.remove( sect ); } /*! - Removes all sections + \brief Remove all sections. */ void QtxResourceMgr::Resources::clear() { @@ -157,6 +224,7 @@ void QtxResourceMgr::Resources::clear() } /*! + \brief Get all sections names. \return list of section names */ QStringList QtxResourceMgr::Resources::sections() const @@ -165,8 +233,9 @@ QStringList QtxResourceMgr::Resources::sections() const } /*! - \return list of parameter names from section - \param sec - name of section + \brief Get all parameters name in specified section. + \param sec section name + \return list of settings names */ QStringList QtxResourceMgr::Resources::parameters( const QString& sec ) const { @@ -177,11 +246,19 @@ QStringList QtxResourceMgr::Resources::parameters( const QString& sec ) const } /*! - \return path of file from directory built by parameter - \return QString::null if built path doesn't exist - \param sec - name of section - \param prefix - name of parameter containing some path - \param name - name of file + \brief Get absolute path to the file which name is defined by the parameter. + + The file name is defined by \a name argument, while directory name is retrieved + from resources parameter \a prefix of section \a sec. Both directory and file name + can be relative. If the directory is relative, it is calculated from the initial + resources file name (see file()). Directory parameter can contain environment + variables, which are substituted automatically. + + \param sec section name + \param prefix parameter containing directory name + \param name file name + \return absolute file path or null QString if file does not exist + \sa fileName(), file(), makeSubstitution() */ QString QtxResourceMgr::Resources::path( const QString& sec, const QString& prefix, const QString& name ) const { @@ -195,7 +272,8 @@ QString QtxResourceMgr::Resources::path( const QString& sec, const QString& pref } /*! - \return corresponding resource manager + \brief Get resource manager + \return resource manager pointer */ QtxResourceMgr* QtxResourceMgr::Resources::resMgr() const { @@ -203,9 +281,14 @@ QtxResourceMgr* QtxResourceMgr::Resources::resMgr() const } /*! - \return instance of section by it's name. Section will be created if it doesn't exist + \brief Get resources section by specified name. + + If section does not exist it is created (empty). + + \param sn section name + \return resources section */ -QtxResourceMgr::Section& QtxResourceMgr::Resources::section( const QString& sn ) +QtxResourceMgr::Section QtxResourceMgr::Resources::section( const QString& sn ) { if ( !mySections.contains( sn ) ) mySections.insert( sn, Section() ); @@ -214,18 +297,31 @@ QtxResourceMgr::Section& QtxResourceMgr::Resources::section( const QString& sn ) } /*! - \return instance of section by it's name. Section will be created if it doesn't exist + \brief Get resources section by specified name. + \param sn section name + \return resources section */ -const QtxResourceMgr::Section& QtxResourceMgr::Resources::section( const QString& sn ) const +const QtxResourceMgr::Section QtxResourceMgr::Resources::section( const QString& sn ) const { return mySections[sn]; } /*! - \return full path of file - \param sect - name of section - \param prefix - name of parameter containing some path - \param name - name of file + \brief Get file path. + + The file name is defined by \a name argument, while directory name is retrieved + from resources parameter \a prefix of section \a sec. Both directory and file name + can be relative. If the directory is relative, it is calculated from the initial + resources file name (see file()). Directory parameter can contain environment + variables, which are substituted automatically. + File existence is not checked. + + \param sec section name + \param prefix parameter containing directory name + \param name file name + \return absolute file path or null QString if \a prefix parameter + does not exist in section \sec + \sa path(), file(), makeSubstitution() */ QString QtxResourceMgr::Resources::fileName( const QString& sect, const QString& prefix, const QString& name ) const { @@ -236,7 +332,7 @@ QString QtxResourceMgr::Resources::fileName( const QString& sect, const QString& if ( !path.isEmpty() ) { if ( QFileInfo( path ).isRelative() ) - path = Qtx::addSlash( QFileInfo( myFileName ).dirPath( true ) ) + path; + path = Qtx::addSlash( Qtx::dir( myFileName, true ) ) + path; path = Qtx::addSlash( path ) + name; } @@ -245,17 +341,23 @@ QString QtxResourceMgr::Resources::fileName( const QString& sect, const QString& { QString fname = QDir::convertSeparators( path ); QFileInfo inf( fname ); - fname = inf.absFilePath(); + fname = inf.absoluteFilePath(); return fname; } return QString(); } /*! - \return QPixmap loaded from file - \param sect - name of section - \param prefix - name of parameter containing some path - \param name - name of picture file + \brief Load and return pixmap from external file. + + If QtxResourceMgr::isPixmapCached() is \c true then cached pixmap is returned + (if it is already loaded), otherwise it is loaded from file. + If the file name is invalid, null pixmap is returned. + + \param sect section name + \param prefix parameter containing resources directory name + \param name pixmap file name + \return pixmap loaded from file */ QPixmap QtxResourceMgr::Resources::loadPixmap( const QString& sect, const QString& prefix, const QString& name ) const { @@ -274,15 +376,55 @@ QPixmap QtxResourceMgr::Resources::loadPixmap( const QString& sect, const QStrin } /*! - \return just created and loaded translator - \param sect - name of section - \param prefix - name of parameter containing some path - \param name - name of file + \brief Load translator. + \param sect section name + \param prefix parameter containing resources directory + \param name translation file name + \return just created and loaded translator or 0 in case of error */ QTranslator* QtxResourceMgr::Resources::loadTranslator( const QString& sect, const QString& prefix, const QString& name ) const { QTranslator* trans = new QTranslator( 0 ); - if ( !trans->load( fileName( sect, prefix, name ) ) ) + QString fname = fileName( sect, prefix, name ); +#ifdef EMULATE_GLOBAL_CONTEXT + char* buf = 0; + QFile file( fname ); + int len = file.size(); + if ( len ) + { + buf = new char[len]; + if ( !file.open( QIODevice::ReadOnly ) || len != (int)file.read( buf, len ) ) + { + delete buf; + buf = 0; + } + file.close(); + } + if ( buf ) + { + char* pattern = "@default"; + size_t pl = strlen( pattern ); + for ( size_t i = 0; i < len - pl; i++ ) + { + char* cur = buf + i; + if ( !strncmp( cur, pattern, pl ) ) + { + *cur = '\0'; + i += pl - 1; + } + } + + if ( !trans->load( (uchar*)buf, len ) ) + { + delete buf; + buf = 0; + } + } + + if ( !buf ) +#else + if ( !trans->load( Qtx::file( fname, false ), Qtx::dir( fname ) ) ) +#endif { delete trans; trans = 0; @@ -291,39 +433,63 @@ QTranslator* QtxResourceMgr::Resources::loadTranslator( const QString& sect, con } /*! - Finds in string variables by patterns: ${name} or $(name) or %name% - \return first found name or QString::null if there is no ones - \param str - string where the search is processed - \param start - integer value for returning start position of variable - \param len - integer value for returning length of variable + \brief Parse given string to retrieve environment variable. + + Looks through the string for the patterns: ${name} or $(name) or %name%. + If string contains variable satisfying any pattern, the variable name + is returned, start index of the variable is returned in the \a start parameter, + and length of the variable is returned in the \a len parameter. + + \param str string being processed + \param start if variable is found, this parameter contains its starting + position in the \a str + \param len if variable is found, this parameter contains its length + \return first found variable or null QString if there is no ones */ QString QtxResourceMgr::Resources::environmentVariable( const QString& str, int& start, int& len ) const { QString varName = QString::null; len = 0; - QRegExp rx( "\\$\\{([a-zA-Z]+[a-zA-Z0-9_]*)\\}|\\$\\(([a-zA-Z]+[a-zA-Z0-9_]*)\\)|\\$([a-zA-Z]+[a-zA-Z0-9_]*)|\\%([a-zA-Z]+[a-zA-Z0-9_]*)\\%" ); + QRegExp rx( "(^\\$\\{|[^\\$]\\$\\{)([a-zA-Z]+[a-zA-Z0-9_]*)(\\})|(^\\$\\(|[^\\$]\\$\\()([a-zA-Z]+[a-zA-Z0-9_]*)(\\))|(^\\$|[^\\$]\\$)([a-zA-Z]+[a-zA-Z0-9_]*)|(^%|[^%]%)([a-zA-Z]+[a-zA-Z0-9_]*)(%[^%]|%$)" ); - int pos = rx.search( str, start ); + int pos = rx.indexIn( str, start ); if ( pos != -1 ) { - start = pos; - len = rx.matchedLength(); - QStringList caps = rx.capturedTexts(); - for ( uint i = 1; i <= caps.count() && varName.isEmpty(); i++ ) - varName = *caps.at( i ); + int i = 1; + while ( i <= rx.numCaptures() && varName.isEmpty() ) + { + QString capStr = rx.cap( i ); + if ( !capStr.contains( "%" ) && !capStr.contains( "$" ) ) + varName = capStr; + i++; + } + + if ( !varName.isEmpty() ) + { + int capIdx = i - 1; + start = rx.pos( capIdx ); + int end = start + varName.length(); + if ( capIdx > 1 && rx.cap( capIdx - 1 ).contains( QRegExp( "\\$|%" ) ) ) + start = rx.pos( capIdx - 1 ) + rx.cap( capIdx - 1 ).indexOf( QRegExp( "\\$|%" ) ); + if ( capIdx < rx.numCaptures() && !rx.cap( capIdx - 1 ).isEmpty() ) + end++; + len = end - start; + } } return varName; } /*! - Substitutes variables by its' values. If variable is from enviroment, - it will be replaced by environment value. If it isn't, method tries to - find it's value among resources - \return new variant of string 'str' - \param str - string to process substitution - \param sect - section, in which the variables will be finding - \param name - name of variable which must be ignored during substitution + \brief Substitute variables by their values. + + Environment variable is substituted by its value. For other variables resource + manager tries to find value among defined resources parameters. + + \param str string to be processed + \param sect section, where variables are searched + \param name name of variable which must be ignored during substitution + \return processed string (with all substitutions made) */ QString QtxResourceMgr::Resources::makeSubstitution( const QString& str, const QString& sect, const QString& name ) const { @@ -339,9 +505,9 @@ QString QtxResourceMgr::Resources::makeSubstitution( const QString& str, const Q if ( envName.isNull() ) break; - QString newStr = QString::null; - if ( ::getenv( envName ) ) - newStr = QString( ::getenv( envName ) ); + QString newStr; + if ( ::getenv( envName.toLatin1() ) ) + newStr = QString( ::getenv( envName.toLatin1() ) ); if ( newStr.isNull() ) { @@ -358,13 +524,18 @@ QString QtxResourceMgr::Resources::makeSubstitution( const QString& str, const Q res.replace( start, len, newStr ); } + res.replace( "$$", "$" ); + res.replace( "%%", "%" ); + return res; } /*! - Class: QtxResourceMgr::IniFormat - Level: Internal + \class QtxResourceMgr::IniFormat + \internal + \brief Reader/writer for .ini resources files. */ + class QtxResourceMgr::IniFormat : public Format { public: @@ -377,7 +548,7 @@ protected: }; /*! - Default constructor + \brief Constructor. */ QtxResourceMgr::IniFormat::IniFormat() : Format( "ini" ) @@ -385,21 +556,22 @@ QtxResourceMgr::IniFormat::IniFormat() } /*! - Destructor + \brief Destructor. */ QtxResourceMgr::IniFormat::~IniFormat() { } /*! - Loads resources from ini-file to map of sections - \param fname - name of resource file - \param secMap - map of sections + \brief Load resources from ini-file. + \param fname resources file name + \param secMap resources map to be filled in + \return \c true on success and \c false on error */ bool QtxResourceMgr::IniFormat::load( const QString& fname, QMap& secMap ) { QFile file( fname ); - if ( !file.open( IO_ReadOnly ) ) + if ( !file.open( QFile::ReadOnly ) ) return false; QTextStream ts( &file ); @@ -425,7 +597,7 @@ bool QtxResourceMgr::IniFormat::load( const QString& fname, QMap& secMap ) { QFile file( fname ); - if ( !file.open( IO_WriteOnly ) ) + if ( !file.open( QFile::WriteOnly ) ) return false; + QTextStream ts( &file ); + bool res = true; for ( QMap::ConstIterator it = secMap.begin(); it != secMap.end() && res; ++it ) { - QString data = QString( "[%1]\n" ).arg( it.key() ); - for ( Section::ConstIterator iter = it.data().begin(); iter != it.data().end(); ++iter ) - data += iter.key() + " = " + iter.data() + "\n"; - data += "\n"; + QStringList data( QString( "[%1]" ).arg( it.key() ) ); + for ( Section::ConstIterator iter = it.value().begin(); iter != it.value().end(); ++iter ) + data.append( iter.key() + " = " + iter.value() ); + data.append( "" ); - res = file.writeBlock( data.latin1(), data.length() ) == (int)data.length(); + for ( QStringList::const_iterator itr = data.begin(); itr != data.end(); ++itr ) + ts << *itr << endl; } file.close(); @@ -490,8 +665,9 @@ bool QtxResourceMgr::IniFormat::save( const QString& fname, const QMap& secMap ) { @@ -539,7 +716,7 @@ bool QtxResourceMgr::XmlFormat::load( const QString& fname, QMap& secMap ) { @@ -639,7 +817,7 @@ bool QtxResourceMgr::XmlFormat::save( const QString& fname, const QMapmySections = sections; else - qDebug( "QtxResourceMgr: Could not load resource file \"%s\"", res->myFileName.latin1() ); + qDebug( "QtxResourceMgr: Could not load resource file \"%s\"", (const char*)res->myFileName.toLatin1() ); return status; } /*! - \brief Perform the saving of the resources into resource file. - \param res - resources object which will be saved + \brief Save resources to the resource file. + \param res resources object + \return \c true on success and \c false on error */ bool QtxResourceMgr::Format::save( Resources* res ) { @@ -820,30 +1013,106 @@ bool QtxResourceMgr::Format::save( Resources* res ) } /*! - Class: QtxResourceMgr - Level: Public -*/ - -/*! - \brief Constructs the resource manager object for application. - \param appName - name of the application which resources will be used. - \param resVarTemplate - template for the resource environment variable name which - should point to the resource directory list. - Default value is "%1Resources". Its mean that for application - with name "MyApp" environment variable "MyAppResources" will - be used. Template may not have the parameter '%1' substituted - by application name. In this case this string will be used as - is without substitution. - Resource environment variable should contains one or several resource directories - separated by symbol ';'. Resource directories list transfered into the setDirList(). - These directories and the user home directory used for the loading application resources. - Each of the resource directories can contains resource file. The name of this file defined - by the function globalFileName(). Resource file name in the user home defined by the - function userFileName(). Any resource looking firstly in the user home resources then - resource directories used in the specified order. All setted resources always stored into - the resource file at the user home. Only user home resource file is saved. - If you want to ignore of loading of Local User Preferences, you needs setup setIngoreUserValues() - as true. + \fn virtual bool QtxResourceMgr::Format::load( const QString& fname, + QMap& secMap ) + \brief Load resources from the specified resources file. + + Should be implemented in the successors. + + \param fname resources file name + \param secMap resources map to be filled in + \return \c true on success and \c false on error +*/ + +/*! + \fn virtual bool QtxResourceMgr::Format::save( const QString& fname, + const QMap& secMap ) + + \brief Save resources to the specified resources file. + + Should be implemented in the successors. + + \param fname resources file name + \param secMap resources map + \return \c true on success and \c false on error +*/ + +/*! + \class QtxResourceMgr + \brief Application resources manager. + + This class can be used to define settings, save/load settings and + application preferences to the resource file(s), load translation files + (internationalization mechanism), load pixmaps and other resources from + external files, etc. + + Currently it supports .ini and .xml resources file formats. To implement + own resources file format, inherit from the Format class and implement virtual + Format::load() and Format::save() methods. + + Resources manager is initialized by the (symbolic) name of the application. + The parameter \a resVarTemplate specifies the template for the environment + variable which should point to the resource directory or list of directories. + Environment variable name is calculated by substitution of "%1" substring in + the \a resVarTemplate parameter (if it contains such substring) by the + application name (\a appName). + By default, \a resVarTemplate is set to "%1Resources". For example, if the application name + is "MyApp", the environment variable "MyAppResources" will be inspected in this case. + + Resource manager can handle several global application configuration files and + one user configuration file. Location of global configuration files is defined + by the environment variable (see above) and these files are always read-only. + The name of the global configuration files is retrieved by calling virtual method + globalFileName() which can be redefined in the QtxResourceMgr class successors. + User configuration file always situated in the user's home directory. It's name + is defined by calling virtual method userFileName() which can be also redefined + in the QtxResourceMgr class successors. This is the only file which the preferences + changed by the user during the application session are written to (usually + when the application closes). + + Resources environment variable should contain one or several resource directories + (separated by ";" symbol on Windows and ":" or ";" on Linux). Each resource directory + can contain application global configuration file. The user configuration file has + the highest priority, for the global configuration files the priority is decreasing from + left to right, i.e. the first directory in the directoris list, defined by the + resources environment variable has higher priority. Priority has the meaning when + searching requested resources (application preference, pixmap file name, translation + file, etc). + Loading of the user configuration file can be omitted by calling setIgnoreUserValues() + with \c true parameter. + + Resources manager operates with such terms like options, sections and parameters. + Parametets are named application resources, for example, application preferences like + integer, double, boolean or string values, pictures, font and color definitions, etc. + Parameters are organized inside the resources files into the named groups - sections. + Options are special kind of resoures which allow customizing resource files interpreting. + For example, by default language settings are defined in the resource file in the + section "language". It is possible to change this section name by setting "language" + option to another value (see setOption()). + + Retrieving preferences values can be done by using one of value() methods, each returns + \c true if the corresponding preference is found. Another way is to use integerValue(), + doubleValue(), etc methods, which allow specifying default value which is used if the + specified preference is not found. Removing of preferences or sections can be done using + remove(const QString& sect) or remove(const QString& sect, const QString& name) methods. + To add the preference or to change exiting preference value use setValue() methods family. + Methods hasSection() and hasValue() can be used to check existence of section or + preference (in the specified section). List of all sections can be retrieved with the + sections() method, and list of all settings names in some specified section can be + obtained with parameters() method. + + Pixmaps can be loaded with the loadPixmap() methods. If the specified pixmap is not found, + the default one is returned. Default pixmap can be set by setDefaultPixmap(). + + One of the key feature of the resources manager is support of application + internationalization mechanism. Translation files for the specified language can be loaded + with loadLanguage() method. +*/ + +/*! + \brief Constructs the resource manager. + \param appName application name + \param resVarTemplate resource environment variable pattern */ QtxResourceMgr::QtxResourceMgr( const QString& appName, const QString& resVarTemplate ) : myAppName( appName ), @@ -856,14 +1125,14 @@ QtxResourceMgr::QtxResourceMgr( const QString& appName, const QString& resVarTem envVar = envVar.arg( appName ); QString dirs; - if ( ::getenv( envVar ) ) - dirs = ::getenv( envVar ); + if ( ::getenv( envVar.toLatin1() ) ) + dirs = ::getenv( envVar.toLatin1() ); #ifdef WIN32 QString dirsep = ";"; // for Windows: ";" is used as directories separator #else QString dirsep = "[:|;]"; // for Linux: both ":" and ";" can be used #endif - setDirList( QStringList::split( QRegExp(dirsep), dirs ) ); + setDirList( dirs.split( QRegExp( dirsep ), QString::SkipEmptyParts ) ); installFormat( new XmlFormat() ); installFormat( new IniFormat() ); @@ -872,22 +1141,25 @@ QtxResourceMgr::QtxResourceMgr( const QString& appName, const QString& resVarTem } /*! - \brief Destructs the resource manager object and free allocated memory. + \brief Destructor. + + Destroy the resource manager and free allocated memory. */ QtxResourceMgr::~QtxResourceMgr() { QStringList prefList = myTranslator.keys(); for ( QStringList::const_iterator it = prefList.begin(); it != prefList.end(); ++it ) removeTranslators( *it ); - for ( ResListIterator resIt( myResources ); resIt.current(); ++resIt ) - delete resIt.current(); + for ( ResList::iterator resIt = myResources.begin(); resIt != myResources.end(); ++resIt ) + delete *resIt; myResources.clear(); - for ( FormatListIterator formIt( myFormats ); formIt.current(); ++formIt ) - delete formIt.current(); + for ( FormatList::iterator formIt = myFormats.begin(); formIt != myFormats.end(); ++formIt ) + delete *formIt; } /*! - \brief Returns the application name. + \brief Get the application name. + \return application name */ QString QtxResourceMgr::appName() const { @@ -895,9 +1167,12 @@ QString QtxResourceMgr::appName() const } /*! - \brief Returns the checking of the existance flag. If its 'true' then resource - will be setted into the manager only if it doesn't exist or has different - value that existing value. + \brief Get the "check existance" flag + + If this flag is \c true then preference can be set (with setValue() method) + only if it doesn't exist or if the value is changed. + + \return \c true if "check existance" flag is set */ bool QtxResourceMgr::checkExisting() const { @@ -905,8 +1180,8 @@ bool QtxResourceMgr::checkExisting() const } /*! - \brief Sets the checking of the existance flag. - \param on - boolean value of the flag. + \brief Set the "check existance" flag. + \param on new flag value */ void QtxResourceMgr::setCheckExisting( const bool on ) { @@ -914,7 +1189,13 @@ void QtxResourceMgr::setCheckExisting( const bool on ) } /*! - \brief Returns the resource directories list except user home directory. + \brief Get the resource directories list. + + Home user directory (where the user application configuration file is situated) + is not included. This is that directories list defined by the application + resources environment variable. + + \return list of directories names */ QStringList QtxResourceMgr::dirList() const { @@ -922,8 +1203,11 @@ QStringList QtxResourceMgr::dirList() const } /*! - \brief Initialise the manager. Prepare the resource containers and load resources. - \param autoLoad - if 'true' then all resources will be loaded. + \brief Initialise resources manager. + + Prepare the resources containers and load resources (if \a autoLoad is \c true). + + \param autoLoad if \c true then all resources are loaded */ void QtxResourceMgr::initialize( const bool autoLoad ) const { @@ -933,12 +1217,12 @@ void QtxResourceMgr::initialize( const bool autoLoad ) const QtxResourceMgr* that = (QtxResourceMgr*)this; if ( !userFileName( appName() ).isEmpty() ) - that->myResources.append( new Resources( this, userFileName( appName() ) ) ); + that->myResources.append( new Resources( that, userFileName( appName() ) ) ); for ( QStringList::const_iterator it = myDirList.begin(); it != myDirList.end(); ++it ) { QString path = Qtx::addSlash( *it ) + globalFileName( appName() ); - that->myResources.append( new Resources( this, path ) ); + that->myResources.append( new Resources( that, path ) ); } if ( autoLoad ) @@ -946,7 +1230,15 @@ void QtxResourceMgr::initialize( const bool autoLoad ) const } /*! - \brief Return true if all loaded pixmaps are stored in internal map; by default: true + \brief Get "cached pixmaps" option value. + + Resources manager allows possibility to cache loaded pixmaps that allow to + improve application performance. This feature is turned on by default - all + loaded pixmaps are stored in the internal map. Switching of this feature on/off + can be done by setIsPixmapCached() method. + + \return \c true if pixmap cache is turned on + \sa setIsPixmapCached() */ bool QtxResourceMgr::isPixmapCached() const { @@ -954,8 +1246,9 @@ bool QtxResourceMgr::isPixmapCached() const } /*! - \brief Set true, if it is necessary to store all loaded pixmap in internal map - (it accelerates following calls of loadPixmap) + \brief Switch "cached pixmaps" option on/off. + \param on enable pixmap cache if \c true and disable it if \c false + \sa isPixmapCached() */ void QtxResourceMgr::setIsPixmapCached( const bool on ) { @@ -963,17 +1256,21 @@ void QtxResourceMgr::setIsPixmapCached( const bool on ) } /*! - \brief Removes all resources from the manager. + \brief Remove all resources from the resources manager. */ void QtxResourceMgr::clear() { - for ( ResListIterator it( myResources ); it.current(); ++it ) - it.current()->clear(); + for ( ResList::iterator it = myResources.begin(); it != myResources.end(); ++it ) + (*it)->clear(); } /*! - Set state 'ignore user values'. - If it is true, then all resources loaded from user home directory is ignored + \brief Set "ignore user values" option value. + + If this option is \c true, then all resources loaded from user home directory are ignored. + + \param val new option value + \sa ignoreUserValues() */ void QtxResourceMgr::setIgnoreUserValues( const bool val ) { @@ -981,7 +1278,10 @@ void QtxResourceMgr::setIgnoreUserValues( const bool val ) } /*! - \return state 'ignore user values' + \brief Get "ignore user values" option value. + + \return "ignore user values" option value + \sa setIgnoreUserValues() */ bool QtxResourceMgr::ignoreUserValues() const { @@ -989,11 +1289,12 @@ bool QtxResourceMgr::ignoreUserValues() const } /*! - \brief Get the resource value as integer. Returns 'true' if it successfull otherwise - returns 'false'. - \param sect - Resource section name which contains resource. - \param name - Name of the resource. - \param iVal - Reference on the variable which should contains the resource output. + \brief Get interger parameter value. + \param sect section name + \param name parameter name + \param iVal parameter to return resulting integer value + \return \c true if parameter is found and \c false if parameter is not found + (in this case \a iVal value is undefined) */ bool QtxResourceMgr::value( const QString& sect, const QString& name, int& iVal ) const { @@ -1008,11 +1309,12 @@ bool QtxResourceMgr::value( const QString& sect, const QString& name, int& iVal } /*! - \brief Get the resource value as double. Returns 'true' if it successfull otherwise - returns 'false'. - \param sect - Resource section name which contains resource. - \param name - Name of the resource. - \param dVal - Reference on the variable which should contains the resource output. + \brief Get double parameter value. + \param sect section name + \param name parameter name + \param dVal parameter to return resulting double value + \return \c true if parameter is found and \c false if parameter is not found + (in this case \a dVal value is undefined) */ bool QtxResourceMgr::value( const QString& sect, const QString& name, double& dVal ) const { @@ -1027,11 +1329,12 @@ bool QtxResourceMgr::value( const QString& sect, const QString& name, double& dV } /*! - \brief Get the resource value as boolean. Returns 'true' if it successfull otherwise - returns 'false'. - \param sect - Resource section name which contains resource. - \param name - Name of the resource. - \param bVal - Reference on the variable which should contains the resource output. + \brief Get boolean parameter value. + \param sect section name + \param name parameter name + \param bVal parameter to return resulting boolean value + \return \c true if parameter is found and \c false if parameter is not found + (in this case \a bVal value is undefined) */ bool QtxResourceMgr::value( const QString& sect, const QString& name, bool& bVal ) const { @@ -1046,7 +1349,7 @@ bool QtxResourceMgr::value( const QString& sect, const QString& name, bool& bVal boolMap["false"] = boolMap["no"] = boolMap["off"] = false; } - val = val.lower(); + val = val.toLower(); bool res = boolMap.contains( val ); if ( res ) bVal = boolMap[val]; @@ -1061,11 +1364,12 @@ bool QtxResourceMgr::value( const QString& sect, const QString& name, bool& bVal } /*! - \brief Get the resource value as color. Returns 'true' if it successfull otherwise - returns 'false'. - \param sect - Resource section name which contains resource. - \param name - Name of the resource. - \param cVal - Reference on the variable which should contains the resource output. + \brief Get color parameter value. + \param sect section name + \param name parameter name + \param cVal parameter to return resulting color value + \return \c true if parameter is found and \c false if parameter is not found + (in this case \a cVal value is undefined) */ bool QtxResourceMgr::value( const QString& sect, const QString& name, QColor& cVal ) const { @@ -1074,30 +1378,47 @@ bool QtxResourceMgr::value( const QString& sect, const QString& name, QColor& cV return false; bool res = true; - QStringList vals = QStringList::split( ",", val, true ); + QStringList vals = val.split( QRegExp( "[\\s|,]" ), QString::SkipEmptyParts ); QIntList nums; for ( QStringList::const_iterator it = vals.begin(); it != vals.end() && res; ++it ) - nums.append( (*it).toInt( &res ) ); + { + int num = 0; + if ( (*it).startsWith( "#" ) ) + num = (*it).mid( 1 ).toInt( &res, 16 ); + else + num = (*it).toInt( &res, 10 ); + if ( res ) + nums.append( num ); + } - if ( res && nums.count() >= 3 ) + res = res && nums.count() >= 3; + if ( res ) cVal.setRgb( nums[0], nums[1], nums[2] ); - else + + if ( !res ) { int pack = val.toInt( &res ); if ( res ) - Qtx::rgbSet( pack, cVal ); + cVal = Qtx::rgbSet( pack ); + } + + if ( !res ) + { + cVal = QColor( val ); + res = cVal.isValid(); } return res; } /*! - \brief Get the resource value as font. Returns 'true' if it successfull otherwise - returns 'false'. - \param sect - Resource section name which contains resource. - \param name - Name of the resource. - \param fVal - Reference on the variable which should contains the resource output. + \brief Get font parameter value. + \param sect section name + \param name parameter name + \param fVal parameter to return resulting font value + \return \c true if parameter is found and \c false if parameter is not found + (in this case \a fVal value is undefined) */ bool QtxResourceMgr::value( const QString& sect, const QString& name, QFont& fVal ) const { @@ -1105,7 +1426,7 @@ bool QtxResourceMgr::value( const QString& sect, const QString& name, QFont& fVa if ( !value( sect, name, val, true ) ) return false; - QStringList fontDescr = QStringList::split( ",", val ); + QStringList fontDescr = val.split( ",", QString::SkipEmptyParts ); if ( fontDescr.count() < 2 ) return false; @@ -1118,7 +1439,7 @@ bool QtxResourceMgr::value( const QString& sect, const QString& name, QFont& fVa for ( int i = 1; i < (int)fontDescr.count(); i++ ) { - QString curval = fontDescr[i].stripWhiteSpace().lower(); + QString curval = fontDescr[i].trimmed().toLower(); if ( curval == QString( "bold" ) ) fVal.setBold( true ); else if ( curval == QString( "italic" ) ) @@ -1138,14 +1459,48 @@ bool QtxResourceMgr::value( const QString& sect, const QString& name, QFont& fVa } /*! - \brief Get the resource value as string (native format). Returns 'true' if it - successfull otherwise returns 'false'. - \param sect - Resource section name which contains resource. - \param name - Name of the resource. - \param val - Reference on the variable which should contains the resource output. - \param subst - If 'true' then manager substitute reference on environment variables - and other resources by thier values. Default value of this parameter - is 'true' + \brief Get byte array parameter value. + \param sect section name + \param name parameter name + \param baVal parameter to return resulting byte array value + \return \c true if parameter is found and \c false if parameter is not found + (in this case \a baVal value is undefined) +*/ +bool QtxResourceMgr::value( const QString& sect, const QString& name, QByteArray& baVal ) const +{ + QString val; + if ( !value( sect, name, val, true ) ) + return false; + + baVal.clear(); + QStringList lst = val.split( QRegExp( "[\\s|,]" ), QString::SkipEmptyParts ); + for ( QStringList::const_iterator it = lst.begin(); it != lst.end(); ++it ) + { + int base = 10; + QString str = *it; + if ( str.startsWith( "#" ) ) + { + base = 16; + str = str.mid( 1 ); + } + bool ok = false; + int num = str.toInt( &ok, base ); + if ( !ok || num < 0 || num > 255 ) + continue; + + baVal.append( (char)num ); + } + return !baVal.isEmpty(); +} + +/*! + \brief Get string parameter value (native format). + \param sect section name + \param name parameter name + \param val parameter to return resulting byte array value + \param subst if \c true perform environment variables substitution + \return \c true if parameter is found and \c false if parameter is not found + (in this case \a val value is undefined) */ bool QtxResourceMgr::value( const QString& sect, const QString& name, QString& val, const bool subst ) const { @@ -1153,26 +1508,30 @@ bool QtxResourceMgr::value( const QString& sect, const QString& name, QString& v bool ok = false; - ResListIterator it( myResources ); + ResList::const_iterator it = myResources.begin(); if ( ignoreUserValues() ) ++it; - for ( ; it.current() && !ok; ++it ) + for ( ; it != myResources.end() && !ok; ++it ) { - ok = it.current()->hasValue( sect, name ); + ok = (*it)->hasValue( sect, name ); if ( ok ) - val = it.current()->value( sect, name, subst ); + val = (*it)->value( sect, name, subst ); } return ok; } /*! - \brief Returns the integer resource value. If resource can not be found or converted - then specified default value will be returned. - \param sect - Resource section name which contains resource. - \param name - Name of the resource. - \param def - Default resource value which will be used when resource not found. + \brief Get interger parameter value. + + If the specified parameter is not found or can not be converted to the integer value, + the specified default value is returned instead. + + \param sect section name + \param name parameter name + \param def default value + \return parameter value (or default value if parameter is not found) */ int QtxResourceMgr::integerValue( const QString& sect, const QString& name, const int def ) const { @@ -1183,11 +1542,15 @@ int QtxResourceMgr::integerValue( const QString& sect, const QString& name, cons } /*! - \brief Returns the double resource value. If resource can not be found or converted - then specified default value will be returned. - \param sect - Resource section name which contains resource. - \param name - Name of the resource. - \param def - Default resource value which will be used when resource not found. + \brief Get double parameter value. + + If the specified parameter is not found or can not be converted to the double value, + the specified default value is returned instead. + + \param sect section name + \param name parameter name + \param def default value + \return parameter value (or default value if parameter is not found) */ double QtxResourceMgr::doubleValue( const QString& sect, const QString& name, const double def ) const { @@ -1198,11 +1561,15 @@ double QtxResourceMgr::doubleValue( const QString& sect, const QString& name, co } /*! - \brief Returns the boolean resource value. If resource can not be found or converted - then specified default value will be returned. - \param sect - Resource section name which contains resource. - \param name - Name of the resource. - \param def - Default resource value which will be used when resource not found. + \brief Get boolean parameter value. + + If the specified parameter is not found or can not be converted to the boolean value, + the specified default value is returned instead. + + \param sect section name + \param name parameter name + \param def default value + \return parameter value (or default value if parameter is not found) */ bool QtxResourceMgr::booleanValue( const QString& sect, const QString& name, const bool def ) const { @@ -1213,11 +1580,15 @@ bool QtxResourceMgr::booleanValue( const QString& sect, const QString& name, con } /*! - \brief Returns the font resource value. If resource can not be found or converted - then specified default value will be returned. - \param sect - Resource section name which contains resource. - \param name - Name of the resource. - \param def - Default resource value which will be used when resource not found. + \brief Get font parameter value. + + If the specified parameter is not found or can not be converted to the font value, + the specified default value is returned instead. + + \param sect section name + \param name parameter name + \param def default value + \return parameter value (or default value if parameter is not found) */ QFont QtxResourceMgr::fontValue( const QString& sect, const QString& name, const QFont& def ) const { @@ -1228,11 +1599,15 @@ QFont QtxResourceMgr::fontValue( const QString& sect, const QString& name, const } /*! - \brief Returns the color resource value. If resource can not be found or converted - then specified default value will be returned. - \param sect - Resource section name which contains resource. - \param name - Name of the resource. - \param def - Default resource value which will be used when resource not found. + \brief Get color parameter value. + + If the specified parameter is not found or can not be converted to the color value, + the specified default value is returned instead. + + \param sect section name + \param name parameter name + \param def default value + \return parameter value (or default value if parameter is not found) */ QColor QtxResourceMgr::colorValue( const QString& sect, const QString& name, const QColor& def ) const { @@ -1243,11 +1618,14 @@ QColor QtxResourceMgr::colorValue( const QString& sect, const QString& name, con } /*! - \brief Returns the string resource value. If resource can not be found or converted - then specified default value will be returned. - \param sect - Resource section name which contains resource. - \param name - Name of the resource. - \param def - Default resource value which will be used when resource not found. + \brief Get string parameter value. + + If the specified parameter is not found, the specified default value is returned instead. + + \param sect section name + \param name parameter name + \param def default value + \return parameter value (or default value if parameter is not found) */ QString QtxResourceMgr::stringValue( const QString& sect, const QString& name, const QString& def ) const { @@ -1258,41 +1636,61 @@ QString QtxResourceMgr::stringValue( const QString& sect, const QString& name, c } /*! - \brief Checks existance of the specified resource. - \param sect - Resource section name which contains resource. - \param name - Name of the resource. + \brief Get byte array parameter value. + + If the specified parameter is not found, the specified default value is returned instead. + + \param sect section name + \param name parameter name + \param def default value + \return parameter value (or default value if parameter is not found) +*/ +QByteArray QtxResourceMgr::byteArrayValue( const QString& sect, const QString& name, const QByteArray& def ) const +{ + QByteArray val; + if ( !value( sect, name, val ) ) + val = def; + return val; +} + +/*! + \brief Check parameter existence. + \param sect section name + \param name parameter name + \return \c true if parameter exists in specified section */ bool QtxResourceMgr::hasValue( const QString& sect, const QString& name ) const { initialize(); bool ok = false; - for ( ResListIterator it( myResources ); it.current() && !ok; ++it ) - ok = it.current()->hasValue( sect, name ); + for ( ResList::const_iterator it = myResources.begin(); it != myResources.end() && !ok; ++it ) + ok = (*it)->hasValue( sect, name ); return ok; } /*! - \brief Checks existance of the specified resource section. - \param sect - Resource section name which contains resource. + \brief Check section existence. + \param sect section name + \return \c true if section exists */ bool QtxResourceMgr::hasSection( const QString& sect ) const { initialize(); bool ok = false; - for ( ResListIterator it( myResources ); it.current() && !ok; ++it ) - ok = it.current()->hasSection( sect ); + for ( ResList::const_iterator it = myResources.begin(); it != myResources.end() && !ok; ++it ) + ok = (*it)->hasSection( sect ); return ok; } /*! - \brief Sets the integer resource value. - \param sect - Resource section name. - \param name - Name of the resource. - \param val - Resource value. + \brief Set integer parameter value. + \param sect section name + \param name parameter name + \param val parameter value */ void QtxResourceMgr::setValue( const QString& sect, const QString& name, int val ) { @@ -1304,10 +1702,10 @@ void QtxResourceMgr::setValue( const QString& sect, const QString& name, int val } /*! - \brief Sets the double resource value. - \param sect - Resource section name. - \param name - Name of the resource. - \param val - Resource value. + \brief Set double parameter value. + \param sect section name + \param name parameter name + \param val parameter value */ void QtxResourceMgr::setValue( const QString& sect, const QString& name, double val ) { @@ -1319,10 +1717,10 @@ void QtxResourceMgr::setValue( const QString& sect, const QString& name, double } /*! - \brief Sets the boolean resource value. - \param sect - Resource section name. - \param name - Name of the resource. - \param val - Resource value. + \brief Set boolean parameter value. + \param sect section name + \param name parameter name + \param val parameter value */ void QtxResourceMgr::setValue( const QString& sect, const QString& name, bool val ) { @@ -1334,10 +1732,10 @@ void QtxResourceMgr::setValue( const QString& sect, const QString& name, bool va } /*! - \brief Sets the color resource value. - \param sect - Resource section name. - \param name - Name of the resource. - \param val - Resource value. + \brief Set color parameter value. + \param sect section name + \param name parameter name + \param val parameter value */ void QtxResourceMgr::setValue( const QString& sect, const QString& name, const QColor& val ) { @@ -1345,14 +1743,14 @@ void QtxResourceMgr::setValue( const QString& sect, const QString& name, const Q if ( checkExisting() && value( sect, name, res ) && res == val ) return; - setResource( sect, name, QString( "%1, %2, %3" ).arg( val.red() ).arg( val.green() ).arg( val.blue() ) ); + setResource( sect, name, val.isValid() ? val.name() : QString() ); } /*! - \brief Sets the font resource value. - \param sect - Resource section name. - \param name - Name of the resource. - \param val - Resource value. + \brief Set font parameter value. + \param sect section name + \param name parameter name + \param val parameter value */ void QtxResourceMgr::setValue( const QString& sect, const QString& name, const QFont& val ) { @@ -1374,10 +1772,10 @@ void QtxResourceMgr::setValue( const QString& sect, const QString& name, const Q } /*! - \brief Sets the string resource value. - \param sect - Resource section name. - \param name - Name of the resource. - \param val - Resource value. + \brief Set string parameter value. + \param sect section name + \param name parameter name + \param val parameter value */ void QtxResourceMgr::setValue( const QString& sect, const QString& name, const QString& val ) { @@ -1389,44 +1787,68 @@ void QtxResourceMgr::setValue( const QString& sect, const QString& name, const Q } /*! - \brief Remove the all specified resource section. - \param sect - Resource section name. + \brief Set byte array parameter value. + \param sect section name + \param name parameter name + \param val parameter value +*/ +void QtxResourceMgr::setValue( const QString& sect, const QString& name, const QByteArray& val ) +{ + QByteArray res; + if ( checkExisting() && value( sect, name, res ) && res == val ) + return; + + char buf[8]; + QStringList lst; + for ( int i = 0; i < val.size(); i++ ) + { + ::sprintf( buf, "#%02X", val.at( i ) ); + lst.append( QString( buf ) ); + } + + setResource( sect, name, lst.join( " " ) ); +} + +/*! + \brief Remove resources section. + \param sect section name */ void QtxResourceMgr::remove( const QString& sect ) { initialize(); - for ( ResListIterator it( myResources ); it.current(); ++it ) - it.current()->removeSection( sect ); + for ( ResList::iterator it = myResources.begin(); it != myResources.end(); ++it ) + (*it)->removeSection( sect ); } /*! - \brief Remove the specified resource. - \param sect - Resource section name. - \param name - Name of the resource. + \brief Remove the specified parameter. + \param sect section name + \param name parameter name */ void QtxResourceMgr::remove( const QString& sect, const QString& name ) { initialize(); - for ( ResListIterator it( myResources ); it.current(); ++it ) - it.current()->removeValue( sect, name ); + for ( ResList::iterator it = myResources.begin(); it != myResources.end(); ++it ) + (*it)->removeValue( sect, name ); } /*! - \brief Returns the current format which operates with resource files. + \brief Get current configuration files format. + \return configuration files format name */ QString QtxResourceMgr::currentFormat() const { QString fmt; if ( !myFormats.isEmpty() ) - fmt = myFormats.getFirst()->format(); + fmt = myFormats[0]->format(); return fmt; } /*! - \brief Sets the current format which operates with resource files. - \param fmt - Resource format name. + \brief Set current configuration files format. + \param fmt configuration files format name */ void QtxResourceMgr::setCurrentFormat( const QString& fmt ) { @@ -1434,41 +1856,46 @@ void QtxResourceMgr::setCurrentFormat( const QString& fmt ) if ( !form ) return; - myFormats.remove( form ); + myFormats.removeAll( form ); myFormats.prepend( form ); if ( myResources.isEmpty() ) return; - ResListIterator resIt( myResources ); - if ( myResources.count() > myDirList.count() && resIt.current() ) { - resIt.current()->setFile( userFileName( appName() ) ); + ResList::iterator resIt = myResources.begin(); + if ( myResources.count() > myDirList.count() && resIt != myResources.end() ) + { + (*resIt)->setFile( userFileName( appName() ) ); ++resIt; } - for ( QStringList::const_iterator it = myDirList.begin(); it != myDirList.end() && resIt.current(); ++it, ++resIt ) - resIt.current()->setFile( Qtx::addSlash( *it ) + globalFileName( appName() ) ); + for ( QStringList::const_iterator it = myDirList.begin(); it != myDirList.end() && resIt != myResources.end(); ++it, ++resIt ) + (*resIt)->setFile( Qtx::addSlash( *it ) + globalFileName( appName() ) ); } /*! - \brief Returns the resource format object by it name. - \param fmt - Resource format name. + \brief Get configuration files format by specified format name. + \param fmt configuration files format name + \return format object or 0 if format is not defined */ QtxResourceMgr::Format* QtxResourceMgr::format( const QString& fmt ) const { Format* form = 0; - for ( FormatListIterator it( myFormats ); it.current() && !form; ++it ) + for ( FormatList::const_iterator it = myFormats.begin(); it != myFormats.end() && !form; ++it ) { - if ( it.current()->format() == fmt ) - form = it.current(); + if ( (*it)->format() == fmt ) + form = *it; } return form; } /*! - \brief Add the resource format to the manager. Newly added become current. - \param form - Resource format object. + \brief Install configuration files format. + + Added format becomes current. + + \param form format object to be installed */ void QtxResourceMgr::installFormat( QtxResourceMgr::Format* form ) { @@ -1477,16 +1904,17 @@ void QtxResourceMgr::installFormat( QtxResourceMgr::Format* form ) } /*! - \brief Remove the resource format from the manager. - \param form - Resource format object. + \brief Remove configuration files format. + \param form format object to be uninstalled */ void QtxResourceMgr::removeFormat( QtxResourceMgr::Format* form ) { - myFormats.remove( form ); + myFormats.removeAll( form ); } /*! - \brief Returns the string list of the resource format options names. + \brief Get resource format options names. + \return list of options names */ QStringList QtxResourceMgr::options() const { @@ -1494,9 +1922,13 @@ QStringList QtxResourceMgr::options() const } /*! - \brief Returns the string value for the specified option. If option doesn't exist - then empty string will be returned. - \param opt - Option name. + \brief Get the string value of the specified resources format option. + + If option does not exist, null QString is returned. + + \param opt option name + \return option value + \sa setOption(), options() */ QString QtxResourceMgr::option( const QString& opt ) const { @@ -1507,9 +1939,10 @@ QString QtxResourceMgr::option( const QString& opt ) const } /*! - \brief Sets the string value for the specified option. - \param opt - Option name. - \param val - Option value. + \brief Set the string value of the specified resources format option. + \param opt option name + \param val option value + \sa option(), options() */ void QtxResourceMgr::setOption( const QString& opt, const QString& val ) { @@ -1517,7 +1950,9 @@ void QtxResourceMgr::setOption( const QString& opt, const QString& val ) } /*! - \brief Load the all resources from the resource files. + \brief Load all resources from all resource files (global and user). + \return \c true on success and \c false on error + \sa save() */ bool QtxResourceMgr::load() { @@ -1528,14 +1963,16 @@ bool QtxResourceMgr::load() return false; bool res = true; - for ( ResListIterator it( myResources ); it.current(); ++it ) - res = fmt->load( it.current() ) && res; + for ( ResList::iterator it = myResources.begin(); it != myResources.end(); ++it ) + res = fmt->load( *it ) && res; return res; } /*! - \brief Import some file with resources + \brief Import resources from specified resource file. + \param fname resources file name + \return \c true on success and \c false on error */ bool QtxResourceMgr::import( const QString& fname ) { @@ -1543,8 +1980,8 @@ bool QtxResourceMgr::import( const QString& fname ) if ( !fmt ) return false; - Resources* r = myResources.getFirst(); - if( !r ) + Resources* r = myResources[0]; + if ( !r ) return false; QString old = r->file(); @@ -1555,7 +1992,8 @@ bool QtxResourceMgr::import( const QString& fname ) } /*! - \brief Save the changed resources in to the user resource file. + \brief Save all resources to the user resource files. + \return \c true on success and \c false on error */ bool QtxResourceMgr::save() { @@ -1568,20 +2006,21 @@ bool QtxResourceMgr::save() if ( myResources.isEmpty() ) return true; - return fmt->save( myResources.getFirst() ); + return fmt->save( myResources[0] ); } /*! - \brief Returns the string list of the existing section names.. + \brief Get all sections names. + \return list of section names */ QStringList QtxResourceMgr::sections() const { initialize(); QMap map; - for ( ResListIterator it( myResources ); it.current(); ++it ) + for ( ResList::const_iterator it = myResources.begin(); it != myResources.end(); ++it ) { - QStringList lst = it.current()->sections(); + QStringList lst = (*it)->sections(); for ( QStringList::const_iterator itr = lst.begin(); itr != lst.end(); ++itr ) map.insert( *itr, 0 ); } @@ -1594,8 +2033,9 @@ QStringList QtxResourceMgr::sections() const } /*! - \brief Returns the string list of the existing resource names in the specified section. - \param sec - Resource section name. + \brief Get all parameters name in specified section. + \param sec section name + \return list of settings names */ QStringList QtxResourceMgr::parameters( const QString& sec ) const { @@ -1607,10 +2047,13 @@ QStringList QtxResourceMgr::parameters( const QString& sec ) const typedef IMap PMap; #endif PMap pmap; - ResListIterator it( myResources ); - it.toLast(); - for ( ; it.current(); --it ) { - QStringList lst = it.current()->parameters( sec ); + ResList lst; + for ( ResList::const_iterator itr = myResources.begin(); itr != myResources.end(); ++itr ) + lst.prepend( *itr ); + + for ( ResList::const_iterator it = lst.begin(); it != lst.end(); ++it ) + { + QStringList lst = (*it)->parameters( sec ); for ( QStringList::const_iterator itr = lst.begin(); itr != lst.end(); ++itr ) pmap.insert( *itr, 0, false ); } @@ -1623,22 +2066,35 @@ QStringList QtxResourceMgr::parameters( const QString& sec ) const } /*! - \return path of file from directory built by parameter - \return QString::null if built path doesn't exist - \param sec - name of section - \param prefix - name of parameter containing some path - \param name - name of file + \brief Get absolute path to the file which name is defined by the parameter. + + The file name is defined by \a name argument, while directory name is retrieved + from resources parameter \a prefix of section \a sec. Both directory and file name + can be relative. If the directory is relative, it is calculated from the initial + resources file name. Directory parameter can contain environment + variables, which are substituted automatically. + + \param sec section name + \param prefix parameter containing directory name + \param name file name + \return absolute file path or null QString if file does not exist */ QString QtxResourceMgr::path( const QString& sect, const QString& prefix, const QString& name ) const { QString res; - for ( ResListIterator it( myResources ); it.current() && res.isEmpty(); ++it ) - res = it.current()->path( sect, prefix, name ); + for ( ResList::const_iterator it = myResources.begin(); it != myResources.end() && res.isEmpty(); ++it ) + res = (*it)->path( sect, prefix, name ); return res; } /*! - \return section corresponding to resources paths + \brief Get application resources section name. + + By default, application resources section name is "resources" but + it can be changed by setting the corresponding resources manager option. + + \return section corresponding to the resources directories + \sa option(), setOption() */ QString QtxResourceMgr::resSection() const { @@ -1649,7 +2105,13 @@ QString QtxResourceMgr::resSection() const } /*! - \return section corresponding to language settings + \brief Get application language section name. + + By default, application language section name is "language" but + it can be changed by setting the corresponding resources manager option. + + \return section corresponding to the application language settings + \sa option(), setOption() */ QString QtxResourceMgr::langSection() const { @@ -1660,7 +2122,12 @@ QString QtxResourceMgr::langSection() const } /*! - \return default image used when during loading the image file doesn't exist + \brief Get default pixmap. + + Default pixmap is used when requested pixmap resource is not found. + + \return default pixmap + \sa setDefaultPixmap(), loadPixmap() */ QPixmap QtxResourceMgr::defaultPixmap() const { @@ -1668,8 +2135,12 @@ QPixmap QtxResourceMgr::defaultPixmap() const } /*! - Set image as default image used when during loading the image file doesn't exist - \param pix - image + \brief Set default pixmap. + + Default pixmap is used when requested pixmap resource is not found. + + \param pix default pixmap + \sa defaultPixmap(), loadPixmap() */ void QtxResourceMgr::setDefaultPixmap( const QPixmap& pix ) { @@ -1677,9 +2148,11 @@ void QtxResourceMgr::setDefaultPixmap( const QPixmap& pix ) } /*! - \return image loaded from file - \param prefix - name of parameter containing some path - \param name - name of file + \brief Load pixmap resource. + \param prefix parameter which refers to the resources directory (directories) + \param name pixmap file name + \return pixmap loaded from the file + \sa defaultPixmap(), setDefaultPixmap() */ QPixmap QtxResourceMgr::loadPixmap( const QString& prefix, const QString& name ) const { @@ -1687,12 +2160,14 @@ QPixmap QtxResourceMgr::loadPixmap( const QString& prefix, const QString& name ) } /*! - \return image loaded from file - \param prefix - name of parameter containing some path - \param name - name of file - \param useDef - indicates if it is possible to use default image returning by defaultPixmap() method. - If it is false, the empty pixmap will be used as default - \sa defaultPixmap() + \brief Load pixmap resource. + \overload + \param prefix parameter which refers to the resources directory (directories) + \param name pixmap file name + \param useDef if \c false, default pixmap is not returned if resource is not found, + in this case null pixmap is returned instead + \return pixmap loaded from the file + \sa defaultPixmap(), setDefaultPixmap() */ QPixmap QtxResourceMgr::loadPixmap( const QString& prefix, const QString& name, const bool useDef ) const { @@ -1700,41 +2175,46 @@ QPixmap QtxResourceMgr::loadPixmap( const QString& prefix, const QString& name, } /*! - Finds in all sections an existing path corresponding to 'prefix' parameter - and load image with name 'name' from this folder - - \return image loaded from file - \param prefix - name of parameter containing some path - \param name - name of file - \param defPix - default image used when file doesn't exist + \brief Load pixmap resource. + \overload + \param prefix parameter which refers to the resources directory (directories) + \param name pixmap file name + \param defPix default which should be used if the resource file doesn't exist + \return pixmap loaded from the file + \sa defaultPixmap(), setDefaultPixmap() */ QPixmap QtxResourceMgr::loadPixmap( const QString& prefix, const QString& name, const QPixmap& defPix ) const { initialize(); QPixmap pix; - for ( ResListIterator it( myResources ); it.current() && pix.isNull(); ++it ) - pix = it.current()->loadPixmap( resSection(), prefix, name ); + for ( ResList::const_iterator it = myResources.begin(); it != myResources.end() && pix.isNull(); ++it ) + pix = (*it)->loadPixmap( resSection(), prefix, name ); if ( pix.isNull() ) pix = defPix; return pix; } /*! - Loads translator for language - Name of translator file is constructed by list returning by option "translators" or, - if it is empty, by predefined pattern "%P_msg_%L.qm". It is recommended to used in translators - name the strings %A, %P, %L whose will be replaced by application name, prefix and language name correspondingly + \brief Load translation files according to the specified language. + + Names of the translation files are calculated according to the pattern specified + by the "translators" option (this option is read from the section "language" of resources files). + By default, "%P_msg_%L.qm" pattern is used. + Keywords \%A, \%P, \%L in the pattern are substituted by the application name, prefix and language name + correspondingly. + For example, for prefix "SUIT" an language "en", all translation files "SUIT_msg_en.qm" are searched and + loaded. - \param pref - name of parameter containing path to translator's file. - If it is empty, the list of parameters from resource section ( resSection() ) - is used. + If prefix is empty or null string, all translation files specified in the "resources" section of resources + files are loaded (actually, the section is retrieved from resSection() method). + If language is not specified, it is retrieved from the langSection() method, and if the latest is also empty, + by default "en" (English) language is used. - \param l - name of language. If it is empty, then value of parameter "language" - from language section ( langSection() ) is used. If it is also empty, then - predefined name "en" is used + \param pref parameter which defines translation context (for example, package name) + \param l language name - \sa resSection(), langSection() + \sa resSection(), langSection(), loadTranslators() */ void QtxResourceMgr::loadLanguage( const QString& pref, const QString& l ) { @@ -1750,7 +2230,7 @@ void QtxResourceMgr::loadLanguage( const QString& pref, const QString& l ) if ( lang.isEmpty() ) { lang = QString( "en" ); - qWarning( QString( "Language not specified. Assumed: %1" ).arg( lang ) ); + qWarning( "Language not specified. Assumed: %s", (const char*)lang.toLatin1() ); } substMap.insert( 'L', lang ); @@ -1758,19 +2238,21 @@ void QtxResourceMgr::loadLanguage( const QString& pref, const QString& l ) QString trs; if ( value( langSection(), "translators", trs, false ) && !trs.isEmpty() ) { - QStringList translators = QStringList::split( "|", option( "translators" ) ); - QStringList newTranslators = QStringList::split( "|", trs ); - for ( uint i = 0; i < newTranslators.count(); i++ ) - if ( translators.find( newTranslators[i] ) == translators.end() ) + QStringList translators = option( "translators" ).split( "|", QString::SkipEmptyParts ); + QStringList newTranslators = trs.split( "|", QString::SkipEmptyParts ); + for ( int i = 0; i < (int)newTranslators.count(); i++ ) + { + if ( translators.indexOf( newTranslators[i] ) < 0 ) translators += newTranslators[i]; + } setOption( "translators", translators.join( "|" ) ); } - QStringList trList = QStringList::split( "|", option( "translators" ) ); + QStringList trList = option( "translators" ).split( "|", QString::SkipEmptyParts ); if ( trList.isEmpty() ) { trList.append( "%P_msg_%L.qm" ); - qWarning( QString( "Translators not defined. Assumed: %1" ).arg( trList.first() ) ); + qWarning( "Translators not defined. Assumed: %s", (const char*)trList[0].toLatin1() ); } QStringList prefixList; @@ -1786,103 +2268,105 @@ void QtxResourceMgr::loadLanguage( const QString& pref, const QString& l ) QStringList trs; for ( QStringList::const_iterator it = trList.begin(); it != trList.end(); ++it ) - trs.append( substMacro( *it, substMap ).stripWhiteSpace() ); + trs.append( substMacro( *it, substMap ).trimmed() ); loadTranslators( prefix, trs ); } } /*! - Loads translators by path and list of files - - \param prefix - value of this parameter must contain path - \param translators - list of translators' files + \brief Load translation files for the specified translation context. + \param prefix parameter which defines translation context (for example, package name) + \param translators list of translation files + \sa loadLanguage() */ void QtxResourceMgr::loadTranslators( const QString& prefix, const QStringList& translators ) { initialize(); + ResList lst; + for ( ResList::iterator iter = myResources.begin(); iter != myResources.end(); ++iter ) + lst.prepend( *iter ); + QTranslator* trans = 0; - ResListIterator it( myResources ); - it.toLast(); - for ( ; it.current(); --it ) + + for ( ResList::iterator it = lst.begin(); it != lst.end(); ++it ) { for ( QStringList::const_iterator itr = translators.begin(); itr != translators.end(); ++itr ) { - trans = it.current()->loadTranslator( resSection(), prefix, *itr ); + trans = (*it)->loadTranslator( resSection(), prefix, *itr ); if ( trans ) { if ( !myTranslator[prefix].contains( trans ) ) myTranslator[prefix].append( trans ); - qApp->installTranslator( trans ); + QApplication::instance()->installTranslator( trans ); } } } } /*! - Loads translator by path and file name - - \param prefix - value of this parameter must contain path - \param name - name of translator file + \brief Load translation file. + \param prefix parameter which defines translation context (for example, package name) + \param name translator file name + \sa loadLanguage(), loadTranslators() */ void QtxResourceMgr::loadTranslator( const QString& prefix, const QString& name ) { initialize(); QTranslator* trans = 0; - ResListIterator it( myResources ); - it.toLast(); - for ( ; it.current(); --it ) + ResList::iterator it = myResources.end(); + for ( ; it != myResources.begin(); --it ) { - trans = it.current()->loadTranslator( resSection(), prefix, name ); + trans = (*it)->loadTranslator( resSection(), prefix, name ); if ( trans ) { if ( !myTranslator[prefix].contains( trans ) ) myTranslator[prefix].append( trans ); - qApp->installTranslator( trans ); + QApplication::instance()->installTranslator( trans ); } } } /*! - Remove all translators corresponding to prefix - - \param prefix - parameter containing path + \brief Remove all translators corresponding to the specified translation context. + \param prefix parameter which defines translation context (for example, package name) */ void QtxResourceMgr::removeTranslators( const QString& prefix ) { if ( !myTranslator.contains( prefix ) ) return; - for ( TransListIterator it( myTranslator[prefix] ); it.current(); ++it ) + for ( TransList::iterator it = myTranslator[prefix].begin(); it != myTranslator[prefix].end(); ++it ) { - qApp->removeTranslator( it.current() ); - delete it.current(); + QApplication::instance()->removeTranslator( *it ); + delete *it; } myTranslator.remove( prefix ); } /*! - Moves translators corresponding to prefix to the top of translator stack - - \param prefix - parameter containing path + \brief Move all translators corresponding to the specified translation context + to the top of translators stack (increase their priority). + \param prefix parameter which defines translation context (for example, package name) */ void QtxResourceMgr::raiseTranslators( const QString& prefix ) { if ( !myTranslator.contains( prefix ) ) return; - for ( TransListIterator it( myTranslator[prefix] ); it.current(); ++it ) + for ( TransList::iterator it = myTranslator[prefix].begin(); it != myTranslator[prefix].end(); ++it ) { - qApp->removeTranslator( it.current() ); - qApp->installTranslator( it.current() ); + QApplication::instance()->removeTranslator( *it ); + QApplication::instance()->installTranslator( *it ); } } /*! - Copies all resources to user resources, so that they will be saved in user home folder + \brief Copy all parameters to the user resources in order to + saved them lately in the user home folder. */ void QtxResourceMgr::refresh() { @@ -1896,22 +2380,26 @@ void QtxResourceMgr::refresh() } /*! - \brief Sets the resource directories list except user home directory and clear resources + \brief Set the resource directories (where global confguration files are searched). + + This function also clears all currently set resources. + + \param dl directories list */ void QtxResourceMgr::setDirList( const QStringList& dl ) { myDirList = dl; - for ( ResListIterator it( myResources ); it.current(); ++it ) - delete it.current(); + for ( ResList::iterator it = myResources.begin(); it != myResources.end(); ++it ) + delete *it; myResources.clear(); } /*! - Sets resource value - \param sect - name of section - \param name - name of parameter - \param val - string representation of value + \brief Set parameter value. + \param sect section name + \param name parameter name + \param val parameter value */ void QtxResourceMgr::setResource( const QString& sect, const QString& name, const QString& val ) { @@ -1922,15 +2410,27 @@ void QtxResourceMgr::setResource( const QString& sect, const QString& name, cons } /*! - \return name of resource file, which is being found in user home directory - \param appName - name of application - \param for_load - flag indicating that file will be used for loading (true) or for saving(false) - It makes possible to use different resource files for loading and saving + \brief Get user configuration file name. + + This method can be redefined in the successor class to customize the user configuration file name. + User configuration file is always situated in the user's home directory. By default .rc + file is used on Linux (e.g. .MyApprc) and . under Windows (e.g. MyApp.xml). + + Parameter \a for_load (not used in default implementation) specifies the usage mode, i.e. if + user configuration file is opened for reading or writing. This allows customizing a way of application + resources initializing (for example, if the user configuraion file includes version number and there is + no file corresponding to this version in the user's home directory, it could be good idea to try + the configuration file from the previous versions of the application). + + \param appName application name + \param for_load boolean flag indicating that file is opened for loading or saving (not used) + \return user configuration file name + \sa globalFileName() */ QString QtxResourceMgr::userFileName( const QString& appName, const bool /*for_load*/ ) const { QString fileName; - QString pathName = QDir::homeDirPath(); + QString pathName = QDir::homePath(); #ifdef WIN32 fileName = QString( "%1.%2" ).arg( appName ).arg( currentFormat() ); @@ -1945,7 +2445,16 @@ QString QtxResourceMgr::userFileName( const QString& appName, const bool /*for_l } /*! - \return name of resource file, which is being found in all resource directories, except user home + \brief Get global configuration file name. + + This method can be redefined in the successor class to customize the global configuration file name. + Global configuration files are searched in the directories specified by the application resources + environment variable (e.g. MyAppResources). By default . file name is used + (e.g. MyApp.xml). + + \param appName application name + \return global configuration file name + \sa userFileName() */ QString QtxResourceMgr::globalFileName( const QString& appName ) const { @@ -1953,10 +2462,13 @@ QString QtxResourceMgr::globalFileName( const QString& appName ) const } /*! - Replaced substrings by pattern %A, %B, etc by values from map + \brief Perform substitution of the patterns like \%A, \%B, etc by values from the map. + + Used by loadLanguage(). - \param src - string to be processed - \param substMap - map of values for replacing + \param src sring to be processed + \param substMap map of values for replacing + \return processed string */ QString QtxResourceMgr::substMacro( const QString& src, const QMap& substMap ) const { @@ -1965,7 +2477,7 @@ QString QtxResourceMgr::substMacro( const QString& src, const QMap= 0 ) + while ( ( idx = rx.indexIn( trg, idx ) ) >= 0 ) { QChar spec = trg.at( idx + 1 ); QString subst; diff --git a/src/Qtx/QtxResourceMgr.h b/src/Qtx/QtxResourceMgr.h index ff72dd3c2..304e25fdc 100644 --- a/src/Qtx/QtxResourceMgr.h +++ b/src/Qtx/QtxResourceMgr.h @@ -16,29 +16,32 @@ // // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com // +// File: QtxResourceMgr.h +// Author: Alexander SOLOVYEV, Sergey TELKOV + #ifndef QTX_RESOURCEMGR_H #define QTX_RESOURCEMGR_H #include "Qtx.h" -#include -#include -#include -#include -#include -#include -#include +#ifndef QTX_NO_INDEXED_MAP +#include "QtxMap.h" +#endif + +#include +#include +#include +#include +#include +#include +#include -class QPixmap; +class QTranslator; #ifdef WIN32 #pragma warning( disable:4251 ) #endif -/*! - Class: QtxResourceMgr -*/ - class QTX_EXPORT QtxResourceMgr { class IniFormat; @@ -48,14 +51,10 @@ class QTX_EXPORT QtxResourceMgr public: class Format; - template class IMap; - template class IMapIterator; - template class IMapConstIterator; - #ifdef QTX_NO_INDEXED_MAP - typedef QMap Section; + typedef QMap Section; //!< resource section #else - typedef IMap Section; + typedef IMap Section; //!< resource section #endif public: @@ -81,6 +80,7 @@ public: bool value( const QString&, const QString&, bool& ) const; bool value( const QString&, const QString&, QColor& ) const; bool value( const QString&, const QString&, QFont& ) const; + bool value( const QString&, const QString&, QByteArray& ) const; bool value( const QString&, const QString&, QString&, const bool = true ) const; int integerValue( const QString&, const QString&, const int = 0 ) const; @@ -89,6 +89,7 @@ public: QFont fontValue( const QString&, const QString&, const QFont& = QFont() ) const; QColor colorValue( const QString&, const QString&, const QColor& = QColor() ) const; QString stringValue( const QString&, const QString&, const QString& = QString::null ) const; + QByteArray byteArrayValue( const QString&, const QString&, const QByteArray& = QByteArray() ) const; bool hasSection( const QString& ) const; bool hasValue( const QString&, const QString& ) const; @@ -99,6 +100,7 @@ public: void setValue( const QString&, const QString&, const QFont& ); void setValue( const QString&, const QString&, const QColor& ); void setValue( const QString&, const QString&, const QString& ); + void setValue( const QString&, const QString&, const QByteArray& ); void remove( const QString& ); void remove( const QString&, const QString& ); @@ -153,36 +155,28 @@ private: QString substMacro( const QString&, const QMap& ) const; private: - typedef QPtrList ResList; - typedef QPtrList FormatList; - typedef QMap OptionsMap; - typedef QPtrListIterator ResListIterator; - typedef QPtrListIterator FormatListIterator; - - typedef QPtrList TransList; - typedef QMap TransListMap; - typedef QPtrListIterator TransListIterator; + typedef QList ResList; + typedef QList TransList; + typedef QList FormatList; + typedef QMap OptionsMap; + typedef QMap TransListMap; private: - QString myAppName; - QStringList myDirList; - FormatList myFormats; - OptionsMap myOptions; - ResList myResources; - bool myCheckExist; - TransListMap myTranslator; - QPixmap myDefaultPix; - bool myIsPixmapCached; - - bool myIsIgnoreUserValues; + QString myAppName; //!< application name + QStringList myDirList; //!< list of resources directories + FormatList myFormats; //!< list of formats + OptionsMap myOptions; //!< options map + ResList myResources; //!< resources list + bool myCheckExist; //!< "check existance" flag + TransListMap myTranslator; //!< map of loaded translators + QPixmap myDefaultPix; //!< default icon + bool myIsPixmapCached; //!< "cached pixmaps" flag + + bool myIsIgnoreUserValues; //!< "ignore user values" flag friend class QtxResourceMgr::Format; }; -/*! - Class: QtxResourceMgr::Format -*/ - class QTX_EXPORT QtxResourceMgr::Format { public: @@ -203,248 +197,8 @@ protected: virtual bool save( const QString&, const QMap& ) = 0; private: - QString myFmt; - QMap myOpt; -}; - -/*! - Class: QtxResourceMgr::Resources -*/ - -class QtxResourceMgr::Resources -{ -public: - Resources( const QtxResourceMgr*, const QString& ); - virtual ~Resources(); - - QString file() const; - void setFile( const QString& ); - - QString value( const QString&, const QString&, const bool ) const; - void setValue( const QString&, const QString&, const QString& ); - - bool hasSection( const QString& ) const; - bool hasValue( const QString&, const QString& ) const; - - void removeSection( const QString& ); - void removeValue( const QString&, const QString& ); - - QPixmap loadPixmap( const QString&, const QString&, const QString& ) const; - QTranslator* loadTranslator( const QString&, const QString&, const QString& ) const; - - QString environmentVariable( const QString&, int&, int& ) const; - QString makeSubstitution( const QString&, const QString&, const QString& ) const; - - void clear(); - - QStringList sections() const; - QStringList parameters( const QString& ) const; - - QString path( const QString&, const QString&, const QString& ) const; - -protected: - QtxResourceMgr* resMgr() const; - -private: - Section& section( const QString& ); - const Section& section( const QString& ) const; - - QString fileName( const QString&, const QString&, const QString& ) const; - -private: - typedef QMap SectionMap; - -private: - SectionMap mySections; - QString myFileName; - QMap myPixmapCache; - QtxResourceMgr* myMgr; - - friend class QtxResourceMgr::Format; -}; - -/*! - Class: QtxResourceMgr::IMapIterator -*/ - -template class QtxResourceMgr::IMapIterator -{ -public: - IMapIterator() : myMap( 0 ), myIndex( 0 ) { init(); } - IMapIterator( const IMap* m ) : myMap( const_cast< IMap* >( m ) ), myIndex( 0 ) { init(); } - IMapIterator( const IMapIterator& i ) : myMap( i.myMap ), myIndex( i.myIndex ) { init(); } - - bool operator==( const IMapIterator& i ) { return !operator!=( i ); } - bool operator!=( const IMapIterator& i ) { return !myMap || myMap != i.myMap || myIndex != i.myIndex; } - - operator bool() const { return myIndex >= 0; } - - const Key& key() const { return myMap->key( myIndex ); } - Value& data() { return myMap->value( myIndex ); } - const Value& data() const { return myMap->value( myIndex ); } - - Value& operator*() { return data(); } - - IMapIterator& operator++() { myIndex++; init(); return *this; } - IMapIterator operator++( int ) { IMapIterator i = *this; myIndex++; init(); return i; } - IMapIterator& operator--() { myIndex--; init(); return *this; } - IMapIterator operator--( int ) { IMapIterator i = *this; myIndex--; init(); return i; } - -private: - IMapIterator( const IMap* m, const int index ) : myMap( const_cast< IMap* >( m ) ), myIndex( index ) { init(); } - void init() { if ( !myMap || myIndex >= myMap->count() ) myIndex = -1; } - -private: - IMap* myMap; - int myIndex; - - friend class IMap; - friend class IMapConstIterator; -}; - -/*! - Class: QtxResourceMgr::IMapConstIterator -*/ - -template class QtxResourceMgr::IMapConstIterator -{ -public: - IMapConstIterator() : myMap( 0 ), myIndex( 0 ) { init(); } - IMapConstIterator( const IMap* m ) : myMap( const_cast< IMap* >( m ) ), myIndex( 0 ) { init(); } - IMapConstIterator( const IMapConstIterator& i ) : myMap( i.myMap ), myIndex( i.myIndex ) { init(); } - IMapConstIterator( const IMapIterator& i ) : myMap( i.myMap ), myIndex( i.myIndex ) { init(); } - - bool operator==( const IMapConstIterator& i ) { return !operator!=( i ); } - bool operator!=( const IMapConstIterator& i ) { return !myMap || myMap != i.myMap || myIndex != i.myIndex; } - - operator bool() const { return myIndex >= 0; } - - const Key& key() const { return myMap->key( myIndex ); } - const Value& data() const { return myMap->value( myIndex ); } - - const Value& operator*() const { return data(); } - - IMapConstIterator& operator++() { myIndex++; init(); return *this; } - IMapConstIterator operator++( int ) { IMapConstIterator i = *this; myIndex++; init(); return i; } - IMapConstIterator& operator--() { myIndex--; init(); return *this; } - IMapConstIterator operator--( int ) { IMapConstIterator i = *this; myIndex--; init(); return i; } - -private: - IMapConstIterator( const IMap* m, const int index ): myMap( const_cast< IMap* >( m ) ), myIndex( index ) { init(); } - void init() { if ( !myMap || myIndex >= myMap->count() ) myIndex = -1; } - -private: - IMap* myMap; - int myIndex; - - friend class IMap; -}; - -/*! - Class: QtxResourceMgr::IMap -*/ - -template class QtxResourceMgr::IMap -{ -public: - typedef IMapIterator Iterator; - typedef IMapConstIterator ConstIterator; - -public: - IMap() {} - IMap( const IMap& m ) : myKeys( m.myKeys ), myData( m.myData ) {} - IMap& operator=( const IMap& m ) { myKeys = m.myKeys; myData = m.myData; return *this; } - - int count() const { return myData.count(); } - int size() const { return myData.count(); } - bool empty() const { return myData.empty(); } - bool isEmpty() const { return myData.empty(); } - - void clear() { myKeys.clear(); myData.clear(); } - - QValueList keys() const { return myKeys; } - QValueList values() const { QValueList l; for ( int i = 0; i < count(); i++ ) l.append( value( i ) ); return l; } - bool contains ( const Key& key ) const { return myData.contains( key ); } - - Iterator begin() { return Iterator( this ); } - Iterator end() { return Iterator( this, count() ); } - ConstIterator begin() const { return ConstIterator( this ); } - ConstIterator end() const { return ConstIterator( this, count() ); } - - Iterator insert( const Key& key, const Value& value, bool overwrite = true ) - { - if ( myData.find( key ) == myData.end() || overwrite ) - { - if ( myData.find( key ) != myData.end() && overwrite ) - myKeys.remove( myKeys.find( key ) ); - myKeys.append( key ); - myData[key] = value; - } - return Iterator( this, index( key ) ); - } - - Iterator replace( const Key& key, const Value& value ) - { - if ( myData.find( key ) == myData.end() ) - myKeys.append( key ); - myData[ key ] = value; - return Iterator( this, index( key ) ); - } - - int index( const Key& key ) const { return myKeys.findIndex( key ); } - Iterator at( const int index ) { return Iterator( this, index ); } - ConstIterator at( const int index ) const { return ConstIterator( this, index ); } - - Key& key( const int index ) - { - if ( index < 0 || index >= (int)myKeys.count() ) - return dummyKey; - return myKeys[index]; - } - - Value& value( const int index ) - { - if ( index < 0 || index >= (int)myKeys.count() ) - return dummyValue; - return myData[ myKeys[index] ]; - } - - Value& operator[]( const Key& key ) - { - if ( myData.find( key ) == myData.end() ) - insert( key, Value() ); - return myData[ key ]; - } - - const Value& operator[]( const Key& key ) const - { - if ( myData.find( key ) == myData.end() ) - return dummyValue; - return myData[ key ]; - } - - void erase( Iterator it ) { remove( it ); } - void erase( const Key& key ) { remove( key ); } - void erase( const int index ) { remove( index ); } - void remove( Iterator it ) { if ( it.myMap != this ) return; remove( it.myIndex ); } - void remove( const Key& key ) { remove( index( key ) ); } - void remove( const int index ) - { - if ( index >= 0 && index < (int)myKeys.count() ) - { - myData.remove( myKeys[ index ] ); - myKeys.remove( myKeys.at( index ) ); - } - } - -private: - QValueList myKeys; - QMap myData; - Key dummyKey; - Value dummyValue; - - friend class IMapIterator; - friend class IMapConstIterator; + QString myFmt; //!< format name + QMap myOpt; //!< options map }; -#endif +#endif // QTX_RESOURCEMGR_H -- 2.39.2