diff --git a/CHANGELOG.md b/CHANGELOG.md index 6c0871b..b3ee732 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# 0.3.1 + + * Make it possible to add a tag from the Tag Browser + # 0.3 * Introduce Tags diff --git a/bouquin/db.py b/bouquin/db.py index c0fdee2..f40dd1e 100644 --- a/bouquin/db.py +++ b/bouquin/db.py @@ -561,6 +561,30 @@ class DBManager: ).fetchall() return [(r[0], r[1], r[2]) for r in rows] + def add_tag(self, name: str, color: str) -> None: + """ + Update a tag's name and colour. + """ + name = name.strip() + color = color.strip() or "#CCCCCC" + + try: + with self.conn: + cur = self.conn.cursor() + cur.execute( + """ + INSERT INTO tags + (name, color) + VALUES (?, ?); + """, + (name, color), + ) + except sqlite.IntegrityError as e: + if "UNIQUE constraint failed: tags.name" in str(e): + raise sqlite.IntegrityError( + strings._("tag_already_exists_with_that_name") + ) from e + def update_tag(self, tag_id: int, name: str, color: str) -> None: """ Update a tag's name and colour. diff --git a/bouquin/locales/en.json b/bouquin/locales/en.json index a3c9228..3fe7c6c 100644 --- a/bouquin/locales/en.json +++ b/bouquin/locales/en.json @@ -127,6 +127,7 @@ "add": "Add", "remove": "Remove", "ok": "OK", + "add_a_tag": "Add a tag", "edit_tag_name": "Edit tag name", "new_tag_name": "New tag name:", "change_color": "Change colour", diff --git a/bouquin/locales/fr.json b/bouquin/locales/fr.json index 4944bf5..0949130 100644 --- a/bouquin/locales/fr.json +++ b/bouquin/locales/fr.json @@ -127,6 +127,7 @@ "add": "Ajouter", "remove": "Supprimer", "ok": "OK", + "add_a_tag": "Ajouter une étiquette", "edit_tag_name": "Modifier le nom de l'étiquette", "new_tag_name": "Nouveau nom de l'étiquette :", "change_color": "Changer la couleur", diff --git a/bouquin/locales/it.json b/bouquin/locales/it.json index 5e956f0..b5006bb 100644 --- a/bouquin/locales/it.json +++ b/bouquin/locales/it.json @@ -126,6 +126,7 @@ "add": "Aggiungi", "remove": "Rimuovi", "ok": "OK", + "add_a_tag": "Aggiungi un tag", "edit_tag_name": "Modifica nome tag", "new_tag_name": "Nuovo nome tag:", "change_color": "Cambia colore", diff --git a/bouquin/tag_browser.py b/bouquin/tag_browser.py index 19a97f4..1e21bd1 100644 --- a/bouquin/tag_browser.py +++ b/bouquin/tag_browser.py @@ -1,4 +1,3 @@ -# tag_browser.py from PySide6.QtCore import Qt, Signal from PySide6.QtGui import QColor from PySide6.QtWidgets import ( @@ -11,6 +10,7 @@ from PySide6.QtWidgets import ( QLabel, QColorDialog, QMessageBox, + QInputDialog, ) from .db import DBManager @@ -52,6 +52,10 @@ class TagBrowserDialog(QDialog): # Tag management buttons btn_row = QHBoxLayout() + self.add_tag_btn = QPushButton(strings._("add_a_tag")) + self.add_tag_btn.clicked.connect(self._add_a_tag) + btn_row.addWidget(self.add_tag_btn) + self.edit_name_btn = QPushButton(strings._("edit_tag_name")) self.edit_name_btn.clicked.connect(self._edit_tag_name) self.edit_name_btn.setEnabled(False) @@ -155,6 +159,23 @@ class TagBrowserDialog(QDialog): self.openDateRequested.emit(date_iso) self.accept() + def _add_a_tag(self): + """Add a new tag""" + + new_name, ok = QInputDialog.getText( + self, strings._("add_a_tag"), strings._("new_tag_name"), text="" + ) + + if ok and new_name: + color = QColorDialog.getColor(QColor(), self) + if color.isValid(): + try: + self._db.add_tag(new_name, color.name()) + self._populate(None) + self.tagsModified.emit() + except IntegrityError as e: + QMessageBox.critical(self, strings._("db_database_error"), str(e)) + def _edit_tag_name(self): """Edit the name of the selected tag""" item = self.tree.currentItem() @@ -169,9 +190,6 @@ class TagBrowserDialog(QDialog): old_name = data["name"] color = data["color"] - # Simple input dialog - from PySide6.QtWidgets import QInputDialog - new_name, ok = QInputDialog.getText( self, strings._("edit_tag_name"), strings._("new_tag_name"), text=old_name ) diff --git a/tests/test_tags.py b/tests/test_tags.py index ab7a0be..302bc4c 100644 --- a/tests/test_tags.py +++ b/tests/test_tags.py @@ -141,6 +141,16 @@ def test_list_all_tags(fresh_db): assert "tag3" in tag_names +def test_add_tag_name_and_color(fresh_db): + """Test adding a tag's name and color""" + fresh_db.add_tag("new123", "#FF0000") + + updated_tags = fresh_db.list_tags() + assert len(updated_tags) == 1 + assert updated_tags[0][1] == "new123" + assert updated_tags[0][2] == "#FF0000" + + def test_update_tag_name_and_color(fresh_db): """Test updating a tag's name and color""" date_iso = "2024-01-15"