def order_widget(self, name, prefs=None): prefs = prefs or tprefs widget = QListWidget(self) widget.addItems(prefs.defaults[name]) widget.setDragEnabled(True) widget.setDragDropMode(widget.InternalMove) widget.viewport().setAcceptDrops(True) widget.setDropIndicatorShown(True) widget.indexesMoved.connect(self.emit_changed) widget.setDefaultDropAction(Qt.MoveAction) widget.setMovement(widget.Snap) widget.setSpacing(5) widget.defaults = prefs.defaults[name] def getter(w): return list(map(unicode, (w.item(i).text() for i in xrange(w.count())))) def setter(w, val): order_map = {x:i for i, x in enumerate(val)} items = list(w.defaults) limit = len(items) items.sort(key=lambda x:order_map.get(x, limit)) w.clear() for x in items: i = QListWidgetItem(w) i.setText(x) i.setFlags(i.flags() | Qt.ItemIsDragEnabled) return self(name, widget=widget, getter=getter, setter=setter, prefs=prefs)
class SelectNames(QDialog): # {{{ def __init__(self, names, txt, parent=None): QDialog.__init__(self, parent) self.l = l = QVBoxLayout(self) self.setLayout(l) self.la = la = QLabel(_('Create a Virtual Library based on %s') % txt) l.addWidget(la) self._names = QListWidget(self) self._names.addItems(QStringList(sorted(names, key=sort_key))) self._names.setSelectionMode(self._names.ExtendedSelection) l.addWidget(self._names) self._and = QCheckBox(_('Match all selected %s names')%txt) l.addWidget(self._and) self.bb = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel) self.bb.accepted.connect(self.accept) self.bb.rejected.connect(self.reject) l.addWidget(self.bb) self.resize(self.sizeHint()) @property def names(self): for item in self._names.selectedItems(): yield unicode(item.data(Qt.DisplayRole).toString()) @property def match_type(self): return ' and ' if self._and.isChecked() else ' or '
def order_widget(self, name, prefs=None): prefs = prefs or tprefs widget = QListWidget(self) widget.addItems(prefs.defaults[name]) widget.setDragEnabled(True) widget.setDragDropMode(widget.InternalMove) widget.viewport().setAcceptDrops(True) widget.setDropIndicatorShown(True) widget.indexesMoved.connect(self.emit_changed) widget.setDefaultDropAction(Qt.MoveAction) widget.setMovement(widget.Snap) widget.setSpacing(5) widget.defaults = prefs.defaults[name] def getter(w): return list( map(unicode, (w.item(i).text() for i in xrange(w.count())))) def setter(w, val): order_map = {x: i for i, x in enumerate(val)} items = list(w.defaults) limit = len(items) items.sort(key=lambda x: order_map.get(x, limit)) w.clear() for x in items: i = QListWidgetItem(w) i.setText(x) i.setFlags(i.flags() | Qt.ItemIsDragEnabled) return self(name, widget=widget, getter=getter, setter=setter, prefs=prefs)
class SelectNames(QDialog): # {{{ def __init__(self, names, txt, parent=None): QDialog.__init__(self, parent) self.l = l = QVBoxLayout(self) self.setLayout(l) self.la = la = QLabel(_("Create a Virtual Library based on %s") % txt) l.addWidget(la) self._names = QListWidget(self) self._names.addItems(QStringList(sorted(names, key=sort_key))) self._names.setSelectionMode(self._names.ExtendedSelection) l.addWidget(self._names) self._or = QRadioButton(_("Match any of the selected %s names") % txt) self._and = QRadioButton(_("Match all of the selected %s names") % txt) self._or.setChecked(True) l.addWidget(self._or) l.addWidget(self._and) self.bb = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel) self.bb.accepted.connect(self.accept) self.bb.rejected.connect(self.reject) l.addWidget(self.bb) self.resize(self.sizeHint()) @property def names(self): for item in self._names.selectedItems(): yield unicode(item.data(Qt.DisplayRole).toString()) @property def match_type(self): return " and " if self._and.isChecked() else " or "
class tab_widget(QtGui.QTabWidget): # Initiate Widget. def __init__(self, parent=MainWindow): super(tab_widget, self).__init__() self.initUI() # Create all object thats in the AddData tab def makeAddDataTab(self, grid): self.author_label = QLabel("Author") self.author_text = QLineEdit() self.author_text.setPlaceholderText("Author") grid.addWidget(self.author_label, 0, 0) grid.addWidget(self.author_text, 1, 0) self.title_label = QLabel("Title") self.title_text = QLineEdit() self.title_text.setPlaceholderText("Title") grid.addWidget(self.title_label, 0, 1) grid.addWidget(self.title_text, 1, 1) self.date_label = QLabel("Date Read") self.date_text = QLineEdit() self.date_text.setPlaceholderText("yy-mm-dd") grid.addWidget(self.date_label, 2, 1) grid.addWidget(self.date_text, 3, 1) self.grade_label = QLabel("Grade") self.grade_text = QLineEdit() self.grade_text.setPlaceholderText("Grade 1-5") grid.addWidget(self.grade_label, 2, 0) grid.addWidget(self.grade_text, 3, 0) self.comments_label = QLabel("Comments") self.comments_text = QLineEdit() self.comments_text.setPlaceholderText("Comments here") grid.addWidget(self.comments_label, 4, 0) grid.addWidget(self.comments_text, 5, 0) self.genre_label = QLabel("Genre") self.genre_list = QListWidget(self) self.genre_list.addItems(self.genreList) self.genre_list.setObjectName('genre_list') self.genre_list.itemClicked.connect(self.item_clicked) grid.addWidget(self.genre_label, 6, 0) grid.addWidget(self.genre_list, 7, 0) self.genre2_label = QLabel("Genre2") self.genre2_list = QListWidget(self) self.genre2_list.setObjectName("genre2") self.genre2_list.addItem("Select genre first") grid.addWidget(self.genre2_label, 6, 1) grid.addWidget(self.genre2_list, 7, 1) self.submit = QtGui.QPushButton("Submit Data", self) self.submit.clicked.connect(self.buttonClicked) grid.addWidget(self.submit, 8, 1) self.quit = QtGui.QPushButton("Exit", self) self.quit.clicked.connect(self.buttonClicked) self.quit.setMaximumWidth(80) grid.addWidget(self.quit, 8, 0) # Creates all the object thats in the getData tab def makeGetDataTab(self, vbox_inner): self.authorData = QLineEdit() self.authorData.setPlaceholderText("Author") self.authorData.setReadOnly(True) vbox_inner.addWidget(self.authorData) self.titleData = QLineEdit() self.titleData.setPlaceholderText("Title") self.titleData.setReadOnly(True) vbox_inner.addWidget(self.titleData) self.genreData = QLineEdit() self.genreData.setPlaceholderText("Genre") self.genreData.setReadOnly(True) vbox_inner.addWidget(self.genreData) self.genre2Data = QLineEdit() self.genre2Data.setPlaceholderText("Genre2") self.genre2Data.setReadOnly(True) vbox_inner.addWidget(self.genre2Data) self.dateData = QLineEdit() self.dateData.setPlaceholderText("Date read") self.dateData.setReadOnly(True) vbox_inner.addWidget(self.dateData) self.gradeData = QLineEdit() self.gradeData.setPlaceholderText("Grade") self.gradeData.setReadOnly(True) vbox_inner.addWidget(self.gradeData) self.commentsData = QLineEdit() self.commentsData.setPlaceholderText("Comments") self.commentsData.setReadOnly(True) vbox_inner.addWidget(self.commentsData) self.titles_list = QListWidget(self) vbox_inner.addWidget(self.titles_list) hbox_inner = QtGui.QHBoxLayout() self.getTitleData = QtGui.QPushButton("Get title data", self) self.getTitleData.clicked.connect(self.buttonClicked) hbox_inner.addWidget(self.getTitleData) self.refreshList = QtGui.QPushButton("Refresh list", self) self.refreshList.clicked.connect(self.buttonClicked) hbox_inner.addWidget(self.refreshList) return hbox_inner # Initiate the widgets gui def initUI(self): vbox_inner = QVBoxLayout() grid = QtGui.QGridLayout() self.genreList = ['Fiction', 'Fact book', 'Poetry'] # Tab 1: Add Data self.makeAddDataTab(grid) # Tab 2:Get Data from file hbox_inner = self.makeGetDataTab(vbox_inner) tab1 = QWidget() tab1.setLayout(grid) vbox_inner.addLayout(hbox_inner) tab2 = QWidget() tab2.setLayout(vbox_inner) self.addTab(tab1, "Add Book Data") self.addTab(tab2, "Get Book Data") self.move(150, 150) self.show() # Function for when a QListWidgetItem is clicked if the genre_list is # clicked, will check which genre was chosen and populate genre2_list # with subgenres of that genre. def item_clicked(self): genre = self.sender() if genre.objectName() == "genre_list": curr = genre.currentItem().text() if curr == self.genreList[0]: genre2List = ["Detective", "autobiography", "Horror", "Comedy"] elif curr == self.genreList[1]: genre2List = ["Technology", "Biology", "Chemistry", "Mathematics"] elif curr == self.genreList[2]: genre2List = ["Tirade", "Ballads", "Rhyme"] else: genre2List = ["None"] self.genre2_list.clear() self.genre2_list.addItems(genre2List) # Function for when a button is clicked, if 'Submit Data' is clicked, # will check that all input is correct format and if so write # the data to the file. If the button pressed is 'Get title data' # will get the selected title in the QListWidget and show the row data def buttonClicked(self): sender = self.sender() if sender.text() == "Refresh list": if self.parent().dialog.isHidden(): titles = getAllTitles(self.parent()) self.titles_list.clear() if titles != "": self.titles_list.addItems(titles) else: QMessageBox.warning(self.parent(), "Failure", "Connect to the server before refreshing.") if sender.text() == "Exit": if self.parent().dialog.isHidden(): self.parent().disconnect() sys.exit(0) elif sender.text() == "Get title data": if self.titles_list.currentItem() != None: if self.parent().dialog.isHidden(): titleToGet = str(self.titles_list.currentItem().text()) if titleToGet != "": titleData = getTitleData(self.parent(), titleToGet) splitLine = [] splitLine = titleData.split(",", 7) if titleData != "Empty": self.authorData.setText(splitLine[0].replace('"', "").lstrip()) self.titleData.setText(splitLine[1].replace('"', "").lstrip()) self.genreData.setText(splitLine[2].replace('"', "").lstrip()) self.genre2Data.setText(splitLine[3].replace('"', "").lstrip()) self.dateData.setText(splitLine[4].replace('"', "").lstrip()) self.gradeData.setText(splitLine[5].replace('"', "").lstrip()) self.commentsData.setText(splitLine[6].replace('"', "").lstrip()) else: QMessageBox.warning(self, "Failed", "Connect to server first") self.dialog.setWindowTitle("Book Library (Disconnected)") elif sender.text() == "Submit Data": while self.parent().dialog.isHidden(): author = str(self.author_text.text()) if containsDigit(author) == True: QMessageBox.warning(self, "Wrong input", "Error: Author cant contain digits") break if len(author) == 0: QMessageBox.warning(self, "Wrong input", "Author is a required field") break title = str(self.title_text.text()) try: title = str(self.title_text.text()) except: QMessageBox.warning(self, "Wrong input", "Error in comments, only A-z, 1-9 allowed") break if len(title) == 0: QMessageBox.warning(self, "Wrong input", "Title is a required field") break if self.genre_list.currentItem() != None: genre = str(self.genre_list.currentItem().text()) else: QMessageBox.warning(self, "Wrong input", "Error: Need to pick genre") break if self.genre2_list.currentItem() != None: genre2 = str(self.genre2_list.currentItem().text()) else: QMessageBox.warning(self, "Wrong input", "Error: Need to pick genre2") break d = str(self.date_text.text()) if len(d) == 0: QMessageBox.warning(self, "Wrong input", "Date is a required field") break try: dateRead = datetime.datetime.strptime(d, '%y-%m-%d').date() except: QMessageBox.warning(self, "Wrong input", "Error: Date needs to be in format: yy-mm-dd") break try: grade = int(str(self.grade_text.text())) except: QMessageBox.warning(self, "Wrong input", "Error: Grade needs to be a number between 1-5") break if grade > 5 or grade < 1: QMessageBox.warning(self, "Wrong input", "Error: Grade needs to be a number between 1-5") break try: comments = str(self.comments_text.text()) except: QMessageBox.warning(self, "Wrong input", "Error: in comments, only A-z, 1-9 allowed") break if len(comments) == 0: comments = "No comments" self.parent().sendBookData(author, title, genre, genre2, str(dateRead), str(grade), comments) break
def addItems(self, *args): try: return QListWidget.addItems(self, *args) finally: self.mark_as_editable()
class tab_widget(QtGui.QTabWidget): # Initiate Widget. def __init__(self, parent=MainWindow): super(tab_widget, self).__init__() self.initUI() # Create interface, and connect buttons to signals. def initUI(self): vbox_inner = QVBoxLayout() grid = QtGui.QGridLayout() self.genreList = ['Fiction', 'Fact book', 'Poetry'] # Tab 1: Add Data self.author_label = QLabel("Author") self.author_text = QLineEdit() self.author_text.setPlaceholderText("Author") grid.addWidget(self.author_label, 0, 0) grid.addWidget(self.author_text, 1, 0) self.title_label = QLabel("Title") self.title_text = QLineEdit() self.title_text.setPlaceholderText("Title") grid.addWidget(self.title_label, 0, 1) grid.addWidget(self.title_text, 1, 1) self.date_label = QLabel("Date Read") self.date_text = QLineEdit() self.date_text.setPlaceholderText("yy-mm-dd") grid.addWidget(self.date_label, 2, 1) grid.addWidget(self.date_text, 3, 1) self.grade_label = QLabel("Grade") self.grade_text = QLineEdit() self.grade_text.setPlaceholderText("Grade 1-5") grid.addWidget(self.grade_label, 2, 0) grid.addWidget(self.grade_text, 3, 0) self.comments_label = QLabel("Comments") self.comments_text = QLineEdit() self.comments_text.setPlaceholderText("Comments here") grid.addWidget(self.comments_label, 4, 0) grid.addWidget(self.comments_text, 5, 0) self.genre_label = QLabel("Genre") self.genre_list = QListWidget(self) self.genre_list.addItems(self.genreList) self.genre_list.setObjectName('genre_list') self.genre_list.itemClicked.connect(self.item_clicked) grid.addWidget(self.genre_label, 6, 0) grid.addWidget(self.genre_list, 7, 0) self.genre2_label = QLabel("Genre2") self.genre2_list = QListWidget(self) self.genre2_list.setObjectName("genre2") self.genre2_list.addItem("Select genre first") grid.addWidget(self.genre2_label, 6, 1) grid.addWidget(self.genre2_list, 7, 1) # Tab 2:Get Data from file self.authorData = QLineEdit() self.authorData.setPlaceholderText("Author") self.authorData.setReadOnly(True) vbox_inner.addWidget(self.authorData) self.titleData = QLineEdit() self.titleData.setPlaceholderText("Title") self.titleData.setReadOnly(True) vbox_inner.addWidget(self.titleData) self.genreData = QLineEdit() self.genreData.setPlaceholderText("Genre") self.genreData.setReadOnly(True) vbox_inner.addWidget(self.genreData) self.genre2Data = QLineEdit() self.genre2Data.setPlaceholderText("Genre2") self.genre2Data.setReadOnly(True) vbox_inner.addWidget(self.genre2Data) self.dateData = QLineEdit() self.dateData.setPlaceholderText("Date read") self.dateData.setReadOnly(True) vbox_inner.addWidget(self.dateData) self.gradeData = QLineEdit() self.gradeData.setPlaceholderText("Grade") self.gradeData.setReadOnly(True) vbox_inner.addWidget(self.gradeData) self.commentsData = QLineEdit() self.commentsData.setPlaceholderText("Comments") self.commentsData.setReadOnly(True) vbox_inner.addWidget(self.commentsData) self.titles_list = QListWidget(self) self.titles_list.addItems(getTitlesFromFile()) vbox_inner.addWidget(self.titles_list) self.getTitleData = QtGui.QPushButton("Get title data", self) self.getTitleData.clicked.connect(self.buttonClicked) vbox_inner.addWidget(self.getTitleData) self.submit = QtGui.QPushButton("Submit Data", self) self.submit.clicked.connect(self.buttonClicked) grid.addWidget(self.submit, 8, 1) self.quit = QtGui.QPushButton("Exit", self) self.quit.clicked.connect(QtCore.QCoreApplication.instance().quit) self.quit.setMaximumWidth(80) grid.addWidget(self.quit, 8, 0) tab1 = QWidget() tab1.setLayout(grid) tab2 = QWidget() tab2.setLayout(vbox_inner) self.addTab(tab1, "Add Data") self.addTab(tab2, "Get Data") self.move(150, 150) self.show() # Function for when a QListWidgetItem is clicked if the genre_list is # clicked, will check which genre was chosen and populate genre2_list # with subgenres of that genre. def item_clicked(self): genre = self.sender() if genre.objectName() == "genre_list": curr = genre.currentItem().text() if curr == self.genreList[0]: genre2List = ["Detective", "autobiograpyh", "Horror", "Comedy"] elif curr == self.genreList[1]: genre2List = ["Technology", "Biology", "Chemistry", "Mathematics"] elif curr == self.genreList[2]: genre2List = ["Tirade", "Ballads", "Rhyme"] else: genre2List = ["None"] self.genre2_list.clear() self.genre2_list.addItems(genre2List) # Function for when a button is clicked, if 'Submit Data' is clicked, # will check that all input is correct format and if so write # the data to the file. If the button pressed is 'Get title data' # will get the selected title in the QListWidget and show the row data def buttonClicked(self): sender = self.sender() if sender.text() == "Get title data": if self.titles_list.currentItem() != None: titleToGet = str(self.titles_list.currentItem().text()) titleData = findTitleData(titleToGet) splitLine = [] splitLine = titleData.split(",", 7) self.authorData.setText('Author: '+splitLine[0].replace('"', "").lstrip()) self.titleData.setText('Title: '+splitLine[1].replace('"', "").lstrip()) self.genreData.setText('Genre: '+splitLine[2].replace('"', "").lstrip()) self.genre2Data.setText('Genre2: '+splitLine[3].replace('"', "").lstrip()) self.dateData.setText('Date Read: '+splitLine[4].replace('"', "").lstrip()) self.gradeData.setText('Grade: '+splitLine[5].replace('"', "").lstrip()) self.commentsData.setText('Comments: '+splitLine[6].replace('"', "").lstrip()) elif sender.text() == "Submit Data": comments = str(self.comments_text.text()) while True: author = str(self.author_text.text()) if containsDigit(author) == True: QMessageBox.about(self, "Wrong input", "Error: Author cant contain digits") break if len(author) == 0: QMessageBox.about(self, "Wrong input", "Author is a required field") break title = str(self.title_text.text()) try: title = str(self.title_text.text()) except: QMessageBox.about(self, "Wrong input", "Error in comments, only A-z, 1-9 allowed") break if len(title) == 0: QMessageBox.about(self, "Wrong input", "Title is a required field") break if self.genre_list.currentItem() != None: genre = str(self.genre_list.currentItem().text()) else: QMessageBox.about(self, "Wrong input", "Error: Need to pick genre") break if self.genre2_list.currentItem() != None: genre2 = str(self.genre2_list.currentItem().text()) else: QMessageBox.about(self, "Wrong input", "Error: Need to pick genre2") break d = str(self.date_text.text()) if len(d) == 0: QMessageBox.about(self, "Wrong input", "Date is a required field") break try: dateRead = datetime.datetime.strptime(d, '%y-%m-%d').date() except: QMessageBox.about(self, "Wrong input", "Error: Date needs to be in format: yy-mm-dd") break try: grade = int(str(self.grade_text.text())) except: QMessageBox.about(self, "Wrong input", "Error: Grade needs to be a number between 1-5") break if grade > 5 or grade < 1: QMessageBox.about(self, "Wrong input", "Error: Grade needs to be a number between 1-5") break try: comments = str(self.comments_text.text()) except: QMessageBox.about(self, "Wrong input", "Error: in comments, only A-z, 1-9 allowed") break if len(comments) == 0: comments = "No comments" writeDataToFile(author, title, genre, genre2, str(dateRead), str(grade), comments) self.titles_list.clear() self.titles_list.addItems(getTitlesFromFile()) QMessageBox.about(self, "Information", "Data added successfully") break