Skip to content

Commit

Permalink
Create a GetInfo like dialog using a Markdown and an MDViewer widget
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinhendricks committed Oct 18, 2024
1 parent 40e9c16 commit 533cca1
Show file tree
Hide file tree
Showing 6 changed files with 328 additions and 5 deletions.
3 changes: 3 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
63 changes: 63 additions & 0 deletions src/Dialogs/MDViewer.cpp
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
**
*************************************************************************/

#include <QLineEdit>

#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()));
}
47 changes: 47 additions & 0 deletions src/Dialogs/MDViewer.h
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
**
*************************************************************************/

#pragma once
#ifndef MDVIEWER_H
#define MDVIEWER_H

#include <QDialog>
#include <QString>
#include <QTextEdit>
#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
73 changes: 73 additions & 0 deletions src/Form_Files/MDViewer.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MDViewer</class>
<widget class="QDialog" name="MDViewer">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>504</width>
<height>504</height>
</rect>
</property>
<property name="windowTitle">
<string>Information</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QTextEdit" name="viewer">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>1</verstretch>
</sizepolicy>
</property>
<property name="focusPolicy">
<enum>Qt::WheelFocus</enum>
</property>
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>5</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>MDViewer</receiver>
<slot>accept()</slot>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>MDViewer</receiver>
<slot>reject()</slot>
</connection>
</connections>
</ui>
138 changes: 133 additions & 5 deletions src/MainUI/BookBrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 <Resource *> 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<HTMLResource *>(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 <Resource *> resources = ValidSelectedResources();
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -1946,6 +2071,7 @@ void BookBrowser::CreateContextMenuActions()
addAction(m_LinkStylesheets);
addAction(m_LinkJavascripts);
addAction(m_AddSemantics);
addAction(m_GetInfo);
}


Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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()));
Expand Down
Loading

1 comment on commit 533cca1

@kevinhendricks
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dougmassay
I created an HTMLResource "GetInfo" dialog using Markdown and a MDViewer widget which uses a QTextEdit in ReadOnly mode.

I tried to include much of the information from Reports widgets but focused on just one file at a time so it will be much faster.
I expanded it to include full Semantic Information as well.

So when you get a free moment, please load an epub2 or epub3 that has semantic info, right click on the file in BookBrowser (just an xhtml file), and select GetInfo.

Take a peak at what output is created.

The code to handle this is in BookBrowser::GetInfo() routine. It is very very simple right now. Basically a list format. I am no Markdown expert but this approach seems quite easy to expand on. So let me know if you think some of this information should go away and others pieces of infomation added. Also feel free to make the Markdown code I generated a lot fancier if you like.

Part of me want to make this a show Semantics only routine and remove all of the extra stuff, but I am unsure if it would be interesting enough on its own.

Please sign in to comment.