Skip to content

Commit

Permalink
sync bug fixes from Sigil SelectFiles to PageEdit
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinhendricks committed Jul 25, 2024
1 parent 5c7b2ca commit ff81edc
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 37 deletions.
63 changes: 36 additions & 27 deletions SelectFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <QEventLoop>
#include <QImage>
#include <QPixmap>
#include <QTimer>
#include <QtWidgets/QLayout>
#include <QtWebEngineWidgets>
#include <QtWebEngineCore>
Expand Down Expand Up @@ -96,22 +97,33 @@ SelectFiles::SelectFiles(QString title,
m_MediaKind(mediakind),
m_MediaBase(mediabase),
m_SelectFilesModel(new QStandardItemModel),
m_PreviewReady(false),
m_PreviewReady(true),
m_PreviewLoaded(false),
m_ThumbnailSize(THUMBNAIL_SIZE),
m_ImageChangedTimer(new QTimer()),
m_WebView(new QWebEngineView(this))
{
ui.setupUi(this);
setWindowTitle(title);
QWebEngineProfile* profile = WebProfileMgr::instance()->GetOneTimeProfile();
m_WebView->setPage(new SimplePage(profile, m_WebView));
m_WebView->setContextMenuPolicy(Qt::NoContextMenu);
m_WebView->setFocusPolicy(Qt::NoFocus);
m_WebView->setAcceptDrops(false);
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
m_WebView->page()->settings()->setAttribute(QWebEngineSettings::ShowScrollBars,false);
#endif
ui.avLayout->addWidget(m_WebView);
ui.Details->setFocusPolicy(Qt::NoFocus);
m_WebView->setFocusPolicy(Qt::NoFocus);
ui.imageTree->setFocusPolicy(Qt::StrongFocus);

// set up ImageChangedTimer to absorb multiple selection
// changed signals since loading the WebView is slow
m_ImageChangedTimer->setSingleShot(true);
m_ImageChangedTimer->setInterval(20);
connect(m_ImageChangedTimer, SIGNAL(timeout()),this, SLOT(SetPreviewImage()));
m_ImageChangedTimer->stop();


ReadSettings();

Expand All @@ -127,6 +139,7 @@ SelectFiles::SelectFiles(QString title,
connectSignalsSlots();

SetPreviewImage();
ui.imageTree->setFocus();
}

SelectFiles::~SelectFiles()
Expand Down Expand Up @@ -159,6 +172,7 @@ void SelectFiles::SetImages()
}
m_WebView->setHtml(html, QUrl());

ui.imageTree->reset();
m_SelectFilesModel->clear();
QStringList header;
header.append(tr("Media Files In the Book"));
Expand All @@ -184,7 +198,7 @@ void SelectFiles::SetImages()
// Don't show resources not matching the selected kind
if ((m_ImageItem->isSelected() && mkind != "image" && mkind != "svgimage") ||
(m_VideoItem->isSelected() && mkind != "video") ||
(m_AudioItem->isSelected() && mkind != "audio")) {
(m_AudioItem->isSelected() && mkind != "audio")) {
continue;
}

Expand All @@ -210,7 +224,7 @@ void SelectFiles::SetImages()
pixmap = pixmap.scaled(QSize(m_ThumbnailSize, m_ThumbnailSize), Qt::KeepAspectRatio);
}
QStandardItem *icon_item = new QStandardItem();
icon_item->setIcon(QIcon(pixmap));
icon_item->setData(QVariant(pixmap), Qt::DecorationRole);
icon_item->setEditable(false);
rowItems << icon_item;
}
Expand Down Expand Up @@ -276,9 +290,8 @@ void SelectFiles::DecreaseThumbnailSize()
void SelectFiles::ReloadPreview()
{
// Make sure we don't load when initial painting is resizing
if (m_PreviewReady) {
SetPreviewImage();
}
if (m_ImageChangedTimer->isActive()) m_ImageChangedTimer->stop();
m_ImageChangedTimer->start();
}

void SelectFiles::SelectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
Expand All @@ -288,16 +301,21 @@ void SelectFiles::SelectionChanged(const QItemSelection &selected, const QItemSe

void SelectFiles::SplitterMoved(int pos, int index)
{
ReloadPreview();
// ReloadPreview();
}

void SelectFiles::resizeEvent(QResizeEvent *event)
{
ReloadPreview();
// ReloadPreview();
}

void SelectFiles::SetPreviewImage()
{
if (!m_PreviewReady) {
m_ImageChangedTimer->stop();
m_ImageChangedTimer->start();
return;
}
m_PreviewReady = false;
QPixmap pixmap;
QString details = "";
Expand All @@ -324,8 +342,6 @@ void SelectFiles::SetPreviewImage()
const double ffmbsize = ffsize / 1024.0;
const QString fmbsize = QLocale().toString(ffmbsize, 'f', 2);

bool loading_resources = false;

// Images
if (mkind == "image" || mkind == "svgimage") {

Expand All @@ -348,12 +364,12 @@ void SelectFiles::SetPreviewImage()
// MainWindow::clearMemoryCaches();
const QUrl resourceUrl = QUrl::fromLocalFile(path);
QString html = IMAGE_HTML_BASE_PREVIEW.arg(resourceUrl.toString());
m_PreviewLoaded = false;
if (Utility::IsDarkMode()) {
html = Utility::AddDarkCSS(html);
html = Utility::AddDarkCSS(html);
}
m_WebView->page()->setBackgroundColor(Utility::WebViewBackgroundColor());
m_PreviewLoaded = false;
m_WebView->setHtml(html, resourceUrl);
loading_resources = true;
}

if (mkind == "video") {
Expand All @@ -363,32 +379,24 @@ void SelectFiles::SetPreviewImage()
html = VIDEO_HTML_BASE.arg(resourceUrl.toString());
m_PreviewLoaded = false;
if (Utility::IsDarkMode()) {
html = Utility::AddDarkCSS(html);
html = Utility::AddDarkCSS(html);
}
m_WebView->page()->setBackgroundColor(Utility::WebViewBackgroundColor());
m_PreviewLoaded = false;
m_WebView->setHtml(html, resourceUrl);
loading_resources = true;
details = QString("%1 MB").arg(fmbsize);
} else if (mkind == "audio") {
QString html;
const QUrl resourceUrl = QUrl::fromLocalFile(path);
// MainWindow::clearMemoryCaches();
html = AUDIO_HTML_BASE.arg(resourceUrl.toString());
m_PreviewLoaded = false;
if (Utility::IsDarkMode()) {
html = Utility::AddDarkCSS(html);
html = Utility::AddDarkCSS(html);
}
m_PreviewLoaded = false;
m_WebView->setHtml(html, resourceUrl);
loading_resources = true;
details = QString("%1 MB").arg(fmbsize);
}

// Technically, we need to wait until Preview is actually loaded
// because setHtml loads external resources asynchronously
if (loading_resources) {
while(!IsPreviewLoaded()) {
qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
}
}
ui.Details->setText(details);
m_PreviewReady = true;
}
Expand All @@ -399,6 +407,7 @@ void SelectFiles::PreviewLoadComplete(bool okay)
m_WebView->stop();
}
m_PreviewLoaded = true;
m_PreviewReady = true;
}

bool SelectFiles::IsPreviewLoaded()
Expand Down
12 changes: 6 additions & 6 deletions SelectFiles.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/************************************************************************
**
**
** Copyright (C) 2015-2019 Kevin B. Hendricks, Stratford Ontario Canada
** Copyright (C) 2015-2024 Kevin B. Hendricks, Stratford Ontario Canada
** Copyright (C) 2012-2013 John Schember <[email protected]>
** Copyright (C) 2012-2013 Dave Heiland
**
Expand Down Expand Up @@ -34,6 +34,8 @@

class QString;
class QWebEngineView;
class QTimer;
class QWidget;

class SelectFiles : public QDialog
{
Expand Down Expand Up @@ -71,11 +73,11 @@ private slots:
void PreviewLoadComplete(bool);
void IncreaseThumbnailSize();
void DecreaseThumbnailSize();

void ReloadPreview();

void SelectionChanged(const QItemSelection &selected, const QItemSelection &deselected);

void SelectDefaultImage();
void SetPreviewImage();

/**
* Filters the list of displayed images
Expand All @@ -92,8 +94,6 @@ private slots:
void ReadSettings();
void connectSignalsSlots();

void SetPreviewImage();

QStringList m_MediaList;
QStringList m_MediaKind;
QString m_MediaBase;
Expand All @@ -114,7 +114,7 @@ private slots:
QListWidgetItem *m_ImageItem;
QListWidgetItem *m_VideoItem;
QListWidgetItem *m_AudioItem;

QTimer * m_ImageChangedTimer;
QWebEngineView *m_WebView;

Ui::SelectFiles ui;
Expand Down
4 changes: 2 additions & 2 deletions SelectFiles.ui
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,14 @@
</property>
<widget class="QTreeView" name="imageTree">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Expanding">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>500</width>
<width>16777215</width>
<height>16777215</height>
</size>
</property>
Expand Down
4 changes: 2 additions & 2 deletions WebProfileMgr.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/************************************************************************
**
** Copyright (C) 2023 Kevin B. Hendricks, Stratford Ontario Canada
** Copyright (C) 2023-2024 Kevin B. Hendricks, Stratford Ontario Canada
**
** This file is part of Sigil.
**
Expand Down Expand Up @@ -70,7 +70,7 @@ void WebProfileMgr::InitializeDefaultSettings(QWebEngineSettings* web_settings)
web_settings->setAttribute(QWebEngineSettings::LocalContentCanAccessFileUrls, false);
web_settings->setAttribute(QWebEngineSettings::PluginsEnabled, false);
web_settings->setAttribute(QWebEngineSettings::AutoLoadIconsForPage, false);
web_settings->setAttribute(QWebEngineSettings::FocusOnNavigationEnabled, true);
web_settings->setAttribute(QWebEngineSettings::FocusOnNavigationEnabled, false);
web_settings->setAttribute(QWebEngineSettings::AllowRunningInsecureContent, false);
web_settings->setAttribute(QWebEngineSettings::XSSAuditingEnabled, true);
web_settings->setAttribute(QWebEngineSettings::AllowGeolocationOnInsecureOrigins, false);
Expand Down

0 comments on commit ff81edc

Please sign in to comment.