src/relpipe-out-gui.cpp
author František Kučera <franta-hg@frantovo.cz>
Fri, 26 Aug 2022 22:36:12 +0200
branchv_0
changeset 54 259f99938898
parent 42 707a6734f364
permissions -rw-r--r--
exception: fix typo: getMessge() → getMessage()
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29
0f9f7d6564cd license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
     1
/**
0f9f7d6564cd license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
     2
 * Relational pipes
0f9f7d6564cd license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
     3
 * Copyright © 2018 František Kučera (Frantovo.cz, GlobalCode.info)
0f9f7d6564cd license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
     4
 *
0f9f7d6564cd license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
0f9f7d6564cd license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
42
707a6734f364 fix license version: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents: 38
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
29
0f9f7d6564cd license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
     8
 *
0f9f7d6564cd license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
0f9f7d6564cd license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0f9f7d6564cd license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0f9f7d6564cd license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    12
 * GNU General Public License for more details.
0f9f7d6564cd license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    13
 *
0f9f7d6564cd license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    14
 * You should have received a copy of the GNU General Public License
0f9f7d6564cd license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
0f9f7d6564cd license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    16
 */
28
04f1ac8a931b Qt Charts: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
    17
#include <sys/prctl.h>
04f1ac8a931b Qt Charts: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
    18
#include <stdio.h>
04f1ac8a931b Qt Charts: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
    19
#include <unistd.h>
0
ba16fce7ef20 create cmake+netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <iostream>
ba16fce7ef20 create cmake+netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
3
0d857519a4c9 add Qt libraries using cmake
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    22
#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
    23
#include <QThread>
3
0d857519a4c9 add Qt libraries using cmake
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    24
2
7ba3ae8cf1fc add relpipe pkg-config libraries using cmake
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    25
#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
    26
#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
    27
#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
    28
#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
    29
#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
    30
4
1622c087f3ea add main window, compile Qt .ui file
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    31
#include "RelpipeChartMainWindow.h"
38
129b8cca9b98 fix typo: Hadler → Handler
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    32
#include "QtRelationalReaderStringHandler.h"
2
7ba3ae8cf1fc add relpipe pkg-config libraries using cmake
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    33
7ba3ae8cf1fc add relpipe pkg-config libraries using cmake
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    34
using namespace relpipe::cli;
7ba3ae8cf1fc add relpipe pkg-config libraries using cmake
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    35
using namespace relpipe::reader;
7ba3ae8cf1fc add relpipe pkg-config libraries using cmake
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    36
20
b13e7ed9eea3 better use of signal/slot mechanism, threads, Qt::ConnectionType::QueuedConnection
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
    37
// 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
    38
b13e7ed9eea3 better use of signal/slot mechanism, threads, Qt::ConnectionType::QueuedConnection
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
    39
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
    40
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
    41
17
dad72beb3ebe rename background thread to RelationalReaderThread
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    42
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
    43
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
    44
	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
    45
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
    46
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
    47
	// 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
    48
17
dad72beb3ebe rename background thread to RelationalReaderThread
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    49
	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
    50
	reader(reader) {
16
b6f790137bb8 Do not destroy background thread before application end.
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
    51
		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
    52
	}
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
    53
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
    54
	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
    55
		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
    56
			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
    57
		} 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
    58
			// TODO: handle exception, show error dialog
54
259f99938898 exception: fix typo: getMessge() → getMessage()
František Kučera <franta-hg@frantovo.cz>
parents: 42
diff changeset
    59
			std::wcerr << L"RelpipeReaderException: " << e.getMessage() << 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
    60
		}
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
    61
	}
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
    62
};
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
    63
0
ba16fce7ef20 create cmake+netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
int main(int argc, char**argv) {
31
47fb4bb3db45 CLI: untieStdIO() to avoid unwanted implicit flush() calls on std::cout → less write() calls
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
    65
	setlocale(LC_ALL, "");
47fb4bb3db45 CLI: untieStdIO() to avoid unwanted implicit flush() calls on std::cout → less write() calls
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
    66
	CLI::untieStdIO();
2
7ba3ae8cf1fc add relpipe pkg-config libraries using cmake
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    67
	CLI cli(argc, argv);
9
d88df75dc2eb comment on possible CLI argument collision
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    68
	// 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
    69
	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
    70
2
7ba3ae8cf1fc add relpipe pkg-config libraries using cmake
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    71
	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
    72
	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
    73
4
1622c087f3ea add main window, compile Qt .ui file
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    74
	RelpipeChartMainWindow window;
1622c087f3ea add main window, compile Qt .ui file
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    75
	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
    76
20
b13e7ed9eea3 better use of signal/slot mechanism, threads, Qt::ConnectionType::QueuedConnection
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
    77
	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
    78
b13e7ed9eea3 better use of signal/slot mechanism, threads, Qt::ConnectionType::QueuedConnection
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
    79
	// Proxy that passes calls from the background thread to the GUI thread using signal-slot mechanism:
38
129b8cca9b98 fix typo: Hadler → Handler
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    80
	QtRelationalReaderStringHandler handler(&t); // &t instead of handler.moveToThread(&t); // QObject::moveToThread: Cannot move objects with a parent
20
b13e7ed9eea3 better use of signal/slot mechanism, threads, Qt::ConnectionType::QueuedConnection
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
    81
b13e7ed9eea3 better use of signal/slot mechanism, threads, Qt::ConnectionType::QueuedConnection
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
    82
	// 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
    83
	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
    84
	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
    85
38
129b8cca9b98 fix typo: Hadler → Handler
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    86
	QObject::connect(&handler, &QtRelationalReaderStringHandler::startRelationReceived, &window, &RelpipeChartMainWindow::startRelation, Qt::ConnectionType::QueuedConnection);
129b8cca9b98 fix typo: Hadler → Handler
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    87
	QObject::connect(&handler, &QtRelationalReaderStringHandler::attributeReceived, &window, &RelpipeChartMainWindow::attribute, Qt::ConnectionType::QueuedConnection);
129b8cca9b98 fix typo: Hadler → Handler
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    88
	QObject::connect(&handler, &QtRelationalReaderStringHandler::endOfPipeReceived, &window, &RelpipeChartMainWindow::endOfPipe, Qt::ConnectionType::QueuedConnection);
20
b13e7ed9eea3 better use of signal/slot mechanism, threads, Qt::ConnectionType::QueuedConnection
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
    89
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
    90
	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
    91
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
    92
	// 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
    93
	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
    94
19
ac70c7af6a9b code clean-up, exit code
František Kučera <franta-hg@frantovo.cz>
parents: 18
diff changeset
    95
	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
    96
19
ac70c7af6a9b code clean-up, exit code
František Kučera <franta-hg@frantovo.cz>
parents: 18
diff changeset
    97
	if (qtResultCode == 0) {
ac70c7af6a9b code clean-up, exit code
František Kučera <franta-hg@frantovo.cz>
parents: 18
diff changeset
    98
		resultCode = CLI::EXIT_CODE_SUCCESS;
ac70c7af6a9b code clean-up, exit code
František Kučera <franta-hg@frantovo.cz>
parents: 18
diff changeset
    99
	} else {
ac70c7af6a9b code clean-up, exit code
František Kučera <franta-hg@frantovo.cz>
parents: 18
diff changeset
   100
		// 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
   101
	}
2
7ba3ae8cf1fc add relpipe pkg-config libraries using cmake
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   102
16
b6f790137bb8 Do not destroy background thread before application end.
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   103
	if (t.isRunning()) {
b6f790137bb8 Do not destroy background thread before application end.
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   104
		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
   105
		t.terminate();
b6f790137bb8 Do not destroy background thread before application end.
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   106
		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
   107
		t.wait();
b6f790137bb8 Do not destroy background thread before application end.
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   108
		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
   109
	}
b6f790137bb8 Do not destroy background thread before application end.
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   110
2
7ba3ae8cf1fc add relpipe pkg-config libraries using cmake
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   111
	return resultCode;
0
ba16fce7ef20 create cmake+netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   112
}