From 65c9ed251d7726102bff3b5f4d2c6729433be4b1 Mon Sep 17 00:00:00 2001
From: Mavridis Philippe <mavridisf@gmail.com>
Date: Wed, 16 Apr 2025 10:58:59 +0300
Subject: KPDF Shell: fix printing action

Signed-off-by: Mavridis Philippe <mavridisf@gmail.com>
---
 kpdf/part.cpp        | 30 +++++++++++++++++++++---------
 kpdf/part.h          |  3 +++
 kpdf/shell/shell.cpp | 18 ++++++++++++++++--
 kpdf/shell/shell.h   |  2 ++
 4 files changed, 42 insertions(+), 11 deletions(-)

diff --git a/kpdf/part.cpp b/kpdf/part.cpp
index 09027948..a82707f7 100644
--- a/kpdf/part.cpp
+++ b/kpdf/part.cpp
@@ -117,7 +117,7 @@ Part::Part(TQWidget *parentWidget, const char *widgetName,
            TQObject *parent, const char *name,
            const TQStringList & /*args*/ )
 	: DCOPObject("kpdf"), KParts::ReadOnlyPart(parent, name), m_showMenuBarAction(0), m_showFullScreenAction(0),
-	m_actionsSearched(false), m_searchStarted(false)
+	m_actionsSearched(false), m_searchStarted(false), m_printable(false)
 {
 	// connect the started signal to tell the job the mimetypes we like
 	connect(this, TQ_SIGNAL(started(TDEIO::Job *)), this, TQ_SLOT(setMimeTypes(TDEIO::Job *)));
@@ -524,7 +524,7 @@ bool Part::openURL(const KURL &url)
         m_viewportDirty.pageNumber = -1;
         emit setWindowCaption(url.filename());
     }
-    emit enablePrintAction(b);
+    setPrintable(b);
     return b;
 }
 
@@ -539,15 +539,16 @@ void Part::setMimeTypes(TDEIO::Job *job)
 
 void Part::readMimeType(TDEIO::Job *, const TQString &mime)
 {
-	m_jobMime = mime;
+    m_jobMime = mime;
 }
 
 void Part::guiActivateEvent(KParts::GUIActivateEvent *e)
 {
-	if (e->activated())
-	{
-		emitWindowCaption();
-	}
+    if (e->activated())
+    {
+        emitWindowCaption();
+        emit enablePrintAction(m_printable);
+    }
 }
 
 void Part::emitWindowCaption()
@@ -573,7 +574,7 @@ bool Part::closeURL()
     m_showProperties->setEnabled( false );
     m_showPresentation->setEnabled( false );
     emit setWindowCaption("");
-    emit enablePrintAction(false);
+    setPrintable(false);
     m_searchStarted = false;
     if (!m_file.isEmpty()) m_watcher->removeFile(m_file);
     m_document->closeDocument();
@@ -636,7 +637,7 @@ void Part::slotDoFileDirty()
       m_toolBox->setCurrentIndex( m_dirtyToolboxIndex );
     }
     if (m_wasPresentationOpen) slotShowPresentation();
-    emit enablePrintAction(true);
+    setPrintable(true);
     emit setWindowCaption(url().filename());
   }
   else
@@ -655,6 +656,17 @@ void Part::close()
   else KMessageBox::information(widget(), i18n("This link points to a close document action that does not work when using the embedded viewer."), TQString(), "warnNoCloseIfNotInKPDF");
 }
 
+void Part::setPrintable(bool printable)
+{
+    m_printable = printable;
+    emit enablePrintAction(printable);
+}
+
+bool Part::printable()
+{
+    return m_printable;
+}
+
 void Part::updateViewActions()
 {
     bool opened = m_document->pages() > 0;
diff --git a/kpdf/part.h b/kpdf/part.h
index bfa24506..067f9316 100644
--- a/kpdf/part.h
+++ b/kpdf/part.h
@@ -83,6 +83,7 @@ public:
 	KURL currentDocument();
 
 	bool isKPDFShell();
+	bool printable();
 
 signals:
 	void enablePrintAction(bool enable);
@@ -127,6 +128,7 @@ protected slots:
 	void saveSplitterSize();
 	void setMimeTypes(TDEIO::Job *job);
 	void readMimeType(TDEIO::Job *job, const TQString &mime);
+	void setPrintable(bool printable);
 	void emitWindowCaption();
 
 public slots:
@@ -194,6 +196,7 @@ private:
 	TDEToggleAction* m_showFullScreenAction;
 	bool m_actionsSearched;
 	bool m_searchStarted;
+	bool m_printable;
 	BrowserExtension *m_bExtension;
 };
 
diff --git a/kpdf/shell/shell.cpp b/kpdf/shell/shell.cpp
index f6ba2f18..40df86dc 100644
--- a/kpdf/shell/shell.cpp
+++ b/kpdf/shell/shell.cpp
@@ -203,7 +203,7 @@ void Shell::setupActions()
   m_recent = KStdAction::openRecent( this, TQ_SLOT(openURL(const KURL&)), actionCollection());
   connect(m_recent, TQ_SIGNAL(activated()), openAction, TQ_SLOT( activate()));
   m_recent->setWhatsThis(i18n("<b>Click</b> to open a file or <b>Click and hold</b> to select a recent file"));
-  m_printAction = KStdAction::print(m_manager->activePart(), TQ_SLOT(slotPrint()), actionCollection());
+  m_printAction = KStdAction::print(this, TQ_SLOT(slotPrint()), actionCollection());
   m_printAction->setEnabled(false);
   KStdAction::quit(this, TQ_SLOT(slotQuit()), actionCollection());
 
@@ -299,6 +299,20 @@ void Shell::applyNewToolbarConfig()
   applyMainWindowSettings(TDEGlobal::config(), "MainWindow");
 }
 
+void Shell::slotSetPrintActionEnabled(bool enabled)
+{
+    const KParts::Part *part = static_cast<const KParts::Part*>(TQObject::sender());
+    if (part == m_manager->activePart())
+    {
+        m_printAction->setEnabled(enabled);
+    }
+}
+
+void Shell::slotPrint()
+{
+    TQTimer::singleShot(0, m_manager->activePart(), TQ_SLOT(slotPrint()));
+}
+
 void Shell::slotQuit()
 {
     tdeApp->closeAllWindows();
@@ -364,7 +378,7 @@ KParts::ReadOnlyPart* Shell::createTab()
   connect(this, TQ_SIGNAL(saveDocumentRestoreInfo(TDEConfig*)),
           part, TQ_SLOT(saveDocumentRestoreInfo(TDEConfig*)));
   connect(part, TQ_SIGNAL(enablePrintAction(bool)),
-          m_printAction, TQ_SLOT(setEnabled(bool)));
+          this, TQ_SLOT(slotSetPrintActionEnabled(bool)));
   connect(part, TQ_SIGNAL(setWindowCaption(const TQString&)),
           this, TQ_SLOT(slotSetTabCaption(const TQString&)));
 
diff --git a/kpdf/shell/shell.h b/kpdf/shell/shell.h
index 6198aa4c..d1ace108 100644
--- a/kpdf/shell/shell.h
+++ b/kpdf/shell/shell.h
@@ -100,6 +100,7 @@ namespace KPDF
     void addTab();
     void removeTab();
 
+    void slotPrint();
     void slotQuit();
 
   private slots:
@@ -122,6 +123,7 @@ namespace KPDF
     void slotMoveTabRight();
     void slotRemoveOtherTabs();
     void slotSetTabCaption(const TQString &caption);
+    void slotSetPrintActionEnabled(bool enabled);
 
   signals:
     void restoreDocument(TDEConfig* config);
-- 
cgit v1.2.3

