From 75081f76c9bf2ed95feae75b4775ce39e87d38f4 Mon Sep 17 00:00:00 2001 From: itsmattkc Date: Sat, 22 Jun 2019 20:45:55 -0700 Subject: [PATCH] tool switching implemented --- app/CMakeLists.txt | 1 + app/core.cpp | 7 +++ app/core.h | 21 ++++++++ app/panel/tool/tool.cpp | 4 ++ app/tool/CMakeLists.txt | 21 ++++++++ app/tool/tool.h | 25 +++++++++ app/widget/toolbar/CMakeLists.txt | 2 + app/widget/toolbar/toolbar.cpp | 80 ++++++++++++++++++++++------ app/widget/toolbar/toolbar.h | 44 ++++++++++----- app/widget/toolbar/toolbarbutton.cpp | 14 +++++ app/widget/toolbar/toolbarbutton.h | 18 +++++++ 11 files changed, 208 insertions(+), 29 deletions(-) create mode 100644 app/tool/CMakeLists.txt create mode 100644 app/tool/tool.h create mode 100644 app/widget/toolbar/toolbarbutton.cpp create mode 100644 app/widget/toolbar/toolbarbutton.h diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index d7614006f..8abc90033 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -25,6 +25,7 @@ set(OLIVE_SOURCES add_subdirectory(panel) add_subdirectory(project) add_subdirectory(render) +add_subdirectory(tool) add_subdirectory(ui) add_subdirectory(widget) add_subdirectory(window) diff --git a/app/core.cpp b/app/core.cpp index 375d02f17..c40566bbe 100644 --- a/app/core.cpp +++ b/app/core.cpp @@ -104,6 +104,13 @@ olive::MainWindow *Core::main_window() return main_window_; } +void Core::SetTool(const olive::tool::Tool &tool) +{ + tool_ = tool; + + emit ToolChanged(tool_); +} + void Core::AddOpenProject(ProjectPtr p) { open_projects_.append(p); diff --git a/app/core.h b/app/core.h index 6bb09bbd0..8b2b61efc 100644 --- a/app/core.h +++ b/app/core.h @@ -25,6 +25,7 @@ #include "project/project.h" #include "window/mainwindow/mainwindow.h" +#include "tool/tool.h" /** * @brief The Core class @@ -66,6 +67,14 @@ public: */ olive::MainWindow* main_window(); +public slots: + /** + * @brief Set the current application-wide tool + * + * @param tool + */ + void SetTool(const olive::tool::Tool& tool); + signals: /** * @brief Signal emitted when a project is opened @@ -76,6 +85,13 @@ signals: */ void ProjectOpened(Project* p); + /** + * @brief Signal emitted when the tool is changed from somewhere + * + * @param tool + */ + void ToolChanged(const olive::tool::Tool& tool); + private: /** * @brief Creates an empty project and adds it to the "open projects" @@ -99,6 +115,11 @@ private: * @brief List of currently open projects */ QList open_projects_; + + /** + * @brief Currently active tool + */ + olive::tool::Tool tool_; }; namespace olive { diff --git a/app/panel/tool/tool.cpp b/app/panel/tool/tool.cpp index 24abfa7c5..77b742834 100644 --- a/app/panel/tool/tool.cpp +++ b/app/panel/tool/tool.cpp @@ -1,5 +1,6 @@ #include "tool.h" +#include "core.h" #include "widget/toolbar/toolbar.h" ToolPanel::ToolPanel(QWidget *parent) : @@ -9,6 +10,9 @@ ToolPanel::ToolPanel(QWidget *parent) : setWidget(t); + connect(t, SIGNAL(ToolChanged(const olive::tool::Tool&)), &olive::core, SLOT(SetTool(const olive::tool::Tool&))); + connect(&olive::core, SIGNAL(ToolChanged(const olive::tool::Tool&)), t, SLOT(SetTool(const olive::tool::Tool&))); + Retranslate(); } diff --git a/app/tool/CMakeLists.txt b/app/tool/CMakeLists.txt new file mode 100644 index 000000000..47d10b945 --- /dev/null +++ b/app/tool/CMakeLists.txt @@ -0,0 +1,21 @@ +# Olive - Non-Linear Video Editor +# Copyright (C) 2019 Olive Team +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +set(OLIVE_SOURCES + ${OLIVE_SOURCES} + tool/tool.h + PARENT_SCOPE +) diff --git a/app/tool/tool.h b/app/tool/tool.h new file mode 100644 index 000000000..36c3a4b04 --- /dev/null +++ b/app/tool/tool.h @@ -0,0 +1,25 @@ +#ifndef TOOL_H +#define TOOL_H + +namespace olive { +namespace tool { + +enum Tool { + kNone, + kPointer, + kEdit, + kRipple, + kRazor, + kSlip, + kSlide, + kHand, + kZoom, + kTransition, + kRecord, + kAdd +}; + +} +} + +#endif // TOOL_H diff --git a/app/widget/toolbar/CMakeLists.txt b/app/widget/toolbar/CMakeLists.txt index 20c429060..329a59d74 100644 --- a/app/widget/toolbar/CMakeLists.txt +++ b/app/widget/toolbar/CMakeLists.txt @@ -18,5 +18,7 @@ set(OLIVE_SOURCES ${OLIVE_SOURCES} widget/toolbar/toolbar.h widget/toolbar/toolbar.cpp + widget/toolbar/toolbarbutton.h + widget/toolbar/toolbarbutton.cpp PARENT_SCOPE ) diff --git a/app/widget/toolbar/toolbar.cpp b/app/widget/toolbar/toolbar.cpp index 2dd98580c..d8ccb988a 100644 --- a/app/widget/toolbar/toolbar.cpp +++ b/app/widget/toolbar/toolbar.cpp @@ -1,6 +1,7 @@ #include "toolbar.h" #include +#include #include #include @@ -12,22 +13,43 @@ Toolbar::Toolbar(QWidget *parent) : layout_ = new FlowLayout(this); layout_->setMargin(0); - btn_pointer_tool_ = CreateToolButton(olive::icon::ToolPointer); - btn_edit_tool_ = CreateToolButton(olive::icon::ToolEdit); - btn_ripple_tool_ = CreateToolButton(olive::icon::ToolRipple); - btn_razor_tool_ = CreateToolButton(olive::icon::ToolRazor); - btn_slip_tool_ = CreateToolButton(olive::icon::ToolSlip); - btn_slide_tool_ = CreateToolButton(olive::icon::ToolSlide); - btn_hand_tool_ = CreateToolButton(olive::icon::ToolHand); - btn_zoom_tool_ = CreateToolButton(olive::icon::ZoomIn); - btn_transition_tool_ = CreateToolButton(olive::icon::ToolTransition); - btn_snapping_toggle_ = CreateToolButton(olive::icon::Snapping); - btn_record_ = CreateToolButton(olive::icon::Record); - btn_add_ = CreateToolButton(olive::icon::Add); + // Create standard tool buttons + btn_pointer_tool_ = CreateToolButton(olive::icon::ToolPointer, olive::tool::kPointer); + btn_edit_tool_ = CreateToolButton(olive::icon::ToolEdit, olive::tool::kEdit); + btn_ripple_tool_ = CreateToolButton(olive::icon::ToolRipple, olive::tool::kRipple); + btn_razor_tool_ = CreateToolButton(olive::icon::ToolRazor, olive::tool::kRazor); + btn_slip_tool_ = CreateToolButton(olive::icon::ToolSlip, olive::tool::kSlip); + btn_slide_tool_ = CreateToolButton(olive::icon::ToolSlide, olive::tool::kSlide); + btn_hand_tool_ = CreateToolButton(olive::icon::ToolHand, olive::tool::kHand); + btn_zoom_tool_ = CreateToolButton(olive::icon::ZoomIn, olive::tool::kZoom); + btn_transition_tool_ = CreateToolButton(olive::icon::ToolTransition, olive::tool::kTransition); + btn_record_ = CreateToolButton(olive::icon::Record, olive::tool::kRecord); + btn_add_ = CreateToolButton(olive::icon::Add, olive::tool::kAdd); + + // Create snapping button, which is not actually a tool, it's a toggle option + btn_snapping_toggle_ = new ToolbarButton(this, olive::icon::Snapping, olive::tool::kNone); + layout_->addWidget(btn_snapping_toggle_); + connect(btn_snapping_toggle_, SIGNAL(clicked(bool)), this, SLOT(SnappingButtonClicked(bool))); Retranslate(); } +void Toolbar::SetTool(const olive::tool::Tool& tool) +{ + // For each tool, set the "checked" state to whether the button's tool is the current tool + for (int i=0;isetChecked(btn->tool() == tool); + } +} + +void Toolbar::SetSnapping(const bool& snapping) +{ + // Set checked state of snapping toggle + btn_snapping_toggle_->setChecked(snapping); +} + void Toolbar::changeEvent(QEvent *e) { if (e->type() == QEvent::LanguageChange) { @@ -52,11 +74,37 @@ void Toolbar::Retranslate() btn_add_->setToolTip(tr("Add Object")); } -QPushButton* Toolbar::CreateToolButton(const QIcon &icon) +ToolbarButton* Toolbar::CreateToolButton(const QIcon &icon, const olive::tool::Tool& tool) { - QPushButton* b = new QPushButton(); - b->setIcon(icon); - b->setCheckable(true); + // Create a ToolbarButton object + ToolbarButton* b = new ToolbarButton(this, icon, tool); + + // Add it to the layout layout_->addWidget(b); + + // Add it to the list for iterating through later + toolbar_btns_.append(b); + + // Connect it to the tool button click handler + connect(b, SIGNAL(clicked(bool)), this, SLOT(ToolButtonClicked())); + return b; } + +void Toolbar::ToolButtonClicked() +{ + // Get new tool from ToolbarButton object + olive::tool::Tool new_tool = static_cast(sender())->tool(); + + // Set checked state of all tool buttons + // NOTE: Not necessary if this is appropriately connected to Core + //SetTool(new_tool); + + // Emit signal that the tool just changed + emit ToolChanged(new_tool); +} + +void Toolbar::SnappingButtonClicked(bool b) +{ + emit SnappingChanged(b); +} diff --git a/app/widget/toolbar/toolbar.h b/app/widget/toolbar/toolbar.h index f5b0c400b..a2222f3fb 100644 --- a/app/widget/toolbar/toolbar.h +++ b/app/widget/toolbar/toolbar.h @@ -4,34 +4,52 @@ #include #include "widget/flowlayout/flowlayout.h" +#include "widget/toolbar/toolbarbutton.h" +#include "tool/tool.h" class Toolbar : public QWidget { + Q_OBJECT public: Toolbar(QWidget* parent); +public slots: + void SetTool(const olive::tool::Tool &tool); + void SetSnapping(const bool &snapping); + protected: virtual void changeEvent(QEvent* e) override; +signals: + void ToolChanged(const olive::tool::Tool&); + void SnappingChanged(const bool&); + private: void Retranslate(); - QPushButton *CreateToolButton(const QIcon& icon); + ToolbarButton* CreateToolButton(const QIcon &icon, const olive::tool::Tool& tool); FlowLayout* layout_; - QPushButton* btn_pointer_tool_; - QPushButton* btn_edit_tool_; - QPushButton* btn_ripple_tool_; - QPushButton* btn_razor_tool_; - QPushButton* btn_slip_tool_; - QPushButton* btn_slide_tool_; - QPushButton* btn_hand_tool_; - QPushButton* btn_transition_tool_; - QPushButton* btn_snapping_toggle_; - QPushButton* btn_zoom_tool_; - QPushButton* btn_record_; - QPushButton* btn_add_; + QList toolbar_btns_; + + ToolbarButton* btn_pointer_tool_; + ToolbarButton* btn_edit_tool_; + ToolbarButton* btn_ripple_tool_; + ToolbarButton* btn_razor_tool_; + ToolbarButton* btn_slip_tool_; + ToolbarButton* btn_slide_tool_; + ToolbarButton* btn_hand_tool_; + ToolbarButton* btn_transition_tool_; + ToolbarButton* btn_zoom_tool_; + ToolbarButton* btn_record_; + ToolbarButton* btn_add_; + + ToolbarButton* btn_snapping_toggle_; + +private slots: + void ToolButtonClicked(); + void SnappingButtonClicked(bool b); }; #endif // TOOLBAR_H diff --git a/app/widget/toolbar/toolbarbutton.cpp b/app/widget/toolbar/toolbarbutton.cpp new file mode 100644 index 000000000..5b297ae41 --- /dev/null +++ b/app/widget/toolbar/toolbarbutton.cpp @@ -0,0 +1,14 @@ +#include "toolbarbutton.h" + +ToolbarButton::ToolbarButton(QWidget *parent, const QIcon &icon, const olive::tool::Tool &tool) : + QPushButton(parent), + tool_(tool) +{ + setCheckable(true); + setIcon(icon); +} + +const olive::tool::Tool &ToolbarButton::tool() +{ + return tool_; +} diff --git a/app/widget/toolbar/toolbarbutton.h b/app/widget/toolbar/toolbarbutton.h new file mode 100644 index 000000000..71a363596 --- /dev/null +++ b/app/widget/toolbar/toolbarbutton.h @@ -0,0 +1,18 @@ +#ifndef TOOLBARBUTTON_H +#define TOOLBARBUTTON_H + +#include + +#include "tool/tool.h" + +class ToolbarButton : public QPushButton +{ +public: + ToolbarButton(QWidget* parent, const QIcon& icon, const olive::tool::Tool& tool); + + const olive::tool::Tool& tool(); +private: + olive::tool::Tool tool_; +}; + +#endif // TOOLBARBUTTON_H