diff --git a/databasewindow.cpp b/databasewindow.cpp index 6c72892..9fed144 100644 --- a/databasewindow.cpp +++ b/databasewindow.cpp @@ -1,36 +1,30 @@ #include #include +#include +#include #include #include +#include #include #include "databasewindow.hpp" #include "filesystem.hpp" #include "functions.hpp" +#include "gtkfunctions.hpp" #include "progresswindow.hpp" #include "tv_rename.hpp" DatabaseWindow::~DatabaseWindow() { auto children = get_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]; - } + freeChildren( children ); } -DatabaseWindow::DatabaseWindow( bool _linux, - Glib::RefPtr< Gtk::Application > _app ) - : linux( _linux ), app( _app ) { +DatabaseWindow::DatabaseWindow( + bool _linux, std::map< std::string, std::string > &_language_map, + Glib::RefPtr< Gtk::Application > _app ) + : linux( _linux ), language_map( _language_map ), app( _app ) { set_title( "Database" ); + property_modal().set_value( true ); set_default_size( 550, 350 ); set_resizable( false ); @@ -87,19 +81,37 @@ DatabaseWindow::DatabaseWindow( bool _linux, row[m_columns_database.m_col_show] = x["SHOW"]; row[m_columns_database.m_col_path] = x["PATH"]; row[m_columns_database.m_col_lang] = x["LANGUAGE"]; + row[m_columns_database.m_col_lang_full] = language_map[x["LANGUAGE"]]; row[m_columns_database.m_col_show_id] = x["TVID"]; row[m_columns_database.m_col_dvd] = x["DVD"] == "1"; } m_tree_database->append_column( "Show", m_columns_database.m_col_show ); - m_tree_database->append_column_editable( "Path", - m_columns_database.m_col_path ); - m_tree_database->append_column_editable( "Language", - m_columns_database.m_col_lang ); - m_tree_database->append_column_editable( "ID", - m_columns_database.m_col_show_id ); + m_tree_database->append_column( "Path", m_columns_database.m_col_path ); + m_tree_database->append_column( "Language", m_combo_language ); m_tree_database->append_column_editable( "DVD", m_columns_database.m_col_dvd ); + m_tree_database->signal_button_press_event().connect( + sigc::mem_fun( *this, &DatabaseWindow::treeViewClick ) ); + + auto model = Gtk::ListStore::create( m_columns_language ); + m_combo_language.property_model().set_value( model ); + m_combo_language.property_editable().set_value( true ); + m_combo_language.property_text_column().set_value( 1 ); + m_combo_language.property_has_entry().set_value( false ); + + m_combo_language.signal_changed().connect( + sigc::mem_fun( *this, &DatabaseWindow::languageChange ) ); + + for ( const auto &x : language_map ) { + auto row = model->append(); + ( *row )[m_columns_language.m_col_code] = x.first; + ( *row )[m_columns_language.m_col_language] = x.second; + } + + // set text of m_combo_language to m_columns_database.m_col_lang_full + m_tree_database->get_column( 2 )->add_attribute( + m_combo_language, "text", m_columns_database.m_col_lang_full ); button_save->signal_clicked().connect( sigc::mem_fun( *this, &DatabaseWindow::save ) ); @@ -114,6 +126,64 @@ DatabaseWindow::DatabaseWindow( bool _linux, show_all_children(); } +void DatabaseWindow::languageChange( const Glib::ustring &str, + const Gtk::TreeIter &it ) { + auto lang_row = *it; + auto database_row = *( m_model->get_iter( str ) ); + database_row[m_columns_database.m_col_lang] = + std::string( lang_row[m_columns_language.m_col_code] ); + database_row[m_columns_database.m_col_lang_full] = + std::string( lang_row[m_columns_language.m_col_language] ); +} + +bool DatabaseWindow::treeViewClick( GdkEventButton *event ) { + Gtk::TreeModel::Path path; + Gtk::TreeViewColumn *column; + // these variables are unused, but required for get_path_at_pos + int x_column, y_column; + m_tree_database->get_path_at_pos( event->x, event->y, path, column, + x_column, y_column ); + if ( column->get_title() == "Show" ) { + // set default values + id_search = "_"; + show_search = "_"; + lang_search = "_"; + sw.reset( new SearchWindow( show_search, id_search, lang_search, + language_map ) ); + sw->signal_hide().connect( + sigc::mem_fun( *this, &DatabaseWindow::finishedSearch ) ); + + app->add_window( *sw ); + sw->show(); + } else if ( column->get_title() == "Path" ) { + Gtk::FileChooserDialog dialog( "Select a directory", + Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER ); + dialog.set_transient_for( *this ); + dialog.add_button( "_Cancel", Gtk::RESPONSE_CANCEL ); + dialog.add_button( "Select", Gtk::RESPONSE_OK ); + auto result = dialog.run(); + if ( result == Gtk::RESPONSE_OK ) { + auto selection = m_tree_database->get_selection(); + auto row = *( selection->get_selected() ); + ( *row )[m_columns_database.m_col_path] = dialog.get_filename(); + } + } + return true; +} + +void DatabaseWindow::finishedSearch() { + app->remove_window( *sw ); + sw.reset(); + if ( id_search == "_" || show_search == "_" || lang_search == "_" ) + return; + auto selection = m_tree_database->get_selection(); + auto row = *( selection->get_selected() ); + ( *row )[m_columns_database.m_col_show] = show_search; + ( *row )[m_columns_database.m_col_show_id] = id_search; + ( *row )[m_columns_database.m_col_lang] = lang_search; + ( *row )[m_columns_database.m_col_lang_full] = language_map[lang_search]; +} + void DatabaseWindow::save() { if ( changed_rows.size() == 0 ) return; diff --git a/databasewindow.hpp b/databasewindow.hpp index b66cb56..cb98d8e 100644 --- a/databasewindow.hpp +++ b/databasewindow.hpp @@ -1,15 +1,20 @@ #ifndef GTKMM_DATABASE_WINDOW #define GTKMM_DATABASE_WINDOW +#include #include #include #include #include +#include "searchwindow.hpp" + class DatabaseWindow : public Gtk::Window { public: DatabaseWindow() = delete; - DatabaseWindow( bool _linux, Glib::RefPtr< Gtk::Application > _app ); + DatabaseWindow( bool _linux, + std::map< std::string, std::string > &_language_map, + Glib::RefPtr< Gtk::Application > _app ); virtual ~DatabaseWindow(); private: @@ -18,6 +23,9 @@ private: void changed( const Gtk::TreeModel::Path & /*UNUSED*/, const Gtk::TreeModel::iterator &row ); void quit(); + bool treeViewClick( GdkEventButton *event ); + void languageChange( const Glib::ustring &str, const Gtk::TreeIter &it ); + void finishedSearch(); void errorPath( const std::string &path ); void errorID( const std::string &show_id ); @@ -28,6 +36,16 @@ private: protected: Gtk::TreeView *m_tree_database = new Gtk::TreeView(); + class LanguageColumns : public Gtk::TreeModel::ColumnRecord { + public: + LanguageColumns() { + add( m_col_code ); + add( m_col_language ); + } + Gtk::TreeModelColumn< std::string > m_col_code; + Gtk::TreeModelColumn< std::string > m_col_language; + }; + class DatabaseColumns : public Gtk::TreeModel::ColumnRecord { public: DatabaseColumns() { @@ -35,6 +53,7 @@ protected: add( m_col_show ); add( m_col_path ); add( m_col_lang ); + add( m_col_lang_full ); add( m_col_show_id ); add( m_col_dvd ); } @@ -42,15 +61,24 @@ protected: Gtk::TreeModelColumn< std::string > m_col_show; Gtk::TreeModelColumn< std::string > m_col_path; Gtk::TreeModelColumn< std::string > m_col_lang; + Gtk::TreeModelColumn< std::string > m_col_lang_full; Gtk::TreeModelColumn< std::string > m_col_show_id; Gtk::TreeModelColumn< bool > m_col_dvd; }; DatabaseColumns m_columns_database; + LanguageColumns m_columns_language; + Glib::RefPtr< Gtk::TreeStore > m_model; + Gtk::CellRendererCombo m_combo_language; bool linux; + std::map< std::string, std::string > &language_map; Glib::RefPtr< Gtk::Application > app; + std::unique_ptr< SearchWindow > sw{ nullptr }; + std::string show_search; + std::string id_search; + std::string lang_search; }; #endif // GTKMM_MAIN_WINDOW