From: stv Date: Thu, 9 Jun 2005 09:32:35 +0000 (+0000) Subject: Base class for resource editor and one completed implementation. X-Git-Tag: T3_0_0_a3~26 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=624ced11294dbd9f0704a95ccb3c6135f58931d3;p=modules%2Fgui.git Base class for resource editor and one completed implementation. --- diff --git a/src/Qtx/QtxListResourceEdit.cxx b/src/Qtx/QtxListResourceEdit.cxx new file mode 100644 index 000000000..717d4f3db --- /dev/null +++ b/src/Qtx/QtxListResourceEdit.cxx @@ -0,0 +1,785 @@ +// File: QtxListResourceEdit.cxx +// Author: Sergey TELKOV + +#include "QtxListResourceEdit.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "QtxIntSpinBox.h" +#include "QtxDblSpinBox.h" + +/* + Class: QtxListResourceEdit + Descr: GUI implementation of QtxResourceEdit - manager of resources +*/ + +QtxListResourceEdit::QtxListResourceEdit( QtxResourceMgr* mgr, QWidget* parent ) +: QFrame( parent ), +QtxResourceEdit( mgr ) +{ + QHBoxLayout* main = new QHBoxLayout( this, 0, 5 ); + + myList = new QListBox( this ); + myStack = new QWidgetStack( this ); + + myList->setSizePolicy( QSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred ) ); + myStack->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred ) ); + + main->addWidget( myList ); + main->addWidget( myStack ); + + myList->setSelectionMode( QListBox::Single ); + + connect( myList, SIGNAL( selectionChanged() ), this, SLOT( onSelectionChanged() ) ); + + setFocusProxy( myList ); + + updateState(); +} + +QtxListResourceEdit::~QtxListResourceEdit() +{ +} + +void QtxListResourceEdit::onSelectionChanged() +{ + int idx = myList->index( myList->selectedItem() ); + if ( idx < 0 ) + return; + + myStack->raiseWidget( idx ); +} + +QtxResourceEdit::Item* QtxListResourceEdit::createItem( const QString& title, const int ) +{ + Item* i = item( title, -1 ); + if ( i ) + return i; + + Category* category = new Category( this, myStack ); + myList->insertItem( title ); + int id = myList->count() - 1; + myStack->addWidget( category, id ); + + if ( !myList->selectedItem() ) + myList->setSelected( 0, true ); + + updateState(); + + return category; +} + +void QtxListResourceEdit::updateState() +{ + if ( myStack->visibleWidget() ) + myStack->show(); + else + myStack->hide(); +} + +/* + Class: QtxListResourceEdit::Category + Descr: GUI implementation of preferences category. +*/ + +QtxListResourceEdit::Category::Category( QtxListResourceEdit* edit, QWidget* parent ) +: QFrame( parent ), +Item( edit ) +{ + QVBoxLayout* main = new QVBoxLayout( this ); + QGroupBox* base = new QGroupBox( 1, Qt::Horizontal, "", this ); + base->setFrameStyle( QFrame::NoFrame ); + base->setInsideMargin( 0 ); + main->addWidget( base, 1 ); + + myTabs = new QTabWidget( base ); + myInfo = new QLabel( base ); + + myInfo->setAlignment( Qt::AlignCenter ); + myInfo->setFrameStyle( QFrame::WinPanel | QFrame::Raised ); + myInfo->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ) ); + + updateState(); +} + +QtxListResourceEdit::Category::~Category() +{ +} + +int QtxListResourceEdit::Category::type() const +{ + return -1; +} + +void QtxListResourceEdit::Category::store() +{ +} + +void QtxListResourceEdit::Category::retrieve() +{ +} + +QVariant QtxListResourceEdit::Category::property( const QString& prop ) const +{ + QVariant var; + if ( prop == QString( "information" ) || prop == QString( "info" ) ) + var = myInfo->text(); + return var; +} + +void QtxListResourceEdit::Category::setProperty( const QString& name, const QVariant& var ) +{ + QVariant prop = var; + if ( !prop.cast( QVariant::String ) ) + return; + + if ( name == QString( "information" ) || name == QString( "info" ) ) + myInfo->setText( prop.toString() ); + + updateState(); +} + +QtxResourceEdit::Item* QtxListResourceEdit::Category::createItem( const QString& title, const int ) +{ + Item* i = item( title, id() ); + if ( i ) + return i; + + Tab* tab = new Tab( resourceEdit(), this, this ); + myTabs->addTab( tab, title ); + + updateState(); + + return tab; +} + +void QtxListResourceEdit::Category::updateState() +{ + if ( myTabs->count() ) + myTabs->show(); + else + myTabs->hide(); + + if ( !myTabs->count() && !myInfo->text().isEmpty() ) + myInfo->show(); + else + myInfo->hide(); +} + +/* + Class: QtxListResourceEdit::Tab + Descr: GUI implementation of resources tab. +*/ + +QtxListResourceEdit::Tab::Tab( QtxResourceEdit* edit, Item* pItem, QWidget* parent ) +: QFrame( parent ), +Item( edit, pItem ) +{ + QVBoxLayout* main = new QVBoxLayout( this ); + myMainFrame = new QGroupBox( 1, Qt::Horizontal, "", this ); + myMainFrame->setFrameStyle( QFrame::NoFrame ); + myMainFrame->setInsideMargin( 5 ); + main->addWidget( myMainFrame ); +} + +QtxListResourceEdit::Tab::~Tab() +{ +} + +int QtxListResourceEdit::Tab::type() const +{ + return -1; +} + +void QtxListResourceEdit::Tab::store() +{ +} + +void QtxListResourceEdit::Tab::retrieve() +{ +} + +void QtxListResourceEdit::Tab::polish() +{ + QFrame::polish(); + + adjustLabels(); +} + +QtxResourceEdit::Item* QtxListResourceEdit::Tab::createItem( const QString& title, const int ) +{ + Item* i = item( title, id() ); + if ( i ) + return i; + + Group* group = new Group( title, resourceEdit(), this, myMainFrame ); + + return group; +} + +void QtxListResourceEdit::Tab::adjustLabels() +{ + QObjectList* labels = queryList( "QLabel" ); + if ( labels ) + { + int w = 0; + for ( QObjectListIt it1( *labels ); it1.current(); ++it1 ) + { + if ( it1.current()->isWidgetType() ) + { + QWidget* wid = (QWidget*)it1.current(); + w = QMAX( w, wid->sizeHint().width() ); + } + } + for ( QObjectListIt it2( *labels ); it2.current(); ++it2 ) + { + if ( it2.current()->isWidgetType() ) + { + QWidget* wid = (QWidget*)it2.current(); + wid->setMinimumWidth( w ); + } + } + delete labels; + } +} + +/* + Class: QtxListResourceEdit::Group + Descr: GUI implementation of resources group. +*/ + +QtxListResourceEdit::Group::Group( const QString& title, QtxResourceEdit* edit, Item* pItem, QWidget* parent ) +: QGroupBox( 2, Qt::Horizontal, title, parent ), +Item( edit, pItem ) +{ +} + +QtxListResourceEdit::Group::~Group() +{ +} + +int QtxListResourceEdit::Group::type() const +{ + return -1; +} + +void QtxListResourceEdit::Group::store() +{ +} + +void QtxListResourceEdit::Group::retrieve() +{ +} + +QVariant QtxListResourceEdit::Group::property( const QString& prop ) const +{ + QVariant var; + if ( prop == "columns" ) + var = QVariant( columns() ); + return var; +} + +void QtxListResourceEdit::Group::setProperty( const QString& name, const QVariant& var ) +{ + QVariant prop = var; + if ( !prop.cast( QVariant::Int ) ) + return; + + if ( name == QString( "columns" ) && prop.toInt() > 0 ) + setColumns( prop.toInt() ); +} + +void QtxListResourceEdit::Group::setTitle( const QString& title ) +{ + Item::setTitle( title ); + QGroupBox::setTitle( title ); +} + +QtxResourceEdit::Item* QtxListResourceEdit::Group::createItem( const QString& title, const int type ) +{ + Item* item = 0; + + switch ( type ) + { + case Color: + item = new ColorItem( title, resourceEdit(), this, this ); + break; + case Bool: + item = new StateItem( title, resourceEdit(), this, this ); + break; + case String: + item = new StringItem( title, resourceEdit(), this, this ); + break; + case List: + item = new ListItem( title, resourceEdit(), this, this ); + break; + case RealSpin: + item = new DoubleSpinItem( title, resourceEdit(), this, this ); + break; + case IntegerSpin: + item = new IntegerSpinItem( title, resourceEdit(), this, this ); + break; + case RealEdit: + item = new DoubleEditItem( title, resourceEdit(), this, this ); + break; + case IntegerEdit: + item = new IntegerEditItem( title, resourceEdit(), this, this ); + break; + case Space: + item = new Spacer( resourceEdit(), this, this ); + break; + } + + return item; +} + +/* + Class: QtxListResourceEdit::PrefItem + Descr: Base class for preferences items. +*/ + +QtxListResourceEdit::PrefItem::PrefItem( const int type, QtxResourceEdit* edit, Item* pi, QWidget* parent ) +: QHBox( parent ), +Item( edit, pi ), +myType( type ) +{ + setSpacing( 5 ); +} + +QtxListResourceEdit::PrefItem::~PrefItem() +{ +} + +int QtxListResourceEdit::PrefItem::type() const +{ + return myType; +} + +QtxResourceEdit::Item* QtxListResourceEdit::PrefItem::createItem( const QString&, const int ) +{ + return 0; +} + +/* + Class: QtxListResourceEdit::Spacer + Descr: GUI implementation of resources spacer. +*/ + +QtxListResourceEdit::Spacer::Spacer( QtxResourceEdit* edit, Item* pItem, QWidget* parent ) +: PrefItem( Space, edit, pItem, parent ) +{ + setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred ) ); +} + +QtxListResourceEdit::Spacer::~Spacer() +{ +} + +void QtxListResourceEdit::Spacer::store() +{ +} + +void QtxListResourceEdit::Spacer::retrieve() +{ +} + +/* + Class: QtxListResourceEdit::ListItem + Descr: GUI implementation of resources list item. +*/ + +QtxListResourceEdit::ListItem::ListItem( const QString& title, QtxResourceEdit* edit, + Item* pItem, QWidget* parent ) +: PrefItem( List, edit, pItem, parent ) +{ + new QLabel( title, this ); + myList = new QComboBox( false, this ); + myList->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred ) ); +} + +QtxListResourceEdit::ListItem::~ListItem() +{ +} + +void QtxListResourceEdit::ListItem::store() +{ + int idx = myList->currentItem(); + if ( myIndex.contains( idx ) ) + setInteger( myIndex[idx] ); +} + +void QtxListResourceEdit::ListItem::retrieve() +{ + int id = getInteger( -1 ); + + int idx = -1; + for ( QMap::ConstIterator it = myIndex.begin(); it != myIndex.end() && idx == -1; ++it ) + { + if ( it.data() == id ) + idx = it.key(); + } + + myList->setCurrentItem( idx ); +} + +QVariant QtxListResourceEdit::ListItem::property( const QString& name ) const +{ + QVariant val; + if ( name == QString( "strings" ) ) + { + QStringList lst; + for ( int i = 0; i < (int)myList->count(); i++ ) + lst.append( myList->text( i ) ); + val = QVariant( lst ); + } + else if ( name == QString( "indexes" ) ) + { + QValueList lst; + for ( int i = 0; i < (int)myList->count(); i++ ) + lst.append( myIndex.contains( i ) ? myIndex[i] : 0 ); + val = QVariant( lst ); + } + return val; +} + +void QtxListResourceEdit::ListItem::setProperty( const QString& name, const QVariant& val ) +{ + if ( name == QString( "strings" ) ) + setStrings( val ); + else if ( name == QString( "indexes" ) ) + setIndexes( val ); +} + +void QtxListResourceEdit::ListItem::setStrings( const QVariant& var ) +{ + if ( var.type() != QVariant::StringList ) + return; + + setStrings( var.toStringList() ); +} + +void QtxListResourceEdit::ListItem::setIndexes( const QVariant& var ) +{ + if ( var.type() != QVariant::List ) + return; + + QValueList varList = var.toList(); + QValueList lst; + for ( QValueList::const_iterator it = varList.begin(); it != varList.end(); ++it ) + { + if ( (*it).canCast( QVariant::Int ) ) + lst.append( (*it).toInt() ); + } + setIndexes( lst ); +} + +void QtxListResourceEdit::ListItem::setStrings( const QStringList& lst ) +{ + myList->clear(); + myList->insertStringList( lst ); +} + +void QtxListResourceEdit::ListItem::setIndexes( const QValueList& lst ) +{ + myIndex.clear(); + + int idx = 0; + for ( QValueList::const_iterator it = lst.begin(); it != lst.end(); ++it, idx++ ) + myIndex.insert( idx, *it ); +} + +/* + Class: QtxListResourceEdit::StateItem + Descr: GUI implementation of resources bool item. +*/ + +QtxListResourceEdit::StateItem::StateItem( const QString& title, QtxResourceEdit* edit, + Item* pItem, QWidget* parent ) +: PrefItem( Bool, edit, pItem, parent ) +{ + myState = new QCheckBox( title, this ); + myState->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred ) ); +} + +QtxListResourceEdit::StateItem::~StateItem() +{ +} + +void QtxListResourceEdit::StateItem::store() +{ + setBoolean( myState->isChecked() ); +} + +void QtxListResourceEdit::StateItem::retrieve() +{ + myState->setChecked( getBoolean() ); +} + +/* + Class: QtxListResourceEdit::StringItem + Descr: GUI implementation of resources string item. +*/ + +QtxListResourceEdit::StringItem::StringItem( const QString& title, QtxResourceEdit* edit, + Item* pItem, QWidget* parent ) +: PrefItem( String, edit, pItem, parent ) +{ + new QLabel( title, this ); + myString = new QLineEdit( this ); + myString->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred ) ); +} + +QtxListResourceEdit::StringItem::~StringItem() +{ +} + +void QtxListResourceEdit::StringItem::store() +{ + setString( myString->text() ); +} + +void QtxListResourceEdit::StringItem::retrieve() +{ + myString->setText( getString() ); +} + +/* + Class: QtxListResourceEdit::IntegerEditItem + Descr: GUI implementation of resources integer item. +*/ + +QtxListResourceEdit::IntegerEditItem::IntegerEditItem( const QString& title, QtxResourceEdit* edit, Item* pItem, QWidget* parent ) +: PrefItem( IntegerEdit, edit, pItem, parent ) +{ + new QLabel( title, this ); + myInteger = new QLineEdit( this ); + myInteger->setValidator( new QIntValidator( myInteger ) ); + myInteger->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred ) ); +} + +QtxListResourceEdit::IntegerEditItem::~IntegerEditItem() +{ +} + +void QtxListResourceEdit::IntegerEditItem::store() +{ + setString( myInteger->text() ); +} + +void QtxListResourceEdit::IntegerEditItem::retrieve() +{ + myInteger->setText( getString() ); +} + +/* + Class: QtxListResourceEdit::IntegerSpinItem + Descr: GUI implementation of resources integer item. +*/ + +QtxListResourceEdit::IntegerSpinItem::IntegerSpinItem( const QString& title, QtxResourceEdit* edit, Item* pItem, QWidget* parent ) +: PrefItem( IntegerSpin, edit, pItem, parent ) +{ + new QLabel( title, this ); + myInteger = new QtxIntSpinBox( this ); + myInteger->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred ) ); +} + +QtxListResourceEdit::IntegerSpinItem::~IntegerSpinItem() +{ +} + +void QtxListResourceEdit::IntegerSpinItem::store() +{ + setInteger( myInteger->value() ); +} + +void QtxListResourceEdit::IntegerSpinItem::retrieve() +{ + myInteger->setValue( getInteger() ); +} + +QVariant QtxListResourceEdit::IntegerSpinItem::property( const QString& name ) const +{ + QVariant var; + if ( name == QString( "minimum" ) || name == QString( "min" ) ) + var = QVariant( myInteger->minValue() ); + else if ( name == QString( "maximum" ) || name == QString( "max" ) ) + var = QVariant( myInteger->maxValue() ); + else if ( name == QString( "step" ) ) + var = QVariant( myInteger->lineStep() ); + return var; +} + +void QtxListResourceEdit::IntegerSpinItem::setProperty( const QString& name, const QVariant& var ) +{ + QVariant prop = var; + if ( !prop.cast( QVariant::Int ) ) + return; + + int val = prop.toInt(); + if ( name == QString( "minimum" ) || name == QString( "min" ) ) + myInteger->setMinValue( val ); + else if ( name == QString( "maximum" ) || name == QString( "max" ) ) + myInteger->setMaxValue( val ); + else if ( name == QString( "step" ) && val > 0 ) + myInteger->setLineStep( val ); +} + +/* + Class: QtxListResourceEdit::DoubleEditItem + Descr: GUI implementation of resources string item. +*/ + +QtxListResourceEdit::DoubleEditItem::DoubleEditItem( const QString& title, QtxResourceEdit* edit, + Item* pItem, QWidget* parent ) +: PrefItem( RealEdit, edit, pItem, parent ) +{ + new QLabel( title, this ); + myDouble = new QLineEdit( this ); + myDouble->setValidator( new QDoubleValidator( myDouble ) ); + myDouble->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred ) ); +} + +QtxListResourceEdit::DoubleEditItem::~DoubleEditItem() +{ +} + +void QtxListResourceEdit::DoubleEditItem::store() +{ + setString( myDouble->text() ); +} + +void QtxListResourceEdit::DoubleEditItem::retrieve() +{ + myDouble->setText( getString() ); +} + +/* + Class: QtxListResourceEdit::DoubleSpinItem + Descr: GUI implementation of resources double item. +*/ + +QtxListResourceEdit::DoubleSpinItem::DoubleSpinItem( const QString& title, QtxResourceEdit* edit, + Item* pItem, QWidget* parent ) +: PrefItem( RealSpin, edit, pItem, parent ) +{ + new QLabel( title, this ); + myDouble = new QtxDblSpinBox( this ); + myDouble->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred ) ); +} + +QtxListResourceEdit::DoubleSpinItem::~DoubleSpinItem() +{ +} + +void QtxListResourceEdit::DoubleSpinItem::store() +{ + setDouble( myDouble->value() ); +} + +void QtxListResourceEdit::DoubleSpinItem::retrieve() +{ + myDouble->setValue( getDouble() ); +} + +QVariant QtxListResourceEdit::DoubleSpinItem::property( const QString& name ) const +{ + QVariant var; + if ( name == QString( "minimum" ) || name == QString( "min" ) ) + var = QVariant( myDouble->minValue() ); + else if ( name == QString( "maximum" ) || name == QString( "max" ) ) + var = QVariant( myDouble->maxValue() ); + else if ( name == QString( "precision" ) ) + var = QVariant( myDouble->precision() ); + else if ( name == QString( "step" ) ) + var = QVariant( myDouble->lineStep() ); + return var; +} + +void QtxListResourceEdit::DoubleSpinItem::setProperty( const QString& name, const QVariant& var ) +{ + QVariant prop = var; + if ( prop.cast( QVariant::Double ) ) + { + double val = prop.toDouble(); + if ( name == QString( "minimum" ) || name == QString( "min" ) ) + myDouble->setMinValue( val ); + else if ( name == QString( "maximum" ) || name == QString( "max" ) ) + myDouble->setMaxValue( val ); + else if ( name == QString( "step" ) && val > 0 ) + myDouble->setLineStep( val ); + } + else + { + QVariant prop = var; + if ( prop.cast( QVariant::Int ) && name == QString( "precision" ) && prop.toInt() > 0 ) + myDouble->setPrecision( prop.toInt() ); + } +} + +/* + Class: QtxListResourceEdit::ColorItem + Descr: GUI implementation of resources color item. +*/ + +QtxListResourceEdit::ColorItem::ColorItem( const QString& title, QtxResourceEdit* edit, + Item* pItem, QWidget* parent ) +: PrefItem( Color, edit, pItem, parent ) +{ + class ColorSelector : public QLabel + { + public: + ColorSelector( QWidget* parent = 0 ) : QLabel( parent ) + { + setFrameStyle( WinPanel | Raised ); + } + virtual ~ColorSelector() {} + virtual QSize minimumSizeHint() const + { + return QLabel::minimumSizeHint() + QSize( 0, 2 ); + } + + protected: + virtual void mousePressEvent( QMouseEvent* e ) + { + if ( e->button() == LeftButton ) + { + setFrameStyle( WinPanel | Sunken ); + QColor c = QColorDialog::getColor( paletteBackgroundColor(), this ); + if ( c.isValid() ) + setPaletteBackgroundColor( c ); + + setFrameStyle( WinPanel | Raised ); + } + } + }; + + new QLabel( title, this ); + myColor = new ColorSelector( this ); + myColor->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred ) ); +} + +QtxListResourceEdit::ColorItem::~ColorItem() +{ +} + +void QtxListResourceEdit::ColorItem::store() +{ + setColor( myColor->paletteBackgroundColor() ); +} + +void QtxListResourceEdit::ColorItem::retrieve() +{ + myColor->setPaletteBackgroundColor( getColor() ); +} diff --git a/src/Qtx/QtxListResourceEdit.h b/src/Qtx/QtxListResourceEdit.h new file mode 100644 index 000000000..554cec23b --- /dev/null +++ b/src/Qtx/QtxListResourceEdit.h @@ -0,0 +1,345 @@ +// File: QtxListResourceEdit.h +// Author: Sergey TELKOV + +#ifndef QTXLISTRESOURCEEDIT_H +#define QTXLISTRESOURCEEDIT_H + +#include "QtxResourceEdit.h" + +#include +#include +#include +#include + +class QLabel; +class QListBox; +class QLineEdit; +class QCheckBox; +class QComboBox; +class QTabWidget; +class QWidgetStack; + +class QtxIntSpinBox; +class QtxDblSpinBox; + +/* + Class: QtxListResourceEdit + Descr: GUI implementation of QtxResourceEdit - manager of resources +*/ + +class QTX_EXPORT QtxListResourceEdit : public QFrame, public QtxResourceEdit +{ + Q_OBJECT + +public: + class Tab; + class Group; + class Category; + class PrefItem; + + class Spacer; + class ListItem; + class ColorItem; + class StateItem; + class StringItem; + class DoubleSpinItem; + class DoubleEditItem; + class IntegerSpinItem; + class IntegerEditItem; + +public: + QtxListResourceEdit( QtxResourceMgr*, QWidget* = 0 ); + virtual ~QtxListResourceEdit(); + +private slots: + void onSelectionChanged(); + +protected: + virtual Item* createItem( const QString&, const int ); + +private: + void updateState(); + +private: + QListBox* myList; + QWidgetStack* myStack; +}; + +/* + Class: QtxListResourceEdit::Category + Descr: GUI implementation of 'Category' frame +*/ + +class QtxListResourceEdit::Category : public QFrame, public Item +{ +public: + Category( QtxListResourceEdit*, QWidget* = 0 ); + virtual ~Category(); + + virtual int type() const; + virtual void store(); + virtual void retrieve(); + + virtual QVariant property( const QString& ) const; + virtual void setProperty( const QString&, const QVariant& ); + +protected: + virtual Item* createItem( const QString&, const int ); + +private: + void updateState(); + +private: + QLabel* myInfo; + QTabWidget* myTabs; +}; + +/* + Class: QtxListResourceEdit::Tab + Descr: GUI implementation of resources tab. +*/ + +class QtxListResourceEdit::Tab : public QFrame, public Item +{ +public: + Tab( QtxResourceEdit*, Item*, QWidget* = 0 ); + virtual ~Tab(); + + virtual int type() const; + virtual void store(); + virtual void retrieve(); + +public: + virtual void polish(); + +protected: + virtual Item* createItem( const QString&, const int ); + +private: + void adjustLabels(); + +private: + QGroupBox* myMainFrame; +}; + +/* + Class: QtxListResourceEdit::Group + Descr: GUI implementation of resources group. +*/ + +class QtxListResourceEdit::Group : public QGroupBox, public Item +{ +public: + Group( const QString&, QtxResourceEdit*, Item*, QWidget* = 0 ); + virtual ~Group(); + + virtual int type() const; + virtual void store(); + virtual void retrieve(); + + virtual QVariant property( const QString& ) const; + virtual void setProperty( const QString&, const QVariant& ); + + virtual void setTitle( const QString& ); + +protected: + virtual Item* createItem( const QString&, const int ); +}; + +/* + Class: QtxListResourceEdit::PrefItem + Descr: Base class for preferences items. +*/ + +class QtxListResourceEdit::PrefItem : public QHBox, public Item +{ +public: + PrefItem( const int, QtxResourceEdit*, Item* = 0, QWidget* = 0 ); + virtual ~PrefItem(); + + virtual int type() const; + +protected: + virtual Item* createItem( const QString&, const int ); + +private: + int myType; +}; + +/* + Class: QtxListResourceEdit::Spacer + Descr: GUI implementation of resources spacer. +*/ + +class QtxListResourceEdit::Spacer : public PrefItem +{ +public: + Spacer( QtxResourceEdit*, Item*, QWidget* = 0 ); + virtual ~Spacer(); + + virtual void store(); + virtual void retrieve(); +}; + +/* + Class: QtxListResourceEdit::ListItem + Descr: GUI implementation of resources list item. +*/ + +class QtxListResourceEdit::ListItem : public PrefItem +{ +public: + ListItem( const QString&, QtxResourceEdit*, Item*, QWidget* = 0 ); + virtual ~ListItem(); + + virtual void store(); + virtual void retrieve(); + + virtual QVariant property( const QString& ) const; + virtual void setProperty( const QString&, const QVariant& ); + +private: + void setStrings( const QVariant& ); + void setIndexes( const QVariant& ); + + void setStrings( const QStringList& ); + void setIndexes( const QValueList& ); + +private: + QComboBox* myList; + QMap myIndex; +}; + +/* + Class: QtxListResourceEdit::StateItem + Descr: GUI implementation of resources bool item. +*/ + +class QtxListResourceEdit::StateItem : public PrefItem +{ +public: + StateItem( const QString&, QtxResourceEdit*, Item*, QWidget* = 0 ); + virtual ~StateItem(); + + virtual void store(); + virtual void retrieve(); + +private: + QCheckBox* myState; +}; + +/* + Class: QtxListResourceEdit::StringItem + Descr: GUI implementation of resources string item. +*/ + +class QtxListResourceEdit::StringItem : public PrefItem +{ +public: + StringItem( const QString&, QtxResourceEdit*, Item*, QWidget* = 0 ); + virtual ~StringItem(); + + virtual void store(); + virtual void retrieve(); + +private: + QLineEdit* myString; +}; + +/* + Class: QtxListResourceEdit::IntegerEditItem + Descr: GUI implementation of resources integer item. +*/ + +class QtxListResourceEdit::IntegerEditItem : public PrefItem +{ +public: + IntegerEditItem( const QString&, QtxResourceEdit*, Item*, QWidget* = 0 ); + virtual ~IntegerEditItem(); + + virtual void store(); + virtual void retrieve(); + +private: + QLineEdit* myInteger; +}; + +/* + Class: QtxListResourceEdit::IntegerSpinItem + Descr: GUI implementation of resources integer item. +*/ + +class QtxListResourceEdit::IntegerSpinItem : public PrefItem +{ +public: + IntegerSpinItem( const QString&, QtxResourceEdit*, Item*, QWidget* = 0 ); + virtual ~IntegerSpinItem(); + + virtual void store(); + virtual void retrieve(); + + virtual QVariant property( const QString& ) const; + virtual void setProperty( const QString&, const QVariant& ); + +private: + QtxIntSpinBox* myInteger; +}; + +/* + Class: QtxListResourceEdit::DoubleEditItem + Descr: GUI implementation of resources double item. +*/ + +class QtxListResourceEdit::DoubleEditItem : public PrefItem +{ +public: + DoubleEditItem( const QString&, QtxResourceEdit*, Item*, QWidget* = 0 ); + virtual ~DoubleEditItem(); + + virtual void store(); + virtual void retrieve(); + +private: + QLineEdit* myDouble; +}; + +/* + Class: QtxListResourceEdit::DoubleSpinItem + Descr: GUI implementation of resources double item. +*/ + +class QtxListResourceEdit::DoubleSpinItem : public PrefItem +{ +public: + DoubleSpinItem( const QString&, QtxResourceEdit*, Item*, QWidget* = 0 ); + virtual ~DoubleSpinItem(); + + virtual void store(); + virtual void retrieve(); + + virtual QVariant property( const QString& ) const; + virtual void setProperty( const QString&, const QVariant& ); + +private: + QtxDblSpinBox* myDouble; +}; + +/* + Class: QtxListResourceEdit::ColorItem + Descr: GUI implementation of resources color item. +*/ + +class QtxListResourceEdit::ColorItem : public PrefItem +{ +public: + ColorItem( const QString&, QtxResourceEdit*, Item*, QWidget* = 0 ); + virtual ~ColorItem(); + + virtual void store(); + virtual void retrieve(); + +private: + QWidget* myColor; +}; + +#endif diff --git a/src/Qtx/QtxResourceEdit.cxx b/src/Qtx/QtxResourceEdit.cxx new file mode 100644 index 000000000..e7b559287 --- /dev/null +++ b/src/Qtx/QtxResourceEdit.cxx @@ -0,0 +1,389 @@ +// File: QtxResourceEdit.cxx +// Author: Sergey TELKOV + +#include "QtxResourceEdit.h" + +#include "QtxResourceMgr.h" + +/* + Class: QtxResourceEdit + Descr: Class for managing preferences items +*/ + +QtxResourceEdit::QtxResourceEdit( QtxResourceMgr* mgr ) +: myResMgr( mgr ) +{ +} + +QtxResourceEdit::~QtxResourceEdit() +{ + for ( ItemMap::ConstIterator it = myItems.begin(); it != myItems.end(); ++it ) + delete it.data(); +} + +QtxResourceMgr* QtxResourceEdit::resourceMgr() const +{ + return myResMgr; +} + +int QtxResourceEdit::addItem( const QString& label, const int pId, const int type, + const QString& section, const QString& param ) +{ + Item* i = createItem( label, type, pId ); + if ( !i ) + return -1; + + myItems.insert( i->id(), i ); + + i->setTitle( label ); + i->setResource( section, param ); + + return i->id(); +} + +QVariant QtxResourceEdit::property( const int id, const QString& propName ) const +{ + QVariant propValue; + Item* i = item( id ); + if ( i ) + propValue = i->property( propName ); + return propValue; +} + +void QtxResourceEdit::setProperty( const int id, const QString& propName, const QVariant& propValue ) +{ + Item* i = item( id ); + if ( i ) + i->setProperty( propName, propValue ); +} + +void QtxResourceEdit::store() +{ + QMap before; + resourceValues( before ); + + for ( ItemMap::ConstIterator it = myItems.begin(); it != myItems.end(); ++it ) + it.data()->store(); + + QMap after; + resourceValues( after ); + + QMap changed; + differentValues( before, after, changed ); + + changedResources( changed ); +} + +void QtxResourceEdit::retrieve() +{ + for ( ItemMap::ConstIterator it = myItems.begin(); it != myItems.end(); ++it ) + it.data()->retrieve(); +} + +void QtxResourceEdit::toBackup() +{ + myBackup.clear(); + resourceValues( myBackup ); +} + +void QtxResourceEdit::fromBackup() +{ + setResourceValues( myBackup ); +} + +void QtxResourceEdit::update() +{ +} + +QtxResourceEdit::Item* QtxResourceEdit::item( const int id ) const +{ + Item* i = 0; + if ( myItems.contains( id ) ) + i = myItems[id]; + return i; +} + +QtxResourceEdit::Item* QtxResourceEdit::item( const QString& title ) const +{ + Item* i = 0; + for ( ItemMap::ConstIterator it = myItems.begin(); it != myItems.end() && !i; ++it ) + { + if ( it.data()->title() == title ) + i = it.data(); + } + return i; +} + +QtxResourceEdit::Item* QtxResourceEdit::item( const QString& title, const int pId ) const +{ + Item* i = 0; + Item* pItem = item( pId ); + for ( ItemMap::ConstIterator it = myItems.begin(); it != myItems.end() && !i; ++it ) + { + if ( it.data()->parentItem() == pItem && it.data()->title() == title ) + i = it.data(); + } + return i; +} + +QtxResourceEdit::Item* QtxResourceEdit::createItem( const QString& label, const int type, const int pId ) +{ + Item* i = 0; + if ( pId < 0 ) + i = createItem( label, type ); + else + { + Item* pItem = item( pId ); + if ( pItem ) + i = pItem->createItem( label, type ); + } + + return i; +} + +void QtxResourceEdit::removeItem( Item* item ) +{ + if ( !item ) + return; + + myItems.remove( item->id() ); +} + +void QtxResourceEdit::resourceValues( QMap& map ) const +{ + for ( ItemMap::ConstIterator it = myItems.begin(); it != myItems.end(); ++it ) + map.insert( it.key(), it.data()->resourceValue() ); +} + +void QtxResourceEdit::resourceValues( QMap& map ) const +{ + for ( ItemMap::ConstIterator it = myItems.begin(); it != myItems.end(); ++it ) + map.insert( it.data(), it.data()->resourceValue() ); +} + +void QtxResourceEdit::setResourceValues( QMap& map ) const +{ + for ( QMap::ConstIterator it = map.begin(); it != map.end(); ++it ) + { + Item* i = item( it.key() ); + if ( i ) + i->setResourceValue( it.data() ); + } +} + +void QtxResourceEdit::setResourceValues( QMap& map ) const +{ + for ( QMap::ConstIterator it = map.begin(); it != map.end(); ++it ) + it.key()->setResourceValue( it.data() ); +} + + +void QtxResourceEdit::differentValues( const QMap& map1, const QMap& map2, + QMap& resMap, const bool fromFirst ) const +{ + resMap.clear(); + const QMap& later = fromFirst ? map1 : map2; + const QMap& early = fromFirst ? map2 : map1; + + for ( QMap::ConstIterator it = later.begin(); it != later.end(); ++it ) + { + if ( !early.contains( it.key() ) || early[it.key()] != it.data() ) + resMap.insert( it.key(), it.data() ); + } +} + +void QtxResourceEdit::differentValues( const QMap& map1, const QMap& map2, + QMap& resMap, const bool fromFirst ) const +{ + resMap.clear(); + const QMap& later = fromFirst ? map1 : map2; + const QMap& early = fromFirst ? map2 : map1; + + for ( QMap::ConstIterator it = later.begin(); it != later.end(); ++it ) + { + if ( !early.contains( it.key() ) || early[it.key()] != it.data() ) + resMap.insert( it.key(), it.data() ); + } +} + +void QtxResourceEdit::changedResources( const QMap& ) +{ +} + +/* + Class: QtxResourceEdit::Item + Descr: Class for incapsulation of one preference item +*/ + +QtxResourceEdit::Item::Item( QtxResourceEdit* edit, Item* parent ) +: myEdit( edit ), +myParent( parent ) +{ + myId = generateId(); +} + +QtxResourceEdit::Item::~Item() +{ + if ( resourceEdit() ) + resourceEdit()->removeItem( this ); +} + +int QtxResourceEdit::Item::id() const +{ + return myId; +} + +QtxResourceEdit::Item* QtxResourceEdit::Item::parentItem() const +{ + return myParent; +} + +void QtxResourceEdit::Item::childItems( QPtrList& lst ) const +{ + for ( ItemListIterator it( myChildren ); it.current(); ++it ) + lst.append( it.current() ); +} + +QString QtxResourceEdit::Item::title() const +{ + return myTitle; +} + +void QtxResourceEdit::Item::resource( QString& sec, QString& param ) const +{ + sec = myResSection; + param = myResParameter; +} + +void QtxResourceEdit::Item::setTitle( const QString& title ) +{ + myTitle = title; +} + +void QtxResourceEdit::Item::setResource( const QString& sec, const QString& param ) +{ + myResSection = sec; + myResParameter = param; +} + +void QtxResourceEdit::Item::update() +{ +} + +QVariant QtxResourceEdit::Item::property( const QString& ) const +{ + return QVariant(); +} + +void QtxResourceEdit::Item::setProperty( const QString&, const QVariant& ) +{ +} + +QString QtxResourceEdit::Item::resourceValue() const +{ + return getString(); +} + +void QtxResourceEdit::Item::setResourceValue( const QString& val ) +{ + setString( val ); +} + +QtxResourceMgr* QtxResourceEdit::Item::resourceMgr() const +{ + QtxResourceMgr* resMgr = 0; + if ( resourceEdit() ) + resMgr = resourceEdit()->resourceMgr(); + return resMgr; +} + +QtxResourceEdit* QtxResourceEdit::Item::resourceEdit() const +{ + return myEdit; +} + +int QtxResourceEdit::Item::getInteger( const int val ) const +{ + QtxResourceMgr* resMgr = resourceMgr(); + return resMgr ? resMgr->integerValue( myResSection, myResParameter, val ) : val; +} + +double QtxResourceEdit::Item::getDouble( const double val ) const +{ + QtxResourceMgr* resMgr = resourceMgr(); + return resMgr ? resMgr->doubleValue( myResSection, myResParameter, val ) : val; +} + +bool QtxResourceEdit::Item::getBoolean( const bool val ) const +{ + QtxResourceMgr* resMgr = resourceMgr(); + return resMgr ? resMgr->booleanValue( myResSection, myResParameter, val ) : val; +} + +QString QtxResourceEdit::Item::getString( const QString& val ) const +{ + QtxResourceMgr* resMgr = resourceMgr(); + return resMgr ? resMgr->stringValue( myResSection, myResParameter, val ) : val; +} + +QColor QtxResourceEdit::Item::getColor( const QColor& val ) const +{ + QtxResourceMgr* resMgr = resourceMgr(); + return resMgr ? resMgr->colorValue( myResSection, myResParameter, val ) : val; +} + +void QtxResourceEdit::Item::setInteger( const int val ) +{ + QtxResourceMgr* resMgr = resourceMgr(); + if ( resMgr ) + resMgr->setValue( myResSection, myResParameter, val ); +} + +void QtxResourceEdit::Item::setDouble( const double val ) +{ + QtxResourceMgr* resMgr = resourceMgr(); + if ( resMgr ) + resMgr->setValue( myResSection, myResParameter, val ); +} + +void QtxResourceEdit::Item::setBoolean( const bool val ) +{ + QtxResourceMgr* resMgr = resourceMgr(); + if ( resMgr ) + resMgr->setValue( myResSection, myResParameter, val ); +} + +void QtxResourceEdit::Item::setString( const QString& val ) +{ + QtxResourceMgr* resMgr = resourceMgr(); + if ( resMgr ) + resMgr->setValue( myResSection, myResParameter, val ); +} + +void QtxResourceEdit::Item::setColor( const QColor& val ) +{ + QtxResourceMgr* resMgr = resourceMgr(); + if ( resMgr ) + resMgr->setValue( myResSection, myResParameter, val ); +} + +QtxResourceEdit::Item* QtxResourceEdit::Item::item( const int id ) const +{ + return resourceEdit() ? resourceEdit()->item( id ) : 0; +} + +QtxResourceEdit::Item* QtxResourceEdit::Item::item( const QString& title ) const +{ + return resourceEdit() ? resourceEdit()->item( title ) : 0; +} + +QtxResourceEdit::Item* QtxResourceEdit::Item::item( const QString& title, const int id ) const +{ + return resourceEdit() ? resourceEdit()->item( title, id ) : 0; +} + +int QtxResourceEdit::Item::generateId() +{ + static int _id = 0; + return _id++; +} diff --git a/src/Qtx/QtxResourceEdit.h b/src/Qtx/QtxResourceEdit.h new file mode 100644 index 000000000..479f05bee --- /dev/null +++ b/src/Qtx/QtxResourceEdit.h @@ -0,0 +1,161 @@ +// File: QtxResourceEdit.h +// Author: Sergey TELKOV + +#ifndef QTXRESOURCEEDIT_H +#define QTXRESOURCEEDIT_H + +#ifdef WIN32 +#pragma warning( disable:4251 ) +#endif + +#include "Qtx.h" + +class QString; +class QtxResourceMgr; + +#include +#include + +class QTX_EXPORT QtxResourceEdit +{ +public: + class Item; + + enum { Space, Bool, Color, String, List, RealSpin, IntegerSpin, RealEdit, IntegerEdit }; + +public: + QtxResourceEdit( QtxResourceMgr* ); + virtual ~QtxResourceEdit(); + + QtxResourceMgr* resourceMgr() const; + + int addItem( const QString& label, const int pId = -1, const int = -1, + const QString& section = QString::null, + const QString& param = QString::null ); + + QVariant property( const int, const QString& ) const; + void setProperty( const int, const QString&, const QVariant& ); + + virtual void store(); + virtual void retrieve(); + + virtual void update(); + + virtual void toBackup(); + virtual void fromBackup(); + +protected: + Item* item( const int ) const; + Item* item( const QString& ) const; + Item* item( const QString&, const int ) const; + + virtual Item* createItem( const QString&, const int ) = 0; + + void resourceValues( QMap& ) const; + void resourceValues( QMap& ) const; + + void setResourceValues( QMap& ) const; + void setResourceValues( QMap& ) const; + + void differentValues( const QMap&, const QMap&, + QMap&, const bool fromFirst = false ) const; + void differentValues( const QMap&, const QMap&, + QMap&, const bool fromFirst = false ) const; + + virtual void changedResources( const QMap& ); + +private: + void removeItem( Item* ); + Item* createItem( const QString&, const int, const int ); + +private: + typedef QMap ItemMap; + +private: + ItemMap myItems; + QtxResourceMgr* myResMgr; + QMap myBackup; + + friend class QtxResourceEdit::Item; +}; + +/* + Class: QtxResourceEditor::Item + Descr: Class for incapsulation of one preference item +*/ + +class QtxResourceEdit::Item +{ +public: + Item( QtxResourceEdit*, Item* = 0 ); + virtual ~Item(); + + int id() const; + virtual int type() const = 0; + + Item* parentItem() const; + void childItems( QPtrList& ) const; + + QString title() const; + void resource( QString&, QString& ) const; + + virtual void setTitle( const QString& ); + virtual void setResource( const QString&, const QString& ); + + virtual void update(); + + QVariant property( const QString& ) const; + virtual void setProperty( const QString&, const QVariant& ); + + virtual void store() = 0; + virtual void retrieve() = 0; + + virtual Item* createItem( const QString&, const int ) = 0; + + QString resourceValue() const; + void setResourceValue( const QString& ); + +protected: + QtxResourceMgr* resourceMgr() const; + QtxResourceEdit* resourceEdit() const; + + int getInteger( const int = 0 ) const; + double getDouble( const double = 0.0 ) const; + bool getBoolean( const bool = false ) const; + QColor getColor( const QColor& = QColor() ) const; + QString getString( const QString& = QString::null ) const; + + void setInteger( const int ); + void setDouble( const double ); + void setBoolean( const bool ); + void setColor( const QColor& ); + void setString( const QString& ); + + Item* item( const int ) const; + Item* item( const QString& ) const; + Item* item( const QString&, const int ) const; + +protected: + typedef QPtrList ItemList; + typedef QPtrListIterator ItemListIterator; + +private: + static int generateId(); + +private: + int myId; + Item* myParent; + ItemList myChildren; + + QString myTitle; + QString myResSection; + QString myResParameter; + + QtxResourceEdit* myEdit; +}; + +#ifdef WIN32 +#pragma warning( default:4251 ) +#endif + +#endif