Skip to content

Commit

Permalink
feat: Add LineWidth to change the preview
Browse files Browse the repository at this point in the history
and Fix: m_selSizeTip length does not follow the text length change bug
  • Loading branch information
XMuli committed Sep 13, 2022
1 parent 96f9c7e commit 0c86585
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 27 deletions.
51 changes: 34 additions & 17 deletions src/screen/screenshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <QGuiApplication>
#include <QDir>
#include <QMouseEvent>
#include <QTimer>
#include <QDebug>

namespace Util {
Expand Down Expand Up @@ -61,7 +62,8 @@ ScreenShot::ScreenShot(QWidget *parent)
, m_edit(new XTextWidget(this))
, m_rtSmartWindow(0, 0, 0, 0)
, m_barOrien(Qt::Horizontal) // Horizontal | Vertical
, m_selSize(new SelectSize("test", this))
, m_selSizeTip(new SelectSize("", this))
, m_lineWidthTip(new SelectSize("", this))
, m_selBar(new SelectBar(m_barOrien, this))
, m_paraBar(new ParameterBar(m_barOrien, this))
{
Expand Down Expand Up @@ -112,7 +114,10 @@ ScreenShot::ScreenShot(QWidget *parent)
m_rtCalcu.scrnType = ScrnType::Wait;

// new refactor
m_selSize->setVisible(false);
m_selSizeTip->setVisible(false);
m_lineWidthTip->setVisible(false);
m_lineWidthTip->setFixedSize(75, 75);
m_lineWidthTip->move(15, 15);
m_selBar->setVisible(false);
m_paraBar->setVisible(false);
connect(m_selBar, &SelectBar::sigEnableDraw, this, &ScreenShot::onEnableDraw);
Expand All @@ -131,6 +136,7 @@ ScreenShot::ScreenShot(QWidget *parent)
connect(m_selBar, &SelectBar::sigEnableDraw, m_paraBar, &ParameterBar::onEnableDraw);
connect(m_selBar, &SelectBar::sigSelShape, m_paraBar, &ParameterBar::onSelShape);
connect(this, &ScreenShot::sigClearScreen, this, &ScreenShot::onClearScreen);
connect(this, &ScreenShot::sigLineWidthChange, this, &ScreenShot::onLineWidthChange);
}

ScreenShot::~ScreenShot()
Expand Down Expand Up @@ -217,7 +223,9 @@ void ScreenShot::onClearScreen()
m_rtCalcu.clear();

m_bFirstSel = false;
m_selSize->setVisible(false);
m_selSizeTip->setVisible(false);
m_lineWidthTip->setVisible(false);

m_selBar->setVisible(false);
m_paraBar->setVisible(false);
m_step.clear();
Expand Down Expand Up @@ -407,7 +415,7 @@ void ScreenShot::onParaBtnId(DrawShape shape, QToolButton* tb)
penWidth = 10;

m_step.pen.setWidth(penWidth);

emit sigLineWidthChange(penWidth);
} else if (bPen) {
} else if (bArrows) {
setProperty(std::to_string((int)shape).c_str(), idx);
Expand All @@ -419,6 +427,17 @@ void ScreenShot::onParaBtnId(DrawShape shape, QToolButton* tb)
}
}

void ScreenShot::onLineWidthChange(int width)
{
static QFont font(m_step.font);
font.setPointSize(14);
m_lineWidthTip->setText(QString::number(width));
m_lineWidthTip->setAlignment(Qt::AlignCenter);
m_lineWidthTip->setFont(font);
m_lineWidthTip->setVisible(true);
QTimer::singleShot(5000, [&]() { m_lineWidthTip->setVisible(false); }); // TODO 2022.09.14: 怎么有时候不到 5s 便消失覆盖了
}

void ScreenShot::onSelColor(QColor col)
{
m_step.pen.setColor(col);
Expand Down Expand Up @@ -805,24 +824,20 @@ const QVector<QPoint> ScreenShot::drawBarPosition(Qt::Orientation orien /*= Qt::
return vec;
}

const QPoint ScreenShot::drawSelSizePosition(const QRect rt)
const QPoint ScreenShot::drawSelSizeTip(const QRect& rt)
{
QPoint ret;
if (!m_selSize)
return ret;

//QString str = QString("rtSel(%1, %2, %3 * %4) m_savePixmap.rect:%5 * %6").arg(rt.left()).arg(rt.top()).arg(rt.width()).arg(rt.height())
// .arg(m_savePixmap.width()).arg(m_savePixmap.height());

QString str = QString("%1 x %2").arg(rt.width()).arg(rt.height());

if (str.compare(m_selSize->text()) != 0) {
emit m_selSize->sigTextChanged(str);
m_selSize->setText(str);
if (str.compare(m_selSizeTip->text()) != 0) {
emit m_selSizeTip->sigTextChanged(str);
m_selSizeTip->setText(str);
}

QPoint ret;
ret.setX(rt.left());
ret.setY(rt.top() - m_selSize->height() - SS_SPACE_TO_SELECTRECT);
ret.setY(rt.top() - m_selSizeTip->height() - SS_SPACE_TO_SELECTRECT);
return ret;
}

Expand Down Expand Up @@ -1198,7 +1213,7 @@ void ScreenShot::drawBorder(QRect& rtSel, QPainter& pa)
const QPen pen(QPen(XHelper::instance().borderColor(), XHelper::instance().borderWidth()));
pa.setPen(pen);
pa.setBrush(Qt::NoBrush);
m_selSize->move(drawSelSizePosition(rtSel));
m_selSizeTip->move(drawSelSizeTip(rtSel));

QRect rt(rtSel);
for (auto it = m_specifyRts.cbegin(); it != m_specifyRts.cend(); ++it) {
Expand Down Expand Up @@ -1420,8 +1435,8 @@ void ScreenShot::mouseReleaseEvent(QMouseEvent *event)
if (!m_selBar->isVisible())
m_selBar->setVisible(true);

if (!m_selSize->isVisible())
m_selSize->setVisible(true);
if (!m_selSizeTip->isVisible())
m_selSizeTip->setVisible(true);
}

if (m_rtCalcu.scrnType != ScrnType::Draw) {
Expand Down Expand Up @@ -1451,6 +1466,8 @@ void ScreenShot::wheelEvent(QWheelEvent* event)
m_step.pen.setWidth(1);
if (m_step.pen.width() >= 100)
m_step.pen.setWidth(100);

emit sigLineWidthChange(m_step.pen.width());
}

event->accept();
Expand Down
8 changes: 6 additions & 2 deletions src/screen/screenshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class ScreenShot : public QWidget

signals:
void sigClearScreen();
void sigLineWidthChange(int width);

public slots:
void onClearScreen();
Expand All @@ -81,6 +82,8 @@ public slots:


void onParaBtnId(DrawShape shape, QToolButton* tb);
void onLineWidthChange(int width);

void onSelColor(QColor col);

private:
Expand All @@ -95,7 +98,7 @@ public slots:
bool isDrawShape(XDrawStep& step);

const QVector<QPoint> drawBarPosition(Qt::Orientation orien = Qt::Horizontal, ToolBarOffset offset = ToolBarOffset::TBO_Middle);
const QPoint drawSelSizePosition(const QRect rt);
const QPoint drawSelSizeTip(const QRect& rt);

// Test
void showAllDrawedShape(QPainter& pa);
Expand Down Expand Up @@ -144,7 +147,8 @@ public slots:
// new refactor
QRect m_rtSmartWindow; // 自动检测窗口矩形大小;用以给其它赋值
Qt::Orientation m_barOrien;
QPointer<SelectSize> m_selSize; // 左上角显示窗口大小
QPointer<SelectSize> m_selSizeTip; // 选中矩形的尺寸提示
QPointer<SelectSize> m_lineWidthTip; // 画笔宽度临时预览
QPointer<SelectBar> m_selBar;
QPointer<ParameterBar> m_paraBar;
QPointer<XTextWidget> m_edit;
Expand Down
6 changes: 2 additions & 4 deletions src/tool/selectsize/selectsize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ void SelectSize::onTextChanged(QString text)
{
QFontMetrics fm(this->font());
QRect bound = fm.boundingRect(QRect(), Qt::AlignCenter, text);

setMinimumWidth(bound.width()); // TODO 2022.07.02: 需要优化,x 坐标比较大时候,则右侧留白会过长
update(rect());
adjustSize();
}

void SelectSize::initUI()
Expand All @@ -52,5 +50,5 @@ void SelectSize::paintEvent(QPaintEvent* e)

pa.setPen("#FFFFFF");
pa.setBrush(Qt::NoBrush);
pa.drawText(contentsRect(), text());
pa.drawText(contentsRect(), Qt::AlignCenter, text());
}
1 change: 0 additions & 1 deletion src/tool/selectsize/selectsize.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public slots:

protected:
void paintEvent(QPaintEvent* e) override;

};

#endif // SELECTSIZE_H
6 changes: 3 additions & 3 deletions src/xglobal.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#ifndef XGLOBAL_H
#define XGLOBAL_H

#define _MYDEBUG // 调试
//#define _MYDEBUG // 调试

// 1. HAIF_INTERVAL 为一半间隔,边框宽度一半高; 2 * HAIF_INTERVAL 为边框的宽度,为 getOuterRect - getRect == getRect - getInnerRect == HAIF_INTERVAL
// 2. HAIF_INTERVAL 为边框一般的灵敏度,光标移动到上面便会变化形态
Expand Down Expand Up @@ -86,8 +86,8 @@

// 左上角用来显示,选中矩形区域 SelectSize 的大小
#define SS_MARGIN_LEFT 3 // 边框左部的间距
#define SS_MARGIN_RIGHT 5 // 边框右部的间距
#define SS_MARGIN_TOP 2 // 边框顶部的间距
#define SS_MARGIN_RIGHT 3 // 边框右部的间距
#define SS_MARGIN_TOP 3 // 边框顶部的间距
#define SS_MARGIN_BOTTOM 3 // 边框底部的间距
#define SS_SPACE_TO_SELECTRECT 10 // 此部件 底部距离选中框的顶部的距离
#define SS_RADIRS 2 // 此部件 圆角矩形的圆角
Expand Down

0 comments on commit 0c86585

Please sign in to comment.