mainwindow.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //
  2. // Created by egonr on 2/11/2023.
  3. //
  4. // You may need to build the project (run Qt uic code generator) to get "ui_MainWindow.h" resolved
  5. #include "mainwindow.h"
  6. #include "ui_MainWindow.h"
  7. #include "windows.h"
  8. #include "../util/ScreenshotUtil.h"
  9. #include "qpictureview.h"
  10. #include <QScreen>
  11. #include <QPainter>
  12. #include <QRgb>
  13. #include <QColorSpace>
  14. #include <QDateTime>
  15. #include <QFileDialog>
  16. #include <QBuffer>
  17. #include "util/Version.h"
  18. MainWindow::MainWindow(QWidget *parent) :
  19. QWidget(parent), ui(new Ui::MainWindow) {
  20. ui->setupUi(this);
  21. setWindowTitle(QString("wincap11 (v%1.%2-%3)").arg(wincap11_VERSION_MAJOR).arg(wincap11_VERSION_MINOR).arg(wincap11_GIT_HASH));
  22. pictureView = QSharedPointer<QPictureView>(new QPictureView(ui->pictureViewContainer));
  23. pictureView->show();
  24. connect(ui->captureButton, SIGNAL(clicked()), this, SLOT(handleCaptureButton()));
  25. connect(&captureTimer, SIGNAL(timeout()), this, SLOT(captureTimerTimeout()));
  26. connect(ui->saveButton, SIGNAL(clicked()), this, SLOT(handleSaveButton()));
  27. }
  28. MainWindow::~MainWindow() {
  29. delete ui;
  30. }
  31. void MainWindow::handleSaveButton() {
  32. QString suggestedName = "Screenshot_" + currentScreenshotTime.toString("yyyyMMdd_HHmmss_zzz") + ".png";
  33. QString saveFileName = QFileDialog::getSaveFileName(this, tr("Save File"), suggestedName);
  34. currentScreenshot->save(saveFileName);
  35. }
  36. void MainWindow::handleCaptureButton() {
  37. SetUIStateCapturing();
  38. this->captureTimer.start(ui->delayBox->value());
  39. }
  40. void MainWindow::captureTimerTimeout() {
  41. ScreenshotUtil screenshotUtil;
  42. currentScreenshot = QSharedPointer<QImage>(screenshotUtil.CaptureForegroundWindow(WINDOWS_11));
  43. currentScreenshotTime = QDateTime::currentDateTime();
  44. pictureView->SetImage(currentScreenshot.get());
  45. this->captureTimer.stop();
  46. ui->delayBox->setValue(captureTimer.interval());
  47. SetUIStateCaptureFinished();
  48. }
  49. void MainWindow::SetUIStateCapturing() {
  50. ui->leftStatusLabel->setText("Capturing...");
  51. ui->rightStatusLabel->setText("");
  52. ui->captureButton->setEnabled(false);
  53. }
  54. void MainWindow::SetUIStateCaptureFinished() {
  55. // window to front
  56. raise();
  57. activateWindow();
  58. QBuffer tmpBuffer;
  59. currentScreenshot->save(&tmpBuffer, "PNG");
  60. ui->leftStatusLabel->setText(
  61. QString("%1 x %2").arg(currentScreenshot->width()).arg(currentScreenshot->height())
  62. );
  63. ui->rightStatusLabel->setText(
  64. QString("%1 (PNG)").arg(locale().formattedDataSize(tmpBuffer.size(), 1))
  65. );
  66. ui->captureButton->setEnabled(true);
  67. ui->saveButton->setEnabled(true);
  68. ui->saveButton->setFocus();
  69. }
  70. void MainWindow::paintEvent(QPaintEvent *event) {
  71. if(this->captureTimer.isActive()) {
  72. ui->delayBox->setValue(this->captureTimer.remainingTime());
  73. }
  74. QWidget::paintEvent(event);
  75. }