-rw-r--r-- | examples/simple-main/config.in | 4 | ||||
-rw-r--r-- | examples/simple-main/example.pro | 17 | ||||
-rw-r--r-- | examples/simple-main/opie-simple.control | 9 | ||||
-rw-r--r-- | examples/simple-main/simple.cpp | 218 | ||||
-rw-r--r-- | examples/simple-main/simple.h | 81 |
5 files changed, 329 insertions, 0 deletions
diff --git a/examples/simple-main/config.in b/examples/simple-main/config.in new file mode 100644 index 0000000..311e594 --- a/dev/null +++ b/examples/simple-main/config.in | |||
@@ -0,0 +1,4 @@ | |||
1 | CONFIG EXAMPLE_SIMPLE | ||
2 | boolean "Simple MainWindow Example" | ||
3 | default "n" | ||
4 | depends (LIBQPE || LIBQPE-X11 ) && EXAMPLES \ No newline at end of file | ||
diff --git a/examples/simple-main/example.pro b/examples/simple-main/example.pro new file mode 100644 index 0000000..2f5f10f --- a/dev/null +++ b/examples/simple-main/example.pro | |||
@@ -0,0 +1,17 @@ | |||
1 | CONFIG += qt warn_on quick-app | ||
2 | |||
3 | |||
4 | TARGET = simple-main | ||
5 | |||
6 | HEADERS = simple.h | ||
7 | SOURCES = simple.cpp | ||
8 | |||
9 | |||
10 | INCLUDEPATH += $(OPIEDIR)/include | ||
11 | DEPENDPATH += $(OPIEDIR)/include | ||
12 | |||
13 | |||
14 | |||
15 | LIBS += -lqpe | ||
16 | |||
17 | include ( $(OPIEDIR)/include.pro ) | ||
diff --git a/examples/simple-main/opie-simple.control b/examples/simple-main/opie-simple.control new file mode 100644 index 0000000..63622d5 --- a/dev/null +++ b/examples/simple-main/opie-simple.control | |||
@@ -0,0 +1,9 @@ | |||
1 | Package: opie-simple-main-example | ||
2 | Files: bin/simple-main apps/Examples/simple-main.desktop | ||
3 | Priority: optional | ||
4 | Section: opie/applications | ||
5 | Maintainer: Holger 'zecke' Freyther <zecke@handhelds.org> | ||
6 | Architecture: arm | ||
7 | Depends: task-opie-minimal, opie-pics | ||
8 | Description: A simple example | ||
9 | Version: $QPE_VERSION$EXTRAVERSION | ||
diff --git a/examples/simple-main/simple.cpp b/examples/simple-main/simple.cpp new file mode 100644 index 0000000..8de911f --- a/dev/null +++ b/examples/simple-main/simple.cpp | |||
@@ -0,0 +1,218 @@ | |||
1 | #include <qaction.h> // action | ||
2 | #include <qmenubar.h> // menubar | ||
3 | #include <qtoolbar.h> // toolbar | ||
4 | #include <qlabel.h> // a label | ||
5 | #include <qpushbutton.h> // the header file for the QPushButton | ||
6 | #include <qlayout.h> | ||
7 | |||
8 | #include <qpe/qpeapplication.h> // the QPEApplication | ||
9 | #include <qpe/resource.h> | ||
10 | |||
11 | #include <opie/oapplicationfactory.h> // a template + macro to save the main method and allow quick launching | ||
12 | |||
13 | #include "simple.h" | ||
14 | |||
15 | /* | ||
16 | * implementation of simple | ||
17 | */ | ||
18 | |||
19 | /* | ||
20 | * The factory is used for quicklaunching | ||
21 | * 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 | ||
22 | * | ||
23 | * Depending on the global quick launch setting this will create | ||
24 | * either a main method or one for our component plugin system | ||
25 | */ | ||
26 | |||
27 | OPIE_EXPORT_APP( OApplicationFactory<MainWindow> ) | ||
28 | |||
29 | MainWindow::MainWindow(QWidget *parent, const char* name, WFlags fl ) | ||
30 | : QMainWindow( parent, name, fl ) { | ||
31 | setCaption(tr("My MainWindow") ); | ||
32 | setIcon( Resource::loadPixmap("new") ); | ||
33 | /* | ||
34 | * out mainwindow should have a menubar | ||
35 | * a toolbar, a mainwidget and use Resource | ||
36 | * to get the IconSets | ||
37 | */ | ||
38 | /* | ||
39 | * we initialize the GUI in a different methid | ||
40 | */ | ||
41 | initUI(); | ||
42 | |||
43 | Simple *simple = new Simple( this ); | ||
44 | setCentralWidget( simple ); | ||
45 | |||
46 | /* | ||
47 | * If you use signal and slots do not include the parameter | ||
48 | * names inside | ||
49 | * so SIGNAL(fooBar(int) ) and NOT SIGNAL(fooBar(int foo) ) | ||
50 | */ | ||
51 | /* | ||
52 | * We connect the activation of our QAction | ||
53 | * to the slot connected to the firebutton | ||
54 | * We could also connect the signal to the clicked | ||
55 | * signal of the button | ||
56 | */ | ||
57 | connect(m_fire, SIGNAL(activated() ), | ||
58 | simple, SLOT(slotFire() ) ); | ||
59 | } | ||
60 | |||
61 | MainWindow::~MainWindow() { | ||
62 | // again nothing to delete because Qt takes care | ||
63 | } | ||
64 | |||
65 | /* | ||
66 | * set Document is a special function used by Document | ||
67 | * centric applications. | ||
68 | * In example if Opie receives a Todo via IrDa it uses | ||
69 | * setDocument via QCOP the IPC system to load the card | ||
70 | * Or If you decide to open a file in filemanager with textedit | ||
71 | * setDocument is called via IPC in textedit. | ||
72 | * Also any call to QPE/Application/xyz and xyz is currently not running | ||
73 | * leads to the start of xyz and delivering of the QCOP call | ||
74 | * But more on QCOP in the next application | ||
75 | */ | ||
76 | void MainWindow::setDocument( const QString& /*str*/ ) { | ||
77 | // in our case empty but you should see if it is a direct | ||
78 | // file request or if it is a DocLnk. | ||
79 | // A DocLnk is Link to an Document so you would end up | ||
80 | // opening the document behind the file you got | ||
81 | } | ||
82 | |||
83 | /* | ||
84 | * Two new concepts with this Method | ||
85 | * 1. QAction. An action can be grouped and emits | ||
86 | * an activated signal. Action a universal useable | ||
87 | * you can just plug/addTo in most Qt widgets. For example | ||
88 | * the same action can be plugged into a ToolBar, MenuBar, | ||
89 | * QPopupMenu and other widgets but you do not need to worry | ||
90 | * about it | ||
91 | * | ||
92 | * 2. an IconSet contains pixmaps and provides them scaled down, scaled up | ||
93 | * enabled and disabled. SO if you use QIConSet and the toolbar | ||
94 | * size changes to use big pixmaps you will not use upscaled icons | ||
95 | * but the right ones thanks to QIconSet and Resource | ||
96 | */ | ||
97 | |||
98 | void MainWindow::initUI() { | ||
99 | /* | ||
100 | * We want to provde a File Menu with Quit as option | ||
101 | * and a Fire Toolbutton ( QAction ) | ||
102 | * So we need two actions | ||
103 | * A toolbar and a popupMenu | ||
104 | */ | ||
105 | setToolBarsMovable( false ); | ||
106 | /* | ||
107 | *We don't want the static toolbar but share it with the | ||
108 | * toolbar on small screens | ||
109 | */ | ||
110 | QToolBar *menuBarHolder = new QToolBar( this ); | ||
111 | /* we allow the menubarholder to become bigger than | ||
112 | * the screen width and to offer a > for the additional items | ||
113 | */ | ||
114 | menuBarHolder->setHorizontalStretchable( true ); | ||
115 | QMenuBar *mb = new QMenuBar( menuBarHolder ); | ||
116 | QToolBar *tb = new QToolBar( this ); | ||
117 | |||
118 | QPopupMenu *fileMenu = new QPopupMenu( this ); | ||
119 | |||
120 | /* | ||
121 | * we create our first action with the Text Quit | ||
122 | * a IconSet, no menu name, no acceleration ( keyboard shortcut ), | ||
123 | * with parent this, and name "quit_action" | ||
124 | */ | ||
125 | /* | ||
126 | * Note if you want a picture out of the inline directory | ||
127 | * you musn't prefix inline/ inline means these pics are built in | ||
128 | * into libqpe so the name without ending and directory is enough | ||
129 | */ | ||
130 | QAction *a = new QAction( tr("Quit"), Resource::loadIconSet("quit_icon"), | ||
131 | QString::null, 0, this, "quit_action" ); | ||
132 | /* | ||
133 | * Connect quit to the QApplication quit slot | ||
134 | */ | ||
135 | connect(a, SIGNAL(activated() ), | ||
136 | qApp, SLOT(quit() ) ); | ||
137 | a->addTo( fileMenu ); | ||
138 | |||
139 | a = new QAction(tr("Fire"), | ||
140 | Resource::loadIconSet("new"), | ||
141 | QString::null, 0, this, "fire_button"); | ||
142 | |||
143 | /* see the power? */ | ||
144 | a->addTo( fileMenu ); | ||
145 | a->addTo( tb ); | ||
146 | m_fire = a; | ||
147 | |||
148 | |||
149 | mb->insertItem(tr("File"), fileMenu ); | ||
150 | |||
151 | } | ||
152 | |||
153 | Simple::Simple( QWidget* parent, const char* name, WFlags fl ) | ||
154 | : QWidget( parent, name, fl ) { | ||
155 | |||
156 | /* | ||
157 | * sets the caption of this toplevel widget | ||
158 | * put all translatable string into tr() | ||
159 | */ | ||
160 | setCaption(tr("My Simple Application") ); | ||
161 | |||
162 | /* | ||
163 | * A simple vertical layout | ||
164 | * either call layout->setAutoAdd( true ) | ||
165 | * or use layout->addWidget( wid ) to add widgets | ||
166 | */ | ||
167 | QVBoxLayout *layout = new QVBoxLayout( this ); | ||
168 | layout->setSpacing( 8 ); | ||
169 | layout->setMargin( 11 ); | ||
170 | |||
171 | /* | ||
172 | * creates a label | ||
173 | * The first parameter is this widget so the Label is a child | ||
174 | * of us and will be deleted when we're deleted. | ||
175 | */ | ||
176 | QLabel *lbl = new QLabel( this, "a name for the label" ); | ||
177 | lbl->setText( tr("Click on the button or follow the white rabbit") ); | ||
178 | layout->addWidget( lbl ); | ||
179 | |||
180 | |||
181 | /* creates a button as child of this widget */ | ||
182 | m_button = new QPushButton(this); | ||
183 | /* | ||
184 | * another way to call tr. The first parameter is the string | ||
185 | * to translate and the second a hint to the translator | ||
186 | */ | ||
187 | m_button->setText( tr("Fire", "translatable quit string" ) ); | ||
188 | layout->addWidget( m_button ); | ||
189 | |||
190 | /* | ||
191 | * Now we bring the action into it. The power of qt is the dynamic | ||
192 | * signal and slots model | ||
193 | * Usage is simple connect m_buttons clicked signal to our | ||
194 | * slotQuit slot. | ||
195 | * We could also have connected a SIGNAL to a SIGNAL or the clicked | ||
196 | * signal directly to qApp and SLOT(quit() ) | ||
197 | */ | ||
198 | connect( m_button, SIGNAL(clicked() ), | ||
199 | this, SLOT( slotFire() ) ); | ||
200 | } | ||
201 | |||
202 | /* | ||
203 | * Our destructor is empty because all child | ||
204 | * widgets and layouts will be deleted by Qt. | ||
205 | * Same applies to QObjects | ||
206 | */ | ||
207 | Simple::~Simple() { | ||
208 | |||
209 | } | ||
210 | |||
211 | void Simple::slotFire() { | ||
212 | /* | ||
213 | * NOTE: Simple is now a child window of MainWindow | ||
214 | * close will hide() Simple and not delete it. But as | ||
215 | * the mainwindow is shown all children will be shown as well | ||
216 | */ | ||
217 | close(); | ||
218 | } | ||
diff --git a/examples/simple-main/simple.h b/examples/simple-main/simple.h new file mode 100644 index 0000000..f75066e --- a/dev/null +++ b/examples/simple-main/simple.h | |||
@@ -0,0 +1,81 @@ | |||
1 | |||
2 | /* | ||
3 | * A Simple widget with a button to quit | ||
4 | * | ||
5 | */ | ||
6 | |||
7 | /* | ||
8 | * The below sequence is called a guard and guards | ||
9 | * against multiple inclusion of header files | ||
10 | * NOTE: you need to use unique names among the header files | ||
11 | */ | ||
12 | #ifndef QUIET_SIMPLE_DEMO_H | ||
13 | #define QUIET_SIMPLE_DEMO_H | ||
14 | |||
15 | |||
16 | |||
17 | |||
18 | #include <qmainwindow.h> // from this class we will inherit | ||
19 | |||
20 | |||
21 | class QPushButton; // forward declaration to not include the header. This can save time when compiling | ||
22 | class QAction; | ||
23 | |||
24 | /* | ||
25 | * A mainwindow is a special QWidget it helps layouting | ||
26 | * toolbar, statusbar, menubar. Got dockable areas | ||
27 | * So in one sentence it is a MainWindow :) | ||
28 | */ | ||
29 | class MainWindow : public QMainWindow { | ||
30 | Q_OBJECT | ||
31 | public: | ||
32 | static QString appName() { return QString::fromLatin1("simple-main"); } | ||
33 | MainWindow( QWidget* parent, const char* name, WFlags fl ); | ||
34 | ~MainWindow(); | ||
35 | |||
36 | public slots: | ||
37 | void setDocument( const QString& ); | ||
38 | |||
39 | private: | ||
40 | void initUI(); | ||
41 | QAction *m_fire; | ||
42 | }; | ||
43 | |||
44 | |||
45 | /* | ||
46 | * Simple inherits from QWidget | ||
47 | */ | ||
48 | class Simple : public QWidget { | ||
49 | /* | ||
50 | * Q_OBJECT must always be the first thing you include | ||
51 | * This is a macro and is responsible for the concepts of | ||
52 | * dynamic signal and slots and other MetaObjects as | ||
53 | * superClass(), inherits(), isA()... | ||
54 | * If you use multiple inheritance include the class derived | ||
55 | * from QObject first | ||
56 | */ | ||
57 | Q_OBJECT | ||
58 | public: | ||
59 | /* | ||
60 | * C'tor for the Simple | ||
61 | * make sure to always have these three when you use | ||
62 | * the quicklaunch factory ( explained in the implementation ) | ||
63 | */ | ||
64 | Simple( QWidget* parent = 0, const char * name = 0, WFlags fl = 0 ); | ||
65 | ~Simple(); | ||
66 | |||
67 | /* | ||
68 | * We now make it public because our mainwindow wants to call it | ||
69 | */ | ||
70 | public slots: | ||
71 | void slotFire(); | ||
72 | |||
73 | private: | ||
74 | /* my variable */ | ||
75 | QPushButton* m_button; | ||
76 | }; | ||
77 | |||
78 | |||
79 | |||
80 | |||
81 | #endif | ||