Beispiel #1
0
    def build_login_menu(self):
        layout = QVBoxLayout()

        layout.addLayout(
            self.create_field(self.username_login_input, self.tr('Username')))

        self.password_login_input.setEchoMode(QLineEdit.Password)
        layout.addLayout(
            self.create_field(self.password_login_input, self.tr('Password')))

        self.remember_login_check_box.setChecked(True)
        layout.addLayout(self.create_field(self.remember_login_check_box))

        btn = PushButton(self.tr('Login'), 70, 30, self.login_click)
        btn.setFixedWidth(100)
        layout.addWidget(btn, alignment=Qt.AlignCenter)

        return layout, self.tr('Login')
Beispiel #2
0
    def build_account_info_menu(self):
        user_data = self.cloud.user()
        layout = QVBoxLayout()

        header_layout = QVBoxLayout()
        header_layout.setContentsMargins(0, 0, 0, 10)

        username_lbl = QLabel('{}'.format(user_data['username']))
        username_lbl.setFont(QFont('SansSerif', 18))
        header_layout.addWidget(username_lbl, alignment=Qt.AlignCenter)
        layout.addLayout(header_layout)

        header_layout.addWidget(QLabel('{}'.format(user_data['email'])),
                                alignment=Qt.AlignCenter)

        btn = PushButton(self.tr('Logout'), 70, 30, self.logout_click)
        btn.setFixedWidth(100)
        layout.addWidget(btn, alignment=Qt.AlignCenter)

        return layout, user_data['username']
Beispiel #3
0
	def setup_ui(self):
		content = QVBoxLayout()
		content.addWidget(QLabel('{}:'.format(self.tr('Title'))), alignment=Qt.AlignLeft)
		content.addWidget(self.title_input)
		content.addWidget(QLabel('{}:'.format(self.tr('Description (optional)'))), alignment=Qt.AlignLeft)
		content.addWidget(self.description_input)
		content.addWidget(QLabel('{}:'.format(self.tr('Date'))), alignment=Qt.AlignLeft)
		content.addWidget(self.date_input)
		content.addWidget(QLabel('{}:'.format(self.tr('Time'))), alignment=Qt.AlignLeft)
		content.addWidget(self.time_input)
		self.repeat_weekly_input.setChecked(False)
		content.addWidget(self.repeat_weekly_input)

		buttons = QHBoxLayout()
		buttons.setAlignment(Qt.AlignRight | Qt.AlignBottom)
		buttons.addWidget(PushButton(self.tr('Save'), 90, 30, self.save_event_click), 0, Qt.AlignRight)
		self.del_btn = PushButton(self.tr('Delete'), 90, 30, self.delete_event)
		buttons.addWidget(self.del_btn)
		buttons.addWidget(PushButton(self.tr('Cancel'), 90, 30, self.close), 0, Qt.AlignRight)
		content.addLayout(buttons)

		self.setLayout(content)
Beispiel #4
0
    def setup_signup_ui(self, tabs):
        tab = QWidget(flags=tabs.windowFlags())
        layout = QVBoxLayout()

        layout.addLayout(
            self.create_field(self.username_signup_input, self.tr('Username')))

        layout.addLayout(
            self.create_field(self.email_signup_input, self.tr('Email')))

        btn = PushButton(self.tr('Register'), 150, 30, self.signup_click)
        layout.addWidget(btn, alignment=Qt.AlignCenter)

        tab.setLayout(layout)
        tabs.addTab(tab, self.tr('New account'))
Beispiel #5
0
	def __init__(self, **kwargs):
		self.settings = Settings()
		super().__init__(None, Qt.WindowFlags())

		self.window().setWindowTitle(APP_NAME)
		self.resize(self.settings.app_size)
		self.move(self.settings.app_pos)
		self.setWindowIcon(self.settings.app_icon())
		self.setMinimumWidth(APP_MIN_WIDTH)
		self.setMinimumHeight(APP_MIN_HEIGHT)

		self.events_list = EventListWidget(**{
			'parent': self,
		})

		# noinspection PyUnresolvedReferences
		self.events_list.itemSelectionChanged.connect(self.events_list_selection_changed)

		self.calendar = self.init_calendar()

		self.btn_new_event = PushButton(self.tr('New'), 90, 30, self.calendar.open_details_event)
		self.btn_edit = PushButton(self.tr('Details'), 90, 30, self.calendar.edit_event_click)
		self.btn_delete = PushButton(self.tr('Delete'), 90, 30, self.calendar.delete_event_click)
		events_widget = self.init_events_widget()

		main_layout = QHBoxLayout()

		# noinspection PyArgumentList
		main_layout.addWidget(self.calendar)
		main_layout.addWidget(events_widget, alignment=Qt.AlignRight)

		central_widget = QWidget(flags=self.windowFlags())
		central_widget.setLayout(main_layout)

		self.setCentralWidget(central_widget)

		# noinspection PyUnresolvedReferences
		self.calendar.selectionChanged.connect(self.date_selection_changed)

		self.setup_navigation_menu()
		self.setFont(QFont('SansSerif', self.settings.app_font))

		self.open_action = QAction('{} {}'.format(self.tr('Open'), APP_NAME), self)
		self.hide_action = QAction(self.tr('Minimize To Tray'), self)
		if self.settings.start_in_tray:
			self.hide_action.setEnabled(False)
		self.close_action = QAction('{} {}'.format(self.tr('Quit'), APP_NAME), self)

		self.tray_icon = self.init_tray(kwargs.get('app'))
		self.tray_icon.show()

		self.setPalette(self.settings.app_theme)
Beispiel #6
0
class MainWindow(QMainWindow):

	def __init__(self, **kwargs):
		self.settings = Settings()
		super().__init__(None, Qt.WindowFlags())

		self.window().setWindowTitle(APP_NAME)
		self.resize(self.settings.app_size)
		self.move(self.settings.app_pos)
		self.setWindowIcon(self.settings.app_icon())
		self.setMinimumWidth(APP_MIN_WIDTH)
		self.setMinimumHeight(APP_MIN_HEIGHT)

		self.events_list = EventListWidget(**{
			'parent': self,
		})

		# noinspection PyUnresolvedReferences
		self.events_list.itemSelectionChanged.connect(self.events_list_selection_changed)

		self.calendar = self.init_calendar()

		self.btn_new_event = PushButton(self.tr('New'), 90, 30, self.calendar.open_details_event)
		self.btn_edit = PushButton(self.tr('Details'), 90, 30, self.calendar.edit_event_click)
		self.btn_delete = PushButton(self.tr('Delete'), 90, 30, self.calendar.delete_event_click)
		events_widget = self.init_events_widget()

		main_layout = QHBoxLayout()

		# noinspection PyArgumentList
		main_layout.addWidget(self.calendar)
		main_layout.addWidget(events_widget, alignment=Qt.AlignRight)

		central_widget = QWidget(flags=self.windowFlags())
		central_widget.setLayout(main_layout)

		self.setCentralWidget(central_widget)

		# noinspection PyUnresolvedReferences
		self.calendar.selectionChanged.connect(self.date_selection_changed)

		self.setup_navigation_menu()
		self.setFont(QFont('SansSerif', self.settings.app_font))

		self.open_action = QAction('{} {}'.format(self.tr('Open'), APP_NAME), self)
		self.hide_action = QAction(self.tr('Minimize To Tray'), self)
		if self.settings.start_in_tray:
			self.hide_action.setEnabled(False)
		self.close_action = QAction('{} {}'.format(self.tr('Quit'), APP_NAME), self)

		self.tray_icon = self.init_tray(kwargs.get('app'))
		self.tray_icon.show()

		self.setPalette(self.settings.app_theme)

	def closeEvent(self, event):
		event.ignore()
		self.hide()

	def date_selection_changed(self):
		if self.calendar.selectedDate().toPyDate() < datetime.today().date():
			self.btn_new_event.setEnabled(False)
		else:
			self.btn_new_event.setEnabled(True)

	def save_state(self):
		self.settings.autocommit(False)
		self.settings.set_pos(self.pos())
		self.settings.set_size(self.size())
		self.settings.commit()

	def quit_app(self):
		self.save_state()
		qApp.quit()

	def init_tray(self, app):
		actions = {
			self.open_action: self.show,
			self.hide_action: self.hide,
			self.close_action: self.quit_app
		}
		tray_menu = QMenu()
		for key, value in actions.items():

			# noinspection PyUnresolvedReferences
			key.triggered.connect(value)
			tray_menu.addAction(key)
		tray_icon = QSystemTrayIcon(self.settings.app_icon(), app)
		tray_icon.setContextMenu(tray_menu)
		return tray_icon

	def init_calendar(self):
		calendar = CalendarWidget(
			self,
			width=self.width() - 400,
			height=self.height(),
			events_list=self.events_list
		)
		calendar.setLocale(QLocale(AVAILABLE_LOCALES[self.settings.app_lang]))
		calendar.setFirstDayOfWeek(Qt.Monday)
		calendar.setSelectedDate(datetime.now())
		return calendar

	def init_events_widget(self):
		scroll_view = QScrollArea()
		scroll_view.setWidget(self.events_list)
		scroll_view.setWidgetResizable(True)
		scroll_view.setFixedWidth(400)

		layout = QVBoxLayout()

		# noinspection PyArgumentList
		layout.addWidget(scroll_view)

		buttons = QHBoxLayout()
		buttons.setAlignment(Qt.AlignRight | Qt.AlignBottom)

		buttons.addWidget(self.btn_new_event, 0, Qt.AlignLeft)

		self.btn_edit.setEnabled(False)
		buttons.addWidget(self.btn_edit, 0, Qt.AlignCenter)

		self.btn_delete.setEnabled(False)
		buttons.addWidget(self.btn_delete, 0, Qt.AlignRight)

		layout.addLayout(buttons)

		widget = QWidget(flags=self.windowFlags())
		widget.setLayout(layout)
		return widget

	def events_list_selection_changed(self):
		if self.events_list.selected_item is not None:
			if len(self.events_list.selected_ids()) == 1:
				self.btn_edit.setEnabled(True)
			else:
				self.btn_edit.setEnabled(False)
			self.btn_delete.setEnabled(True)
		else:
			self.btn_edit.setEnabled(False)
			self.btn_delete.setEnabled(False)

	def hide(self):
		self.hide_action.setEnabled(False)
		self.open_action.setEnabled(True)
		super(MainWindow, self).hide()

	def show(self):
		self.hide_action.setEnabled(True)
		self.open_action.setEnabled(False)
		super(MainWindow, self).show()

	def setup_navigation_menu(self):
		main_menu = self.menuBar()
		self.setup_file_menu(main_menu)
		self.setup_help_menu(main_menu)

	@staticmethod
	def create_action(target, title, fn, shortcut=None, tip=None, icon=None):
		action = QAction(title, target)
		if shortcut:
			action.setShortcut(shortcut)
		if tip:
			action.setStatusTip(tip)
		if icon:
			action.setIcon(icon)

		# noinspection PyUnresolvedReferences
		action.triggered.connect(fn)
		return action

	def create_shortcut(self):
		try:
			shortcut_icon.create()
			info(self, self.tr('Shortcut icon has been created'))
		except ShortcutIconIsNotSupportedError:
			error(self, self.tr('Shortcut icon is not supported on {} by application').format(system.name()))
		except Exception as exc:
			logger.error(log_msg('Unable to create shortcut icon: {}'.format(exc)))
			error(self, self.tr('Unable to create shortcut icon'))

	def setup_file_menu(self, main_menu):
		file_menu = main_menu.addMenu('&{}'.format(self.tr('File')))
		file_menu.addAction(
			self.create_action(
				self,
				'{}...'.format(self.tr('New Event')),
				self.calendar.open_details_event,
				'Ctrl+N'
			)
		)
		file_menu.addAction(
			self.create_action(
				self,
				'{}...'.format(self.tr('Se{}ttings').format('&')),
				self.calendar.open_settings,
				'Ctrl+Alt+S',
				icon=qta.icon('mdi.settings')
			)
		)
		file_menu.addAction(self.create_action(
			self,
			'&{}'.format(self.tr('Create shortcut icon...')),
			self.create_shortcut,
			icon=qta.icon('mdi.desktop-mac')
		))
		file_menu.addAction(
			self.create_action(
				self, '{}...'.format(self.tr('Backup and Restore')),
				self.calendar.open_backup_and_restore,
				'Ctrl+Alt+B',
				icon=qta.icon('mdi.backup-restore')
			)
		)

	def setup_help_menu(self, main_menu):
		help_menu = main_menu.addMenu('&{}'.format(self.tr('Help')))
		help_menu.addAction(self.create_action(
			self, '&{}...'.format(self.tr('Account')),
			self.calendar.open_account_info,
			icon=qta.icon('mdi.account-circle')
		))
		help_menu.addAction(self.create_action(
			self, '&{}'.format(self.tr('About')), self.calendar.open_about, icon=qta.icon('mdi.information-outline')
		))
Beispiel #7
0
    def __init__(self, flags, *args, **kwargs):
        super(BackupDialog, self).__init__(flags=flags, *args)

        if 'palette' in kwargs:
            self.setPalette(kwargs.get('palette'))
        if 'font' in kwargs:
            self.setFont(kwargs.get('font'))
        self.setWindowFlags(Qt.Dialog)

        self.spinner = WaitingSpinner()

        self.thread_pool = QThreadPool()

        self.calendar = kwargs.get('calendar', None)
        if self.calendar is None:
            raise RuntimeError('BackupDialog: calendar is not set')

        self.storage = kwargs.get('storage', Storage())
        self.cloud = kwargs.get('cloud_storage', CloudStorage())

        self.setFixedSize(500, 320)
        self.setWindowTitle(self.tr('Backup and Restore'))

        self.backups_pool = []

        self.settings = Settings()

        self.backup_file_input = QLineEdit()
        self.restore_file_input = QLineEdit()

        self.backup_file_button = PushButton('', 40, 30, self.get_folder_path)
        self.restore_file_button = PushButton('', 40, 30, self.get_file_path)

        self.launch_restore_button = PushButton(self.tr('Start'), 120, 35,
                                                self.launch_restore_local)
        self.launch_backup_button = PushButton(self.tr('Start'), 120, 35,
                                               self.launch_backup_local)

        self.backups_cloud_list_widget = QListWidget()

        self.upload_backup_button = PushButton(' {}'.format(self.tr('Upload')),
                                               120, 35,
                                               self.upload_backup_cloud)
        self.download_backup_button = PushButton(
            ' {}'.format(self.tr('Download')),
            150 if self.settings.app_lang == 'uk_UA' else 120, 35,
            self.download_backup_cloud)
        self.delete_backup_button = PushButton(' {}'.format(self.tr('Delete')),
                                               120, 35,
                                               self.delete_backup_cloud)

        self.setup_ui()

        self.layout().addWidget(self.spinner)
Beispiel #8
0
class BackupDialog(QDialog):
    def __init__(self, flags, *args, **kwargs):
        super(BackupDialog, self).__init__(flags=flags, *args)

        if 'palette' in kwargs:
            self.setPalette(kwargs.get('palette'))
        if 'font' in kwargs:
            self.setFont(kwargs.get('font'))
        self.setWindowFlags(Qt.Dialog)

        self.spinner = WaitingSpinner()

        self.thread_pool = QThreadPool()

        self.calendar = kwargs.get('calendar', None)
        if self.calendar is None:
            raise RuntimeError('BackupDialog: calendar is not set')

        self.storage = kwargs.get('storage', Storage())
        self.cloud = kwargs.get('cloud_storage', CloudStorage())

        self.setFixedSize(500, 320)
        self.setWindowTitle(self.tr('Backup and Restore'))

        self.backups_pool = []

        self.settings = Settings()

        self.backup_file_input = QLineEdit()
        self.restore_file_input = QLineEdit()

        self.backup_file_button = PushButton('', 40, 30, self.get_folder_path)
        self.restore_file_button = PushButton('', 40, 30, self.get_file_path)

        self.launch_restore_button = PushButton(self.tr('Start'), 120, 35,
                                                self.launch_restore_local)
        self.launch_backup_button = PushButton(self.tr('Start'), 120, 35,
                                               self.launch_backup_local)

        self.backups_cloud_list_widget = QListWidget()

        self.upload_backup_button = PushButton(' {}'.format(self.tr('Upload')),
                                               120, 35,
                                               self.upload_backup_cloud)
        self.download_backup_button = PushButton(
            ' {}'.format(self.tr('Download')),
            150 if self.settings.app_lang == 'uk_UA' else 120, 35,
            self.download_backup_cloud)
        self.delete_backup_button = PushButton(' {}'.format(self.tr('Delete')),
                                               120, 35,
                                               self.delete_backup_cloud)

        self.setup_ui()

        self.layout().addWidget(self.spinner)

    def showEvent(self, event):
        self.move(self.calendar.window().frameGeometry().topLeft() +
                  self.calendar.window().rect().center() -
                  self.rect().center())
        self.refresh_backups_cloud()

        btn_color = 'white' if self.settings.is_dark_theme else 'black'
        self.backup_file_button.setIcon(
            qta.icon('mdi.folder-open', color=btn_color, scale_factor=1.5))
        self.restore_file_button.setIcon(
            qta.icon('mdi.file-plus', color=btn_color, scale_factor=1.5))

        self.upload_backup_button.setIcon(
            qta.icon('mdi.cloud-upload', color=btn_color, scale_factor=1.2))
        self.download_backup_button.setIcon(
            qta.icon('mdi.cloud-download', color=btn_color, scale_factor=1.2))
        self.delete_backup_button.setIcon(
            qta.icon('mdi.delete', color=btn_color, scale_factor=1.2))

        super(BackupDialog, self).showEvent(event)

    def setup_ui(self):
        tabs_widget = QTabWidget(self)
        tabs_widget.setMinimumWidth(self.width() - 22)

        self.setup_cloud_ui(tabs_widget)
        self.setup_local_ui(tabs_widget)

        content = QVBoxLayout()
        content.addWidget(tabs_widget, alignment=Qt.AlignLeft)
        self.setLayout(content)

    def setup_local_ui(self, tabs):
        local_backup_tab = QTabWidget(self)

        self.setup_local_backup_ui(local_backup_tab)
        self.setup_local_restore_ui(local_backup_tab)

        tabs.addTab(local_backup_tab, self.tr('Local'))

    def setup_local_backup_ui(self, tabs):
        tab = QWidget(flags=tabs.windowFlags())

        h2_layout = QHBoxLayout()

        # noinspection PyArgumentList
        h2_layout.addWidget(QLabel('{}:'.format(self.tr('Location'))))
        h2_layout.setContentsMargins(10, 0, 10, 20)
        if os.path.exists(self.settings.app_last_backup_path):
            self.backup_file_input.setText(self.settings.app_last_backup_path)

        # noinspection PyArgumentList
        h2_layout.addWidget(self.backup_file_input)

        # noinspection PyArgumentList
        h2_layout.addWidget(self.backup_file_button)

        layout = QVBoxLayout()
        layout.setAlignment(Qt.AlignCenter)
        label = QLabel(self.tr('Create full backup of your calendar notes'))
        label.setContentsMargins(0, 10, 0, 30)
        layout.addWidget(label, alignment=Qt.AlignCenter)
        layout.addLayout(h2_layout)
        layout.addWidget(self.launch_backup_button, alignment=Qt.AlignCenter)

        tab.setLayout(layout)
        tabs.addTab(tab, self.tr('Backup'))

    def setup_local_restore_ui(self, tabs):
        tab = QWidget(flags=tabs.windowFlags())

        h2_layout = QHBoxLayout()

        # noinspection PyArgumentList
        h2_layout.addWidget(QLabel('{}:'.format(self.tr('Location'))))
        h2_layout.setContentsMargins(10, 0, 10, 20)
        if os.path.isfile(self.settings.app_last_restore_path):
            self.restore_file_input.setText(
                self.settings.app_last_restore_path)

        # noinspection PyArgumentList
        h2_layout.addWidget(self.restore_file_input)

        # noinspection PyArgumentList
        h2_layout.addWidget(self.restore_file_button)

        layout = QVBoxLayout()
        layout.setAlignment(Qt.AlignCenter)
        label = QLabel(
            self.tr('Restore all your calendar notes with backup file'))
        label.setWordWrap(True)
        label.setContentsMargins(0, 10, 0, 30)
        layout.addWidget(label, alignment=Qt.AlignCenter)
        layout.addLayout(h2_layout)
        layout.addWidget(self.launch_restore_button, alignment=Qt.AlignCenter)

        tab.setLayout(layout)
        tabs.addTab(tab, self.tr('Restore'))

    def get_folder_path(self):
        # noinspection PyArgumentList
        file_name = QFileDialog().getExistingDirectory(
            caption=self.tr('Select Directory'),
            directory=self.backup_file_input.text())
        if len(file_name) > 0:
            self.backup_file_input.setText(str(file_name))

    def get_file_path(self):
        path = self.restore_file_input.text()

        # noinspection PyArgumentList
        file_name = QFileDialog().getOpenFileName(
            caption=self.tr('Open file'),
            directory=path if os.path.isfile(path) else './',
            filter='(*.bak)')
        if len(file_name) > 0:
            self.restore_file_input.setText(file_name[0])

    def launch_restore_local(self):
        path = self.restore_file_input.text()
        self.settings.set_last_restore_path(path)
        self.exec_worker(self.storage.restore,
                         self.launch_restore_local_success, None, *(path, ))

    def launch_restore_local_success(self):
        self.calendar.update()
        self.calendar.settings_dialog.refresh_settings_values()
        popup.info(self, self.tr('Data has been restored'))

    def launch_backup_local(self):
        path = self.backup_file_input.text()
        self.settings.set_last_backup_path(path)
        self.exec_worker(self.storage.backup, self.launch_backup_local_success,
                         None, *(path, self.settings.include_settings_backup))

    def launch_backup_local_success(self):
        popup.info(self, self.tr('Backup has been created'))

    def setup_cloud_ui(self, tabs):
        tab = QWidget(flags=tabs.windowFlags())

        layout = QVBoxLayout()
        layout.setAlignment(Qt.AlignBottom)

        buttons_layout = QHBoxLayout()

        center_buttons_layout = QHBoxLayout()
        center_buttons_layout.setAlignment(Qt.AlignLeft)
        self.upload_backup_button.setToolTip(self.tr('Upload'))
        self.upload_backup_button.setEnabled(False)
        center_buttons_layout.addWidget(self.upload_backup_button,
                                        alignment=Qt.AlignCenter)

        self.download_backup_button.setToolTip(self.tr('Download'))
        self.download_backup_button.setEnabled(False)
        center_buttons_layout.addWidget(self.download_backup_button,
                                        alignment=Qt.AlignCenter)
        buttons_layout.addLayout(center_buttons_layout)

        self.delete_backup_button.setEnabled(False)
        self.delete_backup_button.setToolTip(self.tr('Delete'))
        buttons_layout.addWidget(self.delete_backup_button,
                                 alignment=Qt.AlignRight)

        layout.addLayout(buttons_layout)

        scroll_view = QScrollArea()

        # noinspection PyUnresolvedReferences
        self.backups_cloud_list_widget.itemSelectionChanged.connect(
            self.selection_changed)
        scroll_view.setWidget(self.backups_cloud_list_widget)
        scroll_view.setWidgetResizable(True)
        scroll_view.setFixedHeight(200)
        scroll_view.setFixedWidth(455)
        layout.addWidget(scroll_view, alignment=Qt.AlignLeft)

        tab.setLayout(layout)
        tabs.addTab(tab, self.tr('Cloud'))

    def selection_changed(self):
        if len(self.backups_cloud_list_widget.selectedItems()) > 0:
            self.delete_backup_button.setEnabled(True)
            self.download_backup_button.setEnabled(True)
        else:
            self.delete_backup_button.setEnabled(False)
            self.download_backup_button.setEnabled(False)

    def refresh_backups_cloud(self):
        self.backups_cloud_list_widget.clear()
        self.exec_worker(self.cloud.backups, None,
                         self.refresh_backups_cloud_success)

    def refresh_backups_cloud_success(self, backups):
        self.upload_backup_button.setEnabled(True)
        for backup in backups:
            self.add_backup_widget(backup)

    def add_backup_widget(self, backup_data):
        num_repr = repr(backup_data['events_count'])
        if len(num_repr) > 1 and int(num_repr[-2]) == 1:
            text_label = self.tr('events')
        elif 1 < int(num_repr[-1]) < 5:
            text_label = self.tr('events*')
        else:
            text_label = self.tr('event{}'.format(
                's' if int(num_repr[-1]) > 1 or backup_data['events_count'] %
                2 == 0 else ''))
        backup_widget = BackupWidget(
            flags=self.backups_cloud_list_widget.windowFlags(),
            parent=self.backups_cloud_list_widget,
            palette=self.palette(),
            font=self.font(),
            hash_sum=backup_data['digest'],
            title=datetime.strptime(backup_data['timestamp'],
                                    EventModel.TIMESTAMP_FORMAT).strftime(
                                        EventModel.DATE_TIME_FORMAT),
            description='{} {} {}, {}'.format(
                backup_data['backup_size'], backup_data['events_count'],
                text_label,
                self.tr('full backup')
                if backup_data['contains_settings'] is True else
                self.tr('excluded settings')))
        list_widget_item = QListWidgetItem(self.backups_cloud_list_widget)
        list_widget_item.setSizeHint(backup_widget.sizeHint())
        self.backups_cloud_list_widget.addItem(list_widget_item)
        self.backups_cloud_list_widget.setItemWidget(list_widget_item,
                                                     backup_widget)

    def upload_backup_cloud(self):
        self.exec_worker(self.upload_backup_cloud_run,
                         self.upload_backup_cloud_success, None)

    def upload_backup_cloud_run(self):
        user = self.cloud.user()
        timestamp = datetime.strftime(datetime.now(), '%Y-%m-%d %H:%M:%S')
        backup_data = self.storage.prepare_backup_data(
            self.storage.to_array(), timestamp,
            self.settings.include_settings_backup, user['username'])
        self.cloud.upload_backup(backup_data)

    def upload_backup_cloud_success(self):
        self.refresh_backups_cloud()
        popup.info(self,
                   self.tr('Backup was successfully uploaded to the cloud'))

    def download_backup_cloud(self):
        current = self.get_current_selected()
        if current is not None:
            self.exec_worker(self.download_backup_cloud_run,
                             self.download_backup_cloud_success, None,
                             *(current, ))

    def download_backup_cloud_run(self, current):
        self.storage.restore_from_dict(
            self.cloud.download_backup(current.hash_sum))

    def download_backup_cloud_success(self):
        self.calendar.reset_palette(self.settings.app_theme)
        self.calendar.reset_font(QFont('SansSerif', self.settings.app_font))
        self.calendar.update()
        self.calendar.settings_dialog.refresh_settings_values()
        popup.info(
            self,
            self.
            tr('Backup was successfully downloaded. Restart application to enable all restored settings.'
               ))

    def delete_backup_cloud(self):
        current = self.get_current_selected()
        if current is not None:
            self.exec_worker(self.cloud.delete_backup,
                             self.delete_backup_cloud_success, None,
                             *(current.hash_sum, ))

    def delete_backup_cloud_success(self):
        self.refresh_backups_cloud()
        popup.info(self, self.tr('Backup was deleted successfully'))

    def get_current_selected(self):
        selected_items = self.backups_cloud_list_widget.selectedItems()
        if len(selected_items) > 0:
            return self.backups_cloud_list_widget.itemWidget(selected_items[0])
        return None

    def exec_worker(self, fn, fn_success, fn_param_success, *args, **kwargs):
        self.spinner.start()
        worker = Worker(fn, *args, **kwargs)
        if fn_success is not None:
            worker.signals.success.connect(fn_success)
        if fn_param_success is not None:
            worker.signals.param_success.connect(fn_param_success)
        worker.signals.error.connect(self.popup_error)
        worker.signals.finished.connect(self.spinner.stop)
        self.thread_pool.start(worker)

    def popup_error(self, err):
        try:
            raise err[0](err[1])
        except AuthRequiredError:
            err_msg = self.tr(
                'Account access failure: authentication is required')
        except UserRetrievingError:
            err_msg = '{} {}'.format(
                self.
                tr('Reading account failure: unable to retrieve account information, status'
                   ), err[1])
        except ReadingBackupsError:
            err_msg = '{} {}'.format(
                self.
                tr('Reading backups failure: unable to retrieve backups data from the server, status'
                   ), err[1])
        except BackupAlreadyExistsError:
            err_msg = self.tr('Upload failure: backup already exists')
        except BackupDownloadingError:
            err_msg = self.tr('Download failure: unable to download backup')
        except BackupDeletingError:
            err_msg = self.tr('Deleting failure: unable to delete backup')
        except (CloudStorageException, RequestException, Exception):
            err_msg = str(err[1])
        popup.error(self, err_msg)
Beispiel #9
0
class EventDetailsDialog(QDialog):

	def __init__(self, flags, *args, **kwargs):
		super(EventDetailsDialog, self).__init__(flags=flags, *args)

		self.setFixedSize(500, 500)
		self.setWindowFlags(Qt.Dialog)

		if 'palette' in kwargs:
			self.setPalette(kwargs.get('palette'))
		if 'font' in kwargs:
			self.setFont(kwargs.get('font'))
		self.setWindowTitle(self.tr('New Event'))

		self.thread_pool = QThreadPool()
		self.spinner = WaitingSpinner()

		self.calendar = kwargs.get('calendar', None)

		if self.calendar is None:
			raise RuntimeError('EventDetailsDialog: calendar is not set')

		self.storage = kwargs.get('storage', Storage(try_to_reconnect=True))
		self.storage.try_to_reconnect = True

		self.title_input = QLineEdit(self)
		self.description_input = QTextEdit(self)
		self.date_input = QDateEdit(self)
		self.time_input = QTimeEdit(self)
		self.repeat_weekly_input = QCheckBox(self.tr('Repeat weekly'), self)

		self.event_id = None
		self.del_btn = None

		self.is_editing = False

		self.setup_ui()

		self.layout().addWidget(self.spinner)

	def showEvent(self, event):
		self.move(
			self.calendar.window().frameGeometry().topLeft() +
			self.calendar.window().rect().center() - self.rect().center()
		)
		super(EventDetailsDialog, self).showEvent(event)

	# noinspection PyArgumentList
	def setup_ui(self):
		content = QVBoxLayout()
		content.addWidget(QLabel('{}:'.format(self.tr('Title'))), alignment=Qt.AlignLeft)
		content.addWidget(self.title_input)
		content.addWidget(QLabel('{}:'.format(self.tr('Description (optional)'))), alignment=Qt.AlignLeft)
		content.addWidget(self.description_input)
		content.addWidget(QLabel('{}:'.format(self.tr('Date'))), alignment=Qt.AlignLeft)
		content.addWidget(self.date_input)
		content.addWidget(QLabel('{}:'.format(self.tr('Time'))), alignment=Qt.AlignLeft)
		content.addWidget(self.time_input)
		self.repeat_weekly_input.setChecked(False)
		content.addWidget(self.repeat_weekly_input)

		buttons = QHBoxLayout()
		buttons.setAlignment(Qt.AlignRight | Qt.AlignBottom)
		buttons.addWidget(PushButton(self.tr('Save'), 90, 30, self.save_event_click), 0, Qt.AlignRight)
		self.del_btn = PushButton(self.tr('Delete'), 90, 30, self.delete_event)
		buttons.addWidget(self.del_btn)
		buttons.addWidget(PushButton(self.tr('Cancel'), 90, 30, self.close), 0, Qt.AlignRight)
		content.addLayout(buttons)

		self.setLayout(content)

	def reset_inputs(self, date=None, event_data=None):
		self.del_btn.setEnabled(False)
		self.is_editing = False
		if date is not None:
			self.event_id = None
			self.date_input.setDate(QDate(date))
			curr_time = (datetime.now() + timedelta(minutes=3)).time().replace(second=0, microsecond=0)
			self.time_input.setTime(QTime(curr_time))
			self.title_input.setText('')
			self.description_input.setText('')
		elif event_data is not None:
			self.event_id = event_data.id
			self.setWindowTitle(self.tr('Update Event'))
			self.title_input.setText(event_data.title)
			self.description_input.setText(event_data.description)
			self.date_input.setDate(event_data.date)
			self.time_input.setTime(QTime(event_data.time))
			self.repeat_weekly_input.setChecked(event_data.repeat_weekly)
			self.del_btn.setEnabled(True)
			self.is_editing = True

	def validate_inputs(self):
		if len(self.title_input.text()) < 1:
			popup.warning(self, '{}!'.format(self.tr('Provide title for the event')))
			return False
		if self.event_id is None:
			if self.date_input.date().toPyDate() < datetime.now().date():
				popup.warning(self, self.tr('Unable to set past event, check date input'))
				return False
			if self.time_input.time().toPyTime() < datetime.now().time() and \
				self.date_input.date().toPyDate() == datetime.now().date():
				popup.warning(self, self.tr('Unable to set past event, check time input'))
				return False
		return True

	def save_event_click(self):
		if self.validate_inputs():
			data = {
				'title': self.title_input.text(),
				'e_date': self.date_input.date().toPyDate(),
				'e_time': self.time_input.time().toPyTime(),
				'description': self.description_input.toPlainText(),
				'repeat_weekly': self.repeat_weekly_input.isChecked()
			}
			err_format = '{}'.format(self.tr('Saving event to database failed')) + ': {}'
			fn = self.storage.create_event
			if self.event_id is not None:
				data['pk'] = self.event_id
				fn = self.storage.update_event
			self.exec_worker(fn, self.close_and_update, err_format, **data)

	def delete_event(self):
		if popup.question(
				self,
				self.tr('Deleting an event'),
				'{}?'.format(self.tr('Do you really want to delete the event'))
		) == QMessageBox.Yes:
			worker = Worker(self.storage.delete_event, *(self.event_id,))
			worker.signals.success.connect(self.close_and_update)
			worker.err_format = '{}'
			worker.signals.error.connect(self.popup_error)
			self.thread_pool.start(worker)

	def close_and_update(self):
		self.close()
		self.calendar.load_events(self.calendar.selectedDate())
		self.calendar.update()

	def exec_worker(self, fn, fn_success, err_format, *args, **kwargs):
		self.spinner.start()
		worker = Worker(fn, *args, **kwargs)
		if fn_success is not None:
			worker.signals.success.connect(fn_success)
		worker.err_format = err_format
		worker.signals.error.connect(self.popup_error)
		worker.signals.finished.connect(self.spinner.stop)
		self.thread_pool.start(worker)

	def popup_error(self, err):
		popup.error(self, '{}'.format(err[1]))