当前位置: 首页>>代码示例>>Python>>正文


Python View.indexOfCurrentElement方法代码示例

本文整理汇总了Python中View.View.indexOfCurrentElement方法的典型用法代码示例。如果您正苦于以下问题:Python View.indexOfCurrentElement方法的具体用法?Python View.indexOfCurrentElement怎么用?Python View.indexOfCurrentElement使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在View.View的用法示例。


在下文中一共展示了View.indexOfCurrentElement方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from View import View [as 别名]
# 或者: from View.View import indexOfCurrentElement [as 别名]
class Control:
    """Control Class: Commands the View and the Model, and deals with interactions between them."""

    def __init__(self, app):
        """Constructor of Control"""
        self.view = View(self, "Phone Book", app)
        self.view.show()
        self.model = Model()

    def exportCSV(self):
        """Export contacts into a CSV file"""
        fileDialog = QtWidgets.QFileDialog.getSaveFileName(self.view,
                                                           "Save File",
                                                           "untitled",
                                                           "CSV files (*.csv);;All Files (*)")
        fileName = str(fileDialog).partition('\'')[2].split('\'')[0]
        if not (fileName is ""):
            with open(fileName, "wb") as file:
                file.write(str(self.model.exportCSV()).encode('Latin-1'))
            QtWidgets.QMessageBox.information(self.view,
                                              "Save succeeded",
                                              "The file has been successfully saved in the folder : " + fileName,
                                              QtWidgets.QMessageBox.Close)
            self.view.isModified = False
        else:
            QtWidgets.QMessageBox.information(self.view, "Save failed", "The file has not been saved.")

    def importCSV(self):
        """Import contacts from a CSV file"""
        fileDialog = QtWidgets.QFileDialog.getOpenFileName(self.view, "Open File", None, "CSV Files (*.csv)")
        fileName = str(fileDialog).partition('\'')[2].split('\'')[0]
        if not (fileName is ""):
            if not self.model.listContact:
                answer = QtWidgets.QMessageBox.No
            else:
                answer = QtWidgets.QMessageBox.question(self.view, "Death or life choice !?",
                                                        "Would you like to append the contacts to your actual list?",
                                                        QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No)
            if answer == QtWidgets.QMessageBox.No:
                self.clearAll()
            with open(fileName, newline='') as csvfile:
                reader = csv.reader(csvfile, delimiter=',')
                for nbLine, row in enumerate(reader):
                    tempElement = QtWidgets.QTreeWidgetItem()
                    if nbLine != 0:
                        for index, word in enumerate(row):
                            tempElement.setText(index, word)
                        self.registerContact(tempElement.clone())
            QtWidgets.QMessageBox.information(self.view,
                                              "Open succeeded",
                                              "The file has been successfully opened in the folder : " + fileName,
                                              QtWidgets.QMessageBox.Close)
        else:
            QtWidgets.QMessageBox.information(self.view, "Open failed", "The file has not been opened.")

    def registerContact(self, newElement, oldElement = None):
        """Register contacts in Model before adding them to the View"""
        if self.model.registerContact(newElement.clone(), oldElement):
            if oldElement is not None:
                self.view.tableOfContact.takeTopLevelItem(self.view.indexOfCurrentElement())
            self.view.tableOfContact.addTopLevelItem(self.model.buffer)
            self.view.tableOfContact.setCurrentItem(self.model.buffer)

    def eraseContact(self, item):
        """Erase contacts in model and by doing so, authorized the View to delete it too"""
        if self.model.listContact:
            if not self.model.eraseContact(item):
                return False
            else:
                return True
        else:
            return False

    def searchContact(self, strContact):
        """Search the text entered by the user among the list of contacts in model"""
        if self.model.listContact:
            newListContact = self.model.searchContact(strContact)
            if newListContact:
                #  Erase contacts in the view before inserting the ones among
                # the new list containing the searched contacts:
                size = int(self.view.tableOfContact.topLevelItemCount())
                if size != 0:
                    for index in reversed(range(size)):
                        self.view.tableOfContact.takeTopLevelItem(index)
                for contact in newListContact:
                    self.view.tableOfContact.addTopLevelItem(contact)

    def clearAll(self):
        """Erase all contacts from the Model and the View"""
        size = int(self.view.tableOfContact.topLevelItemCount())
        if size != 0:
            for index in reversed(range(size)):
                self.model.eraseContact(index)
                self.view.tableOfContact.takeTopLevelItem(index)
开发者ID:Matthiosso,项目名称:Project-Python-Qt-PhoneBook,代码行数:96,代码来源:Control.py


注:本文中的View.View.indexOfCurrentElement方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。