src/relpipe-out-gui.cpp
branchv_0
changeset 30 23354fc917f4
parent 29 0f9f7d6564cd
child 31 47fb4bb3db45
equal deleted inserted replaced
29:0f9f7d6564cd 30:23354fc917f4
       
     1 /**
       
     2  * Relational pipes
       
     3  * Copyright © 2018 František Kučera (Frantovo.cz, GlobalCode.info)
       
     4  *
       
     5  * This program is free software: you can redistribute it and/or modify
       
     6  * it under the terms of the GNU General Public License as published by
       
     7  * the Free Software Foundation, either version 3 of the License, or
       
     8  * (at your option) any later version.
       
     9  *
       
    10  * This program is distributed in the hope that it will be useful,
       
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
       
    13  * GNU General Public License for more details.
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License
       
    16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
       
    17  */
       
    18 #include <sys/prctl.h>
       
    19 #include <stdio.h>
       
    20 #include <unistd.h>
       
    21 #include <iostream>
       
    22 
       
    23 #include <QApplication>
       
    24 #include <QThread>
       
    25 
       
    26 #include <relpipe/cli/CLI.h>
       
    27 #include <relpipe/cli/RelpipeCLIException.h>
       
    28 #include <relpipe/reader/Factory.h>
       
    29 #include <relpipe/reader/RelationalReader.h>
       
    30 #include <relpipe/reader/RelpipeReaderException.h>
       
    31 
       
    32 #include "RelpipeChartMainWindow.h"
       
    33 #include "QtRelationalReaderStringHadler.h"
       
    34 
       
    35 using namespace relpipe::cli;
       
    36 using namespace relpipe::reader;
       
    37 
       
    38 // signal/slot parameters must be declared here and registered with qRegisterMetaType()
       
    39 
       
    40 Q_DECLARE_METATYPE(string_t)
       
    41 Q_DECLARE_METATYPE(std::vector<AttributeMetadata>)
       
    42 
       
    43 class RelationalReaderThread : public QThread {
       
    44 private:
       
    45 	std::shared_ptr<RelationalReader> reader;
       
    46 public:
       
    47 
       
    48 	// TODO: better background thread; lambda?
       
    49 
       
    50 	RelationalReaderThread(std::shared_ptr<RelationalReader> reader) :
       
    51 	reader(reader) {
       
    52 		setTerminationEnabled(true);
       
    53 	}
       
    54 
       
    55 	void run() {
       
    56 		try {
       
    57 			reader->process();
       
    58 		} catch (RelpipeReaderException& e) {
       
    59 			// TODO: handle exception, show error dialog
       
    60 			std::wcerr << L"RelpipeReaderException: " << e.getMessge() << std::endl;
       
    61 		}
       
    62 	}
       
    63 };
       
    64 
       
    65 int main(int argc, char**argv) {
       
    66 	CLI cli(argc, argv);
       
    67 	// TODO: argument name collisions? Filter arguments? Use prefix for Qt? Qt: -title, -style, -geometry
       
    68 	QApplication app(argc, argv);
       
    69 
       
    70 	std::shared_ptr<RelationalReader> reader(Factory::create(std::cin));
       
    71 	int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR;
       
    72 
       
    73 	RelpipeChartMainWindow window;
       
    74 	window.show();
       
    75 
       
    76 	RelationalReaderThread t(reader);
       
    77 
       
    78 	// Proxy that passes calls from the background thread to the GUI thread using signal-slot mechanism:
       
    79 	QtRelationalReaderStringHadler handler(&t); // &t instead of handler.moveToThread(&t); // QObject::moveToThread: Cannot move objects with a parent
       
    80 
       
    81 	// see Q_DECLARE_METATYPE above
       
    82 	qRegisterMetaType<string_t>();
       
    83 	qRegisterMetaType<std::vector < AttributeMetadata >> ();
       
    84 
       
    85 	QObject::connect(&handler, &QtRelationalReaderStringHadler::startRelationReceived, &window, &RelpipeChartMainWindow::startRelation, Qt::ConnectionType::QueuedConnection);
       
    86 	QObject::connect(&handler, &QtRelationalReaderStringHadler::attributeReceived, &window, &RelpipeChartMainWindow::attribute, Qt::ConnectionType::QueuedConnection);
       
    87 	QObject::connect(&handler, &QtRelationalReaderStringHadler::endOfPipeReceived, &window, &RelpipeChartMainWindow::endOfPipe, Qt::ConnectionType::QueuedConnection);
       
    88 
       
    89 	reader->addHandler(&handler);
       
    90 
       
    91 	// Start background thread
       
    92 	t.start();
       
    93 
       
    94 	int qtResultCode = app.exec();
       
    95 
       
    96 	if (qtResultCode == 0) {
       
    97 		resultCode = CLI::EXIT_CODE_SUCCESS;
       
    98 	} else {
       
    99 		// TODO: report and log Qt errors if any
       
   100 	}
       
   101 
       
   102 	if (t.isRunning()) {
       
   103 		std::wcerr << L"Background RelationalReader thread is still running → terminate()" << std::endl;
       
   104 		t.terminate();
       
   105 		std::wcerr << L"Background RelationalReader thread was terminated → wait()" << std::endl;
       
   106 		t.wait();
       
   107 		std::wcerr << L"Background RelationalReader thread wait() finished" << std::endl;
       
   108 	}
       
   109 
       
   110 	return resultCode;
       
   111 }