From 976cd49a731af3b37908904eb9c8cb199f4478a7 Mon Sep 17 00:00:00 2001 From: stv Date: Wed, 7 Mar 2007 10:04:39 +0000 Subject: [PATCH] *** empty log message *** --- src/Qtx/QtxMainWindow.cxx | 166 ++++++++++++++++++++++++++++++++++++- src/Qtx/QtxMainWindow.h | 10 ++- src/Qtx/QtxResourceMgr.cxx | 124 ++++++++++++++++++++++----- src/Qtx/QtxResourceMgr.h | 6 +- 4 files changed, 276 insertions(+), 30 deletions(-) diff --git a/src/Qtx/QtxMainWindow.cxx b/src/Qtx/QtxMainWindow.cxx index 88a84c59f..8f0a3d1f0 100644 --- a/src/Qtx/QtxMainWindow.cxx +++ b/src/Qtx/QtxMainWindow.cxx @@ -85,7 +85,7 @@ bool QtxMainWindow::Filter::eventFilter( QObject* o, QEvent* e ) and geometry store/retrieve. */ QtxMainWindow::QtxMainWindow( QWidget* parent, Qt::WindowFlags f ) -: QMainWindow( parent ), +: QMainWindow( parent, f ), myMode( -1 ), myMenuBar( 0 ), myStatusBar( 0 ) @@ -193,6 +193,158 @@ void QtxMainWindow::setDockableStatusBar( const bool on ) } } +QString QtxMainWindow::saveGeometry() const +{ + QRect frame = frameGeometry(); + QRect screen = QApplication::desktop()->availableGeometry( this ); + + QString x; + if ( frame.left() == screen.left() ) + x = QString( "+0" ); + else if ( frame.right() == screen.right() ) + x = QString( "-0" ); + else + x = QString( "+%1" ).arg( frame.left() ); + + QString y; + if ( frame.top() == screen.top() ) + y = QString( "+0" ); + else if ( frame.bottom() == screen.bottom() ) + y = QString( "-0" ); + else + y = QString( "+%1" ).arg( frame.top() ); + + QString geom = QString( "%1x%2%3%4" ).arg( frame.width() ).arg( frame.height() ).arg( x ).arg( y ); + + QString state; + switch ( windowState() ) + { + case Qt::WindowMaximized: + state = QString( "max" ); + break; + case Qt::WindowMinimized: + state = QString( "min" ); + break; + case Qt::WindowFullScreen: + state = QString( "full" ); + break; + } + + if ( !state.isEmpty() ) + geom += QString( ":" ) + state; + + return geom; +} + +#include + +void QtxMainWindow::loadGeometry( const QString& str ) +{ + QString geom = str; + // geom.remove( '\t' ); + // geom.remove( ' ' ); + + QRect rect = geometry(); + QRect screen = QApplication::desktop()->availableGeometry( this ); + + QByteArray ba = geom.toLatin1(); + const char* s = (const char*)ba; + printf( "Geometry string: %s\n", s ); + + QRegExp szRx( "(\\d+%?)\\s*x\\s*(\\d+%?)" ); + if ( szRx.indexIn( geom ) != -1 ) + { + int w = -1; + bool wp = false; + int ws = geometryValue( szRx.cap( 1 ).trimmed(), w, wp ); + bool wOk = ws != 0; + if ( wOk && wp ) + w = screen.width() * qMax( qMin( w, 100 ), 0 ) / 100; + wOk = wOk && w; + + int h = -1; + bool hp = false; + int hs = geometryValue( szRx.cap( 2 ).trimmed(), h, hp ); + bool hOk = hs != 0; + if ( hOk && hp ) + h = screen.height() * qMax( qMin( h, 100 ), 0 ) / 100; + hOk = hOk && h; + + if ( wOk && hOk ) + rect.setSize( QSize( w, h ) ); + } + + QRegExp posRx( "([+|-]\\d+\%?)\\s*([+|-]\\d+\%?)" ); + if ( posRx.indexIn( geom ) != -1 ) + { + int x = -1; + bool xp = false; + int xs = geometryValue( posRx.cap( 1 ).trimmed(), x, xp ); + bool xOk = xs != 0; + if ( xOk ) + { + if ( xp ) + x = screen.width() * qMax( qMin( x, 100 ), 0 ) / 100; + x = ( xs > 0 ? x : screen.right() - x - rect.width() ) + frameGeometry().x() - geometry().x(); + } + + int y = -1; + bool yp = false; + int ys = geometryValue( posRx.cap( 2 ).trimmed(), y, yp ); + bool yOk = ys != 0; + if ( yOk ) + { + if ( yp ) + y = screen.height() * qMax( qMin( y, 100 ), 0 ) / 100; + y = ( ys > 0 ? y : screen.bottom() - y - rect.height() ) + frameGeometry().y() - geometry().y(); + } + + if ( xOk && yOk ) + rect.moveTo( x, y ); + } + + Qt::WindowState state = Qt::WindowNoState; + + QRegExp stRx( ":(\\w+)" ); + if ( stRx.indexIn( geom ) != -1 ) + { + QString stStr = stRx.cap( 1 ).trimmed().toLower(); + if ( stStr.startsWith( QString( "max" ) ) ) + state = Qt::WindowMaximized; + else if ( stStr.startsWith( QString( "min" ) ) ) + state = Qt::WindowMinimized; + else if ( stStr.startsWith( QString( "full" ) ) ) + state = Qt::WindowFullScreen; + } + + setGeometry( rect ); + if ( state != Qt::WindowNoState ) + setWindowState( state ); +} + +int QtxMainWindow::geometryValue( const QString& str, int& num, bool& percent ) const +{ + num = -1; + int res = 1; + QString numStr = str; + if ( numStr.startsWith( "+" ) || numStr.startsWith( "-" ) ) + { + res = numStr.startsWith( "+" ) ? 1 : -1; + numStr = numStr.mid( 1 ); + } + + percent = numStr.endsWith( "%" ); + if ( percent ) + numStr = numStr.mid( 0, numStr.length() - 1 ); + + bool ok = false; + num = numStr.toInt( &ok ); + if ( !ok ) + res = 0; + + return res; +} + /*! Retrieve the geometry information from the specified resource manager section. \param resMgr - instance of ersource manager @@ -203,7 +355,7 @@ void QtxMainWindow::loadGeometry( QtxResourceMgr* resMgr, const QString& section QString sec = section.trimmed(); if ( !resMgr || sec.isEmpty() ) return; - + /* int winState = -1; if ( !resMgr->value( sec, "state", winState ) ) { @@ -238,6 +390,7 @@ void QtxMainWindow::loadGeometry( QtxResourceMgr* resMgr, const QString& section move( win_x, win_y ); myMode = -1; + */ /* if ( vis ) QApplication::postEvent( this, new QEvent( QEvent::User, (void*)winState ) ); @@ -289,6 +442,7 @@ void QtxMainWindow::customEvent( QEvent* e ) \param wh - left point \param WH - right point */ +/* int QtxMainWindow::relativeCoordinate( const int type, const int WH, const int wh ) const { int res = 0; @@ -306,6 +460,7 @@ int QtxMainWindow::relativeCoordinate( const int type, const int WH, const int w } return res; } +*/ /*! Store the geometry information into the specified resource manager section. @@ -317,7 +472,7 @@ void QtxMainWindow::saveGeometry( QtxResourceMgr* resMgr, const QString& section QString sec = section.trimmed(); if ( !resMgr || sec.isEmpty() ) return; - + /* resMgr->setValue( sec, "pos_x", pos().x() ); resMgr->setValue( sec, "pos_y", pos().y() ); resMgr->setValue( sec, "width", width() ); @@ -330,6 +485,7 @@ void QtxMainWindow::saveGeometry( QtxResourceMgr* resMgr, const QString& section winState = WS_Maximized; resMgr->setValue( sec, "state", winState ); + */ } /*! @@ -368,6 +524,7 @@ void QtxMainWindow::onDestroyed( QObject* obj ) \return flag of window state by it's name \param str - name of flag */ +/* int QtxMainWindow::windowState( const QString& str ) const { static QMap winStateMap; @@ -392,11 +549,13 @@ int QtxMainWindow::windowState( const QString& str ) const res = winStateMap[stateStr]; return res; } +*/ /*! \return flag of position by it's name \param str - name of position */ +/* int QtxMainWindow::windowPosition( const QString& str ) const { static QMap winPosMap; @@ -415,3 +574,4 @@ int QtxMainWindow::windowPosition( const QString& str ) const res = winPosMap[posStr]; return res; } +*/ diff --git a/src/Qtx/QtxMainWindow.h b/src/Qtx/QtxMainWindow.h index e73c7331f..833a95e71 100644 --- a/src/Qtx/QtxMainWindow.h +++ b/src/Qtx/QtxMainWindow.h @@ -48,6 +48,9 @@ public: bool isDockableStatusBar() const; void setDockableStatusBar( const bool ); + QString saveGeometry() const; + void loadGeometry( const QString& ); + void loadGeometry( QtxResourceMgr*, const QString& ); void saveGeometry( QtxResourceMgr*, const QString& ) const; @@ -63,9 +66,10 @@ private slots: void onDestroyed( QObject* ); private: - int windowState( const QString& ) const; - int windowPosition( const QString& ) const; - int relativeCoordinate( const int, const int, const int ) const; + int geometryValue( const QString&, int&, bool& ) const; +// int windowState( const QString& ) const; +// int windowPosition( const QString& ) const; +// int relativeCoordinate( const int, const int, const int ) const; private: int myMode; diff --git a/src/Qtx/QtxResourceMgr.cxx b/src/Qtx/QtxResourceMgr.cxx index 4f0290811..5f6edf1c4 100644 --- a/src/Qtx/QtxResourceMgr.cxx +++ b/src/Qtx/QtxResourceMgr.cxx @@ -43,7 +43,7 @@ class QtxResourceMgr::Resources { public: - Resources( const QtxResourceMgr*, const QString& ); + Resources( QtxResourceMgr*, const QString& ); virtual ~Resources(); QString file() const; @@ -92,9 +92,9 @@ private: friend class QtxResourceMgr::Format; }; -QtxResourceMgr::Resources::Resources( const QtxResourceMgr* mgr, const QString& fileName ) -: myFileName( fileName ), - myMgr( const_cast( mgr ) ) +QtxResourceMgr::Resources::Resources( QtxResourceMgr* mgr, const QString& fileName ) +: myMgr( mgr ), + myFileName( fileName ) { } @@ -347,7 +347,7 @@ QTranslator* QtxResourceMgr::Resources::loadTranslator( const QString& sect, con if ( len ) { buf = new char[len]; - if ( !file.open( QIODevice::ReadOnly ) || len != (uint)file.read( buf, len ) ) + if ( !file.open( QIODevice::ReadOnly ) || len != (int)file.read( buf, len ) ) { delete buf; buf = 0; @@ -398,9 +398,6 @@ QString QtxResourceMgr::Resources::environmentVariable( const QString& str, int& QString varName = QString::null; len = 0; - QByteArray ba = str.toLatin1(); - const char* s = (const char*)ba; - 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.indexIn( str, start ); @@ -426,13 +423,6 @@ QString QtxResourceMgr::Resources::environmentVariable( const QString& str, int& end++; len = end - start; } -/* - start = pos; - len = rx.matchedLength(); - QStringList caps = rx.capturedTexts(); - for ( uint i = 1; i <= caps.count() && varName.isEmpty(); i++ ) - varName = caps[i]; -*/ } return varName; } @@ -1062,12 +1052,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 ) @@ -1203,21 +1193,37 @@ bool QtxResourceMgr::value( const QString& sect, const QString& name, QColor& cV return false; bool res = true; - QStringList vals = val.split( "," ); + 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 ) cVal = Qtx::rgbSet( pack ); } + if ( !res ) + { + cVal = QColor( val ); + res = cVal.isValid(); + } + return res; } @@ -1266,6 +1272,40 @@ bool QtxResourceMgr::value( const QString& sect, const QString& name, QFont& fVa return true; } +/*! + \brief Get the resource value as byte array. Returns 'true' if it successfull otherwise + returns 'false'. + \param sect - Resource section name which contains resource. + \param name - Name of the resource. + \param baVal - Reference on the variable which should contains the resource output. +*/ +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 the resource value as string (native format). Returns 'true' if it successfull otherwise returns 'false'. @@ -1386,6 +1426,21 @@ QString QtxResourceMgr::stringValue( const QString& sect, const QString& name, c return val; } +/*! + \brief Returns the byte array 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. +*/ +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 Checks existance of the specified resource. \param sect - Resource section name which contains resource. @@ -1474,7 +1529,7 @@ 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() ); } /*! @@ -1517,6 +1572,29 @@ void QtxResourceMgr::setValue( const QString& sect, const QString& name, const Q setResource( sect, name, val ); } +/*! + \brief Sets the string resource value. + \param sect - Resource section name. + \param name - Name of the resource. + \param val - Resource 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 the all specified resource section. \param sect - Resource section name. @@ -1893,7 +1971,7 @@ void QtxResourceMgr::loadLanguage( const QString& pref, const QString& l ) { QStringList translators = option( "translators" ).split( "|", QString::SkipEmptyParts ); QStringList newTranslators = trs.split( "|", QString::SkipEmptyParts ); - for ( uint i = 0; i < newTranslators.count(); i++ ) + for ( int i = 0; i < (int)newTranslators.count(); i++ ) { if ( translators.indexOf( newTranslators[i] ) < 0 ) translators += newTranslators[i]; diff --git a/src/Qtx/QtxResourceMgr.h b/src/Qtx/QtxResourceMgr.h index 1c9780f32..13f116fcd 100644 --- a/src/Qtx/QtxResourceMgr.h +++ b/src/Qtx/QtxResourceMgr.h @@ -23,11 +23,12 @@ #include #include +#include #include #include -#include #include +#include #include class QPixmap; @@ -82,6 +83,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; @@ -90,6 +92,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; @@ -100,6 +103,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& ); -- 2.39.2