# -*- coding: utf-8 -*-

# -----------------------------------------------------------------------------
# Copyright (c) 2016-2017 Anaconda, Inc.
#
# May be copied and distributed freely only as part of an Anaconda or
# Miniconda installation.
# -----------------------------------------------------------------------------

"""Qt utilities."""

from __future__ import absolute_import, division, unicode_literals

import os

from qtpy.QtCore import QTimer  # pylint: disable=no-name-in-module
from qtpy.QtWidgets import QApplication  # pylint: disable=no-name-in-module

from navigator_updater.utils.linux_scaling import setup_scale_factor_for_linux


def qapplication(test_time=60):
    """Create QApplication instance."""
    app = QApplication.instance()

    if app is None:
        setup_scale_factor_for_linux()
        app = QApplication(['Anaconda-Navigator'])
        app.setApplicationName('Anaconda-Navigator')

    test_ci = os.environ.get('TEST_CI')

    if test_ci is not None:
        timer_shutdown = QTimer(app)
        timer_shutdown.timeout.connect(app.quit)
        timer_shutdown.start(test_time * 1000)
    return app
