Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <QDebug>
- #include <QDir>
- #include <QLabel>
- #include <QRubberBand>
- #include <QPainter>
- #include <QPixmap>
- #include <QtGui>
- #include <QDesktopWidget>
- #include <QDateTime>
- #include <QNetworkAccessManager>
- #include <QNetworkRequest>
- #include <QNetworkReply>
- #include <QUrl>
- #include <QUrlQuery>
- #include "danshotmainwindow.h"
- #include "ui_danshotmainwindow.h"
- /*
- * My attempt at creating a screen grabbing application because I could not find one that would do what I wanted.
- */
- DanShotMainWindow::DanShotMainWindow(QWidget *parent) :
- QMainWindow(parent),
- ui(new Ui::DanShotMainWindow) {
- ui->setupUi(this);
- /* We want this application to be full screen
- * - this will allow the user to grab an area of the screen as if it was a live view
- */
- QMainWindow::showFullScreen(); // fullscreen our app... we need to make this multimonitor....
- // here we are going to check if the directory that will store our images exists or not
- QDir dir(QDir::homePath() + "/DanShot"); // inisitialise our directory
- if (!dir.exists()) { // check if the folder exists
- dir.mkpath(QDir::homePath() + "/DanShot"); // if it doesn't, create it
- qDebug("Created directory ~/DanShot");
- } else {
- qDebug("~/DanShot exists");
- CaptureAndDisplayImage(); // call the function that will take a screen shot and display it on screen
- }
- }
- DanShotMainWindow::~DanShotMainWindow() {
- delete ui;
- }
- class Selections { // this class will hold our x & y points
- public:
- int TopLeftX;
- int TopLeftY;
- int TopRightX;
- int TopRightY;
- int BottomLeftX;
- int BottomLeftY;
- int BottomRightX;
- int BottomRightY;
- int Height;
- int Width;
- } Selected;
- void DanShotMainWindow::CaptureAndDisplayImage() { // this function will capture our screen and display it
- QPixmap ScreenImage; // this will store our full sized screen shot... we are NOT saving that to file
- QScreen *Screen = QGuiApplication::primaryScreen(); // declare Screen as our computers primary screen
- ScreenImage = Screen->grabWindow(0); // take our screenshot
- ui->ImagePreview->setPixmap(ScreenImage); // display our screenshot in the QLabel
- ui->ImagePreviewScrollArea->setBackgroundRole(QPalette::Dark); // setup our scroll area
- ui->ImagePreviewScrollArea->setWidget(ui->ImagePreview); // add our ImagePreview widget to the scroll area
- setCentralWidget(ui->ImagePreviewScrollArea); // set our scorll area as the central widget
- }
- void DanShotMainWindow::mousePressEvent(QMouseEvent *event) { // this function is run when the user holds their left mouse button down
- RubberPoint = event->pos();
- SelectionArea = new QRubberBand(QRubberBand::Rectangle, this); //new rectangle band
- SelectionArea->setGeometry(QRect(RubberPoint, QSize()));
- SelectionArea->show();
- }
- void DanShotMainWindow::mouseMoveEvent(QMouseEvent *event) { // this function is run when the user moves their mouse
- SelectionArea->setGeometry(QRect(RubberPoint, event->pos()).normalized()); // our selected area
- }
- void DanShotMainWindow::mouseReleaseEvent(QMouseEvent *event) { // this function is run when the user lets og of their mouse button after selecting an area
- /*
- * This function will initiate our upload
- * - TAKE NEW SNAPSHOT OF THE DESKTOP AND USE THAT WITH OUR X,Y POSITIONS TO GET THE AREA SELECTED - GENIUS!
- */
- SelectionArea->hide(); // hide our selection area
- /*
- * Set a bunch of positional variables
- * - corner x pos, corner y pos
- */
- Selected.TopLeftX = QRect(RubberPoint, event->pos()).topLeft().x();
- Selected.TopLeftY = QRect(RubberPoint, event->pos()).topLeft().y();
- Selected.Height = QRect(RubberPoint, event->pos()).height();
- Selected.Width = QRect(RubberPoint, event->pos()).width();
- Selected.TopRightX = QRect(RubberPoint, event->pos()).topRight().x();
- Selected.TopRightY = QRect(RubberPoint, event->pos()).topRight().y();
- Selected.BottomLeftX = QRect(RubberPoint, event->pos()).bottomLeft().x();
- Selected.BottomLeftY = QRect(RubberPoint, event->pos()).bottomLeft().y();
- Selected.BottomRightX = QRect(RubberPoint, event->pos()).bottomRight().x();
- Selected.BottomRightY = QRect(RubberPoint, event->pos()).bottomRight().y();
- /* Basically, we are now ready to take our new screenshot
- * - this WILL be saved to ~/DanShot
- */
- QPixmap ScreenImageToSave; // this will store our full sized screen shot... we are NOT saving that to file
- QScreen *Screen = QGuiApplication::primaryScreen(); // declare Screen as our computers primary screen
- ScreenImageToSave = Screen->grabWindow(0, Selected.TopLeftX, Selected.TopLeftY, Selected.Width, Selected.Height); // take our screenshot
- FileName = QDir::homePath() + "/DanShot/DanShot_" + QTime::currentTime().toString() + ".PNG";
- ScreenImageToSave.save(FileName); // save our file
- UploadImage(); // call our upload functions
- }
- void DanShotMainWindow::UploadImage() {
- /* We are now ready to upload
- * - upload to http://danieljon.es/uploader/upload_sharex.php
- */
- }
- /* WE DO NOT ACTUALLY NEED THIS, IT IS JUST COOL
- void DanShotMainWindow::paintEvent(QPaintEvent *) {
- QPainter painter(this); // <-- you need to comment line 63 for this to work
- //qDebug() << Selected.TopLeftX << Selected.TopLeftY << Selected.TopRightX << Selected.TopRightY << Selected.BottomLeftX << Selected.BottomLeftY << Selected.BottomRightX << Selected.BottomRightY;
- QBrush Brush(Qt::green, Qt::DiagCrossPattern);
- QRect Area(Selected.TopLeftX, Selected.TopLeftY, Selected.Width, Selected.Height);
- painter.setBrush(Brush);
- painter.setPen(QPen(Qt::red, 3));
- painter.drawRect(Area);
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement