Commit 9aa483ca authored by Samuel GAIST's avatar Samuel GAIST
Browse files

[widgets][libraries] Implement load of "used libraries"

parent 0bfe5813
......@@ -31,40 +31,72 @@ from ..widgets.libraries import LibrariesWidget
class TestLibrariesWidget:
"""Test that the library widget works correctly"""
def test_json_dumps_used_libraries(self):
def test_dump_used_libraries(self, qtbot):
no_libraries_set = {}
libraries = LibrariesWidget()
qtbot.addWidget(libraries)
assert libraries.get_used_libraries() == no_libraries_set
def test_load_dump_libraries(self, qtbot):
available_libraries = ["user/lib1/1", "user/lib2/1", "user/lib3/1"]
no_libraries_set = {}
one_library_set = {"lib1": "user/lib1/1"}
libraries = LibrariesWidget()
qtbot.addWidget(libraries)
libraries.set_available_libraries(available_libraries)
assert libraries.get_used_libraries() == no_libraries_set
libraries.set_used_libraries(one_library_set)
assert libraries.get_used_libraries() == one_library_set
def test_load_available_libraries(self, qtbot):
available_libraries = ["user/lib1/1", "user/lib2/1", "user/lib3/1"]
def test_json_load_available_libraries(self):
available_libraries = {
"lib1": "lib1/a/b",
"lib2": "lib2/a/b",
"lib3": "lib3/a/b",
}
libraries = LibrariesWidget()
qtbot.addWidget(libraries)
libraries.set_available_libraries(available_libraries)
assert libraries.get_available_libraries() == available_libraries
def test_json_load_use_dump_libraries(self, qtbot):
available_libraries = {
"lib1": "lib1/a/b",
"lib2": "lib2/a/b",
"lib3": "lib3/a/b",
}
one_library_set = {"lib1": "lib1/a/b"}
two_libraries_set = {"lib1": "lib1/a/b", "lib3": "lib3/a/b"}
def test_use_dump_libraries(self, qtbot):
available_libraries = ["user/lib1/1", "user/lib2/1", "user/lib3/1"]
one_library_set = {"lib1": "user/lib1/1"}
two_libraries_set = {"lib1": "user/lib1/1", "lib3": "user/lib3/1"}
libraries = LibrariesWidget()
qtbot.addWidget(libraries)
libraries.set_available_libraries(available_libraries)
# Add lib1 from the available libraries
libraries.libraries_available.setCurrentRow(0)
libraries.available_libraries_listwidget.setCurrentRow(0)
qtbot.mouseClick(libraries.add_library_button, QtCore.Qt.LeftButton)
assert libraries.get_used_libraries() == one_library_set
# Add lib3 item from the available libraries
libraries.libraries_available.setCurrentRow(1)
libraries.available_libraries_listwidget.setCurrentRow(1)
qtbot.mouseClick(libraries.add_library_button, QtCore.Qt.LeftButton)
assert libraries.get_used_libraries() == two_libraries_set
# Remove lib3 from the used libraries
libraries.libraries_used.selectRow(1)
libraries.used_libraries_tablewidget.selectRow(1)
qtbot.mouseClick(libraries.remove_library_button, QtCore.Qt.LeftButton)
assert libraries.get_used_libraries() == one_library_set
def test_change_alias(self, qtbot):
available_libraries = ["user/lib1/1", "user/lib2/1", "user/lib3/1"]
one_library_set = {"lib1": "user/lib1/1"}
modified_library_set = {"test1": "user/lib1/1"}
libraries = LibrariesWidget()
qtbot.addWidget(libraries)
libraries.set_available_libraries(available_libraries)
# Add lib1 from the available libraries
libraries.available_libraries_listwidget.setCurrentRow(0)
qtbot.mouseClick(libraries.add_library_button, QtCore.Qt.LeftButton)
assert libraries.get_used_libraries() == one_library_set
item = libraries.used_libraries_tablewidget.item(0, 1)
item.setText("test1")
assert libraries.get_used_libraries() == modified_library_set
......@@ -24,6 +24,7 @@
###############################################################################
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtWidgets import QHBoxLayout
......@@ -47,20 +48,21 @@ class LibrariesWidget(QWidget):
super(LibrariesWidget, self).__init__(parent)
self.available_libraries = {}
self._available_libraries = []
# Widgets
self.label_libraries_available = QLabel(self.tr("Available Libraries"))
self.label_libraries_used = QLabel(self.tr("Used Libraries"))
self.libraries_available = QListWidget()
self.libraries_used = QTableWidget()
self.libraries_used.setColumnCount(2)
header = self.libraries_used.horizontalHeader()
available_libraries_label = QLabel(self.tr("Available Libraries"))
self.available_libraries_listwidget = QListWidget()
used_libraries_label = QLabel(self.tr("Used Libraries"))
self.used_libraries_tablewidget = QTableWidget()
self.used_libraries_tablewidget.setColumnCount(2)
header = self.used_libraries_tablewidget.horizontalHeader()
header.setSectionResizeMode(QHeaderView.Stretch)
self.libraries_used.setHorizontalHeaderLabels(
self.used_libraries_tablewidget.setHorizontalHeaderLabels(
[self.tr("Library name"), self.tr("Library alias")]
)
self.libraries_used.setSelectionBehavior(QTableView.SelectRows)
self.used_libraries_tablewidget.setSelectionBehavior(QTableView.SelectRows)
self.add_library_button = QPushButton(self.tr(">"))
self.remove_library_button = QPushButton(self.tr("<"))
......@@ -68,14 +70,16 @@ class LibrariesWidget(QWidget):
layout = QHBoxLayout(self)
# QVBoxLayout for available libraries
libraries_available_layout = QVBoxLayout()
libraries_available_layout.addWidget(self.label_libraries_available)
libraries_available_layout.addWidget(self.libraries_available)
available_libraries_listwidget_layout = QVBoxLayout()
available_libraries_listwidget_layout.addWidget(available_libraries_label)
available_libraries_listwidget_layout.addWidget(
self.available_libraries_listwidget
)
# QVBoxLayout for used libraries
libraries_used_layout = QVBoxLayout()
libraries_used_layout.addWidget(self.label_libraries_used)
libraries_used_layout.addWidget(self.libraries_used)
libraries_used_layout.addWidget(used_libraries_label)
libraries_used_layout.addWidget(self.used_libraries_tablewidget)
# QVBoxLayout for buttons >/<
buttons_layout = QVBoxLayout()
......@@ -85,70 +89,94 @@ class LibrariesWidget(QWidget):
buttons_layout.addStretch(1)
# Layouts Design
layout.addLayout(libraries_available_layout)
layout.addLayout(available_libraries_listwidget_layout)
layout.addLayout(buttons_layout)
layout.addLayout(libraries_used_layout)
# Signal/Slots connections
self.add_library_button.clicked.connect(self.__add_library)
self.remove_library_button.clicked.connect(self.__remove_library)
self.add_library_button.clicked.connect(self.__on_add_library_clicked)
self.remove_library_button.clicked.connect(self.__on_remove_library_clicked)
self.add_library_button.clicked.connect(self.dataChanged)
self.remove_library_button.clicked.connect(self.dataChanged)
def __add_library(self):
selected_libraries = self.libraries_available.selectedItems()
def __use_library(self, library_name, library_alias):
"""Put the library in the used library table and removes it from
from the available list.
"""
# Remove it from available
item_list = self.available_libraries_listwidget.findItems(
library_name, Qt.MatchExactly
)
if len(item_list) > 0:
item = item_list[0]
row = self.available_libraries_listwidget.row(item)
self.available_libraries_listwidget.takeItem(row)
del item
# Add it to used
row = self.used_libraries_tablewidget.rowCount()
self.used_libraries_tablewidget.insertRow(row)
library_name_item = QTableWidgetItem(library_name)
library_name_item.setFlags(library_name_item.flags() & ~Qt.ItemIsEditable)
self.used_libraries_tablewidget.setItem(row, 0, library_name_item)
self.used_libraries_tablewidget.setItem(
row, 1, QTableWidgetItem(library_alias)
)
def __on_add_library_clicked(self):
selected_libraries = self.available_libraries_listwidget.selectedItems()
if selected_libraries:
for library in selected_libraries:
library_name = library.text()
library_alias = [
key
for key in self.available_libraries
if self.available_libraries[key] == library_name
][0]
self.libraries_available.takeItem(self.libraries_available.row(library))
# add to table
row = self.libraries_used.rowCount()
self.libraries_used.insertRow(row)
self.libraries_used.setItem(row, 0, QTableWidgetItem(library_name))
self.libraries_used.setItem(row, 1, QTableWidgetItem(library_alias))
else:
return
def __remove_library(self):
library_alias = library_name.split("/")[1]
self.__use_library(library_name, library_alias)
def __on_remove_library_clicked(self):
# Get reverse order for deletion and set for possible rowcolumn selection
rows = sorted(
set(list(index.row() for index in self.libraries_used.selectedIndexes())),
set(
list(
index.row()
for index in self.used_libraries_tablewidget.selectedIndexes()
)
),
reverse=True,
)
for row in rows:
library_name = self.libraries_used.item(row, 0).text()
self.libraries_available.addItem(library_name)
self.libraries_used.removeRow(row)
library_name = self.used_libraries_tablewidget.item(row, 0).text()
self.available_libraries_listwidget.addItem(library_name)
self.used_libraries_tablewidget.removeRow(row)
def get_used_libraries(self):
"""Returns the libraries used"""
# {key:value} for {library_alias:library_name}
libraries = {}
for row in range(0, self.libraries_used.rowCount()):
for row in range(0, self.used_libraries_tablewidget.rowCount()):
libraries[
self.libraries_used.item(row, 1).text()
] = self.libraries_used.item(row, 0).text()
self.used_libraries_tablewidget.item(row, 1).text()
] = self.used_libraries_tablewidget.item(row, 0).text()
return libraries
def set_used_libraries(self, libraries):
self.set_available_libraries(self._available_libraries)
for alias, library in libraries.items():
self.__use_library(library, alias)
def get_available_libraries(self):
"""Returns the available libraries"""
# {key:value} for {library_alias:library_name}
return self.available_libraries
return self._available_libraries
def set_available_libraries(self, available_libraries):
"""Set the available libraries"""
# {key:value} for {library_alias:library_name}
# clear available and used libraries
self.libraries_available.clear()
self.libraries_used.setRowCount(0)
self.available_libraries = available_libraries
for key in self.available_libraries.keys():
self.libraries_available.addItem(self.available_libraries[key])
self.available_libraries_listwidget.clear()
self.used_libraries_tablewidget.clear()
self.used_libraries_tablewidget.setRowCount(0)
self._available_libraries = available_libraries
self.available_libraries_listwidget.addItems(self._available_libraries)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment