diff --git a/src/Lib/session.cpp b/src/Lib/session.cpp index 5dbf45d..5fb3e78 100644 --- a/src/Lib/session.cpp +++ b/src/Lib/session.cpp @@ -19,8 +19,6 @@ #include -#include - #include "session.h" #include "settings.h" @@ -29,7 +27,8 @@ extern std::unique_ptr< lc::Settings > settings; lc::Session::Session( const QString &argZTreeDataTargetPath, const quint16 argZTreePort, const QString &argZTreeVersionPath, bool argPrintReceiptsForLocalClients, const QString &argAnonymousReceiptsPlaceholder, - const QString &argLatexHeaderName ): + const QString &argLatexHeaderName, QObject *argParent ): + QObject{ argParent }, zTreePort{ argZTreePort }, anonymousReceiptsPlaceholder{ argAnonymousReceiptsPlaceholder }, latexHeaderName{ argLatexHeaderName }, @@ -79,7 +78,9 @@ void lc::Session::InitializeClasses() { zTreeDataTargetPath.append( "/" + date_string + "-" + QString::number( zTreePort ) ); qDebug() << "New session's chosen_zTree_data_target_path:" << zTreeDataTargetPath; - zTreeInstance = new ZTree{ zTreeDataTargetPath, zTreePort, zTreeVersionPath }; + zTreeInstance = new ZTree{ zTreeDataTargetPath, zTreePort, zTreeVersionPath, this }; + connect( zTreeInstance, &ZTree::ZTreeClosed, + this, &Session::OnzTreeClosed ); // Only create a 'Receipts_Handler' instance, if all neccessary variables were set if ( latexHeaderName != "None found" && !settings->dvipsCmd.isEmpty() && !settings->latexCmd.isEmpty() ) { @@ -92,6 +93,11 @@ void lc::Session::InitializeClasses() { } } +void lc::Session::OnzTreeClosed( int argExitCode ) { + qDebug() << "z-Tree running on port" << zTreePort << "closed with exit code" << argExitCode; + emit SessionFinished( this ); +} + void lc::Session::RenameWindow() { // Example: wmctrl -r -T diff --git a/src/Lib/session.h b/src/Lib/session.h index a345137..d76f49f 100644 --- a/src/Lib/session.h +++ b/src/Lib/session.h @@ -39,8 +39,9 @@ public: const int zTreePort = 7000; //! The port this session's zTree instance is running on Session( const QString &argZTreeDataTargetPath, const quint16 argZTreePort, - const QString &argZTreeVersionPath, bool argPrintReceiptsForLocalClients, const QString &argAnonymousReceiptsPlaceholder, - const QString &argLatexHeaderName ); + const QString &argZTreeVersionPath, bool argPrintReceiptsForLocalClients, + const QString &argAnonymousReceiptsPlaceholder, + const QString &argLatexHeaderName, QObject *argParent = nullptr ); ~Session(); /*! Returns the data item with the given index @@ -53,12 +54,13 @@ public: class lcDataTargetPathCreationFailed {}; signals: - // void session_started(); + void SessionFinished( Session *argSession ); private slots: /*! Starts the session by creating instances of the relevant classes */ void InitializeClasses(); + void OnzTreeClosed( int argExitCode ); /*! Changes zTree's window title to contain its port number to make zTree windows distinguishable */ void RenameWindow(); diff --git a/src/Lib/sessionsmodel.cpp b/src/Lib/sessionsmodel.cpp index 6f1af8c..4829097 100644 --- a/src/Lib/sessionsmodel.cpp +++ b/src/Lib/sessionsmodel.cpp @@ -17,22 +17,17 @@ * along with Labcontrol. If not, see . */ +#include + #include "sessionsmodel.h" lc::SessionsModel::SessionsModel( QObject *argParent ) : - QAbstractTableModel{ argParent }, - sessions_vector{ new QVector< Session* > } + QAbstractTableModel{ argParent } { } -lc::SessionsModel::~SessionsModel() { - for (auto s: *sessions_vector) - delete s; - delete sessions_vector; -} - lc::Session *lc::SessionsModel::back() const { - return sessions_vector->back(); + return sessionsList.back(); } int lc::SessionsModel::columnCount(const QModelIndex &parent) const { @@ -44,11 +39,11 @@ QVariant lc::SessionsModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) return QVariant{}; - if (index.row() >= sessions_vector->size() || index.row() < 0) + if (index.row() >= sessionsList.size() || index.row() < 0) return QVariant{}; if (role == Qt::DisplayRole) - return sessions_vector->at( index.row() )->GetDataItem( index.column() ); + return sessionsList.at( index.row() )->GetDataItem( index.column() ); return QVariant{}; } @@ -72,10 +67,20 @@ QVariant lc::SessionsModel::headerData(int section, Qt::Orientation orientation, } void lc::SessionsModel::push_back( Session *argSession ) { - sessions_vector->push_back( argSession ); + connect( argSession, &Session::SessionFinished, + this, &SessionsModel::RemoveSession ); + argSession->setParent( this ); + sessionsList.push_back( argSession ); +} + +void lc::SessionsModel::RemoveSession( Session *argSession ) { + if ( sessionsList.removeAll( argSession ) ) { + qDebug() << "Successfully removed" << argSession << "from lc::SessionsModel"; + argSession->deleteLater(); + } } int lc::SessionsModel::rowCount(const QModelIndex &parent) const { Q_UNUSED(parent); - return sessions_vector->length(); + return sessionsList.length(); } diff --git a/src/Lib/sessionsmodel.h b/src/Lib/sessionsmodel.h index 5f561e2..22e0bca 100644 --- a/src/Lib/sessionsmodel.h +++ b/src/Lib/sessionsmodel.h @@ -30,7 +30,6 @@ class SessionsModel : public QAbstractTableModel { Q_OBJECT public: explicit SessionsModel(QObject *parent = 0); - ~SessionsModel(); SessionsModel(const SessionsModel&) = delete; Session *back() const; int columnCount(const QModelIndex &parent) const; @@ -39,13 +38,15 @@ public: void push_back( Session *argSession ); int rowCount(const QModelIndex &parent) const; - signals: public slots: private: - QVector< Session* > *sessions_vector = nullptr; + QList< Session* > sessionsList; + +private slots: + void RemoveSession( Session *argSession ); }; } diff --git a/src/Lib/ztree.cpp b/src/Lib/ztree.cpp index 3403676..5622908 100644 --- a/src/Lib/ztree.cpp +++ b/src/Lib/ztree.cpp @@ -27,8 +27,9 @@ extern std::unique_ptr< lc::Settings > settings; lc::ZTree::ZTree( const QString &argZTreeDataTargetPath, const int &argZTreePort, - const QString &argZTreeVersionPath ) { - QString program{ settings->tasksetCmd }; + const QString &argZTreeVersionPath, QObject *argParent ) : + QObject{ argParent } +{ QStringList arguments{ QStringList{} << "0x00000001" << settings->wineCmd << QString{ settings->zTreeInstDir + "/zTree_" + argZTreeVersionPath + "/ztree.exe" } @@ -41,14 +42,9 @@ lc::ZTree::ZTree( const QString &argZTreeDataTargetPath, const int &argZTreePort QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); zTreeInstance.setProcessEnvironment( env ); - zTreeInstance.startDetached( program, arguments, QDir::currentPath(), &pid ); + zTreeInstance.start( settings->tasksetCmd, arguments, QIODevice::NotOpen ); connect( &zTreeInstance, SIGNAL( finished( int ) ), - this, SLOT( ZTreeInstanceClosed() ) ); + this, SIGNAL( ZTreeClosed( int ) ) ); - // Output message via the debug messages tab - qDebug() << program << arguments.join( " " ); -} - -void lc::ZTree::ZTreeInstanceClosed() { - emit ZTreeClosed(); + qDebug() << settings->tasksetCmd << arguments.join( " " ); } diff --git a/src/Lib/ztree.h b/src/Lib/ztree.h index eaa862e..2935189 100644 --- a/src/Lib/ztree.h +++ b/src/Lib/ztree.h @@ -37,16 +37,13 @@ class ZTree: public QObject { public: ZTree( const QString &argZTreeDataTargetPath, - const int &argZTreePort, const QString &argZTreeVersionPath ); + const int &argZTreePort, const QString &argZTreeVersionPath, + QObject *argParent = nullptr ); signals: - void ZTreeClosed(); - -private slots: - void ZTreeInstanceClosed(); + void ZTreeClosed( int argExitCode ); private: - qint64 pid = 0; QProcess zTreeInstance; };