Skip to content
This repository has been archived by the owner on Feb 12, 2023. It is now read-only.

Commit

Permalink
fix(screen-grabber): fix crash
Browse files Browse the repository at this point in the history
  • Loading branch information
antis81 committed Jul 21, 2016
1 parent ff92a55 commit 780a017
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 17 deletions.
9 changes: 2 additions & 7 deletions src/widget/form/chatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@

ChatForm::ChatForm(Friend* chatFriend)
: f(chatFriend)
, screenshotGrabber(nullptr)
, isTyping(false)
{
Core* core = Core::getInstance();
Expand Down Expand Up @@ -797,8 +796,8 @@ void ChatForm::onScreenshotClicked()

void ChatForm::doScreenshot()
{
if (!screenshotGrabber)
screenshotGrabber = new ScreenshotGrabber(this);
// note: grabber is self-managed and will destroy itself when done
ScreenshotGrabber* screenshotGrabber = new ScreenshotGrabber;

connect(screenshotGrabber, &ScreenshotGrabber::screenshotTaken,
this, &ChatForm::onScreenshotTaken);
Expand Down Expand Up @@ -827,8 +826,6 @@ void ChatForm::onScreenshotTaken(const QPixmap &pixmap) {
tr("Failed to open temporary file", "Temporary file for screenshot"),
tr("qTox wasn't able to save the screenshot"));

delete screenshotGrabber;
screenshotGrabber = nullptr;
return;
}

Expand All @@ -839,8 +836,6 @@ void ChatForm::onScreenshotTaken(const QPixmap &pixmap) {
QFileInfo fi(file);

emit sendFile(f->getFriendID(), fi.fileName(), fi.filePath(), filesize);
delete screenshotGrabber;
screenshotGrabber = nullptr;
}

void ChatForm::onLoadHistory()
Expand Down
1 change: 0 additions & 1 deletion src/widget/form/chatform.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ private slots:
QAction* loadHistoryAction;
QAction* copyStatusAction;

ScreenshotGrabber* screenshotGrabber;
QHash<uint, FileTransferInstance*> ftransWidgets;
CallConfirmWidget *callConfirm;
bool isTyping;
Expand Down
4 changes: 2 additions & 2 deletions src/widget/form/settings/avform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ void AVForm::on_videoModescomboBox_currentIndexChanged(int index)
return;
}

ScreenshotGrabber* screenshotGrabber = new ScreenshotGrabber(this);
// note: grabber is self-managed and will destroy itself when done
ScreenshotGrabber* screenshotGrabber = new ScreenshotGrabber;

auto onGrabbed = [screenshotGrabber, devName, this] (QRect region)
{
Expand All @@ -170,7 +171,6 @@ void AVForm::on_videoModescomboBox_currentIndexChanged(int index)
Settings::getInstance().setScreenGrabbed(true);

open(devName, mode);
delete screenshotGrabber;
};

connect(screenshotGrabber, &ScreenshotGrabber::regionChosen, this, onGrabbed, Qt::QueuedConnection);
Expand Down
12 changes: 6 additions & 6 deletions src/widget/tool/screenshotgrabber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,13 @@
#include "toolboxgraphicsitem.h"
#include "src/widget/widget.h"

ScreenshotGrabber::ScreenshotGrabber(QObject* parent)
: QObject(parent)
ScreenshotGrabber::ScreenshotGrabber()
: QObject()
, mKeysBlocked(false)
, scene(0)
, mQToxVisible(true)
{
window = new QGraphicsView (scene); // Top-level widget
window->setAttribute(Qt::WA_DeleteOnClose);
window->setWindowFlags(Qt::FramelessWindowHint | Qt::BypassWindowManagerHint);
window->setContentsMargins(0, 0, 0, 0);
window->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
Expand Down Expand Up @@ -71,6 +70,7 @@ void ScreenshotGrabber::reInit()
ScreenshotGrabber::~ScreenshotGrabber()
{
delete scene;
delete window;
}

bool ScreenshotGrabber::eventFilter(QObject* object, QEvent* event)
Expand Down Expand Up @@ -134,9 +134,10 @@ void ScreenshotGrabber::acceptRegion()
emit regionChosen(rect);
qDebug() << "Screenshot accepted, chosen region" << rect;
QPixmap pixmap = this->screenGrab.copy(rect);
this->window->close();
restoreHiddenWindows();
emit screenshotTaken(pixmap);

deleteLater();
}

void ScreenshotGrabber::setupScene()
Expand Down Expand Up @@ -210,9 +211,8 @@ void ScreenshotGrabber::adjustTooltipPosition()

void ScreenshotGrabber::reject()
{
qDebug() << "Rejected screenshot";
this->window->close();
restoreHiddenWindows();
deleteLater();
}

QPixmap ScreenshotGrabber::grabScreen()
Expand Down
2 changes: 1 addition & 1 deletion src/widget/tool/screenshotgrabber.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class ScreenshotGrabber : public QObject
Q_OBJECT
public:

explicit ScreenshotGrabber(QObject* parent);
ScreenshotGrabber();
~ScreenshotGrabber() override;

bool eventFilter(QObject* object, QEvent* event) override;
Expand Down

0 comments on commit 780a017

Please sign in to comment.