Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

simple.cpp

Go to the documentation of this file.
00001 #include <qaction.h> // action
00002 #include <qmenubar.h> // menubar
00003 #include <qtoolbar.h> // toolbar
00004 #include <qlabel.h> // a label
00005 #include <qpushbutton.h> // the header file for the QPushButton
00006 #include <qlayout.h>
00007 
00008 #include <qpe/qpeapplication.h> // the QPEApplication
00009 #include <qpe/sound.h>
00010 
00011 #include <opie2/oapplicationfactory.h> // a template + macro to save the main method and allow quick launching
00012 #include <opie2/oresource.h>
00013 #include <opie2/otabwidget.h>
00014 
00015 #include "simple.h"
00016 
00017 /*
00018  * implementation of simple
00019  */
00020 
00021 /*
00022  * The factory is used for quicklaunching
00023  * It needs a constructor ( c'tor ) with at least QWidget, const char* and WFlags as parameter and a static QString appName() matching the TARGET of the .pro
00024  *
00025  * Depending on the global quick launch setting this will create
00026  * either a main method or one for our component plugin system
00027  */
00028 
00029 /* The OApplicationFactory is in the Opie::Core namespace */
00030 using namespace Opie::Core; 
00031 OPIE_EXPORT_APP( OApplicationFactory<MainWindow> )
00032 
00033 MainWindow::MainWindow(QWidget *parent,  const char* name, WFlags fl )
00034     : QMainWindow( parent, name, fl ) {
00035     setCaption(tr("My MainWindow") );
00036 
00037     initUI();
00038 
00039 
00040     /*
00041      * Tab widget as central
00042      */
00043     Opie::Ui::OTabWidget *tab = new Opie::Ui::OTabWidget(this);
00044     connect(tab, SIGNAL(currentChanged(QWidget*) ),
00045             this, SLOT( slotCurrentChanged(QWidget*) ) );
00046     setCentralWidget( tab );
00047 
00048     Simple1 *simple1 = new Simple1( this );
00049     tab->addTab( simple1, "new",  tr("Simple1") );
00050     tab->setCurrentTab( tr("Simple1") );
00051 
00052     Simple2 *simple2 = new Simple2( this );
00053     tab->addTab( simple2, "trash", tr("Simple2") );
00054 
00055     m_oldCurrent = simple1;
00056 
00057     connect(m_fire, SIGNAL(activated() ),
00058             simple1, SLOT(slotFire() ) );
00059 }
00060 
00061 MainWindow::~MainWindow() {
00062     // again nothing to delete because Qt takes care
00063 }
00064 
00065 
00066 void MainWindow::setDocument( const QString& /*str*/ ) {
00067 }
00068 void MainWindow::slotCurrentChanged( QWidget *wid) {
00069     disconnect(m_fire, SIGNAL(activated() ),
00070                m_oldCurrent, SLOT(slotFire() ) );
00071     connect(m_fire, SIGNAL(activated() ),
00072             wid, SLOT(slotFire() ) );
00073 
00074     m_oldCurrent = wid;
00075 }
00076 
00077 void MainWindow::initUI() {
00078 
00079     setToolBarsMovable( false );
00080 
00081     QToolBar *menuBarHolder = new QToolBar( this );
00082 
00083     menuBarHolder->setHorizontalStretchable( true );
00084     QMenuBar *mb = new QMenuBar( menuBarHolder );
00085     QToolBar *tb = new QToolBar( this );
00086 
00087     QPopupMenu *fileMenu = new QPopupMenu( this );
00088 
00089 
00090     QAction *a = new QAction( tr("Quit"), Opie::Core::OResource::loadPixmap("quit_icon", Opie::Core::OResource::SmallIcon),
00091                               QString::null, 0, this, "quit_action" );
00092     /*
00093      * Connect quit to the QApplication quit slot
00094      */
00095     connect(a, SIGNAL(activated() ),
00096             qApp, SLOT(quit() ) );
00097     a->addTo( fileMenu );
00098 
00099    a =  new QAction(tr("Fire"), Opie::Core::OResource::loadPixmap("new", Opie::Core::OResource::SmallIcon),
00100                              QString::null, 0, this, "fire_button");
00101 
00102     /* see  the power? */
00103     a->addTo( fileMenu );
00104     a->addTo( tb );
00105     m_fire = a;
00106 
00107 
00108     mb->insertItem(tr("File"), fileMenu );
00109 
00110 }
00111 
00112 Simple1::Simple1( QWidget* parent, const char* name,  WFlags fl )
00113     : QWidget( parent, name, fl ) {
00114 
00115     QVBoxLayout *layout = new QVBoxLayout( this );
00116     layout->setSpacing( 8 );
00117     layout->setMargin( 11 );
00118 
00119 
00120     QLabel *lbl = new QLabel( this, "a name for the label" );
00121     lbl->setText( tr("Click on the button or follow the white rabbit") );
00122     layout->addWidget( lbl );
00123 
00124 
00125     m_button = new QPushButton(this);
00126 
00127 
00128     m_button->setText( tr("Fire", "translatable quit string" ) );
00129     layout->addWidget( m_button );
00130 
00131 
00132     connect( m_button, SIGNAL(clicked() ),
00133              this, SLOT( slotFire() ) );
00134 }
00135 
00136 Simple1::~Simple1() {
00137 
00138 }
00139 
00140 void Simple1::slotFire() {
00141     /*
00142      * NOTE: Simple is now a child window of MainWindow
00143      * close will hide() Simple and not delete it. But as
00144      * the mainwindow is shown all children will be shown as well
00145      */
00146     close();
00147 }
00148 
00149 
00150 Simple2::Simple2( QWidget* parent, const char* name,  WFlags fl )
00151     : QWidget( parent, name, fl ) {
00152 
00153     /*
00154      * sets the caption of this toplevel widget
00155      * put all translatable string into tr()
00156      */
00157     setCaption(tr("My Simple Application") );
00158 
00159     /*
00160      * A simple vertical layout
00161      * either call layout->setAutoAdd( true )
00162      * or use layout->addWidget( wid ) to add widgets
00163      */
00164     QVBoxLayout *layout = new QVBoxLayout( this );
00165     layout->setSpacing( 8 );
00166     layout->setMargin( 11 );
00167 
00168     /*
00169      * creates a label
00170      * The first parameter is this widget so the Label is a child
00171      * of us and will be deleted when we're deleted.
00172      */
00173     QLabel *lbl = new QLabel( this, "a name for the label" );
00174     /*
00175      * OResource will search hard for a Pixmap in $OPIEDIR/pics
00176      * to find 'logo/opielogo' You need to pass the subdir
00177      * but not the ending
00178      */
00179     lbl->setPixmap( Opie::Core::OResource::loadPixmap("logo/opielogo", Opie::Core::OResource::SmallIcon) );
00180     layout->addWidget( lbl );
00181 
00182 
00183     /*  creates a button as child of this widget */
00184     m_button = new QPushButton(this);
00185     /*
00186      * another way to call tr. The first parameter is the string
00187      * to translate and the second a hint to the translator
00188      */
00189     m_button->setText( tr("Fire", "translatable fire string" ) );
00190     layout->addWidget( m_button );
00191 
00192 
00193     connect( m_button, SIGNAL(clicked() ),
00194              this, SLOT( slotQuit() ) );
00195 }
00196 
00197 
00198 Simple2::~Simple2() {
00199 
00200 }
00201 
00202 void Simple2::slotFire() {
00203     /*
00204      * We will fire up a sound
00205      * Note that Sound will use Resource as well
00206      * and we do not need to supply an ending
00207      * sounds are found in $OPIEDIR/sounds
00208      */
00209     Sound snd("hit_target01");
00210     snd.play();
00211 
00212 }

Generated on Sat Nov 5 16:15:58 2005 for OPIE by  doxygen 1.4.2