Fix crash and lag using the File Dialog

This commit is contained in:
lupoDharkael
2017-06-12 03:31:13 +02:00
parent e2eb5aee77
commit 8d27dc6660
3 changed files with 39 additions and 27 deletions

View File

@@ -394,11 +394,13 @@ void CaptureWidget::keyPressEvent(QKeyEvent *e) {
}
void CaptureWidget::saveScreenshot() {
hide();
if (m_selection.isNull()) {
m_screenshot->graphicalSave();
m_screenshot->graphicalSave(QRect(), this);
} else { // save full screen when no selection
m_screenshot->graphicalSave(getExtendedSelection());
m_screenshot->graphicalSave(getExtendedSelection(), this);
}
close();
}
void CaptureWidget::copyScreenshot() {

View File

@@ -35,7 +35,8 @@
// Screenshot is an extension of QPixmap which lets you manage specific tasks
Screenshot::Screenshot(const QPixmap &p) : m_baseScreenshot(p),
Screenshot::Screenshot(const QPixmap &p, QObject *parent) : QObject(parent),
m_baseScreenshot(p),
m_modifiedScreenshot(p) {}
Screenshot::~Screenshot() {
@@ -58,7 +59,7 @@ QPixmap Screenshot::getScreenshot() const {
// graphicalSave generates a graphical window to ask about the save path and
// saves the screenshot with all the modifications in such directory
QString Screenshot::graphicalSave(const QRect &selection) const {
QString Screenshot::graphicalSave(const QRect &selection, QWidget *parent) const {
const QString format = "png";
QSettings settings;
@@ -87,7 +88,7 @@ QString Screenshot::graphicalSave(const QRect &selection) const {
}
savePath += tempName + "." + format;
QFileDialog fileDialog(nullptr, QObject::tr("Save As"), savePath);
QFileDialog fileDialog(parent, QObject::tr("Save As"), savePath);
fileDialog.setAcceptMode(QFileDialog::AcceptSave);
fileDialog.setFileMode(QFileDialog::AnyFile);
fileDialog.setDirectory(savePath);
@@ -97,29 +98,36 @@ QString Screenshot::graphicalSave(const QRect &selection) const {
fileDialog.setMimeTypeFilters(mimeTypes);
fileDialog.selectMimeTypeFilter("image/" + format);
fileDialog.setDefaultSuffix(format);
fileDialog.setWindowIcon(QIcon(":img/flameshot.svg"));
if (fileDialog.exec() != QDialog::Accepted) { return ""; }
const QString fileName = fileDialog.selectedFiles().first();
fileDialog.setWindowIcon(QIcon(":img/flameshot.png"));
const QString pathNoFile = fileName.left(fileName.lastIndexOf("/"));
settings.setValue("savePath", pathNoFile);
bool saved = false;
QString fileName;
do {
if (fileDialog.exec() != QDialog::Accepted) { return fileName; }
fileName = fileDialog.selectedFiles().first();
QPixmap pixToSave;
if (selection.isEmpty()) {
pixToSave = m_modifiedScreenshot;
} else { // save full screen when no selection
pixToSave = m_modifiedScreenshot.copy(selection);
}
const QString pathNoFile = fileName.left(fileName.lastIndexOf("/"));
settings.setValue("savePath", pathNoFile);
// if (settings.value("mouseVisible").toBool()) {
// // TO DO
// }
QPixmap pixToSave;
if (selection.isEmpty()) {
pixToSave = m_modifiedScreenshot;
} else { // save full screen when no selection
pixToSave = m_modifiedScreenshot.copy(selection);
}
// if (settings.value("mouseVisible").toBool()) {
// // TO DO
// }
if (!pixToSave.save(fileName)) {
QMessageBox::warning(nullptr, QObject::tr("Save Error"),
QObject::tr("The image could not be saved to \"%1\".")
.arg(QDir::toNativeSeparators(fileName)));
}
saved = pixToSave.save(fileName);
if (!saved) {
QMessageBox saveErrBox(QMessageBox::Warning, QObject::tr("Save Error"),
QObject::tr("The image could not be saved to \"%1\".")
.arg(QDir::toNativeSeparators(fileName)));
saveErrBox.setWindowIcon(QIcon(":img/flameshot.png"));
saveErrBox.exec();
}
} while(!saved);
return fileName;
}

View File

@@ -21,21 +21,23 @@
#include <QPixmap>
#include <QRect>
#include <QPointer>
#include <QObject>
class QString;
class CaptureModification;
class QNetworkAccessManager;
class Screenshot {
class Screenshot : public QObject {
Q_OBJECT
public:
Screenshot(const QPixmap &);
Screenshot(const QPixmap &, QObject *parent = 0);
~Screenshot();
void setScreenshot(const QPixmap &);
QPixmap getBaseScreenshot() const;
QPixmap getScreenshot() const;
QString graphicalSave(const QRect &selection = QRect()) const;
QString graphicalSave(const QRect &selection = QRect(), QWidget *parent = 0) const;
void uploadToImgur(QNetworkAccessManager *,
const QRect &selection = QRect());
QPixmap paintModification(const CaptureModification &);