improve examples

This commit is contained in:
Syping 2021-10-14 07:09:58 +02:00
parent 8ea05274f0
commit 0f94a9920c
4 changed files with 14 additions and 11 deletions

View file

@ -29,7 +29,7 @@ PhotoViewer::PhotoViewer(Gtk::Window *win) : p_win(win)
{ {
} }
void PhotoViewer::open_file(const char *filename) void PhotoViewer::open_file(const std::string &filename)
{ {
if (p_image) if (p_image)
p_image.clear(); p_image.clear();
@ -39,7 +39,7 @@ void PhotoViewer::open_file(const char *filename)
// Read file // Read file
std::ifstream ifs(filename, std::ios::in | std::ios::binary); std::ifstream ifs(filename, std::ios::in | std::ios::binary);
if (!ifs.is_open()) { if (!ifs.is_open()) {
Gtk::MessageDialog msg(*p_win, "Failed to open file: " + Glib::ustring(filename), false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_OK, true); Gtk::MessageDialog msg(*p_win, "Failed to open file: " + filename, false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_OK, true);
msg.set_title("Open Photo"); msg.set_title("Open Photo");
msg.run(); msg.run();
return; return;
@ -52,7 +52,7 @@ void PhotoViewer::open_file(const char *filename)
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); Gtk::MessageDialog msg(*p_win, "Failed to read photo: " + filename, false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_OK, true);
msg.set_title("Open Photo"); msg.set_title("Open Photo");
msg.run(); msg.run();
return; return;

View file

@ -27,7 +27,7 @@ class PhotoViewer : public Gtk::DrawingArea
{ {
public: public:
PhotoViewer(Gtk::Window *win); PhotoViewer(Gtk::Window *win);
void open_file(const char *filename); void open_file(const std::string &filename);
protected: protected:
bool on_draw(const Cairo::RefPtr<Cairo::Context>& cr) override; bool on_draw(const Cairo::RefPtr<Cairo::Context>& cr) override;

View file

@ -96,14 +96,14 @@ 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().c_str()); photo_viewer.open_file(file->get_path());
} }
app->add_window(win); app->add_window(win);
win.show(); win.present();
} }
else { else {
app->add_window(win); app->add_window(win);
win.show(); win.present();
Gtk::MessageDialog msg(win, "Can't open multiple photos at once!", false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_OK, true); 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.set_title("RagePhoto GTK Photo Viewer");
msg.run(); msg.run();
@ -114,6 +114,7 @@ int main(int argc, char *argv[])
vertical_box.show(); vertical_box.show();
win.add(vertical_box); win.add(vertical_box);
win.present();
return app->run(win); return app->run(win);
} }

View file

@ -68,7 +68,7 @@ int main(int argc, char *argv[])
QMainWindow mainWindow; QMainWindow mainWindow;
mainWindow.setWindowTitle("RagePhoto Qt Photo Viewer"); mainWindow.setWindowTitle("RagePhoto Qt Photo Viewer");
mainWindow.setFixedSize(400, 100); mainWindow.setFixedSize(400, 0);
QWidget centralWidget(&mainWindow); QWidget centralWidget(&mainWindow);
mainWindow.setCentralWidget(&centralWidget); mainWindow.setCentralWidget(&centralWidget);
@ -77,7 +77,7 @@ int main(int argc, char *argv[])
verticalLayout.setContentsMargins(0, 0, 0, 0); verticalLayout.setContentsMargins(0, 0, 0, 0);
verticalLayout.setSpacing(6); verticalLayout.setSpacing(6);
QLabel photoLabel(&mainWindow); QLabel photoLabel(&centralWidget);
verticalLayout.addWidget(&photoLabel); verticalLayout.addWidget(&photoLabel);
QHBoxLayout horizontalLayout; QHBoxLayout horizontalLayout;
@ -85,7 +85,7 @@ int main(int argc, char *argv[])
horizontalLayout.setSpacing(6); horizontalLayout.setSpacing(6);
verticalLayout.addLayout(&horizontalLayout); verticalLayout.addLayout(&horizontalLayout);
QPushButton openButton("Open", &mainWindow); QPushButton openButton("Open", &centralWidget);
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, [&](){
@ -98,12 +98,14 @@ int main(int argc, char *argv[])
}); });
} }
}); });
openButton.setAutoDefault(true);
horizontalLayout.addWidget(&openButton); horizontalLayout.addWidget(&openButton);
QPushButton closeButton("Close", &mainWindow); QPushButton closeButton("Close", &centralWidget);
if (QIcon::hasThemeIcon("dialog-close")) if (QIcon::hasThemeIcon("dialog-close"))
closeButton.setIcon(QIcon::fromTheme("dialog-close")); closeButton.setIcon(QIcon::fromTheme("dialog-close"));
QObject::connect(&closeButton, &QPushButton::clicked, &mainWindow, &QMainWindow::close); QObject::connect(&closeButton, &QPushButton::clicked, &mainWindow, &QMainWindow::close);
closeButton.setAutoDefault(true);
horizontalLayout.addWidget(&closeButton); horizontalLayout.addWidget(&closeButton);
const QStringList args = app.arguments(); const QStringList args = app.arguments();