Source code for spinetoolbox.spine_db_editor.widgets.url_toolbar
######################################################################################################################
# Copyright (C) 2017-2021 Spine project consortium
# This file is part of Spine Toolbox.
# Spine Toolbox is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General
# Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option)
# any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
# Public License for more details. You should have received a copy of the GNU Lesser General Public License along with
# this program. If not, see <http://www.gnu.org/licenses/>.
######################################################################################################################
"""
Contains the UrlToolBar class and helpers.
:author: M. Marin (KTH)
:date: 13.5.2020
"""
from PySide2.QtWidgets import QToolBar, QLineEdit, QMenu, QAction
from PySide2.QtGui import QIcon, QKeySequence
from PySide2.QtCore import QSize, Qt, Slot
from spinetoolbox.helpers import CharIconEngine
[docs]class UrlToolBar(QToolBar):
def __init__(self, db_editor):
super().__init__(db_editor)
self.setObjectName("spine_db_editor_url_toolbar")
self._db_editor = db_editor
self._history = []
self._history_index = -1
self._project_urls = {}
self._go_back_action = self.addAction(QIcon(CharIconEngine("\uf060")), "Go back", db_editor.load_previous_urls)
self._go_forward_action = self.addAction(
QIcon(CharIconEngine("\uf061")), "Go forward", db_editor.load_next_urls
)
self.reload_action = self.addAction(QIcon(CharIconEngine("\uf021")), "Reload", db_editor.refresh_session)
self.reload_action.setShortcut(QKeySequence(Qt.CTRL + Qt.Key_R))
self._go_back_action.setEnabled(False)
self._go_forward_action.setEnabled(False)
self.reload_action.setEnabled(False)
self._open_project_url_menu = self._add_open_project_url_menu()
self._line_edit = QLineEdit(self)
self._line_edit.setPlaceholderText("Type the URL of a Spine DB")
self._line_edit.returnPressed.connect(self._handle_line_edit_return_pressed)
self.addWidget(self._line_edit)
self.setMovable(False)
self.setIconSize(QSize(20, 20))
@property
[docs] def _connect_project_item_model_signals(self, slot):
project_item_model = self._db_editor.toolbox.project_item_model
project_item_model.modelReset.connect(slot)
project_item_model.rowsRemoved.connect(slot)
project_item_model.rowsInserted.connect(slot)
[docs] def _disconnect_project_item_model_signals(self, slot):
project_item_model = self._db_editor.toolbox.project_item_model
project_item_model.modelReset.disconnect(slot)
project_item_model.rowsRemoved.disconnect(slot)
project_item_model.rowsInserted.disconnect(slot)
@Slot()
@Slot("QAction")
[docs] def _open_ds_url(self, action):
url = self._project_urls[action.text()]
self._db_editor.load_db_urls({url: action.text()})
[docs] def _update_history_actions_availability(self):
self._go_back_action.setEnabled(self._history_index > 0)
self._go_forward_action.setEnabled(self._history_index < len(self._history) - 1)
[docs] def add_urls_to_history(self, db_urls):
"""Adds url to history.
Args:
db_urls (list of str)
"""
self._history_index += 1
self._update_history_actions_availability()
self._history[self._history_index :] = [db_urls]
[docs] def get_previous_urls(self):
"""Returns previous urls in history.
Returns:
list of str
"""
self._history_index -= 1
self._update_history_actions_availability()
return self._history[self._history_index]
[docs] def get_next_urls(self):
"""Returns next urls in history.
Returns:
list of str
"""
self._history_index += 1
self._update_history_actions_availability()
return self._history[self._history_index]
[docs] def _handle_line_edit_return_pressed(self):
urls = [url.strip() for url in self._line_edit.text().split(";")]
self._db_editor.load_db_urls({url: None for url in urls}, create=True)