file drop import dialog
This commit is contained in:
64
main.py
64
main.py
@@ -3,6 +3,7 @@ import sys
|
|||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
import fitz
|
||||||
|
|
||||||
from PySide2.QtCore import (
|
from PySide2.QtCore import (
|
||||||
QAbstractItemModel,
|
QAbstractItemModel,
|
||||||
@@ -16,16 +17,20 @@ from PySide2.QtCore import (
|
|||||||
|
|
||||||
from PySide2.QtWidgets import (
|
from PySide2.QtWidgets import (
|
||||||
QApplication,
|
QApplication,
|
||||||
|
QDialog,
|
||||||
QDockWidget,
|
QDockWidget,
|
||||||
QFileDialog,
|
QFileDialog,
|
||||||
|
QLabel,
|
||||||
QMainWindow,
|
QMainWindow,
|
||||||
QPushButton,
|
QPushButton,
|
||||||
QTreeView,
|
QTreeView,
|
||||||
|
QTreeWidget,
|
||||||
|
QTreeWidgetItem,
|
||||||
QVBoxLayout,
|
QVBoxLayout,
|
||||||
QWidget,
|
QWidget,
|
||||||
)
|
)
|
||||||
|
|
||||||
from PySide2.QtGui import QIcon, QImage, QPixmap
|
from PySide2.QtGui import QDragEnterEvent, QDropEvent, QIcon, QImage, QPixmap, QStandardItemModel
|
||||||
from PySide2.QtWebChannel import QWebChannel
|
from PySide2.QtWebChannel import QWebChannel
|
||||||
from PySide2.QtWebEngineWidgets import QWebEngineView
|
from PySide2.QtWebEngineWidgets import QWebEngineView
|
||||||
|
|
||||||
@@ -50,7 +55,7 @@ class Bridge(QObject):
|
|||||||
class TreeItem:
|
class TreeItem:
|
||||||
name: str = "Untitled"
|
name: str = "Untitled"
|
||||||
parent: "TreeItem" = None
|
parent: "TreeItem" = None
|
||||||
children: list["TreeItem"] = []
|
children: list["TreeItem"] = list
|
||||||
|
|
||||||
def row(self):
|
def row(self):
|
||||||
return self.parent.children.index(self) if self.parent else 0
|
return self.parent.children.index(self) if self.parent else 0
|
||||||
@@ -156,11 +161,47 @@ class LibraryModel(QAbstractItemModel):
|
|||||||
def supportedDropActions(self) -> Qt.DropActions:
|
def supportedDropActions(self) -> Qt.DropActions:
|
||||||
return Qt.CopyAction | Qt.MoveAction
|
return Qt.CopyAction | Qt.MoveAction
|
||||||
|
|
||||||
|
class ImportPDFDialog(QDialog):
|
||||||
|
def __init__(self, fpath):
|
||||||
|
super().__init__()
|
||||||
|
|
||||||
|
self.layout = QVBoxLayout()
|
||||||
|
self.setLayout(self.layout)
|
||||||
|
|
||||||
|
doc = fitz.Document(fpath)
|
||||||
|
toc = doc.get_toc()
|
||||||
|
|
||||||
|
self.tree = QTreeWidget()
|
||||||
|
self.layout.addWidget(self.tree)
|
||||||
|
self.tree.setColumnCount(1)
|
||||||
|
|
||||||
|
items: list[QTreeWidgetItem] = []
|
||||||
|
last_level = 1
|
||||||
|
for level, title, page in toc:
|
||||||
|
item = QTreeWidgetItem([f'{title} ({page})'])
|
||||||
|
|
||||||
|
if level == 1 or level > last_level:
|
||||||
|
print('>', level, title, page)
|
||||||
|
items.append(item)
|
||||||
|
else:
|
||||||
|
print('<', level, title, page)
|
||||||
|
items[-1].addChild(item)
|
||||||
|
|
||||||
|
last_level = level
|
||||||
|
|
||||||
|
#from pprint import pprint
|
||||||
|
#pprint(items)
|
||||||
|
|
||||||
|
self.tree.insertTopLevelItems(0, items)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class MainWindow(QMainWindow):
|
class MainWindow(QMainWindow):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
|
||||||
|
self.setAcceptDrops(True)
|
||||||
|
|
||||||
self.widget = QWidget()
|
self.widget = QWidget()
|
||||||
self.setCentralWidget(self.widget)
|
self.setCentralWidget(self.widget)
|
||||||
|
|
||||||
@@ -171,6 +212,7 @@ class MainWindow(QMainWindow):
|
|||||||
self._layout.addWidget(self.web_view)
|
self._layout.addWidget(self.web_view)
|
||||||
|
|
||||||
self.dock_widget = QDockWidget("Library")
|
self.dock_widget = QDockWidget("Library")
|
||||||
|
self.addDockWidget(Qt.LeftDockWidgetArea, self.dock_widget)
|
||||||
|
|
||||||
self.dock_qwidget = QWidget()
|
self.dock_qwidget = QWidget()
|
||||||
self.dock_widget.setWidget(self.dock_qwidget)
|
self.dock_widget.setWidget(self.dock_qwidget)
|
||||||
@@ -212,8 +254,6 @@ class MainWindow(QMainWindow):
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
self.addDockWidget(Qt.LeftDockWidgetArea, self.dock_widget)
|
|
||||||
|
|
||||||
self.web_view.load(qurl_from_local("pdf.js/build/generic/web/viewer.html"))
|
self.web_view.load(qurl_from_local("pdf.js/build/generic/web/viewer.html"))
|
||||||
|
|
||||||
def on_load():
|
def on_load():
|
||||||
@@ -256,6 +296,22 @@ class MainWindow(QMainWindow):
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def import_pdf(self, fpath):
|
||||||
|
self._import_pdf_dialog = ImportPDFDialog(fpath)
|
||||||
|
self._import_pdf_dialog.show()
|
||||||
|
self._import_pdf_dialog.raise_()
|
||||||
|
self._import_pdf_dialog.activateWindow()
|
||||||
|
|
||||||
|
def dragEnterEvent(self, event: QDragEnterEvent) -> None:
|
||||||
|
if event.mimeData().hasUrls():
|
||||||
|
event.acceptProposedAction()
|
||||||
|
|
||||||
|
def dropEvent(self, event: QDropEvent) -> None:
|
||||||
|
urls = event.mimeData().urls()
|
||||||
|
url = urls[0].toLocalFile()
|
||||||
|
if url.endswith('.pdf'):
|
||||||
|
self.import_pdf(url)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
app = QApplication()
|
app = QApplication()
|
||||||
|
|||||||
Reference in New Issue
Block a user