From 4c3a1de5289631db05b86a07092f0a334608dcf6 Mon Sep 17 00:00:00 2001 From: zecke Date: Sat, 30 Aug 2003 20:23:05 +0000 Subject: Add four examples #1 simple widget + OApplicationFactory + qmake usage explained #2 simple icons + usage of Resource and playing of sounds #3 simple main + Toolbar + MenuBar IconSets and QActions explained #4 main tab + Usage of OTabWidget and QSignals with parameters disconnect and connect TODO #5 PIM loading and viewing #6 PIM qcop + usage of QCOP BuildSystem integration --- (limited to 'examples/main-tab') diff --git a/examples/main-tab/config.in b/examples/main-tab/config.in new file mode 100644 index 0000000..da50c37 --- a/dev/null +++ b/examples/main-tab/config.in @@ -0,0 +1,4 @@ +CONFIG EXAMPLE_SIMPLE + boolean "Simple MainWindow with Central Widget Example" + default "n" + depends (LIBQPE || LIBQPE-X11 ) && EXAMPLES \ No newline at end of file diff --git a/examples/main-tab/example.pro b/examples/main-tab/example.pro new file mode 100644 index 0000000..3cafc20 --- a/dev/null +++ b/examples/main-tab/example.pro @@ -0,0 +1,17 @@ +CONFIG += qt warn_on quick-app + + +TARGET = main-tab + +HEADERS = simple.h +SOURCES = simple.cpp + + +INCLUDEPATH += $(OPIEDIR)/include +DEPENDPATH += $(OPIEDIR)/include + + +# we now also include opie +LIBS += -lqpe -lopie + +include ( $(OPIEDIR)/include.pro ) diff --git a/examples/main-tab/opie-simple.control b/examples/main-tab/opie-simple.control new file mode 100644 index 0000000..8416ad2 --- a/dev/null +++ b/examples/main-tab/opie-simple.control @@ -0,0 +1,9 @@ +Package: opie-main-tab-example +Files: bin/main-tab apps/Examples/main-tab.desktop +Priority: optional +Section: opie/applications +Maintainer: Holger 'zecke' Freyther +Architecture: arm +Depends: task-opie-minimal, opie-pics +Description: A simple example +Version: $QPE_VERSION$EXTRAVERSION diff --git a/examples/main-tab/simple.cpp b/examples/main-tab/simple.cpp new file mode 100644 index 0000000..69dd00f --- a/dev/null +++ b/examples/main-tab/simple.cpp @@ -0,0 +1,211 @@ +#include // action +#include // menubar +#include // toolbar +#include // a label +#include // the header file for the QPushButton +#include + +#include // the QPEApplication +#include +#include + +#include // a template + macro to save the main method and allow quick launching +#include + +#include "simple.h" + +/* + * implementation of simple + */ + +/* + * The factory is used for quicklaunching + * 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 + * + * Depending on the global quick launch setting this will create + * either a main method or one for our component plugin system + */ + +OPIE_EXPORT_APP( OApplicationFactory ) + +MainWindow::MainWindow(QWidget *parent, const char* name, WFlags fl ) + : QMainWindow( parent, name, fl ) { + setCaption(tr("My MainWindow") ); + + initUI(); + + + /* + * Tab widget as central + */ + OTabWidget *tab = new OTabWidget(this); + connect(tab, SIGNAL(currentChanged(QWidget*) ), + this, SLOT( slotCurrentChanged( QWidget* ) ) ); + setCentralWidget( tab ); + + Simple1 *simple1 = new Simple1( this ); + tab->addTab( simple1, "new", tr("Simple1") ); + tab->setCurrentTab( tr("Simple1") ); + + Simple2 *simple2 = new Simple2( this ); + tab->addTab( simple2, "trash", tr("Simple2") ); + + m_oldCurrent = simple1; + + connect(m_fire, SIGNAL(activated() ), + simple1, SLOT(slotFire() ) ); +} + +MainWindow::~MainWindow() { + // again nothing to delete because Qt takes care +} + + +void MainWindow::setDocument( const QString& /*str*/ ) { +} +void MainWindow::slotCurrentChanged( QWidget *wid) { + disconnect(m_fire, SIGNAL(activated() ), + m_oldCurrent, SLOT(slotFire() ) ); + connect(m_fire, SIGNAL(activated() ), + wid, SLOT(slotFire() ) ); + + m_oldCurrent = wid; +} + +void MainWindow::initUI() { + + setToolBarsMovable( false ); + + QToolBar *menuBarHolder = new QToolBar( this ); + + menuBarHolder->setHorizontalStretchable( true ); + QMenuBar *mb = new QMenuBar( menuBarHolder ); + QToolBar *tb = new QToolBar( this ); + + QPopupMenu *fileMenu = new QPopupMenu( this ); + + + QAction *a = new QAction( tr("Quit"), Resource::loadIconSet("quit_icon"), + QString::null, 0, this, "quit_action" ); + /* + * Connect quit to the QApplication quit slot + */ + connect(a, SIGNAL(activated() ), + qApp, SLOT(quit() ) ); + a->addTo( fileMenu ); + + a = new QAction(tr("Fire"), + Resource::loadIconSet("new"), + QString::null, 0, this, "fire_button"); + + /* see the power? */ + a->addTo( fileMenu ); + a->addTo( tb ); + m_fire = a; + + + mb->insertItem(tr("File"), fileMenu ); + +} + +Simple1::Simple1( QWidget* parent, const char* name, WFlags fl ) + : QWidget( parent, name, fl ) { + + QVBoxLayout *layout = new QVBoxLayout( this ); + layout->setSpacing( 8 ); + layout->setMargin( 11 ); + + + QLabel *lbl = new QLabel( this, "a name for the label" ); + lbl->setText( tr("Click on the button or follow the white rabbit") ); + layout->addWidget( lbl ); + + + m_button = new QPushButton(this); + + + m_button->setText( tr("Fire", "translatable quit string" ) ); + layout->addWidget( m_button ); + + + connect( m_button, SIGNAL(clicked() ), + this, SLOT( slotFire() ) ); +} + +Simple1::~Simple1() { + +} + +void Simple1::slotFire() { + /* + * NOTE: Simple is now a child window of MainWindow + * close will hide() Simple and not delete it. But as + * the mainwindow is shown all children will be shown as well + */ + close(); +} + + +Simple2::Simple2( QWidget* parent, const char* name, WFlags fl ) + : QWidget( parent, name, fl ) { + + /* + * sets the caption of this toplevel widget + * put all translatable string into tr() + */ + setCaption(tr("My Simple Application") ); + + /* + * A simple vertical layout + * either call layout->setAutoAdd( true ) + * or use layout->addWidget( wid ) to add widgets + */ + QVBoxLayout *layout = new QVBoxLayout( this ); + layout->setSpacing( 8 ); + layout->setMargin( 11 ); + + /* + * creates a label + * The first parameter is this widget so the Label is a child + * of us and will be deleted when we're deleted. + */ + QLabel *lbl = new QLabel( this, "a name for the label" ); + /* + * Resource will search hard for a Pixmap in $OPIEDIR/pics + * to find 'logo/opielogo' You need to pass the subdir + * but not the ending + */ + lbl->setPixmap( Resource::loadPixmap("logo/opielogo") ); + layout->addWidget( lbl ); + + + /* creates a button as child of this widget */ + m_button = new QPushButton(this); + /* + * another way to call tr. The first parameter is the string + * to translate and the second a hint to the translator + */ + m_button->setText( tr("Fire", "translatable fire string" ) ); + layout->addWidget( m_button ); + + + connect( m_button, SIGNAL(clicked() ), + this, SLOT( slotQuit() ) ); +} + + +Simple2::~Simple2() { + +} + +void Simple2::slotFire() { + /* + * We will fire up a sound + * Note that Sound will use Resource as well + * and we do not need to supply an ending + * sounds are found in $OPIEDIR/sounds + */ + Sound snd("hit_target01"); + snd.play(); + +} diff --git a/examples/main-tab/simple.h b/examples/main-tab/simple.h new file mode 100644 index 0000000..e0e43c0 --- a/dev/null +++ b/examples/main-tab/simple.h @@ -0,0 +1,87 @@ + +/* + * A Simple widget with a button to quit + * + */ + +/* + * The below sequence is called a guard and guards + * against multiple inclusion of header files + * NOTE: you need to use unique names among the header files + */ +#ifndef QUIET_SIMPLE_DEMO_H +#define QUIET_SIMPLE_DEMO_H + + + + +#include // from this class we will inherit + + +class QPushButton; // forward declaration to not include the header. This can save time when compiling +class QAction; + +/* + * A mainwindow is a special QWidget it helps layouting + * toolbar, statusbar, menubar. Got dockable areas + * So in one sentence it is a MainWindow :) + */ +class MainWindow : public QMainWindow { + Q_OBJECT +public: + static QString appName() { return QString::fromLatin1("main-tab"); } + MainWindow( QWidget* parent, const char* name, WFlags fl ); + ~MainWindow(); + +public slots: + void setDocument( const QString& ); +private slots: + void slotCurrentChanged( QWidget* wid ); + +private: + void initUI(); + QAction *m_fire; + QWidget* m_oldCurrent; +}; + + +/* + * We will just reuse the two simple widgets for now + */ +class Simple1 : public QWidget { + + Q_OBJECT +public: + + Simple1( QWidget* parent = 0, const char * name = 0, WFlags fl = 0 ); + ~Simple1(); + + +public slots: + void slotFire(); + +private: + /* my variable */ + QPushButton* m_button; +}; + +class Simple2 : public QWidget { + Q_OBJECT +public: + + Simple2( QWidget* parent = 0, const char * name = 0, WFlags fl = 0 ); + ~Simple2(); + + + +public slots: + void slotFire(); + +private: + /* my variable */ + QPushButton* m_button; +}; + + + +#endif -- cgit v0.9.0.2