2 Gwenview: an image viewer
3 Copyright 2007-2008 Aurélien Gâteau <aurelien.gateau@free.fr>
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 #include <kactioncollection.h>
28 #include <kfiledialog.h>
30 #include <kiconloader.h>
32 #include <kio/jobuidelegate.h>
34 #include <kstandardaction.h>
35 #include <kparts/genericfactory.h>
36 #include <kpropertiesdialog.h>
39 #include "../lib/document/document.h"
40 #include "../lib/document/documentfactory.h"
41 #include "../lib/documentview/documentview.h"
42 #include "../lib/imageformats/imageformats.h"
43 #include "../lib/urlutils.h"
44 #include "../lib/zoomwidget.h"
45 #include "gvbrowserextension.h"
49 typedef KParts::GenericFactory
<Gwenview::GVPart
> GVPartFactory
;
50 K_EXPORT_COMPONENT_FACTORY( gvpart
/*library name*/, GVPartFactory
)
55 GVPart::GVPart(QWidget
* parentWidget
, QObject
* parent
, const QStringList
& /*args*/)
56 : KParts::ReadOnlyPart(parent
)
58 mDocumentView
= new DocumentView(parentWidget
, actionCollection());
59 mDocumentView
->setZoomWidgetVisible(false);
60 setWidget(mDocumentView
);
62 connect(mDocumentView
, SIGNAL(captionUpdateRequested(const QString
&)),
63 SIGNAL(setWindowCaption(const QString
&)));
64 connect(mDocumentView
, SIGNAL(completed()),
67 mDocumentView
->setContextMenuPolicy(Qt::CustomContextMenu
);
68 connect(mDocumentView
, SIGNAL(customContextMenuRequested(const QPoint
&)),
69 SLOT(showContextMenu()) );
71 KAction
* action
= new KAction(actionCollection());
72 action
->setText(i18nc("@action", "Properties"));
73 connect(action
, SIGNAL(triggered()), SLOT(showProperties()));
74 actionCollection()->addAction("file_show_properties", action
);
76 KStandardAction::saveAs(this, SLOT(saveAs()), actionCollection());
78 Gwenview::ImageFormats::registerPlugins();
79 new GVBrowserExtension(this);
81 setXMLFile("gvpart/gvpart.rc");
85 void GVPart::showProperties() {
86 KPropertiesDialog::showDialog(url(), mDocumentView
);
90 bool GVPart::openFile() {
95 bool GVPart::openUrl(const KUrl
& url
) {
100 Document::Ptr doc
= DocumentFactory::instance()->load(url
);
101 if (arguments().reload()) {
104 if (!UrlUtils::urlIsFastLocalFile(url
)) {
105 // Keep raw data of remote files to avoid downloading them again in
107 doc
->setKeepRawData(true);
109 mDocumentView
->openUrl(url
);
114 KAboutData
* GVPart::createAboutData() {
115 KAboutData
* aboutData
= new KAboutData(
116 "gvpart", /* appname */
117 "gwenview", /* catalogName */
118 ki18n("Gwenview KPart"), /* programName */
119 "2.1"); /* version */
120 aboutData
->setShortDescription(ki18n("An Image Viewer"));
121 aboutData
->setLicense(KAboutData::License_GPL
);
122 aboutData
->setCopyrightStatement(ki18n("Copyright 2000-2008 Aurélien Gâteau"));
123 aboutData
->addAuthor(
124 ki18n("Aurélien Gâteau"),
125 ki18n("Main developer"),
126 "aurelien.gateau@free.fr");
131 inline void addActionToMenu(KMenu
* menu
, KActionCollection
* actionCollection
, const char* name
) {
132 QAction
* action
= actionCollection
->action(name
);
134 menu
->addAction(action
);
139 void GVPart::showContextMenu() {
140 KMenu
menu(mDocumentView
);
141 addActionToMenu(&menu
, actionCollection(), "file_save_as");
143 addActionToMenu(&menu
, actionCollection(), "view_actual_size");
144 addActionToMenu(&menu
, actionCollection(), "view_zoom_to_fit");
145 addActionToMenu(&menu
, actionCollection(), "view_zoom_in");
146 addActionToMenu(&menu
, actionCollection(), "view_zoom_out");
148 addActionToMenu(&menu
, actionCollection(), "file_show_properties");
149 menu
.exec(QCursor::pos());
153 void GVPart::saveAs() {
155 KUrl dstUrl
= KFileDialog::getSaveUrl(srcUrl
.fileName(), QString(), widget());
156 if (!dstUrl
.isValid()) {
161 Document::Ptr doc
= DocumentFactory::instance()->load(srcUrl
);
162 QByteArray rawData
= doc
->rawData();
163 if (rawData
.length() > 0) {
164 job
= KIO::storedPut(rawData
, dstUrl
, -1);
166 job
= KIO::file_copy(srcUrl
, dstUrl
);
168 connect(job
, SIGNAL(result(KJob
*)),
169 this, SLOT(showJobError(KJob
*)) );
173 void GVPart::showJobError(KJob
* job
) {
174 if (job
->error() != 0) {
175 KIO::JobUiDelegate
* ui
= static_cast<KIO::Job
*>(job
)->ui();
177 kError() << "Saving failed. job->ui() is null.";
180 ui
->setWindow(widget());
181 ui
->showErrorMessage();