improve examples

This commit is contained in:
Syping 2021-10-30 15:31:29 +02:00
parent 6b262e2ddc
commit 7bc3cabc89
5 changed files with 49 additions and 143 deletions

View file

@ -27,13 +27,8 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(PkgConfig REQUIRED) find_package(PkgConfig REQUIRED)
pkg_check_modules(GTKMM REQUIRED gtkmm-3.0) pkg_check_modules(GTKMM REQUIRED gtkmm-3.0)
set(GTKVIEWER_HEADERS
src/PhotoViewer.h
)
set(GTKVIEWER_SOURCES set(GTKVIEWER_SOURCES
src/main.cpp src/main.cpp
src/PhotoViewer.cpp
) )
if (TARGET ragephoto) if (TARGET ragephoto)
@ -43,7 +38,7 @@ else()
pkg_check_modules(RAGEPHOTO REQUIRED ragephoto) pkg_check_modules(RAGEPHOTO REQUIRED ragephoto)
endif() endif()
add_executable(ragephoto-gtkviewer WIN32 ${GTKVIEWER_HEADERS} ${GTKVIEWER_SOURCES}) add_executable(ragephoto-gtkviewer WIN32 ${GTKVIEWER_SOURCES})
target_compile_options(ragephoto-gtkviewer PRIVATE ${GTKMM_CFLAGS} ${RAGEPHOTO_CFLAGS}) target_compile_options(ragephoto-gtkviewer PRIVATE ${GTKMM_CFLAGS} ${RAGEPHOTO_CFLAGS})
target_link_libraries(ragephoto-gtkviewer PRIVATE ${GTKMM_LIBRARIES} ${RAGEPHOTO_LIBRARIES}) target_link_libraries(ragephoto-gtkviewer PRIVATE ${GTKMM_LIBRARIES} ${RAGEPHOTO_LIBRARIES})
if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.13.0") if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.13.0")

View file

@ -1,87 +0,0 @@
/*****************************************************************************
* libragephoto RAGE Photo Parser
* Copyright (C) 2021 Syping
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* This software is provided as-is, no warranties are given to you, we are not
* responsible for anything with use of the software, you are self responsible.
*****************************************************************************/
#include "PhotoViewer.h"
#include <cairomm/context.h>
#include <gdkmm/general.h>
#include <gtkmm/messagedialog.h>
#include <RagePhoto.h>
#include <fstream>
#include <iostream>
#include <iterator>
PhotoViewer::PhotoViewer(Gtk::Window *win) : p_win(win)
{
}
void PhotoViewer::open_file(const std::string &filename)
{
if (p_image)
p_image.clear();
RagePhoto ragePhoto;
// Read file
std::ifstream ifs(filename, std::ios::in | std::ios::binary);
if (!ifs.is_open()) {
Gtk::MessageDialog msg(*p_win, "Failed to open file: " + filename, false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_OK, true);
msg.set_title("Open Photo");
msg.run();
return;
}
std::string data(std::istreambuf_iterator<char>{ifs}, {});
ifs.close();
// Load Photo
const bool loaded = ragePhoto.load(data);
if (!loaded) {
const RagePhoto::Error error = ragePhoto.error();
if (error <= RagePhoto::Error::PhotoReadError) {
Gtk::MessageDialog msg(*p_win, "Failed to read photo: " + filename, false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_OK, true);
msg.set_title("Open Photo");
msg.run();
return;
}
}
GdkPixbufLoader *loader = gdk_pixbuf_loader_new();
gdk_pixbuf_loader_write(loader, reinterpret_cast<const guchar*>(ragePhoto.photoData()), ragePhoto.photoSize(), NULL);
GdkPixbuf *c_pixbuf = gdk_pixbuf_loader_get_pixbuf(loader);
gdk_pixbuf_loader_close(loader, NULL);
p_image = Glib::wrap(c_pixbuf);
p_win->set_title("RagePhoto GTK Photo Viewer - " + ragePhoto.title());
if (p_image)
set_size_request(p_image->get_width(), p_image->get_height());
}
bool PhotoViewer::on_draw(const Cairo::RefPtr<Cairo::Context>& cr)
{
if (!p_image)
return false;
Gtk::Allocation allocation = get_allocation();
const int width = allocation.get_width();
const int height = allocation.get_height();
Gdk::Cairo::set_source_pixbuf(cr, p_image, (width - p_image->get_width()) / 2, (height - p_image->get_height()) / 2);
cr->paint();
return true;
}

View file

@ -1,38 +0,0 @@
/*****************************************************************************
* libragephoto RAGE Photo Parser
* Copyright (C) 2021 Syping
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* This software is provided as-is, no warranties are given to you, we are not
* responsible for anything with use of the software, you are self responsible.
*****************************************************************************/
#ifndef PHOTOVIEWER_H
#define PHOTOVIEWER_H
#include <gdkmm/pixbuf.h>
#include <gtkmm/drawingarea.h>
#include <gtkmm/window.h>
class PhotoViewer : public Gtk::DrawingArea
{
public:
PhotoViewer(Gtk::Window *win);
void open_file(const std::string &filename);
protected:
bool on_draw(const Cairo::RefPtr<Cairo::Context>& cr) override;
Glib::RefPtr<Gdk::Pixbuf> p_image;
Gtk::Window *p_win;
};
#endif // PHOTOVIEWER_H

View file

@ -16,16 +16,52 @@
* responsible for anything with use of the software, you are self responsible. * responsible for anything with use of the software, you are self responsible.
*****************************************************************************/ *****************************************************************************/
#include "PhotoViewer.h" #include <RagePhoto.h>
#include <gtkmm/application.h> #include <gtkmm/application.h>
#include <gtkmm/box.h> #include <gtkmm/box.h>
#include <gtkmm/button.h> #include <gtkmm/button.h>
#include <gtkmm/image.h>
#include <gtkmm/filechooserdialog.h> #include <gtkmm/filechooserdialog.h>
#include <gtkmm/filefilter.h> #include <gtkmm/filefilter.h>
#include <gtkmm/label.h> #include <gtkmm/label.h>
#include <gtkmm/messagedialog.h> #include <gtkmm/messagedialog.h>
#include <gtkmm/window.h> #include <gtkmm/window.h>
#include <fstream>
#include <iostream> #include <iostream>
#include <iterator>
bool readPhotoFile(const std::string &filename, Gtk::Window *win, Gtk::Image *image)
{
std::ifstream ifs(filename, std::ios::in | std::ios::binary);
if (ifs.is_open()) {
std::string data(std::istreambuf_iterator<char>{ifs}, {});
ifs.close();
RagePhoto ragePhoto;
const bool loaded = ragePhoto.load(data);
if (!loaded) {
const RagePhoto::Error error = ragePhoto.error();
if (error <= RagePhoto::Error::PhotoReadError) {
Gtk::MessageDialog msg(*win, "Failed to read photo: " + filename, false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_OK, true);
msg.set_title("Open Photo");
msg.run();
return false;
}
}
GdkPixbufLoader *pixbuf_loader = gdk_pixbuf_loader_new();
gdk_pixbuf_loader_write(pixbuf_loader, reinterpret_cast<const guchar*>(ragePhoto.photoData()), ragePhoto.photoSize(), NULL);
GdkPixbuf *pixbuf = gdk_pixbuf_loader_get_pixbuf(pixbuf_loader);
gdk_pixbuf_loader_close(pixbuf_loader, NULL);
image->set(Glib::wrap(pixbuf));
win->set_title("RagePhoto GTK Photo Viewer - " + ragePhoto.title());
return true;
}
else {
Gtk::MessageDialog msg(*win, "Failed to open file: " + filename, false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_OK, true);
msg.set_title("Open Photo");
msg.run();
}
return false;
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
@ -40,9 +76,9 @@ int main(int argc, char *argv[])
vertical_box.set_margin_bottom(6); vertical_box.set_margin_bottom(6);
vertical_box.set_spacing(6); vertical_box.set_spacing(6);
PhotoViewer photo_viewer(&win); Gtk::Image image;
vertical_box.add(photo_viewer); vertical_box.add(image);
photo_viewer.show(); image.show();
Gtk::Box horizontal_box(Gtk::ORIENTATION_HORIZONTAL); Gtk::Box horizontal_box(Gtk::ORIENTATION_HORIZONTAL);
horizontal_box.set_margin_left(6); horizontal_box.set_margin_left(6);
@ -62,17 +98,17 @@ int main(int argc, char *argv[])
dialog.add_button("_Cancel", Gtk::RESPONSE_CANCEL); dialog.add_button("_Cancel", Gtk::RESPONSE_CANCEL);
dialog.add_button("_Open", Gtk::RESPONSE_OK); dialog.add_button("_Open", Gtk::RESPONSE_OK);
Glib::RefPtr<Gtk::FileFilter> filter_photo = Gtk::FileFilter::create(); Glib::RefPtr<Gtk::FileFilter> ragephoto_filter = Gtk::FileFilter::create();
filter_photo->set_name("GTA V Photo"); ragephoto_filter->set_name("RagePhoto compatible");
filter_photo->add_pattern("PGTA5*"); ragephoto_filter->add_pattern("PGTA5*");
dialog.add_filter(filter_photo); ragephoto_filter->add_pattern("PRDR3*");
dialog.add_filter(ragephoto_filter);
int result = dialog.run(); int result = dialog.run();
switch(result) { switch(result) {
case Gtk::RESPONSE_OK: { case Gtk::RESPONSE_OK: {
std::string filename = dialog.get_filename(); readPhotoFile(dialog.get_filename(), &win, &image);
photo_viewer.open_file(filename.c_str());
break; break;
} }
default: default:
@ -96,7 +132,7 @@ int main(int argc, char *argv[])
app->signal_open().connect([&](const Gio::Application::type_vec_files &files, const Glib::ustring &hint) { app->signal_open().connect([&](const Gio::Application::type_vec_files &files, const Glib::ustring &hint) {
if (files.size() == 1) { if (files.size() == 1) {
for (const auto &file : files) { for (const auto &file : files) {
photo_viewer.open_file(file->get_path()); readPhotoFile(file->get_path(), &win, &image);
} }
app->add_window(win); app->add_window(win);
win.present(); win.present();

View file

@ -89,7 +89,7 @@ int main(int argc, char *argv[])
if (QIcon::hasThemeIcon("document-open")) if (QIcon::hasThemeIcon("document-open"))
openButton.setIcon(QIcon::fromTheme("document-open")); openButton.setIcon(QIcon::fromTheme("document-open"));
QObject::connect(&openButton, &QPushButton::clicked, &mainWindow, [&](){ QObject::connect(&openButton, &QPushButton::clicked, &mainWindow, [&](){
const QString filename = QFileDialog::getOpenFileName(&mainWindow, "Open Photo...", QString(), "GTA V Photo (PGTA5*)"); const QString filename = QFileDialog::getOpenFileName(&mainWindow, "Open Photo...", QString(), "RagePhoto compatible (PGTA5* PRDR3*)");
if (filename.isEmpty()) if (filename.isEmpty())
return; return;
if (readPhotoFile(filename, &mainWindow, &photoLabel)) { if (readPhotoFile(filename, &mainWindow, &photoLabel)) {