diff options
Diffstat (limited to 'examples/demo/frame.cpp')
-rw-r--r-- | examples/demo/frame.cpp | 152 |
1 files changed, 152 insertions, 0 deletions
diff --git a/examples/demo/frame.cpp b/examples/demo/frame.cpp new file mode 100644 index 000000000..a712f7b4f --- /dev/null +++ b/examples/demo/frame.cpp @@ -0,0 +1,152 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for TQt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include "frame.h" + +#include <qapplication.h> +#include <qpopupmenu.h> +#include <qmenubar.h> +#include <qaccel.h> +#include <qtoolbox.h> +#include <qpainter.h> +#include <qwidgetstack.h> +#include <qstylefactory.h> +#include <qaction.h> +#include <qsignalmapper.h> +#include <qdict.h> +#include <qdir.h> +#include <qtextcodec.h> +#include <stdlib.h> +#include <qbuttongroup.h> +#include <qtoolbutton.h> + +static TQTranslator *translator = 0; +static TQTranslator *qt_translator = 0; + +Frame::Frame( TQWidget *parent, const char *name ) + : TQMainWindow( parent, name ) +{ + TQMenuBar *mainMenu = menuBar(); + TQPopupMenu *fileMenu = new TQPopupMenu( this, "file" ); + fileMenu->insertItem( tr( "&Exit" ), this, SLOT( close() ), + TQAccel::stringToKey( tr( "Ctrl+Q" ) ) ); + + TQPopupMenu *styleMenu = new TQPopupMenu( this, "style" ); + styleMenu->setCheckable( TRUE ); + TQActionGroup *ag = new TQActionGroup( this, 0 ); + ag->setExclusive( TRUE ); + TQSignalMapper *styleMapper = new TQSignalMapper( this ); + connect( styleMapper, SIGNAL( mapped( const TQString& ) ), + this, SLOT( setStyle( const TQString& ) ) ); + + TQStringList list = TQStyleFactory::keys(); + list.sort(); + TQDict<int> stylesDict( 17, FALSE ); + for ( TQStringList::Iterator it = list.begin(); it != list.end(); ++it ) { + TQString style = *it; + TQString styleAccel = style; + if ( stylesDict[styleAccel.left(1)] ) { + for ( uint i = 0; i < styleAccel.length(); i++ ) { + if ( !stylesDict[styleAccel.mid( i, 1 )] ) { + stylesDict.insert(styleAccel.mid( i, 1 ), (const int *)1); + styleAccel = styleAccel.insert( i, '&' ); + break; + } + } + } else { + stylesDict.insert(styleAccel.left(1), (const int *)1); + styleAccel = "&"+styleAccel; + } + TQAction *a = new TQAction( style, TQIconSet(), + styleAccel, 0, ag, 0, ag->isExclusive() ); + connect( a, SIGNAL( activated() ), styleMapper, SLOT(map()) ); + styleMapper->setMapping( a, a->text() ); + } + ag->addTo( styleMenu ); + + mainMenu->insertItem( tr( "&File" ), fileMenu ); + mainMenu->insertItem( tr( "St&yle" ), styleMenu ); + + stack = new TQWidgetStack( this ); + + setCentralWidget( stack ); +} + +void Frame::setCategories( const TQPtrList<CategoryInterface> &l ) +{ + categories = l; + TQDockWindow *dw = new TQDockWindow( TQDockWindow::InDock, this ); + dw->setResizeEnabled( TRUE ); + dw->setVerticalStretchable( TRUE ); + addDockWindow( dw, DockLeft ); + setDockEnabled( dw, DockTop, FALSE ); + setDockEnabled( dw, DockBottom, FALSE ); + dw->setCloseMode( TQDockWindow::Always ); + + toolBox = new TQToolBox( dw ); + dw->setWidget( toolBox ); + + dw->setCaption( tr( "Demo Categories" ) ); + + for ( int i = 0; i < categories.count(); ++i ) + toolBox->addItem( createCategoryPage( categories.at(i) ), + categories.at(i)->icon(), + categories.at(i)->name() ); + + categories.first()->setCurrentCategory( 0 ); +} + +TQWidget *Frame::createCategoryPage( CategoryInterface *c ) +{ + TQButtonGroup *g = new TQButtonGroup( 1, Horizontal, toolBox ); + g->setFrameStyle( TQFrame::NoFrame ); + g->setEraseColor(green); + g->setBackgroundMode(PaletteBase); + for ( int i = 0; i < c->numCategories(); ++i ) { + TQToolButton *b = new TQToolButton( g ); + b->setBackgroundMode(PaletteBase); + b->setTextLabel( c->categoryName( i ) ); + b->setIconSet( c->categoryIcon( i ) ); + b->setAutoRaise( TRUE ); + b->setTextPosition( TQToolButton::Right ); + b->setUsesTextLabel( TRUE ); + g->insert( b, i + c->categoryOffset() ); + connect( g, SIGNAL( clicked( int ) ), c, SLOT( setCurrentCategory( int ) ) ); + } + return g; +} + +void Frame::setStyle( const TQString& style ) +{ + TQStyle *s = TQStyleFactory::create( style ); + if ( s ) + TQApplication::setStyle( s ); +} + +void Frame::updateTranslators() +{ + if ( !qt_translator ) { + qt_translator = new TQTranslator( qApp ); + translator = new TQTranslator( qApp ); + qApp->installTranslator( qt_translator ); + qApp->installTranslator( translator ); + } + + TQString base = TQDir("../../translations").absPath(); + qt_translator->load( TQString( "qt_%1" ).arg( TQTextCodec::locale() ), base ); + translator->load( TQString( "translations/demo_%1" ).arg( TQTextCodec::locale() ) ); +} + +bool Frame::event( TQEvent *e ) +{ + if ( e->type() == TQEvent::LocaleChange ) + updateTranslators(); + + return TQMainWindow::event( e ); +} |