// ***************** DO NOT EDIT ***********************************
// This is a generated file.
// It will be replaced next time you rebuild.
/*=========================================================================

   Program: @_paraview_client_NAME@
   Module:  @_paraview_client_NAME@_main.cxx

   Copyright (c) 2005-2008 Sandia Corporation, Kitware Inc.
   All rights reserved.

   ParaView is a free software; you can redistribute it and/or modify it
   under the terms of the ParaView license version 1.2.

   See License_v1.2.txt for the full ParaView license.
   A copy of this license can be obtained by contacting
   Kitware Inc.
   28 Corporate Drive
   Clifton Park, NY 12065
   USA

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

=========================================================================*/
#include <QApplication>
#include <QLocale>
#include <QStyleFactory>
#include <QSurfaceFormat>

#include "pq@_paraview_client_NAME@Initializer.h"
#include "pqQtWidgetsConfig.h"

#if PARAVIEW_USE_QTWEBENGINE
#include <QWebEngineUrlScheme>
#endif

// See Applications/ParaView/CMakeLists.txt for why this is necessary.
#cmakedefine01 _paraview_client_have_plugins
#if _paraview_client_have_plugins
@_paraview_client_plugins_includes@
#endif

#cmakedefine01 _paraview_client_DEFAULT_STYLE

#include <vtksys/SystemTools.hxx>
#include <QVTKRenderWindowAdapter.h>

#include <clocale>
#include <cstdlib>

#if !defined(__APPLE__) && !defined(_WIN32)
#include <cstring>
#endif

int main(int argc, char* argv[])
{
  // Workaround for issue 19751
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
  vtksys::SystemTools::PutEnv("QT_AUTO_SCREEN_SCALE_FACTOR=0");
#endif

#if _paraview_client_DEFAULT_STYLE
  QApplication::setStyle(QStyleFactory::create("@_paraview_client_DEFAULT_STYLE@"));
#endif

  // When playing tests, disable all effects.
  if (vtksys::SystemTools::HasEnv("DART_TEST_FROM_DART") ||
      vtksys::SystemTools::HasEnv("DASHBOARD_TEST_FROM_CTEST"))
  {
    QApplication::setDesktopSettingsAware(false);
    QApplication::setEffectEnabled(Qt::UI_AnimateMenu, false);
    QApplication::setEffectEnabled(Qt::UI_FadeMenu, false);
    QApplication::setEffectEnabled(Qt::UI_AnimateCombo, false);
    QApplication::setEffectEnabled(Qt::UI_AnimateTooltip, false);
    QApplication::setEffectEnabled(Qt::UI_FadeTooltip, false);
  }

  QApplication::setApplicationName("@_paraview_client_APPLICATION_NAME@");
  QApplication::setApplicationVersion("@_paraview_client_VERSION@");
  QApplication::setOrganizationName("@_paraview_client_ORGANIZATION@");

  // supports HDPI
  QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
  QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

#if _paraview_client_have_plugins
@_paraview_client_plugins_calls@
#endif

#if PARAVIEW_USE_QTWEBENGINE
{
  QWebEngineUrlScheme qtHelpScheme("qthelp");
  qtHelpScheme.setFlags(
    {QWebEngineUrlScheme::LocalScheme, QWebEngineUrlScheme::LocalAccessAllowed});
  QWebEngineUrlScheme::registerScheme(qtHelpScheme);
}
#endif

  // always request a non-stereo format; when using quadbuffer stereo, we
  // request stereo capable format for the individual `QVTKOpenGLWindow`
  // instance.
  QSurfaceFormat::setDefaultFormat(QVTKRenderWindowAdapter::defaultFormat(/*stereo_capable=*/false));

#if !defined(__APPLE__) && !defined(_WIN32)
  // On non-Apple and non-Windows platforms, ParaView only works with X11, so
  // prefer it to Wayland on those platforms.
  std::string platform_args_data[] = { "-platform", "xcb" };
  char* platform_args[] = {
    // XXX(c++17): platform_args_data[n].data()
    &platform_args_data[0].front(),
    &platform_args_data[1].front(),
  };
  std::vector<char*> new_argc(argc + 2 + 1);
  std::memcpy(new_argc.data(), argv, argc * sizeof(char*));
  new_argc[argc] = platform_args[0];
  new_argc[argc + 1] = platform_args[1];
  new_argc[argc + 2] = nullptr;
  argc += 2;
  argv = new_argc.data();
#endif
  QApplication qtapp(argc, argv);
  // QApplication docs suggest resetting to "C" after the QApplication is
  // initialized.
  setlocale(LC_NUMERIC, "C");

  // However, this is needed to address BUG #17225, #17226.
  QLocale::setDefault(QLocale::c());

  using InitializerT = pq@_paraview_client_NAME@Initializer;

  InitializerT pvInitializer;
  InitializerT::Status status = pvInitializer.Initialize(argc, argv);
  switch (status)
  {
    case InitializerT::ExitSuccess:
      return EXIT_SUCCESS;
    case InitializerT::ExitFailure:
      return EXIT_FAILURE;
    case InitializerT::RunApplication:
      return QApplication::exec();
  }
}
