libkipi from trunk (KDE 4.3) : add support of kipi host settings "file timestamp...
[kdegraphics.git] / gwenview / part / gvpart.cpp
bloba3c43a368f6148354e7c4784035e2e7faf732965
1 /*
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.
20 #include "gvpart.moc"
22 // Qt
24 // KDE
25 #include <kaction.h>
26 #include <kactioncollection.h>
27 #include <kdebug.h>
28 #include <kfiledialog.h>
29 #include <kicon.h>
30 #include <kiconloader.h>
31 #include <kio/job.h>
32 #include <kio/jobuidelegate.h>
33 #include <kmenu.h>
34 #include <kstandardaction.h>
35 #include <kparts/genericfactory.h>
36 #include <kpropertiesdialog.h>
38 // Local
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"
48 //Factory Code
49 typedef KParts::GenericFactory<Gwenview::GVPart> GVPartFactory;
50 K_EXPORT_COMPONENT_FACTORY( gvpart /*library name*/, GVPartFactory )
52 namespace Gwenview {
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()),
65 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() {
91 return false;
95 bool GVPart::openUrl(const KUrl& url) {
96 if (!url.isValid()) {
97 return false;
99 setUrl(url);
100 Document::Ptr doc = DocumentFactory::instance()->load(url);
101 if (arguments().reload()) {
102 doc->reload();
104 if (!UrlUtils::urlIsFastLocalFile(url)) {
105 // Keep raw data of remote files to avoid downloading them again in
106 // saveAs()
107 doc->setKeepRawData(true);
109 mDocumentView->openUrl(url);
110 return true;
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");
127 return aboutData;
131 inline void addActionToMenu(KMenu* menu, KActionCollection* actionCollection, const char* name) {
132 QAction* action = actionCollection->action(name);
133 if (action) {
134 menu->addAction(action);
139 void GVPart::showContextMenu() {
140 KMenu menu(mDocumentView);
141 addActionToMenu(&menu, actionCollection(), "file_save_as");
142 menu.addSeparator();
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");
147 menu.addSeparator();
148 addActionToMenu(&menu, actionCollection(), "file_show_properties");
149 menu.exec(QCursor::pos());
153 void GVPart::saveAs() {
154 KUrl srcUrl = url();
155 KUrl dstUrl = KFileDialog::getSaveUrl(srcUrl.fileName(), QString(), widget());
156 if (!dstUrl.isValid()) {
157 return;
160 KIO::Job* job;
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);
165 } else {
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();
176 if (!ui) {
177 kError() << "Saving failed. job->ui() is null.";
178 return;
180 ui->setWindow(widget());
181 ui->showErrorMessage();
186 } // namespace