aboutsummaryrefslogtreecommitdiff
path: root/scripts/artv2/utilities/interface_utilities.py
blob: 101151d4e9e699aeff09b6a2e03104cbf2c200e6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# -*- coding: utf-8 -*-
"""
:author:
    Jeremy Ernst
:description:
    This module provides utilities for general QT functions and classes to be used as widgets or templates.
"""

import os

try:
    import shiboken as shiboken
except ImportError:
    import shiboken2 as shiboken

import maya.OpenMayaUI as mui
import maya.cmds as cmds
from maya.app.general.mayaMixin import MayaQWidgetBaseMixin
from artv2.third_party.Qt import QtWidgets, QtGui, QtCore
import artv2.utilities.general_utilities as utils


# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
def get_maya_window():
    """ Return Maya's main window as a QWidget"""
    pointer = mui.MQtUtil.mainWindow()
    return shiboken.wrapInstance(long(pointer), QtWidgets.QWidget)


# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
def get_maya_dpi():
    """ Gets the DPI setting value in Maya and returns the multiplier. """

    dpi_percentages = {96: 1, 120: 1.25, 144: 1.5, 192: 2}
    maya_app = QtWidgets.QApplication.instance()
    screens = maya_app.screens()
    dpi = screens[0].logicalDotsPerInch()

    return dpi_percentages.get(round(dpi))


# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
def scale_by_dpi(value):
    """ Scales the given value by the dpi multiplier. """
    scalar = get_maya_dpi()
    return int(round(value * scalar))


# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
def get_style_sheet(file_path):
    """ Read the style sheet data, and then replace any urls with proper file paths on the user's drive."""

    settings = utils.return_settings()
    style_sheet_file = utils.path_join(settings[0], "resources/_styleSheets/" + file_path + ".qss")
    file_object = open(style_sheet_file, "r")
    data = file_object.readlines()
    style = create_style_sheet(data)
    file_object.close()
    return style


# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
def create_style_sheet(data):
    """ Take in a read stylesheet, and replace any urls with actual file paths, then return the altered data."""

    settings = utils.return_settings()
    new_lines = []

    # todo: could be simplified
    for line in data:
        if line.find("url(") != -1:
            if line.find("resources/icons/general") != -1:
                old_path = line.partition("(")[2].rpartition("/")[0]
                replace_path = utils.path_join(settings[2], "general")
                new_line = line.replace(old_path, replace_path)
                new_lines.append(new_line)

            if line.find("resources/icons/general") == -1:
                old_path = line.partition("(")[2].rpartition("/")[0]
                replace_path = settings[2]
                new_line = line.replace(old_path, replace_path)
                new_lines.append(new_line)
        else:
            new_lines.append(line)

    user_dir = utils.path_unify(os.path.join(settings[0], "user"))
    if not os.path.exists(user_dir):
        os.makedirs(user_dir)

    full_path = (utils.path_join(user_dir, "style.qss"))
    file_object = open(full_path, 'w')
    file_object.writelines(new_lines)
    file_object.close()

    file_object = open(full_path, 'r')
    style = file_object.read()
    file_object.close()

    os.remove(full_path)
    return style


# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# noinspection PyUnusedLocal
def search_list_widget(search_field, list_widget, *args):
    """
    Searches the given list_widget for items with the search term in the given search_field. Items that do not match are
    hidden in the list.

    :param search_field: The QLineEdit to grab the search term from
    :param list_widget: the QListWidget to manipulate
    """

    search_term = search_field.text()
    all_items = {}

    for i in range(list_widget.count()):
        item = list_widget.item(i)
        all_items[item.text()] = item
        item.setHidden(True)
        item.setSelected(False)

    for each in list_widget.findItems(search_term, QtCore.Qt.MatchFlag.MatchContains):
        each.setHidden(False)
        each.setSelected(True)


# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
def clear_layout(layout):
    """ Clears the given layout of all child widgets. """
    while layout.count():
        child = layout.takeAt(0)
        if child.widget() is not None:
            child.widget().deleteLater()
        elif child.layout() is not None:
            clear_layout(child.layout())


# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
def toggle_group(ctrl, args):
    """ Sets the given ctrl (groupbox) height based on the check state. """
    state = ctrl.isChecked()
    if state:
        ctrl.setFixedHeight(ctrl.sizeHint().height())

    else:
        ctrl.setFixedHeight(30)


# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
def filter_list(list_widget, search_widget):
    """
    Filters the list widget by the search term in the given search widget.

    :param list_widget: QListWidget widget to filter items in.
    :param search_widget: QLineEdit widget to grab text from for search term.
    """

    search_term = search_widget.text()
    items = get_items(list_widget)
    for item in items:
        widget_item = items.get(item)
        if search_term in item:
            widget_item.setHidden(False)
        else:
            widget_item.setHidden(True)


# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
def get_items(list_widget):
    """
    Given a list widget, get and return the text and pointer to the list widget item in a dict.

    :param list_widget: List widget to query items from.
    :return: Returns a dict of the listWidgetItems' text and pointer to the items.
    """

    items = {}
    for i in range(list_widget.count()):
        item = list_widget.item(i)
        text = item.text()
        items[text] = item

    return items


# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
class ARTv2Message(QtWidgets.QMessageBox):
    """
    Abstract class that adds extra functionality onto QMessageBox. Depending on the message type (warning, error, etc),
    different buttons and icons will be shown. This class is not meant to be instantiated on its own.

    """

    def __init__(self, width, height, title, critical=False, warning=False, info=False, question=False, parent=None):

        super(ARTv2Message, self).__init__(parent)

        if cmds.window(self.WINDOW_NAME, exists=True):
            cmds.deleteUI(self.WINDOW_NAME)

        self.setObjectName(self.WINDOW_NAME)

        self.settings = utils.return_settings()
        self.tools_path = self.settings[0]
        self.script_path = self.settings[1]
        self.icon_path = self.settings[2]
        self.project_path = self.settings[3]

        self.setMinimumSize(QtCore.QSize(width, height))
        self.setMaximumSize(QtCore.QSize(width, height))
        self.setWindowTitle(title)

        os.environ["QT_AUTO_SCREEN_SCALE_FACTOR"] = "1"
        qapp = QtWidgets.QApplication.instance()
        qapp.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)

        if critical:
            self.setIcon(QtWidgets.QMessageBox.Critical)
        elif warning:
            self.setIcon(QtWidgets.QMessageBox.Warning)
        elif info:
            self.setIcon(QtWidgets.QMessageBox.Information)
        elif question:
            self.setIcon(QtWidgets.QMessageBox.Question)
        else:
            self.setIcon(QtWidgets.QMessageBox.NoIcon)


# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
class ARTv2Window(MayaQWidgetBaseMixin, QtWidgets.QMainWindow):
    """
    Abstract class that adds some extra default functionality for things like grabbing settings, dealing with
    different screen resolutions, styling the UI, and saving screen position. This class is not meant to be
    instantiated on its own.
    """

    def __init__(self, width, height, window_title, parent=None):

        super(ARTv2Window, self).__init__(parent)

        if cmds.window(self.WINDOW_NAME, exists=True):
            cmds.deleteUI(self.WINDOW_NAME)

        self.setObjectName(self.WINDOW_NAME)

        self.settings = utils.return_settings()
        self.tools_path = self.settings[0]
        self.script_path = self.settings[1]
        self.icon_path = self.settings[2]
        self.project_path = self.settings[3]

        self.setGeometry(200, 200, width, height)

        self.style_sheet = get_style_sheet("artv2_style_basic")
        self.setStyleSheet(self.style_sheet)
        self.setWindowTitle(window_title)

        window_icon = QtGui.QIcon(os.path.join(self.icon_path, "general/logo.png"))
        self.setWindowIcon(window_icon)

        os.environ["QT_AUTO_SCREEN_SCALE_FACTOR"] = "1"
        qapp = QtWidgets.QApplication.instance()
        qapp.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)

        self.restoreGeometry(self.settings[4].value(self.SETTINGS_NAME))

    # noinspection PyPep8Naming
    def closeEvent(self, event):
        """ Override QMainWindow's close event to remember window position."""
        self.settings[4].setValue(self.SETTINGS_NAME, self.saveGeometry())
        QtWidgets.QMainWindow.closeEvent(self, event)

    @staticmethod
    def launch_help_doc(path):
        """
        Launches a html page from the artv2 help documents.

        :param path: The path of the html file to launch.
        """

        html_file = os.path.join(utils.return_settings()[0], path)
        utils.launch_web_docs(html_file)


# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
class ARTv2Dialog(MayaQWidgetBaseMixin, QtWidgets.QDialog):
    """
    Abstract class that adds some extra default functionality for things like grabbing settings, dealing with
    different screen resolutions, styling the UI, and saving screen position. This class is not meant to be
    instantiated on its own.

    """

    def __init__(self, width, height, window_title, parent=None):

        super(ARTv2Dialog, self).__init__(parent)

        if cmds.window(self.WINDOW_NAME, exists=True):
            cmds.deleteUI(self.WINDOW_NAME)

        self.setObjectName(self.WINDOW_NAME)

        self.settings = utils.return_settings()
        self.tools_path = self.settings[0]
        self.script_path = self.settings[1]
        self.icon_path = self.settings[2]
        self.project_path = self.settings[3]

        self.setGeometry(200, 200, width, height)

        self.style_sheet = get_style_sheet("artv2_style_basic")
        self.setStyleSheet(self.style_sheet)
        self.setWindowTitle(window_title)

        window_icon = QtGui.QIcon(os.path.join(self.icon_path, "general/logo.png"))
        self.setWindowIcon(window_icon)

        os.environ["QT_AUTO_SCREEN_SCALE_FACTOR"] = "1"
        qapp = QtWidgets.QApplication.instance()
        qapp.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)

        self.restoreGeometry(self.settings[4].value(self.SETTINGS_NAME))


# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
class ErrorWidget(ARTv2Message):
    """
    Simple class that displays a QMessageBox widget with an error message, and error icon, and a simple "OK" button.
    Takes in a string message to display.

    .. image:: /images/errorWidget.png

    """

    SETTINGS_NAME = "ARTv2ErrorWidget"
    WINDOW_NAME = "artv2_error_message_window"

    def __init__(self, message, parent=None):

        super(ErrorWidget, self).__init__(300, 150, "Error", critical=True, parent=parent)
        self.setText(message)


# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
class InfoWidget(ARTv2Message):
    """
    Simple class that displays a QMessageBox widget with an error message, and error icon, and a simple "OK" button.
    This class takes in required arguments: A string message, and a string for details. The details string could be
    empty if not required.

    .. code-block:: python

        message = ui_utils.InfoWidget("this is an info widget", "these are some details\\nas are these")

    .. image:: /images/infoWidget.png

    """

    SETTINGS_NAME = "ARTv2InfoWidget"
    WINDOW_NAME = "artv2_info_message_window"

    def __init__(self, message, details, parent=None):

        super(InfoWidget, self).__init__(300, 200, "Attention!", info=True, parent=parent)
        self.setText(message)
        self.setDetailedText(details)


# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
class ConfirmWidget(ARTv2Message):
    """
    Simple class that displays a QMessageBox widget with a question, a question icon, and Yes or Cancel buttons.
    Takes in a string message to display, a string for any detailed text to display, and a string for the title.

    """

    SETTINGS_NAME = "ARTv2ConfirmWidget"
    WINDOW_NAME = "artv2_confirm_message_window"

    def __init__(self, message, details, title, parent=None):

        super(ConfirmWidget, self).__init__(300, 200, title, question=True, parent=parent)
        self.setText(message)
        self.setDetailedText(details)

        self.setStandardButtons(QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.Cancel)
        self.setDefaultButton(QtWidgets.QMessageBox.Yes)