import my own image viewer experiment

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2016-03-16 23:50:28 +00:00
parent 95fd243aef
commit 928369e751
9 changed files with 800 additions and 0 deletions

View file

@ -0,0 +1,51 @@
project(KImageViewer)
find_package(KDE4 REQUIRED)
include(KDE4Defaults)
include_directories(${KDE4_INCLUDES})
# application
set(kimageviewer_SRCS
main.cpp
kimagewidget.cpp
)
add_executable(kimageviewer ${kimageviewer_SRCS})
target_link_libraries(kimageviewer
${KDE4_KDECORE_LIBS}
${KDE4_KPARTS_LIBS}
${KDE4_KDEUI_LIBS}
)
## part
set(kimageviewerpart_SRCS
kimagepart.cpp
kimagewidget.cpp
)
kde4_add_plugin(kimageviewerpart ${kimageviewerpart_SRCS})
target_link_libraries(kimageviewerpart
${KDE4_KDECORE_LIBS}
${KDE4_KPARTS_LIBS}
${KDE4_KDEUI_LIBS}
)
## install everything
install(
TARGETS kimageviewer
DESTINATION ${BIN_INSTALL_DIR}
)
install(
TARGETS kimageviewerpart
DESTINATION ${PLUGIN_INSTALL_DIR}
)
install(
PROGRAMS kimageviewer.desktop
DESTINATION ${XDG_APPS_INSTALL_DIR}
)
install(
PROGRAMS kimageviewerpart.desktop
DESTINATION ${SERVICES_INSTALL_DIR}
)

114
kimageviewer/kimagepart.cpp Normal file
View file

@ -0,0 +1,114 @@
/***********************************************************************
* Copyright 2016 Ivailo Monev <xakepa10@gmail.com>
*
* 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) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* 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, see <http://www.gnu.org/licenses/>.
***********************************************************************/
#include "kimagepart.h"
#include <KAboutData>
#include <KAction>
#include <KActionCollection>
#include <KLocale>
#include <KMessageBox>
#include <KStandardAction>
#include <KStatusBar>
#include <KPluginFactory>
#include <QtCore/QByteArray>
#include <QtCore/QDir>
#include <QtGui/QScrollArea>
namespace KImageViewer {
K_PLUGIN_FACTORY(KImageViewerPartFactory, registerPlugin<KImagePart>();) // produce a factory
K_EXPORT_PLUGIN(KImageViewerPartFactory(KAboutData(
"kimageviewerpart",
0,
ki18n("KImageViewer"),
"1.0.0",
ki18n("Simple image viewer for KDE."),
KAboutData::License_GPL_V2,
ki18n("(c) 2014 Ivailo Monev"),
KLocalizedString(),
"http://github.com/fluxer/kde-playground",
"xakepa10@gmail.com").
setProgramIconName(QLatin1String("KImageViewer")).
setCatalogName("kimageviewer")))
BrowserExtension::BrowserExtension(KImagePart *parent)
: KParts::BrowserExtension(parent)
{}
KImagePart::KImagePart(QWidget *parentWidget, QObject *parent, const QList<QVariant>&)
: ReadOnlyPart(parent)
, m_ext(new BrowserExtension(this))
, m_statusbar(new StatusBarExtension(this))
, m_viewer(0)
{
setComponentData(KImageViewerPartFactory::componentData());
m_viewer = new KImageWidget(parentWidget);
setWidget(m_viewer);
}
void KImagePart::setApplication(bool application)
{
m_viewer->setApplication(application);
}
bool KImagePart::openFile()
{
KUrl url = KUrl::fromPath(localFilePath());
if (url.isEmpty()) {
//do nothing, chances are the user accidently pressed ENTER
} else if (!url.isValid()) {
KMessageBox::information(widget(), i18n("The entered URL is not invalid."));
} else if (url.protocol() != QLatin1String("file")) {
KMessageBox::information(widget(), i18n("Only file paths are accepted."));
} else {
m_viewer->setImage(url.path());
m_statusbar->statusBar()->showMessage(i18n("Opened: %1", url.path()));
return true;
}
return false;
}
bool KImagePart::closeUrl()
{
m_statusbar->statusBar()->showMessage(i18n("Closing viewer..."));
return ReadOnlyPart::closeUrl();
}
void KImagePart::updateURL(const KUrl &u)
{
// update the interface
emit m_ext->openUrlNotify(); //must be done first
emit m_ext->setLocationBarUrl(u.prettyUrl());
m_viewer->setImage(u.path());
//do this last, or it breaks Konqi location bar
setUrl(u);
}
} //namespace KImageViewer
#include "moc_kimagepart.cpp"

68
kimageviewer/kimagepart.h Normal file
View file

@ -0,0 +1,68 @@
/***********************************************************************
* Copyright 2016 Ivailo Monev <xakepa10@gmail.com>
*
* 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) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* 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, see <http://www.gnu.org/licenses/>.
***********************************************************************/
#ifndef KIMAGEPART_H
#define KIMAGEPART_H
#include "kimagewidget.h"
#include <KParts/BrowserExtension>
#include <KParts/StatusBarExtension>
#include <KParts/Part>
#include <KUrl>
#include <QtGui/QGridLayout>
using KParts::StatusBarExtension;
namespace KImageViewer
{
class KImagePart;
class BrowserExtension : public KParts::BrowserExtension
{
public:
explicit BrowserExtension(KImagePart*);
};
class KImagePart : public KParts::ReadOnlyPart
{
Q_OBJECT
public:
KImagePart(QWidget *, QObject *, const QList<QVariant>&);
virtual bool openFile();
virtual bool closeUrl();
void setApplication(bool application);
private slots:
void updateURL(const KUrl &);
private:
BrowserExtension *m_ext;
StatusBarExtension *m_statusbar;
QGridLayout *m_layout;
KImageWidget *m_viewer;
};
} // namespace KImageViewer
#endif // KIMAGEPART_H

172
kimageviewer/kimageui.ui Normal file
View file

@ -0,0 +1,172 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>KImageUI</class>
<widget class="QWidget" name="KImageUI">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>503</width>
<height>399</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="2" column="0">
<widget class="QPushButton" name="m_open">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>16777215</height>
</size>
</property>
<property name="text">
<string>Open</string>
</property>
<property name="icon">
<iconset theme="document-open">
<normaloff/>
</iconset>
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QPushButton" name="m_save">
<property name="enabled">
<bool>false</bool>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Save</string>
</property>
<property name="icon">
<iconset theme="document-save">
<normaloff/>
</iconset>
</property>
</widget>
</item>
<item row="5" column="0">
<widget class="QPushButton" name="m_rotateleft">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Rotate left</string>
</property>
<property name="icon">
<iconset theme="object-rotate-left">
<normaloff/>
</iconset>
</property>
</widget>
</item>
<item row="8" column="0">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>217</height>
</size>
</property>
</spacer>
</item>
<item row="6" column="0">
<widget class="QPushButton" name="m_rotateright">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Rotate right</string>
</property>
<property name="icon">
<iconset theme="object-rotate-right">
<normaloff/>
</iconset>
</property>
</widget>
</item>
<item row="9" column="0">
<widget class="QPushButton" name="m_quit">
<property name="enabled">
<bool>true</bool>
</property>
<property name="text">
<string>Quit</string>
</property>
<property name="icon">
<iconset theme="application-exit">
<normaloff/>
</iconset>
</property>
</widget>
</item>
<item row="0" column="1" rowspan="10">
<widget class="QLabel" name="m_image">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="frameShape">
<enum>QFrame::Box</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Plain</enum>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="7" column="0">
<widget class="QComboBox" name="m_view">
<item>
<property name="text">
<string>Ignore</string>
</property>
</item>
<item>
<property name="text">
<string>Keep</string>
</property>
</item>
<item>
<property name="text">
<string>Expanding</string>
</property>
</item>
</widget>
</item>
<item row="10" column="0" colspan="2">
<widget class="QListWidget" name="m_thumbnail">
<property name="verticalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
<property name="flow">
<enum>QListView::LeftToRight</enum>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

View file

@ -0,0 +1,10 @@
[Desktop Entry]
Name=KImageViewer
GenericName=Simple image viewer for KDE
Comment=A simple program to view your image files
Exec=kimageviewer --icon '%i' --caption '%c' %U
Terminal=false
Icon=view-preview
Type=Application
Categories=Qt;KDE;Graphics;Viewer;Photography;
MimeType=image/gif;image/jpeg;image/png;image/bmp;image/x-eps;image/x-ico;image/x-portable-bitmap;image/x-portable-graymap;image/x-portable-pixmap;image/x-xbitmap;image/x-xpixmap;image/tiff;image/x-psd;image/x-webp;image/webp;

View file

@ -0,0 +1,8 @@
[Desktop Entry]
Icon=view-preview
Name=KImageViwer
Comment=A simple program to view your image files
X-KDE-ServiceTypes=KParts/ReadOnlyPart,Browser/View
X-KDE-Library=kimageviewerpart
Type=Service
MimeType=image/gif;image/jpeg;image/png;image/bmp;image/x-eps;image/x-ico;image/x-portable-bitmap;image/x-portable-graymap;image/x-portable-pixmap;image/x-xbitmap;image/x-xpixmap;image/tiff;image/x-psd;image/x-webp;image/webp;

View file

@ -0,0 +1,222 @@
/***********************************************************************
* Copyright 2016 Ivailo Monev <xakepa10@gmail.com>
*
* 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) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* 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, see <http://www.gnu.org/licenses/>.
***********************************************************************/
#include "kimagewidget.h"
#include "ui_kimageui.h"
#include <KDebug>
#include <KFileDialog>
#include <KMessageBox>
#include <KLocale>
#include <KFileMetaDataWidget>
namespace KImageViewer {
#define mimefilter "image/gif image/jpeg image/png image/bmp image/x-eps image/x-ico image/x-portable-bitmap image/x-portable-graymap image/x-portable-pixmap image/x-xbitmap image/x-xpixmap image/tiff image/x-psd image/x-webp image/webp"
KImageWidget::KImageWidget(QWidget *parent)
: QWidget(parent), m_ui(0)
{
m_ui = new Ui_KImageUI();
m_ui->setupUi(this);
m_mode = Qt::KeepAspectRatio;
setApplication(false);
connect(m_ui->m_open, SIGNAL(clicked()), this, SLOT(openImage()));
connect(m_ui->m_save, SIGNAL(clicked()), this, SLOT(saveImage()));
connect(m_ui->m_rotateleft, SIGNAL(clicked()), this, SLOT(rotateLeft()));
connect(m_ui->m_rotateright, SIGNAL(clicked()), this, SLOT(rotateRight()));
connect(m_ui->m_view, SIGNAL(currentIndexChanged(QString)), this, SLOT(changeMode(QString)));
connect(m_ui->m_quit, SIGNAL(clicked()), qApp, SLOT(quit()));
}
bool KImageWidget::saveImage()
{
if (m_ui->m_image->pixmap()->isNull()) {
return false;
}
QString path = KFileDialog::getSaveFileName(KUrl(), QLatin1String(mimefilter), this);
if (path.isEmpty()) {
// probably canceled open request
return false;
} else if (!m_ui->m_image->pixmap()->save(path)) {
KMessageBox::information(this, i18n("Could not save image to path: %1.", path));
return false;
}
return true;
}
bool KImageWidget::openImage()
{
QString path = KFileDialog::getOpenFileName(KUrl(), QLatin1String(mimefilter), this);
if (path.isEmpty()) {
// probably canceled open request
return false;
}
if (!setImage(path)) {
return false;
}
return true;
}
bool KImageWidget::rotateLeft()
{
// TODO: implement
return false;
}
bool KImageWidget::rotateRight()
{
// TODO: implement
return false;
}
bool KImageWidget::changeMode(QString mode)
{
if (mode == QLatin1String("Ignore")) {
m_ui->m_image->setPixmap(resizeIfNeeded(m_original, Qt::IgnoreAspectRatio));
return true;
} else if (mode == QLatin1String("Keep")) {
m_ui->m_image->setPixmap(resizeIfNeeded(m_original, Qt::KeepAspectRatio));
return true;
} else if (mode == QLatin1String("Expanding")) {
m_ui->m_image->setPixmap(resizeIfNeeded(m_original, Qt::KeepAspectRatioByExpanding));
return true;
}
return false;
}
bool KImageWidget::setImage(QString path, Qt::AspectRatioMode mode)
{
QPixmap p;
p.load(path);
if (p.isNull()) {
return false;
}
m_original = p;
m_ui->m_image->setPixmap(resizeIfNeeded(p, mode));
setWrite(true); // TODO: make that conditional
return true;
}
bool KImageWidget::setImage(QImage image, Qt::AspectRatioMode mode)
{
QPixmap p = QPixmap::fromImage(image);
if (p.isNull()) {
return false;
}
m_original = p;
m_ui->m_image->setPixmap(resizeIfNeeded(p, mode));
setWrite(true); // TODO: make that conditional
return true;
}
bool KImageWidget::setImage(QPixmap pixmap, Qt::AspectRatioMode mode)
{
if (pixmap.isNull()) {
return false;
}
m_original = pixmap;
m_ui->m_image->setPixmap(resizeIfNeeded(pixmap, mode));
setWrite(true); // TODO: make that conditional
return true;
}
void KImageWidget::setMode(Qt::AspectRatioMode mode)
{
m_mode = mode;
if (!m_original.isNull()) {
m_ui->m_image->setPixmap(resizeIfNeeded(m_original, mode));
}
}
Qt::AspectRatioMode KImageWidget::mode()
{
return m_mode;
}
void KImageWidget::setApplication(bool application)
{
m_application = application;
if (application) {
m_ui->m_open->setEnabled(true);
m_ui->m_quit->show();
} else {
m_ui->m_open->setEnabled(false);
m_ui->m_quit->hide();
}
}
bool KImageWidget::application()
{
return m_application;
}
void KImageWidget::setWrite(bool write)
{
m_write = write;
if (write) {
m_ui->m_save->setEnabled(true);
m_ui->m_rotateleft->setEnabled(true);
m_ui->m_rotateright->setEnabled(true);
} else {
m_ui->m_save->setEnabled(false);
m_ui->m_rotateleft->setEnabled(false);
m_ui->m_rotateright->setEnabled(false);
}
}
bool KImageWidget::write()
{
return m_write;
}
QSize KImageWidget::sizeHint() const
{
if (m_original.isNull()) {
// since no image has been set yet the size is useless
return QSize(640, 480);
}
return m_ui->m_image->size();
}
QPixmap KImageWidget::resizeIfNeeded(QPixmap pixmap, Qt::AspectRatioMode mode)
{
if (pixmap.height() > m_ui->m_image->height()) {
return pixmap.scaled(m_ui->m_image->size(), mode);
}
return pixmap;
}
void KImageWidget::resizeEvent(QResizeEvent *event)
{
if(m_original.isNull()) {
// can happend upon application startup before setImage()
return;
}
m_ui->m_image->setPixmap(resizeIfNeeded(m_original, m_mode));
}
} //namespace KImageViewer
#include "moc_kimagewidget.cpp"

View file

@ -0,0 +1,72 @@
/***********************************************************************
* Copyright 2016 Ivailo Monev <xakepa10@gmail.com>
*
* 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) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* 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, see <http://www.gnu.org/licenses/>.
***********************************************************************/
#ifndef KIMAGEWIDGET_H
#define KIMAGEWIDGET_H
#include <QWidget>
#include <QEvent>
class Ui_KImageUI;
namespace KImageViewer
{
class KImageWidget : public QWidget
{
Q_OBJECT
public:
KImageWidget(QWidget *parent = 0);
bool setImage(QString path, Qt::AspectRatioMode mode = Qt::KeepAspectRatio);
bool setImage(QImage image, Qt::AspectRatioMode mode = Qt::KeepAspectRatio);
bool setImage(QPixmap pixmap, Qt::AspectRatioMode mode = Qt::KeepAspectRatio);
void setMode(Qt::AspectRatioMode mode);
Qt::AspectRatioMode mode();
void setApplication(bool application);
bool application();
void setWrite(bool write);
bool write();
QSize sizeHint() const;
public slots:
bool saveImage();
bool openImage();
bool rotateLeft();
bool rotateRight();
bool changeMode(QString mode);
private:
Ui_KImageUI *m_ui;
QPixmap resizeIfNeeded(QPixmap pixmap, Qt::AspectRatioMode mode);
void resizeEvent(QResizeEvent *event);
int m_resizehits;
QPixmap m_original;
Qt::AspectRatioMode m_mode;
bool m_application;
bool m_write;
};
} // namespace KImageViewer
#endif // KIMAGEWIDGET_H

83
kimageviewer/main.cpp Normal file
View file

@ -0,0 +1,83 @@
/***********************************************************************
* Copyright 2016 Ivailo Monev <xakepa10@gmail.com>
*
* 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) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* 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, see <http://www.gnu.org/licenses/>.
***********************************************************************/
#include "kimagepart.h"
#include <KAboutData>
#include <KCmdLineArgs>
#include <KApplication>
#include <KPluginLoader>
#include <KPluginFactory>
#include <KParts/Part>
#include <KLocale>
#include <KMessageBox>
#include <KDebug>
#include <KMainWindow>
int main(int argc, char **argv) {
KAboutData aboutData("kimageviewer", 0, ki18n("KImageViewer"),
"1.0.0", ki18n("Simple image viewer for KDE."),
KAboutData::License_GPL_V2,
ki18n("(c) 2016 Ivailo Monev"),
KLocalizedString(),
"http://github.com/fluxer/kde-playground"
);
aboutData.addAuthor(ki18n("Ivailo Monev"),
ki18n("Maintainer"),
"xakepa10@gmail.com");
aboutData.setProgramIconName(QLatin1String("view-preview"));
KCmdLineArgs::init(argc, argv, &aboutData);
KCmdLineOptions option;
option.add("+[url]", ki18n("URL to be opened"));
KCmdLineArgs::addCmdLineOptions(option);
KApplication *app = new KApplication();
KMainWindow *window = new KMainWindow();
KPluginLoader loader(QLatin1String("kimageviewerpart"));
KPluginFactory *factory = loader.factory();
KParts::ReadOnlyPart *part;
if (factory) {
part = factory->create<KParts::ReadOnlyPart>(window);
}
if (!factory || !part) {
KMessageBox::error(window, i18n("Unable to load KImageViwer's KPart component, please check your installation."));
kWarning() << "Error loading KImageViwer KPart: " << loader.errorString();
return false;
}
part->setObjectName(QLatin1String("KImageViwer"));
static_cast<KImageViewer::KImageWidget*>(part->widget())->setApplication(true);
window->setCentralWidget(part->widget());
// window->setAutoSaveSettings();
window->show();
app->connect(app, SIGNAL(saveYourself()), part, SLOT(closeUrl()));
KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
for (int pos = 0; pos < args->count(); ++pos) {
part->openUrl(args->url(pos));
}
return app->exec();
}