Exemplo n.º 1
0
    def setupTabs(self):
        """ Setup the various tabs in the AddressWidget. """
        groups = ["ABC", "DEF", "GHI", "JKL", "MNO", "PQR", "STU", "VW", "XYZ"]

        for group in groups:
            proxyModel = QSortFilterProxyModel(self)
            proxyModel.setSourceModel(self.tableModel)
            proxyModel.setDynamicSortFilter(True)

            tableView = QTableView()
            tableView.setModel(proxyModel)
            tableView.setSortingEnabled(True)
            tableView.setSelectionBehavior(QAbstractItemView.SelectRows)
            tableView.horizontalHeader().setStretchLastSection(True)
            tableView.verticalHeader().hide()
            tableView.setEditTriggers(QAbstractItemView.NoEditTriggers)
            tableView.setSelectionMode(QAbstractItemView.SingleSelection)

            # This here be the magic: we use the group name (e.g. "ABC") to
            # build the regex for the QSortFilterProxyModel for the group's
            # tab. The regex will end up looking like "^[ABC].*", only
            # allowing this tab to display items where the name starts with
            # "A", "B", or "C". Notice that we set it to be case-insensitive.
            reFilter = "^[%s].*" % group

            proxyModel.setFilterRegExp(QRegExp(reFilter, Qt.CaseInsensitive))
            proxyModel.setFilterKeyColumn(0)  # Filter on the "name" column
            proxyModel.sort(0, Qt.AscendingOrder)

            #tableView.selectionModel().selectionChanged.connect(self.selectionChanged)

            self.addTab(tableView, group)
Exemplo n.º 2
0
    def setupTabs(self):
        """ Setup the various tabs in the AddressWidget. """
        groups = ["ABC", "DEF", "GHI", "JKL", "MNO", "PQR", "STU", "VW", "XYZ"]

        for group in groups:
            proxyModel = QSortFilterProxyModel(self)
            proxyModel.setSourceModel(self.tableModel)
            proxyModel.setDynamicSortFilter(True)

            tableView = QTableView()
            tableView.setModel(proxyModel)
            tableView.setSortingEnabled(True)
            tableView.setSelectionBehavior(QAbstractItemView.SelectRows)
            tableView.horizontalHeader().setStretchLastSection(True)
            tableView.verticalHeader().hide()
            tableView.setEditTriggers(QAbstractItemView.NoEditTriggers)
            tableView.setSelectionMode(QAbstractItemView.SingleSelection)

            # This here be the magic: we use the group name (e.g. "ABC") to
            # build the regex for the QSortFilterProxyModel for the group's
            # tab. The regex will end up looking like "^[ABC].*", only 
            # allowing this tab to display items where the name starts with 
            # "A", "B", or "C". Notice that we set it to be case-insensitive.
            reFilter = "^[%s].*" % group

            proxyModel.setFilterRegExp(QRegExp(reFilter, Qt.CaseInsensitive))
            proxyModel.setFilterKeyColumn(0) # Filter on the "name" column
            proxyModel.sort(0, Qt.AscendingOrder)

            tableView.selectionModel().selectionChanged.connect(self.selectionChanged)

            self.addTab(tableView, group)
Exemplo n.º 3
0
class MainWindow(QWidget):
    
    def __init__(self):
        QWidget.__init__(self)
        self.resize(640, 480)
        vbox = QVBoxLayout()
        self.setWindowTitle('TableDemo')
        self.setLayout(vbox)
        self.table = QTableView()
        self.table.setAlternatingRowColors(True)
        self.table.setSortingEnabled(True)
        self.table.setSelectionBehavior(QAbstractItemView.SelectRows)
        cards = [Card(u'巫医', u'战吼:恢复2点生命值。', 1, 2, 1), 
                 Card(u'狼骑兵', u'冲锋', 3, 3, 1),
                 Card(u'石牙野猪', u'冲锋', 1, 1, 1),
                 Card(u'森金持盾卫士', u'嘲讽', 4, 3, 5),
                ]
        cardModel = CardModel(cards)
        sortModel = QSortFilterProxyModel()
        sortModel.setSourceModel(cardModel)
        self.table.setModel(sortModel)
        vbox.addWidget(self.table)
        
    def printItem(self, idx):
        print idx.model().persons[idx.row()]
Exemplo n.º 4
0
class uiManager(QMainWindow):
	def __init__(self):
		super(uiManager, self).__init__()

		self.setMinimumSize(900,500)
		self.setWindowTitle("Manage")

		self.aFileModel = FileModel() # Add files
		self.sFileModel = FileModel() # Queried files
		self.eFileModel = FileModel() # Editing files

		self.tabWidget = QTabWidget()
		self.setCentralWidget(self.tabWidget)

		self.defActions()

		self.addTab()
		self.searchTab()
		self.editTab()
		self.testtab()

		self.statusBar().showMessage("Hi.")


	def defActions(self):
		self.findAct = QAction("+", self, triggered=self.findFilesDlg)
		self.dropAddTabAct = QAction("-", self, shortcut="Delete",
			triggered=self.dropAddTabRows)
		self.dropEditTabAct = QAction("-", self, shortcut="Delete",
			triggered=self.dropEditTabRows)


	def dropEditTabRows(self):
		self.dropFiles(self.eFileTable, self.eFileModel)


	def dropAddTabRows(self):
		self.dropFiles(self.aFileTable, self.aFileModel)


	def testtab(self):
		self.test = QWidget()
		self.tabWidget.addTab(self.test, "WORDS")
		self.tabWidget.setCornerWidget(self.test)


	def addTab(self):

		self.aFileTable = QTableView()
		self.aFileTable.setModel(self.aFileModel)
		self.aFileTable.setSelectionBehavior(QAbstractItemView.SelectRows)

		self.setColumnsWidths(self.aFileTable)

		self.addWidget = QWidget()

		vLayout = QVBoxLayout()
		vLayout.addWidget(self.aFileTable)

		addBtn = QToolButton()
		addBtn.setDefaultAction(self.findAct)
		# addBtn.clicked.connect(self.findFilesDlg)

		dropBtn = QToolButton()
		dropBtn.setDefaultAction(self.dropAddTabAct)
		# rmvBtn.clicked.connect(self.rmvFiles)

		insBtn = QPushButton("Track")
		insBtn.clicked.connect(self.insert)

		hLayout = QHBoxLayout()

		hLayout.addWidget(addBtn)
		hLayout.addWidget(dropBtn)
		hLayout.addStretch(1)		
		hLayout.addWidget(insBtn)

		vLayout.addLayout(hLayout)

		self.addWidget.setLayout(vLayout)

		self.tabWidget.addTab(self.addWidget, u"Add")


	def searchTab(self):

		# sFileModel + view tab
		self.sFileTable = QTableView()
		self.sFileTable.setModel(self.sFileModel)
		self.sFileTable.setEditTriggers(QAbstractItemView.NoEditTriggers)
		self.sFileTable.setSelectionBehavior(QAbstractItemView.SelectRows)

		self.setColumnsWidths(self.sFileTable)

		self.searchWidget = QWidget()

		# Horizontal layout
		# Button, Line Edit, Button
		hLayout = QHBoxLayout()
		self.searchBox = QLineEdit()
		self.searchBox.returnPressed.connect(self.searchdb)
		hLayout.addWidget(self.searchBox)

		searchBtn = QPushButton("Search")
		searchBtn.clicked.connect(self.searchdb)
		hLayout.addWidget(searchBtn)

		# Vertical Layout
		vLayout = QVBoxLayout()
		vLayout.addLayout(hLayout)
		vLayout.addWidget(self.sFileTable)

		# sub horiz box
		subHLayout = QHBoxLayout()
		subHLayout.addStretch(1)
		
		editBtn = QPushButton("Edit -->")
		editBtn.clicked.connect(self.addToEditTab)

		subHLayout.addWidget(editBtn)
		vLayout.addLayout(subHLayout)

		self.searchWidget.setLayout(vLayout)
		self.tabWidget.addTab(self.searchWidget, u"Retrieve")


	def editTab(self):
		
		self.eFileTable = QTableView()
		self.eFileTable.setModel(self.eFileModel)
		self.eFileTable.setSelectionBehavior(QAbstractItemView.SelectRows)

		self.setColumnsWidths(self.eFileTable)

		self.editWidget = QWidget()

		vLayout = QVBoxLayout()
		vLayout.addWidget(self.eFileTable)

		dropBtn = QToolButton()
		dropBtn.setDefaultAction(self.dropEditTabAct)

		undBtn = QPushButton("Undo Delete")
		undBtn.clicked.connect(self.undeleteFiles)

		rmvBtn = QPushButton("Delete")
		rmvBtn.clicked.connect(self.deleteFiles)

		cmtBtn = QPushButton("Update")
		cmtBtn.clicked.connect(self.update)

		hLayout = QHBoxLayout()
		hLayout.addWidget(dropBtn)
		hLayout.addStretch(1)
		hLayout.addWidget(undBtn)
		hLayout.addWidget(rmvBtn)
		hLayout.addWidget(cmtBtn)

		vLayout.addLayout(hLayout)

		self.editWidget.setLayout(vLayout)

		self.tabWidget.addTab(self.editWidget, u"Edit")


	def setColumnsWidths(self, table):
		table.setColumnWidth(0, 438)
		table.horizontalHeader().setStretchLastSection(True)


	def undeleteFiles(self):
		delfiles = self.eFileModel.delfiles
		for file in delfiles:
			path, tags = file.path, file.tags
			self.addToModel(self.eFileModel, path, tags, True)
		self.eFileModel.delfiles = []


	# Adds files to the models delete list
	# for removal from the DB on next update()
	def deleteFiles(self):
		indexes = self.getSRIndexes(self.eFileTable)

		for index in indexes:
			self.eFileModel.delList(index.row())
			self.eFileModel.removeRows(index.row())
		self.statusBar().showMessage("Pending Changes")


	# Drop files from a table and model
	def dropFiles(self, table, model):
		indexes = self.getSRIndexes(table)

		for index in indexes:
			model.removeRows(index.row())


	# Inserts files into the DB from the add model
	def insert(self):
		p = self.aFileModel.insert()
		if p:
			msgDuplBox = QMessageBox()
			msgDuplBox.setText('\n'.join(p) + 
				"\n\n...file(s) already being tracked.")
			msgDuplBox.exec_()


	def update(self):
		self.eFileModel.update()

	
	''' Order of user selection matters in the view. As a user
	selects an item it is entered into the list of selected rows.
	To remove the correct items first you have to sort the list,
	then reverse it to remove items from the bottom up to prevent
	a row number pointing at the wrong entry because an entry
	above it was already removed.'''
	def getSRIndexes(self, table):
		indexes = table.selectionModel().selectedRows()
		indexes.sort()
		indexes.reverse()
		return indexes


	def findFilesDlg(self):
		finder = getFilesDlg(self)
		finder.sendPaths.connect(self.addToAddTab)
		finder.show()


	@Slot(list)
	def addToAddTab(self, list):
		for path in list:

			if self.colCheck(self.aFileModel, path):

				self.addToModel(self.aFileModel, path)


	def addToSearchTab(self, qresults):
		if qresults:

			self.sFileModel.reset()

			for file in qresults:

				path,tags = file
				self.addToModel(self.sFileModel, path, tags)


	def addToEditTab(self):
		# Need to make sure what they are adding isn't already
		# present in the edit model's files or delfiles lists
		indexes = self.getSRIndexes(self.sFileTable)

		# save original path as update reference for db
		oldpath = True

		for index in indexes:

			path, tags = self.sFileModel.dataTuple(index)

			if self.colCheck(self.eFileModel, path):
			
				self.addToModel(self.eFileModel, path, tags, oldpath)

				self.sFileModel.removeRows(index.row())


	def addToModel(self, model, path, tags=None, oldpath=False):

			model.insertRows(0)

			ix = model.index(0, 0, QModelIndex())
			model.setData(ix, path, Qt.EditRole, oldpath)

			ix = model.index(0, 1, QModelIndex())
			model.setData(ix, tags, Qt.EditRole, oldpath)


	def colCheck(self, model, path):
		# THIS IS REALLY SLOW
		# Linear search, 
		if model.files:
			for file in model.files:
				if path == file.path:
					return False
		return True


	def searchdb(self):
		thedb = myDB()
		query = thedb.query(self.searchBox.text())
		self.addToSearchTab(query)
Exemplo n.º 5
0
class TemplateSelectDialog(QDialog):
    def refresh_templates_list(self):

        documents = documents_service.all_templates()

        self.model.removeRows(0, self.model.rowCount())

        if documents:
            for doc in sorted(list(documents), key=lambda d: d.filename):
                self._add_one_document(doc.filename, doc.template_document_id,
                                       doc.file_size, doc.description)

    def __init__(self, parent=None):
        super(TemplateSelectDialog, self).__init__(parent)

        self.setWindowTitle(_("Select a template"))

        self.template_id = []

        self.model = QStandardItemModel()
        self.view = QTableView()
        self.view.setModel(self.model)
        self.view.verticalHeader().setVisible(False)
        self.view.horizontalHeader().setVisible(False)
        self.view.setMinimumSize(500, 200)
        self.view.setShowGrid(True)
        self.view.setEditTriggers(QAbstractItemView.NoEditTriggers)
        self.view.setSelectionBehavior(QAbstractItemView.SelectRows)

        l = QVBoxLayout()

        l.addWidget(QLabel(_("Please select one or more template.")))
        self.view.doubleClicked.connect(self._doubleClicked)

        l.addWidget(QLabel(u"<h3>{}</h3>".format(_("Documents Templates"))))
        l.addWidget(self.view)

        self.buttons = QDialogButtonBox()
        self.buttons.addButton(QDialogButtonBox.StandardButton.Cancel)
        self.buttons.addButton(QDialogButtonBox.Ok)
        self.buttons.accepted.connect(self.accept)
        self.buttons.rejected.connect(self.reject)

        l.addWidget(self.buttons)

        self.setLayout(l)

    def _add_one_document(self, file_name, doc_id, file_size, description):
        """ Adds a document to the list
        """

        # file_name is either an absolute path or just a file name
        # If it is an absolute path, then the file is expected
        # to exist locally (at it absolute path location of course).
        # If not, then the file is expected to be a remote file
        # and shall be downloaded before opening.

        mainlog.debug(u"{} {} {} {}".format(file_name, doc_id, file_size,
                                            description))
        short_name = file_name
        if os.path.isabs(file_name):
            short_name = os.path.basename(file_name)
            if not os.path.isfile(file_name):
                raise Exception(u"The file {} doesn't exist".format(file_name))

        items = [QStandardItem(short_name)]
        items.append(QStandardItem(description))

        self.model.appendRow(items)
        self.model.setData(self.model.index(self.model.rowCount() - 1, 0),
                           doc_id, Qt.UserRole + 1)

        self.view.horizontalHeader().setResizeMode(0, QHeaderView.Stretch)
        self.view.horizontalHeader().setResizeMode(1, QHeaderView.Stretch)
        self.view.resizeRowsToContents()

    @Slot()
    def accept(self):
        selected_rows = self.view.selectionModel().selectedRows()
        if selected_rows:
            self.template_id = []
            for row in selected_rows:
                ndx_row = row.row()
                self.template_id.append(
                    self.model.data(self.model.index(ndx_row, 0),
                                    Qt.UserRole + 1))
        else:
            self.template_id = []
        return super(TemplateSelectDialog, self).accept()

    @Slot()
    def reject(self):
        return super(TemplateSelectDialog, self).reject()

    @Slot(QModelIndex)
    def _doubleClicked(self, ndx):
        self.accept()
Exemplo n.º 6
0
class FindOrderDialog(QDialog):
    def __init__(self,parent):
        global dao
        super(FindOrderDialog,self).__init__(parent)

        title = _("Find order")
        self.setWindowTitle(title)
        top_layout = QVBoxLayout()
        self.title_widget = TitleWidget(title,self)
        top_layout.addWidget(self.title_widget)

        hlayout = QHBoxLayout()
        hlayout.addWidget(QLabel(_("Search")))
        self.search_criteria = QLineEdit()
        self.search_criteria.setObjectName("search_criteria")
        hlayout.addWidget(self.search_criteria)
        top_layout.addLayout(hlayout)



        self.search_results_view = QTableView()

        self.headers_view = QHeaderView(Qt.Orientation.Horizontal)
        self.header_model = make_header_model([_("Preorder Nr"),_("Order Nr"),_("Customer Nr"),_("Customer"),_("Order Part")])
        self.headers_view.setModel(self.header_model) # qt's doc : The view does *not* take ownership (bt there's something with the selecion mode

        self.search_results_model = QStandardItemModel()
        self.search_results_view.setModel(self.search_results_model)

        self.search_results_view.setEditTriggers(QAbstractItemView.NoEditTriggers)
        self.search_results_view.setHorizontalHeader(self.headers_view)
        self.search_results_view.verticalHeader().hide()
        # self.search_results_view.horizontalHeader().setResizeMode(QHeaderView.ResizeToContents)
        self.search_results_view.horizontalHeader().setResizeMode(3, QHeaderView.Stretch)
        self.search_results_view.horizontalHeader().setResizeMode(4, QHeaderView.Stretch)

        self.search_results_view.setSelectionBehavior(QAbstractItemView.SelectRows)

        self.buttons = QDialogButtonBox()
        self.buttons.addButton( QDialogButtonBox.StandardButton.Cancel)
        self.buttons.addButton( QDialogButtonBox.Ok)
        self.buttons.button(QDialogButtonBox.Ok).setObjectName("go_search")

        top_layout.addWidget(self.search_results_view)
        top_layout.setStretch(2,1000)
        top_layout.addWidget(self.buttons)
        self.setLayout(top_layout)

        self.buttons.accepted.connect(self.accept)
        self.buttons.rejected.connect(self.reject)
        self.search_results_view.activated.connect(self.row_activated)
        self.search_criteria.returnPressed.connect(self.search_criteria_submitted)

        self.setMinimumSize(800,640)

    def find_by_text(self,text):
        text = text.strip()

        try:
            too_many_results, res = dao.order_part_dao.find_ids_by_text(text.strip())

            if too_many_results:
                showWarningBox(_("Too many results"),_("The query you've given brought back too many results. Only a part of them is displayed. Consider refining your query"),object_name="too_many_results")

            return dao.order_part_dao.find_by_ids(res)

        except DataException as de:

            if de.code == DataException.CRITERIA_IS_EMPTY:
                showErrorBox(_("Error in the filter !"),
                             _("The filter can't be empty"),object_name="filter_is_empty")
            elif de.code == DataException.CRITERIA_IS_TOO_SHORT:
                showErrorBox(_("Error in the filter !"),
                             _("The filter is too short"),object_name="filter_is_too_short")
            elif de.code == DataException.CRITERIA_IS_TOO_LONG:
                showErrorBox(_("Error in the filter !"),
                             _("The filter is too long"),object_name="filter_is_too_long")

            return []
        


        # order_part_match = []
        # matches = []
        # super_matches = []

        # import re
        # re_order_part_identifier = re.compile("^([0-9]+)([A-Z]+)$")
        # re_label_identifier = re.compile("^[0-9]+$")

        # if re_order_part_identifier.match(text.upper()):
        #     # Look for an exact (and unique) match on the order part full identifier
        #     p = dao.order_part_dao.find_by_full_id(text.upper())
        #     if p:
        #         # Mimick SQLAlchemy's KeyTuples
        #         # FIXME It seems that I use something that's internal to SQLA
        #         # Search SQLA's doc for KeyedTuple to find about collections.namedtuple()

        #         from sqlalchemy.util._collections import KeyedTuple

        #         kt = KeyedTuple([p.order_id, p.order.preorder_label, p.order.accounting_label, p.order.customer_order_name, p.order.customer.fullname, p.order.creation_date, p.description, p.order_part_id, p.label],
        #                         labels=['order_id','preorder_label','accounting_label','customer_order_name','fullname','creation_date','description','order_part_id','label'])

        #         order_part_match = [ kt ]

        # if re_label_identifier.match(text):
        #     for r in dao.order_dao.find_by_labels(int(text)):
        #         super_matches.append(r)

        # for r in dao.order_dao.find_by_customer_name(text):
        #     # mainlog.debug('customer name match on {}'.format(text))
        #     matches.append(r)

        # for r in dao.order_dao.find_by_customer_order_name(text):
        #     # mainlog.debug('customer name match on {}'.format(text))
        #     matches.append(r)

        # for r in dao.order_part_dao.find_by_description(text):
        #     matches.append(r)

        # # Finally we order the matches to bring the most relevant
        # # first. The "most relevant" is really a business order.

        # return order_part_match + super_matches + \
        #     sorted(matches, lambda a,b: - cmp(a.order_id,b.order_id))


    def _search_results_to_array(self,search_results):
        array = []
        
        for res in search_results:
            # mainlog.debug("_search_results_to_array {}".format(res.creation_date))

            i = QStandardItem(res.preorder_part_label)
            row = [i,
                   QStandardItem(res.accounting_part_label),
                   QStandardItem(res.customer_order_name),
                   QStandardItem(res.fullname)]

            if 'order_part_id' in res.__dict__:
                # It's an order part

                i.setData( res.order_part_id, Qt.UserRole)
                i.setData( 'order_part', Qt.UserRole+1)
                row.append( QStandardItem(res.description))
            else:
                # It's an order

                i.setData( res.order_id, Qt.UserRole)
                i.setData( 'order', Qt.UserRole+1)
                row.append( QStandardItem())

            array.append(row)

        return array


    def load_search_results(self,text=None):
        global dao

        if text is None:
            text = self.search_criteria.text()

        db_results = self.find_by_text(text)
        array = self._search_results_to_array(db_results)

        self.search_results_model.removeRows(0,self.search_results_model.rowCount())
        for row in array:
            self.search_results_model.appendRow(row)
        mainlog.debug("Loaded model : {}".format(self.search_results_model.rowCount()))
        self.search_results_view.resizeColumnsToContents()

        if self.search_results_model.rowCount() > 0:
            self.search_results_view.setCurrentIndex(self.search_results_model.index(0,0))
            self.search_results_view.setFocus(Qt.OtherFocusReason)

        if self.search_results_model.rowCount() == 1:
            self.accept()


    def selected_item(self):
        mainlog.debug("FindOrder.selected_item")
        ndx = self.search_results_view.currentIndex()
        if ndx.isValid():
            ndx = self.search_results_view.model().index( ndx.row(), 0)

            item = ndx.data(Qt.UserRole)
            item_type = ndx.data(Qt.UserRole+1)

            if item_type == 'order':
                mainlog.debug("FindOrder.selected_item order_id={}".format(item))
                return dao.order_dao.find_by_id(item)
            elif item_type == 'order_part':
                mainlog.debug("FindOrder.selected_item order_part_id={}".format(item))
                return dao.order_part_dao.find_by_id(item)
            else:
                mainlog.error("Unsupported item type {}".format(item_type))
        else:
            mainlog.error("Invalid index")
            return None

    @Slot()
    def accept(self):
        # mainlog.debug("accept")
        # self.load_search_results()
        # mainlog.debug("accept - done")
        return super(FindOrderDialog,self).accept()

    @Slot()
    def reject(self):
        return super(FindOrderDialog,self).reject()

    @Slot()
    def search_criteria_submitted(self):
        mainlog.debug("search_criteria_submitted")
        self.load_search_results()

    @Slot(QModelIndex)
    def row_activated(self,ndx):
        mainlog.debug("row_activated")
        self.accept()

    def keyPressEvent(self,event):

        # The goal here is to make sure the accept signal is called only
        # if the user clicks on the "OK" button /with the mouse/ and,
        # not with the keyboard

        if event.key() in (Qt.Key_Enter, Qt.Key_Return):
            return
        else:
            super(FindOrderDialog,self).keyPressEvent(event)