Make it possible to add a tag from the Tag Browser
This commit is contained in:
parent
b1ae56270a
commit
b1ba599e99
7 changed files with 63 additions and 4 deletions
|
|
@ -1,3 +1,7 @@
|
||||||
|
# 0.3.1
|
||||||
|
|
||||||
|
* Make it possible to add a tag from the Tag Browser
|
||||||
|
|
||||||
# 0.3
|
# 0.3
|
||||||
|
|
||||||
* Introduce Tags
|
* Introduce Tags
|
||||||
|
|
|
||||||
|
|
@ -561,6 +561,30 @@ class DBManager:
|
||||||
).fetchall()
|
).fetchall()
|
||||||
return [(r[0], r[1], r[2]) for r in rows]
|
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:
|
def update_tag(self, tag_id: int, name: str, color: str) -> None:
|
||||||
"""
|
"""
|
||||||
Update a tag's name and colour.
|
Update a tag's name and colour.
|
||||||
|
|
|
||||||
|
|
@ -127,6 +127,7 @@
|
||||||
"add": "Add",
|
"add": "Add",
|
||||||
"remove": "Remove",
|
"remove": "Remove",
|
||||||
"ok": "OK",
|
"ok": "OK",
|
||||||
|
"add_a_tag": "Add a tag",
|
||||||
"edit_tag_name": "Edit tag name",
|
"edit_tag_name": "Edit tag name",
|
||||||
"new_tag_name": "New tag name:",
|
"new_tag_name": "New tag name:",
|
||||||
"change_color": "Change colour",
|
"change_color": "Change colour",
|
||||||
|
|
|
||||||
|
|
@ -127,6 +127,7 @@
|
||||||
"add": "Ajouter",
|
"add": "Ajouter",
|
||||||
"remove": "Supprimer",
|
"remove": "Supprimer",
|
||||||
"ok": "OK",
|
"ok": "OK",
|
||||||
|
"add_a_tag": "Ajouter une étiquette",
|
||||||
"edit_tag_name": "Modifier le nom de l'étiquette",
|
"edit_tag_name": "Modifier le nom de l'étiquette",
|
||||||
"new_tag_name": "Nouveau nom de l'étiquette :",
|
"new_tag_name": "Nouveau nom de l'étiquette :",
|
||||||
"change_color": "Changer la couleur",
|
"change_color": "Changer la couleur",
|
||||||
|
|
|
||||||
|
|
@ -126,6 +126,7 @@
|
||||||
"add": "Aggiungi",
|
"add": "Aggiungi",
|
||||||
"remove": "Rimuovi",
|
"remove": "Rimuovi",
|
||||||
"ok": "OK",
|
"ok": "OK",
|
||||||
|
"add_a_tag": "Aggiungi un tag",
|
||||||
"edit_tag_name": "Modifica nome tag",
|
"edit_tag_name": "Modifica nome tag",
|
||||||
"new_tag_name": "Nuovo nome tag:",
|
"new_tag_name": "Nuovo nome tag:",
|
||||||
"change_color": "Cambia colore",
|
"change_color": "Cambia colore",
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
# tag_browser.py
|
|
||||||
from PySide6.QtCore import Qt, Signal
|
from PySide6.QtCore import Qt, Signal
|
||||||
from PySide6.QtGui import QColor
|
from PySide6.QtGui import QColor
|
||||||
from PySide6.QtWidgets import (
|
from PySide6.QtWidgets import (
|
||||||
|
|
@ -11,6 +10,7 @@ from PySide6.QtWidgets import (
|
||||||
QLabel,
|
QLabel,
|
||||||
QColorDialog,
|
QColorDialog,
|
||||||
QMessageBox,
|
QMessageBox,
|
||||||
|
QInputDialog,
|
||||||
)
|
)
|
||||||
|
|
||||||
from .db import DBManager
|
from .db import DBManager
|
||||||
|
|
@ -52,6 +52,10 @@ class TagBrowserDialog(QDialog):
|
||||||
# Tag management buttons
|
# Tag management buttons
|
||||||
btn_row = QHBoxLayout()
|
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 = QPushButton(strings._("edit_tag_name"))
|
||||||
self.edit_name_btn.clicked.connect(self._edit_tag_name)
|
self.edit_name_btn.clicked.connect(self._edit_tag_name)
|
||||||
self.edit_name_btn.setEnabled(False)
|
self.edit_name_btn.setEnabled(False)
|
||||||
|
|
@ -155,6 +159,23 @@ class TagBrowserDialog(QDialog):
|
||||||
self.openDateRequested.emit(date_iso)
|
self.openDateRequested.emit(date_iso)
|
||||||
self.accept()
|
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):
|
def _edit_tag_name(self):
|
||||||
"""Edit the name of the selected tag"""
|
"""Edit the name of the selected tag"""
|
||||||
item = self.tree.currentItem()
|
item = self.tree.currentItem()
|
||||||
|
|
@ -169,9 +190,6 @@ class TagBrowserDialog(QDialog):
|
||||||
old_name = data["name"]
|
old_name = data["name"]
|
||||||
color = data["color"]
|
color = data["color"]
|
||||||
|
|
||||||
# Simple input dialog
|
|
||||||
from PySide6.QtWidgets import QInputDialog
|
|
||||||
|
|
||||||
new_name, ok = QInputDialog.getText(
|
new_name, ok = QInputDialog.getText(
|
||||||
self, strings._("edit_tag_name"), strings._("new_tag_name"), text=old_name
|
self, strings._("edit_tag_name"), strings._("new_tag_name"), text=old_name
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -141,6 +141,16 @@ def test_list_all_tags(fresh_db):
|
||||||
assert "tag3" in tag_names
|
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):
|
def test_update_tag_name_and_color(fresh_db):
|
||||||
"""Test updating a tag's name and color"""
|
"""Test updating a tag's name and color"""
|
||||||
date_iso = "2024-01-15"
|
date_iso = "2024-01-15"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue