Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: UTF-8 -*-
- """ Module mainWindow: This module contains classes for creating a standard PySide Qt QMainWindow instance. """
- import traceback
- from PySide.QtGui import QDialog, QMainWindow
- import logging
- class _GCProtector(object):
- """
- This class acts as a holder for a static class var in order to
- prevent Qt _widgets from being garbage-collected
- """
- # declare static class var
- widgets = []
- class MainWindow(QMainWindow):
- """
- This class creates a new QMainWindow and setups widgets onto it.
- """
- logger = logging.getLogger( __name__ )
- def __init__( self, qmainwindow, parent=None, *args, **kwargs ):
- """
- The constructor. Creates an instance of QMainWindow and handles order of
- data population along with connecting signals/slots.
- :param parent:
- :return:
- """
- # call base class constructor
- super( MainWindow, self ).__init__( *args, **kwargs )
- # setup UI widgets to the QMainWindow
- qmainwindow.setupUi(self)
- # add to garbage collection protection
- _GCProtector.widgets.append(self)
- # create reference to the qmainwindow widget
- self.ui = qmainwindow
- try: self.populateData()
- except Exception:
- self.logger.error('### Could not populate data to widget!!! \n {0}'
- .format(traceback.print_exc()))
- raise Exception
- self.logger.debug('Data successfully populated to widget!')
- try: self.makeConnections()
- except Exception:
- self.logger.error('### Could not make connections to signals/slots!!! \n {0}'
- .format(traceback.print_exc()))
- raise Exception
- self.logger.debug('Connections successfully made for widget!')
- self.show()
- def makeConnections(self, *args, **kwargs):
- """
- This method handles making connections from the
- Qt widgets to corresponding signals/slots.
- :return:
- """
- pass
- def populateData(self, *args, **kwargs ):
- """
- This method populates the widget with data.
- :return:
- """
- pass
- def closeEvent(self, *args, **kwargs):
- """
- This overrides the base event in ``QDialog``.
- :param args:
- :param kwargs:
- :return:
- """
- # Remove widget objects from GC protection
- if self in _GCProtector.widgets:
- _GCProtector.widgets.remove(self)
- def cancelAction(self):
- """
- This method is run when the user chooses to close the widget via the UI.
- :return:
- """
- self.close()
- class MainDialog(QDialog):
- """
- This class creates a new QDialog and setups widgets onto it.
- """
- logger = logging.getLogger( __name__ )
- def __init__( self, qform, parent=None, title=None,
- flags=None, modal=False, *args, **kwargs ):
- """
- The constructor.
- :param qform:
- :param parent: ``QObject`` instance that will act as the parent widget for this dialog.
- :param title: ``str`` indicating the title that the dialog should have.
- :param flags:
- :param modal: ``bool`` determining if the dialog being created should be modal
- application-wide. (i.e. Other widgets have interaction disabled)
- :param args:
- :param kwargs:
- :return:
- """
- # call base class constructor
- super( MainDialog, self ).__init__( *args, **kwargs )
- # create reference to the qmainwindow widget
- self.ui = qform
- self._instance = None
- # setup widgets to the QDialog
- qform.setupUi(self)
- # add to garbage collection protection
- _GCProtector.widgets.append(self)
- if title:
- self.setWindowTitle(title)
- if flags:
- self.setWindowFlags(flags)
- if modal:
- self.setModal(True)
- self.populateData()
- self.logger.debug('Data successfully populated to widget!')
- self.makeConnections()
- self.logger.debug('Connections successfully made for widget!')
- self.show()
- def makeConnections(self, *args, **kwargs):
- """
- This method handles making connections from the
- Qt widgets to corresponding signals/slots.
- :return:
- """
- pass
- def populateData(self, *args, **kwargs ):
- """
- This method populates the widget with data.
- :return:
- """
- pass
- def closeEvent(self, *args, **kwargs):
- """
- This overrides the base event in ``QDialog``.
- :param args:
- :param kwargs:
- :return:
- """
- # Remove widget objects from GC protection
- if self in _GCProtector.widgets:
- _GCProtector.widgets.remove(self)
- def cancelAction(self):
- """
- This method is run when the user chooses to close the widget via the UI.
- :return:
- """
- self.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement