Ejemplo n.º 1
0
    def __init__(self, worldviewer, parent=None):
        global client
        super(WorldViewerDebug, self).__init__(parent)

        self.setWindowTitle("World Viewer Debug")
        pos = QApplication.instance().desktop().availableGeometry()
        self.move(pos.width() / 2, 0)

        self.worldviewer = worldviewer

        self.objects_tree = QTreeWidget()
        self.objects_tree.setColumnCount(2)
        self.objects_tree.setHeaderLabels([
            "ID", "Name", "Location", "Resource", "Last Modified",
            "Dist to Player"
        ])
        self.objects_tree.setSortingEnabled(True)

        layout = QVBoxLayout()
        for w in (self.objects_tree, ):
            layout.addWidget(w)
        self.setLayout(layout)

        self.obj_update_timer = QTimer(self)
        self.connect(self.obj_update_timer, SIGNAL("timeout()"), self.update)
        self.obj_update_timer.start(CLIENT_UPDATE_FREQ)
Ejemplo n.º 2
0
 def mouseReleaseEvent(self, e):
     """:type e: QMouseEvent.QMouseEvent"""
     if self.__drag_item:
         released_item = self.itemAt(e.pos())
         if released_item and released_item is not self.__drag_item:
             drag_indexes = self.get_indexes(self.__drag_item)
             released_indexes = self.get_indexes(released_item)
             Facade.getInstance().sendNotification(Notes.TREE_MODEL_MOVE, TreeModelMoveVO(drag_indexes, released_indexes))
     QTreeWidget.mouseReleaseEvent(self, e)
     self.__drag_item = None
Ejemplo n.º 3
0
 def mouseReleaseEvent(self, e):
     """:type e: QMouseEvent.QMouseEvent"""
     if self.__drag_item:
         released_item = self.itemAt(e.pos())
         if released_item and released_item is not self.__drag_item:
             drag_indexes = self.get_indexes(self.__drag_item)
             released_indexes = self.get_indexes(released_item)
             Facade.getInstance().sendNotification(
                 Notes.TREE_MODEL_MOVE,
                 TreeModelMoveVO(drag_indexes, released_indexes))
     QTreeWidget.mouseReleaseEvent(self, e)
     self.__drag_item = None
Ejemplo n.º 4
0
    def testWidgetIterator(self):
        treeWidget = QTreeWidget()
        treeWidget.setColumnCount(1)
        items = []
        for i in range(10):
            items.append(QTreeWidgetItem(None, ["item: %d" % i]))
        treeWidget.insertTopLevelItems(0, items)

        index = 0
        for it in QTreeWidgetItemIterator(treeWidget):
            self.assertEqual(it.value().text(0), "item: %d" % index)
            index += 1
Ejemplo n.º 5
0
class AbstractContext(QWidget):
    def __init__(self, parent, labels):
        super(AbstractContext, self).__init__(parent)

        self.add = QAction(QIcon(":/img/add.png"), self.tr("Add"), self)

        self.delete = QAction(QIcon(":/img/delete.png"),
                              self.tr("Delete"),
                              self)

        self.edit = QAction(QIcon(":/img/edit.png"), self.tr("Edit"), self)

        self.print_ = QAction(QIcon(":/img/print.png"), self.tr("Print"), self)

        self.toolbar = QToolBar(self.tr("Controls"), self)

        self.toolbar.addAction(self.add)
        self.toolbar.addAction(self.delete)
        self.toolbar.addAction(self.edit)
        self.toolbar.addAction(self.print_)

        self.entries = QTreeWidget(self)
        self.entries.setHeaderLabels(labels)

        self.search = QLineEdit(self)

        self.search_by = QComboBox(self)
        self.search_by.insertItems(len(labels), labels)

        layout = QGridLayout(self)

        layout.addWidget(self.toolbar,                         0, 0)
        layout.addWidget(self.entries,                         1, 0, 1, 8)
        layout.addWidget(QLabel(self.tr("Search for:"), self), 2, 0)
        layout.addWidget(self.search,                          2, 1)
        layout.addWidget(QLabel(self.tr("by"), self),          2, 2)
        layout.addWidget(self.search_by,                       2, 3)

    def add_entry(self, entry):
        pass

    def delete_entry(self, entry):
        index = self.entries.indexOfTopLevelItem(entry)
        self.entries.takeTopLevelItem(index)

    def edit_entry(self, entry, new_data):
        for column, data in new_data:
            entry.setText(column, data)

    def confirm_deletion(self, what):
        return True
Ejemplo n.º 6
0
    def _createOutputTree(self):
        """
        A QtreeWidget. Initially used to display those pesky
        dword comparison to immediate values.
        """
        self.tree_label = QtGui.QLabel('Tree Output')

        self.tree = QTreeWidget()
        self.tree.setColumnCount(3)
        self.tree.setColumnWidth(0, 150)
        self.tree.setColumnWidth(1, 150)
        self.tree.setColumnWidth(2, 50)

        # Connect signals to slots
        self.tree.itemDoubleClicked.connect(self._treeElementDoubleClicked)
Ejemplo n.º 7
0
 def __init__(self, targetLayoutContainer):
     self.tree = QTreeWidget()
     self.tree.addAction(QAction('Add nested favorite', self.tree, triggered=self.add_child))
     self.tree.addAction(QAction('Edit Title', self.tree, triggered=self.edit_title))
     self.tree.addAction(QAction('HTML page', self.tree, triggered=self.create_html_page))
     self.tree.addAction(QAction('Delete', self.tree, triggered=self.delete))
     self.tree.setContextMenuPolicy(Qt.ActionsContextMenu)
     targetLayoutContainer.addWidget(self.tree)
Ejemplo n.º 8
0
    def _init_detail_layout(self):
        detail_layout = QGridLayout()
        self.key_tree = QTreeWidget()
        self.key_tree.setColumnCount(2)
        self.key_tree.setColumnWidth(0, 200)
        self.key_tree.setHeaderLabels(['Key', 'Size'])

        self.value_tree = QTreeWidget()
        self.value_tree.setColumnCount(2)
        self.value_tree.setColumnWidth(0, 200)
        self.value_tree.setHeaderLabels(['Path', 'Size'])

        detail_layout.setColumnStretch(0, 1)
        detail_layout.setColumnStretch(1, 1)
        detail_layout.addWidget(self.value_tree)
        detail_layout.addWidget(self.key_tree)
        return detail_layout
Ejemplo n.º 9
0
    def createGUI(self):
        self.layout = QVBoxLayout()
        # self.pushupsListWidget = QListWidget(self)
        self.pushupsListWidget = QTreeWidget(self)

        self.pushupsListWidget.setMinimumHeight(250)
        #self.pushupsListWidget.setMaximumWidth(500)
        self.pushupsListWidget.setAlternatingRowColors(True)

        self.pushupsListWidget.doubleClicked.connect(self.doubleClick_Test)

        self.pushupsListWidget.setContextMenuPolicy(Qt.CustomContextMenu)
        self.pushupsListWidget.customContextMenuRequested.connect(
            self._customMenu)

        self._populateTree()

        self.layout.addWidget(self.pushupsListWidget)

        self.setLayout(self.layout)
Ejemplo n.º 10
0
	def __init__(self, parent=None):
		super(MainWindow, self).__init__(parent)
		self.stopSign = 0
		self.ws = 0
		self.comment = ''
		self.clipboard = QApplication.clipboard()
		self.setWindowTitle(u'勤務表')
		self.setWindowIcon(QIcon('./img/tray.png'))
		self.systemTrayIcon = QSystemTrayIcon(self)

		self.systemTrayIcon.setIcon(QIcon('./img/tray.png'))
		self.systemTrayIcon.setVisible(True)
		self.systemTrayIcon.show()

		self.systemTrayIcon.activated.connect(self.on_systemTrayIcon_activated)
		self.tableLabel = QLabel('select *.xls file')
		self.pathText   = QLineEdit()
		self.triggerBtn = QPushButton(u'檢查 / 開始')
		self.browseBtn  = QPushButton(u'   瀏覽   ')
		self.stopBtn	= QPushButton(u'停止')
		self.table      = QTableWidget(26,0,self)
		self.setUpTable()


		self.hbox1	  = QHBoxLayout()
		self.hbox2	  = QHBoxLayout()
		self.hbox3    = QHBoxLayout()
		self.hbox4	  = QHBoxLayout()

		self.hbox1.addWidget(self.pathText)
		self.hbox1.addWidget(self.browseBtn)
		self.hbox1.addWidget(self.triggerBtn)
		self.browseBtn.clicked.connect(self.OpenFile)
		self.triggerBtn.clicked.connect(self.setupTypeThread)
		self.stopBtn.clicked.connect(self.threadStop)

		self.status = QTreeWidget(self)
		
		self.status.setHeaderHidden(True)
		self.hbox2.addWidget(self.status)
		self.hbox3.addWidget(self.table)
		self.hbox4.addWidget(self.stopBtn)
		self.setGeometry(200, 200, 700, 400)
		self.status.setFixedHeight (80)
		self.layout = QVBoxLayout()
		self.layout.addWidget(self.tableLabel)
		self.layout.addLayout(self.hbox1)
		self.layout.addLayout(self.hbox2)
		self.layout.addLayout(self.hbox3)
		self.layout.addLayout(self.hbox4)
		self.setLayout(self.layout)
		self.stopBtn.setEnabled(False)
Ejemplo n.º 11
0
    def __init__(self, parent=None):
        QTreeWidget.__init__(self, parent)
        # the following makes this treeview look like a collection of
        # checkboxes and labels, and not like a tree view.
        # use the standard window background
        self.viewport().setBackgroundRole(QPalette.Window)
        # no border
        self.setFrameShape(QFrame.NoFrame)
        # disable selections to hide the selection shadow when moving the mouse
        # over items.  Selection isn't needed anyways, because we are just
        # interested in checking and unchecking items
        self.setSelectionMode(QTreeWidget.NoSelection)
        # disable focus for the whole widget.  This avoids focus lines around
        # items when checking or unchecking items.  Since the items aren't
        # editable, we don't need focus anyways.
        self.setFocusPolicy(Qt.NoFocus)
        # hide the header
        self.setHeaderHidden(True)

        # the following mappings store groups and options
        self._groups = {}
        self._options = {}
    def __init__(self, parent=None):
        QTreeWidget.__init__(self, parent)
        # the following makes this treeview look like a collection of
        # checkboxes and labels, and not like a tree view.
        # use the standard window background
        self.viewport().setBackgroundRole(QPalette.Window)
        # no border
        self.setFrameShape(QFrame.NoFrame)
        # disable selections to hide the selection shadow when moving the mouse
        # over items.  Selection isn't needed anyways, because we are just
        # interested in checking and unchecking items
        self.setSelectionMode(QTreeWidget.NoSelection)
        # disable focus for the whole widget.  This avoids focus lines around
        # items when checking or unchecking items.  Since the items aren't
        # editable, we don't need focus anyways.
        self.setFocusPolicy(Qt.NoFocus)
        # hide the header
        self.setHeaderHidden(True)

        # the following mappings store groups and options
        self._groups = {}
        self._options = {}
Ejemplo n.º 13
0
    def __init__(self, *args, **kwargs ):
        
        self.index = 0
        if kwargs.has_key( 'index' ):
            self.index = kwargs.pop( 'index' )
        
        QWidget.__init__( self, *args, **kwargs )
        mainLayout = QVBoxLayout( self ); mainLayout.setContentsMargins(0,0,0,0)
        mainLayout.setSpacing(0)
        
        treeWidget = QTreeWidget()
        treeWidget.setColumnCount(1)
        headerItem = treeWidget.headerItem()
        headerItem.setText( 0, 'Controller list'.decode('utf-8') )
        button = QPushButton( 'Load Controllers'.decode('utf-8') )
        mainLayout.addWidget( treeWidget )
        mainLayout.addWidget( button )

        self.treeWidget = treeWidget
        QtCore.QObject.connect( button, QtCore.SIGNAL("clicked()"), self.loadControllers )
        
        self.uiInfoPath = Window.infoBaseDir + '/Widget_ctlList_%d.json' % self.index
        self.loadInfo()
Ejemplo n.º 14
0
    def _createOutputTree(self):
        """
        A QtreeWidget. Initially used to display those pesky
        dword comparison to immediate values.
        """
        self.tree_label = QtGui.QLabel('Tree Output')

        self.tree = QTreeWidget()
        self.tree.setColumnCount(3)
        self.tree.setColumnWidth(0, 150)
        self.tree.setColumnWidth(1, 150)
        self.tree.setColumnWidth(2, 50)

        # Connect signals to slots
        self.tree.itemDoubleClicked.connect(self._treeElementDoubleClicked)
Ejemplo n.º 15
0
    def testWidgetIterator(self):
        treeWidget = QTreeWidget()
        treeWidget.setColumnCount(1)
        items = []
        for i in range(10):
            items.append(QTreeWidgetItem(None, ['item: %d' % i]))
        treeWidget.insertTopLevelItems(0, items)

        index = 0
        for it in QTreeWidgetItemIterator(treeWidget):
            self.assertEqual(it.value().text(0), 'item: %d' % index)
            index += 1
Ejemplo n.º 16
0
 def createGUI(self):
     self.layout = QVBoxLayout()
     # self.pushupsListWidget = QListWidget(self)
     self.pushupsListWidget = QTreeWidget(self)
     
     self.pushupsListWidget.setMinimumHeight(250)        
     #self.pushupsListWidget.setMaximumWidth(500)
     self.pushupsListWidget.setAlternatingRowColors(True)
     
     self.pushupsListWidget.doubleClicked.connect(self.doubleClick_Test)
     
     self.pushupsListWidget.setContextMenuPolicy(Qt.CustomContextMenu)
     self.pushupsListWidget.customContextMenuRequested.connect(self._customMenu)
     
     self._populateTree()
     
     self.layout.addWidget(self.pushupsListWidget)
     
     self.setLayout(self.layout)   
Ejemplo n.º 17
0
    def testCase(self):
        treeWidget = QTreeWidget()
        treeWidget.setColumnCount(1)
        items = []
        for i in range(10):
            items.append(QTreeWidgetItem(None, ["item: %i" % i]))

        treeWidget.insertTopLevelItems(0, items);
        _iter = QTreeWidgetItemIterator(treeWidget)
        index = 0
        while(_iter.value()):
            item = _iter.value()
            self.assert_(item is items[index])
            index += 1
            _iter += 1
Ejemplo n.º 18
0
    def __init__(self, worldviewer, parent=None):
        global client
        super(WorldViewerDebug, self).__init__(parent)

        self.setWindowTitle("World Viewer Debug")
        pos = QApplication.instance().desktop().availableGeometry()
        self.move(pos.width()/2, 0)

        self.worldviewer = worldviewer

        self.objects_tree = QTreeWidget()
        self.objects_tree.setColumnCount(2)
        self.objects_tree.setHeaderLabels(["ID", "Name", "Location", "Resource", "Last Modified", "Dist to Player"])
        self.objects_tree.setSortingEnabled(True)

        layout = QVBoxLayout()
        for w in (self.objects_tree,):
            layout.addWidget(w)
        self.setLayout(layout)

        self.obj_update_timer = QTimer(self)
        self.connect(self.obj_update_timer, SIGNAL("timeout()"), self.update)
        self.obj_update_timer.start(CLIENT_UPDATE_FREQ)
Ejemplo n.º 19
0
    def __init__(self, parent, labels):
        super(AbstractContext, self).__init__(parent)

        self.add = QAction(QIcon(":/img/add.png"), self.tr("Add"), self)

        self.delete = QAction(QIcon(":/img/delete.png"),
                              self.tr("Delete"),
                              self)

        self.edit = QAction(QIcon(":/img/edit.png"), self.tr("Edit"), self)

        self.print_ = QAction(QIcon(":/img/print.png"), self.tr("Print"), self)

        self.toolbar = QToolBar(self.tr("Controls"), self)

        self.toolbar.addAction(self.add)
        self.toolbar.addAction(self.delete)
        self.toolbar.addAction(self.edit)
        self.toolbar.addAction(self.print_)

        self.entries = QTreeWidget(self)
        self.entries.setHeaderLabels(labels)

        self.search = QLineEdit(self)

        self.search_by = QComboBox(self)
        self.search_by.insertItems(len(labels), labels)

        layout = QGridLayout(self)

        layout.addWidget(self.toolbar,                         0, 0)
        layout.addWidget(self.entries,                         1, 0, 1, 8)
        layout.addWidget(QLabel(self.tr("Search for:"), self), 2, 0)
        layout.addWidget(self.search,                          2, 1)
        layout.addWidget(QLabel(self.tr("by"), self),          2, 2)
        layout.addWidget(self.search_by,                       2, 3)
class CorporateEventPanel(QtGui.QWidget):
    tableCorporateEvent = None
    rowCorporateEvent = 0
    columnListCorporateEvent = "Asset;Gross Amount;Net Amount;Payment Date".split(
        ";")

    def __init__(self):
        super(self.__class__, self).__init__()
        self.layout = QtGui.QGridLayout(self)
        self.clearTables()

    def clearTables(self):
        self.row = 0
        self.createCorpEventTable()

    def createCorpEventTable(self):
        self.treeCorporateEvent = QTreeWidget()
        self.layout.addWidget(self.treeCorporateEvent, 2, 0, 3, 3)

    def renderCorpEvent(self, corporateEventPositionDictAsset):
        self.clearTables()
        self.treeCorporateEvent.setColumnCount(3)
        self.treeCorporateEvent.setHeaderLabels(self.columnListCorporateEvent)
        for key, cep in corporateEventPositionDictAsset.items():
            l1 = QTreeWidgetItem(
                [key, str(cep.accGrossAmount),
                 str(cep.accNetAmount)])
            for ce in cep.corporateEventList:
                l1_child = QTreeWidgetItem([
                    None,
                    str(ce.grossAmount),
                    str(ce.netAmount),
                    str(ce.paymentDate)
                ])
                l1.addChild(l1_child)
                self.treeCorporateEvent.addTopLevelItem(l1)
Ejemplo n.º 21
0
            else:
                child.setText(level, str(val))                            
            child.setExpanded(True)
    else:
        child = QTreeWidgetItem()
        child.setText(level, str(value))
        item.addChild(child)

def fill_widget(widget, value):
    widget.clear()
    fill_item(widget.invisibleRootItem(), value, 0)

app = QtGui.QApplication.instance()
if app is None:
    app = QtGui.QApplication(sys.argv)

d = { 'key1': 'value1', 
  'key2': 'value2',
  'sub_list': [1,2,3, ['good', 'test'], {'abc': 3, 'cde': 9}],
  'key4': object(),
  'sub_dict': {'key1': 'value1',
            'key2': 'value2'}}

widget = QTreeWidget()
widget.setHeaderLabels(['level0', 'level1', 'level2', 'level3'])
fill_widget(widget, d)
widget.show()

sys.exit(app.exec_())

Ejemplo n.º 22
0
class DirectoryDetectWindow(QWidget):
    def __init__(self):
        QWidget.__init__(self)
        self.setWindowTitle('Directory Detective Tool')
        main_layout = QVBoxLayout()

        path_layout = self._init_path_layout()
        guide_layout = self._init_guide_layout()
        detail_layout = self._init_detail_layout()

        main_layout.addLayout(path_layout)
        main_layout.addLayout(guide_layout)
        main_layout.addLayout(detail_layout)
        self.setLayout(main_layout)
        self.setGeometry(300, 300, 400, 350)
        self._connect()

        self._walk_folder = None
        self._last_path = None
        self.directory_info = None
        self._button_list = list()
        self._last_info = None

    def _init_path_layout(self):
        path_layout = QHBoxLayout()
        self.input_path = QLineEdit(self)
        self.input_path.setFixedHeight(35)
        self.input_path.setPlaceholderText('Type search path here')
        self.button_path = QPushButton('Scan', self)
        self.button_path.setFixedHeight(35)
        self.button_path.setFocus()

        path_layout.addWidget(QLabel('Path:', self))
        path_layout.addWidget(self.input_path, stretch=True)
        path_layout.addWidget(self.button_path)
        return path_layout

    def _init_guide_layout(self):
        self.guide_layout = QHBoxLayout()
        self.guide_layout.setAlignment(Qt.AlignLeft)
        return self.guide_layout

    def refresh_trees(self, store_list, ignore_list):
        self._refresh_key_tree(store_list, ignore_list=ignore_list)
        self._refresh_value_tree(store_list)

    def add_button(self, name, store_list, ignore_list=None):
        button = QPushButton(name)
        button.setFixedSize(60, 35)
        item = (name, button, store_list, ignore_list)
        self._button_list.append(item)
        self.guide_layout.addWidget(button)
        self.refresh_trees(store_list=store_list, ignore_list=ignore_list)
        button.clicked.connect(lambda: self._set_button(item=item))

    def _set_button(self, item):
        index_item = self._button_list.index(item)

        if len(self._button_list) > index_item + 1:
            for name, button, store_list, ignore_list in self._button_list[index_item + 1:]:
                button.setParent(None)

        name, button, store_list, ignore_list = item
        self.refresh_trees(store_list=store_list, ignore_list=ignore_list)

    def _init_detail_layout(self):
        detail_layout = QGridLayout()
        self.key_tree = QTreeWidget()
        self.key_tree.setColumnCount(2)
        self.key_tree.setColumnWidth(0, 200)
        self.key_tree.setHeaderLabels(['Key', 'Size'])

        self.value_tree = QTreeWidget()
        self.value_tree.setColumnCount(2)
        self.value_tree.setColumnWidth(0, 200)
        self.value_tree.setHeaderLabels(['Path', 'Size'])

        detail_layout.setColumnStretch(0, 1)
        detail_layout.setColumnStretch(1, 1)
        detail_layout.addWidget(self.value_tree)
        detail_layout.addWidget(self.key_tree)
        return detail_layout

    def _connect(self):
        self.button_path.clicked.connect(self.button_path_clicked)
        self.key_tree.doubleClicked.connect(self.key_tree_double_clicked)

    def button_path_clicked(self):
        del self._button_list[:]
        for _ in range(self.guide_layout.count()):
            current_item = self.guide_layout.takeAt(0)
            current_item.widget().setParent(None)

        path = unicode.encode(self.input_path.text())
        if self._last_path != path:
            self._walk_folder = walk_folder.run(path=path)

        max_level = 4
        self.directory_info = list(self._walk_folder.get_children_by_level(level=max_level))
        self.add_button('/', self.directory_info)

    def key_tree_double_clicked(self, selected_index=QModelIndex()):
        select_data = selected_index.data()
        ignore_list = [select_data]
        for item in self._button_list[1:]:
            ignore_list.append(item[0])

        store_dict = dict(self._last_info.get_children_by_key(key=select_data, ignore_list=ignore_list).sort_dictionary)
        store_list = list()

        for item in store_dict.values():
            store_list.extend(item['children'])

        store_list = list(set(store_list))
        self.add_button(select_data, store_list or list(), ignore_list=ignore_list)

    def _get_size(self, size):
        return '{} <> {}%'.format(human_size(size), round(size * 100 / self._walk_folder.total_size, 2))

    def _refresh_key_tree(self, directory_info, ignore_list=None):
        info = LabelInfo(make_dictionary_by_classification(directory_info, ignore_list))

        # Clear
        for _ in range(self.key_tree.topLevelItemCount()):
            self.key_tree.takeTopLevelItem(0)

        # Add
        for k, v in info.sort_dictionary:
            tree_widget = QTreeWidgetItem()
            tree_widget.setText(0, k)
            total_size = 0
            for child in v['children']:
                total_size += child.total_size
            tree_widget.setText(1, self._get_size(v[constant.sort_size_name]))
            self.key_tree.addTopLevelItem(tree_widget)
        self._last_info = info

    def _refresh_value_tree(self, directory_info):
        info = sorted(directory_info, key=lambda x: x.total_size, reverse=True)

        # Clear
        for _ in range(self.value_tree.topLevelItemCount()):
            self.value_tree.takeTopLevelItem(0)

        # Add
        for item in info:
            tree_widget = QTreeWidgetItem()
            tree_widget.setText(0, item.part_path)
            tree_widget.setText(1, self._get_size(item.total_size))
            self.value_tree.addTopLevelItem(tree_widget)
 def createCorpEventTable(self):
     self.treeCorporateEvent = QTreeWidget()
     self.layout.addWidget(self.treeCorporateEvent, 2, 0, 3, 3)
Ejemplo n.º 24
0
class WorldViewerDebug(QDialog):
    def __init__(self, worldviewer, parent=None):
        global client
        super(WorldViewerDebug, self).__init__(parent)

        self.setWindowTitle("World Viewer Debug")
        pos = QApplication.instance().desktop().availableGeometry()
        self.move(pos.width() / 2, 0)

        self.worldviewer = worldviewer

        self.objects_tree = QTreeWidget()
        self.objects_tree.setColumnCount(2)
        self.objects_tree.setHeaderLabels([
            "ID", "Name", "Location", "Resource", "Last Modified",
            "Dist to Player"
        ])
        self.objects_tree.setSortingEnabled(True)

        layout = QVBoxLayout()
        for w in (self.objects_tree, ):
            layout.addWidget(w)
        self.setLayout(layout)

        self.obj_update_timer = QTimer(self)
        self.connect(self.obj_update_timer, SIGNAL("timeout()"), self.update)
        self.obj_update_timer.start(CLIENT_UPDATE_FREQ)

    def sizeHint(self):
        desktop = QApplication.instance().desktop()
        geom = desktop.availableGeometry()
        return QSize(geom.width() / 2, geom.height())

    def show(self):
        '''This is overridden to not allow this to be shown when running in
        non-debug mode'''
        super(WorldViewerDebug, self).show()
        if not DEBUG:
            self.hide()

    def update(self):
        items = []
        playerx, playery = 0, 0
        for objid, obj in self.worldviewer.world_objects.items():
            if "Object.ScriptedObject.Character" in obj['_types']:
                playerx = obj['loc']['x']
                playery = obj['loc']['y']
                break

        for objid, obj in self.worldviewer.world_objects.items():
            name = obj['name']
            try:
                locx = obj['loc']['x']
            except KeyError:
                continue
            locy = obj['loc']['y']
            locz = obj['loc']['z']
            resource = obj['resource']
            last_modified = datetime.datetime.fromtimestamp(
                int(str(obj['last_modified']['$date'])[:-3]))

            row = self.objects_tree.findItems(objid, Qt.MatchExactly, column=0)
            if row:
                # Update it if it exists already
                item = row[0]
            if not row:
                # If Item does not exist in the tree widget already, make a new one.
                item = QTreeWidgetItem(self.objects_tree)
                items.append(item)

            for i, param in enumerate(
                (objid, name, (locx, locy, locz), resource, last_modified,
                 "%04d" % euclidian(locx, locy, playerx, playery))):
                item.setText(i, str(param))

        if items:
            self.objects_tree.insertTopLevelItems(0, items)
Ejemplo n.º 25
0
 def mousePressEvent(self, e):
     """:type e: QMouseEvent.QMouseEvent"""
     pressed_item = self.itemAt(e.pos())
     if pressed_item:
         self.__drag_item = pressed_item
     QTreeWidget.mousePressEvent(self, e)
Ejemplo n.º 26
0
    def __init__(self, *args, **kwargs):

        super(Widget_objectList, self).__init__(*args, **kwargs)
        self.installEventFilter(self)
        mainLayout = QVBoxLayout(self)

        treeWidget = QTreeWidget()
        treeWidget.setColumnCount(2)
        headerItem = treeWidget.headerItem()
        headerItem.setText(0, "Target Node")
        headerItem.setText(1, "Attribute Name")
        treeWidget.setRootIsDecorated(False)
        treeWidget.setStyleSheet(
            "QTreeWidget::item { border-left: 1px solid gray;border-bottom: 1px solid gray; padding: 3px}\
                                 QTreeWidget{ font-size:13px;}")
        treeWidget.header().setStyleSheet("font-size:12px;")

        w_buttons = QWidget()
        lay_buttons = QHBoxLayout(w_buttons)
        lay_buttons.setContentsMargins(0, 0, 0, 0)
        lay_buttons.setSpacing(0)
        button_load = QPushButton("Load List")
        button_load.setStyleSheet("font:14px;")
        lay_buttons.addWidget(button_load)

        mainLayout.addWidget(treeWidget)
        mainLayout.addWidget(w_buttons)

        self.treeWidget = treeWidget
        QtCore.QObject.connect(
            treeWidget, QtCore.SIGNAL("itemChanged(QTreeWidgetItem * , int )"),
            self.writeData)
        QtCore.QObject.connect(button_load, QtCore.SIGNAL("clicked()"),
                               self.cmd_loadList)
Ejemplo n.º 27
0
    def __init__(self, parent=None):
        super(MassAttribute_UI, self).__init__(parent)
        # Abstract
        self.applier = self.Applikator(self)
        self.selection = []
        self.callback = None
        self.ctx = None
        # storing found attributes' types to avoid double check
        self.solved = {}
        self.setLocale(QLocale.C)
        self.setAttribute(Qt.WA_DeleteOnClose)
        self.setAttribute(Qt.WA_QuitOnClose)

        self.setFixedWidth(300)
        self.setWindowTitle('Massive Attribute Modifier')

        # UI
        L_main = QVBoxLayout()

        self.WV_title = QLabel('')
        self.WV_title.setVisible(False)
        self.WV_title.setFont(QFont('Verdana', 10))
        self.WV_title.setContentsMargins(0, 0, 0, 7)

        self.WB_select = QPushButton('Select')
        self.WB_select.setVisible(False)
        self.WB_select.setFixedWidth(50)
        self.WB_select.clicked.connect(lambda: cmds.select(self.selection))

        self.WB_update = QPushButton('Update')
        self.WB_update.setFixedWidth(50)
        self.WB_update.clicked.connect(
            lambda: self.update_attributes(cmds.ls(sl=True)))

        self.WV_search = Filter()
        self.WV_search.textChanged.connect(self.filter)

        self.WC_cases = QCheckBox('Case sensitive')
        self.WC_cases.stateChanged.connect(self.filter)

        self.WC_types = QCheckBox('Type filtering')

        self.WL_attrtype = QComboBox()
        self.WL_attrtype.setEnabled(False)

        for i, ctx in enumerate(sorted(self.ctx_wide)):
            self.WL_attrtype.addItem(ctx.title())
            self.WL_attrtype.setItemIcon(i, self.ctx_icons[ctx])

        L_attrtype = line(self.WC_types, self.WL_attrtype)

        self.WC_types.stateChanged.connect(
            partial(self.update_attributes, self.selection))
        self.WC_types.stateChanged.connect(self.WL_attrtype.setEnabled)
        self.WL_attrtype.currentIndexChanged.connect(self.filter)

        self.WC_liveu = QCheckBox('Live')
        self.WC_liveu.stateChanged.connect(self.WB_update.setDisabled)
        self.WC_liveu.stateChanged.connect(self.set_callback)

        self.WC_histo = QCheckBox('Load history')
        self.WC_histo.setChecked(True)
        self.WC_histo.stateChanged.connect(
            partial(self.update_attributes, self.selection))

        self.WC_child = QCheckBox('Children')
        self.WC_child.stateChanged.connect(
            partial(self.update_attributes, self.selection))

        options = group(
            'Options', line(self.WC_cases, L_attrtype),
            line(self.WC_child, self.WC_histo, self.WC_liveu, self.WB_update))
        options.layout().setSpacing(2)

        self.WL_attributes = QTreeWidget()
        self.WL_attributes.setStyleSheet(
            'QTreeView {alternate-background-color: #1b1b1b;}')
        self.WL_attributes.setAlternatingRowColors(True)
        self.WL_attributes.setHeaderHidden(True)
        self.WL_attributes.setRootIsDecorated(False)

        self.objs_attr = set()
        self.shps_attr = set()

        self.W_EDI_float = FloatBox()
        self.W_EDI_int = IntBox()
        self.W_EDI_enum = QComboBox()
        self.W_EDI_bool = QCheckBox()
        self.W_EDI_str = QLineEdit()
        self.W_EDI_d3 = Double3()
        self.W_EDI_d4 = Double4()
        self.W_EDI_color = ColorPicker()

        # Final layout
        L_title = line(self.WV_title, self.WB_select)
        L_title.setStretch(0, 1)
        L_main.addLayout(L_title)
        L_main.setAlignment(Qt.AlignLeft)
        L_main.addWidget(self.WV_search)
        L_main.addWidget(options)
        L_main.addWidget(self.WL_attributes)
        L_edits = col(self.W_EDI_bool, self.W_EDI_int, self.W_EDI_float,
                      self.W_EDI_enum, self.W_EDI_str, self.W_EDI_d3,
                      self.W_EDI_d4, self.W_EDI_color)
        L_edits.setContentsMargins(0, 8, 0, 0)
        L_main.addLayout(L_edits)
        L_main.setStretch(3, 1)
        L_main.setSpacing(2)

        self.appliers = {
            'float': self.applier.apply_float,
            'enum': self.applier.apply_enum,
            'bool': self.applier.apply_bool,
            'time': self.applier.apply_float,
            'byte': self.applier.apply_int,
            'angle': self.applier.apply_float,
            'string': self.applier.apply_str,
            'float3': self.applier.apply_d3,
            'float4': self.applier.apply_d4,
            'color': self.applier.apply_color
        }

        self.setLayout(L_main)

        # final settings
        self.WL_attributes.itemSelectionChanged.connect(self.update_setter)
        self.applier.unset_editors()
Ejemplo n.º 28
0
class MassAttribute_UI(QDialog):
    """
    The main UI
    """
    class Applikator(QObject):
        """
        This is the core applier which toggle the display of the corresponding widget and handling events' connections
        """
        def __init__(self, parent=None):
            super(MassAttribute_UI.Applikator, self).__init__()
            self.root = parent

        def widget_event(self, t):
            """
            Return the correct widget's event depending on attribute's type
            :param t: the attribute's type
            :type  t: str
            :return: the event
            :rtype : Signal
            """
            return {
                'float': self.root.W_EDI_float.valueChanged,
                'enum': self.root.W_EDI_enum.currentIndexChanged,
                'int': self.root.W_EDI_int.valueChanged,
                'bool': self.root.W_EDI_bool.stateChanged,
                'str': self.root.W_EDI_str.textChanged,
                'd3': self.root.W_EDI_d3.valuesChanged,
                'd4': self.root.W_EDI_d4.valuesChanged,
                'color': self.root.W_EDI_color.colorChanged
            }[t]

        def unset_editors(self):
            """
            Toggle off all editors and disconnect the current one
            """
            for widget in (self.root.W_EDI_float, self.root.W_EDI_int,
                           self.root.W_EDI_enum, self.root.W_EDI_bool,
                           self.root.W_EDI_str, self.root.W_EDI_d3,
                           self.root.W_EDI_d4, self.root.W_EDI_color):
                widget.setVisible(False)

            # trying to force disconnection
            try:
                self.widget_event(self.root.ctx).disconnect(
                    self.root.apply_value)
            except (KeyError, RuntimeError):
                pass

        def prepare(applier_name):
            """
            A decorator to prepare the attribute depending on type for the corresponding widget and getting the
            attribute's value
            :param applier_name: attribute's type
            :type  applier_name: str
            """
            def sub_wrapper(func):
                def wrapper(self, attr_path):
                    self.unset_editors()
                    self.root.ctx = applier_name
                    self.root.__getattribute__('W_EDI_%s' %
                                               applier_name).setVisible(True)
                    ret = func(self, cmds.getAttr(attr_path), attr_path)
                    return ret

                return wrapper

            return sub_wrapper

        @staticmethod
        def get_bounds(obj, attr, min_default, max_default):
            """
            Try to retrieve the range for the given attribute, if min or max fail it'll set default values
            :param         obj: the object's name
            :type          obj: str
            :param        attr: attribute's name
            :type         attr: str
            :param min_default: minimum default value
            :param max_default: max default value
            :type  min_default: float | int
            :type  max_default: float | int
            :return: minimum, maximum
            :rtype : tuple
            """
            try:
                assert cmds.attributeQuery(attr, n=obj, mxe=True)
                maxi = cmds.attributeQuery(attr, n=obj, max=True)[0]
            except (RuntimeError, AssertionError):
                maxi = max_default
            try:
                assert cmds.attributeQuery(attr, n=obj, mne=True)
                mini = cmds.attributeQuery(attr, n=obj, min=True)[0]
            except (RuntimeError, AssertionError):
                mini = min_default
            return mini, maxi

        @prepare('float')
        def apply_float(self, value, path):
            """
            Float attribute case
            :param value: attribute's value
            :param  path: attribute's path = obj.attr
            """
            obj, attr = path.split('.', 1)
            self.root.W_EDI_float.setRange(
                *self.get_bounds(obj, attr, -100.0, 100.0))
            self.root.W_EDI_float.setValue(value)

        @prepare('enum')
        def apply_enum(self, value, path):
            """Enum case"""
            self.root.W_EDI_enum.clear()
            obj, attr = path.split('.', 1)
            try:
                enums = [
                    enum.split('=')[0] for enum in cmds.attributeQuery(
                        attr, n=obj, listEnum=True)[0].split(':')
                ]
            except RuntimeError:
                self.apply_int(path)
            else:
                self.root.W_EDI_enum.addItems(enums)
                self.root.W_EDI_enum.setCurrentIndex(
                    enums.index(cmds.getAttr(path, asString=True)))

        @prepare('int')
        def apply_int(self, value, path):
            """Integer case"""
            obj, attr = path.split('.', 1)
            self.root.W_EDI_int.setRange(
                *self.get_bounds(obj, attr, -1000, 1000))
            self.root.W_EDI_int.setValue(value)

        @prepare('bool')
        def apply_bool(self, value, path):
            """Boolean case"""
            self.root.W_EDI_bool.setChecked(value)
            self.root.W_EDI_bool.setText(path.split('.', 1)[1])

        @prepare('str')
        def apply_str(self, value, path):
            """String case"""
            self.root.W_EDI_str.setText(value)

        @prepare('d3')
        def apply_d3(self, value, path):
            """3D array case"""
            self.root.W_EDI_d3.setValues(value[0])

        @prepare('d4')
        def apply_d4(self, value, path):
            """4D array case"""
            self.root.W_EDI_d4.setValues(value[0])

        @prepare('color')
        def apply_color(self, value, path):
            """Color case"""
            try:
                colors = value[0]
                self.root.W_EDI_color.setColor([int(c * 255) for c in colors])
            except TypeError:
                self.apply_int(value, path)

    class Attribute(str):
        """
        A custom string attribute class to ship more information into the string variable
        """
        def __new__(cls, path='', super_type=Object):
            obj, attr = path.split('.', 1)

            str_obj = str.__new__(cls, attr)

            str_obj.obj, str_obj.attr = obj, attr
            str_obj.path = path
            str_obj.super_type = super_type
            str_obj.type = None

            return str_obj

    # static variables to pre-load icons and attributes short names
    ctx_icons = {
        'float': QIcon(':render_decomposeMatrix.png'),
        'enum': QIcon(':showLineNumbers.png'),
        'bool': QIcon(':out_decomposeMatrix.png'),
        'time': QIcon(':time.svg'),
        'byte': QIcon(':out_defaultTextureList.png'),
        'angle': QIcon(':angleDim.png'),
        'string': QIcon(':text.png'),
        'float3': QIcon(':animCurveTA.svg'),
        'float4': QIcon(':animCurveTA.svg'),
        'color': QIcon(':clampColors.svg')
    }

    for ctx in ('doubleLinear', 'double', 'long', 'short'):
        ctx_icons[ctx] = ctx_icons['float']

    ctx_icons['double3'] = ctx_icons['float3']
    ctx_icons['double4'] = ctx_icons['float4']

    ctx_wide = {
        'float': ('float', 'doubleLinear', 'double', 'long', 'short'),
        'enum': ('enum', ),
        'bool': ('bool', ),
        'time': ('time', ),
        'byte': ('byte', ),
        'angle': ('doubleAngle', ),
        'string': ('string', ),
        'float3': ('double3', 'float3'),
        'float4': ('double4', 'float4'),
        'color': ('color', )
    }

    def __init__(self, parent=None):
        super(MassAttribute_UI, self).__init__(parent)
        # Abstract
        self.applier = self.Applikator(self)
        self.selection = []
        self.callback = None
        self.ctx = None
        # storing found attributes' types to avoid double check
        self.solved = {}
        self.setLocale(QLocale.C)
        self.setAttribute(Qt.WA_DeleteOnClose)
        self.setAttribute(Qt.WA_QuitOnClose)

        self.setFixedWidth(300)
        self.setWindowTitle('Massive Attribute Modifier')

        # UI
        L_main = QVBoxLayout()

        self.WV_title = QLabel('')
        self.WV_title.setVisible(False)
        self.WV_title.setFont(QFont('Verdana', 10))
        self.WV_title.setContentsMargins(0, 0, 0, 7)

        self.WB_select = QPushButton('Select')
        self.WB_select.setVisible(False)
        self.WB_select.setFixedWidth(50)
        self.WB_select.clicked.connect(lambda: cmds.select(self.selection))

        self.WB_update = QPushButton('Update')
        self.WB_update.setFixedWidth(50)
        self.WB_update.clicked.connect(
            lambda: self.update_attributes(cmds.ls(sl=True)))

        self.WV_search = Filter()
        self.WV_search.textChanged.connect(self.filter)

        self.WC_cases = QCheckBox('Case sensitive')
        self.WC_cases.stateChanged.connect(self.filter)

        self.WC_types = QCheckBox('Type filtering')

        self.WL_attrtype = QComboBox()
        self.WL_attrtype.setEnabled(False)

        for i, ctx in enumerate(sorted(self.ctx_wide)):
            self.WL_attrtype.addItem(ctx.title())
            self.WL_attrtype.setItemIcon(i, self.ctx_icons[ctx])

        L_attrtype = line(self.WC_types, self.WL_attrtype)

        self.WC_types.stateChanged.connect(
            partial(self.update_attributes, self.selection))
        self.WC_types.stateChanged.connect(self.WL_attrtype.setEnabled)
        self.WL_attrtype.currentIndexChanged.connect(self.filter)

        self.WC_liveu = QCheckBox('Live')
        self.WC_liveu.stateChanged.connect(self.WB_update.setDisabled)
        self.WC_liveu.stateChanged.connect(self.set_callback)

        self.WC_histo = QCheckBox('Load history')
        self.WC_histo.setChecked(True)
        self.WC_histo.stateChanged.connect(
            partial(self.update_attributes, self.selection))

        self.WC_child = QCheckBox('Children')
        self.WC_child.stateChanged.connect(
            partial(self.update_attributes, self.selection))

        options = group(
            'Options', line(self.WC_cases, L_attrtype),
            line(self.WC_child, self.WC_histo, self.WC_liveu, self.WB_update))
        options.layout().setSpacing(2)

        self.WL_attributes = QTreeWidget()
        self.WL_attributes.setStyleSheet(
            'QTreeView {alternate-background-color: #1b1b1b;}')
        self.WL_attributes.setAlternatingRowColors(True)
        self.WL_attributes.setHeaderHidden(True)
        self.WL_attributes.setRootIsDecorated(False)

        self.objs_attr = set()
        self.shps_attr = set()

        self.W_EDI_float = FloatBox()
        self.W_EDI_int = IntBox()
        self.W_EDI_enum = QComboBox()
        self.W_EDI_bool = QCheckBox()
        self.W_EDI_str = QLineEdit()
        self.W_EDI_d3 = Double3()
        self.W_EDI_d4 = Double4()
        self.W_EDI_color = ColorPicker()

        # Final layout
        L_title = line(self.WV_title, self.WB_select)
        L_title.setStretch(0, 1)
        L_main.addLayout(L_title)
        L_main.setAlignment(Qt.AlignLeft)
        L_main.addWidget(self.WV_search)
        L_main.addWidget(options)
        L_main.addWidget(self.WL_attributes)
        L_edits = col(self.W_EDI_bool, self.W_EDI_int, self.W_EDI_float,
                      self.W_EDI_enum, self.W_EDI_str, self.W_EDI_d3,
                      self.W_EDI_d4, self.W_EDI_color)
        L_edits.setContentsMargins(0, 8, 0, 0)
        L_main.addLayout(L_edits)
        L_main.setStretch(3, 1)
        L_main.setSpacing(2)

        self.appliers = {
            'float': self.applier.apply_float,
            'enum': self.applier.apply_enum,
            'bool': self.applier.apply_bool,
            'time': self.applier.apply_float,
            'byte': self.applier.apply_int,
            'angle': self.applier.apply_float,
            'string': self.applier.apply_str,
            'float3': self.applier.apply_d3,
            'float4': self.applier.apply_d4,
            'color': self.applier.apply_color
        }

        self.setLayout(L_main)

        # final settings
        self.WL_attributes.itemSelectionChanged.connect(self.update_setter)
        self.applier.unset_editors()

    def closeEvent(self, *args, **kwargs):
        self.set_callback(False)

    def set_callback(self, state):
        """
        Toggle selection event callback
        :param state: checkbox's state
        :type  state: bool | int
        """
        if state and not self.callback:
            self.callback = MEventMessage.addEventCallback(
                'SelectionChanged', self.update_attributes)
            self.update_attributes(cmds.ls(sl=True))

        elif not state and self.callback:
            MMessage.removeCallback(self.callback)
            self.callback = None

    @staticmethod
    def format_title(nodes):
        """
        Extract the matching characters from a given nodes selection, if begin matches it will return "joint*" with a
        wildcard when names don't match
        :param nodes: objects' list
        :type  nodes: list | tuple
        :return: the formatted name with the corresponding characters
        :rtype : str
        """
        res = None

        if nodes:
            # we get the first node as a reference
            node = nodes[0]
            # and compare with the other nodes
            subs = [w for w in nodes if w != node]

            l = 1
            valid = True
            # will continue until l (length) match the full name's length or until names don't match
            while l < len(node) and valid:
                for sub in subs:
                    if not sub.startswith(node[:l]):
                        valid = False
                        break

                else:
                    l += 1

            # if matching characters isn't long enough we only display the number of nodes selected
            if l <= 3:
                res = '%i objects' % len(nodes)

            # otherwise showing matching pattern
            elif l < len(node) or len(nodes) > 1:
                res = node[:l - 1] + '* (%i objects)' % len(nodes)

            else:
                res = node

        return res

    @staticmethod
    def get_history(node):
        """
        Extract history for the given node
        :rtype: list
        """
        return cmds.listHistory(node, il=2, pdo=True) or []

    @staticmethod
    def get_shapes(node):
        """
        Extract shape(s) for the given node
        :rtype: list
        """
        return cmds.listRelatives(node, s=True, ni=True, f=True)

    def get_attributes_type(self, attrs):
        """
        For a given list of attributes of type Attribute, will loop through and fill the type parameter of the
         attribute with the corresponding type, if type is invalid or not handled, it'll remove it
        :param attrs: attributes' list
        :type  attrs: [MassAttribute_UI.Attribute]
        :return: cleaned and filled attributes' list
        :rtype: [MassAttribute_UI.Attribute]
        """
        attrs = list(attrs)
        # first we sort the attributes' list
        attrs.sort()

        # then we try to extract the attribute's type
        for i, attr in enumerate(attrs):
            try:
                if attr.attr in self.solved:
                    attr.type = self.solved[attr.attr]
                    raise RuntimeError
                tpe = cmds.getAttr(attr.path, typ=True)
                assert tpe
                attr.type = tpe
                self.solved[attr.attr] = tpe
            except (AssertionError, ValueError, RuntimeError):
                pass

        # defining a to-remove list
        rm_list = set()

        layers = {'3': 'XYZ', '4': 'XYZW'}
        for i, attr in enumerate(attrs):
            if i in rm_list:
                continue

            # we handle some special cases here, if ever the attribute list contains RGB and separate R, G and B we
            # assume it's a color, if it's a double3 or float3 and we find the corresponding XYZ, we remove then to
            # avoid duplicates

            if attr.endswith('RGB'):
                if '%sR' % attr[:-3] in attrs:
                    attr.type = 'color'
                    for chan in 'RGB':
                        rm_list.add(attrs.index('%s%s' % (attr[:-3], chan)))

            # if the attribute's type isn't in the list, we remove
            elif attr.type not in MassAttribute_UI.ctx_icons:
                rm_list.add(i)

            elif attr.endswith('R'):
                if '%sG' % attr[:-1] in attrs and attr[:-1] in attrs:
                    attr.type = 'color'
                    for chan in 'RGB':
                        rm_list.add(attrs.index('%s%s' % (attr[:-1], chan)))

            elif attr.type in ('double3', 'double4', 'float3', 'float4'):
                if '%sX' % attr in attrs:
                    for chan in layers[attr.type[-1]]:
                        rm_list.add(attrs.index('%s%s' % (attr, chan)))

        # finally cleaning the list
        for i in sorted(rm_list, reverse=True):
            attrs.pop(i)

        return attrs

    def apply_value(self, value):
        """
        When the value is modified in the UI, we forward the given value and applies to the object's
        :param value: attribute's value, mixed type
        :type  value: mixed
        """
        # We get the only selected object in list and get it's super type (Shape, History or Object) and
        # type (float, int, string)
        item = self.WL_attributes.selectedItems()[0]
        attr = item.attribute
        shape = attr.super_type == Shape
        histo = attr.super_type == History
        tpe = item.attribute.type

        # eq dict for each context
        value = {
            'bool': bool,
            'int': int,
            'float': float,
            'enum': int,
            'str': str,
            'd3': list,
            'd4': list,
            'color': list
        }[self.ctx](value)

        # converting the selection into a set
        cmds.undoInfo(openChunk=True)
        targets = set(self.selection)

        # we propagate to children if 'Children' checkbox is on
        if self.WC_child.isChecked():
            for obj in list(targets):
                targets |= set(cmds.listRelatives(obj, ad=True))

        # if the target attribute is on the history, we add all selection's history to the list
        if histo:
            for obj in list(targets):
                targets.remove(obj)
                targets |= set(self.get_history(obj))

        # then we loop through target objects
        for obj in targets:
            # if the target is on the shape we get object's shape
            if shape and not histo:
                shapes = self.get_shapes(obj)

                if obj in shapes:
                    continue
                else:
                    obj = shapes[0]

            # then we try to apply depending on attribute's type
            try:
                correct_path = attr.path.replace(attr.obj, obj)

                if tpe == 'string':
                    cmds.setAttr(correct_path, value, type='string')

                elif tpe in ('double3', 'double4', 'float3', 'float4',
                             'color'):
                    cmds.setAttr(correct_path,
                                 *value,
                                 type='double%d' % len(value))

                else:
                    cmds.setAttr(correct_path, value)

            except RuntimeError:
                pass

        cmds.undoInfo(closeChunk=True)

    def update_setter(self):
        """
        When the list's selection changes we update the applier widget
        """
        item = self.WL_attributes.selectedItems()
        # abort if no item is selected
        if not len(item):
            return

        # getting attribute's parameter
        attr = item[0].attribute

        if len(self.selection):
            try:
                # looping until we find a context having the current attribute's type
                for applier in self.ctx_wide:
                    if attr.type in self.ctx_wide[applier]:
                        break
                # then we apply for the given path (obj.attribute)
                self.appliers[applier](attr.path)

                # and connecting event to the self.apply_value function
                self.applier.widget_event(self.ctx).connect(self.apply_value)

            # otherwise selection or type is invalid
            except IndexError:
                self.ctx = None

    def update_attributes(self, selection=None, *args):
        """
        Update the attributes for the given selection, looping through objects' attributes, finding attr in common
        between all objects then cleaning the lists, doing the same for shapes and / or histories
        :param selection: object's selection
        """
        # redefining lists as set to intersect union etc
        self.objs_attr = set()
        self.shps_attr = set()

        # pre init
        self.WL_attributes.clear()
        self.applier.unset_editors()

        self.selection = selection or (cmds.ls(
            sl=True) if self.WC_liveu.isChecked() else self.selection)

        self.WV_title.setText(self.format_title(self.selection))
        self.WV_title.setVisible(bool(len(self.selection)))
        self.WB_select.setVisible(bool(len(self.selection)))

        if not len(self.selection):
            return

        def get_usable_attrs(obj, super_type):
            """
            Small internal function to get a compatible attributes' list for the given object and assign the given
            super_type to it (Object, Shape or History)
            :param        obj: object's name
            :type         obj: str
            :param super_type: attribute's main type
            :type  super_type: Object | Shape | History
            :return:
            """
            return set([
                MassAttribute_UI.Attribute('%s.%s' % (obj, attr), super_type)
                for attr in cmds.listAttr(
                    obj, se=True, ro=False, m=True, w=True)
            ])

        if len(self.selection):
            self.objs_attr = get_usable_attrs(self.selection[0], Object)

            # if we also want the object's history we add it to the initial set
            if self.WC_histo.isChecked():
                for histo in self.get_history(self.selection[0]):
                    self.objs_attr |= get_usable_attrs(histo, History)

            # filling the shape's set
            for shape in (self.get_shapes(self.selection[0]) or []):
                self.shps_attr |= get_usable_attrs(shape, Shape)

            # if selection's length bigger than one we compare by intersection with the other sets
            if len(self.selection) > 1:
                for obj in self.selection:
                    sub_attr = get_usable_attrs(obj, Object)

                    if self.WC_histo.isChecked():
                        for histo in self.get_history(obj):
                            sub_attr |= get_usable_attrs(histo, History)

                    self.objs_attr.intersection_update(sub_attr)

                    for shape in (self.get_shapes(self.selection[0]) or []):
                        self.shps_attr.intersection_update(
                            get_usable_attrs(shape, Shape))

            # finally getting all intersecting attributes' types
            self.objs_attr = self.get_attributes_type(self.objs_attr)
            self.shps_attr = self.get_attributes_type(self.shps_attr)

        # and filtering the list
        self.filter()

    def add_set(self, iterable, title=None):
        """
        Adding the given iterable to the list with a first Separator object with given title
        :param iterable: list of item's attributes
        :param    title: Separator's name
        """
        if len(iterable):
            # if title is given we first add a Separator item to indicate coming list title
            if title:
                self.WL_attributes.addTopLevelItem(
                    QTreeWidget_Separator(title))

            items = []
            for attr in sorted(iterable):
                item = QTreeWidgetItem([attr])
                # assigning the attribute itself inside a custom parameter
                item.attribute = attr
                items.append(item)

            # finally adding all the items to the list
            self.WL_attributes.addTopLevelItems(items)

    def filter(self):
        """
        Filter the list with UI's parameters, such as name or type filtering, etc
        """
        # pre cleaning
        self.WL_attributes.clear()

        # using regex compile to avoid re execution over many attributes
        mask = self.WV_search.text()
        case = 0 if self.WC_cases.isChecked() else re.IGNORECASE
        re_start = re.compile(r'^%s.*?' % mask, case)
        re_cont = re.compile(r'.*?%s.*?' % mask, case)

        # getting the four different lists
        obj_start = set([at for at in self.objs_attr if re_start.search(at)])
        shp_start = set([at for at in self.shps_attr if re_start.search(at)])

        # if type filtering is one we only extract the wanted attribute's type
        if self.WC_types.isChecked():
            obj_start = set([
                at for at in obj_start if at.type in self.ctx_wide[
                    self.WL_attrtype.currentText().lower()]
            ])
            shp_start = set([
                at for at in shp_start if at.type in self.ctx_wide[
                    self.WL_attrtype.currentText().lower()]
            ])

        # finally adding the current sets if there is a mask we add the also the containing matches
        if mask:
            # getting contains filtering and type containers filtering
            obj_contains = obj_start.symmetric_difference(
                set([at for at in self.objs_attr if re_cont.search(at)]))
            shp_contains = shp_start.symmetric_difference(
                set([at for at in self.shps_attr if re_cont.search(at)]))
            if self.WC_types.isChecked():
                obj_contains = set([
                    at for at in obj_contains if at.type in self.ctx_wide[
                        self.WL_attrtype.currentText().lower()]
                ])
                shp_contains = set([
                    at for at in shp_contains if at.type in self.ctx_wide[
                        self.WL_attrtype.currentText().lower()]
                ])

            # adding the sets
            self.add_set(obj_start, 'Obj attributes starting with')
            self.add_set(obj_contains, 'Obj attributes containing')
            self.add_set(shp_start, 'Shape attributes starting with')
            self.add_set(shp_contains, 'Shape attributes containing')

        else:
            self.add_set(obj_start, 'Object\'s attributes')
            self.add_set(shp_start, 'Shape\'s attributes')

        # and we select the first one if ever there is something in the list
        if self.WL_attributes.topLevelItemCount():
            self.WL_attributes.setItemSelected(
                self.WL_attributes.topLevelItem(1), True)
Ejemplo n.º 29
0
    def __init__(self, *args, **kwargs):

        super(Widget_connectionList, self).__init__(*args, **kwargs)
        self.installEventFilter(self)
        mainLayout = QVBoxLayout(self)
        mainLayout.setContentsMargins(5, 5, 5, 5)
        mainLayout.setSpacing(0)

        treeWidget = QTreeWidget()
        treeWidget.setColumnCount(3)
        headerItem = treeWidget.headerItem()
        headerItem.setText(0, "Attribute")
        headerItem.setText(1, "Target Node")
        headerItem.setText(2, "Target Attr")
        treeWidget.setRootIsDecorated(False)
        self.treeWidget = treeWidget

        treeWidget.setStyleSheet(
            "QTreeWidget::item { border-bottom: 1px solid gray; padding:1px}\
                                         QTreeWidget{ font-size:13px;}")
        treeWidget.header().setStyleSheet("font-size:12px;")

        w_buttons = QWidget()
        lay_buttons = QHBoxLayout(w_buttons)
        lay_buttons.setContentsMargins(0, 0, 0, 0)
        lay_buttons.setSpacing(5)
        button_add = QPushButton("Add Line")
        button_remove = QPushButton("Remove Line")
        lay_buttons.addWidget(button_add)
        lay_buttons.addWidget(button_remove)

        mainLayout.addWidget(treeWidget)
        mainLayout.addWidget(w_buttons)
        button_add.clicked.connect(self.cmd_addLine)
        button_remove.clicked.connect(self.cmd_removeLine)

        treeWidget.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
        QtCore.QObject.connect(
            treeWidget, QtCore.SIGNAL('customContextMenuRequested(QPoint)'),
            self.loadContextMenu)
        self.treeWidget = treeWidget
        self.readData()
Ejemplo n.º 30
0
class WorldViewerDebug(QDialog):
    def __init__(self, worldviewer, parent=None):
        global client
        super(WorldViewerDebug, self).__init__(parent)

        self.setWindowTitle("World Viewer Debug")
        pos = QApplication.instance().desktop().availableGeometry()
        self.move(pos.width()/2, 0)

        self.worldviewer = worldviewer

        self.objects_tree = QTreeWidget()
        self.objects_tree.setColumnCount(2)
        self.objects_tree.setHeaderLabels(["ID", "Name", "Location", "Resource", "Last Modified", "Dist to Player"])
        self.objects_tree.setSortingEnabled(True)

        layout = QVBoxLayout()
        for w in (self.objects_tree,):
            layout.addWidget(w)
        self.setLayout(layout)

        self.obj_update_timer = QTimer(self)
        self.connect(self.obj_update_timer, SIGNAL("timeout()"), self.update)
        self.obj_update_timer.start(CLIENT_UPDATE_FREQ)

    def sizeHint(self):
        desktop = QApplication.instance().desktop()
        geom = desktop.availableGeometry()
        return QSize(geom.width()/2, geom.height())

    def show(self):
        '''This is overridden to not allow this to be shown when running in
        non-debug mode'''
        super(WorldViewerDebug, self).show()
        if not DEBUG:
            self.hide()

    def update(self):
        items = []
        playerx, playery = 0, 0
        for objid, obj in self.worldviewer.world_objects.items():
            if "Object.ScriptedObject.Character" in obj['_types']:
                playerx = obj['loc']['x']
                playery = obj['loc']['y']
                break

        for objid, obj in self.worldviewer.world_objects.items():
            name = obj['name']
            try:
                locx = obj['loc']['x']
            except KeyError:
                continue
            locy = obj['loc']['y']
            locz = obj['loc']['z']
            resource = obj['resource']
            last_modified = datetime.datetime.fromtimestamp(int(str(obj['last_modified']['$date'])[:-3]))

            row = self.objects_tree.findItems(objid, Qt.MatchExactly, column=0)
            if row:
                # Update it if it exists already
                item = row[0]
            if not row:
                # If Item does not exist in the tree widget already, make a new one.
                item = QTreeWidgetItem(self.objects_tree)
                items.append(item)

            for i, param in enumerate((objid, name, (locx, locy, locz), resource, last_modified, "%04d" % euclidian(locx, locy, playerx, playery))):
                item.setText(i, str(param))

        if items:
            self.objects_tree.insertTopLevelItems(0, items)
Ejemplo n.º 31
0
 def __init__(self, *args, **kwargs):
     QTreeWidget.__init__(self, *args, **kwargs)
     self.setItemDelegate(ExpensesItemDelegate(self.itemDelegate()))
     self._idToName = None
Ejemplo n.º 32
0
    def __init__(self, *args, **kwargs):

        super(Widget_referenceFiles, self).__init__(*args, **kwargs)
        self.installEventFilter(self)
        mainLayout = QVBoxLayout(self)
        mainLayout.setContentsMargins(0, 0, 0, 0)
        mainLayout.setSpacing(8)

        treeWidget = QTreeWidget()
        headerItem = treeWidget.headerItem()
        checkBox_allItems = QCheckBox(treeWidget)
        checkBox_allItems.setFixedWidth(20)
        checkBox_allItems.setChecked(True)
        checkBox_allItems.setStyleSheet("margin: 6px 6px")
        treeWidget.setItemWidget(headerItem, 0, checkBox_allItems)
        headerItem.setText(0, "")
        headerItem.setText(1, "Reference File Name")
        treeWidget.setRootIsDecorated(False)
        treeWidget.setStyleSheet(
            "QTreeWidget::item { border-left: 1px solid gray;border-bottom: 1px solid gray; padding: 3px}\
                                 QTreeWidget{ font-size:13px;}")
        treeWidget.header().setStyleSheet("font-size:12px;")
        treeWidget.setColumnWidth(0, 22)

        w_buttons = QWidget()
        lay_buttons = QHBoxLayout(w_buttons)
        lay_buttons.setContentsMargins(0, 0, 0, 0)
        buttonReference = QPushButton("REFERENCE")
        buttonCleanExceptSelected = QPushButton("CLEAN EXCEPT SELECTED")
        lay_buttons.addWidget(buttonCleanExceptSelected)
        lay_buttons.addWidget(buttonReference)

        mainLayout.addWidget(treeWidget)
        mainLayout.addWidget(w_buttons)

        self.treeWidget = treeWidget

        self.treeWidget.setSelectionMode(QAbstractItemView.ExtendedSelection)
        self.treeWidget.checkBox_allItems = checkBox_allItems

        checkBox_allItems.stateChanged.connect(self.cmd_setCheckAsset)
        treeWidget.itemPressed.connect(self.cmd_selectItems)
        buttonReference.clicked.connect(self.referenceFiles)
        buttonCleanExceptSelected.clicked.connect(self.cmd_cleanScene)

        self.cmd_loadList()
Ejemplo n.º 33
0
if __name__ == '__main__':
    app = 0
    if QApplication.instance():
        app = QApplication.instance()
    else:
        app = QApplication(sys.argv)

    l1 = QTreeWidgetItem(["ALFA", "100"])
    l2 = QTreeWidgetItem(["GRUMA", "200"])

    for i in range(3):
        l1_child = QTreeWidgetItem([None, str(i * 10), "Child C" + str(i)])
        l1.addChild(l1_child)

    for j in range(2):
        l2_child = QTreeWidgetItem([None, str(j * 20), "Child CC" + str(j)])
        l2.addChild(l2_child)

    w = QWidget()
    w.resize(510, 210)

    tw = QTreeWidget(w)
    tw.resize(500, 200)
    tw.setColumnCount(3)
    tw.setHeaderLabels(["Asset", "Gross Amount", "Payment Date"])
    tw.addTopLevelItem(l1)
    tw.addTopLevelItem(l2)

    w.show()
    sys.exit(app.exec_())
Ejemplo n.º 34
0
class PushupList(QWidget):
    '''
    classdocs
    ''' 
    
    deletePushup = Signal(int)
    deletePushups_in_a_day = Signal(tuple)
    
    def __init__(self, pushups):
        '''
        Constructor
        '''
        QWidget.__init__(self)
        
        self.pushups = pushups
        self.createGUI()
    
    def createGUI(self):
        self.layout = QVBoxLayout()
        # self.pushupsListWidget = QListWidget(self)
        self.pushupsListWidget = QTreeWidget(self)
        
        self.pushupsListWidget.setMinimumHeight(250)        
        #self.pushupsListWidget.setMaximumWidth(500)
        self.pushupsListWidget.setAlternatingRowColors(True)
        
        self.pushupsListWidget.doubleClicked.connect(self.doubleClick_Test)
        
        self.pushupsListWidget.setContextMenuPolicy(Qt.CustomContextMenu)
        self.pushupsListWidget.customContextMenuRequested.connect(self._customMenu)
        
        self._populateTree()
        
        self.layout.addWidget(self.pushupsListWidget)
        
        self.setLayout(self.layout)   
    
    # Slot
    def _customMenu(self):
        selectedItems = self.pushupsListWidget.selectedItems()
        
        if selectedItems is not None :
            selectedItem = selectedItems[0] 
            
            if selectedItem.parent() is not None : # Child Item selected
                menu = QMenu()
                
                delete = QAction(self.pushupsListWidget)
                delete.setText("Delete this pushup")
                delete.triggered.connect(self._emitDeleteSignal)
                menu.addAction(delete)
                menu.exec_(QCursor.pos())
            else : # Top level Item selected
                menu = QMenu()
             
                delete = QAction(self.pushupsListWidget)
                delete.setText("Delete this day and all of its exercises")
                delete.triggered.connect(self._emitDeleteDaySignal)
                menu.addAction(delete)
                menu.exec_(QCursor.pos())
    
    def _emitDeleteSignal(self):
        selectedItem = self.pushupsListWidget.selectedItems()[0]
        pushupId = selectedItem.data(0, Qt.UserRole)._id
        
        self.deletePushup.emit(pushupId)
    
    def _emitDeleteDaySignal(self):
        selectedItem = self.pushupsListWidget.selectedItems()[0]
        
        treeWidgetItems = selectedItem.takeChildren()
        
        pushupsIdList = []
        for item in treeWidgetItems:
            pushup = item.data(0, Qt.UserRole)._id
            pushupsIdList.append(pushup)
            
        self.deletePushups_in_a_day.emit(pushupsIdList)

    def _populateTree(self):
        self.pushupsListWidget.clear()
        self.pushupsListWidget.setColumnCount(4)
        self.pushupsListWidget.setHeaderLabels(["Date", "TotalPushups", 
                                                "Series", "Repetitions",
                                                "Average Heart Rate"])
        self.pushupsListWidget.setSortingEnabled(True)
        self.pushupsListWidget.setColumnWidth(0, 180)
        self.pushupsListWidget.setColumnWidth(4, 150)
        
        pushupDict = self._getPushupDictionary()
        
        for it, dayOfExercise in enumerate(sorted(pushupDict.keys())):                  
             
            dateItem = QTreeWidgetItem()
            
            dayLabel = dayOfExercise.strftime("%Y/%m/%d")
            
            dateItem.setText(0, "\n" + dayLabel + "\nDay : " + str(it))
            
            self.pushupsListWidget.addTopLevelItem(dateItem)
            
            totalPushups = 0
            for pushup in pushupDict[dayOfExercise]:
                pushupItem = QTreeWidgetItem()
                
                pushupItem.setText(2, "#" + str(pushup._series))
                pushupItem.setText(3, str(pushup._repetitions))
                pushupItem.setText(4, str(pushup._averageHeartRate))
                pushupItem.setData(0, Qt.UserRole, pushup)
                
                totalPushups = totalPushups + pushup._repetitions 
                
                dateItem.addChild(pushupItem)  
            
            dateItem.setText(1, str(totalPushups))
                 
                
    def doubleClick_Test(self):
        selectedItems = self.pushupsListWidget.selectedItems()
        
        if selectedItems is not None :
            selectedItem = selectedItems[0] 
            
            if selectedItem.parent() is not None : # Child Item selected
                selectedPushups = self.pushupsListWidget.selectedItems()[0].data(0, Qt.UserRole)
                print selectedPushups._id    
            else :
                print "Top level widget double clicked"                
        
    def reloadPushupsList(self, pushups):
        self.pushups = pushups
        self._populateTree()
        
    def _populateListWidget(self):
        ''' 
        unused old method
        '''
        
        self.pushupsListWidget.clear()
        
        pushupDict = self._getPushupDictionary()
        
        for dayOfExercise in pushupDict:                  
            listItemString = "Date : " + dayOfExercise + "\n"
            listItem_Data = []
            
            for pushup in pushupDict[dayOfExercise]:
                listItemString += "Series : " + str(pushup._series) + \
                                  " Repetition : " + str(pushup._repetitions) + "\n"
                listItem_Data.append(pushup)
                             
            listItem = QListWidgetItem(listItemString)
            listItem.setData(Qt.UserRole, listItem_Data)
            
            self.pushupsListWidget.addItem(listItem)        
            
    def _getPushupDictionary(self):
        '''
        Returns a dictionary with the following structure : 
        - Key : date of the exercises. Type datetime.date
        - Value : list containing pushups made that day . Type : [Pushup model object]    
        
        example : 
        {
            2014-08-18: [pushupModelObj1, pushupModelObj2, pushupModelObj3],
            2014-08-19: [pushupModelObj4, pushupModelObj5, pushupModelObj6]
        } 
        '''
        pushupDateList = {} # dictionary initialization
        
        for pushup in self.pushups:
            if not pushupDateList.has_key(pushup._date):
                pushupsList = [pushup]
                pushupDateList[pushup._date] = pushupsList
            else:
                pushupDateList[pushup._date].append(pushup)
                 
#         for k in pushupDateList.keys():
#             print k
#             
#             for pu in pushupDateList[k]:
#                 print pu
         
        return pushupDateList    
        
        
        
        
        
        
        
        
        
        
        
        
        
        
Ejemplo n.º 35
0
    def __init__(self, parent=None):
        super(MassAttribute_UI, self).__init__(parent)
        # Abstract
        self.applier = self.Applikator(self)
        self.selection = []
        self.callback = None
        self.ctx = None
        # storing found attributes' types to avoid double check
        self.solved = {}
        self.setLocale(QLocale.C)
        self.setAttribute(Qt.WA_DeleteOnClose)
        self.setAttribute(Qt.WA_QuitOnClose)

        self.setFixedWidth(300)
        self.setWindowTitle('Massive Attribute Modifier')

        # UI
        L_main = QVBoxLayout()

        self.WV_title = QLabel('')
        self.WV_title.setVisible(False)
        self.WV_title.setFont(QFont('Verdana', 10))
        self.WV_title.setContentsMargins(0, 0, 0, 7)

        self.WB_select = QPushButton('Select')
        self.WB_select.setVisible(False)
        self.WB_select.setFixedWidth(50)
        self.WB_select.clicked.connect(lambda: cmds.select(self.selection))

        self.WB_update = QPushButton('Update')
        self.WB_update.setFixedWidth(50)
        self.WB_update.clicked.connect(lambda:self.update_attributes(cmds.ls(sl=True)))

        self.WV_search = Filter()
        self.WV_search.textChanged.connect(self.filter)

        self.WC_cases = QCheckBox('Case sensitive')
        self.WC_cases.stateChanged.connect(self.filter)

        self.WC_types = QCheckBox('Type filtering')

        self.WL_attrtype = QComboBox()
        self.WL_attrtype.setEnabled(False)

        for i, ctx in enumerate(sorted(self.ctx_wide)):
            self.WL_attrtype.addItem(ctx.title())
            self.WL_attrtype.setItemIcon(i, self.ctx_icons[ctx])

        L_attrtype = line(self.WC_types, self.WL_attrtype)

        self.WC_types.stateChanged.connect(partial(self.update_attributes, self.selection))
        self.WC_types.stateChanged.connect(self.WL_attrtype.setEnabled)
        self.WL_attrtype.currentIndexChanged.connect(self.filter)

        self.WC_liveu = QCheckBox('Live')
        self.WC_liveu.stateChanged.connect(self.WB_update.setDisabled)
        self.WC_liveu.stateChanged.connect(self.set_callback)

        self.WC_histo = QCheckBox('Load history')
        self.WC_histo.setChecked(True)
        self.WC_histo.stateChanged.connect(partial(self.update_attributes, self.selection))

        self.WC_child = QCheckBox('Children')
        self.WC_child.stateChanged.connect(partial(self.update_attributes, self.selection))

        options = group('Options', line(self.WC_cases, L_attrtype),
                        line(self.WC_child, self.WC_histo, self.WC_liveu, self.WB_update))
        options.layout().setSpacing(2)

        self.WL_attributes = QTreeWidget()
        self.WL_attributes.setStyleSheet('QTreeView {alternate-background-color: #1b1b1b;}')
        self.WL_attributes.setAlternatingRowColors(True)
        self.WL_attributes.setHeaderHidden(True)
        self.WL_attributes.setRootIsDecorated(False)

        self.objs_attr = set()
        self.shps_attr = set()

        self.W_EDI_float = FloatBox()
        self.W_EDI_int = IntBox()
        self.W_EDI_enum = QComboBox()
        self.W_EDI_bool = QCheckBox()
        self.W_EDI_str = QLineEdit()
        self.W_EDI_d3 = Double3()
        self.W_EDI_d4 = Double4()
        self.W_EDI_color = ColorPicker()

        # Final layout
        L_title = line(self.WV_title, self.WB_select)
        L_title.setStretch(0, 1)
        L_main.addLayout(L_title)
        L_main.setAlignment(Qt.AlignLeft)
        L_main.addWidget(self.WV_search)
        L_main.addWidget(options)
        L_main.addWidget(self.WL_attributes)
        L_edits = col(self.W_EDI_bool, self.W_EDI_int, self.W_EDI_float,
                      self.W_EDI_enum, self.W_EDI_str, self.W_EDI_d3, self.W_EDI_d4,
                      self.W_EDI_color)
        L_edits.setContentsMargins(0, 8, 0, 0)
        L_main.addLayout(L_edits)
        L_main.setStretch(3, 1)
        L_main.setSpacing(2)

        self.appliers = {'float': self.applier.apply_float,
                         'enum': self.applier.apply_enum,
                         'bool': self.applier.apply_bool,
                         'time': self.applier.apply_float,
                         'byte': self.applier.apply_int,
                         'angle': self.applier.apply_float,
                         'string': self.applier.apply_str,
                         'float3': self.applier.apply_d3,
                         'float4': self.applier.apply_d4,
                         'color': self.applier.apply_color}

        self.setLayout(L_main)

        # final settings
        self.WL_attributes.itemSelectionChanged.connect(self.update_setter)
        self.applier.unset_editors()
Ejemplo n.º 36
0
class MainWindow(QDialog):

	def __init__(self, parent=None):
		super(MainWindow, self).__init__(parent)
		self.stopSign = 0
		self.ws = 0
		self.comment = ''
		self.clipboard = QApplication.clipboard()
		self.setWindowTitle(u'勤務表')
		self.setWindowIcon(QIcon('./img/tray.png'))
		self.systemTrayIcon = QSystemTrayIcon(self)

		self.systemTrayIcon.setIcon(QIcon('./img/tray.png'))
		self.systemTrayIcon.setVisible(True)
		self.systemTrayIcon.show()

		self.systemTrayIcon.activated.connect(self.on_systemTrayIcon_activated)
		self.tableLabel = QLabel('select *.xls file')
		self.pathText   = QLineEdit()
		self.triggerBtn = QPushButton(u'檢查 / 開始')
		self.browseBtn  = QPushButton(u'   瀏覽   ')
		self.stopBtn	= QPushButton(u'停止')
		self.table      = QTableWidget(26,0,self)
		self.setUpTable()


		self.hbox1	  = QHBoxLayout()
		self.hbox2	  = QHBoxLayout()
		self.hbox3    = QHBoxLayout()
		self.hbox4	  = QHBoxLayout()

		self.hbox1.addWidget(self.pathText)
		self.hbox1.addWidget(self.browseBtn)
		self.hbox1.addWidget(self.triggerBtn)
		self.browseBtn.clicked.connect(self.OpenFile)
		self.triggerBtn.clicked.connect(self.setupTypeThread)
		self.stopBtn.clicked.connect(self.threadStop)

		self.status = QTreeWidget(self)
		
		self.status.setHeaderHidden(True)
		self.hbox2.addWidget(self.status)
		self.hbox3.addWidget(self.table)
		self.hbox4.addWidget(self.stopBtn)
		self.setGeometry(200, 200, 700, 400)
		self.status.setFixedHeight (80)
		self.layout = QVBoxLayout()
		self.layout.addWidget(self.tableLabel)
		self.layout.addLayout(self.hbox1)
		self.layout.addLayout(self.hbox2)
		self.layout.addLayout(self.hbox3)
		self.layout.addLayout(self.hbox4)
		self.setLayout(self.layout)
		self.stopBtn.setEnabled(False)

	def setUpTable(self):
		self.table.horizontalHeader().setVisible(False)
		for i in xrange(0, 26, 1):
			self.vhfont = QFont('Times', pointSize = 10, weight=QFont.Bold)
			timePos = i+6
			if   i == 0:
				item = QTableWidgetItem(u'[標題]')
			elif i == 1:
				item = QTableWidgetItem(u'[設定]')
			elif 2 < timePos < 24:
				item = QTableWidgetItem(('{0}~{1}').format(timePos, timePos+1))
			else:
				item = QTableWidgetItem(('{0}~{1}').format(timePos-24, timePos-23))
			item.setFont(self.vhfont)
			item.setTextAlignment(Qt.AlignCenter)
			self.table.setVerticalHeaderItem(i, item)

	def on_systemTrayIcon_activated(self, reason):
		if reason == QSystemTrayIcon.DoubleClick:
			if self.isHidden():
				try:
					self.typeThread
					self.threadStop()
				except:
					pass
				self.show()

			else:
				self.hide()

	def setupTypeThread(self): 
		self.clipboard.setText('')
		det = self.loadDetec()
		self.ws, vaild = self.checkTableValidation(det, self.table)
		ws = []
		for col in self.ws:
			ws.append(col[0])
		self.addStatus( u'  狀態:  檢查勤務表狀態....', 0)
		errStat = self.check(ws)
		

		if vaild != True and self.table.columnCount()!=0:
			self.addStatus( vaild, 1)
		elif self.table.columnCount() ==0:
			self.addStatus( u'  錯誤:  請載入勤務表', 1)
			
		self.typeThread = startType(self.ws)  
		self.typeThread.threadDone.connect(self.showDoneMsg, Qt.QueuedConnection)
		self.typeThread.toTray.connect(self.toTray, Qt.QueuedConnection)
		self.typeThread.toCilpboard.connect(self.toCilpboard, Qt.QueuedConnection) 
		self.typeThread.enableButtons.connect(self.enableButtons, Qt.QueuedConnection)
		self.typeThread.showErrMsg.connect(self.showErrMsg, Qt.QueuedConnection)  
		self.typeThread.addStatus.connect(self.addStatus, Qt.QueuedConnection) 

		if not self.typeThread.isRunning() and vaild == True and errStat == True:
			self.addStatus( u'  狀態:  檢查通過,開始進行登打作業....', -1)
			self.browseBtn.setEnabled(False)
			self.triggerBtn.setEnabled(False)
			self.stopBtn.setEnabled(True)
			self.typeThread.start()

	def toTray(self, state):
		if state:
			pass
			self.hide()
			self.systemTrayIcon.showMessage(u'輸入中',u'勤務表背景輸入中....\n\n雙擊圖示可暫停程序', msecs=1000000)
		else:
			self.show()			

	def toCilpboard(self, text):
		self.clipboard.setText(text)

	def threadStop(self):
		self.typeThread.stopSign = 1
		if self.typeThread.isRunning():
			self.browseBtn.setEnabled(True)
			self.triggerBtn.setEnabled(True)
			self.stopBtn.setEnabled(False)

	def addStatus(self, text, err):
		subTreeItem =  QTreeWidgetItem(self.status)
		subTreeItem.setText(0, text)
		self.activateWindow()

		if err == 1:
			font = QFont('Serif', 10, QFont.Bold)
			subTreeItem.setFont(0, font)
			subTreeItem.setForeground(0, QBrush(Qt.white))
			subTreeItem.setBackground(0, QBrush(QColor(150,0,0)))
		elif err == 0:
			font = QFont('Serif', 10, QFont.Bold)
			subTreeItem.setFont(0, font)
			subTreeItem.setForeground(0, QBrush(Qt.black))
			subTreeItem.setBackground(0, QBrush(Qt.white))
		else:
			font = QFont('Serif', 10, QFont.Bold)
			subTreeItem.setFont(0, font)
			subTreeItem.setForeground(0, QBrush(Qt.white))
			subTreeItem.setBackground(0, QBrush(QColor(0,150,0)))			
		self.status.scrollToItem(subTreeItem, QAbstractItemView.PositionAtCenter)
		
	def showErrMsg(self, title, msg):
		self.addStatus( u'錯誤:  ' + msg, 1)
		QMessageBox.warning(self, title, msg, QMessageBox.Ok)

	def showDoneMsg(self):
		self.addStatus( u'完成:  完成!', 1)
		QMessageBox.warning(self, u'完成!', u'完成!', QMessageBox.Ok)

	def enableButtons(self):
		self.clipboard.setText(self.comment)
		self.browseBtn.setEnabled(True)
		self.triggerBtn.setEnabled(True)
		self.stopBtn.setEnabled(False)
		self.show()
	
	def OpenFile(self):
		fileName	  = QFileDialog.getOpenFileName(self, "Open File.", "/home")
		self.comment  = ''
		ext = fileName[0].split('.')[-1]
		if ext == 'xls' or ext == 'xlsx':
			self.pathText.setText(fileName[0])
			sheet = open_workbook(fileName[0]).sheets()[0]
			self.setFixedHeight(550)
			self.addStatus(  u'  狀態:  載入勤務表: [' + fileName[0] + ']', -1)
			for col in xrange(self.table.columnCount()-1,-1,-1):
				self.table.removeColumn(col)
			ws, header, headerNum = self.loadWS(sheet)
			self.appendTable(header, ws)

			
			self.check(ws)
			self.comment += self.yieldcomment(sheet)
			self.ws = ws
		else:
			self.showErrMsg(u'錯誤',u'選取檔案不是EXCEL檔')
			self.ws = 0


	def checkTableValidation(self, detCol, table):
		#	.[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9]
		ws = [[], [], [], [], [], [], [], [], [], []]
		errStat = True
		if len(detCol) == 0 or (0 in detCol):
			errStat =  u'  狀態:  勤務表標頭錯誤,點選 [ --請選擇-- ] 選取有效標頭'
		for i in xrange(len(detCol)-1):
			if sorted(detCol)[i] == sorted(detCol)[i+1]:
				errStat = u'  狀態:  勤務表標頭重複'
				
		print detCol
		for c in xrange(table.columnCount()):
			col = []
			colNum = detCol[c]
			for r in xrange(2, 26, 1):
				col.append(table.item(r,c).text())
			ws[colNum-1].append(col)
		for i in xrange(len(ws)):
			if len(ws[i]) == 0:
				ws[i].append(['','','','','','','','','','','','','','','','','','','','','','','',''])
		return (ws), errStat


	def loadWS(self, sheet):
		header, headerNum, ws= [],[],[]
		for c in xrange(3, 26 ,1):
			title  = (sheet.cell_value(3, c))
			if len(title) != 0 and len(header) <6:				
				header.append(title)
				col = []
				for m in xrange(7, 31, 1):
					try:
						col.append(str(sheet.cell_value(m, c)).strip('()').replace('.0', '').replace('.', ','))
					except:
						col.append(u'error')
				ws.append(col)
		return ws, header, headerNum

	def appendTable(self, header, ws):
		try:
			font  = QFont('TypeWriter', pointSize = 10, weight=QFont.Bold)
			for text in header:
				self.table.insertColumn(0)
			for c in xrange(len(header)):
				det = self.determine(header[c])
				item = QTableWidgetItem(header[c])
				if det == 0:
					item.setBackground(QBrush(QColor('#FF8D00')))
				else:
					item.setBackground(QBrush(QColor('#005588')))
				item.setFont(font)
				item.setTextAlignment(Qt.AlignCenter)
				self.table.setItem(0, c, item)	

				nComboBox = self.newCombo()
				nComboBox.setCurrentIndex(det)
				self.table.setCellWidget(1, c, (nComboBox))
				
				for r in xrange(2,26,1):
					item = QTableWidgetItem(ws[c][r-2])
					item.setFont(font)
					item.setTextAlignment(Qt.AlignCenter)
					self.table.setItem(r, c, item)
			self.addStatus(  u'  狀態:  勤務表預覽成功', -1)
			return 0
		except:
			self.addStatus(  u'  狀態:  勤務表預覽失敗', 1)
			return 'error'

	def loadDetec(self):
		det = []
		for col in xrange(self.table.columnCount()):
			det.append( self.table.cellWidget(1, col).currentIndex())
		return det

	def newCombo(self):
		taskList  = [u'--請選擇--', u'值班', u'救護勤務', u'備勤', u'待命服勤', u'水源查察', u'消防查察', u'宣導勤務', u'訓(演)練', u'專案勤務', u'南山救護站']
		nComboBox = QComboBox()
		nComboBox.addItems(taskList)
		for i in xrange(len(taskList)):
			if i == 0:
				nComboBox.setItemData(i, QColor('#550000'), Qt.BackgroundColorRole)
			nComboBox.setItemData(i, Qt.AlignCenter, Qt.TextAlignmentRole)
		nComboBox.setStyleSheet("text-align: right; font: bold 13px;")		
		return nComboBox

	def setTableErr(self, row, state):
		if   state == 'illegal'  : color = '#CC0033'
		elif state == 'repeated' : color = '#FF8D00'
		elif state == 'legal'    : color = '#201F1F'
		for col in xrange(self.table.columnCount()):
			self.table.item(row,col).setBackground(QBrush(QColor(color)))

	def check(self, ws):
		errStat = True
		for m in xrange(2,26):
			self.setTableErr(m, 'legal')
		for i in xrange(24):
			ary = []
			for j in xrange(len(ws)):
				for each in ws[j][i].replace(' ', '').split(','):
					try:
						if	each == "A": ary.append(-1)
						elif  each == '' : pass
						else: each == ary.append(int(each))

					except:
						errStat = False
						timePos = i+8
						rptErrMSG = u"  錯誤:  於 {0} ~ {1} 時   數字: {2}  --不合法,  請修正"
						self.setTableErr(i+2, 'illegal')
						print timePos
						if timePos < 23:
							print 'a'
							self.addStatus(rptErrMSG.format(timePos,   timePos+1,   each), 1)
						else:
							self.addStatus(rptErrMSG.format(timePos-24, timePos-23, each), 1)
									
			ary = sorted(ary)
			for idx in xrange(len(ary)-1):
				if ary[idx] == ary[idx+1]:
					errStat = False
					timePos = i+8
					rptErrMSG = u"  錯誤:  於 {0} ~ {1} 時   數字: {2}  --番號重複, 請修正"
					self.setTableErr(i+2, 'repeated')
					if timePos < 23:
						self.addStatus(rptErrMSG.format(timePos,   timePos+1,   str(ary[idx]).replace('-1', 'A')), 1)
					else:
						self.addStatus(rptErrMSG.format(timePos-24, timePos-23, str(ary[idx]).replace('-1', 'A')), 1)

		return errStat

	def determine(self, title):
		cmpfactor = 0

		smp = [u'值班', u'救護分隊務', u'備', u'待命服', u'水源', u'消防', u'宣導', u'訓演練', u'專案其它', u'南山站']
		for index, each in zip(xrange(len(smp)),smp):
			for text in each:
				for elem in title:
					cmpfactor += ( elem == text )
			if cmpfactor > 0:
				return index+1
		return 0

	def yieldcomment(self, sheet):
		comment0, comment1 = '', ''
		# for i,j in [[24,27], [27,27], [29,27], [29,35]]:
		# 	try:
		# 		comment0 += (smart_str(sheet.cell_value(i, j)) + '\n')
		# 	except:
		# 		pass
		for i,j in [[31,3], [32,3], [33,3], [34,3]]:
			try:
				comment1 += (sheet.cell_value(i, j) + '\n')
			except:
				pass
		return comment1
Ejemplo n.º 37
0
class CustomWidget(QtGui.QMainWindow):
    def __init__(self):
        """
        Constructor
        """
        QtGui.QMainWindow.__init__(self)
        self.name = "Custom widget"
        self.central_widget = QtGui.QWidget()
        self.setCentralWidget(self.central_widget)

        # TODO: This is ugly, improve it
        self.iconp = JConfig().icons_path

        self._createLayout()

    def _createGui(self):
        """
        Subclasses must override this
        depending on the elements they want to add
        self._createOutputTree(),
        self._createOutputTable(),
        self._createOutputWindow() and add them to
        the corresponding layouts.
        """
        raise NotImplementedError

    def _createToolBar(self, name):
        """
        Subclasses need to define the
        specific Actions
        """
        self.toolbar = self.addToolBar(name)
        self.toolbar.setMovable(False)

    def _createLayout(self):
        """
        This creates the basic layout:
        Buttons & Outputs
        """

        # Layouts (This is a common disposition)
        main_layout = QtGui.QVBoxLayout()
        self.button_layout = QtGui.QHBoxLayout()
        output_layout = QtGui.QVBoxLayout()

        # You will need to create your buttons
        # and add them to your layout like this:
        # self.button_layout.addWidget(button_1)

        # Output Layout Inner (QSplitter)
        # Add as many widgets as you please
        # They will be ordered vertically and
        # be resizable by the user
        # self.splitter.addWidget(self.table_label)
        # self.splitter.addWidget(...)
        self.splitter = QSplitter(QtCore.Qt.Vertical)

        # Nested layouts
        main_layout.addLayout(self.button_layout)
        output_layout.addWidget(self.splitter)
        main_layout.addLayout(output_layout)
        self.central_widget.setLayout(main_layout)

    def _createOutputWindow(self):
        """
        Some binary analysis commands will output to this.
        """

        self.output_label = QtGui.QLabel('Output')

        self.output_window = QTextEdit()
        self.output_window.setFontPointSize(10)
        self.output_window.setReadOnly(True)
        # Save it for later use
        self.output_window.original_textcolor = self.output_window.textColor()

    def _createOutputTable(self):
        """
        A vanilla QTableWidget. Callbacks modify
        its properties, like number of columns, etc.
        """
        self.table_label = QtGui.QLabel('Table Output')

        self.table = QTableWidget()
        self.table.setColumnCount(3)
        self.table.setColumnWidth(0, 100)
        self.table.setColumnWidth(1, 300)
        self.table.setColumnWidth(2, 300)

        self.table.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)

        # Connect signals to slots
        self.table.customContextMenuRequested.connect(self._tablePopup)
        self.table.horizontalHeader().sectionDoubleClicked.connect(
            self._tableHeaderDoubleClicked)
        self.table.cellDoubleClicked.connect(self._tableCellDoubleClicked)

    def _createOutputTree(self):
        """
        A QtreeWidget. Initially used to display those pesky
        dword comparison to immediate values.
        """
        self.tree_label = QtGui.QLabel('Tree Output')

        self.tree = QTreeWidget()
        self.tree.setColumnCount(3)
        self.tree.setColumnWidth(0, 150)
        self.tree.setColumnWidth(1, 150)
        self.tree.setColumnWidth(2, 50)

        # Connect signals to slots
        self.tree.itemDoubleClicked.connect(self._treeElementDoubleClicked)

    #################################################################
    # GUI Callbacks
    #################################################################
    def _console_output(self, s="", err=False):
        """
        Convenience wrapper
        """
        if err:
            # Error message
            err_color = QColor('red')
            self.output_window.setTextColor(err_color)
            self.output_window.append(s)
            # restore original color
            self.output_window.setTextColor(
                self.output_window.original_textcolor)

        else:
            self.output_window.append(s)

    def _tableCellDoubleClicked(self, row, col):
        """
        Most of the info displayed in QTableWidgets represent addresses.
        Default action: try to jump to it.
        """
        it = self.table.item(row, col).text()

        try:
            addr = int(it, 16)
            jump_to_address(addr)

        except ValueError:
            self._console_output("[!] That does not look like an address...",
                                 err=True)
            return

    def _tablePopup(self, pos):
        """
        Popup menu activated clicking the secondary
        button on the table
        """
        menu = QtGui.QMenu()

        # Add menu entries
        delRow = menu.addAction(QIcon(self.iconp + "close.png"), "Delete Row")
        selItem = menu.addAction(QIcon(self.iconp + "bookmark.png"),
                                 "Mark entry")
        menu.addSeparator()
        origFunc = menu.addAction(QIcon(self.iconp + "lightning.png"),
                                  "Select origin function")
        destFunc = menu.addAction(QIcon(self.iconp + "flag.png"),
                                  "Select destination function")

        # Get entry clicked
        action = menu.exec_(self.mapToGlobal(pos))

        # Dispatch :)
        if action == delRow:
            self.table.removeRow(self.table.currentRow())

        elif action == selItem:
            self.table.currentItem().setBackground(QtGui.QColor('red'))

        elif action == origFunc:
            try:
                addr = self.table.currentItem().text()
                InfoUI.function_orig_ea = int(addr, 16)

            except:
                self._console_output("This does not look like an address...",
                                     err=True)

        elif action == destFunc:
            try:
                addr = self.table.currentItem().text()
                InfoUI.function_dest_ea = int(addr, 16)

            except:
                self._console_output("This does not look like an address...",
                                     err=True)

    def _tableHeaderDoubleClicked(self, index):
        """
        Used to sort the contents
        """
        self.table.sortItems(index, order=QtCore.Qt.AscendingOrder)

    def _treeElementDoubleClicked(self, item, column):
        """
        QTreeWidgetElement callback. Basically it jumps to
        the selected address in IDA disassembly window.
        """
        try:
            # Only interested in addresses
            addr_int = int(item.text(column), 16)
            jump_to_address(addr_int)
            # Paint some color
            item.setBackground(column, QtGui.QColor('green'))

        except:
            self._console_output("[!] That does not look like an address...",
                                 err=True)
Ejemplo n.º 38
0
class PushupList(QWidget):
    '''
    classdocs
    '''

    deletePushup = Signal(int)
    deletePushups_in_a_day = Signal(tuple)

    def __init__(self, pushups):
        '''
        Constructor
        '''
        QWidget.__init__(self)

        self.pushups = pushups
        self.createGUI()

    def createGUI(self):
        self.layout = QVBoxLayout()
        # self.pushupsListWidget = QListWidget(self)
        self.pushupsListWidget = QTreeWidget(self)

        self.pushupsListWidget.setMinimumHeight(250)
        #self.pushupsListWidget.setMaximumWidth(500)
        self.pushupsListWidget.setAlternatingRowColors(True)

        self.pushupsListWidget.doubleClicked.connect(self.doubleClick_Test)

        self.pushupsListWidget.setContextMenuPolicy(Qt.CustomContextMenu)
        self.pushupsListWidget.customContextMenuRequested.connect(
            self._customMenu)

        self._populateTree()

        self.layout.addWidget(self.pushupsListWidget)

        self.setLayout(self.layout)

    # Slot
    def _customMenu(self):
        selectedItems = self.pushupsListWidget.selectedItems()

        if selectedItems is not None:
            selectedItem = selectedItems[0]

            if selectedItem.parent() is not None:  # Child Item selected
                menu = QMenu()

                delete = QAction(self.pushupsListWidget)
                delete.setText("Delete this pushup")
                delete.triggered.connect(self._emitDeleteSignal)
                menu.addAction(delete)
                menu.exec_(QCursor.pos())
            else:  # Top level Item selected
                menu = QMenu()

                delete = QAction(self.pushupsListWidget)
                delete.setText("Delete this day and all of its exercises")
                delete.triggered.connect(self._emitDeleteDaySignal)
                menu.addAction(delete)
                menu.exec_(QCursor.pos())

    def _emitDeleteSignal(self):
        selectedItem = self.pushupsListWidget.selectedItems()[0]
        pushupId = selectedItem.data(0, Qt.UserRole)._id

        self.deletePushup.emit(pushupId)

    def _emitDeleteDaySignal(self):
        selectedItem = self.pushupsListWidget.selectedItems()[0]

        treeWidgetItems = selectedItem.takeChildren()

        pushupsIdList = []
        for item in treeWidgetItems:
            pushup = item.data(0, Qt.UserRole)._id
            pushupsIdList.append(pushup)

        self.deletePushups_in_a_day.emit(pushupsIdList)

    def _populateTree(self):
        self.pushupsListWidget.clear()
        self.pushupsListWidget.setColumnCount(4)
        self.pushupsListWidget.setHeaderLabels([
            "Date", "TotalPushups", "Series", "Repetitions",
            "Average Heart Rate"
        ])
        self.pushupsListWidget.setSortingEnabled(True)
        self.pushupsListWidget.setColumnWidth(0, 180)
        self.pushupsListWidget.setColumnWidth(4, 150)

        pushupDict = self._getPushupDictionary()

        for it, dayOfExercise in enumerate(sorted(pushupDict.keys())):

            dateItem = QTreeWidgetItem()

            dayLabel = dayOfExercise.strftime("%Y/%m/%d")

            dateItem.setText(0, "\n" + dayLabel + "\nDay : " + str(it))

            self.pushupsListWidget.addTopLevelItem(dateItem)

            totalPushups = 0
            for pushup in pushupDict[dayOfExercise]:
                pushupItem = QTreeWidgetItem()

                pushupItem.setText(2, "#" + str(pushup._series))
                pushupItem.setText(3, str(pushup._repetitions))
                pushupItem.setText(4, str(pushup._averageHeartRate))
                pushupItem.setData(0, Qt.UserRole, pushup)

                totalPushups = totalPushups + pushup._repetitions

                dateItem.addChild(pushupItem)

            dateItem.setText(1, str(totalPushups))

    def doubleClick_Test(self):
        selectedItems = self.pushupsListWidget.selectedItems()

        if selectedItems is not None:
            selectedItem = selectedItems[0]

            if selectedItem.parent() is not None:  # Child Item selected
                selectedPushups = self.pushupsListWidget.selectedItems(
                )[0].data(0, Qt.UserRole)
                print selectedPushups._id
            else:
                print "Top level widget double clicked"

    def reloadPushupsList(self, pushups):
        self.pushups = pushups
        self._populateTree()

    def _populateListWidget(self):
        ''' 
        unused old method
        '''

        self.pushupsListWidget.clear()

        pushupDict = self._getPushupDictionary()

        for dayOfExercise in pushupDict:
            listItemString = "Date : " + dayOfExercise + "\n"
            listItem_Data = []

            for pushup in pushupDict[dayOfExercise]:
                listItemString += "Series : " + str(pushup._series) + \
                                  " Repetition : " + str(pushup._repetitions) + "\n"
                listItem_Data.append(pushup)

            listItem = QListWidgetItem(listItemString)
            listItem.setData(Qt.UserRole, listItem_Data)

            self.pushupsListWidget.addItem(listItem)

    def _getPushupDictionary(self):
        '''
        Returns a dictionary with the following structure : 
        - Key : date of the exercises. Type datetime.date
        - Value : list containing pushups made that day . Type : [Pushup model object]    
        
        example : 
        {
            2014-08-18: [pushupModelObj1, pushupModelObj2, pushupModelObj3],
            2014-08-19: [pushupModelObj4, pushupModelObj5, pushupModelObj6]
        } 
        '''
        pushupDateList = {}  # dictionary initialization

        for pushup in self.pushups:
            if not pushupDateList.has_key(pushup._date):
                pushupsList = [pushup]
                pushupDateList[pushup._date] = pushupsList
            else:
                pushupDateList[pushup._date].append(pushup)

#         for k in pushupDateList.keys():
#             print k
#
#             for pu in pushupDateList[k]:
#                 print pu

        return pushupDateList
Ejemplo n.º 39
0
class TreeView():
    def __init__(self, targetLayoutContainer):
        self.tree = QTreeWidget()
        self.tree.addAction(QAction('Add nested favorite', self.tree, triggered=self.add_child))
        self.tree.addAction(QAction('Edit Title', self.tree, triggered=self.edit_title))
        self.tree.addAction(QAction('HTML page', self.tree, triggered=self.create_html_page))
        self.tree.addAction(QAction('Delete', self.tree, triggered=self.delete))
        self.tree.setContextMenuPolicy(Qt.ActionsContextMenu)
        targetLayoutContainer.addWidget(self.tree)
        
    def add_child(self):
        item = self.tree.currentItem()
        print item.data(1, Qt.ItemDataRole.EditRole).getFullPath()
        newItem = add_child(item.data(1, Qt.ItemDataRole.EditRole))
        
        newChildNode = QTreeWidgetItem()
        newChildNode.setText(0, newItem.label)
        newChildNode.setData(1, Qt.ItemDataRole.EditRole, newItem)
        item.addChild(newChildNode)
        
    def get_item(self, node):
        return node.data(1, Qt.ItemDataRole.EditRole)
        
    def set_favorites(self, favoritesRoot):
        self.tree.clear()
        
        item = QTreeWidgetItem()
        item.setText(0, favoritesRoot.label)
        item.setData(1,Qt.ItemDataRole.EditRole, favoritesRoot)
        
        self.tree.addTopLevelItem(item)
        self.add_children(favoritesRoot, item)
    
    def edit_title(self):
        newTitle, result = QInputDialog.getText(None, 'new title', 'enter a new title')
        
        if not result:
            return
            
        item = self.get_item(self.tree.currentItem())
        item.label = newTitle
        save(item, newTitle)
        self.tree.currentItem().setText(0, item.label)
    
    def create_html_page(self):
        item = self.get_item(self.tree.currentItem())
        
        page = None
        
        if item.page == None:
            page = item.add_html_page()
            save(item, page=page)
        else:
            page = item.add_html_page()
        
        self.show_page(page)
    
    def add_children(self, node, item):
        for child in node.children:
            childItem = QTreeWidgetItem()
            childItem.setText(0, child.label)
            childItem.setData(1,Qt.ItemDataRole.EditRole, child)
            item.addChild(childItem)
            
            self.add_children(child, childItem)
            
    def delete(self):
        if(self.get_item(self.tree.currentItem()).parent == None):
            QMessageBox.warning(None, 'cannot delete the root node', 'cannot delete the root node')
            return
        
        delete(self.get_item(self.tree.currentItem()))
        self.tree.currentItem().parent().removeChild(self.tree.currentItem())
        
    def show_page(self, page=None):
        if page == None:
            controller.show_page(self.get_item(self.tree.currentItem()).page)
        else:
            controller.show_page(page)
Ejemplo n.º 40
0
class MassAttribute_UI(QDialog):
    """
    The main UI
    """
    class Applikator(QObject):
        """
        This is the core applier which toggle the display of the corresponding widget and handling events' connections
        """
        def __init__(self, parent=None):
            super(MassAttribute_UI.Applikator, self).__init__()
            self.root = parent

        def widget_event(self, t):
            """
            Return the correct widget's event depending on attribute's type
            :param t: the attribute's type
            :type  t: str
            :return: the event
            :rtype : Signal
            """
            return {'float': self.root.W_EDI_float.valueChanged, 'enum': self.root.W_EDI_enum.currentIndexChanged,
                    'int': self.root.W_EDI_int.valueChanged, 'bool': self.root.W_EDI_bool.stateChanged,
                    'str': self.root.W_EDI_str.textChanged, 'd3': self.root.W_EDI_d3.valuesChanged,
                    'd4': self.root.W_EDI_d4.valuesChanged, 'color': self.root.W_EDI_color.colorChanged}[t]

        def unset_editors(self):
            """
            Toggle off all editors and disconnect the current one
            """
            for widget in (self.root.W_EDI_float, self.root.W_EDI_int, self.root.W_EDI_enum,
                           self.root.W_EDI_bool, self.root.W_EDI_str, self.root.W_EDI_d3,
                           self.root.W_EDI_d4, self.root.W_EDI_color):
                widget.setVisible(False)

            # trying to force disconnection
            try:
                self.widget_event(self.root.ctx).disconnect(self.root.apply_value)
            except (KeyError, RuntimeError):
                pass

        def prepare(applier_name):
            """
            A decorator to prepare the attribute depending on type for the corresponding widget and getting the
            attribute's value
            :param applier_name: attribute's type
            :type  applier_name: str
            """
            def sub_wrapper(func):
                def wrapper(self, attr_path):
                    self.unset_editors()
                    self.root.ctx = applier_name
                    self.root.__getattribute__('W_EDI_%s' % applier_name).setVisible(True)
                    ret = func(self, cmds.getAttr(attr_path), attr_path)
                    return ret
                return wrapper
            return sub_wrapper

        @staticmethod
        def get_bounds(obj, attr, min_default, max_default):
            """
            Try to retrieve the range for the given attribute, if min or max fail it'll set default values
            :param         obj: the object's name
            :type          obj: str
            :param        attr: attribute's name
            :type         attr: str
            :param min_default: minimum default value
            :param max_default: max default value
            :type  min_default: float | int
            :type  max_default: float | int
            :return: minimum, maximum
            :rtype : tuple
            """
            try:
                assert cmds.attributeQuery(attr, n=obj, mxe=True)
                maxi = cmds.attributeQuery(attr, n=obj, max=True)[0]
            except (RuntimeError, AssertionError):
                maxi = max_default
            try:
                assert cmds.attributeQuery(attr, n=obj, mne=True)
                mini = cmds.attributeQuery(attr, n=obj, min=True)[0]
            except (RuntimeError, AssertionError):
                mini = min_default
            return mini, maxi

        @prepare('float')
        def apply_float(self, value, path):
            """
            Float attribute case
            :param value: attribute's value
            :param  path: attribute's path = obj.attr
            """
            obj, attr = path.split('.', 1)
            self.root.W_EDI_float.setRange(*self.get_bounds(obj, attr, -100.0, 100.0))
            self.root.W_EDI_float.setValue(value)

        @prepare('enum')
        def apply_enum(self, value, path):
            """Enum case"""
            self.root.W_EDI_enum.clear()
            obj, attr = path.split('.', 1)
            try:
                enums = [enum.split('=')[0] for enum in cmds.attributeQuery(attr, n=obj, listEnum=True)[0].split(':')]
            except RuntimeError:
                self.apply_int(path)
            else:
                self.root.W_EDI_enum.addItems(enums)
                self.root.W_EDI_enum.setCurrentIndex(enums.index(cmds.getAttr(path, asString=True)))

        @prepare('int')
        def apply_int(self, value, path):
            """Integer case"""
            obj, attr = path.split('.', 1)
            self.root.W_EDI_int.setRange(*self.get_bounds(obj, attr, -1000, 1000))
            self.root.W_EDI_int.setValue(value)

        @prepare('bool')
        def apply_bool(self, value, path):
            """Boolean case"""
            self.root.W_EDI_bool.setChecked(value)
            self.root.W_EDI_bool.setText(path.split('.', 1)[1])

        @prepare('str')
        def apply_str(self, value, path):
            """String case"""
            self.root.W_EDI_str.setText(value)

        @prepare('d3')
        def apply_d3(self, value, path):
            """3D array case"""
            self.root.W_EDI_d3.setValues(value[0])

        @prepare('d4')
        def apply_d4(self, value, path):
            """4D array case"""
            self.root.W_EDI_d4.setValues(value[0])

        @prepare('color')
        def apply_color(self, value, path):
            """Color case"""
            try:
                colors = value[0]
                self.root.W_EDI_color.setColor([int(c * 255) for c in colors])
            except TypeError:
                self.apply_int(value, path)

    class Attribute(str):
        """
        A custom string attribute class to ship more information into the string variable
        """
        def __new__(cls, path='', super_type=Object):
            obj, attr = path.split('.', 1)

            str_obj = str.__new__(cls, attr)

            str_obj.obj, str_obj.attr = obj, attr
            str_obj.path = path
            str_obj.super_type = super_type
            str_obj.type = None

            return str_obj

    # static variables to pre-load icons and attributes short names
    ctx_icons = {'float': QIcon(':render_decomposeMatrix.png'),
                 'enum': QIcon(':showLineNumbers.png'),
                 'bool': QIcon(':out_decomposeMatrix.png'),
                 'time': QIcon(':time.svg'),
                 'byte': QIcon(':out_defaultTextureList.png'),
                 'angle': QIcon(':angleDim.png'),
                 'string': QIcon(':text.png'),
                 'float3': QIcon(':animCurveTA.svg'),
                 'float4': QIcon(':animCurveTA.svg'),
                 'color': QIcon(':clampColors.svg')}

    for ctx in ('doubleLinear', 'double', 'long', 'short'):
        ctx_icons[ctx] = ctx_icons['float']

    ctx_icons['double3'] = ctx_icons['float3']
    ctx_icons['double4'] = ctx_icons['float4']

    ctx_wide = {'float': ('float', 'doubleLinear', 'double', 'long', 'short'),
                'enum': ('enum',),
                'bool': ('bool',),
                'time': ('time',),
                'byte': ('byte',),
                'angle': ('doubleAngle',),
                'string': ('string',),
                'float3': ('double3', 'float3'),
                'float4': ('double4', 'float4'),
                'color': ('color',)}

    def __init__(self, parent=None):
        super(MassAttribute_UI, self).__init__(parent)
        # Abstract
        self.applier = self.Applikator(self)
        self.selection = []
        self.callback = None
        self.ctx = None
        # storing found attributes' types to avoid double check
        self.solved = {}
        self.setLocale(QLocale.C)
        self.setAttribute(Qt.WA_DeleteOnClose)
        self.setAttribute(Qt.WA_QuitOnClose)

        self.setFixedWidth(300)
        self.setWindowTitle('Massive Attribute Modifier')

        # UI
        L_main = QVBoxLayout()

        self.WV_title = QLabel('')
        self.WV_title.setVisible(False)
        self.WV_title.setFont(QFont('Verdana', 10))
        self.WV_title.setContentsMargins(0, 0, 0, 7)

        self.WB_select = QPushButton('Select')
        self.WB_select.setVisible(False)
        self.WB_select.setFixedWidth(50)
        self.WB_select.clicked.connect(lambda: cmds.select(self.selection))

        self.WB_update = QPushButton('Update')
        self.WB_update.setFixedWidth(50)
        self.WB_update.clicked.connect(lambda:self.update_attributes(cmds.ls(sl=True)))

        self.WV_search = Filter()
        self.WV_search.textChanged.connect(self.filter)

        self.WC_cases = QCheckBox('Case sensitive')
        self.WC_cases.stateChanged.connect(self.filter)

        self.WC_types = QCheckBox('Type filtering')

        self.WL_attrtype = QComboBox()
        self.WL_attrtype.setEnabled(False)

        for i, ctx in enumerate(sorted(self.ctx_wide)):
            self.WL_attrtype.addItem(ctx.title())
            self.WL_attrtype.setItemIcon(i, self.ctx_icons[ctx])

        L_attrtype = line(self.WC_types, self.WL_attrtype)

        self.WC_types.stateChanged.connect(partial(self.update_attributes, self.selection))
        self.WC_types.stateChanged.connect(self.WL_attrtype.setEnabled)
        self.WL_attrtype.currentIndexChanged.connect(self.filter)

        self.WC_liveu = QCheckBox('Live')
        self.WC_liveu.stateChanged.connect(self.WB_update.setDisabled)
        self.WC_liveu.stateChanged.connect(self.set_callback)

        self.WC_histo = QCheckBox('Load history')
        self.WC_histo.setChecked(True)
        self.WC_histo.stateChanged.connect(partial(self.update_attributes, self.selection))

        self.WC_child = QCheckBox('Children')
        self.WC_child.stateChanged.connect(partial(self.update_attributes, self.selection))

        options = group('Options', line(self.WC_cases, L_attrtype),
                        line(self.WC_child, self.WC_histo, self.WC_liveu, self.WB_update))
        options.layout().setSpacing(2)

        self.WL_attributes = QTreeWidget()
        self.WL_attributes.setStyleSheet('QTreeView {alternate-background-color: #1b1b1b;}')
        self.WL_attributes.setAlternatingRowColors(True)
        self.WL_attributes.setHeaderHidden(True)
        self.WL_attributes.setRootIsDecorated(False)

        self.objs_attr = set()
        self.shps_attr = set()

        self.W_EDI_float = FloatBox()
        self.W_EDI_int = IntBox()
        self.W_EDI_enum = QComboBox()
        self.W_EDI_bool = QCheckBox()
        self.W_EDI_str = QLineEdit()
        self.W_EDI_d3 = Double3()
        self.W_EDI_d4 = Double4()
        self.W_EDI_color = ColorPicker()

        # Final layout
        L_title = line(self.WV_title, self.WB_select)
        L_title.setStretch(0, 1)
        L_main.addLayout(L_title)
        L_main.setAlignment(Qt.AlignLeft)
        L_main.addWidget(self.WV_search)
        L_main.addWidget(options)
        L_main.addWidget(self.WL_attributes)
        L_edits = col(self.W_EDI_bool, self.W_EDI_int, self.W_EDI_float,
                      self.W_EDI_enum, self.W_EDI_str, self.W_EDI_d3, self.W_EDI_d4,
                      self.W_EDI_color)
        L_edits.setContentsMargins(0, 8, 0, 0)
        L_main.addLayout(L_edits)
        L_main.setStretch(3, 1)
        L_main.setSpacing(2)

        self.appliers = {'float': self.applier.apply_float,
                         'enum': self.applier.apply_enum,
                         'bool': self.applier.apply_bool,
                         'time': self.applier.apply_float,
                         'byte': self.applier.apply_int,
                         'angle': self.applier.apply_float,
                         'string': self.applier.apply_str,
                         'float3': self.applier.apply_d3,
                         'float4': self.applier.apply_d4,
                         'color': self.applier.apply_color}

        self.setLayout(L_main)

        # final settings
        self.WL_attributes.itemSelectionChanged.connect(self.update_setter)
        self.applier.unset_editors()

    def closeEvent(self, *args, **kwargs):
        self.set_callback(False)

    def set_callback(self, state):
        """
        Toggle selection event callback
        :param state: checkbox's state
        :type  state: bool | int
        """
        if state and not self.callback:
            self.callback = MEventMessage.addEventCallback('SelectionChanged', self.update_attributes)
            self.update_attributes(cmds.ls(sl=True))

        elif not state and self.callback:
            MMessage.removeCallback(self.callback)
            self.callback = None

    @staticmethod
    def format_title(nodes):
        """
        Extract the matching characters from a given nodes selection, if begin matches it will return "joint*" with a
        wildcard when names don't match
        :param nodes: objects' list
        :type  nodes: list | tuple
        :return: the formatted name with the corresponding characters
        :rtype : str
        """
        res = None

        if nodes:
            # we get the first node as a reference
            node = nodes[0]
            # and compare with the other nodes
            subs = [w for w in nodes if w != node]

            l = 1
            valid = True
            # will continue until l (length) match the full name's length or until names don't match
            while l < len(node) and valid:
                for sub in subs:
                    if not sub.startswith(node[:l]):
                        valid = False
                        break

                else:
                    l += 1

            # if matching characters isn't long enough we only display the number of nodes selected
            if l <= 3:
                res = '%i objects' % len(nodes)

            # otherwise showing matching pattern
            elif l < len(node) or len(nodes) > 1:
                res = node[:l - 1] + '* (%i objects)' % len(nodes)

            else:
                res = node

        return res

    @staticmethod
    def get_history(node):
        """
        Extract history for the given node
        :rtype: list
        """
        return cmds.listHistory(node, il=2, pdo=True) or []

    @staticmethod
    def get_shapes(node):
        """
        Extract shape(s) for the given node
        :rtype: list
        """
        return cmds.listRelatives(node, s=True, ni=True, f=True)

    def get_attributes_type(self, attrs):
        """
        For a given list of attributes of type Attribute, will loop through and fill the type parameter of the
         attribute with the corresponding type, if type is invalid or not handled, it'll remove it
        :param attrs: attributes' list
        :type  attrs: [MassAttribute_UI.Attribute]
        :return: cleaned and filled attributes' list
        :rtype: [MassAttribute_UI.Attribute]
        """
        attrs = list(attrs)
        # first we sort the attributes' list
        attrs.sort()

        # then we try to extract the attribute's type
        for i, attr in enumerate(attrs):
            try:
                if attr.attr in self.solved:
                    attr.type = self.solved[attr.attr]
                    raise RuntimeError
                tpe = cmds.getAttr(attr.path, typ=True)
                assert tpe
                attr.type = tpe
                self.solved[attr.attr] = tpe
            except (AssertionError, ValueError, RuntimeError):
                pass

        # defining a to-remove list
        rm_list = set()

        layers = {'3': 'XYZ', '4': 'XYZW'}
        for i, attr in enumerate(attrs):
            if i in rm_list:
                continue

            # we handle some special cases here, if ever the attribute list contains RGB and separate R, G and B we
            # assume it's a color, if it's a double3 or float3 and we find the corresponding XYZ, we remove then to
            # avoid duplicates

            if attr.endswith('RGB'):
                if '%sR' % attr[:-3] in attrs:
                    attr.type = 'color'
                    for chan in 'RGB':
                        rm_list.add(attrs.index('%s%s' % (attr[:-3], chan)))

            # if the attribute's type isn't in the list, we remove
            elif attr.type not in MassAttribute_UI.ctx_icons:
                rm_list.add(i)

            elif attr.endswith('R'):
                if '%sG' % attr[:-1] in attrs and attr[:-1] in attrs:
                    attr.type = 'color'
                    for chan in 'RGB':
                        rm_list.add(attrs.index('%s%s' % (attr[:-1], chan)))

            elif attr.type in ('double3', 'double4', 'float3', 'float4'):
                if '%sX' % attr in attrs:
                    for chan in layers[attr.type[-1]]:
                        rm_list.add(attrs.index('%s%s' % (attr, chan)))

        # finally cleaning the list
        for i in sorted(rm_list, reverse=True):
            attrs.pop(i)

        return attrs

    def apply_value(self, value):
        """
        When the value is modified in the UI, we forward the given value and applies to the object's
        :param value: attribute's value, mixed type
        :type  value: mixed
        """
        # We get the only selected object in list and get it's super type (Shape, History or Object) and
        # type (float, int, string)
        item = self.WL_attributes.selectedItems()[0]
        attr = item.attribute
        shape = attr.super_type == Shape
        histo = attr.super_type == History
        tpe = item.attribute.type

        # eq dict for each context
        value = {'bool': bool,
                 'int': int,
                 'float': float,
                 'enum': int,
                 'str': str,
                 'd3': list,
                 'd4': list,
                 'color': list}[self.ctx](value)

        # converting the selection into a set
        cmds.undoInfo(openChunk=True)
        targets = set(self.selection)

        # we propagate to children if 'Children' checkbox is on
        if self.WC_child.isChecked():
            for obj in list(targets):
                targets |= set(cmds.listRelatives(obj, ad=True))

        # if the target attribute is on the history, we add all selection's history to the list
        if histo:
            for obj in list(targets):
                targets.remove(obj)
                targets |= set(self.get_history(obj))

        # then we loop through target objects
        for obj in targets:
            # if the target is on the shape we get object's shape
            if shape and not histo:
                shapes = self.get_shapes(obj)

                if obj in shapes:
                    continue
                else:
                    obj = shapes[0]

            # then we try to apply depending on attribute's type
            try:
                correct_path = attr.path.replace(attr.obj, obj)

                if tpe == 'string':
                    cmds.setAttr(correct_path, value, type='string')

                elif tpe in ('double3', 'double4', 'float3', 'float4', 'color'):
                    cmds.setAttr(correct_path, *value, type='double%d' % len(value))

                else:
                    cmds.setAttr(correct_path, value)

            except RuntimeError:
                pass

        cmds.undoInfo(closeChunk=True)

    def update_setter(self):
        """
        When the list's selection changes we update the applier widget
        """
        item = self.WL_attributes.selectedItems()
        # abort if no item is selected
        if not len(item):
            return

        # getting attribute's parameter
        attr = item[0].attribute

        if len(self.selection):
            try:
                # looping until we find a context having the current attribute's type
                for applier in self.ctx_wide:
                    if attr.type in self.ctx_wide[applier]:
                        break
                # then we apply for the given path (obj.attribute)
                self.appliers[applier](attr.path)

                # and connecting event to the self.apply_value function
                self.applier.widget_event(self.ctx).connect(self.apply_value)

            # otherwise selection or type is invalid
            except IndexError:
                self.ctx = None

    def update_attributes(self, selection=None, *args):
        """
        Update the attributes for the given selection, looping through objects' attributes, finding attr in common
        between all objects then cleaning the lists, doing the same for shapes and / or histories
        :param selection: object's selection
        """
        # redefining lists as set to intersect union etc
        self.objs_attr = set()
        self.shps_attr = set()

        # pre init
        self.WL_attributes.clear()
        self.applier.unset_editors()

        self.selection = selection or (cmds.ls(sl=True) if self.WC_liveu.isChecked() else self.selection)

        self.WV_title.setText(self.format_title(self.selection))
        self.WV_title.setVisible(bool(len(self.selection)))
        self.WB_select.setVisible(bool(len(self.selection)))

        if not len(self.selection):
            return

        def get_usable_attrs(obj, super_type):
            """
            Small internal function to get a compatible attributes' list for the given object and assign the given
            super_type to it (Object, Shape or History)
            :param        obj: object's name
            :type         obj: str
            :param super_type: attribute's main type
            :type  super_type: Object | Shape | History
            :return:
            """
            return set([MassAttribute_UI.Attribute('%s.%s' % (obj, attr), super_type) for attr in
                        cmds.listAttr(obj, se=True, ro=False, m=True, w=True)])

        if len(self.selection):
            self.objs_attr = get_usable_attrs(self.selection[0], Object)

            # if we also want the object's history we add it to the initial set
            if self.WC_histo.isChecked():
                for histo in self.get_history(self.selection[0]):
                    self.objs_attr |= get_usable_attrs(histo, History)

            # filling the shape's set
            for shape in (self.get_shapes(self.selection[0]) or []):
                self.shps_attr |= get_usable_attrs(shape, Shape)

            # if selection's length bigger than one we compare by intersection with the other sets
            if len(self.selection) > 1:
                for obj in self.selection:
                    sub_attr = get_usable_attrs(obj, Object)

                    if self.WC_histo.isChecked():
                        for histo in self.get_history(obj):
                            sub_attr |= get_usable_attrs(histo, History)

                    self.objs_attr.intersection_update(sub_attr)

                    for shape in (self.get_shapes(self.selection[0]) or []):
                        self.shps_attr.intersection_update(get_usable_attrs(shape, Shape))

            # finally getting all intersecting attributes' types
            self.objs_attr = self.get_attributes_type(self.objs_attr)
            self.shps_attr = self.get_attributes_type(self.shps_attr)

        # and filtering the list
        self.filter()

    def add_set(self, iterable, title=None):
        """
        Adding the given iterable to the list with a first Separator object with given title
        :param iterable: list of item's attributes
        :param    title: Separator's name
        """
        if len(iterable):
            # if title is given we first add a Separator item to indicate coming list title
            if title:
                self.WL_attributes.addTopLevelItem(QTreeWidget_Separator(title))

            items = []
            for attr in sorted(iterable):
                item = QTreeWidgetItem([attr])
                # assigning the attribute itself inside a custom parameter
                item.attribute = attr
                items.append(item)

            # finally adding all the items to the list
            self.WL_attributes.addTopLevelItems(items)

    def filter(self):
        """
        Filter the list with UI's parameters, such as name or type filtering, etc
        """
        # pre cleaning
        self.WL_attributes.clear()

        # using regex compile to avoid re execution over many attributes
        mask = self.WV_search.text()
        case = 0 if self.WC_cases.isChecked() else re.IGNORECASE
        re_start = re.compile(r'^%s.*?' % mask, case)
        re_cont = re.compile(r'.*?%s.*?' % mask, case)

        # getting the four different lists
        obj_start = set([at for at in self.objs_attr if re_start.search(at)])
        shp_start = set([at for at in self.shps_attr if re_start.search(at)])

        # if type filtering is one we only extract the wanted attribute's type
        if self.WC_types.isChecked():
            obj_start = set([at for at in obj_start if
                             at.type in self.ctx_wide[self.WL_attrtype.currentText().lower()]])
            shp_start = set([at for at in shp_start if
                             at.type in self.ctx_wide[self.WL_attrtype.currentText().lower()]])

        # finally adding the current sets if there is a mask we add the also the containing matches
        if mask:
            # getting contains filtering and type containers filtering
            obj_contains = obj_start.symmetric_difference(set([at for at in self.objs_attr if re_cont.search(at)]))
            shp_contains = shp_start.symmetric_difference(set([at for at in self.shps_attr if re_cont.search(at)]))
            if self.WC_types.isChecked():
                obj_contains = set([at for at in obj_contains if
                                    at.type in self.ctx_wide[self.WL_attrtype.currentText().lower()]])
                shp_contains = set([at for at in shp_contains if
                                    at.type in self.ctx_wide[self.WL_attrtype.currentText().lower()]])

            # adding the sets
            self.add_set(obj_start, 'Obj attributes starting with')
            self.add_set(obj_contains, 'Obj attributes containing')
            self.add_set(shp_start, 'Shape attributes starting with')
            self.add_set(shp_contains, 'Shape attributes containing')

        else:
            self.add_set(obj_start, 'Object\'s attributes')
            self.add_set(shp_start, 'Shape\'s attributes')

        # and we select the first one if ever there is something in the list
        if self.WL_attributes.topLevelItemCount():
            self.WL_attributes.setItemSelected(self.WL_attributes.topLevelItem(1), True)
Ejemplo n.º 41
0
class CustomWidget(QtGui.QMainWindow):

    def __init__(self):
        """
        Constructor
        """
        QtGui.QMainWindow.__init__(self)
        self.name = "Custom widget"
        self.central_widget = QtGui.QWidget()
        self.setCentralWidget(self.central_widget)

        # TODO: This is ugly, improve it
        self.iconp = JConfig().icons_path

        self._createLayout()


    def _createGui(self):
        """
        Subclasses must override this
        depending on the elements they want to add
        self._createOutputTree(),
        self._createOutputTable(),
        self._createOutputWindow() and add them to
        the corresponding layouts.
        """
        raise NotImplementedError


    def _createToolBar(self, name):
        """
        Subclasses need to define the
        specific Actions
        """
        self.toolbar = self.addToolBar(name)
        self.toolbar.setMovable(False)


    def _createLayout(self):
        """
        This creates the basic layout:
        Buttons & Outputs
        """

        # Layouts (This is a common disposition)
        main_layout = QtGui.QVBoxLayout()
        self.button_layout = QtGui.QHBoxLayout()
        output_layout = QtGui.QVBoxLayout()

        # You will need to create your buttons
        # and add them to your layout like this:
        # self.button_layout.addWidget(button_1)

        # Output Layout Inner (QSplitter)
        # Add as many widgets as you please
        # They will be ordered vertically and
        # be resizable by the user
        # self.splitter.addWidget(self.table_label)
        # self.splitter.addWidget(...)
        self.splitter = QSplitter(QtCore.Qt.Vertical)

        # Nested layouts
        main_layout.addLayout(self.button_layout)
        output_layout.addWidget(self.splitter)
        main_layout.addLayout(output_layout)
        self.central_widget.setLayout(main_layout)


    def _createOutputWindow(self):
        """
        Some binary analysis commands will output to this.
        """

        self.output_label = QtGui.QLabel('Output')

        self.output_window = QTextEdit()
        self.output_window.setFontPointSize(10)
        self.output_window.setReadOnly(True)
        # Save it for later use
        self.output_window.original_textcolor = self.output_window.textColor()


    def _createOutputTable(self):
        """
        A vanilla QTableWidget. Callbacks modify
        its properties, like number of columns, etc.
        """
        self.table_label = QtGui.QLabel('Table Output')

        self.table = QTableWidget()
        self.table.setColumnCount(3)
        self.table.setColumnWidth(0, 100)
        self.table.setColumnWidth(1, 300)
        self.table.setColumnWidth(2, 300)

        self.table.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)

        # Connect signals to slots
        self.table.customContextMenuRequested.connect(self._tablePopup)
        self.table.horizontalHeader().sectionDoubleClicked.connect(self._tableHeaderDoubleClicked)
        self.table.cellDoubleClicked.connect(self._tableCellDoubleClicked)


    def _createOutputTree(self):
        """
        A QtreeWidget. Initially used to display those pesky
        dword comparison to immediate values.
        """
        self.tree_label = QtGui.QLabel('Tree Output')

        self.tree = QTreeWidget()
        self.tree.setColumnCount(3)
        self.tree.setColumnWidth(0, 150)
        self.tree.setColumnWidth(1, 150)
        self.tree.setColumnWidth(2, 50)

        # Connect signals to slots
        self.tree.itemDoubleClicked.connect(self._treeElementDoubleClicked)



    #################################################################
    # GUI Callbacks
    #################################################################
    def _console_output(self, s = "", err = False):
        """
        Convenience wrapper
        """
        if err:
            # Error message
            err_color = QColor('red')
            self.output_window.setTextColor(err_color)
            self.output_window.append(s)
            # restore original color
            self.output_window.setTextColor(self.output_window.original_textcolor)

        else:
            self.output_window.append(s)


    def _tableCellDoubleClicked(self, row, col):
        """
        Most of the info displayed in QTableWidgets represent addresses.
        Default action: try to jump to it.
        """
        it = self.table.item(row, col).text()

        try:
            addr = int(it, 16)
            jump_to_address(addr)

        except ValueError:
            self._console_output("[!] That does not look like an address...", err = True)
            return


    def _tablePopup(self, pos):
        """
        Popup menu activated clicking the secondary
        button on the table
        """
        menu = QtGui.QMenu()

        # Add menu entries
        delRow = menu.addAction(QIcon(self.iconp + "close.png"), "Delete Row")
        selItem = menu.addAction(QIcon(self.iconp + "bookmark.png"), "Mark entry")
        menu.addSeparator()
        origFunc = menu.addAction(QIcon(self.iconp + "lightning.png"), "Select origin function")
        destFunc = menu.addAction(QIcon(self.iconp + "flag.png"), "Select destination function")

        # Get entry clicked
        action = menu.exec_(self.mapToGlobal(pos))

        # Dispatch :)
        if action == delRow:
            self.table.removeRow(self.table.currentRow())

        elif action == selItem:
            self.table.currentItem().setBackground(QtGui.QColor('red'))

        elif action == origFunc:
            try:
                addr = self.table.currentItem().text()
                InfoUI.function_orig_ea = int(addr, 16)

            except:
                self._console_output("This does not look like an address...", err = True)

        elif action == destFunc:
            try:
                addr = self.table.currentItem().text()
                InfoUI.function_dest_ea = int(addr, 16)

            except:
                self._console_output("This does not look like an address...", err = True)


    def _tableHeaderDoubleClicked(self, index):
        """
        Used to sort the contents
        """
        self.table.sortItems(index, order = QtCore.Qt.AscendingOrder)


    def _treeElementDoubleClicked(self, item, column):
        """
        QTreeWidgetElement callback. Basically it jumps to
        the selected address in IDA disassembly window.
        """
        try:
            # Only interested in addresses
            addr_int = int(item.text(column), 16)
            jump_to_address(addr_int)
            # Paint some color
            item.setBackground(column, QtGui.QColor('green'))

        except:
            self._console_output("[!] That does not look like an address...", err = True)
Ejemplo n.º 42
0
class SettingsDialog(QDialog):
    """A dialog for choosing clock speed, log verbosity and GUI colors"""

    def __init__(self, mainwindow, config):
        super(SettingsDialog, self).__init__()
        self.setWindowTitle(self.str_title)
        # Reference kept to set settings in MainWindow
        self.mainwindow = mainwindow
        """A reference to the top window of the app."""
        self.config = config
        """The settings of our app."""
        # Setting log handlers
        logHandlers = QGroupBox(self.str_logHandlers)
        logHandlersLayout = QVBoxLayout()
        logHandlers.setLayout(logHandlersLayout)
        logToGui = QCheckBox(self.str_logToGui)
        logToGui.setChecked(self.config.getboolean('LogHandlers', 'gui'))
        logToGui.stateChanged.connect(
            lambda: self.chooseHandler(logToGui, 'gui'))
        logHandlersLayout.addWidget(logToGui)
        logtoStdout = QCheckBox(self.str_logToStdout)
        logtoStdout.setChecked(self.config.getboolean('LogHandlers', 'stdout'))
        logtoStdout.stateChanged.connect(
            lambda: self.chooseHandler(logtoStdout, 'stdout'))
        logHandlersLayout.addWidget(logtoStdout)
        logToFile = QCheckBox(self.str_logToFile)
        logToFile.setChecked(self.config.getboolean('LogHandlers', 'file'))
        logToFile.stateChanged.connect(
            lambda: self.chooseHandler(logToFile, 'file'))
        logHandlersLayout.addWidget(logToFile)
        # Setting log verbosity
        logVerbosity = QGroupBox(self.str_logVerbosity)
        logVerbosityLayout = QHBoxLayout()
        logVerbosity.setLayout(logVerbosityLayout)
        self.verbosityOptions = QTreeWidget()
        self.verbosityOptions.header().setVisible(False)
        for k, v in self.str_treeItems.items():
            node = QTreeWidgetItem(self.verbosityOptions, [k])
            if isinstance(v, str):
                node.setText(1, v)
                node.setCheckState(
                    0, boolToCheckState(
                        self.config.getboolean('LogVerbosity', v)))
            else:
                count = 0
                for key, val in v.items():
                    item = QTreeWidgetItem(node, [key, val])
                    checked = self.config.getboolean('LogVerbosity', val)
                    item.setCheckState(0, boolToCheckState(checked))
                    if checked:
                        count += 1
                if count == node.childCount():
                    node.setCheckState(0, Qt.Checked)
                elif count == 0:
                    node.setCheckState(0, Qt.Unchecked)
                else:
                    node.setCheckState(0, Qt.PartiallyChecked)
        self.verbosityOptions.itemChanged.connect(self.chooseVerbosity)
        logVerbosityLayout.addWidget(self.verbosityOptions)
        # Setting clock speed
        clock = QGroupBox(self.str_clock)
        clockLayout = QHBoxLayout()
        clock.setLayout(clockLayout)
        clockLayout.addWidget(QLabel(self.str_clockSpeed))
        spin = QDoubleSpinBox()
        spin.setValue(self.config.getfloat('Clock', 'speed'))
        clockLayout.addWidget(spin)
        # Setting appearance
        appearance = QGroupBox(self.str_appearance)
        appearanceLayout = QHBoxLayout()
        appearance.setLayout(appearanceLayout)
        appearanceLayout.addWidget(QLabel(self.str_circBgColor))
        circBgBtn = QPushButton(self.str_choose)
        circBgBtn.setPalette(QPalette(
            QColor(self.config.get('Appearance', 'circ_bg_color'))))
        circBgBtn.clicked.connect(
            lambda: self.chooseColor(circBgBtn, 'circ_bg_color'))
        appearanceLayout.addWidget(circBgBtn)
        appearanceLayout.addWidget(QLabel(self.str_logBgColor))
        logBgBtn = QPushButton(self.str_choose)
        logBgBtn.setPalette(QPalette(
            QColor(self.config.get('Appearance', 'log_bg_color'))))
        logBgBtn.clicked.connect(
            lambda: self.chooseColor(logBgBtn, 'log_bg_color'))
        appearanceLayout.addWidget(logBgBtn)
        # Saving settings to file and effectively setting them
        close = QPushButton(self.str_close)
        close.clicked.connect(self.closeAndApply)
        layout = QGridLayout(self)
        layout.addWidget(logHandlers, 0, 0, 1, 1)
        layout.addWidget(logVerbosity, 0, 1, 1, 1)
        layout.addWidget(clock, 1, 0, 1, 2)
        layout.addWidget(appearance, 2, 0, 1, 2)
        layout.addWidget(close, 3, 1, 1, 1)
        self.setLayout(layout)

    def chooseColor(self, button, option):
        """The user modifies an UI background color."""
        color = QColorDialog.getColor()
        if color.isValid():
            button.setPalette(QPalette(color))
            self.config.set('Appearance', option, color.name())

    def chooseHandler(self, checkbox, option):
        """The user chooses where to log messages."""
        self.config.set('LogHandlers', option, str(checkbox.isChecked()))

    def chooseVerbosity(self, item):
        """The user chooses which logs are shown."""
        option = item.text(1)
        if option:
            self.config.set('LogVerbosity', option, str(
                checkStateToBool(item.checkState(0))))
            if isinstance(item.parent(), QTreeWidgetItem):
                count = item.parent().childCount()
                for i in range(item.parent().childCount()):
                    if not checkStateToBool(
                            item.parent().child(i).checkState(0)):
                        count = count - 1
                self.verbosityOptions.blockSignals(True)
                if count == item.parent().childCount():
                    item.parent().setCheckState(0, Qt.Checked)
                elif count == 0:
                    item.parent().setCheckState(0, Qt.Unchecked)
                else:
                    item.parent().setCheckState(0, Qt.PartiallyChecked)
                self.verbosityOptions.blockSignals(False)
        else:
            for i in range(item.childCount()):
                self.verbosityOptions.blockSignals(True)
                item.child(i).setCheckState(0, item.checkState(0))
                self.config.set(
                    'LogVerbosity', item.child(i).text(1),
                    str(checkStateToBool(item.checkState(0))))
                self.verbosityOptions.blockSignals(False)

    def closeAndApply(self):
        """Settings saved to file and applied to GUI."""
        self.mainwindow.setSettings()
        with open(self.config.configFile, 'w+') as f:
            self.config.write(f)
        self.close()
Ejemplo n.º 43
0
    def __init__(self, *args, **kwargs):

        self.get_csv_form_google_spreadsheets(Widget_FileTree.url,
                                              Widget_FileTree.csv)
        self.dict_extensionList = self.get_dictList_from_csvPath(
            Widget_FileTree.csv)

        super(Widget_FileTree, self).__init__(*args, **kwargs)
        self.installEventFilter(self)

        mainLayout = QVBoxLayout(self)
        mainLayout.setContentsMargins(0, 5, 0, 0)

        w_basePath = QWidget()
        lay_basePath = QHBoxLayout(w_basePath)
        w_basePath.setStyleSheet("font-size:12px")
        lay_basePath.setContentsMargins(5, 0, 3, 0)
        label_basePath = QLabel("Base Path : ")
        lineEdit_basePath = QLineEdit()
        lineEdit_basePath.setMinimumHeight(22)
        lineEdit_basePath.setReadOnly(True)
        lay_basePath.addWidget(label_basePath)
        lay_basePath.addWidget(lineEdit_basePath)
        w_tree = QTreeWidget()
        w_tree.setColumnCount(2)
        headerItem = w_tree.headerItem()
        headerItem.setText(0, "File Path")
        headerItem.setText(1, "Target Node")
        w_tree.itemExpanded.connect(self.setContentsWidth)
        w_tree.itemCollapsed.connect(self.setContentsWidth)
        w_tree.setSelectionMode(QAbstractItemView.ExtendedSelection)

        self.w_typeAttrList = Widget_TypeAttributeList()

        w_tree.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
        QtCore.QObject.connect(
            w_tree, QtCore.SIGNAL('customContextMenuRequested(QPoint)'),
            self.loadContextMenu)

        w_check = QWidget()
        lay_check = QHBoxLayout(w_check)
        lay_check.setContentsMargins(5, 5, 0, 10)
        lay_check.setSpacing(0)
        checkBox = QCheckBox()
        checkBox.setFixedWidth(30)
        label = QLabel("Show Unused files in folder")
        label.setStyleSheet("font-size:12px")
        lay_check.addWidget(checkBox)
        lay_check.addWidget(label)

        mainLayout.addWidget(w_basePath)
        mainLayout.addWidget(w_tree)
        mainLayout.addWidget(w_check)

        self.w_tree = w_tree
        self.checkBox = checkBox
        self.lineEdit = lineEdit_basePath

        self.load_checkBoxValue(checkBox, Widget_FileTree.path_uiInfo)

        QtCore.QObject.connect(checkBox, QtCore.SIGNAL("stateChanged(int)"),
                               self.saveCheckBoxValue)
        QtCore.QObject.connect(w_tree, QtCore.SIGNAL("itemSelectionChanged()"),
                               self.selectItem)
Ejemplo n.º 44
0
 def mousePressEvent(self, e):
     """:type e: QMouseEvent.QMouseEvent"""
     pressed_item = self.itemAt(e.pos())
     if pressed_item:
         self.__drag_item = pressed_item
     QTreeWidget.mousePressEvent(self, e)
Ejemplo n.º 45
0
 def __init__(self, mainwindow, config):
     super(SettingsDialog, self).__init__()
     self.setWindowTitle(self.str_title)
     # Reference kept to set settings in MainWindow
     self.mainwindow = mainwindow
     """A reference to the top window of the app."""
     self.config = config
     """The settings of our app."""
     # Setting log handlers
     logHandlers = QGroupBox(self.str_logHandlers)
     logHandlersLayout = QVBoxLayout()
     logHandlers.setLayout(logHandlersLayout)
     logToGui = QCheckBox(self.str_logToGui)
     logToGui.setChecked(self.config.getboolean('LogHandlers', 'gui'))
     logToGui.stateChanged.connect(
         lambda: self.chooseHandler(logToGui, 'gui'))
     logHandlersLayout.addWidget(logToGui)
     logtoStdout = QCheckBox(self.str_logToStdout)
     logtoStdout.setChecked(self.config.getboolean('LogHandlers', 'stdout'))
     logtoStdout.stateChanged.connect(
         lambda: self.chooseHandler(logtoStdout, 'stdout'))
     logHandlersLayout.addWidget(logtoStdout)
     logToFile = QCheckBox(self.str_logToFile)
     logToFile.setChecked(self.config.getboolean('LogHandlers', 'file'))
     logToFile.stateChanged.connect(
         lambda: self.chooseHandler(logToFile, 'file'))
     logHandlersLayout.addWidget(logToFile)
     # Setting log verbosity
     logVerbosity = QGroupBox(self.str_logVerbosity)
     logVerbosityLayout = QHBoxLayout()
     logVerbosity.setLayout(logVerbosityLayout)
     self.verbosityOptions = QTreeWidget()
     self.verbosityOptions.header().setVisible(False)
     for k, v in self.str_treeItems.items():
         node = QTreeWidgetItem(self.verbosityOptions, [k])
         if isinstance(v, str):
             node.setText(1, v)
             node.setCheckState(
                 0, boolToCheckState(
                     self.config.getboolean('LogVerbosity', v)))
         else:
             count = 0
             for key, val in v.items():
                 item = QTreeWidgetItem(node, [key, val])
                 checked = self.config.getboolean('LogVerbosity', val)
                 item.setCheckState(0, boolToCheckState(checked))
                 if checked:
                     count += 1
             if count == node.childCount():
                 node.setCheckState(0, Qt.Checked)
             elif count == 0:
                 node.setCheckState(0, Qt.Unchecked)
             else:
                 node.setCheckState(0, Qt.PartiallyChecked)
     self.verbosityOptions.itemChanged.connect(self.chooseVerbosity)
     logVerbosityLayout.addWidget(self.verbosityOptions)
     # Setting clock speed
     clock = QGroupBox(self.str_clock)
     clockLayout = QHBoxLayout()
     clock.setLayout(clockLayout)
     clockLayout.addWidget(QLabel(self.str_clockSpeed))
     spin = QDoubleSpinBox()
     spin.setValue(self.config.getfloat('Clock', 'speed'))
     clockLayout.addWidget(spin)
     # Setting appearance
     appearance = QGroupBox(self.str_appearance)
     appearanceLayout = QHBoxLayout()
     appearance.setLayout(appearanceLayout)
     appearanceLayout.addWidget(QLabel(self.str_circBgColor))
     circBgBtn = QPushButton(self.str_choose)
     circBgBtn.setPalette(QPalette(
         QColor(self.config.get('Appearance', 'circ_bg_color'))))
     circBgBtn.clicked.connect(
         lambda: self.chooseColor(circBgBtn, 'circ_bg_color'))
     appearanceLayout.addWidget(circBgBtn)
     appearanceLayout.addWidget(QLabel(self.str_logBgColor))
     logBgBtn = QPushButton(self.str_choose)
     logBgBtn.setPalette(QPalette(
         QColor(self.config.get('Appearance', 'log_bg_color'))))
     logBgBtn.clicked.connect(
         lambda: self.chooseColor(logBgBtn, 'log_bg_color'))
     appearanceLayout.addWidget(logBgBtn)
     # Saving settings to file and effectively setting them
     close = QPushButton(self.str_close)
     close.clicked.connect(self.closeAndApply)
     layout = QGridLayout(self)
     layout.addWidget(logHandlers, 0, 0, 1, 1)
     layout.addWidget(logVerbosity, 0, 1, 1, 1)
     layout.addWidget(clock, 1, 0, 1, 2)
     layout.addWidget(appearance, 2, 0, 1, 2)
     layout.addWidget(close, 3, 1, 1, 1)
     self.setLayout(layout)