<commit_before>/*=========================================================================

 Program:   Medical Imaging & Interaction Toolkit
 Language:  C++
 Date:      $Date$
 Version:   $Revision$

 Copyright (c) German Cancer Research Center, Division of Medical and
 Biological Informatics. All rights reserved.
 See MITKCopyright.txt or http://www.mitk.org/copyright.html for details.

 This software is distributed WITHOUT ANY WARRANTY; without even
 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 PURPOSE.  See the above copyright notices for more information.

 =========================================================================*/

#include "QmitkExtWorkbenchWindowAdvisor.h"
#include "QmitkExtActionBarAdvisor.h"

#include <QMenu>
#include <QMenuBar>
#include <QMainWindow>
#include <QStatusBar>

#include <berryPlatform.h>
#include <berryPlatformUI.h>
#include <berryIWorkbenchWindow.h>
#include <berryIWorkbenchPage.h>
#include <berryIPreferencesService.h>
#include <berryIPerspectiveRegistry.h>
#include <berryIPerspectiveDescriptor.h>
#include <berryIWorkbenchPartConstants.h>
#include <berryQtAssistantUtil.h>

#include <internal/berryQtShowViewAction.h>
#include <internal/berryQtOpenPerspectiveAction.h>

#include <QmitkExtFileOpenAction.h>
#include <QmitkExtFileSaveProjectAction.h>
#include <QmitkFileExitAction.h>
#include <QmitkCloseProjectAction.h>
#include <QmitkStatusBar.h>
#include <QmitkProgressBar.h>
#include <QmitkMemoryUsageIndicatorView.h>
#include <QmitkPreferencesDialog.h>

#include <itkConfigure.h>
#include <vtkConfigure.h>
#include <mitkVersion.h>

// UGLYYY
#include "internal/QmitkExtWorkbenchWindowAdvisorHack.h"
#include "mitkUndoController.h"
#include "mitkVerboseLimitedLinearUndo.h"
#include <QToolBar>
#include <QMessageBox>
#include <QmitkAboutDialog/QmitkAboutDialog.h>

QmitkExtWorkbenchWindowAdvisorHack
    * QmitkExtWorkbenchWindowAdvisorHack::undohack =
        new QmitkExtWorkbenchWindowAdvisorHack();

class PartListenerForTitle: public berry::IPartListener
{
public:

  PartListenerForTitle(QmitkExtWorkbenchWindowAdvisor* wa) :
    windowAdvisor(wa)
  {
  }

  Events::Types GetPartEventTypes() const
  {
    return Events::ACTIVATED | Events::BROUGHT_TO_TOP | Events::CLOSED
        | Events::HIDDEN | Events::VISIBLE;
  }

  void PartActivated(berry::IWorkbenchPartReference::Pointer ref)
  {
    if (ref.Cast<berry::IEditorReference> ())
    {
      windowAdvisor->UpdateTitle(false);
    }
  }

  void PartBroughtToTop(berry::IWorkbenchPartReference::Pointer ref)
  {
    if (ref.Cast<berry::IEditorReference> ())
    {
      windowAdvisor->UpdateTitle(false);
    }
  }

  void PartClosed(berry::IWorkbenchPartReference::Pointer ref)
  {
    windowAdvisor->UpdateTitle(false);
  }

  void PartHidden(berry::IWorkbenchPartReference::Pointer ref)
  {
    if (!windowAdvisor->lastActiveEditor.Expired() &&
        ref->GetPart(false) == windowAdvisor->lastActiveEditor.Lock())
    {
      windowAdvisor->UpdateTitle(true);
    }
  }

  void PartVisible(berry::IWorkbenchPartReference::Pointer ref)
  {
    if (!windowAdvisor->lastActiveEditor.Expired() &&
        ref->GetPart(false) == windowAdvisor->lastActiveEditor.Lock())
    {
      windowAdvisor->UpdateTitle(false);
    }
  }

private:
  QmitkExtWorkbenchWindowAdvisor* windowAdvisor;

};

class PerspectiveListenerForTitle: public berry::IPerspectiveListener
{
public:

  PerspectiveListenerForTitle(QmitkExtWorkbenchWindowAdvisor* wa) :
    windowAdvisor(wa), perspectivesClosed(false)
  {
  }

  Events::Types GetPerspectiveEventTypes() const
  {
    return Events::ACTIVATED | Events::SAVED_AS | Events::DEACTIVATED
      // remove the following line when command framework is finished
      | Events::CLOSED | Events::OPENED;
  }

  void PerspectiveActivated(berry::IWorkbenchPage::Pointer page,
      berry::IPerspectiveDescriptor::Pointer perspective)
  {
    windowAdvisor->UpdateTitle(false);
  }

  void PerspectiveSavedAs(berry::IWorkbenchPage::Pointer page,
      berry::IPerspectiveDescriptor::Pointer oldPerspective,
      berry::IPerspectiveDescriptor::Pointer newPerspective)
  {
    windowAdvisor->UpdateTitle(false);
  }

  void PerspectiveDeactivated(berry::IWorkbenchPage::Pointer page,
      berry::IPerspectiveDescriptor::Pointer perspective)
  {
    windowAdvisor->UpdateTitle(false);
  }

  void PerspectiveOpened(berry::IWorkbenchPage::Pointer page,
          berry::IPerspectiveDescriptor::Pointer perspective)
  {
    if (perspectivesClosed)
    {
      QListIterator<QAction*> i(windowAdvisor->viewActions);
      while (i.hasNext())
      {
        i.next()->setEnabled(false);
      }

      windowAdvisor->fileSaveProjectAction->setEnabled(false);
      windowAdvisor->closeProjectAction->setEnabled(false);
      windowAdvisor->undoAction->setEnabled(false);
      windowAdvisor->redoAction->setEnabled(false);
      windowAdvisor->imageNavigatorAction->setEnabled(false);
      windowAdvisor->resetPerspAction->setEnabled(false);
      windowAdvisor->closePerspAction->setEnabled(false);
    }

    perspectivesClosed = false;
  }

  void PerspectiveClosed(berry::IWorkbenchPage::Pointer page,
          berry::IPerspectiveDescriptor::Pointer perspective)
  {
    berry::IWorkbenchWindow::Pointer wnd = windowAdvisor->GetWindowConfigurer()->GetWindow();<commit_msg>ENH (#3115): enable actions again when opening first perspective<commit_after>/*=========================================================================

 Program:   Medical Imaging & Interaction Toolkit
 Language:  C++
 Date:      $Date$
 Version:   $Revision$

 Copyright (c) German Cancer Research Center, Division of Medical and
 Biological Informatics. All rights reserved.
 See MITKCopyright.txt or http://www.mitk.org/copyright.html for details.

 This software is distributed WITHOUT ANY WARRANTY; without even
 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 PURPOSE.  See the above copyright notices for more information.

 =========================================================================*/

#include "QmitkExtWorkbenchWindowAdvisor.h"
#include "QmitkExtActionBarAdvisor.h"

#include <QMenu>
#include <QMenuBar>
#include <QMainWindow>
#include <QStatusBar>

#include <berryPlatform.h>
#include <berryPlatformUI.h>
#include <berryIWorkbenchWindow.h>
#include <berryIWorkbenchPage.h>
#include <berryIPreferencesService.h>
#include <berryIPerspectiveRegistry.h>
#include <berryIPerspectiveDescriptor.h>
#include <berryIWorkbenchPartConstants.h>
#include <berryQtAssistantUtil.h>

#include <internal/berryQtShowViewAction.h>
#include <internal/berryQtOpenPerspectiveAction.h>

#include <QmitkExtFileOpenAction.h>
#include <QmitkExtFileSaveProjectAction.h>
#include <QmitkFileExitAction.h>
#include <QmitkCloseProjectAction.h>
#include <QmitkStatusBar.h>
#include <QmitkProgressBar.h>
#include <QmitkMemoryUsageIndicatorView.h>
#include <QmitkPreferencesDialog.h>

#include <itkConfigure.h>
#include <vtkConfigure.h>
#include <mitkVersion.h>

// UGLYYY
#include "internal/QmitkExtWorkbenchWindowAdvisorHack.h"
#include "mitkUndoController.h"
#include "mitkVerboseLimitedLinearUndo.h"
#include <QToolBar>
#include <QMessageBox>
#include <QmitkAboutDialog/QmitkAboutDialog.h>

QmitkExtWorkbenchWindowAdvisorHack
    * QmitkExtWorkbenchWindowAdvisorHack::undohack =
        new QmitkExtWorkbenchWindowAdvisorHack();

class PartListenerForTitle: public berry::IPartListener
{
public:

  PartListenerForTitle(QmitkExtWorkbenchWindowAdvisor* wa) :
    windowAdvisor(wa)
  {
  }

  Events::Types GetPartEventTypes() const
  {
    return Events::ACTIVATED | Events::BROUGHT_TO_TOP | Events::CLOSED
        | Events::HIDDEN | Events::VISIBLE;
  }

  void PartActivated(berry::IWorkbenchPartReference::Pointer ref)
  {
    if (ref.Cast<berry::IEditorReference> ())
    {
      windowAdvisor->UpdateTitle(false);
    }
  }

  void PartBroughtToTop(berry::IWorkbenchPartReference::Pointer ref)
  {
    if (ref.Cast<berry::IEditorReference> ())
    {
      windowAdvisor->UpdateTitle(false);
    }
  }

  void PartClosed(berry::IWorkbenchPartReference::Pointer ref)
  {
    windowAdvisor->UpdateTitle(false);
  }

  void PartHidden(berry::IWorkbenchPartReference::Pointer ref)
  {
    if (!windowAdvisor->lastActiveEditor.Expired() &&
        ref->GetPart(false) == windowAdvisor->lastActiveEditor.Lock())
    {
      windowAdvisor->UpdateTitle(true);
    }
  }

  void PartVisible(berry::IWorkbenchPartReference::Pointer ref)
  {
    if (!windowAdvisor->lastActiveEditor.Expired() &&
        ref->GetPart(false) == windowAdvisor->lastActiveEditor.Lock())
    {
      windowAdvisor->UpdateTitle(false);
    }
  }

private:
  QmitkExtWorkbenchWindowAdvisor* windowAdvisor;

};

class PerspectiveListenerForTitle: public berry::IPerspectiveListener
{
public:

  PerspectiveListenerForTitle(QmitkExtWorkbenchWindowAdvisor* wa) :
    windowAdvisor(wa), perspectivesClosed(false)
  {
  }

  Events::Types GetPerspectiveEventTypes() const
  {
    return Events::ACTIVATED | Events::SAVED_AS | Events::DEACTIVATED
      // remove the following line when command framework is finished
      | Events::CLOSED | Events::OPENED;
  }

  void PerspectiveActivated(berry::IWorkbenchPage::Pointer page,
      berry::IPerspectiveDescriptor::Pointer perspective)
  {
    windowAdvisor->UpdateTitle(false);
  }

  void PerspectiveSavedAs(berry::IWorkbenchPage::Pointer page,
      berry::IPerspectiveDescriptor::Pointer oldPerspective,
      berry::IPerspectiveDescriptor::Pointer newPerspective)
  {
    windowAdvisor->UpdateTitle(false);
  }

  void PerspectiveDeactivated(berry::IWorkbenchPage::Pointer page,
      berry::IPerspectiveDescriptor::Pointer perspective)
  {
    windowAdvisor->UpdateTitle(false);
  }

  void PerspectiveOpened(berry::IWorkbenchPage::Pointer page,
          berry::IPerspectiveDescriptor::Pointer perspective)
  {
    if (perspectivesClosed)
    {
      QListIterator<QAction*> i(windowAdvisor->viewActions);
      while (i.hasNext())
      {
        i.next()->setEnabled(true);
      }

      windowAdvisor->fileSaveProjectAction->setEnabled(true);
      windowAdvisor->closeProjectAction->setEnabled(true);
      windowAdvisor->undoAction->setEnabled(true);
      windowAdvisor->redoAction->setEnabled(true);
      windowAdvisor->imageNavigatorAction->setEnabled(true);
      windowAdvisor->resetPerspAction->setEnabled(true);
      windowAdvisor->closePerspAction->setEnabled(true);
    }

    perspectivesClosed = false;
  }

  void PerspectiveClosed(berry::IWorkbenchPage::Pointer page,
          berry::IPerspectiveDescriptor::Pointer perspective)
  {
    berry::IWorkbenchWindow::Pointer wnd = windowAdvisor->GetWindowConfigurer()->GetWindow();
{'fraction_non_alphanumeric': 0.1328612689835088, 'fraction_numerical': 0.0003726823814404174, 'mean_word_length': 5.317834020011771, 'pattern_counts': {'":': 0, '<': 67, '<?xml version=': 0, '>': 131, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5327474', 'n_tokens_mistral': 3290, 'n_tokens_neox': 3194, 'n_words': 531}