from PySide6.QtWidgets import (QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QTextEdit, QListWidget, QListWidgetItem, QLabel, QMenu, QMessageBox, QDialog) from PySide6.QtCore import Qt, Signal, QPoint from PySide6.QtGui import QAction from database import get_session from models import Comment, Document from datetime import datetime class CommentWidget(QWidget): comment_updated = Signal() def __init__(self, comment, parent=None): super().__init__(parent) self.comment = comment self.setup_ui() def setup_ui(self): layout = QVBoxLayout(self) layout.setContentsMargins(5, 5, 5, 5) # Header with timestamp and options header_layout = QHBoxLayout() timestamp = QLabel(self.comment.created_at.strftime("%d/%m/%Y %H:%M")) timestamp.setStyleSheet("color: gray; font-size: 10px;") header_layout.addWidget(timestamp) if self.comment.updated_at and self.comment.updated_at != self.comment.created_at: edited_label = QLabel("(editado)") edited_label.setStyleSheet("color: gray; font-size: 10px;") header_layout.addWidget(edited_label) header_layout.addStretch() # Options button options_btn = QPushButton("...") options_btn.setFixedWidth(30) options_btn.clicked.connect(self.show_options) header_layout.addWidget(options_btn) layout.addLayout(header_layout) # Comment text text_label = QLabel(self.comment.text) text_label.setWordWrap(True) layout.addWidget(text_label) # Location info if available if self.comment.page_number: location = f"Página {self.comment.page_number}" if self.comment.x_coord and self.comment.y_coord: location += f" (x: {self.comment.x_coord}, y: {self.comment.y_coord})" location_label = QLabel(location) location_label.setStyleSheet("color: gray; font-style: italic; font-size: 10px;") layout.addWidget(location_label) def show_options(self): menu = QMenu(self) edit_action = QAction("Editar", self) edit_action.triggered.connect(self.edit_comment) menu.addAction(edit_action) delete_action = QAction("Excluir", self) delete_action.triggered.connect(self.delete_comment) menu.addAction(delete_action) # Show menu at button position button = self.sender() pos = button.mapToGlobal(QPoint(0, button.height())) menu.exec_(pos) def edit_comment(self): dialog = CommentDialog(self.comment.text, self) if dialog.exec_(): try: session = get_session() comment = session.query(Comment).get(self.comment.id) comment.text = dialog.comment_text() comment.updated_at = datetime.utcnow() session.commit() self.comment_updated.emit() except Exception as e: QMessageBox.critical(self, "Erro", f"Erro ao editar comentário: {str(e)}") finally: session.close() def delete_comment(self): reply = QMessageBox.question( self, "Confirmar Exclusão", "Tem certeza que deseja excluir este comentário?", QMessageBox.Yes | QMessageBox.No ) if reply == QMessageBox.Yes: try: session = get_session() comment = session.query(Comment).get(self.comment.id) session.delete(comment) session.commit() self.comment_updated.emit() except Exception as e: QMessageBox.critical(self, "Erro", f"Erro ao excluir comentário: {str(e)}") finally: session.close() class CommentsList(QWidget): def __init__(self, parent=None): super().__init__(parent) self.current_document = None self.setup_ui() def setup_ui(self): layout = QVBoxLayout(self) # Header header_layout = QHBoxLayout() header_layout.addWidget(QLabel("Comentários")) self.add_comment_btn = QPushButton("Novo Comentário") self.add_comment_btn.clicked.connect(self.add_comment) header_layout.addWidget(self.add_comment_btn) layout.addLayout(header_layout) # Comments list self.comments_list = QListWidget() layout.addWidget(self.comments_list) def load_document(self, doc_id): self.current_document = doc_id self.refresh_comments() def refresh_comments(self): self.comments_list.clear() if self.current_document: session = get_session() document = session.query(Document).get(self.current_document) for comment in document.comments: item = QListWidgetItem() widget = CommentWidget(comment) widget.comment_updated.connect(self.refresh_comments) item.setSizeHint(widget.sizeHint()) self.comments_list.addItem(item) self.comments_list.setItemWidget(item, widget) session.close() def add_comment(self): if not self.current_document: return dialog = CommentDialog(parent=self) if dialog.exec_(): try: session = get_session() comment = Comment( document_id=self.current_document, text=dialog.comment_text() ) session.add(comment) session.commit() self.refresh_comments() except Exception as e: QMessageBox.critical(self, "Erro", f"Erro ao adicionar comentário: {str(e)}") finally: session.close() class CommentDialog(QDialog): def __init__(self, initial_text="", parent=None): super().__init__(parent) self.initial_text = initial_text self.setup_ui() def setup_ui(self): self.setWindowTitle("Comentário") layout = QVBoxLayout(self) self.text_edit = QTextEdit() self.text_edit.setPlainText(self.initial_text) layout.addWidget(self.text_edit) buttons_layout = QHBoxLayout() cancel_btn = QPushButton("Cancelar") cancel_btn.clicked.connect(self.reject) buttons_layout.addWidget(cancel_btn) save_btn = QPushButton("Salvar") save_btn.clicked.connect(self.accept) buttons_layout.addWidget(save_btn) layout.addLayout(buttons_layout) def comment_text(self): return self.text_edit.toPlainText()