Browse Source

Добавлено главное окно приложения

Вадим Королёв 1 year ago
parent
commit
b45ffca1cb
6 changed files with 134 additions and 16 deletions
  1. 1 0
      meson.build
  2. 47 0
      src/YTMPV/MainWindow.cpp
  3. 30 0
      src/YTMPV/MainWindow.hpp
  4. 2 2
      src/YTMPV/meson.build
  5. 48 14
      src/YTMPV/ytmpv.cpp
  6. 6 0
      src/YTMPV/ytmpv.hpp

+ 1 - 0
meson.build

@@ -12,6 +12,7 @@ glibmm_dep = dependency('glibmm-2.68')
 curlpp_dep = dependency('curlpp')
 json_dep = dependency('nlohmann_json')
 sqlite3_dep = dependency('sqlite3')
+gtkmm_dep = dependency('gtkmm-4.0')
 
 subdir('lib')
 subdir('src/YTMPV')

+ 47 - 0
src/YTMPV/MainWindow.cpp

@@ -0,0 +1,47 @@
+#include "MainWindow.hpp"
+
+namespace components {
+    
+    MainWindow::MainWindow()
+    : m_btn()
+    {
+        set_title("GTKYT");
+        set_default_size(800, 600);
+        set_icon_name("youtube");
+
+        m_btn.set_child(*(this->getButtonContents("Поиск", "find")));
+        m_btn.signal_clicked().connect(sigc::mem_fun(
+            *this,
+            &MainWindow::on_btn_clicked
+        ));
+        set_child(m_btn);
+    }
+
+    MainWindow::~MainWindow()
+    {
+    }
+
+    void MainWindow::on_btn_clicked()
+    {
+        std::cout << "CLICK!" << std::endl;
+    }
+
+    Gtk::Box* MainWindow::getButtonContents(Glib::ustring text, std::string icon_name)
+    {
+        // Создание иконки и текста
+        auto icon = Gio::Icon::create(icon_name);
+        auto img_icon = Gtk::make_managed<Gtk::Image>(icon);
+        auto lbl_main = Gtk::make_managed<Gtk::Label>(text);
+        lbl_main->set_expand(true);
+
+        // Добавление в контейнер
+        auto box_main = Gtk::make_managed<Gtk::Box>(
+            Gtk::Orientation::HORIZONTAL,
+            5
+        );
+        box_main->append(*img_icon);
+        box_main->append(*lbl_main);
+
+        return box_main;
+    }
+}

+ 30 - 0
src/YTMPV/MainWindow.hpp

@@ -0,0 +1,30 @@
+#pragma once
+#include <iostream>
+#include <gtkmm/window.h>
+#include <gtkmm/button.h>
+#include <gtkmm/image.h>
+#include <gtkmm/box.h>
+#include <gtkmm/label.h>
+
+#include <glibmm/ustring.h>
+#include <giomm/icon.h>
+
+namespace components {
+    // Главное окно приложения
+    class MainWindow : public Gtk::Window {
+    public:
+        MainWindow();
+        ~MainWindow() override;
+
+    protected:
+        // --Обработка событий--
+        void on_btn_clicked();
+
+        // --Виджеты
+        Gtk::Button m_btn;  // Кнопка поиска
+
+        // --Общее--
+        // Возвращает дочерний виджет для кнопок, которым нужны иконка с текстом
+        Gtk::Box* getButtonContents(Glib::ustring text, std::string icon_name);
+    };
+}

+ 2 - 2
src/YTMPV/meson.build

@@ -1,7 +1,7 @@
 ytmpv = executable(
   'ytmpv',
-  'ytmpv.cpp',
+  ['ytmpv.cpp', 'MainWindow.cpp'],
   link_with: [ytapi_lib, database_lib],
   include_directories: [dir_includes],
-  dependencies: [glibmm_dep, curlpp_dep, json_dep]
+  dependencies: [glibmm_dep, curlpp_dep, json_dep, gtkmm_dep]
 )

+ 48 - 14
src/YTMPV/ytmpv.cpp

@@ -33,26 +33,60 @@ db::video getVideoByYTID(std::string yt_id) {
     return output;
 }
 
-int main(int argc, char **argv) {
-    if (argc == 1) {
-        std::cout << "Программа не принимает 0 аргументов";
-        exit(1);
+int main(int argc, char** argv) {
+    // ==Инициализация==
+    // --Язык--
+    std::locale::global(std::locale(""));
+
+    // --Аргументы приложения--
+    Glib::ustring config_path;
+    Glib::OptionEntry opt_config_path = Glib::OptionEntry();
+    opt_config_path.set_long_name("config");
+    opt_config_path.set_short_name('c');
+    opt_config_path.set_flags(Glib::OptionEntry::Flags::NONE);
+    opt_config_path.set_description("Path to JSON configuration file");
+    opt_config_path.set_arg_description("<path to config>");
+
+    Glib::OptionContext option_context = Glib::OptionContext(
+        "- launcher for mpv to watch youtube videos"
+    );
+    Glib::OptionGroup main_group = Glib::OptionGroup(
+        "Main option group",
+        "main option description",
+        "no help."
+    );
+    main_group.add_entry(opt_config_path, config_path);
+    option_context.set_main_group(main_group);
+
+    try {
+        option_context.parse(argc, argv);
+    } catch (Glib::OptionError* e) {
+        std::cout << e->what();
+        return 1;
     }
 
-    // --Инициализация--
-    std::locale::global(std::locale(""));
+    // --Получение данных из файла настроек--
+    // TODO
+    std::string db_path = ":memory:";
+    std::string api_key = "key";
+    
+    // --Общая инициализация--
     curl_global_init(CURL_GLOBAL_ALL);
-    db::databaseInit(argv[1]);
+    db::databaseInit(db_path);
     db::createTables();
-    ytapi::youtubeInit(argv[2]);
+    ytapi::youtubeInit(api_key);
 
-    db::video found;
-    std::vector<std::string> video_ids = ytapi::getVideoIDsByQuery(argv[3]);
-    for (auto id : video_ids) {
-        found = getVideoByYTID(id);
+    // ==Запуск приложения==
+    auto app = Gtk::Application::create("com.github.aquadim.YTMPV");
+    return app->make_window_and_run<components::MainWindow>(argc, argv);
 
-        std::cout << found.title << ":" << found.author_obj.name << std::endl;
-    }
+    //~ db::video found;
+    //~ std::vector<std::string> video_ids = ytapi::getVideoIDsByQuery(argv[3]);
+    //~ for (auto id : video_ids) {
+        //~ found = getVideoByYTID(id);
+
+        //~ std::cout << found.title << ":" << found.author_obj.name << std::endl;
+    //~ }
 
     //~ std::ifstream ifs("example.json");
     //~ std::string content(

+ 6 - 0
src/YTMPV/ytmpv.hpp

@@ -1,7 +1,13 @@
+#pragma once
 #include <YoutubeApi/youtubeapi.hpp>
 #include <Database/database.hpp>
 #include <fstream>
 #include <iostream>
+#include <glibmm/optioncontext.h>
+#include <glibmm/optiongroup.h>
+#include <glibmm/optionentry.h>
+#include <gtkmm/application.h>
+#include "MainWindow.hpp"
 
 // Возвращает объект видео по его Youtube id
 db::video getVideoByYTID(std::string yt_id);