Skip to content

Commit

Permalink
ENH: Use QEnableSharedFromThis to obtain shared pointers from PACSJob…
Browse files Browse the repository at this point in the history
… and its subclasses.

It's a slightly cleaner way to obtain the same result. The
implementation of QEnableSharedFromThis also stores a weak pointer to
create the shared pointer later, thus there's little difference in
practice.
  • Loading branch information
Woundorf committed Apr 19, 2022
1 parent e671f88 commit d9cf18f
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 23 deletions.
7 changes: 1 addition & 6 deletions starviewer/src/inputoutput/pacsjob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void PACSJob::aboutToBeDequeued(ThreadWeaver::QueueAPI *)
{
// Si ens desenqueuen de la cua de jobs pendents d'executar, vol dir que aquest Job no s'executarà, per tant emetem signal indicant que
// ens han cancel·lat
emit PACSJobCancelled(m_selfPointer.toStrongRef());
emit PACSJobCancelled(sharedFromThis());
}

void PACSJob::requestAbort()
Expand All @@ -59,11 +59,6 @@ bool PACSJob::isAbortRequested()
return m_abortIsRequested;
}

void PACSJob::setSelfPointer(const PACSJobPointer &self)
{
m_selfPointer = self;
}

void PACSJob::defaultBegin(const ThreadWeaver::JobPointer &job, ThreadWeaver::Thread *thread)
{
Q_UNUSED(thread)
Expand Down
11 changes: 1 addition & 10 deletions starviewer/src/inputoutput/pacsjob.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ typedef QSharedPointer<PACSJob> PACSJobPointer;
Aquesta classe hereda de ThreadWeaver::Job per així tenir automàticament la gestió de les cues que implementa, i permetre que les operacions
amb el PACS s'executin en un thread independent.
*/
class PACSJob : public QObject, public ThreadWeaver::Job {
class PACSJob : public QObject, public QEnableSharedFromThis<PACSJob>, public ThreadWeaver::Job {
Q_OBJECT
public:
enum PACSJobType { SendDICOMFilesToPACSJobType, RetrieveDICOMFilesFromPACSJobType, QueryPACS };
Expand Down Expand Up @@ -67,9 +67,6 @@ Q_OBJECT
/// des d'aquest mètode emetem el signal PACSJobCancelled
void aboutToBeDequeued(ThreadWeaver::QueueAPI *weaver);

/// Sets the self pointer reference of this job.
void setSelfPointer(const PACSJobPointer &self);

signals:
/// Signal que s'emet quan un PACSJob ha començat a executar-se
void PACSJobStarted(PACSJobPointer);
Expand All @@ -84,12 +81,6 @@ Q_OBJECT
virtual void defaultBegin(const ThreadWeaver::JobPointer &job, ThreadWeaver::Thread *thread);
virtual void defaultEnd(const ThreadWeaver::JobPointer &job, ThreadWeaver::Thread *thread);

protected:
/// Weak reference to a shared pointer of the job itself. It is needed to emit the PACSJobCancelled() signal with a shared pointer from aboutToBeDequeued().
/// Since it's a weak pointer it won't keep the job alive.
/// TODO This should be removed by redesigning the PACS jobs architecture.
QWeakPointer<PACSJob> m_selfPointer;

private:
/// Mètode que han de reimplementar les classes filles per cancel·lar l'execució del job actual
virtual void requestCancelJob() = 0;
Expand Down
2 changes: 0 additions & 2 deletions starviewer/src/inputoutput/pacsmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ PacsManager::PacsManager()

void PacsManager::enqueuePACSJob(PACSJobPointer pacsJob)
{
pacsJob->setSelfPointer(pacsJob);

switch (pacsJob->getPACSJobType())
{
case PACSJob::SendDICOMFilesToPACSJobType:
Expand Down
4 changes: 2 additions & 2 deletions starviewer/src/inputoutput/retrievedicomfilesfrompacsjob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,14 +220,14 @@ PACSRequestStatus::RetrieveRequestStatus RetrieveDICOMFilesFromPACSJob::getStatu

void RetrieveDICOMFilesFromPACSJob::DICOMFileRetrieved(DICOMTagReader *dicomTagReader, int numberOfImagesRetrieved)
{
emit DICOMFileRetrieved(m_selfPointer.toStrongRef(), numberOfImagesRetrieved);
emit DICOMFileRetrieved(sharedFromThis(), numberOfImagesRetrieved);

/// Actualitzem el número de sèries processades si ens arriba una nova imatge que pertanyi a una sèrie no descarregada fins al moment
QString seriesInstancedUIDRetrievedImage = dicomTagReader->getValueAttributeAsQString(DICOMSeriesInstanceUID);
if (!m_retrievedSeriesInstanceUIDSet.contains(seriesInstancedUIDRetrievedImage))
{
m_retrievedSeriesInstanceUIDSet.insert(seriesInstancedUIDRetrievedImage);
emit DICOMSeriesRetrieved(m_selfPointer.toStrongRef(), m_retrievedSeriesInstanceUIDSet.count());
emit DICOMSeriesRetrieved(sharedFromThis(), m_retrievedSeriesInstanceUIDSet.count());
}

// Fem un emit indicat que dicomTagReader està a punt per ser processat per l'Slot processDICOMFile de PatientFiller, no podem fer un connect
Expand Down
6 changes: 3 additions & 3 deletions starviewer/src/inputoutput/senddicomfilestopacsjob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ void SendDICOMFilesToPACSJob::run(ThreadWeaver::JobPointer self, ThreadWeaver::T
{
/// Si s'han envait imatges indiquem que s'ha enviat la última sèrie
m_numberOfSeriesSent++;
emit DICOMSeriesSent(m_selfPointer.toStrongRef(), m_numberOfSeriesSent);
emit DICOMSeriesSent(sharedFromThis(), m_numberOfSeriesSent);
}
}
}
Expand Down Expand Up @@ -169,12 +169,12 @@ void SendDICOMFilesToPACSJob::DICOMFileSent(Image *imageSent, int numberOfDICOMF
{
// Pressuposem que les imatges venen agrupades per sèries, sino és així s'ha de modificar aquest codi, perquè sinó es comptabilitzaran més series enviades
// de les que realment s'han enviat
emit DICOMFileSent(m_selfPointer.toStrongRef(), numberOfDICOMFilesSent);
emit DICOMFileSent(sharedFromThis(), numberOfDICOMFilesSent);

if (imageSent->getParentSeries()->getInstanceUID() != m_lastDICOMFileSeriesInstanceUID && !m_lastDICOMFileSeriesInstanceUID.isEmpty())
{
m_numberOfSeriesSent++;
emit DICOMSeriesSent(m_selfPointer.toStrongRef(), m_numberOfSeriesSent);
emit DICOMSeriesSent(sharedFromThis(), m_numberOfSeriesSent);
}

m_lastDICOMFileSeriesInstanceUID = imageSent->getParentSeries()->getInstanceUID();
Expand Down

0 comments on commit d9cf18f

Please sign in to comment.