diff --git a/3643.pdf b/3643.pdf new file mode 100644 index 0000000..e7abc73 Binary files /dev/null and b/3643.pdf differ diff --git a/3645.pdf b/3645.pdf new file mode 100644 index 0000000..11999e4 Binary files /dev/null and b/3645.pdf differ diff --git a/9781590598313.jpg b/9781590598313.jpg new file mode 100644 index 0000000..ddde254 Binary files /dev/null and b/9781590598313.jpg differ diff --git a/Chapter01/README.txt b/Chapter01/README.txt new file mode 100644 index 0000000..e686bd3 --- /dev/null +++ b/Chapter01/README.txt @@ -0,0 +1,69 @@ +This file is a part of 1590598318-1.zip containing example source code for the +Foundations of Qt Development book available from APress (ISBN 1590598318). + +These are the examples for chapter 1 - The Qt Way of C++ + +plain-cpp + + Listings 1-1, 1-3 + + The MyClass example class in pure C++. No Makefile or QMake project is + included, build the cpp file using your C++ compiler. + + +cpp-with-qobject + + Listings 1-2, 1-4 + + The MyClass example class inheriting QObject. + + +cpp-with-qstring + + Listing 1-5 + + The MyClass example class now uses QString instead of std::string. + + +hello-world + + Listings 1-6, 1-7 + + A simple hello world demostration to show how QMake and QMake project files + are used to build Qt applications. + + +signals-and-slots + + Listings 1-8, 1-9 + + The MyClass example class now emits signals and provides slots. + + +gui-connection + + Listings 1-10, 1-11, 1-12 + + The MyClass example class is used as a bridge to carry text from one widget to + another. + + +qlist + + Listings 1-13, 1-14, 1-15, 1-16 + + Demonstrations of the QList class. + + +stringlist-stack-queue + + Listings 1-17, 1-18 + + Demonstrations of the QStringList, QStack and QQueue classes. + + +map-hash-set + + Listings 1-19, 1-20, 1-21, 1-22, 1-23, 1-24, 1-25, 1-26, 1-27, 1-28, 1-29 + + Demonstrations of the QMap, QHash, QSet, QMultiMap and QMultiHash classes. diff --git a/Chapter01/cpp-with-qobject/cpp-with-qobject.cpp b/Chapter01/cpp-with-qobject/cpp-with-qobject.cpp new file mode 100644 index 0000000..296b7b6 --- /dev/null +++ b/Chapter01/cpp-with-qobject/cpp-with-qobject.cpp @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +using namespace std; + +#include +#include + +class MyClass : public QObject +{ +public: + MyClass( const string &text, QObject *parent = 0 ); + + const string &text(); + void setText( const string &text ); + + int getLengthOfText(); + +private: + string m_text; +}; + +MyClass::MyClass( const string &text, QObject *parent ) : QObject( parent ) +{ + m_text = text; +} +const string &MyClass::text() { return m_text; } +void MyClass::setText( const string &text ) { m_text = text; } +int MyClass::getLengthOfText() { return m_text.size(); } + +int main( int argc, char **argv ) +{ + QObject parent; + MyClass *a, *b, *c; + + a = new MyClass( "foo", &parent ); + b = new MyClass( "ba-a-ar", &parent ); + c = new MyClass( "baz", &parent ); + + qDebug() << QString::fromStdString(a->text()) << " (" << a->getLengthOfText() << ")"; + a->setText( b->text() ); + qDebug() << QString::fromStdString(a->text()) << " (" << a->getLengthOfText() << ")"; + + return a->getLengthOfText() - c->getLengthOfText(); +} diff --git a/Chapter01/cpp-with-qobject/example.pro b/Chapter01/cpp-with-qobject/example.pro new file mode 100644 index 0000000..ce4202e --- /dev/null +++ b/Chapter01/cpp-with-qobject/example.pro @@ -0,0 +1,13 @@ +###################################################################### +# Automatically generated by qmake (2.00a) ti 8. aug 18:09:23 2006 +###################################################################### + +TEMPLATE = app +TARGET += +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += cpp-with-qobject.cpp + +CONFIG += console \ No newline at end of file diff --git a/Chapter01/cpp-with-qstring/cpp-with-qstring.cpp b/Chapter01/cpp-with-qstring/cpp-with-qstring.cpp new file mode 100644 index 0000000..875847a --- /dev/null +++ b/Chapter01/cpp-with-qstring/cpp-with-qstring.cpp @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include + +#include + +class MyClass : public QObject +{ +public: + MyClass( const QString &text, QObject *parent = 0 ); + + const QString &text(); + void setText( const QString &text ); + + int getLengthOfText(); + +private: + QString m_text; +}; + +MyClass::MyClass( const QString &text, QObject *parent ) : QObject( parent ) +{ + m_text = text; +} +const QString &MyClass::text() { return m_text; } +void MyClass::setText( const QString &text ) { m_text = text; } +int MyClass::getLengthOfText() { return m_text.size(); } + +int main( int argc, char **argv ) +{ + QObject parent; + MyClass *a, *b, *c; + + a = new MyClass( "foo", &parent ); + b = new MyClass( "ba-a-ar", &parent ); + c = new MyClass( "baz", &parent ); + + qDebug() << a->text() << " (" << a->getLengthOfText() << ")"; + a->setText( b->text() ); + qDebug() << a->text() << " (" << a->getLengthOfText() << ")"; + + return a->getLengthOfText() - c->getLengthOfText(); +} diff --git a/Chapter01/cpp-with-qstring/example.pro b/Chapter01/cpp-with-qstring/example.pro new file mode 100644 index 0000000..3d00f28 --- /dev/null +++ b/Chapter01/cpp-with-qstring/example.pro @@ -0,0 +1,13 @@ +###################################################################### +# Automatically generated by qmake (2.00a) to 10. aug 14:07:53 2006 +###################################################################### + +TEMPLATE = app +TARGET += +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += cpp-with-qstring.cpp + +CONFIG += console \ No newline at end of file diff --git a/Chapter01/gui-connection/example.pro b/Chapter01/gui-connection/example.pro new file mode 100644 index 0000000..97ee035 --- /dev/null +++ b/Chapter01/gui-connection/example.pro @@ -0,0 +1,13 @@ +###################################################################### +# Automatically generated by qmake (2.00a) sö 13. aug 19:59:30 2006 +###################################################################### + +TEMPLATE = app +TARGET += +DEPENDPATH += . +INCLUDEPATH += . + +# Input +HEADERS += myclass.h +SOURCES += gui-connection.cpp myclass.cpp +CONFIG += console diff --git a/Chapter01/gui-connection/gui-connection.cpp b/Chapter01/gui-connection/gui-connection.cpp new file mode 100644 index 0000000..ba9845d --- /dev/null +++ b/Chapter01/gui-connection/gui-connection.cpp @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "myclass.h" + +#include +#include +#include +#include +#include + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + QWidget widget; + QLineEdit *lineEdit = new QLineEdit(); + QLabel *label = new QLabel(); + + QVBoxLayout *layout = new QVBoxLayout(); + layout->addWidget( lineEdit ); + layout->addWidget( label ); + widget.setLayout( layout ); + + MyClass *bridge = new MyClass( "", &app ); + + QObject::connect( + lineEdit, SIGNAL(textChanged(const QString&)), + bridge, SLOT(setText(const QString&)) ); + QObject::connect( + bridge, SIGNAL(textChanged(const QString&)), + label, SLOT(setText(const QString&)) ); + + widget.show(); + + return app.exec(); +} diff --git a/Chapter01/gui-connection/myclass.cpp b/Chapter01/gui-connection/myclass.cpp new file mode 100644 index 0000000..3828d49 --- /dev/null +++ b/Chapter01/gui-connection/myclass.cpp @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "myclass.h" + +MyClass::MyClass( const QString &text, QObject *parent ) : QObject( parent ) +{ + m_text = text; +} + +const QString &MyClass::text() const +{ + return m_text; +} + +void MyClass::setText( const QString &text ) +{ + if( m_text == text ) + return; + + m_text = text; + emit textChanged( m_text ); +} + +int MyClass::getLengthOfText() const +{ + return m_text.size(); +} diff --git a/Chapter01/gui-connection/myclass.h b/Chapter01/gui-connection/myclass.h new file mode 100644 index 0000000..6e1a96a --- /dev/null +++ b/Chapter01/gui-connection/myclass.h @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef MYCLASS_H +#define MYCLASS_H + +#include +#include + +class MyClass : public QObject +{ + Q_OBJECT + +public: + MyClass( const QString &text, QObject *parent = 0 ); + + const QString& text() const; + int getLengthOfText() const; + +public slots: + void setText( const QString &text ); + +signals: + void textChanged( const QString& ); + +private: + QString m_text; +}; + +#endif // MYCLASS_H diff --git a/Chapter01/hello-world/anything.cpp b/Chapter01/hello-world/anything.cpp new file mode 100644 index 0000000..d126362 --- /dev/null +++ b/Chapter01/hello-world/anything.cpp @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +int main( int argc, char **argv ) +{ + qDebug() << "Hello Qt World!"; + + return 0; +} diff --git a/Chapter01/hello-world/testing.pro b/Chapter01/hello-world/testing.pro new file mode 100644 index 0000000..4b8f97b --- /dev/null +++ b/Chapter01/hello-world/testing.pro @@ -0,0 +1,12 @@ +###################################################################### +# Automatically generated by qmake (2.00a) to 10. aug 17:06:34 2006 +###################################################################### + +TEMPLATE = app +TARGET += +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += anything.cpp +CONFIG += console diff --git a/Chapter01/map-hash-set/example.pro b/Chapter01/map-hash-set/example.pro new file mode 100644 index 0000000..b482bd0 --- /dev/null +++ b/Chapter01/map-hash-set/example.pro @@ -0,0 +1,12 @@ +###################################################################### +# Automatically generated by qmake (2.00a) må 21. aug 15:57:33 2006 +###################################################################### + +TEMPLATE = app +TARGET += +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += map-examples.cpp +CONFIG += console diff --git a/Chapter01/map-hash-set/map-examples.cpp b/Chapter01/map-hash-set/map-examples.cpp new file mode 100644 index 0000000..1000d9f --- /dev/null +++ b/Chapter01/map-hash-set/map-examples.cpp @@ -0,0 +1,207 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include +#include + +#include +#include + +#include + +#include + +void mapStringIntDemo() +{ + QMap map; + + map["foo"] = 42; + map["bar"] = 13; + map["baz"] = 9; + + QString key; + foreach( key, map.keys() ) + qDebug() << key << " = " << map[key]; + + QMap::ConstIterator ii; + for( ii = map.constBegin(); ii != map.constEnd(); ++ii ) + qDebug() << ii.key() << " = " << ii.value(); + + int sum; + sum = map["foo"] + map.value("ingenting"); + + foreach( key, map.keys() ) + qDebug() << key << " = " << map[key]; + + sum = map["foo"] + map["ingenting"]; + + foreach( key, map.keys() ) + qDebug() << key << " = " << map[key]; +} + +void hashStringIntDemo() +{ + QHash hash; + + hash["foo"] = 42; + hash["bar"] = 13; + hash["baz"] = 9; + + QString key; + foreach( key, hash.keys() ) + qDebug() << key << " = " << hash[key]; + + int sum; + sum = hash["foo"] + hash.value("ingenting"); + + foreach( key, hash.keys() ) + qDebug() << key << " = " << hash[key]; + + sum = hash["foo"] + hash["ingenting"]; + + foreach( key, hash.keys() ) + qDebug() << key << " = " << hash[key]; +} + +class Person +{ +public: + Person( QString name, QString number ); + + QString name() const; + QString number() const; + +private: + QString m_name, m_number; +}; + +Person::Person( QString name, QString number ) : m_name( name ), m_number( number ) {} +QString Person::name() const { return m_name; } +QString Person::number() const { return m_number; } + +bool operator==( const Person &a, const Person &b ) +{ + return (a.name() == b.name()) && (a.number() == b.number()); +} + +uint qHash( const Person &key ) +{ + return qHash( key.name() ) ^ qHash( key.number() ); +} + +void hashPersons() +{ + QHash hash; + + hash[ Person( "Anders", "8447070" ) ] = 10; + hash[ Person( "Micke", "7728433" ) ] = 20; + + qDebug() << hash.value( Person( "Anders", "8447070" ) ); // 10 + qDebug() << hash.value( Person( "Anders", "8447071" ) ); // 0 + qDebug() << hash.value( Person( "Micke", "7728433" ) ); // 20 + qDebug() << hash.value( Person( "Michael", "7728433" ) ); // 0 +} + +void setStringDemo() +{ + QSet set; + + set << "Ada" << "C++" << "Ruby"; + + for( QSet::ConstIterator ii = set.begin(); ii != set.end(); ++ii ) + qDebug() << *ii; + + if( set.contains( "FORTRAN" ) ) + qDebug() << "FORTRAN is in the set."; + else + qDebug() << "FORTRAN is out."; +} + +void multimapStringIntDemo() +{ + QMultiMap multi; + + multi.insert( "foo", 10 ); + multi.insert( "foo", 20 ); + multi.insert( "bar", 30 ); + + QSet keys; + foreach( QString key, multi.keys() ) + keys << key; + + foreach( QString key, keys ) + foreach( int value, multi.values(key) ) + qDebug() << key << ": " << value; + + QMultiMap::ConstIterator ii = multi.find( "foo" ); + while( ii != multi.end() && ii.key() == "foo" ) + { + qDebug() << ii.value(); + ++ii; + } +} + +void multihashStringIntDemo() +{ + QMultiHash multi; + + multi.insert( "foo", 10 ); + multi.insert( "foo", 20 ); + multi.insert( "bar", 30 ); + + QSet keys; + foreach( QString key, multi.keys() ) + keys << key; + + foreach( QString key, keys ) + foreach( int value, multi.values(key) ) + qDebug() << key << ": " << value; + + QMultiHash::ConstIterator ii = multi.find( "foo" ); + while( ii != multi.end() && ii.key() == "foo" ) + { + qDebug() << ii.value(); + ++ii; + } +} + +int main( void ) +{ + mapStringIntDemo(); + hashStringIntDemo(); + hashPersons(); + setStringDemo(); + multimapStringIntDemo(); + multihashStringIntDemo(); + + return 0; +} diff --git a/Chapter01/plain-cpp/plain-cpp.cpp b/Chapter01/plain-cpp/plain-cpp.cpp new file mode 100644 index 0000000..89f7650 --- /dev/null +++ b/Chapter01/plain-cpp/plain-cpp.cpp @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include + +using namespace std; + +class MyClass +{ +public: + MyClass( const string &text ); + + const string &text(); + void setText( const string &text ); + + int getLengthOfText(); + +private: + string m_text; +}; + +MyClass::MyClass( const string &text ) { m_text = text; } +const string &MyClass::text() { return m_text; } +void MyClass::setText( const string &text ) { m_text = text; } +int MyClass::getLengthOfText() { return m_text.size(); } + +int main( int argc, char **argv ) +{ + MyClass *a, *b, *c; + + a = new MyClass( "foo" ); + b = new MyClass( "ba-a-ar" ); + c = new MyClass( "baz" ); + + cout << a->text() << " (" << a->getLengthOfText() << ")" << endl; + a->setText( b->text() ); + cout << a->text() << " (" << a->getLengthOfText() << ")" << endl; + + int result = a->getLengthOfText() - c->getLengthOfText(); + + delete a; + delete b; + delete c; + + return result; +} diff --git a/Chapter01/qlist/example.pro b/Chapter01/qlist/example.pro new file mode 100644 index 0000000..e58c781 --- /dev/null +++ b/Chapter01/qlist/example.pro @@ -0,0 +1,12 @@ +###################################################################### +# Automatically generated by qmake (2.00a) må 21. aug 09:53:24 2006 +###################################################################### + +TEMPLATE = app +TARGET += +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += qlist.cpp +CONFIG += console diff --git a/Chapter01/qlist/qlist.cpp b/Chapter01/qlist/qlist.cpp new file mode 100644 index 0000000..cf8006c --- /dev/null +++ b/Chapter01/qlist/qlist.cpp @@ -0,0 +1,121 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include + +#include + +void fillAndPrint() +{ + QList list; + list << "foo" << "bar" << "baz"; + + QString s; + foreach( s, list ) + qDebug() << s; +} + +void constIterators() +{ + QList list; + list << 23 << 27 << 52 << 52; + + QListIterator javaIter( list ); + while( javaIter.hasNext() ) + qDebug() << javaIter.next(); + + QList::const_iterator stlIter; + for( stlIter = list.begin(); stlIter != list.end(); ++stlIter ) + qDebug() << (*stlIter); +} + +void mutableIterators() +{ + QList list; + list << 27 << 33 << 61 << 62; + + QMutableListIterator javaIter( list ); + while( javaIter.hasNext() ) + { + int value = javaIter.next() + 1; + javaIter.setValue( value ); + qDebug() << value; + } + + QList::Iterator stlIter; + for( stlIter = list.begin(); stlIter != list.end(); ++stlIter ) + { + (*stlIter) = (*stlIter)*2; + qDebug() << (*stlIter); + } +} + +void insertAndPrint() +{ + QList list; + + list << "first"; + list.append( "second" ); + list.prepend( "third" ); + list.insert( 1, "fourth" ); + list.insert( 4, "fifth" ); + + QString s; + foreach( s, list ) + qDebug() << s; +} + +void miscExamples() +{ + QList list; + for( int i=0; i<10; i++ ) + list << i; + + int sum = list[5] + list.at(7); +} + +int main( void ) +{ + qDebug() << "Fill and print"; + fillAndPrint(); + + qDebug() << "Const iterators"; + constIterators(); + + qDebug() << "Mutable iterators"; + mutableIterators(); + + qDebug() << "Insert and print"; + insertAndPrint(); + + return 0; +} diff --git a/Chapter01/signals-and-slots/example.pro b/Chapter01/signals-and-slots/example.pro new file mode 100644 index 0000000..28bbb80 --- /dev/null +++ b/Chapter01/signals-and-slots/example.pro @@ -0,0 +1,13 @@ +###################################################################### +# Automatically generated by qmake (2.00a) fr 11. aug 14:12:41 2006 +###################################################################### + +TEMPLATE = app +TARGET += +DEPENDPATH += . +INCLUDEPATH += . + +# Input +HEADERS += myclass.h +SOURCES += myclass.cpp sig-slot.cpp +CONFIG += console diff --git a/Chapter01/signals-and-slots/myclass.cpp b/Chapter01/signals-and-slots/myclass.cpp new file mode 100644 index 0000000..3828d49 --- /dev/null +++ b/Chapter01/signals-and-slots/myclass.cpp @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "myclass.h" + +MyClass::MyClass( const QString &text, QObject *parent ) : QObject( parent ) +{ + m_text = text; +} + +const QString &MyClass::text() const +{ + return m_text; +} + +void MyClass::setText( const QString &text ) +{ + if( m_text == text ) + return; + + m_text = text; + emit textChanged( m_text ); +} + +int MyClass::getLengthOfText() const +{ + return m_text.size(); +} diff --git a/Chapter01/signals-and-slots/myclass.h b/Chapter01/signals-and-slots/myclass.h new file mode 100644 index 0000000..6e1a96a --- /dev/null +++ b/Chapter01/signals-and-slots/myclass.h @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef MYCLASS_H +#define MYCLASS_H + +#include +#include + +class MyClass : public QObject +{ + Q_OBJECT + +public: + MyClass( const QString &text, QObject *parent = 0 ); + + const QString& text() const; + int getLengthOfText() const; + +public slots: + void setText( const QString &text ); + +signals: + void textChanged( const QString& ); + +private: + QString m_text; +}; + +#endif // MYCLASS_H diff --git a/Chapter01/signals-and-slots/sig-slot.cpp b/Chapter01/signals-and-slots/sig-slot.cpp new file mode 100644 index 0000000..ae6f82c --- /dev/null +++ b/Chapter01/signals-and-slots/sig-slot.cpp @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "myclass.h" + +#include + +int main( int argc, char **argv ) +{ + QObject parent; + MyClass *a, *b, *c; + + a = new MyClass( "foo", &parent ); + b = new MyClass( "bar", &parent ); + c = new MyClass( "baz", &parent ); + + QObject::connect( + a, SIGNAL(textChanged(const QString&)), + b, SLOT(setText(const QString&)) ); + QObject::connect( + b, SIGNAL(textChanged(const QString&)), + c, SLOT(setText(const QString&)) ); + QObject::connect( + c, SIGNAL(textChanged(const QString&)), + b, SLOT(setText(const QString&)) ); + + qDebug() << "--- After creation ---"; + qDebug() << "a:" << a->text() << "\nb:" << b->text() << "\nc:" << c->text(); + + c->setText( "test" ); + + qDebug() << "--- After test ---"; + qDebug() << "a:" << a->text() << "\nb:" << b->text() << "\nc:" << c->text(); + + a->setText( "Qt" ); + + qDebug() << "--- After Qt ---"; + qDebug() << "a:" << a->text() << "\nb:" << b->text() << "\nc:" << c->text(); + + return a->getLengthOfText() - c->getLengthOfText(); +} diff --git a/Chapter01/stringlist-stack-queue/example.pro b/Chapter01/stringlist-stack-queue/example.pro new file mode 100644 index 0000000..406e747 --- /dev/null +++ b/Chapter01/stringlist-stack-queue/example.pro @@ -0,0 +1,12 @@ +###################################################################### +# Automatically generated by qmake (2.00a) må 21. aug 14:28:49 2006 +###################################################################### + +TEMPLATE = app +TARGET += +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += special-lists.cpp +CONFIG += console \ No newline at end of file diff --git a/Chapter01/stringlist-stack-queue/special-lists.cpp b/Chapter01/stringlist-stack-queue/special-lists.cpp new file mode 100644 index 0000000..a6b4703 --- /dev/null +++ b/Chapter01/stringlist-stack-queue/special-lists.cpp @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include +#include + +#include + +void stringList() +{ + QString text; + + QStringList list; + list << "foo" << "bar" << "baz"; + foreach( text, list ) + qDebug() << text; + + qDebug() << "---"; + + QString all = list.join(","); + qDebug() << all; + + qDebug() << "---"; + + list.replaceInStrings( "a", "oo" ); + foreach( text, list ) + qDebug() << text; + + qDebug() << "---"; + + list << all.split(","); + foreach( text, list ) + qDebug() << text; +} + +void stackDemo() +{ + QStack stack; + + stack.push( "foo" ); + stack.push( "bar" ); + stack.push( "baz" ); + + QString result; + while( !stack.isEmpty() ) + result += stack.pop(); + qDebug() << result; +} + +void queueDemo() +{ + QQueue queue; + + queue.enqueue( "foo" ); + queue.enqueue( "bar" ); + queue.enqueue( "baz" ); + + QString result; + while( !queue.isEmpty() ) + result += queue.dequeue(); + qDebug() << result; +} + +int main( void ) +{ + qDebug() << "String list"; + stringList(); + + qDebug() << "Stack"; + stackDemo(); + + qDebug() << "Queue"; + queueDemo(); + + return 0; +} diff --git a/Chapter02/README.txt b/Chapter02/README.txt new file mode 100644 index 0000000..7569a7d --- /dev/null +++ b/Chapter02/README.txt @@ -0,0 +1,8 @@ +This file is a part of 1590598318-1.zip containing example source code for the +Foundations of Qt Development book available from APress (ISBN 1590598318). + +These are the examples for chapter 2 - Rapid Application Development Using Qt + +addressbook + + This is the only example of the chapter, covering all listings. diff --git a/Chapter02/addressbook/addressbook.pro b/Chapter02/addressbook/addressbook.pro new file mode 100644 index 0000000..de213bd --- /dev/null +++ b/Chapter02/addressbook/addressbook.pro @@ -0,0 +1,36 @@ +# +# Copyright (c) 2006-2007, Johan Thelin +# +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# * Neither the name of APress nor the names of its contributors +# may be used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# + +TEMPLATE = app +TARGET = addressbook + +SOURCES += main.cpp editdialog.cpp listdialog.cpp +HEADERS += editdialog.h listdialog.h +FORMS += editdialog.ui listdialog.ui diff --git a/Chapter02/addressbook/editdialog.cpp b/Chapter02/addressbook/editdialog.cpp new file mode 100644 index 0000000..94ad4ab --- /dev/null +++ b/Chapter02/addressbook/editdialog.cpp @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "editdialog.h" + +#include + +EditDialog::EditDialog( QWidget *parent ) : QDialog( parent ) +{ + ui.setupUi( this ); +} + +const QString EditDialog::name() const +{ + return ui.nameEdit->text().replace("--","").trimmed(); +} + +void EditDialog::setName( const QString &name ) +{ + ui.nameEdit->setText( name ); +} + +const QString EditDialog::number() const +{ + return ui.numberEdit->text().replace("--","").trimmed(); +} + +void EditDialog::setNumber( const QString &number ) +{ + ui.numberEdit->setText( number ); +} diff --git a/Chapter02/addressbook/editdialog.h b/Chapter02/addressbook/editdialog.h new file mode 100644 index 0000000..319f1ce --- /dev/null +++ b/Chapter02/addressbook/editdialog.h @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef EDITDIALOG_H +#define EDITDIALOG_H + +#include +#include "ui_editdialog.h" + +class EditDialog : public QDialog +{ +public: + EditDialog( QWidget *parent=0 ); + + const QString name() const; + void setName( const QString& ); + + const QString number() const; + void setNumber( const QString& ); + +private: + Ui::EditDialog ui; +}; + +#endif // EDITDIALOG_H diff --git a/Chapter02/addressbook/editdialog.ui b/Chapter02/addressbook/editdialog.ui new file mode 100644 index 0000000..55a07ba --- /dev/null +++ b/Chapter02/addressbook/editdialog.ui @@ -0,0 +1,145 @@ + + + Copyright (c) 2006-2007, Johan Thelin + + All rights reserved. + + Redistribution and use in source and binary forms, with or without modification, + are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + * Neither the name of APress nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + EditDialog + + + + 0 + 0 + 400 + 130 + + + + Phone Book Entry + + + + 9 + + + 6 + + + + + 0 + + + 6 + + + + + + + + + + + Name: + + + + + + + Number: + + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::NoButton|QDialogButtonBox::Ok + + + + + + + nameEdit + numberEdit + + + + + buttonBox + accepted() + EditDialog + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + EditDialog + reject() + + + 316 + 260 + + + 286 + 274 + + + + + diff --git a/Chapter02/addressbook/listdialog.cpp b/Chapter02/addressbook/listdialog.cpp new file mode 100644 index 0000000..97bdfb3 --- /dev/null +++ b/Chapter02/addressbook/listdialog.cpp @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include "listdialog.h" +#include "editdialog.h" + +ListDialog::ListDialog() : QDialog() +{ + ui.setupUi( this ); + + connect( ui.addButton, SIGNAL(clicked()), this, SLOT(addItem()) ); + connect( ui.editButton, SIGNAL(clicked()), this, SLOT(editItem()) ); + connect( ui.deleteButton, SIGNAL(clicked()), this, SLOT(deleteItem()) ); +} + +void ListDialog::addItem() +{ + EditDialog dlg( this ); + + if( dlg.exec() == Accepted ) + ui.list->addItem( dlg.name() + " -- " + dlg.number() ); +} + +void ListDialog::editItem() +{ + if( !ui.list->currentItem() ) + return; + + QStringList parts = ui.list->currentItem()->text().split( "--" ); + + EditDialog dlg( this ); + dlg.setName( parts[0].trimmed() ); + dlg.setNumber( parts[1].trimmed() ); + + if( dlg.exec() == Accepted ) + ui.list->currentItem()->setText( dlg.name() + " -- " + dlg.number() ); +} + +void ListDialog::deleteItem() +{ + delete ui.list->currentItem(); +} diff --git a/Chapter02/addressbook/listdialog.h b/Chapter02/addressbook/listdialog.h new file mode 100644 index 0000000..d60b8e8 --- /dev/null +++ b/Chapter02/addressbook/listdialog.h @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef LISTDIALOG_H +#define LISTDIALOG_H + +#include +#include "ui_listdialog.h" + +class ListDialog : public QDialog +{ + Q_OBJECT + +public: + ListDialog(); + +private slots: + void addItem(); + void editItem(); + void deleteItem(); + +private: + Ui::ListDialog ui; +}; + +#endif // LISTDIALOG_H diff --git a/Chapter02/addressbook/listdialog.ui b/Chapter02/addressbook/listdialog.ui new file mode 100644 index 0000000..d40ee11 --- /dev/null +++ b/Chapter02/addressbook/listdialog.ui @@ -0,0 +1,133 @@ + + + Copyright (c) 2006-2007, Johan Thelin + + All rights reserved. + + Redistribution and use in source and binary forms, with or without modification, + are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + * Neither the name of APress nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + ListDialog + + + + 0 + 0 + 400 + 300 + + + + Phone Book + + + + 9 + + + 6 + + + + + + + + 0 + + + 6 + + + + + Add new... + + + + + + + Edit... + + + + + + + Delete + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + Clear all + + + + + + + + + list + addButton + editButton + deleteButton + clearButton + + + + + clearButton + clicked() + list + clear() + + + 343 + 285 + + + 220 + 170 + + + + + diff --git a/Chapter02/addressbook/main.cpp b/Chapter02/addressbook/main.cpp new file mode 100644 index 0000000..51641b2 --- /dev/null +++ b/Chapter02/addressbook/main.cpp @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include "listdialog.h" + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + ListDialog dlg; + + dlg.show(); + + return app.exec(); +} diff --git a/Chapter03/README.txt b/Chapter03/README.txt new file mode 100644 index 0000000..3cd9d66 --- /dev/null +++ b/Chapter03/README.txt @@ -0,0 +1,127 @@ +This file is a part of 1590598318-1.zip containing example source code for the +Foundations of Qt Development book available from APress (ISBN 1590598318). + +These are the examples for chapter 3 - Widgets and Layouts + +designer-and-code + + Listings 3-1, 3-2, 3-3 + + A simple dialog with layouts and stretch factors created using source code as + well as using Designer. + + +gridlayout + + Listing 3-4 + + Shows buttons in a QGridLayout. + + +pushbutton + + Listing 3-5 + + Shows QPushButton widgets. + + +buttonbox + + Listing 3-6 + + Shows a QDialogButtonBox widget with buttons. + + +label + + Listings 3-7 + + Shows QLabel widgets. + + +lineedit + + Not shown in any Listings, but Figures 3-12 and 3-13. + + Shows QLineEdit widgets in different modes. + + +radiobutton + + Listings 3-8 + + Shows QRadioButton widgets in QButtonGroups. + + +groupbox + + Not shown in any Listings, but Figure 3-15. + + Shows QGroupBox widgets. + + +listwidget + + Listings 3-9, 3-10 + + Shows two QListWidget instances and QPushButton widgets used to move items + between the two lists. + + +spinbox + + Not shown in any Listings, but Figure 3-17. + + Shows a QSpinBox widget connected to a QLCDNumber widget. + + +progressbar + + Not shown in any Listings, but Figure 3-19. + + Shows QProgressBar widgets in different working modes. + + +filedialog + + Listings 3-11, 3-12, 3-13, 3-14 + + Shows the QFileDialog class and the getOpenFileName, getOpenFileNames, + getSaveFileName and getExistingDirectory methods. + + +messagebox + + Listings 3-15, 3-16, 3-17, 3-18, 3-19 + + Shows the QMessageBox class and the QInputDialog class. For the latter, the + getText, getItem and getInteger methods are used. + + +colorandfont + + Listings 3-20, 3-21 + + Shows the QColorDialog and QFontDialog classes. + + +validator + + Listing 3-22 + + Shows the QIntValidator and QDoubleValidator classes. + + +regexp + + Listings 3-23, 3-24 + + Shows how to use a regular expression and the QRegExp class. + + +revalidator + + Listing 3-25 + + Shows how to use the QRegExpValidator and a regular expression to validate + user input. diff --git a/Chapter03/buttonbox/buttonbox.cpp b/Chapter03/buttonbox/buttonbox.cpp new file mode 100644 index 0000000..9f40b76 --- /dev/null +++ b/Chapter03/buttonbox/buttonbox.cpp @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "ButtonBox.h" + +#include +#include +#include + +#include + +#include + +ButtonBoxDialog::ButtonBoxDialog() : QDialog(0) +{ + QVBoxLayout *layout = new QVBoxLayout( this ); + + QPushButton *button; + QDialogButtonBox *box = new QDialogButtonBox( Qt::Horizontal ); + + button = new QPushButton( "Ok" ); + connect( button, SIGNAL(clicked()), this, SLOT(okClicked()) ); + box->addButton( button, QDialogButtonBox::AcceptRole ); + + button = new QPushButton( "Cancel" ); + connect( button, SIGNAL(clicked()), this, SLOT(cancelClicked()) ); + box->addButton( button, QDialogButtonBox::RejectRole ); + + button = new QPushButton( "Apply" ); + connect( button, SIGNAL(clicked()), this, SLOT(applyClicked()) ); + box->addButton( button, QDialogButtonBox::ApplyRole ); + + button = new QPushButton( "Reset" ); + connect( button, SIGNAL(clicked()), this, SLOT(resetClicked()) ); + box->addButton( button, QDialogButtonBox::ResetRole ); + + button = new QPushButton( "Help" ); + connect( button, SIGNAL(clicked()), this, SLOT(helpClicked()) ); + box->addButton( button, QDialogButtonBox::HelpRole ); + + layout->addWidget( new QLabel( "Try out the buttons!" ) ); + layout->addWidget( box ); +} + +void ButtonBoxDialog::okClicked() +{ + QMessageBox::information( this, "Demo", "Ok Clicked" ); +} + +void ButtonBoxDialog::cancelClicked() +{ + QMessageBox::information( this, "Demo", "Cancel Clicked" ); +} + +void ButtonBoxDialog::applyClicked() +{ + QMessageBox::information( this, "Demo", "Apply Clicked" ); +} + +void ButtonBoxDialog::resetClicked() +{ + QMessageBox::information( this, "Demo", "Reset Clicked" ); +} + +void ButtonBoxDialog::helpClicked() +{ + QMessageBox::information( this, "Demo", "Help Clicked" ); +} diff --git a/Chapter03/buttonbox/buttonbox.h b/Chapter03/buttonbox/buttonbox.h new file mode 100644 index 0000000..759e886 --- /dev/null +++ b/Chapter03/buttonbox/buttonbox.h @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef BUTTONBOX_H +#define BUTTONBOX_H + +#include + +class ButtonBoxDialog : public QDialog +{ + Q_OBJECT + +public: + ButtonBoxDialog(); + +public slots: + void okClicked(); + void cancelClicked(); + void applyClicked(); + void resetClicked(); + void helpClicked(); +}; + +#endif // BUTTONBOX_H diff --git a/Chapter03/buttonbox/buttonbox.pro b/Chapter03/buttonbox/buttonbox.pro new file mode 100644 index 0000000..8dc4396 --- /dev/null +++ b/Chapter03/buttonbox/buttonbox.pro @@ -0,0 +1,12 @@ +###################################################################### +# Automatically generated by qmake (2.01a) ti 12. sep 16:50:44 2006 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +HEADERS += buttonbox.h +SOURCES += buttonbox.cpp main.cpp diff --git a/Chapter03/buttonbox/main.cpp b/Chapter03/buttonbox/main.cpp new file mode 100644 index 0000000..bb841a4 --- /dev/null +++ b/Chapter03/buttonbox/main.cpp @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include + +#include "buttonbox.h" + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + app.setStyle( new QCleanlooksStyle ); + + ButtonBoxDialog dlg; + dlg.show(); + + return app.exec(); +} \ No newline at end of file diff --git a/Chapter03/colorandfont/colorandfont.pro b/Chapter03/colorandfont/colorandfont.pro new file mode 100644 index 0000000..cda1fb7 --- /dev/null +++ b/Chapter03/colorandfont/colorandfont.pro @@ -0,0 +1,12 @@ +###################################################################### +# Automatically generated by qmake (2.01a) fr 19. jan 08:47:06 2007 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp +CONFIG += console diff --git a/Chapter03/colorandfont/main.cpp b/Chapter03/colorandfont/main.cpp new file mode 100644 index 0000000..94cd84a --- /dev/null +++ b/Chapter03/colorandfont/main.cpp @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include +#include + +class Tester : public QWidget +{ +public: + void doColor() + { + QColor color = QColorDialog::getColor( + Qt::yellow, + this ); + if( color.isValid() ) + { + + qDebug( "ok" ); + } + } + + void doFont() + { + bool ok; + QFont font = QFontDialog::getFont( + &ok, + QFont( "Arial", 18 ), + this, + tr("Pick a font") ); + if( ok ) + { + qDebug( "ok" ); + } + } +}; + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + Tester t; + t.doColor(); + t.doFont(); + + return 0; +} diff --git a/Chapter03/designer-and-code/designer-dialog.ui b/Chapter03/designer-and-code/designer-dialog.ui new file mode 100644 index 0000000..25b1629 --- /dev/null +++ b/Chapter03/designer-and-code/designer-dialog.ui @@ -0,0 +1,136 @@ + + + Copyright (c) 2006-2007, Johan Thelin + + All rights reserved. + + Redistribution and use in source and binary forms, with or without modification, + are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + * Neither the name of APress nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + Dialog + + + + 0 + 0 + 457 + 169 + + + + Dialog + + + + 9 + + + 6 + + + + + GroupBox + + + + 9 + + + 6 + + + + + Supercalifrailisticexpialidocious + + + + + + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::NoButton|QDialogButtonBox::Ok + + + + + + + + + buttonBox + accepted() + Dialog + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + Dialog + reject() + + + 316 + 260 + + + 286 + 274 + + + + + diff --git a/Chapter03/designer-and-code/example.pro b/Chapter03/designer-and-code/example.pro new file mode 100644 index 0000000..b5f1c79 --- /dev/null +++ b/Chapter03/designer-and-code/example.pro @@ -0,0 +1,32 @@ +# +# Copyright (c) 2006-2007, Johan Thelin +# +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# * Neither the name of APress nor the names of its contributors +# may be used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# + +TEMPLATE = app +SOURCES += main.cpp diff --git a/Chapter03/designer-and-code/main.cpp b/Chapter03/designer-and-code/main.cpp new file mode 100644 index 0000000..c6b98f7 --- /dev/null +++ b/Chapter03/designer-and-code/main.cpp @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include + +#include +#include +#include +#include + +#include +#include + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + QDialog dlg; + + QGroupBox *groupBox = new QGroupBox( "Groupbox" ); + QLabel *label = + new QLabel( "Supercalifragilisticexpialidocious" ); + QLineEdit *lineEdit = new QLineEdit(); + QDialogButtonBox *buttons = + new QDialogButtonBox( QDialogButtonBox::Ok | + QDialogButtonBox::Cancel ); + + QHBoxLayout *hLayout = new QHBoxLayout( groupBox ); + hLayout->addWidget( label ); + hLayout->addWidget( lineEdit ); + + QVBoxLayout *vLayout = new QVBoxLayout( &dlg ); + vLayout->addWidget( groupBox ); + vLayout->addStretch(); + vLayout->addWidget( buttons ); + + QSizePolicy policy = label->sizePolicy(); + policy.setHorizontalStretch( 3 ); + label->setSizePolicy( policy ); + policy.setHorizontalStretch( 1 ); + lineEdit->setSizePolicy( policy ); + + dlg.show(); + return app.exec(); +} diff --git a/Chapter03/filedialog/filedialog.pro b/Chapter03/filedialog/filedialog.pro new file mode 100644 index 0000000..24c2619 --- /dev/null +++ b/Chapter03/filedialog/filedialog.pro @@ -0,0 +1,12 @@ +###################################################################### +# Automatically generated by qmake (2.01a) to 18. jan 13:28:11 2007 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp +CONFIG += console diff --git a/Chapter03/filedialog/main.cpp b/Chapter03/filedialog/main.cpp new file mode 100644 index 0000000..20737c7 --- /dev/null +++ b/Chapter03/filedialog/main.cpp @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include + +class Tester : public QWidget +{ +public: + void openFile() + { + QFileDialog::getOpenFileName( this, tr("Open Document"), QDir::currentPath(), tr("Document files (*.doc *.rtf);;All files (*.*)"), 0, QFileDialog::DontUseNativeDialog ); + + QString filename = QFileDialog::getOpenFileName( + this, + tr("Open Document"), + QDir::currentPath(), + tr("Document files (*.doc *.rtf);;All files (*.*)") ); + if( !filename.isNull() ) + { + qDebug( filename.toAscii() ); + } + } + + void openFiles() + { + QStringList filenames = QFileDialog::getOpenFileNames( + this, + tr("Open Document"), + QDir::currentPath(), + tr("Documents (*.doc);;All files (*.*)") ); + if( !filenames.isEmpty() ) + { + qDebug( filenames.join(",").toAscii() ); + } + } + + void openDir() + { + QString dirname = QFileDialog::getExistingDirectory( + this, + tr("Select a Directory"), + QDir::currentPath() ); + if( !dirname.isNull() ) + { + qDebug( dirname.toAscii() ); + } + } + + void saveFile() + { + QString filename = QFileDialog::getSaveFileName( + this, + tr("Save Document"), + QDir::currentPath(), + tr("Documents (*.doc)") ); + if( !filename.isNull() ) + { + qDebug( filename.toAscii() ); + } + } +}; + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + Tester t; + + t.openFile(); + t.openFiles(); + t.openDir(); + t.saveFile(); + + return 0; +} diff --git a/Chapter03/gridlayout/gridlayout.pro b/Chapter03/gridlayout/gridlayout.pro new file mode 100644 index 0000000..bf874f7 --- /dev/null +++ b/Chapter03/gridlayout/gridlayout.pro @@ -0,0 +1,11 @@ +###################################################################### +# Automatically generated by qmake (2.01a) ti 12. sep 09:05:32 2006 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp diff --git a/Chapter03/gridlayout/main.cpp b/Chapter03/gridlayout/main.cpp new file mode 100644 index 0000000..ab19745 --- /dev/null +++ b/Chapter03/gridlayout/main.cpp @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include + +#include + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + QWidget widget; + + QGridLayout layout( &widget ); + layout.addWidget( new QPushButton( "foo" ), 0, 0, 1, 2 ); + layout.addWidget( new QPushButton( "bar" ), 1, 0 ); + layout.addWidget( new QPushButton( "baz" ), 1, 1 ); + + widget.show(); + + return app.exec(); +} diff --git a/Chapter03/groupbox/groupbox.ui b/Chapter03/groupbox/groupbox.ui new file mode 100644 index 0000000..8a0782f --- /dev/null +++ b/Chapter03/groupbox/groupbox.ui @@ -0,0 +1,189 @@ + + + Copyright (c) 2006-2007, Johan Thelin + + All rights reserved. + + Redistribution and use in source and binary forms, with or without modification, + are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + * Neither the name of APress nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + Dialog + + + + 0 + 0 + 416 + 149 + + + + Dialog + + + + 9 + + + 6 + + + + + Unchecked + + + true + + + false + + + + 9 + + + 6 + + + + + PushButton + + + + + + + + + + Checked + + + true + + + true + + + + 9 + + + 6 + + + + + PushButton + + + + + + + + + + Not Checkable + + + + 9 + + + 6 + + + + + PushButton + + + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::NoButton|QDialogButtonBox::Ok + + + + + + + + + buttonBox + accepted() + Dialog + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + Dialog + reject() + + + 316 + 260 + + + 286 + 274 + + + + + diff --git a/Chapter03/label/label.pro b/Chapter03/label/label.pro new file mode 100644 index 0000000..e3600be --- /dev/null +++ b/Chapter03/label/label.pro @@ -0,0 +1,11 @@ +###################################################################### +# Automatically generated by qmake (2.01a) lö 9. sep 14:30:16 2006 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp diff --git a/Chapter03/label/main.cpp b/Chapter03/label/main.cpp new file mode 100644 index 0000000..3021252 --- /dev/null +++ b/Chapter03/label/main.cpp @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include +#include +#include +#include + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + QDialog dlg; + + QLabel *fooLabel = new QLabel( "&Foo:" ); + QLabel *barLabel = new QLabel( "&Bar:" ); + QLineEdit *fooEdit = new QLineEdit(); + QLineEdit *barEdit = new QLineEdit(); + + fooLabel->setBuddy( fooEdit ); + barLabel->setBuddy( barEdit ); + + QGridLayout *layout = new QGridLayout( &dlg ); + layout->addWidget( fooLabel, 0, 0 ); + layout->addWidget( fooEdit, 0, 1 ); + layout->addWidget( barLabel, 1, 0 ); + layout->addWidget( barEdit, 1, 1 ); + + dlg.show(); + + return app.exec(); +} diff --git a/Chapter03/lineedit/lineedit.ui b/Chapter03/lineedit/lineedit.ui new file mode 100644 index 0000000..c17920b --- /dev/null +++ b/Chapter03/lineedit/lineedit.ui @@ -0,0 +1,216 @@ + + + Copyright (c) 2006-2007, Johan Thelin + + All rights reserved. + + Redistribution and use in source and binary forms, with or without modification, + are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + * Neither the name of APress nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + Dialog + + + + 0 + 0 + 503 + 188 + + + + Dialog + + + + 9 + + + 6 + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::NoButton|QDialogButtonBox::Ok + + + + + + + Contents + + + Qt::AlignCenter + + + + + + + Password echo mode: + + + + + + + + + + 20 + + + + + + + + + + QLineEdit::Password + + + + + + + Standard: + + + + + + + + + + Max length 20: + + + + + + + + + + + + buttonBox + accepted() + Dialog + accept() + + + 223 + 152 + + + 153 + 187 + + + + + buttonBox + rejected() + Dialog + reject() + + + 291 + 152 + + + 282 + 187 + + + + + lineEdit + textChanged(QString) + lineEdit_4 + setText(QString) + + + 314 + 47 + + + 338 + 50 + + + + + lineEdit_2 + textChanged(QString) + lineEdit_5 + setText(QString) + + + 314 + 79 + + + 332 + 75 + + + + + lineEdit_3 + textChanged(QString) + lineEdit_6 + setText(QString) + + + 314 + 107 + + + 342 + 110 + + + + + diff --git a/Chapter03/listwidget/listwidget.pro b/Chapter03/listwidget/listwidget.pro new file mode 100644 index 0000000..da83b0c --- /dev/null +++ b/Chapter03/listwidget/listwidget.pro @@ -0,0 +1,13 @@ +###################################################################### +# Automatically generated by qmake (2.01a) ti 12. sep 15:15:04 2006 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +HEADERS += listwidgetdialog.h +SOURCES += listwidgetdialog.cpp main.cpp +CONFIG += console diff --git a/Chapter03/listwidget/listwidgetdialog.cpp b/Chapter03/listwidget/listwidgetdialog.cpp new file mode 100644 index 0000000..f7c8f45 --- /dev/null +++ b/Chapter03/listwidget/listwidgetdialog.cpp @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "listwidgetdialog.h" + +#include +#include + +#include + +ListWidgetDialog::ListWidgetDialog() : QDialog(0) +{ + QPushButton *left, *right; + + QGridLayout *layout = new QGridLayout( this ); + layout->addWidget( left = new QPushButton( "<<" ), 0, 1 ); + layout->addWidget( right = new QPushButton( ">>" ), 1, 1 ); + layout->addWidget( leftList = new QListWidget(), 0, 0, 3, 1 ); + layout->addWidget( rightList = new QListWidget(), 0, 2, 3, 1 ); + + connect( left, SIGNAL(clicked()), this, SLOT(moveLeft()) ); + connect( right, SIGNAL(clicked()), this, SLOT(moveRight()) ); + + QStringList items; + items << "Argentine" << "Brazilian" << "South African" + << "USA West" << "Monaco" << "Belgian" << "Spanish" + << "Swedish" << "French" << "British" << "German" + << "Austrian" << "Dutch" << "Italian" << "USA East" + << "Canadian"; + leftList->addItems( items ); +} + +void ListWidgetDialog::moveLeft() +{ + if( rightList->selectedItems().count() != 1 ) + return; + + QListWidgetItem *item = rightList->takeItem( rightList->currentRow() ); + leftList->addItem( item ); +} + +void ListWidgetDialog::moveRight() +{ + if( leftList->selectedItems().count() != 1 ) + return; + + QListWidgetItem *item = leftList->takeItem( leftList->currentRow() ); + rightList->addItem( item ); +} diff --git a/Chapter03/listwidget/listwidgetdialog.h b/Chapter03/listwidget/listwidgetdialog.h new file mode 100644 index 0000000..c4df402 --- /dev/null +++ b/Chapter03/listwidget/listwidgetdialog.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef LISTWIDGETDIALOG_H +#define LISTWIDGETDIALOG_H + +#include + +class QListWidget; + +class ListWidgetDialog : public QDialog +{ + Q_OBJECT + +public: + ListWidgetDialog(); + +public slots: + void moveLeft(); + void moveRight(); + +private: + QListWidget *leftList; + QListWidget *rightList; +}; + +#endif // LISTWIDGETDIALOG_H diff --git a/Chapter03/listwidget/main.cpp b/Chapter03/listwidget/main.cpp new file mode 100644 index 0000000..a4c2f3e --- /dev/null +++ b/Chapter03/listwidget/main.cpp @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include "listwidgetdialog.h" + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + ListWidgetDialog dlg; + dlg.show(); + + return app.exec(); +} diff --git a/Chapter03/messagebox/cut.png b/Chapter03/messagebox/cut.png new file mode 100644 index 0000000..3c9a806 Binary files /dev/null and b/Chapter03/messagebox/cut.png differ diff --git a/Chapter03/messagebox/main.cpp b/Chapter03/messagebox/main.cpp new file mode 100644 index 0000000..b2cb2e9 --- /dev/null +++ b/Chapter03/messagebox/main.cpp @@ -0,0 +1,143 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include +#include + +#include + +class Tester : public QWidget +{ +public: + void test() + { + QMessageBox::information( + this, + tr("Application Name"), + tr("An information message.") ); + + QMessageBox::warning( + this, + tr("Application Name"), + tr("A warning message.") ); + + QMessageBox::critical( + this, + tr("Application Name"), + tr("A critical message.") ); + + switch( QMessageBox::question( + this, + tr("Application Name"), + tr("An information message."), + + QMessageBox::Yes | + QMessageBox::No | + QMessageBox::Cancel, + + QMessageBox::Cancel ) ) + { + case QMessageBox::Yes: + qDebug( "yes" ); + break; + case QMessageBox::No: + qDebug( "no" ); + break; + case QMessageBox::Cancel: + qDebug( "cancel" ); + break; + default: + qDebug( "close" ); + break; + } + + setWindowIcon( QIcon("cut.png") ); + +{ + bool ok; + int value = QInputDialog::getInteger( + this, + tr("Integer"), + tr("Enter an angle:"), + 90, + 0, + 360, + 1, + &ok ); + if( ok ) + { + } +} + +{ + bool ok; + QStringList items; + items << tr("Foo") << tr("Bar") << tr("Baz"); + QString item = QInputDialog::getItem( + this, + tr("Item"), + tr("Pick an item:"), + items, + 0, + false, + &ok ); + if( ok && !item.isEmpty() ) + { + } +} + +{ + bool ok; + QString text = QInputDialog::getText( + this, + tr("String"), + tr("Enter a city name:"), + QLineEdit::Normal, + tr("Alingsås"), + &ok ); + if( ok && !text.isEmpty() ) + { + } +} + } +}; + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + Tester t; + t.test(); + + return 0; +} diff --git a/Chapter03/messagebox/messagebox.pro b/Chapter03/messagebox/messagebox.pro new file mode 100644 index 0000000..030e6ec --- /dev/null +++ b/Chapter03/messagebox/messagebox.pro @@ -0,0 +1,12 @@ +###################################################################### +# Automatically generated by qmake (2.01a) to 18. jan 17:50:47 2007 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp +CONFIG += console diff --git a/Chapter03/progressbar/progressbar.ui b/Chapter03/progressbar/progressbar.ui new file mode 100644 index 0000000..0fd492b --- /dev/null +++ b/Chapter03/progressbar/progressbar.ui @@ -0,0 +1,349 @@ + + + Copyright (c) 2006-2007, Johan Thelin + + All rights reserved. + + Redistribution and use in source and binary forms, with or without modification, + are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + * Neither the name of APress nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + Dialog + + + + 0 + 0 + 444 + 304 + + + + Dialog + + + + 9 + + + 6 + + + + + 0 + + + 6 + + + + + Progess: + + + + + + + 100 + + + Qt::Horizontal + + + + + + + Reset + + + + + + + + + GroupBox + + + + 9 + + + 6 + + + + + 0 + + + -1 + + + Qt::Horizontal + + + + + + + 24 + + + Qt::Horizontal + + + + + + + Custom text: + + + + + + + 24 + + + Qt::Horizontal + + + %v out of %m steps completed. + + + + + + + Hidden text: + + + + + + + Default: + + + + + + + Infinite range: + + + + + + + 24 + + + false + + + Qt::Horizontal + + + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Close + + + + + + + + + buttonBox + rejected() + Dialog + reject() + + + 291 + 268 + + + 286 + 274 + + + + + horizontalSlider + valueChanged(int) + progressBar + setValue(int) + + + 297 + 23 + + + 147 + 85 + + + + + horizontalSlider + valueChanged(int) + progressBar_2 + setValue(int) + + + 265 + 26 + + + 179 + 121 + + + + + horizontalSlider + valueChanged(int) + progressBar_3 + setValue(int) + + + 293 + 27 + + + 205 + 145 + + + + + horizontalSlider + valueChanged(int) + progressBar_4 + setValue(int) + + + 321 + 27 + + + 235 + 179 + + + + + pushButton + clicked() + progressBar + reset() + + + 370 + 18 + + + 328 + 86 + + + + + pushButton + clicked() + progressBar_2 + reset() + + + 412 + 27 + + + 342 + 113 + + + + + pushButton + clicked() + progressBar_3 + reset() + + + 414 + 19 + + + 358 + 156 + + + + + pushButton + clicked() + progressBar_4 + reset() + + + 416 + 29 + + + 377 + 173 + + + + + diff --git a/Chapter03/pushbutton/buttondialog.cpp b/Chapter03/pushbutton/buttondialog.cpp new file mode 100644 index 0000000..0c14785 --- /dev/null +++ b/Chapter03/pushbutton/buttondialog.cpp @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include +#include + +#include "buttondialog.h" + +ButtonDialog::ButtonDialog( QWidget *parent ) : QDialog( parent ) +{ + clickButton = new QPushButton( "Click me!", this ); + toggleButton = new QPushButton( "Toggle me!", this ); + toggleButton->setCheckable( true ); + + QHBoxLayout *layout = new QHBoxLayout( this ); + layout->addWidget( clickButton ); + layout->addWidget( toggleButton ); + + connect( clickButton, SIGNAL(clicked()), this, SLOT(buttonClicked()) ); + connect( toggleButton, SIGNAL(clicked()), this, SLOT(buttonToggled()) ); +} + +void ButtonDialog::buttonClicked() +{ + QMessageBox::information( this, "Clicked!", "The button was clicked!" ); +} + +void ButtonDialog::buttonToggled() +{ + QMessageBox::information( this, "Toggled!", QString("The button is %1!").arg(toggleButton->isChecked()?"pressed":"released") ); +} diff --git a/Chapter03/pushbutton/buttondialog.h b/Chapter03/pushbutton/buttondialog.h new file mode 100644 index 0000000..b5136da --- /dev/null +++ b/Chapter03/pushbutton/buttondialog.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef BUTTONDIALOG_H +#define BUTTONDIALOG_H + +#include + +class QPushButton; + +class ButtonDialog : public QDialog +{ + Q_OBJECT + +public: + ButtonDialog( QWidget *parent=0 ); + +private slots: + void buttonClicked(); + void buttonToggled(); + +private: + QPushButton *clickButton; + QPushButton *toggleButton; +}; + +#endif // BUTTONDIALOG_H diff --git a/Chapter03/pushbutton/main.cpp b/Chapter03/pushbutton/main.cpp new file mode 100644 index 0000000..4ce03bc --- /dev/null +++ b/Chapter03/pushbutton/main.cpp @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include "buttondialog.h" + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + ButtonDialog dlg; + dlg.show(); + + return app.exec(); +} \ No newline at end of file diff --git a/Chapter03/pushbutton/pushbutton.pro b/Chapter03/pushbutton/pushbutton.pro new file mode 100644 index 0000000..04dcd40 --- /dev/null +++ b/Chapter03/pushbutton/pushbutton.pro @@ -0,0 +1,12 @@ +###################################################################### +# Automatically generated by qmake (2.01a) lö 9. sep 14:12:57 2006 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +HEADERS += buttondialog.h +SOURCES += buttondialog.cpp main.cpp diff --git a/Chapter03/radiobutton/main.cpp b/Chapter03/radiobutton/main.cpp new file mode 100644 index 0000000..19752da --- /dev/null +++ b/Chapter03/radiobutton/main.cpp @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include +#include +#include + +#include + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + QGroupBox box( "Printing Options" ); + + QRadioButton *portrait = new QRadioButton( "Portrait" ); + QRadioButton *landscape = new QRadioButton( "Landscape" ); + QRadioButton *color = new QRadioButton( "Color" ); + QRadioButton *bw = new QRadioButton( "B&W" ); + + QButtonGroup *orientation = new QButtonGroup( &box ); + QButtonGroup *colorBw = new QButtonGroup( &box ); + + orientation->addButton( portrait ); + orientation->addButton( landscape ); + colorBw->addButton( color ); + colorBw->addButton( bw ); + + QGridLayout *grid = new QGridLayout( &box ); + grid->addWidget( portrait, 0, 0 ); + grid->addWidget( landscape, 0, 1 ); + grid->addWidget( color, 1, 0 ); + grid->addWidget( bw, 1, 1 ); + + box.show(); + + return app.exec(); +} \ No newline at end of file diff --git a/Chapter03/radiobutton/radiobutton.pro b/Chapter03/radiobutton/radiobutton.pro new file mode 100644 index 0000000..6b59bf9 --- /dev/null +++ b/Chapter03/radiobutton/radiobutton.pro @@ -0,0 +1,11 @@ +###################################################################### +# Automatically generated by qmake (2.01a) ti 12. sep 13:45:17 2006 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp diff --git a/Chapter03/regexp/main.cpp b/Chapter03/regexp/main.cpp new file mode 100644 index 0000000..9b625b0 --- /dev/null +++ b/Chapter03/regexp/main.cpp @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include + +#include + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + QRegExp re("^\\+\\d{1,2}\\([089]\\)\\d{2,5}\\-\\d+$"); + + qDebug() << re.indexIn("+46(0)31-445566"); // 0 + qDebug() << re.indexIn("Tel: +46(0)31-445566"); // -1 + qDebug() << re.indexIn("(0)31-445566"); // -1 + + QRegExp reCap("^\\+(\\d{1,2})\\(([089])\\)(\\d{2,5})\\-(\\d+)$"); + + qDebug() << reCap.indexIn("+46(0)31-445566"); // 0 + qDebug() << reCap.cap(0); // "+46(0)31-445566" + qDebug() << reCap.cap(1); // "46" + qDebug() << reCap.cap(2); // "0" + qDebug() << reCap.cap(3); // "31" + qDebug() << reCap.cap(4); // "445566" + + return 0; +} diff --git a/Chapter03/regexp/regexp.pro b/Chapter03/regexp/regexp.pro new file mode 100644 index 0000000..c7f95b2 --- /dev/null +++ b/Chapter03/regexp/regexp.pro @@ -0,0 +1,12 @@ +###################################################################### +# Automatically generated by qmake (2.01a) lö 20. jan 18:14:57 2007 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp +CONFIG += console diff --git a/Chapter03/revalidator/main.cpp b/Chapter03/revalidator/main.cpp new file mode 100644 index 0000000..f4ce84c --- /dev/null +++ b/Chapter03/revalidator/main.cpp @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include + +#include + +#include +#include +#include + +#include + +class ValidationDialog : public QDialog +{ +public: + ValidationDialog() + { + QGridLayout *layout = new QGridLayout( this ); + + QLineEdit *reEdit = new QLineEdit( "+46(0)31-445566" ); + QPushButton *button = new QPushButton( "Close" ); + + layout->addWidget( new QLabel("Phone:"), 0, 0 ); + layout->addWidget( reEdit, 0, 1 ); + layout->addWidget( button, 1, 0, 1, 2 ); + + QRegExpValidator *reVal = new QRegExpValidator( + QRegExp("\\+\\d{1,2}\\([089]\\)\\d{2,5}\\-\\d+"), + this ); + reEdit->setValidator( reVal ); + + connect( button, SIGNAL(clicked()), this, SLOT(accept()) ); + } +}; + +int main( int argc, char** argv ) +{ + QApplication app( argc, argv ); + + ValidationDialog w; + w.show(); + + return app.exec(); +} diff --git a/Chapter03/revalidator/revalidator.pro b/Chapter03/revalidator/revalidator.pro new file mode 100644 index 0000000..6ac7239 --- /dev/null +++ b/Chapter03/revalidator/revalidator.pro @@ -0,0 +1,11 @@ +###################################################################### +# Automatically generated by qmake (2.01a) lö 20. jan 18:35:22 2007 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp diff --git a/Chapter03/spinbox/spinbox.ui b/Chapter03/spinbox/spinbox.ui new file mode 100644 index 0000000..1bb705a --- /dev/null +++ b/Chapter03/spinbox/spinbox.ui @@ -0,0 +1,120 @@ + + + Copyright (c) 2006-2007, Johan Thelin + + All rights reserved. + + Redistribution and use in source and binary forms, with or without modification, + are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + * Neither the name of APress nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + Dialog + + + + 0 + 0 + 181 + 176 + + + + Dialog + + + + 9 + + + 6 + + + + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::NoButton|QDialogButtonBox::Ok + + + + + + + + + buttonBox + accepted() + Dialog + accept() + + + 171 + 154 + + + 157 + 175 + + + + + buttonBox + rejected() + Dialog + reject() + + + 171 + 160 + + + 180 + 175 + + + + + spinBox + valueChanged(int) + lcdNumber + display(int) + + + 22 + 122 + + + 36 + 58 + + + + + diff --git a/Chapter03/validator/main.cpp b/Chapter03/validator/main.cpp new file mode 100644 index 0000000..acc0a32 --- /dev/null +++ b/Chapter03/validator/main.cpp @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include + +#include + +#include +#include +#include + +#include +#include + +class ValidationDialog : public QDialog +{ +public: + ValidationDialog() + { + QGridLayout *layout = new QGridLayout( this ); + + QLineEdit *intEdit = new QLineEdit( "42" ); + QLineEdit *doubleEdit = new QLineEdit( "3.14" ); + QPushButton *button = new QPushButton( "Close" ); + + layout->addWidget( new QLabel("Integer:"), 0, 0 ); + layout->addWidget( intEdit, 0, 1 ); + layout->addWidget( new QLabel("Double:"), 1, 0 ); + layout->addWidget( doubleEdit, 1, 1 ); + layout->addWidget( button, 2, 0, 1, 2 ); + + QIntValidator *intVal = new QIntValidator( 0, 100, this ); + intEdit->setValidator( intVal ); + + QDoubleValidator *doubleVal = new QDoubleValidator( -32.5, 99.0, 1, this ); + doubleEdit->setValidator( doubleVal ); + + connect( button, SIGNAL(clicked()), this, SLOT(accept()) ); + } +}; + +int main( int argc, char** argv ) +{ + QApplication app( argc, argv ); + + ValidationDialog w; + w.show(); + + return app.exec(); +} diff --git a/Chapter03/validator/validator.pro b/Chapter03/validator/validator.pro new file mode 100644 index 0000000..cf22d7d --- /dev/null +++ b/Chapter03/validator/validator.pro @@ -0,0 +1,11 @@ +###################################################################### +# Automatically generated by qmake (2.01a) fr 19. jan 15:56:44 2007 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp diff --git a/Chapter04/README.txt b/Chapter04/README.txt new file mode 100644 index 0000000..d2e3181 --- /dev/null +++ b/Chapter04/README.txt @@ -0,0 +1,32 @@ +This file is a part of 1590598318-1.zip containing example source code for the +Foundations of Qt Development book available from APress (ISBN 1590598318). + +These are the examples for chapter 4 - The Main Window + +sdi + + Listings 4-1, 4-2, 4-3, 4-4, 4-5 + + A simple text editor with a single document interface (SDI). + + +mdi + + Listings 4-6, 4-7, 4-8, 4-9, 4-10, 4-11, 4-12, 4-13, 4-14, 4-15 + + A simple text editor with a multiple document interface (MDI). + + +resources + + Listings 4-16, 4-17, 4-18 + + Example resource files. + + +dock + + Listings 4-19, 4-20, 4-21, 4-22, 4-23 + + Shows how a dock widgets can be added to a QMainWindow. + diff --git a/Chapter04/dock/dock.pro b/Chapter04/dock/dock.pro new file mode 100644 index 0000000..717d151 --- /dev/null +++ b/Chapter04/dock/dock.pro @@ -0,0 +1,15 @@ +###################################################################### +# Automatically generated by qmake (2.01a) to 28. sep 16:43:38 2006 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +HEADERS += sdiwindow.h infowidget.h +SOURCES += main.cpp sdiwindow.cpp infowidget.cpp +RESOURCES += images.qrc + +CONFIG += console diff --git a/Chapter04/dock/images.qrc b/Chapter04/dock/images.qrc new file mode 100644 index 0000000..44947aa --- /dev/null +++ b/Chapter04/dock/images.qrc @@ -0,0 +1,8 @@ + + + images/new.png + images/cut.png + images/copy.png + images/paste.png + + \ No newline at end of file diff --git a/Chapter04/dock/images/copy.png b/Chapter04/dock/images/copy.png new file mode 100644 index 0000000..7052fab Binary files /dev/null and b/Chapter04/dock/images/copy.png differ diff --git a/Chapter04/dock/images/cut.png b/Chapter04/dock/images/cut.png new file mode 100644 index 0000000..3c9a806 Binary files /dev/null and b/Chapter04/dock/images/cut.png differ diff --git a/Chapter04/dock/images/new.png b/Chapter04/dock/images/new.png new file mode 100644 index 0000000..d1e8915 Binary files /dev/null and b/Chapter04/dock/images/new.png differ diff --git a/Chapter04/dock/images/paste.png b/Chapter04/dock/images/paste.png new file mode 100644 index 0000000..ee6558a Binary files /dev/null and b/Chapter04/dock/images/paste.png differ diff --git a/Chapter04/dock/infowidget.cpp b/Chapter04/dock/infowidget.cpp new file mode 100644 index 0000000..66aae2a --- /dev/null +++ b/Chapter04/dock/infowidget.cpp @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "infowidget.h" + +InfoWidget::InfoWidget( QWidget *parent ) : QLabel( parent ) +{ + setAlignment( Qt::AlignCenter ); + setWordWrap( true ); + + setText( tr("Ready") ); +} + +void InfoWidget::documentChanged( int position, int charsRemoved, int charsAdded ) +{ + QString text; + + if( charsRemoved ) + text = tr("%1 removed").arg( charsRemoved ); + + if( charsRemoved && charsAdded ) + text += tr(", "); + + if( charsAdded ) + text += tr("%1 added").arg( charsAdded ); + + setText( text ); +} diff --git a/Chapter04/dock/infowidget.h b/Chapter04/dock/infowidget.h new file mode 100644 index 0000000..278c44e --- /dev/null +++ b/Chapter04/dock/infowidget.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef INFOWIDGET_H +#define INFOWIDGET_H + +#include + +class InfoWidget : public QLabel +{ + Q_OBJECT + +public: + InfoWidget( QWidget *parent=0 ); + +public slots: + void documentChanged( int position, int charsRemoved, int charsAdded ); +}; + +#endif // INFOWIDGET_H diff --git a/Chapter04/dock/main.cpp b/Chapter04/dock/main.cpp new file mode 100644 index 0000000..5dcbd81 --- /dev/null +++ b/Chapter04/dock/main.cpp @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include "sdiwindow.h" + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + (new SdiWindow)->show(); + + return app.exec(); +} diff --git a/Chapter04/dock/sdiwindow.cpp b/Chapter04/dock/sdiwindow.cpp new file mode 100644 index 0000000..7229d9b --- /dev/null +++ b/Chapter04/dock/sdiwindow.cpp @@ -0,0 +1,192 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "infowidget.h" + +#include "sdiwindow.h" + +SdiWindow::SdiWindow( QWidget *parent ) : QMainWindow( parent ) +{ + setAttribute( Qt::WA_DeleteOnClose ); + setWindowTitle( tr("%1[*] - %2" ).arg(tr("unnamed")).arg(tr("SDI")) ); + + docWidget = new QTextEdit( this ); + setCentralWidget( docWidget ); + + connect( docWidget->document(), SIGNAL(modificationChanged(bool)), this, SLOT(setWindowModified(bool)) ); + + createDocks(); + createActions(); + createMenus(); + createToolbars(); + statusBar()->showMessage( tr("Done") ); +} + +void SdiWindow::createDocks() +{ + dock = new QDockWidget( tr("Information"), this ); + InfoWidget *info = new InfoWidget( dock ); + dock->setWidget( info ); + addDockWidget( Qt::LeftDockWidgetArea, dock ); + + connect( docWidget->document(), SIGNAL(contentsChange(int, int, int)), info, SLOT(documentChanged(int, int, int)) ); +} + +void SdiWindow::closeEvent( QCloseEvent *event ) +{ + if( isSafeToClose() ) + event->accept(); + else + event->ignore(); +} + +bool SdiWindow::isSafeToClose() +{ + if( isWindowModified() ) + { + switch( QMessageBox::warning( this, tr("SDI"), + tr("The document has unsaved changes.\n" + "Do you want to save it before it is closed?"), + QMessageBox::Discard | QMessageBox::Cancel ) ) + { + case QMessageBox::Cancel: + return false; + default: + return true; + } + } + + return true; +} + +void SdiWindow::fileNew() +{ + (new SdiWindow())->show(); +} + +void SdiWindow::helpAbout() +{ + QMessageBox::about( this, tr("About SDI"), tr("A single document interface application.") ); +} + +void SdiWindow::createActions() +{ + newAction = new QAction( QIcon(":/images/new.png"), tr("&New"), this ); + newAction->setShortcut( tr("Ctrl+N") ); + newAction->setStatusTip( tr("Create a new document") ); + connect( newAction, SIGNAL(triggered()), this, SLOT(fileNew()) ); + + closeAction = new QAction( tr("&Close"), this ); + closeAction->setShortcut( tr("Ctrl+W") ); + closeAction->setStatusTip( tr("Close this document") ); + connect( closeAction, SIGNAL(triggered()), this, SLOT(close()) ); + + exitAction = new QAction( tr("E&xit"), this ); + exitAction->setShortcut( tr("Ctrl+Q") ); + exitAction->setStatusTip( tr("Quit the application") ); + connect( exitAction, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()) ); + + + cutAction = new QAction( QIcon(":/images/cut.png"), tr("Cu&t"), this ); + cutAction->setShortcut( tr("Ctrl+X") ); + cutAction->setStatusTip( tr("Cut") ); + cutAction->setEnabled(false); + connect( docWidget, SIGNAL(copyAvailable(bool)), cutAction, SLOT(setEnabled(bool)) ); + connect( cutAction, SIGNAL(triggered()), docWidget, SLOT(cut()) ); + + copyAction = new QAction( QIcon(":/images/copy.png"), tr("&Copy"), this ); + copyAction->setShortcut( tr("Ctrl+C") ); + copyAction->setStatusTip( tr("Copy") ); + copyAction->setEnabled(false); + connect( docWidget, SIGNAL(copyAvailable(bool)), copyAction, SLOT(setEnabled(bool)) ); + connect( copyAction, SIGNAL(triggered()), docWidget, SLOT(copy()) ); + + pasteAction = new QAction( QIcon(":/images/paste.png"), tr("&Paste"), this ); + pasteAction->setShortcut( tr("Ctrl+V") ); + pasteAction->setStatusTip( tr("Paste") ); + connect( pasteAction, SIGNAL(triggered()), docWidget, SLOT(paste()) ); + + + aboutAction = new QAction( tr("&About"), this ); + aboutAction->setStatusTip( tr("About this application") ); + connect( aboutAction, SIGNAL(triggered()), this, SLOT(helpAbout()) ); + + aboutQtAction = new QAction( tr("About &Qt"), this ); + aboutQtAction->setStatusTip( tr("About the Qt toolkit") ); + connect( aboutQtAction, SIGNAL(triggered()), qApp, SLOT(aboutQt()) ); +} + +void SdiWindow::createMenus() +{ + QMenu *menu; + + menu = menuBar()->addMenu( tr("&File") ); + menu->addAction( newAction ); + menu->addAction( closeAction ); + menu->addSeparator(); + menu->addAction( exitAction ); + + menu = menuBar()->addMenu( tr("&Edit") ); + menu->addAction( cutAction ); + menu->addAction( copyAction ); + menu->addAction( pasteAction ); + + menu = menuBar()->addMenu( tr("&View") ); + menu->addAction( dock->toggleViewAction() ); + + menu = menuBar()->addMenu( tr("&Help") ); + menu->addAction( aboutAction ); + menu->addAction( aboutQtAction ); +} + +void SdiWindow::createToolbars() +{ + QToolBar *toolbar; + + toolbar = addToolBar( tr("File") ); + toolbar->addAction( newAction ); + + toolbar = addToolBar( tr("Edit") ); + toolbar->addAction( cutAction ); + toolbar->addAction( copyAction ); + toolbar->addAction( pasteAction ); +} + diff --git a/Chapter04/dock/sdiwindow.h b/Chapter04/dock/sdiwindow.h new file mode 100644 index 0000000..21d24b1 --- /dev/null +++ b/Chapter04/dock/sdiwindow.h @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef SDIWINDOW_H +#define SDIWINDOW_H + +#include + +class QAction; +class QTextEdit; +class QDockWidget; + +class SdiWindow : public QMainWindow +{ + Q_OBJECT + +public: + SdiWindow( QWidget *parent = 0 ); + +protected: + void closeEvent( QCloseEvent *event ); + +private slots: + void fileNew(); + void helpAbout(); + +private: + void createDocks(); + void createActions(); + void createMenus(); + void createToolbars(); + + bool isSafeToClose(); + + QTextEdit *docWidget; + + QAction *newAction; + QAction *closeAction; + QAction *exitAction; + + QAction *cutAction; + QAction *copyAction; + QAction *pasteAction; + + QAction *aboutAction; + QAction *aboutQtAction; + + QDockWidget *dock; +}; + +#endif // SDIWINDOW_H diff --git a/Chapter04/mdi/documentwindow.cpp b/Chapter04/mdi/documentwindow.cpp new file mode 100644 index 0000000..a5478be --- /dev/null +++ b/Chapter04/mdi/documentwindow.cpp @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include +#include +#include +#include +#include + +#include + +#include "documentwindow.h" + +DocumentWindow::DocumentWindow( QWidget *parent ) : QTextEdit( parent ) +{ + setAttribute( Qt::WA_DeleteOnClose ); + setWindowTitle( tr("%1[*]" ).arg("unnamed") ); + + connect( document(), SIGNAL(modificationChanged(bool)), this, SLOT(setWindowModified(bool)) ); +} + +void DocumentWindow::closeEvent( QCloseEvent *event ) +{ + if( isSafeToClose() ) + event->accept(); + else + event->ignore(); +} + +bool DocumentWindow::isSafeToClose() +{ + if( document()->isModified() ) + { + switch( QMessageBox::warning( this, tr("MDI"), + tr("The document has unsaved changes.\n" + "Do you want to save it before it is closed?"), + QMessageBox::Discard | QMessageBox::Cancel ) ) + { + case QMessageBox::Cancel: + return false; + default: + return true; + } + } + + return true; +} diff --git a/Chapter04/mdi/documentwindow.h b/Chapter04/mdi/documentwindow.h new file mode 100644 index 0000000..d8deb9f --- /dev/null +++ b/Chapter04/mdi/documentwindow.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef DOCUMENTWINDOW_H +#define DOCUMENTWINDOW_H + +#include + +class DocumentWindow : public QTextEdit +{ + Q_OBJECT + +public: + DocumentWindow( QWidget *parent = 0 ); + +protected: + void closeEvent( QCloseEvent *event ); + +private: + bool isSafeToClose(); +}; + +#endif // DOCUMENTWINDOW_H diff --git a/Chapter04/mdi/images.qrc b/Chapter04/mdi/images.qrc new file mode 100644 index 0000000..44947aa --- /dev/null +++ b/Chapter04/mdi/images.qrc @@ -0,0 +1,8 @@ + + + images/new.png + images/cut.png + images/copy.png + images/paste.png + + \ No newline at end of file diff --git a/Chapter04/mdi/images/copy.png b/Chapter04/mdi/images/copy.png new file mode 100644 index 0000000..7052fab Binary files /dev/null and b/Chapter04/mdi/images/copy.png differ diff --git a/Chapter04/mdi/images/cut.png b/Chapter04/mdi/images/cut.png new file mode 100644 index 0000000..3c9a806 Binary files /dev/null and b/Chapter04/mdi/images/cut.png differ diff --git a/Chapter04/mdi/images/new.png b/Chapter04/mdi/images/new.png new file mode 100644 index 0000000..d1e8915 Binary files /dev/null and b/Chapter04/mdi/images/new.png differ diff --git a/Chapter04/mdi/images/paste.png b/Chapter04/mdi/images/paste.png new file mode 100644 index 0000000..ee6558a Binary files /dev/null and b/Chapter04/mdi/images/paste.png differ diff --git a/Chapter04/mdi/main.cpp b/Chapter04/mdi/main.cpp new file mode 100644 index 0000000..fe4fb5f --- /dev/null +++ b/Chapter04/mdi/main.cpp @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include "mdiwindow.h" + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + MdiWindow win; + win.show(); + + return app.exec(); +} diff --git a/Chapter04/mdi/mdi.pro b/Chapter04/mdi/mdi.pro new file mode 100644 index 0000000..323c7c3 --- /dev/null +++ b/Chapter04/mdi/mdi.pro @@ -0,0 +1,14 @@ +###################################################################### +# Automatically generated by qmake (2.01a) to 28. sep 16:43:38 2006 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +HEADERS += mdiwindow.h documentwindow.h +SOURCES += main.cpp mdiwindow.cpp documentwindow.cpp +RESOURCES += images.qrc +CONFIG+=console diff --git a/Chapter04/mdi/mdiwindow.cpp b/Chapter04/mdi/mdiwindow.cpp new file mode 100644 index 0000000..e2f9f1b --- /dev/null +++ b/Chapter04/mdi/mdiwindow.cpp @@ -0,0 +1,256 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "mdiwindow.h" + +#include "documentwindow.h" + +MdiWindow::MdiWindow( QWidget *parent ) : QMainWindow( parent ) +{ + setWindowTitle( tr( "MDI" ) ); + + workspace = new QWorkspace; + setCentralWidget( workspace ); + + connect( workspace, SIGNAL(windowActivated(QWidget *)), this, SLOT(enableActions())); + mapper = new QSignalMapper( this ); + connect( mapper, SIGNAL(mapped(QWidget*)), workspace, SLOT(setActiveWindow(QWidget*)) ); + + createActions(); + createMenus(); + createToolbars(); + statusBar()->showMessage( tr("Done") ); + + enableActions(); +} + +void MdiWindow::closeEvent( QCloseEvent *event ) +{ + workspace->closeAllWindows(); + + if( !activeDocument() ) + event->accept(); + else + event->ignore(); +} + +void MdiWindow::fileNew() +{ + DocumentWindow *document = new DocumentWindow; + workspace->addWindow( document ); + + connect( document, SIGNAL(copyAvailable(bool)), cutAction, SLOT(setEnabled(bool)) ); + connect( document, SIGNAL(copyAvailable(bool)), copyAction, SLOT(setEnabled(bool)) ); + + document->show(); +} + +void MdiWindow::helpAbout() +{ + QMessageBox::about( this, tr("About MDI"), tr("A multiple document interface application.") ); +} + +DocumentWindow *MdiWindow::activeDocument() +{ + return qobject_cast(workspace->activeWindow()); +} + +void MdiWindow::enableActions() +{ + bool hasDocuments = (activeDocument() != 0 ); + + closeAction->setEnabled( hasDocuments ); + pasteAction->setEnabled( hasDocuments ); + tileAction->setEnabled( hasDocuments ); + cascadeAction->setEnabled( hasDocuments ); + nextAction->setEnabled( hasDocuments ); + previousAction->setEnabled( hasDocuments ); + separatorAction->setVisible( hasDocuments ); + + bool hasSelection = hasDocuments && activeDocument()->textCursor().hasSelection(); + + cutAction->setEnabled( hasSelection ); + copyAction->setEnabled( hasSelection ); +} + +void MdiWindow::createActions() +{ + newAction = new QAction( QIcon(":/images/new.png"), tr("&New"), this ); + newAction->setShortcut( tr("Ctrl+N") ); + newAction->setStatusTip( tr("Create a new document") ); + connect( newAction, SIGNAL(triggered()), this, SLOT(fileNew()) ); + + closeAction = new QAction( tr("&Close"), this ); + closeAction->setShortcut( tr("Ctrl+W") ); + closeAction->setStatusTip( tr("Close this document") ); + connect( closeAction, SIGNAL(triggered()), workspace, SLOT(closeActiveWindow()) ); + + exitAction = new QAction( tr("E&xit"), this ); + exitAction->setShortcut( tr("Ctrl+Q") ); + exitAction->setStatusTip( tr("Quit the application") ); + connect( exitAction, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()) ); + + + cutAction = new QAction( QIcon(":/images/cut.png"), tr("Cu&t"), this ); + cutAction->setShortcut( tr("Ctrl+X") ); + cutAction->setStatusTip( tr("Cut") ); + cutAction->setEnabled(false); + connect( cutAction, SIGNAL(triggered()), this, SLOT(editCut()) ); + + copyAction = new QAction( QIcon(":/images/copy.png"), tr("&Copy"), this ); + copyAction->setShortcut( tr("Ctrl+C") ); + copyAction->setStatusTip( tr("Copy") ); + copyAction->setEnabled(false); + connect( copyAction, SIGNAL(triggered()), this, SLOT(editCopy()) ); + + pasteAction = new QAction( QIcon(":/images/paste.png"), tr("&Paste"), this ); + pasteAction->setShortcut( tr("Ctrl+V") ); + pasteAction->setStatusTip( tr("Paste") ); + connect( pasteAction, SIGNAL(triggered()), this, SLOT(editPaste()) ); + + + tileAction = new QAction( tr("&Tile"), this ); + tileAction->setStatusTip( tr("Tile the windows") ); + connect( tileAction, SIGNAL(triggered()), workspace, SLOT(tile()) ); + + cascadeAction = new QAction( tr("&Cascade"), this ); + cascadeAction->setStatusTip( tr("Cascade the windows") ); + connect( cascadeAction, SIGNAL(triggered()), workspace, SLOT(cascade()) ); + + nextAction = new QAction( tr("&Next window"), this ); + nextAction->setStatusTip( tr("Move to the next window") ); + connect( nextAction, SIGNAL(triggered()), workspace, SLOT(activateNextWindow()) ); + + previousAction = new QAction( tr("&Previous window"), this ); + previousAction->setStatusTip( tr("Move to the previous window") ); + connect( previousAction, SIGNAL(triggered()), workspace, SLOT(activatePreviousWindow()) ); + + separatorAction = new QAction( this ); + separatorAction->setSeparator( true ); + + + aboutAction = new QAction( tr("&About"), this ); + aboutAction->setStatusTip( tr("About this application") ); + connect( aboutAction, SIGNAL(triggered()), this, SLOT(helpAbout()) ); + + aboutQtAction = new QAction( tr("About &Qt"), this ); + aboutQtAction->setStatusTip( tr("About the Qt toolkit") ); + connect( aboutQtAction, SIGNAL(triggered()), qApp, SLOT(aboutQt()) ); +} + +void MdiWindow::createMenus() +{ + QMenu *menu; + + menu = menuBar()->addMenu( tr("&File") ); + menu->addAction( newAction ); + menu->addAction( closeAction ); + menu->addSeparator(); + menu->addAction( exitAction ); + + menu = menuBar()->addMenu( tr("&Edit") ); + menu->addAction( cutAction ); + menu->addAction( copyAction ); + menu->addAction( pasteAction ); + + windowMenu = menuBar()->addMenu( tr("&Window") ); + connect( windowMenu, SIGNAL(aboutToShow()), this, SLOT(updateWindowList()) ); + + menu = menuBar()->addMenu( tr("&Help") ); + menu->addAction( aboutAction ); + menu->addAction( aboutQtAction ); +} + +void MdiWindow::createToolbars() +{ + QToolBar *toolbar; + + toolbar = addToolBar( tr("File") ); + toolbar->addAction( newAction ); + + toolbar = addToolBar( tr("Edit") ); + toolbar->addAction( cutAction ); + toolbar->addAction( copyAction ); + toolbar->addAction( pasteAction ); +} + +void MdiWindow::editCut() +{ + activeDocument()->cut(); +} + +void MdiWindow::editCopy() +{ + activeDocument()->copy(); +} + +void MdiWindow::editPaste() +{ + activeDocument()->paste(); +} + +void MdiWindow::updateWindowList() +{ + windowMenu->clear(); + + windowMenu->addAction( tileAction ); + windowMenu->addAction( cascadeAction ); + windowMenu->addSeparator(); + windowMenu->addAction( nextAction ); + windowMenu->addAction( previousAction ); + windowMenu->addAction( separatorAction ); + + int i=1; + foreach( QWidget *w, workspace->windowList() ) + { + QString text; + if( i<10 ) + text = tr("&%1 %2").arg( i++ ).arg( w->windowTitle() ); + else + text = w->windowTitle(); + + QAction *action = windowMenu->addAction( text ); + action->setCheckable( true ); + action->setChecked( w == activeDocument() ); + connect( action, SIGNAL(triggered()), mapper, SLOT(map()) ); + mapper->setMapping( action, w ); + } +} diff --git a/Chapter04/mdi/mdiwindow.h b/Chapter04/mdi/mdiwindow.h new file mode 100644 index 0000000..85ec44f --- /dev/null +++ b/Chapter04/mdi/mdiwindow.h @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef MDIWINDOW_H +#define MDIWINDOW_H + +#include + +class QAction; +class QWorkspace; +class QSignalMapper; +class QMenu; + +class DocumentWindow; + +class MdiWindow : public QMainWindow +{ + Q_OBJECT + +public: + MdiWindow( QWidget *parent = 0 ); + +protected: + void closeEvent( QCloseEvent *event ); + +private slots: + void fileNew(); + + void editCut(); + void editCopy(); + void editPaste(); + + void helpAbout(); + + void enableActions(); + void updateWindowList(); + +private: + void createActions(); + void createMenus(); + void createToolbars(); + + DocumentWindow *activeDocument(); + + QWorkspace *workspace; + QSignalMapper *mapper; + + QAction *newAction; + QAction *closeAction; + QAction *exitAction; + + QAction *cutAction; + QAction *copyAction; + QAction *pasteAction; + + QAction *tileAction; + QAction *cascadeAction; + QAction *nextAction; + QAction *previousAction; + QAction *separatorAction; + + QAction *aboutAction; + QAction *aboutQtAction; + + QMenu *windowMenu; +}; + +#endif // MDIWINDOW_H diff --git a/Chapter04/resources/alias.qrc b/Chapter04/resources/alias.qrc new file mode 100644 index 0000000..4fb9b8e --- /dev/null +++ b/Chapter04/resources/alias.qrc @@ -0,0 +1,6 @@ + + + images/new.png + images/new.png + + diff --git a/Chapter04/resources/direct.qrc b/Chapter04/resources/direct.qrc new file mode 100644 index 0000000..92c7a1d --- /dev/null +++ b/Chapter04/resources/direct.qrc @@ -0,0 +1,8 @@ + + + images/new.png + images/cut.png + images/copy.png + images/paste.png + + diff --git a/Chapter04/resources/prefix.qrc b/Chapter04/resources/prefix.qrc new file mode 100644 index 0000000..1759b8b --- /dev/null +++ b/Chapter04/resources/prefix.qrc @@ -0,0 +1,10 @@ + + + images/new.png + + + images/cut.png + images/copy.png + images/paste.png + + diff --git a/Chapter04/sdi/images.qrc b/Chapter04/sdi/images.qrc new file mode 100644 index 0000000..44947aa --- /dev/null +++ b/Chapter04/sdi/images.qrc @@ -0,0 +1,8 @@ + + + images/new.png + images/cut.png + images/copy.png + images/paste.png + + \ No newline at end of file diff --git a/Chapter04/sdi/images/copy.png b/Chapter04/sdi/images/copy.png new file mode 100644 index 0000000..7052fab Binary files /dev/null and b/Chapter04/sdi/images/copy.png differ diff --git a/Chapter04/sdi/images/cut.png b/Chapter04/sdi/images/cut.png new file mode 100644 index 0000000..3c9a806 Binary files /dev/null and b/Chapter04/sdi/images/cut.png differ diff --git a/Chapter04/sdi/images/new.png b/Chapter04/sdi/images/new.png new file mode 100644 index 0000000..d1e8915 Binary files /dev/null and b/Chapter04/sdi/images/new.png differ diff --git a/Chapter04/sdi/images/paste.png b/Chapter04/sdi/images/paste.png new file mode 100644 index 0000000..ee6558a Binary files /dev/null and b/Chapter04/sdi/images/paste.png differ diff --git a/Chapter04/sdi/main.cpp b/Chapter04/sdi/main.cpp new file mode 100644 index 0000000..5dcbd81 --- /dev/null +++ b/Chapter04/sdi/main.cpp @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include "sdiwindow.h" + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + (new SdiWindow)->show(); + + return app.exec(); +} diff --git a/Chapter04/sdi/sdi.pro b/Chapter04/sdi/sdi.pro new file mode 100644 index 0000000..78585fc --- /dev/null +++ b/Chapter04/sdi/sdi.pro @@ -0,0 +1,13 @@ +###################################################################### +# Automatically generated by qmake (2.01a) to 28. sep 16:43:38 2006 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +HEADERS += sdiwindow.h +SOURCES += main.cpp sdiwindow.cpp +RESOURCES += images.qrc diff --git a/Chapter04/sdi/sdiwindow.cpp b/Chapter04/sdi/sdiwindow.cpp new file mode 100644 index 0000000..309ad69 --- /dev/null +++ b/Chapter04/sdi/sdiwindow.cpp @@ -0,0 +1,175 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include +#include +#include +#include +#include + +#include + +#include "sdiwindow.h" + +SdiWindow::SdiWindow( QWidget *parent ) : QMainWindow( parent ) +{ + setAttribute( Qt::WA_DeleteOnClose ); + setWindowTitle( tr("%1[*] - %2" ).arg(tr("unnamed")).arg(tr("SDI")) ); + + docWidget = new QTextEdit( this ); + setCentralWidget( docWidget ); + + connect( docWidget->document(), SIGNAL(modificationChanged(bool)), this, SLOT(setWindowModified(bool)) ); + + createActions(); + createMenus(); + createToolbars(); + statusBar()->showMessage( tr("Done") ); +} + +void SdiWindow::closeEvent( QCloseEvent *event ) +{ + if( isSafeToClose() ) + event->accept(); + else + event->ignore(); +} + +bool SdiWindow::isSafeToClose() +{ + if( isWindowModified() ) + { + switch( QMessageBox::warning( this, tr("SDI"), + tr("The document has unsaved changes.\n" + "Do you want to save it before it is closed?"), + QMessageBox::Discard | QMessageBox::Cancel ) ) + { + case QMessageBox::Cancel: + return false; + default: + return true; + } + } + + return true; +} + +void SdiWindow::fileNew() +{ + (new SdiWindow())->show(); +} + +void SdiWindow::helpAbout() +{ + QMessageBox::about( this, tr("About SDI"), tr("A single document interface application.") ); +} + +void SdiWindow::createActions() +{ + newAction = new QAction( QIcon(":/images/new.png"), tr("&New"), this ); + newAction->setShortcut( tr("Ctrl+N") ); + newAction->setStatusTip( tr("Create a new document") ); + connect( newAction, SIGNAL(triggered()), this, SLOT(fileNew()) ); + + closeAction = new QAction( tr("&Close"), this ); + closeAction->setShortcut( tr("Ctrl+W") ); + closeAction->setStatusTip( tr("Close this document") ); + connect( closeAction, SIGNAL(triggered()), this, SLOT(close()) ); + + exitAction = new QAction( tr("E&xit"), this ); + exitAction->setShortcut( tr("Ctrl+Q") ); + exitAction->setStatusTip( tr("Quit the application") ); + connect( exitAction, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()) ); + + + cutAction = new QAction( QIcon(":/images/cut.png"), tr("Cu&t"), this ); + cutAction->setShortcut( tr("Ctrl+X") ); + cutAction->setStatusTip( tr("Cut") ); + cutAction->setEnabled(false); + connect( docWidget, SIGNAL(copyAvailable(bool)), cutAction, SLOT(setEnabled(bool)) ); + connect( cutAction, SIGNAL(triggered()), docWidget, SLOT(cut()) ); + + copyAction = new QAction( QIcon(":/images/copy.png"), tr("&Copy"), this ); + copyAction->setShortcut( tr("Ctrl+C") ); + copyAction->setStatusTip( tr("Copy") ); + copyAction->setEnabled(false); + connect( docWidget, SIGNAL(copyAvailable(bool)), copyAction, SLOT(setEnabled(bool)) ); + connect( copyAction, SIGNAL(triggered()), docWidget, SLOT(copy()) ); + + pasteAction = new QAction( QIcon(":/images/paste.png"), tr("&Paste"), this ); + pasteAction->setShortcut( tr("Ctrl+V") ); + pasteAction->setStatusTip( tr("Paste") ); + connect( pasteAction, SIGNAL(triggered()), docWidget, SLOT(paste()) ); + + + aboutAction = new QAction( tr("&About"), this ); + aboutAction->setStatusTip( tr("About this application") ); + connect( aboutAction, SIGNAL(triggered()), this, SLOT(helpAbout()) ); + + aboutQtAction = new QAction( tr("About &Qt"), this ); + aboutQtAction->setStatusTip( tr("About the Qt toolkit") ); + connect( aboutQtAction, SIGNAL(triggered()), qApp, SLOT(aboutQt()) ); +} + +void SdiWindow::createMenus() +{ + QMenu *menu; + + menu = menuBar()->addMenu( tr("&File") ); + menu->addAction( newAction ); + menu->addAction( closeAction ); + menu->addSeparator(); + menu->addAction( exitAction ); + + menu = menuBar()->addMenu( tr("&Edit") ); + menu->addAction( cutAction ); + menu->addAction( copyAction ); + menu->addAction( pasteAction ); + + menu = menuBar()->addMenu( tr("&Help") ); + menu->addAction( aboutAction ); + menu->addAction( aboutQtAction ); +} + +void SdiWindow::createToolbars() +{ + QToolBar *toolbar; + + toolbar = addToolBar( tr("File") ); + toolbar->addAction( newAction ); + + toolbar = addToolBar( tr("Edit") ); + toolbar->addAction( cutAction ); + toolbar->addAction( copyAction ); + toolbar->addAction( pasteAction ); +} + diff --git a/Chapter04/sdi/sdiwindow.h b/Chapter04/sdi/sdiwindow.h new file mode 100644 index 0000000..628b5f5 --- /dev/null +++ b/Chapter04/sdi/sdiwindow.h @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef SDIWINDOW_H +#define SDIWINDOW_H + +#include + +class QAction; +class QTextEdit; + +class SdiWindow : public QMainWindow +{ + Q_OBJECT + +public: + SdiWindow( QWidget *parent = 0 ); + +protected: + void closeEvent( QCloseEvent *event ); + +private slots: + void fileNew(); + void helpAbout(); + +private: + void createActions(); + void createMenus(); + void createToolbars(); + + bool isSafeToClose(); + + QTextEdit *docWidget; + + QAction *newAction; + QAction *closeAction; + QAction *exitAction; + + QAction *cutAction; + QAction *copyAction; + QAction *pasteAction; + + QAction *aboutAction; + QAction *aboutQtAction; +}; + +#endif // SDIWINDOW_H diff --git a/Chapter05/README.txt b/Chapter05/README.txt new file mode 100644 index 0000000..5f40b94 --- /dev/null +++ b/Chapter05/README.txt @@ -0,0 +1,74 @@ +This file is a part of 1590598318-1.zip containing example source code for the +Foundations of Qt Development book available from APress (ISBN 1590598318). + +These are the examples for chapter 5 - The Model-View Framework + +splitterview + + Listings 5-1, 5-2, 5-3, 5-4, 5-5 + + Shows how to use QStandardItem items in QTreeView, QTableView and QListView. + + +readonlyview + + Listings 5-6, 5-7 + + Shows how to set a QStandardItem to read-only and limits the possible + selections. + + +stringlist + + Listings 5-8 + + Shows how to use a QStringListModel. + + +bardelegate + + Listings 5-9, 5-10, 5-11, 5-12 + + Shows how to implement a custom delegate for showing values as a bar. + + +editdelegate + + Listings 5-13, 5-14, 5-15, 5-16 + + Shows how to extend the delegate to support editing as well as viewing. + + +singleitemview + + Listings 5-17, 5-18, 5-19, 5-20, 5-21, 5-22, 5-23 + + Shows how to implement a custom view. + + +mulmodel + + Listings 5-24, 5-25, 5-26, 5-27, 5-28, 5-29 + + Shows how to implement a custom, read-only table model. + + +treemodel + + Listings 5-30, 5-31, 5-32, 5-33, 5-34, 5-35, 5-36 + + Shows how to implement a custom, read-only tree model based around QObjects. + + +editmodel + + Listings 5-37, 5-38, 5-39, 5-40, 5-41, 5-42, 5-43 + + Shows how to implement a custom, editable list model. + + +sorting + + Listings 5-44, 5-45, 5-46 + + Shows how to use the QSortFilterProxyModel to perform custom sorting. diff --git a/Chapter05/bardelegate/bardelegate.cpp b/Chapter05/bardelegate/bardelegate.cpp new file mode 100644 index 0000000..eecca9b --- /dev/null +++ b/Chapter05/bardelegate/bardelegate.cpp @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include + +#include "bardelegate.h" + +BarDelegate::BarDelegate( QObject *parent ) : QAbstractItemDelegate( parent ) { } + +void BarDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const +{ + if( option.state & QStyle::State_Selected ) + painter->fillRect( option.rect, option.palette.highlight() ); + + int value = index.model()->data( index, Qt::DisplayRole ).toInt(); + double factor = double(value)/100.0; + + painter->save(); + + if( factor > 1 ) + { + painter->setBrush( Qt::red ); + factor = 1; + } + else + painter->setBrush( QColor( 0, int(factor*255), 255-int(factor*255) ) ); + + painter->setPen( Qt::black ); + painter->drawRect( option.rect.x()+2, option.rect.y()+2, int(factor*(option.rect.width()-5)), option.rect.height()-5 ); + painter->restore(); +} + +QSize BarDelegate::sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const +{ + return QSize( 45, 15 ); +} diff --git a/Chapter05/bardelegate/bardelegate.h b/Chapter05/bardelegate/bardelegate.h new file mode 100644 index 0000000..444c1d4 --- /dev/null +++ b/Chapter05/bardelegate/bardelegate.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef BARDELEGATE_H +#define BARDELEGATE_H + +#include + +class BarDelegate : public QAbstractItemDelegate +{ +public: + BarDelegate( QObject *parent = 0 ); + + void paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const; + QSize sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const; +}; + +#endif // BARDELEGATE_H diff --git a/Chapter05/bardelegate/bardelegate.pro b/Chapter05/bardelegate/bardelegate.pro new file mode 100644 index 0000000..4473438 --- /dev/null +++ b/Chapter05/bardelegate/bardelegate.pro @@ -0,0 +1,12 @@ +###################################################################### +# Automatically generated by qmake (2.01a) on 18. okt 20:30:04 2006 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +HEADERS += bardelegate.h +SOURCES += bardelegate.cpp main.cpp diff --git a/Chapter05/bardelegate/main.cpp b/Chapter05/bardelegate/main.cpp new file mode 100644 index 0000000..d98b479 --- /dev/null +++ b/Chapter05/bardelegate/main.cpp @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include +#include + +#include "bardelegate.h" + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + QTableView table; + + QStandardItemModel model( 10, 2 ); + for( int r=0; r<10; ++r ) + { + QStandardItem *item = new QStandardItem( QString("Row %1").arg(r+1) ); + item->setEditable( false ); + model.setItem( r, 0, item ); + + model.setItem( r, 1, new QStandardItem( QString::number((r*30)%100 )) ); + } + table.setModel( &model ); + + BarDelegate delegate; + table.setItemDelegateForColumn( 1, &delegate ); + table.show(); + + return app.exec(); +} diff --git a/Chapter05/editdelegate/bardelegate.cpp b/Chapter05/editdelegate/bardelegate.cpp new file mode 100644 index 0000000..a516d3a --- /dev/null +++ b/Chapter05/editdelegate/bardelegate.cpp @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include +#include + +#include "bardelegate.h" + +BarDelegate::BarDelegate( QObject *parent ) : QAbstractItemDelegate( parent ) { } + +void BarDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const +{ + if( option.state & QStyle::State_Selected ) + painter->fillRect( option.rect, option.palette.highlight() ); + + int value = index.model()->data( index, Qt::DisplayRole ).toInt(); + double factor = double(value)/100.0; + + painter->save(); + + if( factor > 1 ) + { + painter->setBrush( Qt::red ); + factor = 1; + } + else + painter->setBrush( QColor( 0, int(factor*255), 255-int(factor*255) ) ); + + painter->setPen( Qt::black ); + painter->drawRect( option.rect.x()+2, option.rect.y()+2, int(factor*(option.rect.width()-5)), option.rect.height()-5 ); + painter->restore(); +} + +QSize BarDelegate::sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const +{ + return QSize( 45, 15 ); +} + +QWidget *BarDelegate::createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const +{ + QSlider *slider = new QSlider( parent ); + + slider->setAutoFillBackground( true ); + slider->setOrientation( Qt::Horizontal ); + slider->setRange( 0, 100 ); + slider->installEventFilter( const_cast(this) ); + + return slider; +} + +void BarDelegate::updateEditorGeometry( QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index ) const +{ + editor->setGeometry( option.rect ); +} + +void BarDelegate::setEditorData( QWidget *editor, const QModelIndex &index ) const +{ + int value = index.model()->data( index, Qt::DisplayRole ).toInt(); + static_cast( editor )->setValue( value ); +} + +void BarDelegate::setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const +{ + model->setData( index, static_cast( editor )->value() ); +} + diff --git a/Chapter05/editdelegate/bardelegate.h b/Chapter05/editdelegate/bardelegate.h new file mode 100644 index 0000000..128cbda --- /dev/null +++ b/Chapter05/editdelegate/bardelegate.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef BARDELEGATE_H +#define BARDELEGATE_H + +#include + +class BarDelegate : public QAbstractItemDelegate +{ +public: + BarDelegate( QObject *parent = 0 ); + + void paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const; + QSize sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const; + + QWidget *createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const; + void setEditorData( QWidget *editor, const QModelIndex &index ) const; + void setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const; + void updateEditorGeometry( QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index ) const; +}; + +#endif // BARDELEGATE_H diff --git a/Chapter05/editdelegate/editdelegate.pro b/Chapter05/editdelegate/editdelegate.pro new file mode 100644 index 0000000..a39eceb --- /dev/null +++ b/Chapter05/editdelegate/editdelegate.pro @@ -0,0 +1,12 @@ +###################################################################### +# Automatically generated by qmake (2.01a) to 19. okt 15:49:22 2006 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +HEADERS += bardelegate.h +SOURCES += bardelegate.cpp main.cpp diff --git a/Chapter05/editdelegate/main.cpp b/Chapter05/editdelegate/main.cpp new file mode 100644 index 0000000..99ee462 --- /dev/null +++ b/Chapter05/editdelegate/main.cpp @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include +#include + +#include "bardelegate.h" + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + QTableView table; + + QStandardItemModel model( 10, 2 ); + for( int r=0; r<10; ++r ) + { + QStandardItem *item = new QStandardItem( QString("Row %1").arg(r+1) ); + item->setEditable( false ); + model.setItem( r, 0, item ); + + model.setItem( r, 1, new QStandardItem( QString::number((r*30)%100 )) ); + } + table.setModel( &model ); + + BarDelegate delegate; + table.setItemDelegateForColumn( 1, &delegate ); + + table.show(); + + return app.exec(); +} diff --git a/Chapter05/editmodel/editmodel.pro b/Chapter05/editmodel/editmodel.pro new file mode 100644 index 0000000..7fabdeb --- /dev/null +++ b/Chapter05/editmodel/editmodel.pro @@ -0,0 +1,12 @@ +###################################################################### +# Automatically generated by qmake (2.01a) sö 22. okt 16:45:01 2006 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +HEADERS += intmodel.h +SOURCES += intmodel.cpp main.cpp diff --git a/Chapter05/editmodel/intmodel.cpp b/Chapter05/editmodel/intmodel.cpp new file mode 100644 index 0000000..fbdae40 --- /dev/null +++ b/Chapter05/editmodel/intmodel.cpp @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "intmodel.h" + +IntModel::IntModel( int count, QObject *parent ) +{ + for( int i=0; i= m_values.count() ) + return false; + + if( value.toInt() == m_values.at( index.row() ) ) + return false; + + m_values[ index.row() ] = value.toInt(); + + emit dataChanged( index, index ); + return true; +} diff --git a/Chapter05/editmodel/intmodel.h b/Chapter05/editmodel/intmodel.h new file mode 100644 index 0000000..28289a4 --- /dev/null +++ b/Chapter05/editmodel/intmodel.h @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef INTMODEL_H +#define INTMODEL_H + +#include +#include + +class IntModel : public QAbstractListModel +{ +public: + IntModel( int count, QObject *parent = 0 ); + + Qt::ItemFlags flags( const QModelIndex &index ) const; + QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const; + int rowCount( const QModelIndex &parent = QModelIndex() ) const; + + bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ); + +private: + QList m_values; +}; + +#endif // INTMODEL_H diff --git a/Chapter05/editmodel/main.cpp b/Chapter05/editmodel/main.cpp new file mode 100644 index 0000000..5cd3613 --- /dev/null +++ b/Chapter05/editmodel/main.cpp @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include + +#include "intmodel.h" + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + IntModel model( 25 ); + + QListView list; + list.setModel( &model ); + list.show(); + + return app.exec(); +} diff --git a/Chapter05/mulmodel/main.cpp b/Chapter05/mulmodel/main.cpp new file mode 100644 index 0000000..13c0e74 --- /dev/null +++ b/Chapter05/mulmodel/main.cpp @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include + +#include "mulmodel.h" + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + MulModel model( 12, 12 ); + + QTableView table; + table.setModel( &model ); + + table.show(); + + return app.exec(); +} diff --git a/Chapter05/mulmodel/mulmodel.cpp b/Chapter05/mulmodel/mulmodel.cpp new file mode 100644 index 0000000..0568c37 --- /dev/null +++ b/Chapter05/mulmodel/mulmodel.cpp @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "mulmodel.h" + +MulModel::MulModel( int rows, int columns, QObject *parent ) : QAbstractTableModel( parent ) +{ + m_rows = rows; + m_columns = columns; +} + +int MulModel::rowCount( const QModelIndex &parent ) const +{ + return m_rows; +} + +int MulModel::columnCount( const QModelIndex &parent ) const +{ + return m_columns; +} + +Qt::ItemFlags MulModel::flags( const QModelIndex &index ) const +{ + return Qt::ItemIsSelectable | Qt::ItemIsEnabled; +} + +QVariant MulModel::data( const QModelIndex &index, int role ) const +{ + switch( role ) + { + case Qt::DisplayRole: + return (index.row()+1) * (index.column()+1); + + case Qt::ToolTipRole: + return QString( "%1 x %2" ).arg( index.row()+1 ).arg( index.column()+1 ); + + default: + return QVariant(); + } +} + +QVariant MulModel::headerData( int section, Qt::Orientation orientation, int role ) const +{ + if( role != Qt::DisplayRole ) + return QVariant(); + + return QString::number( section+1 ); +} diff --git a/Chapter05/mulmodel/mulmodel.h b/Chapter05/mulmodel/mulmodel.h new file mode 100644 index 0000000..b0450ba --- /dev/null +++ b/Chapter05/mulmodel/mulmodel.h @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef MULMODEL_H +#define MULMODEL_H + +#include + +class MulModel : public QAbstractTableModel +{ +public: + MulModel( int rows, int columns, QObject *parent = 0 ); + + Qt::ItemFlags flags( const QModelIndex &index ) const; + QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const; + QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const; + int rowCount( const QModelIndex &parent = QModelIndex() ) const; + int columnCount( const QModelIndex &parent = QModelIndex() ) const; + +private: + int m_rows, m_columns; +}; + +#endif // MULMODEL_H diff --git a/Chapter05/mulmodel/mulmodel.pro b/Chapter05/mulmodel/mulmodel.pro new file mode 100644 index 0000000..c230edc --- /dev/null +++ b/Chapter05/mulmodel/mulmodel.pro @@ -0,0 +1,12 @@ +###################################################################### +# Automatically generated by qmake (2.01a) lö 21. okt 18:14:24 2006 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +HEADERS += mulmodel.h +SOURCES += mulmodel.cpp main.cpp diff --git a/Chapter05/readonlyview/main.cpp b/Chapter05/readonlyview/main.cpp new file mode 100644 index 0000000..b357f47 --- /dev/null +++ b/Chapter05/readonlyview/main.cpp @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include + +#include +#include +#include + +#include + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + QTreeView *tree = new QTreeView(); + QListView *list = new QListView(); + QTableView *table = new QTableView(); + + QSplitter splitter; + splitter.addWidget( tree ); + splitter.addWidget( list ); + splitter.addWidget( table ); + + QStandardItemModel model( 5, 2 ); + for( int r=0; r<5; r++ ) + for( int c=0; c<2; c++) + { + QStandardItem *item = new QStandardItem( QString("Row:%0, Column:%1").arg(r).arg(c) ); + + if( c == 0 ) + for( int i=0; i<3; i++ ) + { + QStandardItem *child = new QStandardItem( QString("Item %0").arg(i) ); + child->setEditable( false ); + item->appendRow( child ); + } + + model.setItem(r, c, item); + } + + model.setHorizontalHeaderItem( 0, new QStandardItem( "Foo" ) ); + model.setHorizontalHeaderItem( 1, new QStandardItem( "Bar-Baz" ) ); + + tree->setModel( &model ); + list->setModel( &model ); + table->setModel( &model ); + + list->setSelectionModel( tree->selectionModel() ); + table->setSelectionModel( tree->selectionModel() ); + + table->setSelectionBehavior( QAbstractItemView::SelectRows ); + table->setSelectionMode( QAbstractItemView::SingleSelection ); + + splitter.show(); + + return app.exec(); +} diff --git a/Chapter05/readonlyview/readonlyview.pro b/Chapter05/readonlyview/readonlyview.pro new file mode 100644 index 0000000..e1f0242 --- /dev/null +++ b/Chapter05/readonlyview/readonlyview.pro @@ -0,0 +1,11 @@ +###################################################################### +# Automatically generated by qmake (2.01a) to 19. okt 13:22:05 2006 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp diff --git a/Chapter05/singleitemview/main.cpp b/Chapter05/singleitemview/main.cpp new file mode 100644 index 0000000..368d66e --- /dev/null +++ b/Chapter05/singleitemview/main.cpp @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include + +#include +#include + +#include "singleitemview.h" + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + QTableView *table = new QTableView(); + SingleItemView *selectionView = new SingleItemView(); + + QSplitter splitter; + splitter.addWidget( table ); + splitter.addWidget( selectionView ); + + QStandardItemModel model( 5, 2 ); + for( int r=0; r<5; r++ ) + for( int c=0; c<2; c++) + { + QStandardItem *item = new QStandardItem( QString("Row:%0, Column:%1").arg(r).arg(c) ); + model.setItem(r, c, item); + } + + table->setModel( &model ); + selectionView->setModel( &model ); + + selectionView->setSelectionModel( table->selectionModel() ); + + splitter.show(); + + return app.exec(); +} diff --git a/Chapter05/singleitemview/singleitemview.cpp b/Chapter05/singleitemview/singleitemview.cpp new file mode 100644 index 0000000..4d881cd --- /dev/null +++ b/Chapter05/singleitemview/singleitemview.cpp @@ -0,0 +1,142 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include +#include + +#include "singleitemview.h" + +SingleItemView::SingleItemView( QWidget *parent ) : QAbstractItemView( parent ) +{ + QGridLayout *layout = new QGridLayout( this->viewport() ); + label = new QLabel(); + + layout->addWidget( label, 0, 0 ); + + label->setAlignment( Qt::AlignCenter ); + label->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ) ); + label->setText( tr("No data.") ); +} + +QRect SingleItemView::visualRect( const QModelIndex &index ) const +{ + if( selectionModel()->selection().indexes().count() != 1 ) + return QRect(); + + if( currentIndex() != index ) + return QRect(); + + return rect(); +} + +QRegion SingleItemView::visualRegionForSelection( const QItemSelection &selection ) const +{ + if( selection.indexes().count() != 1 ) + return QRect(); + + if( currentIndex() != selection.indexes()[0] ) + return QRect(); + + return rect(); +} + +bool SingleItemView::isIndexHidden( const QModelIndex &index ) const +{ + if( selectionModel()->selection().indexes().count() != 1 ) + return true; + + if( currentIndex() != index ) + return true; + + return false; +} + +QModelIndex SingleItemView::indexAt( const QPoint &point ) const +{ + if( selectionModel()->selection().indexes().count() != 1 ) + return QModelIndex(); + + return currentIndex(); +} + +int SingleItemView::horizontalOffset() const +{ + return horizontalScrollBar()->value(); +} + +int SingleItemView::verticalOffset() const +{ + return verticalScrollBar()->value(); +} + +QModelIndex SingleItemView::moveCursor( CursorAction cursorAction, Qt::KeyboardModifiers modifiers ) +{ + return currentIndex(); +} + +void SingleItemView::setSelection( const QRect &rect, QItemSelectionModel::SelectionFlags flags ) +{ + // do nothing +} + +void SingleItemView::scrollTo( const QModelIndex &index, ScrollHint hint ) +{ + // cannot scroll +} + +void SingleItemView::dataChanged( const QModelIndex &topLeft, const QModelIndex &bottomRight ) +{ + updateText(); +} + +void SingleItemView::selectionChanged( const QItemSelection &selected, const QItemSelection &deselected ) +{ + updateText(); +} + +void SingleItemView::updateText() +{ + switch( selectionModel()->selection().indexes().count() ) + { + case 0: + label->setText( tr("No data.") ); + break; + + case 1: + label->setText( model()->data( currentIndex() ).toString() ); + break; + + default: + label->setText( tr("Too many items selected.
Can only show one item at a time.
") ); + break; + } +} diff --git a/Chapter05/singleitemview/singleitemview.h b/Chapter05/singleitemview/singleitemview.h new file mode 100644 index 0000000..7ffb37a --- /dev/null +++ b/Chapter05/singleitemview/singleitemview.h @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef SINGLEITEMVIEW_H +#define SINGLEITEMVIEW_H + +#include + +class QLabel; + +class SingleItemView : public QAbstractItemView +{ + Q_OBJECT + +public: + SingleItemView( QWidget *parent = 0 ); + + QModelIndex indexAt( const QPoint &point ) const; + void scrollTo( const QModelIndex &index, ScrollHint hint = EnsureVisible ); + QRect visualRect( const QModelIndex &index ) const; + +protected: + int horizontalOffset() const; + bool isIndexHidden( const QModelIndex &index ) const; + QModelIndex moveCursor( CursorAction cursorAction, Qt::KeyboardModifiers modifiers ); + void setSelection( const QRect &rect, QItemSelectionModel::SelectionFlags flags ); + int verticalOffset() const; + QRegion visualRegionForSelection( const QItemSelection &selection ) const; + +protected slots: + void dataChanged( const QModelIndex &topLeft, const QModelIndex &bottomRight ); + void selectionChanged( const QItemSelection &selected, const QItemSelection &deselected ); + +private: + void updateText(); + + QLabel *label; +}; + +#endif // SINGLEITEMVIEW_H diff --git a/Chapter05/singleitemview/singleitemview.pro b/Chapter05/singleitemview/singleitemview.pro new file mode 100644 index 0000000..eb8e352 --- /dev/null +++ b/Chapter05/singleitemview/singleitemview.pro @@ -0,0 +1,12 @@ +###################################################################### +# Automatically generated by qmake (2.01a) to 19. okt 09:18:10 2006 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +HEADERS += singleitemview.h +SOURCES += main.cpp singleitemview.cpp diff --git a/Chapter05/sorting/main.cpp b/Chapter05/sorting/main.cpp new file mode 100644 index 0000000..9ca9b22 --- /dev/null +++ b/Chapter05/sorting/main.cpp @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include + +#include +#include "sortonsecondmodel.h" + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + QStringListModel model; + QStringList list; + list << "Totte" << "Alfons" << "Laban" << "Bamse" << "Skalman"; + model.setStringList( list ); + + SortOnSecondModel sorter; + sorter.setSourceModel( &model ); + + QTableView table; + table.setModel( &sorter ); + table.setSortingEnabled( true ); + table.show(); + + return app.exec(); +} diff --git a/Chapter05/sorting/sorting.pro b/Chapter05/sorting/sorting.pro new file mode 100644 index 0000000..95499c5 --- /dev/null +++ b/Chapter05/sorting/sorting.pro @@ -0,0 +1,12 @@ +###################################################################### +# Automatically generated by qmake (2.01a) sö 22. okt 17:26:05 2006 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +HEADERS += sortonsecondmodel.h +SOURCES += main.cpp sortonsecondmodel.cpp diff --git a/Chapter05/sorting/sortonsecondmodel.cpp b/Chapter05/sorting/sortonsecondmodel.cpp new file mode 100644 index 0000000..0507737 --- /dev/null +++ b/Chapter05/sorting/sortonsecondmodel.cpp @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "sortonsecondmodel.h" + +SortOnSecondModel::SortOnSecondModel( QObject *parent ) : QSortFilterProxyModel( parent ) +{ +} + +bool SortOnSecondModel::lessThan( const QModelIndex &left, const QModelIndex &right ) const +{ + QString leftString = sourceModel()->data( left ).toString(); + QString rightString = sourceModel()->data( right ).toString(); + + if( leftString.length() > 1 ) + leftString = leftString.mid( 1 ); + else + leftString = ""; + + if( rightString.length() > 1 ) + rightString = rightString.mid( 1 ); + else + rightString = ""; + + return leftString < rightString; +} diff --git a/Chapter05/sorting/sortonsecondmodel.h b/Chapter05/sorting/sortonsecondmodel.h new file mode 100644 index 0000000..1fcb173 --- /dev/null +++ b/Chapter05/sorting/sortonsecondmodel.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef SORTONSECONDMODEL_H +#define SORTONSECONDMODEL_H + +#include + +class SortOnSecondModel : public QSortFilterProxyModel +{ +public: + SortOnSecondModel( QObject *parent = 0 ); + +protected: + bool lessThan( const QModelIndex &left, const QModelIndex &right ) const; +}; + +#endif // SORTONSECONDMODEL_H diff --git a/Chapter05/splitterview/main.cpp b/Chapter05/splitterview/main.cpp new file mode 100644 index 0000000..114e3d5 --- /dev/null +++ b/Chapter05/splitterview/main.cpp @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include + +#include +#include +#include + +#include + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + QTreeView *tree = new QTreeView(); + QListView *list = new QListView(); + QTableView *table = new QTableView(); + + QSplitter splitter; + splitter.addWidget( tree ); + splitter.addWidget( list ); + splitter.addWidget( table ); + + QStandardItemModel model( 5, 2 ); + for( int r=0; r<5; r++ ) + for( int c=0; c<2; c++) + { + QStandardItem *item = new QStandardItem( QString("Row:%0, Column:%1").arg(r).arg(c) ); + + if( c == 0 ) + for( int i=0; i<3; i++ ) + item->appendRow( new QStandardItem( QString("Item %0").arg(i) ) ); + + model.setItem(r, c, item); + } + + tree->setModel( &model ); + list->setModel( &model ); + table->setModel( &model ); + + list->setSelectionModel( tree->selectionModel() ); + table->setSelectionModel( tree->selectionModel() ); + + splitter.show(); + + return app.exec(); +} diff --git a/Chapter05/splitterview/splitterview.pro b/Chapter05/splitterview/splitterview.pro new file mode 100644 index 0000000..1628880 --- /dev/null +++ b/Chapter05/splitterview/splitterview.pro @@ -0,0 +1,11 @@ +###################################################################### +# Automatically generated by qmake (2.01a) må 16. okt 15:40:21 2006 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp diff --git a/Chapter05/stringlist/main.cpp b/Chapter05/stringlist/main.cpp new file mode 100644 index 0000000..3cdd9ce --- /dev/null +++ b/Chapter05/stringlist/main.cpp @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include +#include + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + QListView list; + QStringListModel model; + QStringList strings; + + strings << "foo" << "bar" << "baz"; + + model.setStringList( strings ); + list.setModel( &model ); + + list.show(); + + return app.exec(); +} \ No newline at end of file diff --git a/Chapter05/stringlist/stringlist.pro b/Chapter05/stringlist/stringlist.pro new file mode 100644 index 0000000..1fd89f2 --- /dev/null +++ b/Chapter05/stringlist/stringlist.pro @@ -0,0 +1,11 @@ +###################################################################### +# Automatically generated by qmake (2.01a) on 18. okt 19:40:38 2006 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp diff --git a/Chapter05/treemodel/main.cpp b/Chapter05/treemodel/main.cpp new file mode 100644 index 0000000..dc3a492 --- /dev/null +++ b/Chapter05/treemodel/main.cpp @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include + +#include "objecttreemodel.h" + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + QObject root; + root.setObjectName( "root" ); + + QObject *child; + QObject *foo = new QObject( &root ); + foo->setObjectName( "foo" ); + child = new QObject( foo ); + child->setObjectName( "Mark" ); + child = new QObject( foo ); + child->setObjectName( "Bob" ); + child = new QObject( foo ); + child->setObjectName( "Kent" ); + + QObject *bar = new QObject( &root ); + bar->setObjectName( "bar" ); +//+. main + child = new QObject( bar ); + child->setObjectName( "Ole" ); + child = new QObject( bar ); + child->setObjectName( "Trond" ); + child = new QObject( bar ); + child->setObjectName( "Kjetil" ); + child = new QObject( bar ); + child->setObjectName( "Lasse" ); + + QObject *baz = new QObject( &root ); + baz->setObjectName( "baz" ); + child = new QObject( baz ); + child->setObjectName( "Bengt" ); + child = new QObject( baz ); + child->setObjectName( "Sven" ); +//-. main + + ObjectTreeModel model( &root ); + + QTreeView tree; + tree.setModel( &model ); + + tree.show(); + + return app.exec(); +} diff --git a/Chapter05/treemodel/objecttreemodel.cpp b/Chapter05/treemodel/objecttreemodel.cpp new file mode 100644 index 0000000..8083697 --- /dev/null +++ b/Chapter05/treemodel/objecttreemodel.cpp @@ -0,0 +1,147 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include "objecttreemodel.h" + +ObjectTreeModel::ObjectTreeModel( QObject *root, QObject *parent ) : QAbstractItemModel( parent ) +{ + m_root = root; +} + +Qt::ItemFlags ObjectTreeModel::flags(const QModelIndex &index) const +{ + return Qt::ItemIsEnabled | Qt::ItemIsSelectable; +} + +QVariant ObjectTreeModel::data( const QModelIndex &index, int role) const +{ + if( !index.isValid() ) + return QVariant(); + + if( role == Qt::DisplayRole ) + { + switch( index.column() ) + { + case 0: + return static_cast( index.internalPointer() )->objectName(); + + case 1: + return static_cast( index.internalPointer() )->metaObject()->className(); + + default: + break; + } + } + else if( role == Qt::ToolTipRole ) + { + switch( index.column() ) + { + case 0: + return QString( "The name of the object." ); + + case 1: + return QString( "The name of the class." ); + + default: + break; + } + } + + return QVariant(); +} + +QVariant ObjectTreeModel::headerData(int section, Qt::Orientation orientation, int role ) const +{ + if( role != Qt::DisplayRole || orientation != Qt::Horizontal ) + return QVariant(); + + switch( section ) + { + case 0: + return QString( "Object" ); + + case 1: + return QString( "Class" ); + + default: + return QVariant(); + } +} + +int ObjectTreeModel::rowCount(const QModelIndex &parent ) const +{ + QObject *parentObject; + + if( !parent.isValid() ) + parentObject = m_root; + else + parentObject = static_cast( parent.internalPointer() ); + + return parentObject->children().count(); +} + +int ObjectTreeModel::columnCount(const QModelIndex &parent ) const +{ + return 2; +} + +QModelIndex ObjectTreeModel::index(int row, int column, const QModelIndex &parent ) const +{ + QObject *parentObject; + + if( !parent.isValid() ) + parentObject = m_root; + else + parentObject = static_cast( parent.internalPointer() ); + + if( row < parentObject->children().count() ) + return createIndex( row, column, parentObject->children().at( row ) ); + else + return QModelIndex(); +} + +QModelIndex ObjectTreeModel::parent(const QModelIndex &index) const +{ + if( !index.isValid() ) + return QModelIndex(); + + QObject *indexObject = static_cast( index.internalPointer() ); + QObject *parentObject = indexObject->parent(); + + if( parentObject == m_root ) + return QModelIndex(); + + QObject *grandParentObject = parentObject->parent(); + + return createIndex( grandParentObject->children().indexOf( parentObject ), 0, parentObject ); +} diff --git a/Chapter05/treemodel/objecttreemodel.h b/Chapter05/treemodel/objecttreemodel.h new file mode 100644 index 0000000..a4c95ab --- /dev/null +++ b/Chapter05/treemodel/objecttreemodel.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef OBJECTTREEMODEL_H +#define OBJECTTREEMODEL_H + +#include + +class ObjectTreeModel : public QAbstractItemModel +{ +public: + ObjectTreeModel( QObject *root, QObject *parent = 0 ); + + Qt::ItemFlags flags( const QModelIndex &index ) const; + QVariant data( const QModelIndex &index, int role ) const; + QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const; + int rowCount( const QModelIndex &parent = QModelIndex() ) const; + int columnCount( const QModelIndex &parent = QModelIndex() ) const; + + QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const; + QModelIndex parent( const QModelIndex &index ) const; + +private: + QObject *m_root; +}; + +#endif // OBJECTTREEMODEL_H diff --git a/Chapter05/treemodel/treemodel.pro b/Chapter05/treemodel/treemodel.pro new file mode 100644 index 0000000..64672fe --- /dev/null +++ b/Chapter05/treemodel/treemodel.pro @@ -0,0 +1,12 @@ +###################################################################### +# Automatically generated by qmake (2.01a) sö 22. okt 15:48:57 2006 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +HEADERS += objecttreemodel.h +SOURCES += main.cpp objecttreemodel.cpp diff --git a/Chapter06/README.txt b/Chapter06/README.txt new file mode 100644 index 0000000..56365e6 --- /dev/null +++ b/Chapter06/README.txt @@ -0,0 +1,58 @@ +This file is a part of 1590598318-1.zip containing example source code for the +Foundations of Qt Development book available from APress (ISBN 1590598318). + +These are the examples for chapter 6 - Creating Widgets + +composed + + Listings 6-1, 6-2, 6-3, 6-4, 6-5, 6-6 + + Shows how to compose QPushButton widgets into a custom widget providing a + programming interface more adapted to the actual application. + + +clocklabel + + Listings 6-7, 6-8 + + Shows how to inherit an existing widget, a QLabel, and use it to provide new + functionality, i.e. showing the current time. + + +eventlister + + Listings 6-9, 6-10, 6-11, 6-12, 6-13, 6-14, 6-15 + + Implements an event filter that writes out all event information to a log. + You can use this to find the events that you are interested in and to learn + about the parameters passed with each event. + + +eventfilter + + Listings 6-16, 6-17 + + Shows a basic filter, stopping numeric key presses. + + +circlebar + + Listings 6-18, 6-19, 6-20, 6-21, 6-22, 6-23, 6-24 + + Shows custom widget implemented from scratch. It implements a circle + representing a value and allows the user to change the value using the mouse + wheel. + + +designerpromotion + + Not shown in any Listings, but Figure 6-7. + + A Designer file showing a promoted widget. + +designerplugin + + Listings 6-25, 6-26, 6-27, 6-28, 6-29, 6-30, 6-31, 6-32, 6-33 + + Shows how to implement a plug-in that makes the CircleBar widget available + from within Designer. diff --git a/Chapter06/circlebar/circlebar.cpp b/Chapter06/circlebar/circlebar.cpp new file mode 100644 index 0000000..7d993cf --- /dev/null +++ b/Chapter06/circlebar/circlebar.cpp @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include + +#include + +#include "circlebar.h" + +CircleBar::CircleBar( int value, QWidget *parent ) : QWidget( parent ) +{ + m_value = value; + + QSizePolicy policy( QSizePolicy::Preferred, QSizePolicy::Preferred ); + policy.setHeightForWidth( true ); + setSizePolicy( policy ); +} + +int CircleBar::heightForWidth( int width ) const +{ + return width; +} + +QSize CircleBar::sizeHint() const +{ + return QSize( 100, 100 ); +} + +int CircleBar::value() const +{ + return m_value; +} + +void CircleBar::setValue( int value ) +{ + if( value < 0 ) + value = 0; + + if( value > 100 ) + value = 100; + + if( m_value == value ) + return; + + m_value = value; + + update(); + + emit valueChanged( m_value ); +} + +void CircleBar::paintEvent( QPaintEvent *event ) +{ + int radius = width()/2; + double factor = m_value/100.0; + + QPainter p( this ); + p.setPen( Qt::black ); + p.drawEllipse( 0, 0, width()-1, width()-1 ); + p.setBrush( Qt::black ); + p.drawEllipse( int(radius*(1.0-factor)), int(radius*(1.0-factor)), int((width()-1)*factor)+1, int((width()-1)*factor)+1 ); +} + +void CircleBar::wheelEvent( QWheelEvent *event ) +{ + event->accept(); + setValue( value() + event->delta()/20 ); +} diff --git a/Chapter06/circlebar/circlebar.h b/Chapter06/circlebar/circlebar.h new file mode 100644 index 0000000..7b84844 --- /dev/null +++ b/Chapter06/circlebar/circlebar.h @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef CIRCLEBAR_H +#define CIRCLEBAR_H + +#include + +class CircleBar : public QWidget +{ + Q_OBJECT + +public: + CircleBar( int value = 0, QWidget *parent = 0 ); + + int value() const; + + int heightForWidth( int ) const; + QSize sizeHint() const; +public slots: + void setValue( int ); + +signals: + void valueChanged( int ); + +protected: + void paintEvent( QPaintEvent* ); + void wheelEvent( QWheelEvent* ); + +private: + int m_value; +}; + +#endif // CIRCLEBAR_H diff --git a/Chapter06/circlebar/circlebar.pro b/Chapter06/circlebar/circlebar.pro new file mode 100644 index 0000000..fcfa92c --- /dev/null +++ b/Chapter06/circlebar/circlebar.pro @@ -0,0 +1,13 @@ +###################################################################### +# Automatically generated by qmake (2.01a) må 30. okt 07:22:22 2006 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +HEADERS += circlebar.h +SOURCES += circlebar.cpp main.cpp +CONFIG += console diff --git a/Chapter06/circlebar/main.cpp b/Chapter06/circlebar/main.cpp new file mode 100644 index 0000000..91a73d6 --- /dev/null +++ b/Chapter06/circlebar/main.cpp @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +#include + +#include +#include + +#include "circlebar.h" + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + QWidget base; + QVBoxLayout layout( &base ); + + CircleBar bar; + QSlider slider( Qt::Horizontal ); + + layout.addWidget( &bar ); + layout.addWidget( &slider ); + + QObject::connect( &slider, SIGNAL(valueChanged(int)), &bar, SLOT(setValue(int)) ); + QObject::connect( &bar, SIGNAL(valueChanged(int)), &slider, SLOT(setValue(int)) ); + + base.show(); + + return app.exec(); +} diff --git a/Chapter06/clocklabel/clocklabel.cpp b/Chapter06/clocklabel/clocklabel.cpp new file mode 100644 index 0000000..e0af1a0 --- /dev/null +++ b/Chapter06/clocklabel/clocklabel.cpp @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include + +#include "clocklabel.h" + +ClockLabel::ClockLabel( QWidget *parent ) : QLabel( parent ) +{ + QTimer *timer = new QTimer( this ); + timer->setInterval( 1000 ); + connect( timer, SIGNAL(timeout()), this, SLOT(updateTime()) ); + timer->start(); + + updateTime(); +} + +void ClockLabel::updateTime() +{ + setText( QTime::currentTime().toString( "hh:mm" ) ); +} diff --git a/Chapter06/clocklabel/clocklabel.h b/Chapter06/clocklabel/clocklabel.h new file mode 100644 index 0000000..81800a9 --- /dev/null +++ b/Chapter06/clocklabel/clocklabel.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef CLOCKLABEL_H +#define CLOCKLABEL_H + +#include + +class ClockLabel : public QLabel +{ + Q_OBJECT +public: + ClockLabel( QWidget *parent = 0 ); + +private slots: + void updateTime(); +}; + +#endif // CLOCKLABEL_H diff --git a/Chapter06/clocklabel/clocklabel.pro b/Chapter06/clocklabel/clocklabel.pro new file mode 100644 index 0000000..a75c161 --- /dev/null +++ b/Chapter06/clocklabel/clocklabel.pro @@ -0,0 +1,13 @@ +###################################################################### +# Automatically generated by qmake (2.01a) lö 28. okt 16:45:10 2006 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +HEADERS += clocklabel.h +SOURCES += clocklabel.cpp main.cpp +CONFIG += console diff --git a/Chapter06/clocklabel/main.cpp b/Chapter06/clocklabel/main.cpp new file mode 100644 index 0000000..07ae297 --- /dev/null +++ b/Chapter06/clocklabel/main.cpp @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include "clocklabel.h" + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + ClockLabel clock; + clock.show(); + + return app.exec(); +} \ No newline at end of file diff --git a/Chapter06/composed/composed.pro b/Chapter06/composed/composed.pro new file mode 100644 index 0000000..24ac6b8 --- /dev/null +++ b/Chapter06/composed/composed.pro @@ -0,0 +1,12 @@ +###################################################################### +# Automatically generated by qmake (2.01a) on 25. okt 19:15:29 2006 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +HEADERS += numerickeypad.h +SOURCES += main.cpp numerickeypad.cpp diff --git a/Chapter06/composed/main.cpp b/Chapter06/composed/main.cpp new file mode 100644 index 0000000..0d4ad30 --- /dev/null +++ b/Chapter06/composed/main.cpp @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include "numerickeypad.h" + +#include +#include +#include + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + QWidget widget; + QVBoxLayout layout( &widget ); + + NumericKeypad pad; + layout.addWidget( &pad ); + + QLabel label; + layout.addWidget( &label ); + + QObject::connect( &pad, SIGNAL(textChanged(QString)), &label, SLOT(setText(QString)) ); + + widget.show(); + + return app.exec(); +} diff --git a/Chapter06/composed/numerickeypad.cpp b/Chapter06/composed/numerickeypad.cpp new file mode 100644 index 0000000..111b39e --- /dev/null +++ b/Chapter06/composed/numerickeypad.cpp @@ -0,0 +1,126 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include +#include +#include + +#include "numerickeypad.h" + +NumericKeypad::NumericKeypad( QWidget *parent ) +{ + QGridLayout *layout = new QGridLayout( this ); + + m_lineEdit = new QLineEdit(); + m_lineEdit->setAlignment( Qt::AlignRight ); + + QPushButton *button0 = new QPushButton( tr("0") ); + QPushButton *button1 = new QPushButton( tr("1") ); + QPushButton *button2 = new QPushButton( tr("2") ); + QPushButton *button3 = new QPushButton( tr("3") ); + QPushButton *button4 = new QPushButton( tr("4") ); + QPushButton *button5 = new QPushButton( tr("5") ); + QPushButton *button6 = new QPushButton( tr("6") ); + QPushButton *button7 = new QPushButton( tr("7") ); + QPushButton *button8 = new QPushButton( tr("8") ); + QPushButton *button9 = new QPushButton( tr("9") ); + QPushButton *buttonDot = new QPushButton( tr(".") ); + QPushButton *buttonClear = new QPushButton( tr("C") ); + + layout->addWidget( m_lineEdit, 0, 0, 1, 3 ); + + layout->addWidget( button1, 1, 0 ); + layout->addWidget( button2, 1, 1 ); + layout->addWidget( button3, 1, 2 ); + layout->addWidget( button4, 2, 0 ); + layout->addWidget( button5, 2, 1 ); + layout->addWidget( button6, 2, 2 ); + layout->addWidget( button7, 3, 0 ); + layout->addWidget( button8, 3, 1 ); + layout->addWidget( button9, 3, 2 ); + layout->addWidget( button0, 4, 0 ); + layout->addWidget( buttonDot, 4, 1 ); + layout->addWidget( buttonClear, 4, 2 ); + + QSignalMapper *mapper = new QSignalMapper( this ); + + mapper->setMapping( button0, "0" ); + mapper->setMapping( button1, "1" ); + mapper->setMapping( button2, "2" ); + mapper->setMapping( button3, "3" ); + mapper->setMapping( button4, "4" ); + mapper->setMapping( button5, "5" ); + mapper->setMapping( button6, "6" ); + mapper->setMapping( button7, "7" ); + mapper->setMapping( button8, "8" ); + mapper->setMapping( button9, "9" ); + mapper->setMapping( buttonDot, "." ); + + connect( button0, SIGNAL(clicked()), mapper, SLOT(map()) ); + connect( button1, SIGNAL(clicked()), mapper, SLOT(map()) ); + connect( button2, SIGNAL(clicked()), mapper, SLOT(map()) ); + connect( button3, SIGNAL(clicked()), mapper, SLOT(map()) ); + connect( button4, SIGNAL(clicked()), mapper, SLOT(map()) ); + connect( button5, SIGNAL(clicked()), mapper, SLOT(map()) ); + connect( button6, SIGNAL(clicked()), mapper, SLOT(map()) ); + connect( button7, SIGNAL(clicked()), mapper, SLOT(map()) ); + connect( button8, SIGNAL(clicked()), mapper, SLOT(map()) ); + connect( button9, SIGNAL(clicked()), mapper, SLOT(map()) ); + connect( buttonDot, SIGNAL(clicked()), mapper, SLOT(map()) ); + + connect( mapper, SIGNAL(mapped(QString)), this, SLOT(buttonClicked(QString)) ); + + connect( buttonClear, SIGNAL(clicked()), m_lineEdit, SLOT(clear()) ); + connect( m_lineEdit, SIGNAL(textChanged(QString)), this, SLOT(setText(QString)) ); +} + +const QString NumericKeypad::text() const +{ + return m_text; +} + +void NumericKeypad::buttonClicked( const QString &newText ) +{ + setText( m_text + newText ); +} + +void NumericKeypad::setText( const QString &newText ) +{ + if( newText == m_text ) + return; + + m_text = newText; + m_lineEdit->setText( m_text ); + + emit textChanged( m_text ); +} + diff --git a/Chapter06/composed/numerickeypad.h b/Chapter06/composed/numerickeypad.h new file mode 100644 index 0000000..6577631 --- /dev/null +++ b/Chapter06/composed/numerickeypad.h @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef NUMERICKEYPAD_H +#define NUMERICKEYPAD_H + +#include +class QLineEdit; + +class NumericKeypad : public QWidget +{ + Q_OBJECT + +public: + NumericKeypad( QWidget *parent = 0 ); + + const QString text() const; + +public slots: + void setText( const QString &text ); + +signals: + void textChanged( const QString &text ); + +private slots: + void buttonClicked( const QString &text ); + +private: + QLineEdit *m_lineEdit; + QString m_text; +}; + +#endif // NUMERICKEYPAD_H diff --git a/Chapter06/designerplugin/circlebar.cpp b/Chapter06/designerplugin/circlebar.cpp new file mode 100644 index 0000000..e57d862 --- /dev/null +++ b/Chapter06/designerplugin/circlebar.cpp @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include + +#include + +#include "circlebar.h" + +CircleBar::CircleBar( QWidget *parent ) : QWidget( parent ) +{ + m_value = 0; + + QSizePolicy policy( QSizePolicy::Preferred, QSizePolicy::Preferred ); + policy.setHeightForWidth( true ); + setSizePolicy( policy ); +} + +CircleBar::CircleBar( int value, QWidget *parent ) : QWidget( parent ) +{ + m_value = value; + + QSizePolicy policy( QSizePolicy::Preferred, QSizePolicy::Preferred ); + policy.setHeightForWidth( true ); + setSizePolicy( policy ); +} + +int CircleBar::heightForWidth( int width ) const +{ + return width; +} + +QSize CircleBar::sizeHint() const +{ + return QSize( 100, 100 ); +} + +int CircleBar::value() const +{ + return m_value; +} + +void CircleBar::setValue( int value ) +{ + if( value < 0 ) + value = 0; + + if( value > 100 ) + value = 100; + + if( m_value == value ) + return; + + m_value = value; + + update(); + + emit valueChanged( m_value ); +} + +void CircleBar::paintEvent( QPaintEvent *event ) +{ + int radius = width()/2; + double factor = m_value/100.0; + + QPainter p( this ); + p.setPen( Qt::black ); + p.drawEllipse( 0, 0, width()-1, width()-1 ); + p.setBrush( Qt::black ); + p.drawEllipse( int(radius*(1.0-factor)), int(radius*(1.0-factor)), int((width()-1)*factor)+1, int((width()-1)*factor)+1 ); +} + +void CircleBar::wheelEvent( QWheelEvent *event ) +{ + event->accept(); + setValue( value() + event->delta()/20 ); +} diff --git a/Chapter06/designerplugin/circlebar.h b/Chapter06/designerplugin/circlebar.h new file mode 100644 index 0000000..cb20341 --- /dev/null +++ b/Chapter06/designerplugin/circlebar.h @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef CIRCLEBAR_H +#define CIRCLEBAR_H + +#include +#include + +class QDESIGNER_WIDGET_EXPORT CircleBar : public QWidget +{ + Q_OBJECT + +public: + CircleBar( QWidget *parent = 0 ); + CircleBar( int value = 0, QWidget *parent = 0 ); + + int value() const; + + int heightForWidth( int ) const; + QSize sizeHint() const; +public slots: + void setValue( int ); + +signals: + void valueChanged( int ); + +protected: + void paintEvent( QPaintEvent* ); + void wheelEvent( QWheelEvent* ); + +private: + int m_value; +}; + +#endif // CIRCLEBAR_H diff --git a/Chapter06/designerplugin/circlebarplugin.cpp b/Chapter06/designerplugin/circlebarplugin.cpp new file mode 100644 index 0000000..91bda73 --- /dev/null +++ b/Chapter06/designerplugin/circlebarplugin.cpp @@ -0,0 +1,109 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include +#include +#include + +#include "circlebar.h" +#include "circlebarplugin.h" + +CircleBarPlugin::CircleBarPlugin( QObject *parent ) +{ + m_initialized = false; +} + +bool CircleBarPlugin::isInitialized() const +{ + return m_initialized; +} + +void CircleBarPlugin::initialize( QDesignerFormEditorInterface *core ) +{ + if( m_initialized ) + return; + + m_initialized = true; +} + +bool CircleBarPlugin::isContainer() const +{ + return false; +} + +QIcon CircleBarPlugin::icon() const +{ + return QIcon(); +} + +QString CircleBarPlugin::toolTip() const +{ + return ""; +} + +QString CircleBarPlugin::whatsThis() const +{ + return ""; +} + +QString CircleBarPlugin::codeTemplate() const +{ + return ""; +} + +QString CircleBarPlugin::includeFile() const +{ + return "circlebar.h"; +} + +QString CircleBarPlugin::name() const +{ + return "CircleBar"; +} + +QString CircleBarPlugin::domXml() const +{ + return "\n" + "\n"; +} + +QString CircleBarPlugin::group() const +{ + return "Book Widgets"; +} + +QWidget *CircleBarPlugin::createWidget( QWidget *parent ) +{ + return new CircleBar( parent ); +} + +Q_EXPORT_PLUGIN2( circleBarPlugin, CircleBarPlugin ) diff --git a/Chapter06/designerplugin/circlebarplugin.h b/Chapter06/designerplugin/circlebarplugin.h new file mode 100644 index 0000000..bf11795 --- /dev/null +++ b/Chapter06/designerplugin/circlebarplugin.h @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef CIRCLEBARPLUGIN_H +#define CIRCLEBARPLUGIN_H + +#include + +class QExtensionManager; + +class CircleBarPlugin : public QObject, public QDesignerCustomWidgetInterface +{ + Q_OBJECT + Q_INTERFACES(QDesignerCustomWidgetInterface) + +public: + CircleBarPlugin( QObject *parent = 0 ); + + bool isContainer() const; + bool isInitialized() const; + QIcon icon() const; + QString codeTemplate() const; + QString domXml() const; + QString group() const; + QString includeFile() const; + QString name() const; + QString toolTip() const; + QString whatsThis() const; + QWidget *createWidget( QWidget *parent ); + void initialize( QDesignerFormEditorInterface *core ); + +private: + bool m_initialized; +}; + +#endif /* CIRCLEBARPLUGIN_H */ diff --git a/Chapter06/designerplugin/plugin.pro b/Chapter06/designerplugin/plugin.pro new file mode 100644 index 0000000..80e1b64 --- /dev/null +++ b/Chapter06/designerplugin/plugin.pro @@ -0,0 +1,11 @@ +TEMPLATE = lib +CONFIG += designer plugin release + +DEPENDPATH += . + +TARGET = circlebarplugin + +HEADERS += circlebar.h circlebarplugin.h +SOURCES += circlebar.cpp circlebarplugin.cpp + +DESTDIR = $$[QT_INSTALL_DATA]/plugins/designer diff --git a/Chapter06/designerpromotion/promote-dialog.ui b/Chapter06/designerpromotion/promote-dialog.ui new file mode 100644 index 0000000..ad4664c --- /dev/null +++ b/Chapter06/designerpromotion/promote-dialog.ui @@ -0,0 +1,116 @@ + + + Copyright (c) 2006-2007, Johan Thelin + + All rights reserved. + + Redistribution and use in source and binary forms, with or without modification, + are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + * Neither the name of APress nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + Dialog + + + + 0 + 0 + 400 + 300 + + + + Dialog + + + + + 30 + 240 + 341 + 32 + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::NoButton|QDialogButtonBox::Ok + + + + + + 150 + 60 + 53 + 17 + + + + TextLabel + + + + + + CustomWidget + QLabel +
customwidget.h
+
+
+ + + + buttonBox + accepted() + Dialog + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + Dialog + reject() + + + 316 + 260 + + + 286 + 274 + + + + +
diff --git a/Chapter06/eventfilter/eventfilter.pro b/Chapter06/eventfilter/eventfilter.pro new file mode 100644 index 0000000..ac900f2 --- /dev/null +++ b/Chapter06/eventfilter/eventfilter.pro @@ -0,0 +1,11 @@ +###################################################################### +# Automatically generated by qmake (2.01a) lö 28. okt 16:32:43 2006 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp diff --git a/Chapter06/eventfilter/main.cpp b/Chapter06/eventfilter/main.cpp new file mode 100644 index 0000000..5d166e4 --- /dev/null +++ b/Chapter06/eventfilter/main.cpp @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include +#include + +class KeyboardFilter : public QObject +{ +public: + KeyboardFilter( QObject *parent = 0 ) : QObject( parent ) {} + +protected: + bool eventFilter( QObject *dist, QEvent *event ) + { + if( event->type() == QEvent::KeyPress ) + { + QKeyEvent *keyEvent = static_cast( event ); + if( QString("1234567890").indexOf( keyEvent->text() ) != -1 ) + return true; + } + + return false; + } +}; + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + QLineEdit lineEdit; + KeyboardFilter filter; + + lineEdit.installEventFilter( &filter ); + lineEdit.show(); + + return app.exec(); +} diff --git a/Chapter06/eventlister/eventlister.pro b/Chapter06/eventlister/eventlister.pro new file mode 100644 index 0000000..4e586b7 --- /dev/null +++ b/Chapter06/eventlister/eventlister.pro @@ -0,0 +1,12 @@ +###################################################################### +# Automatically generated by qmake (2.01a) to 26. okt 19:29:55 2006 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +HEADERS += eventwidget.h +SOURCES += eventwidget.cpp main.cpp diff --git a/Chapter06/eventlister/eventwidget.cpp b/Chapter06/eventlister/eventwidget.cpp new file mode 100644 index 0000000..ac0f12c --- /dev/null +++ b/Chapter06/eventlister/eventwidget.cpp @@ -0,0 +1,216 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "eventwidget.h" + +EventWidget::EventWidget( QWidget *parent ) : QWidget( parent ) +{ +} + +void EventWidget::closeEvent( QCloseEvent * event ) +{ + emit gotEvent( tr("closeEvent") ); +} + +void EventWidget::enterEvent( QEvent * event ) +{ + emit gotEvent( tr("enterEvent") ); +} + +void EventWidget::leaveEvent( QEvent * event ) +{ + emit gotEvent( tr("leaveEvent") ); +} + +void EventWidget::hideEvent( QHideEvent * event ) +{ + emit gotEvent( tr("hideEvent") ); +} + +void EventWidget::showEvent( QShowEvent * event ) +{ + emit gotEvent( tr("showEvent") ); +} + +void EventWidget::paintEvent( QPaintEvent * event ) +{ + emit gotEvent( tr("paintEvent") ); +} + +void EventWidget::contextMenuEvent( QContextMenuEvent * event ) +{ + emit gotEvent( tr("contextMenuEvent( x:%1, y:%2, reason:%3 )") + .arg(event->x()) + .arg(event->y()) + .arg(event->reason()==2?"Other": + event->reason()==1?"Keyboard": + "Mouse") ); +} + +void EventWidget::focusInEvent( QFocusEvent * event ) +{ + emit gotEvent( tr("focusInEvent( reason:%1 )") + .arg( event->reason()==0?"MouseFocusReason": + event->reason()==1?"TabFocusReason": + event->reason()==2?"BacktabFocusReason": + event->reason()==3?"ActiveWindowFocusReason": + event->reason()==4?"PopupFocusReason": + event->reason()==5?"ShortcutFocusReason": + event->reason()==6?"MenuBarFocusReason": + "OtherFocusReason" ) ); +} + +void EventWidget::focusOutEvent( QFocusEvent * event ) +{ + emit gotEvent( tr("focusOutEvent( reason:%1 )") + .arg( event->reason()==0?"MouseFocusReason": + event->reason()==1?"TabFocusReason": + event->reason()==2?"BacktabFocusReason": + event->reason()==3?"ActiveWindowFocusReason": + event->reason()==4?"PopupFocusReason": + event->reason()==5?"ShortcutFocusReason": + event->reason()==6?"MenuBarFocusReason": + "OtherFocusReason" ) ); +} + +void EventWidget::keyPressEvent( QKeyEvent * event ) +{ + emit gotEvent( tr("keyPressEvent( text:%1, modifiers:%2 )") + .arg( event->text() ) + .arg( event->modifiers()==0?tr("NoModifier"):( + (event->modifiers()&Qt::ShiftModifier ==0?tr(""): + tr("ShiftModifier "))+ + (event->modifiers()&Qt::ControlModifier ==0?tr(""): + tr("ControlModifier "))+ + (event->modifiers()&Qt::AltModifier ==0?tr(""): + tr("AltModifier "))+ + (event->modifiers()&Qt::MetaModifier ==0?tr(""): + tr("MetaModifier "))+ + (event->modifiers()&Qt::KeypadModifier ==0?tr(""): + tr("KeypadModifier "))+ + (event->modifiers()&Qt::GroupSwitchModifier==0?tr(""): + tr("GroupSwitchModifier")) ) ) ); +} + +void EventWidget::keyReleaseEvent( QKeyEvent * event ) +{ + emit gotEvent( tr("keyReleaseEvent( text:%1, modifiers:%2 )") + .arg( event->text() ) + .arg( event->modifiers()==0?tr("NoModifier"):( + (event->modifiers()&Qt::ShiftModifier ==0?tr(""): + tr("ShiftModifier "))+ + (event->modifiers()&Qt::ControlModifier ==0?tr(""): + tr("ControlModifier "))+ + (event->modifiers()&Qt::AltModifier ==0?tr(""): + tr("AltModifier "))+ + (event->modifiers()&Qt::MetaModifier ==0?tr(""): + tr("MetaModifier "))+ + (event->modifiers()&Qt::KeypadModifier ==0?tr(""): + tr("KeypadModifier "))+ + (event->modifiers()&Qt::GroupSwitchModifier==0?tr(""): + tr("GroupSwitchModifier")) ) ) ); +} + +void EventWidget::mouseDoubleClickEvent( QMouseEvent * event ) +{ + emit gotEvent( tr("mouseDoubleClickEvent( x:%1, y:%2, button:%3 )") + .arg( event->x() ) + .arg( event->y() ) + .arg( event->button()==Qt::LeftButton? "LeftButton": + event->button()==Qt::RightButton?"RightButton": + event->button()==Qt::MidButton? "MidButton": + event->button()==Qt::XButton1? "XButton1": + "XButton2" ) ); +} + +void EventWidget::mouseMoveEvent( QMouseEvent * event ) +{ + emit gotEvent( tr("mouseMoveEvent( x:%1, y:%2, button:%3 )") + .arg( event->x() ) + .arg( event->y() ) + .arg( event->button()==Qt::LeftButton? "LeftButton": + event->button()==Qt::RightButton?"RightButton": + event->button()==Qt::MidButton? "MidButton": + event->button()==Qt::XButton1? "XButton1": + "XButton2" ) ); +} + +void EventWidget::mousePressEvent( QMouseEvent * event ) +{ + emit gotEvent( tr("mousePressEvent( x:%1, y:%2, button:%3 )") + .arg( event->x() ) + .arg( event->y() ) + .arg( event->button()==Qt::LeftButton? "LeftButton": + event->button()==Qt::RightButton?"RightButton": + event->button()==Qt::MidButton? "MidButton": + event->button()==Qt::XButton1? "XButton1": + "XButton2" ) ); +} + +void EventWidget::mouseReleaseEvent( QMouseEvent * event ) +{ + emit gotEvent( tr("mouseReleaseEvent( x:%1, y:%2, button:%3 )") + .arg( event->x() ) + .arg( event->y() ) + .arg( event->button()==Qt::LeftButton? "LeftButton": + event->button()==Qt::RightButton?"RightButton": + event->button()==Qt::MidButton? "MidButton": + event->button()==Qt::XButton1? "XButton1": + "XButton2" ) ); +} + +void EventWidget::resizeEvent( QResizeEvent * event ) +{ + emit gotEvent( tr("resizeEvent( w:%1, h:%2 )") + .arg( event->size().width() ) + .arg( event->size().height() ) ); +} + +void EventWidget::wheelEvent( QWheelEvent * event ) +{ + emit gotEvent( tr("wheelEvent( x:%1, y:%2, delta:%3, orientation:%4 )") + .arg( event->x() ) + .arg( event->y() ) + .arg( event->delta() ).arg( event->orientation()==1? + "Horizontal":"Vertical" ) ); +} diff --git a/Chapter06/eventlister/eventwidget.h b/Chapter06/eventlister/eventwidget.h new file mode 100644 index 0000000..b4d587b --- /dev/null +++ b/Chapter06/eventlister/eventwidget.h @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef EVENTWIDGET_H +#define EVENTWIDGET_H + +#include + +class QCloseEvent; +class QContextMenuEvent; +class QEvent; +class QFocusEvent; +class QHideEvent; +class QKeyEvent; +class QMouseEvent; +class QPaintEvent; +class QResizeEvent; +class QShowEvent; +class QWheelEvent; + +class EventWidget : public QWidget +{ + Q_OBJECT + +public: + EventWidget( QWidget *parent = 0 ); + +signals: + void gotEvent( const QString ); + +protected: + void closeEvent( QCloseEvent * event ); + void contextMenuEvent( QContextMenuEvent * event ); + void enterEvent( QEvent * event ); + void focusInEvent( QFocusEvent * event ); + void focusOutEvent( QFocusEvent * event ); + void hideEvent( QHideEvent * event ); + void keyPressEvent( QKeyEvent * event ); + void keyReleaseEvent( QKeyEvent * event ); + void leaveEvent( QEvent * event ); + void mouseDoubleClickEvent( QMouseEvent * event ); + void mouseMoveEvent( QMouseEvent * event ); + void mousePressEvent( QMouseEvent * event ); + void mouseReleaseEvent( QMouseEvent * event ); + void paintEvent( QPaintEvent * event ); + void resizeEvent( QResizeEvent * event ); + void showEvent( QShowEvent * event ); + void wheelEvent( QWheelEvent * event ); +}; + +#endif // EVENTWIDGET_H diff --git a/Chapter06/eventlister/main.cpp b/Chapter06/eventlister/main.cpp new file mode 100644 index 0000000..c00d7d7 --- /dev/null +++ b/Chapter06/eventlister/main.cpp @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include + +#include "eventwidget.h" + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + QTextEdit log; + EventWidget widget; + + QObject::connect( &widget, SIGNAL(gotEvent(QString)), &log, SLOT(append(QString)) ); + + log.show(); + widget.show(); + + return app.exec(); +} diff --git a/Chapter07/README.txt b/Chapter07/README.txt new file mode 100644 index 0000000..8547397 --- /dev/null +++ b/Chapter07/README.txt @@ -0,0 +1,124 @@ +This file is a part of 1590598318-1.zip containing example source code for the +Foundations of Qt Development book available from APress (ISBN 1590598318). + +These are the examples for chapter 7 - Drawing and Printing + +Chapter06/circlebar + + Listing 7-1 + + Shows how a painter is created for painting to a widget. Notice that this + example is found among the chapter 6 examples. + +penbrush + + Listing 7-2 + + Shows how to setup a painter for drawing to a pixmap. + + +drawlines + + Listing 7-3 + + Shows different methods for drawing lines. + + +penpatterns + + Listing 7-4 + + Shows how to draw lines using different patterns. + + +rects + + Listing 7-5 + + Shows how to draw rectangles. + + +circles + + Listing 7-6 + + Shows how to draw ellipses, circles and arcs. + + +text + + Not shown in any Listings, but Figure 7-13. + + Shows different ways of drawing text. + + +paths + + Not shown in any Listings, but Figure 7-14. + + Shows how to draw paths. + + +brushgradients + + Listing 7-7 + + Shows how to setup brush gradients. + + +widgets/custombutton + + Listings 7-8, 7-9, 7-10, 7-11, 7-12 + + Shows how to customize the appearance of a button and how to use it. + +widgets/events + + Listings 7-13, 7-14, 7-15, 7-16, 7-17, 7-18 + + Shows how to implement a completely custom widget with custom painting and + custom event handling. + + +graphicsview/standarditems + + Listing 7-19 + + Shows how to use the standard items available for the QGraphicsScene. + + +graphicsview/transformations + + Listings 7-20, 7-21 + + Shows how to compose a QGraphicsItem from several items and how to apply + transformations. + + +graphicsview/interaction + + Listings 7-22, 7-23, 7-24, 7-25, 7-26, 7-27, 7-28, 7-29, 7-30, 7-31 + + Shows to to provide interactive QGraphicItems and how to use handles to + modify existing items. + + +printing/painter + + Listings 7-32 + + Shows how to print using the QPainter and QPrinter classes. + + +printing/graphicsview + + Listings 7-33 + + Shows how to print a graphics scene. + + +graphicsview/opengl + + Listings 7-34 + + Shows how to render a graphics scene using OpenGL. diff --git a/Chapter07/brushgradients/Thumbs.db b/Chapter07/brushgradients/Thumbs.db new file mode 100644 index 0000000..0354ddf Binary files /dev/null and b/Chapter07/brushgradients/Thumbs.db differ diff --git a/Chapter07/brushgradients/brushgradients.pro b/Chapter07/brushgradients/brushgradients.pro new file mode 100644 index 0000000..aff85cf --- /dev/null +++ b/Chapter07/brushgradients/brushgradients.pro @@ -0,0 +1,11 @@ +###################################################################### +# Automatically generated by qmake (2.01a) Mon Jun 4 19:11:36 2007 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp diff --git a/Chapter07/brushgradients/main.cpp b/Chapter07/brushgradients/main.cpp new file mode 100644 index 0000000..f863ae1 --- /dev/null +++ b/Chapter07/brushgradients/main.cpp @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include +#include +#include + +void createBrushShot( QBrush brush, QString name ) +{ + QPixmap pixmap( 200, 200 ); + pixmap.fill( Qt::white ); + + QPainter painter( &pixmap ); + + painter.setBrush( brush ); + painter.drawRect( pixmap.rect() ); + + pixmap.save( QString("%1.png").arg( name ) ); +} + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + QLinearGradient linGrad( QPointF(80, 80), QPoint( 120, 120 ) ); + linGrad.setColorAt( 0, Qt::black ); + linGrad.setColorAt( 1, Qt::white ); + + linGrad.setSpread( QGradient::PadSpread ); + createBrushShot( QBrush( linGrad ), "LinearPad" ); + linGrad.setSpread( QGradient::RepeatSpread ); + createBrushShot( QBrush( linGrad ), "LinearRepeat" ); + linGrad.setSpread( QGradient::ReflectSpread ); + createBrushShot( QBrush( linGrad ), "LinearReflect" ); + + QRadialGradient radGrad( QPointF(100, 100), 30 ); + radGrad.setColorAt( 0, Qt::black ); + radGrad.setColorAt( 1, Qt::white ); + + radGrad.setSpread( QGradient::PadSpread ); + createBrushShot( QBrush( radGrad ), "RadialPad" ); + radGrad.setSpread( QGradient::RepeatSpread ); + createBrushShot( QBrush( radGrad ), "RadialRepeat" ); + radGrad.setSpread( QGradient::ReflectSpread ); + createBrushShot( QBrush( radGrad ), "RadialReflect" ); + + QConicalGradient conGrad( QPointF(100, 100), -45.0 ); + conGrad.setColorAt( 0, Qt::black ); + conGrad.setColorAt( 1, Qt::white ); + + conGrad.setSpread( QGradient::PadSpread ); + createBrushShot( QBrush( conGrad ), "ConicalPad" ); + conGrad.setSpread( QGradient::RepeatSpread ); + createBrushShot( QBrush( conGrad ), "ConicalRepeat" ); + conGrad.setSpread( QGradient::ReflectSpread ); + createBrushShot( QBrush( conGrad ), "ConicalReflect" ); + + QBrush textureBrush( QImage("texture.png") ); + createBrushShot( textureBrush, "TextureBrush" ); + + return 0; +} diff --git a/Chapter07/brushgradients/texture.png b/Chapter07/brushgradients/texture.png new file mode 100644 index 0000000..5e7c79e Binary files /dev/null and b/Chapter07/brushgradients/texture.png differ diff --git a/Chapter07/circles/circles.pro b/Chapter07/circles/circles.pro new file mode 100644 index 0000000..cae8dea --- /dev/null +++ b/Chapter07/circles/circles.pro @@ -0,0 +1,11 @@ +###################################################################### +# Automatically generated by qmake (2.01a) sö 8. apr 19:14:35 2007 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp diff --git a/Chapter07/circles/main.cpp b/Chapter07/circles/main.cpp new file mode 100644 index 0000000..95e400a --- /dev/null +++ b/Chapter07/circles/main.cpp @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include +#include + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + QPixmap pixmap( 200, 190 ); + pixmap.fill( Qt::white ); + + QPainter painter( &pixmap ); + painter.setRenderHint( QPainter::Antialiasing ); + painter.setPen( Qt::black ); + + painter.drawEllipse( 10, 10, 10, 80 ); + painter.drawEllipse( 30, 10, 20, 80 ); + painter.drawEllipse( 60, 10, 40, 80 ); + painter.drawEllipse( QRect( 110, 10, 80, 80 ) ); + + painter.drawArc( 10, 100, 10, 80, 30*16, 240*16 ); + painter.drawArc( 30, 100, 20, 80, 45*16, 200*16 ); + painter.drawArc( 60, 100, 40, 80, 60*16, 160*16 ); + painter.drawArc( QRect( 110, 100, 80, 80 ), 75*16, 120*16 ); + + pixmap.save( "circles.png" ); + + return 0; +} diff --git a/Chapter07/drawlines/drawlines.pro b/Chapter07/drawlines/drawlines.pro new file mode 100644 index 0000000..a3d73c4 --- /dev/null +++ b/Chapter07/drawlines/drawlines.pro @@ -0,0 +1,11 @@ +###################################################################### +# Automatically generated by qmake (2.01a) sö 8. apr 13:47:20 2007 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp diff --git a/Chapter07/drawlines/main.cpp b/Chapter07/drawlines/main.cpp new file mode 100644 index 0000000..ab3ed22 --- /dev/null +++ b/Chapter07/drawlines/main.cpp @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include +#include + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + QPixmap pixmap( 200, 100 ); + pixmap.fill( Qt::white ); + + QPainter painter( &pixmap ); + painter.setPen( Qt::black ); + + QVector polyPoints; + polyPoints << QPoint( 60, 10 ) << QPoint( 80, 90 ) << QPoint( 75, 10 ) << QPoint( 110, 90 ); + + QVector linePoints; + foreach( QPoint point, polyPoints ) + linePoints << point + QPoint( 80, 0 ); + + painter.drawLine( QPoint( 10, 10 ), QPoint( 30, 90 ) ); + painter.drawPolyline( polyPoints ); + painter.drawLines( linePoints ); + + pixmap.save( "drawlines.png" ); + + return 0; +} \ No newline at end of file diff --git a/Chapter07/graphicsview/interaction/handleitem.cpp b/Chapter07/graphicsview/interaction/handleitem.cpp new file mode 100644 index 0000000..c7bb775 --- /dev/null +++ b/Chapter07/graphicsview/interaction/handleitem.cpp @@ -0,0 +1,149 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "handleitem.h" + +#include +#include + +HandleItem::HandleItem( QGraphicsItem *item, QGraphicsScene *scene, QColor color, HandleItem::HandleRole role, QList handles ) : QGraphicsItem( 0, scene ) +{ + m_role = role; + m_color = color; + + m_item = item; + m_handles = handles; + + m_pressed = false; + setZValue( 100 ); + + setFlag( ItemIsMovable ); +} + +void HandleItem::paint( QPainter *paint, const QStyleOptionGraphicsItem *option, QWidget *widget ) +{ + paint->setPen( m_color ); + paint->setBrush( m_color ); + + QRectF rect = boundingRect(); + QVector points; + + switch( m_role ) + { + case CenterHandle: + paint->drawEllipse( rect ); + break; + case RightHandle: + points << rect.center()+QPointF(3,0) << rect.center()+QPointF(-3,-5) << rect.center()+QPointF(-3,5); + paint->drawConvexPolygon( QPolygonF(points) ); + break; + case TopHandle: + points << rect.center()+QPointF(0,-3) << rect.center()+QPointF(-5,3) << rect.center()+QPointF(5,3); + paint->drawConvexPolygon( QPolygonF(points) ); + break; + } +} + +QRectF HandleItem::boundingRect() const +{ + QPointF point = m_item->boundingRect().center(); + + switch( m_role ) + { + case CenterHandle: + return QRectF( point-QPointF(5, 5), QSize( 10, 10 ) ); + case RightHandle: + point.setX( m_item->boundingRect().right() ); + return QRectF( point-QPointF(3, 5), QSize( 6, 10 ) ); + case TopHandle: + point.setY( m_item->boundingRect().top() ); + return QRectF( point-QPointF(5, 3), QSize( 10, 6 ) ); + } + + return QRectF(); +} + +QVariant HandleItem::itemChange( GraphicsItemChange change, const QVariant &data ) +{ + if( change == ItemPositionChange && m_pressed ) + { + QPointF movement = data.toPoint() - pos(); + QPointF center = m_item->boundingRect().center(); + + switch( m_role ) + { + case CenterHandle: + m_item->moveBy( movement.x(), movement.y() ); + + foreach( HandleItem *handle, m_handles ) + handle->translate( movement.x(), movement.y() ); + + break; + case RightHandle: + if( 2*movement.x() + m_item->sceneBoundingRect().width() <= 5 ) + return QGraphicsItem::itemChange( change, pos() ); + + movement.setY( 0 ); + + m_item->translate( center.x(), center.y() ); + m_item->scale( 1.0+2.0*movement.x()/(m_item->sceneBoundingRect().width()), 1 ); + m_item->translate( -center.x(), -center.y() ); + + break; + case TopHandle: + if( -2*movement.y() + m_item->sceneBoundingRect().height() <= 5 ) + return QGraphicsItem::itemChange( change, pos() ); + + movement.setX( 0 ); + + m_item->translate( center.x(), center.y() ); + m_item->scale( 1, 1.0-2.0*movement.y()/(m_item->sceneBoundingRect().height()) ); + m_item->translate( -center.x(), -center.y() ); + break; + } + + return QGraphicsItem::itemChange( change, pos()+movement ); + } + + return QGraphicsItem::itemChange( change, data ); +} + +void HandleItem::mousePressEvent( QGraphicsSceneMouseEvent *event ) +{ + m_pressed = true; + QGraphicsItem::mousePressEvent( event ); +} + +void HandleItem::mouseReleaseEvent( QGraphicsSceneMouseEvent *event ) +{ + m_pressed = false; + QGraphicsItem::mouseReleaseEvent( event ); +} diff --git a/Chapter07/graphicsview/interaction/handleitem.h b/Chapter07/graphicsview/interaction/handleitem.h new file mode 100644 index 0000000..0c5bc33 --- /dev/null +++ b/Chapter07/graphicsview/interaction/handleitem.h @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef HANDLEITEM_H +#define HANDLEITEM_H + +#include + +class HandleItem; + +class HandleItem : public QGraphicsItem +{ +public: + enum HandleRole + { + CenterHandle, + RightHandle, + TopHandle + }; + + HandleItem( QGraphicsItem *item, QGraphicsScene *scene, QColor color, HandleRole role = CenterHandle, QList handles = QList() ); + + void paint( QPainter *paint, const QStyleOptionGraphicsItem *option, QWidget *widget ); + QRectF boundingRect() const; + +protected: + void mousePressEvent( QGraphicsSceneMouseEvent *event ); + void mouseReleaseEvent( QGraphicsSceneMouseEvent *event ); + + QVariant itemChange( GraphicsItemChange change, const QVariant &data ); + +private: + QGraphicsItem *m_item; + + HandleRole m_role; + QColor m_color; + + QList m_handles; + + bool m_pressed; +}; + +#endif // HANDLEITEM_H diff --git a/Chapter07/graphicsview/interaction/interaction.pro b/Chapter07/graphicsview/interaction/interaction.pro new file mode 100644 index 0000000..9f0ecd0 --- /dev/null +++ b/Chapter07/graphicsview/interaction/interaction.pro @@ -0,0 +1,13 @@ +###################################################################### +# Automatically generated by qmake (2.01a) Sat Jun 2 15:02:51 2007 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +HEADERS += handleitem.h +SOURCES += handleitem.cpp main.cpp +CONFIG += console diff --git a/Chapter07/graphicsview/interaction/main.cpp b/Chapter07/graphicsview/interaction/main.cpp new file mode 100644 index 0000000..da03b16 --- /dev/null +++ b/Chapter07/graphicsview/interaction/main.cpp @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include +#include +#include +#include + +#include "handleitem.h" + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + QGraphicsScene scene( 0, 0, 200, 200 ); + + QGraphicsRectItem *rectItem = new QGraphicsRectItem( QRect( 10, 10, 50, 100 ), 0, &scene ); + QGraphicsEllipseItem *elItem = new QGraphicsEllipseItem( QRect( 80, 40, 100, 80 ), 0, &scene ); + + HandleItem *trh = new HandleItem( rectItem, &scene, Qt::red, HandleItem::TopHandle ); + HandleItem *rrh = new HandleItem( rectItem, &scene, Qt::red, HandleItem::RightHandle ); + HandleItem *crh = new HandleItem( rectItem, &scene, Qt::red, HandleItem::CenterHandle, QList() << trh << rrh ); + + HandleItem *teh = new HandleItem( elItem, &scene, Qt::green, HandleItem::TopHandle ); + HandleItem *reh = new HandleItem( elItem, &scene, Qt::green, HandleItem::RightHandle ); + HandleItem *ceh = new HandleItem( elItem, &scene, Qt::green, HandleItem::CenterHandle, QList() << teh << reh ); + + QGraphicsView view; + view.setScene( &scene ); + view.show(); + + int res = app.exec(); + + delete crh; + delete trh; + delete rrh; + + delete ceh; + delete teh; + delete reh; + + return res; +} diff --git a/Chapter07/graphicsview/opengl/main.cpp b/Chapter07/graphicsview/opengl/main.cpp new file mode 100644 index 0000000..9a303d7 --- /dev/null +++ b/Chapter07/graphicsview/opengl/main.cpp @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include +#include +#include +#include + +QGraphicsItem *createItem( int x, QGraphicsScene *scene ) +{ + QGraphicsRectItem *rectItem = new QGraphicsRectItem( QRect( x+40, 40, 120, 120 ), 0, scene ); + rectItem->setPen( QPen(Qt::black) ); + rectItem->setBrush( Qt::gray ); + + QGraphicsRectItem *innerRectItem = new QGraphicsRectItem( QRect( x+50, 50, 45, 100 ), rectItem, scene ); + innerRectItem->setPen( QPen(Qt::black) ); + innerRectItem->setBrush( Qt::white ); + + QGraphicsEllipseItem *ellipseItem = new QGraphicsEllipseItem( QRect( x+105, 50, 45, 100 ), rectItem, scene ); + ellipseItem->setPen( QPen(Qt::black) ); + ellipseItem->setBrush( Qt::white ); + + return rectItem; +} + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + QGraphicsScene scene( QRect( 0, 00, 1000, 200 ) ); + + QGraphicsItem *item1 = createItem( 0, &scene ); + + QGraphicsItem *item2 = createItem( 200, &scene ); + item2->translate( 300, 100 ); + item2->rotate( 30 ); + item2->translate( -300, -100 ); + + QGraphicsItem *item3 = createItem( 400, &scene ); + item3->translate( 500, 100 ); + item3->scale( 0.5, 0.7 ); + item3->translate( -500, -100 ); + + QGraphicsItem *item4 = createItem( 600, &scene ); + item4->translate( 700, 100 ); + item4->shear( 0.1, 0.3 ); + item4->translate( -700, -100 ); + + QGraphicsItem *item5 = createItem( 800, &scene ); + item5->translate( 900, 100 ); + item5->scale( 0.5, 0.7 ); + item5->rotate( 30 ); + item5->shear( 0.1, 0.3 ); + item5->translate( -900, -100 ); + + QGraphicsView view; + view.setScene( &scene ); + view.setViewport( new QGLWidget() ); + view.show(); + + return app.exec(); +} diff --git a/Chapter07/graphicsview/opengl/opengl.pro b/Chapter07/graphicsview/opengl/opengl.pro new file mode 100644 index 0000000..4f57952 --- /dev/null +++ b/Chapter07/graphicsview/opengl/opengl.pro @@ -0,0 +1,12 @@ +###################################################################### +# Automatically generated by qmake (2.01a) Sun Apr 22 19:28:09 2007 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp +QT += opengl diff --git a/Chapter07/graphicsview/standarditems/main.cpp b/Chapter07/graphicsview/standarditems/main.cpp new file mode 100644 index 0000000..6beb81e --- /dev/null +++ b/Chapter07/graphicsview/standarditems/main.cpp @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include +#include +#include + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + QGraphicsScene scene( QRect( -50, -50, 400, 200 ) ); + + QGraphicsRectItem *rectItem = new QGraphicsRectItem( QRect( -25, 25, 200, 40 ), 0, &scene ); + rectItem->setPen( QPen( Qt::red, 3, Qt::DashDotLine ) ); + rectItem->setBrush( Qt::gray ); + + QGraphicsSimpleTextItem *textItem = new QGraphicsSimpleTextItem( "Foundations of Qt", 0, &scene ); + textItem->setPos( 50, 0 ); + + QGraphicsEllipseItem *ellipseItem = new QGraphicsEllipseItem( QRect( 170, 20, 100, 75 ), 0, &scene ); + ellipseItem->setPen( QPen(Qt::darkBlue) ); + ellipseItem->setBrush( Qt::blue ); + + QGraphicsPolygonItem *polygonItem = new QGraphicsPolygonItem( QPolygonF( QVector() << QPointF( 10, 10 ) << QPointF( 0, 90 ) << QPointF( 40, 70 ) << QPointF( 80, 110 ) << QPointF( 70, 20 ) ), 0, &scene ); + polygonItem->setPen( QPen(Qt::darkGreen) ); + polygonItem->setBrush( Qt::yellow ); + + QGraphicsView view; + view.setScene( &scene ); + view.show(); + + return app.exec(); +} diff --git a/Chapter07/graphicsview/standarditems/standarditems.pro b/Chapter07/graphicsview/standarditems/standarditems.pro new file mode 100644 index 0000000..797c7f6 --- /dev/null +++ b/Chapter07/graphicsview/standarditems/standarditems.pro @@ -0,0 +1,11 @@ +###################################################################### +# Automatically generated by qmake (2.01a) to 12. apr 20:00:42 2007 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp diff --git a/Chapter07/graphicsview/transformations/main.cpp b/Chapter07/graphicsview/transformations/main.cpp new file mode 100644 index 0000000..1ed5220 --- /dev/null +++ b/Chapter07/graphicsview/transformations/main.cpp @@ -0,0 +1,90 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include +#include +#include + +QGraphicsItem *createItem( int x, QGraphicsScene *scene ) +{ + QGraphicsRectItem *rectItem = new QGraphicsRectItem( QRect( x+40, 40, 120, 120 ), 0, scene ); + rectItem->setPen( QPen(Qt::black) ); + rectItem->setBrush( Qt::gray ); + + QGraphicsRectItem *innerRectItem = new QGraphicsRectItem( QRect( x+50, 50, 45, 100 ), rectItem, scene ); + innerRectItem->setPen( QPen(Qt::black) ); + innerRectItem->setBrush( Qt::white ); + + QGraphicsEllipseItem *ellipseItem = new QGraphicsEllipseItem( QRect( x+105, 50, 45, 100 ), rectItem, scene ); + ellipseItem->setPen( QPen(Qt::black) ); + ellipseItem->setBrush( Qt::white ); + + return rectItem; +} + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + QGraphicsScene scene( QRect( 0, 00, 1000, 200 ) ); + + QGraphicsItem *item1 = createItem( 0, &scene ); + + QGraphicsItem *item2 = createItem( 200, &scene ); + item2->translate( 300, 100 ); + item2->rotate( 30 ); + item2->translate( -300, -100 ); + + QGraphicsItem *item3 = createItem( 400, &scene ); + item3->translate( 500, 100 ); + item3->scale( 0.5, 0.7 ); + item3->translate( -500, -100 ); + + QGraphicsItem *item4 = createItem( 600, &scene ); + item4->translate( 700, 100 ); + item4->shear( 0.1, 0.3 ); + item4->translate( -700, -100 ); + + QGraphicsItem *item5 = createItem( 800, &scene ); + item5->translate( 900, 100 ); + item5->scale( 0.5, 0.7 ); + item5->rotate( 30 ); + item5->shear( 0.1, 0.3 ); + item5->translate( -900, -100 ); + + QGraphicsView view; + view.setScene( &scene ); + view.show(); + + return app.exec(); +} diff --git a/Chapter07/graphicsview/transformations/transformations.pro b/Chapter07/graphicsview/transformations/transformations.pro new file mode 100644 index 0000000..29ffc15 --- /dev/null +++ b/Chapter07/graphicsview/transformations/transformations.pro @@ -0,0 +1,11 @@ +###################################################################### +# Automatically generated by qmake (2.01a) sö 15. apr 16:10:32 2007 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp diff --git a/Chapter07/paths/main.cpp b/Chapter07/paths/main.cpp new file mode 100644 index 0000000..1a23ebc --- /dev/null +++ b/Chapter07/paths/main.cpp @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include +#include +#include + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + QPixmap pixmap( 200, 200 ); + pixmap.fill( Qt::white ); + + QPainterPath path; + + path.addEllipse( 80, 80, 80, 80 ); + + path.moveTo( 120, 120 ); + path.lineTo( 120, 40 ); + path.arcTo( 40, 40, 160, 160, 90, 90 ); + path.lineTo( 120, 120 ); + + QFont font = QApplication::font(); + font.setPixelSize( 40 ); + + path.addText( 20, 180, font, "Path" ); + + QPainter painter( &pixmap ); + painter.setRenderHint( QPainter::Antialiasing ); + + painter.setPen( Qt::black ); + painter.setBrush( Qt::gray ); + + painter.drawPath( path ); + + pixmap.save( "path.png" ); + + return 0; +} diff --git a/Chapter07/paths/paths.pro b/Chapter07/paths/paths.pro new file mode 100644 index 0000000..f00b963 --- /dev/null +++ b/Chapter07/paths/paths.pro @@ -0,0 +1,11 @@ +###################################################################### +# Automatically generated by qmake (2.01a) Mon Jun 4 17:45:40 2007 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp diff --git a/Chapter07/penbrush/main.cpp b/Chapter07/penbrush/main.cpp new file mode 100644 index 0000000..aa66bf5 --- /dev/null +++ b/Chapter07/penbrush/main.cpp @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include +#include + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + QPixmap pixmap( 200, 100 ); + QPainter painter( &pixmap ); + + painter.setPen( Qt::red ); + painter.setBrush( Qt::yellow ); + + return 0; +} \ No newline at end of file diff --git a/Chapter07/penbrush/penbrush.pro b/Chapter07/penbrush/penbrush.pro new file mode 100644 index 0000000..911958e --- /dev/null +++ b/Chapter07/penbrush/penbrush.pro @@ -0,0 +1,11 @@ +###################################################################### +# Automatically generated by qmake (2.01a) sö 8. apr 13:12:42 2007 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp diff --git a/Chapter07/penpatterns/main.cpp b/Chapter07/penpatterns/main.cpp new file mode 100644 index 0000000..0327e81 --- /dev/null +++ b/Chapter07/penpatterns/main.cpp @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include +#include + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + QPixmap pixmap( 200, 100 ); + pixmap.fill( Qt::white ); + + QPainter painter( &pixmap ); + + QPen pen( Qt::black ); + + pen.setStyle( Qt::SolidLine ); + painter.setPen( pen ); + painter.drawLine( QPoint( 10, 10 ), QPoint( 190, 10 ) ); + + pen.setStyle( Qt::DashDotLine ); + painter.setPen( pen ); + painter.drawLine( QPoint( 10, 50 ), QPoint( 190, 50 ) ); + + pen.setDashPattern( QVector() << 1 << 1 << 1 << 1 << 2 << 2 << 2 << 2 << 4 << 4 << 4 << 4 << 8 << 8 << 8 << 8 ); + pen.setStyle( Qt::CustomDashLine ); + painter.setPen( pen ); + painter.drawLine( QPoint( 10, 90 ), QPoint( 190, 90 ) ); + + pixmap.save( "penpatterns.png" ); + + return 0; +} \ No newline at end of file diff --git a/Chapter07/penpatterns/penpatterns.pro b/Chapter07/penpatterns/penpatterns.pro new file mode 100644 index 0000000..e020c10 --- /dev/null +++ b/Chapter07/penpatterns/penpatterns.pro @@ -0,0 +1,11 @@ +###################################################################### +# Automatically generated by qmake (2.01a) sö 8. apr 18:27:48 2007 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp diff --git a/Chapter07/printing/graphicsview/graphicsview.pro b/Chapter07/printing/graphicsview/graphicsview.pro new file mode 100644 index 0000000..4ddcb61 --- /dev/null +++ b/Chapter07/printing/graphicsview/graphicsview.pro @@ -0,0 +1,11 @@ +###################################################################### +# Automatically generated by qmake (2.01a) Tue Jun 5 09:41:01 2007 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp diff --git a/Chapter07/printing/graphicsview/main.cpp b/Chapter07/printing/graphicsview/main.cpp new file mode 100644 index 0000000..afa6180 --- /dev/null +++ b/Chapter07/printing/graphicsview/main.cpp @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include +#include +#include + +#include +#include + +#include + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + QGraphicsScene scene( QRect( -50, -50, 400, 200 ) ); + + QGraphicsRectItem *rectItem = new QGraphicsRectItem( QRect( -25, 25, 200, 40 ), 0, &scene ); + rectItem->setPen( QPen( Qt::red, 3, Qt::DashDotLine ) ); + rectItem->setBrush( Qt::gray ); + + QGraphicsSimpleTextItem *textItem = new QGraphicsSimpleTextItem( "Foundations of Qt", 0, &scene ); + textItem->setPos( 50, 0 ); + + QGraphicsEllipseItem *ellipseItem = new QGraphicsEllipseItem( QRect( 170, 20, 100, 75 ), 0, &scene ); + ellipseItem->setPen( QPen(Qt::darkBlue) ); + ellipseItem->setBrush( Qt::blue ); + + QGraphicsPolygonItem *polygonItem = new QGraphicsPolygonItem( QPolygonF( QVector() << QPointF( 10, 10 ) << QPointF( 0, 90 ) << QPointF( 40, 70 ) << QPointF( 80, 110 ) << QPointF( 70, 20 ) ), 0, &scene ); + polygonItem->setPen( QPen(Qt::darkGreen) ); + polygonItem->setBrush( Qt::yellow ); + + QPrinter printer; + QPrintDialog dlg( &printer ); + if( dlg.exec() == QDialog::Accepted ) + { + QPainter painter( &printer ); + + scene.render( &painter, printer.pageRect(), scene.sceneRect(), Qt::KeepAspectRatio ); + } + + return 0; +} diff --git a/Chapter07/printing/painter/main.cpp b/Chapter07/printing/painter/main.cpp new file mode 100644 index 0000000..4b9993d --- /dev/null +++ b/Chapter07/printing/painter/main.cpp @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include +#include + +#include + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + QPrinter printer; + QPrintDialog dlg( &printer ); + if( dlg.exec() == QDialog::Accepted ) + { + QPainter painter( &printer ); + + painter.setPen( Qt::black ); + + for( int page=0; page<5; page++ ) + { + painter.drawRect( printer.pageRect() ); + painter.drawLine( printer.pageRect().topLeft(), printer.pageRect().bottomRight() ); + painter.drawLine( printer.pageRect().topRight(), printer.pageRect().bottomLeft() ); + + QRectF textArea( + printer.pageRect().left() +printer.resolution() * 0.5, + printer.pageRect().top() +printer.resolution() * 0.5, + printer.pageRect().width() -printer.resolution() * 1.0, + printer.pageRect().height()-printer.resolution() * 1.5 ); + + painter.drawRect( textArea ); + + painter.drawText( textArea, Qt::AlignTop | Qt::AlignLeft, QString( "Page %1" ).arg( page+1 ) ); + + if( page != 4 ) + printer.newPage(); + } + } + + return 0; +} diff --git a/Chapter07/printing/painter/painter.pro b/Chapter07/printing/painter/painter.pro new file mode 100644 index 0000000..f64b5f6 --- /dev/null +++ b/Chapter07/printing/painter/painter.pro @@ -0,0 +1,11 @@ +###################################################################### +# Automatically generated by qmake (2.01a) Tue Jun 5 09:31:35 2007 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp diff --git a/Chapter07/rects/main.cpp b/Chapter07/rects/main.cpp new file mode 100644 index 0000000..f188ba5 --- /dev/null +++ b/Chapter07/rects/main.cpp @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include +#include + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + QPixmap pixmap( 200, 100 ); + pixmap.fill( Qt::white ); + + QPainter painter( &pixmap ); + painter.setPen( Qt::black ); + + painter.drawRect( 10, 10, 85, 35 ); + painter.drawRoundRect( 10, 55, 85, 35 ); + + QRect rect( 105, 10, 85, 35 ); + + painter.drawRoundRect( rect ); + painter.drawRect( rect.translated( 0, 45 ) ); + + pixmap.save( "rects.png" ); + + return 0; +} diff --git a/Chapter07/rects/rects.pro b/Chapter07/rects/rects.pro new file mode 100644 index 0000000..9840a7a --- /dev/null +++ b/Chapter07/rects/rects.pro @@ -0,0 +1,11 @@ +###################################################################### +# Automatically generated by qmake (2.01a) må 9. apr 19:02:26 2007 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp diff --git a/Chapter07/text/main.cpp b/Chapter07/text/main.cpp new file mode 100644 index 0000000..5efcb2c --- /dev/null +++ b/Chapter07/text/main.cpp @@ -0,0 +1,99 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include +#include +#include + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + QPixmap pixmap( 200, 330 ); + pixmap.fill( Qt::white ); + + QPainter painter( &pixmap ); + painter.setPen( Qt::black ); + + QPoint point = QPoint( 10, 20 ); + painter.drawText( point, "You can draw text from a point..." ); + painter.drawLine( point+QPoint(-5, 0), point+QPoint(5, 0) ); + painter.drawLine( point+QPoint(0, -5), point+QPoint(0, 5) ); + + QRect rect = QRect(10, 30, 180, 20); + painter.drawText( rect, Qt::AlignCenter, + "...or you can draw it inside a rectangle." ); + painter.drawRect( rect ); + + rect.translate( 0, 30 ); + + QFont font = QApplication::font(); + font.setPixelSize( rect.height() ); + painter.setFont( font ); + + painter.drawText( rect, Qt::AlignRight, "Right." ); + painter.drawText( rect, Qt::AlignLeft, "Left." ); + painter.drawRect( rect ); + + rect.translate( 0, rect.height()+10 ); + rect.setHeight( QFontMetrics( font ).height() ); + + painter.drawText( rect, Qt::AlignRight, "Right." ); + painter.drawText( rect, Qt::AlignLeft, "Left." ); + painter.drawRect( rect ); + + QTextDocument doc; + doc.setHtml( "

A QTextDocument can be used to present formatted text " + "in a nice way.

" + "

It can be formatted " + "in different ways.

" + "

The text can be really long and contain many " + "paragraphs. It is properly wrapped and such...

" ); + + rect.translate( 0, rect.height()+10 ); + rect.setHeight( 160 ); + doc.setTextWidth( rect.width() ); + painter.save(); + painter.translate( rect.topLeft() ); + doc.drawContents( &painter, rect.translated( -rect.topLeft() ) ); + painter.restore(); + painter.drawRect( rect ); + + rect.translate( 0, 160 ); + rect.setHeight( doc.size().height()-160 ); + painter.setBrush( Qt::gray ); + painter.drawRect( rect ); + + pixmap.save( "text.png" ); + + return 0; +} diff --git a/Chapter07/text/text.pro b/Chapter07/text/text.pro new file mode 100644 index 0000000..c3dad7c --- /dev/null +++ b/Chapter07/text/text.pro @@ -0,0 +1,11 @@ +###################################################################### +# Automatically generated by qmake (2.01a) Thu Apr 26 20:03:14 2007 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp diff --git a/Chapter07/widgets/custombutton/custombutton.pro b/Chapter07/widgets/custombutton/custombutton.pro new file mode 100644 index 0000000..eec385a --- /dev/null +++ b/Chapter07/widgets/custombutton/custombutton.pro @@ -0,0 +1,13 @@ +###################################################################### +# Automatically generated by qmake (2.01a) ti 17. apr 20:40:12 2007 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +HEADERS += dialog.h mybutton.h +FORMS += dialog.ui +SOURCES += dialog.cpp main.cpp mybutton.cpp diff --git a/Chapter07/widgets/custombutton/dialog.cpp b/Chapter07/widgets/custombutton/dialog.cpp new file mode 100644 index 0000000..1a60016 --- /dev/null +++ b/Chapter07/widgets/custombutton/dialog.cpp @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "dialog.h" + +#include + +Dialog::Dialog() : QDialog() +{ + ui.setupUi( this ); + + connect( ui.clickButton, SIGNAL(clicked()), this, SLOT(buttonClicked()) ); +} + +void Dialog::buttonClicked() +{ + QMessageBox::information( this, tr("Wohoo!"), tr("You clicked the button!") ); +} diff --git a/Chapter07/widgets/custombutton/dialog.h b/Chapter07/widgets/custombutton/dialog.h new file mode 100644 index 0000000..3678975 --- /dev/null +++ b/Chapter07/widgets/custombutton/dialog.h @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef DIALOG_H +#define DIALOG_H + +#include + +#include "ui_dialog.h" + +class Dialog : public QDialog +{ + Q_OBJECT + +public: + Dialog(); + +private slots: + void buttonClicked(); + +private: + Ui::Dialog ui; +}; + +#endif diff --git a/Chapter07/widgets/custombutton/dialog.ui b/Chapter07/widgets/custombutton/dialog.ui new file mode 100644 index 0000000..5b3493d --- /dev/null +++ b/Chapter07/widgets/custombutton/dialog.ui @@ -0,0 +1,184 @@ + + + Copyright (c) 2006-2007, Johan Thelin + + All rights reserved. + + Redistribution and use in source and binary forms, with or without modification, + are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + * Neither the name of APress nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + Dialog + + + + 0 + 0 + 400 + 300 + + + + Dialog + + + + 9 + + + 6 + + + + + Click This! + + + + + + + Qt::Vertical + + + + 88 + 51 + + + + + + + + Qt::Vertical + + + + 88 + 41 + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Close + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + false + + + This is Disabled + + + + + + + Toggle This! + + + true + + + + + + + + MyButton + QPushButton +
mybutton.h
+
+
+ + + + buttonBox + accepted() + Dialog + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + Dialog + reject() + + + 316 + 260 + + + 286 + 274 + + + + +
diff --git a/Chapter07/widgets/custombutton/main.cpp b/Chapter07/widgets/custombutton/main.cpp new file mode 100644 index 0000000..fe76ca5 --- /dev/null +++ b/Chapter07/widgets/custombutton/main.cpp @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include "dialog.h" + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + Dialog dialog; + dialog.show(); + + return app.exec(); +} diff --git a/Chapter07/widgets/custombutton/mybutton.cpp b/Chapter07/widgets/custombutton/mybutton.cpp new file mode 100644 index 0000000..08a82ab --- /dev/null +++ b/Chapter07/widgets/custombutton/mybutton.cpp @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "mybutton.h" + +#include +#include +#include + +MyButton::MyButton( QWidget *parent ) : QAbstractButton( parent ) +{ +} + +QSize MyButton::sizeHint() const +{ + return QSize( fontMetrics().width( text() )+10, fontMetrics().height()+10 ); +} + +void MyButton::paintEvent( QPaintEvent* ) +{ + QPainter painter( this ); + + QStyleOptionButton option; + option.init( this ); + if( isDown() ) + option.state |= QStyle::State_Sunken; + else if( isChecked() ) + option.state |= QStyle::State_On; + + style()->drawControl( QStyle::CE_PushButtonBevel, &option, &painter, this ); + + painter.setFont( font() ); + + if( !isEnabled() ) + painter.setPen( Qt::darkGray ); + else if( isDown() ) + painter.setPen( Qt::red ); + else + painter.setPen( Qt::darkRed ); + + painter.drawText( rect(), Qt::AlignCenter, text() ); +} diff --git a/Chapter07/widgets/custombutton/mybutton.h b/Chapter07/widgets/custombutton/mybutton.h new file mode 100644 index 0000000..8910465 --- /dev/null +++ b/Chapter07/widgets/custombutton/mybutton.h @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef MYBUTTON_H +#define MYBUTTON_H + +#include + +class MyButton : public QAbstractButton +{ + Q_OBJECT + +public: + MyButton( QWidget *parent=0 ); + + QSize sizeHint() const; + +protected: + void paintEvent( QPaintEvent* ); +}; + +#endif diff --git a/Chapter07/widgets/events/circlewidget.cpp b/Chapter07/widgets/events/circlewidget.cpp new file mode 100644 index 0000000..b5b089f --- /dev/null +++ b/Chapter07/widgets/events/circlewidget.cpp @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "circlewidget.h" + +#include +#include +#include + +CircleWidget::CircleWidget( QWidget *parent ) : QWidget( parent ) +{ + r = 0; + + timer = new QTimer( this ); + timer->setInterval( 50 ); + + connect( timer, SIGNAL(timeout()), this, SLOT(timeout()) ); +} + +QSize CircleWidget::sizeHint() const +{ + return QSize( 200, 200 ); +} + +void CircleWidget::timeout() +{ + if( r == 0 ) + { + x = mx; + y = my; + + color = QColor( qrand()%256, qrand()%256, qrand()%256 ); + } + + int dx = mx-x; + int dy = my-y; + + if( dx*dx+dy*dy <= r*r ) + r++; + else + r--; + + update(); +} + +void CircleWidget::paintEvent( QPaintEvent* ) +{ + if( r > 0 ) + { + QPainter painter( this ); + + painter.setRenderHint( QPainter::Antialiasing ); + + painter.setPen( color ); + painter.setBrush( color ); + painter.drawEllipse( x-r, y-r, 2*r, 2*r ); + } +} + +void CircleWidget::mousePressEvent( QMouseEvent *e ) +{ + mx = e->x(); + my = e->y(); + + timer->start(); +} + +void CircleWidget::mouseMoveEvent( QMouseEvent *e ) +{ + mx = e->x(); + my = e->y(); +} + +void CircleWidget::mouseReleaseEvent( QMouseEvent *e ) +{ + timer->stop(); +} diff --git a/Chapter07/widgets/events/circlewidget.h b/Chapter07/widgets/events/circlewidget.h new file mode 100644 index 0000000..caa42cb --- /dev/null +++ b/Chapter07/widgets/events/circlewidget.h @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef CIRCLEWIDGET_H +#define CIRCLEWIDGET_H + +#include + +class QTimer; + +class CircleWidget : public QWidget +{ + Q_OBJECT + +public: + CircleWidget( QWidget *parent=0 ); + + QSize sizeHint() const; + +private slots: + void timeout(); + +protected: + void paintEvent( QPaintEvent* ); + + void mousePressEvent( QMouseEvent* ); + void mouseMoveEvent( QMouseEvent* ); + void mouseReleaseEvent( QMouseEvent* ); + +private: + int x, y, r; + QColor color; + + int mx, my; + + QTimer *timer; +}; + +#endif diff --git a/Chapter07/widgets/events/events.pro b/Chapter07/widgets/events/events.pro new file mode 100644 index 0000000..5ad199b --- /dev/null +++ b/Chapter07/widgets/events/events.pro @@ -0,0 +1,12 @@ +###################################################################### +# Automatically generated by qmake (2.01a) ti 17. apr 22:04:35 2007 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +HEADERS += circlewidget.h +SOURCES += circlewidget.cpp main.cpp diff --git a/Chapter07/widgets/events/main.cpp b/Chapter07/widgets/events/main.cpp new file mode 100644 index 0000000..c4b1cb5 --- /dev/null +++ b/Chapter07/widgets/events/main.cpp @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include "circlewidget.h" + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + CircleWidget widget; + widget.show(); + + return app.exec(); +} \ No newline at end of file diff --git a/Chapter08/README.txt b/Chapter08/README.txt new file mode 100644 index 0000000..6f1f056 --- /dev/null +++ b/Chapter08/README.txt @@ -0,0 +1,67 @@ +This file is a part of 1590598318-1.zip containing example source code for the +Foundations of Qt Development book available from APress (ISBN 1590598318). + +These are the examples for chapter 8 - Files, Streams, and XML + +rootlist + + Listing 8-1 + + Shows how to find the available drives. + + +fileexists + + Listing 8-2 + + Shows how to determine if a file exist and if it can be written to. + + +textstream + + Listings 8-3, 8-4, 8-5 + + Shows how to open a QTextStream and how to use it for reading and writing. + + +datastream + + Listings 8-6, 8-7, 8-8 + + Shows how to open a QDataStream and how to use it for reading and writing. + + +xmldomwrite + + Listings 8-10, 8-11, 8-12 + + Shows how to create an XML document using the DOM classes. + + +xmldomread + + Listings 8-13, 8-14 + + Shows how to read and XML document using the DOM classes. + + +xmldommodify + + Listings 8-15 + + Shows how to open, read, modify and write to an XML document using the DOM + classes. + + +xmlsaxread + + Listings 8-16, 8-17, 8-18, 8-19 + + Shows how to read an XML document using the SAX classes. + + +readwriteapplication + + Listings 8-20, 8-21, 8-22, 8-23, 8-24, 8-25, 8-26 + + The SDI application from chapter four gets proper file handling. \ No newline at end of file diff --git a/Chapter08/datastream/datastream.pro b/Chapter08/datastream/datastream.pro new file mode 100644 index 0000000..abac80d --- /dev/null +++ b/Chapter08/datastream/datastream.pro @@ -0,0 +1,13 @@ +###################################################################### +# Automatically generated by qmake (2.01a) to 16. nov 19:41:56 2006 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp +CONFIG += console +CONFIG += console diff --git a/Chapter08/datastream/main.cpp b/Chapter08/datastream/main.cpp new file mode 100644 index 0000000..7dcfb41 --- /dev/null +++ b/Chapter08/datastream/main.cpp @@ -0,0 +1,121 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include +#include +#include + +#include + +struct ColorText +{ + QString text; + QColor color; +}; + +QDataStream &operator<<( QDataStream &stream, const ColorText &data ) +{ + stream << data.text << data.color; + + return stream; +} + +QDataStream &operator>>( QDataStream &stream, ColorText &data ) +{ + stream >> data.text; + stream >> data.color; + + return stream; +} + +void saveList() +{ + QList list; + ColorText data; + + data.text = "Red"; + data.color = Qt::red; + list << data; + + data.text = "Blue"; + data.color = Qt::blue; + list << data; + + data.text = "Yellow"; + data.color = Qt::yellow; + list << data; + + data.text = "Green"; + data.color = Qt::green; + list << data; + + QFile file( "test.dat" ); + if( !file.open( QIODevice::WriteOnly ) ) + return; + + QDataStream stream( &file ); + stream.setVersion( QDataStream::Qt_4_2 ); + + stream << list; + + file.close(); +} + +void loadList() +{ + QList list; + + QFile file( "test.dat" ); + if( !file.open( QIODevice::ReadOnly ) ) + return; + + QDataStream stream( &file ); + stream.setVersion( QDataStream::Qt_4_2 ); + + stream >> list; + + file.close(); + + foreach( ColorText data, list ) + qDebug() << data.text << "(" + << data.color.red() << "," + << data.color.green() << "," + << data.color.blue() << ")"; +} + +int main( int argc, char **argv ) +{ + saveList(); + loadList(); + + return 0; +} diff --git a/Chapter08/datastream/test.dat b/Chapter08/datastream/test.dat new file mode 100644 index 0000000..5ad6c9f Binary files /dev/null and b/Chapter08/datastream/test.dat differ diff --git a/Chapter08/fileexist/fileexist.pro b/Chapter08/fileexist/fileexist.pro new file mode 100644 index 0000000..19f74f3 --- /dev/null +++ b/Chapter08/fileexist/fileexist.pro @@ -0,0 +1,12 @@ +###################################################################### +# Automatically generated by qmake (2.01a) sö 12. nov 11:38:54 2006 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp +CONFIG += console diff --git a/Chapter08/fileexist/main.cpp b/Chapter08/fileexist/main.cpp new file mode 100644 index 0000000..7f6d209 --- /dev/null +++ b/Chapter08/fileexist/main.cpp @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include + +int main( int argc, char **argv ) +{ + QFile file( "testfile.txt" ); + + if( !file.exists() ) + { + qDebug() << "The file" << file.fileName() << "does not exist."; + return 0; + } + + if( !file.open( QIODevice::WriteOnly ) ) + { + qDebug() << "Could not open" << file.fileName() << "for writing."; + return 0; + } + + qDebug() << "The file opened."; + + file.close(); + + return 0; +} diff --git a/Chapter08/fileexist/testfile.txt b/Chapter08/fileexist/testfile.txt new file mode 100644 index 0000000..e69de29 diff --git a/Chapter08/readwriteapplication/images.qrc b/Chapter08/readwriteapplication/images.qrc new file mode 100644 index 0000000..44947aa --- /dev/null +++ b/Chapter08/readwriteapplication/images.qrc @@ -0,0 +1,8 @@ + + + images/new.png + images/cut.png + images/copy.png + images/paste.png + + \ No newline at end of file diff --git a/Chapter08/readwriteapplication/images/copy.png b/Chapter08/readwriteapplication/images/copy.png new file mode 100644 index 0000000..7052fab Binary files /dev/null and b/Chapter08/readwriteapplication/images/copy.png differ diff --git a/Chapter08/readwriteapplication/images/cut.png b/Chapter08/readwriteapplication/images/cut.png new file mode 100644 index 0000000..3c9a806 Binary files /dev/null and b/Chapter08/readwriteapplication/images/cut.png differ diff --git a/Chapter08/readwriteapplication/images/new.png b/Chapter08/readwriteapplication/images/new.png new file mode 100644 index 0000000..d1e8915 Binary files /dev/null and b/Chapter08/readwriteapplication/images/new.png differ diff --git a/Chapter08/readwriteapplication/images/paste.png b/Chapter08/readwriteapplication/images/paste.png new file mode 100644 index 0000000..ee6558a Binary files /dev/null and b/Chapter08/readwriteapplication/images/paste.png differ diff --git a/Chapter08/readwriteapplication/main.cpp b/Chapter08/readwriteapplication/main.cpp new file mode 100644 index 0000000..1ea4ed3 --- /dev/null +++ b/Chapter08/readwriteapplication/main.cpp @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include "sdiwindow.h" + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + (new SdiWindow)->show(); + QObject::connect( &app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()) ); + + return app.exec(); +} diff --git a/Chapter08/readwriteapplication/sdi.pro b/Chapter08/readwriteapplication/sdi.pro new file mode 100644 index 0000000..78585fc --- /dev/null +++ b/Chapter08/readwriteapplication/sdi.pro @@ -0,0 +1,13 @@ +###################################################################### +# Automatically generated by qmake (2.01a) to 28. sep 16:43:38 2006 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +HEADERS += sdiwindow.h +SOURCES += main.cpp sdiwindow.cpp +RESOURCES += images.qrc diff --git a/Chapter08/readwriteapplication/sdiwindow.cpp b/Chapter08/readwriteapplication/sdiwindow.cpp new file mode 100644 index 0000000..56cf147 --- /dev/null +++ b/Chapter08/readwriteapplication/sdiwindow.cpp @@ -0,0 +1,271 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include +#include +#include +#include +#include + +#include + +#include +#include +#include + +#include "sdiwindow.h" + +SdiWindow::SdiWindow( QWidget *parent ) : QMainWindow( parent ) +{ + setAttribute( Qt::WA_DeleteOnClose ); + setWindowTitle( tr("%1[*] - %2" ).arg(tr("unnamed")).arg(tr("SDI")) ); + + docWidget = new QTextEdit( this ); + setCentralWidget( docWidget ); + + connect( docWidget->document(), SIGNAL(modificationChanged(bool)), this, SLOT(setWindowModified(bool)) ); + + createActions(); + createMenus(); + createToolbars(); + statusBar()->showMessage( tr("Done") ); +} + +void SdiWindow::closeEvent( QCloseEvent *event ) +{ + if( isSafeToClose() ) + event->accept(); + else + event->ignore(); +} + +void SdiWindow::fileNew() +{ + (new SdiWindow())->show(); +} + +void SdiWindow::helpAbout() +{ + QMessageBox::about( this, tr("About SDI"), tr("A single document interface application.") ); +} + +void SdiWindow::createActions() +{ + newAction = new QAction( QIcon(":/images/new.png"), tr("&New"), this ); + newAction->setShortcut( tr("Ctrl+N") ); + newAction->setStatusTip( tr("Create a new document") ); + connect( newAction, SIGNAL(triggered()), this, SLOT(fileNew()) ); + + openAction = new QAction( tr("&Open"), this ); + openAction->setShortcut( tr("Ctrl+O") ); + openAction->setStatusTip( tr("Open a document") ); + connect( openAction, SIGNAL(triggered()), this, SLOT(fileOpen()) ); + + saveAction = new QAction( tr("&Save"), this ); + saveAction->setShortcut( tr("Ctrl+S") ); + saveAction->setStatusTip( tr("Save the document") ); + connect( saveAction, SIGNAL(triggered()), this, SLOT(fileSave()) ); + + saveAsAction = new QAction( tr("Save &As"), this ); + saveAsAction->setStatusTip( tr("Save the document as") ); + connect( saveAsAction, SIGNAL(triggered()), this, SLOT(fileSaveAs()) ); + + closeAction = new QAction( tr("&Close"), this ); + closeAction->setShortcut( tr("Ctrl+W") ); + closeAction->setStatusTip( tr("Close this document") ); + connect( closeAction, SIGNAL(triggered()), this, SLOT(close()) ); + + exitAction = new QAction( tr("E&xit"), this ); + exitAction->setShortcut( tr("Ctrl+Q") ); + exitAction->setStatusTip( tr("Quit the application") ); + connect( exitAction, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()) ); + + + cutAction = new QAction( QIcon(":/images/cut.png"), tr("Cu&t"), this ); + cutAction->setShortcut( tr("Ctrl+X") ); + cutAction->setStatusTip( tr("Cut") ); + cutAction->setEnabled(false); + connect( docWidget, SIGNAL(copyAvailable(bool)), cutAction, SLOT(setEnabled(bool)) ); + connect( cutAction, SIGNAL(triggered()), docWidget, SLOT(cut()) ); + + copyAction = new QAction( QIcon(":/images/copy.png"), tr("&Copy"), this ); + copyAction->setShortcut( tr("Ctrl+C") ); + copyAction->setStatusTip( tr("Copy") ); + copyAction->setEnabled(false); + connect( docWidget, SIGNAL(copyAvailable(bool)), copyAction, SLOT(setEnabled(bool)) ); + connect( copyAction, SIGNAL(triggered()), docWidget, SLOT(copy()) ); + + + pasteAction = new QAction( QIcon(":/images/paste.png"), tr("&Paste"), this ); + pasteAction->setShortcut( tr("Ctrl+V") ); + pasteAction->setStatusTip( tr("Paste") ); + connect( pasteAction, SIGNAL(triggered()), docWidget, SLOT(paste()) ); + + + aboutAction = new QAction( tr("&About"), this ); + aboutAction->setStatusTip( tr("About this application") ); + connect( aboutAction, SIGNAL(triggered()), this, SLOT(helpAbout()) ); + + aboutQtAction = new QAction( tr("About &Qt"), this ); + aboutQtAction->setStatusTip( tr("About the Qt toolkit") ); + connect( aboutQtAction, SIGNAL(triggered()), qApp, SLOT(aboutQt()) ); +} + +void SdiWindow::createMenus() +{ + QMenu *menu; + + menu = menuBar()->addMenu( tr("&File") ); + menu->addAction( newAction ); + menu->addSeparator(); + menu->addAction( openAction ); + menu->addAction( saveAction ); + menu->addAction( saveAsAction ); + menu->addSeparator(); + menu->addAction( closeAction ); + menu->addSeparator(); + menu->addAction( exitAction ); + + menu = menuBar()->addMenu( tr("&Edit") ); + menu->addAction( cutAction ); + menu->addAction( copyAction ); + menu->addAction( pasteAction ); + + menu = menuBar()->addMenu( tr("&Help") ); + menu->addAction( aboutAction ); + menu->addAction( aboutQtAction ); +} + +void SdiWindow::createToolbars() +{ + QToolBar *toolbar; + + toolbar = addToolBar( tr("File") ); + toolbar->addAction( newAction ); + + toolbar = addToolBar( tr("Edit") ); + toolbar->addAction( cutAction ); + toolbar->addAction( copyAction ); + toolbar->addAction( pasteAction ); +} + + + +bool SdiWindow::isSafeToClose() +{ + if( isWindowModified() ) + { + switch( QMessageBox::warning( this, tr("SDI"), + tr("The document has unsaved changes.\n" + "Do you want to save it before it is closed?"), + QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel ) ) + { + case QMessageBox::Cancel: + return false; + case QMessageBox::Save: + return fileSave(); + default: + return true; + } + } + + return true; +} + +void SdiWindow::fileOpen() +{ + QString filename = QFileDialog::getOpenFileName( this ); + if( filename.isEmpty() ) + return; + + if( currentFilename.isEmpty() && !docWidget->document()->isModified() ) + loadFile( filename ); + else + { + SdiWindow *window = new SdiWindow(); + window->loadFile( filename ); + window->show(); + } +} + +bool SdiWindow::fileSave() +{ + if( currentFilename.isEmpty() ) + return fileSaveAs(); + else + return saveFile( currentFilename ); +} + +bool SdiWindow::fileSaveAs() +{ + QString filename = QFileDialog::getSaveFileName( this, tr("Save As"), currentFilename ); + if( filename.isEmpty() ) + return false; + + return saveFile( filename ); +} + +bool SdiWindow::saveFile( QString filename ) +{ + QFile file( filename ); + if( !file.open( QIODevice::WriteOnly | QIODevice::Text ) ) + { + QMessageBox::warning( this, tr("SDI"), tr("Failed to save file.") ); + return false; + } + + QTextStream stream( &file ); + stream << docWidget->toPlainText(); + + currentFilename = filename; + docWidget->document()->setModified( false ); + setWindowTitle( tr("%1[*] - %2" ).arg(filename).arg(tr("SDI")) ); + + return true; +} + +void SdiWindow::loadFile( QString filename ) +{ + QFile file( filename ); + if( !file.open( QIODevice::ReadOnly | QIODevice::Text ) ) + { + QMessageBox::warning( this, tr("SDI"), tr("Failed to open file.") ); + return; + } + + QTextStream stream( &file ); + docWidget->setPlainText( stream.readAll() ); + + currentFilename = filename; + docWidget->document()->setModified( false ); + setWindowTitle( tr("%1[*] - %2" ).arg(filename).arg(tr("SDI")) ); +} diff --git a/Chapter08/readwriteapplication/sdiwindow.h b/Chapter08/readwriteapplication/sdiwindow.h new file mode 100644 index 0000000..bb07eb6 --- /dev/null +++ b/Chapter08/readwriteapplication/sdiwindow.h @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef SDIWINDOW_H +#define SDIWINDOW_H + +#include + +class QAction; +class QTextEdit; + +class SdiWindow : public QMainWindow +{ + Q_OBJECT + +public: + SdiWindow( QWidget *parent = 0 ); + +protected: + void closeEvent( QCloseEvent *event ); + +private slots: + void fileNew(); + void helpAbout(); + + void fileOpen(); + bool fileSave(); + bool fileSaveAs(); + +private: + void createActions(); + void createMenus(); + void createToolbars(); + + bool isSafeToClose(); + + bool saveFile( QString filename ); + void loadFile( QString filename ); + QString currentFilename; + + QTextEdit *docWidget; + + QAction *newAction; + QAction *openAction; + QAction *saveAction; + QAction *saveAsAction; + QAction *closeAction; + QAction *exitAction; + + QAction *cutAction; + QAction *copyAction; + QAction *pasteAction; + + QAction *aboutAction; + QAction *aboutQtAction; +}; + +#endif // SDIWINDOW_H diff --git a/Chapter08/rootlist/main.cpp b/Chapter08/rootlist/main.cpp new file mode 100644 index 0000000..b0d94cb --- /dev/null +++ b/Chapter08/rootlist/main.cpp @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include + +#include + +int main( int argc, char **argv ) +{ + foreach( QFileInfo drive, QDir::drives() ) + { + qDebug() << "Drive: " << drive.absolutePath(); + + QDir dir = drive.dir(); + dir.setFilter( QDir::Dirs ); + + foreach( QFileInfo rootDirs, dir.entryInfoList() ) + qDebug() << " " << rootDirs.fileName(); + } + + return 0; +} diff --git a/Chapter08/rootlist/rootlist.pro b/Chapter08/rootlist/rootlist.pro new file mode 100644 index 0000000..e3f6afe --- /dev/null +++ b/Chapter08/rootlist/rootlist.pro @@ -0,0 +1,12 @@ +###################################################################### +# Automatically generated by qmake (2.01a) lö 11. nov 18:58:04 2006 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp +CONFIG += console diff --git a/Chapter08/textstream/main.cpp b/Chapter08/textstream/main.cpp new file mode 100644 index 0000000..f3920bf --- /dev/null +++ b/Chapter08/textstream/main.cpp @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include + +#include + +int main( int argc, char **argv ) +{ + QFile file( "main.cpp" ); + if( !file.open( QIODevice::ReadOnly | QIODevice::Text ) ) + qFatal( "Could not open the file" ); + + QTextStream stream( &file ); + + while( !stream.atEnd() ) + { + QString text; + stream >> text; + qDebug() << text; + } + + stream.seek( 0 ); + + while( !stream.atEnd() ) + { + QString text; + text = stream.readLine(); + qDebug() << text; + } + + file.close(); + + return 0; +} diff --git a/Chapter08/textstream/textstream.pro b/Chapter08/textstream/textstream.pro new file mode 100644 index 0000000..831fa2f --- /dev/null +++ b/Chapter08/textstream/textstream.pro @@ -0,0 +1,12 @@ +###################################################################### +# Automatically generated by qmake (2.01a) fr 17. nov 14:45:07 2006 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp +CONFIG += console diff --git a/Chapter08/xmldommodify/main.cpp b/Chapter08/xmldommodify/main.cpp new file mode 100644 index 0000000..79efa2e --- /dev/null +++ b/Chapter08/xmldommodify/main.cpp @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include + +#include +#include +#include + +int main( int argc, char **argv ) +{ + QFile inFile( "simple.xml" ); + if( !inFile.open( QIODevice::ReadOnly | QIODevice::Text ) ) + { + qDebug( "Failed to open file for reading." ); + return 0; + } + + QDomDocument document; + if( !document.setContent( &inFile ) ) + { + qDebug( "Failed to parse the file into a DOM tree." ); + inFile.close(); + return 0; + } + + inFile.close(); + + QDomElement documentElement = document.documentElement(); + QDomNodeList elements = documentElement.elementsByTagName( "bar" ); + if( elements.size() == 0 ) + { + QDomElement bar = document.createElement( "bar" ); + documentElement.insertBefore( bar, QDomNode() ); + } + else if( elements.size() == 1 ) + { + QDomElement bar = elements.at(0).toElement(); + + QDomElement baz = document.createElement( "baz" ); + baz.setAttribute( "count", QString::number( bar.elementsByTagName( "baz" ).size() + 1 ) ); + + bar.appendChild( baz ); + } + + QFile outFile( "simple-modified.xml" ); + if( !outFile.open( QIODevice::WriteOnly | QIODevice::Text ) ) + { + qDebug( "Failed to open file for writing." ); + return 0; + } + + QTextStream stream( &outFile ); + stream << document.toString(); + + outFile.close(); + + return 0; +} diff --git a/Chapter08/xmldommodify/simple-modified.xml b/Chapter08/xmldommodify/simple-modified.xml new file mode 100644 index 0000000..c97b7c0 --- /dev/null +++ b/Chapter08/xmldommodify/simple-modified.xml @@ -0,0 +1,6 @@ + + + + + +Some text diff --git a/Chapter08/xmldommodify/simple.xml b/Chapter08/xmldommodify/simple.xml new file mode 100644 index 0000000..c3c0068 --- /dev/null +++ b/Chapter08/xmldommodify/simple.xml @@ -0,0 +1,5 @@ + + + + +Some text diff --git a/Chapter08/xmldommodify/xmldommodify.pro b/Chapter08/xmldommodify/xmldommodify.pro new file mode 100644 index 0000000..b36870f --- /dev/null +++ b/Chapter08/xmldommodify/xmldommodify.pro @@ -0,0 +1,13 @@ +###################################################################### +# Automatically generated by qmake (2.01a) on 15. nov 19:10:54 2006 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp +QT += xml +CONFIG += console diff --git a/Chapter08/xmldomread/main.cpp b/Chapter08/xmldomread/main.cpp new file mode 100644 index 0000000..26216c5 --- /dev/null +++ b/Chapter08/xmldomread/main.cpp @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include + +#include +#include +#include + +#include + +int main( int argc, char **argv ) +{ + QFile file( "simple.xml" ); + if( !file.open( QIODevice::ReadOnly | QIODevice::Text ) ) + { + qDebug( "Failed to open file for reading." ); + return 0; + } + + QDomDocument document; + if( !document.setContent( &file ) ) + { + qDebug( "Failed to parse the file into a DOM tree." ); + file.close(); + return 0; + } + + file.close(); + + QDomElement documentElement = document.documentElement(); + + QDomNode node = documentElement.firstChild(); + while( !node.isNull() ) + { + if( node.isElement() ) + { + QDomElement element = node.toElement(); + qDebug() << "ELEMENT" << element.tagName(); + qDebug() << "ELEMENT ATTRIBUTE NAME" << element.attribute( "name", "not set" ); + } + + if( node.isText() ) + { + QDomText text = node.toText(); + qDebug() << text.data(); + } + + node = node.nextSibling(); + } + + return 0; +} diff --git a/Chapter08/xmldomread/simple.xml b/Chapter08/xmldomread/simple.xml new file mode 100644 index 0000000..df24d0f --- /dev/null +++ b/Chapter08/xmldomread/simple.xml @@ -0,0 +1,2 @@ + + Some text diff --git a/Chapter08/xmldomread/xmldomread.pro b/Chapter08/xmldomread/xmldomread.pro new file mode 100644 index 0000000..bcdd33b --- /dev/null +++ b/Chapter08/xmldomread/xmldomread.pro @@ -0,0 +1,13 @@ +###################################################################### +# Automatically generated by qmake (2.01a) on 15. nov 18:25:39 2006 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp +QT += xml +CONFIG += console diff --git a/Chapter08/xmldomwrite/main.cpp b/Chapter08/xmldomwrite/main.cpp new file mode 100644 index 0000000..0a92558 --- /dev/null +++ b/Chapter08/xmldomwrite/main.cpp @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include + +#include +#include +#include + +int main( int argc, char **argv ) +{ + QDomDocument document; + + QDomElement foo = document.createElement( "foo" ); + foo.setAttribute( "name", "FooName" ); + + QDomElement bar = document.createElement( "bar" ); + bar.setAttribute( "name", "BarName" ); + + QDomText text = document.createTextNode( "Some text" ); + + document.appendChild( foo ); + foo.appendChild( bar ); + foo.appendChild( text ); + + QFile file( "simple.xml" ); + if( !file.open( QIODevice::WriteOnly | QIODevice::Text ) ) + { + qDebug( "Failed to open file for writing." ); + return 0; + } + + QTextStream stream( &file ); + stream << document.toString(); + + file.close(); + + return 0; +} diff --git a/Chapter08/xmldomwrite/simple.xml b/Chapter08/xmldomwrite/simple.xml new file mode 100644 index 0000000..df24d0f --- /dev/null +++ b/Chapter08/xmldomwrite/simple.xml @@ -0,0 +1,2 @@ + + Some text diff --git a/Chapter08/xmldomwrite/xmldomwrite.pro b/Chapter08/xmldomwrite/xmldomwrite.pro new file mode 100644 index 0000000..c997573 --- /dev/null +++ b/Chapter08/xmldomwrite/xmldomwrite.pro @@ -0,0 +1,13 @@ +###################################################################### +# Automatically generated by qmake (2.01a) on 15. nov 16:40:53 2006 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp +QT += xml +CONFIG += console diff --git a/Chapter08/xmlsaxread/main.cpp b/Chapter08/xmlsaxread/main.cpp new file mode 100644 index 0000000..ac860d9 --- /dev/null +++ b/Chapter08/xmlsaxread/main.cpp @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include +#include +#include + +#include + +class MyHandler : public QXmlDefaultHandler +{ +public: + bool startDocument(); + bool endDocument(); + + bool startElement( const QString &namespaceURI, const QString &localName, const QString &qName, const QXmlAttributes &atts ); + bool endElement( const QString &namespaceURI, const QString &localName, const QString &qName ); + + bool characters( const QString &ch ); +}; + +bool MyHandler::startDocument() +{ + qDebug() << "Start of the document."; + return true; +} + +bool MyHandler::endDocument() +{ + qDebug() << "End of the document."; + return true; +} + +bool MyHandler::startElement( const QString &namespaceURI, const QString &localName, + const QString &qName, const QXmlAttributes &atts ) +{ + qDebug() << "Start of element" << qName; + for( int i=0; i + Some text diff --git a/Chapter08/xmlsaxread/xmlsaxread.pro b/Chapter08/xmlsaxread/xmlsaxread.pro new file mode 100644 index 0000000..3aa1d86 --- /dev/null +++ b/Chapter08/xmlsaxread/xmlsaxread.pro @@ -0,0 +1,13 @@ +###################################################################### +# Automatically generated by qmake (2.01a) on 15. nov 19:58:15 2006 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp +QT += xml +CONFIG += console diff --git a/Chapter09/README.txt b/Chapter09/README.txt new file mode 100644 index 0000000..9578f2d --- /dev/null +++ b/Chapter09/README.txt @@ -0,0 +1,53 @@ +This file is a part of 1590598318-1.zip containing example source code for the +Foundations of Qt Development book available from APress (ISBN 1590598318). + +These are the examples for chapter 9 - Providing Help + +tooltips + + Listings 9-1, 9-2, 9-3, 9-4 + + Shows tooltips with different formatting and embedded images. + + +tooltipzones + + Listing 9-5 + + Shows how to provide different tooltips for different parts of a single + widget. + + +whatsthis + + Not shown in any Listings, but Figures 9-6 and 9-7. + + Shows what's this help with different formatting and embedded images. + + +whatsthislink + + Listings 9-6, 9-7, 9-8, 9-9 + + Shows how to provide clickable links in your what's this help boxes. + + +statusbar + + Listings 9-10 + + Shows how to add different widgets to a status bar. + + +wizard + + Listings 9-11, 9-12, 9-13, 9-14, 9-15 + + Shows how to implement a wizard dialog. + + +assistant + + Listings 9-16, 9-17 + + Shows how to use Qt Assistant as a help browser. \ No newline at end of file diff --git a/Chapter09/assistant/assistant.pro b/Chapter09/assistant/assistant.pro new file mode 100644 index 0000000..3ba9d69 --- /dev/null +++ b/Chapter09/assistant/assistant.pro @@ -0,0 +1,13 @@ +###################################################################### +# Automatically generated by qmake (2.01a) ti 28. nov 19:41:06 2006 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp +CONFIG += assistant +TARGET = assist-demo diff --git a/Chapter09/assistant/documentation/about.txt b/Chapter09/assistant/documentation/about.txt new file mode 100644 index 0000000..3c5d885 --- /dev/null +++ b/Chapter09/assistant/documentation/about.txt @@ -0,0 +1,4 @@ +The Qt Book Example + +This is an example of how to modify +the appearance of Qt Assistant's about box. \ No newline at end of file diff --git a/Chapter09/assistant/documentation/adv1.html b/Chapter09/assistant/documentation/adv1.html new file mode 100644 index 0000000..9c62987 --- /dev/null +++ b/Chapter09/assistant/documentation/adv1.html @@ -0,0 +1,8 @@ + +Advanced #1 + +

Advanced #1

+

Vivamus non mauris. Pellentesque scelerisque velit at nibh. Cras nec enim. Vivamus hendrerit leo non justo. Integer accumsan cursus sapien. Curabitur a dui. Aliquam enim. Proin viverra nibh in metus. Aenean quis dolor. Nunc augue ligula, hendrerit non, sollicitudin non, elementum sed, lacus. Donec dictum purus sed purus. In et libero at eros malesuada rhoncus. Morbi hendrerit.

+ + + diff --git a/Chapter09/assistant/documentation/adv2.html b/Chapter09/assistant/documentation/adv2.html new file mode 100644 index 0000000..7969aa2 --- /dev/null +++ b/Chapter09/assistant/documentation/adv2.html @@ -0,0 +1,8 @@ + +Advanced #2 + +

Advanced #2

+

Vivamus non mauris. Pellentesque scelerisque velit at nibh. Cras nec enim. Vivamus hendrerit leo non justo. Integer accumsan cursus sapien. Curabitur a dui. Aliquam enim. Proin viverra nibh in metus. Aenean quis dolor. Nunc augue ligula, hendrerit non, sollicitudin non, elementum sed, lacus. Donec dictum purus sed purus. In et libero at eros malesuada rhoncus. Morbi hendrerit.

+ + + diff --git a/Chapter09/assistant/documentation/advanced.html b/Chapter09/assistant/documentation/advanced.html new file mode 100644 index 0000000..25ff73b --- /dev/null +++ b/Chapter09/assistant/documentation/advanced.html @@ -0,0 +1,8 @@ + +Advanced Topics + +

Advanced Topics

+

Vivamus non mauris. Pellentesque scelerisque velit at nibh. Cras nec enim. Vivamus hendrerit leo non justo. Integer accumsan cursus sapien. Curabitur a dui. Aliquam enim. Proin viverra nibh in metus. Aenean quis dolor. Nunc augue ligula, hendrerit non, sollicitudin non, elementum sed, lacus. Donec dictum purus sed purus. In et libero at eros malesuada rhoncus. Morbi hendrerit.

+ + + diff --git a/Chapter09/assistant/documentation/appendix.html b/Chapter09/assistant/documentation/appendix.html new file mode 100644 index 0000000..412cfff --- /dev/null +++ b/Chapter09/assistant/documentation/appendix.html @@ -0,0 +1,8 @@ + +Appendix + +

Appendix

+

Curabitur sollicitudin nonummy augue. Nunc et elit id nibh egestas vestibulum. Proin pharetra. Morbi neque. Mauris non erat. Sed interdum tellus sit amet diam. Morbi rhoncus mattis ligula. Ut varius eleifend elit. Phasellus placerat imperdiet tellus. Donec at magna. Nullam eget lorem. Quisque augue augue, sollicitudin ut, pretium nec, placerat id, nisl. Aliquam erat volutpat. Proin nec diam. Duis ut lorem in libero dapibus semper.

+ + + diff --git a/Chapter09/assistant/documentation/basics.html b/Chapter09/assistant/documentation/basics.html new file mode 100644 index 0000000..44acb4b --- /dev/null +++ b/Chapter09/assistant/documentation/basics.html @@ -0,0 +1,8 @@ + +Some Basic Stuff + +

Some Basic Stuff

+

Vivamus non mauris. Pellentesque scelerisque velit at nibh. Cras nec enim. Vivamus hendrerit leo non justo. Integer accumsan cursus sapien. Curabitur a dui. Aliquam enim. Proin viverra nibh in metus. Aenean quis dolor. Nunc augue ligula, hendrerit non, sollicitudin non, elementum sed, lacus. Donec dictum purus sed purus. In et libero at eros malesuada rhoncus. Morbi hendrerit.

+ + + diff --git a/Chapter09/assistant/documentation/easystuff.html b/Chapter09/assistant/documentation/easystuff.html new file mode 100644 index 0000000..285396d --- /dev/null +++ b/Chapter09/assistant/documentation/easystuff.html @@ -0,0 +1,8 @@ + +Another Very Easy Thingie + +

Another Very Easy Thingie

+

Vivamus non mauris. Pellentesque scelerisque velit at nibh. Cras nec enim. Vivamus hendrerit leo non justo. Integer accumsan cursus sapien. Curabitur a dui. Aliquam enim. Proin viverra nibh in metus. Aenean quis dolor. Nunc augue ligula, hendrerit non, sollicitudin non, elementum sed, lacus. Donec dictum purus sed purus. In et libero at eros malesuada rhoncus. Morbi hendrerit.

+ + + diff --git a/Chapter09/assistant/documentation/faq.html b/Chapter09/assistant/documentation/faq.html new file mode 100644 index 0000000..01b3798 --- /dev/null +++ b/Chapter09/assistant/documentation/faq.html @@ -0,0 +1,13 @@ + +F.A.Q. + +

Frequently Asked Questions

+ +

What is foo?

+

Pellentesque eget mi. Phasellus tincidunt pretium massa. Curabitur aliquet arcu vitae neque. Pellentesque mollis tristique enim. Curabitur sed sapien eu justo rhoncus adipiscing. Morbi lectus mauris, accumsan non, eleifend nec, consequat ut, diam. Duis consectetuer libero quis augue. Fusce quis est. Curabitur tempus. Quisque tincidunt aliquam arcu. Nam pellentesque dolor vitae ipsum. Nunc sed lacus. Aliquam in turpis. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos. Aliquam pellentesque quam a eros.

+ +

Why do bar?

+

Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Aliquam sollicitudin. Duis nisl lacus, malesuada vitae, semper a, lacinia a, erat. Etiam scelerisque. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Maecenas convallis commodo nulla. Nam ultrices porttitor sem. Donec rutrum. Aliquam dictum, nunc eu euismod molestie, augue nunc vulputate magna, ut imperdiet quam enim vel eros. Pellentesque pede tortor, luctus vel, pretium eu, porttitor at, est. Nunc tortor mauris, ullamcorper eget, vulputate eu, elementum vel, pede. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Integer blandit rhoncus augue. Suspendisse vestibulum, dolor pulvinar porta porttitor, erat nunc ornare arcu, a rhoncus turpis justo at erat. In hac habitasse platea dictumst. Aenean in urna.

+ + + diff --git a/Chapter09/assistant/documentation/images/qt.png b/Chapter09/assistant/documentation/images/qt.png new file mode 100644 index 0000000..a616ea0 Binary files /dev/null and b/Chapter09/assistant/documentation/images/qt.png differ diff --git a/Chapter09/assistant/documentation/index.html b/Chapter09/assistant/documentation/index.html new file mode 100644 index 0000000..9325f43 --- /dev/null +++ b/Chapter09/assistant/documentation/index.html @@ -0,0 +1,14 @@ + +A Qt Book Example + +

A Qt Book Example

+

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Pellentesque consequat ipsum in est. Donec tincidunt sem ac ipsum. Praesent tempus lacus non massa. Nullam consectetuer dictum massa. Nunc ac nibh eu lectus ultrices malesuada. Quisque a sapien eget ante fringilla elementum. Sed facilisis justo sit amet magna eleifend tempus. Nullam vel magna. Pellentesque euismod mauris et dolor. Nullam nisl quam, eleifend a, lacinia id, lacinia vitae, justo. Morbi congue consectetuer mi. Morbi aliquet felis eget ipsum. Sed eu velit pretium ligula consequat vehicula. Aliquam aliquet nunc ut metus. Donec sem mi, pretium nec, facilisis imperdiet, rutrum ac, arcu. Ut mauris quam, iaculis ac, congue ut, scelerisque quis, pede. Ut est risus, pulvinar id, condimentum eu, tempus sit amet, augue. Praesent eget metus sit amet lacus cursus vulputate. Fusce lacinia elementum nulla. Pellentesque dictum vehicula diam.

+ +

Easy Thingie #1

+

Pellentesque eget mi. Phasellus tincidunt pretium massa. Curabitur aliquet arcu vitae neque. Pellentesque mollis tristique enim. Curabitur sed sapien eu justo rhoncus adipiscing. Morbi lectus mauris, accumsan non, eleifend nec, consequat ut, diam. Duis consectetuer libero quis augue. Fusce quis est. Curabitur tempus. Quisque tincidunt aliquam arcu. Nam pellentesque dolor vitae ipsum. Nunc sed lacus. Aliquam in turpis. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos. Aliquam pellentesque quam a eros.

+ +

Easy Thingie #2

+

Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Aliquam sollicitudin. Duis nisl lacus, malesuada vitae, semper a, lacinia a, erat. Etiam scelerisque. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Maecenas convallis commodo nulla. Nam ultrices porttitor sem. Donec rutrum. Aliquam dictum, nunc eu euismod molestie, augue nunc vulputate magna, ut imperdiet quam enim vel eros. Pellentesque pede tortor, luctus vel, pretium eu, porttitor at, est. Nunc tortor mauris, ullamcorper eget, vulputate eu, elementum vel, pede. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Integer blandit rhoncus augue. Suspendisse vestibulum, dolor pulvinar porta porttitor, erat nunc ornare arcu, a rhoncus turpis justo at erat. In hac habitasse platea dictumst. Aenean in urna.

+ + + diff --git a/Chapter09/assistant/documentation/qtbookexample.adp b/Chapter09/assistant/documentation/qtbookexample.adp new file mode 100644 index 0000000..8951d40 --- /dev/null +++ b/Chapter09/assistant/documentation/qtbookexample.adp @@ -0,0 +1,37 @@ + + + + + + qtbookexample + Qt Book Example + images/qt.png + index.html + About The Qt Book Example + about.txt + . + + + +
+
+
+
+ + Basic Thing One + Basic Thing Two + Another Basic Thing +
+
+
+
+ + Advanced Topic One + Advanced Topic Two +
+ +
+
+ + + \ No newline at end of file diff --git a/Chapter09/assistant/main.cpp b/Chapter09/assistant/main.cpp new file mode 100644 index 0000000..70bb51f --- /dev/null +++ b/Chapter09/assistant/main.cpp @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include + +#include + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + QAssistantClient *assistantClient = new QAssistantClient( QApplication::applicationDirPath(), qApp ); + QStringList arguments; + arguments << "-profile" << "./documentation/qtbookexample.adp"; + assistantClient->setArguments( arguments ); + + assistantClient->openAssistant(); + + QLabel label( "Close me" ); + label.show(); + + int res = app.exec(); + + assistantClient->closeAssistant(); + + return res; +} diff --git a/Chapter09/statusbar/main.cpp b/Chapter09/statusbar/main.cpp new file mode 100644 index 0000000..0b927f2 --- /dev/null +++ b/Chapter09/statusbar/main.cpp @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include +#include + +#include + +#include +#include + +class MainWindow : public QMainWindow +{ +public: + MainWindow(); +}; + +MainWindow::MainWindow() : QMainWindow() +{ + setCentralWidget( new QTextEdit( this ) ); + + QStatusBar *statusBar = this->statusBar(); + + QProgressBar *progressBar = new QProgressBar(); + QLabel *mode = new QLabel( tr(" EDIT ") ); + QLabel *modified = new QLabel( tr(" Y ") ); + QLabel *size = new QLabel( tr(" 999999kB ") ); + + mode->setMinimumSize( mode->sizeHint() ); + mode->setAlignment( Qt::AlignCenter ); + mode->setText( tr("EDIT") ); + mode->setToolTip( tr("The current working mode.") ); + + statusBar->addPermanentWidget( mode ); + + modified->setMinimumSize( modified->sizeHint() ); + modified->setAlignment( Qt::AlignCenter ); + modified->setText( tr("N") ); + modified->setToolTip( tr("Indicates if the current document has been modified or not.") ); + + size->setMinimumSize( size->sizeHint() ); + size->setAlignment( Qt::AlignRight | Qt::AlignVCenter ); + size->setText( tr("%1kB ").arg(0) ); + size->setToolTip( tr("The memory used for the current document.") ); + + progressBar->setTextVisible( false ); + progressBar->setRange( 0, 0 ); + + statusBar->addWidget( progressBar, 1 ); + statusBar->addWidget( modified ); + statusBar->addWidget( size ); + + statusBar->showMessage( tr("Ready"), 2000 ); +} + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + + MainWindow window; + window.show(); + + return app.exec(); +} diff --git a/Chapter09/statusbar/statusbar.pro b/Chapter09/statusbar/statusbar.pro new file mode 100644 index 0000000..ec3db2e --- /dev/null +++ b/Chapter09/statusbar/statusbar.pro @@ -0,0 +1,11 @@ +###################################################################### +# Automatically generated by qmake (2.01a) on 22. nov 13:46:01 2006 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp diff --git a/Chapter09/tooltips/images.qrc b/Chapter09/tooltips/images.qrc new file mode 100644 index 0000000..d50c25b --- /dev/null +++ b/Chapter09/tooltips/images.qrc @@ -0,0 +1,5 @@ + + + images/qt.png + + \ No newline at end of file diff --git a/Chapter09/tooltips/images/qt.png b/Chapter09/tooltips/images/qt.png new file mode 100644 index 0000000..a616ea0 Binary files /dev/null and b/Chapter09/tooltips/images/qt.png differ diff --git a/Chapter09/tooltips/main.cpp b/Chapter09/tooltips/main.cpp new file mode 100644 index 0000000..58a9a63 --- /dev/null +++ b/Chapter09/tooltips/main.cpp @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include "tooltipdialog.h" + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + ToolTipDialog dlg; + dlg.show(); + + return app.exec(); +} diff --git a/Chapter09/tooltips/tooltipdialog.cpp b/Chapter09/tooltips/tooltipdialog.cpp new file mode 100644 index 0000000..531b520 --- /dev/null +++ b/Chapter09/tooltips/tooltipdialog.cpp @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include +#include +#include + +#include + +#include "tooltipdialog.h" + +ToolTipDialog::ToolTipDialog() : QDialog() +{ + QGroupBox *groupBox = new QGroupBox( tr("Group") ); + QGridLayout *gbLayout = new QGridLayout( groupBox ); + + QCheckBox *checkBox = new QCheckBox( tr("Check!") ); + QLabel *label = new QLabel( tr("label") ); + QPushButton *pushButton = new QPushButton( tr("Push me!") ); + + gbLayout->addWidget( checkBox, 0, 0 ); + gbLayout->addWidget( label, 0, 1 ); + gbLayout->addWidget( pushButton, 1, 0, 1, 2 ); + + QGridLayout *dlgLayout = new QGridLayout( this ); + dlgLayout->addWidget( groupBox, 0, 0 ); + + checkBox->setToolTip( tr("This is a simple tool tip for the check box.") ); + groupBox->setToolTip( tr("This is a group box tool tip.\n" + "Notice that it appears between and around the contained widgets.\n" + "It is also spanning several lines.") ); + label->setToolTip( tr("

It is possible to do lists.

" + "
    " + "
  • You can format text.
  • " + "
  • Bold is possible too.
  • " + "
  • And the color and " + "size.
  • " + "
" + "

You can do ordered lists as well.

" + "
    " + "
  1. First.
  2. " + "
  3. Second.
  4. " + "
  5. Third.
  6. " + "
") ); + pushButton->setToolTip( tr("" + "You can also insert images into your tool tips.") ); +} diff --git a/Chapter09/tooltips/tooltipdialog.h b/Chapter09/tooltips/tooltipdialog.h new file mode 100644 index 0000000..61034ec --- /dev/null +++ b/Chapter09/tooltips/tooltipdialog.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef TOOLTIPDIALOG_H +#define TOOLTIPDIALOG_H + +#include + +class ToolTipDialog : public QDialog +{ +public: + ToolTipDialog(); + +}; + +#endif // TOOLTIPDIALOG_H diff --git a/Chapter09/tooltips/tooltips.pro b/Chapter09/tooltips/tooltips.pro new file mode 100644 index 0000000..06b265c --- /dev/null +++ b/Chapter09/tooltips/tooltips.pro @@ -0,0 +1,13 @@ +###################################################################### +# Automatically generated by qmake (2.01a) ti 21. nov 21:01:45 2006 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +HEADERS += tooltipdialog.h +SOURCES += main.cpp tooltipdialog.cpp +RESOURCES += images.qrc diff --git a/Chapter09/tooltipzones/main.cpp b/Chapter09/tooltipzones/main.cpp new file mode 100644 index 0000000..f7f4e20 --- /dev/null +++ b/Chapter09/tooltipzones/main.cpp @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include "tipzones.h" + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + TipZones tipZones; + tipZones.show(); + + return app.exec(); +} diff --git a/Chapter09/tooltipzones/tipzones.cpp b/Chapter09/tooltipzones/tipzones.cpp new file mode 100644 index 0000000..2609f16 --- /dev/null +++ b/Chapter09/tooltipzones/tipzones.cpp @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include +#include +#include + +#include "tipzones.h" + +TipZones::TipZones() : QWidget() +{ +} + +void TipZones::paintEvent( QPaintEvent* ) +{ + QRect redRect, greenRect, blueRect, yellowRect; + + redRect = QRect( 0, 0, width()/2, height()/2 ); + greenRect = QRect( width()/2, 0, width()/2, height()/2 ); + blueRect = QRect( 0, height()/2, width()/2, height()/2 ); + yellowRect = QRect( width()/2, height()/2, width()/2, height()/2 ); + + QPainter p( this ); + + p.setPen( Qt::black ); + + p.setBrush( Qt::red ); + p.drawRect( redRect ); + + p.setBrush( Qt::green ); + p.drawRect( greenRect ); + + p.setBrush( Qt::blue ); + p.drawRect( blueRect ); + + p.setBrush( Qt::yellow ); + p.drawRect( yellowRect ); +} + +bool TipZones::event( QEvent *event ) +{ + if( event->type() == QEvent::ToolTip ) + { + QHelpEvent *helpEvent = static_cast( event ); + + QRect redRect, greenRect, blueRect, yellowRect; + + redRect = QRect( 0, 0, width()/2, height()/2 ); + greenRect = QRect( width()/2, 0, width()/2, height()/2 ); + blueRect = QRect( 0, height()/2, width()/2, height()/2 ); + yellowRect = QRect( width()/2, height()/2, width()/2, height()/2 ); + + if( redRect.contains( helpEvent->pos() ) ) + setToolTip( tr("Red") ); + else if( greenRect.contains( helpEvent->pos() ) ) + setToolTip( tr("Green") ); + else if( blueRect.contains( helpEvent->pos() ) ) + setToolTip( tr("Blue") ); + else + setToolTip( tr("Yellow") ); + } + + return QWidget::event( event ); +} diff --git a/Chapter09/tooltipzones/tipzones.h b/Chapter09/tooltipzones/tipzones.h new file mode 100644 index 0000000..942a7f4 --- /dev/null +++ b/Chapter09/tooltipzones/tipzones.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef TIPZONES_H +#define TIPZONES_H + +#include + +class TipZones : public QWidget +{ +public: + TipZones(); + +protected: + void paintEvent( QPaintEvent* ); + bool event( QEvent* ); +}; + +#endif // TIPZONES_H diff --git a/Chapter09/tooltipzones/tooltipzones.pro b/Chapter09/tooltipzones/tooltipzones.pro new file mode 100644 index 0000000..db1fb23 --- /dev/null +++ b/Chapter09/tooltipzones/tooltipzones.pro @@ -0,0 +1,12 @@ +###################################################################### +# Automatically generated by qmake (2.01a) on 22. nov 09:16:19 2006 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +HEADERS += tipzones.h +SOURCES += main.cpp tipzones.cpp diff --git a/Chapter09/whatsthis/images.qrc b/Chapter09/whatsthis/images.qrc new file mode 100644 index 0000000..d50c25b --- /dev/null +++ b/Chapter09/whatsthis/images.qrc @@ -0,0 +1,5 @@ + + + images/qt.png + + \ No newline at end of file diff --git a/Chapter09/whatsthis/images/qt.png b/Chapter09/whatsthis/images/qt.png new file mode 100644 index 0000000..a616ea0 Binary files /dev/null and b/Chapter09/whatsthis/images/qt.png differ diff --git a/Chapter09/whatsthis/main.cpp b/Chapter09/whatsthis/main.cpp new file mode 100644 index 0000000..9a4bdad --- /dev/null +++ b/Chapter09/whatsthis/main.cpp @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include "whatsthisdialog.h" + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + WhatsThisDialog dlg; + dlg.show(); + + return app.exec(); +} diff --git a/Chapter09/whatsthis/whatsthis.pro b/Chapter09/whatsthis/whatsthis.pro new file mode 100644 index 0000000..e738290 --- /dev/null +++ b/Chapter09/whatsthis/whatsthis.pro @@ -0,0 +1,13 @@ +###################################################################### +# Automatically generated by qmake (2.01a) on 22. nov 09:24:52 2006 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +HEADERS += whatsthisdialog.h +SOURCES += main.cpp whatsthisdialog.cpp +RESOURCES += images.qrc diff --git a/Chapter09/whatsthis/whatsthisdialog.cpp b/Chapter09/whatsthis/whatsthisdialog.cpp new file mode 100644 index 0000000..587cfd6 --- /dev/null +++ b/Chapter09/whatsthis/whatsthisdialog.cpp @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include +#include +#include + +#include + +#include "whatsthisdialog.h" + +WhatsThisDialog::WhatsThisDialog() : QDialog() +{ + QGroupBox *groupBox = new QGroupBox( tr("Group") ); + QGridLayout *gbLayout = new QGridLayout( groupBox ); + + QCheckBox *checkBox = new QCheckBox( tr("Check!") ); + QLabel *label = new QLabel( tr("label") ); + QPushButton *pushButton = new QPushButton( tr("Push me!") ); + + gbLayout->addWidget( checkBox, 0, 0 ); + gbLayout->addWidget( label, 0, 1 ); + gbLayout->addWidget( pushButton, 1, 0, 1, 2 ); + + QGridLayout *dlgLayout = new QGridLayout( this ); + + dlgLayout->addWidget( groupBox, 0, 0 ); + +#ifndef true + checkBox->setWhatsThis( tr("This is a simple What's This help for the check box.") ); +#endif + checkBox->setWhatsThis( tr("

This is a simple What's This help " + "for the check box.

") ); + groupBox->setWhatsThis( tr("This is a group box What's This help.
" + "Notice that it appears between and around the contained widgets.
" + "It is also spanning several lines.") ); + label->setWhatsThis( tr("

It is possible to do lists.

" + "
    " + "
  • You can format text.
  • " + "
  • Bold is possible too.
  • " + "
  • And the color and " + "size.
  • " + "
" + "

You can do ordered lists as well.

" + "
    " + "
  1. First.
  2. " + "
  3. Second.
  4. " + "
  5. Third.
  6. " + "
") ); + pushButton->setWhatsThis( tr("" + "You can also insert images into your What's This help.") ); +} diff --git a/Chapter09/whatsthis/whatsthisdialog.h b/Chapter09/whatsthis/whatsthisdialog.h new file mode 100644 index 0000000..3412b88 --- /dev/null +++ b/Chapter09/whatsthis/whatsthisdialog.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef WHATSTHISDIALOG_H +#define WHATSTHISDIALOG_H + +#include + +class WhatsThisDialog : public QDialog +{ +public: + WhatsThisDialog(); + +}; + +#endif // WHATSTHISDIALOG_H diff --git a/Chapter09/whatsthislink/linkdialog.cpp b/Chapter09/whatsthislink/linkdialog.cpp new file mode 100644 index 0000000..b95179f --- /dev/null +++ b/Chapter09/whatsthislink/linkdialog.cpp @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include + +#include + +#include "linkdialog.h" + +#include "linkfilter.h" + +LinkDialog::LinkDialog() : QDialog() +{ + LinkFilter *filter = new LinkFilter( this ); + this->installEventFilter( filter ); + connect( filter, SIGNAL(linkClicked(QString)), this, SLOT(showLink(QString)) ); + + QPushButton *button = new QPushButton( "What is this?" ); + button->setWhatsThis( "This is a test link." ); + + QGridLayout *layout = new QGridLayout( this ); + layout->addWidget( button, 0, 0 ); +} + +void LinkDialog::showLink( QString link ) +{ + QMessageBox::information( this, tr("Link Clicked"), tr("Link: %1").arg( link ) ); +} diff --git a/Chapter09/whatsthislink/linkdialog.h b/Chapter09/whatsthislink/linkdialog.h new file mode 100644 index 0000000..85c4a5b --- /dev/null +++ b/Chapter09/whatsthislink/linkdialog.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef LINKDIALOG_H +#define LINKDIALOG_H + +#include + +class LinkDialog : public QDialog +{ + Q_OBJECT + +public: + LinkDialog(); + +private slots: + void showLink( QString ); +}; + +#endif // LINKDIALOG_H diff --git a/Chapter09/whatsthislink/linkfilter.cpp b/Chapter09/whatsthislink/linkfilter.cpp new file mode 100644 index 0000000..137af80 --- /dev/null +++ b/Chapter09/whatsthislink/linkfilter.cpp @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include +#include + +#include "linkfilter.h" + +LinkFilter::LinkFilter( QObject *parent ) : QObject( parent ) +{ +} + +bool LinkFilter::eventFilter( QObject *object, QEvent *event ) +{ + if( event->type() == QEvent::WhatsThisClicked ) + { + QWhatsThisClickedEvent *wtcEvent = static_cast(event); + QWhatsThis::hideText(); + emit linkClicked( wtcEvent->href() ); + return true; + } + + return false; +} diff --git a/Chapter09/whatsthislink/linkfilter.h b/Chapter09/whatsthislink/linkfilter.h new file mode 100644 index 0000000..1bf960c --- /dev/null +++ b/Chapter09/whatsthislink/linkfilter.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef LINKFILTER_H +#define LINKFILTER_H + +#include + +class LinkFilter : public QObject +{ + Q_OBJECT + +public: + LinkFilter( QObject *parent=0 ); + +signals: + void linkClicked( QString ); + +protected: + bool eventFilter( QObject*, QEvent* ); +}; + +#endif // LINKFILTER_H diff --git a/Chapter09/whatsthislink/main.cpp b/Chapter09/whatsthislink/main.cpp new file mode 100644 index 0000000..4c83b6a --- /dev/null +++ b/Chapter09/whatsthislink/main.cpp @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include "linkdialog.h" + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + LinkDialog dlg; + dlg.show(); + + return app.exec(); +} diff --git a/Chapter09/whatsthislink/whatsthislink.pro b/Chapter09/whatsthislink/whatsthislink.pro new file mode 100644 index 0000000..d1ce4dc --- /dev/null +++ b/Chapter09/whatsthislink/whatsthislink.pro @@ -0,0 +1,12 @@ +###################################################################### +# Automatically generated by qmake (2.01a) on 22. nov 10:36:59 2006 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +HEADERS += linkdialog.h linkfilter.h +SOURCES += linkdialog.cpp linkfilter.cpp main.cpp diff --git a/Chapter09/wizard/main.cpp b/Chapter09/wizard/main.cpp new file mode 100644 index 0000000..5cc9346 --- /dev/null +++ b/Chapter09/wizard/main.cpp @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include "wizard.h" + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + Wizard dlg; + dlg.show(); + + return app.exec(); +} \ No newline at end of file diff --git a/Chapter09/wizard/wizard.cpp b/Chapter09/wizard/wizard.cpp new file mode 100644 index 0000000..75a7501 --- /dev/null +++ b/Chapter09/wizard/wizard.cpp @@ -0,0 +1,185 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include +#include +#include +#include +#include + +#include + +#include + +#include "wizard.h" + +class PageOne : public QWidget +{ +public: + PageOne( QWidget *parent = 0 ); + + QCheckBox *acceptDeal; +}; + +PageOne::PageOne( QWidget *parent ) : QWidget(parent) +{ + QGridLayout *layout = new QGridLayout( this ); + + QTextEdit *textEdit = new QTextEdit(); + textEdit->setReadOnly( true ); + textEdit->setHtml( tr("

The Rules

" + "

The rules are to be followed!

") ); + + acceptDeal = new QCheckBox( tr("I accept") ); + + layout->addWidget( textEdit, 0, 0, 1, 2 ); + layout->addWidget( acceptDeal, 1, 1 ); +} + + + +class PageTwo : public QWidget +{ +public: + PageTwo( QWidget *parent = 0 ); + + QCheckBox *doThis; + QCheckBox *doThat; + QCheckBox *extra; +}; + +PageTwo::PageTwo( QWidget *parent ) : QWidget(parent) +{ + QGridLayout *layout = new QGridLayout( this ); + + doThis = new QCheckBox( tr("Do this") ); + doThat = new QCheckBox( tr("Do that") ); + extra = new QCheckBox( tr("Add something extra") ); + + layout->addWidget( doThis, 0, 0 ); + layout->addWidget( doThat, 1, 0 ); + layout->addWidget( extra, 2, 0 ); + + layout->setRowMinimumHeight( 3, 0 ); +} + + + +class PageThree : public QWidget +{ +public: + PageThree( QWidget *parent = 0 ); +}; + +PageThree::PageThree( QWidget *parent ) : QWidget(parent) +{ + QGridLayout *layout = new QGridLayout( this ); + + layout->addWidget( new QLabel( tr("All is ready. Press finish to get it done!") ), 0, 0 ); +} + + + +Wizard::Wizard() : QDialog() +{ + QGridLayout *layout = new QGridLayout( this ); + + QPushButton *cancel = new QPushButton( tr("Cancel") ); + next = new QPushButton( tr("Next") ); + previous = new QPushButton( tr("Previous" ) ); + + pages = new QStackedWidget(); + + layout->addWidget( pages, 0, 0, 1, 5 ); + layout->setColumnMinimumWidth( 0, 50 ); + layout->addWidget( previous, 1, 1 ); + layout->addWidget( next, 1, 2 ); + layout->setColumnMinimumWidth( 3, 5 ); + layout->addWidget( cancel, 1, 4 ); + + previous->setEnabled( false ); + next->setEnabled( false ); + + connect( next, SIGNAL(clicked()), this, SLOT(doNext()) ); + connect( previous, SIGNAL(clicked()), this, SLOT(doPrev()) ); + connect( cancel, SIGNAL(clicked()), this, SLOT(reject()) ); + + pages->addWidget( pageOne = new PageOne( pages ) ); + pages->addWidget( pageTwo = new PageTwo( pages ) ); + pages->addWidget( pageThree = new PageThree( pages ) ); + + connect( pageOne->acceptDeal, SIGNAL(toggled(bool)), next, SLOT(setEnabled(bool)) ); +} + +void Wizard::doNext() +{ + switch( pages->currentIndex() ) + { + case 0: + previous->setEnabled( true ); + + disconnect( pageOne->acceptDeal, SIGNAL(toggled(bool)), next, SLOT(setEnabled(bool)) ); + + break; + case 1: + next->setText( tr("Finish") ); + + break; + case 2: + QMessageBox::information( this, tr("Finishing"), tr("Here is where the action takes place.") ); + accept(); + + return; + } + + pages->setCurrentIndex( pages->currentIndex()+1 ); +} + +void Wizard::doPrev() +{ + switch( pages->currentIndex() ) + { + case 1: + previous->setEnabled( false ); + next->setEnabled( pageOne->acceptDeal->isChecked() ); + + connect( pageOne->acceptDeal, SIGNAL(toggled(bool)), next, SLOT(setEnabled(bool)) ); + + break; + case 2: + next->setText( tr("Next") ); + + break; + } + + pages->setCurrentIndex( pages->currentIndex()-1 ); +} diff --git a/Chapter09/wizard/wizard.h b/Chapter09/wizard/wizard.h new file mode 100644 index 0000000..056def5 --- /dev/null +++ b/Chapter09/wizard/wizard.h @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef WIZARD_H +#define WIZARD_H + +#include + +class QPushButton; +class QStackedWidget; + +class PageOne; +class PageTwo; +class PageThree; + +class Wizard : public QDialog +{ + Q_OBJECT + +public: + Wizard(); + +private slots: + void doNext(); + void doPrev(); + +private: + QPushButton *next; + QPushButton *previous; + + QStackedWidget *pages; + + PageOne *pageOne; + PageTwo *pageTwo; + PageThree *pageThree; +}; + +#endif // WIZARD_H diff --git a/Chapter09/wizard/wizard.pro b/Chapter09/wizard/wizard.pro new file mode 100644 index 0000000..6eb6efa --- /dev/null +++ b/Chapter09/wizard/wizard.pro @@ -0,0 +1,13 @@ +###################################################################### +# Automatically generated by qmake (2.01a) ti 28. nov 15:19:13 2006 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +HEADERS += wizard.h +SOURCES += main.cpp wizard.cpp +CONFIG += console diff --git a/Chapter10/README.txt b/Chapter10/README.txt new file mode 100644 index 0000000..07d92bf --- /dev/null +++ b/Chapter10/README.txt @@ -0,0 +1,31 @@ +This file is a part of 1590598318-1.zip containing example source code for the +Foundations of Qt Development book available from APress (ISBN 1590598318). + +These are the examples for chapter 10 - Internationalization and Localization + +sdi + + Listings 10-1, 10-2, 10-3 + + Translates the SDI application from chapter four. + + +noop + + Listing 10-4 + + Shows how to make strings outside tr() macros available to Linguist. + + +dynamic + + Listings 10-8, 10-9, 10-10, 10-11, 10-12 + + Shows how to provide dynamic language switches. + + +locales + + Listings 10-13, 10-15, 10-17 + + Shows how locale settings affects numbers, times and dates. diff --git a/Chapter10/dynamic/dynamic.pro b/Chapter10/dynamic/dynamic.pro new file mode 100644 index 0000000..4bd94ae --- /dev/null +++ b/Chapter10/dynamic/dynamic.pro @@ -0,0 +1,14 @@ +###################################################################### +# Automatically generated by qmake (2.01a) on 6. dec 17:48:46 2006 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +HEADERS += dyndialog.h +SOURCES += dyndialog.cpp main.cpp +TRANSLATIONS += swedish.ts english.ts +CONFIG += console diff --git a/Chapter10/dynamic/dyndialog.cpp b/Chapter10/dynamic/dyndialog.cpp new file mode 100644 index 0000000..9e5a3f3 --- /dev/null +++ b/Chapter10/dynamic/dyndialog.cpp @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include +#include + +#include + +#include + +#include "dyndialog.h" + +extern QTranslator *qTranslator; + +DynDialog::DynDialog() : QDialog( 0 ) +{ + languages = new QGroupBox( this ); + english = new QRadioButton( this ); + swedish = new QRadioButton( this ); + + english->setChecked( true ); + qTranslator->load( "english" ); + + QVBoxLayout *baseLayout = new QVBoxLayout( this ); + baseLayout->addWidget( languages ); + + QVBoxLayout *radioLayout = new QVBoxLayout( languages ); + radioLayout->addWidget( english ); + radioLayout->addWidget( swedish ); + + connect( english, SIGNAL(toggled(bool)), this, SLOT(languageChanged()) ); + connect( swedish, SIGNAL(toggled(bool)), this, SLOT(languageChanged()) ); + + translateUi(); +} + +void DynDialog::changeEvent( QEvent *event ) +{ + if( event->type() == QEvent::LanguageChange ) + { + translateUi(); + } + else + QDialog::changeEvent( event ); +} + +void DynDialog::languageChanged() +{ + if( english->isChecked() ) + qTranslator->load( "english" ); + else + qTranslator->load( "swedish" ); +} + +void DynDialog::translateUi() +{ + languages->setTitle( tr("Languages") ); + + english->setText( tr("English") ); + swedish->setText( tr("Swedish") ); +} diff --git a/Chapter10/dynamic/dyndialog.h b/Chapter10/dynamic/dyndialog.h new file mode 100644 index 0000000..bfafadc --- /dev/null +++ b/Chapter10/dynamic/dyndialog.h @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef DYNDIALOG_H +#define DYNDIALOG_H + +#include + +class QRadioButton; +class QGroupBox; + +class DynDialog : public QDialog +{ + Q_OBJECT + +public: + DynDialog(); + +protected: + void changeEvent( QEvent* ); + +private slots: + void languageChanged(); + +private: + void translateUi(); + + QGroupBox *languages; + + QRadioButton *english; + QRadioButton *swedish; +}; + +#endif // DYNDIALOG_H diff --git a/Chapter10/dynamic/english.ts b/Chapter10/dynamic/english.ts new file mode 100644 index 0000000..b3e5382 --- /dev/null +++ b/Chapter10/dynamic/english.ts @@ -0,0 +1,21 @@ + + + + DynDialog + + + Languages + Languages + + + + English + English + + + + Swedish + Swedish + + + diff --git a/Chapter10/dynamic/main.cpp b/Chapter10/dynamic/main.cpp new file mode 100644 index 0000000..91419c8 --- /dev/null +++ b/Chapter10/dynamic/main.cpp @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include +#include "dyndialog.h" + +QTranslator *qTranslator; + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + qTranslator = new QTranslator(); + app.installTranslator( qTranslator ); + + DynDialog dlg; + dlg.show(); + + return app.exec(); +} diff --git a/Chapter10/dynamic/swedish.qm b/Chapter10/dynamic/swedish.qm new file mode 100644 index 0000000..00fcac7 Binary files /dev/null and b/Chapter10/dynamic/swedish.qm differ diff --git a/Chapter10/dynamic/swedish.ts b/Chapter10/dynamic/swedish.ts new file mode 100644 index 0000000..70d2d80 --- /dev/null +++ b/Chapter10/dynamic/swedish.ts @@ -0,0 +1,21 @@ + + + + DynDialog + + + Languages + SprÃ¥k + + + + English + Engelska + + + + Swedish + Svenska + + + diff --git a/Chapter10/locales/locales.pro b/Chapter10/locales/locales.pro new file mode 100644 index 0000000..36cfce0 --- /dev/null +++ b/Chapter10/locales/locales.pro @@ -0,0 +1,12 @@ +###################################################################### +# Automatically generated by qmake (2.01a) ti 5. dec 18:59:42 2006 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp +CONFIG += console diff --git a/Chapter10/locales/main.cpp b/Chapter10/locales/main.cpp new file mode 100644 index 0000000..8e4ff47 --- /dev/null +++ b/Chapter10/locales/main.cpp @@ -0,0 +1,124 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include + +#include +#include +#include + +#include + +void printDates( QLocale loc ) +{ + QLocale::setDefault( loc ); + + QDate d1( 2006, 10, 12 ); + QDate d2( 2006, 01, 31 ); + QDate d3( 2006, 06, 06 ); + + qDebug() << "short"; + qDebug() << loc.toString( d1, QLocale::ShortFormat ); + qDebug() << loc.toString( d2, QLocale::ShortFormat ); + qDebug() << loc.toString( d3, QLocale::ShortFormat ); + + qDebug() << "long"; + qDebug() << loc.toString( d1, QLocale::LongFormat ); + qDebug() << loc.toString( d2, QLocale::LongFormat ); + qDebug() << loc.toString( d3, QLocale::LongFormat ); + + qDebug() << "default"; + qDebug() << loc.toString( d1 ); + qDebug() << loc.toString( d2 ); + qDebug() << loc.toString( d3 ); +} + +void printTimes( QLocale loc ) +{ + QLocale::setDefault( loc ); + + QTime t1( 6, 15, 45 ); + QTime t2( 12, 00, 00 ); + QTime t3( 18, 20, 25 ); + + qDebug() << "short"; + qDebug() << loc.toString( t1, QLocale::ShortFormat ); + qDebug() << loc.toString( t2, QLocale::ShortFormat ); + qDebug() << loc.toString( t3, QLocale::ShortFormat ); + + qDebug() << "long"; + qDebug() << loc.toString( t1, QLocale::LongFormat ); + qDebug() << loc.toString( t2, QLocale::LongFormat ); + qDebug() << loc.toString( t3, QLocale::LongFormat ); + + qDebug() << "default"; + qDebug() << loc.toString( t1 ); + qDebug() << loc.toString( t2 ); + qDebug() << loc.toString( t3 ); +} + +void printValues( QLocale loc ) +{ + QLocale::setDefault( loc ); + + double v1 = 3.1415; + double v2 = 31415; + double v3 = 1000.001; + + qDebug() << loc.toString( v1 ); + qDebug() << loc.toString( v2 ); + qDebug() << loc.toString( v3 ); +} + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + qDebug() << "Dates"; + qDebug() << "Swedish"; + printDates( QLocale( QLocale::Swedish, QLocale::Sweden ) ); + qDebug() << "US English"; + printDates( QLocale( QLocale::English, QLocale::UnitedStates ) ); + + qDebug() << "Times"; + qDebug() << "Swedish"; + printTimes( QLocale( QLocale::Swedish, QLocale::Sweden ) ); + qDebug() << "US English"; + printTimes( QLocale( QLocale::English, QLocale::UnitedStates ) ); + + qDebug() << "Values"; + qDebug() << "Swedish"; + printValues( QLocale( QLocale::Swedish, QLocale::Sweden ) ); + qDebug() << "US English"; + printValues( QLocale( QLocale::English, QLocale::UnitedStates ) ); +} diff --git a/Chapter10/noop/main.cpp b/Chapter10/noop/main.cpp new file mode 100644 index 0000000..26e2987 --- /dev/null +++ b/Chapter10/noop/main.cpp @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include + +#include + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + QTranslator translator; + translator.load( "noop_sv_SE" ); + app.installTranslator( &translator ); + + char *texts[] = { QT_TRANSLATE_NOOP("main","URL"), + QT_TRANSLATE_NOOP("main","Title"), + QT_TRANSLATE_NOOP("main","Publisher") }; + + char *texts2[] = { QT_TR_NOOP( "This is a very special string."), + QT_TR_NOOP( "And this is just as special.") }; + + QMessageBox::information( 0, qApp->translate("main",texts[2]), qApp->translate(0,texts2[1]) ); + + return 0; +} diff --git a/Chapter10/noop/noop.pro b/Chapter10/noop/noop.pro new file mode 100644 index 0000000..1d236c0 --- /dev/null +++ b/Chapter10/noop/noop.pro @@ -0,0 +1,13 @@ +###################################################################### +# Automatically generated by qmake (2.01a) sö 3. dec 19:31:19 2006 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp +TRANSLATIONS += noop_sv_SE.ts +CONFIG += console diff --git a/Chapter10/noop/noop_sv_SE.qm b/Chapter10/noop/noop_sv_SE.qm new file mode 100644 index 0000000..aa204fd Binary files /dev/null and b/Chapter10/noop/noop_sv_SE.qm differ diff --git a/Chapter10/noop/noop_sv_SE.ts b/Chapter10/noop/noop_sv_SE.ts new file mode 100644 index 0000000..b66454b --- /dev/null +++ b/Chapter10/noop/noop_sv_SE.ts @@ -0,0 +1,34 @@ + + + + + + + This is a very special string. + Detta är en väldigt speciell sträng. + + + + And this is just as special. + Och denna är precis lika speciell. + + + + main + + + URL + URL + + + + Title + Titel + + + + Publisher + Utgivare + + + diff --git a/Chapter10/sdi/images.qrc b/Chapter10/sdi/images.qrc new file mode 100644 index 0000000..44947aa --- /dev/null +++ b/Chapter10/sdi/images.qrc @@ -0,0 +1,8 @@ + + + images/new.png + images/cut.png + images/copy.png + images/paste.png + + \ No newline at end of file diff --git a/Chapter10/sdi/images/copy.png b/Chapter10/sdi/images/copy.png new file mode 100644 index 0000000..7052fab Binary files /dev/null and b/Chapter10/sdi/images/copy.png differ diff --git a/Chapter10/sdi/images/cut.png b/Chapter10/sdi/images/cut.png new file mode 100644 index 0000000..3c9a806 Binary files /dev/null and b/Chapter10/sdi/images/cut.png differ diff --git a/Chapter10/sdi/images/new.png b/Chapter10/sdi/images/new.png new file mode 100644 index 0000000..d1e8915 Binary files /dev/null and b/Chapter10/sdi/images/new.png differ diff --git a/Chapter10/sdi/images/paste.png b/Chapter10/sdi/images/paste.png new file mode 100644 index 0000000..ee6558a Binary files /dev/null and b/Chapter10/sdi/images/paste.png differ diff --git a/Chapter10/sdi/main.cpp b/Chapter10/sdi/main.cpp new file mode 100644 index 0000000..506e602 --- /dev/null +++ b/Chapter10/sdi/main.cpp @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include +#include + +#include + +#include "sdiwindow.h" + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + QTranslator translator; + translator.load( QString("sdi_")+QLocale::system().name() ); + app.installTranslator( &translator ); + + QTranslator qtTranslator; + qtTranslator.load( QString("qt_")+QLocale::system().name() ); + app.installTranslator( &qtTranslator ); + + (new SdiWindow)->show(); + QObject::connect( &app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()) ); + + return app.exec(); +} diff --git a/Chapter10/sdi/qt_sv_SE.qm b/Chapter10/sdi/qt_sv_SE.qm new file mode 100644 index 0000000..f9cacae Binary files /dev/null and b/Chapter10/sdi/qt_sv_SE.qm differ diff --git a/Chapter10/sdi/qt_sv_SE.ts b/Chapter10/sdi/qt_sv_SE.ts new file mode 100644 index 0000000..7c53ba5 --- /dev/null +++ b/Chapter10/sdi/qt_sv_SE.ts @@ -0,0 +1,3124 @@ + + + + PPDOptionsModel + + Name + Namn + + + Value + Värde + + + + Q3Accel + + %1, %2 not defined + %1, %2 är inte definierad + + + Ambiguous %1 not handled + Tvetydigt %1 hanteras inte + + + + Q3DataTable + + True + Sant + + + False + Falskt + + + Insert + Infoga + + + Update + Uppdatera + + + Delete + Ta bort + + + + Q3FileDialog + + Copy or Move a File + Kopiera eller ta bort en fil + + + Read: %1 + Läs: %1 + + + Write: %1 + Skriv: %1 + + + Cancel + Avbryt + + + All Files (*) + Alla filer (*) + + + Name + Namn + + + Size + Storlek + + + Type + Typ + + + Date + Datum + + + Attributes + Attribut + + + &OK + &OK + + + Look &in: + Leta &i: + + + File &name: + Fil&namn: + + + File &type: + Fil&typ: + + + Back + Tillbaka + + + One directory up + En katalog uppÃ¥t + + + Create New Folder + Skapa ny mapp + + + List View + Listvy + + + Detail View + Detaljvy + + + Preview File Info + Förhandsgranska filinformation + + + Preview File Contents + Förhandsgranska filinnehÃ¥ll + + + Read-write + Läs-skriv + + + Read-only + Skrivskyddad + + + Write-only + Lässkyddad + + + Inaccessible + Otillgänglig + + + Symlink to File + Symbolisk länk till fil + + + Symlink to Directory + Symbolisk länk till katalog + + + Symlink to Special + Symbolisk länk till special + + + File + Fil + + + Dir + Katalog + + + Special + Special + + + Open + Öppna + + + Save As + Spara som + + + &Open + &Öppna + + + &Save + &Spara + + + &Rename + &Byt namn + + + &Delete + &Ta bort + + + R&eload + Uppdat&era + + + Sort by &Name + Sortera efter &namn + + + Sort by &Size + Sortera efter &storlek + + + Sort by &Date + Sortera efter &datum + + + &Unsorted + &Osorterad + + + Sort + Sortera + + + Show &hidden files + Visa &dolda filer + + + the file + filen + + + the directory + katalogen + + + the symlink + symboliska länken + + + Delete %1 + Ta bort %1 + + + <qt>Are you sure you wish to delete %1 "%2"?</qt> + <qt>Är du säker pÃ¥ att du vill ta bort %1 "%2"?</qt> + + + &Yes + &Ja + + + &No + &Nej + + + New Folder 1 + Ny mapp 1 + + + New Folder + Ny mapp + + + New Folder %1 + Ny mapp %1 + + + Find Directory + Hitta katalog + + + Directories + Kataloger + + + Directory: + Katalog: + + + Error + Fel + + + %1 +File not found. +Check path and filename. + %1 +Filen hittades inte. +Kontrollera sökväg och filnamn. + + + All Files (*.*) + Alla filer (*.*) + + + Open + Öppna + + + Select a Directory + Välj en katalog + + + + Q3LocalFs + + Could not read directory +%1 + Kunde inte läsa katalogen +%1 + + + Could not create directory +%1 + Kunde inte skapa katalogen +%1 + + + Could not remove file or directory +%1 + Kunde inte ta bort filen eller katalogen +%1 + + + Could not rename +%1 +to +%2 + Kunde inte byta namn pÃ¥ +%1 +till +%2 + + + Could not open +%1 + Kunde inte öppna +%1 + + + Could not write +%1 + Kunde inte skriva till +%1 + + + + Q3MainWindow + + Line up + Rada upp + + + Customize... + Anpassa... + + + + Q3NetworkProtocol + + Operation stopped by the user + Ã…tgärden stoppades av användaren + + + + Q3ProgressDialog + + Cancel + Avbryt + + + + Q3TabDialog + + OK + OK + + + Apply + Verkställ + + + Help + Hjälp + + + Defaults + Standardvärden + + + Cancel + Avbryt + + + + Q3TextEdit + + &Undo + &Ã…ngra + + + &Redo + &Gör om + + + Cu&t + Klipp u&t + + + &Copy + &Kopiera + + + &Paste + Klistra &in + + + Clear + Töm + + + Select All + Markera alla + + + + Q3TitleBar + + System + System + + + Restore up + Ã…terställ uppÃ¥t + + + Minimize + Minimera + + + Restore down + Ã…terställ nedÃ¥t + + + Maximize + Maximera + + + Close + Stäng + + + Contains commands to manipulate the window + InnehÃ¥ller kommandon för att manipulera fönstret + + + Puts a minimized back to normal + Ã…terställer ett minimerat till normalt + + + Moves the window out of the way + Flyttar fönstret ur vägen + + + Puts a maximized window back to normal + Ã…terställer ett maximerat fönster tillbaka till normalt + + + Makes the window full screen + Gör fönstret till helskärm + + + Closes the window + Stänger fönstret + + + Displays the name of the window and contains controls to manipulate it + Visar namnet pÃ¥ fönstret och innehÃ¥ller kontroller för att manipulera det + + + + Q3ToolBar + + More... + Mer... + + + + Q3UrlOperator + + The protocol `%1' is not supported + Protokollet \"%\" stöds inte + + + The protocol `%1' does not support listing directories + Protokollet \"%1\" har inte stöd för att lista kataloger + + + The protocol `%1' does not support creating new directories + Protokollet \"%1\" har inte stöd för att skapa nya kataloger + + + The protocol `%1' does not support removing files or directories + Protokollet \"%1\" har inte stöd för att ta bort filer eller kataloger + + + The protocol `%1' does not support renaming files or directories + Protokollet \"%1\" har inte stöd för att byta namn pÃ¥ filer eller kataloger + + + The protocol `%1' does not support getting files + Protokollet \"%1\" har inte stöd för att hämta filer + + + The protocol `%1' does not support putting files + Protokollet \"%1\" har inte stöd för att lämna filer + + + The protocol `%1' does not support copying or moving files or directories + Protokollet \"%1\" har inte stöd för att kopiera eller flytta filer eller kataloger + + + (unknown) + (okänt) + + + + Q3Wizard + + &Cancel + &Avbryt + + + < &Back + < Till&baka + + + &Next > + &Nästa > + + + &Finish + &Färdig + + + &Help + &Hjälp + + + + QAbstractSocket + + Host not found + Värden hittades inte + + + Connection refused + Anslutningen nekades + + + Socket operation timed out + Tidsgräns för uttagsÃ¥tgärd överstegs + + + Socket is not connected + Uttaget är inte anslutet + + + + QAbstractSpinBox + + &Step up + &Stega uppÃ¥t + + + Step &down + Stega &nedÃ¥t + + + + QApplication + + Activate + Aktivera + + + Executable '%1' requires Qt %2, found Qt %3. + Binären \"%1\" kräver Qt %2, hittade Qt %3. + + + Incompatible Qt Library Error + Inkompatibelt Qt-biblioteksfel + + + QT_LAYOUT_DIRECTION + Translate this string to the string 'LTR' in left-to-right languages or to 'RTL' in right-to-left languages (such as Hebrew and Arabic) to get proper widget layout. + LTR + + + Activates the program's main window + Aktiverar programmets huvudfönster + + + + QAxSelect + + Select ActiveX Control + Välj ActiveX Control + + + OK + OK + + + &Cancel + &Avbryt + + + COM &Object: + COM-&objekt: + + + + QCheckBox + + Uncheck + Avkryssa + + + Check + Kryssa + + + Toggle + Växla + + + + QColorDialog + + Hu&e: + Nya&ns: + + + &Sat: + &Mättnad: + + + &Val: + &Ljushet: + + + &Red: + &Röd: + + + &Green: + &Grön: + + + Bl&ue: + Bl&Ã¥: + + + A&lpha channel: + Alfa&kanal: + + + &Basic colors + &Basfärger + + + &Custom colors + &Anpassade färger + + + &Define Custom Colors >> + &Definiera anpassade färger >> + + + OK + OK + + + Cancel + Avbryt + + + &Add to Custom Colors + &Lägg till i anpassade färger + + + Select color + Välj färg + + + + QComboBox + + Open + Öppna + + + + QDB2Driver + + Unable to connect + Kunde inte ansluta + + + Unable to commit transaction + Kunde inte verkställa transaktion + + + Unable to rollback transaction + Kunde inte rulla tillbaka transaktion + + + Unable to set autocommit + Kunde inte ställa in automatisk verkställning + + + + QDB2Result + + Unable to execute statement + Kunde inte köra frÃ¥gesats + + + Unable to prepare statement + Kunde inte förbereda frÃ¥gesats + + + Unable to bind variable + Kunde inte binda variabel + + + Unable to fetch record %1 + Kunde inte hämta posten %1 + + + Unable to fetch next + Kunde inte hämta nästa + + + Unable to fetch first + Kunde inte hämta första + + + + QDateTimeEdit + + AM + AM + + + am + am + + + PM + PM + + + pm + pm + + + + QDialog + + What's This? + Vad är det här? + + + + QDialogButtonBox + + OK + OK + + + Save + Spara + + + Open + Öppna + + + Cancel + Avbryt + + + Close + Stäng + + + Apply + Verkställ + + + Reset + Ã…terställ + + + Help + Hjälp + + + Don't Save + Spara inte + + + Discard + Förkasta + + + &Yes + &Ja + + + Yes to &All + Ja till &alla + + + &No + &Nej + + + N&o to All + N&ej till alla + + + Save All + Spara alla + + + Abort + Avbryt + + + Retry + Försök igen + + + Ignore + Ignorera + + + Restore Defaults + Ã…terställ standardvärden + + + + QDirModel + + Name + Namn + + + Size + Storlek + + + Kind + Match OS X Finder + Sort + + + Type + All other platforms + Typ + + + Date Modified + Ändringsdatum + + + + QErrorMessage + + Debug Message: + Felsökningsmeddelande: + + + Warning: + Varning: + + + Fatal Error: + Ödesdigert fel: + + + &Show this message again + &Visa detta meddelande igen + + + &OK + &OK + + + + QFileDialog + + All Files (*) + Alla filer (*) + + + Directories + Kataloger + + + &Open + &Öppna + + + &Save + &Spara + + + Open + Öppna + + + Save + Spara + + + %1 already exists. +Do you want to replace it? + %1 finns redan. +Vill du ersätta den? + + + %1 +File not found. +Please verify the correct file name was given. + %1 +Filen hittades inte. +Kontrollera att det korrekta filnamnet angavs. + + + My Computer + Min dator + + + Sort + Sortera + + + &Rename + &Byt namn + + + &Delete + &Ta bort + + + &Reload + &Uppdatera + + + Sort by &Name + Sortera efter &namn + + + Sort by &Size + Sortera efter &storlek + + + Sort by &Date + Sortera efter &datum + + + &Unsorted + &Osorterad + + + Show &hidden files + Visa &dolda filer + + + Back + Tillbaka + + + Parent Directory + Föräldrakatalog + + + Create New Folder + Skapa ny mapp + + + List View + Listvy + + + Detail View + Detaljerad vy + + + Look in: + Leta i: + + + File name: + Filnamn: + + + Files of type: + Filer av typen: + + + Cancel + Avbryt + + + Directory: + Katalog: + + + +File not found. +Please verify the correct file name was given + +Filen hittades inte. +Kontrollera att det korrekta filnamnet angavs + + + %1 +Directory not found. +Please verify the correct directory name was given. + %1 +Katalogen hittades inte. +Kontrollera att det korrekta katalognamnet angavs. + + + '%1' is write protected. +Do you want to delete it anyway? + \"%1\" är skrivskyddad. +Vill du ta bort den ändÃ¥? + + + Are sure you want to delete '%1'? + Är du säker pÃ¥ att du vill ta bort \"%1\"? + + + Could not delete directory. + Kunde inte ta bort katalogen. + + + All Files (*.*) + Alla filer (*.*) + + + Save As + Spara som + + + Open + Öppna + + + Select a Directory + Välj en katalog + + + Drive + Enhet + + + File + Fil + + + Unknown + Okänt + + + + QFontDialog + + &Font + &Typsnitt + + + Font st&yle + T&ypsnittsstil + + + &Size + &Storlek + + + Effects + Effekter + + + Stri&keout + Genomstru&ken + + + &Underline + &Understruken + + + Sample + Test + + + Wr&iting System + Skr&ivsystem + + + Select Font + Välj typsnitt + + + + QFtp + + Not connected + Inte ansluten + + + Host %1 not found + Värden %1 hittades inte + + + Connection refused to host %1 + Anslutningen till värden %1 vägrades + + + Connected to host %1 + Ansluten till värden %1 + + + Connection refused for data connection + Anslutning vägrades för dataanslutning + + + Unknown error + Okänt fel + + + Connecting to host failed: +%1 + Anslutning till värden misslyckades: +%1 + + + Login failed: +%1 + Inloggning misslyckades: +%1 + + + Listing directory failed: +%1 + Listning av katalogen misslyckades: +%1 + + + Changing directory failed: +%1 + Byte av katalog misslyckades: +%1 + + + Downloading file failed: +%1 + Nedladdningen av filen misslyckades: +%1 + + + Uploading file failed: +%1 + Uppladdningen av filen misslyckades: +%1 + + + Removing file failed: +%1 + Borttagning av filen misslyckades: +%1 + + + Creating directory failed: +%1 + Skapandet av katalogen misslyckades: +%1 + + + Removing directory failed: +%1 + Borttagning av katalogen misslyckades: +%1 + + + Connection closed + Anslutningen stängd + + + Host %1 found + Värden %1 hittades + + + Connection to %1 closed + Anslutningen till %1 stängdes + + + Host found + Värden hittades + + + Connected to host + Ansluten till värden + + + + QHostInfo + + Unknown error + Okänt fel + + + + QHostInfoAgent + + Host not found + Värden hittades inte + + + Unknown address type + Okänd adresstyp + + + Unknown error + Okänt fel + + + + QHttp + + Unknown error + Okänt fel + + + Request aborted + Begäran avbröts + + + No server set to connect to + Ingen server inställd att ansluta till + + + Wrong content length + Fel innehÃ¥llslängd + + + Server closed connection unexpectedly + Servern stängde oväntat anslutningen + + + Connection refused + Anslutningen nekades + + + Host %1 not found + Värden %1 hittades inte + + + HTTP request failed + HTTP-begäran misslyckades + + + Invalid HTTP response header + Ogiltig HTTP-svarshuvud + + + Invalid HTTP chunked body + Ogiltig HTTP chunked body + + + Host %1 found + Värden %1 hittades + + + Connected to host %1 + Ansluten till värden %1 + + + Connection to %1 closed + Anslutningen till %1 stängdes + + + Host found + Värden hittades + + + Connected to host + Ansluten till värd + + + Connection closed + Anslutningen stängd + + + + QIBaseDriver + + Error opening database + Fel vid öppning av databas + + + Could not start transaction + Kunde inte starta transaktion + + + Unable to commit transaction + Kunde inte verkställa transaktion + + + Unable to rollback transaction + Kunde inte rulla tillbaka transaktion + + + + QIBaseResult + + Unable to create BLOB + Kunde inte skapa BLOB + + + Unable to write BLOB + Kunde inte skriva BLOB + + + Unable to open BLOB + Kunde inte öppna BLOB + + + Unable to read BLOB + Kunde inte läsa BLOB + + + Could not find array + Kunde inte hitta kedja + + + Could not get array data + Kunde inte fÃ¥ kedjedata + + + Could not get query info + Kunde inte gÃ¥ frÃ¥gesatsinformation + + + Could not start transaction + Kunde inte starta transaktion + + + Unable to commit transaction + Kunde inte verkställa transaktion + + + Could not allocate statement + Kunde inte allokera frÃ¥gesats + + + Could not prepare statement + Kunde inte förbereda frÃ¥gesats + + + Could not describe input statement + Kunde inte beskriva inmatningsfrÃ¥gesats + + + Could not describe statement + Kunde inte beskriva frÃ¥gesats + + + Unable to close statement + Kunde inte stänga frÃ¥gesats + + + Unable to execute query + Kunde inte köra frÃ¥gesats + + + Could not fetch next item + Kunde inte hämta nästa post + + + Could not get statement info + Kunde inte fÃ¥ frÃ¥gesatsinformation + + + + QIODevice + + Permission denied + Ã…tkomst nekad + + + Too many open files + För mÃ¥nga öppna filer + + + No such file or directory + Ingen sÃ¥dan fil eller katalog + + + No space left on device + Inget ledigt utrymme pÃ¥ enheten + + + Unknown error + Okänt fel + + + + QInputContext + + XIM + XIM + + + XIM input method + XIM-inmatningsmetod + + + Windows input method + Windows-inmatningsmetod + + + Mac OS X input method + Mac OS X-inmatningsmetod + + + + QLibrary + + QLibrary::load_sys: Cannot load %1 (%2) + QLibrary::load_sys: Kan inte läsa in %1 (%2) + + + QLibrary::unload_sys: Cannot unload %1 (%2) + QLibrary::unload_sys: Kan inte läsa ur %1 (%2) + + + QLibrary::resolve_sys: Symbol "%1" undefined in %2 (%3) + QLibrary::resolve_sys: Symbolen "%1" är inte definierad i %2 (%3) + + + + QLineEdit + + &Undo + &Ã…ngra + + + &Redo + &Gör om + + + Cu&t + Klipp &ut + + + &Copy + &Kopiera + + + &Paste + Klistra &in + + + Delete + Ta bort + + + Select All + Markera alla + + + + QMYSQLDriver + + Unable to open database ' + Kunde inte öppna databasen \" + + + Unable to connect + Kunde inte ansluta + + + Unable to begin transaction + Kunde inte pÃ¥börja transaktion + + + Unable to commit transaction + Kunde inte verkställa transaktion + + + Unable to rollback transaction + Kunde inte rulla tillbaka transaktion + + + + QMYSQLResult + + Unable to fetch data + Kunde inte hämta data + + + Unable to execute query + Kunde inte köra frÃ¥gesats + + + Unable to store result + Kunde inte lagra resultat + + + Unable to prepare statement + Kunde inte förbereda frÃ¥gesats + + + Unable to reset statement + Kunde inte Ã¥terställa frÃ¥gesats + + + Unable to bind value + Kunde inte binda värde + + + Unable to execute statement + Kunde inte köra frÃ¥gesats + + + Unable to bind outvalues + Kunde inte binda utvärden + + + Unable to store statement results + Kunde inte lagra resultat frÃ¥n frÃ¥gesats + + + + QMenu + + Close + Stäng + + + Open + Öppna + + + Execute + Kör + + + + QMessageBox + + Help + Hjälp + + + OK + OK + + + About Qt + Om Qt + + + <p>This program uses Qt version %1.</p> + <p>Detta program använder Qt version %1.</p> + + + <h3>About Qt</h3>%1<p>Qt is a C++ toolkit for cross-platform application development.</p><p>Qt provides single-source portability across MS&nbsp;Windows, Mac&nbsp;OS&nbsp;X, Linux, and all major commercial Unix variants. Qt is also available for embedded devices as Qtopia Core.</p><p>Qt is a Trolltech product. See <a href="http://www.trolltech.com/qt/">www.trolltech.com/qt/</a> for more information.</p> + <h3>Om Qt</h3>%1<p>Qt är ett C++-verktygssamling för utveckling av krossplattformsprogram.</p><p>Qt tillhandahÃ¥ller portabilitet för samma källkod mellan MS&nbsp;Windows, Mac&nbsp;OS&nbsp;X, Linux, och alla andra stora kommersiella Unix-varianter. Qt finns ocksÃ¥ tillgängligt för inbäddade enheter som Qtopia Core.</p><p>Qt är en produkt frÃ¥n Trolltech. Se <a href="http://www.trolltech.com/qt/">www.trolltech.com/qt/</a> för mer information.</p> + + + <p>This program uses Qt Open Source Edition version %1.</p><p>Qt Open Source Edition is intended for the development of Open Source applications. You need a commercial Qt license for development of proprietary (closed source) applications.</p><p>Please see <a href="http://www.trolltech.com/company/model.html">www.trolltech.com/company/model.html</a> for an overview of Qt licensing.</p> + <p>Detta program använder öppna källkodsutgÃ¥van avQt version %1.</p><p>Öppna källkodsutgÃ¥van av Qt är tänkt för utvecklingen av program med öppen källkod. Du behöver en kommersiell Qt-licens för att utveckla properitära (stängd källkod) program.</p><p>Se <a href="http://www.trolltech.com/company/model.html">www.trolltech.com/company/model.html</a> för en översikt av licensmodellen för Qt.</p> + + + Show Details... + Visa detaljer... + + + Hide Details... + Dölj detaljer,,, + + + + QMultiInputContext + + Select IM + Välj inmatningsmetod + + + + QMultiInputContextPlugin + + Multiple input method switcher + Växlare för flera inmatningsmetoder + + + Multiple input method switcher that uses the context menu of the text widgets + Växlare för flera inmatningsmetoder som använder sammanhangsmenyn för textwidgar + + + + QNativeSocketEngine + + The remote host closed the connection + Fjärrvärden stängde anslutningen + + + Network operation timed out + Tidsgräns för nätverksÃ¥tgärd överstegs + + + Out of resources + Slut pÃ¥ resurser + + + Unsupported socket operation + UttagsÃ¥tgärden stöds inte + + + Protocol type not supported + Protokolltypen stöds inte + + + Invalid socket descriptor + Ogiltig uttagsbeskrivare + + + Network unreachable + Nätverket är inte nÃ¥bart + + + Permission denied + Ã…tkomst nekad + + + Connection timed out + Tidsgränsen för anslutning överstegs + + + Connection refused + Anslutningen vägrades + + + The bound address is already in use + Bindningsadress används redan + + + The address is not available + Adressen är inte tillgänglig + + + The address is protected + Adressen är skyddad + + + Unable to send a message + Kunde inte skicka ett meddelande + + + Unable to receive a message + Kunde inte ta emot ett meddelande + + + Unable to write + Kunde inte skriva + + + Network error + Nätverksfel + + + Another socket is already listening on the same port + Ett annat uttag lyssnar redan pÃ¥ samma port + + + Unable to initialize non-blocking socket + Kunde inte initiera icke-blockerande uttag + + + Unable to initialize broadcast socket + Kunde inte initiera uttag för broadcast + + + Attempt to use IPv6 socket on a platform with no IPv6 support + Försök att använda IPv6-uttag pÃ¥ en plattform som saknar IPv6-stöd + + + Host unreachable + Värden är inte nÃ¥bar + + + Datagram was too large to send + Datagram för för stor för att skicka + + + Operation on non-socket + Ã…tgärd pÃ¥ icke-uttag + + + Unknown error + Okänt fel + + + + QOCIDriver + + QOCIDriver + Unable to initialize + QOCIDriver + + + Unable to logon + Kunde inte logga in + + + + QOCIResult + + Unable to bind column for batch execute + Kunde inte binda kolumn för satskörning + + + Unable to execute batch statement + Kunde inte köra satsfrÃ¥ga + + + Unable to goto next + Kunde inte gÃ¥ till nästa + + + Unable to alloc statement + Kunde inte allokera frÃ¥gesats + + + Unable to prepare statement + Kunde inte förbereda frÃ¥gesats + + + Unable to bind value + Kunde inte binda värde + + + Unable to execute select statement + Kunde inte köra \"select\"-frÃ¥gesats + + + Unable to execute statement + Kunde inte köra frÃ¥gesats + + + + QODBCDriver + + Unable to connect + Kunde inte ansluta + + + Unable to connect - Driver doesn't support all needed functionality + Kunde inte ansluta - Drivrutinen har inte stöd för all nödvändig funktionalitet + + + Unable to disable autocommit + Kunde inte inaktivera automatisk verkställning + + + Unable to commit transaction + Kunde inte verkställa transaktion + + + Unable to rollback transaction + Kunde inte rulla tillbaka transaktion + + + Unable to enable autocommit + Kunde inte aktivera automatisk verkställning + + + + QODBCResult + + QODBCResult::reset: Unable to set 'SQL_CURSOR_STATIC' as statement attribute. Please check your ODBC driver configuration + QODBCResult::reset: Kunde inte ställa in \"SQL_CURSOR_STATIC\" som frÃ¥gesatsattribut. Kontrollera konfigurationen för din ODBC-drivrutin + + + Unable to execute statement + Kunde inte köra frÃ¥gesats + + + Unable to fetch next + Kunde inte hämta nästa + + + Unable to prepare statement + Kunde inte förbereda frÃ¥gesats + + + Unable to bind variable + Kunde inte binda variabel + + + + QObject + + False + Falskt + + + True + Sant + + + + QPSQLDriver + + Unable to connect + Kunde inte ansluta + + + Could not begin transaction + Kunde inte pÃ¥börja transaktion + + + Could not commit transaction + Kunde inte verkställa transaktion + + + Could not rollback transaction + Kunde inte rulla tillbaka transaktion + + + + QPSQLResult + + Unable to create query + Kunde inte skapa frÃ¥ga + + + + QPrintDialog + + locally connected + lokalt ansluten + + + Aliases: %1 + Alias: %1 + + + unknown + okänt + + + Portrait + StÃ¥ende + + + Landscape + Liggande + + + A0 (841 x 1189 mm) + A0 (841 x 1189 mm) + + + A1 (594 x 841 mm) + A1 (594 x 841 mm) + + + A2 (420 x 594 mm) + A2 (420 x 594 mm) + + + A3 (297 x 420 mm) + A3 (297 x 420 mm) + + + A4 (210 x 297 mm, 8.26 x 11.7 inches) + A4 (210 x 297 mm, 8.26 x 11.7 tum) + + + A5 (148 x 210 mm) + A5 (148 x 210 mm) + + + A6 (105 x 148 mm) + A6 (105 x 148 mm) + + + A7 (74 x 105 mm) + A7 (74 x 105 mm) + + + A8 (52 x 74 mm) + A8 (52 x 74 mm) + + + A9 (37 x 52 mm) + A9 (37 x 52 mm) + + + B0 (1000 x 1414 mm) + B0 (1000 x 1414 mm) + + + B1 (707 x 1000 mm) + B1 (707 x 1000 mm) + + + B2 (500 x 707 mm) + B2 (500 x 707 mm) + + + B3 (353 x 500 mm) + B3 (353 x 500 mm) + + + B4 (250 x 353 mm) + B4 (250 x 353 mm) + + + B5 (176 x 250 mm, 6.93 x 9.84 inches) + B5 (176 x 250 mm, 6.93 x 9.84 tum) + + + B6 (125 x 176 mm) + B6 (125 x 176 mm) + + + B7 (88 x 125 mm) + B7 (88 x 125 mm) + + + B8 (62 x 88 mm) + B8 (62 x 88 mm) + + + B9 (44 x 62 mm) + B9 (44 x 62 mm) + + + B10 (31 x 44 mm) + B10 (31 x 44 mm) + + + C5E (163 x 229 mm) + C5E (163 x 229 mm) + + + DLE (110 x 220 mm) + DLE (110 x 220 mm) + + + Executive (7.5 x 10 inches, 191 x 254 mm) + Executive (7.5 x 10 tum, 191 x 254 mm) + + + Folio (210 x 330 mm) + Folio (210 x 330 mm) + + + Ledger (432 x 279 mm) + Ledger (432 x 279 mm) + + + Legal (8.5 x 14 inches, 216 x 356 mm) + Legal (8.5 x 14 tum, 216 x 356 mm) + + + Letter (8.5 x 11 inches, 216 x 279 mm) + Letter (8.5 x 11 tum, 216 x 279 mm) + + + Tabloid (279 x 432 mm) + Tabloid (279 x 432 mm) + + + US Common #10 Envelope (105 x 241 mm) + US Common #10 Envelope (105 x 241 mm) + + + OK + OK + + + Cancel + Avbryt + + + Page size: + Sidstorlek: + + + Orientation: + Orientering: + + + Paper source: + Papperskälla: + + + Print + Skriv ut + + + File + Fil + + + Printer + Skrivare + + + Print To File ... + Skriv ut till fil ... + + + Print dialog + Utskriftsdialog + + + Paper format + Pappersformat + + + Size: + Storlek: + + + Properties + Egenskaper + + + Printer info: + Skrivarinformation: + + + Browse + Bläddra + + + Print to file + Skriv ut till fil + + + Print range + Skriv ut intervall + + + Print all + Skriv ut alla + + + Pages from + Sidor frÃ¥n + + + to + till + + + Selection + Val + + + Copies + Kopior + + + Number of copies: + Antal kopior: + + + Collate + Sortera + + + Print last page first + Skriv ut sista sidan först + + + Other + Annat + + + Print in color if available + Skriv ut i färg om möjligt + + + Double side printing + Dubbelsidig utskrift + + + File %1 is not writable. +Please choose a different file name. + Filen %1 är inte skrivbar. +Välj ett annat filnamn. + + + %1 already exists. +Do you want to overwrite it? + %1 finns redan. +Vill du skriva över den? + + + + QPrintPropertiesDialog + + PPD Properties + PPD-egenskaper + + + Save + Spara + + + OK + OK + + + + QProgressBar + + %1% + %1% + + + + QProgressDialog + + Cancel + Avbryt + + + + QPushButton + + Open + Öppna + + + + QRadioButton + + Check + Kryssa + + + + QRegExp + + no error occurred + inga fel inträffade + + + disabled feature used + inaktiverad funktion används + + + bad char class syntax + felaktig teckenklasssyntax + + + bad lookahead syntax + felaktig seframÃ¥tsyntax + + + bad repetition syntax + felaktig upprepningssyntax + + + invalid octal value + ogiltigt oktalt värde + + + missing left delim + saknar vänster avgränsare + + + unexpected end + oväntat slut + + + met internal limit + nÃ¥dde intern gräns + + + + QSQLite2Driver + + Error to open database + Fel vid öppning av databas + + + Unable to begin transaction + Kunde inte pÃ¥börja transaktion + + + Unable to commit transaction + Kunde inte verkställa transaktion + + + Unable to rollback Transaction + Kunde inte rulla tillbaka transaktion + + + + QSQLite2Result + + Unable to fetch results + Kunde inte hämta resultat + + + Unable to execute statement + Kunde inte köra frÃ¥gesats + + + + QSQLiteDriver + + Error opening database + Fel vid öppning av databas + + + Error closing database + Fel vid stängning av databas + + + Unable to begin transaction + Kunde inte pÃ¥börja transaktion + + + Unable to commit transaction + Kunde inte verkställa transaktion + + + Unable to roll back transaction + Kunde inte rulla tillbaka transaktion + + + + QSQLiteResult + + Unable to fetch row + Kunde inte hämta rad + + + Unable to execute statement + Kunde inte köra frÃ¥gesats + + + Unable to reset statement + Kunde inte Ã¥terställa frÃ¥gesats + + + Unable to bind parameters + Kunde inte binda parametrar + + + Parameter count mismatch + Parameterantal stämmer inte + + + + QScrollBar + + Scroll here + Rulla här + + + Left edge + Vänsterkant + + + Top + Överkant + + + Right edge + Högerkant + + + Bottom + Nederkant + + + Page left + Sida vänster + + + Page up + Sida uppÃ¥t + + + Page right + Sida höger + + + Page down + Sida nedÃ¥t + + + Scroll left + Rulla vänster + + + Scroll up + Rulla uppÃ¥t + + + Scroll right + Rulla höger + + + Scroll down + Rulla nedÃ¥t + + + Line up + Rada upp + + + Position + Position + + + Line down + Rad nedÃ¥t + + + + QShortcut + + Space + Mellanslag + + + Esc + Esc + + + Tab + Tab + + + Backtab + Backtab + + + Backspace + Backsteg + + + Return + Return + + + Enter + Enter + + + Ins + Ins + + + Del + Del + + + Pause + Pause + + + Print + Print + + + SysReq + SysReq + + + Home + Home + + + End + End + + + Left + Vänster + + + Up + Upp + + + Right + Höger + + + Down + Ned + + + PgUp + PgUp + + + PgDown + PgDown + + + CapsLock + CapsLock + + + NumLock + NumLock + + + ScrollLock + ScrollLock + + + Menu + Meny + + + Help + Hjälp + + + Back + BakÃ¥t + + + Forward + FramÃ¥t + + + Stop + Stoppa + + + Refresh + Uppdatera + + + Volume Down + Sänk volym + + + Volume Mute + Volym tyst + + + Volume Up + Höj volym + + + Bass Boost + Förstärk bas + + + Bass Up + Höj bas + + + Bass Down + Sänk bas + + + Treble Up + Höj diskant + + + Treble Down + Sänk diskant + + + Media Play + Media spela upp + + + Media Stop + Media stopp + + + Media Previous + Media föregÃ¥ende + + + Media Next + Media nästa + + + Media Record + Media spela in + + + Favorites + Favoriter + + + Search + Sök + + + Standby + Avvakta + + + Open URL + Öppna url + + + Launch Mail + Starta e-post + + + Launch Media + Starta media + + + Launch (0) + Starta (0) + + + Launch (1) + Starta (1) + + + Launch (2) + Starta (2) + + + Launch (3) + Starta (3) + + + Launch (4) + Starta (4) + + + Launch (5) + Starta (5) + + + Launch (6) + Starta (6) + + + Launch (7) + Starta (7) + + + Launch (8) + Starta (8) + + + Launch (9) + Starta (9) + + + Launch (A) + Starta (A) + + + Launch (B) + Starta (B) + + + Launch (C) + Starta (C) + + + Launch (D) + Starta (D) + + + Launch (E) + Starta (E) + + + Launch (F) + Starta (F) + + + Print Screen + Print Screen + + + Page Up + Page Up + + + Page Down + Page Down + + + Caps Lock + Caps Lock + + + Num Lock + Num Lock + + + Number Lock + Number Lock + + + Scroll Lock + Scroll Lock + + + Insert + Insert + + + Delete + Delete + + + Escape + Escape + + + System Request + System Request + + + Select + Välj + + + Yes + Ja + + + No + Nej + + + Context1 + Sammanhang1 + + + Context2 + Sammanhang2 + + + Context3 + Sammanhang3 + + + Context4 + Sammanhang4 + + + Call + Ring upp + + + Hangup + Lägg pÃ¥ + + + Flip + Vänd + + + Ctrl + Ctrl + + + Shift + Shift + + + Alt + Alt + + + Meta + Meta + + + + + + + + + F%1 + F%1 + + + Home Page + Hemsida + + + + QSlider + + Page left + Sida vänster + + + Page up + Sida uppÃ¥t + + + Position + Position + + + Page right + Sida höger + + + Page down + Sida nedÃ¥t + + + + QSocks5SocketEngine + + Socks5 timeout error connecting to socks server + Tidsgräns för Socks5 överstigen vid anslutningen till socks-server + + + + QSpinBox + + More + Mer + + + Less + Mindre + + + + QSql + + Delete + Ta bort + + + Delete this record? + Ta bort denna post? + + + Yes + Ja + + + No + Nej + + + Insert + Infoga + + + Update + Uppdatera + + + Save edits? + Spara redigeringar? + + + Cancel + Avbryt + + + Confirm + Bekräfta + + + Cancel your edits? + Avbryt dina redigeringar? + + + + QTDSDriver + + Unable to open connection + Kunde inte öppna anslutning + + + Unable to use database + Kunde inte använda databasen + + + + QTabBar + + Scroll Left + Rulla vänster + + + Scroll Right + Rulla höger + + + + QTcpServer + + Socket operation unsupported + UttagsÃ¥tgärd stöds inte + + + + QTextControl + + &Undo + &Ã…ngra + + + &Redo + &Gör om + + + Cu&t + Klipp u&t + + + &Copy + &Kopiera + + + Copy &Link Location + Kopiera &länkplats + + + &Paste + Klistra &in + + + Delete + Ta bort + + + Select All + Markera alla + + + + QToolButton + + Press + Tryck + + + Open + Öppna + + + + QUdpSocket + + This platform does not support IPv6 + Denna plattform saknar stöd för IPv6 + + + + QUndoGroup + + Undo + Ã…ngra + + + Redo + Gör om + + + + QUndoModel + + <empty> + <tom> + + + + QUndoStack + + Undo + Ã…ngra + + + Redo + Gör om + + + + QUnicodeControlCharacterMenu + + LRM Left-to-right mark + U+200E + + + RLM Right-to-left mark + U+200F + + + ZWJ Zero width joiner + U+200D + + + ZWNJ Zero width non-joiner + U+200C + + + ZWSP Zero width space + U+200B + + + LRE Start of left-to-right embedding + U+202A + + + RLE Start of right-to-left embedding + U+202B + + + LRO Start of left-to-right override + U+202D + + + RLO Start of right-to-left override + U+202E + + + PDF Pop directional formatting + U+202C + + + Insert Unicode control character + Infoga unicode-kontrolltecken + + + + QWhatsThisAction + + What's This? + Vad är det här? + + + + QWidget + + * + * + + + + QWorkspace + + &Restore + Ã…te&rställ + + + &Move + &Flytta + + + &Size + &Storlek + + + Mi&nimize + Mi&nimera + + + Ma&ximize + Ma&ximera + + + &Close + &Stäng + + + Stay on &Top + Stanna kvar övers&t + + + Sh&ade + Skugg&a + + + %1 - [%2] + %1 - [%2] + + + Minimize + Minimera + + + Restore Down + Ã…terställ nedÃ¥t + + + Close + Stäng + + + &Unshade + A&vskugga + + + + QXml + + no error occurred + inga fel inträffade + + + error triggered by consumer + fel utlöstes av konsument + + + unexpected end of file + oväntat slut pÃ¥ filen + + + more than one document type definition + fler än en dokumenttypsdefinition + + + error occurred while parsing element + fel inträffade vid tolkning av element + + + tag mismatch + tagg stämmer inte + + + error occurred while parsing content + fel inträffade vid tolkning av innehÃ¥ll + + + unexpected character + oväntat tecken + + + invalid name for processing instruction + ogiltigt namn för behandlingsinstruktion + + + version expected while reading the XML declaration + version förväntades vid läsning av XML-deklareringen + + + wrong value for standalone declaration + fel värde för fristÃ¥ende deklarering + + + encoding declaration or standalone declaration expected while reading the XML declaration + kodningsdeklarering eller fristÃ¥ende deklarering förväntades vid läsning av XML-deklareringen + + + standalone declaration expected while reading the XML declaration + fristÃ¥ende deklarering förväntades vid läsning av XML-deklarering + + + error occurred while parsing document type definition + fel inträffade vid tolkning av dokumenttypsdefinition + + + letter is expected + bokstav förväntades + + + error occurred while parsing comment + fel inträffade vid tolkning av kommentar + + + error occurred while parsing reference + fel inträffade vid tolkning av referens + + + internal general entity reference not allowed in DTD + intern allmän entitetsreferens tillÃ¥ts inte i DTD + + + external parsed general entity reference not allowed in attribute value + extern tolkad allmän entitetsreferens tillÃ¥ts inte i attributvärde + + + external parsed general entity reference not allowed in DTD + extern tolkad allmän entitetsreferens tillÃ¥ts inte i DTD + + + unparsed entity reference in wrong context + otolkad entitetsreferens i fel sammanhang + + + recursive entities + rekursiva entiteter + + + error in the text declaration of an external entity + fel i textdeklareringen av en extern entitet + + + diff --git a/Chapter10/sdi/sdi.pro b/Chapter10/sdi/sdi.pro new file mode 100644 index 0000000..b141ccd --- /dev/null +++ b/Chapter10/sdi/sdi.pro @@ -0,0 +1,16 @@ +###################################################################### +# Automatically generated by qmake (2.01a) to 28. sep 16:43:38 2006 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +HEADERS += sdiwindow.h +SOURCES += main.cpp sdiwindow.cpp +RESOURCES += images.qrc + +TRANSLATIONS += sdi_sv_SE.ts +CONFIG += console diff --git a/Chapter10/sdi/sdi_sv_SE.qm b/Chapter10/sdi/sdi_sv_SE.qm new file mode 100644 index 0000000..5316a4e Binary files /dev/null and b/Chapter10/sdi/sdi_sv_SE.qm differ diff --git a/Chapter10/sdi/sdi_sv_SE.ts b/Chapter10/sdi/sdi_sv_SE.ts new file mode 100644 index 0000000..00ddcab --- /dev/null +++ b/Chapter10/sdi/sdi_sv_SE.ts @@ -0,0 +1,233 @@ + + + + SdiWindow + + + %1[*] - %2 + %1[*] - %2 + + + + unnamed + namnlös + + + + SDI + SDI + + + + Done + Klar + + + + About SDI + Om SDI + + + + A single document interface application. + En applikation med enkeldokumentgränssnitt. + + + + &New + &Ny + + + + Ctrl+N + Ctrl+N + + + + Create a new document + Skapa ett nytt dokument + + + + &Open + &Öppna + + + + Ctrl+O + Ctrl+O + + + + Open a document + Öppna ett dokument + + + + &Save + &Spara + + + + Ctrl+S + Ctrl+S + + + + Save the document + Spara dokumentet + + + + Save &As + Spara so&m + + + + Save the document as + Spara dokumentet som + + + + &Close + S&täng + + + + Ctrl+W + Ctrl+W + + + + Close this document + Stäng detta dokument + + + + E&xit + A&vsluta + + + + Ctrl+Q + Ctrl+Q + + + + Quit the application + Avslutar applikationen + + + + Cu&t + K&lipp + + + + Ctrl+X + Ctrl+X + + + + Cut + Klipp + + + + &Copy + &Kopiera + + + + Ctrl+C + Ctrl+C + + + + Copy + Kopiera + + + + &Paste + Kl&istra in + + + + Ctrl+V + Ctrl+V + + + + Paste + Klista in + + + + &About + &Om + + + + About this application + Om denna applikation + + + + About &Qt + Om &Qt + + + + About the Qt toolkit + Om Qt verktyget + + + + &File + &Arkiv + + + + &Edit + &Redigera + + + + &Help + &Hjälp + + + + File + Arkiv + + + + Edit + Redigera + + + + The document has unsaved changes. +Do you want to save it before it is closed? + Dokumentets ändringar är inte sparade. +Vill du spara ändringarna innan dokumentet stängs? + + + + Save As + Spara som + + + + Failed to save file. + Misslyckades med att spara dokumentet. + + + + Failed to open file. + Misslyckades med att öppna dokumentet. + + + diff --git a/Chapter10/sdi/sdiwindow.cpp b/Chapter10/sdi/sdiwindow.cpp new file mode 100644 index 0000000..56cf147 --- /dev/null +++ b/Chapter10/sdi/sdiwindow.cpp @@ -0,0 +1,271 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include +#include +#include +#include +#include + +#include + +#include +#include +#include + +#include "sdiwindow.h" + +SdiWindow::SdiWindow( QWidget *parent ) : QMainWindow( parent ) +{ + setAttribute( Qt::WA_DeleteOnClose ); + setWindowTitle( tr("%1[*] - %2" ).arg(tr("unnamed")).arg(tr("SDI")) ); + + docWidget = new QTextEdit( this ); + setCentralWidget( docWidget ); + + connect( docWidget->document(), SIGNAL(modificationChanged(bool)), this, SLOT(setWindowModified(bool)) ); + + createActions(); + createMenus(); + createToolbars(); + statusBar()->showMessage( tr("Done") ); +} + +void SdiWindow::closeEvent( QCloseEvent *event ) +{ + if( isSafeToClose() ) + event->accept(); + else + event->ignore(); +} + +void SdiWindow::fileNew() +{ + (new SdiWindow())->show(); +} + +void SdiWindow::helpAbout() +{ + QMessageBox::about( this, tr("About SDI"), tr("A single document interface application.") ); +} + +void SdiWindow::createActions() +{ + newAction = new QAction( QIcon(":/images/new.png"), tr("&New"), this ); + newAction->setShortcut( tr("Ctrl+N") ); + newAction->setStatusTip( tr("Create a new document") ); + connect( newAction, SIGNAL(triggered()), this, SLOT(fileNew()) ); + + openAction = new QAction( tr("&Open"), this ); + openAction->setShortcut( tr("Ctrl+O") ); + openAction->setStatusTip( tr("Open a document") ); + connect( openAction, SIGNAL(triggered()), this, SLOT(fileOpen()) ); + + saveAction = new QAction( tr("&Save"), this ); + saveAction->setShortcut( tr("Ctrl+S") ); + saveAction->setStatusTip( tr("Save the document") ); + connect( saveAction, SIGNAL(triggered()), this, SLOT(fileSave()) ); + + saveAsAction = new QAction( tr("Save &As"), this ); + saveAsAction->setStatusTip( tr("Save the document as") ); + connect( saveAsAction, SIGNAL(triggered()), this, SLOT(fileSaveAs()) ); + + closeAction = new QAction( tr("&Close"), this ); + closeAction->setShortcut( tr("Ctrl+W") ); + closeAction->setStatusTip( tr("Close this document") ); + connect( closeAction, SIGNAL(triggered()), this, SLOT(close()) ); + + exitAction = new QAction( tr("E&xit"), this ); + exitAction->setShortcut( tr("Ctrl+Q") ); + exitAction->setStatusTip( tr("Quit the application") ); + connect( exitAction, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()) ); + + + cutAction = new QAction( QIcon(":/images/cut.png"), tr("Cu&t"), this ); + cutAction->setShortcut( tr("Ctrl+X") ); + cutAction->setStatusTip( tr("Cut") ); + cutAction->setEnabled(false); + connect( docWidget, SIGNAL(copyAvailable(bool)), cutAction, SLOT(setEnabled(bool)) ); + connect( cutAction, SIGNAL(triggered()), docWidget, SLOT(cut()) ); + + copyAction = new QAction( QIcon(":/images/copy.png"), tr("&Copy"), this ); + copyAction->setShortcut( tr("Ctrl+C") ); + copyAction->setStatusTip( tr("Copy") ); + copyAction->setEnabled(false); + connect( docWidget, SIGNAL(copyAvailable(bool)), copyAction, SLOT(setEnabled(bool)) ); + connect( copyAction, SIGNAL(triggered()), docWidget, SLOT(copy()) ); + + + pasteAction = new QAction( QIcon(":/images/paste.png"), tr("&Paste"), this ); + pasteAction->setShortcut( tr("Ctrl+V") ); + pasteAction->setStatusTip( tr("Paste") ); + connect( pasteAction, SIGNAL(triggered()), docWidget, SLOT(paste()) ); + + + aboutAction = new QAction( tr("&About"), this ); + aboutAction->setStatusTip( tr("About this application") ); + connect( aboutAction, SIGNAL(triggered()), this, SLOT(helpAbout()) ); + + aboutQtAction = new QAction( tr("About &Qt"), this ); + aboutQtAction->setStatusTip( tr("About the Qt toolkit") ); + connect( aboutQtAction, SIGNAL(triggered()), qApp, SLOT(aboutQt()) ); +} + +void SdiWindow::createMenus() +{ + QMenu *menu; + + menu = menuBar()->addMenu( tr("&File") ); + menu->addAction( newAction ); + menu->addSeparator(); + menu->addAction( openAction ); + menu->addAction( saveAction ); + menu->addAction( saveAsAction ); + menu->addSeparator(); + menu->addAction( closeAction ); + menu->addSeparator(); + menu->addAction( exitAction ); + + menu = menuBar()->addMenu( tr("&Edit") ); + menu->addAction( cutAction ); + menu->addAction( copyAction ); + menu->addAction( pasteAction ); + + menu = menuBar()->addMenu( tr("&Help") ); + menu->addAction( aboutAction ); + menu->addAction( aboutQtAction ); +} + +void SdiWindow::createToolbars() +{ + QToolBar *toolbar; + + toolbar = addToolBar( tr("File") ); + toolbar->addAction( newAction ); + + toolbar = addToolBar( tr("Edit") ); + toolbar->addAction( cutAction ); + toolbar->addAction( copyAction ); + toolbar->addAction( pasteAction ); +} + + + +bool SdiWindow::isSafeToClose() +{ + if( isWindowModified() ) + { + switch( QMessageBox::warning( this, tr("SDI"), + tr("The document has unsaved changes.\n" + "Do you want to save it before it is closed?"), + QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel ) ) + { + case QMessageBox::Cancel: + return false; + case QMessageBox::Save: + return fileSave(); + default: + return true; + } + } + + return true; +} + +void SdiWindow::fileOpen() +{ + QString filename = QFileDialog::getOpenFileName( this ); + if( filename.isEmpty() ) + return; + + if( currentFilename.isEmpty() && !docWidget->document()->isModified() ) + loadFile( filename ); + else + { + SdiWindow *window = new SdiWindow(); + window->loadFile( filename ); + window->show(); + } +} + +bool SdiWindow::fileSave() +{ + if( currentFilename.isEmpty() ) + return fileSaveAs(); + else + return saveFile( currentFilename ); +} + +bool SdiWindow::fileSaveAs() +{ + QString filename = QFileDialog::getSaveFileName( this, tr("Save As"), currentFilename ); + if( filename.isEmpty() ) + return false; + + return saveFile( filename ); +} + +bool SdiWindow::saveFile( QString filename ) +{ + QFile file( filename ); + if( !file.open( QIODevice::WriteOnly | QIODevice::Text ) ) + { + QMessageBox::warning( this, tr("SDI"), tr("Failed to save file.") ); + return false; + } + + QTextStream stream( &file ); + stream << docWidget->toPlainText(); + + currentFilename = filename; + docWidget->document()->setModified( false ); + setWindowTitle( tr("%1[*] - %2" ).arg(filename).arg(tr("SDI")) ); + + return true; +} + +void SdiWindow::loadFile( QString filename ) +{ + QFile file( filename ); + if( !file.open( QIODevice::ReadOnly | QIODevice::Text ) ) + { + QMessageBox::warning( this, tr("SDI"), tr("Failed to open file.") ); + return; + } + + QTextStream stream( &file ); + docWidget->setPlainText( stream.readAll() ); + + currentFilename = filename; + docWidget->document()->setModified( false ); + setWindowTitle( tr("%1[*] - %2" ).arg(filename).arg(tr("SDI")) ); +} diff --git a/Chapter10/sdi/sdiwindow.h b/Chapter10/sdi/sdiwindow.h new file mode 100644 index 0000000..bb07eb6 --- /dev/null +++ b/Chapter10/sdi/sdiwindow.h @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef SDIWINDOW_H +#define SDIWINDOW_H + +#include + +class QAction; +class QTextEdit; + +class SdiWindow : public QMainWindow +{ + Q_OBJECT + +public: + SdiWindow( QWidget *parent = 0 ); + +protected: + void closeEvent( QCloseEvent *event ); + +private slots: + void fileNew(); + void helpAbout(); + + void fileOpen(); + bool fileSave(); + bool fileSaveAs(); + +private: + void createActions(); + void createMenus(); + void createToolbars(); + + bool isSafeToClose(); + + bool saveFile( QString filename ); + void loadFile( QString filename ); + QString currentFilename; + + QTextEdit *docWidget; + + QAction *newAction; + QAction *openAction; + QAction *saveAction; + QAction *saveAsAction; + QAction *closeAction; + QAction *exitAction; + + QAction *cutAction; + QAction *copyAction; + QAction *pasteAction; + + QAction *aboutAction; + QAction *aboutQtAction; +}; + +#endif // SDIWINDOW_H diff --git a/Chapter11/README.txt b/Chapter11/README.txt new file mode 100644 index 0000000..bc27e22 --- /dev/null +++ b/Chapter11/README.txt @@ -0,0 +1,43 @@ +This file is a part of 1590598318-1.zip containing example source code for the +Foundations of Qt Development book available from APress (ISBN 1590598318). + +These are the examples for chapter 11 - Plugins + +imageplugin + + Listings 11-1, 11-2, 11-3, 11-4, 11-5, 11-6, 11-7, 11-8, 11-9, 11-10, 11-11, + 11-12, 11-13, 11-14 + + Shows how to implement an image format plug-in for reading and writing ASCII + art. + + +customlib + + Listings 11-15, 11-16, 11-17, 11-18, 11-19, 11-20, 11-21, 11-22, 11-23, 11-24, + 11-25 + + Shows how to extend an application with a plugin of your own. + + +staticplugin + + Listings 11-26, 11-27, 11-28, 11-29 + + Shows how to link a plugin statically into an application. + + +factoryplugin + + Listings 11-30, 11-31, 11-32, 11-33, 11-34 + + Shows how to use a factory interface in your plugin to be able to serve + several plugable classes through the same plugin. + + +customplugin + + Listings 11-35, 11-36, 11-37 + + Shows how to interface a plugin directly without involving Qt's plugin + handling classes. \ No newline at end of file diff --git a/Chapter11/customlib/customlib.pro b/Chapter11/customlib/customlib.pro new file mode 100644 index 0000000..abcce76 --- /dev/null +++ b/Chapter11/customlib/customlib.pro @@ -0,0 +1,8 @@ +TEMPLATE = app +TARGET = +DEPENDPATH += . lib +INCLUDEPATH += . + +# Input +SOURCES += main.cpp +CONFIG += console diff --git a/Chapter11/customlib/lib/Makefile b/Chapter11/customlib/lib/Makefile new file mode 100644 index 0000000..5d60f6e --- /dev/null +++ b/Chapter11/customlib/lib/Makefile @@ -0,0 +1,11 @@ +all: sum.dll + +sum.o: sum.c + gcc -c sum.c + +sum.dll: sum.o + gcc -shared -o sum.dll sum.o + +clean: + @del sum.o + @del sum.dll \ No newline at end of file diff --git a/Chapter11/customlib/lib/sum.c b/Chapter11/customlib/lib/sum.c new file mode 100644 index 0000000..3552dba --- /dev/null +++ b/Chapter11/customlib/lib/sum.c @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +int sum( int len, char *data ) +{ + int i; + int sum = 0x5a; + + for( i=0; i + +#include + +typedef int (*SumFunction)(int,char*); + +int main( int argc, char **argv ) +{ + QLibrary library( "lib/sum" ); + + library.load(); + if( !library.isLoaded() ) + { + qDebug() << "Cannot load library."; + return 0; + } + + SumFunction sum = (SumFunction)library.resolve( "sum" ); + if( sum ) + qDebug() << "sum of 'Qt Rocks!' = " << sum( 9, "Qt Rocks!" ); + + return 0; +} diff --git a/Chapter11/customplugin/customplugin.pro b/Chapter11/customplugin/customplugin.pro new file mode 100644 index 0000000..9498aad --- /dev/null +++ b/Chapter11/customplugin/customplugin.pro @@ -0,0 +1,14 @@ +###################################################################### +# Automatically generated by qmake (2.01a) må 11. dec 19:03:41 2006 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +HEADERS += filterdialog.h filterinterface.h +FORMS += filterdialog.ui +SOURCES += filterdialog.cpp main.cpp +CONFIG += console diff --git a/Chapter11/customplugin/filterdialog.cpp b/Chapter11/customplugin/filterdialog.cpp new file mode 100644 index 0000000..14a9f19 --- /dev/null +++ b/Chapter11/customplugin/filterdialog.cpp @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include +#include + +#include "filterinterface.h" + +#include "filterdialog.h" + +FilterDialog::FilterDialog( QWidget *parent ) : QDialog( parent ) +{ + ui.setupUi( this ); + ui.originalLabel->setPixmap( QPixmap( "source.jpeg" ) ); + + connect( ui.filterList, SIGNAL(currentTextChanged(QString)), this, SLOT(filterChanged(QString)) ); + + findFilters(); + filterChanged( QString() ); +} + +void FilterDialog::findFilters() +{ + QDir path( "./plugins" ); + + foreach( QString filename, path.entryList(QDir::Files) ) + { + QPluginLoader loader( path.absoluteFilePath( filename ) ); + QObject *couldBeFilter = loader.instance(); + if( couldBeFilter ) + { + FilterInterface *filter = qobject_cast( couldBeFilter ); + if( filter ) + { + filters[ filter->name() ] = filter; + ui.filterList->addItem( filter->name() ); + } + } + } +} + +void FilterDialog::filterChanged( QString filter ) +{ + if( filter.isEmpty() ) + { + ui.filteredLabel->setPixmap( *(ui.originalLabel->pixmap() ) ); + } + else + { + QImage filtered = filters[ filter ]->filter( ui.originalLabel->pixmap()->toImage() ); + ui.filteredLabel->setPixmap( QPixmap::fromImage( filtered ) ); + } +} diff --git a/Chapter11/customplugin/filterdialog.h b/Chapter11/customplugin/filterdialog.h new file mode 100644 index 0000000..945e4c3 --- /dev/null +++ b/Chapter11/customplugin/filterdialog.h @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef FILTERDIALOG_H +#define FILTERDIALOG_H + +#include +#include +#include + +#include "filterinterface.h" + +#include "ui_filterdialog.h" + +class FilterDialog : public QDialog +{ + Q_OBJECT +public: + FilterDialog( QWidget *parent=0 ); + +private slots: + void filterChanged( QString ); + +private: + void findFilters(); + + QMap filters; + Ui::FilterDialog ui; +}; + +#endif // FILTERDIALOG_H diff --git a/Chapter11/customplugin/filterdialog.ui b/Chapter11/customplugin/filterdialog.ui new file mode 100644 index 0000000..9613740 --- /dev/null +++ b/Chapter11/customplugin/filterdialog.ui @@ -0,0 +1,86 @@ + + + Copyright (c) 2006-2007, Johan Thelin + + All rights reserved. + + Redistribution and use in source and binary forms, with or without modification, + are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + * Neither the name of APress nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + FilterDialog + + + + 0 + 0 + 869 + 502 + + + + Dialog + + + + 9 + + + 6 + + + + + QAbstractItemView::SelectRows + + + + + + + 0 + + + 6 + + + + + + + + + + + + + + + + + + + + + + diff --git a/Chapter11/customplugin/filterinterface.h b/Chapter11/customplugin/filterinterface.h new file mode 100644 index 0000000..1c4d2e2 --- /dev/null +++ b/Chapter11/customplugin/filterinterface.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef FILTERINTERFACE_H +#define FILTERINTERFACE_H + +#include +#include + +class FilterInterface +{ +public: + virtual QString name() const = 0; + virtual QImage filter( const QImage &image ) const = 0; +}; + +Q_DECLARE_INTERFACE( FilterInterface, "se.thelins.CustomPlugin.FilterInterface/0.1" ) + +#endif // FILTERINTERFACE_H diff --git a/Chapter11/customplugin/filters/blur/blur.cpp b/Chapter11/customplugin/filters/blur/blur.cpp new file mode 100644 index 0000000..78e4c8d --- /dev/null +++ b/Chapter11/customplugin/filters/blur/blur.cpp @@ -0,0 +1,98 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include "blur.h" + +QString Blur::name() const +{ + return "Blur"; +} + +QImage Blur::filter( const QImage &image ) const +{ + QImage result( image.width(), image.height(), image.format() ); + + for( int y=0; y= image.width() ) + x = image.width()-1; + + if( y < 0 ) + y = 0; + if( y >= image.height() ) + y = image.height()-1; + + return image.pixel( x, y ); +} + +Q_EXPORT_PLUGIN2( blur, Blur ) diff --git a/Chapter11/customplugin/filters/blur/blur.h b/Chapter11/customplugin/filters/blur/blur.h new file mode 100644 index 0000000..6feec0f --- /dev/null +++ b/Chapter11/customplugin/filters/blur/blur.h @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef BLUR_H +#define BLUR_H + +#include +#include + +#include "filterinterface.h" + +class Blur : public QObject, FilterInterface +{ + Q_OBJECT + Q_INTERFACES(FilterInterface) + +public: + QString name() const; + QImage filter( const QImage &image ) const; + +private: + QRgb getSafePixel( const QImage &image, int x, int y ) const; +}; + +#endif // BLUR_H diff --git a/Chapter11/customplugin/filters/blur/blur.pro b/Chapter11/customplugin/filters/blur/blur.pro new file mode 100644 index 0000000..2fda446 --- /dev/null +++ b/Chapter11/customplugin/filters/blur/blur.pro @@ -0,0 +1,42 @@ +# +# Copyright (c) 2006-2007, Johan Thelin +# +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# * Neither the name of APress nor the names of its contributors +# may be used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# + +TEMPLATE = lib +TARGET = blur +CONFIG += plugin release +VERSION = 1.0.0 + +INCLUDEPATH += ../.. + +HEADERS += blur.h +SOURCES += blur.cpp + +target.path += ../../plugins +INSTALLS += target \ No newline at end of file diff --git a/Chapter11/customplugin/filters/darken/darken.cpp b/Chapter11/customplugin/filters/darken/darken.cpp new file mode 100644 index 0000000..04ff42c --- /dev/null +++ b/Chapter11/customplugin/filters/darken/darken.cpp @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include "darken.h" + +QString Darken::name() const +{ + return "Darken"; +} + +QImage Darken::filter( const QImage &image ) const +{ + QImage result( image.width(), image.height(), image.format() ); + + for( int y=0; y +#include + +#include "filterinterface.h" + +class Darken : public QObject, FilterInterface +{ + Q_OBJECT + Q_INTERFACES(FilterInterface) + +public: + QString name() const; + QImage filter( const QImage &image ) const; +}; + +#endif // DARKEN_H diff --git a/Chapter11/customplugin/filters/darken/darken.pro b/Chapter11/customplugin/filters/darken/darken.pro new file mode 100644 index 0000000..93a6519 --- /dev/null +++ b/Chapter11/customplugin/filters/darken/darken.pro @@ -0,0 +1,42 @@ +# +# Copyright (c) 2006-2007, Johan Thelin +# +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# * Neither the name of APress nor the names of its contributors +# may be used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# + +TEMPLATE = lib +TARGET = darken +CONFIG += plugin release +VERSION = 1.0.0 + +INCLUDEPATH += ../.. + +HEADERS += darken.h +SOURCES += darken.cpp + +target.path += ../../plugins +INSTALLS += target \ No newline at end of file diff --git a/Chapter11/customplugin/filters/flip/flip.cpp b/Chapter11/customplugin/filters/flip/flip.cpp new file mode 100644 index 0000000..be96fd2 --- /dev/null +++ b/Chapter11/customplugin/filters/flip/flip.cpp @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include "flip.h" + +QString Flip::name() const +{ + return "Flip Horizontally"; +} + +QImage Flip::filter( const QImage &image ) const +{ + QImage result( image.width(), image.height(), image.format() ); + + for( int y=0; y +#include + +#include "filterinterface.h" + +class Flip : public QObject, FilterInterface +{ + Q_OBJECT + Q_INTERFACES(FilterInterface) + +public: + QString name() const; + QImage filter( const QImage &image ) const; +}; + +#endif // FLIP_H diff --git a/Chapter11/customplugin/filters/flip/flip.pro b/Chapter11/customplugin/filters/flip/flip.pro new file mode 100644 index 0000000..a33614e --- /dev/null +++ b/Chapter11/customplugin/filters/flip/flip.pro @@ -0,0 +1,42 @@ +# +# Copyright (c) 2006-2007, Johan Thelin +# +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# * Neither the name of APress nor the names of its contributors +# may be used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# + +TEMPLATE = lib +TARGET = flip +CONFIG += plugin release +VERSION = 1.0.0 + +INCLUDEPATH += ../.. + +HEADERS += flip.h +SOURCES += flip.cpp + +target.path += ../../plugins +INSTALLS += target \ No newline at end of file diff --git a/Chapter11/customplugin/main.cpp b/Chapter11/customplugin/main.cpp new file mode 100644 index 0000000..2b2a978 --- /dev/null +++ b/Chapter11/customplugin/main.cpp @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include "filterdialog.h" + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + FilterDialog dlg; + dlg.show(); + + return app.exec(); +} diff --git a/Chapter11/customplugin/source.jpeg b/Chapter11/customplugin/source.jpeg new file mode 100644 index 0000000..0dc33e8 Binary files /dev/null and b/Chapter11/customplugin/source.jpeg differ diff --git a/Chapter11/factoryplugin/customplugin.pro b/Chapter11/factoryplugin/customplugin.pro new file mode 100644 index 0000000..9498aad --- /dev/null +++ b/Chapter11/factoryplugin/customplugin.pro @@ -0,0 +1,14 @@ +###################################################################### +# Automatically generated by qmake (2.01a) må 11. dec 19:03:41 2006 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +HEADERS += filterdialog.h filterinterface.h +FORMS += filterdialog.ui +SOURCES += filterdialog.cpp main.cpp +CONFIG += console diff --git a/Chapter11/factoryplugin/filterdialog.cpp b/Chapter11/factoryplugin/filterdialog.cpp new file mode 100644 index 0000000..80ce3bf --- /dev/null +++ b/Chapter11/factoryplugin/filterdialog.cpp @@ -0,0 +1,98 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include +#include + +#include "filterinterface.h" + +#include "filterdialog.h" + +FilterDialog::FilterDialog( QWidget *parent ) : QDialog( parent ) +{ + ui.setupUi( this ); + ui.originalLabel->setPixmap( QPixmap( "source.jpeg" ) ); + + connect( ui.filterList, SIGNAL(currentTextChanged(QString)), this, SLOT(filterChanged(QString)) ); + + findFilters(); + filterChanged( QString() ); +} + +void FilterDialog::findFilters() +{ + foreach( QObject *couldBeFilter, QPluginLoader::staticInstances() ) + { + FilterInterface *filter = qobject_cast( couldBeFilter ); + if( filter ) + { + foreach( QString name, filter->names() ) + { + filters[ name ] = filter; + ui.filterList->addItem( name ); + } + } + } + + QDir path( "./plugins" ); + + foreach( QString filename, path.entryList(QDir::Files) ) + { + QPluginLoader loader( path.absoluteFilePath( filename ) ); + QObject *couldBeFilter = loader.instance(); + if( couldBeFilter ) + { + FilterInterface *filter = qobject_cast( couldBeFilter ); + if( filter ) + { + foreach( QString name, filter->names() ) + { + filters[ name ] = filter; + ui.filterList->addItem( name ); + } + } + } + } +} + +void FilterDialog::filterChanged( QString filter ) +{ + if( filter.isEmpty() ) + { + ui.filteredLabel->setPixmap( *(ui.originalLabel->pixmap() ) ); + } + else + { + QImage filtered = filters[ filter ]->filter( filter, ui.originalLabel->pixmap()->toImage() ); + ui.filteredLabel->setPixmap( QPixmap::fromImage( filtered ) ); + } +} diff --git a/Chapter11/factoryplugin/filterdialog.h b/Chapter11/factoryplugin/filterdialog.h new file mode 100644 index 0000000..945e4c3 --- /dev/null +++ b/Chapter11/factoryplugin/filterdialog.h @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef FILTERDIALOG_H +#define FILTERDIALOG_H + +#include +#include +#include + +#include "filterinterface.h" + +#include "ui_filterdialog.h" + +class FilterDialog : public QDialog +{ + Q_OBJECT +public: + FilterDialog( QWidget *parent=0 ); + +private slots: + void filterChanged( QString ); + +private: + void findFilters(); + + QMap filters; + Ui::FilterDialog ui; +}; + +#endif // FILTERDIALOG_H diff --git a/Chapter11/factoryplugin/filterdialog.ui b/Chapter11/factoryplugin/filterdialog.ui new file mode 100644 index 0000000..9613740 --- /dev/null +++ b/Chapter11/factoryplugin/filterdialog.ui @@ -0,0 +1,86 @@ + + + Copyright (c) 2006-2007, Johan Thelin + + All rights reserved. + + Redistribution and use in source and binary forms, with or without modification, + are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + * Neither the name of APress nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + FilterDialog + + + + 0 + 0 + 869 + 502 + + + + Dialog + + + + 9 + + + 6 + + + + + QAbstractItemView::SelectRows + + + + + + + 0 + + + 6 + + + + + + + + + + + + + + + + + + + + + + diff --git a/Chapter11/factoryplugin/filterinterface.h b/Chapter11/factoryplugin/filterinterface.h new file mode 100644 index 0000000..2671077 --- /dev/null +++ b/Chapter11/factoryplugin/filterinterface.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef FILTERINTERFACE_H +#define FILTERINTERFACE_H + +#include +#include + +class FilterInterface +{ +public: + virtual QStringList names() const = 0; + virtual QImage filter( const QString &filter, const QImage &image ) const = 0; +}; + +Q_DECLARE_INTERFACE( FilterInterface, "se.thelins.CustomPlugin.FilterInterface/0.2" ) + +#endif // FILTERINTERFACE_H diff --git a/Chapter11/factoryplugin/filters/blur/blur.cpp b/Chapter11/factoryplugin/filters/blur/blur.cpp new file mode 100644 index 0000000..17a89b4 --- /dev/null +++ b/Chapter11/factoryplugin/filters/blur/blur.cpp @@ -0,0 +1,98 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include "blur.h" + +QStringList Blur::names() const +{ + return QStringList() << "Blur"; +} + +QImage Blur::filter( const QString &filter, const QImage &image ) const +{ + QImage result( image.width(), image.height(), image.format() ); + + for( int y=0; y= image.width() ) + x = image.width()-1; + + if( y < 0 ) + y = 0; + if( y >= image.height() ) + y = image.height()-1; + + return image.pixel( x, y ); +} + +Q_EXPORT_PLUGIN2( blur, Blur ) diff --git a/Chapter11/factoryplugin/filters/blur/blur.h b/Chapter11/factoryplugin/filters/blur/blur.h new file mode 100644 index 0000000..09e1e58 --- /dev/null +++ b/Chapter11/factoryplugin/filters/blur/blur.h @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef BLUR_H +#define BLUR_H + +#include +#include +#include + +#include "filterinterface.h" + +class Blur : public QObject, FilterInterface +{ + Q_OBJECT + Q_INTERFACES(FilterInterface) + +public: + QStringList names() const; + QImage filter( const QString &filter, const QImage &image ) const; + +private: + QRgb getSafePixel( const QImage &image, int x, int y ) const; +}; + +#endif // BLUR_H diff --git a/Chapter11/factoryplugin/filters/blur/blur.pro b/Chapter11/factoryplugin/filters/blur/blur.pro new file mode 100644 index 0000000..2fda446 --- /dev/null +++ b/Chapter11/factoryplugin/filters/blur/blur.pro @@ -0,0 +1,42 @@ +# +# Copyright (c) 2006-2007, Johan Thelin +# +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# * Neither the name of APress nor the names of its contributors +# may be used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# + +TEMPLATE = lib +TARGET = blur +CONFIG += plugin release +VERSION = 1.0.0 + +INCLUDEPATH += ../.. + +HEADERS += blur.h +SOURCES += blur.cpp + +target.path += ../../plugins +INSTALLS += target \ No newline at end of file diff --git a/Chapter11/factoryplugin/filters/darken/darken.cpp b/Chapter11/factoryplugin/filters/darken/darken.cpp new file mode 100644 index 0000000..b3865f0 --- /dev/null +++ b/Chapter11/factoryplugin/filters/darken/darken.cpp @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include "darken.h" + +QStringList Darken::names() const +{ + return QStringList() << "Darken"; +} + +QImage Darken::filter( const QString &filter, const QImage &image ) const +{ + QImage result( image.width(), image.height(), image.format() ); + + for( int y=0; y +#include +#include + +#include "filterinterface.h" + +class Darken : public QObject, FilterInterface +{ + Q_OBJECT + Q_INTERFACES(FilterInterface) + +public: + QStringList names() const; + QImage filter( const QString &filter, const QImage &image ) const; +}; + +#endif // DARKEN_H diff --git a/Chapter11/factoryplugin/filters/darken/darken.pro b/Chapter11/factoryplugin/filters/darken/darken.pro new file mode 100644 index 0000000..93a6519 --- /dev/null +++ b/Chapter11/factoryplugin/filters/darken/darken.pro @@ -0,0 +1,42 @@ +# +# Copyright (c) 2006-2007, Johan Thelin +# +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# * Neither the name of APress nor the names of its contributors +# may be used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# + +TEMPLATE = lib +TARGET = darken +CONFIG += plugin release +VERSION = 1.0.0 + +INCLUDEPATH += ../.. + +HEADERS += darken.h +SOURCES += darken.cpp + +target.path += ../../plugins +INSTALLS += target \ No newline at end of file diff --git a/Chapter11/factoryplugin/filters/flip/flip.cpp b/Chapter11/factoryplugin/filters/flip/flip.cpp new file mode 100644 index 0000000..839ff00 --- /dev/null +++ b/Chapter11/factoryplugin/filters/flip/flip.cpp @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include "flip.h" + +QStringList Flip::names() const +{ + return QStringList() << "Flip Horizontally" << "Flip Vertically"; +} + +QImage Flip::filter( const QString &filter, const QImage &image ) const +{ + bool horizontally = (filter=="Flip Horizontally"); + + QImage result( image.width(), image.height(), image.format() ); + + for( int y=0; y +#include +#include + +#include "filterinterface.h" + +class Flip : public QObject, FilterInterface +{ + Q_OBJECT + Q_INTERFACES(FilterInterface) + +public: + QStringList names() const; + QImage filter( const QString &filter, const QImage &image ) const; +}; + +#endif // FLIP_H diff --git a/Chapter11/factoryplugin/filters/flip/flip.pro b/Chapter11/factoryplugin/filters/flip/flip.pro new file mode 100644 index 0000000..a33614e --- /dev/null +++ b/Chapter11/factoryplugin/filters/flip/flip.pro @@ -0,0 +1,42 @@ +# +# Copyright (c) 2006-2007, Johan Thelin +# +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# * Neither the name of APress nor the names of its contributors +# may be used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# + +TEMPLATE = lib +TARGET = flip +CONFIG += plugin release +VERSION = 1.0.0 + +INCLUDEPATH += ../.. + +HEADERS += flip.h +SOURCES += flip.cpp + +target.path += ../../plugins +INSTALLS += target \ No newline at end of file diff --git a/Chapter11/factoryplugin/main.cpp b/Chapter11/factoryplugin/main.cpp new file mode 100644 index 0000000..2b2a978 --- /dev/null +++ b/Chapter11/factoryplugin/main.cpp @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include "filterdialog.h" + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + FilterDialog dlg; + dlg.show(); + + return app.exec(); +} diff --git a/Chapter11/factoryplugin/source.jpeg b/Chapter11/factoryplugin/source.jpeg new file mode 100644 index 0000000..0dc33e8 Binary files /dev/null and b/Chapter11/factoryplugin/source.jpeg differ diff --git a/Chapter11/imageplugin/imageplugin.pro b/Chapter11/imageplugin/imageplugin.pro new file mode 100644 index 0000000..7e7d69c --- /dev/null +++ b/Chapter11/imageplugin/imageplugin.pro @@ -0,0 +1,40 @@ +# +# Copyright (c) 2006-2007, Johan Thelin +# +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# * Neither the name of APress nor the names of its contributors +# may be used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# + +TEMPLATE = lib +TARGET = textimage +CONFIG += plugin release +VERSION = 1.0.0 + +HEADERS += textimagehandler.h textimageplugin.h +SOURCES += textimagehandler.cpp textimageplugin.cpp + +target.path += $$[QT_INSTALL_PLUGINS]/imageformats +INSTALLS += target \ No newline at end of file diff --git a/Chapter11/imageplugin/testread/Thumbs.db b/Chapter11/imageplugin/testread/Thumbs.db new file mode 100644 index 0000000..039dbc9 Binary files /dev/null and b/Chapter11/imageplugin/testread/Thumbs.db differ diff --git a/Chapter11/imageplugin/testread/input.ti b/Chapter11/imageplugin/testread/input.ti new file mode 100644 index 0000000..2c21762 --- /dev/null +++ b/Chapter11/imageplugin/testread/input.ti @@ -0,0 +1,86 @@ +TEXT +100x84 +iiiiiiiiiiiiiiiiiiNMMMMMMMAllNNAAAANllllllllllllliiiiiiiiiiiiiiiiiiiiiiiiAMAMMMMMNiiiAMMAilAMMMMMMMM +iiiiiiiiiiiiiiiiiilMMMMMAMMNilNAAANllllllllNANNNliiiiiiiiiiiiiiiiiiiiiiiiNMAMMMMMANllAMMAiiAMMMMMMMM +iiiiiiiiiiiiiiiiiilAMAMMAAMAlilAANNlllllllAAAAAANlliiiiiiiiiiiiiiiiiiiiiilAAMMMMMMAllAMMNiiAMMMMMMMM +iiiiiiiiiiiiiiiiiilAMMMMANAMNliNANllllllNAAAAAANlNNliiiiiiiiiiiiiiiiilllllAAMMMMMMNilMMMNilMMMMMMMMA +iiiiiiiiiiiiiiiiiiiNMMMMANNMAliNNNlllllNAAAANlllllNNliiiiiiiiiiiiiiilAAAllAMMMMMMMliNMMMllAMMMMMMMMN +iiiiiiiiiiiiiiiiiiilAMMMMAlAAllNNNNlllNAAAANlllllllNlliiiiiiiiiiiiiiNAAAlNMMMMMMMAliNMMMNlMMMMMMMMAl +iiiiiiiiiiiiiiiiiiiiNAMMMANNMAlNNNNllNAAANNllllllllNllllliiiiiiiiiilAAAMAMMMMMMMMAllAMMMNAMMMMMMMMAi +iiiiiiiiiiiiiiiiiiiiiAMMMMANAAllNNNNNNAANllllllllllNNllNNNNNlllliilNAMAMMMMMMMMMMAlNMMMAAMMMMMMMMMNi +iiiiiiiiiiiiiiiiiiiiiNMMMMMNAMNlNNNNNAAANlllllllllNAANNAAAAAAAAANllAAAMMMMMMMMMMMMNAMMMMMMMMMMMMMMli +iiiiiiiiiiiiiiiiiiiiilAMMMMANMANNNNNAAANNllllllllNANNNAAAAAAAAAANllAANMMMMMMMMMMMMMMMMMMMMMMMMMMMAii +iiiiiiiiiiiiiiiiiiiiiiNMMMMMAMANNNNNAMANlllllllNAANNNAMMMAAMMMMMANlAAAMMMMMMMMMMMMMMMMMMMMMMMMMMMNii +iiiiiiiiiiiiiiiiiiiiiilAMMMMMMANNNNNAANNNlllllNAANNNNAMMMMMMMMMMMMAAMMMMMMMMMMMMMMMMMMMMMMMMMMMMMlii +iiiiiiiiiiiiiiiiiiiiiilAMMMMMMANNNNAMANNllllNAAANlNAAAAMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMAlii +iiiiiiiiiiiiiiiiiiiiiilAMMMMMAAANNNNNNNNNllNNAANllNAMANNAMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNiii +iiiiiiiiiiiiiiiiiiiiiilAMAAAANNllllllllllNNNAANlllAMANllNAMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMliii +iiiiiiiiiiiiiiiiiiiiiilAMANNNllllllllllllllAMANllNAANlllNNAMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMAiii +iiiiiiiiiiiiiiiiiiiiiilAANllllllNlllllllllllNNNlNAMANlllNNNAMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMAiii +iiiiiiiiiiiiiiiiiiiiiilNNllllllllNllllllllllllNNNMANlllNNAAMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMAiii +iiiiiiiiiiiiiiiiiiiiiilllllllllllNNllllllllllllNAMANNNNAAMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNllN +iiiiiiiiiiiiiiiiiiiiillllllllllllllllllllllllllNMANNNNAMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNNNM +iiiiiiiiiiiiiiiiiiiilllllllllllllllllllllllllllNMANNNAMMAMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMAAMM +iiiiiiiiiiiiiiiiiillllllliiilllllllllllllllllllNANNNNAAAAMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNAMMM +iiiiiiiiiiiiiiiiilllNlllliiilllllllllNlllllllllNNNNNNNAMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNAMMM +iiiiiiiiiiiiiiiillNNNllllllllllllllllNNNlllllllNNANNNAMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMAMMMM +iiiiiiiiiiiiiilllNNNllllllllllllllllllNNlllllllNNANNAMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM +iiiiiiiiiiiiillNNNNNlNNllllllllllllllllNNNllllNNNANNAMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM +iiiiiiiiiiilllNNNNNNlANllNNNlllllllllllNNNNNNNNNNAAAAMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM +iiiiiiiiiillllNNNNNNlllNNNNllliiillNNNlNNNNNNNNNNAAAAMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM +iiiiiiiillllNNNNNNNNNNNNNNllllllllNNNNNNNNNNNNNNAAAAAAMMMMMMAAMMMMMMMMMMMMAAMMMMMMMMMMMMMMMMMMMMMMMM +iiiiiiillllNNNNNNNNNNNNNNlllllllllNNNNNNNNNNNNNNAAAAAAMMMMMAAMMMMMMMMMMMAAAAAMMMMMMMMMMMMMMMMMMMMMMM +iiiiillllNNNNNNNNNNNNNNNNllllllllNNNNNNNNNNNNNNNAAAAAMMMMMAAMMMMMMMMMMMAAAAANAAAMMMMMMMMMMMMMMMMMMMM +iiilllllNNNNNNNNNNNNNNNNNlNNlllNNNNNNNNNNNNNNNNNAAAAAMMMMMAAMMMMMMMMMMAAAAAAAAAAAMMMMMMMMMMMMMMMMMMM +llllllNNNNNNNNNNNNNNNNNNNNAANNNNNNNNNNNNNNNNNNNAAAAAMMMMMMMMMMMMMMMAAAAAAAAMAAAMAMMMMMMMMMMMMMMMMMMM +lllllNNlNNNNNNNNNNNNNNNNNlNNNNNNNNNNNNNNNNNNNNNAAAAAMMMMMMMMMMMMMMAAAAAAAAAMMMMMMMMMMMMMMMMMMMMMMMMM +lllllNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNAAAAAMMMMMMMMMMMMMAAAAAAAAAAMMMMMMMMMMMMMMMMMMMMMMMMM +llNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNAAAAAMMMMMMMMMMMMMAAAANNAAAAAAMMMMMMMMMMMMMMMMMMMMMMMM +NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNAAAAAMMMMMMMMMMMMAAAANNNNAAAAAAAMMAMMMMMMMMMMMMMMMMMMM +NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNAAAAAAMMMMMMMMMMMMAANNNNNNNNAAAAAAAAAMMMMMMMMMMMMMMMMMM +NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNAAAAAAAMMMMMMMMMMMAANNNNNNNNNAAAAAAAAAMMMMMMMMMMMMMMMMMM +NNNNNNNNNNNNNNNANNNNNNNNNNNNNNNNNNNNNNNNNNNNAAANNNAMMMMAMMMMMMAANNNNNNNNNNAAAAAAAAMMMMMMMMMMMMMMMMMM +NNNNNNNNNNNNNNNNANNNNNNNNNNNNNNNNNNNNNNNNNNNAAAllNAMMAAAMMMMMAANNNNNNNNNNNNNAAAAAAMMMMMMMMMMMMMMMMMM +NNNNNNNNNNNNNNNNNANNNNNNNNNNNNNNNNNNNNNNNNNAAANllNMMMAAAAMMMAANNNNNNNNNNNNNNNNAAAAMMMAMMMMMMMMMMMMMM +lNNNNNNNNNNNNNNNNNNANNNNNNNNNNNNNNNNNNNNNNNAAAlllAMMMAAAAAMMMANNNNNNNNNNNNNNNNNNNNAAAAAMMMMMMMMMMMMM +lNNNNNNNNNNNNNNNNNNNNAAAANNNNNNNNNNNNNNNNNANNNlllAAMMMAAAAAMAANNNNNNNNNNNNNNNNNNNNAAAAAAMMAAMMMMMMMM +NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNllAAAMMMMMAAAAANNNNNNNNNNNNNNNNNNNNNNNNAAAAAAAMMMMMMMM +NNNNNNNNNNNNNNNNNNNNNNNNNNNNANNNNNNNNNNNNANNNlllAAAAAMMMMAAAAANNNNNNllllNNlNNNNNNNNNNAAANAAAAMMMMMMM +NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNAANNNNNNNNNNNANllNMAAAAAMMMMAAANNNNNNliiiiiiilNNNNNNNNNNNAAAAAMMMMMMMM +NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNANANNNNNNNNNNANlNAMMAAAAAAMMMMAAAAANliiiiiiiilNNNNNNNNNNAAMMAAAMMAMMMM +NNNNNNNNNNNNNNNNNNNNNNNNNNNNANNNNNNNNNNNANNNlNAAMMAAAAAAAMMMMMAANliiiiiiilllNNNNNNNNAAAAAAAAAAAAMMMM +NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNANNNNNNNNNNNNNlNAAMMMAAAAAAAAAAANNliiiiillllNNNNNNNNNNNAAAAAAAAAAAMMAA +NNNNNNNNNNNNNNNNNNNNNNNNNNNNNAANANNNNNNNNNNllAAAAMMMAAAANNNNNNlliiilllllNNNNNANNNNNNNAAAAAMMMMAAAMMA +NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNANlNAAAAMMMMAAANNNNNlliilllllNNNNNNNAANNNNNNAAAAMMMMMAANAAA +NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNANNNNNAMMAAAMMMMAANNNNNlllllllNNNNNNNNAAANNNNNNAAAAMMMMAAANNNA +NNNNNNNNNNNNNNNNNNNNNNNNANNAANNNNNNNNNANNNNNAMMMAAAMMMMAANNNlllllllNNNNNNNAAAAAAANNNNNAAAAAMAAANNNlN +NNNNNNNNNNNNNNNNNNNNNNNNNNNAANNNNNNNNANNNNNAAMMMMAAAMMMMMAANlllllNNNNNNNNNAAAAMMAAAAAANAAAAAAAANNNll +NNNNllllNNNNNNNNNNNNNNNANNNNNNNNNNNNNANNNNAAAMMMMMAAAAMMMAANlllNNNNNNNNNNAAAAAAAAANAAAAAAAAAAANNNNll +NNNNlllllNNNNNNNNNNNNNANNAAAAANNNNNNANNNAAAAAAMMMMAAAAAAANNlllNNNNNNNNNANAAAAAAAANNNAAANNAAAAANNNNNl +NNNNNllllNNNNNNNNNNNNNNNNNNNNANNNNNNANNAAAAAAAAAMMMMAAANNNllNNNNNNNNNNNAAAAAAAAAAAANNNNANNAANNNNNNNl +NNNNNNllllNNNNNNNNNNNNNANANNNNNNNNNAANAAAAAAAAAAAAMMMMAAANNNNNNNNNNNNNNNNNNAAAAAAAANNNNNNNNNNNNNNNNl +NNANNNNllllNNNNANNNNNNNNNNNNNNNANNNANNAAAAAAAAAAAAAAAAAAANNNNNNNNNNNNNNNNNNNNNAAAAANNNNNNNNNNNNNNNNN +NAAANNNNllllNNNNNNNANNNNNNNNNNNNNNANNNAAAAAAAAAAAAANNNNNNNNNNNNNNNNANNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN +NAAAAANNNlllllNNNNNNNNNNNNNNNNNNNNANNAAMAAAAAAAAANNNNNNNNNNNNNNNANNNNNNNNlllllllllllNNNNNNNNNNNNNNNN +NAAAAAANNNllllllNNNNNNNNNNNNNNNNNAANAAMMMAAAAAAANNNNNNNNNNNNAAAANANNNNNNlllliiiiiiilNNNNNNNNNNNNNNNN +AAAAAAAAANNNllllllllNNNNNNNNNNNNNAAAAAMMMAAAAAAANNNNNNNNNNNNNNNANNNNNNllllliiiiiiiiilNNNNNNNNNNNNNNN +AAAAAAAAAANNNNlllllllNNNNNNNNNNNAANAAAMMMAAAAAANNNNNNNNANNNNNNNNNNNNNllllliiiiiiiiiilNNNNNNNNNAAANNN +AAAAAAAAAAAANNNNNNNNNNNNNNNNNNNNANAAAMMMAAAAAAANNNNNNNNAANAANANNNNNNlllliiiiiiiiiiiiilNNNNNNNAAAAANN +AAAAAAAAAAAAANNNNNNNNNNNNNNNNNNAAAAAAMMAAAAAAAANNNNNNAANAANANANNNNNllllliiiiiiiiiiiiilNNNNNNNAMMMMAN +AAAAAAAAAAAAAAAAAANNNNNNNNNAANAAAAAAAMMAAAAAAMAAANNNNAAAAAANNNNNNNNllllliiiiiiiiiiiiiilNNNNNNAMMMANN +AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMMMMMMMMMMAAANNNNNAANNNNNNNNNNllllliiiiiiiiiiiiiiiilNNNNNNAAANNN +AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMMMMMMMMMAAAANNNNAAAANNNNNNNNlllllliiiiiiiiiiiiiiiillNNNNNNNNNNN +AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMMMMMMMAAAAANNNNNAANNNNNNNNNllllllliiiiiiiiiiiiiiiiiillNNNNNNNNN +AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMMMMMMMAAAAANNNNNNNNNNNNNNNNlllllllliiiiiiiiiiiiiiiiiiillllllllll +AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMMMMAAAAAAAANNNNNNNNNNNNNNNllllllllliiiiiiiiiiiiiiiiiiiiiiiiiiiii +AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMMMMMMAAAAAAANNNNNNNNNNNNNllllllllllliiiiiiiiiiiiiiiiiiiiiiiiiiiii +AAAAAAAAAAAAAAAAAAAANNAAAAAAAAAAAAMMMMMMMAAAAAANNNNNNNNNNNNllllllllllliiiiiiiiiiiiiiiiiiiiiiiiiiiiii +AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMMMMMMMMAAAAAANNNNNNNNNNNlllllllllllliiiiiiiiiiiiiiiiiiiiiiiiiiiiii +AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMMMMMMMMMAAAAAANNNNNNNNNllllllllllllliiiiiiiiiiiiiiiiiiiiiiiiiiiiii +AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMMMMMMMMMMMMAAAANNNNNNNNNllllllllllllliiiiiiiiiiiiiiiiiiiiiiiiiiiiii +AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMMMMMMMMMMMMMMAANNNNNNNNNllllllllllllliiiiiiiiiiiiiiiiiiiiiiiiiiiiii +AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMAAMAMMMMMMMMMMMAANNNNNNNllllllllllllliiiiiiiiiiiiiiiiiiiiiiiiiiiiiii +AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMMMMMMMMAAANNNNNlllllllllllliiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii +AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMMMAMAAAANNNNllllllllllllliiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii +AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAANNNNNNlllllllllllliiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii +AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAANNNNNlllllllllllliiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii diff --git a/Chapter11/imageplugin/testread/main.cpp b/Chapter11/imageplugin/testread/main.cpp new file mode 100644 index 0000000..126aff5 --- /dev/null +++ b/Chapter11/imageplugin/testread/main.cpp @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include +#include +#include + +#include + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + foreach( QByteArray ba, QImageReader::supportedImageFormats () ) + qDebug() << ba; + + QImage input( "input.ti" ); + if( input.isNull() ) + qDebug() << "Failed to load."; + else + if( !input.save( "test.png", "png" ) ) + qDebug() << "Failed to save."; + + return 0; +} diff --git a/Chapter11/imageplugin/testread/test.png b/Chapter11/imageplugin/testread/test.png new file mode 100644 index 0000000..468fd91 Binary files /dev/null and b/Chapter11/imageplugin/testread/test.png differ diff --git a/Chapter11/imageplugin/testread/testread.pro b/Chapter11/imageplugin/testread/testread.pro new file mode 100644 index 0000000..fed52b0 --- /dev/null +++ b/Chapter11/imageplugin/testread/testread.pro @@ -0,0 +1,12 @@ +###################################################################### +# Automatically generated by qmake (2.01a) lö 9. dec 12:29:02 2006 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp +CONFIG += console diff --git a/Chapter11/imageplugin/testwrite/Thumbs.db b/Chapter11/imageplugin/testwrite/Thumbs.db new file mode 100644 index 0000000..039dbc9 Binary files /dev/null and b/Chapter11/imageplugin/testwrite/Thumbs.db differ diff --git a/Chapter11/imageplugin/testwrite/input.png b/Chapter11/imageplugin/testwrite/input.png new file mode 100644 index 0000000..b3a5c45 Binary files /dev/null and b/Chapter11/imageplugin/testwrite/input.png differ diff --git a/Chapter11/imageplugin/testwrite/main.cpp b/Chapter11/imageplugin/testwrite/main.cpp new file mode 100644 index 0000000..a849fb4 --- /dev/null +++ b/Chapter11/imageplugin/testwrite/main.cpp @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include +#include +#include + +#include + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + foreach( QByteArray ba, QImageReader::supportedImageFormats () ) + qDebug() << ba; + + QImage input( "input.png" ); + if( input.isNull() ) + qDebug() << "Failed to load."; + else + if( !input.save( "test.ti", "ti" ) ) + qDebug() << "Failed to save."; + + return 0; +} diff --git a/Chapter11/imageplugin/testwrite/test.ti b/Chapter11/imageplugin/testwrite/test.ti new file mode 100644 index 0000000..2c21762 --- /dev/null +++ b/Chapter11/imageplugin/testwrite/test.ti @@ -0,0 +1,86 @@ +TEXT +100x84 +iiiiiiiiiiiiiiiiiiNMMMMMMMAllNNAAAANllllllllllllliiiiiiiiiiiiiiiiiiiiiiiiAMAMMMMMNiiiAMMAilAMMMMMMMM +iiiiiiiiiiiiiiiiiilMMMMMAMMNilNAAANllllllllNANNNliiiiiiiiiiiiiiiiiiiiiiiiNMAMMMMMANllAMMAiiAMMMMMMMM +iiiiiiiiiiiiiiiiiilAMAMMAAMAlilAANNlllllllAAAAAANlliiiiiiiiiiiiiiiiiiiiiilAAMMMMMMAllAMMNiiAMMMMMMMM +iiiiiiiiiiiiiiiiiilAMMMMANAMNliNANllllllNAAAAAANlNNliiiiiiiiiiiiiiiiilllllAAMMMMMMNilMMMNilMMMMMMMMA +iiiiiiiiiiiiiiiiiiiNMMMMANNMAliNNNlllllNAAAANlllllNNliiiiiiiiiiiiiiilAAAllAMMMMMMMliNMMMllAMMMMMMMMN +iiiiiiiiiiiiiiiiiiilAMMMMAlAAllNNNNlllNAAAANlllllllNlliiiiiiiiiiiiiiNAAAlNMMMMMMMAliNMMMNlMMMMMMMMAl +iiiiiiiiiiiiiiiiiiiiNAMMMANNMAlNNNNllNAAANNllllllllNllllliiiiiiiiiilAAAMAMMMMMMMMAllAMMMNAMMMMMMMMAi +iiiiiiiiiiiiiiiiiiiiiAMMMMANAAllNNNNNNAANllllllllllNNllNNNNNlllliilNAMAMMMMMMMMMMAlNMMMAAMMMMMMMMMNi +iiiiiiiiiiiiiiiiiiiiiNMMMMMNAMNlNNNNNAAANlllllllllNAANNAAAAAAAAANllAAAMMMMMMMMMMMMNAMMMMMMMMMMMMMMli +iiiiiiiiiiiiiiiiiiiiilAMMMMANMANNNNNAAANNllllllllNANNNAAAAAAAAAANllAANMMMMMMMMMMMMMMMMMMMMMMMMMMMAii +iiiiiiiiiiiiiiiiiiiiiiNMMMMMAMANNNNNAMANlllllllNAANNNAMMMAAMMMMMANlAAAMMMMMMMMMMMMMMMMMMMMMMMMMMMNii +iiiiiiiiiiiiiiiiiiiiiilAMMMMMMANNNNNAANNNlllllNAANNNNAMMMMMMMMMMMMAAMMMMMMMMMMMMMMMMMMMMMMMMMMMMMlii +iiiiiiiiiiiiiiiiiiiiiilAMMMMMMANNNNAMANNllllNAAANlNAAAAMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMAlii +iiiiiiiiiiiiiiiiiiiiiilAMMMMMAAANNNNNNNNNllNNAANllNAMANNAMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNiii +iiiiiiiiiiiiiiiiiiiiiilAMAAAANNllllllllllNNNAANlllAMANllNAMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMliii +iiiiiiiiiiiiiiiiiiiiiilAMANNNllllllllllllllAMANllNAANlllNNAMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMAiii +iiiiiiiiiiiiiiiiiiiiiilAANllllllNlllllllllllNNNlNAMANlllNNNAMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMAiii +iiiiiiiiiiiiiiiiiiiiiilNNllllllllNllllllllllllNNNMANlllNNAAMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMAiii +iiiiiiiiiiiiiiiiiiiiiilllllllllllNNllllllllllllNAMANNNNAAMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNllN +iiiiiiiiiiiiiiiiiiiiillllllllllllllllllllllllllNMANNNNAMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNNNM +iiiiiiiiiiiiiiiiiiiilllllllllllllllllllllllllllNMANNNAMMAMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMAAMM +iiiiiiiiiiiiiiiiiillllllliiilllllllllllllllllllNANNNNAAAAMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNAMMM +iiiiiiiiiiiiiiiiilllNlllliiilllllllllNlllllllllNNNNNNNAMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNAMMM +iiiiiiiiiiiiiiiillNNNllllllllllllllllNNNlllllllNNANNNAMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMAMMMM +iiiiiiiiiiiiiilllNNNllllllllllllllllllNNlllllllNNANNAMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM +iiiiiiiiiiiiillNNNNNlNNllllllllllllllllNNNllllNNNANNAMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM +iiiiiiiiiiilllNNNNNNlANllNNNlllllllllllNNNNNNNNNNAAAAMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM +iiiiiiiiiillllNNNNNNlllNNNNllliiillNNNlNNNNNNNNNNAAAAMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM +iiiiiiiillllNNNNNNNNNNNNNNllllllllNNNNNNNNNNNNNNAAAAAAMMMMMMAAMMMMMMMMMMMMAAMMMMMMMMMMMMMMMMMMMMMMMM +iiiiiiillllNNNNNNNNNNNNNNlllllllllNNNNNNNNNNNNNNAAAAAAMMMMMAAMMMMMMMMMMMAAAAAMMMMMMMMMMMMMMMMMMMMMMM +iiiiillllNNNNNNNNNNNNNNNNllllllllNNNNNNNNNNNNNNNAAAAAMMMMMAAMMMMMMMMMMMAAAAANAAAMMMMMMMMMMMMMMMMMMMM +iiilllllNNNNNNNNNNNNNNNNNlNNlllNNNNNNNNNNNNNNNNNAAAAAMMMMMAAMMMMMMMMMMAAAAAAAAAAAMMMMMMMMMMMMMMMMMMM +llllllNNNNNNNNNNNNNNNNNNNNAANNNNNNNNNNNNNNNNNNNAAAAAMMMMMMMMMMMMMMMAAAAAAAAMAAAMAMMMMMMMMMMMMMMMMMMM +lllllNNlNNNNNNNNNNNNNNNNNlNNNNNNNNNNNNNNNNNNNNNAAAAAMMMMMMMMMMMMMMAAAAAAAAAMMMMMMMMMMMMMMMMMMMMMMMMM +lllllNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNAAAAAMMMMMMMMMMMMMAAAAAAAAAAMMMMMMMMMMMMMMMMMMMMMMMMM +llNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNAAAAAMMMMMMMMMMMMMAAAANNAAAAAAMMMMMMMMMMMMMMMMMMMMMMMM +NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNAAAAAMMMMMMMMMMMMAAAANNNNAAAAAAAMMAMMMMMMMMMMMMMMMMMMM +NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNAAAAAAMMMMMMMMMMMMAANNNNNNNNAAAAAAAAAMMMMMMMMMMMMMMMMMM +NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNAAAAAAAMMMMMMMMMMMAANNNNNNNNNAAAAAAAAAMMMMMMMMMMMMMMMMMM +NNNNNNNNNNNNNNNANNNNNNNNNNNNNNNNNNNNNNNNNNNNAAANNNAMMMMAMMMMMMAANNNNNNNNNNAAAAAAAAMMMMMMMMMMMMMMMMMM +NNNNNNNNNNNNNNNNANNNNNNNNNNNNNNNNNNNNNNNNNNNAAAllNAMMAAAMMMMMAANNNNNNNNNNNNNAAAAAAMMMMMMMMMMMMMMMMMM +NNNNNNNNNNNNNNNNNANNNNNNNNNNNNNNNNNNNNNNNNNAAANllNMMMAAAAMMMAANNNNNNNNNNNNNNNNAAAAMMMAMMMMMMMMMMMMMM +lNNNNNNNNNNNNNNNNNNANNNNNNNNNNNNNNNNNNNNNNNAAAlllAMMMAAAAAMMMANNNNNNNNNNNNNNNNNNNNAAAAAMMMMMMMMMMMMM +lNNNNNNNNNNNNNNNNNNNNAAAANNNNNNNNNNNNNNNNNANNNlllAAMMMAAAAAMAANNNNNNNNNNNNNNNNNNNNAAAAAAMMAAMMMMMMMM +NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNllAAAMMMMMAAAAANNNNNNNNNNNNNNNNNNNNNNNNAAAAAAAMMMMMMMM +NNNNNNNNNNNNNNNNNNNNNNNNNNNNANNNNNNNNNNNNANNNlllAAAAAMMMMAAAAANNNNNNllllNNlNNNNNNNNNNAAANAAAAMMMMMMM +NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNAANNNNNNNNNNNANllNMAAAAAMMMMAAANNNNNNliiiiiiilNNNNNNNNNNNAAAAAMMMMMMMM +NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNANANNNNNNNNNNANlNAMMAAAAAAMMMMAAAAANliiiiiiiilNNNNNNNNNNAAMMAAAMMAMMMM +NNNNNNNNNNNNNNNNNNNNNNNNNNNNANNNNNNNNNNNANNNlNAAMMAAAAAAAMMMMMAANliiiiiiilllNNNNNNNNAAAAAAAAAAAAMMMM +NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNANNNNNNNNNNNNNlNAAMMMAAAAAAAAAAANNliiiiillllNNNNNNNNNNNAAAAAAAAAAAMMAA +NNNNNNNNNNNNNNNNNNNNNNNNNNNNNAANANNNNNNNNNNllAAAAMMMAAAANNNNNNlliiilllllNNNNNANNNNNNNAAAAAMMMMAAAMMA +NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNANlNAAAAMMMMAAANNNNNlliilllllNNNNNNNAANNNNNNAAAAMMMMMAANAAA +NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNANNNNNAMMAAAMMMMAANNNNNlllllllNNNNNNNNAAANNNNNNAAAAMMMMAAANNNA +NNNNNNNNNNNNNNNNNNNNNNNNANNAANNNNNNNNNANNNNNAMMMAAAMMMMAANNNlllllllNNNNNNNAAAAAAANNNNNAAAAAMAAANNNlN +NNNNNNNNNNNNNNNNNNNNNNNNNNNAANNNNNNNNANNNNNAAMMMMAAAMMMMMAANlllllNNNNNNNNNAAAAMMAAAAAANAAAAAAAANNNll +NNNNllllNNNNNNNNNNNNNNNANNNNNNNNNNNNNANNNNAAAMMMMMAAAAMMMAANlllNNNNNNNNNNAAAAAAAAANAAAAAAAAAAANNNNll +NNNNlllllNNNNNNNNNNNNNANNAAAAANNNNNNANNNAAAAAAMMMMAAAAAAANNlllNNNNNNNNNANAAAAAAAANNNAAANNAAAAANNNNNl +NNNNNllllNNNNNNNNNNNNNNNNNNNNANNNNNNANNAAAAAAAAAMMMMAAANNNllNNNNNNNNNNNAAAAAAAAAAAANNNNANNAANNNNNNNl +NNNNNNllllNNNNNNNNNNNNNANANNNNNNNNNAANAAAAAAAAAAAAMMMMAAANNNNNNNNNNNNNNNNNNAAAAAAAANNNNNNNNNNNNNNNNl +NNANNNNllllNNNNANNNNNNNNNNNNNNNANNNANNAAAAAAAAAAAAAAAAAAANNNNNNNNNNNNNNNNNNNNNAAAAANNNNNNNNNNNNNNNNN +NAAANNNNllllNNNNNNNANNNNNNNNNNNNNNANNNAAAAAAAAAAAAANNNNNNNNNNNNNNNNANNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN +NAAAAANNNlllllNNNNNNNNNNNNNNNNNNNNANNAAMAAAAAAAAANNNNNNNNNNNNNNNANNNNNNNNlllllllllllNNNNNNNNNNNNNNNN +NAAAAAANNNllllllNNNNNNNNNNNNNNNNNAANAAMMMAAAAAAANNNNNNNNNNNNAAAANANNNNNNlllliiiiiiilNNNNNNNNNNNNNNNN +AAAAAAAAANNNllllllllNNNNNNNNNNNNNAAAAAMMMAAAAAAANNNNNNNNNNNNNNNANNNNNNllllliiiiiiiiilNNNNNNNNNNNNNNN +AAAAAAAAAANNNNlllllllNNNNNNNNNNNAANAAAMMMAAAAAANNNNNNNNANNNNNNNNNNNNNllllliiiiiiiiiilNNNNNNNNNAAANNN +AAAAAAAAAAAANNNNNNNNNNNNNNNNNNNNANAAAMMMAAAAAAANNNNNNNNAANAANANNNNNNlllliiiiiiiiiiiiilNNNNNNNAAAAANN +AAAAAAAAAAAAANNNNNNNNNNNNNNNNNNAAAAAAMMAAAAAAAANNNNNNAANAANANANNNNNllllliiiiiiiiiiiiilNNNNNNNAMMMMAN +AAAAAAAAAAAAAAAAAANNNNNNNNNAANAAAAAAAMMAAAAAAMAAANNNNAAAAAANNNNNNNNllllliiiiiiiiiiiiiilNNNNNNAMMMANN +AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMMMMMMMMMMAAANNNNNAANNNNNNNNNNllllliiiiiiiiiiiiiiiilNNNNNNAAANNN +AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMMMMMMMMMAAAANNNNAAAANNNNNNNNlllllliiiiiiiiiiiiiiiillNNNNNNNNNNN +AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMMMMMMMAAAAANNNNNAANNNNNNNNNllllllliiiiiiiiiiiiiiiiiillNNNNNNNNN +AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMMMMMMMAAAAANNNNNNNNNNNNNNNNlllllllliiiiiiiiiiiiiiiiiiillllllllll +AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMMMMAAAAAAAANNNNNNNNNNNNNNNllllllllliiiiiiiiiiiiiiiiiiiiiiiiiiiii +AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMMMMMMAAAAAAANNNNNNNNNNNNNllllllllllliiiiiiiiiiiiiiiiiiiiiiiiiiiii +AAAAAAAAAAAAAAAAAAAANNAAAAAAAAAAAAMMMMMMMAAAAAANNNNNNNNNNNNllllllllllliiiiiiiiiiiiiiiiiiiiiiiiiiiiii +AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMMMMMMMMAAAAAANNNNNNNNNNNlllllllllllliiiiiiiiiiiiiiiiiiiiiiiiiiiiii +AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMMMMMMMMMAAAAAANNNNNNNNNllllllllllllliiiiiiiiiiiiiiiiiiiiiiiiiiiiii +AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMMMMMMMMMMMMAAAANNNNNNNNNllllllllllllliiiiiiiiiiiiiiiiiiiiiiiiiiiiii +AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMMMMMMMMMMMMMMAANNNNNNNNNllllllllllllliiiiiiiiiiiiiiiiiiiiiiiiiiiiii +AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMAAMAMMMMMMMMMMMAANNNNNNNllllllllllllliiiiiiiiiiiiiiiiiiiiiiiiiiiiiii +AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMMMMMMMMAAANNNNNlllllllllllliiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii +AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMMMAMAAAANNNNllllllllllllliiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii +AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAANNNNNNlllllllllllliiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii +AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAANNNNNlllllllllllliiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii diff --git a/Chapter11/imageplugin/testwrite/testwrite.pro b/Chapter11/imageplugin/testwrite/testwrite.pro new file mode 100644 index 0000000..093b941 --- /dev/null +++ b/Chapter11/imageplugin/testwrite/testwrite.pro @@ -0,0 +1,12 @@ +###################################################################### +# Automatically generated by qmake (2.01a) fr 8. dec 22:18:27 2006 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp +CONFIG += console diff --git a/Chapter11/imageplugin/textimagehandler.cpp b/Chapter11/imageplugin/textimagehandler.cpp new file mode 100644 index 0000000..6a92e53 --- /dev/null +++ b/Chapter11/imageplugin/textimagehandler.cpp @@ -0,0 +1,162 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include +#include +#include + +#include "textimagehandler.h" + +const char map[] = " .:ilNAM"; + +TextImageHandler::TextImageHandler() +{ +} + +TextImageHandler::~TextImageHandler() +{ +} + +bool TextImageHandler::read( QImage *image ) +{ + QTextStream stream( device() ); + QString line; + + line = stream.readLine(); + if( line != "TEXT" || stream.status() != QTextStream::Ok ) + return false; + + line = stream.readLine(); + QRegExp re( "(\\d+)x(\\d+)" ); + int width, height; + if( re.exactMatch( line ) ) + { + bool ok; + + width = re.cap(1).toInt( &ok ); + if( !ok ) + return false; + + height = re.cap(2).toInt( &ok ); + if( !ok ) + return false; + } + else + return false; + + QImage result( width, height, QImage::Format_ARGB32 ); + for( int y=0; y> 8) & 0xff; + int b = (rgb >> 16) & 0xff; + + stream << map[ 7 - (((r+g+b)/3)>>5) & 0x7 ]; + } + stream << "\n"; + } + + if( stream.status() != QTextStream::Ok ) + return false; + + return true; +} + +bool TextImageHandler::canRead( QIODevice *device ) +{ + if( device->peek(4) == "TEXT" ) + return true; + + return false; +} + +bool TextImageHandler::canRead() const +{ + return canRead( device() ); +} diff --git a/Chapter11/imageplugin/textimagehandler.h b/Chapter11/imageplugin/textimagehandler.h new file mode 100644 index 0000000..324c62c --- /dev/null +++ b/Chapter11/imageplugin/textimagehandler.h @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef TEXTIMAGEHANDLER_H +#define TEXTIMAGEHANDLER_H + +#include + +class TextImageHandler : public QImageIOHandler +{ +public: + TextImageHandler(); + ~TextImageHandler(); + + bool read( QImage *image ); + bool write( const QImage &image ); + + bool canRead() const; + static bool canRead( QIODevice *device ); +}; + +#endif // TEXTIMAGEHANDLER_H diff --git a/Chapter11/imageplugin/textimageplugin.cpp b/Chapter11/imageplugin/textimageplugin.cpp new file mode 100644 index 0000000..9deeb5b --- /dev/null +++ b/Chapter11/imageplugin/textimageplugin.cpp @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "textimageplugin.h" +#include "textimagehandler.h" + +TextImagePlugin::TextImagePlugin() +{ +} + +TextImagePlugin::~TextImagePlugin() +{ +} + +QStringList TextImagePlugin::keys() const +{ + return QStringList() << "ti"; +} + +QImageIOPlugin::Capabilities TextImagePlugin::capabilities( QIODevice *device, const QByteArray &format ) const +{ + if( format == "ti" ) + return (QImageIOPlugin::CanRead | QImageIOPlugin::CanWrite); + + if( !format.isEmpty() ) + return 0; + + if( !device->isOpen() ) + return 0; + + QImageIOPlugin::Capabilities result; + + if( device->isReadable() && TextImageHandler::canRead( device ) ) + result |= QImageIOPlugin::CanRead; + + if( device->isWritable() ) + result |= QImageIOPlugin::CanWrite; + + return result; +} + +QImageIOHandler *TextImagePlugin::create( QIODevice *device, const QByteArray &format ) const +{ + QImageIOHandler *result = new TextImageHandler(); + + result->setDevice( device ); + result->setFormat( format ); + + return result; +} + +Q_EXPORT_PLUGIN2( textimageplugin, TextImagePlugin ) diff --git a/Chapter11/imageplugin/textimageplugin.h b/Chapter11/imageplugin/textimageplugin.h new file mode 100644 index 0000000..7bfdd20 --- /dev/null +++ b/Chapter11/imageplugin/textimageplugin.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef TEXTIMAGEPLUGIN_H +#define TEXTIMAGEPLUGIN_H + +#include + +class TextImagePlugin : public QImageIOPlugin +{ +public: + TextImagePlugin(); + ~TextImagePlugin(); + + QStringList keys() const; + Capabilities capabilities( QIODevice *device, const QByteArray &format ) const; + QImageIOHandler *create( QIODevice *device, const QByteArray &format = QByteArray() ) const; +}; + +#endif // TEXTIMAGEPLUGIN_H diff --git a/Chapter11/staticplugin/customplugin.pro b/Chapter11/staticplugin/customplugin.pro new file mode 100644 index 0000000..29aa1f5 --- /dev/null +++ b/Chapter11/staticplugin/customplugin.pro @@ -0,0 +1,15 @@ +###################################################################### +# Automatically generated by qmake (2.01a) må 11. dec 19:03:41 2006 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +HEADERS += filterdialog.h filterinterface.h +FORMS += filterdialog.ui +SOURCES += filterdialog.cpp main.cpp +CONFIG += console +LIBS += -L./filters/darken/release/ -ldarken diff --git a/Chapter11/staticplugin/filterdialog.cpp b/Chapter11/staticplugin/filterdialog.cpp new file mode 100644 index 0000000..572cf6b --- /dev/null +++ b/Chapter11/staticplugin/filterdialog.cpp @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include +#include + +#include "filterinterface.h" + +#include "filterdialog.h" + +FilterDialog::FilterDialog( QWidget *parent ) : QDialog( parent ) +{ + ui.setupUi( this ); + ui.originalLabel->setPixmap( QPixmap( "source.jpeg" ) ); + + connect( ui.filterList, SIGNAL(currentTextChanged(QString)), this, SLOT(filterChanged(QString)) ); + + findFilters(); + filterChanged( QString() ); +} + +void FilterDialog::findFilters() +{ + foreach( QObject *couldBeFilter, QPluginLoader::staticInstances() ) + { + FilterInterface *filter = qobject_cast( couldBeFilter ); + if( filter ) + { + filters[ filter->name() ] = filter; + ui.filterList->addItem( filter->name() ); + } + } + + QDir path( "./plugins" ); + + foreach( QString filename, path.entryList(QDir::Files) ) + { + QPluginLoader loader( path.absoluteFilePath( filename ) ); + QObject *couldBeFilter = loader.instance(); + if( couldBeFilter ) + { + FilterInterface *filter = qobject_cast( couldBeFilter ); + if( filter ) + { + filters[ filter->name() ] = filter; + ui.filterList->addItem( filter->name() ); + } + } + } +} + +void FilterDialog::filterChanged( QString filter ) +{ + if( filter.isEmpty() ) + { + ui.filteredLabel->setPixmap( *(ui.originalLabel->pixmap() ) ); + } + else + { + QImage filtered = filters[ filter ]->filter( ui.originalLabel->pixmap()->toImage() ); + ui.filteredLabel->setPixmap( QPixmap::fromImage( filtered ) ); + } +} + diff --git a/Chapter11/staticplugin/filterdialog.h b/Chapter11/staticplugin/filterdialog.h new file mode 100644 index 0000000..945e4c3 --- /dev/null +++ b/Chapter11/staticplugin/filterdialog.h @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef FILTERDIALOG_H +#define FILTERDIALOG_H + +#include +#include +#include + +#include "filterinterface.h" + +#include "ui_filterdialog.h" + +class FilterDialog : public QDialog +{ + Q_OBJECT +public: + FilterDialog( QWidget *parent=0 ); + +private slots: + void filterChanged( QString ); + +private: + void findFilters(); + + QMap filters; + Ui::FilterDialog ui; +}; + +#endif // FILTERDIALOG_H diff --git a/Chapter11/staticplugin/filterdialog.ui b/Chapter11/staticplugin/filterdialog.ui new file mode 100644 index 0000000..9613740 --- /dev/null +++ b/Chapter11/staticplugin/filterdialog.ui @@ -0,0 +1,86 @@ + + + Copyright (c) 2006-2007, Johan Thelin + + All rights reserved. + + Redistribution and use in source and binary forms, with or without modification, + are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + * Neither the name of APress nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + FilterDialog + + + + 0 + 0 + 869 + 502 + + + + Dialog + + + + 9 + + + 6 + + + + + QAbstractItemView::SelectRows + + + + + + + 0 + + + 6 + + + + + + + + + + + + + + + + + + + + + + diff --git a/Chapter11/staticplugin/filterinterface.h b/Chapter11/staticplugin/filterinterface.h new file mode 100644 index 0000000..1c4d2e2 --- /dev/null +++ b/Chapter11/staticplugin/filterinterface.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef FILTERINTERFACE_H +#define FILTERINTERFACE_H + +#include +#include + +class FilterInterface +{ +public: + virtual QString name() const = 0; + virtual QImage filter( const QImage &image ) const = 0; +}; + +Q_DECLARE_INTERFACE( FilterInterface, "se.thelins.CustomPlugin.FilterInterface/0.1" ) + +#endif // FILTERINTERFACE_H diff --git a/Chapter11/staticplugin/filters/blur/blur.cpp b/Chapter11/staticplugin/filters/blur/blur.cpp new file mode 100644 index 0000000..78e4c8d --- /dev/null +++ b/Chapter11/staticplugin/filters/blur/blur.cpp @@ -0,0 +1,98 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include "blur.h" + +QString Blur::name() const +{ + return "Blur"; +} + +QImage Blur::filter( const QImage &image ) const +{ + QImage result( image.width(), image.height(), image.format() ); + + for( int y=0; y= image.width() ) + x = image.width()-1; + + if( y < 0 ) + y = 0; + if( y >= image.height() ) + y = image.height()-1; + + return image.pixel( x, y ); +} + +Q_EXPORT_PLUGIN2( blur, Blur ) diff --git a/Chapter11/staticplugin/filters/blur/blur.h b/Chapter11/staticplugin/filters/blur/blur.h new file mode 100644 index 0000000..6feec0f --- /dev/null +++ b/Chapter11/staticplugin/filters/blur/blur.h @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef BLUR_H +#define BLUR_H + +#include +#include + +#include "filterinterface.h" + +class Blur : public QObject, FilterInterface +{ + Q_OBJECT + Q_INTERFACES(FilterInterface) + +public: + QString name() const; + QImage filter( const QImage &image ) const; + +private: + QRgb getSafePixel( const QImage &image, int x, int y ) const; +}; + +#endif // BLUR_H diff --git a/Chapter11/staticplugin/filters/blur/blur.pro b/Chapter11/staticplugin/filters/blur/blur.pro new file mode 100644 index 0000000..2fda446 --- /dev/null +++ b/Chapter11/staticplugin/filters/blur/blur.pro @@ -0,0 +1,42 @@ +# +# Copyright (c) 2006-2007, Johan Thelin +# +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# * Neither the name of APress nor the names of its contributors +# may be used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# + +TEMPLATE = lib +TARGET = blur +CONFIG += plugin release +VERSION = 1.0.0 + +INCLUDEPATH += ../.. + +HEADERS += blur.h +SOURCES += blur.cpp + +target.path += ../../plugins +INSTALLS += target \ No newline at end of file diff --git a/Chapter11/staticplugin/filters/darken/darken.cpp b/Chapter11/staticplugin/filters/darken/darken.cpp new file mode 100644 index 0000000..ea0b3a3 --- /dev/null +++ b/Chapter11/staticplugin/filters/darken/darken.cpp @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include "darken.h" + +QString Darken::name() const +{ + return "Darken"; +} + +QImage Darken::filter( const QImage &image ) const +{ + QImage result( image.width(), image.height(), image.format() ); + + for( int y=0; y +#include + +#include "filterinterface.h" + +class Darken : public QObject, FilterInterface +{ + Q_OBJECT + Q_INTERFACES(FilterInterface) + +public: + QString name() const; + QImage filter( const QImage &image ) const; +}; + +#endif // DARKEN_H diff --git a/Chapter11/staticplugin/filters/darken/darken.pro b/Chapter11/staticplugin/filters/darken/darken.pro new file mode 100644 index 0000000..15c6a6f --- /dev/null +++ b/Chapter11/staticplugin/filters/darken/darken.pro @@ -0,0 +1,44 @@ +# +# Copyright (c) 2006-2007, Johan Thelin +# +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# * Neither the name of APress nor the names of its contributors +# may be used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# + +TEMPLATE = lib +TARGET = darken +CONFIG += plugin release +VERSION = 1.0.0 + +INCLUDEPATH += ../.. + +HEADERS += darken.h +SOURCES += darken.cpp + +target.path += ../../plugins +INSTALLS += target + +CONFIG += static diff --git a/Chapter11/staticplugin/filters/flip/flip.cpp b/Chapter11/staticplugin/filters/flip/flip.cpp new file mode 100644 index 0000000..be96fd2 --- /dev/null +++ b/Chapter11/staticplugin/filters/flip/flip.cpp @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include "flip.h" + +QString Flip::name() const +{ + return "Flip Horizontally"; +} + +QImage Flip::filter( const QImage &image ) const +{ + QImage result( image.width(), image.height(), image.format() ); + + for( int y=0; y +#include + +#include "filterinterface.h" + +class Flip : public QObject, FilterInterface +{ + Q_OBJECT + Q_INTERFACES(FilterInterface) + +public: + QString name() const; + QImage filter( const QImage &image ) const; +}; + +#endif // FLIP_H diff --git a/Chapter11/staticplugin/filters/flip/flip.pro b/Chapter11/staticplugin/filters/flip/flip.pro new file mode 100644 index 0000000..a33614e --- /dev/null +++ b/Chapter11/staticplugin/filters/flip/flip.pro @@ -0,0 +1,42 @@ +# +# Copyright (c) 2006-2007, Johan Thelin +# +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# * Neither the name of APress nor the names of its contributors +# may be used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# + +TEMPLATE = lib +TARGET = flip +CONFIG += plugin release +VERSION = 1.0.0 + +INCLUDEPATH += ../.. + +HEADERS += flip.h +SOURCES += flip.cpp + +target.path += ../../plugins +INSTALLS += target \ No newline at end of file diff --git a/Chapter11/staticplugin/main.cpp b/Chapter11/staticplugin/main.cpp new file mode 100644 index 0000000..0e3cc50 --- /dev/null +++ b/Chapter11/staticplugin/main.cpp @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include + +#include "filterdialog.h" + +Q_IMPORT_PLUGIN( darken ) + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + FilterDialog dlg; + dlg.show(); + + return app.exec(); +} diff --git a/Chapter11/staticplugin/source.jpeg b/Chapter11/staticplugin/source.jpeg new file mode 100644 index 0000000..0dc33e8 Binary files /dev/null and b/Chapter11/staticplugin/source.jpeg differ diff --git a/Chapter12/README.txt b/Chapter12/README.txt new file mode 100644 index 0000000..4c4b12d --- /dev/null +++ b/Chapter12/README.txt @@ -0,0 +1,83 @@ +This file is a part of 1590598318-1.zip containing example source code for the +Foundations of Qt Development book available from APress (ISBN 1590598318). + +These are the examples for chapter 12 - Doing Things in Parallel + +simplethreads + + Listings 12-1, 12-2, 12-3 + + Introduces the QThread class. + + +inorderthreads + + Listing 12-5 + + Orders two QThread classes using a QMutex. + + +orderedthreads + + Listings 12-6, 12-7, 12-8, 12-9 + + Two ordered QThreads are used with a device with a shared counter protected by + a QMutex. + + +readwritethreads + + Listings 12-11, 12-12, 12-13, 12-14, 12-15 + + Protects the shared counter using a QReadWriteLock instead of a mutex. + + +basicsemaphore + + Listing 12-17 + + Introduces the QSemaphore class. + + +semaphorethreads + + Listings 12-18, 12-19, 12-20, 12-21 + + Passes data between two threads (one producer and one consumer) using a buffer + controlled by two semaphores. + + +competingsemaphore + + Listings 12-22, 12-23, 12-24 + + Passes data between several threads (many producers and one consumer) using a + bugger controlled by sempahores. + + +signallingthreads + + Listings 12-26, 12-27, 12-28, 12-29, 12-30 + + Passes QString objects between threads using signals and slots. + + +customsignals + + Listings 12-32, 12-33, 12-34, 12-35 + + Passes a custom type between threads using signals and slots. + + +uithread + + Listings 12-37, 12-38, 12-39 + + Shows how to use threads in conjunction with a graphical user interface. + + +processes + + Listings 12-40, 12-41, 12-42 + + Shows how to use the QProcess object to run and monitor an external process. \ No newline at end of file diff --git a/Chapter12/basicsemaphore/basicsemaphore.pro b/Chapter12/basicsemaphore/basicsemaphore.pro new file mode 100644 index 0000000..eba1371 --- /dev/null +++ b/Chapter12/basicsemaphore/basicsemaphore.pro @@ -0,0 +1,11 @@ +###################################################################### +# Automatically generated by qmake (2.01a) to 4. jan 09:51:01 2007 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp diff --git a/Chapter12/basicsemaphore/main.cpp b/Chapter12/basicsemaphore/main.cpp new file mode 100644 index 0000000..4dd15bb --- /dev/null +++ b/Chapter12/basicsemaphore/main.cpp @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +int main( int argc, char *argv ) +{ + QSemaphore s( 10 ); + + s.acquire(); // s.available() = 9 + s.acquire(5); // s.available() = 4 + s.release(2); // s.available() = 6 + s.release(); // s.available() = 7 + s.release(5); // s.available() = 12 + s.tryAcquire(15); // s.available() = 12 + + return 0; +} diff --git a/Chapter12/competingsemaphore/competingsemaphore.pro b/Chapter12/competingsemaphore/competingsemaphore.pro new file mode 100644 index 0000000..83b049e --- /dev/null +++ b/Chapter12/competingsemaphore/competingsemaphore.pro @@ -0,0 +1,12 @@ +###################################################################### +# Automatically generated by qmake (2.01a) ti 2. jan 15:00:20 2007 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp +CONFIG += console diff --git a/Chapter12/competingsemaphore/main.cpp b/Chapter12/competingsemaphore/main.cpp new file mode 100644 index 0000000..393a7c9 --- /dev/null +++ b/Chapter12/competingsemaphore/main.cpp @@ -0,0 +1,121 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include +#include +#include + +#include + +const int bufferSize = 20; + +QChar buffer[ bufferSize ]; +QSemaphore freeSpace( bufferSize ); +QSemaphore availableData( 0 ); + +QSemaphore atEnd( 0 ); + +class TextProducer : public QThread +{ +public: + TextProducer( QString text ); + + void run(); + +private: + QString m_text; +}; + +TextProducer::TextProducer( QString text ) : QThread() +{ + atEnd.release(); + m_text = text; +} + +void TextProducer::run() +{ + static int index = 0; + static QMutex indexMutex; + + for( int i=0; i +#include + +#include "textdevice.h" +#include "textthread.h" + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + qRegisterMetaType("TextAndNumber"); + + TextDevice device; + TextThread foo( "Foo" ), bar( "Bar" ); + + QObject::connect( &foo, SIGNAL(writeText(TextAndNumber)), &device, SLOT(write(TextAndNumber)) ); + QObject::connect( &bar, SIGNAL(writeText(TextAndNumber)), &device, SLOT(write(TextAndNumber)) ); + + foo.start(); + bar.start(); + device.start(); + + QMessageBox::information( 0, "Threading", "Close me to stop!" ); + + foo.stop(); + bar.stop(); + device.stop(); + + foo.wait(); + bar.wait(); + device.wait(); + + return 0; +} diff --git a/Chapter12/customsignals/textandnumber.cpp b/Chapter12/customsignals/textandnumber.cpp new file mode 100644 index 0000000..473eb59 --- /dev/null +++ b/Chapter12/customsignals/textandnumber.cpp @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + + +#include "textandnumber.h" + +TextAndNumber::TextAndNumber() +{ + number = 0; + text = ""; +} + +TextAndNumber::TextAndNumber( int n, QString t ) +{ + number = n; + text = t; +} diff --git a/Chapter12/customsignals/textandnumber.h b/Chapter12/customsignals/textandnumber.h new file mode 100644 index 0000000..303d5b3 --- /dev/null +++ b/Chapter12/customsignals/textandnumber.h @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef TEXTANDNUMBER_H +#define TEXTANDNUMBER_H + +#include +#include + +class TextAndNumber +{ +public: + TextAndNumber(); + TextAndNumber( int, QString ); + + int number; + QString text; +}; + +Q_DECLARE_METATYPE( TextAndNumber ); + +#endif // TEXTANDNUMBER_H diff --git a/Chapter12/customsignals/textdevice.cpp b/Chapter12/customsignals/textdevice.cpp new file mode 100644 index 0000000..ccfdd50 --- /dev/null +++ b/Chapter12/customsignals/textdevice.cpp @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "textdevice.h" + +#include + +TextDevice::TextDevice() : QThread() +{ + m_count = 0; + m_stop = false; +} + +void TextDevice::stop() +{ + m_stop = true; +} + +void TextDevice::run() +{ + while( !m_stop ) + sleep( 1 ); +} + +void TextDevice::write( TextAndNumber tan ) +{ + QMutexLocker locker( &m_mutex ); + + qDebug() << QString( "Call %1 (%3): %2" ).arg( m_count++ ).arg( tan.text ).arg( tan.number ); +} diff --git a/Chapter12/customsignals/textdevice.h b/Chapter12/customsignals/textdevice.h new file mode 100644 index 0000000..b8f1bdf --- /dev/null +++ b/Chapter12/customsignals/textdevice.h @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef TEXTDEVICE_H +#define TEXTDEVICE_H + +#include +#include +#include "textandnumber.h" + +class TextDevice : public QThread +{ + Q_OBJECT + +public: + TextDevice(); + + void run(); + void stop(); + +public slots: + void write( TextAndNumber tan ); + +private: + int m_count; + QMutex m_mutex; + bool m_stop; +}; + +#endif // TEXTDEVICE_H diff --git a/Chapter12/customsignals/textthread.cpp b/Chapter12/customsignals/textthread.cpp new file mode 100644 index 0000000..ea545e3 --- /dev/null +++ b/Chapter12/customsignals/textthread.cpp @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "textthread.h" + +TextThread::TextThread( QString text ) : QThread() +{ + m_text = text; + m_stop = false; + m_count = 0; +} + +void TextThread::stop() +{ + m_stop = true; +} + +void TextThread::run() +{ + while( !m_stop ) + { + emit writeText( TextAndNumber( m_count++, m_text ) ); + sleep( 1 ); + } +} diff --git a/Chapter12/customsignals/textthread.h b/Chapter12/customsignals/textthread.h new file mode 100644 index 0000000..db812f1 --- /dev/null +++ b/Chapter12/customsignals/textthread.h @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef TEXTTHREAD_H +#define TEXTTHREAD_H + +#include +#include +#include "textandnumber.h" + +class TextThread : public QThread +{ + Q_OBJECT + +public: + TextThread( QString text ); + + void run(); + void stop(); + +signals: + void writeText( TextAndNumber ); + +private: + QString m_text; + int m_count; + bool m_stop; +}; + +#endif // TEXTTHREAD_H diff --git a/Chapter12/inorderthreads/inorderthreads.pro b/Chapter12/inorderthreads/inorderthreads.pro new file mode 100644 index 0000000..c1afa7c --- /dev/null +++ b/Chapter12/inorderthreads/inorderthreads.pro @@ -0,0 +1,12 @@ +###################################################################### +# Automatically generated by qmake (2.01a) on 3. jan 13:45:49 2007 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp +CONFIG += console diff --git a/Chapter12/inorderthreads/main.cpp b/Chapter12/inorderthreads/main.cpp new file mode 100644 index 0000000..79f13d2 --- /dev/null +++ b/Chapter12/inorderthreads/main.cpp @@ -0,0 +1,90 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include +#include +#include + +#include + +class TextThread : public QThread +{ +public: + TextThread( QString text ); + + void run(); + +private: + QString m_text; +}; + +bool stopThreads = false; + +TextThread::TextThread( QString text ) : QThread() +{ + m_text = text; +} + +QMutex mutex; + +void TextThread::run() +{ + while( !stopThreads ) + { + mutex.lock(); + if( stopThreads ) + return; + + qDebug() << m_text; + sleep( 1 ); + mutex.unlock(); + } +} + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + TextThread foo( "Foo" ), bar( "Bar" ); + + foo.start(); + bar.start(); + + QMessageBox::information( 0, "Threading", "Close me to stop!" ); + + stopThreads = true; + + foo.wait(); + bar.wait(); + + return 0; +} diff --git a/Chapter12/orderedthreads/main.cpp b/Chapter12/orderedthreads/main.cpp new file mode 100644 index 0000000..e395e20 --- /dev/null +++ b/Chapter12/orderedthreads/main.cpp @@ -0,0 +1,109 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include +#include +#include + +#include + +class TextDevice +{ +public: + TextDevice(); + + void write( QString ); + +private: + int count; + QMutex mutex; +}; + +TextDevice::TextDevice() +{ + count = 0; +} + +void TextDevice::write( QString text ) +{ + QMutexLocker locker( &mutex ); + qDebug() << QString( "Call %1: %2" ).arg( count++ ).arg( text ); +} + +class TextThread : public QThread +{ +public: + TextThread( QString text, TextDevice *m_device ); + + void run(); + +private: + QString m_text; + TextDevice *m_device; +}; + +bool stopThreads = false; + +TextThread::TextThread( QString text, TextDevice *device ) : QThread() +{ + m_text = text; + m_device = device; +} + +void TextThread::run() +{ + while( !stopThreads ) + { + m_device->write( m_text ); + sleep( 1 ); + } +} + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + TextDevice device; + TextThread foo( "Foo", &device ), bar( "Bar", &device ); + + foo.start(); + bar.start(); + + QMessageBox::information( 0, "Threading", "Close me to stop!" ); + + stopThreads = true; + + foo.wait(); + bar.wait(); + + return 0; +} diff --git a/Chapter12/orderedthreads/orderedthreads.pro b/Chapter12/orderedthreads/orderedthreads.pro new file mode 100644 index 0000000..9fa1290 --- /dev/null +++ b/Chapter12/orderedthreads/orderedthreads.pro @@ -0,0 +1,12 @@ +###################################################################### +# Automatically generated by qmake (2.01a) må 1. jan 16:55:20 2007 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp +CONFIG += console diff --git a/Chapter12/processes/main.cpp b/Chapter12/processes/main.cpp new file mode 100644 index 0000000..ec857b3 --- /dev/null +++ b/Chapter12/processes/main.cpp @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include "processdialog.h" + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + ProcessDialog dlg; + dlg.show(); + + return app.exec(); +} \ No newline at end of file diff --git a/Chapter12/processes/processdialog.cpp b/Chapter12/processes/processdialog.cpp new file mode 100644 index 0000000..c52ebfd --- /dev/null +++ b/Chapter12/processes/processdialog.cpp @@ -0,0 +1,149 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include "processdialog.h" + +ProcessDialog::ProcessDialog() : QDialog() +{ + process = 0; + + ui.setupUi( this ); + + connect( ui.uicButton, SIGNAL(clicked()), this, SLOT(runUic()) ); +} + +void ProcessDialog::runUic() +{ + ui.uicButton->setEnabled( false ); + ui.textEdit->setText( "" ); + + if( process ) + delete process; + process = new QProcess( this ); + + connect( process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(handleError(QProcess::ProcessError)) ); + connect( process, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(handleFinish(int,QProcess::ExitStatus)) ); + connect( process, SIGNAL(readyReadStandardError()), this, SLOT(handleReadStandardError()) ); + connect( process, SIGNAL(readyReadStandardOutput()), this, SLOT(handleReadStandardOutput()) ); + connect( process, SIGNAL(started()), this, SLOT(handleStarted()) ); + connect( process, SIGNAL(stateChanged(QProcess::ProcessState)), this, SLOT(handleStateChange(QProcess::ProcessState)) ); + + QStringList arguments; + arguments << "-tr" << "MYTR" << "processdialog.ui"; + process->start( "uic", arguments ); +} + +void ProcessDialog::handleError( QProcess::ProcessError error ) +{ + QString errorText; + + switch( error ) + { + case QProcess::FailedToStart: + errorText = "Failed to start"; + break; + case QProcess::Crashed: + errorText = "Crashed"; + break; + case QProcess::Timedout: + errorText = "Timed out"; + break; + case QProcess::WriteError: + errorText = "Write error"; + break; + case QProcess::ReadError: + errorText = "Read error"; + break; + case QProcess::UnknownError: + errorText = "Unknown error"; + break; + } + + ui.textEdit->append( QString( "

%1

" ).arg( errorText ) ); +} + +void ProcessDialog::handleFinish( int code, QProcess::ExitStatus status ) +{ + QString statusText; + + switch( status ) + { + case QProcess::NormalExit: + statusText = "Normal exit"; + break; + case QProcess::CrashExit: + statusText = "Crash exit"; + break; + } + + ui.textEdit->append( QString( "

%1 (%2)

" ).arg( statusText ).arg( code ) ); +} + +void ProcessDialog::handleReadStandardError() +{ + QString errorText = process->readAllStandardError(); + ui.textEdit->append( QString( "%1" ).arg( errorText ) ); +} + +void ProcessDialog::handleReadStandardOutput() +{ + QString outputText = process->readAllStandardOutput(); + ui.textEdit->insertPlainText( outputText ); +} + +void ProcessDialog::handleStarted() +{ + ui.textEdit->append( QString("

Started

" ) ); +} + +void ProcessDialog::handleStateChange( QProcess::ProcessState state ) +{ + QString stateText; + + switch( state ) + { + case QProcess::NotRunning: + stateText = "Not running"; + + ui.uicButton->setEnabled( true ); + break; + case QProcess::Starting: + stateText = "Starting"; + break; + case QProcess::Running: + stateText = "Running"; + break; + } + + ui.textEdit->append( QString( "

New status: %1

" ).arg( stateText ) ); +} diff --git a/Chapter12/processes/processdialog.h b/Chapter12/processes/processdialog.h new file mode 100644 index 0000000..97aa94a --- /dev/null +++ b/Chapter12/processes/processdialog.h @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef PROCESSDIALOG_H +#define PROCESSDIALOG_H + +#include +#include + +#include "ui_processdialog.h" + +class ProcessDialog : public QDialog +{ + Q_OBJECT + +public: + ProcessDialog(); + +private slots: + void runUic(); + + void handleError( QProcess::ProcessError ); + void handleFinish( int, QProcess::ExitStatus ); + void handleReadStandardError(); + void handleReadStandardOutput(); + void handleStarted(); + void handleStateChange( QProcess::ProcessState ); + +private: + QProcess *process; + + Ui::ProcessDialog ui; +}; + +#endif // PROCESSDIALOG_H diff --git a/Chapter12/processes/processdialog.ui b/Chapter12/processes/processdialog.ui new file mode 100644 index 0000000..b7a19ad --- /dev/null +++ b/Chapter12/processes/processdialog.ui @@ -0,0 +1,75 @@ + + + Copyright (c) 2006-2007, Johan Thelin + + All rights reserved. + + Redistribution and use in source and binary forms, with or without modification, + are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + * Neither the name of APress nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + ProcessDialog + + + + 0 + 0 + 400 + 300 + + + + Process Demonstration + + + + 9 + + + 6 + + + + + Run uic + + + + + + + <html><head><meta name="qrichtext" content="1" /><style type="text/css"> +p, li { white-space: pre-wrap; } +</style></head><body style=" font-family:'MS Shell Dlg 2'; font-size:7.8pt; font-weight:400; font-style:normal; text-decoration:none;"> +<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-size:8pt;"></p></body></html> + + + Qt::TextSelectableByMouse + + + + + + + + diff --git a/Chapter12/processes/processes.pro b/Chapter12/processes/processes.pro new file mode 100644 index 0000000..a380944 --- /dev/null +++ b/Chapter12/processes/processes.pro @@ -0,0 +1,14 @@ +###################################################################### +# Automatically generated by qmake (2.01a) ti 2. jan 19:20:40 2007 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +HEADERS += processdialog.h +FORMS += processdialog.ui +SOURCES += main.cpp processdialog.cpp +CONFIG += console diff --git a/Chapter12/readwritethreads/main.cpp b/Chapter12/readwritethreads/main.cpp new file mode 100644 index 0000000..0e7ef78 --- /dev/null +++ b/Chapter12/readwritethreads/main.cpp @@ -0,0 +1,144 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include +#include +#include + +#include + +class TextDevice +{ +public: + TextDevice(); + + void increase(); + void write( QString ); + +private: + int count; + QReadWriteLock lock; +}; + +TextDevice::TextDevice() +{ + count = 0; +} + +void TextDevice::increase() +{ + QWriteLocker locker( &lock ); + count++; +} + +void TextDevice::write( QString text ) +{ + QReadLocker locker( &lock ); + qDebug() << QString( "Call %1: %2" ).arg( count ).arg( text ); +} + +bool stopThreads = false; + +class IncreaseThread : public QThread +{ +public: + IncreaseThread( TextDevice *device ); + + void run(); + +private: + TextDevice *m_device; +}; + +IncreaseThread::IncreaseThread( TextDevice *device ) : QThread() +{ + m_device = device; +} + +void IncreaseThread::run() +{ + while( !stopThreads ) + { + msleep( 1200 ); + m_device->increase(); + } +} + +class TextThread : public QThread +{ +public: + TextThread( QString text, TextDevice *m_device ); + + void run(); + +private: + QString m_text; + TextDevice *m_device; +}; + +TextThread::TextThread( QString text, TextDevice *device ) : QThread() +{ + m_text = text; + m_device = device; +} + +void TextThread::run() +{ + while( !stopThreads ) + { + m_device->write( m_text ); + sleep( 1 ); + } +} + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + TextDevice device; + IncreaseThread inc( &device ); + TextThread foo( "Foo", &device ), bar( "Bar", &device ); + + foo.start(); + bar.start(); + inc.start(); + + QMessageBox::information( 0, "Threading", "Close me to stop!" ); + + stopThreads = true; + + foo.wait(); + bar.wait(); + inc.wait(); + + return 0; +} diff --git a/Chapter12/readwritethreads/readwritethreads.pro b/Chapter12/readwritethreads/readwritethreads.pro new file mode 100644 index 0000000..170196e --- /dev/null +++ b/Chapter12/readwritethreads/readwritethreads.pro @@ -0,0 +1,12 @@ +###################################################################### +# Automatically generated by qmake (2.01a) ti 2. jan 10:03:03 2007 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp +CONFIG += console diff --git a/Chapter12/semaphorethreads/main.cpp b/Chapter12/semaphorethreads/main.cpp new file mode 100644 index 0000000..30ec29e --- /dev/null +++ b/Chapter12/semaphorethreads/main.cpp @@ -0,0 +1,111 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include +#include + +#include + +const int bufferSize = 20; + +QChar buffer[ bufferSize ]; +QSemaphore freeSpace( bufferSize ); +QSemaphore availableData( 0 ); + +bool atEnd = false; + +class TextProducer : public QThread +{ +public: + TextProducer( QString text ); + + void run(); + +private: + QString m_text; +}; + +TextProducer::TextProducer( QString text ) : QThread() +{ + m_text = text; +} + +void TextProducer::run() +{ + for( int i=0; i +#include + +#include "textdevice.h" +#include "textthread.h" + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + TextDevice device; + TextThread foo( "Foo" ), bar( "Bar" ); + + QObject::connect( &foo, SIGNAL(writeText(QString)), &device, SLOT(write(QString)) ); + QObject::connect( &bar, SIGNAL(writeText(QString)), &device, SLOT(write(QString)) ); + + foo.start(); + bar.start(); + device.start(); + + QMessageBox::information( 0, "Threading", "Close me to stop!" ); + + foo.stop(); + bar.stop(); + device.stop(); + + foo.wait(); + bar.wait(); + device.wait(); + + return 0; +} diff --git a/Chapter12/signallingthreads/signallingthreads.pro b/Chapter12/signallingthreads/signallingthreads.pro new file mode 100644 index 0000000..2e77cba --- /dev/null +++ b/Chapter12/signallingthreads/signallingthreads.pro @@ -0,0 +1,13 @@ +###################################################################### +# Automatically generated by qmake (2.01a) ti 2. jan 14:31:10 2007 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +HEADERS += textdevice.h textthread.h +SOURCES += main.cpp textdevice.cpp textthread.cpp +CONFIG += console diff --git a/Chapter12/signallingthreads/textdevice.cpp b/Chapter12/signallingthreads/textdevice.cpp new file mode 100644 index 0000000..2af0f6e --- /dev/null +++ b/Chapter12/signallingthreads/textdevice.cpp @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "textdevice.h" + +#include + +TextDevice::TextDevice() : QThread() +{ + m_count = 0; +} + +void TextDevice::stop() +{ + quit(); +} + +void TextDevice::run() +{ + exec(); +} + +void TextDevice::write( QString text ) +{ + QMutexLocker locker( &m_mutex ); + + qDebug() << QString( "Call %1: %2" ).arg( m_count++ ).arg( text ); +} diff --git a/Chapter12/signallingthreads/textdevice.h b/Chapter12/signallingthreads/textdevice.h new file mode 100644 index 0000000..1d98cfb --- /dev/null +++ b/Chapter12/signallingthreads/textdevice.h @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef TEXTDEVICE_H +#define TEXTDEVICE_H + +#include +#include +#include + +class TextDevice : public QThread +{ + Q_OBJECT + +public: + TextDevice(); + + void run(); + void stop(); + +public slots: + void write( QString text ); + +private: + int m_count; + QMutex m_mutex; +}; + +#endif // TEXTDEVICE_H diff --git a/Chapter12/signallingthreads/textthread.cpp b/Chapter12/signallingthreads/textthread.cpp new file mode 100644 index 0000000..2b771f4 --- /dev/null +++ b/Chapter12/signallingthreads/textthread.cpp @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "textthread.h" + +TextThread::TextThread( QString text ) : QThread() +{ + m_text = text; + m_stop = false; +} + +void TextThread::stop() +{ + m_stop = true; +} + +void TextThread::run() +{ + while( !m_stop ) + { + emit writeText( m_text ); + sleep( 1 ); + } +} diff --git a/Chapter12/signallingthreads/textthread.h b/Chapter12/signallingthreads/textthread.h new file mode 100644 index 0000000..69cdf28 --- /dev/null +++ b/Chapter12/signallingthreads/textthread.h @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef TEXTTHREAD_H +#define TEXTTHREAD_H + +#include +#include + +class TextThread : public QThread +{ + Q_OBJECT + +public: + TextThread( QString text ); + + void run(); + void stop(); + +signals: + void writeText( QString ); + +private: + QString m_text; + bool m_stop; +}; + +#endif // TEXTTHREAD_H diff --git a/Chapter12/simplethreads/main.cpp b/Chapter12/simplethreads/main.cpp new file mode 100644 index 0000000..91bebb4 --- /dev/null +++ b/Chapter12/simplethreads/main.cpp @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include +#include + +#include + +class TextThread : public QThread +{ +public: + TextThread( QString text ); + + void run(); + +private: + QString m_text; +}; + +bool stopThreads = false; + +TextThread::TextThread( QString text ) : QThread() +{ + m_text = text; +} + +void TextThread::run() +{ + while( !stopThreads ) + { + qDebug() << m_text; + sleep( 1 ); + } +} + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + TextThread foo( "Foo" ), bar( "Bar" ); + + foo.start(); + bar.start(); + + QMessageBox::information( 0, "Threading", "Close me to stop!" ); + + stopThreads = true; + + foo.wait(); + bar.wait(); + + return 0; +} diff --git a/Chapter12/simplethreads/simplethreads.pro b/Chapter12/simplethreads/simplethreads.pro new file mode 100644 index 0000000..e7c5bd1 --- /dev/null +++ b/Chapter12/simplethreads/simplethreads.pro @@ -0,0 +1,12 @@ +###################################################################### +# Automatically generated by qmake (2.01a) må 1. jan 14:08:28 2007 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp +CONFIG += console diff --git a/Chapter12/uithread/main.cpp b/Chapter12/uithread/main.cpp new file mode 100644 index 0000000..95bc035 --- /dev/null +++ b/Chapter12/uithread/main.cpp @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include + +#include "textdialog.h" +#include "textthread.h" + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + qRegisterMetaType("TextAndNumber"); + + TextDialog dialog; + TextThread foo( "Foo" ), bar( "Bar" ); + + QObject::connect( &foo, SIGNAL(writeText(TextAndNumber)), &dialog, SLOT(showText(TextAndNumber)) ); + QObject::connect( &bar, SIGNAL(writeText(TextAndNumber)), &dialog, SLOT(showText(TextAndNumber)) ); + + foo.start(); + bar.start(); + + dialog.show(); + int result = app.exec(); + + foo.stop(); + bar.stop(); + + foo.wait(); + bar.wait(); + + return result; +} diff --git a/Chapter12/uithread/textandnumber.cpp b/Chapter12/uithread/textandnumber.cpp new file mode 100644 index 0000000..473eb59 --- /dev/null +++ b/Chapter12/uithread/textandnumber.cpp @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + + +#include "textandnumber.h" + +TextAndNumber::TextAndNumber() +{ + number = 0; + text = ""; +} + +TextAndNumber::TextAndNumber( int n, QString t ) +{ + number = n; + text = t; +} diff --git a/Chapter12/uithread/textandnumber.h b/Chapter12/uithread/textandnumber.h new file mode 100644 index 0000000..303d5b3 --- /dev/null +++ b/Chapter12/uithread/textandnumber.h @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef TEXTANDNUMBER_H +#define TEXTANDNUMBER_H + +#include +#include + +class TextAndNumber +{ +public: + TextAndNumber(); + TextAndNumber( int, QString ); + + int number; + QString text; +}; + +Q_DECLARE_METATYPE( TextAndNumber ); + +#endif // TEXTANDNUMBER_H diff --git a/Chapter12/uithread/textdialog.cpp b/Chapter12/uithread/textdialog.cpp new file mode 100644 index 0000000..ed2b2ef --- /dev/null +++ b/Chapter12/uithread/textdialog.cpp @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "textdialog.h" + +#include + +TextDialog::TextDialog() : QDialog() +{ + ui.setupUi( this ); + + connect( ui.buttonBox, SIGNAL(clicked(QAbstractButton*)), this, SLOT(buttonClicked(QAbstractButton*)) ); + + count = 0; +} + +void TextDialog::buttonClicked( QAbstractButton *button ) +{ + if( ui.buttonBox->buttonRole( button ) == QDialogButtonBox::ResetRole ) + ui.listWidget->clear(); +} + +void TextDialog::showText( TextAndNumber tan ) +{ + QMutexLocker locker( &mutex ); + + ui.listWidget->addItem( QString( "Call %1 (%3): %2" ).arg( count++ ).arg( tan.text ).arg( tan.number ) ); +} diff --git a/Chapter12/uithread/textdialog.h b/Chapter12/uithread/textdialog.h new file mode 100644 index 0000000..8bc3d36 --- /dev/null +++ b/Chapter12/uithread/textdialog.h @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef TEXTDIALOG_H +#define TEXTDIALOG_H + +#include +#include +#include + +#include "textandnumber.h" +#include "ui_textdialog.h" + +class TextDialog : public QDialog +{ + Q_OBJECT + +public: + TextDialog(); + +public slots: + void showText( TextAndNumber tan ); + +private slots: + void buttonClicked( QAbstractButton* ); + +private: + int count; + QMutex mutex; + + Ui::TextDialog ui; +}; + +#endif // TEXTDEVICE_H diff --git a/Chapter12/uithread/textdialog.ui b/Chapter12/uithread/textdialog.ui new file mode 100644 index 0000000..7f6684a --- /dev/null +++ b/Chapter12/uithread/textdialog.ui @@ -0,0 +1,85 @@ + + + Copyright (c) 2006-2007, Johan Thelin + + All rights reserved. + + Redistribution and use in source and binary forms, with or without modification, + are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + * Neither the name of APress nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + TextDialog + + + + 0 + 0 + 400 + 300 + + + + Threading Demonstration + + + + 9 + + + 6 + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Close|QDialogButtonBox::NoButton|QDialogButtonBox::Reset + + + + + + + + + buttonBox + rejected() + TextDialog + reject() + + + 291 + 264 + + + 286 + 274 + + + + + diff --git a/Chapter12/uithread/textthread.cpp b/Chapter12/uithread/textthread.cpp new file mode 100644 index 0000000..3c24679 --- /dev/null +++ b/Chapter12/uithread/textthread.cpp @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "textthread.h" + +TextThread::TextThread( QString text ) : QThread() +{ + m_text = text; + m_stop = false; + m_count = 0; +} + +void TextThread::stop() +{ + m_stop = true; +} + +void TextThread::run() +{ + while( !m_stop ) + { + emit writeText( TextAndNumber( m_count++, m_text ) ); + sleep( 1 ); + } +} \ No newline at end of file diff --git a/Chapter12/uithread/textthread.h b/Chapter12/uithread/textthread.h new file mode 100644 index 0000000..db812f1 --- /dev/null +++ b/Chapter12/uithread/textthread.h @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef TEXTTHREAD_H +#define TEXTTHREAD_H + +#include +#include +#include "textandnumber.h" + +class TextThread : public QThread +{ + Q_OBJECT + +public: + TextThread( QString text ); + + void run(); + void stop(); + +signals: + void writeText( TextAndNumber ); + +private: + QString m_text; + int m_count; + bool m_stop; +}; + +#endif // TEXTTHREAD_H diff --git a/Chapter12/uithread/uithread.pro b/Chapter12/uithread/uithread.pro new file mode 100644 index 0000000..fa752f0 --- /dev/null +++ b/Chapter12/uithread/uithread.pro @@ -0,0 +1,14 @@ +###################################################################### +# Automatically generated by qmake (2.01a) ti 2. jan 16:45:28 2007 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +HEADERS += textandnumber.h textdialog.h textthread.h +FORMS += textdialog.ui +SOURCES += main.cpp textandnumber.cpp textdialog.cpp textthread.cpp +CONFIG += console diff --git a/Chapter13/README.txt b/Chapter13/README.txt new file mode 100644 index 0000000..8d87743 --- /dev/null +++ b/Chapter13/README.txt @@ -0,0 +1,42 @@ +This file is a part of 1590598318-1.zip containing example source code for the +Foundations of Qt Development book available from APress (ISBN 1590598318). + +These are the examples for chapter 13 - Databases + +sqltest/mysql + + Listings 13-1 + + Shows how to connect to a MySQL server and demonstrates basic operations. + + +sqltest/sqlite + + Listings 13-2 + + Shows how to open a file based SQLite database and demonstrates basic + operations. + + +sqltest/sqlite-mem + + Listings 13-3, 13-4, 13-5 + + Shows how to open a memory based SQLite database and demonstrates basic + operations. + + +imagebook + + Listings 13-6, 13-7, 13-8, 13-9, 13-10, 13-11, 13-12, 13-13, 13-14, 13-15, + 13-16, 13-17, 13-18, 13-19, 13-20, 13-21, 13-22, 13-23 + + Implements a database driven application split into a user interface layer and + a database layer. + + +modelview + + Listings 13-24, 13-25, 13-16 + + Shows how to use the model/view framework in combination with databases. \ No newline at end of file diff --git a/Chapter13/imagebook/Thumbs.db b/Chapter13/imagebook/Thumbs.db new file mode 100644 index 0000000..ddc648b Binary files /dev/null and b/Chapter13/imagebook/Thumbs.db differ diff --git a/Chapter13/imagebook/imagebook.pro b/Chapter13/imagebook/imagebook.pro new file mode 100644 index 0000000..bb2fd26 --- /dev/null +++ b/Chapter13/imagebook/imagebook.pro @@ -0,0 +1,15 @@ +###################################################################### +# Automatically generated by qmake (2.01a) må 15. jan 17:07:31 2007 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +HEADERS += imagecollection.h imagedialog.h +FORMS += imagedialog.ui +SOURCES += imagecollection.cpp imagedialog.cpp main.cpp +QT += sql +CONFIG += console diff --git a/Chapter13/imagebook/imagecollection.cpp b/Chapter13/imagebook/imagecollection.cpp new file mode 100644 index 0000000..e8d27ef --- /dev/null +++ b/Chapter13/imagebook/imagecollection.cpp @@ -0,0 +1,150 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include +#include + +#include "imagecollection.h" + +ImageCollection::ImageCollection() +{ + QSqlDatabase db = QSqlDatabase::addDatabase( "QSQLITE" ); + + db.setDatabaseName( ":memory:" ); + if( !db.open() ) + qFatal( "Failed to open database" ); + + populateDatabase(); +} + +void ImageCollection::populateDatabase() +{ + QSqlQuery qry; + + qry.prepare( "CREATE TABLE IF NOT EXISTS images (id INTEGER UNIQUE PRIMARY KEY, data BLOB)" ); + if( !qry.exec() ) + qFatal( "Failed to create table images" ); + + qry.prepare( "CREATE TABLE IF NOT EXISTS tags (id INTEGER, tag VARCHAR(30))" ); + if( !qry.exec() ) + qFatal( "Failed to create table tags" ); +} + +QList ImageCollection::getIds( QStringList tags ) +{ + QSqlQuery qry; + + if( tags.count() == 0 ) + qry.prepare( "SELECT images.id FROM images" ); + else + qry.prepare( "SELECT id FROM tags WHERE tag IN ('" + tags.join("','") + "') GROUP BY id" ); + + if( !qry.exec() ) + qFatal( "Failed to get IDs" ); + + QList result; + while( qry.next() ) + result << qry.value(0).toInt(); + + return result; +} + +QStringList ImageCollection::getTags() +{ + QSqlQuery qry; + + qry.prepare( "SELECT tag FROM tags GROUP BY tag" ); + if( !qry.exec() ) + qFatal( "Failed to get tags" ); + + QStringList result; + while( qry.next() ) + result << qry.value(0).toString(); + + return result; +} + +void ImageCollection::addTag( int id, QString tag ) +{ + QSqlQuery qry; + + qry.prepare( "INSERT INTO tags (id, tag) VALUES (:id, :tag)" ); + qry.bindValue( ":id", id ); + qry.bindValue( ":tag", tag ); + if( !qry.exec() ) + qFatal( "Failed to add tag" ); +} + +QImage ImageCollection::getImage( int id ) +{ + QSqlQuery qry; + + qry.prepare( "SELECT data FROM images WHERE id = :id" ); + qry.bindValue( ":id", id ); + if( !qry.exec() ) + qFatal( "Failed to get image" ); + if( !qry.next() ) + qFatal( "Failed to get image id" ); + + QByteArray array = qry.value(0).toByteArray(); + QBuffer buffer(&array); + buffer.open( QIODevice::ReadOnly ); + + QImageReader reader(&buffer, "PNG"); + QImage image = reader.read(); + + return image; +} + +void ImageCollection::addImage( QImage image, QStringList tags ) +{ + QBuffer buffer; + QImageWriter writer(&buffer, "PNG"); + writer.write(image); + + QSqlQuery qry; + + int id; + + qry.prepare( "SELECT COUNT(*) FROM images" ); + qry.exec(); + qry.next(); + id = qry.value(0).toInt() + 1; + + qry.prepare( "INSERT INTO images (id, data) VALUES (:id, :data)" ); + qry.bindValue( ":id", id ); + qry.bindValue( ":data", buffer.data() ); + qry.exec(); + + foreach( QString tag, tags ) + addTag( id, tag ); +} diff --git a/Chapter13/imagebook/imagecollection.h b/Chapter13/imagebook/imagecollection.h new file mode 100644 index 0000000..80d13b3 --- /dev/null +++ b/Chapter13/imagebook/imagecollection.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef IMAGECOLLECTION_H +#define IMAGECOLLECTION_H + +#include +#include +#include + +class ImageCollection +{ +public: + ImageCollection(); + + QImage getImage( int id ); + QList getIds( QStringList tags ); + QStringList getTags(); + + void addTag( int id, QString tag ); + void addImage( QImage image, QStringList tags ); + +private: + void populateDatabase(); +}; + +#endif // IMAGECOLLECTION_H diff --git a/Chapter13/imagebook/imagedialog.cpp b/Chapter13/imagebook/imagedialog.cpp new file mode 100644 index 0000000..1a7dbfb --- /dev/null +++ b/Chapter13/imagebook/imagedialog.cpp @@ -0,0 +1,171 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include +#include + +#include "imagedialog.h" + +ImageDialog::ImageDialog() +{ + ui.setupUi( this ); + + currentImage = -1; + + updateTags(); + updateImages(); + + connect( ui.previousButton, SIGNAL(clicked()), this, SLOT(previousClicked()) ); + connect( ui.nextButton, SIGNAL(clicked()), this, SLOT(nextClicked()) ); + connect( ui.addTagButton, SIGNAL(clicked()), this, SLOT(addTagClicked()) ); + connect( ui.addImageButton, SIGNAL(clicked()), this, SLOT(addImageClicked()) ); + connect( ui.tagList, SIGNAL(itemSelectionChanged()), this, SLOT(tagsChanged()) ); +} + +void ImageDialog::updateCurrentImage() +{ + if( currentImage == -1 ) + { + ui.imageLabel->setPixmap( QPixmap() ); + ui.imageLabel->setText( tr("No Image") ); + + ui.addTagButton->setEnabled( false ); + ui.nextButton->setEnabled( false ); + ui.previousButton->setEnabled( false ); + } + else + { + ui.imageLabel->setPixmap( QPixmap::fromImage( images.getImage( imageIds[ currentImage ] ) ) ); + ui.imageLabel->setText( "" ); + + ui.addTagButton->setEnabled( true ); + ui.nextButton->setEnabled( true ); + ui.previousButton->setEnabled( true ); + } +} + +void ImageDialog::updateImages() +{ + int id; + + if( currentImage != -1 ) + id = imageIds[ currentImage ]; + else + id = -1; + + imageIds = images.getIds( selectedTags() ); + currentImage = imageIds.indexOf( id ); + if( currentImage == -1 && imageIds.count() != 0 ) + currentImage = 0; + + ui.imagesLabel->setText( QString::number( imageIds.count() ) ); + + updateCurrentImage(); +} + +void ImageDialog::updateTags() +{ + QStringList selection = selectedTags(); + + QStringList tags = images.getTags(); + ui.tagList->clear(); + ui.tagList->addItems( tags ); + + for( int i=0; icount(); ++i ) + if( selection.contains( ui.tagList->item(i)->text() ) ) + ui.tagList->item(i)->setSelected( true ); +} + +void ImageDialog::nextClicked() +{ + currentImage = (currentImage+1) % imageIds.count(); + updateCurrentImage(); +} + +void ImageDialog::previousClicked() +{ + currentImage --; + if( currentImage == -1 ) + currentImage = imageIds.count()-1; + + updateCurrentImage(); +} + +void ImageDialog::tagsChanged() +{ + updateImages(); +} + +void ImageDialog::addImageClicked() +{ + QString filename = QFileDialog::getOpenFileName( this, tr("Open file"), QString(), tr("PNG Images (*.png)") ); + if( !filename.isNull() ) + { + QImage image( filename ); + + if( image.isNull() ) + { + QMessageBox::warning( this, tr("Image Book"), tr("Failed to open the file '%1'").arg( filename ) ); + return; + } + + images.addImage( image, selectedTags() ); + updateImages(); + } +} + +void ImageDialog::addTagClicked() +{ + bool ok; + QString tag = QInputDialog::getText( this, tr("Image Book"), tr("Tag:"), QLineEdit::Normal, QString(), &ok ); + if( ok ) + { + tag = tag.toLower(); + QRegExp re( "^[a-z]+$" ); + if( re.indexIn(tag) == -1 ) + { + QMessageBox::warning( this, tr("Image Book"), tr("This is not a valid tag. Tags consists of lower case characters a-z.") ); + return; + } + + images.addTag( imageIds[ currentImage ], tag ); + updateTags(); + } +} + +QStringList ImageDialog::selectedTags() +{ + QStringList result; + foreach( QListWidgetItem *item, ui.tagList->selectedItems() ) + result << item->text(); + return result; +} diff --git a/Chapter13/imagebook/imagedialog.h b/Chapter13/imagebook/imagedialog.h new file mode 100644 index 0000000..e4bf6dd --- /dev/null +++ b/Chapter13/imagebook/imagedialog.h @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef IMAGEDIALOG_H +#define IMAGEDIALOG_H + +#include +#include + +#include "ui_imagedialog.h" + +#include "imagecollection.h" + +class ImageDialog : public QDialog +{ + Q_OBJECT + +public: + ImageDialog(); + +private slots: + void nextClicked(); + void previousClicked(); + void tagsChanged(); + + void addImageClicked(); + void addTagClicked(); + +private: + QStringList selectedTags(); + + void updateImages(); + void updateTags(); + void updateCurrentImage(); + + Ui::ImageDialog ui; + + QList imageIds; + int currentImage; + + ImageCollection images; +}; + +#endif // IMAGEDIALOG_H diff --git a/Chapter13/imagebook/imagedialog.ui b/Chapter13/imagebook/imagedialog.ui new file mode 100644 index 0000000..fe4d3b5 --- /dev/null +++ b/Chapter13/imagebook/imagedialog.ui @@ -0,0 +1,166 @@ + + + Copyright (c) 2006-2007, Johan Thelin + + All rights reserved. + + Redistribution and use in source and binary forms, with or without modification, + are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + * Neither the name of APress nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + ImageDialog + + + + 0 + 0 + 730 + 537 + + + + Image Book + + + + 9 + + + 6 + + + + + No Image + + + Qt::AlignCenter + + + + + + + 0 + + + 6 + + + + + + 150 + 16777215 + + + + QAbstractItemView::MultiSelection + + + + + + + 0 + + + 6 + + + + + Images: + + + + + + + # + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + + + + + 0 + + + 6 + + + + + Previous + + + + + + + Next + + + + + + + Qt::Horizontal + + + + 161 + 27 + + + + + + + + Add Tag + + + + + + + Add Image + + + + + + + + + + diff --git a/Chapter13/imagebook/main.cpp b/Chapter13/imagebook/main.cpp new file mode 100644 index 0000000..1fbdb98 --- /dev/null +++ b/Chapter13/imagebook/main.cpp @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include "imagedialog.h" + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + ImageDialog dlg; + dlg.show(); + + return app.exec(); +} diff --git a/Chapter13/imagebook/test.png b/Chapter13/imagebook/test.png new file mode 100644 index 0000000..4cf6fd9 Binary files /dev/null and b/Chapter13/imagebook/test.png differ diff --git a/Chapter13/modelview/main.cpp b/Chapter13/modelview/main.cpp new file mode 100644 index 0000000..045e2b8 --- /dev/null +++ b/Chapter13/modelview/main.cpp @@ -0,0 +1,161 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include + +#include + +void relModel() +{ + QSqlRelationalTableModel *model = new QSqlRelationalTableModel(); + + model->setTable( "names" ); + model->setRelation( 0, QSqlRelation( "salaries", "id", "annual" ) ); + model->select(); + + model->setHeaderData( 0, Qt::Horizontal, QObject::tr("Annual Pay") ); + model->setHeaderData( 1, Qt::Horizontal, QObject::tr("First Name") ); + model->setHeaderData( 2, Qt::Horizontal, QObject::tr("Last Name") ); + + QTableView *view = new QTableView(); + view->setModel( model ); + view->show(); +} + +void tabModel() +{ + QSqlTableModel *model = new QSqlTableModel(); + + model->setTable( "names" ); + model->setFilter( "lastname = 'Doe'" ); + model->select(); + + model->removeColumn( 0 ); + + QTableView *view = new QTableView(); + view->setModel( model ); + view->show(); +} + +void qryModel() +{ + QSqlQueryModel *model = new QSqlQueryModel(); + model->setQuery( "SELECT firstname, lastname FROM names" ); + + QTableView *view = new QTableView(); + view->setModel( model ); + view->show(); +} + + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + QSqlDatabase db = QSqlDatabase::addDatabase( "QSQLITE" ); + + db.setDatabaseName( ":memory:" ); + + if( !db.open() ) + { + qDebug() << db.lastError(); + qFatal( "Failed to connect." ); + } + + qDebug( "Connected!" ); + + QSqlQuery qry; + + qry.prepare( "CREATE TABLE IF NOT EXISTS names (id INTEGER UNIQUE PRIMARY KEY, firstname VARCHAR(30), lastname VARCHAR(30))" ); + qry.exec(); + + qry.prepare( "INSERT INTO names (id, firstname, lastname) VALUES (1, 'John', 'Doe')" ); + qry.exec(); + + qry.prepare( "INSERT INTO names (id, firstname, lastname) VALUES (2, 'Jane', 'Doe')" ); + qry.exec(); + + qry.prepare( "INSERT INTO names (id, firstname, lastname) VALUES (3, 'James', 'Doe')" ); + qry.exec(); + + qry.prepare( "INSERT INTO names (id, firstname, lastname) VALUES (4, 'Judy', 'Doe')" ); + qry.exec(); + + qry.prepare( "INSERT INTO names (id, firstname, lastname) VALUES (5, 'Richard', 'Roe')" ); + qry.exec(); + + qry.prepare( "INSERT INTO names (id, firstname, lastname) VALUES (6, 'Jane', 'Roe')" ); + qry.exec(); + + qry.prepare( "INSERT INTO names (id, firstname, lastname) VALUES (7, 'John', 'Noakes')" ); + qry.exec(); + + qry.prepare( "INSERT INTO names (id, firstname, lastname) VALUES (8, 'Donna', 'Doe')" ); + qry.exec(); + + qry.prepare( "INSERT INTO names (id, firstname, lastname) VALUES (:id, :firstname, :lastname)" ); + qry.bindValue( ":id", 9 ); + qry.bindValue( ":firstname", "Ralph" ); + qry.bindValue( ":lastname", "Roe" ); + qry.exec(); + + qry.prepare( "CREATE TABLE IF NOT EXISTS salaries (id INTEGER UNIQUE PRIMARY KEY, annual INTEGER)" ); + qry.exec(); + + qry.prepare( "INSERT INTO salaries (id, annual) VALUES (1, 1000)" ); + qry.exec(); + + qry.prepare( "INSERT INTO salaries (id, annual) VALUES (2, 900)" ); + qry.exec(); + + qry.prepare( "INSERT INTO salaries (id, annual) VALUES (3, 900)" ); + qry.exec(); + + qry.prepare( "INSERT INTO salaries (id, annual) VALUES (5, 1100)" ); + qry.exec(); + + qry.prepare( "INSERT INTO salaries (id, annual) VALUES (6, 1000)" ); + qry.exec(); + + qry.prepare( "INSERT INTO salaries (id, annual) VALUES (8, 1200)" ); + qry.exec(); + + qry.prepare( "INSERT INTO salaries (id, annual) VALUES (9, 1200)" ); + qry.exec(); + + relModel(); + tabModel(); + qryModel(); + + return app.exec(); +} diff --git a/Chapter13/modelview/modelview.pro b/Chapter13/modelview/modelview.pro new file mode 100644 index 0000000..c0edc67 --- /dev/null +++ b/Chapter13/modelview/modelview.pro @@ -0,0 +1,12 @@ +###################################################################### +# Automatically generated by qmake (2.01a) on 17. jan 19:15:37 2007 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp +QT += sql diff --git a/Chapter13/sqltest/mysql/main.cpp b/Chapter13/sqltest/mysql/main.cpp new file mode 100644 index 0000000..2c5f63e --- /dev/null +++ b/Chapter13/sqltest/mysql/main.cpp @@ -0,0 +1,371 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include +#include + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + QSqlDatabase db = QSqlDatabase::addDatabase( "QMYSQL" ); + + db.setHostName( "localhost" ); + db.setDatabaseName( "qtbook" ); + + db.setUserName( "root" ); + db.setPassword( "sa" ); + + if( !db.open() ) + { + qDebug() << db.lastError(); + qFatal( "Failed to connect." ); + } + + qDebug( "Connected!" ); + + QSqlQuery qry; + + qry.prepare( "CREATE TABLE IF NOT EXISTS names (id INTEGER UNIQUE PRIMARY KEY, firstname VARCHAR(30), lastname VARCHAR(30))" ); + if( !qry.exec() ) + qDebug() << qry.lastError(); + else + qDebug() << "Table created!"; + + qry.prepare( "INSERT INTO names (id, firstname, lastname) VALUES (1, 'John', 'Doe')" ); + if( !qry.exec() ) + qDebug() << qry.lastError(); + else + qDebug( "Inserted!" ); + + qry.prepare( "INSERT INTO names (id, firstname, lastname) VALUES (2, 'Jane', 'Doe')" ); + if( !qry.exec() ) + qDebug() << qry.lastError(); + else + qDebug( "Inserted!" ); + + qry.prepare( "INSERT INTO names (id, firstname, lastname) VALUES (3, 'James', 'Doe')" ); + if( !qry.exec() ) + qDebug() << qry.lastError(); + else + qDebug( "Inserted!" ); + + qry.prepare( "INSERT INTO names (id, firstname, lastname) VALUES (4, 'Judy', 'Doe')" ); + if( !qry.exec() ) + qDebug() << qry.lastError(); + else + qDebug( "Inserted!" ); + + qry.prepare( "INSERT INTO names (id, firstname, lastname) VALUES (5, 'Richard', 'Roe')" ); + if( !qry.exec() ) + qDebug() << qry.lastError(); + else + qDebug( "Inserted!" ); + + qry.prepare( "INSERT INTO names (id, firstname, lastname) VALUES (6, 'Jane', 'Roe')" ); + if( !qry.exec() ) + qDebug() << qry.lastError(); + else + qDebug( "Inserted!" ); + + qry.prepare( "INSERT INTO names (id, firstname, lastname) VALUES (7, 'John', 'Noakes')" ); + if( !qry.exec() ) + qDebug() << qry.lastError(); + else + qDebug( "Inserted!" ); + + qry.prepare( "INSERT INTO names (id, firstname, lastname) VALUES (8, 'Donna', 'Doe')" ); + if( !qry.exec() ) + qDebug() << qry.lastError(); + else + qDebug( "Inserted!" ); + + qry.prepare( "INSERT INTO names (id, firstname, lastname) VALUES (9, 'Ralph', 'Roe')" ); + if( !qry.exec() ) + qDebug() << qry.lastError(); + else + qDebug( "Inserted!" ); + +// SALARIES + qry.prepare( "CREATE TABLE IF NOT EXISTS salaries (id INTEGER UNIQUE PRIMARY KEY, annual INTEGER)" ); + if( !qry.exec() ) + qDebug() << qry.lastError(); + else + qDebug( "Inserted!" ); + + qry.prepare( "INSERT INTO salaries (id, annual) VALUES (1, 1000)" ); + if( !qry.exec() ) + qDebug() << qry.lastError(); + else + qDebug( "Inserted!" ); + + qry.prepare( "INSERT INTO salaries (id, annual) VALUES (2, 900)" ); + if( !qry.exec() ) + qDebug() << qry.lastError(); + else + qDebug( "Inserted!" ); + + qry.prepare( "INSERT INTO salaries (id, annual) VALUES (3, 900)" ); + if( !qry.exec() ) + qDebug() << qry.lastError(); + else + qDebug( "Inserted!" ); + + qry.prepare( "INSERT INTO salaries (id, annual) VALUES (5, 1100)" ); + if( !qry.exec() ) + qDebug() << qry.lastError(); + else + qDebug( "Inserted!" ); + + qry.prepare( "INSERT INTO salaries (id, annual) VALUES (6, 1000)" ); + if( !qry.exec() ) + qDebug() << qry.lastError(); + else + qDebug( "Inserted!" ); + + qry.prepare( "INSERT INTO salaries (id, annual) VALUES (8, 1200)" ); + if( !qry.exec() ) + qDebug() << qry.lastError(); + else + qDebug( "Inserted!" ); + + qry.prepare( "INSERT INTO salaries (id, annual) VALUES (9, 1200)" ); + if( !qry.exec() ) + qDebug() << qry.lastError(); + else + qDebug( "Inserted!" ); + + qry.prepare( "SELECT * FROM salaries" ); + if( !qry.exec() ) + qDebug() << qry.lastError(); + else + { + qDebug( "Selected!" ); + + QSqlRecord rec = qry.record(); + int cols = rec.count(); + + QString temp; + for( int c=0; c + +#include +#include + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + QSqlDatabase db = QSqlDatabase::addDatabase( "QSQLITE" ); + + db.setDatabaseName( ":memory:" ); + + if( !db.open() ) + { + qDebug() << db.lastError(); + qFatal( "Failed to connect." ); + } + + qDebug( "Connected!" ); + + QSqlQuery qry; + + qry.prepare( "CREATE TABLE IF NOT EXISTS names (id INTEGER UNIQUE PRIMARY KEY, firstname VARCHAR(30), lastname VARCHAR(30))" ); + if( !qry.exec() ) + qDebug() << qry.lastError(); + else + qDebug() << "Table created!"; + + qry.prepare( "INSERT INTO names (id, firstname, lastname) VALUES (1, 'John', 'Doe')" ); + if( !qry.exec() ) + qDebug() << qry.lastError(); + else + qDebug( "Inserted!" ); + + qry.prepare( "INSERT INTO names (id, firstname, lastname) VALUES (2, 'Jane', 'Doe')" ); + if( !qry.exec() ) + qDebug() << qry.lastError(); + else + qDebug( "Inserted!" ); + + qry.prepare( "INSERT INTO names (id, firstname, lastname) VALUES (3, 'James', 'Doe')" ); + if( !qry.exec() ) + qDebug() << qry.lastError(); + else + qDebug( "Inserted!" ); + + qry.prepare( "INSERT INTO names (id, firstname, lastname) VALUES (4, 'Judy', 'Doe')" ); + if( !qry.exec() ) + qDebug() << qry.lastError(); + else + qDebug( "Inserted!" ); + + qry.prepare( "INSERT INTO names (id, firstname, lastname) VALUES (5, 'Richard', 'Roe')" ); + if( !qry.exec() ) + qDebug() << qry.lastError(); + else + qDebug( "Inserted!" ); + + qry.prepare( "INSERT INTO names (id, firstname, lastname) VALUES (6, 'Jane', 'Roe')" ); + if( !qry.exec() ) + qDebug() << qry.lastError(); + else + qDebug( "Inserted!" ); + + qry.prepare( "INSERT INTO names (id, firstname, lastname) VALUES (7, 'John', 'Noakes')" ); + if( !qry.exec() ) + qDebug() << qry.lastError(); + else + qDebug( "Inserted!" ); + + qry.prepare( "INSERT INTO names (id, firstname, lastname) VALUES (8, 'Donna', 'Doe')" ); + if( !qry.exec() ) + qDebug() << qry.lastError(); + else + qDebug( "Inserted!" ); + + qry.prepare( "INSERT INTO names (id, firstname, lastname) VALUES (:id, :firstname, :lastname)" ); + qry.bindValue( ":id", 9 ); + qry.bindValue( ":firstname", "Ralph" ); + qry.bindValue( ":lastname", "Roe" ); + if( !qry.exec() ) + qDebug() << qry.lastError(); + else + qDebug( "Inserted!" ); + +// SALARIES + qry.prepare( "CREATE TABLE IF NOT EXISTS salaries (id INTEGER UNIQUE PRIMARY KEY, annual INTEGER)" ); + if( !qry.exec() ) + qDebug() << qry.lastError(); + else + qDebug( "Inserted!" ); + + qry.prepare( "INSERT INTO salaries (id, annual) VALUES (1, 1000)" ); + if( !qry.exec() ) + qDebug() << qry.lastError(); + else + qDebug( "Inserted!" ); + + qry.prepare( "INSERT INTO salaries (id, annual) VALUES (2, 900)" ); + if( !qry.exec() ) + qDebug() << qry.lastError(); + else + qDebug( "Inserted!" ); + + qry.prepare( "INSERT INTO salaries (id, annual) VALUES (3, 900)" ); + if( !qry.exec() ) + qDebug() << qry.lastError(); + else + qDebug( "Inserted!" ); + + qry.prepare( "INSERT INTO salaries (id, annual) VALUES (5, 1100)" ); + if( !qry.exec() ) + qDebug() << qry.lastError(); + else + qDebug( "Inserted!" ); + + qry.prepare( "INSERT INTO salaries (id, annual) VALUES (6, 1000)" ); + if( !qry.exec() ) + qDebug() << qry.lastError(); + else + qDebug( "Inserted!" ); + + qry.prepare( "INSERT INTO salaries (id, annual) VALUES (8, 1200)" ); + if( !qry.exec() ) + qDebug() << qry.lastError(); + else + qDebug( "Inserted!" ); + + qry.prepare( "INSERT INTO salaries (id, annual) VALUES (9, 1200)" ); + if( !qry.exec() ) + qDebug() << qry.lastError(); + else + qDebug( "Inserted!" ); + + qry.prepare( "SELECT * FROM salaries" ); + if( !qry.exec() ) + qDebug() << qry.lastError(); + else + { + qDebug( "Selected!" ); + + QSqlRecord rec = qry.record(); + int cols = rec.count(); + + QString temp; + for( int c=0; c + +#include +#include + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + QSqlDatabase db = QSqlDatabase::addDatabase( "QSQLITE" ); + + db.setDatabaseName( "./testdatabase.db" ); + + if( !db.open() ) + { + qDebug() << db.lastError(); + qFatal( "Failed to connect." ); + } + + qDebug( "Connected!" ); + + QSqlQuery qry; + + qry.prepare( "CREATE TABLE IF NOT EXISTS names (id INTEGER UNIQUE PRIMARY KEY, firstname VARCHAR(30), lastname VARCHAR(30))" ); + if( !qry.exec() ) + qDebug() << qry.lastError(); + else + qDebug() << "Table created!"; + + qry.prepare( "INSERT INTO names (id, firstname, lastname) VALUES (1, 'John', 'Doe')" ); + if( !qry.exec() ) + qDebug() << qry.lastError(); + else + qDebug( "Inserted!" ); + + qry.prepare( "INSERT INTO names (id, firstname, lastname) VALUES (2, 'Jane', 'Doe')" ); + if( !qry.exec() ) + qDebug() << qry.lastError(); + else + qDebug( "Inserted!" ); + + qry.prepare( "INSERT INTO names (id, firstname, lastname) VALUES (3, 'James', 'Doe')" ); + if( !qry.exec() ) + qDebug() << qry.lastError(); + else + qDebug( "Inserted!" ); + + qry.prepare( "INSERT INTO names (id, firstname, lastname) VALUES (4, 'Judy', 'Doe')" ); + if( !qry.exec() ) + qDebug() << qry.lastError(); + else + qDebug( "Inserted!" ); + + qry.prepare( "INSERT INTO names (id, firstname, lastname) VALUES (5, 'Richard', 'Roe')" ); + if( !qry.exec() ) + qDebug() << qry.lastError(); + else + qDebug( "Inserted!" ); + + qry.prepare( "INSERT INTO names (id, firstname, lastname) VALUES (6, 'Jane', 'Roe')" ); + if( !qry.exec() ) + qDebug() << qry.lastError(); + else + qDebug( "Inserted!" ); + + qry.prepare( "INSERT INTO names (id, firstname, lastname) VALUES (7, 'John', 'Noakes')" ); + if( !qry.exec() ) + qDebug() << qry.lastError(); + else + qDebug( "Inserted!" ); + + qry.prepare( "INSERT INTO names (id, firstname, lastname) VALUES (8, 'Donna', 'Doe')" ); + if( !qry.exec() ) + qDebug() << qry.lastError(); + else + qDebug( "Inserted!" ); + + qry.prepare( "INSERT INTO names (id, firstname, lastname) VALUES (9, 'Ralph', 'Roe')" ); + if( !qry.exec() ) + qDebug() << qry.lastError(); + else + qDebug( "Inserted!" ); + + qry.prepare( "SELECT * FROM names" ); + if( !qry.exec() ) + qDebug() << qry.lastError(); + else + { + qDebug( "Selected!" ); + + QSqlRecord rec = qry.record(); + + int cols = rec.count(); + + for( int c=0; c +#include + +#include "ftpdialog.h" + +FtpDialog::FtpDialog() : QDialog() +{ + file = 0; + + ui.setupUi( this ); + + connect( ui.connectButton, SIGNAL(clicked()), this, SLOT(connectClicked()) ); + connect( ui.disconnectButton, SIGNAL(clicked()), this, SLOT(disconnectClicked()) ); + connect( ui.cdButton, SIGNAL(clicked()), this, SLOT(cdClicked()) ); + connect( ui.upButton, SIGNAL(clicked()), this, SLOT(upClicked()) ); + connect( ui.getButton, SIGNAL(clicked()), this, SLOT(getClicked()) ); + + connect( ui.dirList, SIGNAL(itemSelectionChanged()), this, SLOT(selectionChanged()) ); + + connect( &ftp, SIGNAL(commandFinished(int,bool)), this, SLOT(ftpFinished(int,bool)) ); + connect( &ftp, SIGNAL(listInfo(QUrlInfo)), this, SLOT(ftpListInfo(QUrlInfo)) ); + connect( &ftp, SIGNAL(dataTransferProgress(qint64,qint64)), this, SLOT(ftpProgress(qint64,qint64)) ); + + ui.disconnectButton->setEnabled( false ); + ui.cdButton->setEnabled( false ); + ui.upButton->setEnabled( false ); + ui.getButton->setEnabled( false ); +} + +void FtpDialog::connectClicked() +{ + ui.connectButton->setEnabled( false ); + + ftp.connectToHost( "ftp.trolltech.com" ); + ui.statusLabel->setText( tr("Connecting to host...") ); +} + +void FtpDialog::disconnectClicked() +{ + ui.disconnectButton->setEnabled( false ); + ui.cdButton->setEnabled( false ); + ui.upButton->setEnabled( false ); + ui.getButton->setEnabled( false ); + + ftp.close(); +} + +void FtpDialog::cdClicked() +{ + ui.disconnectButton->setEnabled( false ); + ui.cdButton->setEnabled( false ); + ui.upButton->setEnabled( false ); + ui.getButton->setEnabled( false ); + + ftp.cd( ui.dirList->selectedItems()[0]->text() ); + ui.statusLabel->setText( tr("Changing directory...") ); +} + +void FtpDialog::upClicked() +{ + ui.disconnectButton->setEnabled( false ); + ui.cdButton->setEnabled( false ); + ui.upButton->setEnabled( false ); + ui.getButton->setEnabled( false ); + + ftp.cd(".."); + ui.statusLabel->setText( tr("Changing directory...") ); +} + +void FtpDialog::getClicked() +{ + QString fileName = QFileDialog::getSaveFileName( this, tr("Get File"), ui.dirList->selectedItems()[0]->text() ); + if( fileName.isEmpty() ) + return; + + file = new QFile( fileName, this ); + if( !file->open( QIODevice::WriteOnly ) ) + { + QMessageBox::warning( this, tr("Error"), tr("Failed to open file %1 for writing.").arg( fileName ) ); + + delete file; + file = 0; + + return; + } + + ui.disconnectButton->setEnabled( false ); + ui.cdButton->setEnabled( false ); + ui.upButton->setEnabled( false ); + ui.getButton->setEnabled( false ); + + ftp.get( ui.dirList->selectedItems()[0]->text(), file ); + ui.statusLabel->setText( tr("Downloading file...") ); +} + +void FtpDialog::ftpProgress( qint64 done, qint64 total ) +{ + if( total == 0 ) + return; + + ui.statusLabel->setText( tr("Downloading file... (%1%)").arg( QString::number( double(done)*100/double(total), 'f', 1 ) ) ); +} + +void FtpDialog::selectionChanged() +{ + if( ui.dirList->selectedItems().count() == 1 ) + { + if( files.indexOf( ui.dirList->selectedItems()[0]->text() ) == -1 ) + { + ui.cdButton->setEnabled( ui.disconnectButton->isEnabled() ); + ui.getButton->setEnabled( false ); + } + else + { + ui.cdButton->setEnabled( false ); + ui.getButton->setEnabled( ui.disconnectButton->isEnabled() ); + } + } + else + { + ui.cdButton->setEnabled( false ); + ui.getButton->setEnabled( false ); + } +} + +void FtpDialog::getFileList() +{ + ui.disconnectButton->setEnabled( false ); + ui.cdButton->setEnabled( false ); + ui.upButton->setEnabled( false ); + ui.getButton->setEnabled( false ); + + ui.dirList->clear(); + files.clear(); + + if( ftp.state() == QFtp::LoggedIn ) + ftp.list(); +} + +void FtpDialog::ftpListInfo( QUrlInfo info ) +{ + ui.dirList->addItem( info.name() ); + if( info.isFile() ) + files << info.name(); +} + +void FtpDialog::ftpFinished( int request, bool error ) +{ + if( error ) + { + switch( ftp.currentCommand() ) + { + case QFtp::ConnectToHost: + QMessageBox::warning( this, tr("Error"), tr("Failed to connect to host.") ); + ui.connectButton->setEnabled( true ); + + break; + case QFtp::Login: + QMessageBox::warning( this, tr("Error"), tr("Failed to login.") ); + ui.connectButton->setEnabled( true ); + + break; + case QFtp::List: + QMessageBox::warning( this, tr("Error"), tr("Failed to get file list.\nClosing connection.") ); + ftp.close(); + + break; + case QFtp::Cd: + QMessageBox::warning( this, tr("Error"), tr("Failed to change directory.") ); + getFileList(); + + break; + case QFtp::Get: + QMessageBox::warning( this, tr("Error"), tr("Failed to get file?") ); + file->close(); + file->remove(); + + delete file; + file = 0; + + ui.disconnectButton->setEnabled( true ); + ui.upButton->setEnabled( true ); + selectionChanged(); + + break; + } + + ui.statusLabel->setText( tr("Ready.") ); + } + else + { + switch( ftp.currentCommand() ) + { + case QFtp::ConnectToHost: + ftp.login(); + + break; + case QFtp::Login: + getFileList(); + + break; + case QFtp::Close: + ui.connectButton->setEnabled( true ); + getFileList(); + + break; + case QFtp::List: + ui.disconnectButton->setEnabled( true ); + ui.upButton->setEnabled( true ); + ui.statusLabel->setText( tr("Ready.") ); + + break; + case QFtp::Cd: + getFileList(); + + break; + case QFtp::Get: + file->close(); + + delete file; + file = 0; + + ui.disconnectButton->setEnabled( true ); + ui.upButton->setEnabled( true ); + selectionChanged(); + + ui.statusLabel->setText( tr("Ready.") ); + + break; + } + } +} diff --git a/Chapter14/ftp/ftpdialog.h b/Chapter14/ftp/ftpdialog.h new file mode 100644 index 0000000..e68195b --- /dev/null +++ b/Chapter14/ftp/ftpdialog.h @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef FTPDIALOG_H +#define FTPDIALOG_H + +#include +#include +#include + +#include "ui_ftpdialog.h" + +class FtpDialog : public QDialog +{ + Q_OBJECT + +public: + FtpDialog(); + +private slots: + void connectClicked(); + void disconnectClicked(); + void cdClicked(); + void upClicked(); + void getClicked(); + + void selectionChanged(); + + void ftpFinished(int,bool); + void ftpListInfo(QUrlInfo); + void ftpProgress(qint64,qint64); + +private: + void getFileList(); + + Ui::FtpDialog ui; + + QFtp ftp; + QFile *file; + + QStringList files; +}; + +#endif // FTPDIALOG_H diff --git a/Chapter14/ftp/ftpdialog.ui b/Chapter14/ftp/ftpdialog.ui new file mode 100644 index 0000000..36f3718 --- /dev/null +++ b/Chapter14/ftp/ftpdialog.ui @@ -0,0 +1,144 @@ + + + Copyright (c) 2006-2007, Johan Thelin + + All rights reserved. + + Redistribution and use in source and binary forms, with or without modification, + are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + * Neither the name of APress nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + FtpDialog + + + + 0 + 0 + 451 + 582 + + + + FTP Client + + + + 9 + + + 6 + + + + + ftp.trolltech.com + + + + 9 + + + 6 + + + + + Qt::Vertical + + + QSizePolicy::Preferred + + + + 20 + 40 + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + Connect + + + + + + + Disconnect + + + + + + + Get File + + + + + + + Up + + + + + + + Change Directory + + + + + + + + + + + + + Ready. + + + + + + + + diff --git a/Chapter14/ftp/main.cpp b/Chapter14/ftp/main.cpp new file mode 100644 index 0000000..e74a031 --- /dev/null +++ b/Chapter14/ftp/main.cpp @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include "ftpdialog.h" + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + FtpDialog dlg; + dlg.show(); + + return app.exec(); +} diff --git a/Chapter14/http/http.pro b/Chapter14/http/http.pro new file mode 100644 index 0000000..3b27076 --- /dev/null +++ b/Chapter14/http/http.pro @@ -0,0 +1,15 @@ +###################################################################### +# Automatically generated by qmake (2.01a) må 29. jan 10:06:12 2007 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +HEADERS += httpdialog.h +FORMS += httpdialog.ui +SOURCES += httpdialog.cpp main.cpp +QT += network +CONFIG += console diff --git a/Chapter14/http/httpdialog.cpp b/Chapter14/http/httpdialog.cpp new file mode 100644 index 0000000..6713c0b --- /dev/null +++ b/Chapter14/http/httpdialog.cpp @@ -0,0 +1,205 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include + +#include "httpdialog.h" + +HttpDialog::HttpDialog() : QDialog() +{ + file = 0; + + ui.setupUi( this ); + + connect( ui.getButton, SIGNAL(clicked()), this, SLOT(getClicked()) ); + + connect( &http, SIGNAL(stateChanged(int)), this, SLOT(httpStateChanged(int)) ); + connect( &http, SIGNAL(dataSendProgress(int,int)), this, SLOT(httpDataSent(int,int)) ); + connect( &http, SIGNAL(dataReadProgress(int,int)), this, SLOT(httpDataReceived(int,int)) ); + connect( &http, SIGNAL(responseHeaderReceived(QHttpResponseHeader)), this, SLOT(httpHeaderDone(QHttpResponseHeader)) ); + connect( &http, SIGNAL(readyRead(QHttpResponseHeader)), this, SLOT(httpDataDone(QHttpResponseHeader)) ); + connect( &http, SIGNAL(requestStarted(int)), this, SLOT(httpStarted(int)) ); + connect( &http, SIGNAL(requestFinished(int,bool)), this, SLOT(httpFinished(int,bool)) ); + connect( &http, SIGNAL(done(bool)), this, SLOT(httpDone(bool)) ); +} + +void HttpDialog::getClicked() +{ + QUrl url( ui.requestEdit->text(), QUrl::TolerantMode ); + + if( !url.isValid() ) + { + ui.hostLabel->setText( "" ); + ui.pathLabel->setText( "" ); + ui.portLabel->setText( "" ); + ui.userLabel->setText( "" ); + ui.passwordLabel->setText( "" ); + + QMessageBox::warning( this, tr("Invalid URL"), tr("The URL '%1' is invalid.").arg( ui.requestEdit->text() ) ); + + return; + } + + ui.hostLabel->setText( url.host() ); + ui.pathLabel->setText( url.path() ); + ui.portLabel->setText( QString::number(url.port()==-1?80:url.port()) ); + ui.userLabel->setText( url.userName() ); + ui.passwordLabel->setText( url.password() ); + + http.setHost( url.host(), url.port()==-1?80:url.port() ); + if( !url.userName().isEmpty() ) + http.setUser( url.userName(), url.password() ); + + QString fileName = QFileDialog::getSaveFileName( this ); + if( fileName.isEmpty() ) + return; + + file = new QFile( fileName, this ); + if( !file->open( QIODevice::WriteOnly ) ) + { + QMessageBox::warning( this, tr("Could not write"), tr("Could not open the file %f for writing.").arg( fileName ) ); + + delete file; + file = 0; + + return; + } + + http.get( url.path(), file ); + ui.getButton->setEnabled( false ); +} + +#include + +void HttpDialog::httpStateChanged( int state ) +{ + QString stateText; + + switch( state ) + { + case QHttp::Unconnected: + stateText = "Unconnected"; + break; + case QHttp::HostLookup: + stateText = "HostLookup"; + break; + case QHttp::Connecting: + stateText = "Connecting"; + break; + case QHttp::Sending: + stateText = "Sending"; + break; + case QHttp::Reading: + stateText = "Reading"; + break; + case QHttp::Connected: + stateText = "Connected"; + break; + case QHttp::Closing: + stateText = "Closing"; + break; + default: + stateText = "Undefined"; + break; + } + + ui.statusList->addItem( QString("stateChanged( %1 )").arg( stateText ) ); + qDebug() << QString("stateChanged( %1 )").arg( stateText ); +} + +void HttpDialog::httpDataSent( int done, int total ) +{ + ui.statusList->addItem( QString("dataSendProgress( done: %1, total: %2 )").arg( done ).arg( total ) ); + qDebug() << QString("dataSendProgress( done: %1, total: %2 )").arg( done ).arg( total ); +} + +void HttpDialog::httpDataReceived( int done, int total ) +{ + ui.statusList->addItem( QString("dataReadProgress( done: %1, total: %2 )").arg( done ).arg( total ) ); + qDebug() << QString("dataReadProgress( done: %1, total: %2 )").arg( done ).arg( total ); +} + +void HttpDialog::httpHeaderDone( QHttpResponseHeader header ) +{ + ui.statusList->addItem( QString("responseHeaderReceived(code: %1, reason: %2, version: %3.%4 )").arg( header.statusCode() ).arg( header.reasonPhrase() ).arg( header.majorVersion() ).arg( header.minorVersion() ) ); + qDebug() << QString("responseHeaderReceived(code: %1, reason: %2, version: %3.%4 )").arg( header.statusCode() ).arg( header.reasonPhrase() ).arg( header.majorVersion() ).arg( header.minorVersion() ); +} + +void HttpDialog::httpDataDone( QHttpResponseHeader header ) +{ + ui.statusList->addItem( QString("readReady(code: %1, reason: %2, version: %3.%4 )").arg( header.statusCode() ).arg( header.reasonPhrase() ).arg( header.majorVersion() ).arg( header.minorVersion() ) ); + qDebug() << QString("readReady(code: %1, reason: %2, version: %3.%4 )").arg( header.statusCode() ).arg( header.reasonPhrase() ).arg( header.majorVersion() ).arg( header.minorVersion() ); +} + +void HttpDialog::httpStarted( int id ) +{ + ui.statusList->addItem( QString("requestStarted( %1 )").arg( id ) ); + qDebug() << QString("requestStarted( %1 )").arg( id ); +} + +void HttpDialog::httpFinished( int id, bool error ) +{ + ui.statusList->addItem( QString("requestFinished( %1, %2 )").arg( id ).arg( error?"True":"False" ) ); + qDebug() << QString("requestFinished( %1, %2 )").arg( id ).arg( error?"True":"False" ); + if( error ) + QMessageBox::warning( this, tr("Http: requestFinished"), http.errorString() ); +} + +void HttpDialog::httpDone( bool error ) +{ + ui.statusList->addItem( QString("done( %1 )").arg( error?"True":"False" ) ); + qDebug() << QString("done( %1 )").arg( error?"True":"False" ); + + if( error ) + { + QMessageBox::warning( this, tr("Http: done"), http.errorString() ); + + if( file ) + { + file->close(); + file->remove(); + + delete file; + file = 0; + } + } + + if( file ) + { + file->close(); + + delete file; + file = 0; + } + + ui.getButton->setEnabled( true ); +} diff --git a/Chapter14/http/httpdialog.h b/Chapter14/http/httpdialog.h new file mode 100644 index 0000000..474f45f --- /dev/null +++ b/Chapter14/http/httpdialog.h @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef HTTPDIALOG_H +#define HTTPDIALOG_H + +#include +#include + +#include "ui_httpdialog.h" + +class HttpDialog : public QDialog +{ + Q_OBJECT +public: + HttpDialog(); + +private slots: + void getClicked(); + + void httpStateChanged(int); + void httpDataSent(int,int); + void httpDataReceived(int,int); + void httpHeaderDone(QHttpResponseHeader); + void httpDataDone(QHttpResponseHeader); + void httpStarted(int); + void httpFinished(int,bool); + void httpDone(bool); + +private: + Ui::HttpDialog ui; + + QHttp http; + QFile *file; +}; + +#endif // HTTPDIALOG_H diff --git a/Chapter14/http/httpdialog.ui b/Chapter14/http/httpdialog.ui new file mode 100644 index 0000000..5d7f56a --- /dev/null +++ b/Chapter14/http/httpdialog.ui @@ -0,0 +1,231 @@ + + + Copyright (c) 2006-2007, Johan Thelin + + All rights reserved. + + Redistribution and use in source and binary forms, with or without modification, + are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + * Neither the name of APress nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + HttpDialog + + + + 0 + 0 + 550 + 424 + + + + HTTP Client + + + + 9 + + + 6 + + + + + Request + + + + 9 + + + 6 + + + + + Get + + + + + + + http://www.thelins.se/qt/index.html + + + + + + + + + + URL Components + + + + 9 + + + 6 + + + + + # + + + + + + + Password: + + + + + + + # + + + + + + + User: + + + + + + + # + + + + + + + # + + + + + + + # + + + + + + + Port: + + + + + + + Path: + + + + + + + Host: + + + + + + + + + + HTTP Status + + + + 9 + + + 6 + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + Clear + + + + + + + true + + + QAbstractItemView::NoSelection + + + + + + + + + + + + clearButton + clicked() + statusList + clear() + + + 497 + 276 + + + 357 + 302 + + + + + diff --git a/Chapter14/http/main.cpp b/Chapter14/http/main.cpp new file mode 100644 index 0000000..940a970 --- /dev/null +++ b/Chapter14/http/main.cpp @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include "httpdialog.h" + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + HttpDialog dlg; + dlg.show(); + + return app.exec(); +} diff --git a/Chapter14/tcpclient/clientdialog.cpp b/Chapter14/tcpclient/clientdialog.cpp new file mode 100644 index 0000000..3c659cf --- /dev/null +++ b/Chapter14/tcpclient/clientdialog.cpp @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include +#include + +#include "clientdialog.h" + +ClientDialog::ClientDialog() : QDialog() +{ + ui.setupUi( this ); + + connect( ui.getButton, SIGNAL(clicked()), this, SLOT(getClicked()) ); + + connect( &socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(tcpError(QAbstractSocket::SocketError)) ); + connect( &socket, SIGNAL(readyRead()), this, SLOT(tcpReady()) ); +} + +void ClientDialog::getClicked() +{ + ui.getButton->setEnabled( false ); + + ui.imageLabel->setPixmap( QPixmap() ); + ui.imageLabel->setText( tr("Getting image...") ); + + dataSize = 0; + + socket.abort(); + socket.connectToHost( ui.serverEdit->text(), 9876 ); +} + +void ClientDialog::tcpReady() +{ + if( dataSize == 0 ) + { + QDataStream stream( &socket ); + stream.setVersion( QDataStream::Qt_4_0 ); + + if( socket.bytesAvailable() < sizeof(quint32) ) + return; + + stream >> dataSize; + } + + if( dataSize > socket.bytesAvailable() ) + return; + + QByteArray array = socket.read( dataSize ); + QBuffer buffer(&array); + buffer.open( QIODevice::ReadOnly ); + + QImageReader reader(&buffer, "PNG"); + QImage image = reader.read(); + + if( !image.isNull() ) + { + ui.imageLabel->setPixmap( QPixmap::fromImage( image ) ); + ui.imageLabel->setText( tr("") ); + } + else + { + ui.imageLabel->setText( tr("Invalid image received!") ); + } + + ui.getButton->setEnabled( true ); +} + +void ClientDialog::tcpError( QAbstractSocket::SocketError error ) +{ + if( error == QAbstractSocket::RemoteHostClosedError ) + return; + + QMessageBox::warning( this, tr("Error"), tr("TCP error: %1").arg( socket.errorString() ) ); + ui.imageLabel->setText( tr("No Image") ); + ui.getButton->setEnabled( true ); +} diff --git a/Chapter14/tcpclient/clientdialog.h b/Chapter14/tcpclient/clientdialog.h new file mode 100644 index 0000000..64d1d23 --- /dev/null +++ b/Chapter14/tcpclient/clientdialog.h @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef CLIENTDIALOG_H +#define CLIENTDIALOG_H + +#include +#include + +#include "ui_clientdialog.h" + +class ClientDialog : public QDialog +{ + Q_OBJECT + +public: + ClientDialog(); + +private slots: + void getClicked(); + + void tcpReady(); + void tcpError( QAbstractSocket::SocketError error ); + +private: + Ui::ClientDialog ui; + + QTcpSocket socket; + int dataSize; +}; + +#endif // CLIENTDIALOG_H diff --git a/Chapter14/tcpclient/clientdialog.ui b/Chapter14/tcpclient/clientdialog.ui new file mode 100644 index 0000000..1f52fe8 --- /dev/null +++ b/Chapter14/tcpclient/clientdialog.ui @@ -0,0 +1,120 @@ + + + Copyright (c) 2006-2007, Johan Thelin + + All rights reserved. + + Redistribution and use in source and binary forms, with or without modification, + are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + * Neither the name of APress nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + ClientDialog + + + + 0 + 0 + 467 + 445 + + + + TCP Client + + + + 9 + + + 6 + + + + + Server Settings + + + + 9 + + + 6 + + + + + localhost + + + + + + + Get Image + + + + + + + + + + Image + + + + 9 + + + 6 + + + + + + 7 + 7 + 0 + 0 + + + + <html><head><meta name="qrichtext" content="1" /><style type="text/css"> +p, li { white-space: pre-wrap; } +</style></head><body style=" font-family:'MS Shell Dlg 2'; font-size:7.8pt; font-weight:400; font-style:normal; text-decoration:none;"> +<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:8pt; font-style:italic;">No Image</span></p></body></html> + + + Qt::AlignCenter + + + + + + + + + + + diff --git a/Chapter14/tcpclient/main.cpp b/Chapter14/tcpclient/main.cpp new file mode 100644 index 0000000..dfac967 --- /dev/null +++ b/Chapter14/tcpclient/main.cpp @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include "clientdialog.h" + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + ClientDialog dlg; + dlg.show(); + + return app.exec(); +} \ No newline at end of file diff --git a/Chapter14/tcpclient/tcpclient.pro b/Chapter14/tcpclient/tcpclient.pro new file mode 100644 index 0000000..8fc6977 --- /dev/null +++ b/Chapter14/tcpclient/tcpclient.pro @@ -0,0 +1,15 @@ +###################################################################### +# Automatically generated by qmake (2.01a) to 1. feb 15:14:26 2007 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +HEADERS += clientdialog.h +FORMS += clientdialog.ui +SOURCES += clientdialog.cpp main.cpp +QT += network +CONFIG += console diff --git a/Chapter14/tcpserver/images/Thumbs.db b/Chapter14/tcpserver/images/Thumbs.db new file mode 100644 index 0000000..76241a0 Binary files /dev/null and b/Chapter14/tcpserver/images/Thumbs.db differ diff --git a/Chapter14/tcpserver/images/test-green.png b/Chapter14/tcpserver/images/test-green.png new file mode 100644 index 0000000..c4e0d05 Binary files /dev/null and b/Chapter14/tcpserver/images/test-green.png differ diff --git a/Chapter14/tcpserver/images/test.png b/Chapter14/tcpserver/images/test.png new file mode 100644 index 0000000..4cf6fd9 Binary files /dev/null and b/Chapter14/tcpserver/images/test.png differ diff --git a/Chapter14/tcpserver/main.cpp b/Chapter14/tcpserver/main.cpp new file mode 100644 index 0000000..7846541 --- /dev/null +++ b/Chapter14/tcpserver/main.cpp @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include "server.h" + +int main( int argc, char **argv ) +{ + QCoreApplication app( argc, argv ); + + Server server; + if( !server.listen( QHostAddress::Any, 9876 ) ) + { + qCritical( "Cannot listen to port 9876." ); + return 1; + } + + return app.exec(); +} diff --git a/Chapter14/tcpserver/server.cpp b/Chapter14/tcpserver/server.cpp new file mode 100644 index 0000000..a54a4c8 --- /dev/null +++ b/Chapter14/tcpserver/server.cpp @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include + +#include "server.h" +#include "serverthread.h" + +Server::Server() : QTcpServer() +{ +} + +void Server::incomingConnection( int descriptor ) +{ + ServerThread *thread = new ServerThread( descriptor, this ); + + connect( thread, SIGNAL(finished()), thread, SLOT(deleteLater()) ); + thread->start(); +} diff --git a/Chapter14/tcpserver/server.h b/Chapter14/tcpserver/server.h new file mode 100644 index 0000000..ba6abce --- /dev/null +++ b/Chapter14/tcpserver/server.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef SERVER_H +#define SERVER_H + +#include + +class Server : public QTcpServer +{ +public: + Server(); + +protected: + void incomingConnection( int descriptor ); +}; + +#endif // SERVER_H diff --git a/Chapter14/tcpserver/serverthread.cpp b/Chapter14/tcpserver/serverthread.cpp new file mode 100644 index 0000000..1a675b2 --- /dev/null +++ b/Chapter14/tcpserver/serverthread.cpp @@ -0,0 +1,87 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include +#include +#include +#include +#include + +#include "serverthread.h" + +ServerThread::ServerThread( int descriptor, QObject *parent ) : QThread( parent ) +{ + m_descriptor = descriptor; +} + +void ServerThread::run() +{ + QTcpSocket socket; + + if( !socket.setSocketDescriptor( m_descriptor ) ) + { + qDebug( "Socket error!" ); + return; + } + + QBuffer buffer; + QImageWriter writer(&buffer, "PNG"); + writer.write( randomImage() ); + + QByteArray data; + QDataStream stream( &data, QIODevice::WriteOnly ); + stream.setVersion( QDataStream::Qt_4_0 ); + stream << (quint32)buffer.data().size(); + data.append( buffer.data() ); + + socket.write( data ); + + socket.disconnectFromHost(); + socket.waitForDisconnected(); +} + +QImage ServerThread::randomImage() +{ + qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); + + QDir dir("./images"); + dir.setFilter( QDir::Files ); + QFileInfoList entries = dir.entryInfoList(); + + if( entries.size() == 0 ) + { + qDebug( "No images to show!" ); + return QImage(); + } + + return QImage( entries.at( qrand() % entries.size() ).absoluteFilePath() ); +} diff --git a/Chapter14/tcpserver/serverthread.h b/Chapter14/tcpserver/serverthread.h new file mode 100644 index 0000000..152b424 --- /dev/null +++ b/Chapter14/tcpserver/serverthread.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef SERVERTHREAD_H +#define SERVERTHREAD_H + +#include +#include + +class ServerThread : public QThread +{ +public: + ServerThread( int descriptor, QObject *parent ); + + void run(); + +private: + QImage randomImage(); + + int m_descriptor; +}; + +#endif // SERVERTHREAD_H diff --git a/Chapter14/tcpserver/tcpserver.pro b/Chapter14/tcpserver/tcpserver.pro new file mode 100644 index 0000000..6c04e70 --- /dev/null +++ b/Chapter14/tcpserver/tcpserver.pro @@ -0,0 +1,15 @@ +###################################################################### +# Automatically generated by qmake (2.01a) to 1. feb 13:50:49 2007 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +HEADERS += server.h serverthread.h +SOURCES += main.cpp server.cpp serverthread.cpp +QT += network +QT -= ui +CONFIG += console diff --git a/Chapter14/udpclient/listener.cpp b/Chapter14/udpclient/listener.cpp new file mode 100644 index 0000000..b40118c --- /dev/null +++ b/Chapter14/udpclient/listener.cpp @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "listener.h" + +#include +#include +#include +#include + +Listener::Listener( QWidget *parent ) : QLabel( parent ) +{ + setText( "Waiting for data." ); + + image = 0; + + socket = new QUdpSocket( this ); + socket->bind( 9988 ); + + connect( socket, SIGNAL(readyRead()), this, SLOT(dataPending()) ); +} + +void Listener::dataPending() +{ + while( socket->hasPendingDatagrams() ) + { + QByteArray buffer( socket->pendingDatagramSize(), 0 ); + socket->readDatagram( buffer.data(), buffer.size() ); + + QDataStream stream( buffer ); + stream.setVersion( QDataStream::Qt_4_0 ); + + quint16 width, height, y; + stream >> width >> height >> y; + + if( !image ) + image = new QImage( width, height, QImage::Format_RGB32 ); + else if( image->width() != width || image->height() != height ) + { + delete image; + image = new QImage( width, height, QImage::Format_RGB32 ); + } + + for( int x=0; x> red >> green >> blue; + + image->setPixel( x, y, qRgb( red, green, blue ) ); + } + } + + setText( "" ); + setPixmap( QPixmap::fromImage( *image ) ); + resize( image->size() ); +} diff --git a/Chapter14/udpclient/listener.h b/Chapter14/udpclient/listener.h new file mode 100644 index 0000000..ff6feea --- /dev/null +++ b/Chapter14/udpclient/listener.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef LISTENER_H +#define LISTENER_H + +#include + +class QUdpSocket; +class QImage; + +class Listener : public QLabel +{ + Q_OBJECT + +public: + Listener( QWidget *parent=0 ); + +private slots: + void dataPending(); + +private: + QUdpSocket *socket; + QImage *image; +}; + +#endif // LISTENER_H diff --git a/Chapter14/udpclient/main.cpp b/Chapter14/udpclient/main.cpp new file mode 100644 index 0000000..6e8b5de --- /dev/null +++ b/Chapter14/udpclient/main.cpp @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include "listener.h" + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + Listener listener; + listener.show(); + + return app.exec(); +} \ No newline at end of file diff --git a/Chapter14/udpclient/udpclient.pro b/Chapter14/udpclient/udpclient.pro new file mode 100644 index 0000000..174e6df --- /dev/null +++ b/Chapter14/udpclient/udpclient.pro @@ -0,0 +1,14 @@ +###################################################################### +# Automatically generated by qmake (2.01a) fr 16. mar 19:38:14 2007 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +HEADERS += listener.h +SOURCES += listener.cpp main.cpp +QT += network +CONFIG += console diff --git a/Chapter14/udpserver/main.cpp b/Chapter14/udpserver/main.cpp new file mode 100644 index 0000000..7ff4876 --- /dev/null +++ b/Chapter14/udpserver/main.cpp @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include + +#include "sender.h" + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + Sender sender; + QMessageBox::information( 0, "Info", "Broadcasting image" ); + + return 0; +} diff --git a/Chapter14/udpserver/sender.cpp b/Chapter14/udpserver/sender.cpp new file mode 100644 index 0000000..a7db83a --- /dev/null +++ b/Chapter14/udpserver/sender.cpp @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "sender.h" + +#include +#include +#include +#include +#include + +Sender::Sender() +{ + socket = new QUdpSocket( this ); + + image = new QImage( "test.png" ); + if( image->isNull() ) + qFatal( "Failed to open test.png" ); + + QTimer *timer = new QTimer( this ); + timer->setInterval( 250 ); + timer->start(); + + connect( timer, SIGNAL(timeout()), this, SLOT(broadcastLine()) ); +} + +void Sender::broadcastLine() +{ + QByteArray buffer( 6+3*image->width(), 0 ); + QDataStream stream( &buffer, QIODevice::WriteOnly ); + stream.setVersion( QDataStream::Qt_4_0 ); + + stream << (quint16)image->width() << (quint16)image->height(); + + quint16 y = qrand() % image->height(); + + stream << y; + + for( int x=0; xwidth(); ++x ) + { + QRgb rgb = image->pixel( x, y ); + + stream << (quint8)qRed( rgb ) << (quint8)qGreen( rgb ) << (quint8)qBlue( rgb ); + } + + socket->writeDatagram( buffer, QHostAddress::Broadcast, 9988 ); +} diff --git a/Chapter14/udpserver/sender.h b/Chapter14/udpserver/sender.h new file mode 100644 index 0000000..f405788 --- /dev/null +++ b/Chapter14/udpserver/sender.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef SENDER_H +#define SENDER_H + +#include + +class QUdpSocket; +class QImage; + +class Sender : public QObject +{ + Q_OBJECT + +public: + Sender(); + +private slots: + void broadcastLine(); + +private: + QUdpSocket *socket; + QImage *image; +}; + +#endif // SENDER_H diff --git a/Chapter14/udpserver/test.png b/Chapter14/udpserver/test.png new file mode 100644 index 0000000..660e821 Binary files /dev/null and b/Chapter14/udpserver/test.png differ diff --git a/Chapter14/udpserver/udpserver.pro b/Chapter14/udpserver/udpserver.pro new file mode 100644 index 0000000..1d5945e --- /dev/null +++ b/Chapter14/udpserver/udpserver.pro @@ -0,0 +1,14 @@ +###################################################################### +# Automatically generated by qmake (2.01a) fr 16. mar 19:54:14 2007 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +HEADERS += sender.h +SOURCES += main.cpp sender.cpp +QT += network +CONFIG += console diff --git a/Chapter15/README.txt b/Chapter15/README.txt new file mode 100644 index 0000000..58eadcb --- /dev/null +++ b/Chapter15/README.txt @@ -0,0 +1,31 @@ +This file is a part of 1590598318-1.zip containing example source code for the +Foundations of Qt Development book available from APress (ISBN 1590598318). + +These are the examples for chapter 15 - Building Qt Projects + +qmake/basics + + Listing 15-1 + + A basic QMake project. + + +qmake/complex + + Listing 15-5, 15-6, 15-7 + + A complex QMake project consisting of a lib and an application. + + +cmake/basics + + Listing 15-8 + + A basic CMake project. + + +cmake/complex + + Listings 15-12, 15-13, 15-14 + + A complex CMake project consisting of a lib and an application. \ No newline at end of file diff --git a/Chapter15/cmake/basics/CMakeLists.txt b/Chapter15/cmake/basics/CMakeLists.txt new file mode 100644 index 0000000..3acffd8 --- /dev/null +++ b/Chapter15/cmake/basics/CMakeLists.txt @@ -0,0 +1,48 @@ +# +# Copyright (c) 2006-2007, Johan Thelin +# +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# * Neither the name of APress nor the names of its contributors +# may be used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# + +PROJECT( basics ) + +SET( basics_SOURCES main.cpp mainwindow.cpp otherdialog.cpp preferencedialog.cpp ) +SET( basics_HEADERS mainwindow.h otherdialog.h preferencedialog.h ) +SET( basics_FORMS otherdialog.ui preferencedialog.ui ) + +FIND_PACKAGE( Qt4 REQUIRED ) +INCLUDE( ${QT_USE_FILE} ) + +QT4_WRAP_CPP( basics_HEADERS_MOC ${basics_HEADERS} ) +QT4_WRAP_UI( basics_FORMS_HEADERS ${basics_FORMS} ) + +INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_BINARY_DIR} ) + +ADD_DEFINITIONS( ${QT_DEFINITIONS} ) + +ADD_EXECUTABLE( basics ${basics_SOURCES} ${basics_HEADERS_MOC} ${basics_FORMS_HEADERS} ) +TARGET_LINK_LIBRARIES( basics ${QT_LIBRARIES} ) diff --git a/Chapter15/cmake/basics/main.cpp b/Chapter15/cmake/basics/main.cpp new file mode 100644 index 0000000..efe749c --- /dev/null +++ b/Chapter15/cmake/basics/main.cpp @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include "mainwindow.h" + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + MainWindow *win = new MainWindow(); + win->show(); + + return app.exec(); +} diff --git a/Chapter15/cmake/basics/mainwindow.cpp b/Chapter15/cmake/basics/mainwindow.cpp new file mode 100644 index 0000000..b0b6370 --- /dev/null +++ b/Chapter15/cmake/basics/mainwindow.cpp @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "mainwindow.h" + +#include +#include +#include + +#include "preferencedialog.h" +#include "otherdialog.h" + +MainWindow::MainWindow( QWidget *parent ) : QMainWindow( parent ) +{ + setAttribute( Qt::WA_DeleteOnClose ); + + QAction *fileNewAction = new QAction( tr("&New"), this ); + connect( fileNewAction, SIGNAL(triggered()), this, SLOT(fileNew()) ); + + QAction *fileCloseAction = new QAction( tr( "&Close"), this ); + connect( fileCloseAction, SIGNAL(triggered()), this, SLOT(close()) ); + + QAction *fileExitAction = new QAction( tr("E&xit"), this ); + connect( fileExitAction, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()) ); + + QAction *dialogPreferencesAction = new QAction( tr("Preferences..."), this ); + connect( dialogPreferencesAction, SIGNAL(triggered()), this, SLOT(dialogPreferences()) ); + + QAction *dialogOtherAction = new QAction( tr("Other..."), this ); + connect( dialogOtherAction, SIGNAL(triggered()), this, SLOT(dialogOther()) ); + + QMenu *file = menuBar()->addMenu( tr("&File") ); + file->addAction( fileNewAction ); + file->addAction( fileCloseAction ); + file->addSeparator(); + file->addAction( fileExitAction ); + + QMenu *dialogs = menuBar()->addMenu( tr("&Dialogs") ); + dialogs->addAction( dialogPreferencesAction ); + dialogs->addAction( dialogOtherAction ); +} + +void MainWindow::fileNew() +{ + (new MainWindow())->show(); +} + +void MainWindow::dialogPreferences() +{ + PreferenceDialog dlg; + + dlg.exec(); +} + +void MainWindow::dialogOther() +{ + OtherDialog dlg; + + dlg.exec(); +} diff --git a/Chapter15/cmake/basics/mainwindow.h b/Chapter15/cmake/basics/mainwindow.h new file mode 100644 index 0000000..3b81704 --- /dev/null +++ b/Chapter15/cmake/basics/mainwindow.h @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include + +class MainWindow : public QMainWindow +{ + Q_OBJECT +public: + MainWindow( QWidget *parent=0 ); + +private slots: + void fileNew(); + void dialogPreferences(); + void dialogOther(); +}; + +#endif // MAINWINDOW_H diff --git a/Chapter15/cmake/basics/otherdialog.cpp b/Chapter15/cmake/basics/otherdialog.cpp new file mode 100644 index 0000000..63410f7 --- /dev/null +++ b/Chapter15/cmake/basics/otherdialog.cpp @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "otherdialog.h" + +OtherDialog::OtherDialog( QWidget *parent ) : QDialog( parent ) +{ + ui.setupUi( this ); +} diff --git a/Chapter15/cmake/basics/otherdialog.h b/Chapter15/cmake/basics/otherdialog.h new file mode 100644 index 0000000..9388e5d --- /dev/null +++ b/Chapter15/cmake/basics/otherdialog.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef OTHERDIALOG_H +#define OTHERDIALOG_H + +#include +#include "ui_otherdialog.h" + +class OtherDialog : public QDialog +{ +public: + OtherDialog( QWidget *parent=0 ); + +private: + Ui::OtherDialog ui; +}; + +#endif // OTHERDIALOG_H diff --git a/Chapter15/cmake/basics/otherdialog.ui b/Chapter15/cmake/basics/otherdialog.ui new file mode 100644 index 0000000..2aa8dcd --- /dev/null +++ b/Chapter15/cmake/basics/otherdialog.ui @@ -0,0 +1,96 @@ + + + Copyright (c) 2006-2007, Johan Thelin + + All rights reserved. + + Redistribution and use in source and binary forms, with or without modification, + are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + * Neither the name of APress nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + OtherDialog + + + + 0 + 0 + 400 + 300 + + + + Dialog + + + + + 30 + 240 + 341 + 32 + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::NoButton|QDialogButtonBox::Ok + + + + + + + buttonBox + accepted() + OtherDialog + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + OtherDialog + reject() + + + 316 + 260 + + + 286 + 274 + + + + + diff --git a/Chapter15/cmake/basics/preferencedialog.cpp b/Chapter15/cmake/basics/preferencedialog.cpp new file mode 100644 index 0000000..b584da4 --- /dev/null +++ b/Chapter15/cmake/basics/preferencedialog.cpp @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "preferencedialog.h" + +PreferenceDialog::PreferenceDialog( QWidget *parent ) : QDialog( parent ) +{ + ui.setupUi( this ); +} diff --git a/Chapter15/cmake/basics/preferencedialog.h b/Chapter15/cmake/basics/preferencedialog.h new file mode 100644 index 0000000..e59b4c8 --- /dev/null +++ b/Chapter15/cmake/basics/preferencedialog.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef PREFERENCEDIALOG_H +#define PREFERENCEDIALOG_H + +#include +#include "ui_preferencedialog.h" + +class PreferenceDialog : public QDialog +{ +public: + PreferenceDialog( QWidget *parent=0 ); + +private: + Ui::PreferenceDialog ui; +}; + +#endif // PREFERENCEDIALOG_H diff --git a/Chapter15/cmake/basics/preferencedialog.ui b/Chapter15/cmake/basics/preferencedialog.ui new file mode 100644 index 0000000..79ebe4e --- /dev/null +++ b/Chapter15/cmake/basics/preferencedialog.ui @@ -0,0 +1,113 @@ + + + Copyright (c) 2006-2007, Johan Thelin + + All rights reserved. + + Redistribution and use in source and binary forms, with or without modification, + are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + * Neither the name of APress nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + PreferenceDialog + + + + 0 + 0 + 400 + 300 + + + + Dialog + + + + 9 + + + 6 + + + + + + 24 + + + + What do you prefer? + + + Qt::AlignCenter + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::NoButton|QDialogButtonBox::Ok + + + + + + + + + buttonBox + accepted() + PreferenceDialog + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + PreferenceDialog + reject() + + + 316 + 260 + + + 286 + 274 + + + + + diff --git a/Chapter15/cmake/complex/CMakeLists.txt b/Chapter15/cmake/complex/CMakeLists.txt new file mode 100644 index 0000000..8e88ca9 --- /dev/null +++ b/Chapter15/cmake/complex/CMakeLists.txt @@ -0,0 +1,37 @@ +# +# Copyright (c) 2006-2007, Johan Thelin +# +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# * Neither the name of APress nor the names of its contributors +# may be used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# + +PROJECT( complex ) + +SET( EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin ) +SET( LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib ) + +ADD_SUBDIRECTORY( src ) +ADD_SUBDIRECTORY( app ) diff --git a/Chapter15/cmake/complex/app/CMakeLists.txt b/Chapter15/cmake/complex/app/CMakeLists.txt new file mode 100644 index 0000000..2d7f00b --- /dev/null +++ b/Chapter15/cmake/complex/app/CMakeLists.txt @@ -0,0 +1,44 @@ +# +# Copyright (c) 2006-2007, Johan Thelin +# +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# * Neither the name of APress nor the names of its contributors +# may be used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# + +SET( app_SOURCES main.cpp appwindow.cpp ) +SET( app_HEADERS appwindow.h ) + +FIND_PACKAGE( Qt4 REQUIRED ) +INCLUDE( ${QT_USE_FILE} ) + +QT4_WRAP_CPP( app_HEADERS_MOC ${app_HEADERS} ) + +INCLUDE_DIRECTORIES( ${CMAKE_SOURCE_DIR}/include ) + +ADD_DEFINITIONS( ${QT_DEFINITIONS} ) + +ADD_EXECUTABLE( app ${app_SOURCES} ${app_HEADERS_MOC} ) +TARGET_LINK_LIBRARIES( app base ${QT_LIBRARIES} ) diff --git a/Chapter15/cmake/complex/app/appwindow.cpp b/Chapter15/cmake/complex/app/appwindow.cpp new file mode 100644 index 0000000..46d0fa7 --- /dev/null +++ b/Chapter15/cmake/complex/app/appwindow.cpp @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "appwindow.h" + +#include +#include +#include +#include + +#include "base.h" + +AppWindow::AppWindow( QWidget *parent ) : QWidget( parent ) +{ + spinBox = new QSpinBox( this ); + spinBox->setRange( 1, 100 ); + spinBox->setValue( 42 ); + + QPushButton *button = new QPushButton( this ); + button->setText( tr("Hit me!") ); + connect( button, SIGNAL(clicked()), this, SLOT(buttonClicked()) ); + + QHBoxLayout *layout = new QHBoxLayout( this ); + layout->addWidget( spinBox ); + layout->addWidget( button ); + + base = new Base( this ); + connect( base, SIGNAL(numberPresented(QString)), this, SLOT(showDialog(QString)) ); +} + +void AppWindow::buttonClicked() +{ + base->presentNumber( spinBox->value() ); +} + +void AppWindow::showDialog( const QString &text ) +{ + QMessageBox::information( this, tr("Message Received"), text ); +} diff --git a/Chapter15/cmake/complex/app/appwindow.h b/Chapter15/cmake/complex/app/appwindow.h new file mode 100644 index 0000000..b9f9c43 --- /dev/null +++ b/Chapter15/cmake/complex/app/appwindow.h @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef APPWINDOW_H +#define APPWINDOW_H + +#include + +class QSpinBox; +class Base; + +class AppWindow : public QWidget +{ + Q_OBJECT + +public: + AppWindow( QWidget *parent=0 ); + +private slots: + void buttonClicked(); + void showDialog( const QString& ); + +private: + QSpinBox *spinBox; + Base *base; +}; + +#endif // APPWINDOW_H diff --git a/Chapter15/cmake/complex/app/main.cpp b/Chapter15/cmake/complex/app/main.cpp new file mode 100644 index 0000000..27bf590 --- /dev/null +++ b/Chapter15/cmake/complex/app/main.cpp @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include "appwindow.h" + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + AppWindow appWindow; + appWindow.show(); + + return app.exec(); +} diff --git a/Chapter15/cmake/complex/include/base.h b/Chapter15/cmake/complex/include/base.h new file mode 100644 index 0000000..45d12c3 --- /dev/null +++ b/Chapter15/cmake/complex/include/base.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef BASE_H +#define BASE_H + +#include + +class Base : public QObject +{ + Q_OBJECT + +public: + Base( QObject *parent=0 ); + +public slots: + void presentNumber( int ); + +signals: + void numberPresented( const QString& ); +}; + +#endif // BASE_H diff --git a/Chapter15/cmake/complex/src/CMakeLists.txt b/Chapter15/cmake/complex/src/CMakeLists.txt new file mode 100644 index 0000000..bd8f152 --- /dev/null +++ b/Chapter15/cmake/complex/src/CMakeLists.txt @@ -0,0 +1,43 @@ +# +# Copyright (c) 2006-2007, Johan Thelin +# +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# * Neither the name of APress nor the names of its contributors +# may be used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# + +SET( src_SOURCES base.cpp ) +SET( src_HEADERS ../include/base.h ) + +FIND_PACKAGE( Qt4 REQUIRED ) +INCLUDE( ${QT_USE_FILE} ) + +INCLUDE_DIRECTORIES( ${CMAKE_SOURCE_DIR}/include ) + +QT4_WRAP_CPP( src_HEADERS_MOC ${src_HEADERS} ) + +ADD_DEFINITIONS( ${QT_DEFINITIONS} ) + +ADD_LIBRARY( base STATIC ${src_SOURCES} ${src_HEADERS_MOC} ) diff --git a/Chapter15/cmake/complex/src/base.cpp b/Chapter15/cmake/complex/src/base.cpp new file mode 100644 index 0000000..c43292e --- /dev/null +++ b/Chapter15/cmake/complex/src/base.cpp @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "base.h" + +Base::Base( QObject *parent ) : QObject( parent ) +{ +} + +void Base::presentNumber( int number ) +{ + emit numberPresented( QString( "This is the number to present: %1" ).arg( number ) ); +} diff --git a/Chapter15/qmake/basics/basics.pro b/Chapter15/qmake/basics/basics.pro new file mode 100644 index 0000000..f6206a4 --- /dev/null +++ b/Chapter15/qmake/basics/basics.pro @@ -0,0 +1,13 @@ +###################################################################### +# Automatically generated by qmake (2.01a) må 19. mar 18:20:02 2007 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +HEADERS += mainwindow.h otherdialog.h preferencedialog.h +FORMS += otherdialog.ui preferencedialog.ui +SOURCES += main.cpp mainwindow.cpp otherdialog.cpp preferencedialog.cpp diff --git a/Chapter15/qmake/basics/main.cpp b/Chapter15/qmake/basics/main.cpp new file mode 100644 index 0000000..efe749c --- /dev/null +++ b/Chapter15/qmake/basics/main.cpp @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include "mainwindow.h" + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + MainWindow *win = new MainWindow(); + win->show(); + + return app.exec(); +} diff --git a/Chapter15/qmake/basics/mainwindow.cpp b/Chapter15/qmake/basics/mainwindow.cpp new file mode 100644 index 0000000..b0b6370 --- /dev/null +++ b/Chapter15/qmake/basics/mainwindow.cpp @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "mainwindow.h" + +#include +#include +#include + +#include "preferencedialog.h" +#include "otherdialog.h" + +MainWindow::MainWindow( QWidget *parent ) : QMainWindow( parent ) +{ + setAttribute( Qt::WA_DeleteOnClose ); + + QAction *fileNewAction = new QAction( tr("&New"), this ); + connect( fileNewAction, SIGNAL(triggered()), this, SLOT(fileNew()) ); + + QAction *fileCloseAction = new QAction( tr( "&Close"), this ); + connect( fileCloseAction, SIGNAL(triggered()), this, SLOT(close()) ); + + QAction *fileExitAction = new QAction( tr("E&xit"), this ); + connect( fileExitAction, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()) ); + + QAction *dialogPreferencesAction = new QAction( tr("Preferences..."), this ); + connect( dialogPreferencesAction, SIGNAL(triggered()), this, SLOT(dialogPreferences()) ); + + QAction *dialogOtherAction = new QAction( tr("Other..."), this ); + connect( dialogOtherAction, SIGNAL(triggered()), this, SLOT(dialogOther()) ); + + QMenu *file = menuBar()->addMenu( tr("&File") ); + file->addAction( fileNewAction ); + file->addAction( fileCloseAction ); + file->addSeparator(); + file->addAction( fileExitAction ); + + QMenu *dialogs = menuBar()->addMenu( tr("&Dialogs") ); + dialogs->addAction( dialogPreferencesAction ); + dialogs->addAction( dialogOtherAction ); +} + +void MainWindow::fileNew() +{ + (new MainWindow())->show(); +} + +void MainWindow::dialogPreferences() +{ + PreferenceDialog dlg; + + dlg.exec(); +} + +void MainWindow::dialogOther() +{ + OtherDialog dlg; + + dlg.exec(); +} diff --git a/Chapter15/qmake/basics/mainwindow.h b/Chapter15/qmake/basics/mainwindow.h new file mode 100644 index 0000000..3b81704 --- /dev/null +++ b/Chapter15/qmake/basics/mainwindow.h @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include + +class MainWindow : public QMainWindow +{ + Q_OBJECT +public: + MainWindow( QWidget *parent=0 ); + +private slots: + void fileNew(); + void dialogPreferences(); + void dialogOther(); +}; + +#endif // MAINWINDOW_H diff --git a/Chapter15/qmake/basics/otherdialog.cpp b/Chapter15/qmake/basics/otherdialog.cpp new file mode 100644 index 0000000..63410f7 --- /dev/null +++ b/Chapter15/qmake/basics/otherdialog.cpp @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "otherdialog.h" + +OtherDialog::OtherDialog( QWidget *parent ) : QDialog( parent ) +{ + ui.setupUi( this ); +} diff --git a/Chapter15/qmake/basics/otherdialog.h b/Chapter15/qmake/basics/otherdialog.h new file mode 100644 index 0000000..9388e5d --- /dev/null +++ b/Chapter15/qmake/basics/otherdialog.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef OTHERDIALOG_H +#define OTHERDIALOG_H + +#include +#include "ui_otherdialog.h" + +class OtherDialog : public QDialog +{ +public: + OtherDialog( QWidget *parent=0 ); + +private: + Ui::OtherDialog ui; +}; + +#endif // OTHERDIALOG_H diff --git a/Chapter15/qmake/basics/otherdialog.ui b/Chapter15/qmake/basics/otherdialog.ui new file mode 100644 index 0000000..2aa8dcd --- /dev/null +++ b/Chapter15/qmake/basics/otherdialog.ui @@ -0,0 +1,96 @@ + + + Copyright (c) 2006-2007, Johan Thelin + + All rights reserved. + + Redistribution and use in source and binary forms, with or without modification, + are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + * Neither the name of APress nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + OtherDialog + + + + 0 + 0 + 400 + 300 + + + + Dialog + + + + + 30 + 240 + 341 + 32 + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::NoButton|QDialogButtonBox::Ok + + + + + + + buttonBox + accepted() + OtherDialog + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + OtherDialog + reject() + + + 316 + 260 + + + 286 + 274 + + + + + diff --git a/Chapter15/qmake/basics/preferencedialog.cpp b/Chapter15/qmake/basics/preferencedialog.cpp new file mode 100644 index 0000000..b584da4 --- /dev/null +++ b/Chapter15/qmake/basics/preferencedialog.cpp @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "preferencedialog.h" + +PreferenceDialog::PreferenceDialog( QWidget *parent ) : QDialog( parent ) +{ + ui.setupUi( this ); +} diff --git a/Chapter15/qmake/basics/preferencedialog.h b/Chapter15/qmake/basics/preferencedialog.h new file mode 100644 index 0000000..e59b4c8 --- /dev/null +++ b/Chapter15/qmake/basics/preferencedialog.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef PREFERENCEDIALOG_H +#define PREFERENCEDIALOG_H + +#include +#include "ui_preferencedialog.h" + +class PreferenceDialog : public QDialog +{ +public: + PreferenceDialog( QWidget *parent=0 ); + +private: + Ui::PreferenceDialog ui; +}; + +#endif // PREFERENCEDIALOG_H diff --git a/Chapter15/qmake/basics/preferencedialog.ui b/Chapter15/qmake/basics/preferencedialog.ui new file mode 100644 index 0000000..79ebe4e --- /dev/null +++ b/Chapter15/qmake/basics/preferencedialog.ui @@ -0,0 +1,113 @@ + + + Copyright (c) 2006-2007, Johan Thelin + + All rights reserved. + + Redistribution and use in source and binary forms, with or without modification, + are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + * Neither the name of APress nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + PreferenceDialog + + + + 0 + 0 + 400 + 300 + + + + Dialog + + + + 9 + + + 6 + + + + + + 24 + + + + What do you prefer? + + + Qt::AlignCenter + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::NoButton|QDialogButtonBox::Ok + + + + + + + + + buttonBox + accepted() + PreferenceDialog + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + PreferenceDialog + reject() + + + 316 + 260 + + + 286 + 274 + + + + + diff --git a/Chapter15/qmake/complex/Makefile b/Chapter15/qmake/complex/Makefile new file mode 100644 index 0000000..f62247b --- /dev/null +++ b/Chapter15/qmake/complex/Makefile @@ -0,0 +1,179 @@ +############################################################################# +# Makefile for building: complex +# Generated by qmake (2.01a) (Qt 4.2.2) on: må 26. mar 19:48:45 2007 +# Project: complex.pro +# Template: subdirs +# Command: qmake -win32 -o Makefile complex.pro +############################################################################# + +first: make_default +MAKEFILE = Makefile +QMAKE = qmake +DEL_FILE = del +CHK_DIR_EXISTS= if not exist +MKDIR = mkdir +COPY = copy /y +COPY_FILE = $(COPY) +COPY_DIR = xcopy /s /q /y /i +INSTALL_FILE = $(COPY_FILE) +INSTALL_PROGRAM = +INSTALL_DIR = $(COPY_DIR) +DEL_FILE = del +SYMLINK = +DEL_DIR = rmdir +MOVE = move +CHK_DIR_EXISTS= if not exist +MKDIR = mkdir +SUBTARGETS = \ + sub-src \ + sub-app + +src\$(MAKEFILE): + @$(CHK_DIR_EXISTS) src\ $(MKDIR) src\ + cd src && $(QMAKE) src.pro -win32 -o $(MAKEFILE) +sub-src-qmake_all: FORCE + @$(CHK_DIR_EXISTS) src\ $(MKDIR) src\ + cd src && $(QMAKE) src.pro -win32 -o $(MAKEFILE) +sub-src: src\$(MAKEFILE) FORCE + cd src && $(MAKE) -f $(MAKEFILE) +sub-src-make_default-ordered: src\$(MAKEFILE) FORCE + cd src && $(MAKE) -f $(MAKEFILE) +sub-src-make_default: src\$(MAKEFILE) FORCE + cd src && $(MAKE) -f $(MAKEFILE) +sub-src-make_first-ordered: src\$(MAKEFILE) FORCE + cd src && $(MAKE) -f $(MAKEFILE) first +sub-src-make_first: src\$(MAKEFILE) FORCE + cd src && $(MAKE) -f $(MAKEFILE) first +sub-src-all-ordered: src\$(MAKEFILE) FORCE + cd src && $(MAKE) -f $(MAKEFILE) all +sub-src-all: src\$(MAKEFILE) FORCE + cd src && $(MAKE) -f $(MAKEFILE) all +sub-src-clean-ordered: src\$(MAKEFILE) FORCE + cd src && $(MAKE) -f $(MAKEFILE) clean +sub-src-clean: src\$(MAKEFILE) FORCE + cd src && $(MAKE) -f $(MAKEFILE) clean +sub-src-distclean-ordered: src\$(MAKEFILE) FORCE + cd src && $(MAKE) -f $(MAKEFILE) distclean +sub-src-distclean: src\$(MAKEFILE) FORCE + cd src && $(MAKE) -f $(MAKEFILE) distclean +sub-src-install_subtargets-ordered: src\$(MAKEFILE) FORCE + cd src && $(MAKE) -f $(MAKEFILE) install +sub-src-install_subtargets: src\$(MAKEFILE) FORCE + cd src && $(MAKE) -f $(MAKEFILE) install +sub-src-uninstall_subtargets-ordered: src\$(MAKEFILE) FORCE + cd src && $(MAKE) -f $(MAKEFILE) uninstall +sub-src-uninstall_subtargets: src\$(MAKEFILE) FORCE + cd src && $(MAKE) -f $(MAKEFILE) uninstall +app\$(MAKEFILE): + @$(CHK_DIR_EXISTS) app\ $(MKDIR) app\ + cd app && $(QMAKE) app.pro -win32 -o $(MAKEFILE) +sub-app-qmake_all: FORCE + @$(CHK_DIR_EXISTS) app\ $(MKDIR) app\ + cd app && $(QMAKE) app.pro -win32 -o $(MAKEFILE) +sub-app: app\$(MAKEFILE) FORCE + cd app && $(MAKE) -f $(MAKEFILE) +sub-app-make_default-ordered: app\$(MAKEFILE) sub-src-make_default-ordered FORCE + cd app && $(MAKE) -f $(MAKEFILE) +sub-app-make_default: app\$(MAKEFILE) FORCE + cd app && $(MAKE) -f $(MAKEFILE) +sub-app-make_first-ordered: app\$(MAKEFILE) sub-src-make_first-ordered FORCE + cd app && $(MAKE) -f $(MAKEFILE) first +sub-app-make_first: app\$(MAKEFILE) FORCE + cd app && $(MAKE) -f $(MAKEFILE) first +sub-app-all-ordered: app\$(MAKEFILE) sub-src-all-ordered FORCE + cd app && $(MAKE) -f $(MAKEFILE) all +sub-app-all: app\$(MAKEFILE) FORCE + cd app && $(MAKE) -f $(MAKEFILE) all +sub-app-clean-ordered: app\$(MAKEFILE) sub-src-clean-ordered FORCE + cd app && $(MAKE) -f $(MAKEFILE) clean +sub-app-clean: app\$(MAKEFILE) FORCE + cd app && $(MAKE) -f $(MAKEFILE) clean +sub-app-distclean-ordered: app\$(MAKEFILE) sub-src-distclean-ordered FORCE + cd app && $(MAKE) -f $(MAKEFILE) distclean +sub-app-distclean: app\$(MAKEFILE) FORCE + cd app && $(MAKE) -f $(MAKEFILE) distclean +sub-app-install_subtargets-ordered: app\$(MAKEFILE) sub-src-install_subtargets-ordered FORCE + cd app && $(MAKE) -f $(MAKEFILE) install +sub-app-install_subtargets: app\$(MAKEFILE) FORCE + cd app && $(MAKE) -f $(MAKEFILE) install +sub-app-uninstall_subtargets-ordered: app\$(MAKEFILE) sub-src-uninstall_subtargets-ordered FORCE + cd app && $(MAKE) -f $(MAKEFILE) uninstall +sub-app-uninstall_subtargets: app\$(MAKEFILE) FORCE + cd app && $(MAKE) -f $(MAKEFILE) uninstall + +Makefile: complex.pro c:\coding\Qt\4.2.2\mkspecs\win32-g++\qmake.conf C:/coding/Qt/4.2.2/mkspecs/qconfig.pri \ + c:\coding\Qt\4.2.2\mkspecs\features\qt_functions.prf \ + c:\coding\Qt\4.2.2\mkspecs\features\qt_config.prf \ + c:\coding\Qt\4.2.2\mkspecs\features\exclusive_builds.prf \ + c:\coding\Qt\4.2.2\mkspecs\features\default_pre.prf \ + c:\coding\Qt\4.2.2\mkspecs\features\win32\default_pre.prf \ + c:\coding\Qt\4.2.2\mkspecs\features\debug.prf \ + c:\coding\Qt\4.2.2\mkspecs\features\debug_and_release.prf \ + c:\coding\Qt\4.2.2\mkspecs\features\default_post.prf \ + c:\coding\Qt\4.2.2\mkspecs\features\win32\rtti.prf \ + c:\coding\Qt\4.2.2\mkspecs\features\win32\exceptions.prf \ + c:\coding\Qt\4.2.2\mkspecs\features\win32\stl.prf \ + c:\coding\Qt\4.2.2\mkspecs\features\shared.prf \ + c:\coding\Qt\4.2.2\mkspecs\features\warn_on.prf \ + c:\coding\Qt\4.2.2\mkspecs\features\qt.prf \ + c:\coding\Qt\4.2.2\mkspecs\features\win32\thread.prf \ + c:\coding\Qt\4.2.2\mkspecs\features\moc.prf \ + c:\coding\Qt\4.2.2\mkspecs\features\win32\windows.prf \ + c:\coding\Qt\4.2.2\mkspecs\features\resources.prf \ + c:\coding\Qt\4.2.2\mkspecs\features\uic.prf + $(QMAKE) -win32 -o Makefile complex.pro +C:/coding/Qt/4.2.2/mkspecs/qconfig.pri: +c:\coding\Qt\4.2.2\mkspecs\features\qt_functions.prf: +c:\coding\Qt\4.2.2\mkspecs\features\qt_config.prf: +c:\coding\Qt\4.2.2\mkspecs\features\exclusive_builds.prf: +c:\coding\Qt\4.2.2\mkspecs\features\default_pre.prf: +c:\coding\Qt\4.2.2\mkspecs\features\win32\default_pre.prf: +c:\coding\Qt\4.2.2\mkspecs\features\debug.prf: +c:\coding\Qt\4.2.2\mkspecs\features\debug_and_release.prf: +c:\coding\Qt\4.2.2\mkspecs\features\default_post.prf: +c:\coding\Qt\4.2.2\mkspecs\features\win32\rtti.prf: +c:\coding\Qt\4.2.2\mkspecs\features\win32\exceptions.prf: +c:\coding\Qt\4.2.2\mkspecs\features\win32\stl.prf: +c:\coding\Qt\4.2.2\mkspecs\features\shared.prf: +c:\coding\Qt\4.2.2\mkspecs\features\warn_on.prf: +c:\coding\Qt\4.2.2\mkspecs\features\qt.prf: +c:\coding\Qt\4.2.2\mkspecs\features\win32\thread.prf: +c:\coding\Qt\4.2.2\mkspecs\features\moc.prf: +c:\coding\Qt\4.2.2\mkspecs\features\win32\windows.prf: +c:\coding\Qt\4.2.2\mkspecs\features\resources.prf: +c:\coding\Qt\4.2.2\mkspecs\features\uic.prf: +qmake: qmake_all FORCE + @$(QMAKE) -win32 -o Makefile complex.pro + +qmake_all: sub-src-qmake_all sub-app-qmake_all FORCE + +make_default: sub-src-make_default-ordered sub-app-make_default-ordered FORCE +make_first: sub-src-make_first-ordered sub-app-make_first-ordered FORCE +all: sub-src-all-ordered sub-app-all-ordered FORCE +clean: sub-src-clean-ordered sub-app-clean-ordered FORCE +distclean: sub-src-distclean-ordered sub-app-distclean-ordered FORCE + -$(DEL_FILE) Makefile +install_subtargets: sub-src-install_subtargets-ordered sub-app-install_subtargets-ordered FORCE +uninstall_subtargets: sub-src-uninstall_subtargets-ordered sub-app-uninstall_subtargets-ordered FORCE + +sub-src-sub_Debug-ordered: src\$(MAKEFILE) + cd src && $(MAKE) debug +sub-app-sub_Debug-ordered: app\$(MAKEFILE) sub-src-sub_Debug-ordered + cd app && $(MAKE) debug +debug: sub-src-sub_Debug-ordered sub-app-sub_Debug-ordered + +sub-src-sub_Release-ordered: src\$(MAKEFILE) + cd src && $(MAKE) release +sub-app-sub_Release-ordered: app\$(MAKEFILE) sub-src-sub_Release-ordered + cd app && $(MAKE) release +release: sub-src-sub_Release-ordered sub-app-sub_Release-ordered + +mocclean: compiler_moc_header_clean compiler_moc_source_clean + +mocables: compiler_moc_header_make_all compiler_moc_source_make_all +install: install_subtargets FORCE + +uninstall: uninstall_subtargets FORCE + +FORCE: + diff --git a/Chapter15/qmake/complex/app/app.pro b/Chapter15/qmake/complex/app/app.pro new file mode 100644 index 0000000..3217aaa --- /dev/null +++ b/Chapter15/qmake/complex/app/app.pro @@ -0,0 +1,41 @@ +# +# Copyright (c) 2006-2007, Johan Thelin +# +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# * Neither the name of APress nor the names of its contributors +# may be used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# + +TEMPLATE = app +TARGET = app +DESTDIR = ../bin + +INCLUDEPATH += . ../include +DEPENDPATH += . + +LIBS += -L../lib -lbase + +SOURCES += appwindow.cpp main.cpp +HEADERS += appwindow.h \ No newline at end of file diff --git a/Chapter15/qmake/complex/app/appwindow.cpp b/Chapter15/qmake/complex/app/appwindow.cpp new file mode 100644 index 0000000..46d0fa7 --- /dev/null +++ b/Chapter15/qmake/complex/app/appwindow.cpp @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "appwindow.h" + +#include +#include +#include +#include + +#include "base.h" + +AppWindow::AppWindow( QWidget *parent ) : QWidget( parent ) +{ + spinBox = new QSpinBox( this ); + spinBox->setRange( 1, 100 ); + spinBox->setValue( 42 ); + + QPushButton *button = new QPushButton( this ); + button->setText( tr("Hit me!") ); + connect( button, SIGNAL(clicked()), this, SLOT(buttonClicked()) ); + + QHBoxLayout *layout = new QHBoxLayout( this ); + layout->addWidget( spinBox ); + layout->addWidget( button ); + + base = new Base( this ); + connect( base, SIGNAL(numberPresented(QString)), this, SLOT(showDialog(QString)) ); +} + +void AppWindow::buttonClicked() +{ + base->presentNumber( spinBox->value() ); +} + +void AppWindow::showDialog( const QString &text ) +{ + QMessageBox::information( this, tr("Message Received"), text ); +} diff --git a/Chapter15/qmake/complex/app/appwindow.h b/Chapter15/qmake/complex/app/appwindow.h new file mode 100644 index 0000000..b9f9c43 --- /dev/null +++ b/Chapter15/qmake/complex/app/appwindow.h @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef APPWINDOW_H +#define APPWINDOW_H + +#include + +class QSpinBox; +class Base; + +class AppWindow : public QWidget +{ + Q_OBJECT + +public: + AppWindow( QWidget *parent=0 ); + +private slots: + void buttonClicked(); + void showDialog( const QString& ); + +private: + QSpinBox *spinBox; + Base *base; +}; + +#endif // APPWINDOW_H diff --git a/Chapter15/qmake/complex/app/main.cpp b/Chapter15/qmake/complex/app/main.cpp new file mode 100644 index 0000000..27bf590 --- /dev/null +++ b/Chapter15/qmake/complex/app/main.cpp @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include "appwindow.h" + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); + + AppWindow appWindow; + appWindow.show(); + + return app.exec(); +} diff --git a/Chapter15/qmake/complex/complex.pro b/Chapter15/qmake/complex/complex.pro new file mode 100644 index 0000000..5080ac5 --- /dev/null +++ b/Chapter15/qmake/complex/complex.pro @@ -0,0 +1,3 @@ +TEMPLATE = subdirs +SUBDIRS = src app +CONFIG += ordered \ No newline at end of file diff --git a/Chapter15/qmake/complex/complex.pro.quote b/Chapter15/qmake/complex/complex.pro.quote new file mode 100644 index 0000000..9a5fc9e --- /dev/null +++ b/Chapter15/qmake/complex/complex.pro.quote @@ -0,0 +1,5 @@ +///++ all +TEMPLATE = subdirs +SUBDIRS = src app +CONFIG += ordered +///-- all diff --git a/Chapter15/qmake/complex/files.txt b/Chapter15/qmake/complex/files.txt new file mode 100644 index 0000000..75aba3d --- /dev/null +++ b/Chapter15/qmake/complex/files.txt @@ -0,0 +1,19 @@ +///++ all +| complex.pro +| ++---bin ++---lib +| ++---app +| | app.pro +| | appwindow.cpp +| | appwindow.h +| | main.cpp +| ++---include +| base.h +| +\---src + | base.cpp + | src.pro +///-- all \ No newline at end of file diff --git a/Chapter15/qmake/complex/include/base.h b/Chapter15/qmake/complex/include/base.h new file mode 100644 index 0000000..45d12c3 --- /dev/null +++ b/Chapter15/qmake/complex/include/base.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef BASE_H +#define BASE_H + +#include + +class Base : public QObject +{ + Q_OBJECT + +public: + Base( QObject *parent=0 ); + +public slots: + void presentNumber( int ); + +signals: + void numberPresented( const QString& ); +}; + +#endif // BASE_H diff --git a/Chapter15/qmake/complex/src/base.cpp b/Chapter15/qmake/complex/src/base.cpp new file mode 100644 index 0000000..c43292e --- /dev/null +++ b/Chapter15/qmake/complex/src/base.cpp @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "base.h" + +Base::Base( QObject *parent ) : QObject( parent ) +{ +} + +void Base::presentNumber( int number ) +{ + emit numberPresented( QString( "This is the number to present: %1" ).arg( number ) ); +} diff --git a/Chapter15/qmake/complex/src/src.pro b/Chapter15/qmake/complex/src/src.pro new file mode 100644 index 0000000..b1062d4 --- /dev/null +++ b/Chapter15/qmake/complex/src/src.pro @@ -0,0 +1,42 @@ +# +# Copyright (c) 2006-2007, Johan Thelin +# +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# * Neither the name of APress nor the names of its contributors +# may be used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# + +TEMPLATE = lib +TARGET = base +VERSION = 0.1.0 +CONFIG += static + +DESTDIR = ../lib + +INCLUDEPATH += ../include +DEPENDPATH += ../include + +SOURCES += base.cpp +HEADERS += base.h \ No newline at end of file diff --git a/Chapter16/README.txt b/Chapter16/README.txt new file mode 100644 index 0000000..953d707 --- /dev/null +++ b/Chapter16/README.txt @@ -0,0 +1,52 @@ +This file is a part of 1590598318-1.zip containing example source code for the +Foundations of Qt Development book available from APress (ISBN 1590598318). + +These are the examples for chapter 16 - Unit Testing + +basic + + Listings 16-1 + + A basic framework for running unit-tests. + + +classtest + + Listings 16-2, 16-3, 16-4, 16-5, 16-6 + + Straight forward tests. + + +classdata + + Listings 16-11, 16-12, 16-13 + + Data-driven tests. + + +widgettest + + Listings 16-15, 16-16, 16-17, 16-18 + + Straight forward testing of widgets. + + +widgetdata + + Listings 16-19, 16-20 + + Data-driven testing of widgets. + + +signaltest + + Listing 16-21 + + Testing of signals. + + +imagecollectiontest + + Listings 16-22, 16-23, 16-24, 16-25 + + Testing of the image collection application introduced in chapter 13. diff --git a/Chapter16/basic/main.cpp b/Chapter16/basic/main.cpp new file mode 100644 index 0000000..b57c391 --- /dev/null +++ b/Chapter16/basic/main.cpp @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +class MyTestClass : public QObject +{ + Q_OBJECT + +private slots: + // Test cases goes here +}; + +QTEST_MAIN( DateTest ) diff --git a/Chapter16/classdata/classdata.pro b/Chapter16/classdata/classdata.pro new file mode 100644 index 0000000..2236226 --- /dev/null +++ b/Chapter16/classdata/classdata.pro @@ -0,0 +1,13 @@ +###################################################################### +# Automatically generated by qmake (2.01a) ti 23. jan 18:57:08 2007 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +HEADERS += datetest.h +SOURCES += datetest.cpp main.cpp +CONFIG += qtestlib console diff --git a/Chapter16/classdata/datetest.cpp b/Chapter16/classdata/datetest.cpp new file mode 100644 index 0000000..6c85e8f --- /dev/null +++ b/Chapter16/classdata/datetest.cpp @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include + +#include "datetest.h" + +void DateTest::testAdd() +{ + QDate date( 1979, 5, 16 ); + + QFETCH( int, addYear ); + QFETCH( int, addMonth ); + QFETCH( int, addDay ); + + QDate next = date.addYears( addYear ).addMonths( addMonth ).addDays( addDay ); + + QTEST( next.year(), "year" ); + QTEST( next.month(), "month" ); + QTEST( next.day(), "day" ); +} + +void DateTest::testAdd_data () +{ + QTest::addColumn( "addYear" ); + QTest::addColumn( "addMonth" ); + QTest::addColumn( "addDay" ); + QTest::addColumn( "year" ); + QTest::addColumn( "month" ); + QTest::addColumn( "day" ); + + QTest::newRow( "Start date" ) << 0 << 0 << 0 << 1979 << 5 << 16; + QTest::newRow( "One day" ) << 0 << 0 << 1 << 1979 << 5 << 17; + QTest::newRow( "Twenty days" ) << 0 << 0 << 20 << 1979 << 6 << 5; + QTest::newRow( "366 days" ) << 0 << 0 << 366 << 1980 << 5 << 16; + QTest::newRow( "One month" ) << 0 << 1 << 0 << 1979 << 6 << 16; + QTest::newRow( "Twelve months" ) << 0 << 12 << 0 << 1980 << 5 << 16; + QTest::newRow( "28 years" ) << 28 << 0 << 0 << 2007 << 5 << 16; +} + +void DateTest::testValid() +{ + QFETCH( int, year ); + QFETCH( int, month ); + QFETCH( int, day ); + + QDate date( year, month, day ); + QTEST( date.isValid(), "valid" ); +} + +void DateTest::testValid_data() +{ + QTest::addColumn( "year" ); + QTest::addColumn( "month" ); + QTest::addColumn( "day" ); + QTest::addColumn( "valid" ); + + QTest::newRow( "Valid, normal" ) << 1973 << 8 << 16 << true; + QTest::newRow( "Invalid, normal" ) << 1973 << 9 << 31 << false; + QTest::newRow( "Valid, leap-year" ) << 1980 << 2 << 29 << true; + QTest::newRow( "Invalid, leap-year" ) << 1981 << 2 << 29 << false; +} diff --git a/Chapter16/classdata/datetest.h b/Chapter16/classdata/datetest.h new file mode 100644 index 0000000..3dec726 --- /dev/null +++ b/Chapter16/classdata/datetest.h @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef DATETEST_H +#define DATETEST_H + +#include + +class DateTest : public QObject +{ + Q_OBJECT + +private slots: + void testAdd(); + void testAdd_data(); + + void testValid(); + void testValid_data(); +}; + +#endif // DATETEST_H diff --git a/Chapter16/classdata/main.cpp b/Chapter16/classdata/main.cpp new file mode 100644 index 0000000..b322866 --- /dev/null +++ b/Chapter16/classdata/main.cpp @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include "datetest.h" + +QTEST_MAIN( DateTest ) diff --git a/Chapter16/classtest/classtest.pro b/Chapter16/classtest/classtest.pro new file mode 100644 index 0000000..4a14b1d --- /dev/null +++ b/Chapter16/classtest/classtest.pro @@ -0,0 +1,13 @@ +###################################################################### +# Automatically generated by qmake (2.01a) ti 23. jan 18:26:56 2007 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +HEADERS += datetest.h +SOURCES += datetest.cpp main.cpp +CONFIG += qtestlib console diff --git a/Chapter16/classtest/datetest.cpp b/Chapter16/classtest/datetest.cpp new file mode 100644 index 0000000..0709aa1 --- /dev/null +++ b/Chapter16/classtest/datetest.cpp @@ -0,0 +1,107 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include + +#include "datetest.h" + +void DateTest::testAddDay() +{ + QDate date( 1979, 5, 16 ); + QCOMPARE( date.year(), 1979 ); + QCOMPARE( date.month(), 5 ); + QCOMPARE( date.day(), 16 ); + + QDate next = date.addDays( 1 ); + QCOMPARE( next.year(), 1979 ); + QCOMPARE( next.month(), 5 ); + QCOMPARE( next.day(), 17 ); + + next = date.addDays( 20 ); + QCOMPARE( next.year(), 1979 ); + QCOMPARE( next.month(), 6 ); + QCOMPARE( next.day(), 5 ); + + next = date.addDays( 366 ); + QCOMPARE( next.year(), 1980 ); + QCOMPARE( next.month(), 5 ); + QCOMPARE( next.day(), 16 ); +} + +void DateTest::testAddMonth() +{ + QDate date( 1973, 8, 16 ); + QCOMPARE( date.year(), 1973 ); + QCOMPARE( date.month(), 8 ); + QCOMPARE( date.day(), 16 ); + + QDate next = date.addMonths( 1 ); + QCOMPARE( next.year(), 1973 ); + QCOMPARE( next.month(), 9 ); + QCOMPARE( next.day(), 16 ); + + next = date.addMonths( 12 ); + QCOMPARE( next.year(), 1974 ); + QCOMPARE( next.month(), 8 ); + QCOMPARE( next.day(), 16 ); +} + +void DateTest::testAddYear() +{ + QDate date( 1979, 12, 31 ); + QCOMPARE( date.year(), 1979 ); + QCOMPARE( date.month(), 12 ); + QCOMPARE( date.day(), 31 ); + + QDate next = date.addYears( 28 ); + QCOMPARE( next.year(), 2007 ); + QCOMPARE( next.month(), 12 ); + QCOMPARE( next.day(), 31 ); +} + +void DateTest::testValid() +{ + QDate date; + + date = QDate(); + QVERIFY( !date.isValid() ); + + date = QDate( 1979, 5, 16 ); + QVERIFY( date.isValid() ); + + date = QDate( 1980, 2, 29 ); + QVERIFY( date.isValid() ); + + date = QDate( 1979, 2, 29 ); + QVERIFY( !date.isValid() ); +} diff --git a/Chapter16/classtest/datetest.h b/Chapter16/classtest/datetest.h new file mode 100644 index 0000000..84d8f6c --- /dev/null +++ b/Chapter16/classtest/datetest.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef DATETEST_H +#define DATETEST_H + +#include + +class DateTest : public QObject +{ + Q_OBJECT + +private slots: + void testAddDay(); + void testAddMonth(); + void testAddYear(); + void testValid(); +}; + +#endif // DATETEST_H diff --git a/Chapter16/classtest/main.cpp b/Chapter16/classtest/main.cpp new file mode 100644 index 0000000..b322866 --- /dev/null +++ b/Chapter16/classtest/main.cpp @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include "datetest.h" + +QTEST_MAIN( DateTest ) diff --git a/Chapter16/imagecollectiontest/imagecollection.cpp b/Chapter16/imagecollectiontest/imagecollection.cpp new file mode 100644 index 0000000..e8d27ef --- /dev/null +++ b/Chapter16/imagecollectiontest/imagecollection.cpp @@ -0,0 +1,150 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include +#include + +#include "imagecollection.h" + +ImageCollection::ImageCollection() +{ + QSqlDatabase db = QSqlDatabase::addDatabase( "QSQLITE" ); + + db.setDatabaseName( ":memory:" ); + if( !db.open() ) + qFatal( "Failed to open database" ); + + populateDatabase(); +} + +void ImageCollection::populateDatabase() +{ + QSqlQuery qry; + + qry.prepare( "CREATE TABLE IF NOT EXISTS images (id INTEGER UNIQUE PRIMARY KEY, data BLOB)" ); + if( !qry.exec() ) + qFatal( "Failed to create table images" ); + + qry.prepare( "CREATE TABLE IF NOT EXISTS tags (id INTEGER, tag VARCHAR(30))" ); + if( !qry.exec() ) + qFatal( "Failed to create table tags" ); +} + +QList ImageCollection::getIds( QStringList tags ) +{ + QSqlQuery qry; + + if( tags.count() == 0 ) + qry.prepare( "SELECT images.id FROM images" ); + else + qry.prepare( "SELECT id FROM tags WHERE tag IN ('" + tags.join("','") + "') GROUP BY id" ); + + if( !qry.exec() ) + qFatal( "Failed to get IDs" ); + + QList result; + while( qry.next() ) + result << qry.value(0).toInt(); + + return result; +} + +QStringList ImageCollection::getTags() +{ + QSqlQuery qry; + + qry.prepare( "SELECT tag FROM tags GROUP BY tag" ); + if( !qry.exec() ) + qFatal( "Failed to get tags" ); + + QStringList result; + while( qry.next() ) + result << qry.value(0).toString(); + + return result; +} + +void ImageCollection::addTag( int id, QString tag ) +{ + QSqlQuery qry; + + qry.prepare( "INSERT INTO tags (id, tag) VALUES (:id, :tag)" ); + qry.bindValue( ":id", id ); + qry.bindValue( ":tag", tag ); + if( !qry.exec() ) + qFatal( "Failed to add tag" ); +} + +QImage ImageCollection::getImage( int id ) +{ + QSqlQuery qry; + + qry.prepare( "SELECT data FROM images WHERE id = :id" ); + qry.bindValue( ":id", id ); + if( !qry.exec() ) + qFatal( "Failed to get image" ); + if( !qry.next() ) + qFatal( "Failed to get image id" ); + + QByteArray array = qry.value(0).toByteArray(); + QBuffer buffer(&array); + buffer.open( QIODevice::ReadOnly ); + + QImageReader reader(&buffer, "PNG"); + QImage image = reader.read(); + + return image; +} + +void ImageCollection::addImage( QImage image, QStringList tags ) +{ + QBuffer buffer; + QImageWriter writer(&buffer, "PNG"); + writer.write(image); + + QSqlQuery qry; + + int id; + + qry.prepare( "SELECT COUNT(*) FROM images" ); + qry.exec(); + qry.next(); + id = qry.value(0).toInt() + 1; + + qry.prepare( "INSERT INTO images (id, data) VALUES (:id, :data)" ); + qry.bindValue( ":id", id ); + qry.bindValue( ":data", buffer.data() ); + qry.exec(); + + foreach( QString tag, tags ) + addTag( id, tag ); +} diff --git a/Chapter16/imagecollectiontest/imagecollection.h b/Chapter16/imagecollectiontest/imagecollection.h new file mode 100644 index 0000000..80d13b3 --- /dev/null +++ b/Chapter16/imagecollectiontest/imagecollection.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef IMAGECOLLECTION_H +#define IMAGECOLLECTION_H + +#include +#include +#include + +class ImageCollection +{ +public: + ImageCollection(); + + QImage getImage( int id ); + QList getIds( QStringList tags ); + QStringList getTags(); + + void addTag( int id, QString tag ); + void addImage( QImage image, QStringList tags ); + +private: + void populateDatabase(); +}; + +#endif // IMAGECOLLECTION_H diff --git a/Chapter16/imagecollectiontest/imagecollectiontest.cpp b/Chapter16/imagecollectiontest/imagecollectiontest.cpp new file mode 100644 index 0000000..22f11b6 --- /dev/null +++ b/Chapter16/imagecollectiontest/imagecollectiontest.cpp @@ -0,0 +1,158 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include + +#include "imagecollection.h" + +#include "imagecollectiontest.h" + +void ImageCollectionTest::testTags() +{ + ImageCollection c; + + // Make sure that the collection is empty + QCOMPARE( c.getTags().count(), 0 ); + + // At least one image is needed to be able to add tags + c.addImage( QImage( "test.png" ), QStringList() ); + + // Verify that we have one image and get the id for it + QList ids = c.getIds( QStringList() ); + QCOMPARE( ids.count(), 1 ); + int id = ids[0]; + + // Add one tag, total one + c.addTag( id, "Foo" ); + QCOMPARE( c.getTags().count(), 1 ); + + // Add one tag, total two + c.addTag( id, "Bar" ); + QCOMPARE( c.getTags().count(), 2 ); + + // Add one tag, total three + c.addTag( id, "Baz" ); + QCOMPARE( c.getTags().count(), 3 ); + + // Add a duplicate tag, total three + c.addTag( id, "Foo" ); + QCOMPARE( c.getTags().count(), 3 ); + + // Try to add a tag to a nonexisting id + QEXPECT_FAIL("", "The tag will be added to the non-existing image.", Continue); + c.addTag( id+1, "Foz" ); + QCOMPARE( c.getTags().count(), 3 ); + + QSqlDatabase::removeDatabase( QLatin1String( QSqlDatabase::defaultConnection ) ); +} + +void ImageCollectionTest::testImages() +{ + ImageCollection c; + + QCOMPARE( c.getIds( QStringList() ).count(), 0 ); + + QImage image( "test.png" ); + c.addImage( image, QStringList() ); + + // Verify that we have one image and get the id for it + QList ids = c.getIds( QStringList() ); + QCOMPARE( ids.count(), 1 ); + int id = ids[0]; + + QImage fromDb = c.getImage( id ); + QVERIFY( pixelCompareImages( image, fromDb ) ); + +// Will call qFatal and end the application +// QTest::ignoreMessage( QtFatalMsg, "Failed to get image id" ); +// fromDb = c.getImage( id+1 ); +// QVERIFY( fromDb.isNull() ); + + QSqlDatabase::removeDatabase( QLatin1String( QSqlDatabase::defaultConnection ) ); +} + +void ImageCollectionTest::testImagesFromTags() +{ + ImageCollection c; + + QCOMPARE( c.getIds( QStringList() ).count(), 0 ); + + QImage image( "test.png" ); + + QStringList tags; + tags << "Foo" << "Bar"; + + c.addImage( image, tags ); + QCOMPARE( c.getTags().count(), 2 ); + QCOMPARE( c.getIds( QStringList() ).count(), 1 ); + QCOMPARE( c.getIds( QStringList() << "Foo" ).count(), 1 ); + QCOMPARE( c.getIds( QStringList() << "Bar" ).count(), 1 ); + QCOMPARE( c.getIds( tags ).count(), 1 ); + QCOMPARE( c.getIds( QStringList() << "Baz" ).count(), 0 ); + + tags.clear(); + tags << "Baz"; + c.addImage( image, tags ); + QCOMPARE( c.getTags().count(), 3 ); + QCOMPARE( c.getIds( QStringList() ).count(), 2 ); + QCOMPARE( c.getIds( QStringList() << "Foo" ).count(), 1 ); + QCOMPARE( c.getIds( QStringList() << "Bar" ).count(), 1 ); + QCOMPARE( c.getIds( QStringList() << "Baz" ).count(), 1 ); + + tags.clear(); + tags << "Bar" << "Baz"; + c.addImage( image, tags ); + QCOMPARE( c.getTags().count(), 3 ); + QCOMPARE( c.getIds( QStringList() ).count(), 3 ); + QCOMPARE( c.getIds( QStringList() << "Foo" ).count(), 1 ); + QCOMPARE( c.getIds( QStringList() << "Bar" ).count(), 2 ); + QCOMPARE( c.getIds( tags ).count(), 3 ); + QCOMPARE( c.getIds( QStringList() << "Baz" ).count(), 2 ); + + QSqlDatabase::removeDatabase( QLatin1String( QSqlDatabase::defaultConnection ) ); +} + +bool ImageCollectionTest::pixelCompareImages( const QImage &a, const QImage &b ) +{ + if( a.size() != b.size() ) + return false; + + if( a.format() != b.format() ) + return false; + + for( int x=0; x +#include + +class ImageCollectionTest : public QObject +{ + Q_OBJECT +private slots: + void testTags(); + void testImages(); + void testImagesFromTags(); + +private: + bool pixelCompareImages( const QImage &a, const QImage &b ); +}; + +#endif // IMAGECOLLECTIONTEST_H diff --git a/Chapter16/imagecollectiontest/imagecollectiontest.pro b/Chapter16/imagecollectiontest/imagecollectiontest.pro new file mode 100644 index 0000000..e511ffa --- /dev/null +++ b/Chapter16/imagecollectiontest/imagecollectiontest.pro @@ -0,0 +1,14 @@ +###################################################################### +# Automatically generated by qmake (2.01a) ti 23. jan 15:22:12 2007 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +HEADERS += imagecollection.h imagecollectiontest.h +SOURCES += imagecollection.cpp imagecollectiontest.cpp main.cpp +CONFIG += qtestlib console +QT += sql diff --git a/Chapter16/imagecollectiontest/main.cpp b/Chapter16/imagecollectiontest/main.cpp new file mode 100644 index 0000000..f2418e8 --- /dev/null +++ b/Chapter16/imagecollectiontest/main.cpp @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include "imagecollectiontest.h" + +QTEST_MAIN(ImageCollectionTest) diff --git a/Chapter16/imagecollectiontest/test.png b/Chapter16/imagecollectiontest/test.png new file mode 100644 index 0000000..4cf6fd9 Binary files /dev/null and b/Chapter16/imagecollectiontest/test.png differ diff --git a/Chapter16/signaltest/main.cpp b/Chapter16/signaltest/main.cpp new file mode 100644 index 0000000..39c02c5 --- /dev/null +++ b/Chapter16/signaltest/main.cpp @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include "spinboxtest.h" + +QTEST_MAIN(SpinBoxTest) diff --git a/Chapter16/signaltest/signaltest.pro b/Chapter16/signaltest/signaltest.pro new file mode 100644 index 0000000..f372e6a --- /dev/null +++ b/Chapter16/signaltest/signaltest.pro @@ -0,0 +1,13 @@ +###################################################################### +# Automatically generated by qmake (2.01a) on 24. jan 10:08:34 2007 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +HEADERS += spinboxtest.h +SOURCES += main.cpp spinboxtest.cpp +CONFIG += qtestlib console diff --git a/Chapter16/signaltest/spinboxtest.cpp b/Chapter16/signaltest/spinboxtest.cpp new file mode 100644 index 0000000..4b152eb --- /dev/null +++ b/Chapter16/signaltest/spinboxtest.cpp @@ -0,0 +1,163 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include + +#include "spinboxtest.h" + +Q_DECLARE_METATYPE ( Qt::Key ) + +void SpinBoxTest::testKeys() +{ + QSpinBox spinBox; + spinBox.setRange( 1, 10 ); + + QFETCH( Qt::Key, key ); + QFETCH( int, startValue ); + + spinBox.setValue( startValue ); + + QSignalSpy spy( &spinBox, SIGNAL(valueChanged(int)) ); + + QTest::keyClick( &spinBox, key ); + QTEST( spinBox.value(), "endValue" ); + + QFETCH( bool, willSignal ); + if( willSignal ) + { + QCOMPARE( spy.count(), 1 ); + QTEST( spy.takeFirst()[0].toInt(), "endValue" ); + } + else + QCOMPARE( spy.count(), 0 ); +} + +void SpinBoxTest::testKeys_data() +{ + QTest::addColumn( "key" ); + QTest::addColumn( "startValue" ); + QTest::addColumn( "endValue" ); + QTest::addColumn( "willSignal" ); + + QTest::newRow( "Up" ) << Qt::Key_Up << 5 << 6 << true; + QTest::newRow( "Down" ) << Qt::Key_Down << 5 << 4 << true; + QTest::newRow( "Up, limit" ) << Qt::Key_Up << 10 << 10 << false; + QTest::newRow( "Down, limit" ) << Qt::Key_Down << 1 << 1 << false; +} + +void SpinBoxTest::testClicks() +{ + QSpinBox spinBox; + spinBox.setRange( 1, 10 ); + + QSize size = spinBox.size(); + QPoint upButton = QPoint( size.width()-2, 2 ); + QPoint downButton = QPoint( size.width()-2, size.height()-2 ); + + QFETCH( QString, direction ); + QFETCH( int, startValue ); + + spinBox.setValue( startValue ); + + QSignalSpy spy( &spinBox, SIGNAL(valueChanged(int)) ); + + if( direction.toLower() == "up" ) + QTest::mouseClick( &spinBox, Qt::LeftButton, 0, upButton ); + else if (direction.toLower() == "down" ) + QTest::mouseClick( &spinBox, Qt::LeftButton, 0, downButton ); + else + QWARN( "Unknown direction - no clicks issued." ); + + QTEST( spinBox.value(), "endValue" ); + + QFETCH( bool, willSignal ); + if( willSignal ) + { + QCOMPARE( spy.count(), 1 ); + QTEST( spy.takeFirst()[0].toInt(), "endValue" ); + } + else + QCOMPARE( spy.count(), 0 ); +} + +void SpinBoxTest::testClicks_data() +{ + QTest::addColumn( "direction" ); + QTest::addColumn( "startValue" ); + QTest::addColumn( "endValue" ); + QTest::addColumn( "willSignal" ); + + QTest::newRow( "Up" ) << "Up" << 5 << 6 << true; + QTest::newRow( "Down" ) << "Down" << 5 << 4 << true; + QTest::newRow( "Up, limit" ) << "Up" << 10 << 10 << false; + QTest::newRow( "Down, limit" ) << "Down" << 1 << 1 << false; +} + +void SpinBoxTest::testSetting() +{ + QSpinBox spinBox; + spinBox.setRange( 1, 10 ); + + QFETCH( int, startValue ); + spinBox.setValue( startValue ); + + QSignalSpy spy( &spinBox, SIGNAL(valueChanged(int)) ); + + QFETCH( int, value ); + spinBox.setValue( value ); + QTEST( spinBox.value(), "endValue" ); + + QFETCH( bool, willSignal ); + if( willSignal ) + { + QCOMPARE( spy.count(), 1 ); + QTEST( spy.takeFirst()[0].toInt(), "endValue" ); + } + else + QCOMPARE( spy.count(), 0 ); +} + +void SpinBoxTest::testSetting_data() +{ + QTest::addColumn( "startValue" ); + QTest::addColumn( "value" ); + QTest::addColumn( "endValue" ); + QTest::addColumn( "willSignal" ); + + QTest::newRow( "Valid" ) << 1 << 5 << 5 << true; + QTest::newRow( "Over" ) << 9 << 11 << 10 << true; + QTest::newRow( "Under" ) << 2 << 0 << 1 << true; + QTest::newRow( "Valid, no change" ) << 5 << 5 << 5 << false; + QTest::newRow( "Over, no change" ) << 10 << 11 << 10 << false; + QTest::newRow( "Under, no change" ) << 1 << 0 << 1 << false; +} diff --git a/Chapter16/signaltest/spinboxtest.h b/Chapter16/signaltest/spinboxtest.h new file mode 100644 index 0000000..763125b --- /dev/null +++ b/Chapter16/signaltest/spinboxtest.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef SPINBOXTEST_H +#define SPINBOXTEST_H + +#include + +class SpinBoxTest : public QObject +{ + Q_OBJECT +private slots: + void testKeys(); + void testKeys_data(); + + void testClicks(); + void testClicks_data(); + + void testSetting(); + void testSetting_data(); +}; + +#endif // SPINBOXTEST_H diff --git a/Chapter16/widgetdata/main.cpp b/Chapter16/widgetdata/main.cpp new file mode 100644 index 0000000..39c02c5 --- /dev/null +++ b/Chapter16/widgetdata/main.cpp @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include "spinboxtest.h" + +QTEST_MAIN(SpinBoxTest) diff --git a/Chapter16/widgetdata/spinboxtest.cpp b/Chapter16/widgetdata/spinboxtest.cpp new file mode 100644 index 0000000..1b92602 --- /dev/null +++ b/Chapter16/widgetdata/spinboxtest.cpp @@ -0,0 +1,120 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include + +#include "spinboxtest.h" + +Q_DECLARE_METATYPE( Qt::Key ) + +void SpinBoxTest::testKeys() +{ + QSpinBox spinBox; + spinBox.setRange( 1, 10 ); + + QFETCH( Qt::Key, key ); + QFETCH( int, startValue ); + + spinBox.setValue( startValue ); + QTest::keyClick( &spinBox, key ); + QTEST( spinBox.value(), "endValue" ); +} + +void SpinBoxTest::testKeys_data() +{ + QTest::addColumn( "key" ); + QTest::addColumn( "startValue" ); + QTest::addColumn( "endValue" ); + + QTest::newRow( "Up" ) << Qt::Key_Up << 5 << 6; + QTest::newRow( "Down" ) << Qt::Key_Down << 5 << 4; + QTest::newRow( "Up, limit" ) << Qt::Key_Up << 10 << 10; + QTest::newRow( "Down, limit" ) << Qt::Key_Down << 1 << 1; +} + +void SpinBoxTest::testClicks() +{ + QSpinBox spinBox; + spinBox.setRange( 1, 10 ); + + QSize size = spinBox.size(); + QPoint upButton = QPoint( size.width()-2, 2 ); + QPoint downButton = QPoint( size.width()-2, size.height()-2 ); + + QFETCH( QString, direction ); + QFETCH( int, startValue ); + + spinBox.setValue( startValue ); + + if( direction.toLower() == "up" ) + QTest::mouseClick( &spinBox, Qt::LeftButton, 0, upButton ); + else if (direction.toLower() == "down" ) + QTest::mouseClick( &spinBox, Qt::LeftButton, 0, downButton ); + else + QWARN( "Unknown direction - no clicks issued." ); + + QTEST( spinBox.value(), "endValue" ); +} + +void SpinBoxTest::testClicks_data() +{ + QTest::addColumn( "direction" ); + QTest::addColumn( "startValue" ); + QTest::addColumn( "endValue" ); + + QTest::newRow( "Up" ) << "Up" << 5 << 6; + QTest::newRow( "Down" ) << "Down" << 5 << 4; + QTest::newRow( "Up, limit" ) << "Up" << 10 << 10; + QTest::newRow( "Down, limit" ) << "Down" << 1 << 1; +} + +void SpinBoxTest::testSetting() +{ + QSpinBox spinBox; + spinBox.setRange( 1, 10 ); + + QFETCH( int, value ); + + spinBox.setValue( value ); + QTEST( spinBox.value(), "endValue" ); +} + +void SpinBoxTest::testSetting_data() +{ + QTest::addColumn( "value" ); + QTest::addColumn( "endValue" ); + + QTest::newRow( "Valid" ) << 5 << 5; + QTest::newRow( "Over" ) << 11 << 10; + QTest::newRow( "Under" ) << 0 << 1; +} diff --git a/Chapter16/widgetdata/spinboxtest.h b/Chapter16/widgetdata/spinboxtest.h new file mode 100644 index 0000000..763125b --- /dev/null +++ b/Chapter16/widgetdata/spinboxtest.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef SPINBOXTEST_H +#define SPINBOXTEST_H + +#include + +class SpinBoxTest : public QObject +{ + Q_OBJECT +private slots: + void testKeys(); + void testKeys_data(); + + void testClicks(); + void testClicks_data(); + + void testSetting(); + void testSetting_data(); +}; + +#endif // SPINBOXTEST_H diff --git a/Chapter16/widgetdata/widgetdata.pro b/Chapter16/widgetdata/widgetdata.pro new file mode 100644 index 0000000..c3367c3 --- /dev/null +++ b/Chapter16/widgetdata/widgetdata.pro @@ -0,0 +1,13 @@ +###################################################################### +# Automatically generated by qmake (2.01a) ti 23. jan 19:14:04 2007 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +HEADERS += spinboxtest.h +SOURCES += main.cpp spinboxtest.cpp +CONFIG += qtestlib console diff --git a/Chapter16/widgettest/main.cpp b/Chapter16/widgettest/main.cpp new file mode 100644 index 0000000..39c02c5 --- /dev/null +++ b/Chapter16/widgettest/main.cpp @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include "spinboxtest.h" + +QTEST_MAIN(SpinBoxTest) diff --git a/Chapter16/widgettest/spinboxtest.cpp b/Chapter16/widgettest/spinboxtest.cpp new file mode 100644 index 0000000..a6dc87f --- /dev/null +++ b/Chapter16/widgettest/spinboxtest.cpp @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include + +#include "spinboxtest.h" + +void SpinBoxTest::testKeys() +{ + QSpinBox spinBox; + + spinBox.setRange( 1, 10 ); + spinBox.setValue( 5 ); + + QTest::keyClick( &spinBox, Qt::Key_Up ); + QCOMPARE( spinBox.value(), 6 ); + + QTest::keyClick( &spinBox, Qt::Key_Down ); + QCOMPARE( spinBox.value(), 5 ); + + spinBox.setValue( 10 ); + QTest::keyClick( &spinBox, Qt::Key_Up ); + QCOMPARE( spinBox.value(), 10 ); + + spinBox.setValue( 1 ); + QTest::keyClick( &spinBox, Qt::Key_Down ); + QCOMPARE( spinBox.value(), 1 ); +} + +void SpinBoxTest::testClicks() +{ + QSpinBox spinBox; + + spinBox.setRange( 1, 10 ); + spinBox.setValue( 5 ); + + QSize size = spinBox.size(); + QPoint upButton = QPoint( size.width()-2, 2 ); + QPoint downButton = QPoint( size.width()-2, size.height()-2 ); + + QTest::mouseClick( &spinBox, Qt::LeftButton, 0, upButton ); + QCOMPARE( spinBox.value(), 6 ); + + QTest::mouseClick( &spinBox, Qt::LeftButton, 0, downButton ); + QCOMPARE( spinBox.value(), 5 ); + + spinBox.setValue( 10 ); + QTest::mouseClick( &spinBox, Qt::LeftButton, 0, upButton ); + QCOMPARE( spinBox.value(), 10 ); + + spinBox.setValue( 1 ); + QTest::mouseClick( &spinBox, Qt::LeftButton, 0, downButton ); + QCOMPARE( spinBox.value(), 1 ); +} + +void SpinBoxTest::testSetting() +{ + QSpinBox spinBox; + + spinBox.setRange( 1, 10 ); + + spinBox.setValue( 5 ); + QCOMPARE( spinBox.value(), 5 ); + + spinBox.setValue( 0 ); + QCOMPARE( spinBox.value(), 1 ); + + spinBox.setValue( 11 ); + QCOMPARE( spinBox.value(), 10 ); +} diff --git a/Chapter16/widgettest/spinboxtest.h b/Chapter16/widgettest/spinboxtest.h new file mode 100644 index 0000000..366299a --- /dev/null +++ b/Chapter16/widgettest/spinboxtest.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2006-2007, Johan Thelin + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of APress nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef SPINBOXTEST_H +#define SPINBOXTEST_H + +#include + +class SpinBoxTest : public QObject +{ + Q_OBJECT + +private slots: + void testKeys(); + void testClicks(); + void testSetting(); +}; + +#endif // SPINBOXTEST_H diff --git a/Chapter16/widgettest/widgettest.pro b/Chapter16/widgettest/widgettest.pro new file mode 100644 index 0000000..a371649 --- /dev/null +++ b/Chapter16/widgettest/widgettest.pro @@ -0,0 +1,13 @@ +###################################################################### +# Automatically generated by qmake (2.01a) ti 23. jan 14:37:28 2007 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +HEADERS += spinboxtest.h +SOURCES += main.cpp spinboxtest.cpp +CONFIG += qtestlib console diff --git a/GPL-COPYING.txt b/GPL-COPYING.txt new file mode 100644 index 0000000..df08e58 --- /dev/null +++ b/GPL-COPYING.txt @@ -0,0 +1,361 @@ + + The Qt GUI Toolkit is Copyright (C) 1994-2007 Trolltech ASA. + + You may use, distribute and copy the Qt GUI Toolkit under the terms of + GNU General Public License version 2, which is displayed below. + +------------------------------------------------------------------------- + + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc. + 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Library General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) year name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Library General +Public License instead of this License. + +------------------------------------------------------------------------- + +In addition, as a special exception, Trolltech gives permission to link the +code of its release of Qt with the OpenSSL project's "OpenSSL" library (or +modified versions of the "OpenSSL" library that use the same license as the +original version), and distribute the linked executables. + +You must comply with the GNU General Public License version 2 in all respects +for all of the code used other than the "OpenSSL" code. If you modify this +file, you may extend this exception to your version of the file, but you are +not obligated to do so. If you do not wish to do so, delete this exception +statement from your version of this file. diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..43d7aa5 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,27 @@ +Freeware License, some rights reserved + +Copyright (c) 2007 Johan Thelin + +Permission is hereby granted, free of charge, to anyone obtaining a copy +of this software and associated documentation files (the "Software"), +to work with the Software within the limits of freeware distribution and fair use. +This includes the rights to use, copy, and modify the Software for personal use. +Users are also allowed and encouraged to submit corrections and modifications +to the Software for the benefit of other users. + +It is not allowed to reuse, modify, or redistribute the Software for +commercial use in any way, or for a user’s educational materials such as books +or blog articles without prior permission from the copyright holder. + +The above copyright notice and this permission notice need to be included +in all copies or substantial portions of the software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + + diff --git a/README.md b/README.md new file mode 100644 index 0000000..540b283 --- /dev/null +++ b/README.md @@ -0,0 +1,15 @@ +#Apress Source Code + +This repository accompanies [*Foundations of Qt Development*](http://www.apress.com/9781590598313) by Johan Thelin (Apress, 2007). + +![Cover image](9781590598313.jpg) + +Download the files as a zip using the green button, or clone the repository to your machine using Git. + +##Releases + +Release v1.0 corresponds to the code in the published book, without corrections or updates. + +##Contributions + +See the file Contributing.md for more information on how you can contribute to this repository. diff --git a/README.txt b/README.txt new file mode 100644 index 0000000..403019a --- /dev/null +++ b/README.txt @@ -0,0 +1,47 @@ +This file is a part of 1590598318-1.zip containing example source code for the +Foundations of Qt Development book available from APress (ISBN 1590598318). + +This zip archive contains one directory for each chapter of the book. In each +such chapter directory, there is another README.txt file describing each example +briefly. This is the first revision of this file. + +Notice that most examples are protected by a BSD license, except those listed +below. These files where taked from the open source edition of Qt and are +covered by a GPL license. You can find more details about the GPL license in the +GPL-COPYING.txt file found in the zip archive containing this text file. + + Chapter03/messagebox/cut.png + + Chapter04/sdi/images/copy.png + Chapter04/sdi/images/cut.png + Chapter04/sdi/images/new.png + Chapter04/sdi/images/paste.png + Chapter04/mdi/images/copy.png + Chapter04/mdi/images/cut.png + Chapter04/mdi/images/new.png + Chapter04/mdi/images/paste.png + Chapter04/dock/images/copy.png + Chapter04/dock/images/cut.png + Chapter04/dock/images/new.png + Chapter04/dock/images/paste.png + Chapter08/readwriteapplication/images/copy.png + Chapter08/readwriteapplication/images/cut.png + Chapter08/readwriteapplication/images/new.png + Chapter08/readwriteapplication/images/paste.png + Chapter09/tooltips/images/qt.png + Chapter09/whatsthis/images/qt.png + Chapter09/assistant/documentation/images/qt.png + Chapter10/sdi/images/copy.png + Chapter10/sdi/images/cut.png + Chapter10/sdi/images/new.png + Chapter10/sdi/images/paste.png + +If you have any problems with these examples or find any errors in them, feel +free to visit http://www.thelins.se/qt and contact me so that we can solve these +issues. + +Thank you for using these examples - enjoy! + +Regards, + +Johan Thelin diff --git a/contributing.md b/contributing.md new file mode 100644 index 0000000..f6005ad --- /dev/null +++ b/contributing.md @@ -0,0 +1,14 @@ +# Contributing to Apress Source Code + +Copyright for Apress source code belongs to the author(s). However, under fair use you are encouraged to fork and contribute minor corrections and updates for the benefit of the author(s) and other readers. + +## How to Contribute + +1. Make sure you have a GitHub account. +2. Fork the repository for the relevant book. +3. Create a new branch on which to make your change, e.g. +`git checkout -b my_code_contribution` +4. Commit your change. Include a commit message describing the correction. Please note that if your commit message is not clear, the correction will not be accepted. +5. Submit a pull request. + +Thank you for your contribution! \ No newline at end of file