dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
main.cpp
Go to the documentation of this file.
1 
13 /*
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 3 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful, but
20  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  * for more details.
23  *
24  * You should have received a copy of the GNU General Public License along
25  * with this program; if not, see <http://www.gnu.org/licenses/>
26  */
27 
28 #include "main.h"
29 
30 #include <QtGui/QApplication>
31 
32 MyMain::MyMain(QWidget *parent, Qt::WFlags flags)
33  : QWidget(parent, flags)
34 {
35  ui.setupUi(this);
36  connect(ui.comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(select(int)));
37 }
38 
40 {
41  m_entries.append(obj);
42  ui.comboBox->addItem(obj->title());
43 }
44 
45 void MyMain::select(int index)
46 {
47  IComboEntry *entry = m_entries.at(index);
48  // with multiple inheritance we would use qobject_cast here
49  // instead we use query, to get the components if they exist
50  IText1 *t1 = Aggregation::query<IText1>(entry);
51  IText2 *t2 = Aggregation::query<IText2>(entry);
52  IText3 *t3 = Aggregation::query<IText3>(entry);
53  // set the label texts and enable/disable, depending on whether
54  // the respective interface implementations exist
55  ui.text1->setText(t1 ? t1->text() : tr("N/A"));
56  ui.text2->setText(t2 ? t2->text() : tr("N/A"));
57  ui.text3->setText(t3 ? t3->text() : tr("N/A"));
58  ui.text1->setEnabled(t1);
59  ui.text2->setEnabled(t2);
60  ui.text3->setEnabled(t3);
61 }
62 
64 {
65  // the following deletes all the Aggregate and IComboEntry and ITextX
66  // objects, as well as any other components we might have added
67  qDeleteAll(m_entries);
68 }
69 
70 int main(int argc, char *argv[])
71 {
72  QApplication app(argc, argv);
73  MyMain w;
74  // create and set up some objects
75 
76  // the first does only implement the required IComboEntry
77  // we don't need an aggregation for this
78  IComboEntry *obj1 = new IComboEntry("Entry without text");
79 
80  // the second additionally provides an IText2 implementation
81  // adding a component to the aggregation is done by setting the aggregation as the parent of the component
83  obj2->add(new IComboEntry("Entry with text 2"));
84  obj2->add(new IText2("This is a text for label 2"));
85 
86  // and so on... two more objects...
88  obj3->add(new IComboEntry("Entry with text 1 and 2"));
89  obj3->add(new IText1("I love Qt!"));
90  obj3->add(new IText2("There are software companies..."));
92  obj4->add(new IComboEntry("Entry with text 1 and 3"));
93  obj4->add(new IText1("Some text written here."));
94  obj4->add(new IText3("I'm a troll."));
95 
96  // the API takes IComboEntries, so we convert the them to it
97  // the MyMain object takes the ownership of the whole aggregations
98  w.add(Aggregation::query<IComboEntry>(obj1));
99  w.add(Aggregation::query<IComboEntry>(obj2));
100  w.add(Aggregation::query<IComboEntry>(obj3));
101  w.add(Aggregation::query<IComboEntry>(obj4));
102  w.show();
103  return app.exec();
104 }
void add(IComboEntry *obj)
Definition: main.cpp:39
~MyMain()
Definition: main.cpp:63
int main(int argc, char *argv[])
Definition: main.cpp:70
Defines a collection of related components that can be viewed as a unit.
Definition: aggregate.h:41
QString title() const
Definition: myinterfaces.h:42
Definition: main.h:38
void add(QObject *component)
Definition: aggregate.cpp:229
Definition: myinterfaces.h:35
MyMain(QWidget *parent=0, Qt::WFlags flags=0)
Definition: main.cpp:32
QString text() const
Definition: myinterfaces.h:55