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 
00010 #include <opie2/oapplicationfactory.h> // a template + macro to save the main method and allow quick launching
00011 #include <opie2/oresource.h>
00012 
00013 #include "simple.h"
00014 
00015 /*
00016  * implementation of simple
00017  */
00018 
00019 /*
00020  * The factory is used for quicklaunching
00021  * 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
00022  *
00023  * Depending on the global quick launch setting this will create
00024  * either a main method or one for our component plugin system
00025  */
00026 
00027 using namespace Opie::Core;
00028 OPIE_EXPORT_APP( OApplicationFactory<MainWindow> )
00029 
00030 MainWindow::MainWindow(QWidget *parent,  const char* name, WFlags fl )
00031     : QMainWindow( parent, name, fl ) {
00032     setCaption(tr("My MainWindow") );
00033     setIcon( Opie::Core::OResource::loadPixmap("new", Opie::Core::OResource::SmallIcon) );
00034     /*
00035      * out mainwindow should have a menubar
00036      * a toolbar, a mainwidget and use Resource
00037      * to get the IconSets
00038      */
00039     /*
00040      * we initialize the GUI in a different methid
00041      */
00042     initUI();
00043 
00044     Simple *simple = new Simple( this );
00045     setCentralWidget( simple );
00046 
00047     /*
00048      * If you use signal and slots do not include the parameter
00049      * names inside
00050      * so SIGNAL(fooBar(int) ) and NOT SIGNAL(fooBar(int foo) )
00051      */
00052     /*
00053      * We connect the activation of our QAction
00054      * to the slot connected to the firebutton
00055      * We could also connect the signal to the clicked
00056      * signal of the button
00057      */
00058     connect(m_fire, SIGNAL(activated() ),
00059             simple, SLOT(slotFire() ) );
00060 }
00061 
00062 MainWindow::~MainWindow() {
00063     // again nothing to delete because Qt takes care
00064 }
00065 
00066 /*
00067  * set Document is a special function used by Document
00068  * centric applications.
00069  * In example if Opie receives a Todo via IrDa it uses
00070  * setDocument via QCOP the IPC system to load the card
00071  * Or If you decide to open a file in filemanager with textedit
00072  * setDocument is called via IPC in textedit.
00073  * Also any call to QPE/Application/xyz and xyz is currently not running
00074  * leads to the start of xyz and delivering of the QCOP call
00075  * But more on QCOP in the next application
00076  */
00077 void MainWindow::setDocument( const QString& /*str*/ ) {
00078     // in our case empty but you should see if it is a direct
00079     // file request or if it is a DocLnk.
00080     // A DocLnk is Link to an Document so you would end up
00081     // opening the document behind the file you got
00082 }
00083 
00084 /*
00085  * Two new concepts with this Method
00086  * 1. QAction. An action can be grouped and emits
00087  *      an activated signal. Action a universal useable
00088  *      you can just plug/addTo in most Qt widgets. For example
00089  *      the same action can be plugged into a ToolBar, MenuBar,
00090  *      QPopupMenu and other widgets but you do not need to worry
00091  *      about it
00092  *
00093  * 2. an IconSet contains pixmaps and provides them scaled down, scaled up
00094  *    enabled and disabled. SO if you use QIConSet and the toolbar
00095  *    size changes to use big pixmaps you will not use upscaled icons
00096  *    but the right ones thanks to QIconSet and Resource
00097  */
00098 
00099 void MainWindow::initUI() {
00100 /*
00101  * We want to provde a File Menu with Quit as option
00102  * and a Fire Toolbutton ( QAction )
00103  * So we need two actions
00104  * A toolbar and a popupMenu
00105  */
00106     setToolBarsMovable( false );
00107     /*
00108      *We don't want the static toolbar but share it with the
00109      * toolbar on small screens
00110      */
00111     QToolBar *menuBarHolder = new QToolBar( this );
00112     /* we allow the menubarholder to become bigger than
00113      * the screen width and to offer a > for the additional items
00114      */
00115     menuBarHolder->setHorizontalStretchable( true );
00116     QMenuBar *mb = new QMenuBar( menuBarHolder );
00117     QToolBar *tb = new QToolBar( this );
00118 
00119     QPopupMenu *fileMenu = new QPopupMenu( this );
00120 
00121     /*
00122      * we create our first action with the Text Quit
00123      * a IconSet, no menu name, no acceleration ( keyboard shortcut ),
00124      * with parent this, and name "quit_action"
00125      */
00126     /*
00127      * Note if you want a picture out of  the inline directory
00128      * you musn't prefix inline/ inline means these pics are built in
00129      * into libqpe so the name without ending and directory is enough
00130      */
00131     QAction *a = new QAction( tr("Quit"), Opie::Core::OResource::loadPixmap("quit_icon", Opie::Core::OResource::SmallIcon),
00132                               QString::null, 0, this, "quit_action" );
00133     /*
00134      * Connect quit to the QApplication quit slot
00135      */
00136     connect(a, SIGNAL(activated() ),
00137             qApp, SLOT(quit() ) );
00138     a->addTo( fileMenu );
00139 
00140    a =  new QAction(tr("Fire"),
00141                              Opie::Core::OResource::loadPixmap("new", Opie::Core::OResource::SmallIcon),
00142                              QString::null, 0, this, "fire_button");
00143 
00144     /* see  the power? */
00145     a->addTo( fileMenu );
00146     a->addTo( tb );
00147     m_fire = a;
00148 
00149 
00150     mb->insertItem(tr("File"), fileMenu );
00151 
00152 }
00153 
00154 Simple::Simple( QWidget* parent, const char* name,  WFlags fl )
00155     : QWidget( parent, name, fl ) {
00156 
00157     /*
00158      * sets the caption of this toplevel widget
00159      * put all translatable string into tr()
00160      */
00161     setCaption(tr("My Simple Application") );
00162 
00163     /*
00164      * A simple vertical layout
00165      * either call layout->setAutoAdd( true )
00166      * or use layout->addWidget( wid ) to add widgets
00167      */
00168     QVBoxLayout *layout = new QVBoxLayout( this );
00169     layout->setSpacing( 8 );
00170     layout->setMargin( 11 );
00171 
00172     /*
00173      * creates a label
00174      * The first parameter is this widget so the Label is a child
00175      * of us and will be deleted when we're deleted.
00176      */
00177     QLabel *lbl = new QLabel( this, "a name for the label" );
00178     lbl->setText( tr("Click on the button or follow the white rabbit") );
00179     layout->addWidget( lbl );
00180 
00181 
00182     /*  creates a button as child of this widget */
00183     m_button = new QPushButton(this);
00184     /*
00185      * another way to call tr. The first parameter is the string
00186      * to translate and the second a hint to the translator
00187      */
00188     m_button->setText( tr("Fire", "translatable quit string" ) );
00189     layout->addWidget( m_button );
00190 
00191     /*
00192      * Now we bring the action into it. The power of qt is the dynamic
00193      * signal and slots model
00194      * Usage is simple connect m_buttons clicked signal to  our
00195      * slotQuit slot.
00196      * We could also have connected a SIGNAL to a SIGNAL or the clicked
00197      * signal directly to qApp and SLOT(quit() )
00198      */
00199     connect( m_button, SIGNAL(clicked() ),
00200              this, SLOT( slotFire() ) );
00201 }
00202 
00203 /*
00204  * Our destructor is empty because all child
00205  * widgets and layouts will be deleted by Qt.
00206  * Same applies to QObjects
00207  */
00208 Simple::~Simple() {
00209 
00210 }
00211 
00212 void Simple::slotFire() {
00213     /*
00214      * NOTE: Simple is now a child window of MainWindow
00215      * close will hide() Simple and not delete it. But as
00216      * the mainwindow is shown all children will be shown as well
00217      */
00218     close();
00219 }

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