Advertisement
Guest User

Untitled

a guest
Aug 10th, 2015
1,298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.22 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3.  
  4. """ Module mainWindow: This module contains classes for creating a standard PySide Qt QMainWindow instance. """
  5. import traceback
  6.  
  7. from PySide.QtGui import QDialog, QMainWindow
  8. import logging
  9.  
  10.  
  11. class _GCProtector(object):
  12.     """
  13.    This class acts as a holder for a static class var in order to
  14.    prevent Qt _widgets from being garbage-collected
  15.    """
  16.  
  17.     # declare static class var
  18.     widgets = []
  19.  
  20.  
  21. class MainWindow(QMainWindow):
  22.     """
  23.    This class creates a new QMainWindow and setups widgets onto it.
  24.    """
  25.  
  26.     logger = logging.getLogger( __name__ )
  27.  
  28.  
  29.     def __init__( self, qmainwindow, parent=None, *args, **kwargs ):
  30.         """
  31.        The constructor. Creates an instance of QMainWindow and handles order of
  32.        data population along with connecting signals/slots.
  33.  
  34.        :param parent:
  35.        :return:
  36.        """
  37.  
  38.         # call base class constructor
  39.         super( MainWindow, self ).__init__( *args, **kwargs )
  40.  
  41.         # setup UI widgets to the QMainWindow
  42.         qmainwindow.setupUi(self)
  43.  
  44.         # add to garbage collection protection
  45.         _GCProtector.widgets.append(self)
  46.  
  47.         # create reference to the qmainwindow widget
  48.         self.ui = qmainwindow
  49.         try: self.populateData()
  50.         except Exception:
  51.             self.logger.error('### Could not populate data to widget!!! \n {0}'
  52.                          .format(traceback.print_exc()))
  53.             raise Exception
  54.  
  55.         self.logger.debug('Data successfully populated to widget!')
  56.  
  57.         try: self.makeConnections()
  58.         except Exception:
  59.             self.logger.error('### Could not make connections to signals/slots!!! \n {0}'
  60.                          .format(traceback.print_exc()))
  61.             raise Exception
  62.  
  63.         self.logger.debug('Connections successfully made for widget!')
  64.  
  65.         self.show()
  66.  
  67.  
  68.     def makeConnections(self, *args, **kwargs):
  69.         """
  70.        This method handles making connections from the
  71.        Qt widgets to corresponding signals/slots.
  72.  
  73.        :return:
  74.        """
  75.         pass
  76.  
  77.  
  78.     def populateData(self, *args, **kwargs ):
  79.         """
  80.        This method populates the widget with data.
  81.  
  82.        :return:
  83.        """
  84.         pass
  85.  
  86.  
  87.     def closeEvent(self, *args, **kwargs):
  88.         """
  89.        This overrides the base event in ``QDialog``.
  90.  
  91.        :param args:
  92.        :param kwargs:
  93.        :return:
  94.        """
  95.  
  96.         # Remove widget objects from GC protection
  97.         if self in _GCProtector.widgets:
  98.             _GCProtector.widgets.remove(self)
  99.  
  100.  
  101.     def cancelAction(self):
  102.         """
  103.        This method is run when the user chooses to close the widget via the UI.
  104.  
  105.        :return:
  106.        """
  107.  
  108.         self.close()
  109.  
  110.  
  111. class MainDialog(QDialog):
  112.     """
  113.    This class creates a new QDialog and setups widgets onto it.
  114.    """
  115.  
  116.     logger = logging.getLogger( __name__ )
  117.  
  118.     def __init__( self, qform, parent=None, title=None,
  119.                   flags=None, modal=False, *args, **kwargs ):
  120.         """
  121.        The constructor.
  122.  
  123.        :param qform:
  124.        :param parent: ``QObject`` instance that will act as the parent widget for this dialog.
  125.        :param title: ``str`` indicating the title that the dialog should have.
  126.        :param flags:
  127.        :param modal: ``bool`` determining if the dialog being created should be modal
  128.                    application-wide. (i.e. Other widgets have interaction disabled)
  129.        :param args:
  130.        :param kwargs:
  131.        :return:
  132.        """
  133.  
  134.         # call base class constructor
  135.         super( MainDialog, self ).__init__( *args, **kwargs )
  136.  
  137.         # create reference to the qmainwindow widget
  138.         self.ui = qform
  139.         self._instance = None
  140.  
  141.         # setup widgets to the QDialog
  142.         qform.setupUi(self)
  143.  
  144.         # add to garbage collection protection
  145.         _GCProtector.widgets.append(self)
  146.  
  147.         if title:
  148.             self.setWindowTitle(title)
  149.         if flags:
  150.             self.setWindowFlags(flags)
  151.         if modal:
  152.             self.setModal(True)
  153.  
  154.         self.populateData()
  155.  
  156.         self.logger.debug('Data successfully populated to widget!')
  157.  
  158.         self.makeConnections()
  159.  
  160.         self.logger.debug('Connections successfully made for widget!')
  161.  
  162.         self.show()
  163.  
  164.  
  165.     def makeConnections(self, *args, **kwargs):
  166.         """
  167.        This method handles making connections from the
  168.        Qt widgets to corresponding signals/slots.
  169.  
  170.        :return:
  171.        """
  172.         pass
  173.  
  174.  
  175.     def populateData(self, *args, **kwargs ):
  176.         """
  177.        This method populates the widget with data.
  178.  
  179.        :return:
  180.        """
  181.         pass
  182.  
  183.  
  184.     def closeEvent(self, *args, **kwargs):
  185.         """
  186.        This overrides the base event in ``QDialog``.
  187.  
  188.        :param args:
  189.        :param kwargs:
  190.        :return:
  191.        """
  192.  
  193.         # Remove widget objects from GC protection
  194.         if self in _GCProtector.widgets:
  195.             _GCProtector.widgets.remove(self)
  196.  
  197.  
  198.     def cancelAction(self):
  199.         """
  200.        This method is run when the user chooses to close the widget via the UI.
  201.  
  202.        :return:
  203.        """
  204.  
  205.         self.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement