Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions src/common/utils/compression.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
/*
* Drawy - A simple brainstorming tool with an infinite canvas
* Copyright (C) 2025 - Prayag Jain <[email protected]>
*
* Authors:
* 1. quarterstar - [email protected]
*
* 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 <http://www.gnu.org/licenses/>.
*/
* Drawy - A simple brainstorming tool with an infinite canvas
* Copyright (C) 2025 - Prayag Jain <[email protected]>
*
* Authors:
* 1. quarterstar - [email protected]
*
* 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 <http://www.gnu.org/licenses/>.
*/

// NOTE: qt headers MUST be placed before
// kansi otherwise there will be a macro conflict
Expand Down
38 changes: 19 additions & 19 deletions src/common/utils/compression.hpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
/*
* Drawy - A simple brainstorming tool with an infinite canvas
* Copyright (C) 2025 - Prayag Jain <[email protected]>
*
* Authors:
* 1. quarterstar - [email protected]
*
* 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 <http://www.gnu.org/licenses/>.
*/
* Drawy - A simple brainstorming tool with an infinite canvas
* Copyright (C) 2025 - Prayag Jain <[email protected]>
*
* Authors:
* 1. quarterstar - [email protected]
*
* 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 <http://www.gnu.org/licenses/>.
*/

#pragma once

Expand Down
14 changes: 14 additions & 0 deletions src/common/utils/qt.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "qt.hpp"

namespace Common::Utils::QtUtil {
uint64_t keyToken(const std::variant<Qt::Key, Qt::MouseButton> &v) {
uint64_t type = static_cast<uint64_t>(v.index()); // 0 = Qt::Key, 1 = Qt::MouseButton
uint64_t val = 0;
if (std::holds_alternative<Qt::Key>(v)) {
val = static_cast<uint64_t>(std::get<Qt::Key>(v));
} else {
val = static_cast<uint64_t>(std::get<Qt::MouseButton>(v));
}
return (type << 32) | (val & 0xffffffffULL);
}
} // namespace Common::Utils::QtUtil
10 changes: 10 additions & 0 deletions src/common/utils/qt.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

#include <QKeyEvent>
#include <QMouseEvent>
#include <cstdint>
#include <variant>

namespace Common::Utils::QtUtil {
uint64_t keyToken(const std::variant<Qt::Key, Qt::MouseButton> &v);
}
24 changes: 24 additions & 0 deletions src/controller/action.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "action.hpp"

void ToolTask::start(const TaskContext &ctx, const ActionState &state) {
m_toolBar.curTool().cleanup();
m_canvas.setCursor(m_toolBar.tool(m_tool).cursor());
m_toolBar.tool(m_tool).mousePressed(ctx.m_appContext);

// This is needed so that the thing hovered
// over the first time a press is detected is immediately
// queued for deletion.
if (!state.m_prevHeld && m_tool == Tool::Eraser) {
m_toolBar.tool(m_tool).mouseMoved(ctx.m_appContext);
}
}

void ToolTask::execute(const TaskContext &ctx, const ActionState &state) {
m_toolBar.tool(m_tool).mouseMoved(ctx.m_appContext);
}

void ToolTask::stop(const TaskContext &ctx, const ActionState &state) {
m_toolBar.tool(m_tool).mouseReleased(ctx.m_appContext);
m_toolBar.tool(m_tool).cleanup();
m_canvas.setCursor(m_toolBar.curTool().cursor());
}
125 changes: 125 additions & 0 deletions src/controller/action.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#pragma once

#include <cstdint>
#include <memory>
#include <variant>
#include <vector>

#include "../canvas/canvas.hpp"
#include "../common/utils/qt.hpp"
#include "../components/toolbar.hpp"
#include "../tools/tool.hpp"

using namespace Common::Utils::QtUtil;

struct ActionKey {
std::variant<Qt::Key, Qt::MouseButton> m_id;
bool m_mouse{false};
size_t m_count{1};
};

inline ActionKey key(Qt::MouseButton key, size_t count = 1) {
return ActionKey{.m_id = std::variant<Qt::Key, Qt::MouseButton>{key},
.m_mouse = true,
.m_count = count};
}

inline ActionKey key(Qt::Key key, size_t count = 1) {
return ActionKey{.m_id = key, .m_mouse = false, .m_count = count};
}

struct ActionData {
std::vector<ActionKey> m_keys;
bool m_holdRequired{false};
int64_t m_maxGapMs{0};
};

struct TaskContext {
ApplicationContext *m_appContext;
};
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The class ApplicationContext was recently made a singleton, which means only one instance of it exists in the app. You don't have to pass its pointers anymore. Just access it by using ApplicationContext::instance().


struct ActionState {
bool m_prevHeld{false};
};

class IActionTask {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use the I prefix, it's not a convention I follow here.

public:
virtual void start(const TaskContext &ctx, const ActionState &state) = 0;
virtual void execute(const TaskContext &ctx, const ActionState &state) = 0;
virtual void stop(const TaskContext &ctx, const ActionState &state) = 0;

virtual ~IActionTask() = default;
};

class ToolTask : public IActionTask {
public:
ToolTask(ToolBar &toolBar, Canvas &canvas, Tool::Type tool)
: m_toolBar(toolBar),
m_canvas(canvas),
m_tool(tool) {}

ToolBar &m_toolBar;
Canvas &m_canvas;

Tool::Type m_tool;

void start(const TaskContext &ctx, const ActionState &state) override;
void execute(const TaskContext &ctx, const ActionState &state) override;
void stop(const TaskContext &ctx, const ActionState &state) override;
};

struct ActionFacade {
ActionFacade(std::unique_ptr<IActionTask> task, ActionData data)
: m_task(std::move(task)),
m_data(std::move(data)) {}

std::unique_ptr<IActionTask> m_task;
ActionData m_data;
ActionState m_state{};
bool m_enabled{false};

inline void enable(const TaskContext &ctx) {
if (m_enabled)
return;

if (m_task)
m_task->start(ctx, m_state);

m_enabled = true;
}

inline void execute(const TaskContext &ctx) {
if (!m_enabled || !m_task)
return;

m_task->execute(ctx, m_state);
}

inline void disable(const TaskContext &ctx) {
if (!m_enabled)
return;

if (m_task)
m_task->stop(ctx, m_state);

m_enabled = false;
}

inline void toggle(TaskContext ctx) { m_enabled ? disable(ctx) : enable(ctx); }
};

inline std::vector<ActionFacade> getDefaultActions(ToolBar &toolbar, Canvas &canvas) {
std::vector<ActionFacade> result;

result.push_back(ActionFacade{
std::make_unique<ToolTask>(toolbar, canvas, Tool::Move),
ActionData{{key(Qt::Key_Space), key(Qt::LeftButton)}, true, -2},
});

result.push_back(ActionFacade{
std::make_unique<ToolTask>(toolbar, canvas, Tool::Eraser),
ActionData{{key(Qt::RightButton)}, true, -2},
});

return result;
}
Loading