capturewindow.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #include "capturewindow.h"
  2. #include "ui_capturewindow.h"
  3. CaptureWindow::CaptureWindow(QWidget* parent)
  4. : QMainWindow(parent)
  5. , ui(new Ui::CaptureWindow)
  6. {
  7. ui->setupUi(this);
  8. takeCentralWidget();
  9. setMouseTracking(true);
  10. setAttribute(Qt::WA_DeleteOnClose);
  11. m_mousedownPoint = QPoint(0, 0);
  12. m_mousedownFlag = false;
  13. }
  14. CaptureWindow::~CaptureWindow()
  15. {
  16. delete ui;
  17. }
  18. void CaptureWindow::paintEvent(QPaintEvent* event)
  19. {
  20. QPoint pt = mapFromGlobal(cursor().pos());
  21. qreal n = qApp->devicePixelRatio();
  22. QPainter painter;
  23. painter.begin(this);
  24. painter.drawPixmap(rect(), m_pix, m_pix.rect());
  25. painter.fillRect(rect(), QColor(0, 0, 0, 80));
  26. if (m_mousedownFlag == true)
  27. {
  28. painter.setPen(Qt::red);
  29. painter.setBrush(Qt::NoBrush);
  30. getSelectRect();
  31. m_pixRc.setRect(m_selectRc.left() * n, m_selectRc.top() * n, m_selectRc.width() * n, m_selectRc.height() * n);
  32. painter.drawPixmap(m_selectRc, m_pix, m_pixRc);
  33. painter.drawRect(m_selectRc);
  34. }
  35. else
  36. {
  37. painter.setPen(Qt::red);
  38. painter.drawLine(0, pt.y(), width(), pt.y());
  39. painter.drawLine(pt.x(), 0, pt.x(), height());
  40. }
  41. painter.end();
  42. }
  43. void CaptureWindow::mousePressEvent(QMouseEvent* event)
  44. {
  45. m_mousedownFlag = true;
  46. m_mousedownPoint = mapFromGlobal(cursor().pos());
  47. }
  48. void CaptureWindow::mouseReleaseEvent(QMouseEvent* event)
  49. {
  50. QMenu* menu = new QMenu(this);
  51. QStringList menuNames;
  52. if (event->button() == Qt::RightButton)
  53. {
  54. menuNames << "退出";
  55. }
  56. else
  57. {
  58. menuNames << "复制OCR文本到剪贴板";
  59. menuNames << "追加OCR文本到剪贴板";
  60. menuNames << "截图";
  61. menuNames << "取消";
  62. menuNames << "退出";
  63. }
  64. for (int i = 0; i < menuNames.count(); i++)
  65. {
  66. QAction* act = new QAction(menu);
  67. act->setText(menuNames[i]);
  68. connect(act, &QAction::triggered, this, &CaptureWindow::onMenuTriggered);
  69. menu->addAction(act);
  70. }
  71. QPoint pt = mapToGlobal(cursor().pos());
  72. menu->exec(pt);
  73. delete menu;
  74. m_mousedownFlag = false;
  75. update();
  76. }
  77. void CaptureWindow::mouseMoveEvent(QMouseEvent* event)
  78. {
  79. update();
  80. }
  81. void CaptureWindow::keyPressEvent(QKeyEvent* event)
  82. {
  83. if (event->key() == Qt::Key_Escape)
  84. {
  85. close();
  86. }
  87. }
  88. QPixmap CaptureWindow::pix() const
  89. {
  90. return m_pix;
  91. }
  92. void CaptureWindow::setPix(const QPixmap& pix)
  93. {
  94. m_pix = pix;
  95. update();
  96. }
  97. void CaptureWindow::getSelectRect()
  98. {
  99. QPoint pt = mapFromGlobal(cursor().pos());
  100. int x = m_mousedownPoint.x() < pt.x() ? m_mousedownPoint.x() : pt.x();
  101. int y = m_mousedownPoint.y() < pt.y() ? m_mousedownPoint.y() : pt.y();
  102. int w = abs(m_mousedownPoint.x() - pt.x());
  103. int h = abs(m_mousedownPoint.y() - pt.y());
  104. m_selectRc.setRect(x, y, w, h);
  105. }
  106. void CaptureWindow::onMenuTriggered(bool checked)
  107. {
  108. QAction* act = static_cast<QAction*>(sender());
  109. if (act->text() == "退出")
  110. {
  111. close();
  112. }
  113. else if (act->text() == "截图")
  114. {
  115. QPixmap clipPix = m_pix.copy(m_pixRc);
  116. qApp->clipboard()->setPixmap(clipPix);
  117. close();
  118. }
  119. else if (act->text() == "复制OCR文本到剪贴板")
  120. {
  121. QPixmap clipPix = m_pix.copy(m_pixRc);
  122. }
  123. else if (act->text() == "追加OCR文本到剪贴板")
  124. {
  125. QString txt = qApp->clipboard()->text();
  126. QPixmap clipPix = m_pix.copy(m_pixRc);
  127. qApp->clipboard()->setText(txt);
  128. }
  129. }
  130. void startShootScreen(bool hideWindow)
  131. {
  132. QWidget* activeWindow = qApp->activeWindow();
  133. if (hideWindow)
  134. {
  135. activeWindow->hide();
  136. }
  137. QElapsedTimer t;
  138. t.start();
  139. while (t.elapsed() < 500)
  140. {
  141. QApplication::processEvents();
  142. }
  143. QScreen* screen = QApplication::primaryScreen();
  144. while (t.elapsed() < 500)
  145. {
  146. QApplication::processEvents();
  147. }
  148. CaptureWindow* captureWindow = new CaptureWindow(nullptr);
  149. if (hideWindow)
  150. {
  151. activeWindow->show();
  152. }
  153. captureWindow->showFullScreen();
  154. }