From 533cca1bcf44b3f136bbc5f1b3fc2ecd83d3c681 Mon Sep 17 00:00:00 2001 From: Kevin Hendricks Date: Fri, 18 Oct 2024 13:24:44 -0400 Subject: [PATCH] Create a GetInfo like dialog using a Markdown and an MDViewer widget --- src/CMakeLists.txt | 3 + src/Dialogs/MDViewer.cpp | 63 +++++++++++++++++ src/Dialogs/MDViewer.h | 47 +++++++++++++ src/Form_Files/MDViewer.ui | 73 ++++++++++++++++++++ src/MainUI/BookBrowser.cpp | 138 +++++++++++++++++++++++++++++++++++-- src/MainUI/BookBrowser.h | 9 +++ 6 files changed, 328 insertions(+), 5 deletions(-) create mode 100644 src/Dialogs/MDViewer.cpp create mode 100644 src/Dialogs/MDViewer.h create mode 100644 src/Form_Files/MDViewer.ui diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 3e2be2ee45..527d910d49 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -98,6 +98,8 @@ set( DIALOG_FILES Dialogs/EmptyLayout.h Dialogs/ManageRepos.cpp Dialogs/ManageRepos.h + Dialogs/MDViewer.cpp + Dialogs/MDViewer.h Dialogs/OpenWithName.cpp Dialogs/OpenWithName.h Dialogs/RepoLog.cpp @@ -521,6 +523,7 @@ set( UI_FILES Form_Files/DeleteFiles.ui Form_Files/DeleteStyles.ui Form_Files/ManageRepos.ui + Form_Files/MDViewer.ui Form_Files/OpenWithName.ui Form_Files/RERenamer.ui Form_Files/RETable.ui diff --git a/src/Dialogs/MDViewer.cpp b/src/Dialogs/MDViewer.cpp new file mode 100644 index 0000000000..22c8e338b4 --- /dev/null +++ b/src/Dialogs/MDViewer.cpp @@ -0,0 +1,63 @@ +/************************************************************************ +** +** Copyright (C) 2024 Kevin B. Hendricks, Stratford Ontario Canada +** +** This file is part of Sigil. +** +** Sigil is free software: you can redistribute it and/or modify +** it under the terms of the GNU General Public License as published by +** the Free Software Foundation, either version 3 of the License, or +** (at your option) any later version. +** +** Sigil is distributed in the hope that it will be useful, +** but WITHOUT ANY WARRANTY; without even the implied warranty of +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +** GNU General Public License for more details. +** +** You should have received a copy of the GNU General Public License +** along with Sigil. If not, see . +** +*************************************************************************/ + +#include + +#include "Dialogs/MDViewer.h" +#include "ResourceObjects/HTMLResource.h" +#include "Misc/SettingsStore.h" + +static QString SETTINGS_GROUP = "markdown_viewer"; + +MDViewer::MDViewer(const QString & mdsrc, QWidget *parent) + : + QDialog(parent) +{ + ui.setupUi(this); + connectSignalsSlots(); + ReadSettings(); + ui.viewer->setMarkdown(mdsrc); + ui.viewer->setReadOnly(true); +} + +void MDViewer::ReadSettings() +{ + SettingsStore settings; + settings.beginGroup(SETTINGS_GROUP); + QByteArray geometry = settings.value("geometry").toByteArray(); + if (!geometry.isNull()) { + restoreGeometry(geometry); + } + settings.endGroup(); +} + +void MDViewer::WriteSettings() +{ + SettingsStore settings; + settings.beginGroup(SETTINGS_GROUP); + settings.setValue("geometry", saveGeometry()); + settings.endGroup(); +} + +void MDViewer::connectSignalsSlots() +{ + connect(this, SIGNAL(accepted()), this, SLOT(WriteSettings())); +} diff --git a/src/Dialogs/MDViewer.h b/src/Dialogs/MDViewer.h new file mode 100644 index 0000000000..e0c9fad3c8 --- /dev/null +++ b/src/Dialogs/MDViewer.h @@ -0,0 +1,47 @@ +/************************************************************************ +** +** Copyright (C) 2024 Kevin B. Hendricks, Stratford Ontario Canada +** +** This file is part of Sigil. +** +** Sigil is free software: you can redistribute it and/or modify +** it under the terms of the GNU General Public License as published by +** the Free Software Foundation, either version 3 of the License, or +** (at your option) any later version. +** +** Sigil is distributed in the hope that it will be useful, +** but WITHOUT ANY WARRANTY; without even the implied warranty of +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +** GNU General Public License for more details. +** +** You should have received a copy of the GNU General Public License +** along with Sigil. If not, see . +** +*************************************************************************/ + +#pragma once +#ifndef MDVIEWER_H +#define MDVIEWER_H + +#include +#include +#include +#include "ui_MDViewer.h" + +class MDViewer: public QDialog +{ + Q_OBJECT + +public: + MDViewer(const QString & mdsrc,QWidget *parent = 0); + +private slots: + void WriteSettings(); + +private: + void ReadSettings(); + void connectSignalsSlots(); + Ui::MDViewer ui; +}; + +#endif // MDVIEWER_H diff --git a/src/Form_Files/MDViewer.ui b/src/Form_Files/MDViewer.ui new file mode 100644 index 0000000000..076eea2236 --- /dev/null +++ b/src/Form_Files/MDViewer.ui @@ -0,0 +1,73 @@ + + + MDViewer + + + + 0 + 0 + 504 + 504 + + + + Information + + + + + + + 0 + 1 + + + + Qt::WheelFocus + + + true + + + + + + + Qt::Vertical + + + + 20 + 5 + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + + buttonBox + accepted() + MDViewer + accept() + + + buttonBox + rejected() + MDViewer + reject() + + + diff --git a/src/MainUI/BookBrowser.cpp b/src/MainUI/BookBrowser.cpp index 133cf92d81..26ed73e8e2 100644 --- a/src/MainUI/BookBrowser.cpp +++ b/src/MainUI/BookBrowser.cpp @@ -39,6 +39,7 @@ #include "Dialogs/DeleteFiles.h" #include "Dialogs/RenameTemplate.h" #include "Dialogs/SemanticTargetID.h" +#include "Dialogs/MDViewer.h" #include "Dialogs/AddSemantics.h" #include "Dialogs/SelectFolder.h" #include "Dialogs/RERenamer.h" @@ -1671,7 +1672,128 @@ void BookBrowser::SetCoverImage() m_TreeView->verticalScrollBar()->setSliderPosition(scrollY); } +QString BookBrowser::BuildListMD(const QStringList& lst) +{ + QString res=""; + foreach(QString rec, lst) { + res = res + "- " + rec + "\n"; + } + return res; +} + +void BookBrowser::GetInfo() +{ + QList resources = ValidSelectedResources(); + int scrollY = m_TreeView->verticalScrollBar()->value(); + + if (resources.count()!= 1) { + return; + } + + Resource * resource = resources.first(); + if (resource->Type() != Resource::HTMLResourceType) { + return; + } + + HTMLResource *html_resource = qobject_cast(resource); + if (!html_resource) return; + + QString bookpath = resource->GetRelativePath(); + QString primary_lang = html_resource->GetLanguageAttribute(); + QString fullfilepath = resource->GetFullPath(); + QString version = resource->GetEpubVersion(); + double ffsize = QFile(fullfilepath).size() / 1024.0; + QString fsize = QLocale().toString(ffsize, 'f', 2); + QString source = html_resource->GetText(); + + QStringList mdlst; + mdlst << "# "+ bookpath + "\n"; + + mdlst << tr("FileName"); + mdlst << "- " + resource->Filename() + "\n"; + + mdlst << tr("Folder"); + mdlst << "- " + resource->GetFolder() + "\n"; + + mdlst << tr("Media Type"); + mdlst << "- " + resource->GetMediaType() + "\n"; + + mdlst << tr("Epub Version"); + mdlst << "- " + version + "\n"; + + mdlst << tr("Primary Language"); + mdlst << "- " + html_resource->GetLanguageAttribute() + "\n"; + mdlst << tr("File Size(kb)"); + mdlst << "- " + fsize + "\n"; + + mdlst << tr("WellFormed"); + if (html_resource->FileIsWellFormed()) { + mdlst << "- " + tr("Yes") + "\n"; + } else { + mdlst << "- " + tr("No") + "\n"; + } + + mdlst << tr("Linked Stylesheets"); + mdlst << BuildListMD(XhtmlDoc::GetLinkedStylesheets(source)) + "\n"; + + mdlst << tr("Linked Javascripts"); + mdlst << BuildListMD(XhtmlDoc::GetLinkedJavascripts(source)) + "\n"; + + mdlst << tr("Linked Images"); + mdlst << BuildListMD(XhtmlDoc::GetAllMediaPathsFromMediaChildren(source, GIMAGE_TAGS)) + "\n"; + + mdlst << tr("Linked Audio"); + mdlst << BuildListMD(XhtmlDoc::GetAllMediaPathsFromMediaChildren(source, GAUDIO_TAGS)) + "\n"; + + mdlst << tr("Linked Video"); + mdlst << BuildListMD( XhtmlDoc::GetAllMediaPathsFromMediaChildren(source, GVIDEO_TAGS)) + "\n"; + + // semantics + HTMLResource * nav_resource = nullptr; + if (version.startsWith('3')) { + nav_resource = m_Book->GetConstOPF()->GetNavResource(); + } + QStringList full_semantic_info; + QStringList semantics; + if (version.startsWith("3")) { + NavProcessor navproc(nav_resource); + full_semantic_info = navproc.GetAllLandmarkInfoByBookPath(); + } else { + full_semantic_info = m_Book->GetOPF()->GetAllGuideInfoByBookPath(); + } + foreach(QString rec, full_semantic_info) { + QStringList parts = rec.split(QChar(30), Qt::KeepEmptyParts); + qDebug() << parts.at(0) << parts.at(1) << parts.at(2) << parts.at(3); + if (parts.at(0) == bookpath) { + if (parts.at(1).isEmpty()) { + semantics << parts.at(2) + ": " + parts.at(3); + } else { + semantics << parts.at(2) + ": " + parts.at(3) + " id=\"" + parts.at(1) + "\""; + } + } + } + mdlst << tr("Semantics OPF Guide or Nav Landmarks"); + mdlst << BuildListMD(semantics) + "\n"; + + // manifest properties + QStringList properties; + if (version.startsWith('3')) { + properties = html_resource->GetManifestProperties(); + if (html_resource == nav_resource) properties << "nav"; + } + mdlst << tr("Manifest Properties"); + mdlst << BuildListMD(properties) + "\n"; + + mdlst << tr("Defined Ids"); + mdlst << BuildListMD(XhtmlDoc::GetAllDescendantIDs(source)) + "\n"; + + QString mdsrc = mdlst.join("\n"); + + MDViewer mdv(mdsrc); + mdv.exec(); +} + void BookBrowser::AddSemanticCode() { QList resources = ValidSelectedResources(); @@ -1903,14 +2025,15 @@ void BookBrowser::CreateContextMenuActions() m_LinkStylesheets = new QAction(tr("Link Stylesheets..."), this); m_LinkJavascripts = new QAction(tr("Link Javascripts..."), this); m_AddSemantics = new QAction(tr("Add Semantics..."), this); + m_GetInfo = new QAction(tr("Get Info..."), this); m_ValidateWithW3C = new QAction(tr("Validate with W3C"), this); m_OpenWith = new QAction(tr("Open With") + "...", this); m_SaveAs = new QAction(tr("Save As") + "...", this); - m_OpenWithEditor0 = new QAction("", this); - m_OpenWithEditor1 = new QAction("", this); - m_OpenWithEditor2 = new QAction("", this); - m_OpenWithEditor3 = new QAction("", this); - m_OpenWithEditor4 = new QAction("", this); + m_OpenWithEditor0 = new QAction("", this); + m_OpenWithEditor1 = new QAction("", this); + m_OpenWithEditor2 = new QAction("", this); + m_OpenWithEditor3 = new QAction("", this); + m_OpenWithEditor4 = new QAction("", this); m_CoverImage ->setCheckable(true); m_NoObfuscationMethod ->setCheckable(true); m_AdobesObfuscationMethod->setCheckable(true); @@ -1935,6 +2058,8 @@ void BookBrowser::CreateContextMenuActions() sm->registerAction(this, m_LinkJavascripts, "MainWindow.BookBrowser.LinkJavascripts"); m_AddSemantics->setToolTip(tr("Add Semantics to selected file(s).")); sm->registerAction(this, m_AddSemantics, "MainWindow.BookBrowser.AddSemantics"); + m_GetInfo->setToolTip(tr("Show Information about selected file.")); + sm->registerAction(this, m_GetInfo, "MainWindow.BookBrowser.GetInfo"); // Has to be added to the book browser itself as well // for the keyboard shortcut to work. addAction(m_CopyHTML); @@ -1946,6 +2071,7 @@ void BookBrowser::CreateContextMenuActions() addAction(m_LinkStylesheets); addAction(m_LinkJavascripts); addAction(m_AddSemantics); + addAction(m_GetInfo); } @@ -1994,6 +2120,7 @@ bool BookBrowser::SuccessfullySetupContextMenu(const QPoint &point) m_ContextMenu->addAction(m_LinkJavascripts); m_LinkJavascripts->setEnabled(AllJSResources().count() > 0); m_ContextMenu->addAction(m_AddSemantics); + m_ContextMenu->addAction(m_GetInfo); } if (resource->Type() == Resource::FontResourceType) { @@ -2193,6 +2320,7 @@ void BookBrowser::ConnectSignalsToSlots() connect(m_LinkStylesheets, SIGNAL(triggered()), this, SLOT(LinkStylesheets())); connect(m_LinkJavascripts, SIGNAL(triggered()), this, SLOT(LinkJavascripts())); connect(m_AddSemantics, SIGNAL(triggered()), this, SLOT(AddSemanticCode())); + connect(m_GetInfo, SIGNAL(triggered()), this, SLOT(GetInfo())); connect(m_SaveAs, SIGNAL(triggered()), this, SLOT(SaveAs())); connect(m_ValidateWithW3C, SIGNAL(triggered()), this, SLOT(ValidateStylesheetWithW3C())); connect(m_OpenWith, SIGNAL(triggered()), this, SLOT(OpenWith())); diff --git a/src/MainUI/BookBrowser.h b/src/MainUI/BookBrowser.h index a7ee5bd03a..0ec537580f 100644 --- a/src/MainUI/BookBrowser.h +++ b/src/MainUI/BookBrowser.h @@ -350,6 +350,14 @@ private slots: */ void AddSemanticCode(); + /** + * display information on the currently + * selected resource. + */ + QString BuildListMD(const QStringList& lst); + void GetInfo(); + + /** * Implements the Merge context menu action functionality. */ @@ -533,6 +541,7 @@ private slots: QAction *m_LinkStylesheets; QAction *m_LinkJavascripts; QAction *m_AddSemantics; + QAction *m_GetInfo; QAction *m_SaveAs; QAction *m_ValidateWithW3C;