#include "cutdialog.h" #include "ui_cutdialog.h" #include #include #include #include "HGString.h" #include "sane/sane_option_definitions.h" cutDialog::cutDialog(QWidget *parent) : QWidget(parent), ui(new Ui::cutDialog) { ui->setupUi(this); divisor = 8; dpi = 1; sizeType = 0; paperWidth = 185; h_w = 260.0/185.0; sizeRate = 1; paperHeight = paperWidth*h_w; this->setFixedSize(paperWidth,paperHeight); setMouseTracking(true); m_startPoint = QPoint(10,10); m_endPoint = QPoint(50,50); m_mouse_down = false; initAllCorner(); } cutDialog::~cutDialog() { delete ui; } void cutDialog::setPaperSize(QString type, const int w) { paperType = type; if (paperType == OPTION_VALUE_ZZCC_A3) h_w = 420.0 / 297.0; else if (paperType == OPTION_VALUE_ZZCC_8K) h_w = 390.0 / 270.0; else if (paperType == OPTION_VALUE_ZZCC_16K) h_w = 270.0 / 190.0; else if (paperType == OPTION_VALUE_ZZCC_16KHX) h_w = 190.0 / 270.0; else if (paperType == OPTION_VALUE_ZZCC_A4) h_w = 297.0 / 210.0; else if (paperType == OPTION_VALUE_ZZCC_A4HX) h_w = 210.0 / 297.0; else if (paperType == OPTION_VALUE_ZZCC_A5) h_w = 210.0 / 148.0; else if (paperType == OPTION_VALUE_ZZCC_A5HX) h_w = 148.0 / 210.0; else if (paperType == OPTION_VALUE_ZZCC_A6) h_w = 148.0 / 105.0; else if (paperType == OPTION_VALUE_ZZCC_A6HX) h_w = 105.0 / 148.0; else if (paperType == OPTION_VALUE_ZZCC_B4) h_w = 353.0 / 250.0; else if (paperType == OPTION_VALUE_ZZCC_B5) h_w = 250.0 / 176.0; else if (paperType == OPTION_VALUE_ZZCC_B5HX) h_w = 176.0 / 250.0; else if (paperType == OPTION_VALUE_ZZCC_B6) h_w = 176.0 / 125.0; else if (paperType == OPTION_VALUE_ZZCC_B6HX) h_w = 125.0 / 176.0; else if (paperType == OPTION_VALUE_ZZCC_Letter) h_w = 279.0 / 216.0; else if (paperType == OPTION_VALUE_ZZCC_LetterHX) h_w = 216.0 / 279.0; else if (paperType == OPTION_VALUE_ZZCC_DoubleLetter) h_w = 559.0 / 216.0; else if (paperType == OPTION_VALUE_ZZCC_LEGAL) h_w = 356.0 / 216.0; else if (paperType == OPTION_VALUE_ZZCC_SLSJ) h_w = 560.0 / 270.0; else if(paperType == OPTION_VALUE_ZZCC_ZDSMCC) h_w = 3307.0*2/2338; else if(paperType == OPTION_VALUE_ZZCC_PPYSCC) { if(dpi == 100.0) h_w = 1795.0/1189; if(dpi == 150.0) h_w = 1795.0/1784; if(dpi == 200.0) h_w = 3307.0/2338; if(dpi == 240.0) h_w = 4308.0/2854; if(dpi == 200.0) h_w = 5385.0/3567; }; if(type.contains("3")) divisor = 2; else if (type.contains("4")) divisor = 4; else if (type.contains("5")) divisor = 6; else if (type.contains("6")) divisor = 8; else divisor = 4; paperWidth = w; if(type.contains(StdStringToUtf8("横向").c_str())) paperWidth = paperWidth*h_w*1.5; double realW = paperWidth; if (paperType == OPTION_VALUE_ZZCC_A3 || paperType == OPTION_VALUE_ZZCC_A4HX) realRate = 297.0 / realW; else if (paperType == OPTION_VALUE_ZZCC_A4 || paperType == OPTION_VALUE_ZZCC_A5HX) realRate = 210.0 / realW; else if (paperType == OPTION_VALUE_ZZCC_A5 || paperType == OPTION_VALUE_ZZCC_A6HX) realRate = 148.0 / realW; else if (paperType == OPTION_VALUE_ZZCC_A6) realRate = 105.0 / realW; else if (paperType == OPTION_VALUE_ZZCC_16K) realRate = 190.0 / realW; else if (paperType == OPTION_VALUE_ZZCC_8K || paperType == OPTION_VALUE_ZZCC_16KHX) realRate = 270.0 / realW; else if (paperType == OPTION_VALUE_ZZCC_B4 || paperType == OPTION_VALUE_ZZCC_B5HX) realRate = 250.0 / realW; else if (paperType == OPTION_VALUE_ZZCC_B5 || paperType == OPTION_VALUE_ZZCC_B6HX) realRate = 176.0 / realW; else if (paperType == OPTION_VALUE_ZZCC_B6) realRate = 125.0 / realW; else if (paperType == OPTION_VALUE_ZZCC_Letter) realRate = 216.0 / realW; else if (paperType == OPTION_VALUE_ZZCC_LetterHX) realRate = 279.0 / realW; else if (paperType == OPTION_VALUE_ZZCC_DoubleLetter) realRate = 216.0 / realW; else if (paperType == OPTION_VALUE_ZZCC_LEGAL) realRate = 216.0 / realW; else if (paperType == OPTION_VALUE_ZZCC_SLSJ) realRate = 270.0 / realW; else if(paperType == OPTION_VALUE_ZZCC_ZDSMCC) realRate = 297.0/realW; else if(paperType == OPTION_VALUE_ZZCC_PPYSCC) { if(dpi == 100.0) realRate = 1189.0*0.039377/dpi/realW; if(dpi == 150.0) realRate = 1784*0.039377/dpi/realW; if(dpi == 200.0) realRate = 2338*0.039377/dpi/realW; if(dpi == 240.0) realRate = 2854*0.039377/dpi/realW; if(dpi == 200.0) realRate = 3567*0.039377/dpi/realW; }; paperHeight = paperWidth*h_w; this->setFixedSize(paperWidth+4,paperHeight+4); scaleRec = QRectF(0,0,paperWidth,paperHeight+0.5); m_rect = QRectF(m_startPoint,m_endPoint); update(); refreshView(); } void cutDialog::setDpiValue(const double d) { dpi = d; if(sizeType == PIXEL) sizeRate = 0.03937*dpi; update(); } void cutDialog::setSizeType(const SIZETYPE &t) { sizeType = t; switch (sizeType) { case MILLIM: sizeRate = 1; break; case INCH: sizeRate = 0.03937; break; case PIXEL: sizeRate = 0.03937*dpi; break; } update(); } void cutDialog::setCutRectWidth(double w) { m_endPoint.setX(m_startPoint.x()+scaleRec.width()*w/getPaperSize().width()); update(); } void cutDialog::setCutRectHeight(double h) { m_endPoint.setY(m_startPoint.y()+scaleRec.height()*h/getPaperSize().height()); update(); } void cutDialog::setCutRectStartX(double x) { m_startPoint.setX(scaleRec.width()*x/getPaperSize().width()); update(); } void cutDialog::setCutRectStartY(double y) { m_startPoint.setY(scaleRec.height()*y/getPaperSize().height()); update(); } QSizeF cutDialog::getPaperSize() const { double realW = paperWidth*realRate*sizeRate; double realH = paperHeight*realRate*sizeRate; return QSizeF(realW,realH); } QSizeF cutDialog::getCutRectSize() const { double realCutW = getPaperSize().width()*m_rect.width()/scaleRec.width(); double realCutH = getPaperSize().height()*m_rect.height()/scaleRec.height(); return QSizeF(realCutW,realCutH); } QPointF cutDialog::getCutRectStartPos() const { double realCutX = getPaperSize().width()*m_startPoint.x()/scaleRec.width(); double realCutY = getPaperSize().height()*m_startPoint.y()/scaleRec.height(); return QPointF(realCutX,realCutY); } QRectF cutDialog::getCutRectPixel() const { double x = 0.03937*dpi*paperWidth*realRate*m_startPoint.x()/scaleRec.width(); double y = 0.03937*dpi*paperHeight*realRate*m_startPoint.y()/scaleRec.height(); double w = 0.03937*dpi*paperWidth*realRate*m_rect.width()/scaleRec.width(); double h = 0.03937*dpi*paperHeight*realRate*m_rect.height()/scaleRec.height(); return QRectF(x,y,w,h); } void cutDialog::setCutRectPixel(QRectF &rect) { m_startPoint.setX(scaleRec.width()*rect.x()/(paperWidth*realRate*0.03937*dpi)); m_startPoint.setY(scaleRec.height()*rect.y()/(paperHeight*realRate*0.03937*dpi)); m_endPoint.setX(m_startPoint.x()+scaleRec.width()*rect.width()/(paperWidth*realRate*0.03937*dpi)); m_endPoint.setY(m_startPoint.y()+scaleRec.height()*rect.height()/(paperHeight*realRate*0.03937*dpi)); m_rect = QRectF(m_startPoint,m_endPoint); update(); } QRectF cutDialog::getCutRectMM()const { double x = paperWidth * realRate * m_startPoint.x() / scaleRec.width(); double y = paperHeight * realRate * m_startPoint.y() / scaleRec.height(); double w = paperWidth * realRate * m_rect.width() / scaleRec.width(); double h = paperHeight * realRate * m_rect.height() / scaleRec.height(); return QRectF(x, y, w, h); } void cutDialog::setCutRectMM(QRectF& rect) { m_startPoint.setX(scaleRec.width() * rect.x() / (paperWidth * realRate)); m_startPoint.setY(scaleRec.height() * rect.y() / (paperHeight * realRate)); m_endPoint.setX(m_startPoint.x() + scaleRec.width() * rect.width() / (paperWidth * realRate)); m_endPoint.setY(m_startPoint.y() + scaleRec.height() * rect.height() / (paperHeight * realRate)); m_rect = QRectF(m_startPoint, m_endPoint); update(); } int cutDialog::getCutRectRight() const { return getPaperSize().width()*m_rect.right()/scaleRec.right(); } int cutDialog::getCutRectBottom() const { return getPaperSize().height()*m_rect.bottom()/scaleRec.bottom(); } void cutDialog::mousePressEvent(QMouseEvent *event) { int x = event->x()/**paperWidth/width()*/; int y = event->y()/**paperHeight/height()*/; m_moveStart = QPoint(x,y); m_mouse_down = event->button() == Qt::LeftButton; update(); } void cutDialog::mouseMoveEvent(QMouseEvent *event) { int x = event->x()/**paperWidth/width()*/; int y = event->y()/**paperHeight/height()*/; if(m_mouse_down){ int dx = m_moveStart.x() - x; int dy = m_moveStart.y() - y; m_moveStart = QPoint(x,y); if(m_leftCorn){ m_startPoint.setX(x); }else if(m_rightCorn){ m_endPoint.setX(x); }else if(m_topCorn){ m_startPoint.setY(y); }else if(m_bottomCorn){ m_endPoint.setY(y); }else if(m_leftTop){ m_startPoint.setX(x); m_startPoint.setY(y); }else if(m_leftBottom){ m_startPoint.setX(x); m_endPoint.setY(y); }else if(m_rightTop){ m_startPoint.setY(y); m_endPoint.setX(x); }else if(m_rightBottom){ m_endPoint.setX(x); m_endPoint.setY(y); } else if(!m_out){ if(m_startPoint.x() - dx < 0) dx = 0; if(m_startPoint.y() - dy < 0) dy = 0; if(m_endPoint.x() - dx > scaleRec.right()) dx = 0; if(m_endPoint.y() - dy > scaleRec.bottom()) dy = 0; m_startPoint.setX(m_rect.left()-dx); m_startPoint.setY(m_rect.top()-dy); m_endPoint.setX(m_rect.right()-dx); m_endPoint.setY(m_rect.bottom()-dy); } if(m_startPoint.x()<=0) m_startPoint.setX(0); if(m_startPoint.y()<=0) m_startPoint.setY(0); if(m_endPoint.x()>=scaleRec.width()) m_endPoint.setX(scaleRec.right()); if(m_endPoint.y()>=scaleRec.height()) m_endPoint.setY(scaleRec.bottom()); if(qAbs(m_startPoint.x() - m_endPoint.x()) <= 10 || m_startPoint.x() > m_endPoint.x()){ m_startPoint.setX(m_rect.left()); m_endPoint.setX(m_rect.right()); } if(qAbs(m_startPoint.y() - m_endPoint.y()) <= 10 || m_startPoint.y() > m_endPoint.y()){ m_startPoint.setY(m_rect.top()); m_endPoint.setY(m_rect.bottom()); } update(); }else{ mousePosition(QPoint(x,y)); } } void cutDialog::mouseReleaseEvent(QMouseEvent *) { m_mouse_down = false; update(); } void cutDialog::enterEvent(QEvent *) { emit lineEditEnable(false); } void cutDialog::leaveEvent(QEvent *) { emit lineEditEnable(true); } void cutDialog::paintEvent(QPaintEvent *) { QPainter painter(this); painter.translate(0,0); drawScale(painter); drawCutRect(painter); drawTransparentColor(painter); if(m_mouse_down){ if(sizeType == INCH || sizeType == MILLIM){ emit cutRectX(QString::number(getCutRectStartPos().x(),'f',2).toDouble()); emit cutRectY(QString::number(getCutRectStartPos().y(),'f',2).toDouble()); emit cutRectWidth(QString::number(getCutRectSize().width(),'f',2).toDouble()); emit cutRectHeight(QString::number(getCutRectSize().height(),'f',2).toDouble()); }else{ emit cutRectX(double(int(getCutRectStartPos().x()))); emit cutRectY(double(int(getCutRectStartPos().y()))); emit cutRectWidth(double(int(getCutRectSize().width()))); emit cutRectHeight(double(int(getCutRectSize().height()))); } } painter.end(); } void cutDialog::drawScale(QPainter& painter) { painter.setPen(QPen(Qt::black,1)); scaleRec = QRectF(0,0,paperWidth,paperHeight+0.5); painter.drawRect(scaleRec); for(int i = 6 ; i <= paperWidth-6; i++){ if(int(paperWidth/2) == i || int(paperWidth/4) == i || int(paperWidth*3/4) == i){ painter.setPen(QPen(Qt::gray,1)); painter.drawLine(i,0,i,paperHeight); painter.setPen(QPen(Qt::black,1)); } if(i%divisor == 0){ painter.drawLine(i,paperHeight-4,i,paperHeight); painter.drawLine(i,0,i,4); } if(i%(divisor*5) == 0){ painter.drawLine(i,paperHeight-8,i,paperHeight); painter.drawLine(i,0,i,8); } } for(int i = 6 ; i <= paperHeight-6; i++){ if(int(paperHeight/2) == i || int(paperHeight/4) == i || int(paperHeight*3/4) == i){ painter.setPen(QPen(Qt::gray,1)); painter.drawLine(0,i,paperWidth,i); painter.setPen(QPen(Qt::black,1)); } if(i%divisor == 0){ painter.drawLine(0,i,4,i); painter.drawLine(paperWidth,i,paperWidth-4,i); } if(i%(divisor*5) == 0){ painter.drawLine(0,i,8,i); painter.drawLine(paperWidth,i,paperWidth-8,i); } } } void cutDialog::drawCutRect(QPainter &painter) { painter.setPen(QPen(Qt::green,1)); m_rect = QRectF(m_startPoint,m_endPoint); QRectF r(m_rect); if(r.right() + 6 > paperWidth) r.setRight(paperWidth - 6); if(r.bottom() + 2 > paperHeight) r.setBottom(paperHeight - 2); painter.drawRect(r); painter.setPen(QPen(Qt::red,1)); painter.setBrush(Qt::red); painter.drawEllipse(r.left()-2,(r.bottom()-r.top())/2+r.top()-2,4,4); painter.drawEllipse(r.left()-2,r.top()-2,4,4); painter.drawEllipse(r.left()-2,r.bottom()-2,4,4); painter.drawEllipse(r.right()-2,r.top()-2,4,4); painter.drawEllipse(r.right()-2,r.bottom()-2,4,4); painter.drawEllipse(r.right()-2,(r.bottom()-r.top())/2+r.top()-2,4,4); painter.drawEllipse((r.right()-r.left())/2+r.left()-2,r.top()-2,4,4); painter.drawEllipse((r.right()-r.left())/2+r.left()-2,r.bottom()-2,4,4); } void cutDialog::drawTransparentColor(QPainter &painter) { QPainterPath painterPath; QPainterPath p; p.addRect(scaleRec); painterPath.addRect(m_rect); QPainterPath drawPath =p.subtracted(painterPath); painter.setOpacity(0.7); painter.fillPath(drawPath,QBrush(Qt::gray)); } void cutDialog::mousePosition(const QPoint& e) { initAllCorner(); QRectF r = QRectF(m_rect); int x = e.x(); int y = e.y(); m_left = qAbs(x - r.left()) < 5; m_right = qAbs(x - r.right()) < 5; m_bottom = qAbs(y - r.bottom()) < 5; m_top = qAbs(y - r.top()) < 5; m_out = r.left()-x>=5 || x-r.right()>=5 || r.top()-y>=5 || y-r.bottom()>=5; bool lorr = m_left | m_right; bool torb = m_top | m_bottom; if(lorr && torb) { if((m_left && m_top) || (m_right && m_bottom)) setCursor(Qt::SizeFDiagCursor); else setCursor(Qt::SizeBDiagCursor); } else if(lorr) setCursor(Qt::SizeHorCursor); else if(torb) setCursor(Qt::SizeVerCursor); else if(!m_out) { setCursor(Qt::SizeAllCursor); m_bottom = m_left = m_right = m_top = false; }else if(m_out){ setCursor(Qt::ArrowCursor); m_bottom = m_left = m_right = m_top = false; } if(m_left && m_top) m_leftTop = true; else if(m_left && m_bottom) m_leftBottom = true; else if(m_right && m_top) m_rightTop = true; else if(m_right && m_bottom) m_rightBottom = true; else if(m_left && !m_top && !m_bottom) m_leftCorn = true; else if(m_right && !m_top && !m_bottom) m_rightCorn = true; else if(m_top && !m_left && !m_right) m_topCorn = true; else if(m_bottom && !m_left && !m_right) m_bottomCorn = true; else initAllCorner(); } void cutDialog::refreshView() { if(MILLIM == sizeType || sizeType == INCH){ emit cutRectX(QString::number(getCutRectStartPos().x(),'f',2).toDouble()); emit cutRectY(QString::number(getCutRectStartPos().y(),'f',2).toDouble()); emit cutRectWidth(QString::number(getCutRectSize().width(),'f',2).toDouble()); emit cutRectHeight(QString::number(getCutRectSize().height(),'f',2).toDouble()); }else{ emit cutRectX(QString::number(getCutRectStartPos().x(),'f',0).toDouble()); emit cutRectY(QString::number(getCutRectStartPos().y(),'f',0).toDouble()); emit cutRectWidth(double(int(getCutRectSize().width()))); emit cutRectHeight(double(int(getCutRectSize().height()))); } } void cutDialog::initAllCorner() { m_left = false; m_right = false; m_bottom = false; m_top = false; m_leftCorn = false; m_rightCorn = false; m_topCorn = false; m_bottomCorn = false; m_leftTop = false; m_leftBottom = false; m_rightTop = false; m_rightBottom = false; }