theshell/screenshotwindow.cpp

229 lines
8.6 KiB
C++
Raw Normal View History

2017-08-05 15:20:02 +10:00
/****************************************
*
* theShell - Desktop Environment
* Copyright (C) 2017 Victor Tran
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* *************************************/
2016-12-20 12:18:23 +11:00
#include "screenshotwindow.h"
#include "ui_screenshotwindow.h"
2017-04-13 12:17:43 +10:00
extern float getDPIScaling();
2016-12-20 12:18:23 +11:00
screenshotWindow::screenshotWindow(QWidget *parent) :
QDialog(parent),
ui(new Ui::screenshotWindow)
{
ui->setupUi(this);
ui->discardButton->setProperty("type", "destructive");
this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
this->setAttribute(Qt::WA_TranslucentBackground);
ui->label->setParent(this);
ui->label->raise();
2017-03-01 21:26:35 +11:00
ui->label->installEventFilter(this);
band = new QRubberBand(QRubberBand::Rectangle, ui->label);
bandOrigin = QPoint(0, 0);
band->setGeometry(QRect(bandOrigin, ui->label->size()));
2016-12-20 12:18:23 +11:00
QScreen* currentScreen = NULL;
for (QScreen* screen : QApplication::screens()) {
if (screen->geometry().contains(QCursor::pos())) {
currentScreen = screen;
}
}
if (currentScreen != NULL) {
screenshotPixmap = currentScreen->grabWindow(0); //, currentScreen->geometry().x(), currentScreen->geometry().y(), currentScreen->geometry().width(), currentScreen->geometry().height());
2016-12-20 12:18:23 +11:00
ui->label->setPixmap(screenshotPixmap);
2017-03-01 21:26:35 +11:00
savePixmap = screenshotPixmap;
2016-12-20 12:18:23 +11:00
2016-12-20 13:21:35 +11:00
QSoundEffect* takeScreenshot = new QSoundEffect();
takeScreenshot->setSource(QUrl("qrc:/sounds/screenshot.wav"));
takeScreenshot->play();
connect(takeScreenshot, SIGNAL(playingChanged()), takeScreenshot, SLOT(deleteLater()));
2016-12-20 12:18:23 +11:00
this->setGeometry(currentScreen->geometry());
this->setFixedSize(currentScreen->geometry().size());
} else {
}
2017-03-01 21:26:35 +11:00
Atom DesktopWindowTypeAtom;
DesktopWindowTypeAtom = XInternAtom(QX11Info::display(), "_NET_WM_WINDOW_TYPE_DOCK", False);
XChangeProperty(QX11Info::display(), this->winId(), XInternAtom(QX11Info::display(), "_NET_WM_WINDOW_TYPE", False),
XA_ATOM, 32, PropModeReplace, (unsigned char*) &DesktopWindowTypeAtom, 1); //Change Window Type
2016-12-20 12:18:23 +11:00
}
screenshotWindow::~screenshotWindow()
{
delete ui;
}
void screenshotWindow::paintEvent(QPaintEvent *event) {
QPainter painter(this);
painter.setBrush(QColor(0, 0, 0, 150));
painter.setPen(QColor(0, 0, 0, 0));
painter.drawRect(0, 0, this->width(), this->height());
}
void screenshotWindow::show() {
QDialog::show();
ui->label->setGeometry(0, 0, this->width(), this->height());
2017-03-01 21:26:35 +11:00
originalGeometry = ui->label->geometry();
2016-12-20 12:18:23 +11:00
QRectF endGeometry = ui->label->geometry();
//qreal scaleFactor = endGeometry.width() - (endGeometry.width() - 50);
//endGeometry.adjust(50, endGeometry.height() * scaleFactor, -50, -endGeometry.height() * scaleFactor);
//endGeometry.adjust(75, 75, -75, -75);
2017-04-13 12:17:43 +10:00
qreal newHeight = ((endGeometry.width() - 125 * getDPIScaling()) / endGeometry.width()) * endGeometry.height();
2016-12-20 12:18:23 +11:00
endGeometry.setHeight(newHeight);
2017-04-13 12:17:43 +10:00
endGeometry.setWidth(endGeometry.width() - 125 * getDPIScaling());
endGeometry.moveTo(100 * getDPIScaling() / 2, ((ui->label->height() - newHeight) / 2) - 35 * getDPIScaling());
2016-12-20 12:18:23 +11:00
QPropertyAnimation* anim = new QPropertyAnimation(ui->label, "geometry");
anim->setStartValue(ui->label->geometry());
anim->setEndValue(endGeometry);
anim->setDuration(500);
anim->setEasingCurve(QEasingCurve::OutCubic);
connect(anim, SIGNAL(finished()), anim, SLOT(deleteLater()));
anim->start();
}
void screenshotWindow::on_discardButton_clicked()
{
QRect newGeometry = ui->label->geometry();
newGeometry.moveTop(this->height() / 2);
QParallelAnimationGroup* animGroup = new QParallelAnimationGroup();
QPropertyAnimation* anim = new QPropertyAnimation(ui->label, "geometry");
anim->setStartValue(ui->label->geometry());
anim->setEndValue(newGeometry);
anim->setDuration(500);
anim->setEasingCurve(QEasingCurve::InQuint);
animGroup->addAnimation(anim);
QPropertyAnimation* closeAnim = new QPropertyAnimation(this, "windowOpacity");
closeAnim->setStartValue(1);
closeAnim->setEndValue(0);
closeAnim->setDuration(500);
closeAnim->setEasingCurve(QEasingCurve::InQuint);
animGroup->addAnimation(closeAnim);
connect(animGroup, SIGNAL(finished()), animGroup, SLOT(deleteLater()));
connect(animGroup, SIGNAL(finished()), this, SLOT(close()));
animGroup->start();
2016-12-20 12:18:23 +11:00
}
void screenshotWindow::on_copyButton_clicked()
{
2016-12-20 13:21:35 +11:00
QClipboard* clipboard = QApplication::clipboard();
2017-03-01 21:26:35 +11:00
clipboard->setPixmap(savePixmap);
2017-01-15 00:38:39 +11:00
QRect newGeometry = ui->label->geometry();
newGeometry.moveTop(-this->height() / 2);
QParallelAnimationGroup* animGroup = new QParallelAnimationGroup();
QPropertyAnimation* anim = new QPropertyAnimation(ui->label, "geometry");
anim->setStartValue(ui->label->geometry());
anim->setEndValue(newGeometry);
anim->setDuration(500);
anim->setEasingCurve(QEasingCurve::InQuint);
animGroup->addAnimation(anim);
QPropertyAnimation* closeAnim = new QPropertyAnimation(this, "windowOpacity");
closeAnim->setStartValue(1);
closeAnim->setEndValue(0);
closeAnim->setDuration(500);
closeAnim->setEasingCurve(QEasingCurve::InQuint);
animGroup->addAnimation(closeAnim);
connect(animGroup, SIGNAL(finished()), animGroup, SLOT(deleteLater()));
connect(animGroup, SIGNAL(finished()), this, SLOT(close()));
animGroup->start();
2016-12-20 12:18:23 +11:00
}
void screenshotWindow::on_saveButton_clicked()
{
2017-01-15 00:38:39 +11:00
QFile screenshotFile(QDir::homePath() + "/screenshot" + QDateTime::currentDateTime().toString("hh-mm-ss-yyyy-MM-dd") + ".png");
2017-03-01 21:26:35 +11:00
savePixmap.save(&screenshotFile, "PNG");
2017-01-15 00:38:39 +11:00
QRect newGeometry = ui->label->geometry();
newGeometry.moveTop(-this->height() / 2);
QParallelAnimationGroup* animGroup = new QParallelAnimationGroup();
QPropertyAnimation* anim = new QPropertyAnimation(ui->label, "geometry");
anim->setStartValue(ui->label->geometry());
anim->setEndValue(newGeometry);
anim->setDuration(500);
anim->setEasingCurve(QEasingCurve::InQuint);
animGroup->addAnimation(anim);
QPropertyAnimation* closeAnim = new QPropertyAnimation(this, "windowOpacity");
closeAnim->setStartValue(1);
closeAnim->setEndValue(0);
closeAnim->setDuration(500);
closeAnim->setEasingCurve(QEasingCurve::InQuint);
animGroup->addAnimation(closeAnim);
connect(animGroup, SIGNAL(finished()), animGroup, SLOT(deleteLater()));
connect(animGroup, SIGNAL(finished()), this, SLOT(close()));
animGroup->start();
2016-12-20 12:18:23 +11:00
}
2016-12-20 13:21:35 +11:00
2017-03-01 21:26:35 +11:00
bool screenshotWindow::eventFilter(QObject *object, QEvent *event) {
if (object == ui->label) {
if (event->type() == QEvent::MouseButtonPress) {
QMouseEvent* mouseEvent = (QMouseEvent*) event;
if (mouseEvent->button() == Qt::RightButton) {
bandOrigin = QPoint(0, 0);
band->setGeometry(QRect(bandOrigin, ui->label->size()));
band->hide();
} else {
bandOrigin = mouseEvent->pos();
band->setGeometry(QRect(bandOrigin, mouseEvent->pos()).normalized());
band->show();
}
} else if (event->type() == QEvent::MouseMove) {
QMouseEvent* mouseEvent = (QMouseEvent*) event;
if (mouseEvent->button() != Qt::RightButton) {
band->setGeometry(QRect(bandOrigin, mouseEvent->pos()).normalized());
}
} else if (event->type() == QEvent::MouseButtonRelease) {
QRect copyReigon;
2017-04-13 12:17:43 +10:00
qreal scaleFactor = ((originalGeometry.width() - 125 * getDPIScaling()) / originalGeometry.width());
2017-03-01 21:26:35 +11:00
copyReigon.setLeft(band->geometry().left() / scaleFactor);
copyReigon.setTop(band->geometry().top() / scaleFactor);
copyReigon.setRight(band->geometry().right() / scaleFactor);
copyReigon.setBottom(band->geometry().bottom() / scaleFactor);
savePixmap = screenshotPixmap.copy(copyReigon);
}
}
return false;
}
2016-12-20 13:21:35 +11:00
void screenshotWindow::close() {
QDialog::close();
this->deleteLater();
}