Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created basic layer movement functionality (up, down) #2108

Merged
merged 10 commits into from
Jan 29, 2022
5 changes: 5 additions & 0 deletions data/graphics.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,10 @@
<file>img/material/white/square.svg</file>
<file>img/material/white/text.svg</file>
<file>img/material/white/undo-variant.svg</file>
<file>img/material/black/move_down.svg</file>
<file>img/material/black/move_up.svg</file>
<file>img/material/white/move_down.svg</file>
<file>img/material/white/move_up.svg</file>
<file>img/material/white/delete.svg</file>
</qresource>
</RCC>
3 changes: 3 additions & 0 deletions data/img/material/black/move_down.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions data/img/material/black/move_up.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions data/img/material/white/move_down.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions data/img/material/white/move_up.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions src/tools/capturecontext.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ struct CaptureContext
QPoint mousePos;
// Size of the active tool
int toolSize;
// Current circle count
int circleCount;
// Mode of the capture widget
bool fullscreen;
uint requestId;
Expand Down
11 changes: 11 additions & 0 deletions src/widgets/capture/capturetoolobjects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ void CaptureToolObjects::append(const QPointer<CaptureTool>& captureTool)
}
}

void CaptureToolObjects::insert(int index,
const QPointer<CaptureTool>& captureTool)
{
if (!captureTool.isNull() && index >= 0 &&
index <= m_captureToolObjects.size()) {
m_captureToolObjects.insert(index,
captureTool->copy(captureTool->parent()));
m_imageCache.clear();
}
}

QPointer<CaptureTool> CaptureToolObjects::at(int index)
{
if (index >= 0 && index < m_captureToolObjects.size()) {
Expand Down
1 change: 1 addition & 0 deletions src/widgets/capture/capturetoolobjects.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class CaptureToolObjects : public QObject
explicit CaptureToolObjects(QObject* parent = nullptr);
QList<QPointer<CaptureTool>> captureToolObjects();
void append(const QPointer<CaptureTool>& captureTool);
void insert(int index, const QPointer<CaptureTool>& captureTool);
void removeAt(int index);
void clear();
int size();
Expand Down
74 changes: 65 additions & 9 deletions src/widgets/capture/capturewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ CaptureWidget::CaptureWidget(uint id,
{
m_undoStack.setUndoLimit(ConfigHandler().undoLimit());

m_context.circleCount = 1;

// Base config of the widget
m_eventFilter = new HoverEventFilter(this);
connect(m_eventFilter,
Expand Down Expand Up @@ -552,6 +554,8 @@ bool CaptureWidget::startDrawObjectTool(const QPoint& pos)
// point and shouldn't wait for second point and move event
m_activeTool->drawEnd(m_context.mousePos);

m_activeTool->setCount(m_context.circleCount++);

m_captureToolObjectsBackup = m_captureToolObjects;
m_captureToolObjects.append(m_activeTool);
pushObjectsStateToUndoStack();
Expand Down Expand Up @@ -972,6 +976,14 @@ void CaptureWidget::initPanel()
&UtilityPanel::layerChanged,
this,
&CaptureWidget::updateActiveLayer);
connect(m_panel,
&UtilityPanel::moveUpClicked,
this,
&CaptureWidget::onMoveCaptureToolUp);
connect(m_panel,
&UtilityPanel::moveDownClicked,
this,
&CaptureWidget::onMoveCaptureToolDown);

m_sidePanel = new SidePanelWidget(&m_context.screenshot, this);
connect(m_sidePanel,
Expand Down Expand Up @@ -1271,6 +1283,26 @@ void CaptureWidget::updateActiveLayer(int layer)
updateSelectionState();
}

void CaptureWidget::onMoveCaptureToolUp(int captureToolIndex)
{
m_captureToolObjectsBackup = m_captureToolObjects;
pushObjectsStateToUndoStack();
auto tool = m_captureToolObjects.at(captureToolIndex);
m_captureToolObjects.removeAt(captureToolIndex);
m_captureToolObjects.insert(captureToolIndex - 1, tool);
updateLayersPanel();
}

void CaptureWidget::onMoveCaptureToolDown(int captureToolIndex)
{
m_captureToolObjectsBackup = m_captureToolObjects;
pushObjectsStateToUndoStack();
auto tool = m_captureToolObjects.at(captureToolIndex);
m_captureToolObjects.removeAt(captureToolIndex);
m_captureToolObjects.insert(captureToolIndex + 1, tool);
updateLayersPanel();
}

void CaptureWidget::selectAll()
{
m_selection->show();
Expand All @@ -1284,24 +1316,30 @@ void CaptureWidget::removeToolObject(int index)
{
--index;
if (index >= 0 && index < m_captureToolObjects.size()) {
// in case this tool is circle counter
int removedCircleCount = -1;

const CaptureTool::Type currentToolType =
m_captureToolObjects.at(index)->type();
m_captureToolObjectsBackup = m_captureToolObjects;
update(
paddedUpdateRect(m_captureToolObjects.at(index)->boundingRect()));
if (currentToolType == CaptureTool::TYPE_CIRCLECOUNT) {
removedCircleCount = m_captureToolObjects.at(index)->count();
}
m_captureToolObjects.removeAt(index);
if (currentToolType == CaptureTool::TYPE_CIRCLECOUNT) {
// Do circle count reindex
int circleCount = 1;
--m_context.circleCount;
// Decrement circle counter number starting from deleted circle
for (int cnt = 0; cnt < m_captureToolObjects.size(); cnt++) {
auto toolItem = m_captureToolObjects.at(cnt);
if (toolItem->type() != CaptureTool::TYPE_CIRCLECOUNT) {
continue;
}
if (cnt >= index) {
m_captureToolObjects.at(cnt)->setCount(circleCount);
auto circleTool = m_captureToolObjects.at(cnt);
if (circleTool->count() >= removedCircleCount) {
circleTool->setCount(circleTool->count() - 1);
}
circleCount++;
}
}
pushObjectsStateToUndoStack();
Expand Down Expand Up @@ -1486,11 +1524,7 @@ void CaptureWidget::drawToolsData()
// TODO refactor this for performance. The objects should not all be updated
// at once every time
QPixmap pixmapItem = m_context.origScreenshot;
int circleCount = 1;
for (auto toolItem : m_captureToolObjects.captureToolObjects()) {
if (toolItem->type() == CaptureTool::TYPE_CIRCLECOUNT) {
toolItem->setCount(circleCount++);
}
processPixmapWithTool(&pixmapItem, toolItem);
update(paddedUpdateRect(toolItem->boundingRect()));
}
Expand Down Expand Up @@ -1550,6 +1584,20 @@ void CaptureWidget::makeChild(QWidget* w)
w->installEventFilter(m_eventFilter);
}

void CaptureWidget::restoreCircleCountState()
{
int largest = 1;
for (int cnt = 0; cnt < m_captureToolObjects.size(); cnt++) {
auto toolItem = m_captureToolObjects.at(cnt);
if (toolItem->type() != CaptureTool::TYPE_CIRCLECOUNT) {
continue;
}
if (toolItem->count() > largest)
largest = toolItem->count();
}
m_context.circleCount = largest + 1;
}

/**
* @brief Wrapper around `new QShortcut`, properly handling Enter/Return.
*/
Expand Down Expand Up @@ -1612,6 +1660,10 @@ void CaptureWidget::undo()
m_undoStack.undo();
drawToolsData();
updateLayersPanel();

// FIXME restore m_context.circleCount since this state isn't saved in undo
// stack
restoreCircleCountState();
borgmanJeremy marked this conversation as resolved.
Show resolved Hide resolved
}

void CaptureWidget::redo()
Expand All @@ -1623,6 +1675,10 @@ void CaptureWidget::redo()
drawToolsData();
update();
updateLayersPanel();

// FIXME restore m_context.circleCount since this state isn't saved in undo
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you clarify what needs to be done to remove this FIXME?

// stack
restoreCircleCountState();
}

QRect CaptureWidget::extendedSelection() const
Expand Down
3 changes: 3 additions & 0 deletions src/widgets/capture/capturewidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ private slots:
void onToolSizeChanged(int size);
void onToolSizeSettled(int size);
void updateActiveLayer(int layer);
void onMoveCaptureToolUp(int captureToolIndex);
void onMoveCaptureToolDown(int captureToolIndex);
void selectAll();

public:
Expand Down Expand Up @@ -118,6 +120,7 @@ private slots:
void updateLayersPanel();
void pushToolToStack();
void makeChild(QWidget* w);
void restoreCircleCountState();

QList<QShortcut*> newShortcut(const QKeySequence& key,
QWidget* parent,
Expand Down
60 changes: 53 additions & 7 deletions src/widgets/panel/utilitypanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ UtilityPanel::UtilityPanel(CaptureWidget* captureWidget)
, m_layersLayout(nullptr)
, m_captureTools(nullptr)
, m_buttonDelete(nullptr)
, m_buttonMoveUp(nullptr)
, m_buttonMoveDown(nullptr)
{
initInternalPanel();
setAttribute(Qt::WA_TransparentForMouseEvents);
Expand Down Expand Up @@ -141,19 +143,45 @@ void UtilityPanel::initInternalPanel()

QHBoxLayout* layersButtons = new QHBoxLayout();
m_layersLayout->addLayout(layersButtons);

m_layersLayout->addWidget(m_captureTools);

bool isDark = ColorUtils::colorIsDark(bgColor);
QString coloredIconPath =
isDark ? PathInfo::whiteIconPath() : PathInfo::blackIconPath();

m_buttonDelete = new QPushButton(this);
m_buttonDelete->setIcon(QIcon(":/img/material/black/delete.svg"));
m_buttonDelete->setIcon(QIcon(coloredIconPath + "delete.svg"));
m_buttonDelete->setDisabled(true);

m_buttonMoveUp = new QPushButton(this);
m_buttonMoveUp->setIcon(QIcon(coloredIconPath + "move_up.svg"));
m_buttonMoveUp->setDisabled(true);

m_buttonMoveDown = new QPushButton(this);
m_buttonMoveDown->setIcon(QIcon(coloredIconPath + "move_down.svg"));
m_buttonMoveDown->setDisabled(true);

layersButtons->addWidget(m_buttonDelete);
layersButtons->addWidget(m_buttonMoveUp);
layersButtons->addWidget(m_buttonMoveDown);
layersButtons->addStretch();

connect(m_buttonDelete,
SIGNAL(clicked(bool)),
this,
SLOT(slotButtonDelete(bool)));

connect(m_buttonMoveUp,
&QPushButton::clicked,
this,
&UtilityPanel::slotUpClicked);

connect(m_buttonMoveDown,
&QPushButton::clicked,
this,
&UtilityPanel::slotDownClicked);

// Bottom
QPushButton* closeButton = new QPushButton(this);
closeButton->setText(tr("Close"));
Expand Down Expand Up @@ -192,14 +220,32 @@ int UtilityPanel::activeLayerIndex()

void UtilityPanel::onCurrentRowChanged(int currentRow)
{
if (currentRow > 0) {
m_buttonDelete->setDisabled(false);
} else {
m_buttonDelete->setDisabled(true);
}
m_buttonDelete->setDisabled(currentRow <= 0);
m_buttonMoveDown->setDisabled(currentRow == 0 ||
currentRow + 1 == m_captureTools->count());
m_buttonMoveUp->setDisabled(currentRow <= 1);

emit layerChanged(activeLayerIndex());
}

void UtilityPanel::slotUpClicked(bool clicked)
{
Q_UNUSED(clicked);
// subtract 1 because there's <empty> in m_captureTools as [0] element
int toolRow = m_captureTools->currentRow() - 1;
m_captureTools->setCurrentRow(toolRow);
emit moveUpClicked(toolRow);
}

void UtilityPanel::slotDownClicked(bool clicked)
{
Q_UNUSED(clicked);
// subtract 1 because there's <empty> in m_captureTools as [0] element
int toolRow = m_captureTools->currentRow() - 1;
m_captureTools->setCurrentRow(toolRow + 2);
emit moveDownClicked(toolRow);
}

void UtilityPanel::slotButtonDelete(bool clicked)
{
Q_UNUSED(clicked)
Expand All @@ -218,4 +264,4 @@ void UtilityPanel::slotButtonDelete(bool clicked)
bool UtilityPanel::isVisible() const
{
return !m_internalPanel->isHidden();
}
}
8 changes: 8 additions & 0 deletions src/widgets/panel/utilitypanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,18 @@ class UtilityPanel : public QWidget

signals:
void layerChanged(int layer);
void moveUpClicked(int currentRow);
void moveDownClicked(int currentRow);

public slots:
void toggle();
void slotButtonDelete(bool clicked);
void onCurrentRowChanged(int currentRow);

private slots:
void slotUpClicked(bool clicked);
void slotDownClicked(bool clicked);

private:
void initInternalPanel();

Expand All @@ -55,5 +61,7 @@ public slots:
QVBoxLayout* m_layersLayout;
QListWidget* m_captureTools;
QPushButton* m_buttonDelete;
QPushButton* m_buttonMoveUp;
QPushButton* m_buttonMoveDown;
CaptureWidget* m_captureWidget;
};