summaryrefslogtreecommitdiffstats
path: root/ui/main_window.py
blob: d6687eac009cf218f5b5ae32ce93681ae37837de (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
from __future__ import annotations
import subprocess
from PyQt6.QtWidgets import (
    QMainWindow, QWidget, QHBoxLayout, QVBoxLayout,
    QLabel, QPushButton, QStackedWidget, QFrame,
)
from PyQt6.QtCore import Qt, QFileSystemWatcher
from pathlib import Path
from core.config import Config
from core.article_scanner import scan_articles
from core.models import Article

class SidebarButton(QPushButton):
    def __init__(self, text: str, parent=None):
        super().__init__(text, parent)
        self.setFlat(True)
        self.setCheckable(True)
        self.setStyleSheet("""
            QPushButton { text-align: left; padding: 5px 10px; border-radius: 4px; color: #888; }
            QPushButton:checked { background: #2a2a4e; color: #a855f7; }
            QPushButton:hover:!checked { background: #1a1a2e; color: #ccc; }
        """)

class MainWindow(QMainWindow):
    def __init__(self, config: Config, parent=None):
        super().__init__(parent)
        self.config = config
        self._articles: list[Article] = []
        self._watcher = QFileSystemWatcher(self)
        self._watcher.directoryChanged.connect(self._on_fs_change)
        self.setWindowTitle("my-publisher")
        self.setMinimumSize(1100, 700)
        self._build_ui()
        self._refresh_articles()
        self._setup_watcher()

    def _build_ui(self):
        central = QWidget()
        self.setCentralWidget(central)
        root = QHBoxLayout(central)
        root.setContentsMargins(0, 0, 0, 0)
        root.setSpacing(0)

        # Sidebar
        sidebar = self._build_sidebar()
        root.addWidget(sidebar)

        # Divider
        line = QFrame()
        line.setFrameShape(QFrame.Shape.VLine)
        line.setStyleSheet("color: #2a2a4e;")
        root.addWidget(line)

        # Content stack
        self._stack = QStackedWidget()
        root.addWidget(self._stack, stretch=1)

        from ui.articles_view import ArticlesView, MissingTranslationView

        self._articles_view = ArticlesView()
        self._articles_view.article_selected.connect(self._on_article_selected)
        self._stack.addWidget(self._articles_view)
        self._page_articles = self._stack.count() - 1

        self._missing_view = MissingTranslationView()
        self._missing_view.article_selected.connect(self._on_article_selected)
        self._stack.addWidget(self._missing_view)
        self._page_no_translation = self._stack.count() - 1

        from ui.article_detail import ArticleDetailView

        self._detail_view = ArticleDetailView()
        self._detail_view.open_typora.connect(self._do_open_typora)
        self._detail_view.open_frontmatter.connect(self._do_open_frontmatter)
        self._detail_view.translate.connect(self._do_translate)
        self._detail_view.push_master.connect(lambda a: self._do_git_push("master"))
        self._detail_view.publish.connect(lambda a: self._do_git_push("production"))
        self._detail_view.delete_article.connect(self._do_delete_article)
        self._stack.addWidget(self._detail_view)
        self._page_detail = self._stack.count() - 1

        from ui.translation_view import TranslationView

        self._translation_view = TranslationView(
            self.config.transart_script,
            self.config.typora_bin,
            parent=self,
        )
        self._translation_view.push_master.connect(lambda: self._do_git_push("master"))
        self._translation_view.publish.connect(lambda: self._do_git_push("production"))
        self._stack.addWidget(self._translation_view)
        self._page_translations = self._stack.count() - 1

        from ui.git_view import GitView

        self._git_view = GitView(Path(self.config.blog_repo), parent=self)
        self._git_view.refresh_needed.connect(self._refresh_articles)
        self._stack.addWidget(self._git_view)
        self._page_git = self._stack.count() - 1

        from ui.hugo_panel import HugoPanel

        self._hugo_panel = HugoPanel(Path(self.config.blog_repo), parent=self)
        self._stack.addWidget(self._hugo_panel)
        self._page_hugo = self._stack.count() - 1

        from ui.taxonomy_view import TaxonomyView

        self._taxonomy_view = TaxonomyView(Path(self.config.blog_repo), parent=self)
        self._stack.addWidget(self._taxonomy_view)
        self._page_taxonomy = self._stack.count() - 1

        from ui.media_view import MediaView

        self._media_view = MediaView(Path(self.config.blog_repo), parent=self)
        self._stack.addWidget(self._media_view)
        self._page_media = self._stack.count() - 1

    def _build_sidebar(self) -> QWidget:
        w = QWidget()
        w.setFixedWidth(210)
        w.setStyleSheet("background: #1a1a2e;")
        layout = QVBoxLayout(w)
        layout.setContentsMargins(8, 12, 8, 12)
        layout.setSpacing(2)

        header = QHBoxLayout()
        title = QLabel("πŸ“° my-publisher")
        title.setStyleSheet("color: #fff; font-weight: bold; font-size: 13px; padding: 4px 8px;")
        header.addWidget(title)
        self._refresh_btn = QPushButton("πŸ”„")
        self._refresh_btn.setFlat(True)
        self._refresh_btn.setFixedSize(28, 28)
        self._refresh_btn.setStyleSheet("color: #555; border: none;")
        self._refresh_btn.clicked.connect(self._refresh_articles)
        header.addWidget(self._refresh_btn)
        layout.addLayout(header)

        def section(text):
            lbl = QLabel(text)
            lbl.setStyleSheet("color: #a855f7; font-size: 9px; text-transform: uppercase; letter-spacing: 1px; padding: 8px 8px 2px;")
            layout.addWidget(lbl)

        self._btn_group: list[SidebarButton] = []

        def btn(icon_text: str, page_attr: str) -> SidebarButton:
            b = SidebarButton(icon_text)
            b.clicked.connect(lambda: self._show_page(page_attr, b))
            layout.addWidget(b)
            self._btn_group.append(b)
            return b

        section("CONTENUTO")
        btn("πŸ“‹ Articoli", "_page_articles")

        no_trans_row = QHBoxLayout()
        no_trans_row.setContentsMargins(0, 0, 0, 0)
        self._btn_no_trans = SidebarButton("⚠️ Senza Traduzione")
        self._btn_no_trans.clicked.connect(lambda: self._show_page("_page_no_translation", self._btn_no_trans))
        self._btn_group.append(self._btn_no_trans)
        no_trans_row.addWidget(self._btn_no_trans, stretch=1)
        self._badge_no_trans = QLabel("0")
        self._badge_no_trans.setStyleSheet("background:#3a1a1a;color:#ff6b6b;border-radius:8px;padding:1px 6px;font-size:10px;")
        no_trans_row.addWidget(self._badge_no_trans)
        layout.addLayout(no_trans_row)

        b_new = SidebarButton("βž• Nuovo articolo")
        b_new.clicked.connect(self._do_new_article)
        layout.addWidget(b_new)
        self._btn_group.append(b_new)
        btn("🏷 Tassonomia", "_page_taxonomy")
        btn("πŸ–Ό Media", "_page_media")

        section("WORKFLOW")
        btn("🌍 Traduzioni", "_page_translations")
        btn("πŸ”§ Git ops", "_page_git")
        btn("πŸš€ Hugo server", "_page_hugo")

        section("DEPLOY")
        btn("πŸ§ͺ Test (master)", "_page_git")
        btn("🚒 Production", "_page_git")

        layout.addStretch()
        return w

    def _show_page(self, page_attr: str, active_btn: SidebarButton):
        for b in self._btn_group:
            b.setChecked(False)
        active_btn.setChecked(True)
        self._stack.setCurrentIndex(getattr(self, page_attr))

    def _refresh_articles(self):
        if not self.config.blog_repo:
            return
        self._articles = scan_articles(Path(self.config.blog_repo))
        self._articles_view.set_articles(self._articles)
        self._missing_view.set_articles(self._articles)
        missing = [a for a in self._articles if not a.has_translation]
        self._badge_no_trans.setText(str(len(missing)))

    def _on_article_selected(self, article: Article):
        self._detail_view.set_article(article)
        self._stack.setCurrentIndex(self._page_detail)

    def _do_open_typora(self, article: Article):
        subprocess.Popen([self.config.typora_bin, str(article.path)])

    def _do_open_frontmatter(self, article: Article):
        from ui.frontmatter_editor import FrontmatterEditor
        dlg = FrontmatterEditor(article, Path(self.config.blog_repo), self)
        if dlg.exec():
            self._detail_view.set_article(article)

    def _do_new_article(self):
        from ui.new_article_dialog import NewArticleDialog
        dlg = NewArticleDialog(Path(self.config.blog_repo), self)
        if dlg.exec() and dlg.created_path:
            self._refresh_articles()
            subprocess.Popen([self.config.typora_bin, str(dlg.created_path)])

    def _do_translate(self, article: Article):
        self._translation_view.start_translation(article)
        self._stack.setCurrentIndex(self._page_translations)

    def _do_git_push(self, branch: str):
        from workers.git_worker import GitWorker
        repo = Path(self.config.blog_repo)
        if branch == "master":
            worker = GitWorker.push_master(repo, self)
        else:
            worker = GitWorker.push_production(repo, self)
        worker.error.connect(lambda e: self.statusBar().showMessage(f"Git error: {e}", 5000))
        worker.finished.connect(lambda ok: self.statusBar().showMessage("Push completato." if ok else "Push fallito.", 4000))
        worker.start()

    def _do_delete_article(self, article: Article):
        from PyQt6.QtWidgets import QMessageBox
        from workers.git_worker import GitWorker
        reply = QMessageBox.question(
            self, "Elimina articolo",
            f"Eliminare '{article.slug}' ({article.lang})? L'operazione sarΓ  tracciata in git.",
        )
        if reply == QMessageBox.StandardButton.Yes:
            rel = str(article.path.parent.relative_to(Path(self.config.blog_repo)))
            worker = GitWorker.remove_article(
                Path(self.config.blog_repo), rel, article.slug, article.lang, self
            )
            worker.error.connect(lambda e: self.statusBar().showMessage(f"Errore: {e}", 5000))
            worker.finished.connect(lambda ok: self._refresh_articles() if ok else None)
            worker.start()

    def _setup_watcher(self):
        if not self.config.blog_repo:
            return
        root = Path(self.config.blog_repo)
        for lang in ("it", "en"):
            d = root / "content" / lang / "articles"
            if d.exists():
                self._watcher.addPath(str(d))

    def _on_fs_change(self, _path: str):
        self._refresh_articles()

    def closeEvent(self, event):
        self._hugo_panel._worker.stop()
        super().closeEvent(event)