summaryrefslogtreecommitdiffstats
path: root/ui/media_view.py
blob: c77b808d5269a31c57259a76b86a9344d5fadfc2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
from __future__ import annotations
import shutil
from datetime import date
from pathlib import Path
from PyQt6.QtWidgets import (
    QWidget, QVBoxLayout, QHBoxLayout,
    QLabel, QPushButton, QListWidget, QListWidgetItem, QFileDialog,
)
from PyQt6.QtCore import Qt
from PyQt6.QtGui import QDragEnterEvent, QDropEvent

class MediaView(QWidget):
    def __init__(self, blog_root: Path, parent=None):
        super().__init__(parent)
        self._blog_root = blog_root
        self.setAcceptDrops(True)
        self._build_ui()
        self._refresh_list()

    def _target_dir(self) -> Path:
        today = date.today()
        return self._blog_root / "static" / "uppies" / str(today.year) / f"{today.month:02d}"

    def _build_ui(self):
        layout = QVBoxLayout(self)

        header = QLabel("🖼 Media — static/uppies/YYYY/MM/")
        header.setStyleSheet("color:#fff;font-weight:bold;font-size:13px;padding:10px;")
        layout.addWidget(header)

        drop_zone = QLabel("Trascina file qui, oppure usa il pulsante sotto.")
        drop_zone.setAlignment(Qt.AlignmentFlag.AlignCenter)
        drop_zone.setStyleSheet("border:2px dashed #2a2a4e;color:#555;padding:20px;border-radius:6px;margin:10px;")
        drop_zone.setMinimumHeight(80)
        layout.addWidget(drop_zone)

        btns = QHBoxLayout()
        add_btn = QPushButton("📂 Aggiungi file...")
        add_btn.clicked.connect(self._pick_files)
        btns.addWidget(add_btn)
        btns.addStretch()
        self._path_label = QLabel("")
        self._path_label.setStyleSheet("color:#a855f7;font-size:10px;")
        btns.addWidget(self._path_label)
        layout.addLayout(btns)

        self._list = QListWidget()
        layout.addWidget(self._list, stretch=1)

        self._status = QLabel("")
        self._status.setStyleSheet("color:#888;font-size:10px;padding:4px;")
        layout.addWidget(self._status)

    def _copy_files(self, paths: list[str]):
        target = self._target_dir()
        target.mkdir(parents=True, exist_ok=True)
        last_web_path = ""
        for src in paths:
            dest = target / Path(src).name
            shutil.copy2(src, dest)
            today = date.today()
            last_web_path = f"/uppies/{today.year}/{today.month:02d}/{Path(src).name}"

        if last_web_path:
            from PyQt6.QtWidgets import QApplication
            QApplication.clipboard().setText(last_web_path)
            self._status.setText(f"Copiato in clipboard: {last_web_path}")

        self._refresh_list()

    def _pick_files(self):
        files, _ = QFileDialog.getOpenFileNames(self, "Seleziona file media")
        if files:
            self._copy_files(files)

    def _refresh_list(self):
        self._list.clear()
        target = self._target_dir()
        today = date.today()
        self._path_label.setText(f"Cartella: static/uppies/{today.year}/{today.month:02d}/")
        if not target.exists():
            return
        for f in sorted(target.iterdir()):
            if f.is_file():
                self._list.addItem(QListWidgetItem(f.name))

    def dragEnterEvent(self, event: QDragEnterEvent):
        if event.mimeData().hasUrls():
            event.acceptProposedAction()

    def dropEvent(self, event: QDropEvent):
        paths = [u.toLocalFile() for u in event.mimeData().urls() if u.isLocalFile()]
        if paths:
            self._copy_files(paths)