├── Chapter01 ├── Car │ ├── Car.cpp │ ├── Car.h │ ├── Car.vcxproj │ ├── Car.vcxproj.filters │ ├── Debug │ │ └── Car.log │ ├── Main.cpp │ └── Release │ │ ├── Car.Build.CppClean.log │ │ └── Car.log ├── Dice │ ├── Debug │ │ └── Dice.log │ ├── Dice.vcxproj │ ├── Dice.vcxproj.filters │ └── Main.cpp ├── Person │ ├── Debug │ │ └── Person.log │ ├── Employee.cpp │ ├── Employee.h │ ├── Main.cpp │ ├── Person.cpp │ ├── Person.h │ ├── Person.vcxproj │ ├── Person.vcxproj.filters │ ├── Release │ │ ├── Person.Build.CppClean.log │ │ └── Person.log │ ├── Student.cpp │ └── Student.h ├── Queue │ ├── Cell.cpp │ ├── Cell.h │ ├── Debug │ │ └── Queue.log │ ├── Main.cpp │ ├── Queue.cpp │ ├── Queue.h │ ├── Queue.vcxproj │ ├── Queue.vcxproj.filters │ └── Release │ │ ├── Queue.Build.CppClean.log │ │ └── Queue.log └── Stack │ ├── Cell.cpp │ ├── Cell.h │ ├── Debug │ └── Stack.log │ ├── Main.cpp │ ├── Release │ ├── Stack.Build.CppClean.log │ └── Stack.log │ ├── Stack.cpp │ ├── Stack.h │ ├── Stack.vcxproj │ └── Stack.vcxproj.filters ├── Chapter02 ├── ListAdvanced │ ├── Debug │ │ └── ListAdvanced.log │ ├── List.h │ ├── ListAdvanced.vcxproj │ ├── ListAdvanced.vcxproj.filters │ ├── Main.cpp │ └── Release │ │ ├── ListAdvanced.Build.CppClean.log │ │ └── ListAdvanced.log ├── ListBasic │ ├── Cell.cpp │ ├── Cell.h │ ├── Debug │ │ └── ListBasic.log │ ├── Iterator.cpp │ ├── Iterator.h │ ├── List.cpp │ ├── List.h │ ├── ListBasic.vcxproj │ ├── ListBasic.vcxproj.filters │ ├── Main.cpp │ └── Release │ │ ├── ListBasic.Build.CppClean.log │ │ └── ListBasic.log ├── SearchAndSortAdvanced │ ├── ArrayList.cpp │ ├── ArrayList.h │ ├── Debug │ │ └── SearchAndSortAdvanced.log │ ├── Main.cpp │ ├── Release │ │ ├── ArrayList.obj │ │ ├── Main.obj │ │ ├── SearchAn.18411F9B.tlog │ │ │ ├── CL.read.1.tlog │ │ │ ├── CL.write.1.tlog │ │ │ ├── SearchAndSortAdvanced.lastbuildstate │ │ │ ├── cl.command.1.tlog │ │ │ ├── link.command.1.tlog │ │ │ ├── link.read.1.tlog │ │ │ └── link.write.1.tlog │ │ ├── SearchAndSortAdvanced.log │ │ └── vc120.pdb │ ├── Search.h │ ├── SearchAndSortAdvanced.vcxproj │ ├── SearchAndSortAdvanced.vcxproj.filters │ └── Sort.h ├── SearchAndSortBasic │ ├── ArrayList.cpp │ ├── ArrayList.h │ ├── Debug │ │ └── SearchAndSortBasic.log │ ├── Main.cpp │ ├── Search.cpp │ ├── Search.h │ ├── SearchAndSortBasic.vcxproj │ ├── SearchAndSortBasic.vcxproj.filters │ ├── Sort.cpp │ └── Sort.h ├── SetAdvanced │ ├── Debug │ │ └── SetAdvanced.log │ ├── Main.cpp │ ├── Release │ │ ├── SetAdvanced.Build.CppClean.log │ │ └── SetAdvanced.log │ ├── Set.h │ ├── SetAdvanced.vcxproj │ └── SetAdvanced.vcxproj.filters └── SetBasic │ ├── Debug │ └── SetBasic.log │ ├── Main.cpp │ ├── Release │ ├── SetBasic.Build.CppClean.log │ └── SetBasic.log │ ├── Set.cpp │ ├── Set.h │ ├── SetBasic.vcxproj │ └── SetBasic.vcxproj.filters ├── Chapter03 └── LibraryBasic │ ├── Book.cpp │ ├── Book.h │ ├── Customer.cpp │ ├── Customer.h │ ├── Debug │ └── LibraryBasic.log │ ├── Library.cpp │ ├── Library.h │ ├── LibraryBasic.vcxproj │ ├── LibraryBasic.vcxproj.filters │ └── Main.cpp ├── Chapter04 └── LibraryPointer │ ├── Book.cpp │ ├── Book.h │ ├── Customer.cpp │ ├── Customer.h │ ├── Debug │ └── LibraryPointer.log │ ├── Library.cpp │ ├── Library.h │ ├── LibraryPointer.vcxproj │ ├── LibraryPointer.vcxproj.filters │ ├── Main.cpp │ ├── Register.binary │ └── Release │ ├── LibraryPointer.Build.CppClean.log │ └── LibraryPointer.log ├── Chapter05 ├── ClockBasic │ ├── Clock.cpp │ ├── Clock.h │ ├── ClockBasic.pro │ ├── ClockBasic.pro.user │ └── Main.cpp ├── DrawingBasic │ ├── DrawingBasic.pro │ ├── DrawingBasic.pro.user │ ├── DrawingBasic.qrc │ ├── DrawingWidget.cpp │ ├── DrawingWidget.h │ ├── DrawingWindow.cpp │ ├── DrawingWindow.h │ ├── Ellipse.cpp │ ├── Ellipse.h │ ├── Figure.cpp │ ├── Figure.h │ ├── Line.cpp │ ├── Line.h │ ├── Main.cpp │ ├── Rectangle.cpp │ ├── Rectangle.h │ └── images │ │ ├── center.png │ │ ├── copy.png │ │ ├── cut.png │ │ ├── delete.png │ │ ├── left.png │ │ ├── new.png │ │ ├── open.png │ │ ├── paste.png │ │ ├── qt.png │ │ ├── right.png │ │ └── save.png ├── EditorBasic │ ├── Caret.cpp │ ├── Caret.h │ ├── EditorBasic.pro │ ├── EditorBasic.pro.user │ ├── EditorBasic.qrc │ ├── EditorWidget.cpp │ ├── EditorWidget.h │ ├── EditorWindow.cpp │ ├── EditorWindow.h │ ├── Main.cpp │ └── images │ │ ├── center.png │ │ ├── copy.png │ │ ├── cut.png │ │ ├── left.png │ │ ├── new.png │ │ ├── open.png │ │ ├── paste.png │ │ ├── qt.png │ │ ├── right.png │ │ └── save.png └── MainWindow │ ├── DocumentWidget.cpp │ ├── DocumentWidget.h │ ├── Listener.h │ ├── MainWindow.cpp │ └── MainWindow.h ├── Chapter06 ├── ClockAdvanced │ ├── Clock.cpp │ ├── Clock.h │ ├── ClockAdvanced.pro │ ├── ClockAdvanced.pro.user │ └── Main.cpp ├── DrawingAdvanced │ ├── DrawingAdvanced.pro │ ├── DrawingAdvanced.pro.user │ ├── DrawingAdvanced.qrc │ ├── DrawingWidget.cpp │ ├── DrawingWidget.h │ ├── DrawingWindow.cpp │ ├── DrawingWindow.h │ ├── Ellipse.cpp │ ├── Ellipse.h │ ├── Figure.cpp │ ├── Figure.h │ ├── Line.cpp │ ├── Line.h │ ├── Main.cpp │ ├── Rectangle.cpp │ ├── Rectangle.h │ └── images │ │ ├── center.png │ │ ├── copy.png │ │ ├── cut.png │ │ ├── delete.png │ │ ├── left.png │ │ ├── new.png │ │ ├── open.png │ │ ├── paste.png │ │ ├── qt.png │ │ ├── right.png │ │ └── save.png ├── EditorAdvanced │ ├── Caret.cpp │ ├── Caret.h │ ├── EditorAdvanced.pro │ ├── EditorAdvanced.pro.user │ ├── EditorAdvanced.qrc │ ├── EditorWidget.cpp │ ├── EditorWidget.h │ ├── EditorWindow.cpp │ ├── EditorWindow.h │ ├── Main.cpp │ └── images │ │ ├── center.png │ │ ├── copy.png │ │ ├── cut.png │ │ ├── left.png │ │ ├── new.png │ │ ├── open.png │ │ ├── paste.png │ │ ├── qt.png │ │ ├── right.png │ │ └── save.png └── MainWindow │ ├── DocumentWidget.cpp │ ├── DocumentWidget.h │ ├── Listener.h │ ├── MainWindow.cpp │ └── MainWindow.h ├── Chapter07 ├── MainWindow │ ├── DocumentWidget.cpp │ ├── DocumentWidget.h │ ├── GameWidget.cpp │ ├── GameWidget.h │ ├── Listener.h │ ├── MainWindow.cpp │ └── MainWindow.h ├── NoughtsAndCrossesBasic │ ├── Main.cpp │ ├── NaCWidget.cpp │ ├── NaCWidget.h │ ├── NaCWindow.cpp │ └── NaCWindow.h └── OthelloBasic │ ├── Main.cpp │ ├── OthelloWidget.cpp │ ├── OthelloWidget.h │ ├── OthelloWindow.cpp │ └── OthelloWindow.h ├── Chapter08 ├── MainWindow │ ├── DocumentWidget.cpp │ ├── DocumentWidget.h │ ├── GameWidget.cpp │ ├── GameWidget.h │ ├── Listener.h │ ├── MainWindow.cpp │ └── MainWindow.h ├── NoughtsAndCrossesAdvanced │ ├── Main.cpp │ ├── NaCWidget.cpp │ ├── NaCWidget.h │ ├── NaCWindow.cpp │ └── NaCWindow.h └── OthelloAdvanced │ ├── Main.cpp │ ├── OthelloWidget.cpp │ ├── OthelloWidget.h │ ├── OthelloWindow.cpp │ └── OthelloWindow.h ├── Chapter09 ├── Action.cpp ├── Action.h ├── Colors.cpp ├── Colors.h ├── Error.cpp ├── Error.h ├── Input.dsl ├── Main.cpp ├── Parser.cpp ├── Parser.h ├── Scanner.cpp ├── Scanner.h ├── Token.cpp ├── Token.h ├── Value.cpp ├── Value.h ├── ViewerWidget.cpp └── ViewerWidget.h ├── Chapter10 ├── Action.cpp ├── Action.h ├── Colors.cpp ├── Colors.h ├── Directive.cpp ├── Directive.h ├── Error.cpp ├── Error.h ├── Evaluator.cpp ├── Evaluator.h ├── Function.cpp ├── Function.h ├── Input.dsl ├── Main.cpp ├── Parser.cpp ├── Parser.h ├── Scanner.cpp ├── Scanner.h ├── Token.cpp ├── Token.h ├── Value.cpp ├── Value.h ├── ViewerWidget.cpp └── ViewerWidget.h ├── LICENSE └── README.md /Chapter01/Car/Car.cpp: -------------------------------------------------------------------------------- 1 | #include "Car.h" 2 | 3 | Car::Car() 4 | :m_speed(0), 5 | m_direction(0) { 6 | // Empty. 7 | } 8 | 9 | Car::Car(int speed, int direction) 10 | :m_speed(speed), 11 | m_direction(direction) { 12 | // Empty. 13 | } 14 | 15 | Car::~Car() { 16 | // Empty. 17 | } 18 | 19 | int Car::getSpeed() const { 20 | return m_speed; 21 | } 22 | 23 | int Car::getDirection() const { 24 | return m_direction; 25 | } 26 | 27 | void Car::accelerate(int speed) { 28 | m_speed += speed; 29 | } 30 | 31 | void Car::decelerate(int speed) { 32 | m_speed -= speed; 33 | } 34 | 35 | void Car::turnLeft(int degrees) { 36 | m_direction -= degrees; 37 | } 38 | 39 | void Car::turnRight(int degrees) { 40 | m_direction += degrees; 41 | } 42 | -------------------------------------------------------------------------------- /Chapter01/Car/Car.h: -------------------------------------------------------------------------------- 1 | class Car { 2 | public: 3 | Car(); 4 | Car(int speed, int direction); 5 | 6 | int getSpeed() const; 7 | int getDirection() const; 8 | 9 | void accelerate(int speed); 10 | void decelerate(int speed); 11 | 12 | void turnLeft(int degrees); 13 | void turnRight(int degrees); 14 | 15 | ~Car(); 16 | 17 | private: 18 | int m_speed, m_direction; 19 | }; -------------------------------------------------------------------------------- /Chapter01/Car/Car.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | 14 | 15 | Source Files 16 | 17 | 18 | Source Files 19 | 20 | 21 | 22 | 23 | Header Files 24 | 25 | 26 | -------------------------------------------------------------------------------- /Chapter01/Car/Debug/Car.log: -------------------------------------------------------------------------------- 1 | Build started 2017-12-01 15:26:54. 2 | 3 | Build succeeded. 4 | 5 | Time Elapsed 00:00:00.02 6 | -------------------------------------------------------------------------------- /Chapter01/Car/Main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #include "Car.h" 4 | 5 | void main() { 6 | Car redVolvo; 7 | cout << "Red Volvo Speed: " << redVolvo.getSpeed() << " miles/hour" 8 | << ", Direction: " << redVolvo.getDirection() << " degrees" << endl; 9 | 10 | redVolvo.accelerate(30); 11 | redVolvo.turnRight(30); 12 | cout << "Red Volvo Speed: " << redVolvo.getSpeed() << " miles/hour" 13 | << ", Direction: " << redVolvo.getDirection() << " degrees" << endl; 14 | 15 | redVolvo.decelerate(10); 16 | redVolvo.turnLeft(10); 17 | cout << "Red Volvo Speed: " << redVolvo.getSpeed() << " miles/hour" 18 | << ", Direction: " << redVolvo.getDirection() << " degrees" << endl; 19 | 20 | const Car blueFiat(100, 90); 21 | cout << "Blue Fiat Speed: " << blueFiat.getSpeed() << " miles/hour" 22 | << ", Direction: " << blueFiat.getDirection() << " degrees" 23 | << endl; 24 | } 25 | -------------------------------------------------------------------------------- /Chapter01/Car/Release/Car.Build.CppClean.log: -------------------------------------------------------------------------------- 1 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\car\release\vc120.pdb 2 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\car\release\main.obj 3 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\car\release\car.obj 4 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\release\car.exe 5 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\release\car.pdb 6 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\car\release\car.tlog\cl.command.1.tlog 7 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\car\release\car.tlog\cl.read.1.tlog 8 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\car\release\car.tlog\cl.write.1.tlog 9 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\car\release\car.tlog\link.command.1.tlog 10 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\car\release\car.tlog\link.read.1.tlog 11 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\car\release\car.tlog\link.write.1.tlog 12 | -------------------------------------------------------------------------------- /Chapter01/Car/Release/Car.log: -------------------------------------------------------------------------------- 1 | Build started 2017-06-29 17:45:31. 2 | 3 | Build succeeded. 4 | 5 | Time Elapsed 00:00:00.03 6 | -------------------------------------------------------------------------------- /Chapter01/Dice/Debug/Dice.log: -------------------------------------------------------------------------------- 1 | Build started 2017-12-01 15:26:49. 2 | 3 | Build succeeded. 4 | 5 | Time Elapsed 00:00:00.30 6 | -------------------------------------------------------------------------------- /Chapter01/Dice/Dice.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /Chapter01/Dice/Main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | void main() { 7 | srand((int) time(nullptr)); 8 | int dice = (rand() % 6 ) + 1; 9 | cout << "Dice: " << dice << endl; 10 | } -------------------------------------------------------------------------------- /Chapter01/Person/Debug/Person.log: -------------------------------------------------------------------------------- 1 | Build started 2017-12-01 15:26:54. 2 | 3 | Build succeeded. 4 | 5 | Time Elapsed 00:00:00.01 6 | -------------------------------------------------------------------------------- /Chapter01/Person/Employee.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | #include "Person.h" 6 | #include "Employee.h" 7 | 8 | Employee::Employee(string name, string company) 9 | :Person(name), 10 | m_company(company) { 11 | // Empty. 12 | } 13 | 14 | void Employee::print() { 15 | Person::print(); 16 | cout << "Company " << m_company << endl; 17 | } -------------------------------------------------------------------------------- /Chapter01/Person/Employee.h: -------------------------------------------------------------------------------- 1 | class Employee : public Person { 2 | public: 3 | Employee(string name, string company); 4 | void print(); 5 | 6 | private: 7 | string m_company; 8 | }; -------------------------------------------------------------------------------- /Chapter01/Person/Main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | #include "Person.h" 6 | #include "Student.h" 7 | #include "Employee.h" 8 | 9 | void main() { 10 | Person person("Monica"); 11 | person.print(); 12 | cout << endl; 13 | 14 | Student student("Demi", "Harvard"); 15 | student.print(); 16 | cout << endl; 17 | 18 | Employee employee("Charles", "Microsoft"); 19 | employee.print(); 20 | cout << endl; 21 | 22 | Person* personPtr; 23 | personPtr = &person; 24 | personPtr->print(); 25 | cout << endl; 26 | 27 | personPtr = &student; 28 | personPtr->print(); 29 | cout << endl; 30 | 31 | personPtr = &employee; 32 | personPtr->print(); 33 | } -------------------------------------------------------------------------------- /Chapter01/Person/Person.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | #include "Person.h" 6 | 7 | Person::Person(string name) 8 | :m_name(name) { 9 | // Empty. 10 | } 11 | 12 | void Person::print() { 13 | cout << "Person " << m_name << endl; 14 | } -------------------------------------------------------------------------------- /Chapter01/Person/Person.h: -------------------------------------------------------------------------------- 1 | class Person { 2 | public: 3 | Person(string name); 4 | virtual void print(); 5 | 6 | private: 7 | string m_name; 8 | }; -------------------------------------------------------------------------------- /Chapter01/Person/Person.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | 14 | 15 | Header Files 16 | 17 | 18 | Header Files 19 | 20 | 21 | Header Files 22 | 23 | 24 | 25 | 26 | Source Files 27 | 28 | 29 | Source Files 30 | 31 | 32 | Source Files 33 | 34 | 35 | Source Files 36 | 37 | 38 | -------------------------------------------------------------------------------- /Chapter01/Person/Release/Person.Build.CppClean.log: -------------------------------------------------------------------------------- 1 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\person\release\vc120.pdb 2 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\person\release\student.obj 3 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\person\release\person.obj 4 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\person\release\main.obj 5 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\person\release\employee.obj 6 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\release\person.exe 7 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\release\person.pdb 8 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\person\release\person.tlog\cl.command.1.tlog 9 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\person\release\person.tlog\cl.read.1.tlog 10 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\person\release\person.tlog\cl.write.1.tlog 11 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\person\release\person.tlog\link.command.1.tlog 12 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\person\release\person.tlog\link.read.1.tlog 13 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\person\release\person.tlog\link.write.1.tlog 14 | -------------------------------------------------------------------------------- /Chapter01/Person/Release/Person.log: -------------------------------------------------------------------------------- 1 | Build started 2017-06-29 17:45:31. 2 | 3 | Build succeeded. 4 | 5 | Time Elapsed 00:00:00 6 | -------------------------------------------------------------------------------- /Chapter01/Person/Student.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | #include "Person.h" 6 | #include "Student.h" 7 | 8 | Student::Student(string name, string university) 9 | :Person(name), 10 | m_university(university) { 11 | // Empty. 12 | } 13 | 14 | void Student::print() { 15 | Person::print(); 16 | cout << "University " << m_university << endl; 17 | } -------------------------------------------------------------------------------- /Chapter01/Person/Student.h: -------------------------------------------------------------------------------- 1 | class Student : public Person { 2 | public: 3 | Student(string name, string university); 4 | void print(); 5 | 6 | private: 7 | string m_university; 8 | }; -------------------------------------------------------------------------------- /Chapter01/Queue/Cell.cpp: -------------------------------------------------------------------------------- 1 | #include "Cell.h" 2 | 3 | Cell::Cell(int value, Cell* next) 4 | :m_value(value), 5 | m_next(next) { 6 | // Empty. 7 | } -------------------------------------------------------------------------------- /Chapter01/Queue/Cell.h: -------------------------------------------------------------------------------- 1 | class Cell { 2 | public: 3 | Cell(int value, Cell* next); 4 | int value() const {return m_value;} 5 | Cell* getNext() const { return m_next; } 6 | void setNext(Cell* next) { m_next = next; } 7 | 8 | private: 9 | int m_value; 10 | Cell* m_next; 11 | }; 12 | -------------------------------------------------------------------------------- /Chapter01/Queue/Debug/Queue.log: -------------------------------------------------------------------------------- 1 | Build started 2017-12-01 15:26:51. 2 | 3 | Build succeeded. 4 | 5 | Time Elapsed 00:00:00.01 6 | -------------------------------------------------------------------------------- /Chapter01/Queue/Main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | #include "Cell.h" 7 | #include "Queue.h" 8 | 9 | void main() { 10 | Queue q; 11 | q.enter(1); 12 | q.enter(2); 13 | q.enter(3); 14 | cout << "first " << q.first() << ", size " << q.size() 15 | << ", empty " << (q.empty() ? "true" : "false") << endl; 16 | 17 | q.remove(); 18 | q.remove(); 19 | q.enter(4); 20 | cout << "first " << q.first() << ", size " << q.size() 21 | << ", empty " << (q.empty() ? "true" : "false") << endl; 22 | } -------------------------------------------------------------------------------- /Chapter01/Queue/Queue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #include "Cell.h" 5 | #include "Queue.h" 6 | 7 | Queue::Queue() 8 | :m_firstCellPtr(nullptr), 9 | m_lastCellPtr(nullptr), 10 | m_size(0) { 11 | // Empty. 12 | } 13 | 14 | void Queue::enter(int value) { 15 | Cell *newCellPtr = new Cell(value, nullptr); 16 | assert(newCellPtr != nullptr); 17 | 18 | if (empty()) { 19 | m_firstCellPtr = m_lastCellPtr = newCellPtr; 20 | } 21 | else { 22 | m_lastCellPtr->setNext(newCellPtr); 23 | m_lastCellPtr = newCellPtr; 24 | } 25 | 26 | ++m_size; 27 | } 28 | 29 | int Queue::first() { 30 | assert(!empty()); 31 | return m_firstCellPtr->value(); 32 | } 33 | 34 | void Queue::remove() { 35 | assert(!empty()); 36 | Cell* deleteCellPtr = m_firstCellPtr; 37 | m_firstCellPtr = m_firstCellPtr->getNext(); 38 | delete deleteCellPtr; 39 | --m_size; 40 | } 41 | 42 | int Queue::size() const { 43 | return m_size; 44 | } 45 | 46 | bool Queue::empty() const { 47 | return (m_firstCellPtr == nullptr); 48 | } -------------------------------------------------------------------------------- /Chapter01/Queue/Queue.h: -------------------------------------------------------------------------------- 1 | class Queue { 2 | public: 3 | Queue(); 4 | void enter(int value); 5 | int first(); 6 | void remove(); 7 | int size() const; 8 | bool empty() const; 9 | 10 | private: 11 | Cell *m_firstCellPtr, *m_lastCellPtr; 12 | int m_size; 13 | }; -------------------------------------------------------------------------------- /Chapter01/Queue/Queue.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | Source Files 23 | 24 | 25 | Source Files 26 | 27 | 28 | 29 | 30 | Header Files 31 | 32 | 33 | Header Files 34 | 35 | 36 | -------------------------------------------------------------------------------- /Chapter01/Queue/Release/Queue.Build.CppClean.log: -------------------------------------------------------------------------------- 1 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\queue\release\vc120.pdb 2 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\queue\release\queue.obj 3 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\queue\release\main.obj 4 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\queue\release\cell.obj 5 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\release\queue.exe 6 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\release\queue.pdb 7 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\queue\release\queue.tlog\cl.command.1.tlog 8 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\queue\release\queue.tlog\cl.read.1.tlog 9 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\queue\release\queue.tlog\cl.write.1.tlog 10 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\queue\release\queue.tlog\link.command.1.tlog 11 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\queue\release\queue.tlog\link.read.1.tlog 12 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\queue\release\queue.tlog\link.write.1.tlog 13 | -------------------------------------------------------------------------------- /Chapter01/Queue/Release/Queue.log: -------------------------------------------------------------------------------- 1 | Build started 2017-06-29 17:45:28. 2 | 3 | Build succeeded. 4 | 5 | Time Elapsed 00:00:00 6 | -------------------------------------------------------------------------------- /Chapter01/Stack/Cell.cpp: -------------------------------------------------------------------------------- 1 | #include "Cell.h" 2 | 3 | Cell::Cell(int value, Cell* next) 4 | :m_value(value), 5 | m_next(next) { 6 | // Empty. 7 | } -------------------------------------------------------------------------------- /Chapter01/Stack/Cell.h: -------------------------------------------------------------------------------- 1 | class Cell { 2 | public: 3 | Cell(int value, Cell* next); 4 | int getValue() const {return m_value;} 5 | Cell* getNext() const {return m_next;} 6 | 7 | private: 8 | int m_value; 9 | Cell* m_next; 10 | }; -------------------------------------------------------------------------------- /Chapter01/Stack/Debug/Stack.log: -------------------------------------------------------------------------------- 1 | Build started 2017-12-01 15:26:51. 2 | 3 | Build succeeded. 4 | 5 | Time Elapsed 00:00:00.01 6 | -------------------------------------------------------------------------------- /Chapter01/Stack/Main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | #include "Cell.h" 6 | #include "Stack.h" 7 | 8 | void main() { 9 | Stack s; 10 | s.push(1); 11 | s.push(2); 12 | s.push(3); 13 | cout << "top " << s.top() << ", size " << s.size() 14 | << ", empty " << (s.empty() ? "true" : "false") << endl; 15 | 16 | s.pop(); 17 | s.pop(); 18 | s.push(4); 19 | cout << "top " << s.top() << ", size " << s.size() 20 | << ", empty " << (s.empty() ? "true" : "false") << endl; 21 | } -------------------------------------------------------------------------------- /Chapter01/Stack/Release/Stack.Build.CppClean.log: -------------------------------------------------------------------------------- 1 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\stack\release\vc120.pdb 2 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\stack\release\stack.obj 3 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\stack\release\main.obj 4 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\stack\release\cell.obj 5 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\release\stack.exe 6 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\release\stack.pdb 7 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\stack\release\stack.tlog\cl.command.1.tlog 8 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\stack\release\stack.tlog\cl.read.1.tlog 9 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\stack\release\stack.tlog\cl.write.1.tlog 10 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\stack\release\stack.tlog\link.command.1.tlog 11 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\stack\release\stack.tlog\link.read.1.tlog 12 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\stack\release\stack.tlog\link.write.1.tlog 13 | -------------------------------------------------------------------------------- /Chapter01/Stack/Release/Stack.log: -------------------------------------------------------------------------------- 1 | Build started 2017-06-29 17:45:28. 2 | 3 | Build succeeded. 4 | 5 | Time Elapsed 00:00:00.01 6 | -------------------------------------------------------------------------------- /Chapter01/Stack/Stack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #include "Cell.h" 5 | #include "Stack.h" 6 | Stack::Stack() 7 | :m_firstCellPtr(nullptr), 8 | m_size(0) { 9 | // Empty. 10 | } 11 | 12 | void Stack::push(int value) { 13 | m_firstCellPtr = new Cell(value, m_firstCellPtr); 14 | assert(m_firstCellPtr != nullptr); 15 | ++m_size; 16 | } 17 | 18 | int Stack::top() { 19 | assert(!empty()); 20 | return m_firstCellPtr->getValue(); 21 | } 22 | 23 | void Stack::pop() { 24 | assert(!empty()); 25 | Cell* deleteCellPtr = m_firstCellPtr; 26 | m_firstCellPtr = m_firstCellPtr->getNext(); 27 | delete deleteCellPtr; 28 | --m_size; 29 | } 30 | 31 | int Stack::size() const { 32 | return m_size; 33 | } 34 | 35 | bool Stack::empty() const { 36 | return (m_firstCellPtr == nullptr); 37 | } -------------------------------------------------------------------------------- /Chapter01/Stack/Stack.h: -------------------------------------------------------------------------------- 1 | class Stack { 2 | public: 3 | Stack(); 4 | void push(int value); 5 | int top(); 6 | void pop(); 7 | int size() const; 8 | bool empty() const; 9 | 10 | private: 11 | Cell* m_firstCellPtr; 12 | int m_size; 13 | }; 14 | -------------------------------------------------------------------------------- /Chapter01/Stack/Stack.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | Source Files 23 | 24 | 25 | Source Files 26 | 27 | 28 | 29 | 30 | Header Files 31 | 32 | 33 | Header Files 34 | 35 | 36 | -------------------------------------------------------------------------------- /Chapter02/ListAdvanced/Debug/ListAdvanced.log: -------------------------------------------------------------------------------- 1 | Build started 2017-12-01 15:26:51. 2 | 3 | Build succeeded. 4 | 5 | Time Elapsed 00:00:00.02 6 | -------------------------------------------------------------------------------- /Chapter02/ListAdvanced/ListAdvanced.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | 23 | 24 | Header Files 25 | 26 | 27 | -------------------------------------------------------------------------------- /Chapter02/ListAdvanced/Main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | #include "List.h" 6 | 7 | void main() { 8 | LinkedList list; 9 | cin >> list; 10 | cout << list << endl; 11 | 12 | for (double value : list) { 13 | cout << value << " "; 14 | } 15 | cout << endl; 16 | 17 | for (LinkedList::Iterator iterator = list.begin(); 18 | iterator != list.end(); ++iterator) { 19 | cout << *iterator << " "; 20 | } 21 | cout << endl; 22 | 23 | for (LinkedList::ReverseIterator iterator = 24 | list.rbegin(); iterator != list.rend(); ++iterator) { 25 | cout << *iterator << " "; 26 | } 27 | cout << endl; 28 | } -------------------------------------------------------------------------------- /Chapter02/ListAdvanced/Release/ListAdvanced.Build.CppClean.log: -------------------------------------------------------------------------------- 1 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\listadvanced\release\vc120.pdb 2 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\listadvanced\release\main.obj 3 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\release\listadvanced.exe 4 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\release\listadvanced.pdb 5 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\listadvanced\release\listadvanced.tlog\cl.command.1.tlog 6 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\listadvanced\release\listadvanced.tlog\cl.read.1.tlog 7 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\listadvanced\release\listadvanced.tlog\cl.write.1.tlog 8 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\listadvanced\release\listadvanced.tlog\link.command.1.tlog 9 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\listadvanced\release\listadvanced.tlog\link.read.1.tlog 10 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\listadvanced\release\listadvanced.tlog\link.write.1.tlog 11 | -------------------------------------------------------------------------------- /Chapter02/ListAdvanced/Release/ListAdvanced.log: -------------------------------------------------------------------------------- 1 | Build started 2017-06-29 17:45:28. 2 | 3 | Build succeeded. 4 | 5 | Time Elapsed 00:00:00.01 6 | -------------------------------------------------------------------------------- /Chapter02/ListBasic/Cell.cpp: -------------------------------------------------------------------------------- 1 | #include "Cell.h" 2 | 3 | Cell::Cell(double value, Cell* previous, Cell* next) 4 | :m_value(value), 5 | m_previous(previous), 6 | m_next(next) { 7 | // Empty. 8 | } -------------------------------------------------------------------------------- /Chapter02/ListBasic/Cell.h: -------------------------------------------------------------------------------- 1 | class Cell { 2 | private: 3 | Cell(double value, Cell* previous, Cell* next); 4 | friend class LinkedList; 5 | 6 | public: 7 | double getValue() const { return m_value; } 8 | void setValue(double value) { m_value = value; } 9 | 10 | Cell* getPrevious() const { return m_previous; } 11 | void setPrevious(Cell* previous) { m_previous = previous; } 12 | 13 | Cell* getNext() const { return m_next; } 14 | void setNext(Cell* getNext) { m_next = getNext; } 15 | 16 | private: 17 | double m_value; 18 | Cell* m_previous; 19 | Cell* m_next; 20 | }; -------------------------------------------------------------------------------- /Chapter02/ListBasic/Debug/ListBasic.log: -------------------------------------------------------------------------------- 1 | Build started 2017-12-01 15:26:51. 2 | 3 | Build succeeded. 4 | 5 | Time Elapsed 00:00:00.01 6 | -------------------------------------------------------------------------------- /Chapter02/ListBasic/Iterator.cpp: -------------------------------------------------------------------------------- 1 | #include "Cell.h" 2 | #include "Iterator.h" 3 | 4 | Iterator::Iterator(Cell* cellPtr) 5 | :m_cellPtr(cellPtr) { 6 | // Empty. 7 | } 8 | 9 | Iterator::Iterator() 10 | :m_cellPtr(nullptr) { 11 | // Empty. 12 | } 13 | 14 | Iterator::Iterator(const Iterator& iterator) 15 | :m_cellPtr(iterator.m_cellPtr) { 16 | // Empty. 17 | } -------------------------------------------------------------------------------- /Chapter02/ListBasic/Iterator.h: -------------------------------------------------------------------------------- 1 | class Iterator { 2 | private: 3 | Iterator(Cell* cellPtr); 4 | 5 | public: 6 | Iterator(); 7 | Iterator(const Iterator& iterator); 8 | 9 | double getValue() { return m_cellPtr->getValue(); } 10 | void setValue(double value) { m_cellPtr->setValue(value); } 11 | 12 | bool hasNext() const { return (m_cellPtr != nullptr); } 13 | void next() { m_cellPtr = m_cellPtr->getNext(); } 14 | 15 | bool hasPrevious() const { return (m_cellPtr != nullptr); } 16 | void previous() { m_cellPtr = m_cellPtr->getPrevious(); } 17 | 18 | friend class LinkedList; 19 | 20 | private: 21 | Cell* m_cellPtr; 22 | }; 23 | -------------------------------------------------------------------------------- /Chapter02/ListBasic/List.h: -------------------------------------------------------------------------------- 1 | class LinkedList { 2 | public: 3 | LinkedList(); 4 | LinkedList(const LinkedList& list); 5 | void assign(const LinkedList& list); 6 | ~LinkedList(); 7 | 8 | int size() const {return m_size;} 9 | bool empty() const {return (m_size == 0);} 10 | bool find(double value, Iterator& findIterator); 11 | 12 | bool equal(const LinkedList& list) const; 13 | bool notEqual(const LinkedList& list) const; 14 | 15 | void add(double value); 16 | void add(const LinkedList& list); 17 | 18 | void insert(const Iterator& insertPosition, double value); 19 | void insert(const Iterator& insertPosition, 20 | const LinkedList& list); 21 | 22 | void erase(const Iterator& erasePosition); 23 | void remove(const Iterator& firstPosition, 24 | const Iterator& lastPosition = Iterator(nullptr)); 25 | void clear(); 26 | 27 | Iterator first() const { return Iterator(m_firstCellPtr); } 28 | Iterator last() const { return Iterator(m_lastCellPtr); } 29 | 30 | void read(istream& inStream); 31 | void write(ostream& outStream); 32 | 33 | private: 34 | int m_size; 35 | Cell* m_firstCellPtr; 36 | Cell* m_lastCellPtr; 37 | }; 38 | -------------------------------------------------------------------------------- /Chapter02/ListBasic/ListBasic.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | Source Files 23 | 24 | 25 | Source Files 26 | 27 | 28 | Source Files 29 | 30 | 31 | 32 | 33 | Header Files 34 | 35 | 36 | Header Files 37 | 38 | 39 | Header Files 40 | 41 | 42 | -------------------------------------------------------------------------------- /Chapter02/ListBasic/Main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #include "Cell.h" 5 | #include "Iterator.h" 6 | #include "List.h" 7 | 8 | void main() { 9 | LinkedList list; 10 | list.add(1); 11 | list.add(2); 12 | list.add(3); 13 | list.add(4); 14 | list.add(5); 15 | list.write(cout); 16 | cout << endl; 17 | 18 | { Iterator iterator = list.first(); 19 | while (iterator.hasNext()) { 20 | cout << iterator.getValue() << " "; 21 | iterator.next(); 22 | } 23 | cout << endl; 24 | } 25 | 26 | { Iterator iterator = list.last(); 27 | while (iterator.hasPrevious()) { 28 | cout << iterator.getValue() << " "; 29 | iterator.previous(); 30 | } 31 | cout << endl; 32 | } 33 | } -------------------------------------------------------------------------------- /Chapter02/ListBasic/Release/ListBasic.Build.CppClean.log: -------------------------------------------------------------------------------- 1 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\listbasic\release\vc120.pdb 2 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\listbasic\release\main.obj 3 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\listbasic\release\list.obj 4 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\listbasic\release\iterator.obj 5 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\listbasic\release\cell.obj 6 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\release\listbasic.exe 7 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\release\listbasic.pdb 8 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\listbasic\release\listbasic.tlog\cl.command.1.tlog 9 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\listbasic\release\listbasic.tlog\cl.read.1.tlog 10 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\listbasic\release\listbasic.tlog\cl.write.1.tlog 11 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\listbasic\release\listbasic.tlog\link.command.1.tlog 12 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\listbasic\release\listbasic.tlog\link.read.1.tlog 13 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\listbasic\release\listbasic.tlog\link.write.1.tlog 14 | -------------------------------------------------------------------------------- /Chapter02/ListBasic/Release/ListBasic.log: -------------------------------------------------------------------------------- 1 | Build started 2017-06-29 17:45:28. 2 | 3 | Build succeeded. 4 | 5 | Time Elapsed 00:00:00.01 6 | -------------------------------------------------------------------------------- /Chapter02/SearchAndSortAdvanced/Debug/SearchAndSortAdvanced.log: -------------------------------------------------------------------------------- 1 | Build started 2017-12-01 15:26:52. 2 | 3 | Build succeeded. 4 | 5 | Time Elapsed 00:00:00.06 6 | -------------------------------------------------------------------------------- /Chapter02/SearchAndSortAdvanced/Release/ArrayList.obj: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/CPP17-By-Example/543f6cc865b8fe5fb83dd5886c73e5dff45f377f/Chapter02/SearchAndSortAdvanced/Release/ArrayList.obj -------------------------------------------------------------------------------- /Chapter02/SearchAndSortAdvanced/Release/Main.obj: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/CPP17-By-Example/543f6cc865b8fe5fb83dd5886c73e5dff45f377f/Chapter02/SearchAndSortAdvanced/Release/Main.obj -------------------------------------------------------------------------------- /Chapter02/SearchAndSortAdvanced/Release/SearchAn.18411F9B.tlog/CL.read.1.tlog: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/CPP17-By-Example/543f6cc865b8fe5fb83dd5886c73e5dff45f377f/Chapter02/SearchAndSortAdvanced/Release/SearchAn.18411F9B.tlog/CL.read.1.tlog -------------------------------------------------------------------------------- /Chapter02/SearchAndSortAdvanced/Release/SearchAn.18411F9B.tlog/CL.write.1.tlog: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/CPP17-By-Example/543f6cc865b8fe5fb83dd5886c73e5dff45f377f/Chapter02/SearchAndSortAdvanced/Release/SearchAn.18411F9B.tlog/CL.write.1.tlog -------------------------------------------------------------------------------- /Chapter02/SearchAndSortAdvanced/Release/SearchAn.18411F9B.tlog/SearchAndSortAdvanced.lastbuildstate: -------------------------------------------------------------------------------- 1 | #TargetFrameworkVersion=v4.0:PlatformToolSet=v120:EnableManagedIncrementalBuild=false:VCToolArchitecture=Native32Bit 2 | Release|Win32|C:\Users\Stefan\Documents\A A_Cpp_By_Example\CppByExample\| 3 | -------------------------------------------------------------------------------- /Chapter02/SearchAndSortAdvanced/Release/SearchAn.18411F9B.tlog/cl.command.1.tlog: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/CPP17-By-Example/543f6cc865b8fe5fb83dd5886c73e5dff45f377f/Chapter02/SearchAndSortAdvanced/Release/SearchAn.18411F9B.tlog/cl.command.1.tlog -------------------------------------------------------------------------------- /Chapter02/SearchAndSortAdvanced/Release/SearchAn.18411F9B.tlog/link.command.1.tlog: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/CPP17-By-Example/543f6cc865b8fe5fb83dd5886c73e5dff45f377f/Chapter02/SearchAndSortAdvanced/Release/SearchAn.18411F9B.tlog/link.command.1.tlog -------------------------------------------------------------------------------- /Chapter02/SearchAndSortAdvanced/Release/SearchAn.18411F9B.tlog/link.read.1.tlog: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/CPP17-By-Example/543f6cc865b8fe5fb83dd5886c73e5dff45f377f/Chapter02/SearchAndSortAdvanced/Release/SearchAn.18411F9B.tlog/link.read.1.tlog -------------------------------------------------------------------------------- /Chapter02/SearchAndSortAdvanced/Release/SearchAn.18411F9B.tlog/link.write.1.tlog: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/CPP17-By-Example/543f6cc865b8fe5fb83dd5886c73e5dff45f377f/Chapter02/SearchAndSortAdvanced/Release/SearchAn.18411F9B.tlog/link.write.1.tlog -------------------------------------------------------------------------------- /Chapter02/SearchAndSortAdvanced/Release/SearchAndSortAdvanced.log: -------------------------------------------------------------------------------- 1 | Build started 2017-06-29 17:29:56. 2 | 1>Project "C:\Users\Stefan\Documents\A A_Cpp_By_Example\CppByExample\SearchAndSortAdvanced\SearchAndSortAdvanced.vcxproj" on node 2 (Build target(s)). 3 | 1>ClCompile: 4 | C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\CL.exe /c /Zi /nologo /W3 /WX- /O2 /Oi /Oy- /GL /D WIN32 /D NDEBUG /D _CONSOLE /D _LIB /D _UNICODE /D UNICODE /Gm- /EHsc /MD /GS /Gy /fp:precise /Zc:wchar_t /Zc:forScope /Fo"Release\\" /Fd"Release\vc120.pdb" /Gd /TP /analyze- /errorReport:prompt ArrayList.cpp Main.cpp 5 | ArrayList.cpp 6 | Main.cpp 7 | Link: 8 | C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\link.exe /ERRORREPORT:PROMPT /OUT:"C:\Users\Stefan\Documents\A A_Cpp_By_Example\CppByExample\Release\SearchAndSortAdvanced.exe" /INCREMENTAL:NO /NOLOGO kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /MANIFEST /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /manifest:embed /DEBUG /PDB:"C:\Users\Stefan\Documents\A A_Cpp_By_Example\CppByExample\Release\SearchAndSortAdvanced.pdb" /SUBSYSTEM:CONSOLE /OPT:REF /OPT:ICF /LTCG /TLBID:1 /DYNAMICBASE /NXCOMPAT /IMPLIB:"C:\Users\Stefan\Documents\A A_Cpp_By_Example\CppByExample\Release\SearchAndSortAdvanced.lib" /MACHINE:X86 /SAFESEH Release\ArrayList.obj 9 | Release\Main.obj 10 | Generating code 11 | Finished generating code 12 | SearchAndSortAdvanced.vcxproj -> C:\Users\Stefan\Documents\A A_Cpp_By_Example\CppByExample\Release\SearchAndSortAdvanced.exe 13 | 1>Done Building Project "C:\Users\Stefan\Documents\A A_Cpp_By_Example\CppByExample\SearchAndSortAdvanced\SearchAndSortAdvanced.vcxproj" (Build target(s)). 14 | 15 | Build succeeded. 16 | 17 | Time Elapsed 00:00:01.65 18 | -------------------------------------------------------------------------------- /Chapter02/SearchAndSortAdvanced/Release/vc120.pdb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/CPP17-By-Example/543f6cc865b8fe5fb83dd5886c73e5dff45f377f/Chapter02/SearchAndSortAdvanced/Release/vc120.pdb -------------------------------------------------------------------------------- /Chapter02/SearchAndSortAdvanced/Search.h: -------------------------------------------------------------------------------- 1 | template 2 | int linarySearch(const ValueType& value, const ListType& list) { 3 | int index = 0; 4 | 5 | for (double listValue : list) { 6 | if (listValue == value) { 7 | return index; 8 | } 9 | 10 | ++index; 11 | } 12 | 13 | return -1; 14 | } 15 | 16 | template 17 | int binarySearch(const ValueType& value, const ListType& list) { 18 | ListType::Iterator* positionBuffer = 19 | new ListType::Iterator[list.size()]; 20 | 21 | int index = 0; 22 | for (ListType::Iterator position = list.begin(); 23 | position != list.end(); ++position) { 24 | positionBuffer[index++] = position; 25 | } 26 | 27 | int minIndex = 0, maxIndex = list.size() - 1; 28 | 29 | while (minIndex <= maxIndex) { 30 | int middleIndex = (maxIndex + minIndex) / 2; 31 | ListType::Iterator iterator = positionBuffer[middleIndex]; 32 | const ValueType& middleValue = *iterator; 33 | 34 | if (value == middleValue) { 35 | return middleIndex; 36 | } 37 | else if (value < middleValue) { 38 | maxIndex = middleIndex - 1; 39 | } 40 | else { 41 | minIndex = middleIndex + 1; 42 | } 43 | } 44 | 45 | return -1; 46 | } 47 | 48 | /*template 49 | int linarySearch(const ValueType& value, const ListType& list) { 50 | for (int index = 0; index < list.size(); ++index) { 51 | if (list[index] == value) { 52 | return index; 53 | } 54 | } 55 | 56 | return -1; 57 | } 58 | 59 | template 60 | int binarySearch(const ValueType& value, const ListType& list) { 61 | int minIndex = 0, maxIndex = list.size() - 1; 62 | 63 | while (minIndex <= maxIndex) { 64 | int middleIndex = (maxIndex + minIndex) / 2; 65 | const ValueType& middleValue = list[middleIndex]; 66 | 67 | if (value == middleValue) { 68 | return middleIndex; 69 | } 70 | else if (value < middleValue) { 71 | maxIndex = middleIndex - 1; 72 | } 73 | else { 74 | minIndex = middleIndex + 1; 75 | } 76 | } 77 | 78 | return -1; 79 | }*/ -------------------------------------------------------------------------------- /Chapter02/SearchAndSortAdvanced/SearchAndSortAdvanced.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | Source Files 23 | 24 | 25 | 26 | 27 | Header Files 28 | 29 | 30 | Header Files 31 | 32 | 33 | Header Files 34 | 35 | 36 | Header Files 37 | 38 | 39 | -------------------------------------------------------------------------------- /Chapter02/SearchAndSortBasic/Debug/SearchAndSortBasic.log: -------------------------------------------------------------------------------- 1 | Build started 2017-12-01 15:26:53. 2 | 3 | Build succeeded. 4 | 5 | Time Elapsed 00:00:00.02 6 | -------------------------------------------------------------------------------- /Chapter02/SearchAndSortBasic/Main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | #include "..\\ListBasic\\Cell.h" 7 | #include "..\\ListBasic\\Iterator.h" 8 | #include "..\\ListBasic\\List.h" 9 | 10 | #include "Search.h" 11 | #include "Sort.h" 12 | 13 | void main() { 14 | cout << "LinkedList" << endl; 15 | 16 | LinkedList list; 17 | list.add(9); 18 | list.add(7); 19 | list.add(5); 20 | list.add(3); 21 | list.add(1); 22 | 23 | list.write(cout); 24 | cout << endl; 25 | 26 | Iterator iterator = list.first(); 27 | while (iterator.hasNext()) { 28 | cout << "<" << iterator.getValue() << "," 29 | << linarySearch(iterator.getValue(), list) << "> "; 30 | iterator.next(); 31 | } 32 | 33 | cout << "<0," << linarySearch(0, list) << "> "; 34 | cout << "<6," << linarySearch(6, list) << "> "; 35 | cout << "<10," << linarySearch(10, list) << ">" 36 | << endl; 37 | 38 | cout << "Bubble Sort "; 39 | bubbleSort(list); 40 | list.write(cout); 41 | cout << endl; 42 | 43 | cout << "Select Sort "; 44 | selectSort(list); 45 | list.write(cout); 46 | cout << endl; 47 | 48 | cout << "Insert Sort "; 49 | insertSort(list); 50 | list.write(cout); 51 | cout << endl; 52 | } -------------------------------------------------------------------------------- /Chapter02/SearchAndSortBasic/Search.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #include "..\\ListBasic\\Cell.h" 5 | #include "..\\ListBasic\\Iterator.h" 6 | #include "..\\ListBasic\\List.h" 7 | #include "Search.h" 8 | 9 | int linarySearch(double value, const LinkedList& list) { 10 | int index = 0; 11 | Iterator iterator = list.first(); 12 | 13 | while (iterator.hasNext()) { 14 | if (iterator.getValue() == value) { 15 | return index; 16 | } 17 | 18 | ++index; 19 | iterator.next(); 20 | } 21 | 22 | return -1; 23 | } -------------------------------------------------------------------------------- /Chapter02/SearchAndSortBasic/Search.h: -------------------------------------------------------------------------------- 1 | int linarySearch(double value, const LinkedList& list); 2 | 3 | /*template 4 | int linarySearch(const ValueType& value, const ListType& list) { 5 | int index = 0; 6 | 7 | for (Iterator iterator = list.first(); iterator.hasNext(); iterator.next()) { 8 | if (iterator.getValue() == value) { 9 | return index; 10 | } 11 | 12 | ++index; 13 | } 14 | 15 | return -1; 16 | }*/ -------------------------------------------------------------------------------- /Chapter02/SearchAndSortBasic/SearchAndSortBasic.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | Source Files 23 | 24 | 25 | Source Files 26 | 27 | 28 | Source Files 29 | 30 | 31 | Source Files 32 | 33 | 34 | Source Files 35 | 36 | 37 | Source Files 38 | 39 | 40 | 41 | 42 | Header Files 43 | 44 | 45 | Header Files 46 | 47 | 48 | Header Files 49 | 50 | 51 | Header Files 52 | 53 | 54 | Header Files 55 | 56 | 57 | Header Files 58 | 59 | 60 | -------------------------------------------------------------------------------- /Chapter02/SetAdvanced/Debug/SetAdvanced.log: -------------------------------------------------------------------------------- 1 | Build started 2017-12-01 15:26:52. 2 | 3 | Build succeeded. 4 | 5 | Time Elapsed 00:00:00.01 6 | -------------------------------------------------------------------------------- /Chapter02/SetAdvanced/Main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | #include "..\\ListAdvanced\\List.h" 6 | #include "Set.h" 7 | 8 | void main() { 9 | Set s, t; 10 | cin >> s >> t; 11 | 12 | cout << endl << "s: " << s << endl; 13 | cout << "t: " << t << endl; 14 | 15 | cout << endl << "s: "; 16 | for (double value : s) { 17 | cout << value << " "; 18 | } 19 | 20 | cout << endl << "t: "; 21 | for (Set::Iterator iterator = t.begin(); 22 | iterator != t.end(); ++iterator) { 23 | cout << *iterator << " "; 24 | } 25 | 26 | cout << endl << endl << "union: " << (s + t) << endl; 27 | cout << "intersection: " << (s *t) << endl; 28 | cout << "difference: " << (s - t) << endl << endl; 29 | } -------------------------------------------------------------------------------- /Chapter02/SetAdvanced/Release/SetAdvanced.Build.CppClean.log: -------------------------------------------------------------------------------- 1 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\setadvanced\release\vc120.pdb 2 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\setadvanced\release\main.obj 3 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\release\setadvanced.exe 4 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\release\setadvanced.pdb 5 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\setadvanced\release\setadvanced.tlog\cl.command.1.tlog 6 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\setadvanced\release\setadvanced.tlog\cl.read.1.tlog 7 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\setadvanced\release\setadvanced.tlog\cl.write.1.tlog 8 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\setadvanced\release\setadvanced.tlog\link.command.1.tlog 9 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\setadvanced\release\setadvanced.tlog\link.read.1.tlog 10 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\setadvanced\release\setadvanced.tlog\link.write.1.tlog 11 | -------------------------------------------------------------------------------- /Chapter02/SetAdvanced/Release/SetAdvanced.log: -------------------------------------------------------------------------------- 1 | Build started 2017-06-29 17:45:29. 2 | 3 | Build succeeded. 4 | 5 | Time Elapsed 00:00:00 6 | -------------------------------------------------------------------------------- /Chapter02/SetAdvanced/SetAdvanced.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | 23 | 24 | Header Files 25 | 26 | 27 | Header Files 28 | 29 | 30 | -------------------------------------------------------------------------------- /Chapter02/SetBasic/Debug/SetBasic.log: -------------------------------------------------------------------------------- 1 | Build started 2017-12-01 15:26:52. 2 | 3 | Build succeeded. 4 | 5 | Time Elapsed 00:00:00.03 6 | -------------------------------------------------------------------------------- /Chapter02/SetBasic/Main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | #include "..\\ListBasic\\Cell.h" 6 | #include "..\\ListBasic\\Iterator.h" 7 | #include "..\\ListBasic\\List.h" 8 | #include "Set.h" 9 | 10 | void main() { 11 | Set s, t; 12 | s.read(cin); 13 | t.read(cin); 14 | 15 | cout << endl << "s = "; 16 | s.write(cout); 17 | cout << endl; 18 | 19 | cout << endl << "t = "; 20 | t.write(cout); 21 | cout << endl << endl; 22 | 23 | cout << "union: "; 24 | unionSet(s, t).write(cout); 25 | cout << endl; 26 | 27 | cout << "intersection: "; 28 | unionSet(s, t).write(cout); 29 | cout << endl; 30 | 31 | cout << "difference: "; 32 | unionSet(s, t).write(cout); 33 | cout << endl; 34 | } -------------------------------------------------------------------------------- /Chapter02/SetBasic/Release/SetBasic.Build.CppClean.log: -------------------------------------------------------------------------------- 1 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\setbasic\release\vc120.pdb 2 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\setbasic\release\set.obj 3 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\setbasic\release\main.obj 4 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\setbasic\release\list.obj 5 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\setbasic\release\iterator.obj 6 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\setbasic\release\cell.obj 7 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\release\setbasic.exe 8 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\release\setbasic.pdb 9 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\setbasic\release\setbasic.tlog\cl.command.1.tlog 10 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\setbasic\release\setbasic.tlog\cl.read.1.tlog 11 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\setbasic\release\setbasic.tlog\cl.write.1.tlog 12 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\setbasic\release\setbasic.tlog\link.command.1.tlog 13 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\setbasic\release\setbasic.tlog\link.read.1.tlog 14 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\setbasic\release\setbasic.tlog\link.write.1.tlog 15 | -------------------------------------------------------------------------------- /Chapter02/SetBasic/Release/SetBasic.log: -------------------------------------------------------------------------------- 1 | Build started 2017-06-29 17:45:30. 2 | 3 | Build succeeded. 4 | 5 | Time Elapsed 00:00:00.01 6 | -------------------------------------------------------------------------------- /Chapter02/SetBasic/Set.h: -------------------------------------------------------------------------------- 1 | class Set : private LinkedList { 2 | public: 3 | Set(); 4 | Set(double value); 5 | Set(const Set& set); 6 | void assign(const Set& set); 7 | ~Set(); 8 | 9 | bool equal(const Set& set) const; 10 | bool notEqual(const Set& set) const; 11 | 12 | bool exists(double value) const; 13 | bool exists(const Set& set) const; 14 | 15 | bool insert(double value); 16 | bool insert(const Set& set); 17 | 18 | bool remove(double value); 19 | bool remove(const Set& set); 20 | 21 | int size() const { return LinkedList::size(); } 22 | bool empty() const { return LinkedList::empty(); } 23 | Iterator first() const { return LinkedList::first(); } 24 | 25 | friend Set unionSet(const Set& leftSet, const Set& rightSet); 26 | friend Set intersection(const Set& leftSet, 27 | const Set& rightSet); 28 | friend Set difference(const Set& leftSet, 29 | const Set& rightSet); 30 | 31 | void read(istream& inStream); 32 | void write(ostream& outStream); 33 | }; 34 | -------------------------------------------------------------------------------- /Chapter02/SetBasic/SetBasic.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | Source Files 23 | 24 | 25 | Source Files 26 | 27 | 28 | Source Files 29 | 30 | 31 | Source Files 32 | 33 | 34 | 35 | 36 | Header Files 37 | 38 | 39 | Header Files 40 | 41 | 42 | Header Files 43 | 44 | 45 | Header Files 46 | 47 | 48 | -------------------------------------------------------------------------------- /Chapter03/LibraryBasic/Book.h: -------------------------------------------------------------------------------- 1 | class Book { 2 | public: 3 | Book(void); 4 | Book(const string& author, const string& title); 5 | 6 | // Book(const Book& book); 7 | // Book& operator=(const Book& book); 8 | 9 | void read(ifstream& inStream); 10 | void write(ofstream& outStream) const; 11 | 12 | void borrowBook(int customerId); 13 | void returnBook(); 14 | int reserveBook(int customerId); 15 | void unreserveBook(int customerId); 16 | 17 | int bookId(void) const { return m_bookId; } 18 | bool borrowed(void) const { return m_borrowed; } 19 | int customerId(void) const { return m_customerId; } 20 | 21 | list& reservList(void) { return m_reservationList; } 22 | const string& author(void) const { return m_author; } 23 | const string& title(void) const { return m_title; } 24 | 25 | static int MaxBookId; 26 | friend ostream& operator<<(ostream& outStream, 27 | const Book& book); 28 | 29 | private: 30 | bool m_borrowed = false; 31 | int m_bookId, m_customerId; 32 | string m_author, m_title; 33 | list m_reservationList; 34 | }; -------------------------------------------------------------------------------- /Chapter03/LibraryBasic/Customer.h: -------------------------------------------------------------------------------- 1 | class Customer { 2 | public: 3 | Customer(void); 4 | Customer(const string& name, const string& address); 5 | 6 | //Customer(const Customer& customer); 7 | //Customer& operator=(const Customer& customer); 8 | 9 | void read(ifstream& inStream); 10 | void write(ofstream& outStream) const; 11 | 12 | void borrowBook(int bookId); 13 | void returnBook(int bookId); 14 | void reserveBook(int bookId); 15 | void unreserveBook(int bookId); 16 | 17 | bool hasBorrowed(void) const { return !m_loanSet.empty(); } 18 | 19 | const string& name(void) const {return m_name;} 20 | const string& address(void) const { return m_address; } 21 | int id(void) const { return m_customerId; } 22 | 23 | static int MaxCustomerId; 24 | friend ostream& operator<<(ostream& outStream, 25 | const Customer& customer); 26 | 27 | private: 28 | int m_customerId; 29 | string m_name, m_address; 30 | set m_loanSet, m_reservationSet; 31 | }; -------------------------------------------------------------------------------- /Chapter03/LibraryBasic/Debug/LibraryBasic.log: -------------------------------------------------------------------------------- 1 | Build started 2017-12-01 15:26:50. 2 | 3 | Build succeeded. 4 | 5 | Time Elapsed 00:00:00.01 6 | -------------------------------------------------------------------------------- /Chapter03/LibraryBasic/Library.h: -------------------------------------------------------------------------------- 1 | class Library { 2 | public: 3 | Library(); 4 | 5 | private: 6 | static string s_binaryPath; 7 | 8 | bool lookupBook(const string& author, const string& title, 9 | Book* bookPtr = nullptr); 10 | bool lookupCustomer(const string& name, const string& address, 11 | Customer* customerPtr = nullptr); 12 | 13 | void addBook(void); 14 | void deleteBook(void); 15 | void listBooks(void); 16 | void addCustomer(void); 17 | void deleteCustomer(void); 18 | void listCustomers(void); 19 | void borrowBook(void); 20 | void reserveBook(void); 21 | void returnBook(void); 22 | 23 | void load(); 24 | void save(); 25 | 26 | public: 27 | static map s_bookMap; 28 | static map s_customerMap; 29 | 30 | static map bookLoanMap; 31 | static map> bookReservationSetMap; 32 | 33 | static map> customerLoanSetMap; 34 | static map> customerReservationSetMap; 35 | }; -------------------------------------------------------------------------------- /Chapter03/LibraryBasic/LibraryBasic.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | Source Files 23 | 24 | 25 | Source Files 26 | 27 | 28 | Source Files 29 | 30 | 31 | 32 | 33 | Header Files 34 | 35 | 36 | Header Files 37 | 38 | 39 | Header Files 40 | 41 | 42 | -------------------------------------------------------------------------------- /Chapter03/LibraryBasic/Main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | #include "Book.h" 10 | #include "Customer.h" 11 | #include "Library.h" 12 | 13 | void main(void) { 14 | Library(); 15 | } -------------------------------------------------------------------------------- /Chapter04/LibraryPointer/Book.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | #include "Book.h" 10 | #include "Customer.h" 11 | #include "Library.h" 12 | 13 | Book::Book() { 14 | // Empty. 15 | } 16 | 17 | Book::Book(const string& author, const string& title) 18 | :m_author(author), 19 | m_title(title) { 20 | // Empty. 21 | } 22 | 23 | /*Book::Book(const Book& book) 24 | :m_author(book.m_author), 25 | m_title(book.m_title), 26 | m_borrowerPtr(book.m_borrowerPtr), 27 | m_reservationPtrList(book.m_reservationPtrList) { 28 | // Empty. 29 | } 30 | 31 | Book& Book::operator=(const Book& book) { 32 | m_author = book.m_author; 33 | m_title = book.m_title; 34 | m_borrowerPtr = book.m_borrowerPtr; 35 | m_reservationPtrList = book.m_reservationPtrList; 36 | return *this; 37 | }*/ 38 | 39 | void Book::read(ifstream& inStream) { 40 | getline(inStream, m_author); 41 | getline(inStream, m_title); 42 | } 43 | 44 | void Book::write(ofstream& outStream) const { 45 | outStream << m_author << endl; 46 | outStream << m_title << endl; 47 | } 48 | 49 | void Book::borrowBook(Customer* borrowerPtr) { 50 | m_borrowerPtr = borrowerPtr; 51 | } 52 | 53 | int Book::reserveBook(Customer* borrowerPtr) { 54 | m_reservationPtrList.push_back(borrowerPtr); 55 | return m_reservationPtrList.size(); 56 | } 57 | 58 | void Book::returnBook() { 59 | m_borrowerPtr = nullptr; 60 | } 61 | 62 | void Book::removeReservation(Customer* customerPtr) { 63 | m_reservationPtrList.remove(customerPtr); 64 | } 65 | 66 | ostream& operator<<(ostream& outStream, const Book& book) { 67 | outStream << "\"" << book.m_title << "\" by " << book.m_author; 68 | 69 | if (book.m_borrowerPtr != nullptr) { 70 | outStream << endl << " Borrowed by: " 71 | << book.m_borrowerPtr->name() << "."; 72 | } 73 | 74 | if (!book.m_reservationPtrList.empty()) { 75 | outStream << endl << " Reserved by: "; 76 | 77 | bool first = true; 78 | for (Customer* customerPtr : book.m_reservationPtrList) { 79 | outStream << (first ? "" : ",") << customerPtr->name(); 80 | first = false; 81 | } 82 | 83 | outStream << "."; 84 | } 85 | 86 | return outStream; 87 | } -------------------------------------------------------------------------------- /Chapter04/LibraryPointer/Book.h: -------------------------------------------------------------------------------- 1 | class Customer; 2 | 3 | class Book { 4 | public: 5 | Book(); 6 | Book(const string& author, const string& title); 7 | 8 | // Book(const Book& book); 9 | // Book& operator=(const Book& book); 10 | 11 | const string& author() const { return m_author; } 12 | const string& title() const { return m_title; } 13 | 14 | void read(ifstream& inStream); 15 | void write(ofstream& outStream) const; 16 | 17 | void borrowBook(Customer* customerPtr); 18 | int reserveBook(Customer* customerPtr); 19 | void returnBook(); 20 | void removeReservation(Customer* customerPtr); 21 | 22 | Customer*& borrowerPtr() { return m_borrowerPtr; } 23 | const Customer* borrowerPtr() const { return m_borrowerPtr; } 24 | 25 | list& reservationPtrList() 26 | { return m_reservationPtrList; } 27 | const list reservationPtrList() const 28 | { return m_reservationPtrList; } 29 | 30 | friend ostream& operator<<(ostream& outStream, 31 | const Book& book); 32 | 33 | private: 34 | string m_author, m_title; 35 | Customer* m_borrowerPtr = nullptr; 36 | list m_reservationPtrList; 37 | }; -------------------------------------------------------------------------------- /Chapter04/LibraryPointer/Customer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | #include "Book.h" 9 | #include "Customer.h" 10 | #include "Library.h" 11 | 12 | Customer::Customer() { 13 | // Empty. 14 | } 15 | 16 | Customer::Customer(const string& name, const string& address) 17 | :m_name(name), 18 | m_address(address) { 19 | // Empty. 20 | } 21 | 22 | /*Customer::Customer(const Customer& customer) 23 | :m_name(customer.m_name), 24 | m_address(customer.m_address), 25 | m_loanPtrSet(customer.m_loanPtrSet), 26 | m_reservationPtrSet(customer.m_reservationPtrSet) { 27 | // Empty. 28 | } 29 | 30 | Customer& Customer::operator=(const Customer& customer) { 31 | m_name = customer.m_name; 32 | m_address = customer.m_address; 33 | m_loanPtrSet = customer.m_loanPtrSet; 34 | m_reservationPtrSet = customer.m_reservationPtrSet; 35 | return *this; 36 | }*/ 37 | 38 | void Customer::read(ifstream& inStream) { 39 | getline(inStream, m_name); 40 | getline(inStream, m_address); 41 | } 42 | 43 | void Customer::write(ofstream& outStream) const { 44 | outStream << m_name << endl; 45 | outStream << m_address << endl; 46 | } 47 | 48 | void Customer::borrowBook(Book* bookPtr) { 49 | m_loanPtrSet.insert(bookPtr); 50 | m_reservationPtrSet.erase(bookPtr); 51 | } 52 | 53 | void Customer::returnBook(Book* bookPtr) { 54 | m_loanPtrSet.erase(bookPtr); 55 | } 56 | 57 | void Customer::reserveBook(Book* bookPtr) { 58 | m_reservationPtrSet.insert(bookPtr); 59 | } 60 | 61 | void Customer::unreserveBook(Book* bookPtr) { 62 | m_reservationPtrSet.erase(bookPtr); 63 | } 64 | 65 | ostream& operator<<(ostream& outStream, const Customer& customer){ 66 | outStream << customer.m_name << ", " 67 | << customer.m_address << "."; 68 | 69 | if (!customer.m_loanPtrSet.empty()) { 70 | outStream << endl << " Borrowed books: "; 71 | 72 | bool first = true; 73 | for (const Book* bookPtr : customer.m_loanPtrSet) { 74 | outStream << (first ? "" : ", ") << bookPtr->author(); 75 | first = false; 76 | } 77 | } 78 | 79 | if (!customer.m_reservationPtrSet.empty()) { 80 | outStream << endl << " Reserved books: "; 81 | 82 | bool first = true; 83 | for (Book* bookPtr : customer.m_reservationPtrSet) { 84 | outStream << (first ? "" : ", ") << bookPtr->author(); 85 | first = false; 86 | } 87 | } 88 | 89 | return outStream; 90 | } -------------------------------------------------------------------------------- /Chapter04/LibraryPointer/Customer.h: -------------------------------------------------------------------------------- 1 | class Customer { 2 | public: 3 | Customer(); 4 | Customer(const string& name, const string& address); 5 | 6 | // Customer(const Customer& customer); 7 | // Customer& operator=(const Customer& customer); 8 | 9 | const string& name() const { return m_name; } 10 | const string& address() const { return m_address; } 11 | 12 | void read(ifstream& inStream); 13 | void write(ofstream& outStream) const; 14 | 15 | void borrowBook(Book* bookPtr); 16 | void returnBook(Book* bookPtr); 17 | void reserveBook(Book* bookPtr); 18 | void unreserveBook(Book* bookPtr); 19 | 20 | set& loanPtrSet() { return m_loanPtrSet; } 21 | const set loanPtrSet() const { return m_loanPtrSet; } 22 | 23 | set& reservationPtrSet() { return m_reservationPtrSet; } 24 | const set reservationPtrSet() const { return m_reservationPtrSet; } 25 | 26 | friend ostream& operator<<(ostream& outStream, 27 | const Customer& customer); 28 | 29 | private: 30 | string m_name, m_address; 31 | set m_loanPtrSet, m_reservationPtrSet; 32 | }; 33 | -------------------------------------------------------------------------------- /Chapter04/LibraryPointer/Debug/LibraryPointer.log: -------------------------------------------------------------------------------- 1 | Build started 2017-12-01 15:26:53. 2 | 3 | Build succeeded. 4 | 5 | Time Elapsed 00:00:00.01 6 | -------------------------------------------------------------------------------- /Chapter04/LibraryPointer/Library.h: -------------------------------------------------------------------------------- 1 | class Library { 2 | public: 3 | Library(); 4 | ~Library(); 5 | 6 | private: 7 | static string s_binaryPath; 8 | 9 | Book* lookupBook(const string& author, const string& title); 10 | Customer* lookupCustomer(const string& name, 11 | const string& address); 12 | 13 | void addBook(); 14 | void deleteBook(); 15 | void listBooks(); 16 | void addCustomer(); 17 | void deleteCustomer(); 18 | void listCustomers(); 19 | void borrowBook(); 20 | void reserveBook(); 21 | void returnBook(); 22 | 23 | int lookupBookIndex(const Book* bookPtr); 24 | int lookupCustomerIndex(const Customer* customerPtr); 25 | 26 | Book* lookupBookPtr(int bookIndex); 27 | Customer* lookupCustomerPtr(int customerIndex); 28 | 29 | void save(); 30 | void load(); 31 | 32 | list m_bookPtrList; 33 | list m_customerPtrList; 34 | }; -------------------------------------------------------------------------------- /Chapter04/LibraryPointer/LibraryPointer.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | Source Files 23 | 24 | 25 | Source Files 26 | 27 | 28 | Source Files 29 | 30 | 31 | 32 | 33 | Header Files 34 | 35 | 36 | Header Files 37 | 38 | 39 | Header Files 40 | 41 | 42 | -------------------------------------------------------------------------------- /Chapter04/LibraryPointer/Main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | #include "Book.h" 10 | #include "Customer.h" 11 | #include "Library.h" 12 | 13 | void main() { 14 | Library(); 15 | } -------------------------------------------------------------------------------- /Chapter04/LibraryPointer/Register.binary: -------------------------------------------------------------------------------- 1 | S 2 | H 3 | D 4 | T 5 | X 6 | A 7 | Y 8 | B 9 | Z 10 | C 11 |  -------------------------------------------------------------------------------- /Chapter04/LibraryPointer/Release/LibraryPointer.Build.CppClean.log: -------------------------------------------------------------------------------- 1 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\librarypointer\release\vc120.pdb 2 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\librarypointer\release\main.obj 3 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\librarypointer\release\library.obj 4 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\librarypointer\release\borrower.obj 5 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\librarypointer\release\book.obj 6 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\release\librarypointer.exe 7 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\release\librarypointer.pdb 8 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\librarypointer\release\librarypointer.tlog\cl.command.1.tlog 9 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\librarypointer\release\librarypointer.tlog\cl.read.1.tlog 10 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\librarypointer\release\librarypointer.tlog\cl.write.1.tlog 11 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\librarypointer\release\librarypointer.tlog\link.command.1.tlog 12 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\librarypointer\release\librarypointer.tlog\link.read.1.tlog 13 | c:\users\stefan\documents\a a_cpp_by_example\cppbyexample\librarypointer\release\librarypointer.tlog\link.write.1.tlog 14 | -------------------------------------------------------------------------------- /Chapter04/LibraryPointer/Release/LibraryPointer.log: -------------------------------------------------------------------------------- 1 | Build started 2017-06-29 17:45:30. 2 | 3 | Build succeeded. 4 | 5 | Time Elapsed 00:00:00.03 6 | -------------------------------------------------------------------------------- /Chapter05/ClockBasic/Clock.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Clock.h" 3 | 4 | Clock::Clock(QWidget *parentWidgetPtr /* = nullptr */) 5 | :QWidget(parentWidgetPtr) { 6 | setWindowTitle(tr("Clock")); 7 | resize(1000, 500); 8 | 9 | m_timer.setParent(this); 10 | connect(&m_timer, SIGNAL(timeout()), this, SLOT(update())); 11 | m_timer.start(100); 12 | } 13 | 14 | void Clock::paintEvent(QPaintEvent* /* eventPtr */) { 15 | int side = qMin(width(), height()); 16 | QTime time = QTime::currentTime(); 17 | 18 | QPainter painter(this); 19 | painter.setRenderHint(QPainter::Antialiasing); 20 | painter.setRenderHint(QPainter::TextAntialiasing); 21 | painter.translate(width() / 2, height() / 2); 22 | painter.scale(side / 200.0, side / 200.0); 23 | 24 | for (int second = 0; second <= 60; ++second) { 25 | if ((second % 5) == 0) { 26 | painter.drawLine(QPoint(0, 81), QPoint(0, 98)); 27 | } 28 | else { 29 | painter.drawLine(QPoint(0, 90), QPoint(0, 98)); 30 | } 31 | 32 | painter.rotate(6); 33 | } 34 | 35 | double hours = time.hour(), minutes = time.minute(), 36 | seconds = time.second(), milliseconds = time.msec(); 37 | 38 | painter.setPen(Qt::black); 39 | painter.setBrush(Qt::gray); 40 | 41 | { static const QPoint hourHand[3] = 42 | {QPoint(8, 8), QPoint(-8, 8), QPoint(0, -60)}; 43 | 44 | painter.save(); 45 | double hour = hours + (minutes / 60.0) + (seconds / 3600.0) + 46 | (milliseconds / 3600000.0); 47 | painter.rotate(30.0 * hour); 48 | painter.drawConvexPolygon(hourHand, 3); 49 | painter.restore(); 50 | } 51 | 52 | { static const QPoint minuteHand[3] = 53 | {QPoint(6, 8), QPoint(-6, 8), QPoint(0, -70)}; 54 | 55 | painter.save(); 56 | double minute = minutes + (seconds / 60.0) + 57 | (milliseconds / 60000.0); 58 | painter.rotate(6.0 * minute); 59 | painter.drawConvexPolygon(minuteHand, 3); 60 | painter.restore(); 61 | } 62 | 63 | { static const QPoint secondHand[3] = 64 | {QPoint(4, 8), QPoint(-4, 8), QPoint(0, -80)}; 65 | 66 | painter.save(); 67 | double second = seconds + (milliseconds / 1000); 68 | painter.rotate(6.0 * second); 69 | painter.drawConvexPolygon(secondHand, 3); 70 | painter.restore(); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /Chapter05/ClockBasic/Clock.h: -------------------------------------------------------------------------------- 1 | #ifndef CLOCK_H 2 | #define CLOCK_H 3 | 4 | #include 5 | #include 6 | 7 | class Clock : public QWidget { 8 | Q_OBJECT 9 | 10 | public: 11 | Clock(QWidget *parentWidgetPtr = nullptr); 12 | void paintEvent(QPaintEvent *eventPtr) override; 13 | 14 | private: 15 | QTimer m_timer; 16 | }; 17 | 18 | #endif // CLOCK_H 19 | -------------------------------------------------------------------------------- /Chapter05/ClockBasic/ClockBasic.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2017-06-08T21:31:19 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = ClockBasic 12 | TEMPLATE = app 13 | 14 | # The following define makes your compiler emit warnings if you use 15 | # any feature of Qt which as been marked as deprecated (the exact warnings 16 | # depend on your compiler). Please consult the documentation of the 17 | # deprecated API in order to know how to port your code away from it. 18 | DEFINES += QT_DEPRECATED_WARNINGS 19 | 20 | # You can also make your code fail to compile if you use deprecated APIs. 21 | # In order to do so, uncomment the following line. 22 | # You can also select to disable deprecated APIs only up to a certain version of Qt. 23 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 24 | 25 | 26 | SOURCES += \ 27 | Clock.cpp \ 28 | Main.cpp 29 | 30 | HEADERS += \ 31 | Clock.h 32 | -------------------------------------------------------------------------------- /Chapter05/ClockBasic/Main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Clock.h" 3 | 4 | int main(int argc, char *argv[]) { 5 | QApplication application(argc, argv); 6 | Clock Clock; 7 | Clock.show(); 8 | return application.exec(); 9 | } 10 | -------------------------------------------------------------------------------- /Chapter05/DrawingBasic/DrawingBasic.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2017-06-11T00:35:56 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = DrawingBasic 12 | TEMPLATE = app 13 | 14 | # The following define makes your compiler emit warnings if you use 15 | # any feature of Qt which as been marked as deprecated (the exact warnings 16 | # depend on your compiler). Please consult the documentation of the 17 | # deprecated API in order to know how to port your code away from it. 18 | DEFINES += QT_DEPRECATED_WARNINGS 19 | 20 | # You can also make your code fail to compile if you use deprecated APIs. 21 | # In order to do so, uncomment the following line. 22 | # You can also select to disable deprecated APIs only up to a certain version of Qt. 23 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 24 | 25 | CONFIG += no_batch 26 | 27 | SOURCES += \ 28 | DrawingWidget.cpp \ 29 | DrawingWindow.cpp \ 30 | Ellipse.cpp \ 31 | Figure.cpp \ 32 | Global.cpp \ 33 | Line.cpp \ 34 | Main.cpp \ 35 | Rectangle.cpp \ 36 | ../MainWindow/DocumentWidget.cpp \ 37 | ../MainWindow/MainWindow.cpp 38 | 39 | HEADERS += \ 40 | DrawingWidget.h \ 41 | DrawingWindow.h \ 42 | Ellipse.h \ 43 | Figure.h \ 44 | Global.h \ 45 | Line.h \ 46 | Rectangle.h \ 47 | ../MainWindow/DocumentWidget.h \ 48 | ../MainWindow/MainWindow.h \ 49 | ../MainWindow/Listener.h 50 | 51 | FORMS += 52 | 53 | RESOURCES += \ 54 | DrawingBasic.qrc 55 | -------------------------------------------------------------------------------- /Chapter05/DrawingBasic/DrawingBasic.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | images/qt.png 4 | images/new.png 5 | images/open.png 6 | images/save.png 7 | images/cut.png 8 | images/copy.png 9 | images/paste.png 10 | images/left.png 11 | images/center.png 12 | images/right.png 13 | images/delete.png 14 | 15 | 16 | -------------------------------------------------------------------------------- /Chapter05/DrawingBasic/DrawingWidget.h: -------------------------------------------------------------------------------- 1 | #ifndef DRAWINGWIDGET_H 2 | #define DRAWINGWIDGET_H 3 | 4 | #include "..\\MainWindow\\MainWindow.h" 5 | #include "..\\MainWindow\\DocumentWidget.h" 6 | #include "Figure.h" 7 | 8 | class DrawingWidget : public DocumentWidget { 9 | Q_OBJECT 10 | 11 | public: 12 | DrawingWidget(QWidget* parentWidgetPtr); 13 | ~DrawingWidget(); 14 | 15 | public: 16 | void mousePressEvent(QMouseEvent *eventPtr) override; 17 | void mouseMoveEvent(QMouseEvent *eventPtr) override; 18 | void mouseReleaseEvent(QMouseEvent *eventPtr) override; 19 | void paintEvent(QPaintEvent *eventPtr) override; 20 | 21 | private: 22 | void newDocument() override; 23 | bool writeFile(const QString& filePath) override; 24 | bool readFile(const QString& filePath) override; 25 | Figure* createFigure(FigureId figureId); 26 | 27 | public slots: 28 | void onColor(void); 29 | 30 | DEFINE_LISTENER(DrawingWidget, isFillEnabled); 31 | void onFill(void); 32 | 33 | DEFINE_LISTENER(DrawingWidget, isLineChecked); 34 | DEFINE_LISTENER(DrawingWidget, isRectangleChecked); 35 | DEFINE_LISTENER(DrawingWidget, isEllipseChecked); 36 | 37 | void onLine(); 38 | void onRectangle(); 39 | void onEllipse(); 40 | 41 | private: 42 | enum ApplicationMode {Idle, Create, Move}; 43 | ApplicationMode m_applicationMode = Idle; 44 | void setApplicationMode(ApplicationMode mode); 45 | 46 | QColor m_currColor = Qt::black; 47 | bool m_currFilled = false; 48 | FigureId m_addFigureId = LineId; 49 | 50 | QPoint m_mousePoint; 51 | QList m_figurePtrList; 52 | }; 53 | 54 | #endif // DRAWINGWIDGET_H 55 | -------------------------------------------------------------------------------- /Chapter05/DrawingBasic/DrawingWindow.cpp: -------------------------------------------------------------------------------- 1 | #include "..\\MainWindow\\DocumentWidget.h" 2 | #include "DrawingWindow.h" 3 | 4 | DrawingWindow::DrawingWindow(QWidget 5 | *parentWidgetPtr /* = nullptr */) 6 | :MainWindow(parentWidgetPtr) { 7 | resize(1000, 500); 8 | 9 | m_drawingWidgetPtr = new DrawingWidget(this); 10 | setCentralWidget(m_drawingWidgetPtr); 11 | addFileMenu(); 12 | 13 | { QMenu* formatMenuPtr = menuBar()->addMenu(tr("F&ormat")); 14 | connect(formatMenuPtr, SIGNAL(aboutToShow()), 15 | this, SLOT(onMenuShow())); 16 | 17 | addAction(formatMenuPtr, tr("&Color"), 18 | SLOT(onColor()), QKeySequence(Qt::ALT + Qt::Key_C), 19 | QString(), nullptr, tr("Figure Color")); 20 | 21 | addAction(formatMenuPtr, tr("&Fill"), 22 | SLOT(onFill()), QKeySequence(Qt::CTRL + Qt::Key_F), 23 | QString(), nullptr, tr("Figure Fill"), 24 | LISTENER(isFillEnabled)); 25 | } 26 | 27 | { m_figureGroupPtr = new QActionGroup(this); 28 | 29 | QMenu* figureMenuPtr = menuBar()->addMenu(tr("F&igure")); 30 | connect(figureMenuPtr, SIGNAL(aboutToShow()), 31 | this, SLOT(onMenuShow())); 32 | 33 | addAction(figureMenuPtr, tr("&Line"), 34 | SLOT(onLine()), QKeySequence(Qt::CTRL + Qt::Key_L), 35 | QString(), nullptr, tr("Line Figure"), nullptr, 36 | LISTENER(isLineChecked), m_figureGroupPtr); 37 | addAction(figureMenuPtr, tr("&Rectangle"), 38 | SLOT(onRectangle()), 39 | QKeySequence(Qt::CTRL + Qt::Key_R), 40 | QString(), nullptr, tr("Rectangle Figure"), nullptr, 41 | LISTENER(isRectangleChecked), m_figureGroupPtr); 42 | addAction(figureMenuPtr, tr("&Ellipse"), 43 | SLOT(onEllipse()), 44 | QKeySequence(Qt::CTRL + Qt::Key_E), 45 | QString(), nullptr, tr("Ellipse Figure"), nullptr, 46 | LISTENER(isEllipseChecked), m_figureGroupPtr); 47 | } 48 | } 49 | 50 | DrawingWindow::~DrawingWindow() { 51 | delete m_figureGroupPtr; 52 | } 53 | -------------------------------------------------------------------------------- /Chapter05/DrawingBasic/DrawingWindow.h: -------------------------------------------------------------------------------- 1 | #ifndef DRAWINGWINDOW_H 2 | #define DRAWINGWINDOW_H 3 | 4 | #include 5 | #include 6 | 7 | #include "..\\MainWindow\\MainWindow.h" 8 | #include "DrawingWidget.h" 9 | 10 | class DrawingWindow : public MainWindow { 11 | Q_OBJECT 12 | 13 | public: 14 | DrawingWindow(QWidget *parentWidgetPtr = nullptr); 15 | ~DrawingWindow(); 16 | 17 | public: 18 | void closeEvent(QCloseEvent *eventPtr) override 19 | { m_drawingWidgetPtr->closeEvent(eventPtr); } 20 | 21 | private: 22 | DrawingWidget* m_drawingWidgetPtr; 23 | QActionGroup* m_figureGroupPtr; 24 | }; 25 | 26 | #endif // DRAWINGWINDOW_H 27 | -------------------------------------------------------------------------------- /Chapter05/DrawingBasic/Ellipse.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "..\\MainWindow\\DocumentWidget.h" 3 | #include "Ellipse.h" 4 | 5 | EllipseX::EllipseX() { 6 | // Empty. 7 | } 8 | 9 | bool EllipseX::isClick(QPoint mousePoint) { 10 | QRect normalRect(m_topLeft, m_bottomRight); 11 | normalRect.normalized(); 12 | 13 | if (filled()) { 14 | QRegion normalEllipse(normalRect, QRegion::Ellipse); 15 | return normalEllipse.contains(mousePoint); 16 | } 17 | else { 18 | QRect largeRect(normalRect), smallRect(normalRect); 19 | largeRect += Tolerance; 20 | smallRect -= Tolerance; 21 | 22 | QRegion largeEllipse(largeRect, QRegion::Ellipse), 23 | smallEllipse(smallRect, QRegion::Ellipse); 24 | 25 | return (largeEllipse.contains(mousePoint) && 26 | !smallEllipse.contains(mousePoint)); 27 | } 28 | } 29 | 30 | void EllipseX::draw(QPainter& painter) const { 31 | painter.setPen(color()); 32 | 33 | if (filled()) { 34 | painter.setBrush(color()); 35 | painter.drawEllipse(QRect(m_topLeft, m_bottomRight)); 36 | } 37 | else { 38 | painter.setBrush(Qt::NoBrush); 39 | painter.drawEllipse(QRect(m_topLeft, m_bottomRight)); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Chapter05/DrawingBasic/Ellipse.h: -------------------------------------------------------------------------------- 1 | #ifndef ELLIPSE_H 2 | #define ELLIPSE_H 3 | 4 | #include "Rectangle.h" 5 | 6 | class EllipseX : public RectangleX { 7 | public: 8 | EllipseX(); 9 | FigureId getId() const {return EllipseId;} 10 | 11 | bool isClick(QPoint mousePoint); 12 | void draw(QPainter& painter) const; 13 | }; 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /Chapter05/DrawingBasic/Figure.cpp: -------------------------------------------------------------------------------- 1 | #include "..\\MainWindow\\DocumentWidget.h" 2 | #include "Figure.h" 3 | 4 | const int Figure::Tolerance(6); 5 | 6 | Figure::Figure() { 7 | // Empty. 8 | } 9 | 10 | bool Figure::write(ofstream& outStream) const { 11 | writeColor(outStream, m_color); 12 | outStream.write((char*) &m_filled, sizeof m_filled); 13 | return ((bool) outStream); 14 | } 15 | 16 | bool Figure::read(ifstream& inStream) { 17 | readColor(inStream, m_color); 18 | inStream.read((char*) &m_filled, sizeof m_filled); 19 | return ((bool) inStream); 20 | } 21 | -------------------------------------------------------------------------------- /Chapter05/DrawingBasic/Figure.h: -------------------------------------------------------------------------------- 1 | #ifndef FIGURE_H 2 | #define FIGURE_H 3 | 4 | enum FigureId {LineId, RectangleId, EllipseId}; 5 | 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | class Figure { 11 | public: 12 | Figure(); 13 | virtual FigureId getId() const = 0; 14 | virtual void initializePoints(QPoint point) = 0; 15 | virtual void setLastPoint(QPoint point) = 0; 16 | 17 | virtual bool isClick(QPoint mousePoint) = 0; 18 | virtual void move(QSize distance) = 0; 19 | virtual void draw(QPainter &painter) const = 0; 20 | 21 | virtual bool write(ofstream& outStream) const; 22 | virtual bool read(ifstream& inStream); 23 | 24 | const QColor& color() const {return m_color;} 25 | QColor& color() {return m_color;} 26 | 27 | virtual bool filled() const {return m_filled;} 28 | virtual bool& filled() {return m_filled;} 29 | 30 | static const int Tolerance; 31 | 32 | private: 33 | QColor m_color; 34 | bool m_marked = false, m_filled = false; 35 | }; 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /Chapter05/DrawingBasic/Line.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "..\\MainWindow\\DocumentWidget.h" 3 | #include "Line.h" 4 | 5 | Line::Line() { 6 | // Empty. 7 | } 8 | 9 | void Line::initializePoints(QPoint point) { 10 | m_firstPoint = point; 11 | m_lastPoint = point; 12 | } 13 | 14 | void Line::setLastPoint(QPoint point) { 15 | m_lastPoint = point; 16 | } 17 | 18 | bool Line::isClick(QPoint mousePoint) { 19 | if (m_firstPoint.x() == m_lastPoint.x()) { 20 | QRect lineRect(m_firstPoint, m_lastPoint); 21 | lineRect.normalized(); 22 | lineRect += Tolerance; 23 | return lineRect.contains(mousePoint); 24 | } 25 | else { 26 | QPoint leftPoint = (m_firstPoint.x() < m_lastPoint.x()) 27 | ? m_firstPoint : m_lastPoint, 28 | rightPoint = (m_firstPoint.x() < m_lastPoint.x()) 29 | ? m_lastPoint : m_firstPoint; 30 | 31 | if ((leftPoint.x() <= mousePoint.x()) && 32 | (mousePoint.x() <= rightPoint.x())) { 33 | int lineWidth = rightPoint.x() - leftPoint.x(), 34 | lineHeight = rightPoint.y() - leftPoint.y(); 35 | 36 | int diffWidth = mousePoint.x() - leftPoint.x(), 37 | diffHeight = mousePoint.y() - leftPoint.y(); 38 | 39 | return (fabs(diffHeight - (((double) lineHeight) / 40 | lineWidth) * diffWidth) <= Tolerance); 41 | } 42 | 43 | return false; 44 | } 45 | } 46 | 47 | void Line::move(QSize distance) { 48 | m_firstPoint += distance; 49 | m_lastPoint += distance; 50 | } 51 | 52 | void Line::draw(QPainter& painter) const { 53 | painter.setPen(color()); 54 | painter.drawLine(m_firstPoint, m_lastPoint); 55 | } 56 | 57 | bool Line::write(ofstream& outStream) const { 58 | Figure::write(outStream); 59 | writePoint(outStream, m_firstPoint); 60 | writePoint(outStream, m_lastPoint); 61 | return ((bool) outStream); 62 | } 63 | 64 | bool Line::read(ifstream& inStream) { 65 | Figure::read(inStream); 66 | readPoint(inStream, m_firstPoint); 67 | readPoint(inStream, m_lastPoint); 68 | return ((bool) inStream); 69 | } 70 | -------------------------------------------------------------------------------- /Chapter05/DrawingBasic/Line.h: -------------------------------------------------------------------------------- 1 | #ifndef LINE_H 2 | #define LINE_H 3 | 4 | #include 5 | using namespace std; 6 | 7 | #include "Figure.h" 8 | 9 | class Line : public Figure { 10 | public: 11 | Line(); 12 | FigureId getId() const {return LineId;} 13 | 14 | void initializePoints(QPoint point); 15 | void setLastPoint(QPoint point); 16 | 17 | bool isClick(QPoint mousePoint); 18 | void move(QSize distance); 19 | void draw(QPainter& painter) const; 20 | 21 | bool write(ofstream& outStream) const; 22 | bool read(ifstream& inStream); 23 | 24 | private: 25 | QPoint m_firstPoint, m_lastPoint; 26 | }; 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /Chapter05/DrawingBasic/Main.cpp: -------------------------------------------------------------------------------- 1 | #include "DrawingWindow.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) { 5 | QApplication application(argc, argv); 6 | DrawingWindow drawingWindow; 7 | drawingWindow.show(); 8 | return application.exec(); 9 | } 10 | -------------------------------------------------------------------------------- /Chapter05/DrawingBasic/Rectangle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "..\\MainWindow\\DocumentWidget.h" 3 | #include "Rectangle.h" 4 | 5 | RectangleX::RectangleX() { 6 | // Empty. 7 | } 8 | 9 | void RectangleX::initializePoints(QPoint point) { 10 | m_topLeft = point; 11 | m_bottomRight = point; 12 | } 13 | 14 | void RectangleX::setLastPoint(QPoint point) { 15 | m_bottomRight = point; 16 | } 17 | 18 | bool RectangleX::isClick(QPoint mousePoint) { 19 | QRect areaRect(m_topLeft, m_bottomRight); 20 | 21 | if (filled()) { 22 | return areaRect.contains(mousePoint); 23 | } 24 | else { 25 | QRect largeAreaRect(areaRect), smallAreaRect(areaRect); 26 | 27 | largeAreaRect += Tolerance; 28 | smallAreaRect -= Tolerance; 29 | 30 | return largeAreaRect.contains(mousePoint) && 31 | !smallAreaRect.contains(mousePoint); 32 | } 33 | 34 | return false; 35 | } 36 | 37 | void RectangleX::move(QSize distance) { 38 | m_topLeft += distance; 39 | m_bottomRight += distance; 40 | } 41 | 42 | void RectangleX::draw(QPainter& painter) const { 43 | painter.setPen(color()); 44 | 45 | if (filled()) { 46 | painter.fillRect(QRect(m_topLeft, m_bottomRight), color()); 47 | } 48 | else { 49 | painter.setBrush(Qt::NoBrush); 50 | painter.drawRect(QRect(m_topLeft, m_bottomRight)); 51 | } 52 | } 53 | 54 | bool RectangleX::write(ofstream& outStream) const { 55 | Figure::write(outStream); 56 | writePoint(outStream, m_topLeft); 57 | writePoint(outStream, m_bottomRight); 58 | return ((bool) outStream); 59 | } 60 | 61 | bool RectangleX::read (ifstream& inStream) { 62 | Figure::read(inStream); 63 | readPoint(inStream, m_topLeft); 64 | readPoint(inStream, m_bottomRight); 65 | return ((bool) inStream); 66 | } 67 | -------------------------------------------------------------------------------- /Chapter05/DrawingBasic/Rectangle.h: -------------------------------------------------------------------------------- 1 | #ifndef RECTANGLE_H 2 | #define RECTANGLE_H 3 | 4 | #include 5 | using namespace std; 6 | 7 | #include "Figure.h" 8 | 9 | class RectangleX : public Figure { 10 | public: 11 | RectangleX(); 12 | virtual FigureId getId() const {return RectangleId;} 13 | 14 | virtual void initializePoints(QPoint point); 15 | virtual void setLastPoint(QPoint point); 16 | 17 | virtual bool isClick(QPoint mousePoint); 18 | virtual void move(QSize distance); 19 | virtual void draw(QPainter& painter) const; 20 | 21 | virtual bool write(ofstream& outStream) const; 22 | virtual bool read(ifstream& inStream); 23 | 24 | protected: 25 | QPoint m_topLeft, m_bottomRight; 26 | }; 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /Chapter05/DrawingBasic/images/center.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/CPP17-By-Example/543f6cc865b8fe5fb83dd5886c73e5dff45f377f/Chapter05/DrawingBasic/images/center.png -------------------------------------------------------------------------------- /Chapter05/DrawingBasic/images/copy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/CPP17-By-Example/543f6cc865b8fe5fb83dd5886c73e5dff45f377f/Chapter05/DrawingBasic/images/copy.png -------------------------------------------------------------------------------- /Chapter05/DrawingBasic/images/cut.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/CPP17-By-Example/543f6cc865b8fe5fb83dd5886c73e5dff45f377f/Chapter05/DrawingBasic/images/cut.png -------------------------------------------------------------------------------- /Chapter05/DrawingBasic/images/delete.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/CPP17-By-Example/543f6cc865b8fe5fb83dd5886c73e5dff45f377f/Chapter05/DrawingBasic/images/delete.png -------------------------------------------------------------------------------- /Chapter05/DrawingBasic/images/left.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/CPP17-By-Example/543f6cc865b8fe5fb83dd5886c73e5dff45f377f/Chapter05/DrawingBasic/images/left.png -------------------------------------------------------------------------------- /Chapter05/DrawingBasic/images/new.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/CPP17-By-Example/543f6cc865b8fe5fb83dd5886c73e5dff45f377f/Chapter05/DrawingBasic/images/new.png -------------------------------------------------------------------------------- /Chapter05/DrawingBasic/images/open.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/CPP17-By-Example/543f6cc865b8fe5fb83dd5886c73e5dff45f377f/Chapter05/DrawingBasic/images/open.png -------------------------------------------------------------------------------- /Chapter05/DrawingBasic/images/paste.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/CPP17-By-Example/543f6cc865b8fe5fb83dd5886c73e5dff45f377f/Chapter05/DrawingBasic/images/paste.png -------------------------------------------------------------------------------- /Chapter05/DrawingBasic/images/qt.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/CPP17-By-Example/543f6cc865b8fe5fb83dd5886c73e5dff45f377f/Chapter05/DrawingBasic/images/qt.png -------------------------------------------------------------------------------- /Chapter05/DrawingBasic/images/right.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/CPP17-By-Example/543f6cc865b8fe5fb83dd5886c73e5dff45f377f/Chapter05/DrawingBasic/images/right.png -------------------------------------------------------------------------------- /Chapter05/DrawingBasic/images/save.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/CPP17-By-Example/543f6cc865b8fe5fb83dd5886c73e5dff45f377f/Chapter05/DrawingBasic/images/save.png -------------------------------------------------------------------------------- /Chapter05/EditorBasic/Caret.cpp: -------------------------------------------------------------------------------- 1 | #include "Caret.h" 2 | #include 3 | 4 | Caret::Caret(QWidget *parentWidgetPtr) 5 | :m_parentWidgetPtr(parentWidgetPtr) { 6 | m_timer.setParent(this); 7 | connect(&m_timer, SIGNAL(timeout()), this, SLOT(onTimer())); 8 | m_timer.start(500); 9 | } 10 | 11 | void Caret::show() { 12 | m_visible = true; 13 | m_parentWidgetPtr->update(m_rect); 14 | } 15 | 16 | void Caret::hide() { 17 | m_visible = false; 18 | m_parentWidgetPtr->update(m_rect); 19 | } 20 | 21 | void Caret::set(QRect rect) { 22 | m_rect = rect; 23 | m_rect.setWidth(1); 24 | m_parentWidgetPtr->update(m_rect); 25 | } 26 | 27 | void Caret::onTimer(void) { 28 | m_blink = !m_blink; 29 | m_parentWidgetPtr->update(m_rect); 30 | } 31 | 32 | void Caret::paint(QPainter& painter) { 33 | if (m_visible && m_blink) { 34 | painter.save(); 35 | painter.setPen(Qt::NoPen); 36 | painter.setBrush(Qt::black); 37 | painter.drawRect(m_rect); 38 | painter.restore(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Chapter05/EditorBasic/Caret.h: -------------------------------------------------------------------------------- 1 | #ifndef CARET_H 2 | #define CARET_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | class Caret : public QObject { 9 | Q_OBJECT 10 | 11 | public: 12 | Caret(QWidget *parentWidgetPtr = nullptr); 13 | 14 | void show(); 15 | void hide(); 16 | void set(QRect rect); 17 | void paint(QPainter& painter); 18 | 19 | public slots: 20 | void onTimer(void); 21 | 22 | private: 23 | QWidget* m_parentWidgetPtr; 24 | bool m_visible, m_blink; 25 | QRect m_rect; 26 | QTimer m_timer; 27 | }; 28 | 29 | #endif // CARET_H 30 | -------------------------------------------------------------------------------- /Chapter05/EditorBasic/EditorBasic.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2017-06-11T01:58:41 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = EditorBasic 12 | TEMPLATE = app 13 | 14 | # The following define makes your compiler emit warnings if you use 15 | # any feature of Qt which as been marked as deprecated (the exact warnings 16 | # depend on your compiler). Please consult the documentation of the 17 | # deprecated API in order to know how to port your code away from it. 18 | DEFINES += QT_DEPRECATED_WARNINGS 19 | 20 | # You can also make your code fail to compile if you use deprecated APIs. 21 | # In order to do so, uncomment the following line. 22 | # You can also select to disable deprecated APIs only up to a certain version of Qt. 23 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 24 | 25 | CONFIG += no_batch 26 | 27 | SOURCES += \ 28 | Caret.cpp \ 29 | EditorWidget.cpp \ 30 | EditorWindow.cpp \ 31 | Main.cpp \ 32 | ../MainWindow/DocumentWidget.cpp \ 33 | ../MainWindow/MainWindow.cpp 34 | 35 | HEADERS += \ 36 | Caret.h \ 37 | EditorWidget.h \ 38 | EditorWindow.h \ 39 | ../MainWindow/DocumentWidget.h \ 40 | ../MainWindow/MainWindow.h \ 41 | ../MainWindow/Listener.h 42 | 43 | RESOURCES += \ 44 | EditorBasic.qrc 45 | -------------------------------------------------------------------------------- /Chapter05/EditorBasic/EditorBasic.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | images/qt.png 4 | images/new.png 5 | images/open.png 6 | images/save.png 7 | images/cut.png 8 | images/copy.png 9 | images/paste.png 10 | images/left.png 11 | images/center.png 12 | images/right.png 13 | 14 | 15 | -------------------------------------------------------------------------------- /Chapter05/EditorBasic/EditorWidget.h: -------------------------------------------------------------------------------- 1 | #ifndef EDITORWIDGET_H 2 | #define EDITORWIDGET_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include "Caret.h" 10 | 11 | #include "..\\MainWindow\\DocumentWidget.h" 12 | 13 | class EditorWidget : public DocumentWidget { 14 | Q_OBJECT 15 | 16 | public: 17 | EditorWidget(QWidget* parentWidgetPtr); 18 | void keyPressEvent(QKeyEvent* eventPtr); 19 | void mousePressEvent(QMouseEvent* eventPtr) override; 20 | 21 | private: 22 | int mouseToIndex(QPoint point); 23 | 24 | public: 25 | void paintEvent(QPaintEvent* eventPtr) override; 26 | void resizeEvent(QResizeEvent* eventPtr) override; 27 | 28 | private: 29 | void newDocument(void); 30 | bool writeFile(const QString& filePath); 31 | bool readFile(const QString& filePath); 32 | 33 | private: 34 | void setCaret(); 35 | void simulateMouseClick(int x, int y); 36 | 37 | private: 38 | void calculate(); 39 | 40 | int m_editIndex = 0; 41 | Caret m_caret; 42 | QList> m_lineList; 43 | QString m_editorText; 44 | QList m_rectList; 45 | static const QFont TextFont; 46 | int FontWidth, FontHeight; 47 | }; 48 | 49 | #endif // EDITORWIDGET_H 50 | -------------------------------------------------------------------------------- /Chapter05/EditorBasic/EditorWindow.cpp: -------------------------------------------------------------------------------- 1 | #include "EditorWindow.h" 2 | #include 3 | 4 | EditorWindow::EditorWindow(QWidget *parentWidgetPtr /*= nullptr*/) 5 | :MainWindow(parentWidgetPtr) { 6 | resize(1000, 500); 7 | m_editorWidgetPtr = new EditorWidget(this); 8 | setCentralWidget(m_editorWidgetPtr); 9 | addFileMenu(); 10 | } 11 | 12 | EditorWindow::~EditorWindow() { 13 | // Empty. 14 | } 15 | 16 | void EditorWindow::keyPressEvent(QKeyEvent* eventPtr) { 17 | m_editorWidgetPtr->keyPressEvent(eventPtr); 18 | } 19 | 20 | void EditorWindow::closeEvent(QCloseEvent* eventPtr) { 21 | m_editorWidgetPtr->closeEvent(eventPtr); 22 | } 23 | -------------------------------------------------------------------------------- /Chapter05/EditorBasic/EditorWindow.h: -------------------------------------------------------------------------------- 1 | #ifndef EDITORWINDOW_H 2 | #define EDITORWINDOW_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include "..\\MainWindow\\MainWindow.h" 10 | #include "EditorWidget.h" 11 | 12 | class EditorWindow : public MainWindow { 13 | Q_OBJECT 14 | 15 | public: 16 | EditorWindow(QWidget *parentWidgetPtr = nullptr); 17 | ~EditorWindow(); 18 | 19 | protected: 20 | void keyPressEvent(QKeyEvent* eventPtr) override; 21 | void closeEvent(QCloseEvent* eventPtr) override; 22 | 23 | private: 24 | EditorWidget* m_editorWidgetPtr; 25 | }; 26 | 27 | #endif // EDITORWINDOW_H 28 | -------------------------------------------------------------------------------- /Chapter05/EditorBasic/Main.cpp: -------------------------------------------------------------------------------- 1 | #include "EditorWindow.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) { 5 | QApplication application(argc, argv); 6 | EditorWindow editorWindow; 7 | editorWindow.show(); 8 | return application.exec(); 9 | } 10 | -------------------------------------------------------------------------------- /Chapter05/EditorBasic/images/center.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/CPP17-By-Example/543f6cc865b8fe5fb83dd5886c73e5dff45f377f/Chapter05/EditorBasic/images/center.png -------------------------------------------------------------------------------- /Chapter05/EditorBasic/images/copy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/CPP17-By-Example/543f6cc865b8fe5fb83dd5886c73e5dff45f377f/Chapter05/EditorBasic/images/copy.png -------------------------------------------------------------------------------- /Chapter05/EditorBasic/images/cut.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/CPP17-By-Example/543f6cc865b8fe5fb83dd5886c73e5dff45f377f/Chapter05/EditorBasic/images/cut.png -------------------------------------------------------------------------------- /Chapter05/EditorBasic/images/left.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/CPP17-By-Example/543f6cc865b8fe5fb83dd5886c73e5dff45f377f/Chapter05/EditorBasic/images/left.png -------------------------------------------------------------------------------- /Chapter05/EditorBasic/images/new.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/CPP17-By-Example/543f6cc865b8fe5fb83dd5886c73e5dff45f377f/Chapter05/EditorBasic/images/new.png -------------------------------------------------------------------------------- /Chapter05/EditorBasic/images/open.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/CPP17-By-Example/543f6cc865b8fe5fb83dd5886c73e5dff45f377f/Chapter05/EditorBasic/images/open.png -------------------------------------------------------------------------------- /Chapter05/EditorBasic/images/paste.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/CPP17-By-Example/543f6cc865b8fe5fb83dd5886c73e5dff45f377f/Chapter05/EditorBasic/images/paste.png -------------------------------------------------------------------------------- /Chapter05/EditorBasic/images/qt.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/CPP17-By-Example/543f6cc865b8fe5fb83dd5886c73e5dff45f377f/Chapter05/EditorBasic/images/qt.png -------------------------------------------------------------------------------- /Chapter05/EditorBasic/images/right.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/CPP17-By-Example/543f6cc865b8fe5fb83dd5886c73e5dff45f377f/Chapter05/EditorBasic/images/right.png -------------------------------------------------------------------------------- /Chapter05/EditorBasic/images/save.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/CPP17-By-Example/543f6cc865b8fe5fb83dd5886c73e5dff45f377f/Chapter05/EditorBasic/images/save.png -------------------------------------------------------------------------------- /Chapter05/MainWindow/DocumentWidget.h: -------------------------------------------------------------------------------- 1 | #ifndef DOCUMENTWIDGET_H 2 | #define DOCUMENTWIDGET_H 3 | 4 | #include "Listener.h" 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | class DocumentWidget : public QWidget { 11 | Q_OBJECT 12 | 13 | public: 14 | DocumentWidget(const QString& name, const QString& fileMask, 15 | QWidget* parentWidgetPtr); 16 | ~DocumentWidget(); 17 | 18 | protected: 19 | void setFilePath(QString filePath); 20 | 21 | public: 22 | void setModifiedFlag(bool flag); 23 | 24 | private: 25 | void setMainWindowHeader(); 26 | 27 | public: 28 | virtual void closeEvent(QCloseEvent* eventPtr) /*override*/; 29 | 30 | private: 31 | bool isClearOk(QString title); 32 | 33 | public slots: 34 | virtual void onNew(); 35 | virtual void onOpen(); 36 | virtual bool onSave(); 37 | virtual bool isSaveEnabled(); 38 | virtual bool onSaveAs(); 39 | virtual void onExit(); 40 | 41 | private: 42 | bool tryWriteFile(QString filePath); 43 | 44 | public: 45 | virtual bool isCutEnabled(); 46 | virtual bool isCopyEnabled(); 47 | virtual bool isPasteEnabled(); 48 | virtual bool isDeleteEnabled(); 49 | 50 | protected: 51 | virtual void newDocument() = 0; 52 | virtual bool writeFile(const QString& filePath) = 0; 53 | virtual bool readFile(const QString& filePath) = 0; 54 | 55 | public slots: 56 | virtual void onCut(); 57 | virtual void onCopy(); 58 | virtual void onPaste(); 59 | virtual void onDelete(); 60 | 61 | private: 62 | QString m_applicationName, m_fileMask, m_filePath; 63 | bool m_modifiedFlag = false; 64 | }; 65 | 66 | QPoint& operator+=(QPoint& point, const QSize& size); 67 | QPoint& operator-=(QPoint& point, const QSize& size); 68 | 69 | QRect& operator+=(QRect& rect, int size); 70 | QRect& operator-=(QRect& rect, int size); 71 | 72 | void writePoint(ofstream& outStream, const QPoint& point); 73 | void readPoint(ifstream& inStream, QPoint& point); 74 | 75 | void writeColor(ofstream& outStream, const QColor& color); 76 | void readColor(ifstream& inStream, QColor& color); 77 | 78 | QRect makeRect(const QPoint& centerPoint, int halfSide); 79 | #endif // DOCUMENTWIDGET_H 80 | -------------------------------------------------------------------------------- /Chapter05/MainWindow/Listener.h: -------------------------------------------------------------------------------- 1 | #ifndef LISTENER_H 2 | #define LISTENER_H 3 | 4 | #include 5 | 6 | #define DEFINE_LISTENER(BaseClass, Listener) \ 7 | friend bool Listener(QObject* baseObjectPtr) { \ 8 | return ((BaseClass*) baseObjectPtr)->Listener(); \ 9 | } \ 10 | bool Listener() \ 11 | 12 | #define LISTENER(Listener) (&::Listener) 13 | typedef bool (*Listener)(QObject*); 14 | 15 | #endif // LISTENER_H 16 | -------------------------------------------------------------------------------- /Chapter05/MainWindow/MainWindow.h: -------------------------------------------------------------------------------- 1 | #ifndef MAINWINDOW_H 2 | #define MAINWINDOW_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include "Listener.h" 10 | #include "DocumentWidget.h" 11 | 12 | class MainWindow : public QMainWindow { 13 | Q_OBJECT 14 | 15 | public: 16 | MainWindow(QWidget *parentWidgetPtr = nullptr); 17 | ~MainWindow(); 18 | 19 | protected: 20 | void addFileMenu(); 21 | void addEditMenu(); 22 | 23 | protected: 24 | /* void addAction(QMenu* menuPtr, QString text, 25 | const char* onSelectPtr, QString iconName = QString(), 26 | QToolBar* toolBarPtr = nullptr, 27 | QString statusTip = QString(), 28 | Listener enableListener = nullptr, 29 | Listener checkListener = nullptr, 30 | QActionGroup* groupPtr = nullptr); 31 | */ 32 | void addAction(QMenu* menuPtr, QString text, 33 | const char* onSelectPtr, 34 | QKeySequence acceleratortKey = 0, 35 | QString iconName = QString(), 36 | QToolBar* toolBarPtr = nullptr, 37 | QString statusTip = QString(), 38 | Listener enableListener = nullptr, 39 | Listener checkListener = nullptr, 40 | QActionGroup* groupPtr = nullptr); 41 | 42 | DEFINE_LISTENER(DocumentWidget, isSaveEnabled); 43 | DEFINE_LISTENER(DocumentWidget, isCutEnabled); 44 | DEFINE_LISTENER(DocumentWidget, isCopyEnabled); 45 | DEFINE_LISTENER(DocumentWidget, isPasteEnabled); 46 | DEFINE_LISTENER(DocumentWidget, isDeleteEnabled); 47 | 48 | public slots: 49 | void onMenuShow(); 50 | 51 | private: 52 | QMap> m_enableMap, 53 | m_checkMap; 54 | }; 55 | 56 | #endif // MAINWINDOW_H 57 | -------------------------------------------------------------------------------- /Chapter06/ClockAdvanced/Clock.h: -------------------------------------------------------------------------------- 1 | #ifndef CLOCK_H 2 | #define CLOCK_H 3 | 4 | #include 5 | #include 6 | 7 | class Clock : public QWidget { 8 | Q_OBJECT 9 | 10 | public: 11 | Clock(QWidget *parentWidget = nullptr); 12 | void paintEvent(QPaintEvent *eventPtr) override; 13 | 14 | private: 15 | QTimer m_timer; 16 | }; 17 | 18 | #endif // CLOCK_H 19 | -------------------------------------------------------------------------------- /Chapter06/ClockAdvanced/ClockAdvanced.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2017-06-06T20:55:58 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = ClockAdvanced 12 | TEMPLATE = app 13 | 14 | # The following define makes your compiler emit warnings if you use 15 | # any feature of Qt which as been marked as deprecated (the exact warnings 16 | # depend on your compiler). Please consult the documentation of the 17 | # deprecated API in order to know how to port your code away from it. 18 | DEFINES += QT_DEPRECATED_WARNINGS 19 | 20 | # You can also make your code fail to compile if you use deprecated APIs. 21 | # In order to do so, uncomment the following line. 22 | # You can also select to disable deprecated APIs only up to a certain version of Qt. 23 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 24 | 25 | 26 | SOURCES += \ 27 | Clock.cpp \ 28 | Main.cpp 29 | 30 | HEADERS += \ 31 | Clock.h 32 | -------------------------------------------------------------------------------- /Chapter06/ClockAdvanced/Main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Clock.h" 3 | 4 | int main(int argc, char *argv[]) { 5 | QApplication application(argc, argv); 6 | Clock Clock; 7 | Clock.show(); 8 | return application.exec(); 9 | } 10 | -------------------------------------------------------------------------------- /Chapter06/DrawingAdvanced/DrawingAdvanced.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2017-06-06T20:46:21 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = DrawingAdvanced 12 | TEMPLATE = app 13 | 14 | # The following define makes your compiler emit warnings if you use 15 | # any feature of Qt which as been marked as deprecated (the exact warnings 16 | # depend on your compiler). Please consult the documentation of the 17 | # deprecated API in order to know how to port your code away from it. 18 | DEFINES += QT_DEPRECATED_WARNINGS 19 | 20 | # You can also make your code fail to compile if you use deprecated APIs. 21 | # In order to do so, uncomment the following line. 22 | # You can also select to disable deprecated APIs only up to a certain version of Qt. 23 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 24 | 25 | CONFIG += no_batch 26 | 27 | SOURCES += \ 28 | Ellipse.cpp \ 29 | Figure.cpp \ 30 | Global.cpp \ 31 | Line.cpp \ 32 | Main.cpp \ 33 | Rectangle.cpp \ 34 | DrawingWidget.cpp \ 35 | ../MainWindow/MainWindow.cpp \ 36 | DrawingWindow.cpp \ 37 | ../MainWindow/DocumentWidget.cpp 38 | 39 | HEADERS += \ 40 | Ellipse.h \ 41 | Figure.h \ 42 | Global.h \ 43 | Line.h \ 44 | Rectangle.h \ 45 | DrawingWidget.h \ 46 | ../MainWindow/MainWindow.h \ 47 | DrawingWindow.h \ 48 | ../MainWindow/DocumentWidget.h \ 49 | ../MainWindow/Listener.h 50 | 51 | RESOURCES += \ 52 | DrawingAdvanced.qrc 53 | -------------------------------------------------------------------------------- /Chapter06/DrawingAdvanced/DrawingAdvanced.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | images/qt.png 4 | images/new.png 5 | images/open.png 6 | images/save.png 7 | images/cut.png 8 | images/copy.png 9 | images/paste.png 10 | images/left.png 11 | images/center.png 12 | images/right.png 13 | images/delete.png 14 | 15 | 16 | -------------------------------------------------------------------------------- /Chapter06/DrawingAdvanced/DrawingWidget.h: -------------------------------------------------------------------------------- 1 | #ifndef DRAWINGWIDGET_H 2 | #define DRAWINGWIDGET_H 3 | 4 | #include "..\\MainWindow\\MainWindow.h" 5 | #include "..\\MainWindow\\DocumentWidget.h" 6 | #include "Figure.h" 7 | 8 | class DrawingWidget : public DocumentWidget { 9 | Q_OBJECT 10 | 11 | public: 12 | DrawingWidget(QWidget* parentWidget); 13 | ~DrawingWidget(); 14 | 15 | public: 16 | void mousePressEvent(QMouseEvent *eventPtr) override; 17 | void mouseMoveEvent(QMouseEvent *eventPtr) override; 18 | void mouseReleaseEvent(QMouseEvent *eventPtr) override; 19 | void mouseDoubleClickEvent(QMouseEvent *eventPtr) override; 20 | void paintEvent(QPaintEvent *eventPtr) override; 21 | 22 | private: 23 | void newDocument(void); 24 | bool writeFile(const QString& filePath); 25 | bool readFile(const QString& filePath); 26 | Figure* createFigure(FigureId figureId); 27 | 28 | public slots: 29 | bool isCopyEnabled(); 30 | void onCopy(void); 31 | bool isPasteEnabled(); 32 | void onPaste(void); 33 | void onDelete(void); 34 | void onColor(void); 35 | 36 | DEFINE_LISTENER(DrawingWidget, isFillEnabled); 37 | void onFill(void); 38 | 39 | DEFINE_LISTENER(DrawingWidget, isModifyChecked); 40 | void onModify(void); 41 | 42 | DEFINE_LISTENER(DrawingWidget, isLineChecked); 43 | void onLine(void); 44 | 45 | DEFINE_LISTENER(DrawingWidget, isRectangleChecked); 46 | void onRectangle(void); 47 | 48 | DEFINE_LISTENER(DrawingWidget, isEllipseChecked); 49 | void onEllipse(void); 50 | 51 | private: 52 | enum ApplicationMode {Idle, ModifySingle, 53 | /*MoveMultiple, */ModifyRectangle}; 54 | ApplicationMode m_applicationMode = Idle; 55 | void setApplicationMode(ApplicationMode mode); 56 | 57 | enum ActionMode {Modify, Add}; 58 | ActionMode m_actionMode = Add; 59 | 60 | QColor m_nextColor = Qt::black; 61 | bool m_nextFilled = false; 62 | 63 | Figure* m_clickedFigurePtr; 64 | FigureId m_addFigureId = LineId; 65 | 66 | QPoint m_mousePoint; 67 | QList m_figurePtrList, m_copyPtrList; 68 | QRect m_insideRectangle; 69 | }; 70 | 71 | #endif // DRAWINGWIDGET_H 72 | -------------------------------------------------------------------------------- /Chapter06/DrawingAdvanced/DrawingWindow.cpp: -------------------------------------------------------------------------------- 1 | #include "..\\MainWindow\\DocumentWidget.h" 2 | #include "DrawingWindow.h" 3 | 4 | DrawingWindow::DrawingWindow(QWidget *parentWidget /*= nullptr*/) 5 | :MainWindow(parentWidget) { 6 | resize(1000, 500); 7 | 8 | m_drawingWidgetPtr = new DrawingWidget(this); 9 | setCentralWidget(m_drawingWidgetPtr); 10 | addFileMenu(); 11 | addEditMenu(); 12 | 13 | { QMenu* formatMenuPtr = menuBar()->addMenu(tr("F&ormat")); 14 | connect(formatMenuPtr, SIGNAL(aboutToShow()), 15 | this, SLOT(onMenuShow())); 16 | 17 | addAction(formatMenuPtr, tr("&Color"), 18 | SLOT(onColor()), QKeySequence(Qt::ALT + Qt::Key_C), 19 | QString(), nullptr, tr("Figure Color")); 20 | 21 | addAction(formatMenuPtr, tr("&Fill"), 22 | SLOT(onFill()), QKeySequence(Qt::CTRL + Qt::Key_F), 23 | QString(), nullptr, tr("Figure Fill"), 24 | LISTENER(isFillEnabled)); 25 | 26 | m_figureGroupPtr = new QActionGroup(this); 27 | addAction(formatMenuPtr, tr("&Modify"), 28 | SLOT(onModify()), 29 | QKeySequence(Qt::CTRL + Qt::Key_M), 30 | QString(), nullptr, tr("Modify Figure"), nullptr, 31 | LISTENER(isModifyChecked), m_figureGroupPtr); 32 | 33 | { QMenu* alignmentMenuPtr = 34 | formatMenuPtr->addMenu(tr("&Figure")); 35 | connect(alignmentMenuPtr, SIGNAL(aboutToShow()), 36 | this, SLOT(onMenuShow())); 37 | 38 | addAction(alignmentMenuPtr, tr("&Line"), 39 | SLOT(onLine()), 40 | QKeySequence(Qt::CTRL + Qt::Key_L), 41 | QString(), nullptr, tr("Line Figure"), nullptr, 42 | LISTENER(isLineChecked), m_figureGroupPtr); 43 | 44 | addAction(alignmentMenuPtr, tr("&Rectangle"), 45 | SLOT(onRectangle()), 46 | QKeySequence(Qt::CTRL + Qt::Key_R), 47 | QString(), nullptr, tr("Rectangle Figure"), 48 | nullptr, LISTENER(isRectangleChecked), 49 | m_figureGroupPtr); 50 | 51 | addAction(alignmentMenuPtr, tr("&Ellipse"), 52 | SLOT(onEllipse()), 53 | QKeySequence(Qt::CTRL + Qt::Key_E), 54 | QString(), nullptr, tr("Ellipse Figure"), nullptr, 55 | LISTENER(isEllipseChecked), m_figureGroupPtr); 56 | } 57 | } 58 | } 59 | 60 | DrawingWindow::~DrawingWindow() { 61 | delete m_figureGroupPtr; 62 | } 63 | -------------------------------------------------------------------------------- /Chapter06/DrawingAdvanced/DrawingWindow.h: -------------------------------------------------------------------------------- 1 | #ifndef DRAWINGWINDOW_H 2 | #define DRAWINGWINDOW_H 3 | 4 | #include 5 | #include 6 | 7 | #include "..\\MainWindow\\MainWindow.h" 8 | #include "DrawingWidget.h" 9 | 10 | class DrawingWindow : public MainWindow { 11 | Q_OBJECT 12 | 13 | public: 14 | DrawingWindow(QWidget *parentWidget = nullptr); 15 | ~DrawingWindow(); 16 | 17 | public: 18 | void closeEvent(QCloseEvent *eventPtr) override 19 | { m_drawingWidgetPtr->closeEvent(eventPtr); } 20 | 21 | private: 22 | DrawingWidget* m_drawingWidgetPtr; 23 | QActionGroup* m_figureGroupPtr; 24 | }; 25 | 26 | #endif // DRAWINGWINDOW_H 27 | -------------------------------------------------------------------------------- /Chapter06/DrawingAdvanced/Ellipse.h: -------------------------------------------------------------------------------- 1 | #ifndef ELLIPSE_H 2 | #define ELLIPSE_H 3 | 4 | #include "Rectangle.h" 5 | 6 | class EllipseX : public RectangleX { 7 | public: 8 | EllipseX(); 9 | FigureId getId() const {return EllipseId;} 10 | 11 | EllipseX(const EllipseX& ellipse); 12 | Figure* clone() const; 13 | 14 | bool isClick(QPoint mousePoint); 15 | void modify(QSize distance); 16 | void draw(QPainter& painter) const; 17 | 18 | private: 19 | enum {CreateEllipse, LeftPoint, TopPoint, RightPoint, 20 | BottomPoint, MoveEllipse} m_ellipseMode; 21 | }; 22 | 23 | #endif 24 | -------------------------------------------------------------------------------- /Chapter06/DrawingAdvanced/Figure.cpp: -------------------------------------------------------------------------------- 1 | #include "..\\MainWindow\\DocumentWidget.h" 2 | #include "Figure.h" 3 | 4 | const int Figure::Tolerance(6); 5 | 6 | Figure::Figure() { 7 | // Empty. 8 | } 9 | 10 | bool Figure::write(ofstream& outStream) const { 11 | writeColor(outStream, m_color); 12 | outStream.write((char*) &m_filled, sizeof m_filled); 13 | return ((bool) outStream); 14 | } 15 | 16 | bool Figure::read(ifstream& inStream) { 17 | readColor(inStream, m_color); 18 | inStream.read((char*) &m_filled, sizeof m_filled); 19 | return ((bool) inStream); 20 | } 21 | -------------------------------------------------------------------------------- /Chapter06/DrawingAdvanced/Figure.h: -------------------------------------------------------------------------------- 1 | #ifndef FIGURE_H 2 | #define FIGURE_H 3 | 4 | enum FigureId {LineId, RectangleId, EllipseId}; 5 | 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | class Figure { 11 | public: 12 | Figure(); 13 | virtual Figure* clone() const = 0; 14 | 15 | virtual FigureId getId() const = 0; 16 | virtual void initializePoints(QPoint point) = 0; 17 | 18 | virtual bool isClick(QPoint mousePoint) = 0; 19 | virtual bool isInside(QRect area) = 0; 20 | virtual void doubleClick(QPoint mousePoint) = 0; 21 | 22 | virtual void modify(QSize distance) = 0; 23 | virtual void move(QSize distance) = 0; 24 | 25 | virtual void draw(QPainter &painter) const = 0; 26 | 27 | virtual bool write(ofstream& outStream) const; 28 | virtual bool read(ifstream& inStream); 29 | 30 | bool marked() const {return m_marked;} 31 | bool& marked() {return m_marked;} 32 | 33 | const QColor& color() const {return m_color;} 34 | QColor& color() {return m_color;} 35 | 36 | virtual bool filled() const {return m_filled;} 37 | virtual bool& filled() {return m_filled;} 38 | 39 | static const int Tolerance; 40 | 41 | private: 42 | QColor m_color; 43 | bool m_marked = false, m_filled = false; 44 | }; 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /Chapter06/DrawingAdvanced/Line.h: -------------------------------------------------------------------------------- 1 | #ifndef LINE_H 2 | #define LINE_H 3 | 4 | #include 5 | using namespace std; 6 | 7 | #include "Figure.h" 8 | 9 | class Line : public Figure { 10 | public: 11 | Line(); 12 | FigureId getId() const {return LineId;} 13 | 14 | Line(const Line& line); 15 | Figure* clone() const; 16 | 17 | void initializePoints(QPoint point); 18 | bool isClick(QPoint mousePoint); 19 | bool isInside(QRect rectangleArea); 20 | void doubleClick(QPoint /* mousePoint */) {/* Empty. */} 21 | 22 | void modify(QSize distance); 23 | void move(QSize distance); 24 | 25 | QRect area() const; 26 | void draw(QPainter& painter) const; 27 | 28 | bool write(ofstream& outStream) const; 29 | bool read(ifstream& inStream); 30 | 31 | private: 32 | enum {FirstPoint, LastPoint, MoveLine} m_lineMode; 33 | QPoint m_firstPoint, m_lastPoint; 34 | 35 | static bool isPointInLine(QPoint m_firstPoint, 36 | QPoint m_lastPoint, QPoint point); 37 | }; 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /Chapter06/DrawingAdvanced/Main.cpp: -------------------------------------------------------------------------------- 1 | #include "DrawingWindow.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) { 5 | QApplication application(argc, argv); 6 | DrawingWindow drawingWindow; 7 | drawingWindow.show(); 8 | return application.exec(); 9 | } 10 | -------------------------------------------------------------------------------- /Chapter06/DrawingAdvanced/Rectangle.h: -------------------------------------------------------------------------------- 1 | #ifndef RECTANGLE_H 2 | #define RECTANGLE_H 3 | 4 | #include 5 | using namespace std; 6 | 7 | #include "Figure.h" 8 | 9 | class RectangleX : public Figure { 10 | public: 11 | RectangleX(); 12 | virtual FigureId getId() const {return RectangleId;} 13 | 14 | RectangleX(const RectangleX& rectangle); 15 | Figure* clone() const; 16 | 17 | virtual void initializePoints(QPoint point); 18 | 19 | virtual bool isClick(QPoint mousePoint); 20 | virtual bool isInside(QRect rectangleArea); 21 | virtual void doubleClick(QPoint mousePoint); 22 | 23 | virtual void modify(QSize distance); 24 | virtual void move(QSize distance); 25 | 26 | virtual QRect area() const; 27 | virtual void draw(QPainter& painter) const; 28 | 29 | virtual bool write(ofstream& outStream) const; 30 | virtual bool read(ifstream& inStream); 31 | 32 | private: 33 | enum {TopLeftPoint, TopRightPoint, BottomRightPoint, 34 | BottomLeftPoint, MoveRectangle} m_rectangleMode; 35 | 36 | protected: 37 | QPoint m_topLeft, m_bottomRight; 38 | }; 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /Chapter06/DrawingAdvanced/images/center.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/CPP17-By-Example/543f6cc865b8fe5fb83dd5886c73e5dff45f377f/Chapter06/DrawingAdvanced/images/center.png -------------------------------------------------------------------------------- /Chapter06/DrawingAdvanced/images/copy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/CPP17-By-Example/543f6cc865b8fe5fb83dd5886c73e5dff45f377f/Chapter06/DrawingAdvanced/images/copy.png -------------------------------------------------------------------------------- /Chapter06/DrawingAdvanced/images/cut.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/CPP17-By-Example/543f6cc865b8fe5fb83dd5886c73e5dff45f377f/Chapter06/DrawingAdvanced/images/cut.png -------------------------------------------------------------------------------- /Chapter06/DrawingAdvanced/images/delete.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/CPP17-By-Example/543f6cc865b8fe5fb83dd5886c73e5dff45f377f/Chapter06/DrawingAdvanced/images/delete.png -------------------------------------------------------------------------------- /Chapter06/DrawingAdvanced/images/left.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/CPP17-By-Example/543f6cc865b8fe5fb83dd5886c73e5dff45f377f/Chapter06/DrawingAdvanced/images/left.png -------------------------------------------------------------------------------- /Chapter06/DrawingAdvanced/images/new.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/CPP17-By-Example/543f6cc865b8fe5fb83dd5886c73e5dff45f377f/Chapter06/DrawingAdvanced/images/new.png -------------------------------------------------------------------------------- /Chapter06/DrawingAdvanced/images/open.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/CPP17-By-Example/543f6cc865b8fe5fb83dd5886c73e5dff45f377f/Chapter06/DrawingAdvanced/images/open.png -------------------------------------------------------------------------------- /Chapter06/DrawingAdvanced/images/paste.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/CPP17-By-Example/543f6cc865b8fe5fb83dd5886c73e5dff45f377f/Chapter06/DrawingAdvanced/images/paste.png -------------------------------------------------------------------------------- /Chapter06/DrawingAdvanced/images/qt.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/CPP17-By-Example/543f6cc865b8fe5fb83dd5886c73e5dff45f377f/Chapter06/DrawingAdvanced/images/qt.png -------------------------------------------------------------------------------- /Chapter06/DrawingAdvanced/images/right.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/CPP17-By-Example/543f6cc865b8fe5fb83dd5886c73e5dff45f377f/Chapter06/DrawingAdvanced/images/right.png -------------------------------------------------------------------------------- /Chapter06/DrawingAdvanced/images/save.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/CPP17-By-Example/543f6cc865b8fe5fb83dd5886c73e5dff45f377f/Chapter06/DrawingAdvanced/images/save.png -------------------------------------------------------------------------------- /Chapter06/EditorAdvanced/Caret.cpp: -------------------------------------------------------------------------------- 1 | #include "Caret.h" 2 | #include 3 | 4 | Caret::Caret(QWidget *parentWidgetPtr) 5 | :m_parentWidgetPtr(parentWidgetPtr) { 6 | m_timer.setParent(this); 7 | connect(&m_timer, SIGNAL(timeout()), this, SLOT(onTimer())); 8 | m_timer.start(500); 9 | } 10 | 11 | void Caret::show() { 12 | m_visible = true; 13 | m_parentWidgetPtr->update(m_rect); 14 | } 15 | 16 | void Caret::hide() { 17 | m_visible = false; 18 | m_parentWidgetPtr->update(m_rect); 19 | } 20 | 21 | void Caret::set(QRect rect) { 22 | m_rect = rect; 23 | m_rect.setWidth(1); 24 | m_parentWidgetPtr->update(m_rect); 25 | } 26 | 27 | void Caret::onTimer(void) { 28 | m_black = !m_black; 29 | m_parentWidgetPtr->update(m_rect); 30 | } 31 | 32 | void Caret::paint(QPainter* painterPtr) { 33 | if (m_visible && m_black) { 34 | painterPtr->save(); 35 | painterPtr->setPen(Qt::NoPen); 36 | painterPtr->setBrush(Qt::black); 37 | painterPtr->drawRect(m_rect); 38 | painterPtr->restore(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Chapter06/EditorAdvanced/Caret.h: -------------------------------------------------------------------------------- 1 | #ifndef CARET_H 2 | #define CARET_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | class Caret : public QObject { 9 | Q_OBJECT 10 | 11 | public: 12 | Caret(QWidget *parentWidgetPtr = nullptr); 13 | 14 | void show(); 15 | void hide(); 16 | void set(QRect rect); 17 | void paint(QPainter* painterPtr); 18 | 19 | public slots: 20 | void onTimer(void); 21 | 22 | private: 23 | QWidget* m_parentWidgetPtr; 24 | bool m_visible, m_black; 25 | QRect m_rect; 26 | QTimer m_timer; 27 | }; 28 | 29 | #endif // CARET_H 30 | -------------------------------------------------------------------------------- /Chapter06/EditorAdvanced/EditorAdvanced.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2017-06-06T21:07:45 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = EditorAdvanced 12 | TEMPLATE = app 13 | 14 | # The following define makes your compiler emit warnings if you use 15 | # any feature of Qt which as been marked as deprecated (the exact warnings 16 | # depend on your compiler). Please consult the documentation of the 17 | # deprecated API in order to know how to port your code away from it. 18 | DEFINES += QT_DEPRECATED_WARNINGS 19 | 20 | # You can also make your code fail to compile if you use deprecated APIs. 21 | # In order to do so, uncomment the following line. 22 | # You can also select to disable deprecated APIs only up to a certain version of Qt. 23 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 24 | 25 | CONFIG += no_batch 26 | 27 | SOURCES += \ 28 | Caret.cpp \ 29 | EditorWidget.cpp \ 30 | Main.cpp \ 31 | ../MainWindow/MainWindow.cpp \ 32 | EditorWindow.cpp \ 33 | ../MainWindow/DocumentWidget.cpp 34 | 35 | HEADERS += \ 36 | Caret.h \ 37 | EditorWidget.h \ 38 | EditorWindow.h \ 39 | ../MainWindow/MainWindow.h \ 40 | ../MainWindow/DocumentWidget.h \ 41 | ../MainWindow/Listener.h 42 | 43 | RESOURCES += \ 44 | EditorAdvanced.qrc 45 | -------------------------------------------------------------------------------- /Chapter06/EditorAdvanced/EditorAdvanced.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | images/qt.png 4 | images/new.png 5 | images/open.png 6 | images/save.png 7 | images/cut.png 8 | images/copy.png 9 | images/paste.png 10 | images/left.png 11 | images/center.png 12 | images/right.png 13 | 14 | 15 | -------------------------------------------------------------------------------- /Chapter06/EditorAdvanced/EditorWidget.h: -------------------------------------------------------------------------------- 1 | #ifndef EDITORWIDGET_H 2 | #define EDITORWIDGET_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include "Caret.h" 10 | 11 | #include "..\\MainWindow\\Listener.h" 12 | #include "..\\MainWindow\\DocumentWidget.h" 13 | 14 | class EditorWidget : public DocumentWidget { 15 | Q_OBJECT 16 | 17 | public: 18 | EditorWidget(QWidget* parentWidgetPtr); 19 | void keyPressEvent(QKeyEvent* eventPtr) override; 20 | 21 | private: 22 | void keyEditPressEvent(QKeyEvent* eventPtr); 23 | void keyMarkPressEvent(QKeyEvent* eventPtr); 24 | 25 | public: 26 | void mousePressEvent(QMouseEvent* eventPtr) override; 27 | void mouseMoveEvent(QMouseEvent* eventPtr) override; 28 | void mouseReleaseEvent(QMouseEvent* eventPtr) override; 29 | 30 | private: 31 | int mouseToIndex(QPoint point); 32 | 33 | public: 34 | void paintEvent(QPaintEvent* eventPtr) override; 35 | void resizeEvent(QResizeEvent* eventPtr) override; 36 | 37 | private: 38 | void newDocument(void) override; 39 | bool writeFile(const QString& filePath) override; 40 | bool readFile(const QString& filePath) override; 41 | 42 | public slots: 43 | bool isCopyEnabled() override; 44 | void onCopy(void) override; 45 | bool isPasteEnabled()override; 46 | void onPaste(void) override; 47 | void onDelete(void) override; 48 | 49 | DEFINE_LISTENER(EditorWidget, isFontEnabled); 50 | void onFont(void); 51 | 52 | DEFINE_LISTENER(EditorWidget, isLeftChecked); 53 | void onLeft(void); 54 | DEFINE_LISTENER(EditorWidget, isCenterChecked); 55 | void onCenter(void); 56 | DEFINE_LISTENER(EditorWidget, isRightChecked); 57 | void onRight(void); 58 | 59 | private: 60 | void setCaret(); 61 | void simulateMouseClick(int x, int y); 62 | 63 | private: 64 | enum Mode {Edit, Mark} m_mode; 65 | enum Alignment {Left, Center, Right} m_alignment; 66 | int m_editIndex, m_firstIndex, m_lastIndex; 67 | Caret m_caret; 68 | void calculate(); 69 | QList> m_lineList; 70 | QString m_editorText, m_copyText; 71 | QList m_rectList; 72 | int m_fontHeight; 73 | QFont m_textFont; 74 | Qt::MouseButton m_button; 75 | }; 76 | 77 | #endif // EDITORWIDGET_H 78 | -------------------------------------------------------------------------------- /Chapter06/EditorAdvanced/EditorWindow.cpp: -------------------------------------------------------------------------------- 1 | #include "EditorWindow.h" 2 | #include 3 | 4 | EditorWindow::EditorWindow(QWidget *parentWidgetPtr /*= nullptr*/) 5 | :MainWindow(parentWidgetPtr) { 6 | resize(1000, 500); 7 | 8 | m_editorWidgetPtr = new EditorWidget(this); 9 | setCentralWidget(m_editorWidgetPtr); 10 | addFileMenu(); 11 | addEditMenu(); 12 | 13 | { QMenu* formatMenuPtr = menuBar()->addMenu(tr("F&ormat")); 14 | connect(formatMenuPtr, SIGNAL(aboutToShow()), this, 15 | SLOT(onMenuShow())); 16 | addAction(formatMenuPtr, tr("&Font"), SLOT(onFont()), 17 | 0, QString(), nullptr, QString(), 18 | LISTENER(isFontEnabled)); 19 | 20 | { QMenu* alignmentMenuPtr = 21 | formatMenuPtr->addMenu(tr("&Alignment")); 22 | connect(alignmentMenuPtr, SIGNAL(aboutToShow()), 23 | this, SLOT(onMenuShow())); 24 | QToolBar* alignmentToolBarPtr = addToolBar(tr("Alignment")); 25 | m_alignmentGroupPtr = new QActionGroup(this); 26 | 27 | addAction(alignmentMenuPtr, tr("&Left"), SLOT(onLeft()), 28 | QKeySequence(Qt::ALT + Qt::Key_L), tr("left"), 29 | alignmentToolBarPtr, tr("Left-aligned text"), 30 | nullptr, LISTENER(isLeftChecked)); 31 | addAction(alignmentMenuPtr, tr("&Center"), 32 | SLOT(onCenter()), 33 | QKeySequence(Qt::ALT + Qt::Key_C), 34 | tr("center"), alignmentToolBarPtr, 35 | tr("Center-aligned text"), nullptr, 36 | LISTENER(isCenterChecked)); 37 | addAction(alignmentMenuPtr, tr("&Right"), 38 | SLOT(onRight()), 39 | QKeySequence(Qt::ALT + Qt::Key_R), 40 | tr("right"), alignmentToolBarPtr, 41 | tr("Right-aligned text"), nullptr, 42 | LISTENER(isRightChecked)); 43 | } 44 | } 45 | 46 | m_editorWidgetPtr->setModifiedFlag(false); 47 | } 48 | 49 | EditorWindow::~EditorWindow() { 50 | delete m_alignmentGroupPtr; 51 | } 52 | 53 | void EditorWindow::keyPressEvent(QKeyEvent* eventPtr) { 54 | m_editorWidgetPtr->keyPressEvent(eventPtr); 55 | } 56 | 57 | void EditorWindow::closeEvent(QCloseEvent* eventPtr) { 58 | m_editorWidgetPtr->closeEvent(eventPtr); 59 | } 60 | -------------------------------------------------------------------------------- /Chapter06/EditorAdvanced/EditorWindow.h: -------------------------------------------------------------------------------- 1 | #ifndef EDITORWINDOW_H 2 | #define EDITORWINDOW_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include "..\\MainWindow\\MainWindow.h" 10 | #include "EditorWidget.h" 11 | 12 | class EditorWindow : public MainWindow { 13 | Q_OBJECT 14 | 15 | public: 16 | EditorWindow(QWidget *parentWidgetPtr = nullptr); 17 | ~EditorWindow(); 18 | 19 | protected: 20 | void keyPressEvent(QKeyEvent* eventPtr) override; 21 | void closeEvent(QCloseEvent* eventPtr) override; 22 | 23 | private: 24 | EditorWidget* m_editorWidgetPtr; 25 | QActionGroup* m_alignmentGroupPtr; 26 | }; 27 | 28 | #endif // EDITORWINDOW_H 29 | -------------------------------------------------------------------------------- /Chapter06/EditorAdvanced/Main.cpp: -------------------------------------------------------------------------------- 1 | #include "EditorWindow.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) { 5 | QApplication application(argc, argv); 6 | EditorWindow editorWindow; 7 | editorWindow.show(); 8 | return application.exec(); 9 | } 10 | -------------------------------------------------------------------------------- /Chapter06/EditorAdvanced/images/center.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/CPP17-By-Example/543f6cc865b8fe5fb83dd5886c73e5dff45f377f/Chapter06/EditorAdvanced/images/center.png -------------------------------------------------------------------------------- /Chapter06/EditorAdvanced/images/copy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/CPP17-By-Example/543f6cc865b8fe5fb83dd5886c73e5dff45f377f/Chapter06/EditorAdvanced/images/copy.png -------------------------------------------------------------------------------- /Chapter06/EditorAdvanced/images/cut.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/CPP17-By-Example/543f6cc865b8fe5fb83dd5886c73e5dff45f377f/Chapter06/EditorAdvanced/images/cut.png -------------------------------------------------------------------------------- /Chapter06/EditorAdvanced/images/left.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/CPP17-By-Example/543f6cc865b8fe5fb83dd5886c73e5dff45f377f/Chapter06/EditorAdvanced/images/left.png -------------------------------------------------------------------------------- /Chapter06/EditorAdvanced/images/new.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/CPP17-By-Example/543f6cc865b8fe5fb83dd5886c73e5dff45f377f/Chapter06/EditorAdvanced/images/new.png -------------------------------------------------------------------------------- /Chapter06/EditorAdvanced/images/open.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/CPP17-By-Example/543f6cc865b8fe5fb83dd5886c73e5dff45f377f/Chapter06/EditorAdvanced/images/open.png -------------------------------------------------------------------------------- /Chapter06/EditorAdvanced/images/paste.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/CPP17-By-Example/543f6cc865b8fe5fb83dd5886c73e5dff45f377f/Chapter06/EditorAdvanced/images/paste.png -------------------------------------------------------------------------------- /Chapter06/EditorAdvanced/images/qt.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/CPP17-By-Example/543f6cc865b8fe5fb83dd5886c73e5dff45f377f/Chapter06/EditorAdvanced/images/qt.png -------------------------------------------------------------------------------- /Chapter06/EditorAdvanced/images/right.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/CPP17-By-Example/543f6cc865b8fe5fb83dd5886c73e5dff45f377f/Chapter06/EditorAdvanced/images/right.png -------------------------------------------------------------------------------- /Chapter06/EditorAdvanced/images/save.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/CPP17-By-Example/543f6cc865b8fe5fb83dd5886c73e5dff45f377f/Chapter06/EditorAdvanced/images/save.png -------------------------------------------------------------------------------- /Chapter06/MainWindow/DocumentWidget.h: -------------------------------------------------------------------------------- 1 | #ifndef DOCUMENTWIDGET_H 2 | #define DOCUMENTWIDGET_H 3 | 4 | #include "Listener.h" 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | class DocumentWidget : public QWidget { 11 | Q_OBJECT 12 | 13 | public: 14 | DocumentWidget(const QString& name, const QString& fileMask, 15 | QWidget* parentWidgetPtr); 16 | ~DocumentWidget(); 17 | 18 | protected: 19 | void setFilePath(QString filePath); 20 | 21 | public: 22 | void setModifiedFlag(bool flag); 23 | 24 | private: 25 | void setMainWindowHeader(); 26 | 27 | public: 28 | virtual void closeEvent(QCloseEvent* eventPtr) /*override*/; 29 | 30 | private: 31 | bool isClearOk(QString title); 32 | 33 | public slots: 34 | virtual void onNew(); 35 | virtual void onOpen(); 36 | virtual bool onSave(); 37 | virtual bool isSaveEnabled(); 38 | virtual bool onSaveAs(); 39 | virtual void onExit(); 40 | 41 | private: 42 | bool tryWriteFile(QString filePath); 43 | 44 | public: 45 | virtual bool isCutEnabled(); 46 | virtual bool isCopyEnabled(); 47 | virtual bool isPasteEnabled(); 48 | virtual bool isDeleteEnabled(); 49 | 50 | protected: 51 | virtual void newDocument() = 0; 52 | virtual bool writeFile(const QString& filePath) = 0; 53 | virtual bool readFile(const QString& filePath) = 0; 54 | 55 | public slots: 56 | virtual void onCut(); 57 | virtual void onCopy(); 58 | virtual void onPaste(); 59 | virtual void onDelete(); 60 | 61 | private: 62 | QString m_applicationName, m_fileMask, m_filePath; 63 | bool m_modifiedFlag = false; 64 | }; 65 | 66 | QPoint& operator+=(QPoint& point, const QSize& size); 67 | QPoint& operator-=(QPoint& point, const QSize& size); 68 | 69 | QRect& operator+=(QRect& rect, int size); 70 | QRect& operator-=(QRect& rect, int size); 71 | 72 | void writePoint(ofstream& outStream, const QPoint& point); 73 | void readPoint(ifstream& inStream, QPoint& point); 74 | 75 | void writeColor(ofstream& outStream, const QColor& color); 76 | void readColor(ifstream& inStream, QColor& color); 77 | 78 | QRect makeRect(const QPoint& centerPoint, int halfSide); 79 | #endif // DOCUMENTWIDGET_H 80 | -------------------------------------------------------------------------------- /Chapter06/MainWindow/Listener.h: -------------------------------------------------------------------------------- 1 | #ifndef LISTENER_H 2 | #define LISTENER_H 3 | 4 | #include 5 | 6 | #define DEFINE_LISTENER(BaseClass, Listener) \ 7 | friend bool Listener(QObject* baseObjectPtr) { \ 8 | return ((BaseClass*) baseObjectPtr)->Listener(); \ 9 | } \ 10 | bool Listener() \ 11 | 12 | #define LISTENER(Listener) (&::Listener) 13 | typedef bool (*Listener)(QObject*); 14 | 15 | #endif // LISTENER_H 16 | -------------------------------------------------------------------------------- /Chapter06/MainWindow/MainWindow.h: -------------------------------------------------------------------------------- 1 | #ifndef MAINWINDOW_H 2 | #define MAINWINDOW_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include "Listener.h" 10 | #include "DocumentWidget.h" 11 | 12 | class MainWindow : public QMainWindow { 13 | Q_OBJECT 14 | 15 | public: 16 | MainWindow(QWidget *parentWidgetPtr = nullptr); 17 | ~MainWindow(); 18 | 19 | protected: 20 | void addFileMenu(); 21 | void addEditMenu(); 22 | 23 | protected: 24 | /* void addAction(QMenu* menuPtr, QString text, 25 | const char* onSelectPtr, QString iconName = QString(), 26 | QToolBar* toolBarPtr = nullptr, 27 | QString statusTip = QString(), 28 | Listener enableListener = nullptr, 29 | Listener checkListener = nullptr, 30 | QActionGroup* groupPtr = nullptr); 31 | */ 32 | void addAction(QMenu* menuPtr, QString text, 33 | const char* onSelectPtr, 34 | QKeySequence acceleratortKey = 0, 35 | QString iconName = QString(), 36 | QToolBar* toolBarPtr = nullptr, 37 | QString statusTip = QString(), 38 | Listener enableListener = nullptr, 39 | Listener checkListener = nullptr, 40 | QActionGroup* groupPtr = nullptr); 41 | 42 | DEFINE_LISTENER(DocumentWidget, isSaveEnabled); 43 | DEFINE_LISTENER(DocumentWidget, isCutEnabled); 44 | DEFINE_LISTENER(DocumentWidget, isCopyEnabled); 45 | DEFINE_LISTENER(DocumentWidget, isPasteEnabled); 46 | DEFINE_LISTENER(DocumentWidget, isDeleteEnabled); 47 | 48 | public slots: 49 | void onMenuShow(); 50 | 51 | private: 52 | QMap> m_enableMap, 53 | m_checkMap; 54 | }; 55 | 56 | #endif // MAINWINDOW_H 57 | -------------------------------------------------------------------------------- /Chapter07/MainWindow/DocumentWidget.h: -------------------------------------------------------------------------------- 1 | #ifndef DOCUMENTWIDGET_H 2 | #define DOCUMENTWIDGET_H 3 | 4 | #include "Listener.h" 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | class DocumentWidget : public QWidget { 11 | Q_OBJECT 12 | 13 | public: 14 | DocumentWidget(const QString& name, const QString& fileMask, 15 | QWidget* parentWidgetPtr); 16 | ~DocumentWidget(); 17 | 18 | protected: 19 | void setFilePath(QString filePath); 20 | 21 | public: 22 | void setModifiedFlag(bool flag); 23 | 24 | private: 25 | void setMainWindowHeader(); 26 | 27 | public: 28 | virtual void closeEvent(QCloseEvent* eventPtr) /*override*/; 29 | 30 | private: 31 | bool isClearOk(QString title); 32 | 33 | public slots: 34 | virtual void onNew(); 35 | virtual void onOpen(); 36 | virtual bool onSave(); 37 | virtual bool isSaveEnabled(); 38 | virtual bool onSaveAs(); 39 | virtual void onExit(); 40 | 41 | private: 42 | bool tryWriteFile(QString filePath); 43 | 44 | public: 45 | virtual bool isCutEnabled(); 46 | virtual bool isCopyEnabled(); 47 | virtual bool isPasteEnabled(); 48 | virtual bool isDeleteEnabled(); 49 | 50 | protected: 51 | virtual void newDocument() = 0; 52 | virtual bool writeFile(const QString& filePath) = 0; 53 | virtual bool readFile(const QString& filePath) = 0; 54 | 55 | public slots: 56 | virtual void onCut(); 57 | virtual void onCopy(); 58 | virtual void onPaste(); 59 | virtual void onDelete(); 60 | 61 | private: 62 | QString m_applicationName, m_fileMask, m_filePath; 63 | bool m_modifiedFlag = false; 64 | }; 65 | 66 | QPoint& operator+=(QPoint& point, const QSize& size); 67 | QPoint& operator-=(QPoint& point, const QSize& size); 68 | 69 | QRect& operator+=(QRect& rect, int size); 70 | QRect& operator-=(QRect& rect, int size); 71 | 72 | void writePoint(ofstream& outStream, const QPoint& point); 73 | void readPoint(ifstream& inStream, QPoint& point); 74 | 75 | void writeColor(ofstream& outStream, const QColor& color); 76 | void readColor(ifstream& inStream, QColor& color); 77 | 78 | QRect makeRect(const QPoint& centerPoint, int halfSide); 79 | #endif // DOCUMENTWIDGET_H 80 | -------------------------------------------------------------------------------- /Chapter07/MainWindow/GameWidget.h: -------------------------------------------------------------------------------- 1 | #ifndef GAMEWIDGET_H 2 | #define GAMEWIDGET_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #include "..\\MainWindow\\MainWindow.h" 9 | 10 | class GameWidget : public QWidget { 11 | Q_OBJECT 12 | 13 | public: 14 | GameWidget(int rows, int columns, QWidget* parentWidget); 15 | void clearGrid(); 16 | 17 | void resizeEvent(QResizeEvent *eventPtr) /*override*/; 18 | void mousePressEvent(QMouseEvent *eventPtr) /*override*/; 19 | void paintEvent(QPaintEvent *eventPtr) /*override*/; 20 | void closeEvent(QCloseEvent *eventPtr) /*override*/; 21 | 22 | virtual void mouseMark(int row, int column) = 0; 23 | virtual void drawMark(QPainter& painter, 24 | const QRect& markRect, int mark) = 0; 25 | 26 | private: 27 | bool isQuitOk(); 28 | 29 | public slots: 30 | DEFINE_LISTENER(GameWidget, isQuitEnabled); 31 | void onQuit(); 32 | void onExit(); 33 | 34 | protected: 35 | bool isGameInProgress() const {return m_gameInProgress;} 36 | void setGameInProgress(bool active) 37 | {m_gameInProgress = active;} 38 | 39 | protected: 40 | int get(int row, int column) const; 41 | void set(int row, int column, int value); 42 | 43 | private: 44 | bool m_gameInProgress = false; 45 | int m_rows, m_columns; 46 | int m_rowHeight, m_columnWidth; 47 | int* m_gameGrid; 48 | }; 49 | 50 | #endif // GAMEWIDGET_H 51 | -------------------------------------------------------------------------------- /Chapter07/MainWindow/Listener.h: -------------------------------------------------------------------------------- 1 | #ifndef LISTENER_H 2 | #define LISTENER_H 3 | 4 | #include 5 | 6 | #define DEFINE_LISTENER(BaseClass, Listener) \ 7 | friend bool Listener(QObject* baseObjectPtr) { \ 8 | return ((BaseClass*) baseObjectPtr)->Listener(); \ 9 | } \ 10 | bool Listener() \ 11 | 12 | #define LISTENER(Listener) (&::Listener) 13 | typedef bool (*Listener)(QObject*); 14 | 15 | #endif // LISTENER_H 16 | -------------------------------------------------------------------------------- /Chapter07/MainWindow/MainWindow.h: -------------------------------------------------------------------------------- 1 | #ifndef MAINWINDOW_H 2 | #define MAINWINDOW_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include "Listener.h" 10 | #include "DocumentWidget.h" 11 | 12 | class MainWindow : public QMainWindow { 13 | Q_OBJECT 14 | 15 | public: 16 | MainWindow(QWidget *parentWidgetPtr = nullptr); 17 | ~MainWindow(); 18 | 19 | protected: 20 | void addFileMenu(); 21 | void addEditMenu(); 22 | 23 | protected: 24 | /* void addAction(QMenu* menuPtr, QString text, 25 | const char* onSelectPtr, QString iconName = QString(), 26 | QToolBar* toolBarPtr = nullptr, 27 | QString statusTip = QString(), 28 | Listener enableListener = nullptr, 29 | Listener checkListener = nullptr, 30 | QActionGroup* groupPtr = nullptr); 31 | */ 32 | void addAction(QMenu* menuPtr, QString text, 33 | const char* onSelectPtr, 34 | QKeySequence acceleratortKey = 0, 35 | QString iconName = QString(), 36 | QToolBar* toolBarPtr = nullptr, 37 | QString statusTip = QString(), 38 | Listener enableListener = nullptr, 39 | Listener checkListener = nullptr, 40 | QActionGroup* groupPtr = nullptr); 41 | 42 | DEFINE_LISTENER(DocumentWidget, isSaveEnabled); 43 | DEFINE_LISTENER(DocumentWidget, isCutEnabled); 44 | DEFINE_LISTENER(DocumentWidget, isCopyEnabled); 45 | DEFINE_LISTENER(DocumentWidget, isPasteEnabled); 46 | DEFINE_LISTENER(DocumentWidget, isDeleteEnabled); 47 | 48 | public slots: 49 | void onMenuShow(); 50 | 51 | private: 52 | QMap> m_enableMap, 53 | m_checkMap; 54 | }; 55 | 56 | #endif // MAINWINDOW_H 57 | -------------------------------------------------------------------------------- /Chapter07/NoughtsAndCrossesBasic/Main.cpp: -------------------------------------------------------------------------------- 1 | #include "NaCWidget.h" 2 | #include "NaCWindow.h" 3 | #include 4 | 5 | int main(int argc, char *argv[]) { 6 | QApplication application(argc, argv); 7 | NaCWindow mainWindow; 8 | mainWindow.show(); 9 | return application.exec(); 10 | } 11 | -------------------------------------------------------------------------------- /Chapter07/NoughtsAndCrossesBasic/NaCWidget.h: -------------------------------------------------------------------------------- 1 | #ifndef NACWIDGET_H 2 | #define NACWIDGET_H 3 | 4 | #include "..\\MainWindow\\GameWidget.h" 5 | 6 | #define ROWS 26 7 | #define COLUMNS 26 8 | 9 | enum Mark {Empty = 0, Nought, Cross}; 10 | 11 | class NaCWidget : public GameWidget { 12 | Q_OBJECT 13 | 14 | public: 15 | NaCWidget(QWidget* parentWidget); 16 | 17 | void mouseMark(int row, int column) override; 18 | void drawMark(QPainter& painter, 19 | const QRect& markRect, int mark); 20 | 21 | public slots: 22 | DEFINE_LISTENER(NaCWidget, isNoughtStartsEnabled); 23 | void onNoughtStarts(); 24 | 25 | DEFINE_LISTENER(NaCWidget, isCrossStartsEnabled); 26 | void onCrossStarts(); 27 | 28 | private: 29 | void checkWinner(int row, int column, Mark mark); 30 | int countMarks(int row, int column, int rowStep, 31 | int columnStep, Mark mark); 32 | 33 | Mark m_nextMark; 34 | }; 35 | 36 | #endif // NACWIDGET_H 37 | -------------------------------------------------------------------------------- /Chapter07/NoughtsAndCrossesBasic/NaCWindow.cpp: -------------------------------------------------------------------------------- 1 | #include "NaCWindow.h" 2 | #include 3 | 4 | NaCWindow::NaCWindow(QWidget *parentWidget /*= nullptr*/) 5 | :MainWindow(parentWidget) { 6 | setWindowTitle(tr("Noughts and Crosses")); 7 | resize(1000, 500); 8 | 9 | m_nacWidgetPtr = new NaCWidget(this); 10 | setCentralWidget(m_nacWidgetPtr); 11 | 12 | { QMenu* gameMenuPtr = menuBar()->addMenu(tr("&Game")); 13 | connect(gameMenuPtr, SIGNAL(aboutToShow()), 14 | this, SLOT(onMenuShow())); 15 | 16 | addAction(gameMenuPtr, tr("&Nought Starts"), 17 | SLOT(onNoughtStarts()), 0, 18 | tr("Nought Starts"), nullptr, tr("Nought Starts"), 19 | LISTENER(isNoughtStartsEnabled)); 20 | 21 | addAction(gameMenuPtr, tr("&Cross Starts"), 22 | SLOT(onCrossStarts()), 0, 23 | tr("Cross Starts"), nullptr, tr("Cross Starts"), 24 | LISTENER(isCrossStartsEnabled)); 25 | 26 | gameMenuPtr->addSeparator(); 27 | 28 | addAction(gameMenuPtr, tr("&Quit the Game"), 29 | SLOT(onQuit()), 30 | QKeySequence(Qt::CTRL + Qt::Key_Q), tr("Quit Game"), 31 | nullptr, tr("Quit the Game"), 32 | LISTENER(isQuitEnabled)); 33 | 34 | addAction(gameMenuPtr, tr("E&xit"), 35 | SLOT(onExit()), QKeySequence::Quit); 36 | } 37 | } 38 | 39 | NaCWindow::~NaCWindow() { 40 | delete m_nacWidgetPtr; 41 | } 42 | -------------------------------------------------------------------------------- /Chapter07/NoughtsAndCrossesBasic/NaCWindow.h: -------------------------------------------------------------------------------- 1 | #ifndef NACWINDOW_H 2 | #define NACWINDOW_H 3 | 4 | #include "..\\MainWindow\\MainWindow.h" 5 | #include "NaCWidget.h" 6 | 7 | class NaCWindow : public MainWindow { 8 | Q_OBJECT 9 | 10 | public: 11 | NaCWindow(QWidget *parentWidget = nullptr); 12 | ~NaCWindow(); 13 | 14 | public: 15 | void closeEvent(QCloseEvent *eventPtr) override 16 | {m_nacWidgetPtr->closeEvent(eventPtr);} 17 | 18 | private: 19 | NaCWidget* m_nacWidgetPtr; 20 | }; 21 | 22 | #endif // NACWINDOW_H 23 | -------------------------------------------------------------------------------- /Chapter07/OthelloBasic/Main.cpp: -------------------------------------------------------------------------------- 1 | #include "OthelloWidget.h" 2 | #include "OthelloWindow.h" 3 | #include 4 | 5 | int main(int argc, char *argv[]) { 6 | QApplication application(argc, argv); 7 | OthelloWindow othelloWindow; 8 | othelloWindow.show(); 9 | return application.exec(); 10 | } 11 | -------------------------------------------------------------------------------- /Chapter07/OthelloBasic/OthelloWidget.h: -------------------------------------------------------------------------------- 1 | #ifndef OTHELLOWIDGET_H 2 | #define OTHELLOWIDGET_H 3 | 4 | #include "..\\MainWindow\\GameWidget.h" 5 | 6 | #define ROWS 8 7 | #define COLUMNS 8 8 | 9 | enum Mark {Empty = 0, Black, White}; 10 | 11 | class OthelloWidget : public GameWidget { 12 | Q_OBJECT 13 | 14 | public: 15 | OthelloWidget(QWidget* parentWidget); 16 | 17 | void mouseMark(int row, int column); 18 | void drawMark(QPainter& painter, 19 | const QRect& markRect, int mark); 20 | 21 | public slots: 22 | DEFINE_LISTENER(OthelloWidget, isBlackStartsEnabled); 23 | void onBlackStarts(); 24 | 25 | DEFINE_LISTENER(OthelloWidget, isWhiteStartsEnabled); 26 | void onWhiteStarts(); 27 | 28 | private: 29 | void checkWinner(); 30 | void turn(int row, int column, Mark mark); 31 | void calculateMark(int row, int column, Mark mark, 32 | QSet>& resultSet); 33 | Mark m_nextMark; 34 | }; 35 | 36 | #endif // OTHELLOWIDGET_H 37 | -------------------------------------------------------------------------------- /Chapter07/OthelloBasic/OthelloWindow.cpp: -------------------------------------------------------------------------------- 1 | #include "OthelloWidget.h" 2 | #include "OthelloWindow.h" 3 | #include 4 | 5 | OthelloWindow::OthelloWindow(QWidget *parentWidget /*= nullptr*/) 6 | :MainWindow(parentWidget) { 7 | setWindowTitle(tr("Othello")); 8 | resize(1000, 500); 9 | 10 | m_othelloWidgetPtr = new OthelloWidget(this); 11 | setCentralWidget(m_othelloWidgetPtr); 12 | 13 | { QMenu* gameMenuPtr = menuBar()->addMenu(tr("&Game")); 14 | connect(gameMenuPtr, SIGNAL(aboutToShow()), 15 | this, SLOT(onMenuShow())); 16 | 17 | addAction(gameMenuPtr, tr("&Black Starts"), 18 | SLOT(onBlackStarts()), 0, 19 | tr("Black Starts"), nullptr,tr("Black Starts"), 20 | LISTENER(isBlackStartsEnabled)); 21 | 22 | addAction(gameMenuPtr, tr("&White Starts"), 23 | SLOT(onWhiteStarts()), 0, 24 | tr("White Starts"), nullptr, tr("White Starts"), 25 | LISTENER(isWhiteStartsEnabled)); 26 | 27 | gameMenuPtr->addSeparator(); 28 | 29 | addAction(gameMenuPtr, tr("&Quit the Game"), 30 | SLOT(onQuit()), 31 | QKeySequence(Qt::CTRL + Qt::Key_Q), 32 | tr("Quit Game"), nullptr, tr("Quit the Game"), 33 | LISTENER(isQuitEnabled)); 34 | 35 | addAction(gameMenuPtr, tr("E&xit"), 36 | SLOT(onExit()), QKeySequence::Quit); 37 | } 38 | } 39 | 40 | OthelloWindow::~OthelloWindow() { 41 | delete m_othelloWidgetPtr; 42 | } 43 | -------------------------------------------------------------------------------- /Chapter07/OthelloBasic/OthelloWindow.h: -------------------------------------------------------------------------------- 1 | #ifndef OTHELLOWINDOW_H 2 | #define OTHELLOWINDOW_H 3 | 4 | #include "..\\MainWindow\\MainWindow.h" 5 | #include "OthelloWidget.h" 6 | 7 | class OthelloWindow : public MainWindow { 8 | Q_OBJECT 9 | 10 | public: 11 | OthelloWindow(QWidget *parentWidget = nullptr); 12 | ~OthelloWindow(); 13 | 14 | void closeEvent(QCloseEvent *eventPtr) override 15 | {m_othelloWidgetPtr->closeEvent(eventPtr);} 16 | 17 | private: 18 | OthelloWidget* m_othelloWidgetPtr; 19 | }; 20 | 21 | #endif // OTHELLOWINDOW_H 22 | -------------------------------------------------------------------------------- /Chapter08/MainWindow/DocumentWidget.h: -------------------------------------------------------------------------------- 1 | #ifndef DOCUMENTWIDGET_H 2 | #define DOCUMENTWIDGET_H 3 | 4 | #include "Listener.h" 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | class DocumentWidget : public QWidget { 11 | Q_OBJECT 12 | 13 | public: 14 | DocumentWidget(const QString& name, const QString& fileMask, 15 | QWidget* parentWidgetPtr); 16 | ~DocumentWidget(); 17 | 18 | protected: 19 | void setFilePath(QString filePath); 20 | 21 | public: 22 | void setModifiedFlag(bool flag); 23 | 24 | private: 25 | void setMainWindowHeader(); 26 | 27 | public: 28 | virtual void closeEvent(QCloseEvent* eventPtr) /*override*/; 29 | 30 | private: 31 | bool isClearOk(QString title); 32 | 33 | public slots: 34 | virtual void onNew(); 35 | virtual void onOpen(); 36 | virtual bool onSave(); 37 | virtual bool isSaveEnabled(); 38 | virtual bool onSaveAs(); 39 | virtual void onExit(); 40 | 41 | private: 42 | bool tryWriteFile(QString filePath); 43 | 44 | public: 45 | virtual bool isCutEnabled(); 46 | virtual bool isCopyEnabled(); 47 | virtual bool isPasteEnabled(); 48 | virtual bool isDeleteEnabled(); 49 | 50 | protected: 51 | virtual void newDocument() = 0; 52 | virtual bool writeFile(const QString& filePath) = 0; 53 | virtual bool readFile(const QString& filePath) = 0; 54 | 55 | public slots: 56 | virtual void onCut(); 57 | virtual void onCopy(); 58 | virtual void onPaste(); 59 | virtual void onDelete(); 60 | 61 | private: 62 | QString m_applicationName, m_fileMask, m_filePath; 63 | bool m_modifiedFlag = false; 64 | }; 65 | 66 | QPoint& operator+=(QPoint& point, const QSize& size); 67 | QPoint& operator-=(QPoint& point, const QSize& size); 68 | 69 | QRect& operator+=(QRect& rect, int size); 70 | QRect& operator-=(QRect& rect, int size); 71 | 72 | void writePoint(ofstream& outStream, const QPoint& point); 73 | void readPoint(ifstream& inStream, QPoint& point); 74 | 75 | void writeColor(ofstream& outStream, const QColor& color); 76 | void readColor(ifstream& inStream, QColor& color); 77 | 78 | QRect makeRect(const QPoint& centerPoint, int halfSide); 79 | #endif // DOCUMENTWIDGET_H 80 | -------------------------------------------------------------------------------- /Chapter08/MainWindow/GameWidget.h: -------------------------------------------------------------------------------- 1 | #ifndef GAMEWIDGET_H 2 | #define GAMEWIDGET_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #include "..\\MainWindow\\MainWindow.h" 9 | 10 | class GameWidget : public QWidget { 11 | Q_OBJECT 12 | 13 | public: 14 | GameWidget(int rows, int columns, QWidget* parentWidget); 15 | void clearGrid(); 16 | 17 | void resizeEvent(QResizeEvent *eventPtr) /*override*/; 18 | void mousePressEvent(QMouseEvent *eventPtr) /*override*/; 19 | void paintEvent(QPaintEvent *eventPtr) /*override*/; 20 | void closeEvent(QCloseEvent *eventPtr) /*override*/; 21 | 22 | virtual void mouseMark(int row, int column) = 0; 23 | virtual void drawMark(QPainter& painter, 24 | const QRect& markRect, int mark) = 0; 25 | 26 | private: 27 | bool isQuitOk(); 28 | 29 | public slots: 30 | DEFINE_LISTENER(GameWidget, isQuitEnabled); 31 | void onQuit(); 32 | void onExit(); 33 | 34 | protected: 35 | bool isGameInProgress() const {return m_gameInProgress;} 36 | void setGameInProgress(bool active) 37 | {m_gameInProgress = active;} 38 | 39 | protected: 40 | int get(int row, int column) const; 41 | void set(int row, int column, int value); 42 | 43 | private: 44 | bool m_gameInProgress = false; 45 | int m_rows, m_columns; 46 | int m_rowHeight, m_columnWidth; 47 | int* m_gameGrid; 48 | }; 49 | 50 | #endif // GAMEWIDGET_H 51 | -------------------------------------------------------------------------------- /Chapter08/MainWindow/Listener.h: -------------------------------------------------------------------------------- 1 | #ifndef LISTENER_H 2 | #define LISTENER_H 3 | 4 | #include 5 | 6 | #define DEFINE_LISTENER(BaseClass, Listener) \ 7 | friend bool Listener(QObject* baseObjectPtr) { \ 8 | return ((BaseClass*) baseObjectPtr)->Listener(); \ 9 | } \ 10 | bool Listener() \ 11 | 12 | #define LISTENER(Listener) (&::Listener) 13 | typedef bool (*Listener)(QObject*); 14 | 15 | #endif // LISTENER_H 16 | -------------------------------------------------------------------------------- /Chapter08/MainWindow/MainWindow.h: -------------------------------------------------------------------------------- 1 | #ifndef MAINWINDOW_H 2 | #define MAINWINDOW_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include "Listener.h" 10 | #include "DocumentWidget.h" 11 | 12 | class MainWindow : public QMainWindow { 13 | Q_OBJECT 14 | 15 | public: 16 | MainWindow(QWidget *parentWidgetPtr = nullptr); 17 | ~MainWindow(); 18 | 19 | protected: 20 | void addFileMenu(); 21 | void addEditMenu(); 22 | 23 | protected: 24 | /* void addAction(QMenu* menuPtr, QString text, 25 | const char* onSelectPtr, QString iconName = QString(), 26 | QToolBar* toolBarPtr = nullptr, 27 | QString statusTip = QString(), 28 | Listener enableListener = nullptr, 29 | Listener checkListener = nullptr, 30 | QActionGroup* groupPtr = nullptr); 31 | */ 32 | void addAction(QMenu* menuPtr, QString text, 33 | const char* onSelectPtr, 34 | QKeySequence acceleratortKey = 0, 35 | QString iconName = QString(), 36 | QToolBar* toolBarPtr = nullptr, 37 | QString statusTip = QString(), 38 | Listener enableListener = nullptr, 39 | Listener checkListener = nullptr, 40 | QActionGroup* groupPtr = nullptr); 41 | 42 | DEFINE_LISTENER(DocumentWidget, isSaveEnabled); 43 | DEFINE_LISTENER(DocumentWidget, isCutEnabled); 44 | DEFINE_LISTENER(DocumentWidget, isCopyEnabled); 45 | DEFINE_LISTENER(DocumentWidget, isPasteEnabled); 46 | DEFINE_LISTENER(DocumentWidget, isDeleteEnabled); 47 | 48 | public slots: 49 | void onMenuShow(); 50 | 51 | private: 52 | QMap> m_enableMap, 53 | m_checkMap; 54 | }; 55 | 56 | #endif // MAINWINDOW_H 57 | -------------------------------------------------------------------------------- /Chapter08/NoughtsAndCrossesAdvanced/Main.cpp: -------------------------------------------------------------------------------- 1 | #include "NaCWidget.h" 2 | #include "NaCWindow.h" 3 | #include 4 | 5 | int main(int argc, char *argv[]) { 6 | QApplication application(argc, argv); 7 | NaCWindow mainWindow; 8 | mainWindow.show(); 9 | return application.exec(); 10 | } 11 | -------------------------------------------------------------------------------- /Chapter08/NoughtsAndCrossesAdvanced/NaCWidget.h: -------------------------------------------------------------------------------- 1 | #ifndef NACWIDGET_H 2 | #define NACWIDGET_H 3 | 4 | #include "..\\MainWindow\\GameWidget.h" 5 | 6 | #define ROWS 26 7 | #define COLUMNS 26 8 | 9 | enum Mark {Empty = 0, Nought, Cross}; 10 | 11 | class NaCWidget : public GameWidget { 12 | Q_OBJECT 13 | 14 | public: 15 | NaCWidget(QWidget* parentWidget); 16 | 17 | void mouseMark(int row, int column) override; 18 | void drawMark(QPainter& painter, 19 | const QRect& markRect, int mark) override; 20 | 21 | public slots: 22 | DEFINE_LISTENER(NaCWidget, isComputerStartsNoughtEnabled); 23 | DEFINE_LISTENER(NaCWidget, isComputerStartsCrossEnabled); 24 | DEFINE_LISTENER(NaCWidget, isHumanStartsNoughtEnabled); 25 | DEFINE_LISTENER(NaCWidget, isHumanStartsCrossEnabled); 26 | 27 | void onComputerStartsNought(); 28 | void onComputerStartsCross(); 29 | void onHumanStartsNought(); 30 | void onHumanStartsCross(); 31 | 32 | private: 33 | bool checkWinner(int row, int column, Mark mark); 34 | double countMarks(int row, int column, int rowStep, 35 | int columnStep, Mark mark); 36 | void calculateComputerMove(int& row, int &column); 37 | double calculateMarkValue(int row, int column, Mark mark); 38 | Mark m_humanMark, m_computerMark; 39 | }; 40 | 41 | #endif // NACWIDGET_H 42 | -------------------------------------------------------------------------------- /Chapter08/NoughtsAndCrossesAdvanced/NaCWindow.cpp: -------------------------------------------------------------------------------- 1 | #include "NaCWindow.h" 2 | #include 3 | 4 | NaCWindow::NaCWindow(QWidget *parentWidget /*= nullptr*/) 5 | :MainWindow(parentWidget) { 6 | setWindowTitle(tr("Noughts and Crosses Advanced")); 7 | resize(1000, 500); 8 | 9 | m_nacWidgetPtr = new NaCWidget(this); 10 | setCentralWidget(m_nacWidgetPtr); 11 | 12 | { QMenu* gameMenuPtr = menuBar()->addMenu(tr("&Game")); 13 | connect(gameMenuPtr, SIGNAL(aboutToShow()), 14 | this, SLOT(onMenuShow())); 15 | 16 | { QMenu* computerStartsMenuPtr = 17 | gameMenuPtr->addMenu(tr("&Computer Starts")); 18 | connect(computerStartsMenuPtr, SIGNAL(aboutToShow()), 19 | this, SLOT(onMenuShow())); 20 | 21 | addAction(computerStartsMenuPtr, tr("Computer &Nought"), 22 | SLOT(onComputerStartsNought()), 0, 23 | tr("Computer Nought"), nullptr, 24 | tr("Computer Nought"), 25 | LISTENER(isComputerStartsNoughtEnabled)); 26 | 27 | addAction(computerStartsMenuPtr, tr("Computer &Cross"), 28 | SLOT(onComputerStartsCross()), 0, 29 | tr("Computer Cross"), nullptr, 30 | tr("Computer Cross"), 31 | LISTENER(isComputerStartsCrossEnabled)); 32 | } 33 | 34 | { QMenu* humanStartsMenuPtr = 35 | gameMenuPtr->addMenu(tr("&Human Starts")); 36 | connect(humanStartsMenuPtr, SIGNAL(aboutToShow()), 37 | this, SLOT(onMenuShow())); 38 | 39 | addAction(humanStartsMenuPtr, tr("Human &Nought"), 40 | SLOT(onHumanStartsNought()), 0, tr("Human Nought"), 41 | nullptr, tr("Human Nought"), 42 | LISTENER(isHumanStartsNoughtEnabled)); 43 | 44 | addAction(humanStartsMenuPtr, tr("Human &Cross"), 45 | SLOT(onHumanStartsCross()), 0, tr("Human Cross"), 46 | nullptr, tr("Human Cross"), 47 | LISTENER(isHumanStartsCrossEnabled)); 48 | } 49 | 50 | gameMenuPtr->addSeparator(); 51 | 52 | addAction(gameMenuPtr, tr("&Quit the Game"), 53 | SLOT(onQuit()), 54 | QKeySequence(Qt::CTRL + Qt::Key_Q), tr("Quit Game"), 55 | nullptr, tr("Quit the Game"), 56 | LISTENER(isQuitEnabled)); 57 | 58 | addAction(gameMenuPtr, tr("E&xit"), 59 | SLOT(onExit()), QKeySequence::Quit); 60 | } 61 | } 62 | 63 | NaCWindow::~NaCWindow() { 64 | delete m_nacWidgetPtr; 65 | } 66 | -------------------------------------------------------------------------------- /Chapter08/NoughtsAndCrossesAdvanced/NaCWindow.h: -------------------------------------------------------------------------------- 1 | #ifndef NACWINDOW_H 2 | #define NACWINDOW_H 3 | 4 | #include "..\\MainWindow\\MainWindow.h" 5 | #include "NaCWidget.h" 6 | 7 | class NaCWindow : public MainWindow { 8 | Q_OBJECT 9 | 10 | public: 11 | NaCWindow(QWidget *parentWidget = nullptr); 12 | ~NaCWindow(); 13 | 14 | public: 15 | void closeEvent(QCloseEvent *eventPtr) override 16 | {m_nacWidgetPtr->closeEvent(eventPtr);} 17 | 18 | private: 19 | NaCWidget* m_nacWidgetPtr; 20 | }; 21 | 22 | #endif // NACWINDOW_H 23 | -------------------------------------------------------------------------------- /Chapter08/OthelloAdvanced/Main.cpp: -------------------------------------------------------------------------------- 1 | #include "OthelloWidget.h" 2 | #include "OthelloWindow.h" 3 | #include 4 | 5 | int main(int argc, char *argv[]) { 6 | QApplication application(argc, argv); 7 | OthelloWindow othelloWindow; 8 | othelloWindow.show(); 9 | return application.exec(); 10 | } 11 | -------------------------------------------------------------------------------- /Chapter08/OthelloAdvanced/OthelloWidget.h: -------------------------------------------------------------------------------- 1 | #ifndef OTHELLOWIDGET_H 2 | #define OTHELLOWIDGET_H 3 | 4 | #include "..\\MainWindow\\GameWidget.h" 5 | 6 | #define ROWS 8 7 | #define COLUMNS 8 8 | 9 | enum Mark {Empty = 0, Black, White}; 10 | 11 | class OthelloWidget : public GameWidget { 12 | Q_OBJECT 13 | 14 | public: 15 | OthelloWidget(QWidget* parentWidget); 16 | 17 | void mouseMark(int row, int column); 18 | void drawMark(QPainter& painter, 19 | const QRect& markRect, int mark); 20 | 21 | public slots: 22 | DEFINE_LISTENER(OthelloWidget, isComputerStartsBlackEnabled); 23 | DEFINE_LISTENER(OthelloWidget, isComputerStartsWhiteEnabled); 24 | DEFINE_LISTENER(OthelloWidget, isHumanStartsBlackEnabled); 25 | DEFINE_LISTENER(OthelloWidget, isHumanStartsWhiteEnabled); 26 | 27 | void onComputerStartsBlack(); 28 | void onComputerStartsWhite(); 29 | void onHumanStartsBlack(); 30 | void onHumanStartsWhite(); 31 | 32 | private: 33 | bool checkWinner(); 34 | void turn(int row, int column, Mark mark); 35 | void calculateComputerMove(); 36 | void calculateTurns(int row, int column, Mark mark, 37 | QSet>& totalSet, 38 | int& neighbours); 39 | Mark m_humanMark, m_computerMark; 40 | }; 41 | 42 | #endif // OTHELLOWIDGET_H 43 | -------------------------------------------------------------------------------- /Chapter08/OthelloAdvanced/OthelloWindow.h: -------------------------------------------------------------------------------- 1 | #ifndef OTHELLOWINDOW_H 2 | #define OTHELLOWINDOW_H 3 | 4 | #include "..\\MainWindow\\MainWindow.h" 5 | #include "OthelloWidget.h" 6 | 7 | class OthelloWindow : public MainWindow { 8 | Q_OBJECT 9 | 10 | public: 11 | OthelloWindow(QWidget *parentWidget = nullptr); 12 | ~OthelloWindow(); 13 | 14 | void closeEvent(QCloseEvent *eventPtr) override 15 | {m_othelloWidgetPtr->closeEvent(eventPtr);} 16 | 17 | private: 18 | OthelloWidget* m_othelloWidgetPtr; 19 | }; 20 | 21 | #endif // OTHELLOWINDOW_H 22 | -------------------------------------------------------------------------------- /Chapter09/Action.cpp: -------------------------------------------------------------------------------- 1 | #include "Action.h" 2 | 3 | Action::Action(TokenId actionId, 4 | const Value& value1 /*= Value()*/, 5 | const Value& value2 /*= Value()*/ ) 6 | :m_actionId(actionId), 7 | m_value1(value1), 8 | m_value2(value2) { 9 | // Empty. 10 | } 11 | 12 | Action::Action(const Action& action) 13 | :m_actionId(action.m_actionId), 14 | m_value1(action.m_value1), 15 | m_value2(action.m_value2) { 16 | // Empty. 17 | } 18 | 19 | Action Action::operator=(const Action& action) { 20 | if (this != &action) { 21 | m_actionId = action.m_actionId; 22 | m_value1 = action.m_value1; 23 | m_value2 = action.m_value2; 24 | } 25 | 26 | return *this; 27 | } 28 | -------------------------------------------------------------------------------- /Chapter09/Action.h: -------------------------------------------------------------------------------- 1 | #ifndef ACTION_H 2 | #define ACTION_H 3 | 4 | #include "Token.h" 5 | #include "Value.h" 6 | 7 | class Action { 8 | public: 9 | Action(TokenId actionId, const Value& value1 = Value(), 10 | const Value& value2 = Value()); 11 | 12 | Action(const Action& action); 13 | Action operator=(const Action& action); 14 | 15 | TokenId id() const {return m_actionId;} 16 | const Value& value1() const {return m_value1;} 17 | const Value& value2() const {return m_value2;} 18 | 19 | private: 20 | TokenId m_actionId; 21 | Value m_value1, m_value2; 22 | }; 23 | 24 | #endif // ACTION_H 25 | -------------------------------------------------------------------------------- /Chapter09/Colors.h: -------------------------------------------------------------------------------- 1 | #ifndef COLOR_H 2 | #define COLOR_H 3 | 4 | #include 5 | 6 | extern QColor 7 | AliceBlue, AntiqueWhite, Aqua, Aquamarine, 8 | Azure, Beige, Bisque, Black, BlanchedAlmond, 9 | Blue, BlueViolet, Brown, Burlywood, CadetBlue, 10 | Chartreuse, Chocolate, Coral, CornflowerBlue, 11 | Cornsilk, Crimson, Cyan, DarkBlue, DarkCyan, 12 | DarkGoldenRod, DarkGray, DarkGreen, DarkKhaki, 13 | DarkMagenta, DarkOliveGreen, DarkOrange, DarkOrchid, 14 | DarkRed, DarkSalmon, DarkSeaGreen, DarkSlateBlue, 15 | DarkSlateGray, DarkTurquoise, DarkViolet, DeepPink, 16 | DeepSkyBlue, DimGray, DodgerBlue, FireBrick, 17 | FloralWhite, ForestGreen, Fuchsia, Gainsboro, 18 | GhostWhite, Gold, GoldenRod, Gray, Green, GreenYellow, 19 | HoneyDew, HotPink, IndianRed, Indigo, Ivory, Khaki, 20 | Lavender, LavenderBlush, Lawngreen, LemonChiffon, 21 | LightBlue, LightCoral, LightCyan, LightGoldenRodYellow, 22 | LightGreen, LightGray, LightPink, LightSalmon, 23 | LightSeaGreen, LightSkyBlue, LightSlateGray, 24 | LightSteelBlue, LightYellow, Lime, LimeGreen, Linen, 25 | Magenta, Maroon, MediumAquamarine, MediumBlue, 26 | MediumOrchid, MediumPurple, MediumSeaGreen, 27 | MediumSlateBlue, MediumSpringGreen, MediumTurquoise, 28 | MediumVioletRed, MidnightBlue, MdoubleCream, MistyRose, 29 | Moccasin, NavajoWhite, Navy, Navyblue, OldLace, Olive, 30 | OliveDrab, Orange, OrangeRed, Orchid, PaleGoldenRod, 31 | PaleGreen, PaleTurquoise, PaleVioletRed, PapayaWhip, 32 | PeachPuff, Peru, Pink, Plum, PowderBlue, Purple, 33 | Red, RosyBrown, RoyalBlue, SaddleBrown, Salmon, 34 | SandyBrown, SeaGreen, SeaShell, Sienna, Silver, SkyBlue, 35 | SlateBlue, SlateGray, Snow, SpringGreen, SteelBlue, 36 | SystemColor, Tan, Teal, Thistle, Tomato, Turquoise, 37 | Violet, Wheat, White, WhiteSmoke, Yellow, YellowGreen; 38 | 39 | #endif // COLOR_H 40 | -------------------------------------------------------------------------------- /Chapter09/Error.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | #include "Error.h" 6 | 7 | extern int g_lineNo = 1; 8 | 9 | void error(const QString& message) { 10 | throw exception(message.toStdString().c_str()); 11 | } 12 | 13 | void syntaxError() { 14 | stringstream stringStream; 15 | stringStream << "Syntax error at line " 16 | << g_lineNo << "."; 17 | error(stringStream.str().c_str()); 18 | } 19 | 20 | void syntaxError(const QString& message) { 21 | stringstream stringStream; 22 | stringStream << "Syntax error at line " << g_lineNo 23 | << ": " << message.toStdString() << "."; 24 | error(stringStream.str().c_str()); 25 | } 26 | 27 | void semanticError(const QString& message) { 28 | stringstream stringStream; 29 | stringStream << "Sematic error: " << message.toStdString() << "."; 30 | error(stringStream.str().c_str()); 31 | } 32 | 33 | void check(bool condition, const QString& message) { 34 | if (!condition) { 35 | semanticError(message); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Chapter09/Error.h: -------------------------------------------------------------------------------- 1 | #ifndef ERROR_H 2 | #define ERROR_H 3 | 4 | #include 5 | 6 | void error(const QString& message); 7 | void syntaxError(); 8 | void syntaxError(const QString& message); 9 | void semanticError(const QString& message); 10 | void check(bool condition, const QString& message); 11 | 12 | #endif // ERROR_H 13 | -------------------------------------------------------------------------------- /Chapter09/Input.dsl: -------------------------------------------------------------------------------- 1 | topLeft = point(100, 100); 2 | left = xCoordinate(topleft); 3 | top = yCoordinate(topLeft); 4 | bottomRight = point(left + 100, top + 100); 5 | SetPenStyle(DashLine); 6 | SetBrushStyle(CrossPattern); 7 | SetPenColor(Black); 8 | purpleColor = color(128, 0, 128); 9 | SetBrushColor(purpleColor); 10 | DrawRectangle(topLeft, bottomRight); 11 | 12 | SetFont(font("Times New Roman", 12)); 13 | SetHorizontalAlignment(AlignLeft); 14 | SetVerticalAlignment(AlignTop); 15 | DrawText(point(300, 150), "Hello, DSL!"); 16 | -------------------------------------------------------------------------------- /Chapter09/Main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "Action.h" 5 | #include "Error.h" 6 | #include "Scanner.h" 7 | #include "Parser.h" 8 | #include "ViewerWidget.h" 9 | 10 | int main(int argc, char *argv[]) { 11 | Scanner::init(); 12 | QApplication application(argc, argv); 13 | 14 | try { 15 | QString path = "C:\\Users\\Stefan\\Documents\\A A_Cpp_By_Example\\DSL_Basic\\Input.dsl"; 16 | 17 | QFile file(path); 18 | if (!file.open(QIODevice::ReadOnly)) { 19 | error("Cannot open file \"" + path + "\" for reading."); 20 | } 21 | 22 | QString buffer(file.readAll()); 23 | Scanner scanner(buffer); 24 | 25 | QList actionList; 26 | Parser(scanner, actionList); 27 | 28 | ViewerWidget mainWidget(actionList); 29 | mainWidget.show(); 30 | return application.exec(); 31 | } 32 | catch (exception e) { 33 | QMessageBox messageBox(QMessageBox::Information, 34 | QString("Error"), QString(e.what())); 35 | messageBox.exec(); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Chapter09/Parser.h: -------------------------------------------------------------------------------- 1 | #ifndef PARSER_H 2 | #define PARSER_H 3 | 4 | #include "Action.h" 5 | #include "Scanner.h" 6 | 7 | class Parser { 8 | public: 9 | Parser(Scanner& m_scanner, QList& actionList); 10 | 11 | private: 12 | void match(TokenId tokenId); 13 | 14 | void instructionList(); 15 | void instruction(); 16 | 17 | Value expression(); 18 | Value expressionRest(Value leftValue); 19 | Value mulDivExpression(); 20 | Value mulDivExpressionRest(Value leftValue); 21 | Value primaryExpression(); 22 | 23 | void checkType(TokenId operatorId, const Value& value); 24 | void checkType(TokenId operatorId, const Value& leftValue, 25 | const Value& rightValue); 26 | Value evaluate(TokenId operatorId, const Value& value); 27 | Value evaluate(TokenId operatorId, const Value& leftValue, 28 | const Value& rightValue); 29 | 30 | private: 31 | Token m_lookAHead; 32 | Scanner& m_scanner; 33 | QList& m_actionList; 34 | QMap m_assignMap; }; 35 | 36 | #endif // PARSER_H 37 | -------------------------------------------------------------------------------- /Chapter09/Scanner.h: -------------------------------------------------------------------------------- 1 | #ifndef SCANNER_H 2 | #define SCANNER_H 3 | 4 | #include "Token.h" 5 | #include "Colors.h" 6 | 7 | class Scanner { 8 | public: 9 | static void init(); 10 | Scanner(QString& buffer); 11 | 12 | public: 13 | Token nextToken(); 14 | 15 | private: 16 | QString m_buffer; 17 | int m_bufferIndex = 0; 18 | 19 | static QMap m_valueMap; 20 | static QMap m_keywordMap; 21 | static QList> m_operatorList; 22 | }; 23 | 24 | #endif // SCANNER_H 25 | -------------------------------------------------------------------------------- /Chapter09/Token.cpp: -------------------------------------------------------------------------------- 1 | #include "Token.h" 2 | 3 | Token::Token() 4 | :m_tokenId(EndOfFileId) { 5 | // Empty. 6 | } 7 | 8 | Token::Token(TokenId tokenId) 9 | :m_tokenId(tokenId) { 10 | // Empty. 11 | } 12 | 13 | Token::Token(TokenId tokenId, const QString& name) 14 | :m_tokenId(tokenId), 15 | m_name(name) { 16 | // Empty. 17 | } 18 | 19 | Token::Token(TokenId tokenId, const Value& value) 20 | :m_tokenId(tokenId), 21 | m_value(value) { 22 | // Empty. 23 | } 24 | -------------------------------------------------------------------------------- /Chapter09/Token.h: -------------------------------------------------------------------------------- 1 | #ifndef TOKEN_H 2 | #define TOKEN_H 3 | 4 | #include 5 | #include "Value.h" 6 | 7 | enum TokenId {ColorId, DrawEllipseId, DrawLineId, 8 | DrawRectangleId, DrawTextId, FontId, 9 | PointId, SetBrushColorId, SetBrushStyleId, 10 | SetFontId, SetHorizontalAlignmentId, 11 | SetPenColorId, SetPenStyleId, 12 | SetVerticalAlignmentId, 13 | XCoordinateId, YCoordinateId, 14 | AddId, SubtractId, MultiplyId, DivideId, 15 | LeftParenthesisId, RightParenthesisId, 16 | AssignId, CommaId, SemicolonId, 17 | NameId, ValueId, EndOfFileId}; 18 | 19 | class Token{ 20 | public: 21 | Token(); 22 | Token(TokenId tokenId); 23 | Token(TokenId tokenId, const QString& name); 24 | Token(TokenId tokenId, const Value& value); 25 | 26 | TokenId id() const {return m_tokenId;} 27 | const QString& name() const { return m_name; } 28 | const Value& value() const { return m_value; } 29 | 30 | private: 31 | TokenId m_tokenId; 32 | QString m_name; 33 | Value m_value; 34 | }; 35 | 36 | #endif // TOKEN_H 37 | -------------------------------------------------------------------------------- /Chapter09/Value.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #include "Value.h" 5 | 6 | Value::Value() { 7 | // Empty. 8 | } 9 | 10 | Value::Value(double numericalValue) 11 | :m_typeId(NumericalTypeId), 12 | m_numericalValue(numericalValue) { 13 | // Empty. 14 | } 15 | 16 | Value::Value(const QString& stringValue) 17 | :m_typeId(StringTypeId), 18 | m_stringValue(stringValue) { 19 | // Empty. 20 | } 21 | 22 | Value::Value(const QPoint& pointValue) 23 | :m_typeId(PointTypeId), 24 | m_pointValue(pointValue) { 25 | // Empty. 26 | } 27 | 28 | Value::Value(const QColor& colorValue) 29 | :m_typeId(ColorTypeId), 30 | m_colorValue(colorValue) { 31 | // Empty. 32 | } 33 | 34 | Value::Value(const QFont& fontValue) 35 | :m_typeId(FontTypeId), 36 | m_fontValue(fontValue) { 37 | // Empty. 38 | } 39 | 40 | Value::Value(const Qt::PenStyle& penStyleValue) 41 | :m_typeId(PenStyleTypeId), 42 | m_penStyleValue(penStyleValue) { 43 | // Empty. 44 | } 45 | 46 | Value::Value(const Qt::BrushStyle& brushStyleValue) 47 | :m_typeId(BrushStyleId), 48 | m_brushStyleValue(brushStyleValue) { 49 | // Empty. 50 | } 51 | 52 | Value::Value(const Qt::AlignmentFlag& alignmentValue) 53 | :m_typeId(AlignmentTypeId), 54 | m_alignmentValue(alignmentValue) { 55 | // Empty. 56 | } 57 | 58 | Value::Value(const Value& value) 59 | :m_typeId(value.m_typeId), 60 | m_numericalValue(value.m_numericalValue), 61 | m_stringValue(value.m_stringValue), 62 | m_pointValue(value.m_pointValue), 63 | m_colorValue(value.m_colorValue), 64 | m_fontValue(value.m_fontValue), 65 | m_penStyleValue(value.m_penStyleValue), 66 | m_brushStyleValue(value.m_brushStyleValue), 67 | m_alignmentValue(value.m_alignmentValue) { 68 | // Empty. 69 | } 70 | 71 | Value& Value::operator=(const Value& value) { 72 | m_typeId = value.m_typeId; 73 | m_numericalValue = value.m_numericalValue; 74 | m_stringValue = value.m_stringValue; 75 | m_pointValue = value.m_pointValue; 76 | m_colorValue = value.m_colorValue; 77 | m_fontValue = value.m_fontValue; 78 | m_penStyleValue = value.m_penStyleValue; 79 | m_brushStyleValue = value.m_brushStyleValue; 80 | m_alignmentValue = value.m_alignmentValue; 81 | return *this; 82 | } 83 | -------------------------------------------------------------------------------- /Chapter09/Value.h: -------------------------------------------------------------------------------- 1 | #ifndef VALUE_H 2 | #define VALUE_H 3 | 4 | #include 5 | using namespace std; 6 | 7 | #include 8 | 9 | enum TypeId {NumericalTypeId, StringTypeId, ColorTypeId, 10 | PenStyleTypeId, BrushStyleId, AlignmentTypeId, 11 | FontTypeId, PointTypeId}; 12 | 13 | class Value { 14 | public: 15 | Value(); 16 | Value(double numericalValue); 17 | Value(const QString& stringValue); 18 | Value(const QPoint& pointValue); 19 | Value(const QColor& colorValue); 20 | Value(const QFont& fontValue); 21 | Value(const Qt::PenStyle& penStyleValue); 22 | Value(const Qt::BrushStyle& brushStyleValue); 23 | Value(const Qt::AlignmentFlag& alignment); 24 | 25 | Value(const Value& value); 26 | Value& operator=(const Value& value); 27 | 28 | bool isNumerical() const {return (m_typeId==NumericalTypeId);} 29 | bool isString() const { return (m_typeId == StringTypeId); } 30 | bool isColor() const { return (m_typeId == ColorTypeId); } 31 | bool isFont() const { return (m_typeId == FontTypeId); } 32 | bool isPoint() const { return (m_typeId == PointTypeId); } 33 | bool isPenStyle() const {return (m_typeId == PenStyleTypeId);} 34 | bool isBrushStyle() const {return (m_typeId == BrushStyleId);} 35 | bool isAlignment() const {return (m_typeId==AlignmentTypeId);} 36 | 37 | double numericalValue() const { return m_numericalValue; } 38 | const QString& stringValue() const { return m_stringValue; } 39 | const QColor& colorValue() const { return m_colorValue; } 40 | const QFont& fontValue() const { return m_fontValue; } 41 | const QPoint& pointValue() const { return m_pointValue; } 42 | const Qt::PenStyle& penStyleValue() const 43 | { return m_penStyleValue; } 44 | const Qt::BrushStyle& brushStyleValue() const 45 | { return m_brushStyleValue; } 46 | const Qt::AlignmentFlag& alignmentValue() const 47 | { return m_alignmentValue; } 48 | 49 | private: 50 | TypeId m_typeId; 51 | double m_numericalValue; 52 | QString m_stringValue; 53 | QPoint m_pointValue; 54 | QColor m_colorValue; 55 | QFont m_fontValue; 56 | Qt::PenStyle m_penStyleValue; 57 | Qt::BrushStyle m_brushStyleValue; 58 | Qt::AlignmentFlag m_alignmentValue; 59 | }; 60 | 61 | #endif // VALUE_H 62 | -------------------------------------------------------------------------------- /Chapter09/ViewerWidget.h: -------------------------------------------------------------------------------- 1 | #ifndef MAINWIDGET_H 2 | #define MAINWIDGET_H 3 | 4 | #include 5 | #include 6 | #include "Value.h" 7 | #include "Colors.h" 8 | #include "Action.h" 9 | 10 | class ViewerWidget : public QWidget { 11 | Q_OBJECT 12 | 13 | public: 14 | ViewerWidget(const QList& actionList, 15 | QWidget *parentTableWidget = nullptr); 16 | 17 | void paintEvent(QPaintEvent *eventPtr) /*override*/; 18 | 19 | private: 20 | Qt::Alignment m_horizontalAlignment = Qt::AlignHCenter, 21 | m_verticalAlignment = Qt::AlignVCenter; 22 | const QList& m_actionList; 23 | }; 24 | 25 | #endif // MAINWIDGET_H 26 | -------------------------------------------------------------------------------- /Chapter10/Action.cpp: -------------------------------------------------------------------------------- 1 | #include "Action.h" 2 | 3 | Action::Action(TokenId actionId, 4 | const Value& value1 /*= Value()*/, 5 | const Value& value2 /*= Value()*/ ) 6 | :m_actionId(actionId), 7 | m_value1(value1), 8 | m_value2(value2) { 9 | // Empty. 10 | } 11 | 12 | Action::Action(const Action& action) 13 | :m_actionId(action.m_actionId), 14 | m_value1(action.m_value1), 15 | m_value2(action.m_value2) { 16 | // Empty. 17 | } 18 | 19 | Action Action::operator=(const Action& action) { 20 | m_actionId = action.m_actionId; 21 | m_value1 = action.m_value1; 22 | m_value2 = action.m_value2; 23 | return *this; 24 | } 25 | -------------------------------------------------------------------------------- /Chapter10/Action.h: -------------------------------------------------------------------------------- 1 | #ifndef ACTION_H 2 | #define ACTION_H 3 | 4 | #include "Colors.h" 5 | #include "Token.h" 6 | #include "Value.h" 7 | 8 | class Action { 9 | public: 10 | explicit Action(TokenId actionId, 11 | const Value& value1 = Value(), 12 | const Value& value2 = Value()); 13 | 14 | Action(const Action& action); 15 | Action operator=(const Action& action); 16 | 17 | TokenId id() const {return m_actionId;} 18 | const Value& value1() const {return m_value1;} 19 | const Value& value2() const {return m_value2;} 20 | 21 | private: 22 | TokenId m_actionId; 23 | Value m_value1, m_value2; 24 | }; 25 | 26 | #endif // ACTION_H 27 | -------------------------------------------------------------------------------- /Chapter10/Colors.cpp: -------------------------------------------------------------------------------- 1 | #include "Colors.h" 2 | 3 | QColor 4 | Aqua(0, 255, 255), Black(0, 0, 0), Blue(0, 0, 255), 5 | Brown(165, 42, 42), Cyan(0, 255, 255), Gray(127, 127, 127), 6 | Green(0, 128, 0), Lime(0, 255, 0), Magenta(255, 0, 255), 7 | Navyblue(159, 175, 223), Orange(255, 165, 0), 8 | Orchid(218, 112, 214), Pink(255, 192, 203), 9 | Purple(128, 0, 128), Red(255, 0, 0), Silver(192, 192, 192), 10 | Snow(255, 250, 250), SteelBlue(70, 130, 180), 11 | SystemColor(0, 0, 0), Turquoise(64, 224, 208), 12 | Violet(238, 130, 238), White(255, 255, 255), Yellow(255, 255, 0); 13 | -------------------------------------------------------------------------------- /Chapter10/Colors.h: -------------------------------------------------------------------------------- 1 | #ifndef COLOR_H 2 | #define COLOR_H 3 | 4 | #include 5 | 6 | extern QColor 7 | Aqua, Black, Blue, Brown, Cyan, Gray, Green, Lime, Magenta, 8 | Navyblue, Orange, Orchid, Pink, Purple, Red, Silver, 9 | Snow, SteelBlue, SystemColor, Turquoise, Violet, White, Yellow; 10 | 11 | #endif // COLOR_H 12 | -------------------------------------------------------------------------------- /Chapter10/Directive.cpp: -------------------------------------------------------------------------------- 1 | #include "Directive.h" 2 | 3 | Directive::Directive(TokenId directiveId) 4 | :m_directiveId(directiveId) { 5 | // Empty. 6 | } 7 | 8 | Directive::Directive(TokenId directiveId, int address) 9 | :m_directiveId(directiveId), 10 | m_address(address) { 11 | // Empty. 12 | } 13 | 14 | Directive::Directive(TokenId directiveId, const QString& name) 15 | :m_directiveId(directiveId), 16 | m_name(name) { 17 | // Empty. 18 | } 19 | 20 | Directive::Directive(TokenId directiveId, const QString& name, 21 | int parameters) 22 | :m_directiveId(directiveId), 23 | m_name(name), 24 | m_parameters(parameters) { 25 | // Empty. 26 | } 27 | 28 | Directive::Directive(TokenId directiveId, const Value& value) 29 | :m_directiveId(directiveId), 30 | m_value(value) { 31 | // Empty. 32 | } 33 | 34 | Directive::Directive(TokenId directiveId, 35 | const Function& function) 36 | :m_directiveId(directiveId), 37 | m_function(function) { 38 | // Empty. 39 | } 40 | 41 | Directive::Directive(const Directive& directive) 42 | :m_directiveId(directive.m_directiveId), 43 | m_name(directive.m_name), 44 | m_parameters(directive.m_parameters), 45 | m_address(directive.m_address), 46 | m_value(directive.m_value), 47 | m_function(directive.m_function) { 48 | // Empty. 49 | } 50 | 51 | Directive Directive::operator=(const Directive& directive) { 52 | m_directiveId = directive.m_directiveId; 53 | m_name = directive.m_name; 54 | m_parameters = directive.m_parameters; 55 | m_address = directive.m_address; 56 | m_value = directive.m_value; 57 | m_function = directive.m_function; 58 | return *this; 59 | } 60 | -------------------------------------------------------------------------------- /Chapter10/Directive.h: -------------------------------------------------------------------------------- 1 | #ifndef DIRECTIVE_H 2 | #define DIRECTIVE_H 3 | 4 | #include 5 | 6 | #include "Token.h" 7 | #include "Value.h" 8 | #include "Function.h" 9 | 10 | class Directive { 11 | public: 12 | Directive(TokenId tokenId); 13 | Directive(TokenId tokenId, int address); 14 | Directive(TokenId tokenId, const QString& name); 15 | Directive(TokenId tokenId, const QString& name, 16 | int parameters); 17 | Directive(TokenId tokenId, const Value& value); 18 | Directive(TokenId tokenId, const Function& function); 19 | 20 | Directive(const Directive& directive); 21 | Directive operator=(const Directive& directive); 22 | 23 | TokenId directiveId() {return m_directiveId;} 24 | const QString& name() {return m_name;} 25 | const Value& value() {return m_value;} 26 | const Function& function() {return m_function;} 27 | 28 | int parameters() const {return m_parameters;} 29 | int address() const {return m_address;} 30 | void setAddress(int address) {m_address = address;} 31 | 32 | private: 33 | TokenId m_directiveId; 34 | QString m_name; 35 | int m_parameters, m_address; 36 | Value m_value; 37 | Function m_function; 38 | }; 39 | 40 | #endif // DIRECTIVE_H 41 | -------------------------------------------------------------------------------- /Chapter10/Error.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | #include "Error.h" 6 | 7 | extern int g_lineNo = 1; 8 | 9 | void error(const QString& message) { 10 | throw exception(message.toStdString().c_str()); 11 | } 12 | 13 | void syntaxError() { 14 | stringstream stringStream; 15 | stringStream << "Syntax error at line " 16 | << g_lineNo << "."; 17 | error(stringStream.str().c_str()); 18 | } 19 | 20 | void syntaxError(const QString& message) { 21 | stringstream stringStream; 22 | stringStream << "Syntax error at line " << g_lineNo 23 | << ": " << message.toStdString() << "."; 24 | error(stringStream.str().c_str()); 25 | } 26 | 27 | void semanticError(const QString& message) { 28 | stringstream stringStream; 29 | stringStream << "Sematic error: " << message.toStdString() << "."; 30 | error(stringStream.str().c_str()); 31 | } 32 | 33 | void check(bool condition, const QString& message) { 34 | if (!condition) { 35 | semanticError(message); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Chapter10/Error.h: -------------------------------------------------------------------------------- 1 | #ifndef ERROR_H 2 | #define ERROR_H 3 | 4 | #include 5 | 6 | void error(const QString& message); 7 | void syntaxError(); 8 | void syntaxError(const QString& message); 9 | void semanticError(const QString& message); 10 | void check(bool condition, const QString& message); 11 | 12 | #endif // ERROR_H 13 | -------------------------------------------------------------------------------- /Chapter10/Evaluator.h: -------------------------------------------------------------------------------- 1 | #ifndef GENERATOR_H 2 | #define GENERATOR_H 3 | 4 | #include 5 | 6 | #include "Error.h" 7 | #include "Directive.h" 8 | #include "Action.h" 9 | #include "Function.h" 10 | 11 | class Evaluator { 12 | public: 13 | Evaluator(const QList& directiveList, 14 | QList& actionList, 15 | QMap functionMap); 16 | private: 17 | void checkType(TokenId tokenId, const Value& value); 18 | void checkType(TokenId tokenId, const Value& leftValue, 19 | const Value& rightValue); 20 | 21 | Value evaluate(TokenId tokenId, const Value& value); 22 | Value evaluate(TokenId tokenId, const Value& leftValue, 23 | const Value& rightValue); 24 | 25 | QStack m_valueStack; 26 | QMap m_valueMap; 27 | QStack> m_valueMapStack; 28 | QStack m_returnAddressStack; 29 | }; 30 | 31 | #endif // GENERATOR_H 32 | -------------------------------------------------------------------------------- /Chapter10/Function.cpp: -------------------------------------------------------------------------------- 1 | #include "Function.h" 2 | 3 | Function::Function(const QList& nameList, int address) 4 | :m_nameList(nameList), 5 | m_address(address) { 6 | // Empty. 7 | } 8 | 9 | Function::Function(const Function& function) 10 | :m_nameList(function.m_nameList), 11 | m_address(function.m_address) { 12 | // Empty. 13 | } 14 | 15 | Function Function::operator=(const Function& function) { 16 | m_nameList = function.m_nameList; 17 | m_address = function.m_address; 18 | return *this; 19 | } 20 | -------------------------------------------------------------------------------- /Chapter10/Function.h: -------------------------------------------------------------------------------- 1 | #ifndef FUNCTION_H 2 | #define FUNCTION_H 3 | 4 | #include 5 | 6 | #include "Value.h" 7 | #include "Action.h" 8 | 9 | class Function { 10 | public: 11 | Function() {} 12 | Function(const QList& nameList, int address); 13 | const QList& nameList() const {return m_nameList;} 14 | int address() {return m_address;} 15 | 16 | Function(const Function& function); 17 | Function operator=(const Function& function); 18 | 19 | private: 20 | QList m_nameList; 21 | int m_address; 22 | }; 23 | 24 | #endif // FUNCTION_H 25 | -------------------------------------------------------------------------------- /Chapter10/Input.dsl: -------------------------------------------------------------------------------- 1 | function main() { 2 | index = 0; 3 | left = 50; 4 | length = 50; 5 | 6 | while (index < 4) { 7 | if ((index % 2) == 0) { 8 | SetPenStyle(SolidLine); 9 | } 10 | else { 11 | SetPenStyle(DashLine); 12 | } 13 | 14 | topLeft = point(left, 25); 15 | call drawTriangle(topLeft, length); 16 | 17 | length = length + 25; 18 | left = left + length; 19 | index = index + 1; 20 | } 21 | } 22 | 23 | function drawTriangle(topLeft, length) { 24 | topRight = call getTopRight(topLeft, length); 25 | bottomMiddle = call getBottomMiddle(topLeft, length); 26 | 27 | drawLine(topLeft, topRight); 28 | drawLine(topRight, bottomMiddle); 29 | drawLine(bottomMiddle, topLeft); 30 | } 31 | 32 | function getTopRight(topLeft, length) { 33 | return point(xCoordinate(topLeft) + length, 34 | yCoordinate(topLeft)); 35 | } 36 | 37 | function getBottomMiddle(topLeft, length) { 38 | left = xCoordinate(topLeft); 39 | top = yCoordinate(topLeft); 40 | middle = left + length / 2; 41 | bottom = top + length; 42 | return point(middle, bottom); 43 | } 44 | -------------------------------------------------------------------------------- /Chapter10/Main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | #include "Action.h" 7 | #include "Error.h" 8 | #include "Scanner.h" 9 | #include "Parser.h" 10 | #include "Evaluator.h" 11 | #include "ViewerWidget.h" 12 | 13 | int main(int argc, char *argv[]) { 14 | Scanner::init(); 15 | QApplication application(argc, argv); 16 | 17 | try { 18 | QString path = "C:\\Users\\Stefan\\Documents\\A A_Cpp_By_Example\\DSL_Advanced\\Input.dsl"; 19 | 20 | QFile file(path); 21 | if (!file.open(QIODevice::ReadOnly)) { 22 | error("Cannot open file \"" + path + "\" for reading."); 23 | } 24 | 25 | QString buffer(file.readAll()); 26 | Scanner scanner(buffer); 27 | 28 | QList directiveList; 29 | QMap functionMap; 30 | Parser(scanner, directiveList, functionMap); 31 | 32 | QList actionList; 33 | Evaluator generator(directiveList, actionList, functionMap); 34 | 35 | ViewerWidget mainWidget(actionList); 36 | mainWidget.show(); 37 | return application.exec(); 38 | } 39 | catch (exception e) { 40 | QMessageBox messageBox(QMessageBox::Information, 41 | QString("Error"), QString(e.what())); 42 | messageBox.exec(); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Chapter10/Parser.h: -------------------------------------------------------------------------------- 1 | #ifndef PARSER_H 2 | #define PARSER_H 3 | 4 | #include "Directive.h" 5 | #include "Action.h" 6 | #include "Function.h" 7 | #include "Scanner.h" 8 | 9 | class Parser { 10 | public: 11 | Parser(Scanner& m_scanner, QList& directiveList, 12 | QMap& functionMap); 13 | 14 | private: 15 | void match(TokenId tokenId); 16 | 17 | void functionDefinitionList(); 18 | void functionDefinition(); 19 | QList nameList(); 20 | 21 | void instructionList(); 22 | void instruction(); 23 | void callInstruction(); 24 | int expressionList(); 25 | 26 | /* void returnInstruction(); 27 | void ifInstruction(); 28 | void whileInstruction(); 29 | void assignInstruction(); 30 | void blockInstruction();*/ 31 | 32 | void saveInstruction(); 33 | void restoreInstruction(); 34 | void setPenColorInstruction(); 35 | void setPenStyleInstruction(); 36 | void setBrushColorInstruction(); 37 | void setBrushStyleInstruction(); 38 | void setFontInstruction(); 39 | void setHorizontalAlignmentInstruction(); 40 | void setVerticalAlignmentInstruction(); 41 | void setScaleInstruction(); 42 | void setRotateInstruction(); 43 | void setOriginInstruction(); 44 | void drawLineInstruction(); 45 | void drawRectangleInstruction(); 46 | void drawEllipseInstruction(); 47 | void drawTextInstruction(); 48 | 49 | void expression(); 50 | void expressionRest(); 51 | void andExpression(); 52 | void andExpressionRest(); 53 | void relationExpression(); 54 | void relationExpressionRest(); 55 | void termExpression(); 56 | void termExpressionRest(); 57 | void factorExpression(); 58 | void factorExpressionRest(); 59 | void prefixExpression(); 60 | void primaryExpression(); 61 | void pointExpression(); 62 | void coordinateExpression(TokenId tokenId); 63 | void colorExpression(); 64 | void fontExpression(); 65 | void callExpression(); 66 | 67 | private: 68 | Token m_lookAHead; 69 | Scanner& m_scanner; 70 | QMap& m_functionMap; 71 | QList& m_directiveList; 72 | }; 73 | 74 | #endif // PARSER_H 75 | -------------------------------------------------------------------------------- /Chapter10/Scanner.h: -------------------------------------------------------------------------------- 1 | #ifndef SCANNER_H 2 | #define SCANNER_H 3 | 4 | #include "Token.h" 5 | 6 | class Scanner { 7 | public: 8 | static void init(); 9 | Scanner(QString& buffer); 10 | 11 | public: 12 | Token NextToken(); 13 | bool ScanValue(double& value); 14 | 15 | private: 16 | QString m_buffer; 17 | static QMap m_valueMap; 18 | static QMap m_keywordMap; 19 | static QList> m_tokenList; 20 | }; 21 | 22 | #endif // SCANNER_H 23 | -------------------------------------------------------------------------------- /Chapter10/Token.cpp: -------------------------------------------------------------------------------- 1 | #include "Token.h" 2 | 3 | Token::Token() 4 | :m_tokenId(EndOfFileId) { 5 | // Empty. 6 | } 7 | 8 | Token::Token(TokenId tokenId) 9 | :m_tokenId(tokenId) { 10 | // Empty. 11 | } 12 | 13 | Token::Token(TokenId tokenId, const QString& name) 14 | :m_tokenId(tokenId), 15 | m_name(name) { 16 | // Empty. 17 | } 18 | 19 | Token::Token(TokenId tokenId, const Value& value) 20 | :m_tokenId(tokenId), 21 | m_value(value) { 22 | // Empty. 23 | } 24 | 25 | TokenId m_tokenId; 26 | QString m_name; 27 | Value m_value; 28 | 29 | Token::Token(const Token& token) 30 | :m_tokenId(token.m_tokenId), 31 | m_name(token.m_name), 32 | m_value(token.m_value) { 33 | // Empty. 34 | } 35 | 36 | Token Token::operator=(const Token& token) { 37 | m_tokenId = token.m_tokenId; 38 | m_name = token.m_name; 39 | m_value = token.m_value; 40 | return *this; 41 | } 42 | -------------------------------------------------------------------------------- /Chapter10/Token.h: -------------------------------------------------------------------------------- 1 | #ifndef TOKEN_H 2 | #define TOKEN_H 3 | 4 | #include 5 | #include "Value.h" 6 | 7 | enum TokenId {BlockId, CallId, ElseId, FunctionId, GotoId, 8 | IfId, IfNotGotoId, ReturnId, WhileId, 9 | ColorId, DrawEllipseId, 10 | DrawLineId, DrawRectangleId, DrawTextId, 11 | FontId, PointId, RestoreId, 12 | SaveId, SetRotateId, SetBrushColorId, 13 | SetBrushStyleId, SetFontId, 14 | SetHorizontalAlignmentId, SetOriginId, 15 | SetPenColorId, SetPenStyleId, SetScaleId, 16 | SetVerticalAlignmentId, 17 | XCoordinateId, YCoordinateId, 18 | AndId, OrId, NotId, 19 | AddId, UnaryAddId, SubtractId, UnarySubtractId, 20 | MultiplyId, DivideId, ModulusId, 21 | EqualId, NotEqualId, LessThanId, LessThanEqualId, 22 | GreaterThanId, GreaterThanEqualId, 23 | LeftParenthesisId, RightParenthesisId, 24 | LeftBracketId, RightBracketId, 25 | AssignId, CommaId, SemicolonId, 26 | NameId, ValueId, EndOfFileId}; 27 | 28 | class Token{ 29 | public: 30 | Token(); 31 | Token(TokenId tokenId); 32 | Token(TokenId tokenId, const QString& name); 33 | Token(TokenId tokenId, const Value& value); 34 | 35 | Token(const Token& token); 36 | Token operator=(const Token& token); 37 | 38 | TokenId id() const {return m_tokenId;} 39 | const QString& name() const { return m_name; } 40 | const Value& value() const { return m_value; } 41 | 42 | private: 43 | TokenId m_tokenId; 44 | QString m_name; 45 | Value m_value; 46 | }; 47 | 48 | #endif // TOKEN_H 49 | -------------------------------------------------------------------------------- /Chapter10/ViewerWidget.h: -------------------------------------------------------------------------------- 1 | #ifndef MAINWIDGET_H 2 | #define MAINWIDGET_H 3 | 4 | #include 5 | #include 6 | #include "Value.h" 7 | #include "Colors.h" 8 | #include "Action.h" 9 | 10 | class ViewerWidget : public QWidget { 11 | Q_OBJECT 12 | 13 | public: 14 | ViewerWidget(const QList& actionList, 15 | QWidget *parentTableWidget = nullptr); 16 | 17 | void paintEvent(QPaintEvent *eventPtr) /*override*/; 18 | 19 | private: 20 | Qt::Alignment m_horizontalAlignment = Qt::AlignHCenter, 21 | m_verticalAlignment = Qt::AlignVCenter; 22 | const QList& m_actionList; 23 | }; 24 | 25 | #endif // MAINWIDGET_H 26 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Packt 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # C++17 By Example 2 | This is the code repository for [C++17 By Example](https://www.packtpub.com/application-development/c17-example?utm_source=github&utm_medium=repository&utm_campaign=9781788391818), published by [Packt](https://www.packtpub.com/?utm_source=github). It contains all the supporting project files necessary to work through the book from start to finish. 3 | ## About the Book 4 | C++ is a general-purpose programming language built with a bias towards embedded programming and systems programming. Over the years, C++ has evolved and is used to develop software for many different sectors. Given its versatility and robustness, C++is a wonderful language to start your coding journey with. This book covers exciting projects built in C++ that show how to implement the language in different scenarios. While developing these projects, you will not only learn the language constructs but also how you can use C++ to meet your software requirements. 5 | ## Instructions and Navigation 6 | All of the code is organized into folders. Each folder starts with a number followed by the application name. For example, Chapter02. 7 | 8 | All code files are present in their respective folders. 9 | 10 | The code will look like the following: 11 | ``` 12 | class Person { 13 | public: 14 | Person(string name); 15 | virtual void print(); 16 | private: 17 | string m_name; 18 | }; 19 | ``` 20 | 21 | This book is intended for every reader, from the beginner to the more proficient C++ 22 | programmer. However, some previous experience with C++ is useful. 23 | The examples of this book are developed in Visual Studio and Qt Creator. 24 | 25 | ## Related Products 26 | * [Beginning C++ Programming](https://www.packtpub.com/application-development/beginning-c-programming?utm_source=github&utm_medium=repository&utm_campaign=9781787124943) 27 | 28 | * [Modern C++ Programming Cookbook](https://www.packtpub.com/application-development/modern-c-programming-cookbook?utm_source=github&utm_medium=repository&utm_campaign=9781786465184) 29 | 30 | * [Mastering C++ Programming](https://www.packtpub.com/application-development/mastering-c-programming?utm_source=github&utm_medium=repository&utm_campaign=9781786461629) 31 | 32 | ### Suggestions and Feedback 33 | [Click here](https://docs.google.com/forms/d/e/1FAIpQLSe5qwunkGf6PUvzPirPDtuy1Du5Rlzew23UBp2S-P3wB-GcwQ/viewform) if you have any feedback or suggestions. 34 | --------------------------------------------------------------------------------