author | František Kučera <franta-hg@frantovo.cz> |
Tue, 30 Oct 2018 23:52:55 +0100 | |
branch | v_0 |
changeset 27 | 8c5364450a46 |
parent 20 | b13e7ed9eea3 |
child 28 | 04f1ac8a931b |
permissions | -rw-r--r-- |
0
ba16fce7ef20
create cmake+netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
1 |
#include <iostream> |
ba16fce7ef20
create cmake+netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
2 |
|
3
0d857519a4c9
add Qt libraries using cmake
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
3 |
#include <QApplication> |
13
c84042d014c9
background thread for STDIN reading + proxy with signals/slots for passing data to the GUI thread
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
4 |
#include <QThread> |
3
0d857519a4c9
add Qt libraries using cmake
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
5 |
|
2
7ba3ae8cf1fc
add relpipe pkg-config libraries using cmake
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
6 |
#include <relpipe/cli/CLI.h> |
7ba3ae8cf1fc
add relpipe pkg-config libraries using cmake
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
7 |
#include <relpipe/cli/RelpipeCLIException.h> |
7ba3ae8cf1fc
add relpipe pkg-config libraries using cmake
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
8 |
#include <relpipe/reader/Factory.h> |
7ba3ae8cf1fc
add relpipe pkg-config libraries using cmake
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
9 |
#include <relpipe/reader/RelationalReader.h> |
7ba3ae8cf1fc
add relpipe pkg-config libraries using cmake
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
10 |
#include <relpipe/reader/RelpipeReaderException.h> |
7ba3ae8cf1fc
add relpipe pkg-config libraries using cmake
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
11 |
|
4
1622c087f3ea
add main window, compile Qt .ui file
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
12 |
#include "RelpipeChartMainWindow.h" |
13
c84042d014c9
background thread for STDIN reading + proxy with signals/slots for passing data to the GUI thread
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
13 |
#include "QtRelationalReaderStringHadler.h" |
2
7ba3ae8cf1fc
add relpipe pkg-config libraries using cmake
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
14 |
|
7ba3ae8cf1fc
add relpipe pkg-config libraries using cmake
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
15 |
using namespace relpipe::cli; |
7ba3ae8cf1fc
add relpipe pkg-config libraries using cmake
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
16 |
using namespace relpipe::reader; |
7ba3ae8cf1fc
add relpipe pkg-config libraries using cmake
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
17 |
|
20
b13e7ed9eea3
better use of signal/slot mechanism, threads, Qt::ConnectionType::QueuedConnection
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
18 |
// signal/slot parameters must be declared here and registered with qRegisterMetaType() |
b13e7ed9eea3
better use of signal/slot mechanism, threads, Qt::ConnectionType::QueuedConnection
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
19 |
|
b13e7ed9eea3
better use of signal/slot mechanism, threads, Qt::ConnectionType::QueuedConnection
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
20 |
Q_DECLARE_METATYPE(string_t) |
b13e7ed9eea3
better use of signal/slot mechanism, threads, Qt::ConnectionType::QueuedConnection
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
21 |
Q_DECLARE_METATYPE(std::vector<AttributeMetadata>) |
b13e7ed9eea3
better use of signal/slot mechanism, threads, Qt::ConnectionType::QueuedConnection
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
22 |
|
17
dad72beb3ebe
rename background thread to RelationalReaderThread
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
23 |
class RelationalReaderThread : public QThread { |
13
c84042d014c9
background thread for STDIN reading + proxy with signals/slots for passing data to the GUI thread
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
24 |
private: |
c84042d014c9
background thread for STDIN reading + proxy with signals/slots for passing data to the GUI thread
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
25 |
std::shared_ptr<RelationalReader> reader; |
c84042d014c9
background thread for STDIN reading + proxy with signals/slots for passing data to the GUI thread
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
26 |
public: |
c84042d014c9
background thread for STDIN reading + proxy with signals/slots for passing data to the GUI thread
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
27 |
|
c84042d014c9
background thread for STDIN reading + proxy with signals/slots for passing data to the GUI thread
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
28 |
// TODO: better background thread; lambda? |
c84042d014c9
background thread for STDIN reading + proxy with signals/slots for passing data to the GUI thread
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
29 |
|
17
dad72beb3ebe
rename background thread to RelationalReaderThread
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
30 |
RelationalReaderThread(std::shared_ptr<RelationalReader> reader) : |
13
c84042d014c9
background thread for STDIN reading + proxy with signals/slots for passing data to the GUI thread
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
31 |
reader(reader) { |
16
b6f790137bb8
Do not destroy background thread before application end.
František Kučera <franta-hg@frantovo.cz>
parents:
13
diff
changeset
|
32 |
setTerminationEnabled(true); |
13
c84042d014c9
background thread for STDIN reading + proxy with signals/slots for passing data to the GUI thread
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
33 |
} |
c84042d014c9
background thread for STDIN reading + proxy with signals/slots for passing data to the GUI thread
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
34 |
|
c84042d014c9
background thread for STDIN reading + proxy with signals/slots for passing data to the GUI thread
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
35 |
void run() { |
c84042d014c9
background thread for STDIN reading + proxy with signals/slots for passing data to the GUI thread
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
36 |
try { |
c84042d014c9
background thread for STDIN reading + proxy with signals/slots for passing data to the GUI thread
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
37 |
reader->process(); |
c84042d014c9
background thread for STDIN reading + proxy with signals/slots for passing data to the GUI thread
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
38 |
} catch (RelpipeReaderException& e) { |
c84042d014c9
background thread for STDIN reading + proxy with signals/slots for passing data to the GUI thread
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
39 |
// TODO: handle exception, show error dialog |
16
b6f790137bb8
Do not destroy background thread before application end.
František Kučera <franta-hg@frantovo.cz>
parents:
13
diff
changeset
|
40 |
std::wcerr << L"RelpipeReaderException: " << e.getMessge() << std::endl; |
13
c84042d014c9
background thread for STDIN reading + proxy with signals/slots for passing data to the GUI thread
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
41 |
} |
c84042d014c9
background thread for STDIN reading + proxy with signals/slots for passing data to the GUI thread
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
42 |
} |
c84042d014c9
background thread for STDIN reading + proxy with signals/slots for passing data to the GUI thread
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
43 |
}; |
c84042d014c9
background thread for STDIN reading + proxy with signals/slots for passing data to the GUI thread
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
44 |
|
0
ba16fce7ef20
create cmake+netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
45 |
int main(int argc, char**argv) { |
2
7ba3ae8cf1fc
add relpipe pkg-config libraries using cmake
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
46 |
CLI cli(argc, argv); |
9
d88df75dc2eb
comment on possible CLI argument collision
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
47 |
// TODO: argument name collisions? Filter arguments? Use prefix for Qt? Qt: -title, -style, -geometry |
3
0d857519a4c9
add Qt libraries using cmake
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
48 |
QApplication app(argc, argv); |
13
c84042d014c9
background thread for STDIN reading + proxy with signals/slots for passing data to the GUI thread
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
49 |
|
2
7ba3ae8cf1fc
add relpipe pkg-config libraries using cmake
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
50 |
std::shared_ptr<RelationalReader> reader(Factory::create(std::cin)); |
7ba3ae8cf1fc
add relpipe pkg-config libraries using cmake
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
51 |
int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR; |
13
c84042d014c9
background thread for STDIN reading + proxy with signals/slots for passing data to the GUI thread
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
52 |
|
4
1622c087f3ea
add main window, compile Qt .ui file
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
53 |
RelpipeChartMainWindow window; |
1622c087f3ea
add main window, compile Qt .ui file
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
54 |
window.show(); |
13
c84042d014c9
background thread for STDIN reading + proxy with signals/slots for passing data to the GUI thread
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
55 |
|
20
b13e7ed9eea3
better use of signal/slot mechanism, threads, Qt::ConnectionType::QueuedConnection
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
56 |
RelationalReaderThread t(reader); |
b13e7ed9eea3
better use of signal/slot mechanism, threads, Qt::ConnectionType::QueuedConnection
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
57 |
|
b13e7ed9eea3
better use of signal/slot mechanism, threads, Qt::ConnectionType::QueuedConnection
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
58 |
// Proxy that passes calls from the background thread to the GUI thread using signal-slot mechanism: |
b13e7ed9eea3
better use of signal/slot mechanism, threads, Qt::ConnectionType::QueuedConnection
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
59 |
QtRelationalReaderStringHadler handler(&t); // &t instead of handler.moveToThread(&t); // QObject::moveToThread: Cannot move objects with a parent |
b13e7ed9eea3
better use of signal/slot mechanism, threads, Qt::ConnectionType::QueuedConnection
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
60 |
|
b13e7ed9eea3
better use of signal/slot mechanism, threads, Qt::ConnectionType::QueuedConnection
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
61 |
// see Q_DECLARE_METATYPE above |
b13e7ed9eea3
better use of signal/slot mechanism, threads, Qt::ConnectionType::QueuedConnection
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
62 |
qRegisterMetaType<string_t>(); |
b13e7ed9eea3
better use of signal/slot mechanism, threads, Qt::ConnectionType::QueuedConnection
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
63 |
qRegisterMetaType<std::vector < AttributeMetadata >> (); |
b13e7ed9eea3
better use of signal/slot mechanism, threads, Qt::ConnectionType::QueuedConnection
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
64 |
|
b13e7ed9eea3
better use of signal/slot mechanism, threads, Qt::ConnectionType::QueuedConnection
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
65 |
QObject::connect(&handler, &QtRelationalReaderStringHadler::startRelationReceived, &window, &RelpipeChartMainWindow::startRelation, Qt::ConnectionType::QueuedConnection); |
b13e7ed9eea3
better use of signal/slot mechanism, threads, Qt::ConnectionType::QueuedConnection
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
66 |
QObject::connect(&handler, &QtRelationalReaderStringHadler::attributeReceived, &window, &RelpipeChartMainWindow::attribute, Qt::ConnectionType::QueuedConnection); |
b13e7ed9eea3
better use of signal/slot mechanism, threads, Qt::ConnectionType::QueuedConnection
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
67 |
QObject::connect(&handler, &QtRelationalReaderStringHadler::endOfPipeReceived, &window, &RelpipeChartMainWindow::endOfPipe, Qt::ConnectionType::QueuedConnection); |
b13e7ed9eea3
better use of signal/slot mechanism, threads, Qt::ConnectionType::QueuedConnection
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
68 |
|
13
c84042d014c9
background thread for STDIN reading + proxy with signals/slots for passing data to the GUI thread
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
69 |
reader->addHandler(&handler); |
c84042d014c9
background thread for STDIN reading + proxy with signals/slots for passing data to the GUI thread
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
70 |
|
c84042d014c9
background thread for STDIN reading + proxy with signals/slots for passing data to the GUI thread
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
71 |
// Start background thread |
c84042d014c9
background thread for STDIN reading + proxy with signals/slots for passing data to the GUI thread
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
72 |
t.start(); |
c84042d014c9
background thread for STDIN reading + proxy with signals/slots for passing data to the GUI thread
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
73 |
|
19
ac70c7af6a9b
code clean-up, exit code
František Kučera <franta-hg@frantovo.cz>
parents:
18
diff
changeset
|
74 |
int qtResultCode = app.exec(); |
13
c84042d014c9
background thread for STDIN reading + proxy with signals/slots for passing data to the GUI thread
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
75 |
|
19
ac70c7af6a9b
code clean-up, exit code
František Kučera <franta-hg@frantovo.cz>
parents:
18
diff
changeset
|
76 |
if (qtResultCode == 0) { |
ac70c7af6a9b
code clean-up, exit code
František Kučera <franta-hg@frantovo.cz>
parents:
18
diff
changeset
|
77 |
resultCode = CLI::EXIT_CODE_SUCCESS; |
ac70c7af6a9b
code clean-up, exit code
František Kučera <franta-hg@frantovo.cz>
parents:
18
diff
changeset
|
78 |
} else { |
ac70c7af6a9b
code clean-up, exit code
František Kučera <franta-hg@frantovo.cz>
parents:
18
diff
changeset
|
79 |
// TODO: report and log Qt errors if any |
ac70c7af6a9b
code clean-up, exit code
František Kučera <franta-hg@frantovo.cz>
parents:
18
diff
changeset
|
80 |
} |
2
7ba3ae8cf1fc
add relpipe pkg-config libraries using cmake
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
81 |
|
16
b6f790137bb8
Do not destroy background thread before application end.
František Kučera <franta-hg@frantovo.cz>
parents:
13
diff
changeset
|
82 |
if (t.isRunning()) { |
b6f790137bb8
Do not destroy background thread before application end.
František Kučera <franta-hg@frantovo.cz>
parents:
13
diff
changeset
|
83 |
std::wcerr << L"Background RelationalReader thread is still running → terminate()" << std::endl; |
b6f790137bb8
Do not destroy background thread before application end.
František Kučera <franta-hg@frantovo.cz>
parents:
13
diff
changeset
|
84 |
t.terminate(); |
b6f790137bb8
Do not destroy background thread before application end.
František Kučera <franta-hg@frantovo.cz>
parents:
13
diff
changeset
|
85 |
std::wcerr << L"Background RelationalReader thread was terminated → wait()" << std::endl; |
b6f790137bb8
Do not destroy background thread before application end.
František Kučera <franta-hg@frantovo.cz>
parents:
13
diff
changeset
|
86 |
t.wait(); |
b6f790137bb8
Do not destroy background thread before application end.
František Kučera <franta-hg@frantovo.cz>
parents:
13
diff
changeset
|
87 |
std::wcerr << L"Background RelationalReader thread wait() finished" << std::endl; |
b6f790137bb8
Do not destroy background thread before application end.
František Kučera <franta-hg@frantovo.cz>
parents:
13
diff
changeset
|
88 |
} |
b6f790137bb8
Do not destroy background thread before application end.
František Kučera <franta-hg@frantovo.cz>
parents:
13
diff
changeset
|
89 |
|
2
7ba3ae8cf1fc
add relpipe pkg-config libraries using cmake
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
90 |
return resultCode; |
0
ba16fce7ef20
create cmake+netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
91 |
} |