gvpart.cpp 5.49 KB
Newer Older
1 2
/*
Gwenview: an image viewer
Aurélien Gâteau's avatar
Aurélien Gâteau committed
3
Copyright 2007-2012 Aurélien Gâteau <agateau@kde.org>
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program 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 General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

*/
20
// Qt
21 22 23 24
#include <QUrl>
#include <QAction>
#include <QDebug>
#include <QMenu>
25

26
// KDE
Aurélien Gâteau's avatar
Aurélien Gâteau committed
27 28
#include <KAboutData>
#include <KActionCollection>
29
#include <QFileDialog>
Aurélien Gâteau's avatar
Aurélien Gâteau committed
30 31 32
#include <KIconLoader>
#include <KIO/Job>
#include <KIO/JobUiDelegate>
33
#include <KLocalizedString>
Aurélien Gâteau's avatar
Aurélien Gâteau committed
34 35 36
#include <KStandardAction>
#include <KPluginFactory>
#include <KPropertiesDialog>
37
#include <KJobWidgets>
38

39
// Local
40
#include "../lib/about.h"
41
#include "../lib/document/document.h"
42
#include "../lib/document/documentfactory.h"
43
#include "../lib/documentview/documentview.h"
Aurélien Gâteau's avatar
Aurélien Gâteau committed
44
#include "../lib/documentview/documentviewcontainer.h"
45
#include "../lib/documentview/documentviewcontroller.h"
46
#include "../lib/urlutils.h"
Aurélien Gâteau's avatar
Aurélien Gâteau committed
47
#include "../lib/zoomwidget.h"
48
#include "gvbrowserextension.h"
49
#include "gvpart.h"
50

51
//Factory Code
52
K_PLUGIN_FACTORY_WITH_JSON(GVPartFactory, "gvpart.json", registerPlugin<Gwenview::GVPart>();)
53

54 55
namespace Gwenview
{
56

57
GVPart::GVPart(QWidget* parentWidget, QObject* parent, const QVariantList& /*args*/)
58
: KParts::ReadOnlyPart(parent)
59
{
60 61 62
    QScopedPointer<KAboutData> aboutData(createAboutData());
    setComponentData(*aboutData, false);

63
    DocumentViewContainer* container = new DocumentViewContainer(parentWidget);
64 65
    setWidget(container);
    mDocumentView = container->createView();
66

Laurent Montel's avatar
Laurent Montel committed
67 68
    connect(mDocumentView, &DocumentView::captionUpdateRequested,
            this, &KParts::Part::setWindowCaption);
69 70
    connect(mDocumentView, SIGNAL(completed()),
            SIGNAL(completed()));
Aurélien Gâteau's avatar
Aurélien Gâteau committed
71

Laurent Montel's avatar
Laurent Montel committed
72 73
    connect(mDocumentView, &DocumentView::contextMenuRequested,
            this, &GVPart::showContextMenu);
74

75 76 77
    // Necessary to have zoom actions
    DocumentViewController* documentViewController = new DocumentViewController(actionCollection(), this);
    documentViewController->setView(mDocumentView);
78

David Edmundson's avatar
David Edmundson committed
79
    QAction * action = new QAction(actionCollection());
80
    action->setText(i18nc("@action", "Properties"));
81
    action->setShortcut(QKeySequence(Qt::ALT + Qt::Key_Return));
Laurent Montel's avatar
Laurent Montel committed
82
    connect(action, &QAction::triggered, this, &GVPart::showProperties);
Laurent Montel's avatar
Laurent Montel committed
83
    actionCollection()->addAction(QStringLiteral("file_show_properties"), action);
84

85

86
    KStandardAction::saveAs(this, SLOT(saveAs()), actionCollection());
87

88
    new GVBrowserExtension(this);
Aurélien Gâteau's avatar
Aurélien Gâteau committed
89

90
    setXMLFile(QStringLiteral("gvpart.rc"), true);
Aurélien Gâteau's avatar
Aurélien Gâteau committed
91 92
}

93 94 95
void GVPart::showProperties()
{
    KPropertiesDialog::showDialog(url(), widget());
96 97
}

98
bool GVPart::openUrl(const QUrl& url)
99 100 101 102 103 104 105 106 107 108 109 110 111 112
{
    if (!url.isValid()) {
        return false;
    }
    setUrl(url);
    Document::Ptr doc = DocumentFactory::instance()->load(url);
    if (arguments().reload()) {
        doc->reload();
    }
    if (!UrlUtils::urlIsFastLocalFile(url)) {
        // Keep raw data of remote files to avoid downloading them again in
        // saveAs()
        doc->setKeepRawData(true);
    }
113 114 115
    DocumentView::Setup setup;
    setup.zoomToFit = true;
    mDocumentView->openUrl(url, setup);
116
    return true;
117 118
}

119 120
KAboutData* GVPart::createAboutData()
{
121
    KAboutData* aboutData = Gwenview::createAboutData(
122 123
        QStringLiteral("gvpart"), /* appname */
        i18n("Gwenview KPart")    /* programName */
124
        );
125
    aboutData->setShortDescription(i18n("An Image Viewer"));
126
    return aboutData;
127 128
}

129
inline void addActionToMenu(QMenu* menu, KActionCollection* actionCollection, const char* name)
130 131 132 133 134
{
    QAction* action = actionCollection->action(name);
    if (action) {
        menu->addAction(action);
    }
135 136
}

137 138
void GVPart::showContextMenu()
{
139
    QMenu menu(widget());
140 141 142 143 144 145 146 147 148
    addActionToMenu(&menu, actionCollection(), "file_save_as");
    menu.addSeparator();
    addActionToMenu(&menu, actionCollection(), "view_actual_size");
    addActionToMenu(&menu, actionCollection(), "view_zoom_to_fit");
    addActionToMenu(&menu, actionCollection(), "view_zoom_in");
    addActionToMenu(&menu, actionCollection(), "view_zoom_out");
    menu.addSeparator();
    addActionToMenu(&menu, actionCollection(), "file_show_properties");
    menu.exec(QCursor::pos());
149 150
}

151 152
void GVPart::saveAs()
{
153
    const QUrl srcUrl = url();
154
    const QUrl dstUrl = QFileDialog::getSaveFileUrl(widget(), QString(), srcUrl);
155 156 157 158 159 160 161 162 163 164 165 166
    if (!dstUrl.isValid()) {
        return;
    }

    KIO::Job* job;
    Document::Ptr doc = DocumentFactory::instance()->load(srcUrl);
    QByteArray rawData = doc->rawData();
    if (rawData.length() > 0) {
        job = KIO::storedPut(rawData, dstUrl, -1);
    } else {
        job = KIO::file_copy(srcUrl, dstUrl);
    }
Laurent Montel's avatar
Laurent Montel committed
167 168
    connect(job, &KJob::result,
            this, &GVPart::showJobError);
169 170
}

171 172 173
void GVPart::showJobError(KJob* job)
{
    if (job->error() != 0) {
174
        KJobUiDelegate* ui = static_cast<KIO::Job*>(job)->uiDelegate();
175
        if (!ui) {
Laurent Montel's avatar
Laurent Montel committed
176
            qCritical() << "Saving failed. job->ui() is null.";
177 178
            return;
        }
179
        KJobWidgets::setWindow(job, widget());
180 181
        ui->showErrorMessage();
    }
Aurélien Gâteau's avatar
Aurélien Gâteau committed
182 183
}

184
} // namespace
185 186

#include "gvpart.moc"