improve examples

This commit is contained in:
Syping 2021-08-27 23:17:57 +02:00
parent dd47087afd
commit 61a9770951
3 changed files with 33 additions and 10 deletions

View file

@ -19,6 +19,7 @@
#include "PhotoViewer.h" #include "PhotoViewer.h"
#include <cairomm/context.h> #include <cairomm/context.h>
#include <gdkmm/general.h> #include <gdkmm/general.h>
#include <gtkmm/messagedialog.h>
#include <RagePhoto.h> #include <RagePhoto.h>
#include <iostream> #include <iostream>
@ -32,6 +33,9 @@ void PhotoViewer::open_file(const char *filename)
// Read file // Read file
FILE *file = fopen(filename, "rb"); FILE *file = fopen(filename, "rb");
if (!file) { if (!file) {
Gtk::MessageDialog msg(*p_win, "Failed to read file: " + Glib::ustring(filename), false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_OK, true);
msg.set_title("Open Photo");
msg.run();
return; return;
} }
const int fseek_end_value = fseek(file, 0, SEEK_END); const int fseek_end_value = fseek(file, 0, SEEK_END);
@ -60,9 +64,13 @@ void PhotoViewer::open_file(const char *filename)
free(data); free(data);
if (!loaded) { if (!loaded) {
const RagePhoto::Error error = ragePhoto.error(); const RagePhoto::Error error = ragePhoto.error();
if (error <= RagePhoto::Error::PhotoReadError) if (error <= RagePhoto::Error::PhotoReadError) {
Gtk::MessageDialog msg(*p_win, "Failed to read photo: " + Glib::ustring(filename), false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_OK, true);
msg.set_title("Open Photo");
msg.run();
return; return;
} }
}
guchar *photoData = static_cast<guchar*>(malloc(ragePhoto.photoSize())); guchar *photoData = static_cast<guchar*>(malloc(ragePhoto.photoSize()));
if (!photoData) if (!photoData)
@ -71,7 +79,7 @@ void PhotoViewer::open_file(const char *filename)
memcpy(photoData, ragePhoto.photoData(), ragePhoto.photoSize()); memcpy(photoData, ragePhoto.photoData(), ragePhoto.photoSize());
GdkPixbufLoader *loader = gdk_pixbuf_loader_new(); GdkPixbufLoader *loader = gdk_pixbuf_loader_new();
gdk_pixbuf_loader_write(loader, photoData, static_cast<gsize>(ragePhoto.photoSize()), nullptr); gdk_pixbuf_loader_write(loader, photoData, static_cast<gsize>(ragePhoto.photoSize()), NULL);
GdkPixbuf *c_pixbuf = gdk_pixbuf_loader_get_pixbuf(loader); GdkPixbuf *c_pixbuf = gdk_pixbuf_loader_get_pixbuf(loader);
p_image = Glib::wrap(c_pixbuf); p_image = Glib::wrap(c_pixbuf);

View file

@ -23,6 +23,7 @@
#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/window.h> #include <gtkmm/window.h>
#include <iostream> #include <iostream>
@ -53,6 +54,7 @@ int main(int argc, char *argv[])
open_button.set_label("Open"); open_button.set_label("Open");
open_button.set_hexpand(true); open_button.set_hexpand(true);
open_button.set_size_request(100); open_button.set_size_request(100);
open_button.set_image_from_icon_name("document-open");
open_button.signal_clicked().connect([&](){ open_button.signal_clicked().connect([&](){
Gtk::FileChooserDialog dialog("Open Photo...", Gtk::FILE_CHOOSER_ACTION_OPEN); Gtk::FileChooserDialog dialog("Open Photo...", Gtk::FILE_CHOOSER_ACTION_OPEN);
dialog.set_transient_for(win); dialog.set_transient_for(win);
@ -84,6 +86,7 @@ int main(int argc, char *argv[])
close_button.set_label("Close"); close_button.set_label("Close");
close_button.set_hexpand(true); close_button.set_hexpand(true);
close_button.set_size_request(100); close_button.set_size_request(100);
close_button.set_image_from_icon_name("dialog-close");
close_button.signal_clicked().connect([&](){ close_button.signal_clicked().connect([&](){
win.close(); win.close();
}); });
@ -91,11 +94,20 @@ int main(int argc, char *argv[])
close_button.show(); close_button.show();
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) {
for (const auto &file : files) { for (const auto &file : files) {
photo_viewer.open_file(file->get_path().c_str()); photo_viewer.open_file(file->get_path().c_str());
} }
app->add_window(win); app->add_window(win);
win.show(); win.show();
}
else {
app->add_window(win);
win.show();
Gtk::MessageDialog msg(win, "Can't open multiple photos at once!", false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_OK, true);
msg.set_title("RagePhoto GTK Photo Viewer");
msg.run();
}
}); });
horizontal_box.show(); horizontal_box.show();

View file

@ -73,20 +73,21 @@ int main(int argc, char *argv[])
QWidget centralWidget(&mainWindow); QWidget centralWidget(&mainWindow);
mainWindow.setCentralWidget(&centralWidget); mainWindow.setCentralWidget(&centralWidget);
QVBoxLayout verticalLayout(&mainWindow); QVBoxLayout verticalLayout(&centralWidget);
verticalLayout.setContentsMargins(0, 0, 0, 0); verticalLayout.setContentsMargins(0, 0, 0, 0);
verticalLayout.setSpacing(6); verticalLayout.setSpacing(6);
centralWidget.setLayout(&verticalLayout);
QLabel photoLabel(&mainWindow); QLabel photoLabel(&mainWindow);
verticalLayout.addWidget(&photoLabel); verticalLayout.addWidget(&photoLabel);
QHBoxLayout horizontalLayout(&mainWindow); QHBoxLayout horizontalLayout;
horizontalLayout.setContentsMargins(6, 0, 6, 6); horizontalLayout.setContentsMargins(6, 0, 6, 6);
horizontalLayout.setSpacing(6); horizontalLayout.setSpacing(6);
verticalLayout.addLayout(&horizontalLayout); verticalLayout.addLayout(&horizontalLayout);
QPushButton openButton("Open", &mainWindow); QPushButton openButton("Open", &mainWindow);
if (QIcon::hasThemeIcon("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(), "GTA V Photo (PGTA5*)");
if (filename.isEmpty()) if (filename.isEmpty())
@ -100,6 +101,8 @@ int main(int argc, char *argv[])
horizontalLayout.addWidget(&openButton); horizontalLayout.addWidget(&openButton);
QPushButton closeButton("Close", &mainWindow); QPushButton closeButton("Close", &mainWindow);
if (QIcon::hasThemeIcon("dialog-close"))
closeButton.setIcon(QIcon::fromTheme("dialog-close"));
QObject::connect(&closeButton, &QPushButton::clicked, &mainWindow, &QMainWindow::close); QObject::connect(&closeButton, &QPushButton::clicked, &mainWindow, &QMainWindow::close);
horizontalLayout.addWidget(&closeButton); horizontalLayout.addWidget(&closeButton);