#include "paperedithandle.h" #include "qpainter.h" #include #include #include #include #define painter_offset 46 CPaperEditHandle::CPaperEditHandle(QWidget *parent) : QWidget(parent), m_room(1.0f), m_offset(QPoint(0, 0)), m_mousestatus(Mouse_Normal), m_bpressed(false), m_startPoint(QPoint(0, 0)), m_endPoint(QPoint(0, 0)), m_pBatchInfo(nullptr), m_pPapers(nullptr), m_nCurrentPaper(0), m_locateW(0), m_locateH(0), m_bLocateChecked(false), m_bEraserChecked(false) { ui.setupUi(this); ui.widget_eraser->hide(); ui.widget_locate->hide(); ClickDrag(); connect(ui.toolButton_drag, SIGNAL(clicked()), this, SLOT(slot_onButtonClick())); connect(ui.toolButton_roomin, SIGNAL(clicked()), this, SLOT(slot_onButtonClick())); connect(ui.toolButton_rommout, SIGNAL(clicked()), this, SLOT(slot_onButtonClick())); connect(ui.toolButton_90, SIGNAL(clicked()), this, SLOT(slot_onButtonClick())); connect(ui.toolButton_N90, SIGNAL(clicked()), this, SLOT(slot_onButtonClick())); connect(ui.toolButton_locate, SIGNAL(clicked()), this, SLOT(slot_onButtonClick())); connect(ui.toolButton_eraser, SIGNAL(clicked()), this, SLOT(slot_onButtonClick())); connect(ui.toolButton_switchpage, SIGNAL(clicked()), this, SLOT(slot_onButtonClick())); connect(ui.toolButton_undo, SIGNAL(clicked()), this, SLOT(slot_onButtonClick())); connect(ui.toolButton_restore, SIGNAL(clicked()), this, SLOT(slot_onButtonClick())); connect(ui.toolButton_locate1, SIGNAL(clicked()), this, SLOT(slot_onButtonClick())); connect(ui.toolButton_locate2, SIGNAL(clicked()), this, SLOT(slot_onButtonClick())); connect(ui.toolButton_locate3, SIGNAL(clicked()), this, SLOT(slot_onButtonClick())); connect(ui.toolButton_locate4, SIGNAL(clicked()), this, SLOT(slot_onButtonClick())); connect(ui.toolButton_eraser1, SIGNAL(clicked()), this, SLOT(slot_onButtonClick())); connect(ui.toolButton_eraser2, SIGNAL(clicked()), this, SLOT(slot_onButtonClick())); connect(ui.toolButton_eraser3, SIGNAL(clicked()), this, SLOT(slot_onButtonClick())); connect(ui.toolButton_page0, SIGNAL(clicked()), this, SLOT(slot_onButtonClick())); connect(ui.toolButton_page1, SIGNAL(clicked()), this, SLOT(slot_onButtonClick())); connect(ui.toolButton_paper_pre, SIGNAL(clicked()), this, SLOT(slot_onButtonClick())); connect(ui.toolButton_paper_next, SIGNAL(clicked()), this, SLOT(slot_onButtonClick())); ui.widget_painter->m_bgimage = &m_bgimage; ui.widget_painter->m_offset = &m_offset; ui.widget_painter->m_room = &m_room; this->setMouseTracking(true);// 追踪鼠标 //初始化定位点映射map m_locatePointMap[ui.toolButton_locate] = QPoint(0, 0); m_locatePointMap[ui.toolButton_locate1] = QPoint(21, 10); m_locatePointMap[ui.toolButton_locate2] = QPoint(40, 20); m_locatePointMap[ui.toolButton_locate3] = QPoint(50, 28); m_locatePointMap[ui.toolButton_locate4] = QPoint(60, 30); } CPaperEditHandle::~CPaperEditHandle() { } void CPaperEditHandle::slot_onButtonClick() { ui.widget_locate->hide(); ui.widget_eraser->hide(); QObject* sender = this->sender(); if (sender == ui.toolButton_drag) { ClickDrag(); } else if (sender == ui.toolButton_roomin) { ClickRoomin(); } else if (sender == ui.toolButton_rommout) { ClickRoomout(); } else if (sender == ui.toolButton_90) { ClickRotate90(); } else if (sender == ui.toolButton_N90) { ClickRotateN90(); } else if (m_locatePointMap.count(sender) == 1) { ClickLocate(sender); } else if (sender == ui.toolButton_eraser || sender == ui.toolButton_eraser1 || sender == ui.toolButton_eraser2 || sender == ui.toolButton_eraser3) { ClickEraser(sender); } else if (sender == ui.toolButton_switchpage) { ClickSwitchPage(); } else if (sender == ui.toolButton_undo) { ClickUndo(); } else if (sender == ui.toolButton_restore) { ClickRestore(); } else if (sender == ui.toolButton_page0 || sender == ui.toolButton_page1) { if (!m_pPapers || (*m_pPapers).size() == 0 || m_nCurrentPaper >= (*m_pPapers).size()) { return; } if (sender == ui.toolButton_page0) { m_nCurrentPage = 0; } else { m_nCurrentPage = 1; } m_bgimage = QImage(m_mapStackPage[m_nCurrentPage].top().c_str()); CalcRoom(); update(); } else if (sender == ui.toolButton_paper_next || sender == ui.toolButton_paper_pre) { if (!m_pPapers || (*m_pPapers).size() == 0 || m_nCurrentPaper >= (*m_pPapers).size()) { return; } if (sender == ui.toolButton_paper_pre) { if (m_nCurrentPaper == 0) { return; } m_nCurrentPaper--; } else { if (m_nCurrentPaper + 1 == (*m_pPapers).size()) { return; } m_nCurrentPaper++; } ui.label_total_count->setText(QString::fromStdWString(L"%1/%2").arg(m_nCurrentPaper + 1).arg((*m_pPapers).size())); ui.toolButton_page0->setChecked(true); m_nCurrentPage = 0; m_bgimage = QImage((*m_pPapers)[m_nCurrentPaper].page0_path); CalcRoom(); m_mapStackPage.clear(); m_mapStackPage[0].push_back((*m_pPapers)[m_nCurrentPaper].page0_path); m_mapStackPage[1].push_back((*m_pPapers)[m_nCurrentPaper].page1_path); update(); } } void CPaperEditHandle::ClickDrag() { if (ui.toolButton_drag->isChecked()) { m_mousestatus = Mouse_Drag; } else { m_mousestatus = Mouse_Normal; } setCursor(Qt::SizeAllCursor); m_bLocateChecked = false; m_bEraserChecked = false; } void CPaperEditHandle::ClickRoomin() { m_offset.setX(0); m_offset.setY(0); m_room *= 1.1f; update(); //if (mousestatus == Mouse_Locate) //{ // QPixmap pixmap((locateW + 1)*room, (locateH + 1)*room); // QPainter painterim(&pixmap); // painterim.setRenderHint(QPainter::Antialiasing); // pixmap.fill(Qt::black); //填充1像素值 // painterim.drawRect(0, 0, (locateW + 1)*room, (locateH + 1)*room); // QCursor cursor(pixmap, 0, 0); //加载xpm生成的图标文件 // setCursor(cursor); //设置自定义的鼠标样式 //} } void CPaperEditHandle::ClickRoomout() { m_offset.setX(0); m_offset.setY(0); m_room /= 1.1f; update(); //if (mousestatus == Mouse_Locate) //{ // QPixmap pixmap((locateW + 1)*room, (locateH + 1)*room); // QPainter painterim(&pixmap); // painterim.setRenderHint(QPainter::Antialiasing); // pixmap.fill(Qt::black); //填充1像素值 // painterim.drawRect(0, 0, (locateW + 1)*room, (locateH + 1)*room); // QCursor cursor(pixmap, 0, 0); //加载xpm生成的图标文件 // setCursor(cursor); //设置自定义的鼠标样式 //} } void CPaperEditHandle::ClickRotate90() { m_offset.setX(0); m_offset.setY(0); QMatrix matrix; matrix.rotate(90.0); m_bgimage = m_bgimage.transformed(matrix, Qt::FastTransformation); update(); SaveTempImg(); /*if (!ui.toolButton_cur->isChecked()) { return; } m_bIsEdited = true; SaveTempImg(&bgimage);*/ } void CPaperEditHandle::ClickRotateN90() { m_offset.setX(0); m_offset.setY(0); QMatrix matrix; matrix.rotate(-90.0); m_bgimage = m_bgimage.transformed(matrix, Qt::FastTransformation); update(); SaveTempImg(); /*if (!ui.toolButton_cur->isChecked()) { return; } m_bIsEdited = true; SaveTempImg(&bgimage);*/ } void CPaperEditHandle::ClickLocate(QObject* sender) { int locX, locY; if (sender == ui.toolButton_locate) { m_bEraserChecked = false; if (ui.toolButton_locate->isChecked() && m_bLocateChecked == false) { m_mousestatus = Mouse_Locate; m_bLocateChecked = true; ui.widget_locate->show(); QObject * tObj = ui.toolButton_locate1->isChecked() ? ui.toolButton_locate1 : ui.toolButton_locate2; tObj = ui.toolButton_locate3->isChecked() ? ui.toolButton_locate3 : tObj; locX = m_locatePointMap[tObj].x(); locY = m_locatePointMap[sender].y(); } else { /*m_mousestatus = Mouse_Normal; ui.widget_locate->hide();*/ m_bLocateChecked = false; ui.toolButton_drag->setChecked(true); ClickDrag(); return; } } else { locX = m_locatePointMap[sender].x(); locY = m_locatePointMap[sender].y(); } double pdiX = m_bgimage.dotsPerMeterX(); double pdiY = m_bgimage.dotsPerMeterY(); int x = qRound(pdiX*0.0254); int y = qRound(pdiY*0.0254); m_locateW = locX * (x / 96); m_locateH = locY * (y / 96); QPixmap pixmap((m_locateW + 1)*m_room, (m_locateH + 1)*m_room); QPainter painterim(&pixmap); painterim.setRenderHint(QPainter::Antialiasing); pixmap.fill(Qt::black); //填充1像素值 painterim.drawRect(0, 0, (m_locateW + 1)*m_room, (m_locateH + 1)*m_room); QCursor cursor(pixmap, 0, 0); //加载xpm生成的图标文件 setCursor(cursor); //设置自定义的鼠标样式 } void CPaperEditHandle::ClickEraser(QObject* sender) { if (sender == ui.toolButton_eraser) { m_bLocateChecked = false; if (ui.toolButton_eraser->isChecked() && m_bEraserChecked == false) { m_mousestatus = Mouse_Eraser; m_bEraserChecked = true; ui.widget_eraser->show(); } else { /*m_mousestatus = Mouse_Normal; ui.widget_eraser->hide();*/ m_bEraserChecked = false; ui.toolButton_drag->setChecked(true); ClickDrag(); return; } m_eraserW = 17; m_eraserH = 13; } else if (sender == ui.toolButton_eraser1) { m_eraserW = 13; m_eraserH = 9; } else if (sender == ui.toolButton_eraser2) { m_eraserW = 17; m_eraserH = 13; } else if (sender == ui.toolButton_eraser3) { m_eraserW = 23; m_eraserH = 17; } QPixmap pixmap(m_eraserW + 1, m_eraserH + 1); QPainter painterim(&pixmap); painterim.setRenderHint(QPainter::Antialiasing); pixmap.fill(Qt::white); //填充1像素值 painterim.drawRect(0, 0, m_eraserW + 1, m_eraserH + 1); QCursor cursor(pixmap, 0, 0); //加载xpm生成的图标文件 setCursor(cursor); //设置自定义的鼠标样式 } void CPaperEditHandle::ClickSwitchPage() { QString msg = QString::fromLocal8Bit("换页后,将清空所有对图片的修改,"); msg += QString::fromLocal8Bit("确定要换页吗?"); QMessageBox::StandardButton result = QMessageBox::information(NULL, QString::fromLocal8Bit("提示"), msg, QMessageBox::Yes | QMessageBox::No); if (result == QMessageBox::Yes) { if (!m_pPapers || (*m_pPapers).size() == 0 || m_nCurrentPaper >= (*m_pPapers).size()) { return; } string strPath0 = (*m_pPapers)[m_nCurrentPaper].page0_path; string strPath1 = (*m_pPapers)[m_nCurrentPaper].page1_path; strcpy_s((*m_pPapers)[m_nCurrentPaper].page0_path, strPath1.c_str()); strcpy_s((*m_pPapers)[m_nCurrentPaper].page1_path, strPath0.c_str()); m_bgimage = QImage((*m_pPapers)[m_nCurrentPaper].page0_path); m_mapStackPage.clear(); m_mapStackPage[0].push_back((*m_pPapers)[m_nCurrentPaper].page0_path); m_mapStackPage[1].push_back((*m_pPapers)[m_nCurrentPaper].page1_path); ui.toolButton_page0->click(); } } void CPaperEditHandle::ClickUndo() { if (m_mapStackPage[m_nCurrentPage].size() == 1) // 只有原始试卷的路径 { return; } m_mapStackPage[m_nCurrentPage].pop(); m_bgimage = QImage(m_mapStackPage[m_nCurrentPage].top().c_str()); update(); } void CPaperEditHandle::ClickRestore() { QString msg = QString::fromLocal8Bit("还原后,将清空所有对图片的修改,"); while (m_mapStackPage[m_nCurrentPage].size() > 1) { m_mapStackPage[m_nCurrentPage].pop(); } m_bgimage = QImage(m_mapStackPage[m_nCurrentPage].top().c_str()); update(); } void CPaperEditHandle::resizeEvent(QResizeEvent *event) { QRect rcClient = geometry(); QRect rcTool = rcClient; rcTool.setBottom(rcTool.top() + ui.widget_tool->height()); ui.widget_tool->setGeometry(rcTool); QRect rcLocate = ui.toolButton_locate->geometry(); rcLocate.setTop(rcLocate.bottom() + 2); rcLocate.setBottom(rcLocate.top() + 44); rcLocate.setLeft(245); rcLocate.setRight(rcLocate.left() + 211); ui.widget_locate->setGeometry(rcLocate); QRect rcEraser = ui.toolButton_eraser->geometry(); rcEraser.setTop(rcEraser.bottom() + 2); rcEraser.setBottom(rcEraser.top() + 44); rcEraser.setLeft(297); rcEraser.setRight(rcEraser.left() + 162); ui.widget_eraser->setGeometry(rcEraser); QRect rcLeft = ui.toolButton_drag->geometry(); rcLeft.setTop(rcLeft.bottom() + 2); rcLeft.setBottom(rcLeft.top() + ui.widget_leftbutton->height()); rcLeft.setRight(rcLeft.x() + ui.widget_leftbutton->width()); ui.widget_leftbutton->setGeometry(rcLeft); QRect rcRight = rcTool; rcRight.setTop(rcRight.bottom() + 2); rcRight.setBottom(rcRight.top() + ui.widget_rightbutton->height()); rcRight.setLeft(rcRight.x() + ui.widget_tool->width() - ui.widget_rightbutton->width()); ui.widget_rightbutton->setGeometry(rcRight); QRect rcPainter = rcClient; rcPainter.setLeft(painter_offset); rcPainter.setTop(painter_offset); rcPainter.setWidth(rcClient.width() - painter_offset * 2); rcPainter.setHeight(rcClient.height() - painter_offset); ui.widget_painter->setGeometry(rcPainter); } QPoint CPaperEditHandle::PointSwitchToImg(QPoint sPoint) { sPoint.setX(sPoint.x() - painter_offset); sPoint.setY(sPoint.y() - painter_offset); int x = sPoint.x() - m_offset.x(); x = x / m_room; int y = sPoint.y() - m_offset.y(); y = y / m_room; return QPoint(x, y); } QRect CPaperEditHandle::RectSwitchImg(QRect sRect) { QPoint lefttop = PointSwitchToImg(QPoint(sRect.left(), sRect.top())); QPoint rightbottom = PointSwitchToImg(QPoint(sRect.right(), sRect.bottom())); return QRect(lefttop, rightbottom); } void CPaperEditHandle::SaveTempImg() { QDateTime currentTime = QDateTime::currentDateTime(); char path[512] = { 0 }; sprintf(path, "%stemp/%s.jpg", m_pBatchInfo->work_dir, currentTime.toString("yyyyMMddhhmmsszzz").toStdString().c_str()); m_bgimage.save(path, "JPG", 255); m_mapStackPage[m_nCurrentPage].push(path); } void CPaperEditHandle::mouseMoveEvent(QMouseEvent *event) { QPoint pos(event->pos()); QPainter painterim(&m_bgimage); painterim.setRenderHint(QPainter::Antialiasing); if (m_bpressed) { if (m_mousestatus == Mouse_Eraser) { painterim.eraseRect(RectSwitchImg(QRect(pos.x(), pos.y(), m_eraserW, m_eraserH))); update(); SaveTempImg(); } else if (m_mousestatus == Mouse_Drag) { m_endPoint = pos; m_offset.setX(m_endPoint.x() - m_startPoint.x() + m_offset.x()); m_offset.setY(m_endPoint.y() - m_startPoint.y() + m_offset.y()); m_startPoint = m_endPoint; QRect rcClient = ui.widget_painter->geometry(); if (m_bgimage.width() * m_room <= rcClient.width()) //不能拖动 { m_offset.setX(0); } else { int offxMax = 0, offxMin = rcClient.width() - m_bgimage.width() * m_room; if (m_offset.x() < offxMin) { m_offset.setX(offxMin); } else if (m_offset.x() > offxMax) { m_offset.setX(offxMax); } } if (m_bgimage.height()*m_room <= rcClient.height()) //不能拖动 { m_offset.setY(0); } else { int offyMax = 0, offyMin = rcClient.height() - m_bgimage.height()*m_room; if (m_offset.y() < offyMin) { m_offset.setY(offyMin); } else if (m_offset.y() > offyMax) { m_offset.setY(offyMax); } } update(); } } } void CPaperEditHandle::mouseReleaseEvent(QMouseEvent *event) { QPoint pos(event->pos()); m_bpressed = false; if (m_mousestatus == Mouse_Eraser) { QPainter painterim(&m_bgimage); painterim.setRenderHint(QPainter::Antialiasing); painterim.eraseRect(RectSwitchImg(QRect(pos.x(), pos.y(), m_eraserW, m_eraserH))); SaveTempImg(); update(); } else if (m_mousestatus == Mouse_Locate) { QPainter painterim(&m_bgimage); painterim.setRenderHint(QPainter::SmoothPixmapTransform); QPoint pt = PointSwitchToImg(pos); QRect rc(pt.x(), pt.y(), m_locateW, m_locateH); QRect target = rc; painterim.setBrush(Qt::black); painterim.drawRect(target); SaveTempImg(); update(); } } void CPaperEditHandle::mousePressEvent(QMouseEvent *event) { ui.widget_locate->hide(); ui.widget_eraser->hide(); QPoint pos(event->pos()); m_startPoint = pos; m_endPoint = pos; /*if (!ui.widget_Drag->isVisible()) { ispressed = true; }*/ m_bpressed = true; } void CPaperEditHandle::paintEvent(QPaintEvent *e) { /*QRect rc(this->rect()); QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing); painter.translate(m_offset.x() + 50, m_offset.y() + 50); painter.scale(m_room, m_room); painter.drawImage(QPoint(0, 0), m_bgimage);*/ } void CPaperEditHandle::setCurrentBatch(batch_info* batchInfo, vector* papers) { m_pBatchInfo = batchInfo; m_pPapers = papers; m_nCurrentPaper = 0; m_nCurrentPage = 0; m_room = 1.0f; m_locateW = 0; m_locateH = 0; m_offset = QPoint(0, 0); ui.toolButton_drag->setChecked(true); ui.widget_locate->hide(); ui.widget_eraser->hide(); ClickDrag(); m_mapStackPage.clear(); if (!m_pPapers || (*m_pPapers).size() == 0) { return; } QString temp_path(m_pBatchInfo->work_dir); temp_path.append("temp"); QDir dir(temp_path); if (!dir.exists()) { dir.mkpath(temp_path); } ui.label_total_count->setText(QString::fromStdWString(L"%1/%2").arg(m_nCurrentPaper + 1).arg((*m_pPapers).size())); m_bgimage = QImage((*m_pPapers)[m_nCurrentPaper].page0_path); CalcRoom(); // m_stackImage.push_back(m_bgimage); m_mapStackPage[0].push_back((*m_pPapers)[m_nCurrentPaper].page0_path); m_mapStackPage[1].push_back((*m_pPapers)[m_nCurrentPaper].page1_path); update(); } void CPaperEditHandle::GetCurrentPaper(paper_info& paperInfo) { if (!m_pPapers || (*m_pPapers).size() == 0 || m_nCurrentPaper >= (*m_pPapers).size() || m_mapStackPage.size() == 0) { return; } paperInfo.id = (*m_pPapers)[m_nCurrentPaper].id; strcpy_s(paperInfo.page0_path, m_mapStackPage[0].top().c_str()); strcpy_s(paperInfo.page1_path, m_mapStackPage[1].top().c_str()); } void CPaperEditHandle::CalcRoom() { if (m_bgimage.width() == 0 || m_bgimage.height() == 0) { return; } QRect rcClient = geometry(); int _width = rcClient.width() - 2 * painter_offset; int _height = rcClient.height() - painter_offset; if (m_bgimage.width() > _width || m_bgimage.height() > _height) { m_room = min(_width / (float)m_bgimage.width(), _height / (float)m_bgimage.height()); } } void CPaperEditHandle::wheelEvent(QWheelEvent *event) // 滚轮事件 { /*if (QApplication::keyboardModifiers() == Qt::ControlModifier)*/ { if (event->delta() > 0) { // 当滚轮远离使用者时 ClickRoomin(); // 进行放大 } else { // 当滚轮向使用者方向旋转时 ClickRoomout(); // 进行缩小 } } }