KParts::ReadOnlyPart Class

Header: #include <ReadOnlyPart>
CMake: find_package(KF6 REQUIRED COMPONENTS Parts)
target_link_libraries(mytarget PRIVATE KF6::Parts)
Inherits: KParts::Part
Inherited By:

KParts::ReadWritePart

Public Functions

ReadOnlyPart(QObject *parent = nullptr, const KPluginMetaData &data = {})
virtual ~ReadOnlyPart() override
KParts::OpenUrlArguments arguments() const
bool closeStream()
virtual bool closeUrl()
bool isProgressInfoEnabled() const
KParts::NavigationExtension *navigationExtension() const
bool openStream(const QString &mimeType, const QUrl &url)
void setArguments(const KParts::OpenUrlArguments &arguments)
void setProgressInfoEnabled(bool show)
QUrl url() const
bool writeStream(const QByteArray &data)

Public Slots

virtual bool openUrl(const QUrl &url)

Signals

void canceled(const QString &errMsg)
void completed()
void completedWithPendingAction()
void started(KIO::Job *job)
void urlChanged(const QUrl &url)

Protected Functions

void abortLoad()
QString localFilePath() const
virtual bool openFile()
void setLocalFilePath(const QString &localFilePath)
void setUrl(const QUrl &url)

Reimplemented Protected Functions

virtual void guiActivateEvent(KParts::GUIActivateEvent *event) override

Detailed Description

@class ReadOnlyPart readonlypart.h <KParts/ReadOnlyPart>

@short Base class for any "viewer" part.

This class takes care of network transparency for you, in the simplest way (downloading to a temporary file, then letting the part load from the temporary file). To use the built-in network transparency, you only need to implement openFile(), not openUrl().

To implement network transparency differently (e.g. for progressive loading, like a web browser does for instance), or to prevent network transparency (but why would you do that?), you can override openUrl().

An application using KParts can use the signals to show feedback while the URL is being loaded.

ReadOnlyPart handles the window caption by setting it to the current URL (set in openUrl(), and each time the part is activated). If you want another caption, set it in openFile() and, if the part might ever be used with a part manager, in guiActivateEvent().

Member Function Documentation

[explicit] ReadOnlyPart::ReadOnlyPart(QObject *parent = nullptr, const KPluginMetaData &data = {})

Constructor. See also Part for the setXXX methods to call.

[override virtual noexcept] ReadOnlyPart::~ReadOnlyPart()

Destructor.

[protected] void ReadOnlyPart::abortLoad()

@internal

KParts::OpenUrlArguments ReadOnlyPart::arguments() const

@return the arguments that were used to open this URL.

See also setArguments().

[signal] void ReadOnlyPart::canceled(const QString &errMsg)

Emit this if loading is canceled by the user or by an error. @param errMsg the error message, empty if the user canceled the loading voluntarily.

bool ReadOnlyPart::closeStream()

Terminate the sending of data to the part. With some data types (text, html...) closeStream might never actually be called, in the case of continuous streams, for instance plain text or HTML data.

[virtual] bool ReadOnlyPart::closeUrl()

Called when closing the current URL (for example, a document), for instance when switching to another URL (note that openUrl() calls it automatically in this case). If the current URL is not fully loaded yet, aborts loading. Deletes the temporary file used when the URL is remote. Resets the current url() to QUrl(). @return always true, but the return value exists for reimplementations

[signal] void ReadOnlyPart::completed()

Emit this when you have completed loading data. Hosting applications will want to know when the process of loading the data is finished, so that they can access the data when everything is loaded.

[signal] void ReadOnlyPart::completedWithPendingAction()

This signal is similar to the @c KParts::ReadOnlyPart::completed() signal except it is only emitted if there is still a pending action to be executed on a delayed timer.

An example of this is the meta-refresh tags on web pages used to reload/redirect after a certain period of time. This signal is useful if you want to give the user the ability to cancel such pending actions.

@since 5.81

[override virtual protected] void ReadOnlyPart::guiActivateEvent(KParts::GUIActivateEvent *event)

Reimplements: Part::guiActivateEvent(KParts::GUIActivateEvent *event).

Reimplemented from Part, so that the window caption is set to the current URL (decoded) when the part is activated. This is the usual behavior in 99% of applications. Reimplement if you don't like it - test for event->activated()!

@note This is done with GUIActivateEvent and not with PartActivateEvent because it's handled by the main window (which gets the event after the PartActivateEvent events have been sent).

bool ReadOnlyPart::isProgressInfoEnabled() const

Returns whether the part shows the progress info dialog used by the internal KIO job.

[protected] QString ReadOnlyPart::localFilePath() const

Returns the local file path associated with this part.

@note The result will only be valid if openUrl() or setLocalFilePath() has previously been called.

See also setLocalFilePath().

This convenience method returns the NavigationExtension for this part, or @c nullptr if there isn't one.

[virtual protected] bool ReadOnlyPart::openFile()

If the part uses the standard implementation of openUrl(), it must reimplement this to open the local file. The default implementation simply returns false.

If this method returns true the part emits completed(), otherwise it emits canceled().

@see completed(), canceled()

bool ReadOnlyPart::openStream(const QString &mimeType, const QUrl &url)

Initiate sending data to this part. This is an alternative to openUrl(), which allows the user of the part to load the data itself, and send it progressively to the part.

@param mimeType the type of data that is going to be sent to this part. @param url the URL representing this data. Although not directly used, every ReadOnlyPart has a URL (see url()), so this simply sets it. @return true if the part supports progressive loading and accepts data, false otherwise.

[virtual slot] bool ReadOnlyPart::openUrl(const QUrl &url)

Only reimplement this if you don't want the network transparency support to download from the URL into a temporary file (when the URL isn't local). Otherwise, reimplement openFile() only.

If you reimplement it, don't forget to set the caption, usually with @code Q_EMIT setWindowCaption( url.toDisplayString(QUrl::PreferLocalFile) ); @endcode and also, if the URL refers to a local file, resolve it to a local path and call setLocalFilePath().

void ReadOnlyPart::setArguments(const KParts::OpenUrlArguments &arguments)

Sets the arguments to use for the next openUrl() call.

See also arguments().

[protected] void ReadOnlyPart::setLocalFilePath(const QString &localFilePath)

Sets the local file path associated with this part.

See also localFilePath().

void ReadOnlyPart::setProgressInfoEnabled(bool show)

Call this to turn off the progress info dialog used by the internal KIO job. Use this if you provide another way of displaying progress info (e.g. a statusbar), using the signals emitted by this class, and/or those emitted by the job given by started().

See also isProgressInfoEnabled().

[protected] void ReadOnlyPart::setUrl(const QUrl &url)

Sets the URL associated with this part.

See also url().

[signal] void ReadOnlyPart::started(KIO::Job *job)

The part emits this when starting to load data. If using a KIO::Job, it provides the @p job so that progress information can be shown. Otherwise, @p job is @c nullptr.

QUrl ReadOnlyPart::url() const

Returns the URL currently opened in (or being opened by) this part. @note The URL is not cleared if openUrl() fails to load the URL. Call closeUrl() if you need to explicitly reset it.

@return The current URL.

Note: Getter function for property url.

See also setUrl().

[signal] void ReadOnlyPart::urlChanged(const QUrl &url)

Emitted by the part when url() changes @since 4.10

bool ReadOnlyPart::writeStream(const QByteArray &data)

Send some data to the part. openStream must have been called previously, and must have returned true. @return true if the data was accepted by the part. If false is returned, the application should stop sending data, and doesn't have to call closeStream.