diff --git a/kuiviewer/kuiviewer.cpp b/kuiviewer/kuiviewer.cpp index 22c671b..ab1da6c 100644 --- a/kuiviewer/kuiviewer.cpp +++ b/kuiviewer/kuiviewer.cpp @@ -1,140 +1,149 @@ /* * This file is part of the kuiviewer package * Copyright (c) 2003 Richard Moore * Copyright (c) 2003 Ian Reinhart Geiser * Copyright (c) 2004 Benjamin C. Meyer * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License version 2 as published by the Free Software Foundation. * * This library 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 * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #include "kuiviewer.h" #include "kuiviewer_part.h" #include "kuiviewer_part_interface.h" // KF #include #include #include #include #include #include +#include // Qt #include #include #include #include #include #include KUIViewer::KUIViewer() - : KParts::MainWindow() + : KParts::MainWindow(), + m_part(nullptr) { setObjectName(QStringLiteral("KUIViewer")); // setup our actions setupActions(); setMinimumSize(300, 200); // Bring up the gui setupGUI(); // this routine will find and load our Part. it finds the Part by // name which is a bad idea usually.. but it's alright in this // case since our Part is made for this Shell - KPluginFactory* factory = KPluginLoader(QStringLiteral("kuiviewerpart")).factory(); - if (factory) { - // now that the Part is loaded, we cast it to a Part to get - // our hands on it - m_part = factory->create(this); - - if (m_part) { - m_part->setObjectName(QStringLiteral("kuiviewer_part")); - // tell the KParts::MainWindow that this is indeed the main widget - setCentralWidget(m_part->widget()); - - // and integrate the part's GUI with the shell's - createGUI(m_part); + + const KService::Ptr service = KService::serviceByDesktopName(QStringLiteral("kuiviewer_part")); + if (service) + { + KPluginFactory *factory = KPluginLoader(*service).factory(); + if (factory) { + // now that the Part is loaded, we cast it to a Part to get + // our hands on it + m_part = factory->create(this); + + if (m_part) { + m_part->setObjectName(QStringLiteral("kuiviewer_part")); + // tell the KParts::MainWindow that this is indeed the main widget + setCentralWidget(m_part->widget()); + + // and integrate the part's GUI with the shell's + createGUI(m_part); + } } - } else { + } + + if (m_part==nullptr) { // if we couldn't find our Part, we exit since the Shell by // itself can't do anything useful //FIXME improve message, which Part is this referring to? - KMessageBox::error(this, i18n("Unable to locate Kuiviewer kpart.")); + KMessageBox::error(this, i18n("Unable to locate or load KUiViewer KPart.")); QApplication::quit(); // we return here, cause kapp->quit() only means "exit the // next time we enter the event loop... return; } } KUIViewer::~KUIViewer() { } void KUIViewer::load(const QUrl& url) { m_part->openUrl(url); adjustSize(); } void KUIViewer::setupActions() { KStandardAction::open(this, &KUIViewer::fileOpen, actionCollection()); KStandardAction::quit(this, &KUIViewer::close, actionCollection()); } void KUIViewer::fileOpen() { // this slot is called whenever the File->Open menu is selected, // the Open shortcut is pressed (usually CTRL+O) or the Open toolbar // button is clicked QUrl file_name = QFileDialog::getOpenFileUrl(this, QString(), QUrl(), i18n("*.ui *.UI|User Interface Files")); if (!file_name.isEmpty()) { // About this function, the style guide ( // http://developer.kde.org/documentation/standards/kde/style/basics/index.html ) // says that it should open a new window if the document is _not_ // in its initial state. This is what we do here.. if (m_part->url().isEmpty()) { // we open the file in this window... load(file_name); } else { // we open the file in a new window... KUIViewer* newWin = new KUIViewer; newWin->load(file_name); newWin->show(); } } } void KUIViewer::takeScreenshot(const QString& filename, int w, int h) { auto uiviewerInterface = qobject_cast(m_part); if (!uiviewerInterface) { return; } if (w != -1 && h != -1) { // resize widget to the desired size uiviewerInterface->setWidgetSize(QSize(w, h)); } const QPixmap pixmap = uiviewerInterface->renderWidgetAsPixmap(); pixmap.save(filename, "PNG"); } diff --git a/kuiviewer/kuiviewer.h b/kuiviewer/kuiviewer.h index e300fcf..b0fc499 100644 --- a/kuiviewer/kuiviewer.h +++ b/kuiviewer/kuiviewer.h @@ -1,85 +1,90 @@ /* * This file is part of the kuiviewer package * Copyright (c) 2003 Richard Moore * Copyright (c) 2003 Ian Reinhart Geiser * Copyright (c) 2004 Benjamin C. Meyer * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License version 2 as published by the Free Software Foundation. * * This library 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 * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #ifndef KUIVIEWER_H #define KUIVIEWER_H // KF #include class KToggleAction; namespace KParts { class ReadOnlyPart; } /** * This is the application "Shell". It has a menubar, toolbar, and * statusbar but relies on the "Part" to do all the real work. * * @short KUI Viewer Shell * @author Richard Moore * @author Ian Reinhart Geiser * @version 1.0 */ class KUIViewer : public KParts::MainWindow { Q_OBJECT public: /** * Default Constructor */ KUIViewer(); /** * Default Destructor */ ~KUIViewer() override; /** * Use this method to load whatever file/URL you have */ void load(const QUrl& url); + /** + * Check whether the viewer KPart is correctly found and loaded + */ + bool isReady() const { return (m_part!=nullptr); } + /** * Take screenshot of current ui file * @param filename to save image in * @param h height of image * @param w width of image */ void takeScreenshot(const QString& filename, int h = -1, int w = -1); private Q_SLOTS: void fileOpen(); private: void setupActions(); private: KParts::ReadOnlyPart* m_part; KToggleAction* m_toolbarAction; KToggleAction* m_statusbarAction; }; #endif // KUIVIEWER_H diff --git a/kuiviewer/main.cpp b/kuiviewer/main.cpp index b2408d5..aacfb02 100644 --- a/kuiviewer/main.cpp +++ b/kuiviewer/main.cpp @@ -1,100 +1,101 @@ /* * This file is part of the kuiviewer package * Copyright (c) 2003 Richard Moore * Copyright (c) 2003 Ian Reinhart Geiser * Copyright (c) 2004 Benjamin C. Meyer * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License version 2 as published by the Free Software Foundation. * * This library 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 * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #include "kuiviewer.h" // KF #include #include // Qt #include #include #include #include #include int main(int argc, char** argv) { QApplication app(argc, argv); KLocalizedString::setApplicationDomain("kuiviewer"); KAboutData about(QStringLiteral("kuiviewer"), i18n("KUIViewer"), QStringLiteral("0.3.0"), i18n("Displays Designer's UI files"), KAboutLicense::LGPL); about.addAuthor(i18n("Richard Moore"), i18n("Original author"), QStringLiteral("rich@kde.org")); about.addAuthor(i18n("Ian Reinhart Geiser"), i18n("Original author"), QStringLiteral("geiseri@kde.org")); // Screenshot capability about.addAuthor(i18n("Benjamin C. Meyer"), i18n("Screenshot capability"), QStringLiteral("ben+kuiviewer@meyerhome.net")); about.addAuthor(i18n("Friedrich W. H. Kossebau"), i18n("Subwindow-like display of UI files"), QStringLiteral("kossebau@kde.org")); KAboutData::setApplicationData(about); QCommandLineParser parser; about.setupCommandLine(&parser); parser.addPositionalArgument(QLatin1String("[URL]"), i18n("Document to open")); const QString takeScreenshotOptionKey(QStringLiteral("takescreenshot")); const QString screenshotWidthOptionKey(QStringLiteral("screenshotwidth")); const QString screenshotHeightOptionKey(QStringLiteral("screenshotheight")); parser.addOption(QCommandLineOption(QStringList() << QLatin1String("s") << takeScreenshotOptionKey, i18n("Save screenshot to file and exit"), QLatin1String("filename"))); parser.addOption(QCommandLineOption({QStringLiteral("sw"), screenshotWidthOptionKey}, i18n("Screenshot width"), QLatin1String("int"), QLatin1String("-1"))); parser.addOption(QCommandLineOption({QStringLiteral("sh"), screenshotHeightOptionKey}, i18n("Screenshot height"), QLatin1String("int"), QLatin1String("-1"))); parser.process(app); about.processCommandLine(&parser); // see if we are starting with session management if (app.isSessionRestored()) { RESTORE(KUIViewer) } else { // no session.. just start up normally + KUIViewer* widget = new KUIViewer; + if (!widget->isReady()) return 1; + const auto positionalArguments = parser.positionalArguments(); if (positionalArguments.isEmpty()) { - KUIViewer* widget = new KUIViewer; widget->show(); } else { const bool takeScreenshot = parser.isSet(takeScreenshotOptionKey); - KUIViewer* widget = new KUIViewer; // show before loading, so widget geometries will be properly updated when requested // TODO: investigate how to do this properly with perhaps showevents & Co.? if (takeScreenshot) { widget->showMinimized(); } else { widget->show(); } widget->load(QUrl::fromUserInput(positionalArguments.at(0), QDir::currentPath())); if (takeScreenshot) { widget->takeScreenshot(parser.value(takeScreenshotOptionKey), parser.value(screenshotWidthOptionKey).toInt(), parser.value(screenshotHeightOptionKey).toInt()); return 0; } } } return app.exec(); }