Advertisement
BeamNG_IRC

Untitled

Sep 18th, 2015
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.75 KB | None | 0 0
  1. #include <QDebug>
  2. #include <QDir>
  3. #include <QLabel>
  4. #include <QRubberBand>
  5. #include <QPainter>
  6. #include <QPixmap>
  7. #include <QtGui>
  8. #include <QDesktopWidget>
  9. #include <QDateTime>
  10. #include <QNetworkAccessManager>
  11. #include <QNetworkRequest>
  12. #include <QNetworkReply>
  13. #include <QUrl>
  14. #include <QUrlQuery>
  15. #include "danshotmainwindow.h"
  16. #include "ui_danshotmainwindow.h"
  17.  
  18.  
  19. /*
  20.  *  My attempt at creating a screen grabbing application because I could not find one that would do what I wanted.
  21.  */
  22.  
  23.  
  24. DanShotMainWindow::DanShotMainWindow(QWidget *parent) :
  25.     QMainWindow(parent),
  26.     ui(new Ui::DanShotMainWindow) {
  27.     ui->setupUi(this);
  28.     /* We want this application to be full screen
  29.      * - this will allow the user to grab an area of the screen as if it was a live view
  30.      */
  31.     QMainWindow::showFullScreen(); // fullscreen our app... we need to make this multimonitor....
  32.     // here we are going to check if the directory that will store our images exists or not
  33.     QDir dir(QDir::homePath() + "/DanShot"); // inisitialise our directory
  34.     if (!dir.exists()) { // check if the folder exists
  35.         dir.mkpath(QDir::homePath() + "/DanShot"); // if it doesn't, create it
  36.         qDebug("Created directory ~/DanShot");
  37.     } else {
  38.         qDebug("~/DanShot exists");
  39.         CaptureAndDisplayImage(); // call the function that will take a screen shot and display it on screen
  40.     }
  41. }
  42.  
  43. DanShotMainWindow::~DanShotMainWindow() {
  44.     delete ui;
  45. }
  46.  
  47. class Selections { // this class will hold our x & y points
  48. public:
  49.     int TopLeftX;
  50.     int TopLeftY;
  51.     int TopRightX;
  52.     int TopRightY;
  53.     int BottomLeftX;
  54.     int BottomLeftY;
  55.     int BottomRightX;
  56.     int BottomRightY;
  57.     int Height;
  58.     int Width;
  59. } Selected;
  60.  
  61. void DanShotMainWindow::CaptureAndDisplayImage() { // this function will capture our screen and display it
  62.     QPixmap ScreenImage; // this will store our full sized screen shot... we are NOT saving that to file
  63.     QScreen *Screen = QGuiApplication::primaryScreen(); // declare Screen as our computers primary screen
  64.     ScreenImage = Screen->grabWindow(0); // take our screenshot
  65.     ui->ImagePreview->setPixmap(ScreenImage); // display our screenshot in the QLabel
  66.     ui->ImagePreviewScrollArea->setBackgroundRole(QPalette::Dark); // setup our scroll area
  67.     ui->ImagePreviewScrollArea->setWidget(ui->ImagePreview); // add our ImagePreview widget to the scroll area
  68.     setCentralWidget(ui->ImagePreviewScrollArea); // set our scorll area as the central widget
  69. }
  70.  
  71. void DanShotMainWindow::mousePressEvent(QMouseEvent *event) { // this function is run when the user holds their left mouse button down
  72.     RubberPoint = event->pos();
  73.     SelectionArea = new QRubberBand(QRubberBand::Rectangle, this); //new rectangle band
  74.     SelectionArea->setGeometry(QRect(RubberPoint, QSize()));
  75.     SelectionArea->show();
  76.  }
  77.  
  78.  void DanShotMainWindow::mouseMoveEvent(QMouseEvent *event) { // this function is run when the user moves their mouse
  79.     SelectionArea->setGeometry(QRect(RubberPoint, event->pos()).normalized()); // our selected area
  80.  }
  81.  
  82.  void DanShotMainWindow::mouseReleaseEvent(QMouseEvent *event) { // this function is run when the user lets og of their mouse button after selecting an area
  83.      /*
  84.       * This function will initiate our upload
  85.       *  - TAKE NEW SNAPSHOT OF THE DESKTOP AND USE THAT WITH OUR X,Y POSITIONS TO GET THE AREA SELECTED - GENIUS!
  86.       */
  87.     SelectionArea->hide(); // hide our selection area
  88.     /*
  89.      * Set a bunch of positional variables
  90.      * - corner x pos, corner y pos
  91.      */
  92.     Selected.TopLeftX = QRect(RubberPoint, event->pos()).topLeft().x();
  93.     Selected.TopLeftY = QRect(RubberPoint, event->pos()).topLeft().y();
  94.     Selected.Height = QRect(RubberPoint, event->pos()).height();
  95.     Selected.Width = QRect(RubberPoint, event->pos()).width();
  96.  
  97.  
  98.     Selected.TopRightX = QRect(RubberPoint, event->pos()).topRight().x();
  99.     Selected.TopRightY = QRect(RubberPoint, event->pos()).topRight().y();
  100.  
  101.     Selected.BottomLeftX = QRect(RubberPoint, event->pos()).bottomLeft().x();
  102.     Selected.BottomLeftY = QRect(RubberPoint, event->pos()).bottomLeft().y();
  103.  
  104.     Selected.BottomRightX = QRect(RubberPoint, event->pos()).bottomRight().x();
  105.     Selected.BottomRightY = QRect(RubberPoint, event->pos()).bottomRight().y();
  106.  
  107.     /* Basically, we are now ready to take our new screenshot
  108.      * - this WILL be saved to ~/DanShot
  109.      */
  110.     QPixmap ScreenImageToSave; // this will store our full sized screen shot... we are NOT saving that to file
  111.     QScreen *Screen = QGuiApplication::primaryScreen(); // declare Screen as our computers primary screen
  112.     ScreenImageToSave = Screen->grabWindow(0, Selected.TopLeftX, Selected.TopLeftY, Selected.Width, Selected.Height); // take our screenshot
  113.     FileName = QDir::homePath() + "/DanShot/DanShot_" + QTime::currentTime().toString() + ".PNG";
  114.     ScreenImageToSave.save(FileName); // save our file
  115.     UploadImage(); // call our upload functions
  116.  }
  117.  
  118.  void DanShotMainWindow::UploadImage() {
  119.      /* We are now ready to upload
  120.       * - upload to http://danieljon.es/uploader/upload_sharex.php
  121.       */
  122.  
  123.  }
  124.  
  125.  
  126.  /* WE DO NOT ACTUALLY NEED THIS, IT IS JUST COOL
  127.  void DanShotMainWindow::paintEvent(QPaintEvent *) {
  128.      QPainter painter(this); // <-- you need to comment line 63 for this to work
  129.      //qDebug() << Selected.TopLeftX << Selected.TopLeftY << Selected.TopRightX << Selected.TopRightY << Selected.BottomLeftX << Selected.BottomLeftY << Selected.BottomRightX << Selected.BottomRightY;
  130.      QBrush Brush(Qt::green, Qt::DiagCrossPattern);
  131.      QRect Area(Selected.TopLeftX, Selected.TopLeftY, Selected.Width, Selected.Height);
  132.      painter.setBrush(Brush);
  133.      painter.setPen(QPen(Qt::red, 3));
  134.      painter.drawRect(Area);
  135.  
  136.  }
  137. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement