Exemplo n.º 1
0
class EventViewer(QDialog):
    def __init__(self, alid=0, db=0, mode=0):
        super().__init__()
        self.font = mainfont
        self.resize(1000, 600)
        layout = QGridLayout()
        self.layout = layout
        self.buttext = []
        self.dictViewer = QWidget()

        self.linedits = {}
        self.last_eid = 0
        self.photo_ids = 0
        self.QLABELM_TYPE = type(QLabelM())

        self.listWidget = QListWidget()
        self.listWidget.itemDoubleClicked.connect(self.doubleClick)
        self.listWidget.itemSelectionChanged.connect(self.itemChanged)

        self.setFont(mainfont)
        self.db = db
        self.alid = alid
        self.event_ids = db.get_all_event_ids(self.alid)
        self.photo_gallery = QLabel()
        #self.photo_gallery.setAlignment(Qt.AlignCenter)
        #self.photo_gallery.setPixmap(QPixmap('f.png').scaled(400,400,Qt.KeepAspectRatio))
        print(self.event_ids)
        if self.event_ids:
            for eid in self.event_ids:
                event = db.get_event_data(self.alid, eid)
                print(event)
                if event:
                    text = ''
                    text = event['event_head'] if type(event) == type(
                        {}) and 'event_head' in event.keys(
                        ) and event['event_head'] != '' else text
                    item = QListWidgetItem(text)
                    if type(event) == type({}) and 'eid' in event.keys():
                        item.setWhatsThis(str(event['eid']))
                    self.listWidget.addItem(item)
            if self.listWidget.count():
                self.listWidget.setCurrentRow(0)

        self.mode = mode

        def openMenu(position):
            # Создание PopupMenu
            menu = QMenu()
            if mode > 0:
                addAction = menu.addAction('Добавить событие')
                #menu.addSeparator()
                editAction = menu.addAction('Переименовать событие')
                #menu.addSeparator()
                delAction = menu.addAction('Удалить событие')
                delAllAction = menu.addAction('Удалить все события')
                menu.addSeparator()
            else:
                addAction, editAction, delAction, delAllAction = QAction(
                ), QAction(), QAction(), QAction()
            quitAction = menu.addAction('Выход')
            action = menu.exec_(self.mapToGlobal(position))

            # Привязка событий к Actions
            if action == addAction:
                text, ok = QInputDialog().getText(self, "Название события",
                                                  "Ввкдите название события:",
                                                  QLineEdit.Normal, '')
                if ok:
                    text = 'Новое событие' if text == '' else text
                    res = self.db.add_event({
                        'alid': self.alid,
                        'event_head': text
                    })
                    if len(res) == 1:
                        event = res[0]
                        text = event['event_head'] if type(event) == type(
                            {}
                        ) and 'event_head' in event.keys(
                        ) and event['event_head'] != '' else 'Новое событие'
                        item = QListWidgetItem(text)
                        item.setWhatsThis(str(event['eid']))
                        self.listWidget.addItem(item)
                        db.events.save()
                        #self.changed = True
                self.event_ids = db.get_all_event_ids(self.alid)

            if action == editAction:
                eid = self.listWidget.currentItem()
                if eid is not None:
                    eid = self.listWidget.currentItem().whatsThis()
                    last_name = self.db.get_event_data(self.alid,
                                                       eid)['event_head']
                    text, ok = QInputDialog().getText(
                        self, "Название события",
                        "Ввкдите новое название события:", QLineEdit.Normal,
                        str(last_name))
                    if ok:
                        event = self.db.edit_event({
                            'alid': self.alid,
                            'eid': eid,
                            'event_head': text
                        })
                        self.listWidget.currentItem().setText(text)
                        self.db.events.save()
                        if event:
                            event = self.db.get_event_data(alid, eid)
                            #b = layout.takeAt(1)
                            self.dictViewer.close()
                            #b.widget().deleteLater()
                            self.dictViewer = DictViewer(
                                event, 1, self.db.events.invizible_fields,
                                self.db.events.editable_fields)
                            self.layout.addWidget(self.dictViewer, 0, 2)
                    self.event_ids = db.get_all_event_ids(self.alid)

            if action == delAction:
                res = QMessageBox.question(
                    self, 'ВНИМАНИЕ!!!',
                    "Вы действительно хотите удалить событие?",
                    QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
                if res == QMessageBox.Yes:
                    eid = self.listWidget.currentItem()
                    if eid is not None:
                        eid = self.listWidget.currentItem().whatsThis()
                        self.db.del_event({'alid': self.alid, 'eid': eid})
                        self.listWidget.takeItem(self.listWidget.currentRow())
                        self.db.events.save()
                        #self.changed = True
                    self.event_ids = db.get_all_event_ids(self.alid)

            if action == delAllAction:
                res = QMessageBox.question(
                    self, 'ВНИМАНИЕ!!!',
                    "Вы действительно хотите удалить все события?",
                    QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
                if res == QMessageBox.Yes:
                    if self.db.del_all_events(int(self.alid)):
                        self.listWidget.clear()
                        self.db.events.save()
                        #self.changed = True
                    self.event_ids = db.get_all_event_ids(self.alid)

                    #db.photos.save()

            if action == quitAction:
                self.accept()

        self.listWidget.setContextMenuPolicy(Qt.CustomContextMenu)
        self.listWidget.customContextMenuRequested.connect(openMenu)

        layout.addWidget(self.listWidget, 0, 0, 1, 1)
        layout.addWidget(self.photo_gallery, 0, 1, 1, 1)

        self.setLayout(layout)

    def doubleClick(self, item):
        if item is not None:
            eid = item.whatsThis()
            text = self.db.get_event_path(self.alid, eid)
            print(eid)

    def closeEvent(self, event):
        if self.last_eid:
            self.check_and_save(self.last_eid)

    def itemChanged(self):
        if self.last_eid:
            self.check_and_save(self.last_eid)

        eid = self.listWidget.currentItem()
        if eid is not None:
            eid = self.listWidget.currentItem().whatsThis()
            event = self.db.get_event_data(self.alid, eid)

            if self.photo_gallery:
                self.photo_gallery.close()

            # Работаем с фотками
            self.photo_gallery = QLabelM(self.alid, self.db, 1, eid)
            self.layout.addWidget(self.photo_gallery, 0, 1,
                                  int(self.db.events.nkeys / 2), 1)

            # Заполняем данными
            self.fill_event_data(event)

            print(event)
            self.last_eid = eid
        self.setFocus()
        #print('item changed = ' + str(eid))

    def keyPressEvent(self, event):
        if event.key() == Qt.Key_Left:
            #print('left pressed')
            if type(self.photo_gallery) == self.QLABELM_TYPE:
                self.photo_gallery.prev_show()
        if event.key() == Qt.Key_Right:
            #print('right pressed')
            if type(self.photo_gallery) == self.QLABELM_TYPE:
                self.photo_gallery.next_show()
        #super().keyPressEvent(event)

    def fill_event_data(self, event):
        if type(event) != type({}):
            return

        self.dictViewer.close()
        self.dictViewer = DictViewer(event, 1, self.db.events.invizible_fields,
                                     self.db.events.editable_fields)
        self.layout.addWidget(self.dictViewer, 0, 2, 1, 1)

    def check_and_save(self, eid):
        print('check_and_save' + str(eid))
        changes = self.dictViewer.get_changes()
        event = self.db.get_event_data(self.alid, eid)

        if self.photo_gallery.changed:
            changes['photo_ids'] = self.photo_gallery.photo_ids
        print(changes)
        if len(changes) > 0 and type(event) == type({}):
            print('event changes saved')
            event.update(changes)
            self.db.edit_event(event)
            self.db.events.save()
Exemplo n.º 2
0
class AlbumViewer(QDialog):
    def __init__(self, pid=0, db=0, mode=0):
        super().__init__()
        self.font = mainfont
        self.resize(500, self.height())
        layout = QGridLayout()
        self.layout = layout
        self.buttext = []
        self.last_alid = 0

        self.listWidget = QListWidget()
        self.listWidget.itemDoubleClicked.connect(self.doubleClick)
        self.listWidget.itemSelectionChanged.connect(self.itemChanged)

        self.dictAlbum = QWidget()
        self.DICTVIEWERTYPE = type(DictViewer())
        self.setFont(mainfont)
        self.db = db
        self.pid = pid
        self.album_ids = db.get_all_albums_ids()
        print(self.album_ids)
        if self.album_ids:
            for alid in self.album_ids:
                album = db.get_album_data(alid)
                print(album)
                self.dictAlbum = DictViewer(album, 1,
                                            self.db.albums.invizible_fields,
                                            self.db.albums.editable_fields)

                if album:
                    text = ''
                    text = album['title'] if type(album) == type(
                        {}) and 'title' in album.keys(
                        ) and album['title'] != '' else text
                    item = QListWidgetItem(text)
                    if type(album) == type({}) and 'alid' in album.keys():
                        item.setWhatsThis(str(album['alid']))
                    self.listWidget.addItem(item)
            self.listWidget.setCurrentRow(0)
        self.mode = mode

        def openMenu(position):
            # Создание PopupMenu
            menu = QMenu()
            openAction = menu.addAction('Открыть альбом')
            openEAction = menu.addAction('Открыть события')
            menu.addSeparator()
            if mode > 0:
                #importAction = menu.addAction('Выгрузить альбом')
                exportAction = menu.addAction('Добавить альбом')
                renameAction = menu.addAction('Переименовать альбом')
                #menu.addSeparator()
                #editAction = menu.addAction('Заменить')
                menu.addSeparator()
                delAction = menu.addAction('Удалить альбом')
                delAllAction = menu.addAction('Удалить все альбомы')
                menu.addSeparator()
            else:
                exportAction, delAction, delAllAction = QAction(), QAction(
                ), QAction()
            quitAction = menu.addAction('Выход')
            action = menu.exec_(self.mapToGlobal(position))

            # Привязка событий к Actions
            if action == openAction:
                self.doubleClick(self.listWidget.currentItem())

            if action == openEAction:
                item = self.listWidget.currentItem()
                if item is not None:
                    alid = item.whatsThis()
                    self.events = EventViewer(int(alid), db, 1)
                    self.events.show()

            if action == exportAction:
                text, ok = QInputDialog().getText(self, "Название альбома",
                                                  "Ввкдите название альбома:",
                                                  QLineEdit.Normal, 'Альбом')
                if ok:
                    res = self.db.add_album({'imid': -1, 'title': text})
                    if len(res) == 1:
                        album = res[0]
                        item = QListWidgetItem(text)
                        item.setWhatsThis(str(album['alid']))
                        self.listWidget.addItem(item)
                self.album_ids = db.get_all_albums_ids()

            if action == renameAction:
                item = self.listWidget.currentItem()
                if item is not None:
                    alid = item.whatsThis()
                    album = db.get_album_data(alid)
                    text, ok = QInputDialog().getText(
                        self, "Название альбома", "Ввкдите название альбома:",
                        QLineEdit.Normal, album['title'])
                    if ok:
                        album = db.edit_album({
                            'alid': int(alid),
                            'imid': -1,
                            'title': text
                        })
                        item.setText(text)
                        if album:
                            album = self.db.get_album_data(alid)
                            # b = layout.takeAt(1)
                            # self.dictAlbum.close()
                            # b.widget().deleteLater()
                            self.dictAlbum = DictViewer(
                                album, 1, self.db.albums.invizible_fields,
                                self.db.albums.editable_fields)
                            self.layout.addWidget(self.dictAlbum, 0, 1)
                        self.album_ids = db.get_all_albums_ids()

            if action == delAction:
                res = QMessageBox.question(
                    self, 'ВНИМАНИЕ!!!',
                    "Вы действительно хотите удалить альбом?",
                    QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
                if res == QMessageBox.Yes:
                    self.delete_item()

            if action == delAllAction:
                res = QMessageBox.question(
                    self, 'ВНИМАНИЕ!!!',
                    "Вы действительно хотите удалить все альбомы?",
                    QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
                if res == QMessageBox.Yes:
                    item = self.listWidget.currentItem()
                    while item is not None:
                        self.delete_item()
                        item = self.listWidget.currentItem()

                    self.album_ids = db.get_all_albums_ids()

            if action == quitAction:
                self.accept()

        self.setContextMenuPolicy(Qt.CustomContextMenu)
        self.customContextMenuRequested.connect(openMenu)

        layout.addWidget(self.listWidget, 0, 0)
        layout.addWidget(self.dictAlbum, 0, 1)

        self.setLayout(layout)

    def delete_item(self):
        item = self.listWidget.currentItem()
        if item is not None:

            alid = item.whatsThis()
            self.db.del_album(int(alid))
            self.listWidget.takeItem(self.listWidget.currentRow())
            self.album_ids = self.db.get_all_albums_ids()

    def itemChanged(self):
        if self.last_alid:
            self.check_and_save(self.last_alid)

        item = self.listWidget.currentItem()
        if item is not None:
            alid = item.whatsThis()

            album = self.db.get_album_data(alid)
            self.dictAlbum = DictViewer(album, 1,
                                        self.db.albums.invizible_fields,
                                        self.db.albums.editable_fields)
            self.layout.addWidget(self.dictAlbum, 0, 1)
            self.last_alid = alid
        self.setFocus()

    def doubleClick(self, item):
        if item is not None:
            alid = item.whatsThis()
            print(alid)
            self.viewer = QLabelMA(alid, self.db, 1)
            self.viewer.show()

    def check_and_save(self, alid):
        changes = self.dictAlbum.get_changes()
        album = self.db.get_album_data(alid)

        print(changes)
        if len(changes) > 0 and type(album) == type({}):
            print('album changes saved')
            album.update(changes)
            self.db.edit_album(album)