Skip to content

Commit

Permalink
feat: [UI] Add some new features
Browse files Browse the repository at this point in the history
  1.Adjust crop button position to left side toolbar
  2.When the crop button is clicked, the view window fills the entire scene
  3.Can adjust the size of the cropping box through four edges
  4.When cropping, the unselected parts of the drawing board need to be masked with gray
  5.Display the correct cursor style when resizing

Log: [UI] Add some new features
  • Loading branch information
starhcq committed Nov 15, 2024
1 parent 306bd6b commit 2ca6cb5
Show file tree
Hide file tree
Showing 14 changed files with 310 additions and 33 deletions.
3 changes: 1 addition & 2 deletions src/deepin-draw/drawfiles/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ void MainWindow::initUI()
DrawToolFactory::registTool(cut, CCutTool::createTool);
m_drawBoard->toolManager()->addTool(cut);
m_drawBoard->toolManager()->addTool(EUndoTool);
//设置左侧裁剪窗口隐藏
m_drawBoard->toolManager()->tool(cut)->toolButton()->hide();

setWgtAccesibleName(this, "MainWindow");
drawApp->setWidgetAllPosterityNoFocus(titlebar());
setWindowTitle(tr("Draw"));
Expand Down
102 changes: 102 additions & 0 deletions src/deepin-draw/drawitems/maskitem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

#include "maskitem.h"
#include "pagescene.h"
#include "pageitem_p.h"
#include <QPainter>
REGISTITEMCLASS(MaskItem, MaskItemType)

MaskItem::MaskItem(PageScene *scene):
PageItem(nullptr)
{
setZValue(10000);
if (scene != nullptr) {
setRect(scene->sceneRect());
} else {
setRect(QRectF(0, 0, 1920, 1080));
}
}

int MaskItem::type() const
{
return MaskItemType;
}

QRectF MaskItem::itemRect() const
{
return rect();
}

void MaskItem::setRect(const QRectF &rect)
{
prepareGeometryChange();
m_rect = rect;
updateShape();
}

QRectF MaskItem::rect() const
{
return m_rect;
}

bool MaskItem::contains(const QPointF &point) const
{
return false;
}

bool MaskItem::isPosPenetrable(const QPointF &posLocal)
{
return true;
}

bool MaskItem::isRectPenetrable(const QRectF &rectLocal)
{
return true;
}

void MaskItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
painter->setBrush(Qt::transparent);
painter->setPen(Qt::NoPen);
painter->drawRect(orgRect());
PageItem::paint(painter, option, widget);

if (pageScene()->page()->currentTool() == cut) {
PageItem* cutItem = nullptr;
QList<PageItem *> pageItems = pageScene()->allPageItems();
foreach (auto item, pageItems) {
if (item && item->type() == CutType) {
cutItem = item;
break;
}
}

if (cutItem && cutItem->isVisible()) {
painter->save();
QRectF layerRct = mapToScene(itemRect()).boundingRect();
QRectF cutRct = cutItem->mapToScene(cutItem->itemRect()).boundingRect();
QRegion r1(cutRct.toRect());
QRegion r2(layerRct.toRect());
QRegion r3 = r2.subtracted(r1);
QPainterPath path;
path.addRegion(r3);

QColor background(0, 0, 0, 51);
painter->setPen(Qt::NoPen);
painter->setBrush(background);
painter->drawPath(path);
painter->restore();
}
}
}

void MaskItem::updateShape()
{
// QRectF rct;
// if (scene() != nullptr) {
// rct = scene()->sceneRect();
// }
// setRect(rct);
}
45 changes: 45 additions & 0 deletions src/deepin-draw/drawitems/maskitem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

#ifndef MASKITEM_H
#define MASKITEM_H
#include "pageitem.h"

class PageScene;
class MaskItem: public PageItem
{
public:
MaskItem(PageScene *scene = nullptr);

int type() const override;

/**
* @brief type 图元的包裹矩形
*/
QRectF itemRect() const override;

/**
* @brief setRect 设置蒙板矩形
*/
void setRect(const QRectF &rect);

/**
* @brief rect 蒙板矩形
*/
QRectF rect() const;

private:

bool contains(const QPointF &point) const override;
bool isPosPenetrable(const QPointF &posLocal) override;
bool isRectPenetrable(const QRectF &rectLocal) override;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget) override;
void updateShape() override;

QRectF m_rect;
};
Q_DECLARE_METATYPE(MaskItem *)

#endif // MASKITEM_H
44 changes: 39 additions & 5 deletions src/deepin-draw/drawtools/ccuttool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,15 @@ QAbstractButton *CCutTool::initToolButton()
m_cutBtn->setShortcut(QKeySequence(QKeySequence(Qt::Key_C)));
setWgtAccesibleName(m_cutBtn, "Crop tool button");
m_cutBtn->setToolTip(tr("Crop canvas (C)"));
m_cutBtn->setIconSize(QSize(48, 48));
m_cutBtn->setFixedSize(QSize(37, 37));
m_cutBtn->setIconSize(TOOL_ICON_RECT);
m_cutBtn->setFixedSize(TOOL_BUTTON_RECT);
m_cutBtn->setCheckable(true);
connect(m_cutBtn, &DToolButton::toggled, m_cutBtn, [ = ](bool b) {
QIcon icon = QIcon::fromTheme("ddc_screenshot tool_normal");
QIcon activeIcon = QIcon::fromTheme("ddc_screenshot tool_active");
QIcon icon = QIcon::fromTheme("crop_normal");
QIcon activeIcon = QIcon::fromTheme("crop_highlight");
m_cutBtn->setIcon(b ? activeIcon : icon);
});
m_cutBtn->setIcon(QIcon::fromTheme("ddc_screenshot tool_normal"));
m_cutBtn->setIcon(QIcon::fromTheme("crop_normal"));
return m_cutBtn;
}

Expand Down Expand Up @@ -126,6 +126,7 @@ void CCutTool::funcStart(ToolSceneEvent *event)
Q_UNUSED(event)
m_pCutItem = getCurCutItem();

event->view()->page()->blockSettingDrawCursor(true);
QGraphicsItem *pFirstItem = event->itemsUnderPressedPos().isEmpty() ? nullptr : event->itemsUnderPressedPos().first();
if (pFirstItem != nullptr) {
event->view()->page()->setDrawCursor(Qt::ClosedHandCursor);
Expand All @@ -148,6 +149,7 @@ void CCutTool::funcFinished(ToolSceneEvent *event, int decided)
{
Q_UNUSED(decided)
Q_UNUSED(event)
event->view()->page()->blockSettingDrawCursor(false);
funHover(event);
}

Expand Down Expand Up @@ -227,6 +229,35 @@ void CCutTool::deleteCutItem(PageScene *scene)
}
}

void CCutTool::createMaskItem(PageScene *scene)
{
if (!m_maskItems.contains(scene)) {
deleteCutItem(scene);

scene->clearSelections();

m_pMaskItem = new MaskItem(scene);
scene->addPageItem(m_pMaskItem);

m_maskItems.insert(scene, m_pMaskItem);
}
}

void CCutTool::deleteMaskItem(PageScene *scene)
{
if (m_maskItems.contains(scene)) {
auto itf = m_maskItems[scene];
MaskItem *pMaskItem = itf;
scene->removePageItem(pMaskItem);
if (pMaskItem == m_pMaskItem) {
m_pMaskItem = nullptr;
}

delete pMaskItem;
m_maskItems.remove(scene);
}
}

QSizeF CCutTool::changeCutType(int type, PageScene *scene)
{
CutItem *pItem = getCutItem(scene);
Expand Down Expand Up @@ -318,9 +349,12 @@ void CCutTool::onStatusChanged(EStatus oldStatus, EStatus newStatus)
auto page = currentPage();
if (oldStatus == EIdle && newStatus == EReady) {
page->installEventFilter(this);
page->view()->fitInViewEx();
createMaskItem(page->scene());
createCutItem(page->scene());
} else if (newStatus == EIdle) {
page->removeEventFilter(this);
deleteMaskItem(page->scene());
deleteCutItem(page->scene());
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/deepin-draw/drawtools/ccuttool.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "drawfunctiontool.h"
#include "cutitem.h"
#include "maskitem.h"
#include "csizehandlerect.h"

class CCutWidget;
Expand Down Expand Up @@ -91,6 +92,9 @@ class CCutTool : public DrawFunctionTool
* @param scene 场景句柄
*/
void deleteCutItem(PageScene *scene);

void createMaskItem(PageScene *scene);
void deleteMaskItem(PageScene *scene);
/**
* @brief changeCutType 改变裁剪类型
* @param scene 场景句柄
Expand Down Expand Up @@ -138,13 +142,15 @@ class CCutTool : public DrawFunctionTool

private:
CutItem *m_pCutItem;
MaskItem *m_pMaskItem = nullptr;
HandleNode::EInnerType m_dragHandle; //选中的方块方向
HandleNode::EInnerType m_clickHandle;

bool m_bModify;

// 用于保存剪裁图元避免多个场景有裁剪的时候其它场景不显示
QMap<PageScene *, CutItem *> m_cutItems;
QMap<PageScene *, MaskItem *> m_maskItems;

QMap<PageScene *, QRectF> m_originSizeMap;
};
Expand Down
3 changes: 2 additions & 1 deletion src/deepin-draw/widgets/toptoolbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ void TopTilte::initUI()
m_editDrawBorad->resize(QSize(35, 35));
m_editDrawBorad->setToolTip(tr("Crop canvas (C)"));
m_editDrawBorad->setShortcut(QKeySequence(QKeySequence(Qt::Key_C)));
m_editDrawBorad->hide();

m_label = new QLabel(this);
m_label->setText(tr("Unnamed"));
Expand Down Expand Up @@ -86,7 +87,7 @@ void TopTilte::initUI()

setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

connect(m_editDrawBorad, &QToolButton::toggled, this, &TopTilte::editProportion);
//connect(m_editDrawBorad, &QToolButton::toggled, this, &TopTilte::editProportion);
}

void TopTilte::initComboBox()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,32 @@ QCursor HandleNode::cursor() const
return d_HandleNode()->getCursor(nodeType());
}

QRectF HandleNode::boundingRect() const
{
if (d_HandleNode()->showIcon)
return QGraphicsSvgItem::boundingRect();

auto rect = parentPageItem()->orgRect();
QRectF oldBoundingRect = QGraphicsSvgItem::boundingRect();
QRectF newBoundingRect = oldBoundingRect;
switch (nodeType()) {
case Resize_T:
case Resize_B: {
newBoundingRect.setWidth(rect.width() - 2 * 15);
break;
}
case Resize_L:
case Resize_R: {
newBoundingRect.setHeight(rect.height() - 2 * 15);
break;
}
default:
newBoundingRect = oldBoundingRect;
break;
}
return newBoundingRect;
}

void HandleNode::setIconVisible(bool flag)
{
d_HandleNode()->showIcon = flag;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class DRAWLIB_EXPORT HandleNode : public QGraphicsSvgItem

virtual QCursor cursor()const;

QRectF boundingRect() const override;

protected:
bool contains(const QPointF &point) const override;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
Expand Down
8 changes: 8 additions & 0 deletions src/drawboard/drawboard/drawboard/gui/pageview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1169,6 +1169,14 @@ void PageView::scale(qreal scale, EScaleCenter center, const QPoint &viewPos)
scaleWithCenter(multiple, centerViewPos);
}

void PageView::fitInViewEx()
{
fitInView(sceneRect(), Qt::KeepAspectRatio);
QTransform t = transform();
d_PageView()->m_scale = t.m11();
emit signalSetScale(d_PageView()->m_scale);
}

qreal PageView::getScale()
{
return d_PageView()->m_scale;
Expand Down
6 changes: 6 additions & 0 deletions src/drawboard/drawboard/drawboard/gui/pageview.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ class DRAWLIB_EXPORT PageView : public QGraphicsView
*/
void scale(qreal scale, EScaleCenter center = EViewCenter, const QPoint &viewPos = QPoint());

/**
* @brief 缩放到视图窗口大小
* @param
*/
void fitInViewEx();

/**
* @brief scale 获取缩放接口
* @return scale 缩放比例
Expand Down
28 changes: 28 additions & 0 deletions src/drawboard/drawboard/res/toolIconsNew/crop_highlight_20px.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 2ca6cb5

Please sign in to comment.