<commit_before>// ========================================================================== //
// This file is part of DO++, a basic set of libraries in C++ for computer 
// vision.
//
// Copyright (C) 2013 <NAME> <<EMAIL>>
//
// This Source Code Form is subject to the terms of the Mozilla Public 
// License v. 2.0. If a copy of the MPL was not distributed with this file, 
// you can obtain one at http://mozilla.org/MPL/2.0/.
// ========================================================================== //

#include "GraphicsApplicationImpl.hpp"
#include <QFileDialog>
#include <QDebug>

namespace DO {

  GraphicsApplication::Impl::
  Impl(int argc_, char **argv_)
    : QApplication(argc_, argv_)
    , argc(argc_)
    , argv(argv_)
    , activeWindow(0)
    //, graphicsView(0)
    , mutex(QMutex::NonRecursive)
  {
    // Register painting data types.
    qRegisterMetaType<PaintingWindow *>("PaintingWindow *");
    qRegisterMetaType<QPolygonF>("QPolygonF");

    // Register mesh data structure.
    qRegisterMetaType<SimpleTriangleMesh3f>("SimpleTriangleMesh3f");

    // Register graphics view data types.
    qRegisterMetaType<QGraphicsPixmapItem *>("QGraphicsPixmapItem *");
    //qRegisterMetaType<QGraphicsItem *>("QGraphicsItem *");

    // Register Event data types
    qRegisterMetaType<Event>("Event");

    // Make sure you quit after the user thread is finished.
    connect(&userThread, SIGNAL(finished()), this, SLOT(quit()));
    setQuitOnLastWindowClosed(false);
  }

   void
   GraphicsApplication::Impl::
   createPaintingWindow(int w, int h, const QString& windowTitle,
                        int x, int y)
   {
     createdWindows << new PaintingWindow(w, h, windowTitle, x, y);
     if (createdWindows.size() == 1)
     {
       activeWindow = createdWindows.front();
       setActiveWindow(activeWindow);
     }
   }

   void
   GraphicsApplication::Impl::
   createOpenGLWindow(int w, int h, const QString& windowTitle,
                      int x, int y)
   {
     createdWindows << new OpenGLWindow(w, h, windowTitle, x, y);
     if (createdWindows.size() == 1)
     {
       activeWindow = createdWindows.front();
       setActiveWindow(activeWindow);
     }
   }

   void 
   GraphicsApplication::Impl::
   createGraphicsView(int w, int h, const QString& windowTitle,
                      int x, int y)
   {
     createdWindows << new GraphicsView(w, h, windowTitle, x, y);
     if (createdWindows.size() == 1)
     {
       activeWindow = createdWindows.front();
       setActiveWindow(activeWindow);
     }
   }

   void 
   GraphicsApplication::Impl::
   setActiveWindow(QWidget *w)
   {
     if (w == 0)
     {
       qWarning() << "I can't make a null window active!";
       return;
     }

     // Disconnect the communication between:
     // All windows and user thread in both directions.
     // Why? I don't want the user thread to listens to communicate from any
     // window but the requested window 'w'.
     disconnectAllWindowsIOEventsToUserThread();

     // This is now our current active window
     activeWindow = w;
     connectWindowIOEventsToUserThread(w);
   }

   void
   GraphicsApplication::Impl::
   closeWindow(QWidget *w) 
   {
     QList<QPointer<QWidget> >::iterator wi = qFind(
       createdWindows.begin(), createdWindows.end(), w);
     if (wi == createdWindows.end())
     {
       qFatal("Could not find window!");
       quit();
     }

     // Store closing result here.
     bool closed = false;
     // Close the painting window if it is one.
     PaintingWindow *paintingWindow = qobject_cast<PaintingWindow *>(*wi);
     if (paintingWindow)
       closed = paintingWindow->scrollArea()->close();
     else
       closed = (*wi)->close();
     // Check the closing is successful.
     if (!closed)
     {
       qFatal("Could not close window!");
       quit();
     }

     if (!(*wi).isNull())
       delete *wi;
     createdWindows.erase(wi);
   }

   void
   GraphicsApplication::Impl::
   getFileFromDialogBox()
   {
     dialogBoxInfo.filename = 
       QFileDialog::getOpenFileName(0, "Open File", "/home", 
       "Images (*.png *.xpm *.jpg)");
   }

  bool
  GraphicsApplication::Impl::
  activeWindowIsVisible()
  {
    mutex.lock();
    if (activeWindow.isNull())
    {
      mutex.unlock();
      qWarning() << "No active window!";
      return false;
    }
    if (activeWindow->isHidden())
    {
      mutex.unlock();
      qWarning() << "Active window is hidden!";
      return false;
    }
    mutex.unlock();
    return true;<commit_msg>Use `deleteLater` method to destroy `QWidget` instead.<commit_after>// ========================================================================== //
// This file is part of DO++, a basic set of libraries in C++ for computer 
// vision.
//
// Copyright (C) 2013 <NAME> <<EMAIL>>
//
// This Source Code Form is subject to the terms of the Mozilla Public 
// License v. 2.0. If a copy of the MPL was not distributed with this file, 
// you can obtain one at http://mozilla.org/MPL/2.0/.
// ========================================================================== //

#include "GraphicsApplicationImpl.hpp"
#include <QFileDialog>
#include <QDebug>

namespace DO {

  GraphicsApplication::Impl::
  Impl(int argc_, char **argv_)
    : QApplication(argc_, argv_)
    , argc(argc_)
    , argv(argv_)
    , activeWindow(0)
    //, graphicsView(0)
    , mutex(QMutex::NonRecursive)
  {
    // Register painting data types.
    qRegisterMetaType<PaintingWindow *>("PaintingWindow *");
    qRegisterMetaType<QPolygonF>("QPolygonF");

    // Register mesh data structure.
    qRegisterMetaType<SimpleTriangleMesh3f>("SimpleTriangleMesh3f");

    // Register graphics view data types.
    qRegisterMetaType<QGraphicsPixmapItem *>("QGraphicsPixmapItem *");
    //qRegisterMetaType<QGraphicsItem *>("QGraphicsItem *");

    // Register Event data types
    qRegisterMetaType<Event>("Event");

    // Make sure you quit after the user thread is finished.
    connect(&userThread, SIGNAL(finished()), this, SLOT(quit()));
    setQuitOnLastWindowClosed(false);
  }

   void
   GraphicsApplication::Impl::
   createPaintingWindow(int w, int h, const QString& windowTitle,
                        int x, int y)
   {
     createdWindows << new PaintingWindow(w, h, windowTitle, x, y);
     if (createdWindows.size() == 1)
     {
       activeWindow = createdWindows.front();
       setActiveWindow(activeWindow);
     }
   }

   void
   GraphicsApplication::Impl::
   createOpenGLWindow(int w, int h, const QString& windowTitle,
                      int x, int y)
   {
     createdWindows << new OpenGLWindow(w, h, windowTitle, x, y);
     if (createdWindows.size() == 1)
     {
       activeWindow = createdWindows.front();
       setActiveWindow(activeWindow);
     }
   }

   void 
   GraphicsApplication::Impl::
   createGraphicsView(int w, int h, const QString& windowTitle,
                      int x, int y)
   {
     createdWindows << new GraphicsView(w, h, windowTitle, x, y);
     if (createdWindows.size() == 1)
     {
       activeWindow = createdWindows.front();
       setActiveWindow(activeWindow);
     }
   }

   void 
   GraphicsApplication::Impl::
   setActiveWindow(QWidget *w)
   {
     if (w == 0)
     {
       qWarning() << "I can't make a null window active!";
       return;
     }

     // Disconnect the communication between:
     // All windows and user thread in both directions.
     // Why? I don't want the user thread to listens to communicate from any
     // window but the requested window 'w'.
     disconnectAllWindowsIOEventsToUserThread();

     // This is now our current active window
     activeWindow = w;
     connectWindowIOEventsToUserThread(w);
   }

   void
   GraphicsApplication::Impl::
   closeWindow(QWidget *w) 
   {
     QList<QPointer<QWidget> >::iterator wi = qFind(
       createdWindows.begin(), createdWindows.end(), w);
     if (wi == createdWindows.end())
     {
       qFatal("Could not find window!");
       quit();
     }

     // Store closing result here.
     bool closed = false;
     // Close the painting window if it is one.
     PaintingWindow *paintingWindow = qobject_cast<PaintingWindow *>(*wi);
     if (paintingWindow)
       closed = paintingWindow->scrollArea()->close();
     else
       closed = (*wi)->close();
     // Check the closing is successful.
     if (!closed)
     {
       qFatal("Could not close window!");
       quit();
     }

     if (!(*wi).isNull())
       (*wi)->deleteLater();
     createdWindows.erase(wi);
   }

   void
   GraphicsApplication::Impl::
   getFileFromDialogBox()
   {
     dialogBoxInfo.filename = 
       QFileDialog::getOpenFileName(0, "Open File", "/home", 
       "Images (*.png *.xpm *.jpg)");
   }

  bool
  GraphicsApplication::Impl::
  activeWindowIsVisible()
  {
    mutex.lock();
    if (activeWindow.isNull())
    {
      mutex.unlock();
      qWarning() << "No active window!";
      return false;
    }
    if (activeWindow->isHidden())
    {
      mutex.unlock();
      qWarning() << "Active window is hidden!";
      return false;
    }
    mutex.unlock();
    return true;
{'fraction_non_alphanumeric': 0.15471367568159422, 'fraction_numerical': 0.0037227636045111135, 'mean_word_length': 2.7465135356849877, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '7318878', 'n_tokens_mistral': 2729, 'n_tokens_neox': 2587, 'n_words': 710}