Move some shared functions into gtkfunctions.cpp
This commit is contained in:
parent
7f69b3c662
commit
8e4f348b8d
65
gtkfunctions.cpp
Normal file
65
gtkfunctions.cpp
Normal file
@ -0,0 +1,65 @@
|
||||
#include "gtkfunctions.hpp"
|
||||
|
||||
#include <gtkmm/liststore.h>
|
||||
#include <gtkmm/messagedialog.h>
|
||||
|
||||
#include "tv_rename.hpp"
|
||||
|
||||
void searchShow( Gtk::Entry *entry_show, Gtk::ComboBox *combo_possible,
|
||||
const Gtk::TreeModelColumn< std::string > &col_show,
|
||||
const Gtk::TreeModelColumn< std::string > &col_id,
|
||||
const std::string &language_code, Gtk::Window *parent ) {
|
||||
// check required field is filled out
|
||||
if ( entry_show->get_text().empty() ) {
|
||||
Gtk::MessageDialog dialog( *parent, "Show field is empty" );
|
||||
dialog.run();
|
||||
return;
|
||||
}
|
||||
|
||||
// fill up m_combo_possible with possible tv shows
|
||||
auto possible_shows =
|
||||
searchShow( std::string( entry_show->get_text() ), language_code );
|
||||
|
||||
// if no possible shows were found, tell the user
|
||||
if ( possible_shows.size() == 0 ) {
|
||||
Gtk::MessageDialog dialog( *parent,
|
||||
"No results found for given show name" );
|
||||
dialog.run();
|
||||
return;
|
||||
}
|
||||
|
||||
// show widgets
|
||||
combo_possible->show();
|
||||
|
||||
// fill up combo box with results from thetvdb
|
||||
auto model =
|
||||
static_cast< Gtk::ListStore * >( combo_possible->get_model().get() );
|
||||
|
||||
auto row = *( model->append() );
|
||||
|
||||
row[col_show] = possible_shows[0].first;
|
||||
row[col_id] = possible_shows[0].second;
|
||||
combo_possible->set_active( row );
|
||||
|
||||
for ( size_t i = 1; i < possible_shows.size(); i++ ) {
|
||||
auto row = *( model->append() );
|
||||
row[col_show] = possible_shows[i].first;
|
||||
row[col_id] = possible_shows[i].second;
|
||||
}
|
||||
}
|
||||
|
||||
void freeChildren( std::vector< Gtk::Widget * > &children ) {
|
||||
size_t max = children.size();
|
||||
size_t index{};
|
||||
while ( index < max ) {
|
||||
if ( auto *p = dynamic_cast< Gtk::Container * >( children[index] ) ) {
|
||||
auto temp = p->get_children();
|
||||
children.insert( children.end(), temp.begin(), temp.end() );
|
||||
max = children.size();
|
||||
}
|
||||
index++;
|
||||
}
|
||||
for ( int i = max - 1; i >= 0; i-- ) {
|
||||
delete children[i];
|
||||
}
|
||||
}
|
14
gtkfunctions.hpp
Normal file
14
gtkfunctions.hpp
Normal file
@ -0,0 +1,14 @@
|
||||
#ifndef GTK_FUNCTIONS_HPP
|
||||
#define GTK_FUNCTIONS_HPP
|
||||
|
||||
#include <gtkmm/combobox.h>
|
||||
#include <gtkmm/entry.h>
|
||||
|
||||
void searchShow( Gtk::Entry *entry_show, Gtk::ComboBox *combo_possible,
|
||||
const Gtk::TreeModelColumn< std::string > &col_show,
|
||||
const Gtk::TreeModelColumn< std::string > &col_id,
|
||||
const std::string &language_code, Gtk::Window *parent );
|
||||
|
||||
void freeChildren( std::vector< Gtk::Widget * > &children );
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user