source: trunk/examples/demo/frame.cpp@ 168

Last change on this file since 168 was 160, checked in by dmik, 19 years ago

Imported table and iconview modules and a bunch of dependent examples from the official release 3.3.1 from Trolltech.

  • Property svn:keywords set to Id
File size: 4.7 KB
Line 
1/****************************************************************************
2** $Id: frame.cpp 160 2006-12-11 20:15:57Z dmik $
3**
4** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
5**
6** This file is part of an example program for Qt. This example
7** program may be used, distributed and modified without limitation.
8**
9*****************************************************************************/
10
11#include "frame.h"
12
13#include <qapplication.h>
14#include <qpopupmenu.h>
15#include <qmenubar.h>
16#include <qaccel.h>
17#include <qtoolbox.h>
18#include <qpainter.h>
19#include <qwidgetstack.h>
20#include <qstylefactory.h>
21#include <qaction.h>
22#include <qsignalmapper.h>
23#include <qdict.h>
24#include <qdir.h>
25#include <qtextcodec.h>
26#include <stdlib.h>
27#include <qbuttongroup.h>
28#include <qtoolbutton.h>
29
30static QTranslator *translator = 0;
31static QTranslator *qt_translator = 0;
32
33Frame::Frame( QWidget *parent, const char *name )
34 : QMainWindow( parent, name )
35{
36 QMenuBar *mainMenu = menuBar();
37 QPopupMenu *fileMenu = new QPopupMenu( this, "file" );
38 fileMenu->insertItem( tr( "&Exit" ), this, SLOT( close() ),
39 QAccel::stringToKey( tr( "Ctrl+Q" ) ) );
40
41 QPopupMenu *styleMenu = new QPopupMenu( this, "style" );
42 styleMenu->setCheckable( TRUE );
43 QActionGroup *ag = new QActionGroup( this, 0 );
44 ag->setExclusive( TRUE );
45 QSignalMapper *styleMapper = new QSignalMapper( this );
46 connect( styleMapper, SIGNAL( mapped( const QString& ) ),
47 this, SLOT( setStyle( const QString& ) ) );
48
49 QStringList list = QStyleFactory::keys();
50 list.sort();
51 QDict<int> stylesDict( 17, FALSE );
52 for ( QStringList::Iterator it = list.begin(); it != list.end(); ++it ) {
53 QString style = *it;
54 QString styleAccel = style;
55 if ( stylesDict[styleAccel.left(1)] ) {
56 for ( uint i = 0; i < styleAccel.length(); i++ ) {
57 if ( !stylesDict[styleAccel.mid( i, 1 )] ) {
58 stylesDict.insert(styleAccel.mid( i, 1 ), (const int *)1);
59 styleAccel = styleAccel.insert( i, '&' );
60 break;
61 }
62 }
63 } else {
64 stylesDict.insert(styleAccel.left(1), (const int *)1);
65 styleAccel = "&"+styleAccel;
66 }
67 QAction *a = new QAction( style, QIconSet(),
68 styleAccel, 0, ag, 0, ag->isExclusive() );
69 connect( a, SIGNAL( activated() ), styleMapper, SLOT(map()) );
70 styleMapper->setMapping( a, a->text() );
71 }
72 ag->addTo( styleMenu );
73
74 mainMenu->insertItem( tr( "&File" ), fileMenu );
75 mainMenu->insertItem( tr( "St&yle" ), styleMenu );
76
77 stack = new QWidgetStack( this );
78
79 setCentralWidget( stack );
80}
81
82void Frame::setCategories( const QPtrList<CategoryInterface> &l )
83{
84 categories = l;
85 QDockWindow *dw = new QDockWindow( QDockWindow::InDock, this );
86 dw->setResizeEnabled( TRUE );
87 dw->setVerticalStretchable( TRUE );
88 addDockWindow( dw, DockLeft );
89 setDockEnabled( dw, DockTop, FALSE );
90 setDockEnabled( dw, DockBottom, FALSE );
91 dw->setCloseMode( QDockWindow::Always );
92
93 toolBox = new QToolBox( dw );
94 dw->setWidget( toolBox );
95
96 dw->setCaption( tr( "Demo Categories" ) );
97
98 for ( int i = 0; i < categories.count(); ++i )
99 toolBox->addItem( createCategoryPage( categories.at(i) ),
100 categories.at(i)->icon(),
101 categories.at(i)->name() );
102
103 categories.first()->setCurrentCategory( 0 );
104}
105
106QWidget *Frame::createCategoryPage( CategoryInterface *c )
107{
108 QButtonGroup *g = new QButtonGroup( 1, Horizontal, toolBox );
109 g->setFrameStyle( QFrame::NoFrame );
110 g->setEraseColor(green);
111 g->setBackgroundMode(PaletteBase);
112 for ( int i = 0; i < c->numCategories(); ++i ) {
113 QToolButton *b = new QToolButton( g );
114 b->setBackgroundMode(PaletteBase);
115 b->setTextLabel( c->categoryName( i ) );
116 b->setIconSet( c->categoryIcon( i ) );
117 b->setAutoRaise( TRUE );
118 b->setTextPosition( QToolButton::Right );
119 b->setUsesTextLabel( TRUE );
120 g->insert( b, i + c->categoryOffset() );
121 connect( g, SIGNAL( clicked( int ) ), c, SLOT( setCurrentCategory( int ) ) );
122 }
123 return g;
124}
125
126void Frame::setStyle( const QString& style )
127{
128 QStyle *s = QStyleFactory::create( style );
129 if ( s )
130 QApplication::setStyle( s );
131}
132
133void Frame::updateTranslators()
134{
135 if ( !qt_translator ) {
136 qt_translator = new QTranslator( qApp );
137 translator = new QTranslator( qApp );
138 qApp->installTranslator( qt_translator );
139 qApp->installTranslator( translator );
140 }
141
142 QString base = QDir("../../translations").absPath();
143 qt_translator->load( QString( "qt_%1" ).arg( QTextCodec::locale() ), base );
144 translator->load( QString( "translations/demo_%1" ).arg( QTextCodec::locale() ) );
145}
146
147bool Frame::event( QEvent *e )
148{
149 if ( e->type() == QEvent::LocaleChange )
150 updateTranslators();
151
152 return QMainWindow::event( e );
153}
Note: See TracBrowser for help on using the repository browser.