From 7f6fbfd48855848d449ac7fc9306a7862a555af5 Mon Sep 17 00:00:00 2001 From: nvpopov Date: Wed, 14 Jul 2021 11:18:18 +0800 Subject: [PATCH] correct handling of qt scale factor was introduced --- data/style.qss | 1 - src/qppcad/core/app_state.cpp | 15 -- src/qppcad/core/app_state.hpp | 1 + src/qppcad/core/main.cpp | 19 +- .../python/python_embedded_workspace.cpp | 6 + src/qppcad/ui/main_window.cpp | 58 +----- src/qppcad/ui/main_window.hpp | 6 +- src/qppcad/ui/size_guide.cpp | 4 - src/qppcad/ui/size_guide.hpp | 4 +- src/qppcad/ui/ws_viewer_widget.cpp | 46 +---- .../ws_item/ws_item_behaviour_manager.cpp | 181 +++++------------- 11 files changed, 87 insertions(+), 254 deletions(-) diff --git a/data/style.qss b/data/style.qss index dc489068..9c1e7703 100644 --- a/data/style.qss +++ b/data/style.qss @@ -1,6 +1,5 @@ * { font-family : "Nunito"; - font-size: 100%; font-weight: normal; color: rgb(209, 209, 209); background: rgb(79, 79, 79); diff --git a/src/qppcad/core/app_state.cpp b/src/qppcad/core/app_state.cpp index 71022f5a..4e2a406d 100644 --- a/src/qppcad/core/app_state.cpp +++ b/src/qppcad/core/app_state.cpp @@ -20,10 +20,8 @@ app_state_t *app_state_t::get_inst() { } app_state_t::app_state_t() { - astate_evd = new app_state_event_disp_t; m_utility_thread_count = std::thread::hardware_concurrency(); - } void app_state_t::init_glapi(){ @@ -32,7 +30,6 @@ void app_state_t::init_glapi(){ } void app_state_t::init_shaders() { - sp_default = shader_generators::gen_sp_default(); sp_default_suprematic = shader_generators::gen_sp_default_suprematic(); sp_unit_line = shader_generators::gen_sp_unit_line(); @@ -45,7 +42,6 @@ void app_state_t::init_shaders() { sp_buf_bs = shader_generators::gen_sp_buf_bs_sphere(); sp_2c_cylinder = shader_generators::gen_sp_2c_cylinder(); sp_2c_cylinder_suprematic = shader_generators::gen_sp_2c_cylinder_suprematic(); - } void app_state_t::init_meshes() { @@ -237,11 +233,9 @@ void app_state_t::load_settings() { settings.beginGroup("hotkey_manager"); hotkey_mgr->load_from_settings(settings); settings.endGroup(); - } void app_state_t::save_settings() { - QSettings settings; settings.setValue("test", 68); @@ -250,14 +244,12 @@ void app_state_t::save_settings() { settings.setValue("console_font_size", m_console_font_size); if (m_fixtures_dir_is_set) { - // join fixtures dir std::string fd_joined; std::for_each(m_fixtures_dirs.begin(), m_fixtures_dirs.end(), [&fd_joined](const std::string &piece){ fd_joined += piece + ";"; }); settings.setValue("fixtures_dir", QString::fromStdString(fd_joined)); - } // end of general settings @@ -275,7 +267,6 @@ void app_state_t::save_settings() { for (auto &rec : table->arecs) if (rec.m_redefined) { - settings.setArrayIndex(i); settings.setValue("number", int(rec.m_number)); settings.setValue("c_r", double(rec.m_color_jmol[0])); @@ -284,7 +275,6 @@ void app_state_t::save_settings() { settings.setValue("r", double(rec.m_radius)); settings.setValue("covrad", double(rec.m_covrad_slater)); i+=1; - } settings.endArray(); @@ -307,7 +297,6 @@ void app_state_t::save_settings() { settings.beginWriteArray("recent_files"); for (int q = 0; q < static_cast(m_recent_files.size()); q++) { - settings.setArrayIndex(q); settings.setValue("filename", QString::fromStdString(m_recent_files[q].m_file_name)); std::string ff_name = @@ -341,12 +330,10 @@ void app_state_t::save_settings() { settings.beginGroup("cache_vector"); for (auto &rec : m_env_vec3) { - QStringList vecl; vecl.reserve(3); for (size_t q = 0; q < 3; q++) vecl.push_back(QString("%1").arg(rec.second[q])); settings.setValue(QString::fromStdString(rec.first), vecl); - } settings.endGroup(); @@ -354,7 +341,6 @@ void app_state_t::save_settings() { settings.beginGroup("hotkey_manager"); hotkey_mgr->save_to_settings(settings); settings.endGroup(); - } void app_state_t::pylog(std::string logstr) { @@ -365,7 +351,6 @@ void app_state_t::pylog(std::string logstr) { void app_state_t::add_recent_file(const std::string &file_name, const bool is_native, const size_t ff_id) { - QFileInfo file_info(QString::fromStdString(file_name)); m_last_dir = file_info.absoluteDir().path(); tlog("M_LAST_DIR= {}", m_last_dir.toStdString()); diff --git a/src/qppcad/core/app_state.hpp b/src/qppcad/core/app_state.hpp index d7bcf66d..052ce589 100644 --- a/src/qppcad/core/app_state.hpp +++ b/src/qppcad/core/app_state.hpp @@ -199,6 +199,7 @@ class app_state_t { bool m_disable_app{false}; bool m_viewport_dirty{true}; bool m_immersive_mode{false}; + std::optional m_qt_scale_factor{}; size_t m_gv_overview_max_atoms{15000}; size_t m_gv_overview_max_sel_types{6}; diff --git a/src/qppcad/core/main.cpp b/src/qppcad/core/main.cpp index dafc1cb3..77a6e9cc 100644 --- a/src/qppcad/core/main.cpp +++ b/src/qppcad/core/main.cpp @@ -12,6 +12,7 @@ #include #include #include +#include using namespace qpp; using namespace qpp::cad; @@ -43,6 +44,18 @@ int main (int argc, char **argv) { QCoreApplication::setOrganizationDomain("100ways.dev"); QCoreApplication::setApplicationName("qppcad"); QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling); + QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps); + + app_state_t::init_inst(); + app_state_t *astate = app_state_t::get_inst(); + astate->init_managers(); + astate->ws_mgr->init_ws_item_bhv_mgr(); + astate->load_settings(); + //bool scale_factor_redefined{false}; + if (qEnvironmentVariableIsSet("QT_SCALE_FACTOR")) { + astate->m_qt_scale_factor = qEnvironmentVariable("QT_SCALE_FACTOR").toFloat(); + astate->size_guide.m_scale_factor = astate->m_qt_scale_factor.value_or(1.0f); + } QApplication app(argc, argv); @@ -73,18 +86,12 @@ int main (int argc, char **argv) { std::signal(SIGTERM, on_app_exit); } - app_state_t::init_inst(); - app_state_t *astate = app_state_t::get_inst(); astate->tlog("@GIT_REVISION={}, @BUILD_DATE={}", build_info_t::get_git_version(), build_info_t::get_git_version()); std::ifstream test_in_dev_env("../data/refs/laf3_p3.vasp"); bool under_dev_env = test_in_dev_env.good(); - astate->init_managers(); - astate->ws_mgr->init_ws_item_bhv_mgr(); - astate->load_settings(); - astate->init_fixtures(); if (under_dev_env) { diff --git a/src/qppcad/python/python_embedded_workspace.cpp b/src/qppcad/python/python_embedded_workspace.cpp index 2b6ace63..e6c5f853 100644 --- a/src/qppcad/python/python_embedded_workspace.cpp +++ b/src/qppcad/python/python_embedded_workspace.cpp @@ -21,6 +21,7 @@ #include #include +#include using namespace qpp; using namespace qpp::cad; @@ -125,7 +126,12 @@ void cws_changed() { astate->astate_evd->cur_ws_changed(); } +void ev_update() { + qApp->processEvents(); +} + PYBIND11_EMBEDDED_MODULE(cad, m) { + m.def("ev_update", &ev_update); py::class_ py_hist_doc_base_t(m, "hist_doc_base_t"); py_hist_doc_base_t.def("get_cur_epoch", &hs_doc_base_t::get_cur_epoch); diff --git a/src/qppcad/ui/main_window.cpp b/src/qppcad/ui/main_window.cpp index 3a271bfa..eecf70e0 100644 --- a/src/qppcad/ui/main_window.cpp +++ b/src/qppcad/ui/main_window.cpp @@ -449,11 +449,9 @@ void main_window_t::init_menus() { //m_menu_wdgt->setDefaultWidget(m_menu_wdgt_hldr); menuBar()->setCornerWidget(m_menu_wdgt_hldr); - } void main_window_t::init_widgets() { - app_state_t* astate = app_state_t::get_inst(); m_tp_wdgt = new QFrame(nullptr); @@ -719,7 +717,6 @@ void main_window_t::init_widgets() { m_inline_btm_tool = new qinline_tool_window_t(qinline_tool_type_e::tool_horizontal, nullptr); m_inline_btm_tool->setVisible(false); - } void main_window_t::init_layouts() { @@ -1084,11 +1081,9 @@ void main_window_t::change_cur_ws_bg() { app_state_t* astate = app_state_t::get_inst(); auto [ok, cur_ws] = astate->ws_mgr->get_sel_tuple_ws(error_ctx_mbox); if (ok) { - QColor stored_color = QColor::fromRgbF( - cur_ws->m_bg_color[0], - cur_ws->m_bg_color[1], - cur_ws->m_bg_color[2] - ); + QColor stored_color = QColor::fromRgbF(cur_ws->m_bg_color[0], + cur_ws->m_bg_color[1], + cur_ws->m_bg_color[2]); const QColor clr = QColorDialog::getColor(stored_color, this, "Select workspace`s background color"); if (clr.isValid()) { @@ -1188,7 +1183,6 @@ void main_window_t::cur_ws_edit_type_changed() { } void main_window_t::cur_ws_sel_atoms_list_sel_changed() { - app_state_t* astate = app_state_t::get_inst(); auto [cur_ws, cur_item, as_al, ok] = astate->ws_mgr->get_sel_tpl_itmc(); @@ -1266,7 +1260,6 @@ void main_window_t::cur_ws_sel_atoms_list_sel_changed() { m_tp_msr_angle->blockSignals(false); } /* end of angle between 3 atoms */ - } if (need_to_hide_add_primitives) { @@ -1292,11 +1285,9 @@ void main_window_t::cur_ws_sel_atoms_list_sel_changed() { if (need_to_hide_make_psg) { m_tp_add_point_sym_group->hide(); } - } void main_window_t::tp_dist_button_clicked(bool checked) { - app_state_t* astate = app_state_t::get_inst(); auto [cur_ws, cur_item, as_al, ok] = astate->ws_mgr->get_sel_tpl_itmc(); if (!ok) @@ -1316,11 +1307,9 @@ void main_window_t::tp_dist_button_clicked(bool checked) { } astate->make_viewport_dirty(); - } void main_window_t::tp_angle_button_clicked(bool checked) { - app_state_t* astate = app_state_t::get_inst(); auto [cur_ws, cur_item, as_al, ok] = astate->ws_mgr->get_sel_tpl_itmc(); if (!ok) @@ -1345,7 +1334,6 @@ void main_window_t::tp_angle_button_clicked(bool checked) { } astate->make_viewport_dirty(); - } void main_window_t::ws_edit_mode_selector_button_clicked(int id) { @@ -1387,7 +1375,6 @@ void main_window_t::tp_force_sel_lbl_vis_button_clicked(bool checked) { } void main_window_t::tp_toggle_atom_override_button_clicked(bool checked) { - app_state_t* astate = app_state_t::get_inst(); auto [cur_ws, cur_item, as_al, ok] = astate->ws_mgr->get_sel_tpl_itmc(); @@ -1405,7 +1392,6 @@ void main_window_t::tp_toggle_atom_override_button_clicked(bool checked) { astate->astate_evd->cur_ws_selected_item_need_to_update_obj_insp(); astate->make_viewport_dirty(); - } void main_window_t::tp_camera_tool_button_triggered(QAction *action) { @@ -1512,11 +1498,10 @@ void main_window_t::stop_update_cycle() { } void main_window_t::act_sel_all_cnt() { - app_state_t* astate = app_state_t::get_inst(); auto [cur_ws, cur_it, al] = astate->ws_mgr->get_sel_tpl_itm(); - if (al) al->sel_atoms(true); - + if (al) + al->sel_atoms(true); } void main_window_t::act_unsel_all_cnt() { @@ -1564,7 +1549,6 @@ void main_window_t::act_redo() { } void main_window_t::rebuild_recent_files_menu() { - if (!m_file_menu_recent_files) return; @@ -1588,11 +1572,9 @@ void main_window_t::rebuild_recent_files_menu() { astate->m_recent_files[i].m_file_name); m_file_menu_recent_entries[i]->setText(QString::fromStdString(rec_menu_entry)); } - } void main_window_t::recent_files_clicked() { - int idx = -1; app_state_t* astate = app_state_t::get_inst(); @@ -1615,13 +1597,10 @@ void main_window_t::recent_files_clicked() { astate->ws_mgr->load_from_file_autodeduce(rec_idx.m_file_name, "", true, true); } } - } void main_window_t::inline_tool_left_ctrl_visibility(bool visible) { - m_inline_left_tool->setVisible(visible); - } void main_window_t::inline_tool_bottom_ctrl_visibility(bool visible) { @@ -1633,7 +1612,6 @@ void main_window_t::modern_menu_clicked() { } void main_window_t::build_bhv_menus_and_actions() { - app_state_t* astate = app_state_t::get_inst(); ws_item_behaviour_manager_t *bhv_mgr = astate->ws_mgr->m_bhv_mgr.get(); @@ -1708,11 +1686,9 @@ void main_window_t::build_bhv_menus_and_actions() { } //TODO: make lookup for bhv - } void main_window_t::build_bhv_tools_menus() { - app_state_t* astate = app_state_t::get_inst(); using pss = std::tuple; @@ -1764,11 +1740,9 @@ void main_window_t::build_bhv_tools_menus() { m_tools_menu->addAction(new_act); m_tools_menu_acts.push_back(new_act); } - } void main_window_t::build_bhv_tool_panel() { - app_state_t *astate = app_state_t::get_inst(); ws_item_behaviour_manager_t *bhv_mgr = astate->ws_mgr->m_bhv_mgr.get(); @@ -1778,11 +1752,9 @@ void main_window_t::build_bhv_tool_panel() { new_tb->init_element(m_tp_wdgt); m_toolbar_elements.push_back(new_tb); } - } void main_window_t::act_bhv_tools_menus_clicked() { - app_state_t* astate = app_state_t::get_inst(); auto [cur_ws, cur_it, ok] = astate->ws_mgr->get_sel_tpl_itm_nc(error_ctx_ignore); @@ -1821,11 +1793,9 @@ void main_window_t::act_bhv_tools_menus_clicked() { } } - } void main_window_t::process_bhv_tool(size_t tool_id, ws_item_t *sel_item) { - app_state_t *astate = app_state_t::get_inst(); auto &bhv_mgr = *astate->ws_mgr->m_bhv_mgr.get(); @@ -1897,11 +1867,9 @@ void main_window_t::process_bhv_tool(size_t tool_id, ws_item_t *sel_item) { inline_tool_left_ctrl_visibility(false); inline_tool_bottom_ctrl_visibility(true); } - } void main_window_t::control_bhv_tools_menus_activity() { - app_state_t* astate = app_state_t::get_inst(); ws_item_behaviour_manager_t *bhv_mgr = astate->ws_mgr->m_bhv_mgr.get(); @@ -1923,11 +1891,9 @@ void main_window_t::control_bhv_tools_menus_activity() { } } } - } void main_window_t::act_bhv_import_to_cur_ws() { - app_state_t* astate = app_state_t::get_inst(); ws_item_behaviour_manager_t *bhv_mgr = astate->ws_mgr->m_bhv_mgr.get(); @@ -1949,11 +1915,9 @@ void main_window_t::act_bhv_import_to_cur_ws() { if (!file_name.empty()) astate->ws_mgr->import_from_file(file_name, b_id, false, false); } - } void main_window_t::act_bhv_import_as_new_ws() { - app_state_t* astate = app_state_t::get_inst(); ws_item_behaviour_manager_t *bhv_mgr = astate->ws_mgr->m_bhv_mgr.get(); @@ -1975,11 +1939,9 @@ void main_window_t::act_bhv_import_as_new_ws() { if (!file_name.empty()) astate->ws_mgr->import_from_file(file_name, b_id, true, true); } - } void main_window_t::act_bhv_export_sel() { - app_state_t* astate = app_state_t::get_inst(); ws_item_behaviour_manager_t *bhv_mgr = astate->ws_mgr->m_bhv_mgr.get(); @@ -2006,35 +1968,30 @@ void main_window_t::act_bhv_export_sel() { astate->ws_mgr->save_ws_item_to_file(file_name, cur_it, b_id); } } - } void main_window_t::control_bhv_menus_activity() { - app_state_t* astate = app_state_t::get_inst(); if (!astate->ws_mgr) return; ws_item_behaviour_manager_t *bhv_mgr = astate->ws_mgr->m_bhv_mgr.get(); - auto [cur_ws, cur_it, ok] = astate->ws_mgr->get_sel_tpl_itm_nc(); - if (cur_ws) m_file_menu_import_to_cur_ws->setEnabled(true); else m_file_menu_import_to_cur_ws->setEnabled(false); if (!ok) { - // m_file_menu_export_sel_as->setEnabled(false); } else { if (cur_it) { m_file_menu_export_sel_as->setEnabled(true); for (auto &exp_act : m_file_menu_export_sel_as_acts) { size_t bhv_id = exp_act->m_joined_data[0]; - if (bhv_mgr->m_ws_item_io[bhv_id]->can_save() && - bhv_mgr->m_ws_item_io[bhv_id]->m_accepted_type == cur_it->get_type()) { + if (bhv_mgr->m_ws_item_io[bhv_id]->can_save() + && bhv_mgr->m_ws_item_io[bhv_id]->m_accepted_type == cur_it->get_type()) { //need_to_enable_export_menu = true; exp_act->setEnabled(true); //save item dialog @@ -2044,7 +2001,6 @@ void main_window_t::control_bhv_menus_activity() { } } } // not ok - } void main_window_t::make_screenshot() { diff --git a/src/qppcad/ui/main_window.hpp b/src/qppcad/ui/main_window.hpp index 4f73a9b6..207fec0f 100644 --- a/src/qppcad/ui/main_window.hpp +++ b/src/qppcad/ui/main_window.hpp @@ -35,9 +35,7 @@ #include #include -namespace qpp { - -namespace cad { +namespace qpp::cad { class toolbar_element_t; @@ -303,6 +301,4 @@ public slots: } // namespace qpp::cad -} // namespace qpp - #endif diff --git a/src/qppcad/ui/size_guide.cpp b/src/qppcad/ui/size_guide.cpp index fc1d3547..5e20d935 100644 --- a/src/qppcad/ui/size_guide.cpp +++ b/src/qppcad/ui/size_guide.cpp @@ -3,10 +3,6 @@ using namespace qpp; using namespace qpp::cad; -size_guide_t::size_guide_t() { - -} - int size_guide_t::main_window_h() { return 300 ; } diff --git a/src/qppcad/ui/size_guide.hpp b/src/qppcad/ui/size_guide.hpp index f105021f..704178c0 100644 --- a/src/qppcad/ui/size_guide.hpp +++ b/src/qppcad/ui/size_guide.hpp @@ -16,7 +16,9 @@ class size_guide_t { public: - size_guide_t(); + float m_scale_factor{1.0f}; + + size_guide_t() = default; /** * @brief main_window_h diff --git a/src/qppcad/ui/ws_viewer_widget.cpp b/src/qppcad/ui/ws_viewer_widget.cpp index 9e16bfb9..f40cde3d 100644 --- a/src/qppcad/ui/ws_viewer_widget.cpp +++ b/src/qppcad/ui/ws_viewer_widget.cpp @@ -6,7 +6,6 @@ using namespace qpp; using namespace qpp::cad; ws_viewer_widget_t::ws_viewer_widget_t(QWidget *parent) : QOpenGLWidget (parent) { - m_update_timer = new QTimer; m_update_timer->connect(m_update_timer, @@ -30,11 +29,9 @@ ws_viewer_widget_t::ws_viewer_widget_t(QWidget *parent) : QOpenGLWidget (parent) m_update_timer_cpu = new QElapsedTimer; m_update_timer_gpu = new QElapsedTimer; - } void ws_viewer_widget_t::draw_text_logo(QPainter &painter) { - app_state_t* astate = app_state_t::get_inst(); QFont font(QFont(astate->m_font_name, 42, QFont::Medium)); @@ -50,20 +47,16 @@ void ws_viewer_widget_t::draw_text_logo(QPainter &painter) { painter.setPen(logo_color); painter.drawText(c_x, c_y, logo_string); - } void ws_viewer_widget_t::draw_scanline(QPainter &painter) { - app_state_t* astate = app_state_t::get_inst(); painter.resetTransform(); //painter.setPen(QPen(astate->m_app_palette.color(QPalette::Window), 3)); painter.drawLine(0, 0, painter.window().width(), 0); - } void ws_viewer_widget_t::update_cycle() { - app_state_t* astate = app_state_t::get_inst(); if (astate->is_viewport_dirty()) { @@ -89,7 +82,6 @@ void ws_viewer_widget_t::update_cycle() { astate->is_mouse_moving = false; astate->ws_mgr->utility_event_loop(); - } void ws_viewer_widget_t::initializeGL() { @@ -101,14 +93,13 @@ void ws_viewer_widget_t::initializeGL() { void ws_viewer_widget_t::resizeGL(int w, int h) { app_state_t* astate = app_state_t::get_inst(); - astate->viewport_size[0] = w; - astate->viewport_size[1] = h; + astate->viewport_size[0] = w ; + astate->viewport_size[1] = h ; if (astate->camera) astate->camera->update_camera(); } void ws_viewer_widget_t::paintGL() { - #ifdef WITH_VTUNE_INSTRUMENTATION __itt_task_begin(instrumentation::d_qppcad, __itt_null, @@ -124,10 +115,12 @@ void ws_viewer_widget_t::paintGL() { if (astate->m_disable_app) return; + auto scale_factor = astate->m_qt_scale_factor.value_or(1.0); + //astate->tlog("@@@ SCALE FACTOR IS {}", scale_factor); glapi->glViewport(static_cast(0), static_cast(0), - static_cast(astate->viewport_size(0)), - static_cast(astate->viewport_size(1))); + static_cast(astate->viewport_size(0) * scale_factor), + static_cast(astate->viewport_size(1) * scale_factor)); QPainter painter(this); painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing); @@ -141,9 +134,7 @@ void ws_viewer_widget_t::paintGL() { astate->dp->cull_func(draw_pipeline_cull_func::cull_disable); if (astate->m_show_debug_frame_stats && !astate->ws_mgr->m_ws.empty()) { - painter.setFont(QFont(astate->m_font_name, 13)); - //build contrast color for font QColor debug_hud_color = Qt::black; if (astate->ws_mgr->has_wss()) { @@ -152,23 +143,19 @@ void ws_viewer_widget_t::paintGL() { debug_hud_color.setGreenF(1 - cur_ws->m_bg_color[1]); debug_hud_color.setBlueF(1 - cur_ws->m_bg_color[2]); } - painter.setPen(debug_hud_color); painter.drawText(width()-240, height()-30, 280, 30, Qt::AlignLeft, QString::fromStdString(fmt::format("Frame time GPU: {:6.6f} ms.", (astate->m_last_frame_time_gpu)/ - 1000000.0)) - ); + 1000000.0))); painter.drawText(width()-240, height()-60, 280, 30, Qt::AlignLeft, QString::fromStdString(fmt::format("Frame time CPU: {:6.6f} ms.", (astate->m_last_frame_time_cpu)/ - 1000000.0)) - ); + 1000000.0))); } astate->ws_mgr->render_cur_ws_overlay(painter); - if (!astate->ws_mgr->has_wss()) draw_text_logo(painter); @@ -178,13 +165,10 @@ void ws_viewer_widget_t::paintGL() { #ifdef WITH_VTUNE_INSTRUMENTATION __itt_task_end(instrumentation::d_qppcad); #endif - } void ws_viewer_widget_t::mousePressEvent(QMouseEvent *event) { - app_state_t* astate = app_state_t::get_inst(); - if (event) { if (event->button() == Qt::LeftButton) { astate->mouse_lb_pressed = true; @@ -201,13 +185,10 @@ void ws_viewer_widget_t::mousePressEvent(QMouseEvent *event) { astate->mouse_md_pressed = true; } } - } void ws_viewer_widget_t::mouseReleaseEvent(QMouseEvent *event) { - app_state_t* astate = app_state_t::get_inst(); - if (event) { bool need_to_cancel_cam_transform = false; if (event->button() == Qt::LeftButton) { @@ -228,15 +209,11 @@ void ws_viewer_widget_t::mouseReleaseEvent(QMouseEvent *event) { astate->camera->m_move_camera = false; } } - } void ws_viewer_widget_t::mouseMoveEvent(QMouseEvent *event) { - app_state_t* astate = app_state_t::get_inst(); - if (event) { - astate->mouse_x_old = astate->mouse_x; astate->mouse_y_old = astate->mouse_y; astate->mouse_x_dc_old = astate->mouse_x_dc; @@ -279,31 +256,24 @@ void ws_viewer_widget_t::mouseMoveEvent(QMouseEvent *event) { astate->camera->m_rotate_camera && (kb_mod & Qt::ControlModifier); } } - } void ws_viewer_widget_t::mouseDoubleClickEvent(QMouseEvent *event) { - app_state_t* astate = app_state_t::get_inst(); - if (event && event->button() == Qt::LeftButton) { astate->ws_mgr->mouse_double_click(); event->accept(); } - } void ws_viewer_widget_t::wheelEvent(QWheelEvent *event) { - app_state_t* astate = app_state_t::get_inst(); - if (astate->camera) { astate->camera->update_camera_zoom( event->delta() / 180.0f * astate->m_middle_mb_translate_mode); astate->camera->update_camera(); astate->make_viewport_dirty(); } - } void ws_viewer_widget_t::show_context_menu(const QPoint &pos) { diff --git a/src/qppcad/ws_item/ws_item_behaviour_manager.cpp b/src/qppcad/ws_item/ws_item_behaviour_manager.cpp index df095c8d..b7b3de7a 100644 --- a/src/qppcad/ws_item/ws_item_behaviour_manager.cpp +++ b/src/qppcad/ws_item/ws_item_behaviour_manager.cpp @@ -15,58 +15,43 @@ ws_item_behaviour_manager_t::ws_item_behaviour_manager_t() { } void ws_item_behaviour_manager_t::load_fixtures_from_path( - const std::vector &file_paths) { - + const std::vector &file_paths) { app_state_t* astate = app_state_t::get_inst(); for (const auto &rec : file_paths) { - QDirIterator fxts_dir(QString::fromStdString(rec), QDirIterator::NoIteratorFlags); - while (fxts_dir.hasNext()) { - auto fxt_dir = fxts_dir.next(); - if (fxt_dir.endsWith("/..") || fxt_dir.endsWith("/.")) continue; - + if (fxt_dir.endsWith("/..") || fxt_dir.endsWith("/.")) + continue; auto fxt_manifest_fn = QString("%1/%2").arg(fxt_dir).arg("manifest.json"); QFileInfo check_file(fxt_manifest_fn); - astate->tlog("Loading fixture from path {}, manifest path = {}", fxt_dir.toStdString(), fxt_manifest_fn.toStdString()); - if (check_file.exists() && check_file.isFile()) { - fixture_info_t new_fxt; new_fxt.load_from_file(fxt_manifest_fn.toStdString(), fxt_dir.toStdString()); - if (new_fxt.m_initialized) { - size_t fixture_hash = astate->hash_reg->calc_hash(new_fxt.m_fxt_name); astate->tlog("Fixture \"{}\" loaded!", new_fxt.m_fxt_name); m_fixtures_info.insert({fixture_hash, std::move(new_fxt)}); - } else { astate->tlog("Cannot load fixture!"); } - } - } // end while } - } std::shared_ptr ws_item_behaviour_manager_t::load_ws_itm_from_file( - const std::string &file_name, - size_t io_bhv_idx, - workspace_t *ws, - hs_doc_rec_type_e rec_type) { - + const std::string &file_name, + size_t io_bhv_idx, + workspace_t *ws, + hs_doc_rec_type_e rec_type) { app_state_t* astate = app_state_t::get_inst(); std::setlocale(LC_ALL, "C"); - astate->tlog("Loading ws_item from file {}", file_name); auto new_ws_item = fbr_ws_item_by_type(m_ws_item_io[io_bhv_idx]->m_accepted_type); @@ -74,27 +59,21 @@ std::shared_ptr ws_item_behaviour_manager_t::load_ws_itm_from_file( ws->add_item_to_ws(new_ws_item, false); if (new_ws_item) { - std::ifstream input(file_name); new_ws_item->m_name.set_value(extract_base_name(file_name)); new_ws_item->begin_recording(rec_type); m_ws_item_io[io_bhv_idx]->load_from_stream(input, new_ws_item.get(), ws); new_ws_item->end_recording(); return new_ws_item; - } else { - return nullptr; - } - } std::shared_ptr ws_item_behaviour_manager_t::load_ws_itm_from_file( const std::string &file_name, workspace_t *ws, hs_doc_rec_type_e rec_type) { - QFileInfo check_file(QString::fromStdString(file_name)); if (!check_file.exists() || !check_file.isFile()) return nullptr; @@ -108,25 +87,22 @@ std::shared_ptr ws_item_behaviour_manager_t::load_ws_itm_from_file( } return nullptr; - } bool ws_item_behaviour_manager_t::save_ws_itm_to_file(std::string &file_name, std::shared_ptr ws_item, size_t bhv_id, std::string &message) { - app_state_t* astate = app_state_t::get_inst(); - if (!ws_item) return false; + if (!ws_item) + return false; if (bhv_id < m_ws_item_io.size() && m_ws_item_io[bhv_id]->can_save() && m_ws_item_io[bhv_id]->m_accepted_type == ws_item->get_type()) { - astate->tlog("Saving ws_item[{}] to file {} from workspace {}", ws_item->m_name.get_value(), file_name, ws_item->m_parent_ws->m_ws_name); - bool check = m_ws_item_io[bhv_id]->check_before_save(ws_item.get(), message); if (check) { std::ofstream output(file_name); @@ -136,67 +112,56 @@ bool ws_item_behaviour_manager_t::save_ws_itm_to_file(std::string &file_name, astate->tlog("Checking failed for ws_item={}, file={}, workspace={}", ws_item->m_name.get_value(), file_name, ws_item->m_parent_ws->m_ws_name); return false; - } - } return false; - } std::string ws_item_behaviour_manager_t::get_ff_full_name(size_t file_format_hash) { - auto it = m_file_formats.find(file_format_hash); - if (it != m_file_formats.end()) return it->second.m_shortname; - else return "NOT_FOUND"; - + if (it != m_file_formats.end()) + return it->second.m_shortname; + else + return "NOT_FOUND"; } std::string ws_item_behaviour_manager_t::get_ff_short_name(size_t file_format_hash) { - auto it = m_file_formats.find(file_format_hash); - if (it != m_file_formats.end()) return it->second.m_shortname; - else return "NOT_FOUND"; - + if (it != m_file_formats.end()) + return it->second.m_shortname; + else return + "NOT_FOUND"; } size_t ws_item_behaviour_manager_t::reg_ff(std::string full_name, std::string short_name, size_t file_format_group_hash, std::vector finger_prints) { - app_state_t *astate = app_state_t::get_inst(); size_t ff_hash = astate->hash_reg->calc_hash_ub(full_name); auto it = m_file_formats.find(ff_hash); - if (it == m_file_formats.end()) { - ws_item_io_file_format_t new_file_format; new_file_format.m_full_name = full_name; new_file_format.m_shortname = short_name; new_file_format.m_finger_prints = finger_prints; new_file_format.m_group_hash = file_format_group_hash; - auto it_ffg = m_file_format_groups.find(file_format_group_hash); if (it_ffg != m_file_format_groups.end()) m_file_format_groups[file_format_group_hash].m_ffs_lookup.insert(ff_hash); - m_file_formats.emplace(ff_hash, std::move(new_file_format)); - } astate->tlog("Registering file format {}[{}] - hash {}, ghash {}", full_name, short_name, ff_hash, file_format_group_hash); return ff_hash; - } size_t ws_item_behaviour_manager_t::reg_ffg(std::string full_name, std::string short_name) { - app_state_t *astate = app_state_t::get_inst(); size_t file_format_group_hash = astate->hash_reg->calc_hash_ub(full_name); @@ -213,31 +178,25 @@ size_t ws_item_behaviour_manager_t::reg_ffg(std::string full_name, std::string s full_name, short_name, file_format_group_hash); return file_format_group_hash; - } size_t ws_item_behaviour_manager_t::reg_tool_grp(std::string full_name) { - app_state_t *astate = app_state_t::get_inst(); - ws_item_tool_group_t tool_grp; tool_grp.m_full_name = full_name; size_t g_hash = astate->hash_reg->calc_hash_ub(full_name); m_tools_groups.emplace(g_hash, std::move(tool_grp)); - return g_hash; - } size_t ws_item_behaviour_manager_t::reg_tool( - std::string full_name, - size_t grgp_hash, - size_t type_hash, - bool itm_req, - ws_item_tool_type_e tool_type, - std::function()> f_fabric, - std::function f_can_apply) { - + std::string full_name, + size_t grgp_hash, + size_t type_hash, + bool itm_req, + ws_item_tool_type_e tool_type, + std::function()> f_fabric, + std::function f_can_apply) { app_state_t *astate = app_state_t::get_inst(); ws_item_tool_info_t tinfo; @@ -253,11 +212,9 @@ size_t ws_item_behaviour_manager_t::reg_tool( m_tools_info.emplace(tinfo_hash, std::move(tinfo)); return tinfo_hash; - } size_t ws_item_behaviour_manager_t::reg_sflow_grp(std::string group_name) { - app_state_t *astate = app_state_t::get_inst(); size_t g_hash = astate->hash_reg->calc_hash_ub(group_name); @@ -269,14 +226,12 @@ size_t ws_item_behaviour_manager_t::reg_sflow_grp(std::string group_name) { m_sflow_node_group_info.emplace(g_hash, std::move(sflow_grp)); return g_hash; - } size_t ws_item_behaviour_manager_t::reg_reg_sf_fbr( - std::string full_name, - size_t g_hash, - std::function ()> fabric) { - + std::string full_name, + size_t g_hash, + std::function ()> fabric) { app_state_t *astate = app_state_t::get_inst(); sflow_node_info_t sinfo; @@ -288,24 +243,23 @@ size_t ws_item_behaviour_manager_t::reg_reg_sf_fbr( m_sflow_node_info.emplace(sinfo_hash, std::move(sinfo)); return sinfo_hash; - } -void ws_item_behaviour_manager_t::exec_tool(ws_item_t* item, - size_t tool_hash, - uint32_t error_ctx) { - +void ws_item_behaviour_manager_t::exec_tool(ws_item_t* item, size_t tool_hash, uint32_t error_ctx) { auto tinfo = m_tools_info.find(tool_hash); - //is tool info exists? - if (tinfo == m_tools_info.end()) return; + //Does the tool exist? + if (tinfo == m_tools_info.end()) + return; //item must exsist if tool needs it - if (tinfo->second.m_item_required && !item) return; + if (tinfo->second.m_item_required && !item) + return; - //item type must be equal to accepted type + //item type must be equal to the accepted type if (tinfo->second.m_item_required && - item->get_type() != tinfo->second.m_accepted_type) return; + item->get_type() != tinfo->second.m_accepted_type) + return; auto tool_inst = tinfo->second.m_fabric(); @@ -315,70 +269,55 @@ void ws_item_behaviour_manager_t::exec_tool(ws_item_t* item, } ws_item_tool_type_e ws_item_behaviour_manager_t::get_tool_type(size_t tool_hash) { - auto tinfo = m_tools_info.find(tool_hash); - if (tinfo == m_tools_info.end()) return ws_item_tool_type_e::ws_item_tool_invalid; + if (tinfo == m_tools_info.end()) + return ws_item_tool_type_e::ws_item_tool_invalid; return tinfo->second.m_tool_type; - } void ws_item_behaviour_manager_t::exec_tool_by_name(std::string tool_name, ws_item_t *item, uint32_t error_ctx) { - auto it = std::find_if(m_tools_info.begin(), m_tools_info.end(), [&tool_name](const auto &e){return e.second.m_full_name == tool_name;}); - - if (it != m_tools_info.end()) exec_tool(item, it->first, error_ctx); - + if (it != m_tools_info.end()) + exec_tool(item, it->first, error_ctx); } std::optional ws_item_behaviour_manager_t::get_ff_by_finger_print( - const std::string &file_name) { - + const std::string &file_name) { for (auto &elem : m_file_formats) for (auto &ffp : elem.second.m_finger_prints) if (file_name.find(ffp) != std::string::npos) { - app_state_t *astate = app_state_t::get_inst(); astate->tlog("Compare ff {} {} - fname {}", elem.first, elem.second.m_full_name, file_name); return std::optional(elem.first); - } return std::nullopt; - } std::optional ws_item_behaviour_manager_t::get_ff_by_short_name( - const std::string &ffmt_short_name) { - + const std::string &ffmt_short_name) { auto find_iter = std::find_if(std::begin(m_file_formats), std::end(m_file_formats), [&ffmt_short_name](const auto &rec){ return rec.second.m_shortname == ffmt_short_name; }); - - if (find_iter != m_file_formats.cend()) return std::optional(find_iter->first); - + if (find_iter != m_file_formats.cend()) + return std::optional(find_iter->first); return std::nullopt; - } std::optional ws_item_behaviour_manager_t::get_io_bhv_by_file_format(size_t file_format) { - for (size_t i = 0; i < m_ws_item_io.size(); i++) if (m_ws_item_io[i]->m_accepted_file_format == file_format) { - app_state_t *astate = app_state_t::get_inst(); astate->tlog("Compare ff {} - io_bhv {}",file_format, i); return std::optional(i); - } - return std::nullopt; - } std::optional ws_item_behaviour_manager_t::get_io_bhv_by_file_format_ex(size_t file_format, @@ -387,15 +326,12 @@ std::optional ws_item_behaviour_manager_t::get_io_bhv_by_file_format_ex( if (m_ws_item_io[i]->m_accepted_file_format == file_format && m_ws_item_io[i]->m_accepted_type == type_hash) return std::optional(i); - return std::nullopt; - } void ws_item_behaviour_manager_t::reg_io_bhv(std::shared_ptr io_bhv_inst, size_t accepted_file_format, size_t accepted_type) { - app_state_t *astate = app_state_t::get_inst(); io_bhv_inst->m_accepted_type = accepted_type; @@ -408,12 +344,12 @@ void ws_item_behaviour_manager_t::reg_io_bhv(std::shared_ptrcan_load()); m_ws_item_io.push_back(io_bhv_inst); - } void ws_item_behaviour_manager_t::unreg_ff(size_t file_format_hash) { auto it = m_file_formats.find(file_format_hash); - if (it != m_file_formats.end()) m_file_formats.erase(it); + if (it != m_file_formats.end()) + m_file_formats.erase(it); } void ws_item_behaviour_manager_t::reg_item_fbr(size_t hash, @@ -424,9 +360,7 @@ void ws_item_behaviour_manager_t::reg_item_fbr(size_t hash, void ws_item_behaviour_manager_t::reg_obj_insp_fbr( size_t hash, std::function ()> func) { - m_obj_insp_fabric[hash] = func; - } void ws_item_behaviour_manager_t::reg_ext_editor_fbr( @@ -434,15 +368,12 @@ void ws_item_behaviour_manager_t::reg_ext_editor_fbr( size_t editor_order, std::string editor_name, std::function()> func) { - ws_item_extended_editor_info_t new_ee_info; new_ee_info.m_type = hash; new_ee_info.m_order = editor_order; new_ee_info.m_full_name = editor_name; new_ee_info.m_fabric = func; - m_ext_editors_info[{hash, editor_order}] = std::move(new_ee_info); - } void ws_item_behaviour_manager_t::reg_toolbar_elem_fbr( @@ -453,22 +384,17 @@ void ws_item_behaviour_manager_t::reg_toolbar_elem_fbr( } bool ws_item_behaviour_manager_t::is_obj_insp_fbr_exists(size_t hash) { - auto it = m_obj_insp_fabric.find(hash); return it != m_obj_insp_fabric.end(); - } bool ws_item_behaviour_manager_t::is_obj_insp_exists(size_t hash) { - auto it = m_obj_insp_widgets.find(hash); return it != m_obj_insp_widgets.end(); - } std::shared_ptr ws_item_behaviour_manager_t::get_obj_insp_widget_sp( size_t hash) { - app_state_t *astate = app_state_t::get_inst(); astate->tlog("get_obj_insp_widget_sp with type_id = {}, query?", hash); @@ -482,12 +408,10 @@ std::shared_ptr ws_item_behaviour_manager_t::get_obj_ if (it == m_obj_insp_widgets.end()) { auto it_f = m_obj_insp_fabric.find(hash); if (it_f != m_obj_insp_fabric.end()) { - astate->tlog("get_obj_insp_widget_sp with type_id = {}, constructing", hash); auto cnstr = it_f->second(); m_obj_insp_widgets.emplace(hash, cnstr); return cnstr; - } else { return nullptr; @@ -497,25 +421,20 @@ std::shared_ptr ws_item_behaviour_manager_t::get_obj_ astate->tlog("get_obj_insp_widget_sp with type_id = {}, not found", hash); return nullptr; - } void ws_item_behaviour_manager_t::cache_obj_insp_widgets() { - auto obj_insp_w = get_obj_insp_widget_sp(geom_view_t::get_type_static()); - } std::shared_ptr ws_item_behaviour_manager_t::get_ext_editor_widget_sp( size_t hash, size_t ed_order) { - app_state_t *astate = app_state_t::get_inst(); astate->tlog("get_ext_editor_widget_sp with type_id = {}, query?", hash); auto it = m_ext_editors_info.find({hash, ed_order}); if (it != m_ext_editors_info.end()) { - if (it->second.m_inst) { astate->tlog("get_ext_editor_widget_sp with type_id = {}, exists", hash); } else { @@ -528,29 +447,25 @@ std::shared_ptr ws_item_behaviour_manager_t::get_ext_ astate->tlog("get_ext_editor_widget_sp with type_id = {}, not found", hash); return nullptr; - } std::shared_ptr ws_item_behaviour_manager_t::fbr_ws_item_by_type(size_t type_id) { - app_state_t *astate = app_state_t::get_inst(); astate->tlog("Fabric new ws_item with type_id = {}", type_id); auto it = m_fabric_ws_item.find(type_id); - if (it != m_fabric_ws_item.end()) return it->second(); + if (it != m_fabric_ws_item.end()) + return it->second(); astate->tlog("Cannot fabric new ws_item with type_id = {}!", type_id); return nullptr; - } std::shared_ptr ws_item_behaviour_manager_t::fbr_ws_item_by_name( const std::string &type_name) { - app_state_t *astate = app_state_t::get_inst(); return fbr_ws_item_by_type(astate->hash_reg->calc_hash_ub(type_name)); - } bool ws_item_io_behaviour_t::is_type_accepted(size_t input_type) {